1 From 1de387abd06fb67aaa8e27a48e378ffd9aaddd74 Mon Sep 17 00:00:00 2001
2 From: Gabor Juhos <juhosg@openwrt.org>
3 Date: Mon, 20 Jun 2011 19:26:11 +0200
4 Subject: [PATCH 14/27] SERIAL: AR933X: Add driver for the built-in UART of the SoC
6 This patch adds the driver for the built-in UART of the
9 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
10 Cc: linux-mips@linux-mips.org
11 Cc: Kathy Giori <kgiori@qca.qualcomm.com>
12 Cc: "Luis R. Rodriguez" <rodrigue@qca.qualcomm.com>
13 Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>
14 Cc: linux-serial@vger.kernel.org
15 Patchwork: https://patchwork.linux-mips.org/patch/2526/
16 Signed-off-by: Alan Cox <alan@linux.intel.com>
17 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
19 .../include/asm/mach-ath79/ar933x_uart_platform.h | 18 +
20 drivers/tty/serial/Kconfig | 23 +
21 drivers/tty/serial/Makefile | 1 +
22 drivers/tty/serial/ar933x_uart.c | 688 ++++++++++++++++++++
23 include/linux/serial_core.h | 4 +
24 5 files changed, 734 insertions(+), 0 deletions(-)
25 create mode 100644 arch/mips/include/asm/mach-ath79/ar933x_uart_platform.h
26 create mode 100644 drivers/tty/serial/ar933x_uart.c
29 +++ b/arch/mips/include/asm/mach-ath79/ar933x_uart_platform.h
32 + * Platform data definition for Atheros AR933X UART
34 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
36 + * This program is free software; you can redistribute it and/or modify it
37 + * under the terms of the GNU General Public License version 2 as published
38 + * by the Free Software Foundation.
41 +#ifndef _AR933X_UART_PLATFORM_H
42 +#define _AR933X_UART_PLATFORM_H
44 +struct ar933x_uart_platform_data {
48 +#endif /* _AR933X_UART_PLATFORM_H */
49 --- a/drivers/tty/serial/Kconfig
50 +++ b/drivers/tty/serial/Kconfig
51 @@ -1610,4 +1610,27 @@ config SERIAL_XILINX_PS_UART_CONSOLE
53 Enable a Xilinx PS UART port to be the system console.
56 + bool "AR933X serial port support"
57 + depends on SOC_AR933X
60 + If you have an Atheros AR933X SOC based board and want to use the
61 + built-in UART of the SoC, say Y to this option.
63 +config SERIAL_AR933X_CONSOLE
64 + bool "Console on AR933X serial port"
65 + depends on SERIAL_AR933X=y
66 + select SERIAL_CORE_CONSOLE
68 + Enable a built-in UART port of the AR933X to be the system console.
70 +config SERIAL_AR933X_NR_UARTS
71 + int "Maximum number of AR933X serial ports"
72 + depends on SERIAL_AR933X
75 + Set this to the number of serial ports you want the driver
79 --- a/drivers/tty/serial/Makefile
80 +++ b/drivers/tty/serial/Makefile
81 @@ -94,3 +94,4 @@ obj-$(CONFIG_SERIAL_MSM_SMD) += msm_smd_
82 obj-$(CONFIG_SERIAL_MXS_AUART) += mxs-auart.o
83 obj-$(CONFIG_SERIAL_LANTIQ) += lantiq.o
84 obj-$(CONFIG_SERIAL_XILINX_PS_UART) += xilinx_uartps.o
85 +obj-$(CONFIG_SERIAL_AR933X) += ar933x_uart.o
87 +++ b/drivers/tty/serial/ar933x_uart.c
90 + * Atheros AR933X SoC built-in UART driver
92 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
94 + * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
96 + * This program is free software; you can redistribute it and/or modify it
97 + * under the terms of the GNU General Public License version 2 as published
98 + * by the Free Software Foundation.
101 +#include <linux/module.h>
102 +#include <linux/ioport.h>
103 +#include <linux/init.h>
104 +#include <linux/console.h>
105 +#include <linux/sysrq.h>
106 +#include <linux/delay.h>
107 +#include <linux/platform_device.h>
108 +#include <linux/tty.h>
109 +#include <linux/tty_flip.h>
110 +#include <linux/serial_core.h>
111 +#include <linux/serial.h>
112 +#include <linux/slab.h>
113 +#include <linux/io.h>
114 +#include <linux/irq.h>
116 +#include <asm/mach-ath79/ar933x_uart.h>
117 +#include <asm/mach-ath79/ar933x_uart_platform.h>
119 +#define DRIVER_NAME "ar933x-uart"
121 +#define AR933X_DUMMY_STATUS_RD 0x01
123 +static struct uart_driver ar933x_uart_driver;
125 +struct ar933x_uart_port {
126 + struct uart_port port;
127 + unsigned int ier; /* shadow Interrupt Enable Register */
130 +static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
133 + return readl(up->port.membase + offset);
136 +static inline void ar933x_uart_write(struct ar933x_uart_port *up,
137 + int offset, unsigned int value)
139 + writel(value, up->port.membase + offset);
142 +static inline void ar933x_uart_rmw(struct ar933x_uart_port *up,
143 + unsigned int offset,
149 + t = ar933x_uart_read(up, offset);
152 + ar933x_uart_write(up, offset, t);
155 +static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up,
156 + unsigned int offset,
159 + ar933x_uart_rmw(up, offset, 0, val);
162 +static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up,
163 + unsigned int offset,
166 + ar933x_uart_rmw(up, offset, val, 0);
169 +static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up)
171 + up->ier |= AR933X_UART_INT_TX_EMPTY;
172 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
175 +static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
177 + up->ier &= ~AR933X_UART_INT_TX_EMPTY;
178 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
181 +static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
183 + unsigned int rdata;
185 + rdata = ch & AR933X_UART_DATA_TX_RX_MASK;
186 + rdata |= AR933X_UART_DATA_TX_CSR;
187 + ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata);
190 +static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
192 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
193 + unsigned long flags;
194 + unsigned int rdata;
196 + spin_lock_irqsave(&up->port.lock, flags);
197 + rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
198 + spin_unlock_irqrestore(&up->port.lock, flags);
200 + return (rdata & AR933X_UART_DATA_TX_CSR) ? 0 : TIOCSER_TEMT;
203 +static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
208 +static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
212 +static void ar933x_uart_start_tx(struct uart_port *port)
214 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
216 + ar933x_uart_start_tx_interrupt(up);
219 +static void ar933x_uart_stop_tx(struct uart_port *port)
221 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
223 + ar933x_uart_stop_tx_interrupt(up);
226 +static void ar933x_uart_stop_rx(struct uart_port *port)
228 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
230 + up->ier &= ~AR933X_UART_INT_RX_VALID;
231 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
234 +static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
236 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
237 + unsigned long flags;
239 + spin_lock_irqsave(&up->port.lock, flags);
240 + if (break_state == -1)
241 + ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
242 + AR933X_UART_CS_TX_BREAK);
244 + ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
245 + AR933X_UART_CS_TX_BREAK);
246 + spin_unlock_irqrestore(&up->port.lock, flags);
249 +static void ar933x_uart_enable_ms(struct uart_port *port)
253 +static void ar933x_uart_set_termios(struct uart_port *port,
254 + struct ktermios *new,
255 + struct ktermios *old)
257 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
259 + unsigned long flags;
260 + unsigned int baud, scale;
262 + /* Only CS8 is supported */
263 + new->c_cflag &= ~CSIZE;
264 + new->c_cflag |= CS8;
266 + /* Only one stop bit is supported */
267 + new->c_cflag &= ~CSTOPB;
270 + if (new->c_cflag & PARENB) {
271 + if (!(new->c_cflag & PARODD))
272 + cs |= AR933X_UART_CS_PARITY_EVEN;
274 + cs |= AR933X_UART_CS_PARITY_ODD;
276 + cs |= AR933X_UART_CS_PARITY_NONE;
279 + /* Mark/space parity is not supported */
280 + new->c_cflag &= ~CMSPAR;
282 + baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
283 + scale = (port->uartclk / (16 * baud)) - 1;
286 + * Ok, we're now changing the port state. Do it with
287 + * interrupts disabled.
289 + spin_lock_irqsave(&up->port.lock, flags);
291 + /* Update the per-port timeout. */
292 + uart_update_timeout(port, new->c_cflag, baud);
294 + up->port.ignore_status_mask = 0;
296 + /* ignore all characters if CREAD is not set */
297 + if ((new->c_cflag & CREAD) == 0)
298 + up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD;
300 + ar933x_uart_write(up, AR933X_UART_CLOCK_REG,
301 + scale << AR933X_UART_CLOCK_SCALE_S | 8192);
303 + /* setup configuration register */
304 + ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs);
306 + /* enable host interrupt */
307 + ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
308 + AR933X_UART_CS_HOST_INT_EN);
310 + spin_unlock_irqrestore(&up->port.lock, flags);
312 + if (tty_termios_baud_rate(new))
313 + tty_termios_encode_baud_rate(new, baud, baud);
316 +static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
318 + struct tty_struct *tty;
319 + int max_count = 256;
321 + tty = tty_port_tty_get(&up->port.state->port);
323 + unsigned int rdata;
326 + rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
327 + if ((rdata & AR933X_UART_DATA_RX_CSR) == 0)
330 + /* remove the character from the FIFO */
331 + ar933x_uart_write(up, AR933X_UART_DATA_REG,
332 + AR933X_UART_DATA_RX_CSR);
335 + /* discard the data if no tty available */
339 + up->port.icount.rx++;
340 + ch = rdata & AR933X_UART_DATA_TX_RX_MASK;
342 + if (uart_handle_sysrq_char(&up->port, ch))
345 + if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0)
346 + tty_insert_flip_char(tty, ch, TTY_NORMAL);
347 + } while (max_count-- > 0);
350 + tty_flip_buffer_push(tty);
355 +static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
357 + struct circ_buf *xmit = &up->port.state->xmit;
360 + if (uart_tx_stopped(&up->port))
363 + count = up->port.fifosize;
365 + unsigned int rdata;
367 + rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG);
368 + if ((rdata & AR933X_UART_DATA_TX_CSR) == 0)
371 + if (up->port.x_char) {
372 + ar933x_uart_putc(up, up->port.x_char);
373 + up->port.icount.tx++;
374 + up->port.x_char = 0;
378 + if (uart_circ_empty(xmit))
381 + ar933x_uart_putc(up, xmit->buf[xmit->tail]);
383 + xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
384 + up->port.icount.tx++;
385 + } while (--count > 0);
387 + if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
388 + uart_write_wakeup(&up->port);
390 + if (!uart_circ_empty(xmit))
391 + ar933x_uart_start_tx_interrupt(up);
394 +static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
396 + struct ar933x_uart_port *up = dev_id;
397 + unsigned int status;
399 + status = ar933x_uart_read(up, AR933X_UART_CS_REG);
400 + if ((status & AR933X_UART_CS_HOST_INT) == 0)
403 + spin_lock(&up->port.lock);
405 + status = ar933x_uart_read(up, AR933X_UART_INT_REG);
406 + status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
408 + if (status & AR933X_UART_INT_RX_VALID) {
409 + ar933x_uart_write(up, AR933X_UART_INT_REG,
410 + AR933X_UART_INT_RX_VALID);
411 + ar933x_uart_rx_chars(up);
414 + if (status & AR933X_UART_INT_TX_EMPTY) {
415 + ar933x_uart_write(up, AR933X_UART_INT_REG,
416 + AR933X_UART_INT_TX_EMPTY);
417 + ar933x_uart_stop_tx_interrupt(up);
418 + ar933x_uart_tx_chars(up);
421 + spin_unlock(&up->port.lock);
423 + return IRQ_HANDLED;
426 +static int ar933x_uart_startup(struct uart_port *port)
428 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
429 + unsigned long flags;
432 + ret = request_irq(up->port.irq, ar933x_uart_interrupt,
433 + up->port.irqflags, dev_name(up->port.dev), up);
437 + spin_lock_irqsave(&up->port.lock, flags);
439 + /* Enable HOST interrupts */
440 + ar933x_uart_rmw_set(up, AR933X_UART_CS_REG,
441 + AR933X_UART_CS_HOST_INT_EN);
443 + /* Enable RX interrupts */
444 + up->ier = AR933X_UART_INT_RX_VALID;
445 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
447 + spin_unlock_irqrestore(&up->port.lock, flags);
452 +static void ar933x_uart_shutdown(struct uart_port *port)
454 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
456 + /* Disable all interrupts */
458 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
460 + /* Disable break condition */
461 + ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG,
462 + AR933X_UART_CS_TX_BREAK);
464 + free_irq(up->port.irq, up);
467 +static const char *ar933x_uart_type(struct uart_port *port)
469 + return (port->type == PORT_AR933X) ? "AR933X UART" : NULL;
472 +static void ar933x_uart_release_port(struct uart_port *port)
474 + /* Nothing to release ... */
477 +static int ar933x_uart_request_port(struct uart_port *port)
479 + /* UARTs always present */
483 +static void ar933x_uart_config_port(struct uart_port *port, int flags)
485 + if (flags & UART_CONFIG_TYPE)
486 + port->type = PORT_AR933X;
489 +static int ar933x_uart_verify_port(struct uart_port *port,
490 + struct serial_struct *ser)
492 + if (ser->type != PORT_UNKNOWN &&
493 + ser->type != PORT_AR933X)
496 + if (ser->irq < 0 || ser->irq >= NR_IRQS)
499 + if (ser->baud_base < 28800)
505 +static struct uart_ops ar933x_uart_ops = {
506 + .tx_empty = ar933x_uart_tx_empty,
507 + .set_mctrl = ar933x_uart_set_mctrl,
508 + .get_mctrl = ar933x_uart_get_mctrl,
509 + .stop_tx = ar933x_uart_stop_tx,
510 + .start_tx = ar933x_uart_start_tx,
511 + .stop_rx = ar933x_uart_stop_rx,
512 + .enable_ms = ar933x_uart_enable_ms,
513 + .break_ctl = ar933x_uart_break_ctl,
514 + .startup = ar933x_uart_startup,
515 + .shutdown = ar933x_uart_shutdown,
516 + .set_termios = ar933x_uart_set_termios,
517 + .type = ar933x_uart_type,
518 + .release_port = ar933x_uart_release_port,
519 + .request_port = ar933x_uart_request_port,
520 + .config_port = ar933x_uart_config_port,
521 + .verify_port = ar933x_uart_verify_port,
524 +#ifdef CONFIG_SERIAL_AR933X_CONSOLE
526 +static struct ar933x_uart_port *
527 +ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
529 +static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up)
531 + unsigned int status;
532 + unsigned int timeout = 60000;
534 + /* Wait up to 60ms for the character(s) to be sent. */
536 + status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
537 + if (--timeout == 0)
540 + } while ((status & AR933X_UART_DATA_TX_CSR) == 0);
543 +static void ar933x_uart_console_putchar(struct uart_port *port, int ch)
545 + struct ar933x_uart_port *up = (struct ar933x_uart_port *) port;
547 + ar933x_uart_wait_xmitr(up);
548 + ar933x_uart_putc(up, ch);
551 +static void ar933x_uart_console_write(struct console *co, const char *s,
552 + unsigned int count)
554 + struct ar933x_uart_port *up = ar933x_console_ports[co->index];
555 + unsigned long flags;
556 + unsigned int int_en;
559 + local_irq_save(flags);
561 + if (up->port.sysrq)
563 + else if (oops_in_progress)
564 + locked = spin_trylock(&up->port.lock);
566 + spin_lock(&up->port.lock);
569 + * First save the IER then disable the interrupts
571 + int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG);
572 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0);
574 + uart_console_write(&up->port, s, count, ar933x_uart_console_putchar);
577 + * Finally, wait for transmitter to become empty
578 + * and restore the IER
580 + ar933x_uart_wait_xmitr(up);
581 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en);
583 + ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS);
586 + spin_unlock(&up->port.lock);
588 + local_irq_restore(flags);
591 +static int ar933x_uart_console_setup(struct console *co, char *options)
593 + struct ar933x_uart_port *up;
599 + if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS)
602 + up = ar933x_console_ports[co->index];
607 + uart_parse_options(options, &baud, &parity, &bits, &flow);
609 + return uart_set_options(&up->port, co, baud, parity, bits, flow);
612 +static struct console ar933x_uart_console = {
614 + .write = ar933x_uart_console_write,
615 + .device = uart_console_device,
616 + .setup = ar933x_uart_console_setup,
617 + .flags = CON_PRINTBUFFER,
619 + .data = &ar933x_uart_driver,
622 +static void ar933x_uart_add_console_port(struct ar933x_uart_port *up)
624 + ar933x_console_ports[up->port.line] = up;
627 +#define AR933X_SERIAL_CONSOLE (&ar933x_uart_console)
631 +static inline void ar933x_uart_add_console_port(struct ar933x_uart_port *up) {}
633 +#define AR933X_SERIAL_CONSOLE NULL
635 +#endif /* CONFIG_SERIAL_AR933X_CONSOLE */
637 +static struct uart_driver ar933x_uart_driver = {
638 + .owner = THIS_MODULE,
639 + .driver_name = DRIVER_NAME,
640 + .dev_name = "ttyATH",
641 + .nr = CONFIG_SERIAL_AR933X_NR_UARTS,
642 + .cons = AR933X_SERIAL_CONSOLE,
645 +static int __devinit ar933x_uart_probe(struct platform_device *pdev)
647 + struct ar933x_uart_platform_data *pdata;
648 + struct ar933x_uart_port *up;
649 + struct uart_port *port;
650 + struct resource *mem_res;
651 + struct resource *irq_res;
655 + pdata = pdev->dev.platform_data;
663 + if (id > CONFIG_SERIAL_AR933X_NR_UARTS)
666 + mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
668 + dev_err(&pdev->dev, "no MEM resource\n");
672 + irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
674 + dev_err(&pdev->dev, "no IRQ resource\n");
678 + up = kzalloc(sizeof(struct ar933x_uart_port), GFP_KERNEL);
683 + port->mapbase = mem_res->start;
685 + port->membase = ioremap(mem_res->start, AR933X_UART_REGS_SIZE);
686 + if (!port->membase) {
692 + port->irq = irq_res->start;
693 + port->dev = &pdev->dev;
694 + port->type = PORT_AR933X;
695 + port->iotype = UPIO_MEM32;
696 + port->uartclk = pdata->uartclk;
698 + port->regshift = 2;
699 + port->fifosize = AR933X_UART_FIFO_SIZE;
700 + port->ops = &ar933x_uart_ops;
702 + ar933x_uart_add_console_port(up);
704 + ret = uart_add_one_port(&ar933x_uart_driver, &up->port);
708 + platform_set_drvdata(pdev, up);
712 + iounmap(up->port.membase);
718 +static int __devexit ar933x_uart_remove(struct platform_device *pdev)
720 + struct ar933x_uart_port *up;
722 + up = platform_get_drvdata(pdev);
723 + platform_set_drvdata(pdev, NULL);
726 + uart_remove_one_port(&ar933x_uart_driver, &up->port);
727 + iounmap(up->port.membase);
734 +static struct platform_driver ar933x_uart_platform_driver = {
735 + .probe = ar933x_uart_probe,
736 + .remove = __devexit_p(ar933x_uart_remove),
738 + .name = DRIVER_NAME,
739 + .owner = THIS_MODULE,
743 +static int __init ar933x_uart_init(void)
747 + ar933x_uart_driver.nr = CONFIG_SERIAL_AR933X_NR_UARTS;
748 + ret = uart_register_driver(&ar933x_uart_driver);
752 + ret = platform_driver_register(&ar933x_uart_platform_driver);
754 + goto err_unregister_uart_driver;
758 +err_unregister_uart_driver:
759 + uart_unregister_driver(&ar933x_uart_driver);
764 +static void __exit ar933x_uart_exit(void)
766 + platform_driver_unregister(&ar933x_uart_platform_driver);
767 + uart_unregister_driver(&ar933x_uart_driver);
770 +module_init(ar933x_uart_init);
771 +module_exit(ar933x_uart_exit);
773 +MODULE_DESCRIPTION("Atheros AR933X UART driver");
774 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
775 +MODULE_LICENSE("GPL v2");
776 +MODULE_ALIAS("platform:" DRIVER_NAME);
777 --- a/include/linux/serial_core.h
778 +++ b/include/linux/serial_core.h
780 /* Xilinx PSS UART */
781 #define PORT_XUARTPS 98
783 +/* Atheros AR933X SoC */
784 +#define PORT_AR933X 99
789 #include <linux/compiler.h>