samsung.c 63 KB

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