samsung.c 63 KB

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