samsung.c 63 KB

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