samsung.c 63 KB

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