clps711x.c 14 KB

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