clps711x.c 13 KB

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