samsung.c 36 KB

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