samsung.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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 = 16;
  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. if (dma_get_cache_alignment() >= 16)
  700. dma->tx_conf.dst_maxburst = 16;
  701. else
  702. dma->tx_conf.dst_maxburst = 1;
  703. dma_cap_zero(mask);
  704. dma_cap_set(DMA_SLAVE, mask);
  705. dma->rx_chan = dma_request_slave_channel_compat(mask, dma->fn,
  706. dma->rx_param, p->port.dev, "rx");
  707. if (!dma->rx_chan)
  708. return -ENODEV;
  709. dmaengine_slave_config(dma->rx_chan, &dma->rx_conf);
  710. dma->tx_chan = dma_request_slave_channel_compat(mask, dma->fn,
  711. dma->tx_param, p->port.dev, "tx");
  712. if (!dma->tx_chan) {
  713. dma_release_channel(dma->rx_chan);
  714. return -ENODEV;
  715. }
  716. dmaengine_slave_config(dma->tx_chan, &dma->tx_conf);
  717. /* RX buffer */
  718. dma->rx_size = PAGE_SIZE;
  719. dma->rx_buf = kmalloc(dma->rx_size, GFP_KERNEL);
  720. if (!dma->rx_buf) {
  721. dma_release_channel(dma->rx_chan);
  722. dma_release_channel(dma->tx_chan);
  723. return -ENOMEM;
  724. }
  725. dma->rx_addr = dma_map_single(dma->rx_chan->device->dev, dma->rx_buf,
  726. dma->rx_size, DMA_FROM_DEVICE);
  727. spin_lock_irqsave(&p->port.lock, flags);
  728. /* TX buffer */
  729. dma->tx_addr = dma_map_single(dma->tx_chan->device->dev,
  730. p->port.state->xmit.buf,
  731. UART_XMIT_SIZE, DMA_TO_DEVICE);
  732. spin_unlock_irqrestore(&p->port.lock, flags);
  733. return 0;
  734. }
  735. static void s3c24xx_serial_release_dma(struct s3c24xx_uart_port *p)
  736. {
  737. struct s3c24xx_uart_dma *dma = p->dma;
  738. if (dma->rx_chan) {
  739. dmaengine_terminate_all(dma->rx_chan);
  740. dma_unmap_single(dma->rx_chan->device->dev, dma->rx_addr,
  741. dma->rx_size, DMA_FROM_DEVICE);
  742. kfree(dma->rx_buf);
  743. dma_release_channel(dma->rx_chan);
  744. dma->rx_chan = NULL;
  745. }
  746. if (dma->tx_chan) {
  747. dmaengine_terminate_all(dma->tx_chan);
  748. dma_unmap_single(dma->tx_chan->device->dev, dma->tx_addr,
  749. UART_XMIT_SIZE, DMA_TO_DEVICE);
  750. dma_release_channel(dma->tx_chan);
  751. dma->tx_chan = NULL;
  752. }
  753. }
  754. static void s3c24xx_serial_shutdown(struct uart_port *port)
  755. {
  756. struct s3c24xx_uart_port *ourport = to_ourport(port);
  757. if (ourport->tx_claimed) {
  758. if (!s3c24xx_serial_has_interrupt_mask(port))
  759. free_irq(ourport->tx_irq, ourport);
  760. tx_enabled(port) = 0;
  761. ourport->tx_claimed = 0;
  762. ourport->tx_mode = 0;
  763. }
  764. if (ourport->rx_claimed) {
  765. if (!s3c24xx_serial_has_interrupt_mask(port))
  766. free_irq(ourport->rx_irq, ourport);
  767. ourport->rx_claimed = 0;
  768. rx_enabled(port) = 0;
  769. }
  770. /* Clear pending interrupts and mask all interrupts */
  771. if (s3c24xx_serial_has_interrupt_mask(port)) {
  772. free_irq(port->irq, ourport);
  773. wr_regl(port, S3C64XX_UINTP, 0xf);
  774. wr_regl(port, S3C64XX_UINTM, 0xf);
  775. }
  776. if (ourport->dma)
  777. s3c24xx_serial_release_dma(ourport);
  778. ourport->tx_in_progress = 0;
  779. }
  780. static int s3c24xx_serial_startup(struct uart_port *port)
  781. {
  782. struct s3c24xx_uart_port *ourport = to_ourport(port);
  783. int ret;
  784. dbg("s3c24xx_serial_startup: port=%p (%08llx,%p)\n",
  785. port, (unsigned long long)port->mapbase, port->membase);
  786. rx_enabled(port) = 1;
  787. ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
  788. s3c24xx_serial_portname(port), ourport);
  789. if (ret != 0) {
  790. dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
  791. return ret;
  792. }
  793. ourport->rx_claimed = 1;
  794. dbg("requesting tx irq...\n");
  795. tx_enabled(port) = 1;
  796. ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
  797. s3c24xx_serial_portname(port), ourport);
  798. if (ret) {
  799. dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
  800. goto err;
  801. }
  802. ourport->tx_claimed = 1;
  803. dbg("s3c24xx_serial_startup ok\n");
  804. /* the port reset code should have done the correct
  805. * register setup for the port controls */
  806. return ret;
  807. err:
  808. s3c24xx_serial_shutdown(port);
  809. return ret;
  810. }
  811. static int s3c64xx_serial_startup(struct uart_port *port)
  812. {
  813. struct s3c24xx_uart_port *ourport = to_ourport(port);
  814. unsigned long flags;
  815. unsigned int ufcon;
  816. int ret;
  817. dbg("s3c64xx_serial_startup: port=%p (%08llx,%p)\n",
  818. port, (unsigned long long)port->mapbase, port->membase);
  819. wr_regl(port, S3C64XX_UINTM, 0xf);
  820. if (ourport->dma) {
  821. ret = s3c24xx_serial_request_dma(ourport);
  822. if (ret < 0) {
  823. dev_warn(port->dev, "DMA request failed\n");
  824. return ret;
  825. }
  826. }
  827. ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
  828. s3c24xx_serial_portname(port), ourport);
  829. if (ret) {
  830. dev_err(port->dev, "cannot get irq %d\n", port->irq);
  831. return ret;
  832. }
  833. /* For compatibility with s3c24xx Soc's */
  834. rx_enabled(port) = 1;
  835. ourport->rx_claimed = 1;
  836. tx_enabled(port) = 0;
  837. ourport->tx_claimed = 1;
  838. spin_lock_irqsave(&port->lock, flags);
  839. ufcon = rd_regl(port, S3C2410_UFCON);
  840. ufcon |= S3C2410_UFCON_RESETRX | S5PV210_UFCON_RXTRIG8;
  841. if (!uart_console(port))
  842. ufcon |= S3C2410_UFCON_RESETTX;
  843. wr_regl(port, S3C2410_UFCON, ufcon);
  844. enable_rx_pio(ourport);
  845. spin_unlock_irqrestore(&port->lock, flags);
  846. /* Enable Rx Interrupt */
  847. s3c24xx_clear_bit(port, S3C64XX_UINTM_RXD, S3C64XX_UINTM);
  848. dbg("s3c64xx_serial_startup ok\n");
  849. return ret;
  850. }
  851. /* power power management control */
  852. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  853. unsigned int old)
  854. {
  855. struct s3c24xx_uart_port *ourport = to_ourport(port);
  856. int timeout = 10000;
  857. ourport->pm_level = level;
  858. switch (level) {
  859. case 3:
  860. while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
  861. udelay(100);
  862. if (!IS_ERR(ourport->baudclk))
  863. clk_disable_unprepare(ourport->baudclk);
  864. clk_disable_unprepare(ourport->clk);
  865. break;
  866. case 0:
  867. clk_prepare_enable(ourport->clk);
  868. if (!IS_ERR(ourport->baudclk))
  869. clk_prepare_enable(ourport->baudclk);
  870. break;
  871. default:
  872. dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
  873. }
  874. }
  875. /* baud rate calculation
  876. *
  877. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  878. * of different sources, including the peripheral clock ("pclk") and an
  879. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  880. * with a programmable extra divisor.
  881. *
  882. * The following code goes through the clock sources, and calculates the
  883. * baud clocks (and the resultant actual baud rates) and then tries to
  884. * pick the closest one and select that.
  885. *
  886. */
  887. #define MAX_CLK_NAME_LENGTH 15
  888. static inline int s3c24xx_serial_getsource(struct uart_port *port)
  889. {
  890. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  891. unsigned int ucon;
  892. if (info->num_clks == 1)
  893. return 0;
  894. ucon = rd_regl(port, S3C2410_UCON);
  895. ucon &= info->clksel_mask;
  896. return ucon >> info->clksel_shift;
  897. }
  898. static void s3c24xx_serial_setsource(struct uart_port *port,
  899. unsigned int clk_sel)
  900. {
  901. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  902. unsigned int ucon;
  903. if (info->num_clks == 1)
  904. return;
  905. ucon = rd_regl(port, S3C2410_UCON);
  906. if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
  907. return;
  908. ucon &= ~info->clksel_mask;
  909. ucon |= clk_sel << info->clksel_shift;
  910. wr_regl(port, S3C2410_UCON, ucon);
  911. }
  912. static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
  913. unsigned int req_baud, struct clk **best_clk,
  914. unsigned int *clk_num)
  915. {
  916. struct s3c24xx_uart_info *info = ourport->info;
  917. struct clk *clk;
  918. unsigned long rate;
  919. unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
  920. char clkname[MAX_CLK_NAME_LENGTH];
  921. int calc_deviation, deviation = (1 << 30) - 1;
  922. clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
  923. ourport->info->def_clk_sel;
  924. for (cnt = 0; cnt < info->num_clks; cnt++) {
  925. if (!(clk_sel & (1 << cnt)))
  926. continue;
  927. sprintf(clkname, "clk_uart_baud%d", cnt);
  928. clk = clk_get(ourport->port.dev, clkname);
  929. if (IS_ERR(clk))
  930. continue;
  931. rate = clk_get_rate(clk);
  932. if (!rate)
  933. continue;
  934. if (ourport->info->has_divslot) {
  935. unsigned long div = rate / req_baud;
  936. /* The UDIVSLOT register on the newer UARTs allows us to
  937. * get a divisor adjustment of 1/16th on the baud clock.
  938. *
  939. * We don't keep the UDIVSLOT value (the 16ths we
  940. * calculated by not multiplying the baud by 16) as it
  941. * is easy enough to recalculate.
  942. */
  943. quot = div / 16;
  944. baud = rate / div;
  945. } else {
  946. quot = (rate + (8 * req_baud)) / (16 * req_baud);
  947. baud = rate / (quot * 16);
  948. }
  949. quot--;
  950. calc_deviation = req_baud - baud;
  951. if (calc_deviation < 0)
  952. calc_deviation = -calc_deviation;
  953. if (calc_deviation < deviation) {
  954. *best_clk = clk;
  955. best_quot = quot;
  956. *clk_num = cnt;
  957. deviation = calc_deviation;
  958. }
  959. }
  960. return best_quot;
  961. }
  962. /* udivslot_table[]
  963. *
  964. * This table takes the fractional value of the baud divisor and gives
  965. * the recommended setting for the UDIVSLOT register.
  966. */
  967. static u16 udivslot_table[16] = {
  968. [0] = 0x0000,
  969. [1] = 0x0080,
  970. [2] = 0x0808,
  971. [3] = 0x0888,
  972. [4] = 0x2222,
  973. [5] = 0x4924,
  974. [6] = 0x4A52,
  975. [7] = 0x54AA,
  976. [8] = 0x5555,
  977. [9] = 0xD555,
  978. [10] = 0xD5D5,
  979. [11] = 0xDDD5,
  980. [12] = 0xDDDD,
  981. [13] = 0xDFDD,
  982. [14] = 0xDFDF,
  983. [15] = 0xFFDF,
  984. };
  985. static void s3c24xx_serial_set_termios(struct uart_port *port,
  986. struct ktermios *termios,
  987. struct ktermios *old)
  988. {
  989. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  990. struct s3c24xx_uart_port *ourport = to_ourport(port);
  991. struct clk *clk = ERR_PTR(-EINVAL);
  992. unsigned long flags;
  993. unsigned int baud, quot, clk_sel = 0;
  994. unsigned int ulcon;
  995. unsigned int umcon;
  996. unsigned int udivslot = 0;
  997. /*
  998. * We don't support modem control lines.
  999. */
  1000. termios->c_cflag &= ~(HUPCL | CMSPAR);
  1001. termios->c_cflag |= CLOCAL;
  1002. /*
  1003. * Ask the core to calculate the divisor for us.
  1004. */
  1005. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  1006. quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
  1007. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  1008. quot = port->custom_divisor;
  1009. if (IS_ERR(clk))
  1010. return;
  1011. /* check to see if we need to change clock source */
  1012. if (ourport->baudclk != clk) {
  1013. clk_prepare_enable(clk);
  1014. s3c24xx_serial_setsource(port, clk_sel);
  1015. if (!IS_ERR(ourport->baudclk)) {
  1016. clk_disable_unprepare(ourport->baudclk);
  1017. ourport->baudclk = ERR_PTR(-EINVAL);
  1018. }
  1019. ourport->baudclk = clk;
  1020. ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
  1021. }
  1022. if (ourport->info->has_divslot) {
  1023. unsigned int div = ourport->baudclk_rate / baud;
  1024. if (cfg->has_fracval) {
  1025. udivslot = (div & 15);
  1026. dbg("fracval = %04x\n", udivslot);
  1027. } else {
  1028. udivslot = udivslot_table[div & 15];
  1029. dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
  1030. }
  1031. }
  1032. switch (termios->c_cflag & CSIZE) {
  1033. case CS5:
  1034. dbg("config: 5bits/char\n");
  1035. ulcon = S3C2410_LCON_CS5;
  1036. break;
  1037. case CS6:
  1038. dbg("config: 6bits/char\n");
  1039. ulcon = S3C2410_LCON_CS6;
  1040. break;
  1041. case CS7:
  1042. dbg("config: 7bits/char\n");
  1043. ulcon = S3C2410_LCON_CS7;
  1044. break;
  1045. case CS8:
  1046. default:
  1047. dbg("config: 8bits/char\n");
  1048. ulcon = S3C2410_LCON_CS8;
  1049. break;
  1050. }
  1051. /* preserve original lcon IR settings */
  1052. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  1053. if (termios->c_cflag & CSTOPB)
  1054. ulcon |= S3C2410_LCON_STOPB;
  1055. if (termios->c_cflag & PARENB) {
  1056. if (termios->c_cflag & PARODD)
  1057. ulcon |= S3C2410_LCON_PODD;
  1058. else
  1059. ulcon |= S3C2410_LCON_PEVEN;
  1060. } else {
  1061. ulcon |= S3C2410_LCON_PNONE;
  1062. }
  1063. spin_lock_irqsave(&port->lock, flags);
  1064. dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  1065. ulcon, quot, udivslot);
  1066. wr_regl(port, S3C2410_ULCON, ulcon);
  1067. wr_regl(port, S3C2410_UBRDIV, quot);
  1068. umcon = rd_regl(port, S3C2410_UMCON);
  1069. if (termios->c_cflag & CRTSCTS) {
  1070. umcon |= S3C2410_UMCOM_AFC;
  1071. /* Disable RTS when RX FIFO contains 63 bytes */
  1072. umcon &= ~S3C2412_UMCON_AFC_8;
  1073. } else {
  1074. umcon &= ~S3C2410_UMCOM_AFC;
  1075. }
  1076. wr_regl(port, S3C2410_UMCON, umcon);
  1077. if (ourport->info->has_divslot)
  1078. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  1079. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  1080. rd_regl(port, S3C2410_ULCON),
  1081. rd_regl(port, S3C2410_UCON),
  1082. rd_regl(port, S3C2410_UFCON));
  1083. /*
  1084. * Update the per-port timeout.
  1085. */
  1086. uart_update_timeout(port, termios->c_cflag, baud);
  1087. /*
  1088. * Which character status flags are we interested in?
  1089. */
  1090. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  1091. if (termios->c_iflag & INPCK)
  1092. port->read_status_mask |= S3C2410_UERSTAT_FRAME |
  1093. S3C2410_UERSTAT_PARITY;
  1094. /*
  1095. * Which character status flags should we ignore?
  1096. */
  1097. port->ignore_status_mask = 0;
  1098. if (termios->c_iflag & IGNPAR)
  1099. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  1100. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  1101. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  1102. /*
  1103. * Ignore all characters if CREAD is not set.
  1104. */
  1105. if ((termios->c_cflag & CREAD) == 0)
  1106. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  1107. spin_unlock_irqrestore(&port->lock, flags);
  1108. }
  1109. static const char *s3c24xx_serial_type(struct uart_port *port)
  1110. {
  1111. switch (port->type) {
  1112. case PORT_S3C2410:
  1113. return "S3C2410";
  1114. case PORT_S3C2440:
  1115. return "S3C2440";
  1116. case PORT_S3C2412:
  1117. return "S3C2412";
  1118. case PORT_S3C6400:
  1119. return "S3C6400/10";
  1120. default:
  1121. return NULL;
  1122. }
  1123. }
  1124. #define MAP_SIZE (0x100)
  1125. static void s3c24xx_serial_release_port(struct uart_port *port)
  1126. {
  1127. release_mem_region(port->mapbase, MAP_SIZE);
  1128. }
  1129. static int s3c24xx_serial_request_port(struct uart_port *port)
  1130. {
  1131. const char *name = s3c24xx_serial_portname(port);
  1132. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  1133. }
  1134. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  1135. {
  1136. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1137. if (flags & UART_CONFIG_TYPE &&
  1138. s3c24xx_serial_request_port(port) == 0)
  1139. port->type = info->type;
  1140. }
  1141. /*
  1142. * verify the new serial_struct (for TIOCSSERIAL).
  1143. */
  1144. static int
  1145. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  1146. {
  1147. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1148. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  1149. return -EINVAL;
  1150. return 0;
  1151. }
  1152. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1153. static struct console s3c24xx_serial_console;
  1154. static int __init s3c24xx_serial_console_init(void)
  1155. {
  1156. register_console(&s3c24xx_serial_console);
  1157. return 0;
  1158. }
  1159. console_initcall(s3c24xx_serial_console_init);
  1160. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  1161. #else
  1162. #define S3C24XX_SERIAL_CONSOLE NULL
  1163. #endif
  1164. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1165. static int s3c24xx_serial_get_poll_char(struct uart_port *port);
  1166. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1167. unsigned char c);
  1168. #endif
  1169. static struct uart_ops s3c24xx_serial_ops = {
  1170. .pm = s3c24xx_serial_pm,
  1171. .tx_empty = s3c24xx_serial_tx_empty,
  1172. .get_mctrl = s3c24xx_serial_get_mctrl,
  1173. .set_mctrl = s3c24xx_serial_set_mctrl,
  1174. .stop_tx = s3c24xx_serial_stop_tx,
  1175. .start_tx = s3c24xx_serial_start_tx,
  1176. .stop_rx = s3c24xx_serial_stop_rx,
  1177. .break_ctl = s3c24xx_serial_break_ctl,
  1178. .startup = s3c24xx_serial_startup,
  1179. .shutdown = s3c24xx_serial_shutdown,
  1180. .set_termios = s3c24xx_serial_set_termios,
  1181. .type = s3c24xx_serial_type,
  1182. .release_port = s3c24xx_serial_release_port,
  1183. .request_port = s3c24xx_serial_request_port,
  1184. .config_port = s3c24xx_serial_config_port,
  1185. .verify_port = s3c24xx_serial_verify_port,
  1186. #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
  1187. .poll_get_char = s3c24xx_serial_get_poll_char,
  1188. .poll_put_char = s3c24xx_serial_put_poll_char,
  1189. #endif
  1190. };
  1191. static struct uart_driver s3c24xx_uart_drv = {
  1192. .owner = THIS_MODULE,
  1193. .driver_name = "s3c2410_serial",
  1194. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  1195. .cons = S3C24XX_SERIAL_CONSOLE,
  1196. .dev_name = S3C24XX_SERIAL_NAME,
  1197. .major = S3C24XX_SERIAL_MAJOR,
  1198. .minor = S3C24XX_SERIAL_MINOR,
  1199. };
  1200. #define __PORT_LOCK_UNLOCKED(i) \
  1201. __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[i].port.lock)
  1202. static struct s3c24xx_uart_port
  1203. s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  1204. [0] = {
  1205. .port = {
  1206. .lock = __PORT_LOCK_UNLOCKED(0),
  1207. .iotype = UPIO_MEM,
  1208. .uartclk = 0,
  1209. .fifosize = 16,
  1210. .ops = &s3c24xx_serial_ops,
  1211. .flags = UPF_BOOT_AUTOCONF,
  1212. .line = 0,
  1213. }
  1214. },
  1215. [1] = {
  1216. .port = {
  1217. .lock = __PORT_LOCK_UNLOCKED(1),
  1218. .iotype = UPIO_MEM,
  1219. .uartclk = 0,
  1220. .fifosize = 16,
  1221. .ops = &s3c24xx_serial_ops,
  1222. .flags = UPF_BOOT_AUTOCONF,
  1223. .line = 1,
  1224. }
  1225. },
  1226. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  1227. [2] = {
  1228. .port = {
  1229. .lock = __PORT_LOCK_UNLOCKED(2),
  1230. .iotype = UPIO_MEM,
  1231. .uartclk = 0,
  1232. .fifosize = 16,
  1233. .ops = &s3c24xx_serial_ops,
  1234. .flags = UPF_BOOT_AUTOCONF,
  1235. .line = 2,
  1236. }
  1237. },
  1238. #endif
  1239. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  1240. [3] = {
  1241. .port = {
  1242. .lock = __PORT_LOCK_UNLOCKED(3),
  1243. .iotype = UPIO_MEM,
  1244. .uartclk = 0,
  1245. .fifosize = 16,
  1246. .ops = &s3c24xx_serial_ops,
  1247. .flags = UPF_BOOT_AUTOCONF,
  1248. .line = 3,
  1249. }
  1250. }
  1251. #endif
  1252. };
  1253. #undef __PORT_LOCK_UNLOCKED
  1254. /* s3c24xx_serial_resetport
  1255. *
  1256. * reset the fifos and other the settings.
  1257. */
  1258. static void s3c24xx_serial_resetport(struct uart_port *port,
  1259. struct s3c2410_uartcfg *cfg)
  1260. {
  1261. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1262. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  1263. unsigned int ucon_mask;
  1264. ucon_mask = info->clksel_mask;
  1265. if (info->type == PORT_S3C2440)
  1266. ucon_mask |= S3C2440_UCON0_DIVMASK;
  1267. ucon &= ucon_mask;
  1268. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  1269. /* reset both fifos */
  1270. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  1271. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  1272. /* some delay is required after fifo reset */
  1273. udelay(1);
  1274. }
  1275. #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
  1276. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  1277. unsigned long val, void *data)
  1278. {
  1279. struct s3c24xx_uart_port *port;
  1280. struct uart_port *uport;
  1281. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  1282. uport = &port->port;
  1283. /* check to see if port is enabled */
  1284. if (port->pm_level != 0)
  1285. return 0;
  1286. /* try and work out if the baudrate is changing, we can detect
  1287. * a change in rate, but we do not have support for detecting
  1288. * a disturbance in the clock-rate over the change.
  1289. */
  1290. if (IS_ERR(port->baudclk))
  1291. goto exit;
  1292. if (port->baudclk_rate == clk_get_rate(port->baudclk))
  1293. goto exit;
  1294. if (val == CPUFREQ_PRECHANGE) {
  1295. /* we should really shut the port down whilst the
  1296. * frequency change is in progress. */
  1297. } else if (val == CPUFREQ_POSTCHANGE) {
  1298. struct ktermios *termios;
  1299. struct tty_struct *tty;
  1300. if (uport->state == NULL)
  1301. goto exit;
  1302. tty = uport->state->port.tty;
  1303. if (tty == NULL)
  1304. goto exit;
  1305. termios = &tty->termios;
  1306. if (termios == NULL) {
  1307. dev_warn(uport->dev, "%s: no termios?\n", __func__);
  1308. goto exit;
  1309. }
  1310. s3c24xx_serial_set_termios(uport, termios, NULL);
  1311. }
  1312. exit:
  1313. return 0;
  1314. }
  1315. static inline int
  1316. s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  1317. {
  1318. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  1319. return cpufreq_register_notifier(&port->freq_transition,
  1320. CPUFREQ_TRANSITION_NOTIFIER);
  1321. }
  1322. static inline void
  1323. s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  1324. {
  1325. cpufreq_unregister_notifier(&port->freq_transition,
  1326. CPUFREQ_TRANSITION_NOTIFIER);
  1327. }
  1328. #else
  1329. static inline int
  1330. s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  1331. {
  1332. return 0;
  1333. }
  1334. static inline void
  1335. s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  1336. {
  1337. }
  1338. #endif
  1339. /* s3c24xx_serial_init_port
  1340. *
  1341. * initialise a single serial port from the platform device given
  1342. */
  1343. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  1344. struct platform_device *platdev)
  1345. {
  1346. struct uart_port *port = &ourport->port;
  1347. struct s3c2410_uartcfg *cfg = ourport->cfg;
  1348. struct resource *res;
  1349. int ret;
  1350. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  1351. if (platdev == NULL)
  1352. return -ENODEV;
  1353. if (port->mapbase != 0)
  1354. return -EINVAL;
  1355. /* setup info for port */
  1356. port->dev = &platdev->dev;
  1357. /* Startup sequence is different for s3c64xx and higher SoC's */
  1358. if (s3c24xx_serial_has_interrupt_mask(port))
  1359. s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
  1360. port->uartclk = 1;
  1361. if (cfg->uart_flags & UPF_CONS_FLOW) {
  1362. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  1363. port->flags |= UPF_CONS_FLOW;
  1364. }
  1365. /* sort our the physical and virtual addresses for each UART */
  1366. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  1367. if (res == NULL) {
  1368. dev_err(port->dev, "failed to find memory resource for uart\n");
  1369. return -EINVAL;
  1370. }
  1371. dbg("resource %pR)\n", res);
  1372. port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
  1373. if (!port->membase) {
  1374. dev_err(port->dev, "failed to remap controller address\n");
  1375. return -EBUSY;
  1376. }
  1377. port->mapbase = res->start;
  1378. ret = platform_get_irq(platdev, 0);
  1379. if (ret < 0)
  1380. port->irq = 0;
  1381. else {
  1382. port->irq = ret;
  1383. ourport->rx_irq = ret;
  1384. ourport->tx_irq = ret + 1;
  1385. }
  1386. ret = platform_get_irq(platdev, 1);
  1387. if (ret > 0)
  1388. ourport->tx_irq = ret;
  1389. /*
  1390. * DMA is currently supported only on DT platforms, if DMA properties
  1391. * are specified.
  1392. */
  1393. if (platdev->dev.of_node && of_find_property(platdev->dev.of_node,
  1394. "dmas", NULL)) {
  1395. ourport->dma = devm_kzalloc(port->dev,
  1396. sizeof(*ourport->dma),
  1397. GFP_KERNEL);
  1398. if (!ourport->dma) {
  1399. ret = -ENOMEM;
  1400. goto err;
  1401. }
  1402. }
  1403. ourport->clk = clk_get(&platdev->dev, "uart");
  1404. if (IS_ERR(ourport->clk)) {
  1405. pr_err("%s: Controller clock not found\n",
  1406. dev_name(&platdev->dev));
  1407. ret = PTR_ERR(ourport->clk);
  1408. goto err;
  1409. }
  1410. ret = clk_prepare_enable(ourport->clk);
  1411. if (ret) {
  1412. pr_err("uart: clock failed to prepare+enable: %d\n", ret);
  1413. clk_put(ourport->clk);
  1414. goto err;
  1415. }
  1416. /* Keep all interrupts masked and cleared */
  1417. if (s3c24xx_serial_has_interrupt_mask(port)) {
  1418. wr_regl(port, S3C64XX_UINTM, 0xf);
  1419. wr_regl(port, S3C64XX_UINTP, 0xf);
  1420. wr_regl(port, S3C64XX_UINTSP, 0xf);
  1421. }
  1422. dbg("port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
  1423. &port->mapbase, port->membase, port->irq,
  1424. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  1425. /* reset the fifos (and setup the uart) */
  1426. s3c24xx_serial_resetport(port, cfg);
  1427. return 0;
  1428. err:
  1429. port->mapbase = 0;
  1430. return ret;
  1431. }
  1432. /* Device driver serial port probe */
  1433. static const struct of_device_id s3c24xx_uart_dt_match[];
  1434. static int probe_index;
  1435. static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
  1436. struct platform_device *pdev)
  1437. {
  1438. #ifdef CONFIG_OF
  1439. if (pdev->dev.of_node) {
  1440. const struct of_device_id *match;
  1441. match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
  1442. return (struct s3c24xx_serial_drv_data *)match->data;
  1443. }
  1444. #endif
  1445. return (struct s3c24xx_serial_drv_data *)
  1446. platform_get_device_id(pdev)->driver_data;
  1447. }
  1448. static int s3c24xx_serial_probe(struct platform_device *pdev)
  1449. {
  1450. struct device_node *np = pdev->dev.of_node;
  1451. struct s3c24xx_uart_port *ourport;
  1452. int index = probe_index;
  1453. int ret;
  1454. if (np) {
  1455. ret = of_alias_get_id(np, "serial");
  1456. if (ret >= 0)
  1457. index = ret;
  1458. }
  1459. dbg("s3c24xx_serial_probe(%p) %d\n", pdev, index);
  1460. ourport = &s3c24xx_serial_ports[index];
  1461. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  1462. if (!ourport->drv_data) {
  1463. dev_err(&pdev->dev, "could not find driver data\n");
  1464. return -ENODEV;
  1465. }
  1466. ourport->baudclk = ERR_PTR(-EINVAL);
  1467. ourport->info = ourport->drv_data->info;
  1468. ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
  1469. dev_get_platdata(&pdev->dev) :
  1470. ourport->drv_data->def_cfg;
  1471. if (np)
  1472. of_property_read_u32(np,
  1473. "samsung,uart-fifosize", &ourport->port.fifosize);
  1474. if (ourport->drv_data->fifosize[index])
  1475. ourport->port.fifosize = ourport->drv_data->fifosize[index];
  1476. else if (ourport->info->fifosize)
  1477. ourport->port.fifosize = ourport->info->fifosize;
  1478. /*
  1479. * DMA transfers must be aligned at least to cache line size,
  1480. * so find minimal transfer size suitable for DMA mode
  1481. */
  1482. ourport->min_dma_size = max_t(int, ourport->port.fifosize,
  1483. dma_get_cache_alignment());
  1484. dbg("%s: initialising port %p...\n", __func__, ourport);
  1485. ret = s3c24xx_serial_init_port(ourport, pdev);
  1486. if (ret < 0)
  1487. return ret;
  1488. if (!s3c24xx_uart_drv.state) {
  1489. ret = uart_register_driver(&s3c24xx_uart_drv);
  1490. if (ret < 0) {
  1491. pr_err("Failed to register Samsung UART driver\n");
  1492. return ret;
  1493. }
  1494. }
  1495. dbg("%s: adding port\n", __func__);
  1496. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  1497. platform_set_drvdata(pdev, &ourport->port);
  1498. /*
  1499. * Deactivate the clock enabled in s3c24xx_serial_init_port here,
  1500. * so that a potential re-enablement through the pm-callback overlaps
  1501. * and keeps the clock enabled in this case.
  1502. */
  1503. clk_disable_unprepare(ourport->clk);
  1504. ret = s3c24xx_serial_cpufreq_register(ourport);
  1505. if (ret < 0)
  1506. dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
  1507. probe_index++;
  1508. return 0;
  1509. }
  1510. static int s3c24xx_serial_remove(struct platform_device *dev)
  1511. {
  1512. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  1513. if (port) {
  1514. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  1515. uart_remove_one_port(&s3c24xx_uart_drv, port);
  1516. }
  1517. uart_unregister_driver(&s3c24xx_uart_drv);
  1518. return 0;
  1519. }
  1520. /* UART power management code */
  1521. #ifdef CONFIG_PM_SLEEP
  1522. static int s3c24xx_serial_suspend(struct device *dev)
  1523. {
  1524. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1525. if (port)
  1526. uart_suspend_port(&s3c24xx_uart_drv, port);
  1527. return 0;
  1528. }
  1529. static int s3c24xx_serial_resume(struct device *dev)
  1530. {
  1531. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1532. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1533. if (port) {
  1534. clk_prepare_enable(ourport->clk);
  1535. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1536. clk_disable_unprepare(ourport->clk);
  1537. uart_resume_port(&s3c24xx_uart_drv, port);
  1538. }
  1539. return 0;
  1540. }
  1541. static int s3c24xx_serial_resume_noirq(struct device *dev)
  1542. {
  1543. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1544. if (port) {
  1545. /* restore IRQ mask */
  1546. if (s3c24xx_serial_has_interrupt_mask(port)) {
  1547. unsigned int uintm = 0xf;
  1548. if (tx_enabled(port))
  1549. uintm &= ~S3C64XX_UINTM_TXD_MSK;
  1550. if (rx_enabled(port))
  1551. uintm &= ~S3C64XX_UINTM_RXD_MSK;
  1552. wr_regl(port, S3C64XX_UINTM, uintm);
  1553. }
  1554. }
  1555. return 0;
  1556. }
  1557. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1558. .suspend = s3c24xx_serial_suspend,
  1559. .resume = s3c24xx_serial_resume,
  1560. .resume_noirq = s3c24xx_serial_resume_noirq,
  1561. };
  1562. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1563. #else /* !CONFIG_PM_SLEEP */
  1564. #define SERIAL_SAMSUNG_PM_OPS NULL
  1565. #endif /* CONFIG_PM_SLEEP */
  1566. /* Console code */
  1567. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1568. static struct uart_port *cons_uart;
  1569. static int
  1570. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1571. {
  1572. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1573. unsigned long ufstat, utrstat;
  1574. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1575. /* fifo mode - check amount of data in fifo registers... */
  1576. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1577. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1578. }
  1579. /* in non-fifo mode, we go and use the tx buffer empty */
  1580. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1581. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1582. }
  1583. static bool
  1584. s3c24xx_port_configured(unsigned int ucon)
  1585. {
  1586. /* consider the serial port configured if the tx/rx mode set */
  1587. return (ucon & 0xf) != 0;
  1588. }
  1589. #ifdef CONFIG_CONSOLE_POLL
  1590. /*
  1591. * Console polling routines for writing and reading from the uart while
  1592. * in an interrupt or debug context.
  1593. */
  1594. static int s3c24xx_serial_get_poll_char(struct uart_port *port)
  1595. {
  1596. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1597. unsigned int ufstat;
  1598. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1599. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  1600. return NO_POLL_CHAR;
  1601. return rd_regb(port, S3C2410_URXH);
  1602. }
  1603. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1604. unsigned char c)
  1605. {
  1606. unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
  1607. unsigned int ucon = rd_regl(port, S3C2410_UCON);
  1608. /* not possible to xmit on unconfigured port */
  1609. if (!s3c24xx_port_configured(ucon))
  1610. return;
  1611. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1612. cpu_relax();
  1613. wr_regb(port, S3C2410_UTXH, c);
  1614. }
  1615. #endif /* CONFIG_CONSOLE_POLL */
  1616. static void
  1617. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1618. {
  1619. unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
  1620. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1621. cpu_relax();
  1622. wr_regb(port, S3C2410_UTXH, ch);
  1623. }
  1624. static void
  1625. s3c24xx_serial_console_write(struct console *co, const char *s,
  1626. unsigned int count)
  1627. {
  1628. unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
  1629. /* not possible to xmit on unconfigured port */
  1630. if (!s3c24xx_port_configured(ucon))
  1631. return;
  1632. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1633. }
  1634. static void __init
  1635. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1636. int *parity, int *bits)
  1637. {
  1638. struct clk *clk;
  1639. unsigned int ulcon;
  1640. unsigned int ucon;
  1641. unsigned int ubrdiv;
  1642. unsigned long rate;
  1643. unsigned int clk_sel;
  1644. char clk_name[MAX_CLK_NAME_LENGTH];
  1645. ulcon = rd_regl(port, S3C2410_ULCON);
  1646. ucon = rd_regl(port, S3C2410_UCON);
  1647. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1648. dbg("s3c24xx_serial_get_options: port=%p\n"
  1649. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1650. port, ulcon, ucon, ubrdiv);
  1651. if (s3c24xx_port_configured(ucon)) {
  1652. switch (ulcon & S3C2410_LCON_CSMASK) {
  1653. case S3C2410_LCON_CS5:
  1654. *bits = 5;
  1655. break;
  1656. case S3C2410_LCON_CS6:
  1657. *bits = 6;
  1658. break;
  1659. case S3C2410_LCON_CS7:
  1660. *bits = 7;
  1661. break;
  1662. case S3C2410_LCON_CS8:
  1663. default:
  1664. *bits = 8;
  1665. break;
  1666. }
  1667. switch (ulcon & S3C2410_LCON_PMASK) {
  1668. case S3C2410_LCON_PEVEN:
  1669. *parity = 'e';
  1670. break;
  1671. case S3C2410_LCON_PODD:
  1672. *parity = 'o';
  1673. break;
  1674. case S3C2410_LCON_PNONE:
  1675. default:
  1676. *parity = 'n';
  1677. }
  1678. /* now calculate the baud rate */
  1679. clk_sel = s3c24xx_serial_getsource(port);
  1680. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1681. clk = clk_get(port->dev, clk_name);
  1682. if (!IS_ERR(clk))
  1683. rate = clk_get_rate(clk);
  1684. else
  1685. rate = 1;
  1686. *baud = rate / (16 * (ubrdiv + 1));
  1687. dbg("calculated baud %d\n", *baud);
  1688. }
  1689. }
  1690. static int __init
  1691. s3c24xx_serial_console_setup(struct console *co, char *options)
  1692. {
  1693. struct uart_port *port;
  1694. int baud = 9600;
  1695. int bits = 8;
  1696. int parity = 'n';
  1697. int flow = 'n';
  1698. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1699. co, co->index, options);
  1700. /* is this a valid port */
  1701. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1702. co->index = 0;
  1703. port = &s3c24xx_serial_ports[co->index].port;
  1704. /* is the port configured? */
  1705. if (port->mapbase == 0x0)
  1706. return -ENODEV;
  1707. cons_uart = port;
  1708. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1709. /*
  1710. * Check whether an invalid uart number has been specified, and
  1711. * if so, search for the first available port that does have
  1712. * console support.
  1713. */
  1714. if (options)
  1715. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1716. else
  1717. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1718. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1719. return uart_set_options(port, co, baud, parity, bits, flow);
  1720. }
  1721. static struct console s3c24xx_serial_console = {
  1722. .name = S3C24XX_SERIAL_NAME,
  1723. .device = uart_console_device,
  1724. .flags = CON_PRINTBUFFER,
  1725. .index = -1,
  1726. .write = s3c24xx_serial_console_write,
  1727. .setup = s3c24xx_serial_console_setup,
  1728. .data = &s3c24xx_uart_drv,
  1729. };
  1730. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1731. #ifdef CONFIG_CPU_S3C2410
  1732. static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
  1733. .info = &(struct s3c24xx_uart_info) {
  1734. .name = "Samsung S3C2410 UART",
  1735. .type = PORT_S3C2410,
  1736. .fifosize = 16,
  1737. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1738. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1739. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1740. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1741. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1742. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1743. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1744. .num_clks = 2,
  1745. .clksel_mask = S3C2410_UCON_CLKMASK,
  1746. .clksel_shift = S3C2410_UCON_CLKSHIFT,
  1747. },
  1748. .def_cfg = &(struct s3c2410_uartcfg) {
  1749. .ucon = S3C2410_UCON_DEFAULT,
  1750. .ufcon = S3C2410_UFCON_DEFAULT,
  1751. },
  1752. };
  1753. #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
  1754. #else
  1755. #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1756. #endif
  1757. #ifdef CONFIG_CPU_S3C2412
  1758. static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
  1759. .info = &(struct s3c24xx_uart_info) {
  1760. .name = "Samsung S3C2412 UART",
  1761. .type = PORT_S3C2412,
  1762. .fifosize = 64,
  1763. .has_divslot = 1,
  1764. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1765. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1766. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1767. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1768. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1769. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1770. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1771. .num_clks = 4,
  1772. .clksel_mask = S3C2412_UCON_CLKMASK,
  1773. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1774. },
  1775. .def_cfg = &(struct s3c2410_uartcfg) {
  1776. .ucon = S3C2410_UCON_DEFAULT,
  1777. .ufcon = S3C2410_UFCON_DEFAULT,
  1778. },
  1779. };
  1780. #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
  1781. #else
  1782. #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1783. #endif
  1784. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
  1785. defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
  1786. static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
  1787. .info = &(struct s3c24xx_uart_info) {
  1788. .name = "Samsung S3C2440 UART",
  1789. .type = PORT_S3C2440,
  1790. .fifosize = 64,
  1791. .has_divslot = 1,
  1792. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1793. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1794. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1795. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1796. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1797. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1798. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1799. .num_clks = 4,
  1800. .clksel_mask = S3C2412_UCON_CLKMASK,
  1801. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1802. },
  1803. .def_cfg = &(struct s3c2410_uartcfg) {
  1804. .ucon = S3C2410_UCON_DEFAULT,
  1805. .ufcon = S3C2410_UFCON_DEFAULT,
  1806. },
  1807. };
  1808. #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
  1809. #else
  1810. #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1811. #endif
  1812. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
  1813. static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1814. .info = &(struct s3c24xx_uart_info) {
  1815. .name = "Samsung S3C6400 UART",
  1816. .type = PORT_S3C6400,
  1817. .fifosize = 64,
  1818. .has_divslot = 1,
  1819. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1820. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1821. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1822. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1823. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1824. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1825. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1826. .num_clks = 4,
  1827. .clksel_mask = S3C6400_UCON_CLKMASK,
  1828. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1829. },
  1830. .def_cfg = &(struct s3c2410_uartcfg) {
  1831. .ucon = S3C2410_UCON_DEFAULT,
  1832. .ufcon = S3C2410_UFCON_DEFAULT,
  1833. },
  1834. };
  1835. #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
  1836. #else
  1837. #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1838. #endif
  1839. #ifdef CONFIG_CPU_S5PV210
  1840. static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1841. .info = &(struct s3c24xx_uart_info) {
  1842. .name = "Samsung S5PV210 UART",
  1843. .type = PORT_S3C6400,
  1844. .has_divslot = 1,
  1845. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1846. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1847. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1848. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1849. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1850. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1851. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1852. .num_clks = 2,
  1853. .clksel_mask = S5PV210_UCON_CLKMASK,
  1854. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1855. },
  1856. .def_cfg = &(struct s3c2410_uartcfg) {
  1857. .ucon = S5PV210_UCON_DEFAULT,
  1858. .ufcon = S5PV210_UFCON_DEFAULT,
  1859. },
  1860. .fifosize = { 256, 64, 16, 16 },
  1861. };
  1862. #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
  1863. #else
  1864. #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1865. #endif
  1866. #if defined(CONFIG_ARCH_EXYNOS)
  1867. #define EXYNOS_COMMON_SERIAL_DRV_DATA \
  1868. .info = &(struct s3c24xx_uart_info) { \
  1869. .name = "Samsung Exynos UART", \
  1870. .type = PORT_S3C6400, \
  1871. .has_divslot = 1, \
  1872. .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
  1873. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
  1874. .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
  1875. .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
  1876. .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
  1877. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
  1878. .def_clk_sel = S3C2410_UCON_CLKSEL0, \
  1879. .num_clks = 1, \
  1880. .clksel_mask = 0, \
  1881. .clksel_shift = 0, \
  1882. }, \
  1883. .def_cfg = &(struct s3c2410_uartcfg) { \
  1884. .ucon = S5PV210_UCON_DEFAULT, \
  1885. .ufcon = S5PV210_UFCON_DEFAULT, \
  1886. .has_fracval = 1, \
  1887. } \
  1888. static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  1889. EXYNOS_COMMON_SERIAL_DRV_DATA,
  1890. .fifosize = { 256, 64, 16, 16 },
  1891. };
  1892. static struct s3c24xx_serial_drv_data exynos5433_serial_drv_data = {
  1893. EXYNOS_COMMON_SERIAL_DRV_DATA,
  1894. .fifosize = { 64, 256, 16, 256 },
  1895. };
  1896. #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
  1897. #define EXYNOS5433_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos5433_serial_drv_data)
  1898. #else
  1899. #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1900. #define EXYNOS5433_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1901. #endif
  1902. static const struct platform_device_id s3c24xx_serial_driver_ids[] = {
  1903. {
  1904. .name = "s3c2410-uart",
  1905. .driver_data = S3C2410_SERIAL_DRV_DATA,
  1906. }, {
  1907. .name = "s3c2412-uart",
  1908. .driver_data = S3C2412_SERIAL_DRV_DATA,
  1909. }, {
  1910. .name = "s3c2440-uart",
  1911. .driver_data = S3C2440_SERIAL_DRV_DATA,
  1912. }, {
  1913. .name = "s3c6400-uart",
  1914. .driver_data = S3C6400_SERIAL_DRV_DATA,
  1915. }, {
  1916. .name = "s5pv210-uart",
  1917. .driver_data = S5PV210_SERIAL_DRV_DATA,
  1918. }, {
  1919. .name = "exynos4210-uart",
  1920. .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
  1921. }, {
  1922. .name = "exynos5433-uart",
  1923. .driver_data = EXYNOS5433_SERIAL_DRV_DATA,
  1924. },
  1925. { },
  1926. };
  1927. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  1928. #ifdef CONFIG_OF
  1929. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  1930. { .compatible = "samsung,s3c2410-uart",
  1931. .data = (void *)S3C2410_SERIAL_DRV_DATA },
  1932. { .compatible = "samsung,s3c2412-uart",
  1933. .data = (void *)S3C2412_SERIAL_DRV_DATA },
  1934. { .compatible = "samsung,s3c2440-uart",
  1935. .data = (void *)S3C2440_SERIAL_DRV_DATA },
  1936. { .compatible = "samsung,s3c6400-uart",
  1937. .data = (void *)S3C6400_SERIAL_DRV_DATA },
  1938. { .compatible = "samsung,s5pv210-uart",
  1939. .data = (void *)S5PV210_SERIAL_DRV_DATA },
  1940. { .compatible = "samsung,exynos4210-uart",
  1941. .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
  1942. { .compatible = "samsung,exynos5433-uart",
  1943. .data = (void *)EXYNOS5433_SERIAL_DRV_DATA },
  1944. {},
  1945. };
  1946. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  1947. #endif
  1948. static struct platform_driver samsung_serial_driver = {
  1949. .probe = s3c24xx_serial_probe,
  1950. .remove = s3c24xx_serial_remove,
  1951. .id_table = s3c24xx_serial_driver_ids,
  1952. .driver = {
  1953. .name = "samsung-uart",
  1954. .pm = SERIAL_SAMSUNG_PM_OPS,
  1955. .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
  1956. },
  1957. };
  1958. module_platform_driver(samsung_serial_driver);
  1959. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1960. /*
  1961. * Early console.
  1962. */
  1963. struct samsung_early_console_data {
  1964. u32 txfull_mask;
  1965. };
  1966. static void samsung_early_busyuart(struct uart_port *port)
  1967. {
  1968. while (!(readl(port->membase + S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXFE))
  1969. ;
  1970. }
  1971. static void samsung_early_busyuart_fifo(struct uart_port *port)
  1972. {
  1973. struct samsung_early_console_data *data = port->private_data;
  1974. while (readl(port->membase + S3C2410_UFSTAT) & data->txfull_mask)
  1975. ;
  1976. }
  1977. static void samsung_early_putc(struct uart_port *port, int c)
  1978. {
  1979. if (readl(port->membase + S3C2410_UFCON) & S3C2410_UFCON_FIFOMODE)
  1980. samsung_early_busyuart_fifo(port);
  1981. else
  1982. samsung_early_busyuart(port);
  1983. writeb(c, port->membase + S3C2410_UTXH);
  1984. }
  1985. static void samsung_early_write(struct console *con, const char *s, unsigned n)
  1986. {
  1987. struct earlycon_device *dev = con->data;
  1988. uart_console_write(&dev->port, s, n, samsung_early_putc);
  1989. }
  1990. static int __init samsung_early_console_setup(struct earlycon_device *device,
  1991. const char *opt)
  1992. {
  1993. if (!device->port.membase)
  1994. return -ENODEV;
  1995. device->con->write = samsung_early_write;
  1996. return 0;
  1997. }
  1998. /* S3C2410 */
  1999. static struct samsung_early_console_data s3c2410_early_console_data = {
  2000. .txfull_mask = S3C2410_UFSTAT_TXFULL,
  2001. };
  2002. static int __init s3c2410_early_console_setup(struct earlycon_device *device,
  2003. const char *opt)
  2004. {
  2005. device->port.private_data = &s3c2410_early_console_data;
  2006. return samsung_early_console_setup(device, opt);
  2007. }
  2008. OF_EARLYCON_DECLARE(s3c2410, "samsung,s3c2410-uart",
  2009. s3c2410_early_console_setup);
  2010. /* S3C2412, S3C2440, S3C64xx */
  2011. static struct samsung_early_console_data s3c2440_early_console_data = {
  2012. .txfull_mask = S3C2440_UFSTAT_TXFULL,
  2013. };
  2014. static int __init s3c2440_early_console_setup(struct earlycon_device *device,
  2015. const char *opt)
  2016. {
  2017. device->port.private_data = &s3c2440_early_console_data;
  2018. return samsung_early_console_setup(device, opt);
  2019. }
  2020. OF_EARLYCON_DECLARE(s3c2412, "samsung,s3c2412-uart",
  2021. s3c2440_early_console_setup);
  2022. OF_EARLYCON_DECLARE(s3c2440, "samsung,s3c2440-uart",
  2023. s3c2440_early_console_setup);
  2024. OF_EARLYCON_DECLARE(s3c6400, "samsung,s3c6400-uart",
  2025. s3c2440_early_console_setup);
  2026. /* S5PV210, EXYNOS */
  2027. static struct samsung_early_console_data s5pv210_early_console_data = {
  2028. .txfull_mask = S5PV210_UFSTAT_TXFULL,
  2029. };
  2030. static int __init s5pv210_early_console_setup(struct earlycon_device *device,
  2031. const char *opt)
  2032. {
  2033. device->port.private_data = &s5pv210_early_console_data;
  2034. return samsung_early_console_setup(device, opt);
  2035. }
  2036. OF_EARLYCON_DECLARE(s5pv210, "samsung,s5pv210-uart",
  2037. s5pv210_early_console_setup);
  2038. OF_EARLYCON_DECLARE(exynos4210, "samsung,exynos4210-uart",
  2039. s5pv210_early_console_setup);
  2040. #endif
  2041. MODULE_ALIAS("platform:samsung-uart");
  2042. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  2043. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  2044. MODULE_LICENSE("GPL v2");