2 * Atheros AR933X SoC built-in UART driver
4 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/ioport.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/sysrq.h>
18 #include <linux/delay.h>
19 #include <linux/platform_device.h>
21 #include <linux/of_platform.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/serial_core.h>
25 #include <linux/serial.h>
26 #include <linux/slab.h>
28 #include <linux/irq.h>
29 #include <linux/clk.h>
31 #include <asm/div64.h>
33 #include <asm/mach-ath79/ar933x_uart.h>
35 #define DRIVER_NAME "ar933x-uart"
37 #define AR933X_UART_MAX_SCALE 0xff
38 #define AR933X_UART_MAX_STEP 0xffff
40 #define AR933X_UART_MIN_BAUD 300
41 #define AR933X_UART_MAX_BAUD 3000000
43 #define AR933X_DUMMY_STATUS_RD 0x01
45 static struct uart_driver ar933x_uart_driver;
47 struct ar933x_uart_port {
48 struct uart_port port;
49 unsigned int ier; /* shadow Interrupt Enable Register */
50 unsigned int min_baud;
51 unsigned int max_baud;
55 static inline bool ar933x_uart_console_enabled(void)
57 return config_enabled(CONFIG_SERIAL_AR933X_CONSOLE);
60 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
63 return readl(up->port.membase + offset);
66 static inline void ar933x_uart_write(struct ar933x_uart_port *up,
67 int offset, unsigned int value)
69 writel(value, up->port.membase + offset);
72 static inline void ar933x_uart_rmw(struct ar933x_uart_port *up,
79 t = ar933x_uart_read(up, offset);
82 ar933x_uart_write(up, offset, t);
85 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
89 ar933x_uart_rmw(up, offset, 0, val);
92 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
96 ar933x_uart_rmw(up, offset, val, 0);
99 static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up)
101 up->ier |= AR933X_UART_INT_TX_EMPTY;
102 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
105 static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
107 up->ier &= ~AR933X_UART_INT_TX_EMPTY;
108 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
111 static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
115 rdata = ch & AR933X_UART_DATA_TX_RX_MASK;
116 rdata |= AR933X_UART_DATA_TX_CSR;
117 ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata);
120 static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
122 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
126 spin_lock_irqsave(&up->port.lock, flags);
127 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
128 spin_unlock_irqrestore(&up->port.lock, flags);
130 return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT;
133 static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
138 static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
142 static void ar933x_uart_start_tx(struct uart_port *port)
144 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
146 ar933x_uart_start_tx_interrupt(up);
149 static void ar933x_uart_stop_tx(struct uart_port *port)
151 struct ar933x_uart_port *up = (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 = (struct ar933x_uart_port *) port;
160 up->ier &= ~AR933X_UART_INT_RX_VALID;
161 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
164 static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
166 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
169 spin_lock_irqsave(&up->port.lock, flags);
170 if (break_state == -1)
171 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
172 AR933X_UART_CS_TX_BREAK);
174 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
175 AR933X_UART_CS_TX_BREAK);
176 spin_unlock_irqrestore(&up->port.lock, flags);
180 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
182 static unsigned long ar933x_uart_get_baud(unsigned int clk,
189 div = (2 << 16) * (scale + 1);
198 static void ar933x_uart_get_scale_step(unsigned int clk,
210 for (tscale = 0; tscale < AR933X_UART_MAX_SCALE; tscale++) {
214 tstep = baud * (tscale + 1);
218 if (tstep > AR933X_UART_MAX_STEP)
221 diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud);
222 if (diff < min_diff) {
230 static void ar933x_uart_set_termios(struct uart_port *port,
231 struct ktermios *new,
232 struct ktermios *old)
234 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
237 unsigned int baud, scale, step;
239 /* Only CS8 is supported */
240 new->c_cflag &= ~CSIZE;
243 /* Only one stop bit is supported */
244 new->c_cflag &= ~CSTOPB;
247 if (new->c_cflag & PARENB) {
248 if (!(new->c_cflag & PARODD))
249 cs |= AR933X_UART_CS_PARITY_EVEN;
251 cs |= AR933X_UART_CS_PARITY_ODD;
253 cs |= AR933X_UART_CS_PARITY_NONE;
256 /* Mark/space parity is not supported */
257 new->c_cflag &= ~CMSPAR;
259 baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud);
260 ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step);
263 * Ok, we're now changing the port state. Do it with
264 * interrupts disabled.
266 spin_lock_irqsave(&up->port.lock, flags);
268 /* disable the UART */
269 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
270 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S);
272 /* Update the per-port timeout. */
273 uart_update_timeout(port, new->c_cflag, baud);
275 up->port.ignore_status_mask = 0;
277 /* ignore all characters if CREAD is not set */
278 if ((new->c_cflag & CREAD) == 0)
279 up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD;
281 ar933x_uart_write(up, AR933X_UART_CLOCK_REG,
282 scale << AR933X_UART_CLOCK_SCALE_S | step);
284 /* setup configuration register */
285 ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs);
287 /* enable host interrupt */
288 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
289 AR933X_UART_CS_HOST_INT_EN);
291 /* reenable the UART */
292 ar933x_uart_rmw(up, AR933X_UART_CS_REG,
293 AR933X_UART_CS_IF_MODE_M << AR933X_UART_CS_IF_MODE_S,
294 AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S);
296 spin_unlock_irqrestore(&up->port.lock, flags);
298 if (tty_termios_baud_rate(new))
299 tty_termios_encode_baud_rate(new, baud, baud);
302 static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
304 struct tty_port *port = &up->port.state->port;
311 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
312 if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
315 /* remove the character from the FIFO */
316 ar933x_uart_write(up, AR933X_UART_DATA_REG,
317 AR933X_UART_DATA_RX_CSR);
319 up->port.icount.rx++;
320 ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
322 if (uart_handle_sysrq_char(&up->port, ch))
325 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
326 tty_insert_flip_char(port, ch, TTY_NORMAL);
327 } while (max_count-- > 0);
329 spin_unlock(&up->port.lock);
330 tty_flip_buffer_push(port);
331 spin_lock(&up->port.lock);
334 static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
336 struct circ_buf *xmit = &up->port.state->xmit;
339 if (uart_tx_stopped(&up->port))
342 count = up->port.fifosize;
346 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
347 if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
350 if (up->port.x_char) {
351 ar933x_uart_putc(up, up->port.x_char);
352 up->port.icount.tx++;
357 if (uart_circ_empty(xmit))
360 ar933x_uart_putc(up, xmit->buf[xmit->tail]);
362 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
363 up->port.icount.tx++;
364 } while (--count > 0);
366 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
367 uart_write_wakeup(&up->port);
369 if (!uart_circ_empty(xmit))
370 ar933x_uart_start_tx_interrupt(up);
373 static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
375 struct ar933x_uart_port *up = dev_id;
378 status = ar933x_uart_read(up, AR933X_UART_CS_REG);
379 if ((status & AR933X_UART_CS_HOST_INT) == 0)
382 spin_lock(&up->port.lock);
384 status = ar933x_uart_read(up, AR933X_UART_INT_REG);
385 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
387 if (status & AR933X_UART_INT_RX_VALID) {
388 ar933x_uart_write(up, AR933X_UART_INT_REG,
389 AR933X_UART_INT_RX_VALID);
390 ar933x_uart_rx_chars(up);
393 if (status & AR933X_UART_INT_TX_EMPTY) {
394 ar933x_uart_write(up, AR933X_UART_INT_REG,
395 AR933X_UART_INT_TX_EMPTY);
396 ar933x_uart_stop_tx_interrupt(up);
397 ar933x_uart_tx_chars(up);
400 spin_unlock(&up->port.lock);
405 static int ar933x_uart_startup(struct uart_port *port)
407 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
411 ret = request_irq(up->port.irq, ar933x_uart_interrupt,
412 up->port.irqflags, dev_name(up->port.dev), up);
416 spin_lock_irqsave(&up->port.lock, flags);
418 /* Enable HOST interrupts */
419 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
420 AR933X_UART_CS_HOST_INT_EN);
422 /* Enable RX interrupts */
423 up->ier = AR933X_UART_INT_RX_VALID;
424 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
426 spin_unlock_irqrestore(&up->port.lock, flags);
431 static void ar933x_uart_shutdown(struct uart_port *port)
433 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
435 /* Disable all interrupts */
437 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
439 /* Disable break condition */
440 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
441 AR933X_UART_CS_TX_BREAK);
443 free_irq(up->port.irq, up);
446 static const char *ar933x_uart_type(struct uart_port *port)
448 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
451 static void ar933x_uart_release_port(struct uart_port *port)
453 /* Nothing to release ... */
456 static int ar933x_uart_request_port(struct uart_port *port)
458 /* UARTs always present */
462 static void ar933x_uart_config_port(struct uart_port *port, int flags)
464 if (flags & UART_CONFIG_TYPE)
465 port->type = PORT_AR933X;
468 static int ar933x_uart_verify_port(struct uart_port *port,
469 struct serial_struct *ser)
471 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
473 if (ser->type != PORT_UNKNOWN &&
474 ser->type != PORT_AR933X)
477 if (ser->irq < 0 || ser->irq >= NR_IRQS)
480 if (ser->baud_base < up->min_baud ||
481 ser->baud_base > up->max_baud)
487 static struct uart_ops ar933x_uart_ops = {
488 .tx_empty = ar933x_uart_tx_empty,
489 .set_mctrl = ar933x_uart_set_mctrl,
490 .get_mctrl = ar933x_uart_get_mctrl,
491 .stop_tx = ar933x_uart_stop_tx,
492 .start_tx = ar933x_uart_start_tx,
493 .stop_rx = ar933x_uart_stop_rx,
494 .break_ctl = ar933x_uart_break_ctl,
495 .startup = ar933x_uart_startup,
496 .shutdown = ar933x_uart_shutdown,
497 .set_termios = ar933x_uart_set_termios,
498 .type = ar933x_uart_type,
499 .release_port = ar933x_uart_release_port,
500 .request_port = ar933x_uart_request_port,
501 .config_port = ar933x_uart_config_port,
502 .verify_port = ar933x_uart_verify_port,
505 static struct ar933x_uart_port *
506 ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
508 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
511 unsigned int timeout = 60000;
513 /* Wait up to 60ms for the character(s) to be sent. */
515 status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
519 } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
522 static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
524 struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
526 ar933x_uart_wait_xmitr(up);
527 ar933x_uart_putc(up, ch);
530 static void ar933x_uart_console_write(struct console *co, const char *s,
533 struct ar933x_uart_port *up = ar933x_console_ports[co->index];
538 local_irq_save(flags);
542 else if (oops_in_progress)
543 locked = spin_trylock(&up->port.lock);
545 spin_lock(&up->port.lock);
548 * First save the IER then disable the interrupts
550 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
551 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
553 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
556 * Finally, wait for transmitter to become empty
557 * and restore the IER
559 ar933x_uart_wait_xmitr(up);
560 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
562 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
565 spin_unlock(&up->port.lock);
567 local_irq_restore(flags);
570 static int ar933x_uart_console_setup(struct console *co, char *options)
572 struct ar933x_uart_port *up;
578 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
581 up = ar933x_console_ports[co->index];
586 uart_parse_options(options, &baud, &parity, &bits, &flow);
588 return uart_set_options(&up->port, co, baud, parity, bits, flow);
591 static struct console ar933x_uart_console = {
593 .write = ar933x_uart_console_write,
594 .device = uart_console_device,
595 .setup = ar933x_uart_console_setup,
596 .flags = CON_PRINTBUFFER,
598 .data = &ar933x_uart_driver,
601 static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
603 if (!ar933x_uart_console_enabled())
606 ar933x_console_ports[up->port.line] = up;
609 static struct uart_driver ar933x_uart_driver = {
610 .owner = THIS_MODULE,
611 .driver_name = DRIVER_NAME,
612 .dev_name = "ttyATH",
613 .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
614 .cons = NULL, /* filled in runtime */
617 static int ar933x_uart_probe(struct platform_device *pdev)
619 struct ar933x_uart_port *up;
620 struct uart_port *port;
621 struct resource *mem_res;
622 struct resource *irq_res;
623 struct device_node *np;
628 np = pdev->dev.of_node;
629 if (config_enabled(CONFIG_OF) && np) {
630 id = of_alias_get_id(np, "serial");
632 dev_err(&pdev->dev, "unable to get alias id, err=%d\n",
642 if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
645 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
647 dev_err(&pdev->dev, "no IRQ resource\n");
651 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port),
656 up->clk = devm_clk_get(&pdev->dev, "uart");
657 if (IS_ERR(up->clk)) {
658 dev_err(&pdev->dev, "unable to get UART clock\n");
659 return PTR_ERR(up->clk);
664 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
665 port->membase = devm_ioremap_resource(&pdev->dev, mem_res);
666 if (IS_ERR(port->membase))
667 return PTR_ERR(port->membase);
669 ret = clk_prepare_enable(up->clk);
673 port->uartclk = clk_get_rate(up->clk);
674 if (!port->uartclk) {
676 goto err_disable_clk;
679 port->mapbase = mem_res->start;
681 port->irq = irq_res->start;
682 port->dev = &pdev->dev;
683 port->type = PORT_AR933X;
684 port->iotype = UPIO_MEM32;
687 port->fifosize = AR933X_UART_FIFO_SIZE;
688 port->ops = &ar933x_uart_ops;
690 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
691 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
693 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
694 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
696 ar933x_uart_add_console_port(up);
698 ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
700 goto err_disable_clk;
702 platform_set_drvdata(pdev, up);
706 clk_disable_unprepare(up->clk);
710 static int ar933x_uart_remove(struct platform_device *pdev)
712 struct ar933x_uart_port *up;
714 up = platform_get_drvdata(pdev);
717 uart_remove_one_port(&ar933x_uart_driver, &up->port);
718 clk_disable_unprepare(up->clk);
725 static const struct of_device_id ar933x_uart_of_ids[] = {
726 { .compatible = "qca,ar9330-uart" },
729 MODULE_DEVICE_TABLE(of, ar933x_uart_of_ids);
732 static struct platform_driver ar933x_uart_platform_driver = {
733 .probe = ar933x_uart_probe,
734 .remove = ar933x_uart_remove,
737 .owner = THIS_MODULE,
738 .of_match_table = of_match_ptr(ar933x_uart_of_ids),
742 static int __init ar933x_uart_init(void)
746 if (ar933x_uart_console_enabled())
747 ar933x_uart_driver.cons = &ar933x_uart_console;
749 ret = uart_register_driver(&ar933x_uart_driver);
753 ret = platform_driver_register(&ar933x_uart_platform_driver);
755 goto err_unregister_uart_driver;
759 err_unregister_uart_driver:
760 uart_unregister_driver(&ar933x_uart_driver);
765 static void __exit ar933x_uart_exit(void)
767 platform_driver_unregister(&ar933x_uart_platform_driver);
768 uart_unregister_driver(&ar933x_uart_driver);
771 module_init(ar933x_uart_init);
772 module_exit(ar933x_uart_exit);
774 MODULE_DESCRIPTION("Atheros AR933X UART driver");
775 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
776 MODULE_LICENSE("GPL v2");
777 MODULE_ALIAS("platform:" DRIVER_NAME);