2 * ***************************************************************************
3 * Copyright (C) 2015 Marvell International Ltd.
4 * ***************************************************************************
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation, either version 2 of the License, or any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * ***************************************************************************
19 #include <linux/clk.h>
20 #include <linux/console.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
25 #include <linux/iopoll.h>
26 #include <linux/module.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/of_irq.h>
31 #include <linux/of_platform.h>
32 #include <linux/platform_device.h>
33 #include <linux/serial.h>
34 #include <linux/serial_core.h>
35 #include <linux/slab.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
41 #define RBR_BRK_DET BIT(15)
42 #define RBR_FRM_ERR_DET BIT(14)
43 #define RBR_PAR_ERR_DET BIT(13)
44 #define RBR_OVR_ERR_DET BIT(12)
48 #define UART_CTRL 0x08
49 #define CTRL_SOFT_RST BIT(31)
50 #define CTRL_TXFIFO_RST BIT(15)
51 #define CTRL_RXFIFO_RST BIT(14)
52 #define CTRL_ST_MIRR_EN BIT(13)
53 #define CTRL_LPBK_EN BIT(12)
54 #define CTRL_SND_BRK_SEQ BIT(11)
55 #define CTRL_PAR_EN BIT(10)
56 #define CTRL_TWO_STOP BIT(9)
57 #define CTRL_TX_HFL_INT BIT(8)
58 #define CTRL_RX_HFL_INT BIT(7)
59 #define CTRL_TX_EMP_INT BIT(6)
60 #define CTRL_TX_RDY_INT BIT(5)
61 #define CTRL_RX_RDY_INT BIT(4)
62 #define CTRL_BRK_DET_INT BIT(3)
63 #define CTRL_FRM_ERR_INT BIT(2)
64 #define CTRL_PAR_ERR_INT BIT(1)
65 #define CTRL_OVR_ERR_INT BIT(0)
66 #define CTRL_RX_INT (CTRL_RX_RDY_INT | CTRL_BRK_DET_INT |\
67 CTRL_FRM_ERR_INT | CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
69 #define UART_STAT 0x0c
70 #define STAT_TX_FIFO_EMP BIT(13)
71 #define STAT_RX_FIFO_EMP BIT(12)
72 #define STAT_TX_FIFO_FUL BIT(11)
73 #define STAT_TX_FIFO_HFL BIT(10)
74 #define STAT_RX_TOGL BIT(9)
75 #define STAT_RX_FIFO_FUL BIT(8)
76 #define STAT_RX_FIFO_HFL BIT(7)
77 #define STAT_TX_EMP BIT(6)
78 #define STAT_TX_RDY BIT(5)
79 #define STAT_RX_RDY BIT(4)
80 #define STAT_BRK_DET BIT(3)
81 #define STAT_FRM_ERR BIT(2)
82 #define STAT_PAR_ERR BIT(1)
83 #define STAT_OVR_ERR BIT(0)
84 #define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
85 | STAT_PAR_ERR | STAT_OVR_ERR)
87 #define UART_BRDV 0x10
89 #define MVEBU_NR_UARTS 1
91 #define MVEBU_UART_TYPE "mvebu-uart"
93 static struct uart_port mvebu_uart_ports
[MVEBU_NR_UARTS
];
95 struct mvebu_uart_data
{
96 struct uart_port
*port
;
100 /* Core UART Driver Operations */
101 static unsigned int mvebu_uart_tx_empty(struct uart_port
*port
)
106 spin_lock_irqsave(&port
->lock
, flags
);
107 st
= readl(port
->membase
+ UART_STAT
);
108 spin_unlock_irqrestore(&port
->lock
, flags
);
110 return (st
& STAT_TX_FIFO_EMP
) ? TIOCSER_TEMT
: 0;
113 static unsigned int mvebu_uart_get_mctrl(struct uart_port
*port
)
115 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
118 static void mvebu_uart_set_mctrl(struct uart_port
*port
,
122 * Even if we do not support configuring the modem control lines, this
123 * function must be proided to the serial core
127 static void mvebu_uart_stop_tx(struct uart_port
*port
)
129 unsigned int ctl
= readl(port
->membase
+ UART_CTRL
);
131 ctl
&= ~CTRL_TX_RDY_INT
;
132 writel(ctl
, port
->membase
+ UART_CTRL
);
135 static void mvebu_uart_start_tx(struct uart_port
*port
)
137 unsigned int ctl
= readl(port
->membase
+ UART_CTRL
);
139 ctl
|= CTRL_TX_RDY_INT
;
140 writel(ctl
, port
->membase
+ UART_CTRL
);
143 static void mvebu_uart_stop_rx(struct uart_port
*port
)
145 unsigned int ctl
= readl(port
->membase
+ UART_CTRL
);
148 writel(ctl
, port
->membase
+ UART_CTRL
);
151 static void mvebu_uart_break_ctl(struct uart_port
*port
, int brk
)
156 spin_lock_irqsave(&port
->lock
, flags
);
157 ctl
= readl(port
->membase
+ UART_CTRL
);
159 ctl
|= CTRL_SND_BRK_SEQ
;
161 ctl
&= ~CTRL_SND_BRK_SEQ
;
162 writel(ctl
, port
->membase
+ UART_CTRL
);
163 spin_unlock_irqrestore(&port
->lock
, flags
);
166 static void mvebu_uart_rx_chars(struct uart_port
*port
, unsigned int status
)
168 struct tty_port
*tport
= &port
->state
->port
;
169 unsigned char ch
= 0;
173 if (status
& STAT_RX_RDY
) {
174 ch
= readl(port
->membase
+ UART_RBR
);
179 if (status
& STAT_PAR_ERR
)
180 port
->icount
.parity
++;
183 if (status
& STAT_BRK_DET
) {
185 status
&= ~(STAT_FRM_ERR
| STAT_PAR_ERR
);
186 if (uart_handle_break(port
))
190 if (status
& STAT_OVR_ERR
)
191 port
->icount
.overrun
++;
193 if (status
& STAT_FRM_ERR
)
194 port
->icount
.frame
++;
196 if (uart_handle_sysrq_char(port
, ch
))
199 if (status
& port
->ignore_status_mask
& STAT_PAR_ERR
)
200 status
&= ~STAT_RX_RDY
;
202 status
&= port
->read_status_mask
;
204 if (status
& STAT_PAR_ERR
)
207 status
&= ~port
->ignore_status_mask
;
209 if (status
& STAT_RX_RDY
)
210 tty_insert_flip_char(tport
, ch
, flag
);
212 if (status
& STAT_BRK_DET
)
213 tty_insert_flip_char(tport
, 0, TTY_BREAK
);
215 if (status
& STAT_FRM_ERR
)
216 tty_insert_flip_char(tport
, 0, TTY_FRAME
);
218 if (status
& STAT_OVR_ERR
)
219 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
222 status
= readl(port
->membase
+ UART_STAT
);
223 } while (status
& (STAT_RX_RDY
| STAT_BRK_DET
));
225 tty_flip_buffer_push(tport
);
228 static void mvebu_uart_tx_chars(struct uart_port
*port
, unsigned int status
)
230 struct circ_buf
*xmit
= &port
->state
->xmit
;
235 writel(port
->x_char
, port
->membase
+ UART_TSH
);
241 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
242 mvebu_uart_stop_tx(port
);
246 for (count
= 0; count
< port
->fifosize
; count
++) {
247 writel(xmit
->buf
[xmit
->tail
], port
->membase
+ UART_TSH
);
248 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
251 if (uart_circ_empty(xmit
))
254 st
= readl(port
->membase
+ UART_STAT
);
255 if (st
& STAT_TX_FIFO_FUL
)
259 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
260 uart_write_wakeup(port
);
262 if (uart_circ_empty(xmit
))
263 mvebu_uart_stop_tx(port
);
266 static irqreturn_t
mvebu_uart_isr(int irq
, void *dev_id
)
268 struct uart_port
*port
= (struct uart_port
*)dev_id
;
269 unsigned int st
= readl(port
->membase
+ UART_STAT
);
271 if (st
& (STAT_RX_RDY
| STAT_OVR_ERR
| STAT_FRM_ERR
| STAT_BRK_DET
))
272 mvebu_uart_rx_chars(port
, st
);
274 if (st
& STAT_TX_RDY
)
275 mvebu_uart_tx_chars(port
, st
);
280 static int mvebu_uart_startup(struct uart_port
*port
)
284 writel(CTRL_TXFIFO_RST
| CTRL_RXFIFO_RST
,
285 port
->membase
+ UART_CTRL
);
287 writel(CTRL_RX_INT
, port
->membase
+ UART_CTRL
);
289 ret
= request_irq(port
->irq
, mvebu_uart_isr
, port
->irqflags
, "serial",
292 dev_err(port
->dev
, "failed to request irq\n");
299 static void mvebu_uart_shutdown(struct uart_port
*port
)
301 writel(0, port
->membase
+ UART_CTRL
);
304 static void mvebu_uart_set_termios(struct uart_port
*port
,
305 struct ktermios
*termios
,
306 struct ktermios
*old
)
311 spin_lock_irqsave(&port
->lock
, flags
);
313 port
->read_status_mask
= STAT_RX_RDY
| STAT_OVR_ERR
|
314 STAT_TX_RDY
| STAT_TX_FIFO_FUL
;
316 if (termios
->c_iflag
& INPCK
)
317 port
->read_status_mask
|= STAT_FRM_ERR
| STAT_PAR_ERR
;
319 port
->ignore_status_mask
= 0;
320 if (termios
->c_iflag
& IGNPAR
)
321 port
->ignore_status_mask
|=
322 STAT_FRM_ERR
| STAT_PAR_ERR
| STAT_OVR_ERR
;
324 if ((termios
->c_cflag
& CREAD
) == 0)
325 port
->ignore_status_mask
|= STAT_RX_RDY
| STAT_BRK_ERR
;
328 tty_termios_copy_hw(termios
, old
);
330 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 460800);
331 uart_update_timeout(port
, termios
->c_cflag
, baud
);
333 spin_unlock_irqrestore(&port
->lock
, flags
);
336 static const char *mvebu_uart_type(struct uart_port
*port
)
338 return MVEBU_UART_TYPE
;
341 static void mvebu_uart_release_port(struct uart_port
*port
)
343 /* Nothing to do here */
346 static int mvebu_uart_request_port(struct uart_port
*port
)
351 #ifdef CONFIG_CONSOLE_POLL
352 static int mvebu_uart_get_poll_char(struct uart_port
*port
)
354 unsigned int st
= readl(port
->membase
+ UART_STAT
);
356 if (!(st
& STAT_RX_RDY
))
359 return readl(port
->membase
+ UART_RBR
);
362 static void mvebu_uart_put_poll_char(struct uart_port
*port
, unsigned char c
)
367 st
= readl(port
->membase
+ UART_STAT
);
369 if (!(st
& STAT_TX_FIFO_FUL
))
375 writel(c
, port
->membase
+ UART_TSH
);
379 static const struct uart_ops mvebu_uart_ops
= {
380 .tx_empty
= mvebu_uart_tx_empty
,
381 .set_mctrl
= mvebu_uart_set_mctrl
,
382 .get_mctrl
= mvebu_uart_get_mctrl
,
383 .stop_tx
= mvebu_uart_stop_tx
,
384 .start_tx
= mvebu_uart_start_tx
,
385 .stop_rx
= mvebu_uart_stop_rx
,
386 .break_ctl
= mvebu_uart_break_ctl
,
387 .startup
= mvebu_uart_startup
,
388 .shutdown
= mvebu_uart_shutdown
,
389 .set_termios
= mvebu_uart_set_termios
,
390 .type
= mvebu_uart_type
,
391 .release_port
= mvebu_uart_release_port
,
392 .request_port
= mvebu_uart_request_port
,
393 #ifdef CONFIG_CONSOLE_POLL
394 .poll_get_char
= mvebu_uart_get_poll_char
,
395 .poll_put_char
= mvebu_uart_put_poll_char
,
399 /* Console Driver Operations */
401 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
403 static void mvebu_uart_putc(struct uart_port
*port
, int c
)
408 st
= readl(port
->membase
+ UART_STAT
);
409 if (!(st
& STAT_TX_FIFO_FUL
))
413 writel(c
, port
->membase
+ UART_TSH
);
416 st
= readl(port
->membase
+ UART_STAT
);
417 if (st
& STAT_TX_FIFO_EMP
)
422 static void mvebu_uart_putc_early_write(struct console
*con
,
426 struct earlycon_device
*dev
= con
->data
;
428 uart_console_write(&dev
->port
, s
, n
, mvebu_uart_putc
);
432 mvebu_uart_early_console_setup(struct earlycon_device
*device
,
435 if (!device
->port
.membase
)
438 device
->con
->write
= mvebu_uart_putc_early_write
;
443 EARLYCON_DECLARE(ar3700_uart
, mvebu_uart_early_console_setup
);
444 OF_EARLYCON_DECLARE(ar3700_uart
, "marvell,armada-3700-uart",
445 mvebu_uart_early_console_setup
);
447 static void wait_for_xmitr(struct uart_port
*port
)
451 readl_poll_timeout_atomic(port
->membase
+ UART_STAT
, val
,
452 (val
& STAT_TX_EMP
), 1, 10000);
455 static void mvebu_uart_console_putchar(struct uart_port
*port
, int ch
)
457 wait_for_xmitr(port
);
458 writel(ch
, port
->membase
+ UART_TSH
);
461 static void mvebu_uart_console_write(struct console
*co
, const char *s
,
464 struct uart_port
*port
= &mvebu_uart_ports
[co
->index
];
469 if (oops_in_progress
)
470 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
472 spin_lock_irqsave(&port
->lock
, flags
);
474 ier
= readl(port
->membase
+ UART_CTRL
) &
475 (CTRL_RX_INT
| CTRL_TX_RDY_INT
);
476 writel(0, port
->membase
+ UART_CTRL
);
478 uart_console_write(port
, s
, count
, mvebu_uart_console_putchar
);
480 wait_for_xmitr(port
);
483 writel(ier
, port
->membase
+ UART_CTRL
);
486 spin_unlock_irqrestore(&port
->lock
, flags
);
489 static int mvebu_uart_console_setup(struct console
*co
, char *options
)
491 struct uart_port
*port
;
497 if (co
->index
< 0 || co
->index
>= MVEBU_NR_UARTS
)
500 port
= &mvebu_uart_ports
[co
->index
];
502 if (!port
->mapbase
|| !port
->membase
) {
503 pr_debug("console on ttyMV%i not present\n", co
->index
);
508 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
510 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
513 static struct uart_driver mvebu_uart_driver
;
515 static struct console mvebu_uart_console
= {
517 .write
= mvebu_uart_console_write
,
518 .device
= uart_console_device
,
519 .setup
= mvebu_uart_console_setup
,
520 .flags
= CON_PRINTBUFFER
,
522 .data
= &mvebu_uart_driver
,
525 static int __init
mvebu_uart_console_init(void)
527 register_console(&mvebu_uart_console
);
531 console_initcall(mvebu_uart_console_init
);
534 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
536 static struct uart_driver mvebu_uart_driver
= {
537 .owner
= THIS_MODULE
,
538 .driver_name
= "mvebu_serial",
540 .nr
= MVEBU_NR_UARTS
,
541 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
542 .cons
= &mvebu_uart_console
,
546 static int mvebu_uart_probe(struct platform_device
*pdev
)
548 struct resource
*reg
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
549 struct resource
*irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
550 struct uart_port
*port
;
551 struct mvebu_uart_data
*data
;
555 dev_err(&pdev
->dev
, "no registers/irq defined\n");
559 port
= &mvebu_uart_ports
[0];
561 spin_lock_init(&port
->lock
);
563 port
->dev
= &pdev
->dev
;
564 port
->type
= PORT_MVEBU
;
565 port
->ops
= &mvebu_uart_ops
;
569 port
->iotype
= UPIO_MEM32
;
570 port
->flags
= UPF_FIXED_PORT
;
571 port
->line
= 0; /* single port: force line number to 0 */
573 port
->irq
= irq
->start
;
575 port
->mapbase
= reg
->start
;
577 port
->membase
= devm_ioremap_resource(&pdev
->dev
, reg
);
578 if (IS_ERR(port
->membase
))
579 return -PTR_ERR(port
->membase
);
581 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct mvebu_uart_data
),
588 port
->private_data
= data
;
589 platform_set_drvdata(pdev
, data
);
591 ret
= uart_add_one_port(&mvebu_uart_driver
, port
);
597 static int mvebu_uart_remove(struct platform_device
*pdev
)
599 struct mvebu_uart_data
*data
= platform_get_drvdata(pdev
);
601 uart_remove_one_port(&mvebu_uart_driver
, data
->port
);
602 data
->port
->private_data
= NULL
;
603 data
->port
->mapbase
= 0;
607 /* Match table for of_platform binding */
608 static const struct of_device_id mvebu_uart_of_match
[] = {
609 { .compatible
= "marvell,armada-3700-uart", },
612 MODULE_DEVICE_TABLE(of
, mvebu_uart_of_match
);
614 static struct platform_driver mvebu_uart_platform_driver
= {
615 .probe
= mvebu_uart_probe
,
616 .remove
= mvebu_uart_remove
,
618 .owner
= THIS_MODULE
,
619 .name
= "mvebu-uart",
620 .of_match_table
= of_match_ptr(mvebu_uart_of_match
),
624 static int __init
mvebu_uart_init(void)
628 ret
= uart_register_driver(&mvebu_uart_driver
);
632 ret
= platform_driver_register(&mvebu_uart_platform_driver
);
634 uart_unregister_driver(&mvebu_uart_driver
);
639 static void __exit
mvebu_uart_exit(void)
641 platform_driver_unregister(&mvebu_uart_platform_driver
);
642 uart_unregister_driver(&mvebu_uart_driver
);
645 arch_initcall(mvebu_uart_init
);
646 module_exit(mvebu_uart_exit
);
648 MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
649 MODULE_DESCRIPTION("Marvell Armada-3700 Serial Driver");
650 MODULE_LICENSE("GPL");