samsung.c 34 KB

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