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 | STAT_FRM_ERR\
69 | STAT_PAR_ERR | STAT_OVR_ERR)
71 #define UART_BRDV 0x10
72 #define BRDV_BAUD_MASK 0x3FF
74 #define MVEBU_NR_UARTS 2
76 #define MVEBU_UART_TYPE "mvebu-uart"
77 #define DRIVER_NAME "mvebu_serial"
80 /* Either there is only one summed IRQ... */
82 /* ...or there are two separate IRQ for RX and TX */
88 /* Diverging register offsets */
89 struct uart_regs_layout
{
98 unsigned int ctrl_tx_rdy_int
;
99 unsigned int ctrl_rx_rdy_int
;
100 unsigned int stat_tx_rdy
;
101 unsigned int stat_rx_rdy
;
104 /* Driver data, a structure for each UART port */
105 struct mvebu_uart_driver_data
{
107 struct uart_regs_layout regs
;
108 struct uart_flags flags
;
111 /* MVEBU UART driver structure */
113 struct uart_port
*port
;
115 int irq
[UART_IRQ_COUNT
];
116 unsigned char __iomem
*nb
;
117 struct mvebu_uart_driver_data
*data
;
120 static struct mvebu_uart
*to_mvuart(struct uart_port
*port
)
122 return (struct mvebu_uart
*)port
->private_data
;
125 #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
127 #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
128 #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
129 #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
130 #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
132 #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
133 #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
134 #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
135 #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
137 static struct uart_port mvebu_uart_ports
[MVEBU_NR_UARTS
];
139 /* Core UART Driver Operations */
140 static unsigned int mvebu_uart_tx_empty(struct uart_port
*port
)
145 spin_lock_irqsave(&port
->lock
, flags
);
146 st
= readl(port
->membase
+ UART_STAT
);
147 spin_unlock_irqrestore(&port
->lock
, flags
);
149 return (st
& STAT_TX_FIFO_EMP
) ? TIOCSER_TEMT
: 0;
152 static unsigned int mvebu_uart_get_mctrl(struct uart_port
*port
)
154 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
157 static void mvebu_uart_set_mctrl(struct uart_port
*port
,
161 * Even if we do not support configuring the modem control lines, this
162 * function must be proided to the serial core
166 static void mvebu_uart_stop_tx(struct uart_port
*port
)
168 unsigned int ctl
= readl(port
->membase
+ UART_INTR(port
));
170 ctl
&= ~CTRL_TX_RDY_INT(port
);
171 writel(ctl
, port
->membase
+ UART_INTR(port
));
174 static void mvebu_uart_start_tx(struct uart_port
*port
)
177 struct circ_buf
*xmit
= &port
->state
->xmit
;
179 if (IS_EXTENDED(port
) && !uart_circ_empty(xmit
)) {
180 writel(xmit
->buf
[xmit
->tail
], port
->membase
+ UART_TSH(port
));
181 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
185 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_stop_rx(struct uart_port
*port
)
194 ctl
= readl(port
->membase
+ UART_CTRL(port
));
195 ctl
&= ~CTRL_BRK_INT
;
196 writel(ctl
, port
->membase
+ UART_CTRL(port
));
198 ctl
= readl(port
->membase
+ UART_INTR(port
));
199 ctl
&= ~CTRL_RX_RDY_INT(port
);
200 writel(ctl
, port
->membase
+ UART_INTR(port
));
203 static void mvebu_uart_break_ctl(struct uart_port
*port
, int brk
)
208 spin_lock_irqsave(&port
->lock
, flags
);
209 ctl
= readl(port
->membase
+ UART_CTRL(port
));
211 ctl
|= CTRL_SND_BRK_SEQ
;
213 ctl
&= ~CTRL_SND_BRK_SEQ
;
214 writel(ctl
, port
->membase
+ UART_CTRL(port
));
215 spin_unlock_irqrestore(&port
->lock
, flags
);
218 static void mvebu_uart_rx_chars(struct uart_port
*port
, unsigned int status
)
220 struct tty_port
*tport
= &port
->state
->port
;
221 unsigned char ch
= 0;
225 if (status
& STAT_RX_RDY(port
)) {
226 ch
= readl(port
->membase
+ UART_RBR(port
));
231 if (status
& STAT_PAR_ERR
)
232 port
->icount
.parity
++;
235 if (status
& STAT_BRK_DET
) {
237 status
&= ~(STAT_FRM_ERR
| STAT_PAR_ERR
);
238 if (uart_handle_break(port
))
242 if (status
& STAT_OVR_ERR
)
243 port
->icount
.overrun
++;
245 if (status
& STAT_FRM_ERR
)
246 port
->icount
.frame
++;
248 if (uart_handle_sysrq_char(port
, ch
))
251 if (status
& port
->ignore_status_mask
& STAT_PAR_ERR
)
252 status
&= ~STAT_RX_RDY(port
);
254 status
&= port
->read_status_mask
;
256 if (status
& STAT_PAR_ERR
)
259 status
&= ~port
->ignore_status_mask
;
261 if (status
& STAT_RX_RDY(port
))
262 tty_insert_flip_char(tport
, ch
, flag
);
264 if (status
& STAT_BRK_DET
)
265 tty_insert_flip_char(tport
, 0, TTY_BREAK
);
267 if (status
& STAT_FRM_ERR
)
268 tty_insert_flip_char(tport
, 0, TTY_FRAME
);
270 if (status
& STAT_OVR_ERR
)
271 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
274 status
= readl(port
->membase
+ UART_STAT
);
275 } while (status
& (STAT_RX_RDY(port
) | STAT_BRK_DET
));
277 tty_flip_buffer_push(tport
);
280 static void mvebu_uart_tx_chars(struct uart_port
*port
, unsigned int status
)
282 struct circ_buf
*xmit
= &port
->state
->xmit
;
287 writel(port
->x_char
, port
->membase
+ UART_TSH(port
));
293 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
294 mvebu_uart_stop_tx(port
);
298 for (count
= 0; count
< port
->fifosize
; count
++) {
299 writel(xmit
->buf
[xmit
->tail
], port
->membase
+ UART_TSH(port
));
300 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
303 if (uart_circ_empty(xmit
))
306 st
= readl(port
->membase
+ UART_STAT
);
307 if (st
& STAT_TX_FIFO_FUL
)
311 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
312 uart_write_wakeup(port
);
314 if (uart_circ_empty(xmit
))
315 mvebu_uart_stop_tx(port
);
318 static irqreturn_t
mvebu_uart_isr(int irq
, void *dev_id
)
320 struct uart_port
*port
= (struct uart_port
*)dev_id
;
321 unsigned int st
= readl(port
->membase
+ UART_STAT
);
323 if (st
& (STAT_RX_RDY(port
) | STAT_OVR_ERR
| STAT_FRM_ERR
|
325 mvebu_uart_rx_chars(port
, st
);
327 if (st
& STAT_TX_RDY(port
))
328 mvebu_uart_tx_chars(port
, st
);
333 static irqreturn_t
mvebu_uart_rx_isr(int irq
, void *dev_id
)
335 struct uart_port
*port
= (struct uart_port
*)dev_id
;
336 unsigned int st
= readl(port
->membase
+ UART_STAT
);
338 if (st
& (STAT_RX_RDY(port
) | STAT_OVR_ERR
| STAT_FRM_ERR
|
340 mvebu_uart_rx_chars(port
, st
);
345 static irqreturn_t
mvebu_uart_tx_isr(int irq
, void *dev_id
)
347 struct uart_port
*port
= (struct uart_port
*)dev_id
;
348 unsigned int st
= readl(port
->membase
+ UART_STAT
);
350 if (st
& STAT_TX_RDY(port
))
351 mvebu_uart_tx_chars(port
, st
);
356 static int mvebu_uart_startup(struct uart_port
*port
)
358 struct mvebu_uart
*mvuart
= to_mvuart(port
);
362 writel(CTRL_TXFIFO_RST
| CTRL_RXFIFO_RST
,
363 port
->membase
+ UART_CTRL(port
));
366 /* Clear the error bits of state register before IRQ request */
367 ret
= readl(port
->membase
+ UART_STAT
);
369 writel(ret
, port
->membase
+ UART_STAT
);
371 writel(CTRL_BRK_INT
, port
->membase
+ UART_CTRL(port
));
373 ctl
= readl(port
->membase
+ UART_INTR(port
));
374 ctl
|= CTRL_RX_RDY_INT(port
);
375 writel(ctl
, port
->membase
+ UART_INTR(port
));
377 if (!mvuart
->irq
[UART_TX_IRQ
]) {
378 /* Old bindings with just one interrupt (UART0 only) */
379 ret
= devm_request_irq(port
->dev
, mvuart
->irq
[UART_IRQ_SUM
],
380 mvebu_uart_isr
, port
->irqflags
,
381 dev_name(port
->dev
), port
);
383 dev_err(port
->dev
, "unable to request IRQ %d\n",
384 mvuart
->irq
[UART_IRQ_SUM
]);
388 /* New bindings with an IRQ for RX and TX (both UART) */
389 ret
= devm_request_irq(port
->dev
, mvuart
->irq
[UART_RX_IRQ
],
390 mvebu_uart_rx_isr
, port
->irqflags
,
391 dev_name(port
->dev
), port
);
393 dev_err(port
->dev
, "unable to request IRQ %d\n",
394 mvuart
->irq
[UART_RX_IRQ
]);
398 ret
= devm_request_irq(port
->dev
, mvuart
->irq
[UART_TX_IRQ
],
399 mvebu_uart_tx_isr
, port
->irqflags
,
403 dev_err(port
->dev
, "unable to request IRQ %d\n",
404 mvuart
->irq
[UART_TX_IRQ
]);
405 devm_free_irq(port
->dev
, mvuart
->irq
[UART_RX_IRQ
],
414 static void mvebu_uart_shutdown(struct uart_port
*port
)
416 struct mvebu_uart
*mvuart
= to_mvuart(port
);
418 writel(0, port
->membase
+ UART_INTR(port
));
420 if (!mvuart
->irq
[UART_TX_IRQ
]) {
421 devm_free_irq(port
->dev
, mvuart
->irq
[UART_IRQ_SUM
], port
);
423 devm_free_irq(port
->dev
, mvuart
->irq
[UART_RX_IRQ
], port
);
424 devm_free_irq(port
->dev
, mvuart
->irq
[UART_TX_IRQ
], port
);
428 static int mvebu_uart_baud_rate_set(struct uart_port
*port
, unsigned int baud
)
430 struct mvebu_uart
*mvuart
= to_mvuart(port
);
431 unsigned int baud_rate_div
;
434 if (IS_ERR(mvuart
->clk
))
435 return -PTR_ERR(mvuart
->clk
);
438 * The UART clock is divided by the value of the divisor to generate
439 * UCLK_OUT clock, which is 16 times faster than the baudrate.
440 * This prescaler can achieve all standard baudrates until 230400.
441 * Higher baudrates could be achieved for the extended UART by using the
442 * programmable oversampling stack (also called fractional divisor).
444 baud_rate_div
= DIV_ROUND_UP(port
->uartclk
, baud
* 16);
445 brdv
= readl(port
->membase
+ UART_BRDV
);
446 brdv
&= ~BRDV_BAUD_MASK
;
447 brdv
|= baud_rate_div
;
448 writel(brdv
, port
->membase
+ UART_BRDV
);
453 static void mvebu_uart_set_termios(struct uart_port
*port
,
454 struct ktermios
*termios
,
455 struct ktermios
*old
)
460 spin_lock_irqsave(&port
->lock
, flags
);
462 port
->read_status_mask
= STAT_RX_RDY(port
) | STAT_OVR_ERR
|
463 STAT_TX_RDY(port
) | STAT_TX_FIFO_FUL
;
465 if (termios
->c_iflag
& INPCK
)
466 port
->read_status_mask
|= STAT_FRM_ERR
| STAT_PAR_ERR
;
468 port
->ignore_status_mask
= 0;
469 if (termios
->c_iflag
& IGNPAR
)
470 port
->ignore_status_mask
|=
471 STAT_FRM_ERR
| STAT_PAR_ERR
| STAT_OVR_ERR
;
473 if ((termios
->c_cflag
& CREAD
) == 0)
474 port
->ignore_status_mask
|= STAT_RX_RDY(port
) | STAT_BRK_ERR
;
477 * Maximum achievable frequency with simple baudrate divisor is 230400.
478 * Since the error per bit frame would be of more than 15%, achieving
479 * higher frequencies would require to implement the fractional divisor
482 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
483 if (mvebu_uart_baud_rate_set(port
, baud
)) {
484 /* No clock available, baudrate cannot be changed */
486 baud
= uart_get_baud_rate(port
, old
, NULL
, 0, 230400);
488 tty_termios_encode_baud_rate(termios
, baud
, baud
);
489 uart_update_timeout(port
, termios
->c_cflag
, baud
);
492 /* Only the following flag changes are supported */
494 termios
->c_iflag
&= INPCK
| IGNPAR
;
495 termios
->c_iflag
|= old
->c_iflag
& ~(INPCK
| IGNPAR
);
496 termios
->c_cflag
&= CREAD
| CBAUD
;
497 termios
->c_cflag
|= old
->c_cflag
& ~(CREAD
| CBAUD
);
500 spin_unlock_irqrestore(&port
->lock
, flags
);
503 static const char *mvebu_uart_type(struct uart_port
*port
)
505 return MVEBU_UART_TYPE
;
508 static void mvebu_uart_release_port(struct uart_port
*port
)
510 /* Nothing to do here */
513 static int mvebu_uart_request_port(struct uart_port
*port
)
518 #ifdef CONFIG_CONSOLE_POLL
519 static int mvebu_uart_get_poll_char(struct uart_port
*port
)
521 unsigned int st
= readl(port
->membase
+ UART_STAT
);
523 if (!(st
& STAT_RX_RDY(port
)))
526 return readl(port
->membase
+ UART_RBR(port
));
529 static void mvebu_uart_put_poll_char(struct uart_port
*port
, unsigned char c
)
534 st
= readl(port
->membase
+ UART_STAT
);
536 if (!(st
& STAT_TX_FIFO_FUL
))
542 writel(c
, port
->membase
+ UART_TSH(port
));
546 static const struct uart_ops mvebu_uart_ops
= {
547 .tx_empty
= mvebu_uart_tx_empty
,
548 .set_mctrl
= mvebu_uart_set_mctrl
,
549 .get_mctrl
= mvebu_uart_get_mctrl
,
550 .stop_tx
= mvebu_uart_stop_tx
,
551 .start_tx
= mvebu_uart_start_tx
,
552 .stop_rx
= mvebu_uart_stop_rx
,
553 .break_ctl
= mvebu_uart_break_ctl
,
554 .startup
= mvebu_uart_startup
,
555 .shutdown
= mvebu_uart_shutdown
,
556 .set_termios
= mvebu_uart_set_termios
,
557 .type
= mvebu_uart_type
,
558 .release_port
= mvebu_uart_release_port
,
559 .request_port
= mvebu_uart_request_port
,
560 #ifdef CONFIG_CONSOLE_POLL
561 .poll_get_char
= mvebu_uart_get_poll_char
,
562 .poll_put_char
= mvebu_uart_put_poll_char
,
566 /* Console Driver Operations */
568 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
570 static void mvebu_uart_putc(struct uart_port
*port
, int c
)
575 st
= readl(port
->membase
+ UART_STAT
);
576 if (!(st
& STAT_TX_FIFO_FUL
))
580 /* At early stage, DT is not parsed yet, only use UART0 */
581 writel(c
, port
->membase
+ UART_STD_TSH
);
584 st
= readl(port
->membase
+ UART_STAT
);
585 if (st
& STAT_TX_FIFO_EMP
)
590 static void mvebu_uart_putc_early_write(struct console
*con
,
594 struct earlycon_device
*dev
= con
->data
;
596 uart_console_write(&dev
->port
, s
, n
, mvebu_uart_putc
);
600 mvebu_uart_early_console_setup(struct earlycon_device
*device
,
603 if (!device
->port
.membase
)
606 device
->con
->write
= mvebu_uart_putc_early_write
;
611 EARLYCON_DECLARE(ar3700_uart
, mvebu_uart_early_console_setup
);
612 OF_EARLYCON_DECLARE(ar3700_uart
, "marvell,armada-3700-uart",
613 mvebu_uart_early_console_setup
);
615 static void wait_for_xmitr(struct uart_port
*port
)
619 readl_poll_timeout_atomic(port
->membase
+ UART_STAT
, val
,
620 (val
& STAT_TX_EMP
), 1, 10000);
623 static void mvebu_uart_console_putchar(struct uart_port
*port
, int ch
)
625 wait_for_xmitr(port
);
626 writel(ch
, port
->membase
+ UART_TSH(port
));
629 static void mvebu_uart_console_write(struct console
*co
, const char *s
,
632 struct uart_port
*port
= &mvebu_uart_ports
[co
->index
];
634 unsigned int ier
, intr
, ctl
;
637 if (oops_in_progress
)
638 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
640 spin_lock_irqsave(&port
->lock
, flags
);
642 ier
= readl(port
->membase
+ UART_CTRL(port
)) & CTRL_BRK_INT
;
643 intr
= readl(port
->membase
+ UART_INTR(port
)) &
644 (CTRL_RX_RDY_INT(port
) | CTRL_TX_RDY_INT(port
));
645 writel(0, port
->membase
+ UART_CTRL(port
));
646 writel(0, port
->membase
+ UART_INTR(port
));
648 uart_console_write(port
, s
, count
, mvebu_uart_console_putchar
);
650 wait_for_xmitr(port
);
653 writel(ier
, port
->membase
+ UART_CTRL(port
));
656 ctl
= intr
| readl(port
->membase
+ UART_INTR(port
));
657 writel(ctl
, port
->membase
+ UART_INTR(port
));
661 spin_unlock_irqrestore(&port
->lock
, flags
);
664 static int mvebu_uart_console_setup(struct console
*co
, char *options
)
666 struct uart_port
*port
;
672 if (co
->index
< 0 || co
->index
>= MVEBU_NR_UARTS
)
675 port
= &mvebu_uart_ports
[co
->index
];
677 if (!port
->mapbase
|| !port
->membase
) {
678 pr_debug("console on ttyMV%i not present\n", co
->index
);
683 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
685 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
688 static struct uart_driver mvebu_uart_driver
;
690 static struct console mvebu_uart_console
= {
692 .write
= mvebu_uart_console_write
,
693 .device
= uart_console_device
,
694 .setup
= mvebu_uart_console_setup
,
695 .flags
= CON_PRINTBUFFER
,
697 .data
= &mvebu_uart_driver
,
700 static int __init
mvebu_uart_console_init(void)
702 register_console(&mvebu_uart_console
);
706 console_initcall(mvebu_uart_console_init
);
709 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
711 static struct uart_driver mvebu_uart_driver
= {
712 .owner
= THIS_MODULE
,
713 .driver_name
= DRIVER_NAME
,
715 .nr
= MVEBU_NR_UARTS
,
716 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
717 .cons
= &mvebu_uart_console
,
721 static const struct of_device_id mvebu_uart_of_match
[];
723 /* Counter to keep track of each UART port id when not using CONFIG_OF */
724 static int uart_num_counter
;
726 static int mvebu_uart_probe(struct platform_device
*pdev
)
728 struct resource
*reg
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
729 const struct of_device_id
*match
= of_match_device(mvebu_uart_of_match
,
731 struct uart_port
*port
;
732 struct mvebu_uart
*mvuart
;
736 dev_err(&pdev
->dev
, "no registers defined\n");
740 /* Assume that all UART ports have a DT alias or none has */
741 id
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
742 if (!pdev
->dev
.of_node
|| id
< 0)
743 pdev
->id
= uart_num_counter
++;
747 if (pdev
->id
>= MVEBU_NR_UARTS
) {
748 dev_err(&pdev
->dev
, "cannot have more than %d UART ports\n",
753 port
= &mvebu_uart_ports
[pdev
->id
];
755 spin_lock_init(&port
->lock
);
757 port
->dev
= &pdev
->dev
;
758 port
->type
= PORT_MVEBU
;
759 port
->ops
= &mvebu_uart_ops
;
763 port
->iotype
= UPIO_MEM32
;
764 port
->flags
= UPF_FIXED_PORT
;
765 port
->line
= pdev
->id
;
768 * IRQ number is not stored in this structure because we may have two of
769 * them per port (RX and TX). Instead, use the driver UART structure
770 * array so called ->irq[].
774 port
->mapbase
= reg
->start
;
776 port
->membase
= devm_ioremap_resource(&pdev
->dev
, reg
);
777 if (IS_ERR(port
->membase
))
778 return -PTR_ERR(port
->membase
);
780 mvuart
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_uart
),
785 /* Get controller data depending on the compatible string */
786 mvuart
->data
= (struct mvebu_uart_driver_data
*)match
->data
;
789 port
->private_data
= mvuart
;
790 platform_set_drvdata(pdev
, mvuart
);
792 /* Get fixed clock frequency */
793 mvuart
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
794 if (IS_ERR(mvuart
->clk
)) {
795 if (PTR_ERR(mvuart
->clk
) == -EPROBE_DEFER
)
796 return PTR_ERR(mvuart
->clk
);
798 if (IS_EXTENDED(port
)) {
799 dev_err(&pdev
->dev
, "unable to get UART clock\n");
800 return PTR_ERR(mvuart
->clk
);
803 if (!clk_prepare_enable(mvuart
->clk
))
804 port
->uartclk
= clk_get_rate(mvuart
->clk
);
807 /* Manage interrupts */
808 if (platform_irq_count(pdev
) == 1) {
809 /* Old bindings: no name on the single unamed UART0 IRQ */
810 irq
= platform_get_irq(pdev
, 0);
812 dev_err(&pdev
->dev
, "unable to get UART IRQ\n");
816 mvuart
->irq
[UART_IRQ_SUM
] = irq
;
819 * New bindings: named interrupts (RX, TX) for both UARTS,
820 * only make use of uart-rx and uart-tx interrupts, do not use
821 * uart-sum of UART0 port.
823 irq
= platform_get_irq_byname(pdev
, "uart-rx");
825 dev_err(&pdev
->dev
, "unable to get 'uart-rx' IRQ\n");
829 mvuart
->irq
[UART_RX_IRQ
] = irq
;
831 irq
= platform_get_irq_byname(pdev
, "uart-tx");
833 dev_err(&pdev
->dev
, "unable to get 'uart-tx' IRQ\n");
837 mvuart
->irq
[UART_TX_IRQ
] = irq
;
841 writel(CTRL_SOFT_RST
, port
->membase
+ UART_CTRL(port
));
843 writel(0, port
->membase
+ UART_CTRL(port
));
845 ret
= uart_add_one_port(&mvebu_uart_driver
, port
);
851 static struct mvebu_uart_driver_data uart_std_driver_data
= {
853 .regs
.rbr
= UART_STD_RBR
,
854 .regs
.tsh
= UART_STD_TSH
,
855 .regs
.ctrl
= UART_STD_CTRL1
,
856 .regs
.intr
= UART_STD_CTRL2
,
857 .flags
.ctrl_tx_rdy_int
= CTRL_STD_TX_RDY_INT
,
858 .flags
.ctrl_rx_rdy_int
= CTRL_STD_RX_RDY_INT
,
859 .flags
.stat_tx_rdy
= STAT_STD_TX_RDY
,
860 .flags
.stat_rx_rdy
= STAT_STD_RX_RDY
,
863 static struct mvebu_uart_driver_data uart_ext_driver_data
= {
865 .regs
.rbr
= UART_EXT_RBR
,
866 .regs
.tsh
= UART_EXT_TSH
,
867 .regs
.ctrl
= UART_EXT_CTRL1
,
868 .regs
.intr
= UART_EXT_CTRL2
,
869 .flags
.ctrl_tx_rdy_int
= CTRL_EXT_TX_RDY_INT
,
870 .flags
.ctrl_rx_rdy_int
= CTRL_EXT_RX_RDY_INT
,
871 .flags
.stat_tx_rdy
= STAT_EXT_TX_RDY
,
872 .flags
.stat_rx_rdy
= STAT_EXT_RX_RDY
,
875 /* Match table for of_platform binding */
876 static const struct of_device_id mvebu_uart_of_match
[] = {
878 .compatible
= "marvell,armada-3700-uart",
879 .data
= (void *)&uart_std_driver_data
,
882 .compatible
= "marvell,armada-3700-uart-ext",
883 .data
= (void *)&uart_ext_driver_data
,
888 static struct platform_driver mvebu_uart_platform_driver
= {
889 .probe
= mvebu_uart_probe
,
891 .name
= "mvebu-uart",
892 .of_match_table
= of_match_ptr(mvebu_uart_of_match
),
893 .suppress_bind_attrs
= true,
897 static int __init
mvebu_uart_init(void)
901 ret
= uart_register_driver(&mvebu_uart_driver
);
905 ret
= platform_driver_register(&mvebu_uart_platform_driver
);
907 uart_unregister_driver(&mvebu_uart_driver
);
911 arch_initcall(mvebu_uart_init
);