4 * Copyright (C) 2015 ARM Limited
6 * Author: Vladimir Murzin <vladimir.murzin@arm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * TODO: support for SysRq
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/bitops.h>
18 #include <linux/clk.h>
19 #include <linux/console.h>
21 #include <linux/kernel.h>
22 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/serial_core.h>
26 #include <linux/tty_flip.h>
27 #include <linux/types.h>
29 #define SERIAL_NAME "ttyMPS"
30 #define DRIVER_NAME "mps2-uart"
31 #define MAKE_NAME(x) (DRIVER_NAME # x)
33 #define UARTn_DATA 0x00
35 #define UARTn_STATE 0x04
36 #define UARTn_STATE_TX_FULL BIT(0)
37 #define UARTn_STATE_RX_FULL BIT(1)
38 #define UARTn_STATE_TX_OVERRUN BIT(2)
39 #define UARTn_STATE_RX_OVERRUN BIT(3)
41 #define UARTn_CTRL 0x08
42 #define UARTn_CTRL_TX_ENABLE BIT(0)
43 #define UARTn_CTRL_RX_ENABLE BIT(1)
44 #define UARTn_CTRL_TX_INT_ENABLE BIT(2)
45 #define UARTn_CTRL_RX_INT_ENABLE BIT(3)
46 #define UARTn_CTRL_TX_OVERRUN_INT_ENABLE BIT(4)
47 #define UARTn_CTRL_RX_OVERRUN_INT_ENABLE BIT(5)
49 #define UARTn_INT 0x0c
50 #define UARTn_INT_TX BIT(0)
51 #define UARTn_INT_RX BIT(1)
52 #define UARTn_INT_TX_OVERRUN BIT(2)
53 #define UARTn_INT_RX_OVERRUN BIT(3)
55 #define UARTn_BAUDDIV 0x10
56 #define UARTn_BAUDDIV_MASK GENMASK(20, 0)
59 * Helpers to make typical enable/disable operations more readable.
61 #define UARTn_CTRL_TX_GRP (UARTn_CTRL_TX_ENABLE |\
62 UARTn_CTRL_TX_INT_ENABLE |\
63 UARTn_CTRL_TX_OVERRUN_INT_ENABLE)
65 #define UARTn_CTRL_RX_GRP (UARTn_CTRL_RX_ENABLE |\
66 UARTn_CTRL_RX_INT_ENABLE |\
67 UARTn_CTRL_RX_OVERRUN_INT_ENABLE)
69 #define MPS2_MAX_PORTS 3
71 struct mps2_uart_port
{
72 struct uart_port port
;
78 static inline struct mps2_uart_port
*to_mps2_port(struct uart_port
*port
)
80 return container_of(port
, struct mps2_uart_port
, port
);
83 static void mps2_uart_write8(struct uart_port
*port
, u8 val
, unsigned int off
)
85 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
87 writeb(val
, mps_port
->port
.membase
+ off
);
90 static u8
mps2_uart_read8(struct uart_port
*port
, unsigned int off
)
92 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
94 return readb(mps_port
->port
.membase
+ off
);
97 static void mps2_uart_write32(struct uart_port
*port
, u32 val
, unsigned int off
)
99 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
101 writel_relaxed(val
, mps_port
->port
.membase
+ off
);
104 static unsigned int mps2_uart_tx_empty(struct uart_port
*port
)
106 u8 status
= mps2_uart_read8(port
, UARTn_STATE
);
108 return (status
& UARTn_STATE_TX_FULL
) ? 0 : TIOCSER_TEMT
;
111 static void mps2_uart_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
115 static unsigned int mps2_uart_get_mctrl(struct uart_port
*port
)
117 return TIOCM_CAR
| TIOCM_CTS
| TIOCM_DSR
;
120 static void mps2_uart_stop_tx(struct uart_port
*port
)
122 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
124 control
&= ~UARTn_CTRL_TX_INT_ENABLE
;
126 mps2_uart_write8(port
, control
, UARTn_CTRL
);
129 static void mps2_uart_tx_chars(struct uart_port
*port
)
131 struct circ_buf
*xmit
= &port
->state
->xmit
;
133 while (!(mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_TX_FULL
)) {
135 mps2_uart_write8(port
, port
->x_char
, UARTn_DATA
);
141 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
144 mps2_uart_write8(port
, xmit
->buf
[xmit
->tail
], UARTn_DATA
);
145 xmit
->tail
= (xmit
->tail
+ 1) % UART_XMIT_SIZE
;
149 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
150 uart_write_wakeup(port
);
152 if (uart_circ_empty(xmit
))
153 mps2_uart_stop_tx(port
);
156 static void mps2_uart_start_tx(struct uart_port
*port
)
158 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
160 control
|= UARTn_CTRL_TX_INT_ENABLE
;
162 mps2_uart_write8(port
, control
, UARTn_CTRL
);
165 * We've just unmasked the TX IRQ and now slow-starting via
166 * polling; if there is enough data to fill up the internal
167 * write buffer in one go, the TX IRQ should assert, at which
168 * point we switch to fully interrupt-driven TX.
171 mps2_uart_tx_chars(port
);
174 static void mps2_uart_stop_rx(struct uart_port
*port
)
176 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
178 control
&= ~UARTn_CTRL_RX_GRP
;
180 mps2_uart_write8(port
, control
, UARTn_CTRL
);
183 static void mps2_uart_break_ctl(struct uart_port
*port
, int ctl
)
187 static void mps2_uart_rx_chars(struct uart_port
*port
)
189 struct tty_port
*tport
= &port
->state
->port
;
191 while (mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_RX_FULL
) {
192 u8 rxdata
= mps2_uart_read8(port
, UARTn_DATA
);
195 tty_insert_flip_char(&port
->state
->port
, rxdata
, TTY_NORMAL
);
198 tty_flip_buffer_push(tport
);
201 static irqreturn_t
mps2_uart_rxirq(int irq
, void *data
)
203 struct uart_port
*port
= data
;
204 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
206 if (unlikely(!(irqflag
& UARTn_INT_RX
)))
209 spin_lock(&port
->lock
);
211 mps2_uart_write8(port
, UARTn_INT_RX
, UARTn_INT
);
212 mps2_uart_rx_chars(port
);
214 spin_unlock(&port
->lock
);
219 static irqreturn_t
mps2_uart_txirq(int irq
, void *data
)
221 struct uart_port
*port
= data
;
222 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
224 if (unlikely(!(irqflag
& UARTn_INT_TX
)))
227 spin_lock(&port
->lock
);
229 mps2_uart_write8(port
, UARTn_INT_TX
, UARTn_INT
);
230 mps2_uart_tx_chars(port
);
232 spin_unlock(&port
->lock
);
237 static irqreturn_t
mps2_uart_oerrirq(int irq
, void *data
)
239 irqreturn_t handled
= IRQ_NONE
;
240 struct uart_port
*port
= data
;
241 u8 irqflag
= mps2_uart_read8(port
, UARTn_INT
);
243 spin_lock(&port
->lock
);
245 if (irqflag
& UARTn_INT_RX_OVERRUN
) {
246 struct tty_port
*tport
= &port
->state
->port
;
248 mps2_uart_write8(port
, UARTn_INT_RX_OVERRUN
, UARTn_INT
);
249 port
->icount
.overrun
++;
250 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
251 tty_flip_buffer_push(tport
);
252 handled
= IRQ_HANDLED
;
256 * It's never been seen in practice and it never *should* happen since
257 * we check if there is enough room in TX buffer before sending data.
258 * So we keep this check in case something suspicious has happened.
260 if (irqflag
& UARTn_INT_TX_OVERRUN
) {
261 mps2_uart_write8(port
, UARTn_INT_TX_OVERRUN
, UARTn_INT
);
262 handled
= IRQ_HANDLED
;
265 spin_unlock(&port
->lock
);
270 static int mps2_uart_startup(struct uart_port
*port
)
272 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
273 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
276 control
&= ~(UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
);
278 mps2_uart_write8(port
, control
, UARTn_CTRL
);
280 ret
= request_irq(mps_port
->rx_irq
, mps2_uart_rxirq
, 0,
281 MAKE_NAME(-rx
), mps_port
);
283 dev_err(port
->dev
, "failed to register rxirq (%d)\n", ret
);
287 ret
= request_irq(mps_port
->tx_irq
, mps2_uart_txirq
, 0,
288 MAKE_NAME(-tx
), mps_port
);
290 dev_err(port
->dev
, "failed to register txirq (%d)\n", ret
);
294 ret
= request_irq(port
->irq
, mps2_uart_oerrirq
, IRQF_SHARED
,
295 MAKE_NAME(-overrun
), mps_port
);
298 dev_err(port
->dev
, "failed to register oerrirq (%d)\n", ret
);
302 control
|= UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
;
304 mps2_uart_write8(port
, control
, UARTn_CTRL
);
309 free_irq(mps_port
->tx_irq
, mps_port
);
311 free_irq(mps_port
->rx_irq
, mps_port
);
316 static void mps2_uart_shutdown(struct uart_port
*port
)
318 struct mps2_uart_port
*mps_port
= to_mps2_port(port
);
319 u8 control
= mps2_uart_read8(port
, UARTn_CTRL
);
321 control
&= ~(UARTn_CTRL_RX_GRP
| UARTn_CTRL_TX_GRP
);
323 mps2_uart_write8(port
, control
, UARTn_CTRL
);
325 free_irq(mps_port
->rx_irq
, mps_port
);
326 free_irq(mps_port
->tx_irq
, mps_port
);
327 free_irq(port
->irq
, mps_port
);
331 mps2_uart_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
332 struct ktermios
*old
)
335 unsigned int baud
, bauddiv
;
337 termios
->c_cflag
&= ~(CRTSCTS
| CMSPAR
);
338 termios
->c_cflag
&= ~CSIZE
;
339 termios
->c_cflag
|= CS8
;
340 termios
->c_cflag
&= ~PARENB
;
341 termios
->c_cflag
&= ~CSTOPB
;
343 baud
= uart_get_baud_rate(port
, termios
, old
,
344 DIV_ROUND_CLOSEST(port
->uartclk
, UARTn_BAUDDIV_MASK
),
345 DIV_ROUND_CLOSEST(port
->uartclk
, 16));
347 bauddiv
= DIV_ROUND_CLOSEST(port
->uartclk
, baud
);
349 spin_lock_irqsave(&port
->lock
, flags
);
351 uart_update_timeout(port
, termios
->c_cflag
, baud
);
352 mps2_uart_write32(port
, bauddiv
, UARTn_BAUDDIV
);
354 spin_unlock_irqrestore(&port
->lock
, flags
);
356 if (tty_termios_baud_rate(termios
))
357 tty_termios_encode_baud_rate(termios
, baud
, baud
);
360 static const char *mps2_uart_type(struct uart_port
*port
)
362 return (port
->type
== PORT_MPS2UART
) ? DRIVER_NAME
: NULL
;
365 static void mps2_uart_release_port(struct uart_port
*port
)
369 static int mps2_uart_request_port(struct uart_port
*port
)
374 static void mps2_uart_config_port(struct uart_port
*port
, int type
)
376 if (type
& UART_CONFIG_TYPE
&& !mps2_uart_request_port(port
))
377 port
->type
= PORT_MPS2UART
;
380 static int mps2_uart_verify_port(struct uart_port
*port
, struct serial_struct
*serinfo
)
385 static const struct uart_ops mps2_uart_pops
= {
386 .tx_empty
= mps2_uart_tx_empty
,
387 .set_mctrl
= mps2_uart_set_mctrl
,
388 .get_mctrl
= mps2_uart_get_mctrl
,
389 .stop_tx
= mps2_uart_stop_tx
,
390 .start_tx
= mps2_uart_start_tx
,
391 .stop_rx
= mps2_uart_stop_rx
,
392 .break_ctl
= mps2_uart_break_ctl
,
393 .startup
= mps2_uart_startup
,
394 .shutdown
= mps2_uart_shutdown
,
395 .set_termios
= mps2_uart_set_termios
,
396 .type
= mps2_uart_type
,
397 .release_port
= mps2_uart_release_port
,
398 .request_port
= mps2_uart_request_port
,
399 .config_port
= mps2_uart_config_port
,
400 .verify_port
= mps2_uart_verify_port
,
403 static struct mps2_uart_port mps2_uart_ports
[MPS2_MAX_PORTS
];
405 #ifdef CONFIG_SERIAL_MPS2_UART_CONSOLE
406 static void mps2_uart_console_putchar(struct uart_port
*port
, int ch
)
408 while (mps2_uart_read8(port
, UARTn_STATE
) & UARTn_STATE_TX_FULL
)
411 mps2_uart_write8(port
, ch
, UARTn_DATA
);
414 static void mps2_uart_console_write(struct console
*co
, const char *s
, unsigned int cnt
)
416 struct uart_port
*port
= &mps2_uart_ports
[co
->index
].port
;
418 uart_console_write(port
, s
, cnt
, mps2_uart_console_putchar
);
421 static int mps2_uart_console_setup(struct console
*co
, char *options
)
423 struct mps2_uart_port
*mps_port
;
429 if (co
->index
< 0 || co
->index
>= MPS2_MAX_PORTS
)
432 mps_port
= &mps2_uart_ports
[co
->index
];
435 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
437 return uart_set_options(&mps_port
->port
, co
, baud
, parity
, bits
, flow
);
440 static struct uart_driver mps2_uart_driver
;
442 static struct console mps2_uart_console
= {
444 .device
= uart_console_device
,
445 .write
= mps2_uart_console_write
,
446 .setup
= mps2_uart_console_setup
,
447 .flags
= CON_PRINTBUFFER
,
449 .data
= &mps2_uart_driver
,
452 #define MPS2_SERIAL_CONSOLE (&mps2_uart_console)
454 static void mps2_early_putchar(struct uart_port
*port
, int ch
)
456 while (readb(port
->membase
+ UARTn_STATE
) & UARTn_STATE_TX_FULL
)
459 writeb((unsigned char)ch
, port
->membase
+ UARTn_DATA
);
462 static void mps2_early_write(struct console
*con
, const char *s
, unsigned int n
)
464 struct earlycon_device
*dev
= con
->data
;
466 uart_console_write(&dev
->port
, s
, n
, mps2_early_putchar
);
469 static int __init
mps2_early_console_setup(struct earlycon_device
*device
,
472 if (!device
->port
.membase
)
475 device
->con
->write
= mps2_early_write
;
480 OF_EARLYCON_DECLARE(mps2
, "arm,mps2-uart", mps2_early_console_setup
);
483 #define MPS2_SERIAL_CONSOLE NULL
486 static struct uart_driver mps2_uart_driver
= {
487 .driver_name
= DRIVER_NAME
,
488 .dev_name
= SERIAL_NAME
,
489 .nr
= MPS2_MAX_PORTS
,
490 .cons
= MPS2_SERIAL_CONSOLE
,
493 static struct mps2_uart_port
*mps2_of_get_port(struct platform_device
*pdev
)
495 struct device_node
*np
= pdev
->dev
.of_node
;
501 id
= of_alias_get_id(np
, "serial");
505 if (WARN_ON(id
>= MPS2_MAX_PORTS
))
508 mps2_uart_ports
[id
].port
.line
= id
;
509 return &mps2_uart_ports
[id
];
512 static int mps2_init_port(struct mps2_uart_port
*mps_port
,
513 struct platform_device
*pdev
)
515 struct resource
*res
;
518 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
519 mps_port
->port
.membase
= devm_ioremap_resource(&pdev
->dev
, res
);
520 if (IS_ERR(mps_port
->port
.membase
))
521 return PTR_ERR(mps_port
->port
.membase
);
523 mps_port
->port
.mapbase
= res
->start
;
524 mps_port
->port
.mapsize
= resource_size(res
);
526 mps_port
->rx_irq
= platform_get_irq(pdev
, 0);
527 mps_port
->tx_irq
= platform_get_irq(pdev
, 1);
528 mps_port
->port
.irq
= platform_get_irq(pdev
, 2);
530 mps_port
->port
.iotype
= UPIO_MEM
;
531 mps_port
->port
.flags
= UPF_BOOT_AUTOCONF
;
532 mps_port
->port
.fifosize
= 1;
533 mps_port
->port
.ops
= &mps2_uart_pops
;
534 mps_port
->port
.dev
= &pdev
->dev
;
536 mps_port
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
537 if (IS_ERR(mps_port
->clk
))
538 return PTR_ERR(mps_port
->clk
);
540 ret
= clk_prepare_enable(mps_port
->clk
);
544 mps_port
->port
.uartclk
= clk_get_rate(mps_port
->clk
);
546 clk_disable_unprepare(mps_port
->clk
);
551 static int mps2_serial_probe(struct platform_device
*pdev
)
553 struct mps2_uart_port
*mps_port
;
556 mps_port
= mps2_of_get_port(pdev
);
560 ret
= mps2_init_port(mps_port
, pdev
);
564 ret
= uart_add_one_port(&mps2_uart_driver
, &mps_port
->port
);
568 platform_set_drvdata(pdev
, mps_port
);
574 static const struct of_device_id mps2_match
[] = {
575 { .compatible
= "arm,mps2-uart", },
580 static struct platform_driver mps2_serial_driver
= {
581 .probe
= mps2_serial_probe
,
585 .of_match_table
= of_match_ptr(mps2_match
),
586 .suppress_bind_attrs
= true,
590 static int __init
mps2_uart_init(void)
594 ret
= uart_register_driver(&mps2_uart_driver
);
598 ret
= platform_driver_register(&mps2_serial_driver
);
600 uart_unregister_driver(&mps2_uart_driver
);
604 arch_initcall(mps2_uart_init
);