samsung.c 64 KB

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