altera_uart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * altera_uart.c -- Altera UART driver
  3. *
  4. * Based on mcf.c -- Freescale ColdFire UART driver
  5. *
  6. * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
  7. * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
  8. * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/console.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/serial.h>
  23. #include <linux/serial_core.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/io.h>
  26. #include <linux/altera_uart.h>
  27. #define DRV_NAME "altera_uart"
  28. /*
  29. * Altera UART register definitions according to the Nios UART datasheet:
  30. * http://www.altera.com/literature/ds/ds_nios_uart.pdf
  31. */
  32. #define ALTERA_UART_SIZE 32
  33. #define ALTERA_UART_RXDATA_REG 0
  34. #define ALTERA_UART_TXDATA_REG 4
  35. #define ALTERA_UART_STATUS_REG 8
  36. #define ALTERA_UART_CONTROL_REG 12
  37. #define ALTERA_UART_DIVISOR_REG 16
  38. #define ALTERA_UART_EOP_REG 20
  39. #define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
  40. #define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
  41. #define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
  42. #define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
  43. #define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
  44. #define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
  45. #define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
  46. #define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
  47. #define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
  48. #define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
  49. #define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
  50. #define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
  51. /* Enable interrupt on... */
  52. #define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
  53. #define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
  54. #define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
  55. #define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
  56. #define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
  57. #define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
  58. #define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
  59. #define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
  60. #define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
  61. #define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
  62. #define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
  63. #define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
  64. #define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
  65. /*
  66. * Local per-uart structure.
  67. */
  68. struct altera_uart {
  69. struct uart_port port;
  70. unsigned int sigs; /* Local copy of line sigs */
  71. unsigned short imr; /* Local IMR mirror */
  72. };
  73. static unsigned int altera_uart_tx_empty(struct uart_port *port)
  74. {
  75. return (readl(port->membase + ALTERA_UART_STATUS_REG) &
  76. ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
  77. }
  78. static unsigned int altera_uart_get_mctrl(struct uart_port *port)
  79. {
  80. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  81. unsigned int sigs;
  82. sigs =
  83. (readl(port->membase + ALTERA_UART_STATUS_REG) &
  84. ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
  85. sigs |= (pp->sigs & TIOCM_RTS);
  86. return sigs;
  87. }
  88. static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
  89. {
  90. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  91. pp->sigs = sigs;
  92. if (sigs & TIOCM_RTS)
  93. pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
  94. else
  95. pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
  96. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  97. }
  98. static void altera_uart_start_tx(struct uart_port *port)
  99. {
  100. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  101. pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
  102. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  103. }
  104. static void altera_uart_stop_tx(struct uart_port *port)
  105. {
  106. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  107. pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
  108. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  109. }
  110. static void altera_uart_stop_rx(struct uart_port *port)
  111. {
  112. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  113. pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
  114. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  115. }
  116. static void altera_uart_break_ctl(struct uart_port *port, int break_state)
  117. {
  118. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  119. unsigned long flags;
  120. spin_lock_irqsave(&port->lock, flags);
  121. if (break_state == -1)
  122. pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
  123. else
  124. pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
  125. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  126. spin_unlock_irqrestore(&port->lock, flags);
  127. }
  128. static void altera_uart_enable_ms(struct uart_port *port)
  129. {
  130. }
  131. static void altera_uart_set_termios(struct uart_port *port,
  132. struct ktermios *termios,
  133. struct ktermios *old)
  134. {
  135. unsigned long flags;
  136. unsigned int baud, baudclk;
  137. baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  138. baudclk = port->uartclk / baud;
  139. if (old)
  140. tty_termios_copy_hw(termios, old);
  141. tty_termios_encode_baud_rate(termios, baud, baud);
  142. spin_lock_irqsave(&port->lock, flags);
  143. writel(baudclk, port->membase + ALTERA_UART_DIVISOR_REG);
  144. spin_unlock_irqrestore(&port->lock, flags);
  145. }
  146. static void altera_uart_rx_chars(struct altera_uart *pp)
  147. {
  148. struct uart_port *port = &pp->port;
  149. unsigned char ch, flag;
  150. unsigned short status;
  151. while ((status = readl(port->membase + ALTERA_UART_STATUS_REG)) &
  152. ALTERA_UART_STATUS_RRDY_MSK) {
  153. ch = readl(port->membase + ALTERA_UART_RXDATA_REG);
  154. flag = TTY_NORMAL;
  155. port->icount.rx++;
  156. if (status & ALTERA_UART_STATUS_E_MSK) {
  157. writel(status, port->membase + ALTERA_UART_STATUS_REG);
  158. if (status & ALTERA_UART_STATUS_BRK_MSK) {
  159. port->icount.brk++;
  160. if (uart_handle_break(port))
  161. continue;
  162. } else if (status & ALTERA_UART_STATUS_PE_MSK) {
  163. port->icount.parity++;
  164. } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
  165. port->icount.overrun++;
  166. } else if (status & ALTERA_UART_STATUS_FE_MSK) {
  167. port->icount.frame++;
  168. }
  169. status &= port->read_status_mask;
  170. if (status & ALTERA_UART_STATUS_BRK_MSK)
  171. flag = TTY_BREAK;
  172. else if (status & ALTERA_UART_STATUS_PE_MSK)
  173. flag = TTY_PARITY;
  174. else if (status & ALTERA_UART_STATUS_FE_MSK)
  175. flag = TTY_FRAME;
  176. }
  177. if (uart_handle_sysrq_char(port, ch))
  178. continue;
  179. uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
  180. flag);
  181. }
  182. tty_flip_buffer_push(port->state->port.tty);
  183. }
  184. static void altera_uart_tx_chars(struct altera_uart *pp)
  185. {
  186. struct uart_port *port = &pp->port;
  187. struct circ_buf *xmit = &port->state->xmit;
  188. if (port->x_char) {
  189. /* Send special char - probably flow control */
  190. writel(port->x_char, port->membase + ALTERA_UART_TXDATA_REG);
  191. port->x_char = 0;
  192. port->icount.tx++;
  193. return;
  194. }
  195. while (readl(port->membase + ALTERA_UART_STATUS_REG) &
  196. ALTERA_UART_STATUS_TRDY_MSK) {
  197. if (xmit->head == xmit->tail)
  198. break;
  199. writel(xmit->buf[xmit->tail],
  200. port->membase + ALTERA_UART_TXDATA_REG);
  201. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  202. port->icount.tx++;
  203. }
  204. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  205. uart_write_wakeup(port);
  206. if (xmit->head == xmit->tail) {
  207. pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
  208. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  209. }
  210. }
  211. static irqreturn_t altera_uart_interrupt(int irq, void *data)
  212. {
  213. struct uart_port *port = data;
  214. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  215. unsigned int isr;
  216. isr = readl(port->membase + ALTERA_UART_STATUS_REG) & pp->imr;
  217. spin_lock(&port->lock);
  218. if (isr & ALTERA_UART_STATUS_RRDY_MSK)
  219. altera_uart_rx_chars(pp);
  220. if (isr & ALTERA_UART_STATUS_TRDY_MSK)
  221. altera_uart_tx_chars(pp);
  222. spin_unlock(&port->lock);
  223. return IRQ_RETVAL(isr);
  224. }
  225. static void altera_uart_config_port(struct uart_port *port, int flags)
  226. {
  227. port->type = PORT_ALTERA_UART;
  228. /* Clear mask, so no surprise interrupts. */
  229. writel(0, port->membase + ALTERA_UART_CONTROL_REG);
  230. /* Clear status register */
  231. writel(0, port->membase + ALTERA_UART_STATUS_REG);
  232. }
  233. static int altera_uart_startup(struct uart_port *port)
  234. {
  235. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  236. unsigned long flags;
  237. int ret;
  238. ret = request_irq(port->irq, altera_uart_interrupt, IRQF_DISABLED,
  239. DRV_NAME, port);
  240. if (ret) {
  241. pr_err(DRV_NAME ": unable to attach Altera UART %d "
  242. "interrupt vector=%d\n", port->line, port->irq);
  243. return ret;
  244. }
  245. spin_lock_irqsave(&port->lock, flags);
  246. /* Enable RX interrupts now */
  247. pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
  248. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  249. spin_unlock_irqrestore(&port->lock, flags);
  250. return 0;
  251. }
  252. static void altera_uart_shutdown(struct uart_port *port)
  253. {
  254. struct altera_uart *pp = container_of(port, struct altera_uart, port);
  255. unsigned long flags;
  256. spin_lock_irqsave(&port->lock, flags);
  257. /* Disable all interrupts now */
  258. pp->imr = 0;
  259. writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
  260. spin_unlock_irqrestore(&port->lock, flags);
  261. free_irq(port->irq, port);
  262. }
  263. static const char *altera_uart_type(struct uart_port *port)
  264. {
  265. return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
  266. }
  267. static int altera_uart_request_port(struct uart_port *port)
  268. {
  269. /* UARTs always present */
  270. return 0;
  271. }
  272. static void altera_uart_release_port(struct uart_port *port)
  273. {
  274. /* Nothing to release... */
  275. }
  276. static int altera_uart_verify_port(struct uart_port *port,
  277. struct serial_struct *ser)
  278. {
  279. if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
  280. return -EINVAL;
  281. return 0;
  282. }
  283. /*
  284. * Define the basic serial functions we support.
  285. */
  286. static struct uart_ops altera_uart_ops = {
  287. .tx_empty = altera_uart_tx_empty,
  288. .get_mctrl = altera_uart_get_mctrl,
  289. .set_mctrl = altera_uart_set_mctrl,
  290. .start_tx = altera_uart_start_tx,
  291. .stop_tx = altera_uart_stop_tx,
  292. .stop_rx = altera_uart_stop_rx,
  293. .enable_ms = altera_uart_enable_ms,
  294. .break_ctl = altera_uart_break_ctl,
  295. .startup = altera_uart_startup,
  296. .shutdown = altera_uart_shutdown,
  297. .set_termios = altera_uart_set_termios,
  298. .type = altera_uart_type,
  299. .request_port = altera_uart_request_port,
  300. .release_port = altera_uart_release_port,
  301. .config_port = altera_uart_config_port,
  302. .verify_port = altera_uart_verify_port,
  303. };
  304. static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
  305. #if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
  306. int __init early_altera_uart_setup(struct altera_uart_platform_uart *platp)
  307. {
  308. struct uart_port *port;
  309. int i;
  310. for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS && platp[i].mapbase; i++) {
  311. port = &altera_uart_ports[i].port;
  312. port->line = i;
  313. port->type = PORT_ALTERA_UART;
  314. port->mapbase = platp[i].mapbase;
  315. port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
  316. port->iotype = SERIAL_IO_MEM;
  317. port->irq = platp[i].irq;
  318. port->uartclk = platp[i].uartclk;
  319. port->flags = ASYNC_BOOT_AUTOCONF;
  320. port->ops = &altera_uart_ops;
  321. }
  322. return 0;
  323. }
  324. static void altera_uart_console_putc(struct uart_port *port, const char c)
  325. {
  326. while (!(readl(port->membase + ALTERA_UART_STATUS_REG) &
  327. ALTERA_UART_STATUS_TRDY_MSK))
  328. cpu_relax();
  329. writel(c, port->membase + ALTERA_UART_TXDATA_REG);
  330. }
  331. static void altera_uart_console_write(struct console *co, const char *s,
  332. unsigned int count)
  333. {
  334. struct uart_port *port = &(altera_uart_ports + co->index)->port;
  335. for (; count; count--, s++) {
  336. altera_uart_console_putc(port, *s);
  337. if (*s == '\n')
  338. altera_uart_console_putc(port, '\r');
  339. }
  340. }
  341. static int __init altera_uart_console_setup(struct console *co, char *options)
  342. {
  343. struct uart_port *port;
  344. int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
  345. int bits = 8;
  346. int parity = 'n';
  347. int flow = 'n';
  348. if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
  349. return -EINVAL;
  350. port = &altera_uart_ports[co->index].port;
  351. if (port->membase == 0)
  352. return -ENODEV;
  353. if (options)
  354. uart_parse_options(options, &baud, &parity, &bits, &flow);
  355. return uart_set_options(port, co, baud, parity, bits, flow);
  356. }
  357. static struct uart_driver altera_uart_driver;
  358. static struct console altera_uart_console = {
  359. .name = "ttyS",
  360. .write = altera_uart_console_write,
  361. .device = uart_console_device,
  362. .setup = altera_uart_console_setup,
  363. .flags = CON_PRINTBUFFER,
  364. .index = -1,
  365. .data = &altera_uart_driver,
  366. };
  367. static int __init altera_uart_console_init(void)
  368. {
  369. register_console(&altera_uart_console);
  370. return 0;
  371. }
  372. console_initcall(altera_uart_console_init);
  373. #define ALTERA_UART_CONSOLE (&altera_uart_console)
  374. #else
  375. #define ALTERA_UART_CONSOLE NULL
  376. #endif /* CONFIG_ALTERA_UART_CONSOLE */
  377. /*
  378. * Define the altera_uart UART driver structure.
  379. */
  380. static struct uart_driver altera_uart_driver = {
  381. .owner = THIS_MODULE,
  382. .driver_name = DRV_NAME,
  383. .dev_name = "ttyS",
  384. .major = TTY_MAJOR,
  385. .minor = 64,
  386. .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
  387. .cons = ALTERA_UART_CONSOLE,
  388. };
  389. static int __devinit altera_uart_probe(struct platform_device *pdev)
  390. {
  391. struct altera_uart_platform_uart *platp = pdev->dev.platform_data;
  392. struct uart_port *port;
  393. int i;
  394. for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS && platp[i].mapbase; i++) {
  395. port = &altera_uart_ports[i].port;
  396. port->line = i;
  397. port->type = PORT_ALTERA_UART;
  398. port->mapbase = platp[i].mapbase;
  399. port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
  400. port->iotype = SERIAL_IO_MEM;
  401. port->irq = platp[i].irq;
  402. port->uartclk = platp[i].uartclk;
  403. port->ops = &altera_uart_ops;
  404. port->flags = ASYNC_BOOT_AUTOCONF;
  405. uart_add_one_port(&altera_uart_driver, port);
  406. }
  407. return 0;
  408. }
  409. static int __devexit altera_uart_remove(struct platform_device *pdev)
  410. {
  411. struct uart_port *port;
  412. int i;
  413. for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++) {
  414. port = &altera_uart_ports[i].port;
  415. if (port)
  416. uart_remove_one_port(&altera_uart_driver, port);
  417. }
  418. return 0;
  419. }
  420. static struct platform_driver altera_uart_platform_driver = {
  421. .probe = altera_uart_probe,
  422. .remove = __devexit_p(altera_uart_remove),
  423. .driver = {
  424. .name = DRV_NAME,
  425. .owner = THIS_MODULE,
  426. .pm = NULL,
  427. },
  428. };
  429. static int __init altera_uart_init(void)
  430. {
  431. int rc;
  432. rc = uart_register_driver(&altera_uart_driver);
  433. if (rc)
  434. return rc;
  435. rc = platform_driver_register(&altera_uart_platform_driver);
  436. if (rc) {
  437. uart_unregister_driver(&altera_uart_driver);
  438. return rc;
  439. }
  440. return 0;
  441. }
  442. static void __exit altera_uart_exit(void)
  443. {
  444. platform_driver_unregister(&altera_uart_platform_driver);
  445. uart_unregister_driver(&altera_uart_driver);
  446. }
  447. module_init(altera_uart_init);
  448. module_exit(altera_uart_exit);
  449. MODULE_DESCRIPTION("Altera UART driver");
  450. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  451. MODULE_LICENSE("GPL");
  452. MODULE_ALIAS("platform:" DRV_NAME);