samsung.c 44 KB

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