1 // SPDX-License-Identifier: GPL-2.0+
3 * altera_uart.c -- Altera UART driver
5 * Based on mcf.c -- Freescale ColdFire UART driver
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/console.h>
18 #include <linux/tty.h>
19 #include <linux/tty_flip.h>
20 #include <linux/serial.h>
21 #include <linux/serial_core.h>
22 #include <linux/platform_device.h>
25 #include <linux/altera_uart.h>
27 #define DRV_NAME "altera_uart"
28 #define SERIAL_ALTERA_MAJOR 204
29 #define SERIAL_ALTERA_MINOR 213
32 * Altera UART register definitions according to the Nios UART datasheet:
33 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
36 #define ALTERA_UART_SIZE 32
38 #define ALTERA_UART_RXDATA_REG 0
39 #define ALTERA_UART_TXDATA_REG 4
40 #define ALTERA_UART_STATUS_REG 8
41 #define ALTERA_UART_CONTROL_REG 12
42 #define ALTERA_UART_DIVISOR_REG 16
43 #define ALTERA_UART_EOP_REG 20
45 #define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
46 #define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
47 #define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
48 #define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
49 #define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
50 #define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
51 #define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
52 #define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
53 #define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
54 #define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
55 #define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
56 #define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
58 /* Enable interrupt on... */
59 #define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
60 #define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
61 #define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
62 #define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
63 #define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
64 #define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
65 #define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
66 #define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
67 #define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
69 #define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
70 #define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
71 #define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
72 #define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
75 * Local per-uart structure.
78 struct uart_port port;
79 struct timer_list tmr;
80 unsigned int sigs; /* Local copy of line sigs */
81 unsigned short imr; /* Local IMR mirror */
84 static u32 altera_uart_readl(struct uart_port *port, int reg)
86 return readl(port->membase + (reg << port->regshift));
89 static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
91 writel(dat, port->membase + (reg << port->regshift));
94 static unsigned int altera_uart_tx_empty(struct uart_port *port)
96 return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
97 ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
100 static unsigned int altera_uart_get_mctrl(struct uart_port *port)
102 struct altera_uart *pp = container_of(port, struct altera_uart, port);
105 sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
106 ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
107 sigs |= (pp->sigs & TIOCM_RTS);
112 static void altera_uart_update_ctrl_reg(struct altera_uart *pp)
114 unsigned short imr = pp->imr;
117 * If the device doesn't have an irq, ensure that the irq bits are
118 * masked out to keep the irq line inactive.
121 imr &= ALTERA_UART_CONTROL_TRBK_MSK | ALTERA_UART_CONTROL_RTS_MSK;
123 altera_uart_writel(&pp->port, imr, ALTERA_UART_CONTROL_REG);
126 static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
128 struct altera_uart *pp = container_of(port, struct altera_uart, port);
131 if (sigs & TIOCM_RTS)
132 pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
134 pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
135 altera_uart_update_ctrl_reg(pp);
138 static void altera_uart_start_tx(struct uart_port *port)
140 struct altera_uart *pp = container_of(port, struct altera_uart, port);
142 pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
143 altera_uart_update_ctrl_reg(pp);
146 static void altera_uart_stop_tx(struct uart_port *port)
148 struct altera_uart *pp = container_of(port, struct altera_uart, port);
150 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
151 altera_uart_update_ctrl_reg(pp);
154 static void altera_uart_stop_rx(struct uart_port *port)
156 struct altera_uart *pp = container_of(port, struct altera_uart, port);
158 pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
159 altera_uart_update_ctrl_reg(pp);
162 static void altera_uart_break_ctl(struct uart_port *port, int break_state)
164 struct altera_uart *pp = container_of(port, struct altera_uart, port);
167 uart_port_lock_irqsave(port, &flags);
168 if (break_state == -1)
169 pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
171 pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
172 altera_uart_update_ctrl_reg(pp);
173 uart_port_unlock_irqrestore(port, flags);
176 static void altera_uart_set_termios(struct uart_port *port,
177 struct ktermios *termios,
178 const struct ktermios *old)
181 unsigned int baud, baudclk;
183 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
184 baudclk = port->uartclk / baud;
187 tty_termios_copy_hw(termios, old);
188 tty_termios_encode_baud_rate(termios, baud, baud);
190 uart_port_lock_irqsave(port, &flags);
191 uart_update_timeout(port, termios->c_cflag, baud);
192 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
193 uart_port_unlock_irqrestore(port, flags);
196 * FIXME: port->read_status_mask and port->ignore_status_mask
197 * need to be initialized based on termios settings for
198 * INPCK, IGNBRK, IGNPAR, PARMRK, BRKINT
202 static void altera_uart_rx_chars(struct uart_port *port)
204 unsigned short status;
207 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
208 ALTERA_UART_STATUS_RRDY_MSK) {
209 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
213 if (status & ALTERA_UART_STATUS_E_MSK) {
214 altera_uart_writel(port, status,
215 ALTERA_UART_STATUS_REG);
217 if (status & ALTERA_UART_STATUS_BRK_MSK) {
219 if (uart_handle_break(port))
221 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
222 port->icount.parity++;
223 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
224 port->icount.overrun++;
225 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
226 port->icount.frame++;
229 status &= port->read_status_mask;
231 if (status & ALTERA_UART_STATUS_BRK_MSK)
233 else if (status & ALTERA_UART_STATUS_PE_MSK)
235 else if (status & ALTERA_UART_STATUS_FE_MSK)
239 if (uart_handle_sysrq_char(port, ch))
241 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
245 tty_flip_buffer_push(&port->state->port);
248 static void altera_uart_tx_chars(struct uart_port *port)
252 uart_port_tx(port, ch,
253 altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
254 ALTERA_UART_STATUS_TRDY_MSK,
255 altera_uart_writel(port, ch, ALTERA_UART_TXDATA_REG));
258 static irqreturn_t altera_uart_interrupt(int irq, void *data)
260 struct uart_port *port = data;
261 struct altera_uart *pp = container_of(port, struct altera_uart, port);
265 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
267 uart_port_lock_irqsave(port, &flags);
268 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
269 altera_uart_rx_chars(port);
270 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
271 altera_uart_tx_chars(port);
272 uart_port_unlock_irqrestore(port, flags);
274 return IRQ_RETVAL(isr);
277 static void altera_uart_timer(struct timer_list *t)
279 struct altera_uart *pp = from_timer(pp, t, tmr);
280 struct uart_port *port = &pp->port;
282 altera_uart_interrupt(0, port);
283 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
286 static void altera_uart_config_port(struct uart_port *port, int flags)
288 port->type = PORT_ALTERA_UART;
290 /* Clear mask, so no surprise interrupts. */
291 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
292 /* Clear status register */
293 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
296 static int altera_uart_startup(struct uart_port *port)
298 struct altera_uart *pp = container_of(port, struct altera_uart, port);
302 timer_setup(&pp->tmr, altera_uart_timer, 0);
303 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
307 ret = request_irq(port->irq, altera_uart_interrupt, 0,
310 pr_err(DRV_NAME ": unable to attach Altera UART %d "
311 "interrupt vector=%d\n", port->line, port->irq);
316 uart_port_lock_irqsave(port, &flags);
318 /* Enable RX interrupts now */
319 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
320 altera_uart_update_ctrl_reg(pp);
322 uart_port_unlock_irqrestore(port, flags);
327 static void altera_uart_shutdown(struct uart_port *port)
329 struct altera_uart *pp = container_of(port, struct altera_uart, port);
332 uart_port_lock_irqsave(port, &flags);
334 /* Disable all interrupts now */
336 altera_uart_update_ctrl_reg(pp);
338 uart_port_unlock_irqrestore(port, flags);
341 free_irq(port->irq, port);
343 del_timer_sync(&pp->tmr);
346 static const char *altera_uart_type(struct uart_port *port)
348 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
351 static int altera_uart_request_port(struct uart_port *port)
353 /* UARTs always present */
357 static void altera_uart_release_port(struct uart_port *port)
359 /* Nothing to release... */
362 static int altera_uart_verify_port(struct uart_port *port,
363 struct serial_struct *ser)
365 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
370 #ifdef CONFIG_CONSOLE_POLL
371 static int altera_uart_poll_get_char(struct uart_port *port)
373 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
374 ALTERA_UART_STATUS_RRDY_MSK))
377 return altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
380 static void altera_uart_poll_put_char(struct uart_port *port, unsigned char c)
382 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
383 ALTERA_UART_STATUS_TRDY_MSK))
386 altera_uart_writel(port, c, ALTERA_UART_TXDATA_REG);
391 * Define the basic serial functions we support.
393 static const struct uart_ops altera_uart_ops = {
394 .tx_empty = altera_uart_tx_empty,
395 .get_mctrl = altera_uart_get_mctrl,
396 .set_mctrl = altera_uart_set_mctrl,
397 .start_tx = altera_uart_start_tx,
398 .stop_tx = altera_uart_stop_tx,
399 .stop_rx = altera_uart_stop_rx,
400 .break_ctl = altera_uart_break_ctl,
401 .startup = altera_uart_startup,
402 .shutdown = altera_uart_shutdown,
403 .set_termios = altera_uart_set_termios,
404 .type = altera_uart_type,
405 .request_port = altera_uart_request_port,
406 .release_port = altera_uart_release_port,
407 .config_port = altera_uart_config_port,
408 .verify_port = altera_uart_verify_port,
409 #ifdef CONFIG_CONSOLE_POLL
410 .poll_get_char = altera_uart_poll_get_char,
411 .poll_put_char = altera_uart_poll_put_char,
415 static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
417 #if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
419 static void altera_uart_console_putc(struct uart_port *port, unsigned char c)
421 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
422 ALTERA_UART_STATUS_TRDY_MSK))
425 altera_uart_writel(port, c, ALTERA_UART_TXDATA_REG);
428 static void altera_uart_console_write(struct console *co, const char *s,
431 struct uart_port *port = &(altera_uart_ports + co->index)->port;
433 uart_console_write(port, s, count, altera_uart_console_putc);
436 static int __init altera_uart_console_setup(struct console *co, char *options)
438 struct uart_port *port;
439 int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
444 if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
446 port = &altera_uart_ports[co->index].port;
451 uart_parse_options(options, &baud, &parity, &bits, &flow);
453 return uart_set_options(port, co, baud, parity, bits, flow);
456 static struct uart_driver altera_uart_driver;
458 static struct console altera_uart_console = {
460 .write = altera_uart_console_write,
461 .device = uart_console_device,
462 .setup = altera_uart_console_setup,
463 .flags = CON_PRINTBUFFER,
465 .data = &altera_uart_driver,
468 static int __init altera_uart_console_init(void)
470 register_console(&altera_uart_console);
474 console_initcall(altera_uart_console_init);
476 #define ALTERA_UART_CONSOLE (&altera_uart_console)
478 static void altera_uart_earlycon_write(struct console *co, const char *s,
481 struct earlycon_device *dev = co->data;
483 uart_console_write(&dev->port, s, count, altera_uart_console_putc);
486 static int __init altera_uart_earlycon_setup(struct earlycon_device *dev,
489 struct uart_port *port = &dev->port;
494 /* Enable RX interrupts now */
495 altera_uart_writel(port, ALTERA_UART_CONTROL_RRDY_MSK,
496 ALTERA_UART_CONTROL_REG);
499 unsigned int baudclk = port->uartclk / dev->baud;
501 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
504 dev->con->write = altera_uart_earlycon_write;
508 OF_EARLYCON_DECLARE(uart, "altr,uart-1.0", altera_uart_earlycon_setup);
512 #define ALTERA_UART_CONSOLE NULL
514 #endif /* CONFIG_SERIAL_ALTERA_UART_CONSOLE */
517 * Define the altera_uart UART driver structure.
519 static struct uart_driver altera_uart_driver = {
520 .owner = THIS_MODULE,
521 .driver_name = DRV_NAME,
523 .major = SERIAL_ALTERA_MAJOR,
524 .minor = SERIAL_ALTERA_MINOR,
525 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
526 .cons = ALTERA_UART_CONSOLE,
529 static int altera_uart_probe(struct platform_device *pdev)
531 struct altera_uart_platform_uart *platp = dev_get_platdata(&pdev->dev);
532 struct uart_port *port;
533 struct resource *res_mem;
537 /* if id is -1 scan for a free id and use that one */
539 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++)
540 if (altera_uart_ports[i].port.mapbase == 0)
544 if (i < 0 || i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
547 port = &altera_uart_ports[i].port;
549 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551 port->mapbase = res_mem->start;
553 port->mapbase = platp->mapbase;
557 ret = platform_get_irq_optional(pdev, 0);
558 if (ret < 0 && ret != -ENXIO)
563 port->irq = platp->irq;
565 /* Check platform data first so we can override device node data */
567 port->uartclk = platp->uartclk;
569 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
575 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
580 port->regshift = platp->bus_shift;
585 port->type = PORT_ALTERA_UART;
586 port->iotype = SERIAL_IO_MEM;
587 port->ops = &altera_uart_ops;
588 port->flags = UPF_BOOT_AUTOCONF;
589 port->dev = &pdev->dev;
591 platform_set_drvdata(pdev, port);
593 uart_add_one_port(&altera_uart_driver, port);
598 static int altera_uart_remove(struct platform_device *pdev)
600 struct uart_port *port = platform_get_drvdata(pdev);
603 uart_remove_one_port(&altera_uart_driver, port);
605 iounmap(port->membase);
612 static const struct of_device_id altera_uart_match[] = {
613 { .compatible = "ALTR,uart-1.0", },
614 { .compatible = "altr,uart-1.0", },
617 MODULE_DEVICE_TABLE(of, altera_uart_match);
618 #endif /* CONFIG_OF */
620 static struct platform_driver altera_uart_platform_driver = {
621 .probe = altera_uart_probe,
622 .remove = altera_uart_remove,
625 .of_match_table = of_match_ptr(altera_uart_match),
629 static int __init altera_uart_init(void)
633 rc = uart_register_driver(&altera_uart_driver);
636 rc = platform_driver_register(&altera_uart_platform_driver);
638 uart_unregister_driver(&altera_uart_driver);
642 static void __exit altera_uart_exit(void)
644 platform_driver_unregister(&altera_uart_platform_driver);
645 uart_unregister_driver(&altera_uart_driver);
648 module_init(altera_uart_init);
649 module_exit(altera_uart_exit);
651 MODULE_DESCRIPTION("Altera UART driver");
653 MODULE_LICENSE("GPL");
654 MODULE_ALIAS("platform:" DRV_NAME);
655 MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_ALTERA_MAJOR);