samsung.c 37 KB

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