samsung.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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/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/delay.h>
  40. #include <linux/clk.h>
  41. #include <linux/cpufreq.h>
  42. #include <linux/of.h>
  43. #include <asm/irq.h>
  44. #include <mach/hardware.h>
  45. #include <mach/map.h>
  46. #include <plat/regs-serial.h>
  47. #include <plat/clock.h>
  48. #include "samsung.h"
  49. /* UART name and device definitions */
  50. #define S3C24XX_SERIAL_NAME "ttySAC"
  51. #define S3C24XX_SERIAL_MAJOR 204
  52. #define S3C24XX_SERIAL_MINOR 64
  53. /* macros to change one thing to another */
  54. #define tx_enabled(port) ((port)->unused[0])
  55. #define rx_enabled(port) ((port)->unused[1])
  56. /* flag to ignore all characters coming in */
  57. #define RXSTAT_DUMMY_READ (0x10000000)
  58. static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
  59. {
  60. return container_of(port, struct s3c24xx_uart_port, port);
  61. }
  62. /* translate a port to the device name */
  63. static inline const char *s3c24xx_serial_portname(struct uart_port *port)
  64. {
  65. return to_platform_device(port->dev)->name;
  66. }
  67. static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
  68. {
  69. return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
  70. }
  71. /*
  72. * s3c64xx and later SoC's include the interrupt mask and status registers in
  73. * the controller itself, unlike the s3c24xx SoC's which have these registers
  74. * in the interrupt controller. Check if the port type is s3c64xx or higher.
  75. */
  76. static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
  77. {
  78. return to_ourport(port)->info->type == PORT_S3C6400;
  79. }
  80. static void s3c24xx_serial_rx_enable(struct uart_port *port)
  81. {
  82. unsigned long flags;
  83. unsigned int ucon, ufcon;
  84. int count = 10000;
  85. spin_lock_irqsave(&port->lock, flags);
  86. while (--count && !s3c24xx_serial_txempty_nofifo(port))
  87. udelay(100);
  88. ufcon = rd_regl(port, S3C2410_UFCON);
  89. ufcon |= S3C2410_UFCON_RESETRX;
  90. wr_regl(port, S3C2410_UFCON, ufcon);
  91. ucon = rd_regl(port, S3C2410_UCON);
  92. ucon |= S3C2410_UCON_RXIRQMODE;
  93. wr_regl(port, S3C2410_UCON, ucon);
  94. rx_enabled(port) = 1;
  95. spin_unlock_irqrestore(&port->lock, flags);
  96. }
  97. static void s3c24xx_serial_rx_disable(struct uart_port *port)
  98. {
  99. unsigned long flags;
  100. unsigned int ucon;
  101. spin_lock_irqsave(&port->lock, flags);
  102. ucon = rd_regl(port, S3C2410_UCON);
  103. ucon &= ~S3C2410_UCON_RXIRQMODE;
  104. wr_regl(port, S3C2410_UCON, ucon);
  105. rx_enabled(port) = 0;
  106. spin_unlock_irqrestore(&port->lock, flags);
  107. }
  108. static void s3c24xx_serial_stop_tx(struct uart_port *port)
  109. {
  110. struct s3c24xx_uart_port *ourport = to_ourport(port);
  111. if (tx_enabled(port)) {
  112. if (s3c24xx_serial_has_interrupt_mask(port))
  113. __set_bit(S3C64XX_UINTM_TXD,
  114. portaddrl(port, S3C64XX_UINTM));
  115. else
  116. disable_irq_nosync(ourport->tx_irq);
  117. tx_enabled(port) = 0;
  118. if (port->flags & UPF_CONS_FLOW)
  119. s3c24xx_serial_rx_enable(port);
  120. }
  121. }
  122. static void s3c24xx_serial_start_tx(struct uart_port *port)
  123. {
  124. struct s3c24xx_uart_port *ourport = to_ourport(port);
  125. if (!tx_enabled(port)) {
  126. if (port->flags & UPF_CONS_FLOW)
  127. s3c24xx_serial_rx_disable(port);
  128. if (s3c24xx_serial_has_interrupt_mask(port))
  129. __clear_bit(S3C64XX_UINTM_TXD,
  130. portaddrl(port, S3C64XX_UINTM));
  131. else
  132. enable_irq(ourport->tx_irq);
  133. tx_enabled(port) = 1;
  134. }
  135. }
  136. static void s3c24xx_serial_stop_rx(struct uart_port *port)
  137. {
  138. struct s3c24xx_uart_port *ourport = to_ourport(port);
  139. if (rx_enabled(port)) {
  140. dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
  141. if (s3c24xx_serial_has_interrupt_mask(port))
  142. __set_bit(S3C64XX_UINTM_RXD,
  143. portaddrl(port, S3C64XX_UINTM));
  144. else
  145. disable_irq_nosync(ourport->rx_irq);
  146. rx_enabled(port) = 0;
  147. }
  148. }
  149. static void s3c24xx_serial_enable_ms(struct uart_port *port)
  150. {
  151. }
  152. static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
  153. {
  154. return to_ourport(port)->info;
  155. }
  156. static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
  157. {
  158. struct s3c24xx_uart_port *ourport;
  159. if (port->dev == NULL)
  160. return NULL;
  161. ourport = container_of(port, struct s3c24xx_uart_port, port);
  162. return ourport->cfg;
  163. }
  164. static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
  165. unsigned long ufstat)
  166. {
  167. struct s3c24xx_uart_info *info = ourport->info;
  168. if (ufstat & info->rx_fifofull)
  169. return ourport->port.fifosize;
  170. return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
  171. }
  172. /* ? - where has parity gone?? */
  173. #define S3C2410_UERSTAT_PARITY (0x1000)
  174. static irqreturn_t
  175. s3c24xx_serial_rx_chars(int irq, void *dev_id)
  176. {
  177. struct s3c24xx_uart_port *ourport = dev_id;
  178. struct uart_port *port = &ourport->port;
  179. struct tty_struct *tty = port->state->port.tty;
  180. unsigned int ufcon, ch, flag, ufstat, uerstat;
  181. unsigned long flags;
  182. int max_count = 64;
  183. spin_lock_irqsave(&port->lock, flags);
  184. while (max_count-- > 0) {
  185. ufcon = rd_regl(port, S3C2410_UFCON);
  186. ufstat = rd_regl(port, S3C2410_UFSTAT);
  187. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  188. break;
  189. uerstat = rd_regl(port, S3C2410_UERSTAT);
  190. ch = rd_regb(port, S3C2410_URXH);
  191. if (port->flags & UPF_CONS_FLOW) {
  192. int txe = s3c24xx_serial_txempty_nofifo(port);
  193. if (rx_enabled(port)) {
  194. if (!txe) {
  195. rx_enabled(port) = 0;
  196. continue;
  197. }
  198. } else {
  199. if (txe) {
  200. ufcon |= S3C2410_UFCON_RESETRX;
  201. wr_regl(port, S3C2410_UFCON, ufcon);
  202. rx_enabled(port) = 1;
  203. goto out;
  204. }
  205. continue;
  206. }
  207. }
  208. /* insert the character into the buffer */
  209. flag = TTY_NORMAL;
  210. port->icount.rx++;
  211. if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
  212. dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
  213. ch, uerstat);
  214. /* check for break */
  215. if (uerstat & S3C2410_UERSTAT_BREAK) {
  216. dbg("break!\n");
  217. port->icount.brk++;
  218. if (uart_handle_break(port))
  219. goto ignore_char;
  220. }
  221. if (uerstat & S3C2410_UERSTAT_FRAME)
  222. port->icount.frame++;
  223. if (uerstat & S3C2410_UERSTAT_OVERRUN)
  224. port->icount.overrun++;
  225. uerstat &= port->read_status_mask;
  226. if (uerstat & S3C2410_UERSTAT_BREAK)
  227. flag = TTY_BREAK;
  228. else if (uerstat & S3C2410_UERSTAT_PARITY)
  229. flag = TTY_PARITY;
  230. else if (uerstat & (S3C2410_UERSTAT_FRAME |
  231. S3C2410_UERSTAT_OVERRUN))
  232. flag = TTY_FRAME;
  233. }
  234. if (uart_handle_sysrq_char(port, ch))
  235. goto ignore_char;
  236. uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
  237. ch, flag);
  238. ignore_char:
  239. continue;
  240. }
  241. tty_flip_buffer_push(tty);
  242. out:
  243. spin_unlock_irqrestore(&port->lock, flags);
  244. return IRQ_HANDLED;
  245. }
  246. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
  247. {
  248. struct s3c24xx_uart_port *ourport = id;
  249. struct uart_port *port = &ourport->port;
  250. struct circ_buf *xmit = &port->state->xmit;
  251. unsigned long flags;
  252. int count = 256;
  253. spin_lock_irqsave(&port->lock, flags);
  254. if (port->x_char) {
  255. wr_regb(port, S3C2410_UTXH, port->x_char);
  256. port->icount.tx++;
  257. port->x_char = 0;
  258. goto out;
  259. }
  260. /* if there isn't anything more to transmit, or the uart is now
  261. * stopped, disable the uart and exit
  262. */
  263. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  264. s3c24xx_serial_stop_tx(port);
  265. goto out;
  266. }
  267. /* try and drain the buffer... */
  268. while (!uart_circ_empty(xmit) && count-- > 0) {
  269. if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
  270. break;
  271. wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
  272. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  273. port->icount.tx++;
  274. }
  275. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
  276. spin_unlock(&port->lock);
  277. uart_write_wakeup(port);
  278. spin_lock(&port->lock);
  279. }
  280. if (uart_circ_empty(xmit))
  281. s3c24xx_serial_stop_tx(port);
  282. out:
  283. spin_unlock_irqrestore(&port->lock, flags);
  284. return IRQ_HANDLED;
  285. }
  286. /* interrupt handler for s3c64xx and later SoC's.*/
  287. static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
  288. {
  289. struct s3c24xx_uart_port *ourport = id;
  290. struct uart_port *port = &ourport->port;
  291. unsigned int pend = rd_regl(port, S3C64XX_UINTP);
  292. irqreturn_t ret = IRQ_HANDLED;
  293. if (pend & S3C64XX_UINTM_RXD_MSK) {
  294. ret = s3c24xx_serial_rx_chars(irq, id);
  295. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
  296. }
  297. if (pend & S3C64XX_UINTM_TXD_MSK) {
  298. ret = s3c24xx_serial_tx_chars(irq, id);
  299. wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
  300. }
  301. return ret;
  302. }
  303. static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
  304. {
  305. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  306. unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
  307. unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
  308. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  309. if ((ufstat & info->tx_fifomask) != 0 ||
  310. (ufstat & info->tx_fifofull))
  311. return 0;
  312. return 1;
  313. }
  314. return s3c24xx_serial_txempty_nofifo(port);
  315. }
  316. /* no modem control lines */
  317. static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
  318. {
  319. unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
  320. if (umstat & S3C2410_UMSTAT_CTS)
  321. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  322. else
  323. return TIOCM_CAR | TIOCM_DSR;
  324. }
  325. static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  326. {
  327. /* todo - possibly remove AFC and do manual CTS */
  328. }
  329. static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
  330. {
  331. unsigned long flags;
  332. unsigned int ucon;
  333. spin_lock_irqsave(&port->lock, flags);
  334. ucon = rd_regl(port, S3C2410_UCON);
  335. if (break_state)
  336. ucon |= S3C2410_UCON_SBREAK;
  337. else
  338. ucon &= ~S3C2410_UCON_SBREAK;
  339. wr_regl(port, S3C2410_UCON, ucon);
  340. spin_unlock_irqrestore(&port->lock, flags);
  341. }
  342. static void s3c24xx_serial_shutdown(struct uart_port *port)
  343. {
  344. struct s3c24xx_uart_port *ourport = to_ourport(port);
  345. if (ourport->tx_claimed) {
  346. if (!s3c24xx_serial_has_interrupt_mask(port))
  347. free_irq(ourport->tx_irq, ourport);
  348. tx_enabled(port) = 0;
  349. ourport->tx_claimed = 0;
  350. }
  351. if (ourport->rx_claimed) {
  352. if (!s3c24xx_serial_has_interrupt_mask(port))
  353. free_irq(ourport->rx_irq, ourport);
  354. ourport->rx_claimed = 0;
  355. rx_enabled(port) = 0;
  356. }
  357. /* Clear pending interrupts and mask all interrupts */
  358. if (s3c24xx_serial_has_interrupt_mask(port)) {
  359. wr_regl(port, S3C64XX_UINTP, 0xf);
  360. wr_regl(port, S3C64XX_UINTM, 0xf);
  361. }
  362. }
  363. static int s3c24xx_serial_startup(struct uart_port *port)
  364. {
  365. struct s3c24xx_uart_port *ourport = to_ourport(port);
  366. int ret;
  367. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  368. port->mapbase, port->membase);
  369. rx_enabled(port) = 1;
  370. ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
  371. s3c24xx_serial_portname(port), ourport);
  372. if (ret != 0) {
  373. dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
  374. return ret;
  375. }
  376. ourport->rx_claimed = 1;
  377. dbg("requesting tx irq...\n");
  378. tx_enabled(port) = 1;
  379. ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
  380. s3c24xx_serial_portname(port), ourport);
  381. if (ret) {
  382. dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
  383. goto err;
  384. }
  385. ourport->tx_claimed = 1;
  386. dbg("s3c24xx_serial_startup ok\n");
  387. /* the port reset code should have done the correct
  388. * register setup for the port controls */
  389. return ret;
  390. err:
  391. s3c24xx_serial_shutdown(port);
  392. return ret;
  393. }
  394. static int s3c64xx_serial_startup(struct uart_port *port)
  395. {
  396. struct s3c24xx_uart_port *ourport = to_ourport(port);
  397. int ret;
  398. dbg("s3c64xx_serial_startup: port=%p (%08lx,%p)\n",
  399. port->mapbase, port->membase);
  400. ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
  401. s3c24xx_serial_portname(port), ourport);
  402. if (ret) {
  403. dev_err(port->dev, "cannot get irq %d\n", port->irq);
  404. return ret;
  405. }
  406. /* For compatibility with s3c24xx Soc's */
  407. rx_enabled(port) = 1;
  408. ourport->rx_claimed = 1;
  409. tx_enabled(port) = 0;
  410. ourport->tx_claimed = 1;
  411. /* Enable Rx Interrupt */
  412. __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
  413. dbg("s3c64xx_serial_startup ok\n");
  414. return ret;
  415. }
  416. /* power power management control */
  417. static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
  418. unsigned int old)
  419. {
  420. struct s3c24xx_uart_port *ourport = to_ourport(port);
  421. ourport->pm_level = level;
  422. switch (level) {
  423. case 3:
  424. if (!IS_ERR(ourport->baudclk))
  425. clk_disable_unprepare(ourport->baudclk);
  426. clk_disable_unprepare(ourport->clk);
  427. break;
  428. case 0:
  429. clk_prepare_enable(ourport->clk);
  430. if (!IS_ERR(ourport->baudclk))
  431. clk_prepare_enable(ourport->baudclk);
  432. break;
  433. default:
  434. dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
  435. }
  436. }
  437. /* baud rate calculation
  438. *
  439. * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
  440. * of different sources, including the peripheral clock ("pclk") and an
  441. * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
  442. * with a programmable extra divisor.
  443. *
  444. * The following code goes through the clock sources, and calculates the
  445. * baud clocks (and the resultant actual baud rates) and then tries to
  446. * pick the closest one and select that.
  447. *
  448. */
  449. #define MAX_CLK_NAME_LENGTH 15
  450. static inline int s3c24xx_serial_getsource(struct uart_port *port)
  451. {
  452. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  453. unsigned int ucon;
  454. if (info->num_clks == 1)
  455. return 0;
  456. ucon = rd_regl(port, S3C2410_UCON);
  457. ucon &= info->clksel_mask;
  458. return ucon >> info->clksel_shift;
  459. }
  460. static void s3c24xx_serial_setsource(struct uart_port *port,
  461. unsigned int clk_sel)
  462. {
  463. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  464. unsigned int ucon;
  465. if (info->num_clks == 1)
  466. return;
  467. ucon = rd_regl(port, S3C2410_UCON);
  468. if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
  469. return;
  470. ucon &= ~info->clksel_mask;
  471. ucon |= clk_sel << info->clksel_shift;
  472. wr_regl(port, S3C2410_UCON, ucon);
  473. }
  474. static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
  475. unsigned int req_baud, struct clk **best_clk,
  476. unsigned int *clk_num)
  477. {
  478. struct s3c24xx_uart_info *info = ourport->info;
  479. struct clk *clk;
  480. unsigned long rate;
  481. unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
  482. char clkname[MAX_CLK_NAME_LENGTH];
  483. int calc_deviation, deviation = (1 << 30) - 1;
  484. clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
  485. ourport->info->def_clk_sel;
  486. for (cnt = 0; cnt < info->num_clks; cnt++) {
  487. if (!(clk_sel & (1 << cnt)))
  488. continue;
  489. sprintf(clkname, "clk_uart_baud%d", cnt);
  490. clk = clk_get(ourport->port.dev, clkname);
  491. if (IS_ERR(clk))
  492. continue;
  493. rate = clk_get_rate(clk);
  494. if (!rate)
  495. continue;
  496. if (ourport->info->has_divslot) {
  497. unsigned long div = rate / req_baud;
  498. /* The UDIVSLOT register on the newer UARTs allows us to
  499. * get a divisor adjustment of 1/16th on the baud clock.
  500. *
  501. * We don't keep the UDIVSLOT value (the 16ths we
  502. * calculated by not multiplying the baud by 16) as it
  503. * is easy enough to recalculate.
  504. */
  505. quot = div / 16;
  506. baud = rate / div;
  507. } else {
  508. quot = (rate + (8 * req_baud)) / (16 * req_baud);
  509. baud = rate / (quot * 16);
  510. }
  511. quot--;
  512. calc_deviation = req_baud - baud;
  513. if (calc_deviation < 0)
  514. calc_deviation = -calc_deviation;
  515. if (calc_deviation < deviation) {
  516. *best_clk = clk;
  517. best_quot = quot;
  518. *clk_num = cnt;
  519. deviation = calc_deviation;
  520. }
  521. }
  522. return best_quot;
  523. }
  524. /* udivslot_table[]
  525. *
  526. * This table takes the fractional value of the baud divisor and gives
  527. * the recommended setting for the UDIVSLOT register.
  528. */
  529. static u16 udivslot_table[16] = {
  530. [0] = 0x0000,
  531. [1] = 0x0080,
  532. [2] = 0x0808,
  533. [3] = 0x0888,
  534. [4] = 0x2222,
  535. [5] = 0x4924,
  536. [6] = 0x4A52,
  537. [7] = 0x54AA,
  538. [8] = 0x5555,
  539. [9] = 0xD555,
  540. [10] = 0xD5D5,
  541. [11] = 0xDDD5,
  542. [12] = 0xDDDD,
  543. [13] = 0xDFDD,
  544. [14] = 0xDFDF,
  545. [15] = 0xFFDF,
  546. };
  547. static void s3c24xx_serial_set_termios(struct uart_port *port,
  548. struct ktermios *termios,
  549. struct ktermios *old)
  550. {
  551. struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
  552. struct s3c24xx_uart_port *ourport = to_ourport(port);
  553. struct clk *clk = ERR_PTR(-EINVAL);
  554. unsigned long flags;
  555. unsigned int baud, quot, clk_sel = 0;
  556. unsigned int ulcon;
  557. unsigned int umcon;
  558. unsigned int udivslot = 0;
  559. /*
  560. * We don't support modem control lines.
  561. */
  562. termios->c_cflag &= ~(HUPCL | CMSPAR);
  563. termios->c_cflag |= CLOCAL;
  564. /*
  565. * Ask the core to calculate the divisor for us.
  566. */
  567. baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
  568. quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
  569. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  570. quot = port->custom_divisor;
  571. if (IS_ERR(clk))
  572. return;
  573. /* check to see if we need to change clock source */
  574. if (ourport->baudclk != clk) {
  575. s3c24xx_serial_setsource(port, clk_sel);
  576. if (!IS_ERR(ourport->baudclk)) {
  577. clk_disable_unprepare(ourport->baudclk);
  578. ourport->baudclk = ERR_PTR(-EINVAL);
  579. }
  580. clk_prepare_enable(clk);
  581. ourport->baudclk = clk;
  582. ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
  583. }
  584. if (ourport->info->has_divslot) {
  585. unsigned int div = ourport->baudclk_rate / baud;
  586. if (cfg->has_fracval) {
  587. udivslot = (div & 15);
  588. dbg("fracval = %04x\n", udivslot);
  589. } else {
  590. udivslot = udivslot_table[div & 15];
  591. dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
  592. }
  593. }
  594. switch (termios->c_cflag & CSIZE) {
  595. case CS5:
  596. dbg("config: 5bits/char\n");
  597. ulcon = S3C2410_LCON_CS5;
  598. break;
  599. case CS6:
  600. dbg("config: 6bits/char\n");
  601. ulcon = S3C2410_LCON_CS6;
  602. break;
  603. case CS7:
  604. dbg("config: 7bits/char\n");
  605. ulcon = S3C2410_LCON_CS7;
  606. break;
  607. case CS8:
  608. default:
  609. dbg("config: 8bits/char\n");
  610. ulcon = S3C2410_LCON_CS8;
  611. break;
  612. }
  613. /* preserve original lcon IR settings */
  614. ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
  615. if (termios->c_cflag & CSTOPB)
  616. ulcon |= S3C2410_LCON_STOPB;
  617. umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
  618. if (termios->c_cflag & PARENB) {
  619. if (termios->c_cflag & PARODD)
  620. ulcon |= S3C2410_LCON_PODD;
  621. else
  622. ulcon |= S3C2410_LCON_PEVEN;
  623. } else {
  624. ulcon |= S3C2410_LCON_PNONE;
  625. }
  626. spin_lock_irqsave(&port->lock, flags);
  627. dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
  628. ulcon, quot, udivslot);
  629. wr_regl(port, S3C2410_ULCON, ulcon);
  630. wr_regl(port, S3C2410_UBRDIV, quot);
  631. wr_regl(port, S3C2410_UMCON, umcon);
  632. if (ourport->info->has_divslot)
  633. wr_regl(port, S3C2443_DIVSLOT, udivslot);
  634. dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
  635. rd_regl(port, S3C2410_ULCON),
  636. rd_regl(port, S3C2410_UCON),
  637. rd_regl(port, S3C2410_UFCON));
  638. /*
  639. * Update the per-port timeout.
  640. */
  641. uart_update_timeout(port, termios->c_cflag, baud);
  642. /*
  643. * Which character status flags are we interested in?
  644. */
  645. port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
  646. if (termios->c_iflag & INPCK)
  647. port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
  648. /*
  649. * Which character status flags should we ignore?
  650. */
  651. port->ignore_status_mask = 0;
  652. if (termios->c_iflag & IGNPAR)
  653. port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
  654. if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
  655. port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
  656. /*
  657. * Ignore all characters if CREAD is not set.
  658. */
  659. if ((termios->c_cflag & CREAD) == 0)
  660. port->ignore_status_mask |= RXSTAT_DUMMY_READ;
  661. spin_unlock_irqrestore(&port->lock, flags);
  662. }
  663. static const char *s3c24xx_serial_type(struct uart_port *port)
  664. {
  665. switch (port->type) {
  666. case PORT_S3C2410:
  667. return "S3C2410";
  668. case PORT_S3C2440:
  669. return "S3C2440";
  670. case PORT_S3C2412:
  671. return "S3C2412";
  672. case PORT_S3C6400:
  673. return "S3C6400/10";
  674. default:
  675. return NULL;
  676. }
  677. }
  678. #define MAP_SIZE (0x100)
  679. static void s3c24xx_serial_release_port(struct uart_port *port)
  680. {
  681. release_mem_region(port->mapbase, MAP_SIZE);
  682. }
  683. static int s3c24xx_serial_request_port(struct uart_port *port)
  684. {
  685. const char *name = s3c24xx_serial_portname(port);
  686. return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
  687. }
  688. static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
  689. {
  690. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  691. if (flags & UART_CONFIG_TYPE &&
  692. s3c24xx_serial_request_port(port) == 0)
  693. port->type = info->type;
  694. }
  695. /*
  696. * verify the new serial_struct (for TIOCSSERIAL).
  697. */
  698. static int
  699. s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
  700. {
  701. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  702. if (ser->type != PORT_UNKNOWN && ser->type != info->type)
  703. return -EINVAL;
  704. return 0;
  705. }
  706. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  707. static struct console s3c24xx_serial_console;
  708. static int __init s3c24xx_serial_console_init(void)
  709. {
  710. register_console(&s3c24xx_serial_console);
  711. return 0;
  712. }
  713. console_initcall(s3c24xx_serial_console_init);
  714. #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
  715. #else
  716. #define S3C24XX_SERIAL_CONSOLE NULL
  717. #endif
  718. #ifdef CONFIG_CONSOLE_POLL
  719. static int s3c24xx_serial_get_poll_char(struct uart_port *port);
  720. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  721. unsigned char c);
  722. #endif
  723. static struct uart_ops s3c24xx_serial_ops = {
  724. .pm = s3c24xx_serial_pm,
  725. .tx_empty = s3c24xx_serial_tx_empty,
  726. .get_mctrl = s3c24xx_serial_get_mctrl,
  727. .set_mctrl = s3c24xx_serial_set_mctrl,
  728. .stop_tx = s3c24xx_serial_stop_tx,
  729. .start_tx = s3c24xx_serial_start_tx,
  730. .stop_rx = s3c24xx_serial_stop_rx,
  731. .enable_ms = s3c24xx_serial_enable_ms,
  732. .break_ctl = s3c24xx_serial_break_ctl,
  733. .startup = s3c24xx_serial_startup,
  734. .shutdown = s3c24xx_serial_shutdown,
  735. .set_termios = s3c24xx_serial_set_termios,
  736. .type = s3c24xx_serial_type,
  737. .release_port = s3c24xx_serial_release_port,
  738. .request_port = s3c24xx_serial_request_port,
  739. .config_port = s3c24xx_serial_config_port,
  740. .verify_port = s3c24xx_serial_verify_port,
  741. #ifdef CONFIG_CONSOLE_POLL
  742. .poll_get_char = s3c24xx_serial_get_poll_char,
  743. .poll_put_char = s3c24xx_serial_put_poll_char,
  744. #endif
  745. };
  746. static struct uart_driver s3c24xx_uart_drv = {
  747. .owner = THIS_MODULE,
  748. .driver_name = "s3c2410_serial",
  749. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  750. .cons = S3C24XX_SERIAL_CONSOLE,
  751. .dev_name = S3C24XX_SERIAL_NAME,
  752. .major = S3C24XX_SERIAL_MAJOR,
  753. .minor = S3C24XX_SERIAL_MINOR,
  754. };
  755. static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  756. [0] = {
  757. .port = {
  758. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
  759. .iotype = UPIO_MEM,
  760. .uartclk = 0,
  761. .fifosize = 16,
  762. .ops = &s3c24xx_serial_ops,
  763. .flags = UPF_BOOT_AUTOCONF,
  764. .line = 0,
  765. }
  766. },
  767. [1] = {
  768. .port = {
  769. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
  770. .iotype = UPIO_MEM,
  771. .uartclk = 0,
  772. .fifosize = 16,
  773. .ops = &s3c24xx_serial_ops,
  774. .flags = UPF_BOOT_AUTOCONF,
  775. .line = 1,
  776. }
  777. },
  778. #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
  779. [2] = {
  780. .port = {
  781. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
  782. .iotype = UPIO_MEM,
  783. .uartclk = 0,
  784. .fifosize = 16,
  785. .ops = &s3c24xx_serial_ops,
  786. .flags = UPF_BOOT_AUTOCONF,
  787. .line = 2,
  788. }
  789. },
  790. #endif
  791. #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
  792. [3] = {
  793. .port = {
  794. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
  795. .iotype = UPIO_MEM,
  796. .uartclk = 0,
  797. .fifosize = 16,
  798. .ops = &s3c24xx_serial_ops,
  799. .flags = UPF_BOOT_AUTOCONF,
  800. .line = 3,
  801. }
  802. }
  803. #endif
  804. };
  805. /* s3c24xx_serial_resetport
  806. *
  807. * reset the fifos and other the settings.
  808. */
  809. static void s3c24xx_serial_resetport(struct uart_port *port,
  810. struct s3c2410_uartcfg *cfg)
  811. {
  812. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  813. unsigned long ucon = rd_regl(port, S3C2410_UCON);
  814. unsigned int ucon_mask;
  815. ucon_mask = info->clksel_mask;
  816. if (info->type == PORT_S3C2440)
  817. ucon_mask |= S3C2440_UCON0_DIVMASK;
  818. ucon &= ucon_mask;
  819. wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
  820. /* reset both fifos */
  821. wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
  822. wr_regl(port, S3C2410_UFCON, cfg->ufcon);
  823. /* some delay is required after fifo reset */
  824. udelay(1);
  825. }
  826. #ifdef CONFIG_CPU_FREQ
  827. static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
  828. unsigned long val, void *data)
  829. {
  830. struct s3c24xx_uart_port *port;
  831. struct uart_port *uport;
  832. port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
  833. uport = &port->port;
  834. /* check to see if port is enabled */
  835. if (port->pm_level != 0)
  836. return 0;
  837. /* try and work out if the baudrate is changing, we can detect
  838. * a change in rate, but we do not have support for detecting
  839. * a disturbance in the clock-rate over the change.
  840. */
  841. if (IS_ERR(port->baudclk))
  842. goto exit;
  843. if (port->baudclk_rate == clk_get_rate(port->baudclk))
  844. goto exit;
  845. if (val == CPUFREQ_PRECHANGE) {
  846. /* we should really shut the port down whilst the
  847. * frequency change is in progress. */
  848. } else if (val == CPUFREQ_POSTCHANGE) {
  849. struct ktermios *termios;
  850. struct tty_struct *tty;
  851. if (uport->state == NULL)
  852. goto exit;
  853. tty = uport->state->port.tty;
  854. if (tty == NULL)
  855. goto exit;
  856. termios = &tty->termios;
  857. if (termios == NULL) {
  858. dev_warn(uport->dev, "%s: no termios?\n", __func__);
  859. goto exit;
  860. }
  861. s3c24xx_serial_set_termios(uport, termios, NULL);
  862. }
  863. exit:
  864. return 0;
  865. }
  866. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  867. {
  868. port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
  869. return cpufreq_register_notifier(&port->freq_transition,
  870. CPUFREQ_TRANSITION_NOTIFIER);
  871. }
  872. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  873. {
  874. cpufreq_unregister_notifier(&port->freq_transition,
  875. CPUFREQ_TRANSITION_NOTIFIER);
  876. }
  877. #else
  878. static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
  879. {
  880. return 0;
  881. }
  882. static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
  883. {
  884. }
  885. #endif
  886. /* s3c24xx_serial_init_port
  887. *
  888. * initialise a single serial port from the platform device given
  889. */
  890. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  891. struct platform_device *platdev)
  892. {
  893. struct uart_port *port = &ourport->port;
  894. struct s3c2410_uartcfg *cfg = ourport->cfg;
  895. struct resource *res;
  896. int ret;
  897. dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
  898. if (platdev == NULL)
  899. return -ENODEV;
  900. if (port->mapbase != 0)
  901. return 0;
  902. /* setup info for port */
  903. port->dev = &platdev->dev;
  904. /* Startup sequence is different for s3c64xx and higher SoC's */
  905. if (s3c24xx_serial_has_interrupt_mask(port))
  906. s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
  907. port->uartclk = 1;
  908. if (cfg->uart_flags & UPF_CONS_FLOW) {
  909. dbg("s3c24xx_serial_init_port: enabling flow control\n");
  910. port->flags |= UPF_CONS_FLOW;
  911. }
  912. /* sort our the physical and virtual addresses for each UART */
  913. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  914. if (res == NULL) {
  915. dev_err(port->dev, "failed to find memory resource for uart\n");
  916. return -EINVAL;
  917. }
  918. dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
  919. port->mapbase = res->start;
  920. port->membase = S3C_VA_UART + (res->start & 0xfffff);
  921. ret = platform_get_irq(platdev, 0);
  922. if (ret < 0)
  923. port->irq = 0;
  924. else {
  925. port->irq = ret;
  926. ourport->rx_irq = ret;
  927. ourport->tx_irq = ret + 1;
  928. }
  929. ret = platform_get_irq(platdev, 1);
  930. if (ret > 0)
  931. ourport->tx_irq = ret;
  932. ourport->clk = clk_get(&platdev->dev, "uart");
  933. /* Keep all interrupts masked and cleared */
  934. if (s3c24xx_serial_has_interrupt_mask(port)) {
  935. wr_regl(port, S3C64XX_UINTM, 0xf);
  936. wr_regl(port, S3C64XX_UINTP, 0xf);
  937. wr_regl(port, S3C64XX_UINTSP, 0xf);
  938. }
  939. dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
  940. port->mapbase, port->membase, port->irq,
  941. ourport->rx_irq, ourport->tx_irq, port->uartclk);
  942. /* reset the fifos (and setup the uart) */
  943. s3c24xx_serial_resetport(port, cfg);
  944. return 0;
  945. }
  946. static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
  947. struct device_attribute *attr,
  948. char *buf)
  949. {
  950. struct uart_port *port = s3c24xx_dev_to_port(dev);
  951. struct s3c24xx_uart_port *ourport = to_ourport(port);
  952. if (IS_ERR(ourport->baudclk))
  953. return -EINVAL;
  954. return snprintf(buf, PAGE_SIZE, "* %s\n",
  955. ourport->baudclk->name ?: "(null)");
  956. }
  957. static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
  958. /* Device driver serial port probe */
  959. static const struct of_device_id s3c24xx_uart_dt_match[];
  960. static int probe_index;
  961. static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
  962. struct platform_device *pdev)
  963. {
  964. #ifdef CONFIG_OF
  965. if (pdev->dev.of_node) {
  966. const struct of_device_id *match;
  967. match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
  968. return (struct s3c24xx_serial_drv_data *)match->data;
  969. }
  970. #endif
  971. return (struct s3c24xx_serial_drv_data *)
  972. platform_get_device_id(pdev)->driver_data;
  973. }
  974. static int s3c24xx_serial_probe(struct platform_device *pdev)
  975. {
  976. struct s3c24xx_uart_port *ourport;
  977. int ret;
  978. dbg("s3c24xx_serial_probe(%p) %d\n", pdev, probe_index);
  979. ourport = &s3c24xx_serial_ports[probe_index];
  980. ourport->drv_data = s3c24xx_get_driver_data(pdev);
  981. if (!ourport->drv_data) {
  982. dev_err(&pdev->dev, "could not find driver data\n");
  983. return -ENODEV;
  984. }
  985. ourport->baudclk = ERR_PTR(-EINVAL);
  986. ourport->info = ourport->drv_data->info;
  987. ourport->cfg = (pdev->dev.platform_data) ?
  988. (struct s3c2410_uartcfg *)pdev->dev.platform_data :
  989. ourport->drv_data->def_cfg;
  990. ourport->port.fifosize = (ourport->info->fifosize) ?
  991. ourport->info->fifosize :
  992. ourport->drv_data->fifosize[probe_index];
  993. probe_index++;
  994. dbg("%s: initialising port %p...\n", __func__, ourport);
  995. ret = s3c24xx_serial_init_port(ourport, pdev);
  996. if (ret < 0)
  997. goto probe_err;
  998. dbg("%s: adding port\n", __func__);
  999. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  1000. platform_set_drvdata(pdev, &ourport->port);
  1001. ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
  1002. if (ret < 0)
  1003. dev_err(&pdev->dev, "failed to add clock source attr.\n");
  1004. ret = s3c24xx_serial_cpufreq_register(ourport);
  1005. if (ret < 0)
  1006. dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
  1007. return 0;
  1008. probe_err:
  1009. return ret;
  1010. }
  1011. static int s3c24xx_serial_remove(struct platform_device *dev)
  1012. {
  1013. struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
  1014. if (port) {
  1015. s3c24xx_serial_cpufreq_deregister(to_ourport(port));
  1016. device_remove_file(&dev->dev, &dev_attr_clock_source);
  1017. uart_remove_one_port(&s3c24xx_uart_drv, port);
  1018. }
  1019. return 0;
  1020. }
  1021. /* UART power management code */
  1022. #ifdef CONFIG_PM_SLEEP
  1023. static int s3c24xx_serial_suspend(struct device *dev)
  1024. {
  1025. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1026. if (port)
  1027. uart_suspend_port(&s3c24xx_uart_drv, port);
  1028. return 0;
  1029. }
  1030. static int s3c24xx_serial_resume(struct device *dev)
  1031. {
  1032. struct uart_port *port = s3c24xx_dev_to_port(dev);
  1033. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1034. if (port) {
  1035. clk_prepare_enable(ourport->clk);
  1036. s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
  1037. clk_disable_unprepare(ourport->clk);
  1038. uart_resume_port(&s3c24xx_uart_drv, port);
  1039. }
  1040. return 0;
  1041. }
  1042. static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
  1043. .suspend = s3c24xx_serial_suspend,
  1044. .resume = s3c24xx_serial_resume,
  1045. };
  1046. #define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
  1047. #else /* !CONFIG_PM_SLEEP */
  1048. #define SERIAL_SAMSUNG_PM_OPS NULL
  1049. #endif /* CONFIG_PM_SLEEP */
  1050. /* Console code */
  1051. #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
  1052. static struct uart_port *cons_uart;
  1053. static int
  1054. s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
  1055. {
  1056. struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
  1057. unsigned long ufstat, utrstat;
  1058. if (ufcon & S3C2410_UFCON_FIFOMODE) {
  1059. /* fifo mode - check amount of data in fifo registers... */
  1060. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1061. return (ufstat & info->tx_fifofull) ? 0 : 1;
  1062. }
  1063. /* in non-fifo mode, we go and use the tx buffer empty */
  1064. utrstat = rd_regl(port, S3C2410_UTRSTAT);
  1065. return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
  1066. }
  1067. #ifdef CONFIG_CONSOLE_POLL
  1068. /*
  1069. * Console polling routines for writing and reading from the uart while
  1070. * in an interrupt or debug context.
  1071. */
  1072. static int s3c24xx_serial_get_poll_char(struct uart_port *port)
  1073. {
  1074. struct s3c24xx_uart_port *ourport = to_ourport(port);
  1075. unsigned int ufstat;
  1076. ufstat = rd_regl(port, S3C2410_UFSTAT);
  1077. if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
  1078. return NO_POLL_CHAR;
  1079. return rd_regb(port, S3C2410_URXH);
  1080. }
  1081. static void s3c24xx_serial_put_poll_char(struct uart_port *port,
  1082. unsigned char c)
  1083. {
  1084. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1085. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1086. cpu_relax();
  1087. wr_regb(cons_uart, S3C2410_UTXH, c);
  1088. }
  1089. #endif /* CONFIG_CONSOLE_POLL */
  1090. static void
  1091. s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
  1092. {
  1093. unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
  1094. while (!s3c24xx_serial_console_txrdy(port, ufcon))
  1095. barrier();
  1096. wr_regb(cons_uart, S3C2410_UTXH, ch);
  1097. }
  1098. static void
  1099. s3c24xx_serial_console_write(struct console *co, const char *s,
  1100. unsigned int count)
  1101. {
  1102. uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
  1103. }
  1104. static void __init
  1105. s3c24xx_serial_get_options(struct uart_port *port, int *baud,
  1106. int *parity, int *bits)
  1107. {
  1108. struct clk *clk;
  1109. unsigned int ulcon;
  1110. unsigned int ucon;
  1111. unsigned int ubrdiv;
  1112. unsigned long rate;
  1113. unsigned int clk_sel;
  1114. char clk_name[MAX_CLK_NAME_LENGTH];
  1115. ulcon = rd_regl(port, S3C2410_ULCON);
  1116. ucon = rd_regl(port, S3C2410_UCON);
  1117. ubrdiv = rd_regl(port, S3C2410_UBRDIV);
  1118. dbg("s3c24xx_serial_get_options: port=%p\n"
  1119. "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
  1120. port, ulcon, ucon, ubrdiv);
  1121. if ((ucon & 0xf) != 0) {
  1122. /* consider the serial port configured if the tx/rx mode set */
  1123. switch (ulcon & S3C2410_LCON_CSMASK) {
  1124. case S3C2410_LCON_CS5:
  1125. *bits = 5;
  1126. break;
  1127. case S3C2410_LCON_CS6:
  1128. *bits = 6;
  1129. break;
  1130. case S3C2410_LCON_CS7:
  1131. *bits = 7;
  1132. break;
  1133. default:
  1134. case S3C2410_LCON_CS8:
  1135. *bits = 8;
  1136. break;
  1137. }
  1138. switch (ulcon & S3C2410_LCON_PMASK) {
  1139. case S3C2410_LCON_PEVEN:
  1140. *parity = 'e';
  1141. break;
  1142. case S3C2410_LCON_PODD:
  1143. *parity = 'o';
  1144. break;
  1145. case S3C2410_LCON_PNONE:
  1146. default:
  1147. *parity = 'n';
  1148. }
  1149. /* now calculate the baud rate */
  1150. clk_sel = s3c24xx_serial_getsource(port);
  1151. sprintf(clk_name, "clk_uart_baud%d", clk_sel);
  1152. clk = clk_get(port->dev, clk_name);
  1153. if (!IS_ERR(clk))
  1154. rate = clk_get_rate(clk);
  1155. else
  1156. rate = 1;
  1157. *baud = rate / (16 * (ubrdiv + 1));
  1158. dbg("calculated baud %d\n", *baud);
  1159. }
  1160. }
  1161. static int __init
  1162. s3c24xx_serial_console_setup(struct console *co, char *options)
  1163. {
  1164. struct uart_port *port;
  1165. int baud = 9600;
  1166. int bits = 8;
  1167. int parity = 'n';
  1168. int flow = 'n';
  1169. dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
  1170. co, co->index, options);
  1171. /* is this a valid port */
  1172. if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
  1173. co->index = 0;
  1174. port = &s3c24xx_serial_ports[co->index].port;
  1175. /* is the port configured? */
  1176. if (port->mapbase == 0x0)
  1177. return -ENODEV;
  1178. cons_uart = port;
  1179. dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
  1180. /*
  1181. * Check whether an invalid uart number has been specified, and
  1182. * if so, search for the first available port that does have
  1183. * console support.
  1184. */
  1185. if (options)
  1186. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1187. else
  1188. s3c24xx_serial_get_options(port, &baud, &parity, &bits);
  1189. dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
  1190. return uart_set_options(port, co, baud, parity, bits, flow);
  1191. }
  1192. static struct console s3c24xx_serial_console = {
  1193. .name = S3C24XX_SERIAL_NAME,
  1194. .device = uart_console_device,
  1195. .flags = CON_PRINTBUFFER,
  1196. .index = -1,
  1197. .write = s3c24xx_serial_console_write,
  1198. .setup = s3c24xx_serial_console_setup,
  1199. .data = &s3c24xx_uart_drv,
  1200. };
  1201. #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
  1202. #ifdef CONFIG_CPU_S3C2410
  1203. static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
  1204. .info = &(struct s3c24xx_uart_info) {
  1205. .name = "Samsung S3C2410 UART",
  1206. .type = PORT_S3C2410,
  1207. .fifosize = 16,
  1208. .rx_fifomask = S3C2410_UFSTAT_RXMASK,
  1209. .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
  1210. .rx_fifofull = S3C2410_UFSTAT_RXFULL,
  1211. .tx_fifofull = S3C2410_UFSTAT_TXFULL,
  1212. .tx_fifomask = S3C2410_UFSTAT_TXMASK,
  1213. .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
  1214. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1215. .num_clks = 2,
  1216. .clksel_mask = S3C2410_UCON_CLKMASK,
  1217. .clksel_shift = S3C2410_UCON_CLKSHIFT,
  1218. },
  1219. .def_cfg = &(struct s3c2410_uartcfg) {
  1220. .ucon = S3C2410_UCON_DEFAULT,
  1221. .ufcon = S3C2410_UFCON_DEFAULT,
  1222. },
  1223. };
  1224. #define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
  1225. #else
  1226. #define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1227. #endif
  1228. #ifdef CONFIG_CPU_S3C2412
  1229. static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
  1230. .info = &(struct s3c24xx_uart_info) {
  1231. .name = "Samsung S3C2412 UART",
  1232. .type = PORT_S3C2412,
  1233. .fifosize = 64,
  1234. .has_divslot = 1,
  1235. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1236. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1237. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1238. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1239. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1240. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1241. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1242. .num_clks = 4,
  1243. .clksel_mask = S3C2412_UCON_CLKMASK,
  1244. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1245. },
  1246. .def_cfg = &(struct s3c2410_uartcfg) {
  1247. .ucon = S3C2410_UCON_DEFAULT,
  1248. .ufcon = S3C2410_UFCON_DEFAULT,
  1249. },
  1250. };
  1251. #define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
  1252. #else
  1253. #define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1254. #endif
  1255. #if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
  1256. defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
  1257. static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
  1258. .info = &(struct s3c24xx_uart_info) {
  1259. .name = "Samsung S3C2440 UART",
  1260. .type = PORT_S3C2440,
  1261. .fifosize = 64,
  1262. .has_divslot = 1,
  1263. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1264. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1265. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1266. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1267. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1268. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1269. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1270. .num_clks = 4,
  1271. .clksel_mask = S3C2412_UCON_CLKMASK,
  1272. .clksel_shift = S3C2412_UCON_CLKSHIFT,
  1273. },
  1274. .def_cfg = &(struct s3c2410_uartcfg) {
  1275. .ucon = S3C2410_UCON_DEFAULT,
  1276. .ufcon = S3C2410_UFCON_DEFAULT,
  1277. },
  1278. };
  1279. #define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
  1280. #else
  1281. #define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1282. #endif
  1283. #if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410) || \
  1284. defined(CONFIG_CPU_S5P6440) || defined(CONFIG_CPU_S5P6450) || \
  1285. defined(CONFIG_CPU_S5PC100)
  1286. static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
  1287. .info = &(struct s3c24xx_uart_info) {
  1288. .name = "Samsung S3C6400 UART",
  1289. .type = PORT_S3C6400,
  1290. .fifosize = 64,
  1291. .has_divslot = 1,
  1292. .rx_fifomask = S3C2440_UFSTAT_RXMASK,
  1293. .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
  1294. .rx_fifofull = S3C2440_UFSTAT_RXFULL,
  1295. .tx_fifofull = S3C2440_UFSTAT_TXFULL,
  1296. .tx_fifomask = S3C2440_UFSTAT_TXMASK,
  1297. .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
  1298. .def_clk_sel = S3C2410_UCON_CLKSEL2,
  1299. .num_clks = 4,
  1300. .clksel_mask = S3C6400_UCON_CLKMASK,
  1301. .clksel_shift = S3C6400_UCON_CLKSHIFT,
  1302. },
  1303. .def_cfg = &(struct s3c2410_uartcfg) {
  1304. .ucon = S3C2410_UCON_DEFAULT,
  1305. .ufcon = S3C2410_UFCON_DEFAULT,
  1306. },
  1307. };
  1308. #define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
  1309. #else
  1310. #define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1311. #endif
  1312. #ifdef CONFIG_CPU_S5PV210
  1313. static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
  1314. .info = &(struct s3c24xx_uart_info) {
  1315. .name = "Samsung S5PV210 UART",
  1316. .type = PORT_S3C6400,
  1317. .has_divslot = 1,
  1318. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1319. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1320. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1321. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1322. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1323. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1324. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1325. .num_clks = 2,
  1326. .clksel_mask = S5PV210_UCON_CLKMASK,
  1327. .clksel_shift = S5PV210_UCON_CLKSHIFT,
  1328. },
  1329. .def_cfg = &(struct s3c2410_uartcfg) {
  1330. .ucon = S5PV210_UCON_DEFAULT,
  1331. .ufcon = S5PV210_UFCON_DEFAULT,
  1332. },
  1333. .fifosize = { 256, 64, 16, 16 },
  1334. };
  1335. #define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
  1336. #else
  1337. #define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1338. #endif
  1339. #if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) || \
  1340. defined(CONFIG_SOC_EXYNOS4412) || defined(CONFIG_SOC_EXYNOS5250) || \
  1341. defined(CONFIG_SOC_EXYNOS5440)
  1342. static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
  1343. .info = &(struct s3c24xx_uart_info) {
  1344. .name = "Samsung Exynos4 UART",
  1345. .type = PORT_S3C6400,
  1346. .has_divslot = 1,
  1347. .rx_fifomask = S5PV210_UFSTAT_RXMASK,
  1348. .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
  1349. .rx_fifofull = S5PV210_UFSTAT_RXFULL,
  1350. .tx_fifofull = S5PV210_UFSTAT_TXFULL,
  1351. .tx_fifomask = S5PV210_UFSTAT_TXMASK,
  1352. .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
  1353. .def_clk_sel = S3C2410_UCON_CLKSEL0,
  1354. .num_clks = 1,
  1355. .clksel_mask = 0,
  1356. .clksel_shift = 0,
  1357. },
  1358. .def_cfg = &(struct s3c2410_uartcfg) {
  1359. .ucon = S5PV210_UCON_DEFAULT,
  1360. .ufcon = S5PV210_UFCON_DEFAULT,
  1361. .has_fracval = 1,
  1362. },
  1363. .fifosize = { 256, 64, 16, 16 },
  1364. };
  1365. #define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
  1366. #else
  1367. #define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
  1368. #endif
  1369. static struct platform_device_id s3c24xx_serial_driver_ids[] = {
  1370. {
  1371. .name = "s3c2410-uart",
  1372. .driver_data = S3C2410_SERIAL_DRV_DATA,
  1373. }, {
  1374. .name = "s3c2412-uart",
  1375. .driver_data = S3C2412_SERIAL_DRV_DATA,
  1376. }, {
  1377. .name = "s3c2440-uart",
  1378. .driver_data = S3C2440_SERIAL_DRV_DATA,
  1379. }, {
  1380. .name = "s3c6400-uart",
  1381. .driver_data = S3C6400_SERIAL_DRV_DATA,
  1382. }, {
  1383. .name = "s5pv210-uart",
  1384. .driver_data = S5PV210_SERIAL_DRV_DATA,
  1385. }, {
  1386. .name = "exynos4210-uart",
  1387. .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
  1388. },
  1389. { },
  1390. };
  1391. MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
  1392. #ifdef CONFIG_OF
  1393. static const struct of_device_id s3c24xx_uart_dt_match[] = {
  1394. { .compatible = "samsung,s3c2410-uart",
  1395. .data = (void *)S3C2410_SERIAL_DRV_DATA },
  1396. { .compatible = "samsung,s3c2412-uart",
  1397. .data = (void *)S3C2412_SERIAL_DRV_DATA },
  1398. { .compatible = "samsung,s3c2440-uart",
  1399. .data = (void *)S3C2440_SERIAL_DRV_DATA },
  1400. { .compatible = "samsung,s3c6400-uart",
  1401. .data = (void *)S3C6400_SERIAL_DRV_DATA },
  1402. { .compatible = "samsung,s5pv210-uart",
  1403. .data = (void *)S5PV210_SERIAL_DRV_DATA },
  1404. { .compatible = "samsung,exynos4210-uart",
  1405. .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
  1406. {},
  1407. };
  1408. MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
  1409. #else
  1410. #define s3c24xx_uart_dt_match NULL
  1411. #endif
  1412. static struct platform_driver samsung_serial_driver = {
  1413. .probe = s3c24xx_serial_probe,
  1414. .remove = s3c24xx_serial_remove,
  1415. .id_table = s3c24xx_serial_driver_ids,
  1416. .driver = {
  1417. .name = "samsung-uart",
  1418. .owner = THIS_MODULE,
  1419. .pm = SERIAL_SAMSUNG_PM_OPS,
  1420. .of_match_table = s3c24xx_uart_dt_match,
  1421. },
  1422. };
  1423. /* module initialisation code */
  1424. static int __init s3c24xx_serial_modinit(void)
  1425. {
  1426. int ret;
  1427. ret = uart_register_driver(&s3c24xx_uart_drv);
  1428. if (ret < 0) {
  1429. pr_err("Failed to register Samsung UART driver\n");
  1430. return ret;
  1431. }
  1432. return platform_driver_register(&samsung_serial_driver);
  1433. }
  1434. static void __exit s3c24xx_serial_modexit(void)
  1435. {
  1436. uart_unregister_driver(&s3c24xx_uart_drv);
  1437. }
  1438. module_init(s3c24xx_serial_modinit);
  1439. module_exit(s3c24xx_serial_modexit);
  1440. MODULE_ALIAS("platform:samsung-uart");
  1441. MODULE_DESCRIPTION("Samsung SoC Serial port driver");
  1442. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1443. MODULE_LICENSE("GPL v2");