samsung.c 63 KB

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