clps711x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Driver for CLPS711x serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Copyright 1999 ARM Limited
  7. * Copyright (C) 2000 Deep Blue Solutions Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  15. #define SUPPORT_SYSRQ
  16. #endif
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/console.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/serial.h>
  22. #include <linux/io.h>
  23. #include <linux/clk.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/ioport.h>
  27. #include <linux/platform_device.h>
  28. #include <mach/hardware.h>
  29. #define UART_CLPS711X_NAME "uart-clps711x"
  30. #define UART_CLPS711X_NR 2
  31. #define UART_CLPS711X_MAJOR 204
  32. #define UART_CLPS711X_MINOR 40
  33. #define UBRLCR(port) ((port)->line ? UBRLCR2 : UBRLCR1)
  34. #define UARTDR(port) ((port)->line ? UARTDR2 : UARTDR1)
  35. #define SYSFLG(port) ((port)->line ? SYSFLG2 : SYSFLG1)
  36. #define SYSCON(port) ((port)->line ? SYSCON2 : SYSCON1)
  37. #define TX_IRQ(port) ((port)->line ? IRQ_UTXINT2 : IRQ_UTXINT1)
  38. #define RX_IRQ(port) ((port)->line ? IRQ_URXINT2 : IRQ_URXINT1)
  39. struct clps711x_port {
  40. struct uart_driver uart;
  41. struct clk *uart_clk;
  42. struct uart_port port[UART_CLPS711X_NR];
  43. int tx_enabled[UART_CLPS711X_NR];
  44. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  45. struct console console;
  46. #endif
  47. };
  48. static void uart_clps711x_stop_tx(struct uart_port *port)
  49. {
  50. struct clps711x_port *s = dev_get_drvdata(port->dev);
  51. if (s->tx_enabled[port->line]) {
  52. disable_irq(TX_IRQ(port));
  53. s->tx_enabled[port->line] = 0;
  54. }
  55. }
  56. static void uart_clps711x_start_tx(struct uart_port *port)
  57. {
  58. struct clps711x_port *s = dev_get_drvdata(port->dev);
  59. if (!s->tx_enabled[port->line]) {
  60. enable_irq(TX_IRQ(port));
  61. s->tx_enabled[port->line] = 1;
  62. }
  63. }
  64. static void uart_clps711x_stop_rx(struct uart_port *port)
  65. {
  66. disable_irq(RX_IRQ(port));
  67. }
  68. static void uart_clps711x_enable_ms(struct uart_port *port)
  69. {
  70. /* Do nothing */
  71. }
  72. static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
  73. {
  74. struct uart_port *port = dev_id;
  75. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  76. unsigned int status, ch, flg;
  77. if (!tty)
  78. return IRQ_HANDLED;
  79. for (;;) {
  80. status = clps_readl(SYSFLG(port));
  81. if (status & SYSFLG_URXFE)
  82. break;
  83. ch = clps_readw(UARTDR(port));
  84. status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
  85. ch &= 0xff;
  86. port->icount.rx++;
  87. flg = TTY_NORMAL;
  88. if (unlikely(status)) {
  89. if (status & UARTDR_PARERR)
  90. port->icount.parity++;
  91. else if (status & UARTDR_FRMERR)
  92. port->icount.frame++;
  93. else if (status & UARTDR_OVERR)
  94. port->icount.overrun++;
  95. status &= port->read_status_mask;
  96. if (status & UARTDR_PARERR)
  97. flg = TTY_PARITY;
  98. else if (status & UARTDR_FRMERR)
  99. flg = TTY_FRAME;
  100. else if (status & UARTDR_OVERR)
  101. flg = TTY_OVERRUN;
  102. }
  103. if (uart_handle_sysrq_char(port, ch))
  104. continue;
  105. if (status & port->ignore_status_mask)
  106. continue;
  107. uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
  108. }
  109. tty_flip_buffer_push(tty);
  110. tty_kref_put(tty);
  111. return IRQ_HANDLED;
  112. }
  113. static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
  114. {
  115. struct uart_port *port = dev_id;
  116. struct clps711x_port *s = dev_get_drvdata(port->dev);
  117. struct circ_buf *xmit = &port->state->xmit;
  118. if (port->x_char) {
  119. clps_writew(port->x_char, UARTDR(port));
  120. port->icount.tx++;
  121. port->x_char = 0;
  122. return IRQ_HANDLED;
  123. }
  124. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  125. disable_irq_nosync(TX_IRQ(port));
  126. s->tx_enabled[port->line] = 0;
  127. return IRQ_HANDLED;
  128. }
  129. while (!uart_circ_empty(xmit)) {
  130. clps_writew(xmit->buf[xmit->tail], UARTDR(port));
  131. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  132. port->icount.tx++;
  133. if (clps_readl(SYSFLG(port) & SYSFLG_UTXFF))
  134. break;
  135. }
  136. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  137. uart_write_wakeup(port);
  138. return IRQ_HANDLED;
  139. }
  140. static unsigned int uart_clps711x_tx_empty(struct uart_port *port)
  141. {
  142. return (clps_readl(SYSFLG(port) & SYSFLG_UBUSY)) ? 0 : TIOCSER_TEMT;
  143. }
  144. static unsigned int uart_clps711x_get_mctrl(struct uart_port *port)
  145. {
  146. unsigned int status, result = 0;
  147. if (port->line == 0) {
  148. status = clps_readl(SYSFLG1);
  149. if (status & SYSFLG1_DCD)
  150. result |= TIOCM_CAR;
  151. if (status & SYSFLG1_DSR)
  152. result |= TIOCM_DSR;
  153. if (status & SYSFLG1_CTS)
  154. result |= TIOCM_CTS;
  155. } else
  156. result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
  157. return result;
  158. }
  159. static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl)
  160. {
  161. /* Do nothing */
  162. }
  163. static void uart_clps711x_break_ctl(struct uart_port *port, int break_state)
  164. {
  165. unsigned long flags;
  166. unsigned int ubrlcr;
  167. spin_lock_irqsave(&port->lock, flags);
  168. ubrlcr = clps_readl(UBRLCR(port));
  169. if (break_state)
  170. ubrlcr |= UBRLCR_BREAK;
  171. else
  172. ubrlcr &= ~UBRLCR_BREAK;
  173. clps_writel(ubrlcr, UBRLCR(port));
  174. spin_unlock_irqrestore(&port->lock, flags);
  175. }
  176. static int uart_clps711x_startup(struct uart_port *port)
  177. {
  178. struct clps711x_port *s = dev_get_drvdata(port->dev);
  179. int ret;
  180. s->tx_enabled[port->line] = 1;
  181. /* Allocate the IRQs */
  182. ret = devm_request_irq(port->dev, TX_IRQ(port), uart_clps711x_int_tx,
  183. 0, UART_CLPS711X_NAME " TX", port);
  184. if (ret)
  185. return ret;
  186. ret = devm_request_irq(port->dev, RX_IRQ(port), uart_clps711x_int_rx,
  187. 0, UART_CLPS711X_NAME " RX", port);
  188. if (ret) {
  189. devm_free_irq(port->dev, TX_IRQ(port), port);
  190. return ret;
  191. }
  192. /* Disable break */
  193. clps_writel(clps_readl(UBRLCR(port)) & ~UBRLCR_BREAK, UBRLCR(port));
  194. /* Enable the port */
  195. clps_writel(clps_readl(SYSCON(port)) | SYSCON_UARTEN, SYSCON(port));
  196. return 0;
  197. }
  198. static void uart_clps711x_shutdown(struct uart_port *port)
  199. {
  200. /* Free the interrupts */
  201. devm_free_irq(port->dev, TX_IRQ(port), port);
  202. devm_free_irq(port->dev, RX_IRQ(port), port);
  203. /* Disable the port */
  204. clps_writel(clps_readl(SYSCON(port)) & ~SYSCON_UARTEN, SYSCON(port));
  205. }
  206. static void uart_clps711x_set_termios(struct uart_port *port,
  207. struct ktermios *termios,
  208. struct ktermios *old)
  209. {
  210. unsigned int ubrlcr, baud, quot;
  211. unsigned long flags;
  212. /* Mask termios capabilities we don't support */
  213. termios->c_cflag &= ~CMSPAR;
  214. termios->c_iflag &= ~(BRKINT | IGNBRK);
  215. /* Ask the core to calculate the divisor for us */
  216. baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
  217. port->uartclk / 16);
  218. quot = uart_get_divisor(port, baud);
  219. switch (termios->c_cflag & CSIZE) {
  220. case CS5:
  221. ubrlcr = UBRLCR_WRDLEN5;
  222. break;
  223. case CS6:
  224. ubrlcr = UBRLCR_WRDLEN6;
  225. break;
  226. case CS7:
  227. ubrlcr = UBRLCR_WRDLEN7;
  228. break;
  229. case CS8:
  230. default:
  231. ubrlcr = UBRLCR_WRDLEN8;
  232. break;
  233. }
  234. if (termios->c_cflag & CSTOPB)
  235. ubrlcr |= UBRLCR_XSTOP;
  236. if (termios->c_cflag & PARENB) {
  237. ubrlcr |= UBRLCR_PRTEN;
  238. if (!(termios->c_cflag & PARODD))
  239. ubrlcr |= UBRLCR_EVENPRT;
  240. }
  241. /* Enable FIFO */
  242. ubrlcr |= UBRLCR_FIFOEN;
  243. spin_lock_irqsave(&port->lock, flags);
  244. /* Set read status mask */
  245. port->read_status_mask = UARTDR_OVERR;
  246. if (termios->c_iflag & INPCK)
  247. port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
  248. /* Set status ignore mask */
  249. port->ignore_status_mask = 0;
  250. if (!(termios->c_cflag & CREAD))
  251. port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
  252. UARTDR_FRMERR;
  253. uart_update_timeout(port, termios->c_cflag, baud);
  254. clps_writel(ubrlcr | (quot - 1), UBRLCR(port));
  255. spin_unlock_irqrestore(&port->lock, flags);
  256. }
  257. static const char *uart_clps711x_type(struct uart_port *port)
  258. {
  259. return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
  260. }
  261. static void uart_clps711x_config_port(struct uart_port *port, int flags)
  262. {
  263. if (flags & UART_CONFIG_TYPE)
  264. port->type = PORT_CLPS711X;
  265. }
  266. static void uart_clps711x_release_port(struct uart_port *port)
  267. {
  268. /* Do nothing */
  269. }
  270. static int uart_clps711x_request_port(struct uart_port *port)
  271. {
  272. /* Do nothing */
  273. return 0;
  274. }
  275. static const struct uart_ops uart_clps711x_ops = {
  276. .tx_empty = uart_clps711x_tx_empty,
  277. .set_mctrl = uart_clps711x_set_mctrl,
  278. .get_mctrl = uart_clps711x_get_mctrl,
  279. .stop_tx = uart_clps711x_stop_tx,
  280. .start_tx = uart_clps711x_start_tx,
  281. .stop_rx = uart_clps711x_stop_rx,
  282. .enable_ms = uart_clps711x_enable_ms,
  283. .break_ctl = uart_clps711x_break_ctl,
  284. .startup = uart_clps711x_startup,
  285. .shutdown = uart_clps711x_shutdown,
  286. .set_termios = uart_clps711x_set_termios,
  287. .type = uart_clps711x_type,
  288. .config_port = uart_clps711x_config_port,
  289. .release_port = uart_clps711x_release_port,
  290. .request_port = uart_clps711x_request_port,
  291. };
  292. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  293. static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
  294. {
  295. while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
  296. barrier();
  297. clps_writew(ch, UARTDR(port));
  298. }
  299. static void uart_clps711x_console_write(struct console *co, const char *c,
  300. unsigned n)
  301. {
  302. struct clps711x_port *s = (struct clps711x_port *)co->data;
  303. struct uart_port *port = &s->port[co->index];
  304. u32 syscon;
  305. /* Ensure that the port is enabled */
  306. syscon = clps_readl(SYSCON(port));
  307. clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
  308. uart_console_write(port, c, n, uart_clps711x_console_putchar);
  309. /* Wait for transmitter to become empty */
  310. while (clps_readl(SYSFLG(port)) & SYSFLG_UBUSY)
  311. barrier();
  312. /* Restore the uart state */
  313. clps_writel(syscon, SYSCON(port));
  314. }
  315. static void uart_clps711x_console_get_options(struct uart_port *port,
  316. int *baud, int *parity,
  317. int *bits)
  318. {
  319. if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
  320. unsigned int ubrlcr, quot;
  321. ubrlcr = clps_readl(UBRLCR(port));
  322. *parity = 'n';
  323. if (ubrlcr & UBRLCR_PRTEN) {
  324. if (ubrlcr & UBRLCR_EVENPRT)
  325. *parity = 'e';
  326. else
  327. *parity = 'o';
  328. }
  329. if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
  330. *bits = 7;
  331. else
  332. *bits = 8;
  333. quot = ubrlcr & UBRLCR_BAUD_MASK;
  334. *baud = port->uartclk / (16 * (quot + 1));
  335. }
  336. }
  337. static int uart_clps711x_console_setup(struct console *co, char *options)
  338. {
  339. int baud = 38400, bits = 8, parity = 'n', flow = 'n';
  340. struct clps711x_port *s = (struct clps711x_port *)co->data;
  341. struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0];
  342. if (options)
  343. uart_parse_options(options, &baud, &parity, &bits, &flow);
  344. else
  345. uart_clps711x_console_get_options(port, &baud, &parity, &bits);
  346. return uart_set_options(port, co, baud, parity, bits, flow);
  347. }
  348. #endif
  349. static int uart_clps711x_probe(struct platform_device *pdev)
  350. {
  351. struct clps711x_port *s;
  352. int ret, i;
  353. s = devm_kzalloc(&pdev->dev, sizeof(struct clps711x_port), GFP_KERNEL);
  354. if (!s) {
  355. dev_err(&pdev->dev, "Error allocating port structure\n");
  356. return -ENOMEM;
  357. }
  358. platform_set_drvdata(pdev, s);
  359. s->uart_clk = devm_clk_get(&pdev->dev, "uart");
  360. if (IS_ERR(s->uart_clk)) {
  361. dev_err(&pdev->dev, "Can't get UART clocks\n");
  362. ret = PTR_ERR(s->uart_clk);
  363. goto err_out;
  364. }
  365. s->uart.owner = THIS_MODULE;
  366. s->uart.dev_name = "ttyCL";
  367. s->uart.major = UART_CLPS711X_MAJOR;
  368. s->uart.minor = UART_CLPS711X_MINOR;
  369. s->uart.nr = UART_CLPS711X_NR;
  370. #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
  371. s->uart.cons = &s->console;
  372. s->uart.cons->device = uart_console_device;
  373. s->uart.cons->write = uart_clps711x_console_write;
  374. s->uart.cons->setup = uart_clps711x_console_setup;
  375. s->uart.cons->flags = CON_PRINTBUFFER;
  376. s->uart.cons->index = -1;
  377. s->uart.cons->data = s;
  378. strcpy(s->uart.cons->name, "ttyCL");
  379. #endif
  380. ret = uart_register_driver(&s->uart);
  381. if (ret) {
  382. dev_err(&pdev->dev, "Registering UART driver failed\n");
  383. devm_clk_put(&pdev->dev, s->uart_clk);
  384. goto err_out;
  385. }
  386. for (i = 0; i < UART_CLPS711X_NR; i++) {
  387. s->port[i].line = i;
  388. s->port[i].dev = &pdev->dev;
  389. s->port[i].irq = TX_IRQ(&s->port[i]);
  390. s->port[i].iobase = SYSCON(&s->port[i]);
  391. s->port[i].type = PORT_CLPS711X;
  392. s->port[i].fifosize = 16;
  393. s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
  394. s->port[i].uartclk = clk_get_rate(s->uart_clk);
  395. s->port[i].ops = &uart_clps711x_ops;
  396. WARN_ON(uart_add_one_port(&s->uart, &s->port[i]));
  397. }
  398. return 0;
  399. err_out:
  400. platform_set_drvdata(pdev, NULL);
  401. return ret;
  402. }
  403. static int uart_clps711x_remove(struct platform_device *pdev)
  404. {
  405. struct clps711x_port *s = platform_get_drvdata(pdev);
  406. int i;
  407. for (i = 0; i < UART_CLPS711X_NR; i++)
  408. uart_remove_one_port(&s->uart, &s->port[i]);
  409. devm_clk_put(&pdev->dev, s->uart_clk);
  410. uart_unregister_driver(&s->uart);
  411. platform_set_drvdata(pdev, NULL);
  412. return 0;
  413. }
  414. static struct platform_driver clps711x_uart_driver = {
  415. .driver = {
  416. .name = UART_CLPS711X_NAME,
  417. .owner = THIS_MODULE,
  418. },
  419. .probe = uart_clps711x_probe,
  420. .remove = uart_clps711x_remove,
  421. };
  422. module_platform_driver(clps711x_uart_driver);
  423. static struct platform_device clps711x_uart_device = {
  424. .name = UART_CLPS711X_NAME,
  425. };
  426. static int __init uart_clps711x_init(void)
  427. {
  428. return platform_device_register(&clps711x_uart_device);
  429. }
  430. module_init(uart_clps711x_init);
  431. static void __exit uart_clps711x_exit(void)
  432. {
  433. platform_device_unregister(&clps711x_uart_device);
  434. }
  435. module_exit(uart_clps711x_exit);
  436. MODULE_AUTHOR("Deep Blue Solutions Ltd");
  437. MODULE_DESCRIPTION("CLPS711X serial driver");
  438. MODULE_LICENSE("GPL");