1 // SPDX-License-Identifier: GPL-2.0+
3 * ***************************************************************************
4 * Marvell Armada-3700 Serial Driver
5 * Author: Wilson Ding <dingwei@marvell.com>
6 * Copyright (C) 2015 Marvell International Ltd.
7 * ***************************************************************************
10 #include <linux/clk.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
16 #include <linux/iopoll.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/serial.h>
24 #include <linux/serial_core.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
30 #define UART_STD_RBR 0x00
31 #define UART_EXT_RBR 0x18
33 #define UART_STD_TSH 0x04
34 #define UART_EXT_TSH 0x1C
36 #define UART_STD_CTRL1 0x08
37 #define UART_EXT_CTRL1 0x04
38 #define CTRL_SOFT_RST BIT(31)
39 #define CTRL_TXFIFO_RST BIT(15)
40 #define CTRL_RXFIFO_RST BIT(14)
41 #define CTRL_SND_BRK_SEQ BIT(11)
42 #define CTRL_BRK_DET_INT BIT(3)
43 #define CTRL_FRM_ERR_INT BIT(2)
44 #define CTRL_PAR_ERR_INT BIT(1)
45 #define CTRL_OVR_ERR_INT BIT(0)
46 #define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
47 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
49 #define UART_STD_CTRL2 UART_STD_CTRL1
50 #define UART_EXT_CTRL2 0x20
51 #define CTRL_STD_TX_RDY_INT BIT(5)
52 #define CTRL_EXT_TX_RDY_INT BIT(6)
53 #define CTRL_STD_RX_RDY_INT BIT(4)
54 #define CTRL_EXT_RX_RDY_INT BIT(5)
56 #define UART_STAT 0x0C
57 #define STAT_TX_FIFO_EMP BIT(13)
58 #define STAT_TX_FIFO_FUL BIT(11)
59 #define STAT_TX_EMP BIT(6)
60 #define STAT_STD_TX_RDY BIT(5)
61 #define STAT_EXT_TX_RDY BIT(15)
62 #define STAT_STD_RX_RDY BIT(4)
63 #define STAT_EXT_RX_RDY BIT(14)
64 #define STAT_BRK_DET BIT(3)
65 #define STAT_FRM_ERR BIT(2)
66 #define STAT_PAR_ERR BIT(1)
67 #define STAT_OVR_ERR BIT(0)
68 #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR \
69 | STAT_PAR_ERR | STAT_OVR_ERR)
71 #define UART_BRDV 0x10
72 #define BRDV_BAUD_MASK 0x3FF
74 #define UART_OSAMP 0x14
75 #define OSAMP_DEFAULT_DIVISOR 16
77 #define MVEBU_NR_UARTS 2
79 #define MVEBU_UART_TYPE "mvebu-uart"
80 #define DRIVER_NAME "mvebu_serial"
83 /* Either there is only one summed IRQ... */
85 /* ...or there are two separate IRQ for RX and TX */
91 /* Diverging register offsets */
92 struct uart_regs_layout {
101 unsigned int ctrl_tx_rdy_int;
102 unsigned int ctrl_rx_rdy_int;
103 unsigned int stat_tx_rdy;
104 unsigned int stat_rx_rdy;
107 /* Driver data, a structure for each UART port */
108 struct mvebu_uart_driver_data {
110 struct uart_regs_layout regs;
111 struct uart_flags flags;
114 /* Saved registers during suspend */
115 struct mvebu_uart_pm_regs {
125 /* MVEBU UART driver structure */
127 struct uart_port *port;
129 int irq[UART_IRQ_COUNT];
130 unsigned char __iomem *nb;
131 struct mvebu_uart_driver_data *data;
132 #if defined(CONFIG_PM)
133 struct mvebu_uart_pm_regs pm_regs;
134 #endif /* CONFIG_PM */
137 static struct mvebu_uart *to_mvuart(struct uart_port *port)
139 return (struct mvebu_uart *)port->private_data;
142 #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
144 #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
145 #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
146 #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
147 #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
149 #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
150 #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
151 #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
152 #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
154 static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
156 /* Core UART Driver Operations */
157 static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
162 spin_lock_irqsave(&port->lock, flags);
163 st = readl(port->membase + UART_STAT);
164 spin_unlock_irqrestore(&port->lock, flags);
166 return (st & STAT_TX_EMP) ? TIOCSER_TEMT : 0;
169 static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
171 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
174 static void mvebu_uart_set_mctrl(struct uart_port *port,
178 * Even if we do not support configuring the modem control lines, this
179 * function must be proided to the serial core
183 static void mvebu_uart_stop_tx(struct uart_port *port)
185 unsigned int ctl = readl(port->membase + UART_INTR(port));
187 ctl &= ~CTRL_TX_RDY_INT(port);
188 writel(ctl, port->membase + UART_INTR(port));
191 static void mvebu_uart_start_tx(struct uart_port *port)
194 struct circ_buf *xmit = &port->state->xmit;
196 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
197 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
198 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
202 ctl = readl(port->membase + UART_INTR(port));
203 ctl |= CTRL_TX_RDY_INT(port);
204 writel(ctl, port->membase + UART_INTR(port));
207 static void mvebu_uart_stop_rx(struct uart_port *port)
211 ctl = readl(port->membase + UART_CTRL(port));
212 ctl &= ~CTRL_BRK_INT;
213 writel(ctl, port->membase + UART_CTRL(port));
215 ctl = readl(port->membase + UART_INTR(port));
216 ctl &= ~CTRL_RX_RDY_INT(port);
217 writel(ctl, port->membase + UART_INTR(port));
220 static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
225 spin_lock_irqsave(&port->lock, flags);
226 ctl = readl(port->membase + UART_CTRL(port));
228 ctl |= CTRL_SND_BRK_SEQ;
230 ctl &= ~CTRL_SND_BRK_SEQ;
231 writel(ctl, port->membase + UART_CTRL(port));
232 spin_unlock_irqrestore(&port->lock, flags);
235 static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
237 struct tty_port *tport = &port->state->port;
238 unsigned char ch = 0;
242 if (status & STAT_RX_RDY(port)) {
243 ch = readl(port->membase + UART_RBR(port));
248 if (status & STAT_PAR_ERR)
249 port->icount.parity++;
252 if (status & STAT_BRK_DET) {
254 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
255 if (uart_handle_break(port))
259 if (status & STAT_OVR_ERR)
260 port->icount.overrun++;
262 if (status & STAT_FRM_ERR)
263 port->icount.frame++;
265 if (uart_handle_sysrq_char(port, ch))
268 if (status & port->ignore_status_mask & STAT_PAR_ERR)
269 status &= ~STAT_RX_RDY(port);
271 status &= port->read_status_mask;
273 if (status & STAT_PAR_ERR)
276 status &= ~port->ignore_status_mask;
278 if (status & STAT_RX_RDY(port))
279 tty_insert_flip_char(tport, ch, flag);
281 if (status & STAT_BRK_DET)
282 tty_insert_flip_char(tport, 0, TTY_BREAK);
284 if (status & STAT_FRM_ERR)
285 tty_insert_flip_char(tport, 0, TTY_FRAME);
287 if (status & STAT_OVR_ERR)
288 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
291 status = readl(port->membase + UART_STAT);
292 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
294 tty_flip_buffer_push(tport);
297 static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
299 struct circ_buf *xmit = &port->state->xmit;
304 writel(port->x_char, port->membase + UART_TSH(port));
310 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
311 mvebu_uart_stop_tx(port);
315 for (count = 0; count < port->fifosize; count++) {
316 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
317 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
320 if (uart_circ_empty(xmit))
323 st = readl(port->membase + UART_STAT);
324 if (st & STAT_TX_FIFO_FUL)
328 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
329 uart_write_wakeup(port);
331 if (uart_circ_empty(xmit))
332 mvebu_uart_stop_tx(port);
335 static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
337 struct uart_port *port = (struct uart_port *)dev_id;
338 unsigned int st = readl(port->membase + UART_STAT);
340 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
342 mvebu_uart_rx_chars(port, st);
344 if (st & STAT_TX_RDY(port))
345 mvebu_uart_tx_chars(port, st);
350 static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
352 struct uart_port *port = (struct uart_port *)dev_id;
353 unsigned int st = readl(port->membase + UART_STAT);
355 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
357 mvebu_uart_rx_chars(port, st);
362 static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
364 struct uart_port *port = (struct uart_port *)dev_id;
365 unsigned int st = readl(port->membase + UART_STAT);
367 if (st & STAT_TX_RDY(port))
368 mvebu_uart_tx_chars(port, st);
373 static int mvebu_uart_startup(struct uart_port *port)
375 struct mvebu_uart *mvuart = to_mvuart(port);
379 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
380 port->membase + UART_CTRL(port));
383 /* Clear the error bits of state register before IRQ request */
384 ret = readl(port->membase + UART_STAT);
386 writel(ret, port->membase + UART_STAT);
388 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
390 ctl = readl(port->membase + UART_INTR(port));
391 ctl |= CTRL_RX_RDY_INT(port);
392 writel(ctl, port->membase + UART_INTR(port));
394 if (!mvuart->irq[UART_TX_IRQ]) {
395 /* Old bindings with just one interrupt (UART0 only) */
396 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
397 mvebu_uart_isr, port->irqflags,
398 dev_name(port->dev), port);
400 dev_err(port->dev, "unable to request IRQ %d\n",
401 mvuart->irq[UART_IRQ_SUM]);
405 /* New bindings with an IRQ for RX and TX (both UART) */
406 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
407 mvebu_uart_rx_isr, port->irqflags,
408 dev_name(port->dev), port);
410 dev_err(port->dev, "unable to request IRQ %d\n",
411 mvuart->irq[UART_RX_IRQ]);
415 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
416 mvebu_uart_tx_isr, port->irqflags,
420 dev_err(port->dev, "unable to request IRQ %d\n",
421 mvuart->irq[UART_TX_IRQ]);
422 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
431 static void mvebu_uart_shutdown(struct uart_port *port)
433 struct mvebu_uart *mvuart = to_mvuart(port);
435 writel(0, port->membase + UART_INTR(port));
437 if (!mvuart->irq[UART_TX_IRQ]) {
438 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
440 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
441 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
445 static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
447 struct mvebu_uart *mvuart = to_mvuart(port);
448 unsigned int d_divisor, m_divisor;
451 if (IS_ERR(mvuart->clk))
452 return -PTR_ERR(mvuart->clk);
455 * The baudrate is derived from the UART clock thanks to two divisors:
456 * > D ("baud generator"): can divide the clock from 2 to 2^10 - 1.
457 * > M ("fractional divisor"): allows a better accuracy for
458 * baudrates higher than 230400.
460 * As the derivation of M is rather complicated, the code sticks to its
461 * default value (x16) when all the prescalers are zeroed, and only
462 * makes use of D to configure the desired baudrate.
464 m_divisor = OSAMP_DEFAULT_DIVISOR;
465 d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor);
467 brdv = readl(port->membase + UART_BRDV);
468 brdv &= ~BRDV_BAUD_MASK;
470 writel(brdv, port->membase + UART_BRDV);
475 static void mvebu_uart_set_termios(struct uart_port *port,
476 struct ktermios *termios,
477 struct ktermios *old)
480 unsigned int baud, min_baud, max_baud;
482 spin_lock_irqsave(&port->lock, flags);
484 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
485 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
487 if (termios->c_iflag & INPCK)
488 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
490 port->ignore_status_mask = 0;
491 if (termios->c_iflag & IGNPAR)
492 port->ignore_status_mask |=
493 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
495 if ((termios->c_cflag & CREAD) == 0)
496 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
499 * Maximal divisor is 1023 * 16 when using default (x16) scheme.
500 * Maximum achievable frequency with simple baudrate divisor is 230400.
501 * Since the error per bit frame would be of more than 15%, achieving
502 * higher frequencies would require to implement the fractional divisor
505 min_baud = DIV_ROUND_UP(port->uartclk, 1023 * 16);
508 baud = uart_get_baud_rate(port, termios, old, min_baud, max_baud);
509 if (mvebu_uart_baud_rate_set(port, baud)) {
510 /* No clock available, baudrate cannot be changed */
512 baud = uart_get_baud_rate(port, old, NULL,
515 tty_termios_encode_baud_rate(termios, baud, baud);
516 uart_update_timeout(port, termios->c_cflag, baud);
519 /* Only the following flag changes are supported */
521 termios->c_iflag &= INPCK | IGNPAR;
522 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
523 termios->c_cflag &= CREAD | CBAUD;
524 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
525 termios->c_cflag |= CS8;
528 spin_unlock_irqrestore(&port->lock, flags);
531 static const char *mvebu_uart_type(struct uart_port *port)
533 return MVEBU_UART_TYPE;
536 static void mvebu_uart_release_port(struct uart_port *port)
538 /* Nothing to do here */
541 static int mvebu_uart_request_port(struct uart_port *port)
546 #ifdef CONFIG_CONSOLE_POLL
547 static int mvebu_uart_get_poll_char(struct uart_port *port)
549 unsigned int st = readl(port->membase + UART_STAT);
551 if (!(st & STAT_RX_RDY(port)))
554 return readl(port->membase + UART_RBR(port));
557 static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
562 st = readl(port->membase + UART_STAT);
564 if (!(st & STAT_TX_FIFO_FUL))
570 writel(c, port->membase + UART_TSH(port));
574 static const struct uart_ops mvebu_uart_ops = {
575 .tx_empty = mvebu_uart_tx_empty,
576 .set_mctrl = mvebu_uart_set_mctrl,
577 .get_mctrl = mvebu_uart_get_mctrl,
578 .stop_tx = mvebu_uart_stop_tx,
579 .start_tx = mvebu_uart_start_tx,
580 .stop_rx = mvebu_uart_stop_rx,
581 .break_ctl = mvebu_uart_break_ctl,
582 .startup = mvebu_uart_startup,
583 .shutdown = mvebu_uart_shutdown,
584 .set_termios = mvebu_uart_set_termios,
585 .type = mvebu_uart_type,
586 .release_port = mvebu_uart_release_port,
587 .request_port = mvebu_uart_request_port,
588 #ifdef CONFIG_CONSOLE_POLL
589 .poll_get_char = mvebu_uart_get_poll_char,
590 .poll_put_char = mvebu_uart_put_poll_char,
594 /* Console Driver Operations */
596 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
598 static void mvebu_uart_putc(struct uart_port *port, int c)
603 st = readl(port->membase + UART_STAT);
604 if (!(st & STAT_TX_FIFO_FUL))
608 /* At early stage, DT is not parsed yet, only use UART0 */
609 writel(c, port->membase + UART_STD_TSH);
612 st = readl(port->membase + UART_STAT);
613 if (st & STAT_TX_FIFO_EMP)
618 static void mvebu_uart_putc_early_write(struct console *con,
622 struct earlycon_device *dev = con->data;
624 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
628 mvebu_uart_early_console_setup(struct earlycon_device *device,
631 if (!device->port.membase)
634 device->con->write = mvebu_uart_putc_early_write;
639 EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
640 OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
641 mvebu_uart_early_console_setup);
643 static void wait_for_xmitr(struct uart_port *port)
647 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
648 (val & STAT_TX_RDY(port)), 1, 10000);
651 static void wait_for_xmite(struct uart_port *port)
655 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
656 (val & STAT_TX_EMP), 1, 10000);
659 static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
661 wait_for_xmitr(port);
662 writel(ch, port->membase + UART_TSH(port));
665 static void mvebu_uart_console_write(struct console *co, const char *s,
668 struct uart_port *port = &mvebu_uart_ports[co->index];
670 unsigned int ier, intr, ctl;
673 if (oops_in_progress)
674 locked = spin_trylock_irqsave(&port->lock, flags);
676 spin_lock_irqsave(&port->lock, flags);
678 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
679 intr = readl(port->membase + UART_INTR(port)) &
680 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
681 writel(0, port->membase + UART_CTRL(port));
682 writel(0, port->membase + UART_INTR(port));
684 uart_console_write(port, s, count, mvebu_uart_console_putchar);
686 wait_for_xmite(port);
689 writel(ier, port->membase + UART_CTRL(port));
692 ctl = intr | readl(port->membase + UART_INTR(port));
693 writel(ctl, port->membase + UART_INTR(port));
697 spin_unlock_irqrestore(&port->lock, flags);
700 static int mvebu_uart_console_setup(struct console *co, char *options)
702 struct uart_port *port;
708 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
711 port = &mvebu_uart_ports[co->index];
713 if (!port->mapbase || !port->membase) {
714 pr_debug("console on ttyMV%i not present\n", co->index);
719 uart_parse_options(options, &baud, &parity, &bits, &flow);
721 return uart_set_options(port, co, baud, parity, bits, flow);
724 static struct uart_driver mvebu_uart_driver;
726 static struct console mvebu_uart_console = {
728 .write = mvebu_uart_console_write,
729 .device = uart_console_device,
730 .setup = mvebu_uart_console_setup,
731 .flags = CON_PRINTBUFFER,
733 .data = &mvebu_uart_driver,
736 static int __init mvebu_uart_console_init(void)
738 register_console(&mvebu_uart_console);
742 console_initcall(mvebu_uart_console_init);
745 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
747 static struct uart_driver mvebu_uart_driver = {
748 .owner = THIS_MODULE,
749 .driver_name = DRIVER_NAME,
751 .nr = MVEBU_NR_UARTS,
752 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
753 .cons = &mvebu_uart_console,
757 #if defined(CONFIG_PM)
758 static int mvebu_uart_suspend(struct device *dev)
760 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
761 struct uart_port *port = mvuart->port;
763 uart_suspend_port(&mvebu_uart_driver, port);
765 mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port));
766 mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port));
767 mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port));
768 mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port));
769 mvuart->pm_regs.stat = readl(port->membase + UART_STAT);
770 mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV);
771 mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP);
773 device_set_wakeup_enable(dev, true);
778 static int mvebu_uart_resume(struct device *dev)
780 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
781 struct uart_port *port = mvuart->port;
783 writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port));
784 writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port));
785 writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port));
786 writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port));
787 writel(mvuart->pm_regs.stat, port->membase + UART_STAT);
788 writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV);
789 writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP);
791 uart_resume_port(&mvebu_uart_driver, port);
796 static const struct dev_pm_ops mvebu_uart_pm_ops = {
797 .suspend = mvebu_uart_suspend,
798 .resume = mvebu_uart_resume,
800 #endif /* CONFIG_PM */
802 static const struct of_device_id mvebu_uart_of_match[];
804 /* Counter to keep track of each UART port id when not using CONFIG_OF */
805 static int uart_num_counter;
807 static int mvebu_uart_probe(struct platform_device *pdev)
809 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
810 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
812 struct uart_port *port;
813 struct mvebu_uart *mvuart;
817 dev_err(&pdev->dev, "no registers defined\n");
821 /* Assume that all UART ports have a DT alias or none has */
822 id = of_alias_get_id(pdev->dev.of_node, "serial");
823 if (!pdev->dev.of_node || id < 0)
824 pdev->id = uart_num_counter++;
828 if (pdev->id >= MVEBU_NR_UARTS) {
829 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
834 port = &mvebu_uart_ports[pdev->id];
836 spin_lock_init(&port->lock);
838 port->dev = &pdev->dev;
839 port->type = PORT_MVEBU;
840 port->ops = &mvebu_uart_ops;
844 port->iotype = UPIO_MEM32;
845 port->flags = UPF_FIXED_PORT;
846 port->line = pdev->id;
849 * IRQ number is not stored in this structure because we may have two of
850 * them per port (RX and TX). Instead, use the driver UART structure
851 * array so called ->irq[].
855 port->mapbase = reg->start;
857 port->membase = devm_ioremap_resource(&pdev->dev, reg);
858 if (IS_ERR(port->membase))
859 return PTR_ERR(port->membase);
861 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
866 /* Get controller data depending on the compatible string */
867 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
870 port->private_data = mvuart;
871 platform_set_drvdata(pdev, mvuart);
873 /* Get fixed clock frequency */
874 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
875 if (IS_ERR(mvuart->clk)) {
876 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
877 return PTR_ERR(mvuart->clk);
879 if (IS_EXTENDED(port)) {
880 dev_err(&pdev->dev, "unable to get UART clock\n");
881 return PTR_ERR(mvuart->clk);
884 if (!clk_prepare_enable(mvuart->clk))
885 port->uartclk = clk_get_rate(mvuart->clk);
888 /* Manage interrupts */
889 if (platform_irq_count(pdev) == 1) {
890 /* Old bindings: no name on the single unamed UART0 IRQ */
891 irq = platform_get_irq(pdev, 0);
893 dev_err(&pdev->dev, "unable to get UART IRQ\n");
897 mvuart->irq[UART_IRQ_SUM] = irq;
900 * New bindings: named interrupts (RX, TX) for both UARTS,
901 * only make use of uart-rx and uart-tx interrupts, do not use
902 * uart-sum of UART0 port.
904 irq = platform_get_irq_byname(pdev, "uart-rx");
906 dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
910 mvuart->irq[UART_RX_IRQ] = irq;
912 irq = platform_get_irq_byname(pdev, "uart-tx");
914 dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
918 mvuart->irq[UART_TX_IRQ] = irq;
922 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
924 writel(0, port->membase + UART_CTRL(port));
926 ret = uart_add_one_port(&mvebu_uart_driver, port);
932 static struct mvebu_uart_driver_data uart_std_driver_data = {
934 .regs.rbr = UART_STD_RBR,
935 .regs.tsh = UART_STD_TSH,
936 .regs.ctrl = UART_STD_CTRL1,
937 .regs.intr = UART_STD_CTRL2,
938 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
939 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
940 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
941 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
944 static struct mvebu_uart_driver_data uart_ext_driver_data = {
946 .regs.rbr = UART_EXT_RBR,
947 .regs.tsh = UART_EXT_TSH,
948 .regs.ctrl = UART_EXT_CTRL1,
949 .regs.intr = UART_EXT_CTRL2,
950 .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
951 .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
952 .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
953 .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
956 /* Match table for of_platform binding */
957 static const struct of_device_id mvebu_uart_of_match[] = {
959 .compatible = "marvell,armada-3700-uart",
960 .data = (void *)&uart_std_driver_data,
963 .compatible = "marvell,armada-3700-uart-ext",
964 .data = (void *)&uart_ext_driver_data,
969 static struct platform_driver mvebu_uart_platform_driver = {
970 .probe = mvebu_uart_probe,
972 .name = "mvebu-uart",
973 .of_match_table = of_match_ptr(mvebu_uart_of_match),
974 .suppress_bind_attrs = true,
975 #if defined(CONFIG_PM)
976 .pm = &mvebu_uart_pm_ops,
977 #endif /* CONFIG_PM */
981 static int __init mvebu_uart_init(void)
985 ret = uart_register_driver(&mvebu_uart_driver);
989 ret = platform_driver_register(&mvebu_uart_platform_driver);
991 uart_unregister_driver(&mvebu_uart_driver);
995 arch_initcall(mvebu_uart_init);