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>
25 #include <linux/idr.h>
27 #define SERIAL_NAME "ttyMPS"
28 #define DRIVER_NAME "mps2-uart"
29 #define MAKE_NAME(x) (DRIVER_NAME # x)
31 #define UARTn_DATA 0x00
33 #define UARTn_STATE 0x04
34 #define UARTn_STATE_TX_FULL BIT(0)
35 #define UARTn_STATE_RX_FULL BIT(1)
36 #define UARTn_STATE_TX_OVERRUN BIT(2)
37 #define UARTn_STATE_RX_OVERRUN BIT(3)
39 #define UARTn_CTRL 0x08
40 #define UARTn_CTRL_TX_ENABLE BIT(0)
41 #define UARTn_CTRL_RX_ENABLE BIT(1)
42 #define UARTn_CTRL_TX_INT_ENABLE BIT(2)
43 #define UARTn_CTRL_RX_INT_ENABLE BIT(3)
44 #define UARTn_CTRL_TX_OVERRUN_INT_ENABLE BIT(4)
45 #define UARTn_CTRL_RX_OVERRUN_INT_ENABLE BIT(5)
47 #define UARTn_INT 0x0c
48 #define UARTn_INT_TX BIT(0)
49 #define UARTn_INT_RX BIT(1)
50 #define UARTn_INT_TX_OVERRUN BIT(2)
51 #define UARTn_INT_RX_OVERRUN BIT(3)
53 #define UARTn_BAUDDIV 0x10
54 #define UARTn_BAUDDIV_MASK GENMASK(20, 0)
57 * Helpers to make typical enable/disable operations more readable.
59 #define UARTn_CTRL_TX_GRP (UARTn_CTRL_TX_ENABLE |\
60 UARTn_CTRL_TX_INT_ENABLE |\
61 UARTn_CTRL_TX_OVERRUN_INT_ENABLE)
63 #define UARTn_CTRL_RX_GRP (UARTn_CTRL_RX_ENABLE |\
64 UARTn_CTRL_RX_INT_ENABLE |\
65 UARTn_CTRL_RX_OVERRUN_INT_ENABLE)
67 #define MPS2_MAX_PORTS 3
69 #define UART_PORT_COMBINED_IRQ BIT(0)
71 struct mps2_uart_port
{
72 struct uart_port port
;
79 static inline struct mps2_uart_port
*to_mps2_port(struct uart_port
*port
)
81 return container_of(port
, struct mps2_uart_port
, port
);
84 static void mps2_uart_write8(struct uart_port
*port
, u8 val
, unsigned int off
)
86 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
88 writeb(val
, mps_port
->port
.membase
+ off
);
91 static u8
mps2_uart_read8(struct uart_port
*port
, unsigned int off
)
93 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
95 return readb(mps_port
->port
.membase
+ off
);
98 static void mps2_uart_write32(struct uart_port
*port
, u32 val
, unsigned int off
)
100 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
102 writel_relaxed(val
, mps_port
->port
.membase
+ off
);
105 static unsigned int mps2_uart_tx_empty(struct uart_port
*port
)
107 u8 status
= mps2_uart_read8(port
, UARTn_STATE
);
109 return (status
& UARTn_STATE_TX_FULL
) ? 0 : TIOCSER_TEMT
;
112 static void mps2_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
116 static unsigned int mps2_uart_get_mctrl(struct uart_port
*port
)
118 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
;
121 static void mps2_uart_stop_tx(struct uart_port
*port
)
123 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
125 control
&= ~UARTn_CTRL_TX_INT_ENABLE
;
127 mps2_uart_write8(port
, control
, UARTn_CTRL
);
130 static void mps2_uart_tx_chars(struct uart_port
*port
)
132 struct circ_buf
*xmit
= &port
->state
->xmit
;
134 while (!(mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_TX_FULL
)) {
136 mps2_uart_write8(port
, port
->x_char
, UARTn_DATA
);
142 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
145 mps2_uart_write8(port
, xmit
->buf
[xmit
->tail
], UARTn_DATA
);
146 xmit
->tail
= (xmit
->tail
+ 1) % UART_XMIT_SIZE
;
150 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
151 uart_write_wakeup(port
);
153 if (uart_circ_empty(xmit
))
154 mps2_uart_stop_tx(port
);
157 static void mps2_uart_start_tx(struct uart_port
*port
)
159 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
161 control
|= UARTn_CTRL_TX_INT_ENABLE
;
163 mps2_uart_write8(port
, control
, UARTn_CTRL
);
166 * We've just unmasked the TX IRQ and now slow-starting via
167 * polling; if there is enough data to fill up the internal
168 * write buffer in one go, the TX IRQ should assert, at which
169 * point we switch to fully interrupt-driven TX.
172 mps2_uart_tx_chars(port
);
175 static void mps2_uart_stop_rx(struct uart_port
*port
)
177 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
179 control
&= ~UARTn_CTRL_RX_GRP
;
181 mps2_uart_write8(port
, control
, UARTn_CTRL
);
184 static void mps2_uart_break_ctl(struct uart_port
*port
, int ctl
)
188 static void mps2_uart_rx_chars(struct uart_port
*port
)
190 struct tty_port
*tport
= &port
->state
->port
;
192 while (mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_RX_FULL
) {
193 u8 rxdata
= mps2_uart_read8(port
, UARTn_DATA
);
196 tty_insert_flip_char(&port
->state
->port
, rxdata
, TTY_NORMAL
);
199 tty_flip_buffer_push(tport
);
202 static irqreturn_t
mps2_uart_rxirq(int irq
, void *data
)
204 struct uart_port
*port
= data
;
205 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
207 if (unlikely(!(irqflag
& UARTn_INT_RX
)))
210 spin_lock(&port
->lock
);
212 mps2_uart_write8(port
, UARTn_INT_RX
, UARTn_INT
);
213 mps2_uart_rx_chars(port
);
215 spin_unlock(&port
->lock
);
220 static irqreturn_t
mps2_uart_txirq(int irq
, void *data
)
222 struct uart_port
*port
= data
;
223 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
225 if (unlikely(!(irqflag
& UARTn_INT_TX
)))
228 spin_lock(&port
->lock
);
230 mps2_uart_write8(port
, UARTn_INT_TX
, UARTn_INT
);
231 mps2_uart_tx_chars(port
);
233 spin_unlock(&port
->lock
);
238 static irqreturn_t
mps2_uart_oerrirq(int irq
, void *data
)
240 irqreturn_t handled
= IRQ_NONE
;
241 struct uart_port
*port
= data
;
242 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
244 spin_lock(&port
->lock
);
246 if (irqflag
& UARTn_INT_RX_OVERRUN
) {
247 struct tty_port
*tport
= &port
->state
->port
;
249 mps2_uart_write8(port
, UARTn_INT_RX_OVERRUN
, UARTn_INT
);
250 port
->icount
.overrun
++;
251 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
252 tty_flip_buffer_push(tport
);
253 handled
= IRQ_HANDLED
;
257 * It's never been seen in practice and it never *should* happen since
258 * we check if there is enough room in TX buffer before sending data.
259 * So we keep this check in case something suspicious has happened.
261 if (irqflag
& UARTn_INT_TX_OVERRUN
) {
262 mps2_uart_write8(port
, UARTn_INT_TX_OVERRUN
, UARTn_INT
);
263 handled
= IRQ_HANDLED
;
266 spin_unlock(&port
->lock
);
271 static irqreturn_t
mps2_uart_combinedirq(int irq
, void *data
)
273 if (mps2_uart_rxirq(irq
, data
) == IRQ_HANDLED
)
276 if (mps2_uart_txirq(irq
, data
) == IRQ_HANDLED
)
279 if (mps2_uart_oerrirq(irq
, data
) == IRQ_HANDLED
)
285 static int mps2_uart_startup(struct uart_port
*port
)
287 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
288 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
291 control
&= ~(UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
);
293 mps2_uart_write8(port
, control
, UARTn_CTRL
);
295 if (mps_port
->flags
& UART_PORT_COMBINED_IRQ
) {
296 ret
= request_irq(port
->irq
, mps2_uart_combinedirq
, 0,
297 MAKE_NAME(-combined
), mps_port
);
300 dev_err(port
->dev
, "failed to register combinedirq (%d)\n", ret
);
304 ret
= request_irq(port
->irq
, mps2_uart_oerrirq
, IRQF_SHARED
,
305 MAKE_NAME(-overrun
), mps_port
);
308 dev_err(port
->dev
, "failed to register oerrirq (%d)\n", ret
);
312 ret
= request_irq(mps_port
->rx_irq
, mps2_uart_rxirq
, 0,
313 MAKE_NAME(-rx
), mps_port
);
315 dev_err(port
->dev
, "failed to register rxirq (%d)\n", ret
);
316 goto err_free_oerrirq
;
319 ret
= request_irq(mps_port
->tx_irq
, mps2_uart_txirq
, 0,
320 MAKE_NAME(-tx
), mps_port
);
322 dev_err(port
->dev
, "failed to register txirq (%d)\n", ret
);
328 control
|= UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
;
330 mps2_uart_write8(port
, control
, UARTn_CTRL
);
335 free_irq(mps_port
->rx_irq
, mps_port
);
337 free_irq(port
->irq
, mps_port
);
342 static void mps2_uart_shutdown(struct uart_port
*port
)
344 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
345 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
347 control
&= ~(UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
);
349 mps2_uart_write8(port
, control
, UARTn_CTRL
);
351 if (!(mps_port
->flags
& UART_PORT_COMBINED_IRQ
)) {
352 free_irq(mps_port
->rx_irq
, mps_port
);
353 free_irq(mps_port
->tx_irq
, mps_port
);
356 free_irq(port
->irq
, mps_port
);
360 mps2_uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
361 struct ktermios
*old
)
364 unsigned int baud
, bauddiv
;
366 termios
->c_cflag
&= ~(CRTSCTS
| CMSPAR
);
367 termios
->c_cflag
&= ~CSIZE
;
368 termios
->c_cflag
|= CS8
;
369 termios
->c_cflag
&= ~PARENB
;
370 termios
->c_cflag
&= ~CSTOPB
;
372 baud
= uart_get_baud_rate(port
, termios
, old
,
373 DIV_ROUND_CLOSEST(port
->uartclk
, UARTn_BAUDDIV_MASK
),
374 DIV_ROUND_CLOSEST(port
->uartclk
, 16));
376 bauddiv
= DIV_ROUND_CLOSEST(port
->uartclk
, baud
);
378 spin_lock_irqsave(&port
->lock
, flags
);
380 uart_update_timeout(port
, termios
->c_cflag
, baud
);
381 mps2_uart_write32(port
, bauddiv
, UARTn_BAUDDIV
);
383 spin_unlock_irqrestore(&port
->lock
, flags
);
385 if (tty_termios_baud_rate(termios
))
386 tty_termios_encode_baud_rate(termios
, baud
, baud
);
389 static const char *mps2_uart_type(struct uart_port
*port
)
391 return (port
->type
== PORT_MPS2UART
) ? DRIVER_NAME
: NULL
;
394 static void mps2_uart_release_port(struct uart_port
*port
)
398 static int mps2_uart_request_port(struct uart_port
*port
)
403 static void mps2_uart_config_port(struct uart_port
*port
, int type
)
405 if (type
& UART_CONFIG_TYPE
&& !mps2_uart_request_port(port
))
406 port
->type
= PORT_MPS2UART
;
409 static int mps2_uart_verify_port(struct uart_port
*port
, struct serial_struct
*serinfo
)
414 static const struct uart_ops mps2_uart_pops
= {
415 .tx_empty
= mps2_uart_tx_empty
,
416 .set_mctrl
= mps2_uart_set_mctrl
,
417 .get_mctrl
= mps2_uart_get_mctrl
,
418 .stop_tx
= mps2_uart_stop_tx
,
419 .start_tx
= mps2_uart_start_tx
,
420 .stop_rx
= mps2_uart_stop_rx
,
421 .break_ctl
= mps2_uart_break_ctl
,
422 .startup
= mps2_uart_startup
,
423 .shutdown
= mps2_uart_shutdown
,
424 .set_termios
= mps2_uart_set_termios
,
425 .type
= mps2_uart_type
,
426 .release_port
= mps2_uart_release_port
,
427 .request_port
= mps2_uart_request_port
,
428 .config_port
= mps2_uart_config_port
,
429 .verify_port
= mps2_uart_verify_port
,
432 static DEFINE_IDR(ports_idr
);
434 #ifdef CONFIG_SERIAL_MPS2_UART_CONSOLE
435 static void mps2_uart_console_putchar(struct uart_port
*port
, int ch
)
437 while (mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_TX_FULL
)
440 mps2_uart_write8(port
, ch
, UARTn_DATA
);
443 static void mps2_uart_console_write(struct console
*co
, const char *s
, unsigned int cnt
)
445 struct mps2_uart_port
*mps_port
= idr_find(&ports_idr
, co
->index
);
446 struct uart_port
*port
= &mps_port
->port
;
448 uart_console_write(port
, s
, cnt
, mps2_uart_console_putchar
);
451 static int mps2_uart_console_setup(struct console
*co
, char *options
)
453 struct mps2_uart_port
*mps_port
;
459 if (co
->index
< 0 || co
->index
>= MPS2_MAX_PORTS
)
462 mps_port
= idr_find(&ports_idr
, co
->index
);
468 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
470 return uart_set_options(&mps_port
->port
, co
, baud
, parity
, bits
, flow
);
473 static struct uart_driver mps2_uart_driver
;
475 static struct console mps2_uart_console
= {
477 .device
= uart_console_device
,
478 .write
= mps2_uart_console_write
,
479 .setup
= mps2_uart_console_setup
,
480 .flags
= CON_PRINTBUFFER
,
482 .data
= &mps2_uart_driver
,
485 #define MPS2_SERIAL_CONSOLE (&mps2_uart_console)
487 static void mps2_early_putchar(struct uart_port
*port
, int ch
)
489 while (readb(port
->membase
+ UARTn_STATE
) & UARTn_STATE_TX_FULL
)
492 writeb((unsigned char)ch
, port
->membase
+ UARTn_DATA
);
495 static void mps2_early_write(struct console
*con
, const char *s
, unsigned int n
)
497 struct earlycon_device
*dev
= con
->data
;
499 uart_console_write(&dev
->port
, s
, n
, mps2_early_putchar
);
502 static int __init
mps2_early_console_setup(struct earlycon_device
*device
,
505 if (!device
->port
.membase
)
508 device
->con
->write
= mps2_early_write
;
513 OF_EARLYCON_DECLARE(mps2
, "arm,mps2-uart", mps2_early_console_setup
);
516 #define MPS2_SERIAL_CONSOLE NULL
519 static struct uart_driver mps2_uart_driver
= {
520 .driver_name
= DRIVER_NAME
,
521 .dev_name
= SERIAL_NAME
,
522 .nr
= MPS2_MAX_PORTS
,
523 .cons
= MPS2_SERIAL_CONSOLE
,
526 static int mps2_of_get_port(struct platform_device
*pdev
,
527 struct mps2_uart_port
*mps_port
)
529 struct device_node
*np
= pdev
->dev
.of_node
;
535 id
= of_alias_get_id(np
, "serial");
538 id
= idr_alloc_cyclic(&ports_idr
, (void *)mps_port
, 0, MPS2_MAX_PORTS
, GFP_KERNEL
);
540 id
= idr_alloc(&ports_idr
, (void *)mps_port
, id
, MPS2_MAX_PORTS
, GFP_KERNEL
);
545 /* Only combined irq is presesnt */
546 if (platform_irq_count(pdev
) == 1)
547 mps_port
->flags
|= UART_PORT_COMBINED_IRQ
;
549 mps_port
->port
.line
= id
;
554 static int mps2_init_port(struct platform_device
*pdev
,
555 struct mps2_uart_port
*mps_port
)
557 struct resource
*res
;
560 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
561 mps_port
->port
.membase
= devm_ioremap_resource(&pdev
->dev
, res
);
562 if (IS_ERR(mps_port
->port
.membase
))
563 return PTR_ERR(mps_port
->port
.membase
);
565 mps_port
->port
.mapbase
= res
->start
;
566 mps_port
->port
.mapsize
= resource_size(res
);
567 mps_port
->port
.iotype
= UPIO_MEM
;
568 mps_port
->port
.flags
= UPF_BOOT_AUTOCONF
;
569 mps_port
->port
.fifosize
= 1;
570 mps_port
->port
.ops
= &mps2_uart_pops
;
571 mps_port
->port
.dev
= &pdev
->dev
;
573 mps_port
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
574 if (IS_ERR(mps_port
->clk
))
575 return PTR_ERR(mps_port
->clk
);
577 ret
= clk_prepare_enable(mps_port
->clk
);
581 mps_port
->port
.uartclk
= clk_get_rate(mps_port
->clk
);
583 clk_disable_unprepare(mps_port
->clk
);
586 if (mps_port
->flags
& UART_PORT_COMBINED_IRQ
) {
587 mps_port
->port
.irq
= platform_get_irq(pdev
, 0);
589 mps_port
->rx_irq
= platform_get_irq(pdev
, 0);
590 mps_port
->tx_irq
= platform_get_irq(pdev
, 1);
591 mps_port
->port
.irq
= platform_get_irq(pdev
, 2);
597 static int mps2_serial_probe(struct platform_device
*pdev
)
599 struct mps2_uart_port
*mps_port
;
602 mps_port
= devm_kzalloc(&pdev
->dev
, sizeof(struct mps2_uart_port
), GFP_KERNEL
);
607 ret
= mps2_of_get_port(pdev
, mps_port
);
611 ret
= mps2_init_port(pdev
, mps_port
);
615 ret
= uart_add_one_port(&mps2_uart_driver
, &mps_port
->port
);
619 platform_set_drvdata(pdev
, mps_port
);
625 static const struct of_device_id mps2_match
[] = {
626 { .compatible
= "arm,mps2-uart", },
631 static struct platform_driver mps2_serial_driver
= {
632 .probe
= mps2_serial_probe
,
636 .of_match_table
= of_match_ptr(mps2_match
),
637 .suppress_bind_attrs
= true,
641 static int __init
mps2_uart_init(void)
645 ret
= uart_register_driver(&mps2_uart_driver
);
649 ret
= platform_driver_register(&mps2_serial_driver
);
651 uart_unregister_driver(&mps2_uart_driver
);
655 arch_initcall(mps2_uart_init
);