samsung.c 63 KB

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