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/clk-provider.h>
12 #include <linux/console.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
17 #include <linux/iopoll.h>
18 #include <linux/math64.h>
20 #include <linux/of_address.h>
21 #include <linux/of_device.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/serial.h>
26 #include <linux/serial_core.h>
27 #include <linux/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
32 #define UART_STD_RBR 0x00
33 #define UART_EXT_RBR 0x18
35 #define UART_STD_TSH 0x04
36 #define UART_EXT_TSH 0x1C
38 #define UART_STD_CTRL1 0x08
39 #define UART_EXT_CTRL1 0x04
40 #define CTRL_SOFT_RST BIT(31)
41 #define CTRL_TXFIFO_RST BIT(15)
42 #define CTRL_RXFIFO_RST BIT(14)
43 #define CTRL_SND_BRK_SEQ BIT(11)
44 #define CTRL_BRK_DET_INT BIT(3)
45 #define CTRL_FRM_ERR_INT BIT(2)
46 #define CTRL_PAR_ERR_INT BIT(1)
47 #define CTRL_OVR_ERR_INT BIT(0)
48 #define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
49 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
51 #define UART_STD_CTRL2 UART_STD_CTRL1
52 #define UART_EXT_CTRL2 0x20
53 #define CTRL_STD_TX_RDY_INT BIT(5)
54 #define CTRL_EXT_TX_RDY_INT BIT(6)
55 #define CTRL_STD_RX_RDY_INT BIT(4)
56 #define CTRL_EXT_RX_RDY_INT BIT(5)
58 #define UART_STAT 0x0C
59 #define STAT_TX_FIFO_EMP BIT(13)
60 #define STAT_TX_FIFO_FUL BIT(11)
61 #define STAT_TX_EMP BIT(6)
62 #define STAT_STD_TX_RDY BIT(5)
63 #define STAT_EXT_TX_RDY BIT(15)
64 #define STAT_STD_RX_RDY BIT(4)
65 #define STAT_EXT_RX_RDY BIT(14)
66 #define STAT_BRK_DET BIT(3)
67 #define STAT_FRM_ERR BIT(2)
68 #define STAT_PAR_ERR BIT(1)
69 #define STAT_OVR_ERR BIT(0)
70 #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR \
71 | STAT_PAR_ERR | STAT_OVR_ERR)
74 * Marvell Armada 3700 Functional Specifications describes that bit 21 of UART
75 * Clock Control register controls UART1 and bit 20 controls UART2. But in
76 * reality bit 21 controls UART2 and bit 20 controls UART1. This seems to be an
77 * error in Marvell's documentation. Hence following CLK_DIS macros are swapped.
80 #define UART_BRDV 0x10
81 /* These bits are located in UART1 address space and control UART2 */
82 #define UART2_CLK_DIS BIT(21)
83 /* These bits are located in UART1 address space and control UART1 */
84 #define UART1_CLK_DIS BIT(20)
85 /* These bits are located in UART1 address space and control both UARTs */
86 #define CLK_NO_XTAL BIT(19)
87 #define CLK_TBG_DIV1_SHIFT 15
88 #define CLK_TBG_DIV1_MASK 0x7
89 #define CLK_TBG_DIV1_MAX 6
90 #define CLK_TBG_DIV2_SHIFT 12
91 #define CLK_TBG_DIV2_MASK 0x7
92 #define CLK_TBG_DIV2_MAX 6
93 #define CLK_TBG_SEL_SHIFT 10
94 #define CLK_TBG_SEL_MASK 0x3
95 /* These bits are located in both UARTs address space */
96 #define BRDV_BAUD_MASK 0x3FF
97 #define BRDV_BAUD_MAX BRDV_BAUD_MASK
99 #define UART_OSAMP 0x14
100 #define OSAMP_DEFAULT_DIVISOR 16
101 #define OSAMP_DIVISORS_MASK 0x3F3F3F3F
102 #define OSAMP_MAX_DIVISOR 63
104 #define MVEBU_NR_UARTS 2
106 #define MVEBU_UART_TYPE "mvebu-uart"
107 #define DRIVER_NAME "mvebu_serial"
110 /* Either there is only one summed IRQ... */
112 /* ...or there are two separate IRQ for RX and TX */
118 /* Diverging register offsets */
119 struct uart_regs_layout {
126 /* Diverging flags */
128 unsigned int ctrl_tx_rdy_int;
129 unsigned int ctrl_rx_rdy_int;
130 unsigned int stat_tx_rdy;
131 unsigned int stat_rx_rdy;
134 /* Driver data, a structure for each UART port */
135 struct mvebu_uart_driver_data {
137 struct uart_regs_layout regs;
138 struct uart_flags flags;
141 /* Saved registers during suspend */
142 struct mvebu_uart_pm_regs {
152 /* MVEBU UART driver structure */
154 struct uart_port *port;
156 int irq[UART_IRQ_COUNT];
157 struct mvebu_uart_driver_data *data;
158 #if defined(CONFIG_PM)
159 struct mvebu_uart_pm_regs pm_regs;
160 #endif /* CONFIG_PM */
163 static struct mvebu_uart *to_mvuart(struct uart_port *port)
165 return (struct mvebu_uart *)port->private_data;
168 #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
170 #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
171 #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
172 #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
173 #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
175 #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
176 #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
177 #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
178 #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
180 static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
182 static DEFINE_SPINLOCK(mvebu_uart_lock);
184 /* Core UART Driver Operations */
185 static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
190 spin_lock_irqsave(&port->lock, flags);
191 st = readl(port->membase + UART_STAT);
192 spin_unlock_irqrestore(&port->lock, flags);
194 return (st & STAT_TX_EMP) ? TIOCSER_TEMT : 0;
197 static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
199 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
202 static void mvebu_uart_set_mctrl(struct uart_port *port,
206 * Even if we do not support configuring the modem control lines, this
207 * function must be proided to the serial core
211 static void mvebu_uart_stop_tx(struct uart_port *port)
213 unsigned int ctl = readl(port->membase + UART_INTR(port));
215 ctl &= ~CTRL_TX_RDY_INT(port);
216 writel(ctl, port->membase + UART_INTR(port));
219 static void mvebu_uart_start_tx(struct uart_port *port)
222 struct circ_buf *xmit = &port->state->xmit;
224 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
225 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
226 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
230 ctl = readl(port->membase + UART_INTR(port));
231 ctl |= CTRL_TX_RDY_INT(port);
232 writel(ctl, port->membase + UART_INTR(port));
235 static void mvebu_uart_stop_rx(struct uart_port *port)
239 ctl = readl(port->membase + UART_CTRL(port));
240 ctl &= ~CTRL_BRK_INT;
241 writel(ctl, port->membase + UART_CTRL(port));
243 ctl = readl(port->membase + UART_INTR(port));
244 ctl &= ~CTRL_RX_RDY_INT(port);
245 writel(ctl, port->membase + UART_INTR(port));
248 static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
253 spin_lock_irqsave(&port->lock, flags);
254 ctl = readl(port->membase + UART_CTRL(port));
256 ctl |= CTRL_SND_BRK_SEQ;
258 ctl &= ~CTRL_SND_BRK_SEQ;
259 writel(ctl, port->membase + UART_CTRL(port));
260 spin_unlock_irqrestore(&port->lock, flags);
263 static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
265 struct tty_port *tport = &port->state->port;
266 unsigned char ch = 0;
271 if (status & STAT_RX_RDY(port)) {
272 ch = readl(port->membase + UART_RBR(port));
277 if (status & STAT_PAR_ERR)
278 port->icount.parity++;
282 * For UART2, error bits are not cleared on buffer read.
283 * This causes interrupt loop and system hang.
285 if (IS_EXTENDED(port) && (status & STAT_BRK_ERR)) {
286 ret = readl(port->membase + UART_STAT);
288 writel(ret, port->membase + UART_STAT);
291 if (status & STAT_BRK_DET) {
293 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
294 if (uart_handle_break(port))
298 if (status & STAT_OVR_ERR)
299 port->icount.overrun++;
301 if (status & STAT_FRM_ERR)
302 port->icount.frame++;
304 if (uart_handle_sysrq_char(port, ch))
307 if (status & port->ignore_status_mask & STAT_PAR_ERR)
308 status &= ~STAT_RX_RDY(port);
310 status &= port->read_status_mask;
312 if (status & STAT_PAR_ERR)
315 status &= ~port->ignore_status_mask;
317 if (status & STAT_RX_RDY(port))
318 tty_insert_flip_char(tport, ch, flag);
320 if (status & STAT_BRK_DET)
321 tty_insert_flip_char(tport, 0, TTY_BREAK);
323 if (status & STAT_FRM_ERR)
324 tty_insert_flip_char(tport, 0, TTY_FRAME);
326 if (status & STAT_OVR_ERR)
327 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
330 status = readl(port->membase + UART_STAT);
331 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
333 tty_flip_buffer_push(tport);
336 static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
338 struct circ_buf *xmit = &port->state->xmit;
343 writel(port->x_char, port->membase + UART_TSH(port));
349 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
350 mvebu_uart_stop_tx(port);
354 for (count = 0; count < port->fifosize; count++) {
355 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
356 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
359 if (uart_circ_empty(xmit))
362 st = readl(port->membase + UART_STAT);
363 if (st & STAT_TX_FIFO_FUL)
367 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
368 uart_write_wakeup(port);
370 if (uart_circ_empty(xmit))
371 mvebu_uart_stop_tx(port);
374 static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
376 struct uart_port *port = (struct uart_port *)dev_id;
377 unsigned int st = readl(port->membase + UART_STAT);
379 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
381 mvebu_uart_rx_chars(port, st);
383 if (st & STAT_TX_RDY(port))
384 mvebu_uart_tx_chars(port, st);
389 static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
391 struct uart_port *port = (struct uart_port *)dev_id;
392 unsigned int st = readl(port->membase + UART_STAT);
394 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
396 mvebu_uart_rx_chars(port, st);
401 static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
403 struct uart_port *port = (struct uart_port *)dev_id;
404 unsigned int st = readl(port->membase + UART_STAT);
406 if (st & STAT_TX_RDY(port))
407 mvebu_uart_tx_chars(port, st);
412 static int mvebu_uart_startup(struct uart_port *port)
414 struct mvebu_uart *mvuart = to_mvuart(port);
418 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
419 port->membase + UART_CTRL(port));
422 /* Clear the error bits of state register before IRQ request */
423 ret = readl(port->membase + UART_STAT);
425 writel(ret, port->membase + UART_STAT);
427 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
429 ctl = readl(port->membase + UART_INTR(port));
430 ctl |= CTRL_RX_RDY_INT(port);
431 writel(ctl, port->membase + UART_INTR(port));
433 if (!mvuart->irq[UART_TX_IRQ]) {
434 /* Old bindings with just one interrupt (UART0 only) */
435 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
436 mvebu_uart_isr, port->irqflags,
437 dev_name(port->dev), port);
439 dev_err(port->dev, "unable to request IRQ %d\n",
440 mvuart->irq[UART_IRQ_SUM]);
444 /* New bindings with an IRQ for RX and TX (both UART) */
445 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
446 mvebu_uart_rx_isr, port->irqflags,
447 dev_name(port->dev), port);
449 dev_err(port->dev, "unable to request IRQ %d\n",
450 mvuart->irq[UART_RX_IRQ]);
454 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
455 mvebu_uart_tx_isr, port->irqflags,
459 dev_err(port->dev, "unable to request IRQ %d\n",
460 mvuart->irq[UART_TX_IRQ]);
461 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
470 static void mvebu_uart_shutdown(struct uart_port *port)
472 struct mvebu_uart *mvuart = to_mvuart(port);
474 writel(0, port->membase + UART_INTR(port));
476 if (!mvuart->irq[UART_TX_IRQ]) {
477 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
479 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
480 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
484 static unsigned int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
486 unsigned int d_divisor, m_divisor;
494 * The baudrate is derived from the UART clock thanks to divisors:
495 * > d1 * d2 ("TBG divisors"): can divide only TBG clock from 1 to 6
496 * > D ("baud generator"): can divide the clock from 1 to 1023
497 * > M ("fractional divisor"): allows a better accuracy (from 1 to 63)
499 * Exact formulas for calculating baudrate:
501 * with default x16 scheme:
502 * baudrate = xtal / (d * 16)
503 * baudrate = tbg / (d1 * d2 * d * 16)
505 * with fractional divisor:
506 * baudrate = 10 * xtal / (d * (3 * (m1 + m2) + 2 * (m3 + m4)))
507 * baudrate = 10 * tbg / (d1*d2 * d * (3 * (m1 + m2) + 2 * (m3 + m4)))
509 * Oversampling value:
510 * osamp = (m1 << 0) | (m2 << 8) | (m3 << 16) | (m4 << 24);
512 * Where m1 controls number of clock cycles per bit for bits 1,2,3;
513 * m2 for bits 4,5,6; m3 for bits 7,8 and m4 for bits 9,10.
515 * To simplify baudrate setup set all the M prescalers to the same
516 * value. For baudrates 9600 Bd and higher, it is enough to use the
517 * default (x16) divisor or fractional divisor with M = 63, so there
518 * is no need to use real fractional support (where the M prescalers
521 * When all the M prescalers are zeroed then default (x16) divisor is
522 * used. Default x16 scheme is more stable than M (fractional divisor),
523 * so use M only when D divisor is not enough to derive baudrate.
525 * Member port->uartclk is either xtal clock rate or TBG clock rate
526 * divided by (d1 * d2). So d1 and d2 are already set by the UART clock
527 * driver (and UART driver itself cannot change them). Moreover they are
528 * shared between both UARTs.
531 m_divisor = OSAMP_DEFAULT_DIVISOR;
532 d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor);
534 if (d_divisor > BRDV_BAUD_MAX) {
536 * Experiments show that small M divisors are unstable.
537 * Use maximal possible M = 63 and calculate D divisor.
539 m_divisor = OSAMP_MAX_DIVISOR;
540 d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor);
545 else if (d_divisor > BRDV_BAUD_MAX)
546 d_divisor = BRDV_BAUD_MAX;
548 spin_lock_irqsave(&mvebu_uart_lock, flags);
549 brdv = readl(port->membase + UART_BRDV);
550 brdv &= ~BRDV_BAUD_MASK;
552 writel(brdv, port->membase + UART_BRDV);
553 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
555 osamp = readl(port->membase + UART_OSAMP);
556 osamp &= ~OSAMP_DIVISORS_MASK;
557 if (m_divisor != OSAMP_DEFAULT_DIVISOR)
558 osamp |= (m_divisor << 0) | (m_divisor << 8) |
559 (m_divisor << 16) | (m_divisor << 24);
560 writel(osamp, port->membase + UART_OSAMP);
562 return DIV_ROUND_CLOSEST(port->uartclk, d_divisor * m_divisor);
565 static void mvebu_uart_set_termios(struct uart_port *port,
566 struct ktermios *termios,
567 const struct ktermios *old)
570 unsigned int baud, min_baud, max_baud;
572 spin_lock_irqsave(&port->lock, flags);
574 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
575 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
577 if (termios->c_iflag & INPCK)
578 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
580 port->ignore_status_mask = 0;
581 if (termios->c_iflag & IGNPAR)
582 port->ignore_status_mask |=
583 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
585 if ((termios->c_cflag & CREAD) == 0)
586 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
589 * Maximal divisor is 1023 and maximal fractional divisor is 63. And
590 * experiments show that baudrates above 1/80 of parent clock rate are
591 * not stable. So disallow baudrates above 1/80 of the parent clock
592 * rate. If port->uartclk is not available, then
593 * mvebu_uart_baud_rate_set() fails, so values min_baud and max_baud
594 * in this case do not matter.
596 min_baud = DIV_ROUND_UP(port->uartclk, BRDV_BAUD_MAX *
598 max_baud = port->uartclk / 80;
600 baud = uart_get_baud_rate(port, termios, old, min_baud, max_baud);
601 baud = mvebu_uart_baud_rate_set(port, baud);
603 /* In case baudrate cannot be changed, report previous old value */
604 if (baud == 0 && old)
605 baud = tty_termios_baud_rate(old);
607 /* Only the following flag changes are supported */
609 termios->c_iflag &= INPCK | IGNPAR;
610 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
611 termios->c_cflag &= CREAD | CBAUD;
612 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
613 termios->c_cflag |= CS8;
617 tty_termios_encode_baud_rate(termios, baud, baud);
618 uart_update_timeout(port, termios->c_cflag, baud);
621 spin_unlock_irqrestore(&port->lock, flags);
624 static const char *mvebu_uart_type(struct uart_port *port)
626 return MVEBU_UART_TYPE;
629 static void mvebu_uart_release_port(struct uart_port *port)
631 /* Nothing to do here */
634 static int mvebu_uart_request_port(struct uart_port *port)
639 #ifdef CONFIG_CONSOLE_POLL
640 static int mvebu_uart_get_poll_char(struct uart_port *port)
642 unsigned int st = readl(port->membase + UART_STAT);
644 if (!(st & STAT_RX_RDY(port)))
647 return readl(port->membase + UART_RBR(port));
650 static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
655 st = readl(port->membase + UART_STAT);
657 if (!(st & STAT_TX_FIFO_FUL))
663 writel(c, port->membase + UART_TSH(port));
667 static const struct uart_ops mvebu_uart_ops = {
668 .tx_empty = mvebu_uart_tx_empty,
669 .set_mctrl = mvebu_uart_set_mctrl,
670 .get_mctrl = mvebu_uart_get_mctrl,
671 .stop_tx = mvebu_uart_stop_tx,
672 .start_tx = mvebu_uart_start_tx,
673 .stop_rx = mvebu_uart_stop_rx,
674 .break_ctl = mvebu_uart_break_ctl,
675 .startup = mvebu_uart_startup,
676 .shutdown = mvebu_uart_shutdown,
677 .set_termios = mvebu_uart_set_termios,
678 .type = mvebu_uart_type,
679 .release_port = mvebu_uart_release_port,
680 .request_port = mvebu_uart_request_port,
681 #ifdef CONFIG_CONSOLE_POLL
682 .poll_get_char = mvebu_uart_get_poll_char,
683 .poll_put_char = mvebu_uart_put_poll_char,
687 /* Console Driver Operations */
689 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
691 static void mvebu_uart_putc(struct uart_port *port, unsigned char c)
696 st = readl(port->membase + UART_STAT);
697 if (!(st & STAT_TX_FIFO_FUL))
701 /* At early stage, DT is not parsed yet, only use UART0 */
702 writel(c, port->membase + UART_STD_TSH);
705 st = readl(port->membase + UART_STAT);
706 if (st & STAT_TX_FIFO_EMP)
711 static void mvebu_uart_putc_early_write(struct console *con,
715 struct earlycon_device *dev = con->data;
717 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
721 mvebu_uart_early_console_setup(struct earlycon_device *device,
724 if (!device->port.membase)
727 device->con->write = mvebu_uart_putc_early_write;
732 EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
733 OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
734 mvebu_uart_early_console_setup);
736 static void wait_for_xmitr(struct uart_port *port)
740 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
741 (val & STAT_TX_RDY(port)), 1, 10000);
744 static void wait_for_xmite(struct uart_port *port)
748 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
749 (val & STAT_TX_EMP), 1, 10000);
752 static void mvebu_uart_console_putchar(struct uart_port *port, unsigned char ch)
754 wait_for_xmitr(port);
755 writel(ch, port->membase + UART_TSH(port));
758 static void mvebu_uart_console_write(struct console *co, const char *s,
761 struct uart_port *port = &mvebu_uart_ports[co->index];
763 unsigned int ier, intr, ctl;
766 if (oops_in_progress)
767 locked = spin_trylock_irqsave(&port->lock, flags);
769 spin_lock_irqsave(&port->lock, flags);
771 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
772 intr = readl(port->membase + UART_INTR(port)) &
773 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
774 writel(0, port->membase + UART_CTRL(port));
775 writel(0, port->membase + UART_INTR(port));
777 uart_console_write(port, s, count, mvebu_uart_console_putchar);
779 wait_for_xmite(port);
782 writel(ier, port->membase + UART_CTRL(port));
785 ctl = intr | readl(port->membase + UART_INTR(port));
786 writel(ctl, port->membase + UART_INTR(port));
790 spin_unlock_irqrestore(&port->lock, flags);
793 static int mvebu_uart_console_setup(struct console *co, char *options)
795 struct uart_port *port;
801 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
804 port = &mvebu_uart_ports[co->index];
806 if (!port->mapbase || !port->membase) {
807 pr_debug("console on ttyMV%i not present\n", co->index);
812 uart_parse_options(options, &baud, &parity, &bits, &flow);
814 return uart_set_options(port, co, baud, parity, bits, flow);
817 static struct uart_driver mvebu_uart_driver;
819 static struct console mvebu_uart_console = {
821 .write = mvebu_uart_console_write,
822 .device = uart_console_device,
823 .setup = mvebu_uart_console_setup,
824 .flags = CON_PRINTBUFFER,
826 .data = &mvebu_uart_driver,
829 static int __init mvebu_uart_console_init(void)
831 register_console(&mvebu_uart_console);
835 console_initcall(mvebu_uart_console_init);
838 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
840 static struct uart_driver mvebu_uart_driver = {
841 .owner = THIS_MODULE,
842 .driver_name = DRIVER_NAME,
844 .nr = MVEBU_NR_UARTS,
845 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
846 .cons = &mvebu_uart_console,
850 #if defined(CONFIG_PM)
851 static int mvebu_uart_suspend(struct device *dev)
853 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
854 struct uart_port *port = mvuart->port;
857 uart_suspend_port(&mvebu_uart_driver, port);
859 mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port));
860 mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port));
861 mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port));
862 mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port));
863 mvuart->pm_regs.stat = readl(port->membase + UART_STAT);
864 spin_lock_irqsave(&mvebu_uart_lock, flags);
865 mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV);
866 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
867 mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP);
869 device_set_wakeup_enable(dev, true);
874 static int mvebu_uart_resume(struct device *dev)
876 struct mvebu_uart *mvuart = dev_get_drvdata(dev);
877 struct uart_port *port = mvuart->port;
880 writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port));
881 writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port));
882 writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port));
883 writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port));
884 writel(mvuart->pm_regs.stat, port->membase + UART_STAT);
885 spin_lock_irqsave(&mvebu_uart_lock, flags);
886 writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV);
887 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
888 writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP);
890 uart_resume_port(&mvebu_uart_driver, port);
895 static const struct dev_pm_ops mvebu_uart_pm_ops = {
896 .suspend = mvebu_uart_suspend,
897 .resume = mvebu_uart_resume,
899 #endif /* CONFIG_PM */
901 static const struct of_device_id mvebu_uart_of_match[];
903 /* Counter to keep track of each UART port id when not using CONFIG_OF */
904 static int uart_num_counter;
906 static int mvebu_uart_probe(struct platform_device *pdev)
908 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
909 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
911 struct uart_port *port;
912 struct mvebu_uart *mvuart;
916 dev_err(&pdev->dev, "no registers defined\n");
920 /* Assume that all UART ports have a DT alias or none has */
921 id = of_alias_get_id(pdev->dev.of_node, "serial");
922 if (!pdev->dev.of_node || id < 0)
923 pdev->id = uart_num_counter++;
927 if (pdev->id >= MVEBU_NR_UARTS) {
928 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
933 port = &mvebu_uart_ports[pdev->id];
935 spin_lock_init(&port->lock);
937 port->dev = &pdev->dev;
938 port->type = PORT_MVEBU;
939 port->ops = &mvebu_uart_ops;
943 port->iotype = UPIO_MEM32;
944 port->flags = UPF_FIXED_PORT;
945 port->line = pdev->id;
948 * IRQ number is not stored in this structure because we may have two of
949 * them per port (RX and TX). Instead, use the driver UART structure
950 * array so called ->irq[].
954 port->mapbase = reg->start;
956 port->membase = devm_ioremap_resource(&pdev->dev, reg);
957 if (IS_ERR(port->membase))
958 return PTR_ERR(port->membase);
960 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
965 /* Get controller data depending on the compatible string */
966 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
969 port->private_data = mvuart;
970 platform_set_drvdata(pdev, mvuart);
972 /* Get fixed clock frequency */
973 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
974 if (IS_ERR(mvuart->clk)) {
975 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
976 return PTR_ERR(mvuart->clk);
978 if (IS_EXTENDED(port)) {
979 dev_err(&pdev->dev, "unable to get UART clock\n");
980 return PTR_ERR(mvuart->clk);
983 if (!clk_prepare_enable(mvuart->clk))
984 port->uartclk = clk_get_rate(mvuart->clk);
987 /* Manage interrupts */
988 if (platform_irq_count(pdev) == 1) {
989 /* Old bindings: no name on the single unamed UART0 IRQ */
990 irq = platform_get_irq(pdev, 0);
994 mvuart->irq[UART_IRQ_SUM] = irq;
997 * New bindings: named interrupts (RX, TX) for both UARTS,
998 * only make use of uart-rx and uart-tx interrupts, do not use
999 * uart-sum of UART0 port.
1001 irq = platform_get_irq_byname(pdev, "uart-rx");
1005 mvuart->irq[UART_RX_IRQ] = irq;
1007 irq = platform_get_irq_byname(pdev, "uart-tx");
1011 mvuart->irq[UART_TX_IRQ] = irq;
1014 /* UART Soft Reset*/
1015 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
1017 writel(0, port->membase + UART_CTRL(port));
1019 return uart_add_one_port(&mvebu_uart_driver, port);
1022 static struct mvebu_uart_driver_data uart_std_driver_data = {
1024 .regs.rbr = UART_STD_RBR,
1025 .regs.tsh = UART_STD_TSH,
1026 .regs.ctrl = UART_STD_CTRL1,
1027 .regs.intr = UART_STD_CTRL2,
1028 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
1029 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
1030 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
1031 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
1034 static struct mvebu_uart_driver_data uart_ext_driver_data = {
1036 .regs.rbr = UART_EXT_RBR,
1037 .regs.tsh = UART_EXT_TSH,
1038 .regs.ctrl = UART_EXT_CTRL1,
1039 .regs.intr = UART_EXT_CTRL2,
1040 .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
1041 .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
1042 .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
1043 .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
1046 /* Match table for of_platform binding */
1047 static const struct of_device_id mvebu_uart_of_match[] = {
1049 .compatible = "marvell,armada-3700-uart",
1050 .data = (void *)&uart_std_driver_data,
1053 .compatible = "marvell,armada-3700-uart-ext",
1054 .data = (void *)&uart_ext_driver_data,
1059 static struct platform_driver mvebu_uart_platform_driver = {
1060 .probe = mvebu_uart_probe,
1062 .name = "mvebu-uart",
1063 .of_match_table = of_match_ptr(mvebu_uart_of_match),
1064 .suppress_bind_attrs = true,
1065 #if defined(CONFIG_PM)
1066 .pm = &mvebu_uart_pm_ops,
1067 #endif /* CONFIG_PM */
1071 /* This code is based on clk-fixed-factor.c driver and modified. */
1073 struct mvebu_uart_clock {
1074 struct clk_hw clk_hw;
1076 u32 pm_context_reg1;
1077 u32 pm_context_reg2;
1080 struct mvebu_uart_clock_base {
1081 struct mvebu_uart_clock clocks[2];
1082 unsigned int parent_rates[5];
1090 #define PARENT_CLOCK_XTAL 4
1092 #define to_uart_clock(hw) container_of(hw, struct mvebu_uart_clock, clk_hw)
1093 #define to_uart_clock_base(uart_clock) container_of(uart_clock, \
1094 struct mvebu_uart_clock_base, clocks[uart_clock->clock_idx])
1096 static int mvebu_uart_clock_prepare(struct clk_hw *hw)
1098 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1099 struct mvebu_uart_clock_base *uart_clock_base =
1100 to_uart_clock_base(uart_clock);
1101 unsigned int prev_clock_idx, prev_clock_rate, prev_d1d2;
1102 unsigned int parent_clock_idx, parent_clock_rate;
1103 unsigned long flags;
1104 unsigned int d1, d2;
1109 * This function just reconfigures UART Clock Control register (located
1110 * in UART1 address space which controls both UART1 and UART2) to
1111 * selected UART base clock and recalculates current UART1/UART2
1112 * divisors in their address spaces, so that final baudrate will not be
1113 * changed by switching UART parent clock. This is required for
1114 * otherwise kernel's boot log stops working - we need to ensure that
1115 * UART baudrate does not change during this setup. It is a one time
1116 * operation, it will execute only once and set `configured` to true,
1117 * and be skipped on subsequent calls. Because this UART Clock Control
1118 * register (UART_BRDV) is shared between UART1 baudrate function,
1119 * UART1 clock selector and UART2 clock selector, every access to
1120 * UART_BRDV (reg1) needs to be protected by a lock.
1123 spin_lock_irqsave(&mvebu_uart_lock, flags);
1125 if (uart_clock_base->configured) {
1126 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
1130 parent_clock_idx = uart_clock_base->parent_idx;
1131 parent_clock_rate = uart_clock_base->parent_rates[parent_clock_idx];
1133 val = readl(uart_clock_base->reg1);
1135 if (uart_clock_base->div > CLK_TBG_DIV1_MAX) {
1136 d1 = CLK_TBG_DIV1_MAX;
1137 d2 = uart_clock_base->div / CLK_TBG_DIV1_MAX;
1139 d1 = uart_clock_base->div;
1143 if (val & CLK_NO_XTAL) {
1144 prev_clock_idx = (val >> CLK_TBG_SEL_SHIFT) & CLK_TBG_SEL_MASK;
1145 prev_d1d2 = ((val >> CLK_TBG_DIV1_SHIFT) & CLK_TBG_DIV1_MASK) *
1146 ((val >> CLK_TBG_DIV2_SHIFT) & CLK_TBG_DIV2_MASK);
1148 prev_clock_idx = PARENT_CLOCK_XTAL;
1152 /* Note that uart_clock_base->parent_rates[i] may not be available */
1153 prev_clock_rate = uart_clock_base->parent_rates[prev_clock_idx];
1155 /* Recalculate UART1 divisor so UART1 baudrate does not change */
1156 if (prev_clock_rate) {
1157 divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) *
1158 parent_clock_rate * prev_d1d2,
1159 prev_clock_rate * d1 * d2);
1162 else if (divisor > BRDV_BAUD_MAX)
1163 divisor = BRDV_BAUD_MAX;
1164 val = (val & ~BRDV_BAUD_MASK) | divisor;
1167 if (parent_clock_idx != PARENT_CLOCK_XTAL) {
1168 /* Do not use XTAL, select TBG clock and TBG d1 * d2 divisors */
1170 val &= ~(CLK_TBG_DIV1_MASK << CLK_TBG_DIV1_SHIFT);
1171 val |= d1 << CLK_TBG_DIV1_SHIFT;
1172 val &= ~(CLK_TBG_DIV2_MASK << CLK_TBG_DIV2_SHIFT);
1173 val |= d2 << CLK_TBG_DIV2_SHIFT;
1174 val &= ~(CLK_TBG_SEL_MASK << CLK_TBG_SEL_SHIFT);
1175 val |= parent_clock_idx << CLK_TBG_SEL_SHIFT;
1177 /* Use XTAL, TBG bits are then ignored */
1178 val &= ~CLK_NO_XTAL;
1181 writel(val, uart_clock_base->reg1);
1183 /* Recalculate UART2 divisor so UART2 baudrate does not change */
1184 if (prev_clock_rate) {
1185 val = readl(uart_clock_base->reg2);
1186 divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) *
1187 parent_clock_rate * prev_d1d2,
1188 prev_clock_rate * d1 * d2);
1191 else if (divisor > BRDV_BAUD_MAX)
1192 divisor = BRDV_BAUD_MAX;
1193 val = (val & ~BRDV_BAUD_MASK) | divisor;
1194 writel(val, uart_clock_base->reg2);
1197 uart_clock_base->configured = true;
1199 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
1204 static int mvebu_uart_clock_enable(struct clk_hw *hw)
1206 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1207 struct mvebu_uart_clock_base *uart_clock_base =
1208 to_uart_clock_base(uart_clock);
1209 unsigned long flags;
1212 spin_lock_irqsave(&mvebu_uart_lock, flags);
1214 val = readl(uart_clock_base->reg1);
1216 if (uart_clock->clock_idx == 0)
1217 val &= ~UART1_CLK_DIS;
1219 val &= ~UART2_CLK_DIS;
1221 writel(val, uart_clock_base->reg1);
1223 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
1228 static void mvebu_uart_clock_disable(struct clk_hw *hw)
1230 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1231 struct mvebu_uart_clock_base *uart_clock_base =
1232 to_uart_clock_base(uart_clock);
1233 unsigned long flags;
1236 spin_lock_irqsave(&mvebu_uart_lock, flags);
1238 val = readl(uart_clock_base->reg1);
1240 if (uart_clock->clock_idx == 0)
1241 val |= UART1_CLK_DIS;
1243 val |= UART2_CLK_DIS;
1245 writel(val, uart_clock_base->reg1);
1247 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
1250 static int mvebu_uart_clock_is_enabled(struct clk_hw *hw)
1252 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1253 struct mvebu_uart_clock_base *uart_clock_base =
1254 to_uart_clock_base(uart_clock);
1257 val = readl(uart_clock_base->reg1);
1259 if (uart_clock->clock_idx == 0)
1260 return !(val & UART1_CLK_DIS);
1262 return !(val & UART2_CLK_DIS);
1265 static int mvebu_uart_clock_save_context(struct clk_hw *hw)
1267 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1268 struct mvebu_uart_clock_base *uart_clock_base =
1269 to_uart_clock_base(uart_clock);
1270 unsigned long flags;
1272 spin_lock_irqsave(&mvebu_uart_lock, flags);
1273 uart_clock->pm_context_reg1 = readl(uart_clock_base->reg1);
1274 uart_clock->pm_context_reg2 = readl(uart_clock_base->reg2);
1275 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
1280 static void mvebu_uart_clock_restore_context(struct clk_hw *hw)
1282 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1283 struct mvebu_uart_clock_base *uart_clock_base =
1284 to_uart_clock_base(uart_clock);
1285 unsigned long flags;
1287 spin_lock_irqsave(&mvebu_uart_lock, flags);
1288 writel(uart_clock->pm_context_reg1, uart_clock_base->reg1);
1289 writel(uart_clock->pm_context_reg2, uart_clock_base->reg2);
1290 spin_unlock_irqrestore(&mvebu_uart_lock, flags);
1293 static unsigned long mvebu_uart_clock_recalc_rate(struct clk_hw *hw,
1294 unsigned long parent_rate)
1296 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1297 struct mvebu_uart_clock_base *uart_clock_base =
1298 to_uart_clock_base(uart_clock);
1300 return parent_rate / uart_clock_base->div;
1303 static long mvebu_uart_clock_round_rate(struct clk_hw *hw, unsigned long rate,
1304 unsigned long *parent_rate)
1306 struct mvebu_uart_clock *uart_clock = to_uart_clock(hw);
1307 struct mvebu_uart_clock_base *uart_clock_base =
1308 to_uart_clock_base(uart_clock);
1310 return *parent_rate / uart_clock_base->div;
1313 static int mvebu_uart_clock_set_rate(struct clk_hw *hw, unsigned long rate,
1314 unsigned long parent_rate)
1317 * We must report success but we can do so unconditionally because
1318 * mvebu_uart_clock_round_rate returns values that ensure this call is a
1325 static const struct clk_ops mvebu_uart_clock_ops = {
1326 .prepare = mvebu_uart_clock_prepare,
1327 .enable = mvebu_uart_clock_enable,
1328 .disable = mvebu_uart_clock_disable,
1329 .is_enabled = mvebu_uart_clock_is_enabled,
1330 .save_context = mvebu_uart_clock_save_context,
1331 .restore_context = mvebu_uart_clock_restore_context,
1332 .round_rate = mvebu_uart_clock_round_rate,
1333 .set_rate = mvebu_uart_clock_set_rate,
1334 .recalc_rate = mvebu_uart_clock_recalc_rate,
1337 static int mvebu_uart_clock_register(struct device *dev,
1338 struct mvebu_uart_clock *uart_clock,
1340 const char *parent_name)
1342 struct clk_init_data init = { };
1344 uart_clock->clk_hw.init = &init;
1347 init.ops = &mvebu_uart_clock_ops;
1349 init.num_parents = 1;
1350 init.parent_names = &parent_name;
1352 return devm_clk_hw_register(dev, &uart_clock->clk_hw);
1355 static int mvebu_uart_clock_probe(struct platform_device *pdev)
1357 static const char *const uart_clk_names[] = { "uart_1", "uart_2" };
1358 static const char *const parent_clk_names[] = { "TBG-A-P", "TBG-B-P",
1359 "TBG-A-S", "TBG-B-S",
1361 struct clk *parent_clks[ARRAY_SIZE(parent_clk_names)];
1362 struct mvebu_uart_clock_base *uart_clock_base;
1363 struct clk_hw_onecell_data *hw_clk_data;
1364 struct device *dev = &pdev->dev;
1365 int i, parent_clk_idx, ret;
1366 unsigned long div, rate;
1367 struct resource *res;
1368 unsigned int d1, d2;
1370 BUILD_BUG_ON(ARRAY_SIZE(uart_clk_names) !=
1371 ARRAY_SIZE(uart_clock_base->clocks));
1372 BUILD_BUG_ON(ARRAY_SIZE(parent_clk_names) !=
1373 ARRAY_SIZE(uart_clock_base->parent_rates));
1375 uart_clock_base = devm_kzalloc(dev,
1376 sizeof(*uart_clock_base),
1378 if (!uart_clock_base)
1381 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1383 dev_err(dev, "Couldn't get first register\n");
1388 * UART Clock Control register (reg1 / UART_BRDV) is in the address
1389 * space of UART1 (standard UART variant), controls parent clock and
1390 * dividers for both UART1 and UART2 and is supplied via DT as the first
1391 * resource. Therefore use ioremap() rather than ioremap_resource() to
1392 * avoid conflicts with UART1 driver. Access to UART_BRDV is protected
1393 * by a lock shared between clock and UART driver.
1395 uart_clock_base->reg1 = devm_ioremap(dev, res->start,
1396 resource_size(res));
1397 if (!uart_clock_base->reg1)
1400 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1402 dev_err(dev, "Couldn't get second register\n");
1407 * UART 2 Baud Rate Divisor register (reg2 / UART_BRDV) is in address
1408 * space of UART2 (extended UART variant), controls only one UART2
1409 * specific divider and is supplied via DT as second resource.
1410 * Therefore use ioremap() rather than ioremap_resource() to avoid
1411 * conflicts with UART2 driver. Access to UART_BRDV is protected by a
1412 * by lock shared between clock and UART driver.
1414 uart_clock_base->reg2 = devm_ioremap(dev, res->start,
1415 resource_size(res));
1416 if (!uart_clock_base->reg2)
1419 hw_clk_data = devm_kzalloc(dev,
1420 struct_size(hw_clk_data, hws,
1421 ARRAY_SIZE(uart_clk_names)),
1426 hw_clk_data->num = ARRAY_SIZE(uart_clk_names);
1427 for (i = 0; i < ARRAY_SIZE(uart_clk_names); i++) {
1428 hw_clk_data->hws[i] = &uart_clock_base->clocks[i].clk_hw;
1429 uart_clock_base->clocks[i].clock_idx = i;
1432 parent_clk_idx = -1;
1434 for (i = 0; i < ARRAY_SIZE(parent_clk_names); i++) {
1435 parent_clks[i] = devm_clk_get(dev, parent_clk_names[i]);
1436 if (IS_ERR(parent_clks[i])) {
1437 if (PTR_ERR(parent_clks[i]) == -EPROBE_DEFER)
1438 return -EPROBE_DEFER;
1439 dev_warn(dev, "Couldn't get the parent clock %s: %ld\n",
1440 parent_clk_names[i], PTR_ERR(parent_clks[i]));
1444 ret = clk_prepare_enable(parent_clks[i]);
1446 dev_warn(dev, "Couldn't enable parent clock %s: %d\n",
1447 parent_clk_names[i], ret);
1450 rate = clk_get_rate(parent_clks[i]);
1451 uart_clock_base->parent_rates[i] = rate;
1453 if (i != PARENT_CLOCK_XTAL) {
1455 * Calculate the smallest TBG d1 and d2 divisors that
1456 * still can provide 9600 baudrate.
1458 d1 = DIV_ROUND_UP(rate, 9600 * OSAMP_MAX_DIVISOR *
1462 else if (d1 > CLK_TBG_DIV1_MAX)
1463 d1 = CLK_TBG_DIV1_MAX;
1465 d2 = DIV_ROUND_UP(rate, 9600 * OSAMP_MAX_DIVISOR *
1466 BRDV_BAUD_MAX * d1);
1469 else if (d2 > CLK_TBG_DIV2_MAX)
1470 d2 = CLK_TBG_DIV2_MAX;
1473 * When UART clock uses XTAL clock as a source then it
1474 * is not possible to use d1 and d2 divisors.
1479 /* Skip clock source which cannot provide 9600 baudrate */
1480 if (rate > 9600 * OSAMP_MAX_DIVISOR * BRDV_BAUD_MAX * d1 * d2)
1484 * Choose TBG clock source with the smallest divisors. Use XTAL
1485 * clock source only in case TBG is not available as XTAL cannot
1486 * be used for baudrates higher than 230400.
1488 if (parent_clk_idx == -1 ||
1489 (i != PARENT_CLOCK_XTAL && div > d1 * d2)) {
1495 for (i = 0; i < ARRAY_SIZE(parent_clk_names); i++) {
1496 if (i == parent_clk_idx || IS_ERR(parent_clks[i]))
1498 clk_disable_unprepare(parent_clks[i]);
1499 devm_clk_put(dev, parent_clks[i]);
1502 if (parent_clk_idx == -1) {
1503 dev_err(dev, "No usable parent clock\n");
1507 uart_clock_base->parent_idx = parent_clk_idx;
1508 uart_clock_base->div = div;
1510 dev_notice(dev, "Using parent clock %s as base UART clock\n",
1511 __clk_get_name(parent_clks[parent_clk_idx]));
1513 for (i = 0; i < ARRAY_SIZE(uart_clk_names); i++) {
1514 ret = mvebu_uart_clock_register(dev,
1515 &uart_clock_base->clocks[i],
1517 __clk_get_name(parent_clks[parent_clk_idx]));
1519 dev_err(dev, "Can't register UART clock %d: %d\n",
1525 return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
1529 static const struct of_device_id mvebu_uart_clock_of_match[] = {
1530 { .compatible = "marvell,armada-3700-uart-clock", },
1534 static struct platform_driver mvebu_uart_clock_platform_driver = {
1535 .probe = mvebu_uart_clock_probe,
1537 .name = "mvebu-uart-clock",
1538 .of_match_table = mvebu_uart_clock_of_match,
1542 static int __init mvebu_uart_init(void)
1546 ret = uart_register_driver(&mvebu_uart_driver);
1550 ret = platform_driver_register(&mvebu_uart_clock_platform_driver);
1552 uart_unregister_driver(&mvebu_uart_driver);
1556 ret = platform_driver_register(&mvebu_uart_platform_driver);
1558 platform_driver_unregister(&mvebu_uart_clock_platform_driver);
1559 uart_unregister_driver(&mvebu_uart_driver);
1565 arch_initcall(mvebu_uart_init);