samsung.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver core for Samsung SoC onboard UARTs.
  4. *
  5. * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
  6. * http://armlinux.simtec.co.uk/
  7. */
  8. /* Hote on 2410 error handling
  9. *
  10. * The s3c2410 manual has a love/hate affair with the contents of the
  11. * UERSTAT register in the UART blocks, and keeps marking some of the
  12. * error bits as reserved. Having checked with the s3c2410x01,
  13. * it copes with BREAKs properly, so I am happy to ignore the RESERVED
  14. * feature from the latter versions of the manual.
  15. *
  16. * If it becomes aparrent that latter versions of the 2410 remove these
  17. * bits, then action will have to be taken to differentiate the versions
  18. * and change the policy on BREAK
  19. *
  20. * BJD, 04-Nov-2004
  21. */
  22. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  23. #define SUPPORT_SYSRQ
  24. #endif
  25. #include <linux/dmaengine.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/ioport.h>
  30. #include <linux/io.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/init.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/console.h>
  35. #include <linux/tty.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/serial_core.h>
  38. #include <linux/serial.h>
  39. #include <linux/serial_s3c.h>
  40. #include <linux/delay.h>
  41. #include <linux/clk.h>
  42. #include <linux/cpufreq.h>
  43. #include <linux/of.h>
  44. #include <asm/irq.h>
  45. #include "samsung.h"
  46. #if defined(CONFIG_SERIAL_SAMSUNG_DEBUG) && \
  47. !defined(MODULE)
  48. extern void printascii(const char *);
  49. __printf(1, 2)
  50. static void dbg(const char *fmt, ...)
  51. {
  52. va_list va;
  53. char buff[256];
  54. va_start(va, fmt);
  55. vscnprintf(buff, sizeof(buff), fmt, va);
  56. va_end(va);
  57. printascii(buff);
  58. }
  59. #else
  60. #define dbg(fmt, ...) do { if (0) no_printk(fmt, ##__VA_ARGS__); } while (0)
  61. #endif
  62. /* UART name and device definitions */
  63. #define S3C24XX_SERIAL_NAME "ttySAC"
  64. #define S3C24XX_SERIAL_MAJOR 204
  65. #define S3C24XX_SERIAL_MINOR 64
  66. #define S3C24XX_TX_PIO 1
  67. #define S3C24XX_TX_DMA 2
  68. #define S3C24XX_RX_PIO 1
  69. #define S3C24XX_RX_DMA 2
  70. /* macros to change one thing to another */
  71. #define tx_enabled(port) ((port)->unused[0])
  72. #define rx_enabled(port) ((port)->unused[1])
  73. /* flag to ignore all characters coming in */
  74. #define RXSTAT_DUMMY_READ (0x10000000)
  75. static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
  76. {
  77. return container_of(port, struct s3c24xx_uart_port, port);
  78. }
  79. /* translate a port to the device name */
  80. static inline const char *s3c24xx_serial_portname(struct uart_port *port)
  81. {
  82. return to_platform_device(port->dev)->name;
  83. }
  84. static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
  85. {
  86. return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
  87. }
  88. /*
  89. * s3c64xx and later SoC's include the interrupt mask and status registers in
  90. * the controller itself, unlike the s3c24xx SoC's which have these registers
  91. * in the interrupt controller. Check if the port type is s3c64xx or higher.
  92. */
  93. static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
  94. {
  95. return to_ourport(port)->info->type == PORT_S3C6400;
  96. }
  97. static void s3c24xx_serial_rx_enable(struct uart_port *port)
  98. {
  99. unsigned long flags;
  100. unsigned int ucon, ufcon;
  101. int count = 10000;
  102. spin_lock_irqsave(&port->lock, flags);
  103. while (--count && !s3c24xx_serial_txempty_nofifo(port))
  104. udelay(100);
  105. ufcon = rd_regl(port, S3C2410_UFCON);
  106. ufcon |= S3C2410_UFCON_RESETRX;
  107. wr_regl(port, S3C2410_UFCON, ufcon);
  108. ucon = rd_regl(port, S3C2410_UCON);
  109. ucon |= S3C2410_UCON_RXIRQMODE;
  110. wr_regl(port, S3C2410_UCON, ucon);
  111. rx_enabled(port) = 1;
  112. spin_unlock_irqrestore(&port->lock, flags);
  113. }
  114. static void s3c24xx_serial_rx_disable(struct uart_port *port)
  115. {
  116. unsigned long flags;
  117. unsigned int ucon;
  118. spin_lock_irqsave(&port->lock, flags);
  119. ucon = rd_regl(port, S3C2410_UCON);
  120. ucon &= ~S3C2410_UCON_RXIRQMODE;
  121. wr_regl(port, S3C2410_UCON, ucon);
  122. rx_enabled(port) = 0;
  123. spin_unlock_irqrestore(&port->lock, flags);
  124. }
  125. static void s3c24xx_serial_stop_tx(struct uart_port *port)
  126. {
  127. struct s3c24xx_uart_port *ourport = to_ourport(port);
  128. struct s3c24xx_uart_dma *dma = ourport->dma;
  129. struct circ_buf *xmit = &port->state->xmit;
  130. struct dma_tx_state state;
  131. int count;
  132. if (!tx_enabled(port))
  133. return;
  134. if (s3c24xx_serial_has_interrupt_mask(port))
  135. s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
  136. else
  137. disable_irq_nosync(ourport->tx_irq);
  138. if (dma && dma->tx_chan && ourport->tx_in_progress == S3C24XX_TX_DMA) {
  139. dmaengine_pause(dma->tx_chan);
  140. dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
  141. dmaengine_terminate_all(dma->tx_chan);
  142. dma_sync_single_for_cpu(ourport->port.dev,
  143. dma->tx_transfer_addr, dma->tx_size, DMA_TO_DEVICE);
  144. async_tx_ack(dma->tx_desc);
  145. count = dma->tx_bytes_requested - state.residue;
  146. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  147. port->icount.tx += count;
  148. }
  149. tx_enabled(port) = 0;
  150. ourport->tx_in_progress = 0;
  151. if (port->flags & UPF_CONS_FLOW)
  152. s3c24xx_serial_rx_enable(port);
  153. ourport->tx_mode = 0;
  154. }
  155. static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport);
  156. static void s3c24xx_serial_tx_dma_complete(void *args)
  157. {
  158. struct s3c24xx_uart_port *ourport = args;
  159. struct uart_port *port = &ourport->port;
  160. struct circ_buf *xmit = &port->state->xmit;
  161. struct s3c24xx_uart_dma *dma = ourport->dma;
  162. struct dma_tx_state state;
  163. unsigned long flags;
  164. int count;
  165. dmaengine_tx_status(dma->tx_chan, dma->tx_cookie, &state);
  166. count = dma->tx_bytes_requested - state.residue;
  167. async_tx_ack(dma->tx_desc);
  168. dma_sync_single_for_cpu(ourport->port.dev, dma->tx_transfer_addr,
  169. dma->tx_size, DMA_TO_DEVICE);
  170. spin_lock_irqsave(&port->lock, flags);
  171. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  172. port->icount.tx += count;
  173. ourport->tx_in_progress = 0;
  174. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  175. uart_write_wakeup(port);
  176. s3c24xx_serial_start_next_tx(ourport);
  177. spin_unlock_irqrestore(&port->lock, flags);
  178. }
  179. static void enable_tx_dma(struct s3c24xx_uart_port *ourport)
  180. {
  181. struct uart_port *port = &ourport->port;
  182. u32 ucon;
  183. /* Mask Tx interrupt */
  184. if (s3c24xx_serial_has_interrupt_mask(port))
  185. s3c24xx_set_bit(port, S3C64XX_UINTM_TXD, S3C64XX_UINTM);
  186. else
  187. disable_irq_nosync(ourport->tx_irq);
  188. /* Enable tx dma mode */
  189. ucon = rd_regl(port, S3C2410_UCON);
  190. ucon &= ~(S3C64XX_UCON_TXBURST_MASK | S3C64XX_UCON_TXMODE_MASK);
  191. ucon |= (dma_get_cache_alignment() >= 16) ?
  192. S3C64XX_UCON_TXBURST_16 : S3C64XX_UCON_TXBURST_1;
  193. ucon |= S3C64XX_UCON_TXMODE_DMA;
  194. wr_regl(port, S3C2410_UCON, ucon);
  195. ourport->tx_mode = S3C24XX_TX_DMA;
  196. }
  197. static void enable_tx_pio(struct s3c24xx_uart_port *ourport)
  198. {
  199. struct uart_port *port = &ourport->port;
  200. u32 ucon, ufcon;
  201. /* Set ufcon txtrig */
  202. ourport->tx_in_progress = S3C24XX_TX_PIO;
  203. ufcon = rd_regl(port, S3C2410_UFCON);
  204. wr_regl(port, S3C2410_UFCON, ufcon);
  205. /* Enable tx pio mode */
  206. ucon = rd_regl(port, S3C2410_UCON);
  207. ucon &= ~(S3C64XX_UCON_TXMODE_MASK);
  208. ucon |= S3C64XX_UCON_TXMODE_CPU;
  209. wr_regl(port, S3C2410_UCON, ucon);
  210. /* Unmask Tx interrupt */
  211. if (s3c24xx_serial_has_interrupt_mask(port))
  212. s3c24xx_clear_bit(port, S3C64XX_UINTM_TXD,
  213. S3C64XX_UINTM);
  214. else
  215. enable_irq(ourport->tx_irq);
  216. ourport->tx_mode = S3C24XX_TX_PIO;
  217. }
  218. static void s3c24xx_serial_start_tx_pio(struct s3c24xx_uart_port *ourport)
  219. {
  220. if (ourport->tx_mode != S3C24XX_TX_PIO)
  221. enable_tx_pio(ourport);
  222. }
  223. static int s3c24xx_serial_start_tx_dma(struct s3c24xx_uart_port *ourport,
  224. unsigned int count)
  225. {
  226. struct uart_port *port = &ourport->port;
  227. struct circ_buf *xmit = &port->state->xmit;
  228. struct s3c24xx_uart_dma *dma = ourport->dma;
  229. if (ourport->tx_mode != S3C24XX_TX_DMA)
  230. enable_tx_dma(ourport);
  231. dma->tx_size = count & ~(dma_get_cache_alignment() - 1);
  232. dma->tx_transfer_addr = dma->tx_addr + xmit->tail;
  233. dma_sync_single_for_device(ourport->port.dev, dma->tx_transfer_addr,
  234. dma->tx_size, DMA_TO_DEVICE);
  235. dma->tx_desc = dmaengine_prep_slave_single(dma->tx_chan,
  236. dma->tx_transfer_addr, dma->tx_size,
  237. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  238. if (!dma->tx_desc) {
  239. dev_err(ourport->port.dev, "Unable to get desc for Tx\n");
  240. return -EIO;
  241. }
  242. dma->tx_desc->callback = s3c24xx_serial_tx_dma_complete;
  243. dma->tx_desc->callback_param = ourport;
  244. dma->tx_bytes_requested = dma->tx_size;
  245. ourport->tx_in_progress = S3C24XX_TX_DMA;
  246. dma->tx_cookie = dmaengine_submit(dma->tx_desc);
  247. dma_async_issue_pending(dma->tx_chan);
  248. return 0;
  249. }
  250. static void s3c24xx_serial_start_next_tx(struct s3c24xx_uart_port *ourport)
  251. {
  252. struct uart_port *port = &ourport->port;
  253. struct circ_buf *xmit = &port->state->xmit;
  254. unsigned long count;
  255. /* Get data size up to the end of buffer */
  256. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  257. if (!count) {
  258. s3c24xx_serial_stop_tx(port);
  259. return;
  260. }
  261. if (!ourport->dma || !ourport->dma->tx_chan ||
  262. count < ourport->min_dma_size ||
  263. xmit->tail & (dma_get_cache_alignment() - 1))
  264. s3c24xx_serial_start_tx_pio(ourport);
  265. else
  266. s3c24xx_serial_start_tx_dma(ourport, count);
  267. }
  268. static void s3c24xx_serial_start_tx(struct uart_port *port)
  269. {
  270. struct s3c24xx_uart_port *ourport = to_ourport(port);
  271. struct circ_buf *xmit = &port->state->xmit;
  272. if (!tx_enabled(port)) {
  273. if (port->flags & UPF_CONS_FLOW)
  274. s3c24xx_serial_rx_disable(port);
  275. tx_enabled(port) = 1;
  276. if (!ourport->dma || !ourport->dma->tx_chan)
  277. s3c24xx_serial_start_tx_pio(ourport);
  278. }
  279. if (ourport->dma && ourport->dma->tx_chan) {
  280. if (!uart_circ_empty(xmit) && !ourport->tx_in_progress)
  281. s3c24xx_serial_start_next_tx(ourport);
  282. }
  283. }
  284. static void s3c24xx_uart_copy_rx_to_tty(struct s3c24xx_uart_port *ourport,
  285. struct tty_port *tty, int count)
  286. {
  287. struct s3c24xx_uart_dma *dma = ourport->dma;
  288. int copied;
  289. if (!count)
  290. return;
  291. dma_sync_single_for_cpu(ourport->port.dev, dma->rx_addr,
  292. dma->rx_size, DMA_FROM_DEVICE);
  293. ourport->port.icount.rx += count;
  294. if (!tty) {
  295. dev_err(ourport->port.dev, "No tty port\n");
  296. return;
  297. }
  298. copied = tty_insert_flip_string(tty,
  299. ((unsigned char *)(ourport->dma->rx_buf)), count);
  300. if (copied != count) {
  301. WARN_ON(1);
  302. dev_err(ourport->port.dev, "RxData copy to tty layer failed\n");
  303. }
  304. }
  305. static void s3c24xx_serial_stop_rx(struct uart_port *port)
  306. {
  307. struct s3c24xx_uart_port *ourport = to_ourport(port);
  308. struct s3c24xx_uart_dma *dma = ourport->dma;
  309. struct tty_port *t = &port->state->port;
  310. struct dma_tx_state state;
  311. enum dma_status dma_status;
  312. unsigned int received;
  313. if (rx_enabled(port)) {
  314. dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
  315. if (s3c24xx_serial_has_interrupt_mask(port))
  316. s3c24xx_set_bit(port, S3C64XX_UINTM_RXD,
  317. S3C64XX_UINTM);
  318. else
  319. disable_irq_nosync(ourport->rx_irq);
  320. rx_enabled(port) = 0;
  321. }
  322. if (dma && dma->rx_chan) {
  323. dmaengine_pause(dma->tx_chan);
  324. dma_status = dmaengine_tx_status(dma->rx_chan,
  325. dma->rx_cookie, &state);
  326. if (dma_status == DMA_IN_PROGRESS ||
  327. dma_status == DMA_PAUSED) {
  328. received = dma->rx_bytes_requested - state.residue;
  329. dmaengine_terminate_all(dma->rx_chan);
  330. s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
  331. }
  332. }
  333. }
  334. static inline struct s3c24xx_uart_info
  335. *s3c24xx_port_to_info(struct uart_port *port)
  336. {
  337. return to_ourport(port)->info;
  338. }
  339. static inline struct s3c2410_uartcfg
  340. *s3c24xx_port_to_cfg(struct uart_port *port)
  341. {
  342. struct s3c24xx_uart_port *ourport;
  343. if (port->dev == NULL)
  344. return NULL;
  345. ourport = container_of(port, struct s3c24xx_uart_port, port);
  346. return ourport->cfg;
  347. }
  348. static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
  349. unsigned long ufstat)
  350. {
  351. struct s3c24xx_uart_info *info = ourport->info;
  352. if (ufstat & info->rx_fifofull)
  353. return ourport->port.fifosize;
  354. return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
  355. }
  356. static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport);
  357. static void s3c24xx_serial_rx_dma_complete(void *args)
  358. {
  359. struct s3c24xx_uart_port *ourport = args;
  360. struct uart_port *port = &ourport->port;
  361. struct s3c24xx_uart_dma *dma = ourport->dma;
  362. struct tty_port *t = &port->state->port;
  363. struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
  364. struct dma_tx_state state;
  365. unsigned long flags;
  366. int received;
  367. dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
  368. received = dma->rx_bytes_requested - state.residue;
  369. async_tx_ack(dma->rx_desc);
  370. spin_lock_irqsave(&port->lock, flags);
  371. if (received)
  372. s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
  373. if (tty) {
  374. tty_flip_buffer_push(t);
  375. tty_kref_put(tty);
  376. }
  377. s3c64xx_start_rx_dma(ourport);
  378. spin_unlock_irqrestore(&port->lock, flags);
  379. }
  380. static void s3c64xx_start_rx_dma(struct s3c24xx_uart_port *ourport)
  381. {
  382. struct s3c24xx_uart_dma *dma = ourport->dma;
  383. dma_sync_single_for_device(ourport->port.dev, dma->rx_addr,
  384. dma->rx_size, DMA_FROM_DEVICE);
  385. dma->rx_desc = dmaengine_prep_slave_single(dma->rx_chan,
  386. dma->rx_addr, dma->rx_size, DMA_DEV_TO_MEM,
  387. DMA_PREP_INTERRUPT);
  388. if (!dma->rx_desc) {
  389. dev_err(ourport->port.dev, "Unable to get desc for Rx\n");
  390. return;
  391. }
  392. dma->rx_desc->callback = s3c24xx_serial_rx_dma_complete;
  393. dma->rx_desc->callback_param = ourport;
  394. dma->rx_bytes_requested = dma->rx_size;
  395. dma->rx_cookie = dmaengine_submit(dma->rx_desc);
  396. dma_async_issue_pending(dma->rx_chan);
  397. }
  398. /* ? - where has parity gone?? */
  399. #define S3C2410_UERSTAT_PARITY (0x1000)
  400. static void enable_rx_dma(struct s3c24xx_uart_port *ourport)
  401. {
  402. struct uart_port *port = &ourport->port;
  403. unsigned int ucon;
  404. /* set Rx mode to DMA mode */
  405. ucon = rd_regl(port, S3C2410_UCON);
  406. ucon &= ~(S3C64XX_UCON_RXBURST_MASK |
  407. S3C64XX_UCON_TIMEOUT_MASK |
  408. S3C64XX_UCON_EMPTYINT_EN |
  409. S3C64XX_UCON_DMASUS_EN |
  410. S3C64XX_UCON_TIMEOUT_EN |
  411. S3C64XX_UCON_RXMODE_MASK);
  412. ucon |= S3C64XX_UCON_RXBURST_16 |
  413. 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
  414. S3C64XX_UCON_EMPTYINT_EN |
  415. S3C64XX_UCON_TIMEOUT_EN |
  416. S3C64XX_UCON_RXMODE_DMA;
  417. wr_regl(port, S3C2410_UCON, ucon);
  418. ourport->rx_mode = S3C24XX_RX_DMA;
  419. }
  420. static void enable_rx_pio(struct s3c24xx_uart_port *ourport)
  421. {
  422. struct uart_port *port = &ourport->port;
  423. unsigned int ucon;
  424. /* set Rx mode to DMA mode */
  425. ucon = rd_regl(port, S3C2410_UCON);
  426. ucon &= ~(S3C64XX_UCON_TIMEOUT_MASK |
  427. S3C64XX_UCON_EMPTYINT_EN |
  428. S3C64XX_UCON_DMASUS_EN |
  429. S3C64XX_UCON_TIMEOUT_EN |
  430. S3C64XX_UCON_RXMODE_MASK);
  431. ucon |= 0xf << S3C64XX_UCON_TIMEOUT_SHIFT |
  432. S3C64XX_UCON_TIMEOUT_EN |
  433. S3C64XX_UCON_RXMODE_CPU;
  434. wr_regl(port, S3C2410_UCON, ucon);
  435. ourport->rx_mode = S3C24XX_RX_PIO;
  436. }
  437. static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport);
  438. static irqreturn_t s3c24xx_serial_rx_chars_dma(void *dev_id)
  439. {
  440. unsigned int utrstat, ufstat, received;
  441. struct s3c24xx_uart_port *ourport = dev_id;
  442. struct uart_port *port = &ourport->port;
  443. struct s3c24xx_uart_dma *dma = ourport->dma;
  444. struct tty_struct *tty = tty_port_tty_get(&ourport->port.state->port);
  445. struct tty_port *t = &port->state->port;
  446. unsigned long flags;
  447. struct dma_tx_state state;
  448. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  449. ufstat = rd_regl(port, S3C2410_UFSTAT);
  450. spin_lock_irqsave(&port->lock, flags);
  451. if (!(utrstat & S3C2410_UTRSTAT_TIMEOUT)) {
  452. s3c64xx_start_rx_dma(ourport);
  453. if (ourport->rx_mode == S3C24XX_RX_PIO)
  454. enable_rx_dma(ourport);
  455. goto finish;
  456. }
  457. if (ourport->rx_mode == S3C24XX_RX_DMA) {
  458. dmaengine_pause(dma->rx_chan);
  459. dmaengine_tx_status(dma->rx_chan, dma->rx_cookie, &state);
  460. dmaengine_terminate_all(dma->rx_chan);
  461. received = dma->rx_bytes_requested - state.residue;
  462. s3c24xx_uart_copy_rx_to_tty(ourport, t, received);
  463. enable_rx_pio(ourport);
  464. }
  465. s3c24xx_serial_rx_drain_fifo(ourport);
  466. if (tty) {
  467. tty_flip_buffer_push(t);
  468. tty_kref_put(tty);
  469. }
  470. wr_regl(port, S3C2410_UTRSTAT, S3C2410_UTRSTAT_TIMEOUT);
  471. finish:
  472. spin_unlock_irqrestore(&port->lock, flags);
  473. return IRQ_HANDLED;
  474. }
  475. static void s3c24xx_serial_rx_drain_fifo(struct s3c24xx_uart_port *ourport)
  476. {
  477. struct uart_port *port = &ourport->port;
  478. unsigned int ufcon, ch, flag, ufstat, uerstat;
  479. unsigned int fifocnt = 0;
  480. int max_count = port->fifosize;
  481. while (max_count-- > 0) {
  482. /*
  483. * Receive all characters known to be in FIFO
  484. * before reading FIFO level again
  485. */
  486. if (fifocnt == 0) {
  487. ufstat = rd_regl(port, S3C2410_UFSTAT);
  488. fifocnt = s3c24xx_serial_rx_fifocnt(ourport, ufstat);
  489. if (fifocnt == 0)
  490. break;
  491. }
  492. fifocnt--;
  493. uerstat = rd_regl(port, S3C2410_UERSTAT);
  494. ch = rd_regb(port, S3C2410_URXH);
  495. if (port->flags & UPF_CONS_FLOW) {
  496. int txe = s3c24xx_serial_txempty_nofifo(port);
  497. if (rx_enabled(port)) {
  498. if (!txe) {
  499. rx_enabled(port) = 0;
  500. continue;
  501. }
  502. } else {
  503. if (txe) {
  504. ufcon = rd_regl(port, S3C2410_UFCON);
  505. ufcon |= S3C2410_UFCON_RESETRX;
  506. wr_regl(port, S3C2410_UFCON, ufcon);
  507. rx_enabled(port) = 1;
  508. return;
  509. }
  510. continue;
  511. }
  512. }
  513. /* insert the character into the buffer */
  514. flag = TTY_NORMAL;
  515. port->icount.rx++;
  516. if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
  517. dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
  518. ch, uerstat);
  519. /* check for break */
  520. if (uerstat & S3C2410_UERSTAT_BREAK) {
  521. dbg("break!\n");
  522. port->icount.brk++;
  523. if (uart_handle_break(port))
  524. continue; /* Ignore character */
  525. }
  526. if (uerstat & S3C2410_UERSTAT_FRAME)
  527. port->icount.frame++;
  528. if (uerstat & S3C2410_UERSTAT_OVERRUN)
  529. port->icount.overrun++;
  530. uerstat &= port->read_status_mask;
  531. if (uerstat & S3C2410_UERSTAT_BREAK)
  532. flag = TTY_BREAK;
  533. else if (uerstat & S3C2410_UERSTAT_PARITY)
  534. flag = TTY_PARITY;
  535. else if (uerstat & (S3C2410_UERSTAT_FRAME |
  536. S3C2410_UERSTAT_OVERRUN))
  537. flag = TTY_FRAME;
  538. }
  539. if (uart_handle_sysrq_char(port, ch))
  540. continue; /* Ignore character */
  541. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
  542. ch, flag);
  543. }
  544. tty_flip_buffer_push(&port->state->port);
  545. }
  546. static irqreturn_t s3c24xx_serial_rx_chars_pio(void *dev_id)
  547. {
  548. struct s3c24xx_uart_port *ourport = dev_id;
  549. struct uart_port *port = &ourport->port;
  550. unsigned long flags;
  551. spin_lock_irqsave(&port->lock, flags);
  552. s3c24xx_serial_rx_drain_fifo(ourport);
  553. spin_unlock_irqrestore(&port->lock, flags);
  554. return IRQ_HANDLED;
  555. }
  556. static irqreturn_t s3c24xx_serial_rx_chars(int irq, void *dev_id)
  557. {
  558. struct s3c24xx_uart_port *ourport = dev_id;
  559. if (ourport->dma && ourport->dma->rx_chan)
  560. return s3c24xx_serial_rx_chars_dma(dev_id);
  561. return s3c24xx_serial_rx_chars_pio(dev_id);
  562. }
  563. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
  564. {
  565. struct s3c24xx_uart_port *ourport = id;
  566. struct uart_port *port = &ourport->port;
  567. struct circ_buf *xmit = &port->state->xmit;
  568. unsigned long flags;
  569. int count, dma_count = 0;
  570. spin_lock_irqsave(&port->lock, flags);
  571. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  572. if (ourport->dma && ourport->dma->tx_chan &&
  573. count >= ourport->min_dma_size) {
  574. int align = dma_get_cache_alignment() -
  575. (xmit->tail & (dma_get_cache_alignment() - 1));
  576. if (count-align >= ourport->min_dma_size) {
  577. dma_count = count-align;
  578. count = align;
  579. }
  580. }
  581. if (port->x_char) {
  582. wr_regb(port, S3C2410_UTXH, port->x_char);
  583. port->icount.tx++;
  584. port->x_char = 0;
  585. goto out;
  586. }
  587. /* if there isn't anything more to transmit, or the uart is now
  588. * stopped, disable the uart and exit
  589. */
  590. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  591. s3c24xx_serial_stop_tx(port);
  592. goto out;
  593. }
  594. /* try and drain the buffer... */
  595. if (count > port->fifosize) {
  596. count = port->fifosize;
  597. dma_count = 0;
  598. }
  599. while (!uart_circ_empty(xmit) && count > 0) {
  600. if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
  601. break;
  602. wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
  603. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  604. port->icount.tx++;
  605. count--;
  606. }
  607. if (!count && dma_count) {
  608. s3c24xx_serial_start_tx_dma(ourport, dma_count);
  609. goto out;
  610. }
  611. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
  612. spin_unlock(&port->lock);
  613. uart_write_wakeup(port);
  614. spin_lock(&port->lock);
  615. }
  616. if (uart_circ_empty(xmit))
  617. s3c24xx_serial_stop_tx(port);
  618. out:
  619. spin_unlock_irqrestore(&port->lock, flags);
  620. return IRQ_HANDLED;
  621. }
  622. /* interrupt handler for s3c64xx and later SoC's.*/
  623. static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
  624. {
  625. struct s3c24xx_uart_port *ourport = id;
  626. struct uart_port *port = &ourport->port;
  627. unsigned int pend = rd_regl(port, S3C64XX_UINTP);
  628. irqreturn_t ret = IRQ_HANDLED;
  629. if (pend & S3C64XX_UINTM_RXD_MSK) {
  630. ret = s3c24xx_serial_rx_chars(irq, id);
  631. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
  632. }
  633. if (pend & S3C64XX_UINTM_TXD_MSK) {
  634. ret = s3c24xx_serial_tx_chars(irq, id);
  635. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
  636. }
  637. return ret;
  638. }
  639. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  640. {
  641. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  642. unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
  643. unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
  644. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  645. if ((ufstat & info->tx_fifomask) != 0 ||
  646. (ufstat & info->tx_fifofull))
  647. return 0;
  648. return 1;
  649. }
  650. return s3c24xx_serial_txempty_nofifo(port);
  651. }
  652. /* no modem control lines */
  653. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  654. {
  655. unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
  656. if (umstat & S3C2410_UMSTAT_CTS)
  657. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  658. else
  659. return TIOCM_CAR | TIOCM_DSR;
  660. }
  661. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  662. {
  663. unsigned int umcon = rd_regl(port, S3C2410_UMCON);
  664. if (mctrl & TIOCM_RTS)
  665. umcon |= S3C2410_UMCOM_RTS_LOW;
  666. else
  667. umcon &= ~S3C2410_UMCOM_RTS_LOW;
  668. wr_regl(port, S3C2410_UMCON, umcon);
  669. }
  670. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  671. {
  672. unsigned long flags;
  673. unsigned int ucon;
  674. spin_lock_irqsave(&port->lock, flags);
  675. ucon = rd_regl(port, S3C2410_UCON);
  676. if (break_state)
  677. ucon |= S3C2410_UCON_SBREAK;
  678. else
  679. ucon &= ~S3C2410_UCON_SBREAK;
  680. wr_regl(port, S3C2410_UCON, ucon);
  681. spin_unlock_irqrestore(&port->lock, flags);
  682. }
  683. static int s3c24xx_serial_request_dma(struct s3c24xx_uart_port *p)
  684. {
  685. struct s3c24xx_uart_dma *dma = p->dma;
  686. int ret;
  687. /* Default slave configuration parameters */
  688. dma->rx_conf.direction = DMA_DEV_TO_MEM;
  689. dma->rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  690. dma->rx_conf.src_addr = p->port.mapbase + S3C2410_URXH;
  691. dma->rx_conf.src_maxburst = 16;
  692. dma->tx_conf.direction = DMA_MEM_TO_DEV;
  693. dma->tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  694. dma->tx_conf.dst_addr = p->port.mapbase + S3C2410_UTXH;
  695. if (dma_get_cache_alignment() >= 16)
  696. dma->tx_conf.dst_maxburst = 16;
  697. else
  698. dma->tx_conf.dst_maxburst = 1;
  699. dma->rx_chan = dma_request_chan(p->port.dev, "rx");
  700. if (IS_ERR(dma->rx_chan))
  701. return PTR_ERR(dma->rx_chan);
  702. dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
  703. dma->tx_chan = dma_request_chan(p->port.dev, "tx");
  704. if (IS_ERR(dma->tx_chan)) {
  705. ret = PTR_ERR(dma->tx_chan);
  706. goto err_release_rx;
  707. }
  708. dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
  709. /* RX buffer */
  710. dma->rx_size = PAGE_SIZE;
  711. dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
  712. if (!dma->rx_buf) {
  713. ret = -ENOMEM;
  714. goto err_release_tx;
  715. }
  716. dma->rx_addr = dma_map_single(p->port.dev, dma->rx_buf,
  717. dma->rx_size, DMA_FROM_DEVICE);
  718. if (dma_mapping_error(p->port.dev, dma->rx_addr)) {
  719. ret = -EIO;
  720. goto err_free_rx;
  721. }
  722. /* TX buffer */
  723. dma->tx_addr = dma_map_single(p->port.dev, p->port.state->xmit.buf,
  724. UART_XMIT_SIZE, DMA_TO_DEVICE);
  725. if (dma_mapping_error(p->port.dev, dma->tx_addr)) {
  726. ret = -EIO;
  727. goto err_unmap_rx;
  728. }
  729. return 0;
  730. err_unmap_rx:
  731. dma_unmap_single(p->port.dev, dma->rx_addr, dma->rx_size,
  732. DMA_FROM_DEVICE);
  733. err_free_rx:
  734. kfree(dma->rx_buf);
  735. err_release_tx:
  736. dma_release_channel(dma->tx_chan);
  737. err_release_rx:
  738. dma_release_channel(dma->rx_chan);
  739. return ret;
  740. }
  741. static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
  742. {
  743. struct s3c24xx_uart_dma *dma = p->dma;
  744. if (dma->rx_chan) {
  745. dmaengine_terminate_all(dma->rx_chan);
  746. dma_unmap_single(p->port.dev, dma->rx_addr,
  747. dma->rx_size, DMA_FROM_DEVICE);
  748. kfree(dma->rx_buf);
  749. dma_release_channel(dma->rx_chan);
  750. dma->rx_chan = NULL;
  751. }
  752. if (dma->tx_chan) {
  753. dmaengine_terminate_all(dma->tx_chan);
  754. dma_unmap_single(p->port.dev, dma->tx_addr,
  755. UART_XMIT_SIZE, DMA_TO_DEVICE);
  756. dma_release_channel(dma->tx_chan);
  757. dma->tx_chan = NULL;
  758. }
  759. }
  760. static void s3c24xx_serial_shutdown(struct uart_port *port)
  761. {
  762. struct s3c24xx_uart_port *ourport = to_ourport(port);
  763. if (ourport->tx_claimed) {
  764. if (!s3c24xx_serial_has_interrupt_mask(port))
  765. free_irq(ourport->tx_irq, ourport);
  766. tx_enabled(port) = 0;
  767. ourport->tx_claimed = 0;
  768. ourport->tx_mode = 0;
  769. }
  770. if (ourport->rx_claimed) {
  771. if (!s3c24xx_serial_has_interrupt_mask(port))
  772. free_irq(ourport->rx_irq, ourport);
  773. ourport->rx_claimed = 0;
  774. rx_enabled(port) = 0;
  775. }
  776. /* Clear pending interrupts and mask all interrupts */
  777. if (s3c24xx_serial_has_interrupt_mask(port)) {
  778. free_irq(port->irq, ourport);
  779. wr_regl(port, S3C64XX_UINTP, 0xf);
  780. wr_regl(port, S3C64XX_UINTM, 0xf);
  781. }
  782. if (ourport->dma)
  783. s3c24xx_serial_release_dma(ourport);
  784. ourport->tx_in_progress = 0;
  785. }
  786. static int s3c24xx_serial_startup(struct uart_port *port)
  787. {
  788. struct s3c24xx_uart_port *ourport = to_ourport(port);
  789. int ret;
  790. dbg("s3c24xx_serial_startup: port=%p (%08llx,%p)\n",
  791. port, (unsigned long long)port->mapbase, port->membase);
  792. rx_enabled(port) = 1;
  793. ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
  794. s3c24xx_serial_portname(port), ourport);
  795. if (ret != 0) {
  796. dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
  797. return ret;
  798. }
  799. ourport->rx_claimed = 1;
  800. dbg("requesting tx irq...\n");
  801. tx_enabled(port) = 1;
  802. ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
  803. s3c24xx_serial_portname(port), ourport);
  804. if (ret) {
  805. dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
  806. goto err;
  807. }
  808. ourport->tx_claimed = 1;
  809. dbg("s3c24xx_serial_startup ok\n");
  810. /* the port reset code should have done the correct
  811. * register setup for the port controls */
  812. return ret;
  813. err:
  814. s3c24xx_serial_shutdown(port);
  815. return ret;
  816. }
  817. static int s3c64xx_serial_startup(struct uart_port *port)
  818. {
  819. struct s3c24xx_uart_port *ourport = to_ourport(port);
  820. unsigned long flags;
  821. unsigned int ufcon;
  822. int ret;
  823. dbg("s3c64xx_serial_startup: port=%p (%08llx,%p)\n",
  824. port, (unsigned long long)port->mapbase, port->membase);
  825. wr_regl(port, S3C64XX_UINTM, 0xf);
  826. if (ourport->dma) {
  827. ret = s3c24xx_serial_request_dma(ourport);
  828. if (ret < 0) {
  829. dev_warn(port->dev,
  830. "DMA request failed, DMA will not be used\n");
  831. devm_kfree(port->dev, ourport->dma);
  832. ourport->dma = NULL;
  833. }
  834. }
  835. ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
  836. s3c24xx_serial_portname(port), ourport);
  837. if (ret) {
  838. dev_err(port->dev, "cannot get irq %d\n", port->irq);
  839. return ret;
  840. }
  841. /* For compatibility with s3c24xx Soc's */
  842. rx_enabled(port) = 1;
  843. ourport->rx_claimed = 1;
  844. tx_enabled(port) = 0;
  845. ourport->tx_claimed = 1;
  846. spin_lock_irqsave(&port->lock, flags);
  847. ufcon = rd_regl(port, S3C2410_UFCON);
  848. ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
  849. if (!uart_console(port))
  850. ufcon |= S3C2410_UFCON_RESETTX;
  851. wr_regl(port, S3C2410_UFCON, ufcon);
  852. enable_rx_pio(ourport);
  853. spin_unlock_irqrestore(&port->lock, flags);
  854. /* Enable Rx Interrupt */
  855. s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
  856. dbg("s3c64xx_serial_startup ok\n");
  857. return ret;
  858. }
  859. /* power power management control */
  860. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  861. unsigned int old)
  862. {
  863. struct s3c24xx_uart_port *ourport = to_ourport(port);
  864. int timeout = 10000;
  865. ourport->pm_level = level;
  866. switch (level) {
  867. case 3:
  868. while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
  869. udelay(100);
  870. if (!IS_ERR(ourport->baudclk))
  871. clk_disable_unprepare(ourport->baudclk);
  872. clk_disable_unprepare(ourport->clk);
  873. break;
  874. case 0:
  875. clk_prepare_enable(ourport->clk);
  876. if (!IS_ERR(ourport->baudclk))
  877. clk_prepare_enable(ourport->baudclk);
  878. break;
  879. default:
  880. dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
  881. }
  882. }
  883. /* baud rate calculation
  884. *
  885. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  886. * of different sources, including the peripheral clock ("pclk") and an
  887. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  888. * with a programmable extra divisor.
  889. *
  890. * The following code goes through the clock sources, and calculates the
  891. * baud clocks (and the resultant actual baud rates) and then tries to
  892. * pick the closest one and select that.
  893. *
  894. */
  895. #define MAX_CLK_NAME_LENGTH 15
  896. static inline int s3c24xx_serial_getsource(struct uart_port *port)
  897. {
  898. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  899. unsigned int ucon;
  900. if (info->num_clks == 1)
  901. return 0;
  902. ucon = rd_regl(port, S3C2410_UCON);
  903. ucon &= info->clksel_mask;
  904. return ucon >> info->clksel_shift;
  905. }
  906. static void s3c24xx_serial_setsource(struct uart_port *port,
  907. unsigned int clk_sel)
  908. {
  909. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  910. unsigned int ucon;
  911. if (info->num_clks == 1)
  912. return;
  913. ucon = rd_regl(port, S3C2410_UCON);
  914. if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
  915. return;
  916. ucon &= ~info->clksel_mask;
  917. ucon |= clk_sel << info->clksel_shift;
  918. wr_regl(port, S3C2410_UCON, ucon);
  919. }
  920. static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
  921. unsigned int req_baud, struct clk **best_clk,
  922. unsigned int *clk_num)
  923. {
  924. struct s3c24xx_uart_info *info = ourport->info;
  925. struct clk *clk;
  926. unsigned long rate;
  927. unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
  928. char clkname[MAX_CLK_NAME_LENGTH];
  929. int calc_deviation, deviation = (1 << 30) - 1;
  930. clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
  931. ourport->info->def_clk_sel;
  932. for (cnt = 0; cnt < info->num_clks; cnt++) {
  933. if (!(clk_sel & (1 << cnt)))
  934. continue;
  935. sprintf(clkname, "clk_uart_baud%d", cnt);
  936. clk = clk_get(ourport->port.dev, clkname);
  937. if (IS_ERR(clk))
  938. continue;
  939. rate = clk_get_rate(clk);
  940. if (!rate)
  941. continue;
  942. if (ourport->info->has_divslot) {
  943. unsigned long div = rate / req_baud;
  944. /* The UDIVSLOT register on the newer UARTs allows us to
  945. * get a divisor adjustment of 1/16th on the baud clock.
  946. *
  947. * We don't keep the UDIVSLOT value (the 16ths we
  948. * calculated by not multiplying the baud by 16) as it
  949. * is easy enough to recalculate.
  950. */
  951. quot = div / 16;
  952. baud = rate / div;
  953. } else {
  954. quot = (rate + (8 * req_baud)) / (16 * req_baud);
  955. baud = rate / (quot * 16);
  956. }
  957. quot--;
  958. calc_deviation = req_baud - baud;
  959. if (calc_deviation < 0)
  960. calc_deviation = -calc_deviation;
  961. if (calc_deviation < deviation) {
  962. *best_clk = clk;
  963. best_quot = quot;
  964. *clk_num = cnt;
  965. deviation = calc_deviation;
  966. }
  967. }
  968. return best_quot;
  969. }
  970. /* udivslot_table[]
  971. *
  972. * This table takes the fractional value of the baud divisor and gives
  973. * the recommended setting for the UDIVSLOT register.
  974. */
  975. static u16 udivslot_table[16] = {
  976. [0] = 0x0000,
  977. [1] = 0x0080,
  978. [2] = 0x0808,
  979. [3] = 0x0888,
  980. [4] = 0x2222,
  981. [5] = 0x4924,
  982. [6] = 0x4A52,
  983. [7] = 0x54AA,
  984. [8] = 0x5555,
  985. [9] = 0xD555,
  986. [10] = 0xD5D5,
  987. [11] = 0xDDD5,
  988. [12] = 0xDDDD,
  989. [13] = 0xDFDD,
  990. [14] = 0xDFDF,
  991. [15] = 0xFFDF,
  992. };
  993. static void s3c24xx_serial_set_termios(struct uart_port *port,
  994. struct ktermios *termios,
  995. struct ktermios *old)
  996. {
  997. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  998. struct s3c24xx_uart_port *ourport = to_ourport(port);
  999. struct clk *clk = ERR_PTR(-EINVAL);
  1000. unsigned long flags;
  1001. unsigned int baud, quot, clk_sel = 0;
  1002. unsigned int ulcon;
  1003. unsigned int umcon;
  1004. unsigned int udivslot = 0;
  1005. /*
  1006. * We don't support modem control lines.
  1007. */
  1008. termios->c_cflag &= ~(HUPCL | CMSPAR);
  1009. termios->c_cflag |= CLOCAL;
  1010. /*
  1011. * Ask the core to calculate the divisor for us.
  1012. */
  1013. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  1014. quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
  1015. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  1016. quot = port->custom_divisor;
  1017. if (IS_ERR(clk))
  1018. return;
  1019. /* check to see if we need to change clock source */
  1020. if (ourport->baudclk != clk) {
  1021. clk_prepare_enable(clk);
  1022. s3c24xx_serial_setsource(port, clk_sel);
  1023. if (!IS_ERR(ourport->baudclk)) {
  1024. clk_disable_unprepare(ourport->baudclk);
  1025. ourport->baudclk = ERR_PTR(-EINVAL);
  1026. }
  1027. ourport->baudclk = clk;
  1028. ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
  1029. }
  1030. if (ourport->info->has_divslot) {
  1031. unsigned int div = ourport->baudclk_rate / baud;
  1032. if (cfg->has_fracval) {
  1033. udivslot = (div & 15);
  1034. dbg("fracval = %04x\n", udivslot);
  1035. } else {
  1036. udivslot = udivslot_table[div & 15];
  1037. dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
  1038. }
  1039. }
  1040. switch (termios->c_cflag & CSIZE) {
  1041. case CS5:
  1042. dbg("config: 5bits/char\n");
  1043. ulcon = S3C2410_LCON_CS5;
  1044. break;
  1045. case CS6:
  1046. dbg("config: 6bits/char\n");
  1047. ulcon = S3C2410_LCON_CS6;
  1048. break;
  1049. case CS7:
  1050. dbg("config: 7bits/char\n");
  1051. ulcon = S3C2410_LCON_CS7;
  1052. break;
  1053. case CS8:
  1054. default:
  1055. dbg("config: 8bits/char\n");
  1056. ulcon = S3C2410_LCON_CS8;
  1057. break;
  1058. }
  1059. /* preserve original lcon IR settings */
  1060. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  1061. if (termios->c_cflag & CSTOPB)
  1062. ulcon |= S3C2410_LCON_STOPB;
  1063. if (termios->c_cflag & PARENB) {
  1064. if (termios->c_cflag & PARODD)
  1065. ulcon |= S3C2410_LCON_PODD;
  1066. else
  1067. ulcon |= S3C2410_LCON_PEVEN;
  1068. } else {
  1069. ulcon |= S3C2410_LCON_PNONE;
  1070. }
  1071. spin_lock_irqsave(&port->lock, flags);
  1072. dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  1073. ulcon, quot, udivslot);
  1074. wr_regl(port, S3C2410_ULCON, ulcon);
  1075. wr_regl(port, S3C2410_UBRDIV, quot);
  1076. umcon = rd_regl(port, S3C2410_UMCON);
  1077. if (termios->c_cflag & CRTSCTS) {
  1078. umcon |= S3C2410_UMCOM_AFC;
  1079. /* Disable RTS when RX FIFO contains 63 bytes */
  1080. umcon &= ~S3C2412_UMCON_AFC_8;
  1081. } else {
  1082. umcon &= ~S3C2410_UMCOM_AFC;
  1083. }
  1084. wr_regl(port, S3C2410_UMCON, umcon);
  1085. if (ourport->info->has_divslot)
  1086. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  1087. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  1088. rd_regl(port, S3C2410_ULCON),
  1089. rd_regl(port, S3C2410_UCON),
  1090. rd_regl(port, S3C2410_UFCON));
  1091. /*
  1092. * Update the per-port timeout.
  1093. */
  1094. uart_update_timeout(port, termios->c_cflag, baud);
  1095. /*
  1096. * Which character status flags are we interested in?
  1097. */
  1098. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  1099. if (termios->c_iflag & INPCK)
  1100. port->read_status_mask |= S3C2410_UERSTAT_FRAME |
  1101. S3C2410_UERSTAT_PARITY;
  1102. /*
  1103. * Which character status flags should we ignore?
  1104. */
  1105. port->ignore_status_mask = 0;
  1106. if (termios->c_iflag & IGNPAR)
  1107. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  1108. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  1109. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  1110. /*
  1111. * Ignore all characters if CREAD is not set.
  1112. */
  1113. if ((termios->c_cflag & CREAD) == 0)
  1114. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  1115. spin_unlock_irqrestore(&port->lock, flags);
  1116. }
  1117. static const char *s3c24xx_serial_type(struct uart_port *port)
  1118. {
  1119. switch (port->type) {
  1120. case PORT_S3C2410:
  1121. return "S3C2410";
  1122. case PORT_S3C2440:
  1123. return "S3C2440";
  1124. case PORT_S3C2412:
  1125. return "S3C2412";
  1126. case PORT_S3C6400:
  1127. return "S3C6400/10";
  1128. default:
  1129. return NULL;
  1130. }
  1131. }
  1132. #define MAP_SIZE (0x100)
  1133. static void s3c24xx_serial_release_port(struct uart_port *port)
  1134. {
  1135. release_mem_region(port->mapbase, MAP_SIZE);
  1136. }
  1137. static int s3c24xx_serial_request_port(struct uart_port *port)
  1138. {
  1139. const char *name = s3c24xx_serial_portname(port);
  1140. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  1141. }
  1142. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  1143. {
  1144. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1145. if (flags & UART_CONFIG_TYPE &&
  1146. s3c24xx_serial_request_port(port) == 0)
  1147. port->type = info->type;
  1148. }
  1149. /*
  1150. * verify the new serial_struct (for TIOCSSERIAL).
  1151. */
  1152. static int
  1153. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  1154. {
  1155. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1156. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  1157. return -EINVAL;
  1158. return 0;
  1159. }
  1160. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1161. static struct console s3c24xx_serial_console;
  1162. static int __init s3c24xx_serial_console_init(void)
  1163. {
  1164. register_console(&s3c24xx_serial_console);
  1165. return 0;
  1166. }
  1167. console_initcall(s3c24xx_serial_console_init);
  1168. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  1169. #else
  1170. #define S3C24XX_SERIAL_CONSOLE NULL
  1171. #endif
  1172. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1173. static int s3c24xx_serial_get_poll_char(struct uart_port *port);
  1174. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1175. unsigned char c);
  1176. #endif
  1177. static struct uart_ops s3c24xx_serial_ops = {
  1178. .pm = s3c24xx_serial_pm,
  1179. .tx_empty = s3c24xx_serial_tx_empty,
  1180. .get_mctrl = s3c24xx_serial_get_mctrl,
  1181. .set_mctrl = s3c24xx_serial_set_mctrl,
  1182. .stop_tx = s3c24xx_serial_stop_tx,
  1183. .start_tx = s3c24xx_serial_start_tx,
  1184. .stop_rx = s3c24xx_serial_stop_rx,
  1185. .break_ctl = s3c24xx_serial_break_ctl,
  1186. .startup = s3c24xx_serial_startup,
  1187. .shutdown = s3c24xx_serial_shutdown,
  1188. .set_termios = s3c24xx_serial_set_termios,
  1189. .type = s3c24xx_serial_type,
  1190. .release_port = s3c24xx_serial_release_port,
  1191. .request_port = s3c24xx_serial_request_port,
  1192. .config_port = s3c24xx_serial_config_port,
  1193. .verify_port = s3c24xx_serial_verify_port,
  1194. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1195. .poll_get_char = s3c24xx_serial_get_poll_char,
  1196. .poll_put_char = s3c24xx_serial_put_poll_char,
  1197. #endif
  1198. };
  1199. static struct uart_driver s3c24xx_uart_drv = {
  1200. .owner = THIS_MODULE,
  1201. .driver_name = "s3c2410_serial",
  1202. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  1203. .cons = S3C24XX_SERIAL_CONSOLE,
  1204. .dev_name = S3C24XX_SERIAL_NAME,
  1205. .major = S3C24XX_SERIAL_MAJOR,
  1206. .minor = S3C24XX_SERIAL_MINOR,
  1207. };
  1208. #define __PORT_LOCK_UNLOCKED(i) \
  1209. __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
  1210. static struct s3c24xx_uart_port
  1211. s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  1212. [0] = {
  1213. .port = {
  1214. .lock = __PORT_LOCK_UNLOCKED(0),
  1215. .iotype = UPIO_MEM,
  1216. .uartclk = 0,
  1217. .fifosize = 16,
  1218. .ops = &s3c24xx_serial_ops,
  1219. .flags = UPF_BOOT_AUTOCONF,
  1220. .line = 0,
  1221. }
  1222. },
  1223. [1] = {
  1224. .port = {
  1225. .lock = __PORT_LOCK_UNLOCKED(1),
  1226. .iotype = UPIO_MEM,
  1227. .uartclk = 0,
  1228. .fifosize = 16,
  1229. .ops = &s3c24xx_serial_ops,
  1230. .flags = UPF_BOOT_AUTOCONF,
  1231. .line = 1,
  1232. }
  1233. },
  1234. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  1235. [2] = {
  1236. .port = {
  1237. .lock = __PORT_LOCK_UNLOCKED(2),
  1238. .iotype = UPIO_MEM,
  1239. .uartclk = 0,
  1240. .fifosize = 16,
  1241. .ops = &s3c24xx_serial_ops,
  1242. .flags = UPF_BOOT_AUTOCONF,
  1243. .line = 2,
  1244. }
  1245. },
  1246. #endif
  1247. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  1248. [3] = {
  1249. .port = {
  1250. .lock = __PORT_LOCK_UNLOCKED(3),
  1251. .iotype = UPIO_MEM,
  1252. .uartclk = 0,
  1253. .fifosize = 16,
  1254. .ops = &s3c24xx_serial_ops,
  1255. .flags = UPF_BOOT_AUTOCONF,
  1256. .line = 3,
  1257. }
  1258. }
  1259. #endif
  1260. };
  1261. #undef __PORT_LOCK_UNLOCKED
  1262. /* s3c24xx_serial_resetport
  1263. *
  1264. * reset the fifos and other the settings.
  1265. */
  1266. static void s3c24xx_serial_resetport(struct uart_port *port,
  1267. struct s3c2410_uartcfg *cfg)
  1268. {
  1269. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1270. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1271. unsigned int ucon_mask;
  1272. ucon_mask = info->clksel_mask;
  1273. if (info->type == PORT_S3C2440)
  1274. ucon_mask |= S3C2440_UCON0_DIVMASK;
  1275. ucon &= ucon_mask;
  1276. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1277. /* reset both fifos */
  1278. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1279. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1280. /* some delay is required after fifo reset */
  1281. udelay(1);
  1282. }
  1283. #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
  1284. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  1285. unsigned long val, void *data)
  1286. {
  1287. struct s3c24xx_uart_port *port;
  1288. struct uart_port *uport;
  1289. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  1290. uport = &port->port;
  1291. /* check to see if port is enabled */
  1292. if (port->pm_level != 0)
  1293. return 0;
  1294. /* try and work out if the baudrate is changing, we can detect
  1295. * a change in rate, but we do not have support for detecting
  1296. * a disturbance in the clock-rate over the change.
  1297. */
  1298. if (IS_ERR(port->baudclk))
  1299. goto exit;
  1300. if (port->baudclk_rate == clk_get_rate(port->baudclk))
  1301. goto exit;
  1302. if (val == CPUFREQ_PRECHANGE) {
  1303. /* we should really shut the port down whilst the
  1304. * frequency change is in progress. */
  1305. } else if (val == CPUFREQ_POSTCHANGE) {
  1306. struct ktermios *termios;
  1307. struct tty_struct *tty;
  1308. if (uport->state == NULL)
  1309. goto exit;
  1310. tty = uport->state->port.tty;
  1311. if (tty == NULL)
  1312. goto exit;
  1313. termios = &tty->termios;
  1314. if (termios == NULL) {
  1315. dev_warn(uport->dev, "%s: no termios?\n", __func__);
  1316. goto exit;
  1317. }
  1318. s3c24xx_serial_set_termios(uport, termios, NULL);
  1319. }
  1320. exit:
  1321. return 0;
  1322. }
  1323. static inline int
  1324. s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  1325. {
  1326. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  1327. return cpufreq_register_notifier(&port->freq_transition,
  1328. CPUFREQ_TRANSITION_NOTIFIER);
  1329. }
  1330. static inline void
  1331. s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  1332. {
  1333. cpufreq_unregister_notifier(&port->freq_transition,
  1334. CPUFREQ_TRANSITION_NOTIFIER);
  1335. }
  1336. #else
  1337. static inline int
  1338. s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  1339. {
  1340. return 0;
  1341. }
  1342. static inline void
  1343. s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  1344. {
  1345. }
  1346. #endif
  1347. /* s3c24xx_serial_init_port
  1348. *
  1349. * initialise a single serial port from the platform device given
  1350. */
  1351. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  1352. struct platform_device *platdev)
  1353. {
  1354. struct uart_port *port = &ourport->port;
  1355. struct s3c2410_uartcfg *cfg = ourport->cfg;
  1356. struct resource *res;
  1357. int ret;
  1358. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  1359. if (platdev == NULL)
  1360. return -ENODEV;
  1361. if (port->mapbase != 0)
  1362. return -EINVAL;
  1363. /* setup info for port */
  1364. port->dev = &platdev->dev;
  1365. /* Startup sequence is different for s3c64xx and higher SoC's */
  1366. if (s3c24xx_serial_has_interrupt_mask(port))
  1367. s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
  1368. port->uartclk = 1;
  1369. if (cfg->uart_flags & UPF_CONS_FLOW) {
  1370. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  1371. port->flags |= UPF_CONS_FLOW;
  1372. }
  1373. /* sort our the physical and virtual addresses for each UART */
  1374. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  1375. if (res == NULL) {
  1376. dev_err(port->dev, "failed to find memory resource for uart\n");
  1377. return -EINVAL;
  1378. }
  1379. dbg("resource %pR)\n", res);
  1380. port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
  1381. if (!port->membase) {
  1382. dev_err(port->dev, "failed to remap controller address\n");
  1383. return -EBUSY;
  1384. }
  1385. port->mapbase = res->start;
  1386. ret = platform_get_irq(platdev, 0);
  1387. if (ret < 0)
  1388. port->irq = 0;
  1389. else {
  1390. port->irq = ret;
  1391. ourport->rx_irq = ret;
  1392. ourport->tx_irq = ret + 1;
  1393. }
  1394. ret = platform_get_irq(platdev, 1);
  1395. if (ret > 0)
  1396. ourport->tx_irq = ret;
  1397. /*
  1398. * DMA is currently supported only on DT platforms, if DMA properties
  1399. * are specified.
  1400. */
  1401. if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
  1402. "dmas", NULL)) {
  1403. ourport->dma = devm_kzalloc(port->dev,
  1404. sizeof(*ourport->dma),
  1405. GFP_KERNEL);
  1406. if (!ourport->dma) {
  1407. ret = -ENOMEM;
  1408. goto err;
  1409. }
  1410. }
  1411. ourport->clk = clk_get(&platdev->dev, "uart");
  1412. if (IS_ERR(ourport->clk)) {
  1413. pr_err("%s: Controller clock not found\n",
  1414. dev_name(&platdev->dev));
  1415. ret = PTR_ERR(ourport->clk);
  1416. goto err;
  1417. }
  1418. ret = clk_prepare_enable(ourport->clk);
  1419. if (ret) {
  1420. pr_err("uart: clock failed to prepare+enable: %d\n", ret);
  1421. clk_put(ourport->clk);
  1422. goto err;
  1423. }
  1424. /* Keep all interrupts masked and cleared */
  1425. if (s3c24xx_serial_has_interrupt_mask(port)) {
  1426. wr_regl(port, S3C64XX_UINTM, 0xf);
  1427. wr_regl(port, S3C64XX_UINTP, 0xf);
  1428. wr_regl(port, S3C64XX_UINTSP, 0xf);
  1429. }
  1430. dbg("port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
  1431. &port->mapbase, port->membase, port->irq,
  1432. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  1433. /* reset the fifos (and setup the uart) */
  1434. s3c24xx_serial_resetport(port, cfg);
  1435. return 0;
  1436. err:
  1437. port->mapbase = 0;
  1438. return ret;
  1439. }
  1440. /* Device driver serial port probe */
  1441. static const struct of_device_id s3c24xx_uart_dt_match[];
  1442. static int probe_index;
  1443. static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
  1444. struct platform_device *pdev)
  1445. {
  1446. #ifdef CONFIG_OF
  1447. if (pdev->dev.of_node) {
  1448. const struct of_device_id *match;
  1449. match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
  1450. return (struct s3c24xx_serial_drv_data *)match->data;
  1451. }
  1452. #endif
  1453. return (struct s3c24xx_serial_drv_data *)
  1454. platform_get_device_id(pdev)->driver_data;
  1455. }
  1456. static int s3c24xx_serial_probe(struct platform_device *pdev)
  1457. {
  1458. struct device_node *np = pdev->dev.of_node;
  1459. struct s3c24xx_uart_port *ourport;
  1460. int index = probe_index;
  1461. int ret;
  1462. if (np) {
  1463. ret = of_alias_get_id(np, "serial");
  1464. if (ret >= 0)
  1465. index = ret;
  1466. }
  1467. dbg("s3c24xx_serial_probe(%p) %d\n", pdev, index);
  1468. if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
  1469. dev_err(&pdev->dev, "serial%d out of range\n", index);
  1470. return -EINVAL;
  1471. }
  1472. ourport = &s3c24xx_serial_ports[index];
  1473. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  1474. if (!ourport->drv_data) {
  1475. dev_err(&pdev->dev, "could not find driver data\n");
  1476. return -ENODEV;
  1477. }
  1478. ourport->baudclk = ERR_PTR(-EINVAL);
  1479. ourport->info = ourport->drv_data->info;
  1480. ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
  1481. dev_get_platdata(&pdev->dev) :
  1482. ourport->drv_data->def_cfg;
  1483. if (np)
  1484. of_property_read_u32(np,
  1485. "samsung,uart-fifosize", &ourport->port.fifosize);
  1486. if (ourport->drv_data->fifosize[index])
  1487. ourport->port.fifosize = ourport->drv_data->fifosize[index];
  1488. else if (ourport->info->fifosize)
  1489. ourport->port.fifosize = ourport->info->fifosize;
  1490. /*
  1491. * DMA transfers must be aligned at least to cache line size,
  1492. * so find minimal transfer size suitable for DMA mode
  1493. */
  1494. ourport->min_dma_size = max_t(int, ourport->port.fifosize,
  1495. dma_get_cache_alignment());
  1496. dbg("%s: initialising port %p...\n", __func__, ourport);
  1497. ret = s3c24xx_serial_init_port(ourport, pdev);
  1498. if (ret < 0)
  1499. return ret;
  1500. if (!s3c24xx_uart_drv.state) {
  1501. ret = uart_register_driver(&s3c24xx_uart_drv);
  1502. if (ret < 0) {
  1503. pr_err("Failed to register Samsung UART driver\n");
  1504. return ret;
  1505. }
  1506. }
  1507. dbg("%s: adding port\n", __func__);
  1508. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  1509. platform_set_drvdata(pdev, &ourport->port);
  1510. /*
  1511. * Deactivate the clock enabled in s3c24xx_serial_init_port here,
  1512. * so that a potential re-enablement through the pm-callback overlaps
  1513. * and keeps the clock enabled in this case.
  1514. */
  1515. clk_disable_unprepare(ourport->clk);
  1516. ret = s3c24xx_serial_cpufreq_register(ourport);
  1517. if (ret < 0)
  1518. dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
  1519. probe_index++;
  1520. return 0;
  1521. }
  1522. static int s3c24xx_serial_remove(struct platform_device *dev)
  1523. {
  1524. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  1525. if (port) {
  1526. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  1527. uart_remove_one_port(&s3c24xx_uart_drv, port);
  1528. }
  1529. uart_unregister_driver(&s3c24xx_uart_drv);
  1530. return 0;
  1531. }
  1532. /* UART power management code */
  1533. #ifdef CONFIG_PM_SLEEP
  1534. static int s3c24xx_serial_suspend(struct device *dev)
  1535. {
  1536. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1537. if (port)
  1538. uart_suspend_port(&s3c24xx_uart_drv, port);
  1539. return 0;
  1540. }
  1541. static int s3c24xx_serial_resume(struct device *dev)
  1542. {
  1543. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1544. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1545. if (port) {
  1546. clk_prepare_enable(ourport->clk);
  1547. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1548. clk_disable_unprepare(ourport->clk);
  1549. uart_resume_port(&s3c24xx_uart_drv, port);
  1550. }
  1551. return 0;
  1552. }
  1553. static int s3c24xx_serial_resume_noirq(struct device *dev)
  1554. {
  1555. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1556. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1557. if (port) {
  1558. /* restore IRQ mask */
  1559. if (s3c24xx_serial_has_interrupt_mask(port)) {
  1560. unsigned int uintm = 0xf;
  1561. if (tx_enabled(port))
  1562. uintm &= ~S3C64XX_UINTM_TXD_MSK;
  1563. if (rx_enabled(port))
  1564. uintm &= ~S3C64XX_UINTM_RXD_MSK;
  1565. clk_prepare_enable(ourport->clk);
  1566. wr_regl(port, S3C64XX_UINTM, uintm);
  1567. clk_disable_unprepare(ourport->clk);
  1568. }
  1569. }
  1570. return 0;
  1571. }
  1572. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1573. .suspend = s3c24xx_serial_suspend,
  1574. .resume = s3c24xx_serial_resume,
  1575. .resume_noirq = s3c24xx_serial_resume_noirq,
  1576. };
  1577. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1578. #else /* !CONFIG_PM_SLEEP */
  1579. #define SERIAL_SAMSUNG_PM_OPS NULL
  1580. #endif /* CONFIG_PM_SLEEP */
  1581. /* Console code */
  1582. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1583. static struct uart_port *cons_uart;
  1584. static int
  1585. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1586. {
  1587. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1588. unsigned long ufstat, utrstat;
  1589. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1590. /* fifo mode - check amount of data in fifo registers... */
  1591. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1592. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1593. }
  1594. /* in non-fifo mode, we go and use the tx buffer empty */
  1595. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1596. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1597. }
  1598. static bool
  1599. s3c24xx_port_configured(unsigned int ucon)
  1600. {
  1601. /* consider the serial port configured if the tx/rx mode set */
  1602. return (ucon & 0xf) != 0;
  1603. }
  1604. #ifdef CONFIG_CONSOLE_POLL
  1605. /*
  1606. * Console polling routines for writing and reading from the uart while
  1607. * in an interrupt or debug context.
  1608. */
  1609. static int s3c24xx_serial_get_poll_char(struct uart_port *port)
  1610. {
  1611. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1612. unsigned int ufstat;
  1613. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1614. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  1615. return NO_POLL_CHAR;
  1616. return rd_regb(port, S3C2410_URXH);
  1617. }
  1618. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1619. unsigned char c)
  1620. {
  1621. unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
  1622. unsigned int ucon = rd_regl(port, S3C2410_UCON);
  1623. /* not possible to xmit on unconfigured port */
  1624. if (!s3c24xx_port_configured(ucon))
  1625. return;
  1626. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1627. cpu_relax();
  1628. wr_regb(port, S3C2410_UTXH, c);
  1629. }
  1630. #endif /* CONFIG_CONSOLE_POLL */
  1631. static void
  1632. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1633. {
  1634. unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
  1635. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1636. cpu_relax();
  1637. wr_regb(port, S3C2410_UTXH, ch);
  1638. }
  1639. static void
  1640. s3c24xx_serial_console_write(struct console *co, const char *s,
  1641. unsigned int count)
  1642. {
  1643. unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
  1644. /* not possible to xmit on unconfigured port */
  1645. if (!s3c24xx_port_configured(ucon))
  1646. return;
  1647. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1648. }
  1649. static void __init
  1650. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1651. int *parity, int *bits)
  1652. {
  1653. struct clk *clk;
  1654. unsigned int ulcon;
  1655. unsigned int ucon;
  1656. unsigned int ubrdiv;
  1657. unsigned long rate;
  1658. unsigned int clk_sel;
  1659. char clk_name[MAX_CLK_NAME_LENGTH];
  1660. ulcon = rd_regl(port, S3C2410_ULCON);
  1661. ucon = rd_regl(port, S3C2410_UCON);
  1662. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1663. dbg("s3c24xx_serial_get_options: port=%p\n"
  1664. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1665. port, ulcon, ucon, ubrdiv);
  1666. if (s3c24xx_port_configured(ucon)) {
  1667. switch (ulcon & S3C2410_LCON_CSMASK) {
  1668. case S3C2410_LCON_CS5:
  1669. *bits = 5;
  1670. break;
  1671. case S3C2410_LCON_CS6:
  1672. *bits = 6;
  1673. break;
  1674. case S3C2410_LCON_CS7:
  1675. *bits = 7;
  1676. break;
  1677. case S3C2410_LCON_CS8:
  1678. default:
  1679. *bits = 8;
  1680. break;
  1681. }
  1682. switch (ulcon & S3C2410_LCON_PMASK) {
  1683. case S3C2410_LCON_PEVEN:
  1684. *parity = 'e';
  1685. break;
  1686. case S3C2410_LCON_PODD:
  1687. *parity = 'o';
  1688. break;
  1689. case S3C2410_LCON_PNONE:
  1690. default:
  1691. *parity = 'n';
  1692. }
  1693. /* now calculate the baud rate */
  1694. clk_sel = s3c24xx_serial_getsource(port);
  1695. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1696. clk = clk_get(port->dev, clk_name);
  1697. if (!IS_ERR(clk))
  1698. rate = clk_get_rate(clk);
  1699. else
  1700. rate = 1;
  1701. *baud = rate / (16 * (ubrdiv + 1));
  1702. dbg("calculated baud %d\n", *baud);
  1703. }
  1704. }
  1705. static int __init
  1706. s3c24xx_serial_console_setup(struct console *co, char *options)
  1707. {
  1708. struct uart_port *port;
  1709. int baud = 9600;
  1710. int bits = 8;
  1711. int parity = 'n';
  1712. int flow = 'n';
  1713. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1714. co, co->index, options);
  1715. /* is this a valid port */
  1716. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1717. co->index = 0;
  1718. port = &s3c24xx_serial_ports[co->index].port;
  1719. /* is the port configured? */
  1720. if (port->mapbase == 0x0)
  1721. return -ENODEV;
  1722. cons_uart = port;
  1723. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1724. /*
  1725. * Check whether an invalid uart number has been specified, and
  1726. * if so, search for the first available port that does have
  1727. * console support.
  1728. */
  1729. if (options)
  1730. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1731. else
  1732. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1733. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1734. return uart_set_options(port, co, baud, parity, bits, flow);
  1735. }
  1736. static struct console s3c24xx_serial_console = {
  1737. .name = S3C24XX_SERIAL_NAME,
  1738. .device = uart_console_device,
  1739. .flags = CON_PRINTBUFFER,
  1740. .index = -1,
  1741. .write = s3c24xx_serial_console_write,
  1742. .setup = s3c24xx_serial_console_setup,
  1743. .data = &s3c24xx_uart_drv,
  1744. };
  1745. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1746. #ifdef CONFIG_CPU_S3C2410
  1747. static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
  1748. .info = &(struct s3c24xx_uart_info) {
  1749. .name = "Samsung S3C2410 UART",
  1750. .type = PORT_S3C2410,
  1751. .fifosize = 16,
  1752. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1753. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1754. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1755. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1756. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1757. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1758. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1759. .num_clks = 2,
  1760. .clksel_mask = S3C2410_UCON_CLKMASK,
  1761. .clksel_shift = S3C2410_UCON_CLKSHIFT,
  1762. },
  1763. .def_cfg = &(struct s3c2410_uartcfg) {
  1764. .ucon = S3C2410_UCON_DEFAULT,
  1765. .ufcon = S3C2410_UFCON_DEFAULT,
  1766. },
  1767. };
  1768. #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
  1769. #else
  1770. #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1771. #endif
  1772. #ifdef CONFIG_CPU_S3C2412
  1773. static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
  1774. .info = &(struct s3c24xx_uart_info) {
  1775. .name = "Samsung S3C2412 UART",
  1776. .type = PORT_S3C2412,
  1777. .fifosize = 64,
  1778. .has_divslot = 1,
  1779. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1780. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1781. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1782. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1783. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1784. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1785. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1786. .num_clks = 4,
  1787. .clksel_mask = S3C2412_UCON_CLKMASK,
  1788. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1789. },
  1790. .def_cfg = &(struct s3c2410_uartcfg) {
  1791. .ucon = S3C2410_UCON_DEFAULT,
  1792. .ufcon = S3C2410_UFCON_DEFAULT,
  1793. },
  1794. };
  1795. #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
  1796. #else
  1797. #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1798. #endif
  1799. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
  1800. defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
  1801. static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
  1802. .info = &(struct s3c24xx_uart_info) {
  1803. .name = "Samsung S3C2440 UART",
  1804. .type = PORT_S3C2440,
  1805. .fifosize = 64,
  1806. .has_divslot = 1,
  1807. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1808. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1809. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1810. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1811. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1812. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1813. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1814. .num_clks = 4,
  1815. .clksel_mask = S3C2412_UCON_CLKMASK,
  1816. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1817. },
  1818. .def_cfg = &(struct s3c2410_uartcfg) {
  1819. .ucon = S3C2410_UCON_DEFAULT,
  1820. .ufcon = S3C2410_UFCON_DEFAULT,
  1821. },
  1822. };
  1823. #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
  1824. #else
  1825. #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1826. #endif
  1827. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
  1828. static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1829. .info = &(struct s3c24xx_uart_info) {
  1830. .name = "Samsung S3C6400 UART",
  1831. .type = PORT_S3C6400,
  1832. .fifosize = 64,
  1833. .has_divslot = 1,
  1834. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1835. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1836. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1837. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1838. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1839. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1840. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1841. .num_clks = 4,
  1842. .clksel_mask = S3C6400_UCON_CLKMASK,
  1843. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1844. },
  1845. .def_cfg = &(struct s3c2410_uartcfg) {
  1846. .ucon = S3C2410_UCON_DEFAULT,
  1847. .ufcon = S3C2410_UFCON_DEFAULT,
  1848. },
  1849. };
  1850. #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
  1851. #else
  1852. #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1853. #endif
  1854. #ifdef CONFIG_CPU_S5PV210
  1855. static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1856. .info = &(struct s3c24xx_uart_info) {
  1857. .name = "Samsung S5PV210 UART",
  1858. .type = PORT_S3C6400,
  1859. .has_divslot = 1,
  1860. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1861. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1862. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1863. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1864. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1865. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1866. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1867. .num_clks = 2,
  1868. .clksel_mask = S5PV210_UCON_CLKMASK,
  1869. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1870. },
  1871. .def_cfg = &(struct s3c2410_uartcfg) {
  1872. .ucon = S5PV210_UCON_DEFAULT,
  1873. .ufcon = S5PV210_UFCON_DEFAULT,
  1874. },
  1875. .fifosize = { 256, 64, 16, 16 },
  1876. };
  1877. #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
  1878. #else
  1879. #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1880. #endif
  1881. #if defined(CONFIG_ARCH_EXYNOS)
  1882. #define EXYNOS_COMMON_SERIAL_DRV_DATA \
  1883. .info = &(struct s3c24xx_uart_info) { \
  1884. .name = "Samsung Exynos UART", \
  1885. .type = PORT_S3C6400, \
  1886. .has_divslot = 1, \
  1887. .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
  1888. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
  1889. .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
  1890. .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
  1891. .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
  1892. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
  1893. .def_clk_sel = S3C2410_UCON_CLKSEL0, \
  1894. .num_clks = 1, \
  1895. .clksel_mask = 0, \
  1896. .clksel_shift = 0, \
  1897. }, \
  1898. .def_cfg = &(struct s3c2410_uartcfg) { \
  1899. .ucon = S5PV210_UCON_DEFAULT, \
  1900. .ufcon = S5PV210_UFCON_DEFAULT, \
  1901. .has_fracval = 1, \
  1902. } \
  1903. static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  1904. EXYNOS_COMMON_SERIAL_DRV_DATA,
  1905. .fifosize = { 256, 64, 16, 16 },
  1906. };
  1907. static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
  1908. EXYNOS_COMMON_SERIAL_DRV_DATA,
  1909. .fifosize = { 64, 256, 16, 256 },
  1910. };
  1911. #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
  1912. #define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data)
  1913. #else
  1914. #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1915. #define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1916. #endif
  1917. static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
  1918. {
  1919. .name = "s3c2410-uart",
  1920. .driver_data = S3C2410_SERIAL_DRV_DATA,
  1921. }, {
  1922. .name = "s3c2412-uart",
  1923. .driver_data = S3C2412_SERIAL_DRV_DATA,
  1924. }, {
  1925. .name = "s3c2440-uart",
  1926. .driver_data = S3C2440_SERIAL_DRV_DATA,
  1927. }, {
  1928. .name = "s3c6400-uart",
  1929. .driver_data = S3C6400_SERIAL_DRV_DATA,
  1930. }, {
  1931. .name = "s5pv210-uart",
  1932. .driver_data = S5PV210_SERIAL_DRV_DATA,
  1933. }, {
  1934. .name = "exynos4210-uart",
  1935. .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
  1936. }, {
  1937. .name = "exynos5433-uart",
  1938. .driver_data = EXYNOS5433_SERIAL_DRV_DATA,
  1939. },
  1940. { },
  1941. };
  1942. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  1943. #ifdef CONFIG_OF
  1944. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  1945. { .compatible = "samsung,s3c2410-uart",
  1946. .data = (void *)S3C2410_SERIAL_DRV_DATA },
  1947. { .compatible = "samsung,s3c2412-uart",
  1948. .data = (void *)S3C2412_SERIAL_DRV_DATA },
  1949. { .compatible = "samsung,s3c2440-uart",
  1950. .data = (void *)S3C2440_SERIAL_DRV_DATA },
  1951. { .compatible = "samsung,s3c6400-uart",
  1952. .data = (void *)S3C6400_SERIAL_DRV_DATA },
  1953. { .compatible = "samsung,s5pv210-uart",
  1954. .data = (void *)S5PV210_SERIAL_DRV_DATA },
  1955. { .compatible = "samsung,exynos4210-uart",
  1956. .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
  1957. { .compatible = "samsung,exynos5433-uart",
  1958. .data = (void *)EXYNOS5433_SERIAL_DRV_DATA },
  1959. {},
  1960. };
  1961. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  1962. #endif
  1963. static struct platform_driver samsung_serial_driver = {
  1964. .probe = s3c24xx_serial_probe,
  1965. .remove = s3c24xx_serial_remove,
  1966. .id_table = s3c24xx_serial_driver_ids,
  1967. .driver = {
  1968. .name = "samsung-uart",
  1969. .pm = SERIAL_SAMSUNG_PM_OPS,
  1970. .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
  1971. },
  1972. };
  1973. module_platform_driver(samsung_serial_driver);
  1974. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1975. /*
  1976. * Early console.
  1977. */
  1978. struct samsung_early_console_data {
  1979. u32 txfull_mask;
  1980. };
  1981. static void samsung_early_busyuart(struct uart_port *port)
  1982. {
  1983. while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
  1984. ;
  1985. }
  1986. static void samsung_early_busyuart_fifo(struct uart_port *port)
  1987. {
  1988. struct samsung_early_console_data *data = port->private_data;
  1989. while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
  1990. ;
  1991. }
  1992. static void samsung_early_putc(struct uart_port *port, int c)
  1993. {
  1994. if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
  1995. samsung_early_busyuart_fifo(port);
  1996. else
  1997. samsung_early_busyuart(port);
  1998. writeb(c, port->membase + S3C2410_UTXH);
  1999. }
  2000. static void samsung_early_write(struct console *con, const char *s, unsigned n)
  2001. {
  2002. struct earlycon_device *dev = con->data;
  2003. uart_console_write(&dev->port, s, n, samsung_early_putc);
  2004. }
  2005. static int __init samsung_early_console_setup(struct earlycon_device *device,
  2006. const char *opt)
  2007. {
  2008. if (!device->port.membase)
  2009. return -ENODEV;
  2010. device->con->write = samsung_early_write;
  2011. return 0;
  2012. }
  2013. /* S3C2410 */
  2014. static struct samsung_early_console_data s3c2410_early_console_data = {
  2015. .txfull_mask = S3C2410_UFSTAT_TXFULL,
  2016. };
  2017. static int __init s3c2410_early_console_setup(struct earlycon_device *device,
  2018. const char *opt)
  2019. {
  2020. device->port.private_data = &s3c2410_early_console_data;
  2021. return samsung_early_console_setup(device, opt);
  2022. }
  2023. OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
  2024. s3c2410_early_console_setup);
  2025. /* S3C2412, S3C2440, S3C64xx */
  2026. static struct samsung_early_console_data s3c2440_early_console_data = {
  2027. .txfull_mask = S3C2440_UFSTAT_TXFULL,
  2028. };
  2029. static int __init s3c2440_early_console_setup(struct earlycon_device *device,
  2030. const char *opt)
  2031. {
  2032. device->port.private_data = &s3c2440_early_console_data;
  2033. return samsung_early_console_setup(device, opt);
  2034. }
  2035. OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
  2036. s3c2440_early_console_setup);
  2037. OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
  2038. s3c2440_early_console_setup);
  2039. OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
  2040. s3c2440_early_console_setup);
  2041. /* S5PV210, EXYNOS */
  2042. static struct samsung_early_console_data s5pv210_early_console_data = {
  2043. .txfull_mask = S5PV210_UFSTAT_TXFULL,
  2044. };
  2045. static int __init s5pv210_early_console_setup(struct earlycon_device *device,
  2046. const char *opt)
  2047. {
  2048. device->port.private_data = &s5pv210_early_console_data;
  2049. return samsung_early_console_setup(device, opt);
  2050. }
  2051. OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
  2052. s5pv210_early_console_setup);
  2053. OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
  2054. s5pv210_early_console_setup);
  2055. #endif
  2056. MODULE_ALIAS("platform:samsung-uart");
  2057. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  2058. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  2059. MODULE_LICENSE("GPL v2");