1 // SPDX-License-Identifier: GPL-2.0
3 * Atheros AR933X SoC built-in UART driver
7 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
10 #include <linux/module.h>
11 #include <linux/ioport.h>
12 #include <linux/init.h>
13 #include <linux/console.h>
14 #include <linux/sysrq.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
18 #include <linux/of_platform.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/serial_core.h>
22 #include <linux/serial.h>
23 #include <linux/slab.h>
25 #include <linux/irq.h>
26 #include <linux/clk.h>
28 #include <asm/div64.h>
30 #include <asm/mach-ath79/ar933x_uart.h>
32 #define DRIVER_NAME "ar933x-uart"
34 #define AR933X_UART_MAX_SCALE 0xff
35 #define AR933X_UART_MAX_STEP 0xffff
37 #define AR933X_UART_MIN_BAUD 300
38 #define AR933X_UART_MAX_BAUD 3000000
40 #define AR933X_DUMMY_STATUS_RD 0x01
42 static struct uart_driver ar933x_uart_driver;
44 struct ar933x_uart_port {
45 struct uart_port port;
46 unsigned int ier; /* shadow Interrupt Enable Register */
47 unsigned int min_baud;
48 unsigned int max_baud;
52 static inline bool ar933x_uart_console_enabled(void)
54 return IS_ENABLED(CONFIG_SERIAL_AR933X_CONSOLE);
57 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
60 return readl(up->port.membase + offset);
63 static inline void ar933x_uart_write(struct ar933x_uart_port *up,
64 int offset, unsigned int value)
66 writel(value, up->port.membase + offset);
69 static inline void ar933x_uart_rmw(struct ar933x_uart_port *up,
76 t = ar933x_uart_read(up, offset);
79 ar933x_uart_write(up, offset, t);
82 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
86 ar933x_uart_rmw(up, offset, 0, val);
89 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
93 ar933x_uart_rmw(up, offset, val, 0);
96 static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up)
98 up->ier |= AR933X_UART_INT_TX_EMPTY;
99 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
102 static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
104 up->ier &= ~AR933X_UART_INT_TX_EMPTY;
105 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
108 static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
112 rdata = ch & AR933X_UART_DATA_TX_RX_MASK;
113 rdata |= AR933X_UART_DATA_TX_CSR;
114 ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata);
117 static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
119 struct ar933x_uart_port *up =
120 container_of(port, struct ar933x_uart_port, port);
124 spin_lock_irqsave(&up->port.lock, flags);
125 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
126 spin_unlock_irqrestore(&up->port.lock, flags);
128 return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT;
131 static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
136 static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
140 static void ar933x_uart_start_tx(struct uart_port *port)
142 struct ar933x_uart_port *up =
143 container_of(port, struct ar933x_uart_port, port);
145 ar933x_uart_start_tx_interrupt(up);
148 static void ar933x_uart_stop_tx(struct uart_port *port)
150 struct ar933x_uart_port *up =
151 container_of(port, struct ar933x_uart_port, port);
153 ar933x_uart_stop_tx_interrupt(up);
156 static void ar933x_uart_stop_rx(struct uart_port *port)
158 struct ar933x_uart_port *up =
159 container_of(port, struct ar933x_uart_port, port);
161 up->ier &= ~AR933X_UART_INT_RX_VALID;
162 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
165 static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
167 struct ar933x_uart_port *up =
168 container_of(port, struct ar933x_uart_port, port);
171 spin_lock_irqsave(&up->port.lock, flags);
172 if (break_state == -1)
173 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
174 AR933X_UART_CS_TX_BREAK);
176 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
177 AR933X_UART_CS_TX_BREAK);
178 spin_unlock_irqrestore(&up->port.lock, flags);
182 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
184 static unsigned long ar933x_uart_get_baud(unsigned int clk,
191 div = (2 << 16) * (scale + 1);
200 static void ar933x_uart_get_scale_step(unsigned int clk,
212 for (tscale = 0; tscale < AR933X_UART_MAX_SCALE; tscale++) {
216 tstep = baud * (tscale + 1);
220 if (tstep > AR933X_UART_MAX_STEP)
223 diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud);
224 if (diff < min_diff) {
232 static void ar933x_uart_set_termios(struct uart_port *port,
233 struct ktermios *new,
234 struct ktermios *old)
236 struct ar933x_uart_port *up =
237 container_of(port, struct ar933x_uart_port, port);
240 unsigned int baud, scale, step;
242 /* Only CS8 is supported */
243 new->c_cflag &= ~CSIZE;
246 /* Only one stop bit is supported */
247 new->c_cflag &= ~CSTOPB;
250 if (new->c_cflag & PARENB) {
251 if (!(new->c_cflag & PARODD))
252 cs |= AR933X_UART_CS_PARITY_EVEN;
254 cs |= AR933X_UART_CS_PARITY_ODD;
256 cs |= AR933X_UART_CS_PARITY_NONE;
259 /* Mark/space parity is not supported */
260 new->c_cflag &= ~CMSPAR;
262 baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud);
263 ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step);
266 * Ok, we're now changing the port state. Do it with
267 * interrupts disabled.
269 spin_lock_irqsave(&up->port.lock, flags);
271 /* disable the UART */
272 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
273 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S);
275 /* Update the per-port timeout. */
276 uart_update_timeout(port, new->c_cflag, baud);
278 up->port.ignore_status_mask = 0;
280 /* ignore all characters if CREAD is not set */
281 if ((new->c_cflag & CREAD) == 0)
282 up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD;
284 ar933x_uart_write(up, AR933X_UART_CLOCK_REG,
285 scale << AR933X_UART_CLOCK_SCALE_S | step);
287 /* setup configuration register */
288 ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs);
290 /* enable host interrupt */
291 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
292 AR933X_UART_CS_HOST_INT_EN);
294 /* reenable the UART */
295 ar933x_uart_rmw(up, AR933X_UART_CS_REG,
296 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S,
297 AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S);
299 spin_unlock_irqrestore(&up->port.lock, flags);
301 if (tty_termios_baud_rate(new))
302 tty_termios_encode_baud_rate(new, baud, baud);
305 static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
307 struct tty_port *port = &up->port.state->port;
314 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
315 if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
318 /* remove the character from the FIFO */
319 ar933x_uart_write(up, AR933X_UART_DATA_REG,
320 AR933X_UART_DATA_RX_CSR);
322 up->port.icount.rx++;
323 ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
325 if (uart_handle_sysrq_char(&up->port, ch))
328 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
329 tty_insert_flip_char(port, ch, TTY_NORMAL);
330 } while (max_count-- > 0);
332 spin_unlock(&up->port.lock);
333 tty_flip_buffer_push(port);
334 spin_lock(&up->port.lock);
337 static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
339 struct circ_buf *xmit = &up->port.state->xmit;
342 if (uart_tx_stopped(&up->port))
345 count = up->port.fifosize;
349 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
350 if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
353 if (up->port.x_char) {
354 ar933x_uart_putc(up, up->port.x_char);
355 up->port.icount.tx++;
360 if (uart_circ_empty(xmit))
363 ar933x_uart_putc(up, xmit->buf[xmit->tail]);
365 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
366 up->port.icount.tx++;
367 } while (--count > 0);
369 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
370 uart_write_wakeup(&up->port);
372 if (!uart_circ_empty(xmit))
373 ar933x_uart_start_tx_interrupt(up);
376 static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
378 struct ar933x_uart_port *up = dev_id;
381 status = ar933x_uart_read(up, AR933X_UART_CS_REG);
382 if ((status & AR933X_UART_CS_HOST_INT) == 0)
385 spin_lock(&up->port.lock);
387 status = ar933x_uart_read(up, AR933X_UART_INT_REG);
388 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
390 if (status & AR933X_UART_INT_RX_VALID) {
391 ar933x_uart_write(up, AR933X_UART_INT_REG,
392 AR933X_UART_INT_RX_VALID);
393 ar933x_uart_rx_chars(up);
396 if (status & AR933X_UART_INT_TX_EMPTY) {
397 ar933x_uart_write(up, AR933X_UART_INT_REG,
398 AR933X_UART_INT_TX_EMPTY);
399 ar933x_uart_stop_tx_interrupt(up);
400 ar933x_uart_tx_chars(up);
403 spin_unlock(&up->port.lock);
408 static int ar933x_uart_startup(struct uart_port *port)
410 struct ar933x_uart_port *up =
411 container_of(port, struct ar933x_uart_port, port);
415 ret = request_irq(up->port.irq, ar933x_uart_interrupt,
416 up->port.irqflags, dev_name(up->port.dev), up);
420 spin_lock_irqsave(&up->port.lock, flags);
422 /* Enable HOST interrupts */
423 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
424 AR933X_UART_CS_HOST_INT_EN);
426 /* Enable RX interrupts */
427 up->ier = AR933X_UART_INT_RX_VALID;
428 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
430 spin_unlock_irqrestore(&up->port.lock, flags);
435 static void ar933x_uart_shutdown(struct uart_port *port)
437 struct ar933x_uart_port *up =
438 container_of(port, struct ar933x_uart_port, port);
440 /* Disable all interrupts */
442 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
444 /* Disable break condition */
445 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
446 AR933X_UART_CS_TX_BREAK);
448 free_irq(up->port.irq, up);
451 static const char *ar933x_uart_type(struct uart_port *port)
453 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
456 static void ar933x_uart_release_port(struct uart_port *port)
458 /* Nothing to release ... */
461 static int ar933x_uart_request_port(struct uart_port *port)
463 /* UARTs always present */
467 static void ar933x_uart_config_port(struct uart_port *port, int flags)
469 if (flags & UART_CONFIG_TYPE)
470 port->type = PORT_AR933X;
473 static int ar933x_uart_verify_port(struct uart_port *port,
474 struct serial_struct *ser)
476 struct ar933x_uart_port *up =
477 container_of(port, struct ar933x_uart_port, port);
479 if (ser->type != PORT_UNKNOWN &&
480 ser->type != PORT_AR933X)
483 if (ser->irq < 0 || ser->irq >= NR_IRQS)
486 if (ser->baud_base < up->min_baud ||
487 ser->baud_base > up->max_baud)
493 static const struct uart_ops ar933x_uart_ops = {
494 .tx_empty = ar933x_uart_tx_empty,
495 .set_mctrl = ar933x_uart_set_mctrl,
496 .get_mctrl = ar933x_uart_get_mctrl,
497 .stop_tx = ar933x_uart_stop_tx,
498 .start_tx = ar933x_uart_start_tx,
499 .stop_rx = ar933x_uart_stop_rx,
500 .break_ctl = ar933x_uart_break_ctl,
501 .startup = ar933x_uart_startup,
502 .shutdown = ar933x_uart_shutdown,
503 .set_termios = ar933x_uart_set_termios,
504 .type = ar933x_uart_type,
505 .release_port = ar933x_uart_release_port,
506 .request_port = ar933x_uart_request_port,
507 .config_port = ar933x_uart_config_port,
508 .verify_port = ar933x_uart_verify_port,
511 static struct ar933x_uart_port *
512 ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
514 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
517 unsigned int timeout = 60000;
519 /* Wait up to 60ms for the character(s) to be sent. */
521 status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
525 } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
528 static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
530 struct ar933x_uart_port *up =
531 container_of(port, struct ar933x_uart_port, port);
533 ar933x_uart_wait_xmitr(up);
534 ar933x_uart_putc(up, ch);
537 static void ar933x_uart_console_write(struct console *co, const char *s,
540 struct ar933x_uart_port *up = ar933x_console_ports[co->index];
545 local_irq_save(flags);
549 else if (oops_in_progress)
550 locked = spin_trylock(&up->port.lock);
552 spin_lock(&up->port.lock);
555 * First save the IER then disable the interrupts
557 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
558 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
560 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
563 * Finally, wait for transmitter to become empty
564 * and restore the IER
566 ar933x_uart_wait_xmitr(up);
567 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
569 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
572 spin_unlock(&up->port.lock);
574 local_irq_restore(flags);
577 static int ar933x_uart_console_setup(struct console *co, char *options)
579 struct ar933x_uart_port *up;
585 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
588 up = ar933x_console_ports[co->index];
593 uart_parse_options(options, &baud, &parity, &bits, &flow);
595 return uart_set_options(&up->port, co, baud, parity, bits, flow);
598 static struct console ar933x_uart_console = {
600 .write = ar933x_uart_console_write,
601 .device = uart_console_device,
602 .setup = ar933x_uart_console_setup,
603 .flags = CON_PRINTBUFFER,
605 .data = &ar933x_uart_driver,
608 static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
610 if (!ar933x_uart_console_enabled())
613 ar933x_console_ports[up->port.line] = up;
616 static struct uart_driver ar933x_uart_driver = {
617 .owner = THIS_MODULE,
618 .driver_name = DRIVER_NAME,
619 .dev_name = "ttyATH",
620 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
621 .cons = NULL, /* filled in runtime */
624 static int ar933x_uart_probe(struct platform_device *pdev)
626 struct ar933x_uart_port *up;
627 struct uart_port *port;
628 struct resource *mem_res;
629 struct resource *irq_res;
630 struct device_node *np;
635 np = pdev->dev.of_node;
636 if (IS_ENABLED(CONFIG_OF) && np) {
637 id = of_alias_get_id(np, "serial");
639 dev_err(&pdev->dev, "unable to get alias id, err=%d\n",
649 if (id >= CONFIG_SERIAL_AR933X_NR_UARTS)
652 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
654 dev_err(&pdev->dev, "no IRQ resource\n");
658 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
663 up->clk = devm_clk_get(&pdev->dev, "uart");
664 if (IS_ERR(up->clk)) {
665 dev_err(&pdev->dev, "unable to get UART clock\n");
666 return PTR_ERR(up->clk);
671 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
672 port->membase = devm_ioremap_resource(&pdev->dev, mem_res);
673 if (IS_ERR(port->membase))
674 return PTR_ERR(port->membase);
676 ret = clk_prepare_enable(up->clk);
680 port->uartclk = clk_get_rate(up->clk);
681 if (!port->uartclk) {
683 goto err_disable_clk;
686 port->mapbase = mem_res->start;
688 port->irq = irq_res->start;
689 port->dev = &pdev->dev;
690 port->type = PORT_AR933X;
691 port->iotype = UPIO_MEM32;
694 port->fifosize = AR933X_UART_FIFO_SIZE;
695 port->ops = &ar933x_uart_ops;
697 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
698 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
700 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
701 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
703 ar933x_uart_add_console_port(up);
705 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
707 goto err_disable_clk;
709 platform_set_drvdata(pdev, up);
713 clk_disable_unprepare(up->clk);
717 static int ar933x_uart_remove(struct platform_device *pdev)
719 struct ar933x_uart_port *up;
721 up = platform_get_drvdata(pdev);
724 uart_remove_one_port(&ar933x_uart_driver, &up->port);
725 clk_disable_unprepare(up->clk);
732 static const struct of_device_id ar933x_uart_of_ids[] = {
733 { .compatible = "qca,ar9330-uart" },
736 MODULE_DEVICE_TABLE(of, ar933x_uart_of_ids);
739 static struct platform_driver ar933x_uart_platform_driver = {
740 .probe = ar933x_uart_probe,
741 .remove = ar933x_uart_remove,
744 .of_match_table = of_match_ptr(ar933x_uart_of_ids),
748 static int __init ar933x_uart_init(void)
752 if (ar933x_uart_console_enabled())
753 ar933x_uart_driver.cons = &ar933x_uart_console;
755 ret = uart_register_driver(&ar933x_uart_driver);
759 ret = platform_driver_register(&ar933x_uart_platform_driver);
761 goto err_unregister_uart_driver;
765 err_unregister_uart_driver:
766 uart_unregister_driver(&ar933x_uart_driver);
771 static void __exit ar933x_uart_exit(void)
773 platform_driver_unregister(&ar933x_uart_platform_driver);
774 uart_unregister_driver(&ar933x_uart_driver);
777 module_init(ar933x_uart_init);
778 module_exit(ar933x_uart_exit);
780 MODULE_DESCRIPTION("Atheros AR933X UART driver");
782 MODULE_LICENSE("GPL v2");
783 MODULE_ALIAS("platform:" DRIVER_NAME);