clps711x.c 13 KB

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