samsung.c 63 KB

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