2 * altera_uart.c -- Altera UART driver
4 * Based on mcf.c -- Freescale ColdFire UART driver
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>
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.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/timer.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/console.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial.h>
25 #include <linux/serial_core.h>
26 #include <linux/platform_device.h>
28 #include <linux/altera_uart.h>
30 #define DRV_NAME "altera_uart"
33 * Altera UART register definitions according to the Nios UART datasheet:
34 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
37 #define ALTERA_UART_SIZE 32
39 #define ALTERA_UART_RXDATA_REG 0
40 #define ALTERA_UART_TXDATA_REG 4
41 #define ALTERA_UART_STATUS_REG 8
42 #define ALTERA_UART_CONTROL_REG 12
43 #define ALTERA_UART_DIVISOR_REG 16
44 #define ALTERA_UART_EOP_REG 20
46 #define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
47 #define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
48 #define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
49 #define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
50 #define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
51 #define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
52 #define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
53 #define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
54 #define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
55 #define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
56 #define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
57 #define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
59 /* Enable interrupt on... */
60 #define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
61 #define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
62 #define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
63 #define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
64 #define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
65 #define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
66 #define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
67 #define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
68 #define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
70 #define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
71 #define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
72 #define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
73 #define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
76 * Local per-uart structure.
79 struct uart_port port;
80 struct timer_list tmr;
81 unsigned int sigs; /* Local copy of line sigs */
82 unsigned short imr; /* Local IMR mirror */
85 static u32 altera_uart_readl(struct uart_port *port, int reg)
87 struct altera_uart_platform_uart *platp = port->private_data;
89 return readl(port->membase + (reg << platp->bus_shift));
92 static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
94 struct altera_uart_platform_uart *platp = port->private_data;
96 writel(dat, port->membase + (reg << platp->bus_shift));
99 static unsigned int altera_uart_tx_empty(struct uart_port *port)
101 return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
102 ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
105 static unsigned int altera_uart_get_mctrl(struct uart_port *port)
107 struct altera_uart *pp = container_of(port, struct altera_uart, port);
110 sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
111 ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
112 sigs |= (pp->sigs & TIOCM_RTS);
117 static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
119 struct altera_uart *pp = container_of(port, struct altera_uart, port);
122 if (sigs & TIOCM_RTS)
123 pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
125 pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
126 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
129 static void altera_uart_start_tx(struct uart_port *port)
131 struct altera_uart *pp = container_of(port, struct altera_uart, port);
133 pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
134 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
137 static void altera_uart_stop_tx(struct uart_port *port)
139 struct altera_uart *pp = container_of(port, struct altera_uart, port);
141 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
142 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
145 static void altera_uart_stop_rx(struct uart_port *port)
147 struct altera_uart *pp = container_of(port, struct altera_uart, port);
149 pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
150 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
153 static void altera_uart_break_ctl(struct uart_port *port, int break_state)
155 struct altera_uart *pp = container_of(port, struct altera_uart, port);
158 spin_lock_irqsave(&port->lock, flags);
159 if (break_state == -1)
160 pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
162 pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
163 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
164 spin_unlock_irqrestore(&port->lock, flags);
167 static void altera_uart_enable_ms(struct uart_port *port)
171 static void altera_uart_set_termios(struct uart_port *port,
172 struct ktermios *termios,
173 struct ktermios *old)
176 unsigned int baud, baudclk;
178 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
179 baudclk = port->uartclk / baud;
182 tty_termios_copy_hw(termios, old);
183 tty_termios_encode_baud_rate(termios, baud, baud);
185 spin_lock_irqsave(&port->lock, flags);
186 uart_update_timeout(port, termios->c_cflag, baud);
187 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
188 spin_unlock_irqrestore(&port->lock, flags);
191 static void altera_uart_rx_chars(struct altera_uart *pp)
193 struct uart_port *port = &pp->port;
194 unsigned char ch, flag;
195 unsigned short status;
197 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
198 ALTERA_UART_STATUS_RRDY_MSK) {
199 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
203 if (status & ALTERA_UART_STATUS_E_MSK) {
204 altera_uart_writel(port, status,
205 ALTERA_UART_STATUS_REG);
207 if (status & ALTERA_UART_STATUS_BRK_MSK) {
209 if (uart_handle_break(port))
211 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
212 port->icount.parity++;
213 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
214 port->icount.overrun++;
215 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
216 port->icount.frame++;
219 status &= port->read_status_mask;
221 if (status & ALTERA_UART_STATUS_BRK_MSK)
223 else if (status & ALTERA_UART_STATUS_PE_MSK)
225 else if (status & ALTERA_UART_STATUS_FE_MSK)
229 if (uart_handle_sysrq_char(port, ch))
231 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
235 tty_flip_buffer_push(port->state->port.tty);
238 static void altera_uart_tx_chars(struct altera_uart *pp)
240 struct uart_port *port = &pp->port;
241 struct circ_buf *xmit = &port->state->xmit;
244 /* Send special char - probably flow control */
245 altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
251 while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
252 ALTERA_UART_STATUS_TRDY_MSK) {
253 if (xmit->head == xmit->tail)
255 altera_uart_writel(port, xmit->buf[xmit->tail],
256 ALTERA_UART_TXDATA_REG);
257 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
261 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
262 uart_write_wakeup(port);
264 if (xmit->head == xmit->tail) {
265 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
266 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
270 static irqreturn_t altera_uart_interrupt(int irq, void *data)
272 struct uart_port *port = data;
273 struct altera_uart *pp = container_of(port, struct altera_uart, port);
276 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
278 spin_lock(&port->lock);
279 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
280 altera_uart_rx_chars(pp);
281 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
282 altera_uart_tx_chars(pp);
283 spin_unlock(&port->lock);
285 return IRQ_RETVAL(isr);
288 static void altera_uart_timer(unsigned long data)
290 struct uart_port *port = (void *)data;
291 struct altera_uart *pp = container_of(port, struct altera_uart, port);
293 altera_uart_interrupt(0, port);
294 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
297 static void altera_uart_config_port(struct uart_port *port, int flags)
299 port->type = PORT_ALTERA_UART;
301 /* Clear mask, so no surprise interrupts. */
302 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
303 /* Clear status register */
304 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
307 static int altera_uart_startup(struct uart_port *port)
309 struct altera_uart *pp = container_of(port, struct altera_uart, port);
314 setup_timer(&pp->tmr, altera_uart_timer, (unsigned long)port);
315 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
319 ret = request_irq(port->irq, altera_uart_interrupt, IRQF_DISABLED,
322 pr_err(DRV_NAME ": unable to attach Altera UART %d "
323 "interrupt vector=%d\n", port->line, port->irq);
327 spin_lock_irqsave(&port->lock, flags);
329 /* Enable RX interrupts now */
330 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
331 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
333 spin_unlock_irqrestore(&port->lock, flags);
338 static void altera_uart_shutdown(struct uart_port *port)
340 struct altera_uart *pp = container_of(port, struct altera_uart, port);
343 spin_lock_irqsave(&port->lock, flags);
345 /* Disable all interrupts now */
347 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
349 spin_unlock_irqrestore(&port->lock, flags);
352 free_irq(port->irq, port);
354 del_timer_sync(&pp->tmr);
357 static const char *altera_uart_type(struct uart_port *port)
359 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
362 static int altera_uart_request_port(struct uart_port *port)
364 /* UARTs always present */
368 static void altera_uart_release_port(struct uart_port *port)
370 /* Nothing to release... */
373 static int altera_uart_verify_port(struct uart_port *port,
374 struct serial_struct *ser)
376 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
382 * Define the basic serial functions we support.
384 static struct uart_ops altera_uart_ops = {
385 .tx_empty = altera_uart_tx_empty,
386 .get_mctrl = altera_uart_get_mctrl,
387 .set_mctrl = altera_uart_set_mctrl,
388 .start_tx = altera_uart_start_tx,
389 .stop_tx = altera_uart_stop_tx,
390 .stop_rx = altera_uart_stop_rx,
391 .enable_ms = altera_uart_enable_ms,
392 .break_ctl = altera_uart_break_ctl,
393 .startup = altera_uart_startup,
394 .shutdown = altera_uart_shutdown,
395 .set_termios = altera_uart_set_termios,
396 .type = altera_uart_type,
397 .request_port = altera_uart_request_port,
398 .release_port = altera_uart_release_port,
399 .config_port = altera_uart_config_port,
400 .verify_port = altera_uart_verify_port,
403 static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
405 #if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
407 int __init early_altera_uart_setup(struct altera_uart_platform_uart *platp)
409 struct uart_port *port;
412 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS && platp[i].mapbase; i++) {
413 port = &altera_uart_ports[i].port;
416 port->type = PORT_ALTERA_UART;
417 port->mapbase = platp[i].mapbase;
418 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
419 port->iotype = SERIAL_IO_MEM;
420 port->irq = platp[i].irq;
421 port->uartclk = platp[i].uartclk;
422 port->flags = ASYNC_BOOT_AUTOCONF;
423 port->ops = &altera_uart_ops;
424 port->private_data = platp;
430 static void altera_uart_console_putc(struct uart_port *port, const char c)
432 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
433 ALTERA_UART_STATUS_TRDY_MSK))
436 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
439 static void altera_uart_console_write(struct console *co, const char *s,
442 struct uart_port *port = &(altera_uart_ports + co->index)->port;
444 for (; count; count--, s++) {
445 altera_uart_console_putc(port, *s);
447 altera_uart_console_putc(port, '\r');
451 static int __init altera_uart_console_setup(struct console *co, char *options)
453 struct uart_port *port;
454 int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
459 if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
461 port = &altera_uart_ports[co->index].port;
462 if (port->membase == 0)
466 uart_parse_options(options, &baud, &parity, &bits, &flow);
468 return uart_set_options(port, co, baud, parity, bits, flow);
471 static struct uart_driver altera_uart_driver;
473 static struct console altera_uart_console = {
475 .write = altera_uart_console_write,
476 .device = uart_console_device,
477 .setup = altera_uart_console_setup,
478 .flags = CON_PRINTBUFFER,
480 .data = &altera_uart_driver,
483 static int __init altera_uart_console_init(void)
485 register_console(&altera_uart_console);
489 console_initcall(altera_uart_console_init);
491 #define ALTERA_UART_CONSOLE (&altera_uart_console)
495 #define ALTERA_UART_CONSOLE NULL
497 #endif /* CONFIG_ALTERA_UART_CONSOLE */
500 * Define the altera_uart UART driver structure.
502 static struct uart_driver altera_uart_driver = {
503 .owner = THIS_MODULE,
504 .driver_name = DRV_NAME,
508 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
509 .cons = ALTERA_UART_CONSOLE,
512 static int __devinit altera_uart_probe(struct platform_device *pdev)
514 struct altera_uart_platform_uart *platp = pdev->dev.platform_data;
515 struct uart_port *port;
516 struct resource *res_mem;
517 struct resource *res_irq;
520 /* -1 emphasizes that the platform must have one port, no .N suffix */
524 if (i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
527 port = &altera_uart_ports[i].port;
529 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
531 port->mapbase = res_mem->start;
532 else if (platp->mapbase)
533 port->mapbase = platp->mapbase;
537 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
539 port->irq = res_irq->start;
541 port->irq = platp->irq;
543 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
548 port->type = PORT_ALTERA_UART;
549 port->iotype = SERIAL_IO_MEM;
550 port->uartclk = platp->uartclk;
551 port->ops = &altera_uart_ops;
552 port->flags = ASYNC_BOOT_AUTOCONF;
553 port->private_data = platp;
555 uart_add_one_port(&altera_uart_driver, port);
560 static int __devexit altera_uart_remove(struct platform_device *pdev)
562 struct uart_port *port = &altera_uart_ports[pdev->id].port;
564 uart_remove_one_port(&altera_uart_driver, port);
568 static struct platform_driver altera_uart_platform_driver = {
569 .probe = altera_uart_probe,
570 .remove = __devexit_p(altera_uart_remove),
573 .owner = THIS_MODULE,
578 static int __init altera_uart_init(void)
582 rc = uart_register_driver(&altera_uart_driver);
585 rc = platform_driver_register(&altera_uart_platform_driver);
587 uart_unregister_driver(&altera_uart_driver);
593 static void __exit altera_uart_exit(void)
595 platform_driver_unregister(&altera_uart_platform_driver);
596 uart_unregister_driver(&altera_uart_driver);
599 module_init(altera_uart_init);
600 module_exit(altera_uart_exit);
602 MODULE_DESCRIPTION("Altera UART driver");
603 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
604 MODULE_LICENSE("GPL");
605 MODULE_ALIAS("platform:" DRV_NAME);