samsung.c 42 KB

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