1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015 ARM Limited
7 * Author: Vladimir Murzin <vladimir.murzin@arm.com>
9 * TODO: support for SysRq
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/bitops.h>
15 #include <linux/clk.h>
16 #include <linux/console.h>
18 #include <linux/kernel.h>
19 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/serial_core.h>
23 #include <linux/tty_flip.h>
24 #include <linux/types.h>
26 #define SERIAL_NAME "ttyMPS"
27 #define DRIVER_NAME "mps2-uart"
28 #define MAKE_NAME(x) (DRIVER_NAME # x)
30 #define UARTn_DATA 0x00
32 #define UARTn_STATE 0x04
33 #define UARTn_STATE_TX_FULL BIT(0)
34 #define UARTn_STATE_RX_FULL BIT(1)
35 #define UARTn_STATE_TX_OVERRUN BIT(2)
36 #define UARTn_STATE_RX_OVERRUN BIT(3)
38 #define UARTn_CTRL 0x08
39 #define UARTn_CTRL_TX_ENABLE BIT(0)
40 #define UARTn_CTRL_RX_ENABLE BIT(1)
41 #define UARTn_CTRL_TX_INT_ENABLE BIT(2)
42 #define UARTn_CTRL_RX_INT_ENABLE BIT(3)
43 #define UARTn_CTRL_TX_OVERRUN_INT_ENABLE BIT(4)
44 #define UARTn_CTRL_RX_OVERRUN_INT_ENABLE BIT(5)
46 #define UARTn_INT 0x0c
47 #define UARTn_INT_TX BIT(0)
48 #define UARTn_INT_RX BIT(1)
49 #define UARTn_INT_TX_OVERRUN BIT(2)
50 #define UARTn_INT_RX_OVERRUN BIT(3)
52 #define UARTn_BAUDDIV 0x10
53 #define UARTn_BAUDDIV_MASK GENMASK(20, 0)
56 * Helpers to make typical enable/disable operations more readable.
58 #define UARTn_CTRL_TX_GRP (UARTn_CTRL_TX_ENABLE |\
59 UARTn_CTRL_TX_INT_ENABLE |\
60 UARTn_CTRL_TX_OVERRUN_INT_ENABLE)
62 #define UARTn_CTRL_RX_GRP (UARTn_CTRL_RX_ENABLE |\
63 UARTn_CTRL_RX_INT_ENABLE |\
64 UARTn_CTRL_RX_OVERRUN_INT_ENABLE)
66 #define MPS2_MAX_PORTS 3
68 struct mps2_uart_port
{
69 struct uart_port port
;
75 static inline struct mps2_uart_port
*to_mps2_port(struct uart_port
*port
)
77 return container_of(port
, struct mps2_uart_port
, port
);
80 static void mps2_uart_write8(struct uart_port
*port
, u8 val
, unsigned int off
)
82 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
84 writeb(val
, mps_port
->port
.membase
+ off
);
87 static u8
mps2_uart_read8(struct uart_port
*port
, unsigned int off
)
89 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
91 return readb(mps_port
->port
.membase
+ off
);
94 static void mps2_uart_write32(struct uart_port
*port
, u32 val
, unsigned int off
)
96 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
98 writel_relaxed(val
, mps_port
->port
.membase
+ off
);
101 static unsigned int mps2_uart_tx_empty(struct uart_port
*port
)
103 u8 status
= mps2_uart_read8(port
, UARTn_STATE
);
105 return (status
& UARTn_STATE_TX_FULL
) ? 0 : TIOCSER_TEMT
;
108 static void mps2_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
112 static unsigned int mps2_uart_get_mctrl(struct uart_port
*port
)
114 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
;
117 static void mps2_uart_stop_tx(struct uart_port
*port
)
119 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
121 control
&= ~UARTn_CTRL_TX_INT_ENABLE
;
123 mps2_uart_write8(port
, control
, UARTn_CTRL
);
126 static void mps2_uart_tx_chars(struct uart_port
*port
)
128 struct circ_buf
*xmit
= &port
->state
->xmit
;
130 while (!(mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_TX_FULL
)) {
132 mps2_uart_write8(port
, port
->x_char
, UARTn_DATA
);
138 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
141 mps2_uart_write8(port
, xmit
->buf
[xmit
->tail
], UARTn_DATA
);
142 xmit
->tail
= (xmit
->tail
+ 1) % UART_XMIT_SIZE
;
146 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
147 uart_write_wakeup(port
);
149 if (uart_circ_empty(xmit
))
150 mps2_uart_stop_tx(port
);
153 static void mps2_uart_start_tx(struct uart_port
*port
)
155 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
157 control
|= UARTn_CTRL_TX_INT_ENABLE
;
159 mps2_uart_write8(port
, control
, UARTn_CTRL
);
162 * We've just unmasked the TX IRQ and now slow-starting via
163 * polling; if there is enough data to fill up the internal
164 * write buffer in one go, the TX IRQ should assert, at which
165 * point we switch to fully interrupt-driven TX.
168 mps2_uart_tx_chars(port
);
171 static void mps2_uart_stop_rx(struct uart_port
*port
)
173 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
175 control
&= ~UARTn_CTRL_RX_GRP
;
177 mps2_uart_write8(port
, control
, UARTn_CTRL
);
180 static void mps2_uart_break_ctl(struct uart_port
*port
, int ctl
)
184 static void mps2_uart_rx_chars(struct uart_port
*port
)
186 struct tty_port
*tport
= &port
->state
->port
;
188 while (mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_RX_FULL
) {
189 u8 rxdata
= mps2_uart_read8(port
, UARTn_DATA
);
192 tty_insert_flip_char(&port
->state
->port
, rxdata
, TTY_NORMAL
);
195 tty_flip_buffer_push(tport
);
198 static irqreturn_t
mps2_uart_rxirq(int irq
, void *data
)
200 struct uart_port
*port
= data
;
201 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
203 if (unlikely(!(irqflag
& UARTn_INT_RX
)))
206 spin_lock(&port
->lock
);
208 mps2_uart_write8(port
, UARTn_INT_RX
, UARTn_INT
);
209 mps2_uart_rx_chars(port
);
211 spin_unlock(&port
->lock
);
216 static irqreturn_t
mps2_uart_txirq(int irq
, void *data
)
218 struct uart_port
*port
= data
;
219 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
221 if (unlikely(!(irqflag
& UARTn_INT_TX
)))
224 spin_lock(&port
->lock
);
226 mps2_uart_write8(port
, UARTn_INT_TX
, UARTn_INT
);
227 mps2_uart_tx_chars(port
);
229 spin_unlock(&port
->lock
);
234 static irqreturn_t
mps2_uart_oerrirq(int irq
, void *data
)
236 irqreturn_t handled
= IRQ_NONE
;
237 struct uart_port
*port
= data
;
238 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
240 spin_lock(&port
->lock
);
242 if (irqflag
& UARTn_INT_RX_OVERRUN
) {
243 struct tty_port
*tport
= &port
->state
->port
;
245 mps2_uart_write8(port
, UARTn_INT_RX_OVERRUN
, UARTn_INT
);
246 port
->icount
.overrun
++;
247 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
248 tty_flip_buffer_push(tport
);
249 handled
= IRQ_HANDLED
;
253 * It's never been seen in practice and it never *should* happen since
254 * we check if there is enough room in TX buffer before sending data.
255 * So we keep this check in case something suspicious has happened.
257 if (irqflag
& UARTn_INT_TX_OVERRUN
) {
258 mps2_uart_write8(port
, UARTn_INT_TX_OVERRUN
, UARTn_INT
);
259 handled
= IRQ_HANDLED
;
262 spin_unlock(&port
->lock
);
267 static int mps2_uart_startup(struct uart_port
*port
)
269 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
270 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
273 control
&= ~(UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
);
275 mps2_uart_write8(port
, control
, UARTn_CTRL
);
277 ret
= request_irq(mps_port
->rx_irq
, mps2_uart_rxirq
, 0,
278 MAKE_NAME(-rx
), mps_port
);
280 dev_err(port
->dev
, "failed to register rxirq (%d)\n", ret
);
284 ret
= request_irq(mps_port
->tx_irq
, mps2_uart_txirq
, 0,
285 MAKE_NAME(-tx
), mps_port
);
287 dev_err(port
->dev
, "failed to register txirq (%d)\n", ret
);
291 ret
= request_irq(port
->irq
, mps2_uart_oerrirq
, IRQF_SHARED
,
292 MAKE_NAME(-overrun
), mps_port
);
295 dev_err(port
->dev
, "failed to register oerrirq (%d)\n", ret
);
299 control
|= UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
;
301 mps2_uart_write8(port
, control
, UARTn_CTRL
);
306 free_irq(mps_port
->tx_irq
, mps_port
);
308 free_irq(mps_port
->rx_irq
, mps_port
);
313 static void mps2_uart_shutdown(struct uart_port
*port
)
315 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
316 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
318 control
&= ~(UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
);
320 mps2_uart_write8(port
, control
, UARTn_CTRL
);
322 free_irq(mps_port
->rx_irq
, mps_port
);
323 free_irq(mps_port
->tx_irq
, mps_port
);
324 free_irq(port
->irq
, mps_port
);
328 mps2_uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
329 struct ktermios
*old
)
332 unsigned int baud
, bauddiv
;
334 termios
->c_cflag
&= ~(CRTSCTS
| CMSPAR
);
335 termios
->c_cflag
&= ~CSIZE
;
336 termios
->c_cflag
|= CS8
;
337 termios
->c_cflag
&= ~PARENB
;
338 termios
->c_cflag
&= ~CSTOPB
;
340 baud
= uart_get_baud_rate(port
, termios
, old
,
341 DIV_ROUND_CLOSEST(port
->uartclk
, UARTn_BAUDDIV_MASK
),
342 DIV_ROUND_CLOSEST(port
->uartclk
, 16));
344 bauddiv
= DIV_ROUND_CLOSEST(port
->uartclk
, baud
);
346 spin_lock_irqsave(&port
->lock
, flags
);
348 uart_update_timeout(port
, termios
->c_cflag
, baud
);
349 mps2_uart_write32(port
, bauddiv
, UARTn_BAUDDIV
);
351 spin_unlock_irqrestore(&port
->lock
, flags
);
353 if (tty_termios_baud_rate(termios
))
354 tty_termios_encode_baud_rate(termios
, baud
, baud
);
357 static const char *mps2_uart_type(struct uart_port
*port
)
359 return (port
->type
== PORT_MPS2UART
) ? DRIVER_NAME
: NULL
;
362 static void mps2_uart_release_port(struct uart_port
*port
)
366 static int mps2_uart_request_port(struct uart_port
*port
)
371 static void mps2_uart_config_port(struct uart_port
*port
, int type
)
373 if (type
& UART_CONFIG_TYPE
&& !mps2_uart_request_port(port
))
374 port
->type
= PORT_MPS2UART
;
377 static int mps2_uart_verify_port(struct uart_port
*port
, struct serial_struct
*serinfo
)
382 static const struct uart_ops mps2_uart_pops
= {
383 .tx_empty
= mps2_uart_tx_empty
,
384 .set_mctrl
= mps2_uart_set_mctrl
,
385 .get_mctrl
= mps2_uart_get_mctrl
,
386 .stop_tx
= mps2_uart_stop_tx
,
387 .start_tx
= mps2_uart_start_tx
,
388 .stop_rx
= mps2_uart_stop_rx
,
389 .break_ctl
= mps2_uart_break_ctl
,
390 .startup
= mps2_uart_startup
,
391 .shutdown
= mps2_uart_shutdown
,
392 .set_termios
= mps2_uart_set_termios
,
393 .type
= mps2_uart_type
,
394 .release_port
= mps2_uart_release_port
,
395 .request_port
= mps2_uart_request_port
,
396 .config_port
= mps2_uart_config_port
,
397 .verify_port
= mps2_uart_verify_port
,
400 static struct mps2_uart_port mps2_uart_ports
[MPS2_MAX_PORTS
];
402 #ifdef CONFIG_SERIAL_MPS2_UART_CONSOLE
403 static void mps2_uart_console_putchar(struct uart_port
*port
, int ch
)
405 while (mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_TX_FULL
)
408 mps2_uart_write8(port
, ch
, UARTn_DATA
);
411 static void mps2_uart_console_write(struct console
*co
, const char *s
, unsigned int cnt
)
413 struct uart_port
*port
= &mps2_uart_ports
[co
->index
].port
;
415 uart_console_write(port
, s
, cnt
, mps2_uart_console_putchar
);
418 static int mps2_uart_console_setup(struct console
*co
, char *options
)
420 struct mps2_uart_port
*mps_port
;
426 if (co
->index
< 0 || co
->index
>= MPS2_MAX_PORTS
)
429 mps_port
= &mps2_uart_ports
[co
->index
];
432 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
434 return uart_set_options(&mps_port
->port
, co
, baud
, parity
, bits
, flow
);
437 static struct uart_driver mps2_uart_driver
;
439 static struct console mps2_uart_console
= {
441 .device
= uart_console_device
,
442 .write
= mps2_uart_console_write
,
443 .setup
= mps2_uart_console_setup
,
444 .flags
= CON_PRINTBUFFER
,
446 .data
= &mps2_uart_driver
,
449 #define MPS2_SERIAL_CONSOLE (&mps2_uart_console)
451 static void mps2_early_putchar(struct uart_port
*port
, int ch
)
453 while (readb(port
->membase
+ UARTn_STATE
) & UARTn_STATE_TX_FULL
)
456 writeb((unsigned char)ch
, port
->membase
+ UARTn_DATA
);
459 static void mps2_early_write(struct console
*con
, const char *s
, unsigned int n
)
461 struct earlycon_device
*dev
= con
->data
;
463 uart_console_write(&dev
->port
, s
, n
, mps2_early_putchar
);
466 static int __init
mps2_early_console_setup(struct earlycon_device
*device
,
469 if (!device
->port
.membase
)
472 device
->con
->write
= mps2_early_write
;
477 OF_EARLYCON_DECLARE(mps2
, "arm,mps2-uart", mps2_early_console_setup
);
480 #define MPS2_SERIAL_CONSOLE NULL
483 static struct uart_driver mps2_uart_driver
= {
484 .driver_name
= DRIVER_NAME
,
485 .dev_name
= SERIAL_NAME
,
486 .nr
= MPS2_MAX_PORTS
,
487 .cons
= MPS2_SERIAL_CONSOLE
,
490 static struct mps2_uart_port
*mps2_of_get_port(struct platform_device
*pdev
)
492 struct device_node
*np
= pdev
->dev
.of_node
;
498 id
= of_alias_get_id(np
, "serial");
502 if (WARN_ON(id
>= MPS2_MAX_PORTS
))
505 mps2_uart_ports
[id
].port
.line
= id
;
506 return &mps2_uart_ports
[id
];
509 static int mps2_init_port(struct mps2_uart_port
*mps_port
,
510 struct platform_device
*pdev
)
512 struct resource
*res
;
515 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
516 mps_port
->port
.membase
= devm_ioremap_resource(&pdev
->dev
, res
);
517 if (IS_ERR(mps_port
->port
.membase
))
518 return PTR_ERR(mps_port
->port
.membase
);
520 mps_port
->port
.mapbase
= res
->start
;
521 mps_port
->port
.mapsize
= resource_size(res
);
523 mps_port
->rx_irq
= platform_get_irq(pdev
, 0);
524 mps_port
->tx_irq
= platform_get_irq(pdev
, 1);
525 mps_port
->port
.irq
= platform_get_irq(pdev
, 2);
527 mps_port
->port
.iotype
= UPIO_MEM
;
528 mps_port
->port
.flags
= UPF_BOOT_AUTOCONF
;
529 mps_port
->port
.fifosize
= 1;
530 mps_port
->port
.ops
= &mps2_uart_pops
;
531 mps_port
->port
.dev
= &pdev
->dev
;
533 mps_port
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
534 if (IS_ERR(mps_port
->clk
))
535 return PTR_ERR(mps_port
->clk
);
537 ret
= clk_prepare_enable(mps_port
->clk
);
541 mps_port
->port
.uartclk
= clk_get_rate(mps_port
->clk
);
543 clk_disable_unprepare(mps_port
->clk
);
548 static int mps2_serial_probe(struct platform_device
*pdev
)
550 struct mps2_uart_port
*mps_port
;
553 mps_port
= mps2_of_get_port(pdev
);
557 ret
= mps2_init_port(mps_port
, pdev
);
561 ret
= uart_add_one_port(&mps2_uart_driver
, &mps_port
->port
);
565 platform_set_drvdata(pdev
, mps_port
);
571 static const struct of_device_id mps2_match
[] = {
572 { .compatible
= "arm,mps2-uart", },
577 static struct platform_driver mps2_serial_driver
= {
578 .probe
= mps2_serial_probe
,
582 .of_match_table
= of_match_ptr(mps2_match
),
583 .suppress_bind_attrs
= true,
587 static int __init
mps2_uart_init(void)
591 ret
= uart_register_driver(&mps2_uart_driver
);
595 ret
= platform_driver_register(&mps2_serial_driver
);
597 uart_unregister_driver(&mps2_uart_driver
);
601 arch_initcall(mps2_uart_init
);