1 // SPDX-License-Identifier: GPL-2.0+
3 * altera_uart.c -- Altera UART driver
5 * Based on mcf.c -- Freescale ColdFire UART driver
7 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
8 * (C) Copyright 2008, Thomas Chou <thomas@wytron.com.tw>
9 * (C) Copyright 2010, Tobias Klauser <tklauser@distanz.ch>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/timer.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/console.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/serial_core.h>
27 #include <linux/platform_device.h>
30 #include <linux/altera_uart.h>
32 #define DRV_NAME "altera_uart"
33 #define SERIAL_ALTERA_MAJOR 204
34 #define SERIAL_ALTERA_MINOR 213
37 * Altera UART register definitions according to the Nios UART datasheet:
38 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
41 #define ALTERA_UART_SIZE 32
43 #define ALTERA_UART_RXDATA_REG 0
44 #define ALTERA_UART_TXDATA_REG 4
45 #define ALTERA_UART_STATUS_REG 8
46 #define ALTERA_UART_CONTROL_REG 12
47 #define ALTERA_UART_DIVISOR_REG 16
48 #define ALTERA_UART_EOP_REG 20
50 #define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
51 #define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
52 #define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
53 #define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
54 #define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
55 #define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
56 #define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
57 #define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
58 #define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
59 #define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
60 #define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
61 #define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
63 /* Enable interrupt on... */
64 #define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
65 #define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
66 #define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
67 #define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
68 #define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
69 #define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
70 #define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
71 #define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
72 #define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
74 #define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
75 #define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
76 #define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
77 #define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
80 * Local per-uart structure.
83 struct uart_port port;
84 struct timer_list tmr;
85 unsigned int sigs; /* Local copy of line sigs */
86 unsigned short imr; /* Local IMR mirror */
89 static u32 altera_uart_readl(struct uart_port *port, int reg)
91 return readl(port->membase + (reg << port->regshift));
94 static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
96 writel(dat, port->membase + (reg << port->regshift));
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_set_termios(struct uart_port *port,
168 struct ktermios *termios,
169 struct ktermios *old)
172 unsigned int baud, baudclk;
174 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
175 baudclk = port->uartclk / baud;
178 tty_termios_copy_hw(termios, old);
179 tty_termios_encode_baud_rate(termios, baud, baud);
181 spin_lock_irqsave(&port->lock, flags);
182 uart_update_timeout(port, termios->c_cflag, baud);
183 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
184 spin_unlock_irqrestore(&port->lock, flags);
187 * FIXME: port->read_status_mask and port->ignore_status_mask
188 * need to be initialized based on termios settings for
189 * INPCK, IGNBRK, IGNPAR, PARMRK, BRKINT
193 static void altera_uart_rx_chars(struct altera_uart *pp)
195 struct uart_port *port = &pp->port;
196 unsigned char ch, flag;
197 unsigned short status;
199 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
200 ALTERA_UART_STATUS_RRDY_MSK) {
201 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
205 if (status & ALTERA_UART_STATUS_E_MSK) {
206 altera_uart_writel(port, status,
207 ALTERA_UART_STATUS_REG);
209 if (status & ALTERA_UART_STATUS_BRK_MSK) {
211 if (uart_handle_break(port))
213 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
214 port->icount.parity++;
215 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
216 port->icount.overrun++;
217 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
218 port->icount.frame++;
221 status &= port->read_status_mask;
223 if (status & ALTERA_UART_STATUS_BRK_MSK)
225 else if (status & ALTERA_UART_STATUS_PE_MSK)
227 else if (status & ALTERA_UART_STATUS_FE_MSK)
231 if (uart_handle_sysrq_char(port, ch))
233 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
237 spin_unlock(&port->lock);
238 tty_flip_buffer_push(&port->state->port);
239 spin_lock(&port->lock);
242 static void altera_uart_tx_chars(struct altera_uart *pp)
244 struct uart_port *port = &pp->port;
245 struct circ_buf *xmit = &port->state->xmit;
248 /* Send special char - probably flow control */
249 altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
255 while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
256 ALTERA_UART_STATUS_TRDY_MSK) {
257 if (xmit->head == xmit->tail)
259 altera_uart_writel(port, xmit->buf[xmit->tail],
260 ALTERA_UART_TXDATA_REG);
261 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
265 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
266 uart_write_wakeup(port);
268 if (xmit->head == xmit->tail) {
269 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
270 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
274 static irqreturn_t altera_uart_interrupt(int irq, void *data)
276 struct uart_port *port = data;
277 struct altera_uart *pp = container_of(port, struct altera_uart, port);
280 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
282 spin_lock(&port->lock);
283 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
284 altera_uart_rx_chars(pp);
285 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
286 altera_uart_tx_chars(pp);
287 spin_unlock(&port->lock);
289 return IRQ_RETVAL(isr);
292 static void altera_uart_timer(struct timer_list *t)
294 struct altera_uart *pp = from_timer(pp, t, tmr);
295 struct uart_port *port = &pp->port;
297 altera_uart_interrupt(0, port);
298 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
301 static void altera_uart_config_port(struct uart_port *port, int flags)
303 port->type = PORT_ALTERA_UART;
305 /* Clear mask, so no surprise interrupts. */
306 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
307 /* Clear status register */
308 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
311 static int altera_uart_startup(struct uart_port *port)
313 struct altera_uart *pp = container_of(port, struct altera_uart, port);
318 timer_setup(&pp->tmr, altera_uart_timer, 0);
319 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
323 ret = request_irq(port->irq, altera_uart_interrupt, 0,
326 pr_err(DRV_NAME ": unable to attach Altera UART %d "
327 "interrupt vector=%d\n", port->line, port->irq);
331 spin_lock_irqsave(&port->lock, flags);
333 /* Enable RX interrupts now */
334 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
335 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
337 spin_unlock_irqrestore(&port->lock, flags);
342 static void altera_uart_shutdown(struct uart_port *port)
344 struct altera_uart *pp = container_of(port, struct altera_uart, port);
347 spin_lock_irqsave(&port->lock, flags);
349 /* Disable all interrupts now */
351 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
353 spin_unlock_irqrestore(&port->lock, flags);
356 free_irq(port->irq, port);
358 del_timer_sync(&pp->tmr);
361 static const char *altera_uart_type(struct uart_port *port)
363 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
366 static int altera_uart_request_port(struct uart_port *port)
368 /* UARTs always present */
372 static void altera_uart_release_port(struct uart_port *port)
374 /* Nothing to release... */
377 static int altera_uart_verify_port(struct uart_port *port,
378 struct serial_struct *ser)
380 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
385 #ifdef CONFIG_CONSOLE_POLL
386 static int altera_uart_poll_get_char(struct uart_port *port)
388 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
389 ALTERA_UART_STATUS_RRDY_MSK))
392 return altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
395 static void altera_uart_poll_put_char(struct uart_port *port, unsigned char c)
397 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
398 ALTERA_UART_STATUS_TRDY_MSK))
401 altera_uart_writel(port, c, ALTERA_UART_TXDATA_REG);
406 * Define the basic serial functions we support.
408 static const struct uart_ops altera_uart_ops = {
409 .tx_empty = altera_uart_tx_empty,
410 .get_mctrl = altera_uart_get_mctrl,
411 .set_mctrl = altera_uart_set_mctrl,
412 .start_tx = altera_uart_start_tx,
413 .stop_tx = altera_uart_stop_tx,
414 .stop_rx = altera_uart_stop_rx,
415 .break_ctl = altera_uart_break_ctl,
416 .startup = altera_uart_startup,
417 .shutdown = altera_uart_shutdown,
418 .set_termios = altera_uart_set_termios,
419 .type = altera_uart_type,
420 .request_port = altera_uart_request_port,
421 .release_port = altera_uart_release_port,
422 .config_port = altera_uart_config_port,
423 .verify_port = altera_uart_verify_port,
424 #ifdef CONFIG_CONSOLE_POLL
425 .poll_get_char = altera_uart_poll_get_char,
426 .poll_put_char = altera_uart_poll_put_char,
430 static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
432 #if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
434 static void altera_uart_console_putc(struct uart_port *port, int c)
436 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
437 ALTERA_UART_STATUS_TRDY_MSK))
440 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
443 static void altera_uart_console_write(struct console *co, const char *s,
446 struct uart_port *port = &(altera_uart_ports + co->index)->port;
448 uart_console_write(port, s, count, altera_uart_console_putc);
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;
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)
493 static void altera_uart_earlycon_write(struct console *co, const char *s,
496 struct earlycon_device *dev = co->data;
498 uart_console_write(&dev->port, s, count, altera_uart_console_putc);
501 static int __init altera_uart_earlycon_setup(struct earlycon_device *dev,
504 struct uart_port *port = &dev->port;
509 /* Enable RX interrupts now */
510 writel(ALTERA_UART_CONTROL_RRDY_MSK,
511 port->membase + ALTERA_UART_CONTROL_REG);
514 unsigned int baudclk = port->uartclk / dev->baud;
516 writel(baudclk, port->membase + ALTERA_UART_DIVISOR_REG);
519 dev->con->write = altera_uart_earlycon_write;
523 OF_EARLYCON_DECLARE(uart, "altr,uart-1.0", altera_uart_earlycon_setup);
527 #define ALTERA_UART_CONSOLE NULL
529 #endif /* CONFIG_SERIAL_ALTERA_UART_CONSOLE */
532 * Define the altera_uart UART driver structure.
534 static struct uart_driver altera_uart_driver = {
535 .owner = THIS_MODULE,
536 .driver_name = DRV_NAME,
538 .major = SERIAL_ALTERA_MAJOR,
539 .minor = SERIAL_ALTERA_MINOR,
540 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
541 .cons = ALTERA_UART_CONSOLE,
544 static int altera_uart_probe(struct platform_device *pdev)
546 struct altera_uart_platform_uart *platp = dev_get_platdata(&pdev->dev);
547 struct uart_port *port;
548 struct resource *res_mem;
549 struct resource *res_irq;
553 /* if id is -1 scan for a free id and use that one */
555 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++)
556 if (altera_uart_ports[i].port.mapbase == 0)
560 if (i < 0 || i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
563 port = &altera_uart_ports[i].port;
565 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
567 port->mapbase = res_mem->start;
569 port->mapbase = platp->mapbase;
573 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
575 port->irq = res_irq->start;
577 port->irq = platp->irq;
579 /* Check platform data first so we can override device node data */
581 port->uartclk = platp->uartclk;
583 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
589 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
594 port->regshift = platp->bus_shift;
599 port->type = PORT_ALTERA_UART;
600 port->iotype = SERIAL_IO_MEM;
601 port->ops = &altera_uart_ops;
602 port->flags = UPF_BOOT_AUTOCONF;
603 port->dev = &pdev->dev;
605 platform_set_drvdata(pdev, port);
607 uart_add_one_port(&altera_uart_driver, port);
612 static int altera_uart_remove(struct platform_device *pdev)
614 struct uart_port *port = platform_get_drvdata(pdev);
617 uart_remove_one_port(&altera_uart_driver, port);
619 iounmap(port->membase);
626 static const struct of_device_id altera_uart_match[] = {
627 { .compatible = "ALTR,uart-1.0", },
628 { .compatible = "altr,uart-1.0", },
631 MODULE_DEVICE_TABLE(of, altera_uart_match);
632 #endif /* CONFIG_OF */
634 static struct platform_driver altera_uart_platform_driver = {
635 .probe = altera_uart_probe,
636 .remove = altera_uart_remove,
639 .of_match_table = of_match_ptr(altera_uart_match),
643 static int __init altera_uart_init(void)
647 rc = uart_register_driver(&altera_uart_driver);
650 rc = platform_driver_register(&altera_uart_platform_driver);
652 uart_unregister_driver(&altera_uart_driver);
656 static void __exit altera_uart_exit(void)
658 platform_driver_unregister(&altera_uart_platform_driver);
659 uart_unregister_driver(&altera_uart_driver);
662 module_init(altera_uart_init);
663 module_exit(altera_uart_exit);
665 MODULE_DESCRIPTION("Altera UART driver");
666 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
667 MODULE_LICENSE("GPL");
668 MODULE_ALIAS("platform:" DRV_NAME);
669 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR);