samsung.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510
  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. umcon = rd_regl(port, S3C2410_UMCON);
  1067. if (termios->c_cflag & CRTSCTS) {
  1068. umcon |= S3C2410_UMCOM_AFC;
  1069. /* Disable RTS when RX FIFO contains 63 bytes */
  1070. umcon &= ~S3C2412_UMCON_AFC_8;
  1071. } else {
  1072. umcon &= ~S3C2410_UMCOM_AFC;
  1073. }
  1074. wr_regl(port, S3C2410_UMCON, umcon);
  1075. if (ourport->info->has_divslot)
  1076. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  1077. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  1078. rd_regl(port, S3C2410_ULCON),
  1079. rd_regl(port, S3C2410_UCON),
  1080. rd_regl(port, S3C2410_UFCON));
  1081. /*
  1082. * Update the per-port timeout.
  1083. */
  1084. uart_update_timeout(port, termios->c_cflag, baud);
  1085. /*
  1086. * Which character status flags are we interested in?
  1087. */
  1088. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  1089. if (termios->c_iflag & INPCK)
  1090. port->read_status_mask |= S3C2410_UERSTAT_FRAME |
  1091. S3C2410_UERSTAT_PARITY;
  1092. /*
  1093. * Which character status flags should we ignore?
  1094. */
  1095. port->ignore_status_mask = 0;
  1096. if (termios->c_iflag & IGNPAR)
  1097. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  1098. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  1099. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  1100. /*
  1101. * Ignore all characters if CREAD is not set.
  1102. */
  1103. if ((termios->c_cflag & CREAD) == 0)
  1104. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  1105. spin_unlock_irqrestore(&port->lock, flags);
  1106. }
  1107. static const char *s3c24xx_serial_type(struct uart_port *port)
  1108. {
  1109. switch (port->type) {
  1110. case PORT_S3C2410:
  1111. return "S3C2410";
  1112. case PORT_S3C2440:
  1113. return "S3C2440";
  1114. case PORT_S3C2412:
  1115. return "S3C2412";
  1116. case PORT_S3C6400:
  1117. return "S3C6400/10";
  1118. default:
  1119. return NULL;
  1120. }
  1121. }
  1122. #define MAP_SIZE (0x100)
  1123. static void s3c24xx_serial_release_port(struct uart_port *port)
  1124. {
  1125. release_mem_region(port->mapbase, MAP_SIZE);
  1126. }
  1127. static int s3c24xx_serial_request_port(struct uart_port *port)
  1128. {
  1129. const char *name = s3c24xx_serial_portname(port);
  1130. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  1131. }
  1132. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  1133. {
  1134. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1135. if (flags & UART_CONFIG_TYPE &&
  1136. s3c24xx_serial_request_port(port) == 0)
  1137. port->type = info->type;
  1138. }
  1139. /*
  1140. * verify the new serial_struct (for TIOCSSERIAL).
  1141. */
  1142. static int
  1143. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  1144. {
  1145. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1146. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  1147. return -EINVAL;
  1148. return 0;
  1149. }
  1150. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1151. static struct console s3c24xx_serial_console;
  1152. static int __init s3c24xx_serial_console_init(void)
  1153. {
  1154. register_console(&s3c24xx_serial_console);
  1155. return 0;
  1156. }
  1157. console_initcall(s3c24xx_serial_console_init);
  1158. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  1159. #else
  1160. #define S3C24XX_SERIAL_CONSOLE NULL
  1161. #endif
  1162. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1163. static int s3c24xx_serial_get_poll_char(struct uart_port *port);
  1164. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1165. unsigned char c);
  1166. #endif
  1167. static struct uart_ops s3c24xx_serial_ops = {
  1168. .pm = s3c24xx_serial_pm,
  1169. .tx_empty = s3c24xx_serial_tx_empty,
  1170. .get_mctrl = s3c24xx_serial_get_mctrl,
  1171. .set_mctrl = s3c24xx_serial_set_mctrl,
  1172. .stop_tx = s3c24xx_serial_stop_tx,
  1173. .start_tx = s3c24xx_serial_start_tx,
  1174. .stop_rx = s3c24xx_serial_stop_rx,
  1175. .break_ctl = s3c24xx_serial_break_ctl,
  1176. .startup = s3c24xx_serial_startup,
  1177. .shutdown = s3c24xx_serial_shutdown,
  1178. .set_termios = s3c24xx_serial_set_termios,
  1179. .type = s3c24xx_serial_type,
  1180. .release_port = s3c24xx_serial_release_port,
  1181. .request_port = s3c24xx_serial_request_port,
  1182. .config_port = s3c24xx_serial_config_port,
  1183. .verify_port = s3c24xx_serial_verify_port,
  1184. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1185. .poll_get_char = s3c24xx_serial_get_poll_char,
  1186. .poll_put_char = s3c24xx_serial_put_poll_char,
  1187. #endif
  1188. };
  1189. static struct uart_driver s3c24xx_uart_drv = {
  1190. .owner = THIS_MODULE,
  1191. .driver_name = "s3c2410_serial",
  1192. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  1193. .cons = S3C24XX_SERIAL_CONSOLE,
  1194. .dev_name = S3C24XX_SERIAL_NAME,
  1195. .major = S3C24XX_SERIAL_MAJOR,
  1196. .minor = S3C24XX_SERIAL_MINOR,
  1197. };
  1198. #define __PORT_LOCK_UNLOCKED(i) \
  1199. __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
  1200. static struct s3c24xx_uart_port
  1201. s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  1202. [0] = {
  1203. .port = {
  1204. .lock = __PORT_LOCK_UNLOCKED(0),
  1205. .iotype = UPIO_MEM,
  1206. .uartclk = 0,
  1207. .fifosize = 16,
  1208. .ops = &s3c24xx_serial_ops,
  1209. .flags = UPF_BOOT_AUTOCONF,
  1210. .line = 0,
  1211. }
  1212. },
  1213. [1] = {
  1214. .port = {
  1215. .lock = __PORT_LOCK_UNLOCKED(1),
  1216. .iotype = UPIO_MEM,
  1217. .uartclk = 0,
  1218. .fifosize = 16,
  1219. .ops = &s3c24xx_serial_ops,
  1220. .flags = UPF_BOOT_AUTOCONF,
  1221. .line = 1,
  1222. }
  1223. },
  1224. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  1225. [2] = {
  1226. .port = {
  1227. .lock = __PORT_LOCK_UNLOCKED(2),
  1228. .iotype = UPIO_MEM,
  1229. .uartclk = 0,
  1230. .fifosize = 16,
  1231. .ops = &s3c24xx_serial_ops,
  1232. .flags = UPF_BOOT_AUTOCONF,
  1233. .line = 2,
  1234. }
  1235. },
  1236. #endif
  1237. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  1238. [3] = {
  1239. .port = {
  1240. .lock = __PORT_LOCK_UNLOCKED(3),
  1241. .iotype = UPIO_MEM,
  1242. .uartclk = 0,
  1243. .fifosize = 16,
  1244. .ops = &s3c24xx_serial_ops,
  1245. .flags = UPF_BOOT_AUTOCONF,
  1246. .line = 3,
  1247. }
  1248. }
  1249. #endif
  1250. };
  1251. #undef __PORT_LOCK_UNLOCKED
  1252. /* s3c24xx_serial_resetport
  1253. *
  1254. * reset the fifos and other the settings.
  1255. */
  1256. static void s3c24xx_serial_resetport(struct uart_port *port,
  1257. struct s3c2410_uartcfg *cfg)
  1258. {
  1259. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1260. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1261. unsigned int ucon_mask;
  1262. ucon_mask = info->clksel_mask;
  1263. if (info->type == PORT_S3C2440)
  1264. ucon_mask |= S3C2440_UCON0_DIVMASK;
  1265. ucon &= ucon_mask;
  1266. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1267. /* reset both fifos */
  1268. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1269. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1270. /* some delay is required after fifo reset */
  1271. udelay(1);
  1272. }
  1273. #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
  1274. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  1275. unsigned long val, void *data)
  1276. {
  1277. struct s3c24xx_uart_port *port;
  1278. struct uart_port *uport;
  1279. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  1280. uport = &port->port;
  1281. /* check to see if port is enabled */
  1282. if (port->pm_level != 0)
  1283. return 0;
  1284. /* try and work out if the baudrate is changing, we can detect
  1285. * a change in rate, but we do not have support for detecting
  1286. * a disturbance in the clock-rate over the change.
  1287. */
  1288. if (IS_ERR(port->baudclk))
  1289. goto exit;
  1290. if (port->baudclk_rate == clk_get_rate(port->baudclk))
  1291. goto exit;
  1292. if (val == CPUFREQ_PRECHANGE) {
  1293. /* we should really shut the port down whilst the
  1294. * frequency change is in progress. */
  1295. } else if (val == CPUFREQ_POSTCHANGE) {
  1296. struct ktermios *termios;
  1297. struct tty_struct *tty;
  1298. if (uport->state == NULL)
  1299. goto exit;
  1300. tty = uport->state->port.tty;
  1301. if (tty == NULL)
  1302. goto exit;
  1303. termios = &tty->termios;
  1304. if (termios == NULL) {
  1305. dev_warn(uport->dev, "%s: no termios?\n", __func__);
  1306. goto exit;
  1307. }
  1308. s3c24xx_serial_set_termios(uport, termios, NULL);
  1309. }
  1310. exit:
  1311. return 0;
  1312. }
  1313. static inline int
  1314. s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  1315. {
  1316. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  1317. return cpufreq_register_notifier(&port->freq_transition,
  1318. CPUFREQ_TRANSITION_NOTIFIER);
  1319. }
  1320. static inline void
  1321. s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  1322. {
  1323. cpufreq_unregister_notifier(&port->freq_transition,
  1324. CPUFREQ_TRANSITION_NOTIFIER);
  1325. }
  1326. #else
  1327. static inline int
  1328. s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  1329. {
  1330. return 0;
  1331. }
  1332. static inline void
  1333. s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  1334. {
  1335. }
  1336. #endif
  1337. /* s3c24xx_serial_init_port
  1338. *
  1339. * initialise a single serial port from the platform device given
  1340. */
  1341. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  1342. struct platform_device *platdev)
  1343. {
  1344. struct uart_port *port = &ourport->port;
  1345. struct s3c2410_uartcfg *cfg = ourport->cfg;
  1346. struct resource *res;
  1347. int ret;
  1348. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  1349. if (platdev == NULL)
  1350. return -ENODEV;
  1351. if (port->mapbase != 0)
  1352. return -EINVAL;
  1353. /* setup info for port */
  1354. port->dev = &platdev->dev;
  1355. /* Startup sequence is different for s3c64xx and higher SoC's */
  1356. if (s3c24xx_serial_has_interrupt_mask(port))
  1357. s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
  1358. port->uartclk = 1;
  1359. if (cfg->uart_flags & UPF_CONS_FLOW) {
  1360. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  1361. port->flags |= UPF_CONS_FLOW;
  1362. }
  1363. /* sort our the physical and virtual addresses for each UART */
  1364. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  1365. if (res == NULL) {
  1366. dev_err(port->dev, "failed to find memory resource for uart\n");
  1367. return -EINVAL;
  1368. }
  1369. dbg("resource %pR)\n", res);
  1370. port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
  1371. if (!port->membase) {
  1372. dev_err(port->dev, "failed to remap controller address\n");
  1373. return -EBUSY;
  1374. }
  1375. port->mapbase = res->start;
  1376. ret = platform_get_irq(platdev, 0);
  1377. if (ret < 0)
  1378. port->irq = 0;
  1379. else {
  1380. port->irq = ret;
  1381. ourport->rx_irq = ret;
  1382. ourport->tx_irq = ret + 1;
  1383. }
  1384. ret = platform_get_irq(platdev, 1);
  1385. if (ret > 0)
  1386. ourport->tx_irq = ret;
  1387. /*
  1388. * DMA is currently supported only on DT platforms, if DMA properties
  1389. * are specified.
  1390. */
  1391. if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
  1392. "dmas", NULL)) {
  1393. ourport->dma = devm_kzalloc(port->dev,
  1394. sizeof(*ourport->dma),
  1395. GFP_KERNEL);
  1396. if (!ourport->dma) {
  1397. ret = -ENOMEM;
  1398. goto err;
  1399. }
  1400. }
  1401. ourport->clk = clk_get(&platdev->dev, "uart");
  1402. if (IS_ERR(ourport->clk)) {
  1403. pr_err("%s: Controller clock not found\n",
  1404. dev_name(&platdev->dev));
  1405. ret = PTR_ERR(ourport->clk);
  1406. goto err;
  1407. }
  1408. ret = clk_prepare_enable(ourport->clk);
  1409. if (ret) {
  1410. pr_err("uart: clock failed to prepare+enable: %d\n", ret);
  1411. clk_put(ourport->clk);
  1412. goto err;
  1413. }
  1414. /* Keep all interrupts masked and cleared */
  1415. if (s3c24xx_serial_has_interrupt_mask(port)) {
  1416. wr_regl(port, S3C64XX_UINTM, 0xf);
  1417. wr_regl(port, S3C64XX_UINTP, 0xf);
  1418. wr_regl(port, S3C64XX_UINTSP, 0xf);
  1419. }
  1420. dbg("port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
  1421. &port->mapbase, port->membase, port->irq,
  1422. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  1423. /* reset the fifos (and setup the uart) */
  1424. s3c24xx_serial_resetport(port, cfg);
  1425. return 0;
  1426. err:
  1427. port->mapbase = 0;
  1428. return ret;
  1429. }
  1430. /* Device driver serial port probe */
  1431. static const struct of_device_id s3c24xx_uart_dt_match[];
  1432. static int probe_index;
  1433. static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
  1434. struct platform_device *pdev)
  1435. {
  1436. #ifdef CONFIG_OF
  1437. if (pdev->dev.of_node) {
  1438. const struct of_device_id *match;
  1439. match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
  1440. return (struct s3c24xx_serial_drv_data *)match->data;
  1441. }
  1442. #endif
  1443. return (struct s3c24xx_serial_drv_data *)
  1444. platform_get_device_id(pdev)->driver_data;
  1445. }
  1446. static int s3c24xx_serial_probe(struct platform_device *pdev)
  1447. {
  1448. struct device_node *np = pdev->dev.of_node;
  1449. struct s3c24xx_uart_port *ourport;
  1450. int index = probe_index;
  1451. int ret;
  1452. if (np) {
  1453. ret = of_alias_get_id(np, "serial");
  1454. if (ret >= 0)
  1455. index = ret;
  1456. }
  1457. dbg("s3c24xx_serial_probe(%p) %d\n", pdev, index);
  1458. if (index >= ARRAY_SIZE(s3c24xx_serial_ports)) {
  1459. dev_err(&pdev->dev, "serial%d out of range\n", index);
  1460. return -EINVAL;
  1461. }
  1462. ourport = &s3c24xx_serial_ports[index];
  1463. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  1464. if (!ourport->drv_data) {
  1465. dev_err(&pdev->dev, "could not find driver data\n");
  1466. return -ENODEV;
  1467. }
  1468. ourport->baudclk = ERR_PTR(-EINVAL);
  1469. ourport->info = ourport->drv_data->info;
  1470. ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
  1471. dev_get_platdata(&pdev->dev) :
  1472. ourport->drv_data->def_cfg;
  1473. if (np)
  1474. of_property_read_u32(np,
  1475. "samsung,uart-fifosize", &ourport->port.fifosize);
  1476. if (ourport->drv_data->fifosize[index])
  1477. ourport->port.fifosize = ourport->drv_data->fifosize[index];
  1478. else if (ourport->info->fifosize)
  1479. ourport->port.fifosize = ourport->info->fifosize;
  1480. /*
  1481. * DMA transfers must be aligned at least to cache line size,
  1482. * so find minimal transfer size suitable for DMA mode
  1483. */
  1484. ourport->min_dma_size = max_t(int, ourport->port.fifosize,
  1485. dma_get_cache_alignment());
  1486. dbg("%s: initialising port %p...\n", __func__, ourport);
  1487. ret = s3c24xx_serial_init_port(ourport, pdev);
  1488. if (ret < 0)
  1489. return ret;
  1490. if (!s3c24xx_uart_drv.state) {
  1491. ret = uart_register_driver(&s3c24xx_uart_drv);
  1492. if (ret < 0) {
  1493. pr_err("Failed to register Samsung UART driver\n");
  1494. return ret;
  1495. }
  1496. }
  1497. dbg("%s: adding port\n", __func__);
  1498. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  1499. platform_set_drvdata(pdev, &ourport->port);
  1500. /*
  1501. * Deactivate the clock enabled in s3c24xx_serial_init_port here,
  1502. * so that a potential re-enablement through the pm-callback overlaps
  1503. * and keeps the clock enabled in this case.
  1504. */
  1505. clk_disable_unprepare(ourport->clk);
  1506. ret = s3c24xx_serial_cpufreq_register(ourport);
  1507. if (ret < 0)
  1508. dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
  1509. probe_index++;
  1510. return 0;
  1511. }
  1512. static int s3c24xx_serial_remove(struct platform_device *dev)
  1513. {
  1514. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  1515. if (port) {
  1516. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  1517. uart_remove_one_port(&s3c24xx_uart_drv, port);
  1518. }
  1519. uart_unregister_driver(&s3c24xx_uart_drv);
  1520. return 0;
  1521. }
  1522. /* UART power management code */
  1523. #ifdef CONFIG_PM_SLEEP
  1524. static int s3c24xx_serial_suspend(struct device *dev)
  1525. {
  1526. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1527. if (port)
  1528. uart_suspend_port(&s3c24xx_uart_drv, port);
  1529. return 0;
  1530. }
  1531. static int s3c24xx_serial_resume(struct device *dev)
  1532. {
  1533. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1534. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1535. if (port) {
  1536. clk_prepare_enable(ourport->clk);
  1537. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1538. clk_disable_unprepare(ourport->clk);
  1539. uart_resume_port(&s3c24xx_uart_drv, port);
  1540. }
  1541. return 0;
  1542. }
  1543. static int s3c24xx_serial_resume_noirq(struct device *dev)
  1544. {
  1545. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1546. if (port) {
  1547. /* restore IRQ mask */
  1548. if (s3c24xx_serial_has_interrupt_mask(port)) {
  1549. unsigned int uintm = 0xf;
  1550. if (tx_enabled(port))
  1551. uintm &= ~S3C64XX_UINTM_TXD_MSK;
  1552. if (rx_enabled(port))
  1553. uintm &= ~S3C64XX_UINTM_RXD_MSK;
  1554. wr_regl(port, S3C64XX_UINTM, uintm);
  1555. }
  1556. }
  1557. return 0;
  1558. }
  1559. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1560. .suspend = s3c24xx_serial_suspend,
  1561. .resume = s3c24xx_serial_resume,
  1562. .resume_noirq = s3c24xx_serial_resume_noirq,
  1563. };
  1564. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1565. #else /* !CONFIG_PM_SLEEP */
  1566. #define SERIAL_SAMSUNG_PM_OPS NULL
  1567. #endif /* CONFIG_PM_SLEEP */
  1568. /* Console code */
  1569. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1570. static struct uart_port *cons_uart;
  1571. static int
  1572. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1573. {
  1574. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1575. unsigned long ufstat, utrstat;
  1576. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1577. /* fifo mode - check amount of data in fifo registers... */
  1578. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1579. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1580. }
  1581. /* in non-fifo mode, we go and use the tx buffer empty */
  1582. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1583. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1584. }
  1585. static bool
  1586. s3c24xx_port_configured(unsigned int ucon)
  1587. {
  1588. /* consider the serial port configured if the tx/rx mode set */
  1589. return (ucon & 0xf) != 0;
  1590. }
  1591. #ifdef CONFIG_CONSOLE_POLL
  1592. /*
  1593. * Console polling routines for writing and reading from the uart while
  1594. * in an interrupt or debug context.
  1595. */
  1596. static int s3c24xx_serial_get_poll_char(struct uart_port *port)
  1597. {
  1598. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1599. unsigned int ufstat;
  1600. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1601. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  1602. return NO_POLL_CHAR;
  1603. return rd_regb(port, S3C2410_URXH);
  1604. }
  1605. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1606. unsigned char c)
  1607. {
  1608. unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
  1609. unsigned int ucon = rd_regl(port, S3C2410_UCON);
  1610. /* not possible to xmit on unconfigured port */
  1611. if (!s3c24xx_port_configured(ucon))
  1612. return;
  1613. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1614. cpu_relax();
  1615. wr_regb(port, S3C2410_UTXH, c);
  1616. }
  1617. #endif /* CONFIG_CONSOLE_POLL */
  1618. static void
  1619. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1620. {
  1621. unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
  1622. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1623. cpu_relax();
  1624. wr_regb(port, S3C2410_UTXH, ch);
  1625. }
  1626. static void
  1627. s3c24xx_serial_console_write(struct console *co, const char *s,
  1628. unsigned int count)
  1629. {
  1630. unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
  1631. /* not possible to xmit on unconfigured port */
  1632. if (!s3c24xx_port_configured(ucon))
  1633. return;
  1634. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1635. }
  1636. static void __init
  1637. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1638. int *parity, int *bits)
  1639. {
  1640. struct clk *clk;
  1641. unsigned int ulcon;
  1642. unsigned int ucon;
  1643. unsigned int ubrdiv;
  1644. unsigned long rate;
  1645. unsigned int clk_sel;
  1646. char clk_name[MAX_CLK_NAME_LENGTH];
  1647. ulcon = rd_regl(port, S3C2410_ULCON);
  1648. ucon = rd_regl(port, S3C2410_UCON);
  1649. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1650. dbg("s3c24xx_serial_get_options: port=%p\n"
  1651. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1652. port, ulcon, ucon, ubrdiv);
  1653. if (s3c24xx_port_configured(ucon)) {
  1654. switch (ulcon & S3C2410_LCON_CSMASK) {
  1655. case S3C2410_LCON_CS5:
  1656. *bits = 5;
  1657. break;
  1658. case S3C2410_LCON_CS6:
  1659. *bits = 6;
  1660. break;
  1661. case S3C2410_LCON_CS7:
  1662. *bits = 7;
  1663. break;
  1664. case S3C2410_LCON_CS8:
  1665. default:
  1666. *bits = 8;
  1667. break;
  1668. }
  1669. switch (ulcon & S3C2410_LCON_PMASK) {
  1670. case S3C2410_LCON_PEVEN:
  1671. *parity = 'e';
  1672. break;
  1673. case S3C2410_LCON_PODD:
  1674. *parity = 'o';
  1675. break;
  1676. case S3C2410_LCON_PNONE:
  1677. default:
  1678. *parity = 'n';
  1679. }
  1680. /* now calculate the baud rate */
  1681. clk_sel = s3c24xx_serial_getsource(port);
  1682. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1683. clk = clk_get(port->dev, clk_name);
  1684. if (!IS_ERR(clk))
  1685. rate = clk_get_rate(clk);
  1686. else
  1687. rate = 1;
  1688. *baud = rate / (16 * (ubrdiv + 1));
  1689. dbg("calculated baud %d\n", *baud);
  1690. }
  1691. }
  1692. static int __init
  1693. s3c24xx_serial_console_setup(struct console *co, char *options)
  1694. {
  1695. struct uart_port *port;
  1696. int baud = 9600;
  1697. int bits = 8;
  1698. int parity = 'n';
  1699. int flow = 'n';
  1700. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1701. co, co->index, options);
  1702. /* is this a valid port */
  1703. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1704. co->index = 0;
  1705. port = &s3c24xx_serial_ports[co->index].port;
  1706. /* is the port configured? */
  1707. if (port->mapbase == 0x0)
  1708. return -ENODEV;
  1709. cons_uart = port;
  1710. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1711. /*
  1712. * Check whether an invalid uart number has been specified, and
  1713. * if so, search for the first available port that does have
  1714. * console support.
  1715. */
  1716. if (options)
  1717. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1718. else
  1719. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1720. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1721. return uart_set_options(port, co, baud, parity, bits, flow);
  1722. }
  1723. static struct console s3c24xx_serial_console = {
  1724. .name = S3C24XX_SERIAL_NAME,
  1725. .device = uart_console_device,
  1726. .flags = CON_PRINTBUFFER,
  1727. .index = -1,
  1728. .write = s3c24xx_serial_console_write,
  1729. .setup = s3c24xx_serial_console_setup,
  1730. .data = &s3c24xx_uart_drv,
  1731. };
  1732. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1733. #ifdef CONFIG_CPU_S3C2410
  1734. static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
  1735. .info = &(struct s3c24xx_uart_info) {
  1736. .name = "Samsung S3C2410 UART",
  1737. .type = PORT_S3C2410,
  1738. .fifosize = 16,
  1739. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1740. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1741. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1742. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1743. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1744. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1745. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1746. .num_clks = 2,
  1747. .clksel_mask = S3C2410_UCON_CLKMASK,
  1748. .clksel_shift = S3C2410_UCON_CLKSHIFT,
  1749. },
  1750. .def_cfg = &(struct s3c2410_uartcfg) {
  1751. .ucon = S3C2410_UCON_DEFAULT,
  1752. .ufcon = S3C2410_UFCON_DEFAULT,
  1753. },
  1754. };
  1755. #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
  1756. #else
  1757. #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1758. #endif
  1759. #ifdef CONFIG_CPU_S3C2412
  1760. static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
  1761. .info = &(struct s3c24xx_uart_info) {
  1762. .name = "Samsung S3C2412 UART",
  1763. .type = PORT_S3C2412,
  1764. .fifosize = 64,
  1765. .has_divslot = 1,
  1766. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1767. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1768. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1769. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1770. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1771. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1772. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1773. .num_clks = 4,
  1774. .clksel_mask = S3C2412_UCON_CLKMASK,
  1775. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1776. },
  1777. .def_cfg = &(struct s3c2410_uartcfg) {
  1778. .ucon = S3C2410_UCON_DEFAULT,
  1779. .ufcon = S3C2410_UFCON_DEFAULT,
  1780. },
  1781. };
  1782. #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
  1783. #else
  1784. #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1785. #endif
  1786. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
  1787. defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
  1788. static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
  1789. .info = &(struct s3c24xx_uart_info) {
  1790. .name = "Samsung S3C2440 UART",
  1791. .type = PORT_S3C2440,
  1792. .fifosize = 64,
  1793. .has_divslot = 1,
  1794. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1795. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1796. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1797. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1798. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1799. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1800. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1801. .num_clks = 4,
  1802. .clksel_mask = S3C2412_UCON_CLKMASK,
  1803. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1804. },
  1805. .def_cfg = &(struct s3c2410_uartcfg) {
  1806. .ucon = S3C2410_UCON_DEFAULT,
  1807. .ufcon = S3C2410_UFCON_DEFAULT,
  1808. },
  1809. };
  1810. #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
  1811. #else
  1812. #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1813. #endif
  1814. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
  1815. static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1816. .info = &(struct s3c24xx_uart_info) {
  1817. .name = "Samsung S3C6400 UART",
  1818. .type = PORT_S3C6400,
  1819. .fifosize = 64,
  1820. .has_divslot = 1,
  1821. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1822. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1823. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1824. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1825. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1826. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1827. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1828. .num_clks = 4,
  1829. .clksel_mask = S3C6400_UCON_CLKMASK,
  1830. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1831. },
  1832. .def_cfg = &(struct s3c2410_uartcfg) {
  1833. .ucon = S3C2410_UCON_DEFAULT,
  1834. .ufcon = S3C2410_UFCON_DEFAULT,
  1835. },
  1836. };
  1837. #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
  1838. #else
  1839. #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1840. #endif
  1841. #ifdef CONFIG_CPU_S5PV210
  1842. static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1843. .info = &(struct s3c24xx_uart_info) {
  1844. .name = "Samsung S5PV210 UART",
  1845. .type = PORT_S3C6400,
  1846. .has_divslot = 1,
  1847. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1848. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1849. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1850. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1851. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1852. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1853. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1854. .num_clks = 2,
  1855. .clksel_mask = S5PV210_UCON_CLKMASK,
  1856. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1857. },
  1858. .def_cfg = &(struct s3c2410_uartcfg) {
  1859. .ucon = S5PV210_UCON_DEFAULT,
  1860. .ufcon = S5PV210_UFCON_DEFAULT,
  1861. },
  1862. .fifosize = { 256, 64, 16, 16 },
  1863. };
  1864. #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
  1865. #else
  1866. #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1867. #endif
  1868. #if defined(CONFIG_ARCH_EXYNOS)
  1869. #define EXYNOS_COMMON_SERIAL_DRV_DATA \
  1870. .info = &(struct s3c24xx_uart_info) { \
  1871. .name = "Samsung Exynos UART", \
  1872. .type = PORT_S3C6400, \
  1873. .has_divslot = 1, \
  1874. .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
  1875. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
  1876. .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
  1877. .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
  1878. .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
  1879. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
  1880. .def_clk_sel = S3C2410_UCON_CLKSEL0, \
  1881. .num_clks = 1, \
  1882. .clksel_mask = 0, \
  1883. .clksel_shift = 0, \
  1884. }, \
  1885. .def_cfg = &(struct s3c2410_uartcfg) { \
  1886. .ucon = S5PV210_UCON_DEFAULT, \
  1887. .ufcon = S5PV210_UFCON_DEFAULT, \
  1888. .has_fracval = 1, \
  1889. } \
  1890. static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  1891. EXYNOS_COMMON_SERIAL_DRV_DATA,
  1892. .fifosize = { 256, 64, 16, 16 },
  1893. };
  1894. static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
  1895. EXYNOS_COMMON_SERIAL_DRV_DATA,
  1896. .fifosize = { 64, 256, 16, 256 },
  1897. };
  1898. #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
  1899. #define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data)
  1900. #else
  1901. #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1902. #define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1903. #endif
  1904. static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
  1905. {
  1906. .name = "s3c2410-uart",
  1907. .driver_data = S3C2410_SERIAL_DRV_DATA,
  1908. }, {
  1909. .name = "s3c2412-uart",
  1910. .driver_data = S3C2412_SERIAL_DRV_DATA,
  1911. }, {
  1912. .name = "s3c2440-uart",
  1913. .driver_data = S3C2440_SERIAL_DRV_DATA,
  1914. }, {
  1915. .name = "s3c6400-uart",
  1916. .driver_data = S3C6400_SERIAL_DRV_DATA,
  1917. }, {
  1918. .name = "s5pv210-uart",
  1919. .driver_data = S5PV210_SERIAL_DRV_DATA,
  1920. }, {
  1921. .name = "exynos4210-uart",
  1922. .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
  1923. }, {
  1924. .name = "exynos5433-uart",
  1925. .driver_data = EXYNOS5433_SERIAL_DRV_DATA,
  1926. },
  1927. { },
  1928. };
  1929. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  1930. #ifdef CONFIG_OF
  1931. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  1932. { .compatible = "samsung,s3c2410-uart",
  1933. .data = (void *)S3C2410_SERIAL_DRV_DATA },
  1934. { .compatible = "samsung,s3c2412-uart",
  1935. .data = (void *)S3C2412_SERIAL_DRV_DATA },
  1936. { .compatible = "samsung,s3c2440-uart",
  1937. .data = (void *)S3C2440_SERIAL_DRV_DATA },
  1938. { .compatible = "samsung,s3c6400-uart",
  1939. .data = (void *)S3C6400_SERIAL_DRV_DATA },
  1940. { .compatible = "samsung,s5pv210-uart",
  1941. .data = (void *)S5PV210_SERIAL_DRV_DATA },
  1942. { .compatible = "samsung,exynos4210-uart",
  1943. .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
  1944. { .compatible = "samsung,exynos5433-uart",
  1945. .data = (void *)EXYNOS5433_SERIAL_DRV_DATA },
  1946. {},
  1947. };
  1948. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  1949. #endif
  1950. static struct platform_driver samsung_serial_driver = {
  1951. .probe = s3c24xx_serial_probe,
  1952. .remove = s3c24xx_serial_remove,
  1953. .id_table = s3c24xx_serial_driver_ids,
  1954. .driver = {
  1955. .name = "samsung-uart",
  1956. .pm = SERIAL_SAMSUNG_PM_OPS,
  1957. .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
  1958. },
  1959. };
  1960. module_platform_driver(samsung_serial_driver);
  1961. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1962. /*
  1963. * Early console.
  1964. */
  1965. struct samsung_early_console_data {
  1966. u32 txfull_mask;
  1967. };
  1968. static void samsung_early_busyuart(struct uart_port *port)
  1969. {
  1970. while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
  1971. ;
  1972. }
  1973. static void samsung_early_busyuart_fifo(struct uart_port *port)
  1974. {
  1975. struct samsung_early_console_data *data = port->private_data;
  1976. while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
  1977. ;
  1978. }
  1979. static void samsung_early_putc(struct uart_port *port, int c)
  1980. {
  1981. if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
  1982. samsung_early_busyuart_fifo(port);
  1983. else
  1984. samsung_early_busyuart(port);
  1985. writeb(c, port->membase + S3C2410_UTXH);
  1986. }
  1987. static void samsung_early_write(struct console *con, const char *s, unsigned n)
  1988. {
  1989. struct earlycon_device *dev = con->data;
  1990. uart_console_write(&dev->port, s, n, samsung_early_putc);
  1991. }
  1992. static int __init samsung_early_console_setup(struct earlycon_device *device,
  1993. const char *opt)
  1994. {
  1995. if (!device->port.membase)
  1996. return -ENODEV;
  1997. device->con->write = samsung_early_write;
  1998. return 0;
  1999. }
  2000. /* S3C2410 */
  2001. static struct samsung_early_console_data s3c2410_early_console_data = {
  2002. .txfull_mask = S3C2410_UFSTAT_TXFULL,
  2003. };
  2004. static int __init s3c2410_early_console_setup(struct earlycon_device *device,
  2005. const char *opt)
  2006. {
  2007. device->port.private_data = &s3c2410_early_console_data;
  2008. return samsung_early_console_setup(device, opt);
  2009. }
  2010. OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
  2011. s3c2410_early_console_setup);
  2012. /* S3C2412, S3C2440, S3C64xx */
  2013. static struct samsung_early_console_data s3c2440_early_console_data = {
  2014. .txfull_mask = S3C2440_UFSTAT_TXFULL,
  2015. };
  2016. static int __init s3c2440_early_console_setup(struct earlycon_device *device,
  2017. const char *opt)
  2018. {
  2019. device->port.private_data = &s3c2440_early_console_data;
  2020. return samsung_early_console_setup(device, opt);
  2021. }
  2022. OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
  2023. s3c2440_early_console_setup);
  2024. OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
  2025. s3c2440_early_console_setup);
  2026. OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
  2027. s3c2440_early_console_setup);
  2028. /* S5PV210, EXYNOS */
  2029. static struct samsung_early_console_data s5pv210_early_console_data = {
  2030. .txfull_mask = S5PV210_UFSTAT_TXFULL,
  2031. };
  2032. static int __init s5pv210_early_console_setup(struct earlycon_device *device,
  2033. const char *opt)
  2034. {
  2035. device->port.private_data = &s5pv210_early_console_data;
  2036. return samsung_early_console_setup(device, opt);
  2037. }
  2038. OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
  2039. s5pv210_early_console_setup);
  2040. OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
  2041. s5pv210_early_console_setup);
  2042. #endif
  2043. MODULE_ALIAS("platform:samsung-uart");
  2044. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  2045. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  2046. MODULE_LICENSE("GPL v2");