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
76 #define MVEBU_NR_UARTS 2
78 #define MVEBU_UART_TYPE "mvebu-uart"
79 #define DRIVER_NAME "mvebu_serial"
82 /* Either there is only one summed IRQ... */
84 /* ...or there are two separate IRQ for RX and TX */
90 /* Diverging register offsets */
91 struct uart_regs_layout
{
100 unsigned int ctrl_tx_rdy_int
;
101 unsigned int ctrl_rx_rdy_int
;
102 unsigned int stat_tx_rdy
;
103 unsigned int stat_rx_rdy
;
106 /* Driver data, a structure for each UART port */
107 struct mvebu_uart_driver_data
{
109 struct uart_regs_layout regs
;
110 struct uart_flags flags
;
113 /* Saved registers during suspend */
114 struct mvebu_uart_pm_regs
{
124 /* MVEBU UART driver structure */
126 struct uart_port
*port
;
128 int irq
[UART_IRQ_COUNT
];
129 unsigned char __iomem
*nb
;
130 struct mvebu_uart_driver_data
*data
;
131 #if defined(CONFIG_PM)
132 struct mvebu_uart_pm_regs pm_regs
;
133 #endif /* CONFIG_PM */
136 static struct mvebu_uart
*to_mvuart(struct uart_port
*port
)
138 return (struct mvebu_uart
*)port
->private_data
;
141 #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
143 #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
144 #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
145 #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
146 #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
148 #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
149 #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
150 #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
151 #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
153 static struct uart_port mvebu_uart_ports
[MVEBU_NR_UARTS
];
155 /* Core UART Driver Operations */
156 static unsigned int mvebu_uart_tx_empty(struct uart_port
*port
)
161 spin_lock_irqsave(&port
->lock
, flags
);
162 st
= readl(port
->membase
+ UART_STAT
);
163 spin_unlock_irqrestore(&port
->lock
, flags
);
165 return (st
& STAT_TX_FIFO_EMP
) ? TIOCSER_TEMT
: 0;
168 static unsigned int mvebu_uart_get_mctrl(struct uart_port
*port
)
170 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
173 static void mvebu_uart_set_mctrl(struct uart_port
*port
,
177 * Even if we do not support configuring the modem control lines, this
178 * function must be proided to the serial core
182 static void mvebu_uart_stop_tx(struct uart_port
*port
)
184 unsigned int ctl
= readl(port
->membase
+ UART_INTR(port
));
186 ctl
&= ~CTRL_TX_RDY_INT(port
);
187 writel(ctl
, port
->membase
+ UART_INTR(port
));
190 static void mvebu_uart_start_tx(struct uart_port
*port
)
193 struct circ_buf
*xmit
= &port
->state
->xmit
;
195 if (IS_EXTENDED(port
) && !uart_circ_empty(xmit
)) {
196 writel(xmit
->buf
[xmit
->tail
], port
->membase
+ UART_TSH(port
));
197 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
201 ctl
= readl(port
->membase
+ UART_INTR(port
));
202 ctl
|= CTRL_TX_RDY_INT(port
);
203 writel(ctl
, port
->membase
+ UART_INTR(port
));
206 static void mvebu_uart_stop_rx(struct uart_port
*port
)
210 ctl
= readl(port
->membase
+ UART_CTRL(port
));
211 ctl
&= ~CTRL_BRK_INT
;
212 writel(ctl
, port
->membase
+ UART_CTRL(port
));
214 ctl
= readl(port
->membase
+ UART_INTR(port
));
215 ctl
&= ~CTRL_RX_RDY_INT(port
);
216 writel(ctl
, port
->membase
+ UART_INTR(port
));
219 static void mvebu_uart_break_ctl(struct uart_port
*port
, int brk
)
224 spin_lock_irqsave(&port
->lock
, flags
);
225 ctl
= readl(port
->membase
+ UART_CTRL(port
));
227 ctl
|= CTRL_SND_BRK_SEQ
;
229 ctl
&= ~CTRL_SND_BRK_SEQ
;
230 writel(ctl
, port
->membase
+ UART_CTRL(port
));
231 spin_unlock_irqrestore(&port
->lock
, flags
);
234 static void mvebu_uart_rx_chars(struct uart_port
*port
, unsigned int status
)
236 struct tty_port
*tport
= &port
->state
->port
;
237 unsigned char ch
= 0;
241 if (status
& STAT_RX_RDY(port
)) {
242 ch
= readl(port
->membase
+ UART_RBR(port
));
247 if (status
& STAT_PAR_ERR
)
248 port
->icount
.parity
++;
251 if (status
& STAT_BRK_DET
) {
253 status
&= ~(STAT_FRM_ERR
| STAT_PAR_ERR
);
254 if (uart_handle_break(port
))
258 if (status
& STAT_OVR_ERR
)
259 port
->icount
.overrun
++;
261 if (status
& STAT_FRM_ERR
)
262 port
->icount
.frame
++;
264 if (uart_handle_sysrq_char(port
, ch
))
267 if (status
& port
->ignore_status_mask
& STAT_PAR_ERR
)
268 status
&= ~STAT_RX_RDY(port
);
270 status
&= port
->read_status_mask
;
272 if (status
& STAT_PAR_ERR
)
275 status
&= ~port
->ignore_status_mask
;
277 if (status
& STAT_RX_RDY(port
))
278 tty_insert_flip_char(tport
, ch
, flag
);
280 if (status
& STAT_BRK_DET
)
281 tty_insert_flip_char(tport
, 0, TTY_BREAK
);
283 if (status
& STAT_FRM_ERR
)
284 tty_insert_flip_char(tport
, 0, TTY_FRAME
);
286 if (status
& STAT_OVR_ERR
)
287 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
290 status
= readl(port
->membase
+ UART_STAT
);
291 } while (status
& (STAT_RX_RDY(port
) | STAT_BRK_DET
));
293 tty_flip_buffer_push(tport
);
296 static void mvebu_uart_tx_chars(struct uart_port
*port
, unsigned int status
)
298 struct circ_buf
*xmit
= &port
->state
->xmit
;
303 writel(port
->x_char
, port
->membase
+ UART_TSH(port
));
309 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
310 mvebu_uart_stop_tx(port
);
314 for (count
= 0; count
< port
->fifosize
; count
++) {
315 writel(xmit
->buf
[xmit
->tail
], port
->membase
+ UART_TSH(port
));
316 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
319 if (uart_circ_empty(xmit
))
322 st
= readl(port
->membase
+ UART_STAT
);
323 if (st
& STAT_TX_FIFO_FUL
)
327 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
328 uart_write_wakeup(port
);
330 if (uart_circ_empty(xmit
))
331 mvebu_uart_stop_tx(port
);
334 static irqreturn_t
mvebu_uart_isr(int irq
, void *dev_id
)
336 struct uart_port
*port
= (struct uart_port
*)dev_id
;
337 unsigned int st
= readl(port
->membase
+ UART_STAT
);
339 if (st
& (STAT_RX_RDY(port
) | STAT_OVR_ERR
| STAT_FRM_ERR
|
341 mvebu_uart_rx_chars(port
, st
);
343 if (st
& STAT_TX_RDY(port
))
344 mvebu_uart_tx_chars(port
, st
);
349 static irqreturn_t
mvebu_uart_rx_isr(int irq
, void *dev_id
)
351 struct uart_port
*port
= (struct uart_port
*)dev_id
;
352 unsigned int st
= readl(port
->membase
+ UART_STAT
);
354 if (st
& (STAT_RX_RDY(port
) | STAT_OVR_ERR
| STAT_FRM_ERR
|
356 mvebu_uart_rx_chars(port
, st
);
361 static irqreturn_t
mvebu_uart_tx_isr(int irq
, void *dev_id
)
363 struct uart_port
*port
= (struct uart_port
*)dev_id
;
364 unsigned int st
= readl(port
->membase
+ UART_STAT
);
366 if (st
& STAT_TX_RDY(port
))
367 mvebu_uart_tx_chars(port
, st
);
372 static int mvebu_uart_startup(struct uart_port
*port
)
374 struct mvebu_uart
*mvuart
= to_mvuart(port
);
378 writel(CTRL_TXFIFO_RST
| CTRL_RXFIFO_RST
,
379 port
->membase
+ UART_CTRL(port
));
382 /* Clear the error bits of state register before IRQ request */
383 ret
= readl(port
->membase
+ UART_STAT
);
385 writel(ret
, port
->membase
+ UART_STAT
);
387 writel(CTRL_BRK_INT
, port
->membase
+ UART_CTRL(port
));
389 ctl
= readl(port
->membase
+ UART_INTR(port
));
390 ctl
|= CTRL_RX_RDY_INT(port
);
391 writel(ctl
, port
->membase
+ UART_INTR(port
));
393 if (!mvuart
->irq
[UART_TX_IRQ
]) {
394 /* Old bindings with just one interrupt (UART0 only) */
395 ret
= devm_request_irq(port
->dev
, mvuart
->irq
[UART_IRQ_SUM
],
396 mvebu_uart_isr
, port
->irqflags
,
397 dev_name(port
->dev
), port
);
399 dev_err(port
->dev
, "unable to request IRQ %d\n",
400 mvuart
->irq
[UART_IRQ_SUM
]);
404 /* New bindings with an IRQ for RX and TX (both UART) */
405 ret
= devm_request_irq(port
->dev
, mvuart
->irq
[UART_RX_IRQ
],
406 mvebu_uart_rx_isr
, port
->irqflags
,
407 dev_name(port
->dev
), port
);
409 dev_err(port
->dev
, "unable to request IRQ %d\n",
410 mvuart
->irq
[UART_RX_IRQ
]);
414 ret
= devm_request_irq(port
->dev
, mvuart
->irq
[UART_TX_IRQ
],
415 mvebu_uart_tx_isr
, port
->irqflags
,
419 dev_err(port
->dev
, "unable to request IRQ %d\n",
420 mvuart
->irq
[UART_TX_IRQ
]);
421 devm_free_irq(port
->dev
, mvuart
->irq
[UART_RX_IRQ
],
430 static void mvebu_uart_shutdown(struct uart_port
*port
)
432 struct mvebu_uart
*mvuart
= to_mvuart(port
);
434 writel(0, port
->membase
+ UART_INTR(port
));
436 if (!mvuart
->irq
[UART_TX_IRQ
]) {
437 devm_free_irq(port
->dev
, mvuart
->irq
[UART_IRQ_SUM
], port
);
439 devm_free_irq(port
->dev
, mvuart
->irq
[UART_RX_IRQ
], port
);
440 devm_free_irq(port
->dev
, mvuart
->irq
[UART_TX_IRQ
], port
);
444 static int mvebu_uart_baud_rate_set(struct uart_port
*port
, unsigned int baud
)
446 struct mvebu_uart
*mvuart
= to_mvuart(port
);
447 unsigned int baud_rate_div
;
450 if (IS_ERR(mvuart
->clk
))
451 return -PTR_ERR(mvuart
->clk
);
454 * The UART clock is divided by the value of the divisor to generate
455 * UCLK_OUT clock, which is 16 times faster than the baudrate.
456 * This prescaler can achieve all standard baudrates until 230400.
457 * Higher baudrates could be achieved for the extended UART by using the
458 * programmable oversampling stack (also called fractional divisor).
460 baud_rate_div
= DIV_ROUND_UP(port
->uartclk
, baud
* 16);
461 brdv
= readl(port
->membase
+ UART_BRDV
);
462 brdv
&= ~BRDV_BAUD_MASK
;
463 brdv
|= baud_rate_div
;
464 writel(brdv
, port
->membase
+ UART_BRDV
);
469 static void mvebu_uart_set_termios(struct uart_port
*port
,
470 struct ktermios
*termios
,
471 struct ktermios
*old
)
476 spin_lock_irqsave(&port
->lock
, flags
);
478 port
->read_status_mask
= STAT_RX_RDY(port
) | STAT_OVR_ERR
|
479 STAT_TX_RDY(port
) | STAT_TX_FIFO_FUL
;
481 if (termios
->c_iflag
& INPCK
)
482 port
->read_status_mask
|= STAT_FRM_ERR
| STAT_PAR_ERR
;
484 port
->ignore_status_mask
= 0;
485 if (termios
->c_iflag
& IGNPAR
)
486 port
->ignore_status_mask
|=
487 STAT_FRM_ERR
| STAT_PAR_ERR
| STAT_OVR_ERR
;
489 if ((termios
->c_cflag
& CREAD
) == 0)
490 port
->ignore_status_mask
|= STAT_RX_RDY(port
) | STAT_BRK_ERR
;
493 * Maximum achievable frequency with simple baudrate divisor is 230400.
494 * Since the error per bit frame would be of more than 15%, achieving
495 * higher frequencies would require to implement the fractional divisor
498 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
499 if (mvebu_uart_baud_rate_set(port
, baud
)) {
500 /* No clock available, baudrate cannot be changed */
502 baud
= uart_get_baud_rate(port
, old
, NULL
, 0, 230400);
504 tty_termios_encode_baud_rate(termios
, baud
, baud
);
505 uart_update_timeout(port
, termios
->c_cflag
, baud
);
508 /* Only the following flag changes are supported */
510 termios
->c_iflag
&= INPCK
| IGNPAR
;
511 termios
->c_iflag
|= old
->c_iflag
& ~(INPCK
| IGNPAR
);
512 termios
->c_cflag
&= CREAD
| CBAUD
;
513 termios
->c_cflag
|= old
->c_cflag
& ~(CREAD
| CBAUD
);
516 spin_unlock_irqrestore(&port
->lock
, flags
);
519 static const char *mvebu_uart_type(struct uart_port
*port
)
521 return MVEBU_UART_TYPE
;
524 static void mvebu_uart_release_port(struct uart_port
*port
)
526 /* Nothing to do here */
529 static int mvebu_uart_request_port(struct uart_port
*port
)
534 #ifdef CONFIG_CONSOLE_POLL
535 static int mvebu_uart_get_poll_char(struct uart_port
*port
)
537 unsigned int st
= readl(port
->membase
+ UART_STAT
);
539 if (!(st
& STAT_RX_RDY(port
)))
542 return readl(port
->membase
+ UART_RBR(port
));
545 static void mvebu_uart_put_poll_char(struct uart_port
*port
, unsigned char c
)
550 st
= readl(port
->membase
+ UART_STAT
);
552 if (!(st
& STAT_TX_FIFO_FUL
))
558 writel(c
, port
->membase
+ UART_TSH(port
));
562 static const struct uart_ops mvebu_uart_ops
= {
563 .tx_empty
= mvebu_uart_tx_empty
,
564 .set_mctrl
= mvebu_uart_set_mctrl
,
565 .get_mctrl
= mvebu_uart_get_mctrl
,
566 .stop_tx
= mvebu_uart_stop_tx
,
567 .start_tx
= mvebu_uart_start_tx
,
568 .stop_rx
= mvebu_uart_stop_rx
,
569 .break_ctl
= mvebu_uart_break_ctl
,
570 .startup
= mvebu_uart_startup
,
571 .shutdown
= mvebu_uart_shutdown
,
572 .set_termios
= mvebu_uart_set_termios
,
573 .type
= mvebu_uart_type
,
574 .release_port
= mvebu_uart_release_port
,
575 .request_port
= mvebu_uart_request_port
,
576 #ifdef CONFIG_CONSOLE_POLL
577 .poll_get_char
= mvebu_uart_get_poll_char
,
578 .poll_put_char
= mvebu_uart_put_poll_char
,
582 /* Console Driver Operations */
584 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
586 static void mvebu_uart_putc(struct uart_port
*port
, int c
)
591 st
= readl(port
->membase
+ UART_STAT
);
592 if (!(st
& STAT_TX_FIFO_FUL
))
596 /* At early stage, DT is not parsed yet, only use UART0 */
597 writel(c
, port
->membase
+ UART_STD_TSH
);
600 st
= readl(port
->membase
+ UART_STAT
);
601 if (st
& STAT_TX_FIFO_EMP
)
606 static void mvebu_uart_putc_early_write(struct console
*con
,
610 struct earlycon_device
*dev
= con
->data
;
612 uart_console_write(&dev
->port
, s
, n
, mvebu_uart_putc
);
616 mvebu_uart_early_console_setup(struct earlycon_device
*device
,
619 if (!device
->port
.membase
)
622 device
->con
->write
= mvebu_uart_putc_early_write
;
627 EARLYCON_DECLARE(ar3700_uart
, mvebu_uart_early_console_setup
);
628 OF_EARLYCON_DECLARE(ar3700_uart
, "marvell,armada-3700-uart",
629 mvebu_uart_early_console_setup
);
631 static void wait_for_xmitr(struct uart_port
*port
)
635 readl_poll_timeout_atomic(port
->membase
+ UART_STAT
, val
,
636 (val
& STAT_TX_RDY(port
)), 1, 10000);
639 static void mvebu_uart_console_putchar(struct uart_port
*port
, int ch
)
641 wait_for_xmitr(port
);
642 writel(ch
, port
->membase
+ UART_TSH(port
));
645 static void mvebu_uart_console_write(struct console
*co
, const char *s
,
648 struct uart_port
*port
= &mvebu_uart_ports
[co
->index
];
650 unsigned int ier
, intr
, ctl
;
653 if (oops_in_progress
)
654 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
656 spin_lock_irqsave(&port
->lock
, flags
);
658 ier
= readl(port
->membase
+ UART_CTRL(port
)) & CTRL_BRK_INT
;
659 intr
= readl(port
->membase
+ UART_INTR(port
)) &
660 (CTRL_RX_RDY_INT(port
) | CTRL_TX_RDY_INT(port
));
661 writel(0, port
->membase
+ UART_CTRL(port
));
662 writel(0, port
->membase
+ UART_INTR(port
));
664 uart_console_write(port
, s
, count
, mvebu_uart_console_putchar
);
666 wait_for_xmitr(port
);
669 writel(ier
, port
->membase
+ UART_CTRL(port
));
672 ctl
= intr
| readl(port
->membase
+ UART_INTR(port
));
673 writel(ctl
, port
->membase
+ UART_INTR(port
));
677 spin_unlock_irqrestore(&port
->lock
, flags
);
680 static int mvebu_uart_console_setup(struct console
*co
, char *options
)
682 struct uart_port
*port
;
688 if (co
->index
< 0 || co
->index
>= MVEBU_NR_UARTS
)
691 port
= &mvebu_uart_ports
[co
->index
];
693 if (!port
->mapbase
|| !port
->membase
) {
694 pr_debug("console on ttyMV%i not present\n", co
->index
);
699 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
701 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
704 static struct uart_driver mvebu_uart_driver
;
706 static struct console mvebu_uart_console
= {
708 .write
= mvebu_uart_console_write
,
709 .device
= uart_console_device
,
710 .setup
= mvebu_uart_console_setup
,
711 .flags
= CON_PRINTBUFFER
,
713 .data
= &mvebu_uart_driver
,
716 static int __init
mvebu_uart_console_init(void)
718 register_console(&mvebu_uart_console
);
722 console_initcall(mvebu_uart_console_init
);
725 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
727 static struct uart_driver mvebu_uart_driver
= {
728 .owner
= THIS_MODULE
,
729 .driver_name
= DRIVER_NAME
,
731 .nr
= MVEBU_NR_UARTS
,
732 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
733 .cons
= &mvebu_uart_console
,
737 #if defined(CONFIG_PM)
738 static int mvebu_uart_suspend(struct device
*dev
)
740 struct mvebu_uart
*mvuart
= dev_get_drvdata(dev
);
741 struct uart_port
*port
= mvuart
->port
;
743 uart_suspend_port(&mvebu_uart_driver
, port
);
745 mvuart
->pm_regs
.rbr
= readl(port
->membase
+ UART_RBR(port
));
746 mvuart
->pm_regs
.tsh
= readl(port
->membase
+ UART_TSH(port
));
747 mvuart
->pm_regs
.ctrl
= readl(port
->membase
+ UART_CTRL(port
));
748 mvuart
->pm_regs
.intr
= readl(port
->membase
+ UART_INTR(port
));
749 mvuart
->pm_regs
.stat
= readl(port
->membase
+ UART_STAT
);
750 mvuart
->pm_regs
.brdv
= readl(port
->membase
+ UART_BRDV
);
751 mvuart
->pm_regs
.osamp
= readl(port
->membase
+ UART_OSAMP
);
753 device_set_wakeup_enable(dev
, true);
758 static int mvebu_uart_resume(struct device
*dev
)
760 struct mvebu_uart
*mvuart
= dev_get_drvdata(dev
);
761 struct uart_port
*port
= mvuart
->port
;
763 writel(mvuart
->pm_regs
.rbr
, port
->membase
+ UART_RBR(port
));
764 writel(mvuart
->pm_regs
.tsh
, port
->membase
+ UART_TSH(port
));
765 writel(mvuart
->pm_regs
.ctrl
, port
->membase
+ UART_CTRL(port
));
766 writel(mvuart
->pm_regs
.intr
, port
->membase
+ UART_INTR(port
));
767 writel(mvuart
->pm_regs
.stat
, port
->membase
+ UART_STAT
);
768 writel(mvuart
->pm_regs
.brdv
, port
->membase
+ UART_BRDV
);
769 writel(mvuart
->pm_regs
.osamp
, port
->membase
+ UART_OSAMP
);
771 uart_resume_port(&mvebu_uart_driver
, port
);
776 static const struct dev_pm_ops mvebu_uart_pm_ops
= {
777 .suspend
= mvebu_uart_suspend
,
778 .resume
= mvebu_uart_resume
,
780 #endif /* CONFIG_PM */
782 static const struct of_device_id mvebu_uart_of_match
[];
784 /* Counter to keep track of each UART port id when not using CONFIG_OF */
785 static int uart_num_counter
;
787 static int mvebu_uart_probe(struct platform_device
*pdev
)
789 struct resource
*reg
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
790 const struct of_device_id
*match
= of_match_device(mvebu_uart_of_match
,
792 struct uart_port
*port
;
793 struct mvebu_uart
*mvuart
;
797 dev_err(&pdev
->dev
, "no registers defined\n");
801 /* Assume that all UART ports have a DT alias or none has */
802 id
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
803 if (!pdev
->dev
.of_node
|| id
< 0)
804 pdev
->id
= uart_num_counter
++;
808 if (pdev
->id
>= MVEBU_NR_UARTS
) {
809 dev_err(&pdev
->dev
, "cannot have more than %d UART ports\n",
814 port
= &mvebu_uart_ports
[pdev
->id
];
816 spin_lock_init(&port
->lock
);
818 port
->dev
= &pdev
->dev
;
819 port
->type
= PORT_MVEBU
;
820 port
->ops
= &mvebu_uart_ops
;
824 port
->iotype
= UPIO_MEM32
;
825 port
->flags
= UPF_FIXED_PORT
;
826 port
->line
= pdev
->id
;
829 * IRQ number is not stored in this structure because we may have two of
830 * them per port (RX and TX). Instead, use the driver UART structure
831 * array so called ->irq[].
835 port
->mapbase
= reg
->start
;
837 port
->membase
= devm_ioremap_resource(&pdev
->dev
, reg
);
838 if (IS_ERR(port
->membase
))
839 return -PTR_ERR(port
->membase
);
841 mvuart
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_uart
),
846 /* Get controller data depending on the compatible string */
847 mvuart
->data
= (struct mvebu_uart_driver_data
*)match
->data
;
850 port
->private_data
= mvuart
;
851 platform_set_drvdata(pdev
, mvuart
);
853 /* Get fixed clock frequency */
854 mvuart
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
855 if (IS_ERR(mvuart
->clk
)) {
856 if (PTR_ERR(mvuart
->clk
) == -EPROBE_DEFER
)
857 return PTR_ERR(mvuart
->clk
);
859 if (IS_EXTENDED(port
)) {
860 dev_err(&pdev
->dev
, "unable to get UART clock\n");
861 return PTR_ERR(mvuart
->clk
);
864 if (!clk_prepare_enable(mvuart
->clk
))
865 port
->uartclk
= clk_get_rate(mvuart
->clk
);
868 /* Manage interrupts */
869 if (platform_irq_count(pdev
) == 1) {
870 /* Old bindings: no name on the single unamed UART0 IRQ */
871 irq
= platform_get_irq(pdev
, 0);
873 dev_err(&pdev
->dev
, "unable to get UART IRQ\n");
877 mvuart
->irq
[UART_IRQ_SUM
] = irq
;
880 * New bindings: named interrupts (RX, TX) for both UARTS,
881 * only make use of uart-rx and uart-tx interrupts, do not use
882 * uart-sum of UART0 port.
884 irq
= platform_get_irq_byname(pdev
, "uart-rx");
886 dev_err(&pdev
->dev
, "unable to get 'uart-rx' IRQ\n");
890 mvuart
->irq
[UART_RX_IRQ
] = irq
;
892 irq
= platform_get_irq_byname(pdev
, "uart-tx");
894 dev_err(&pdev
->dev
, "unable to get 'uart-tx' IRQ\n");
898 mvuart
->irq
[UART_TX_IRQ
] = irq
;
902 writel(CTRL_SOFT_RST
, port
->membase
+ UART_CTRL(port
));
904 writel(0, port
->membase
+ UART_CTRL(port
));
906 ret
= uart_add_one_port(&mvebu_uart_driver
, port
);
912 static struct mvebu_uart_driver_data uart_std_driver_data
= {
914 .regs
.rbr
= UART_STD_RBR
,
915 .regs
.tsh
= UART_STD_TSH
,
916 .regs
.ctrl
= UART_STD_CTRL1
,
917 .regs
.intr
= UART_STD_CTRL2
,
918 .flags
.ctrl_tx_rdy_int
= CTRL_STD_TX_RDY_INT
,
919 .flags
.ctrl_rx_rdy_int
= CTRL_STD_RX_RDY_INT
,
920 .flags
.stat_tx_rdy
= STAT_STD_TX_RDY
,
921 .flags
.stat_rx_rdy
= STAT_STD_RX_RDY
,
924 static struct mvebu_uart_driver_data uart_ext_driver_data
= {
926 .regs
.rbr
= UART_EXT_RBR
,
927 .regs
.tsh
= UART_EXT_TSH
,
928 .regs
.ctrl
= UART_EXT_CTRL1
,
929 .regs
.intr
= UART_EXT_CTRL2
,
930 .flags
.ctrl_tx_rdy_int
= CTRL_EXT_TX_RDY_INT
,
931 .flags
.ctrl_rx_rdy_int
= CTRL_EXT_RX_RDY_INT
,
932 .flags
.stat_tx_rdy
= STAT_EXT_TX_RDY
,
933 .flags
.stat_rx_rdy
= STAT_EXT_RX_RDY
,
936 /* Match table for of_platform binding */
937 static const struct of_device_id mvebu_uart_of_match
[] = {
939 .compatible
= "marvell,armada-3700-uart",
940 .data
= (void *)&uart_std_driver_data
,
943 .compatible
= "marvell,armada-3700-uart-ext",
944 .data
= (void *)&uart_ext_driver_data
,
949 static struct platform_driver mvebu_uart_platform_driver
= {
950 .probe
= mvebu_uart_probe
,
952 .name
= "mvebu-uart",
953 .of_match_table
= of_match_ptr(mvebu_uart_of_match
),
954 .suppress_bind_attrs
= true,
955 #if defined(CONFIG_PM)
956 .pm
= &mvebu_uart_pm_ops
,
957 #endif /* CONFIG_PM */
961 static int __init
mvebu_uart_init(void)
965 ret
= uart_register_driver(&mvebu_uart_driver
);
969 ret
= platform_driver_register(&mvebu_uart_platform_driver
);
971 uart_unregister_driver(&mvebu_uart_driver
);
975 arch_initcall(mvebu_uart_init
);