samsung.c 65 KB

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