Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / tty / serial / mps2-uart.c
blob9f8f63719126df9fa3459a7bd398de400ccf1326
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * MPS2 UART driver
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>
17 #include <linux/io.h>
18 #include <linux/kernel.h>
19 #include <linux/of_device.h>
20 #include <linux/of.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;
70 struct clk *clk;
71 unsigned int tx_irq;
72 unsigned int rx_irq;
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)) {
131 if (port->x_char) {
132 mps2_uart_write8(port, port->x_char, UARTn_DATA);
133 port->x_char = 0;
134 port->icount.tx++;
135 continue;
138 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
139 break;
141 mps2_uart_write8(port, xmit->buf[xmit->tail], UARTn_DATA);
142 xmit->tail = (xmit->tail + 1) % UART_XMIT_SIZE;
143 port->icount.tx++;
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);
191 port->icount.rx++;
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)))
204 return IRQ_NONE;
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);
213 return IRQ_HANDLED;
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)))
222 return IRQ_NONE;
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);
231 return IRQ_HANDLED;
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);
264 return handled;
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);
271 int ret;
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);
279 if (ret) {
280 dev_err(port->dev, "failed to register rxirq (%d)\n", ret);
281 return ret;
284 ret = request_irq(mps_port->tx_irq, mps2_uart_txirq, 0,
285 MAKE_NAME(-tx), mps_port);
286 if (ret) {
287 dev_err(port->dev, "failed to register txirq (%d)\n", ret);
288 goto err_free_rxirq;
291 ret = request_irq(port->irq, mps2_uart_oerrirq, IRQF_SHARED,
292 MAKE_NAME(-overrun), mps_port);
294 if (ret) {
295 dev_err(port->dev, "failed to register oerrirq (%d)\n", ret);
296 goto err_free_txirq;
299 control |= UARTn_CTRL_RX_GRP | UARTn_CTRL_TX_GRP;
301 mps2_uart_write8(port, control, UARTn_CTRL);
303 return 0;
305 err_free_txirq:
306 free_irq(mps_port->tx_irq, mps_port);
307 err_free_rxirq:
308 free_irq(mps_port->rx_irq, mps_port);
310 return ret;
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);
327 static void
328 mps2_uart_set_termios(struct uart_port *port, struct ktermios *termios,
329 struct ktermios *old)
331 unsigned long flags;
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)
368 return 0;
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)
379 return -EINVAL;
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)
406 cpu_relax();
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;
421 int baud = 9600;
422 int bits = 8;
423 int parity = 'n';
424 int flow = 'n';
426 if (co->index < 0 || co->index >= MPS2_MAX_PORTS)
427 return -ENODEV;
429 mps_port = &mps2_uart_ports[co->index];
431 if (options)
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 = {
440 .name = SERIAL_NAME,
441 .device = uart_console_device,
442 .write = mps2_uart_console_write,
443 .setup = mps2_uart_console_setup,
444 .flags = CON_PRINTBUFFER,
445 .index = -1,
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)
454 cpu_relax();
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,
467 const char *opt)
469 if (!device->port.membase)
470 return -ENODEV;
472 device->con->write = mps2_early_write;
474 return 0;
477 OF_EARLYCON_DECLARE(mps2, "arm,mps2-uart", mps2_early_console_setup);
479 #else
480 #define MPS2_SERIAL_CONSOLE NULL
481 #endif
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;
493 int id;
495 if (!np)
496 return NULL;
498 id = of_alias_get_id(np, "serial");
499 if (id < 0)
500 id = 0;
502 if (WARN_ON(id >= MPS2_MAX_PORTS))
503 return NULL;
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;
513 int ret;
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);
538 if (ret)
539 return ret;
541 mps_port->port.uartclk = clk_get_rate(mps_port->clk);
543 clk_disable_unprepare(mps_port->clk);
545 return ret;
548 static int mps2_serial_probe(struct platform_device *pdev)
550 struct mps2_uart_port *mps_port;
551 int ret;
553 mps_port = mps2_of_get_port(pdev);
554 if (!mps_port)
555 return -ENODEV;
557 ret = mps2_init_port(mps_port, pdev);
558 if (ret)
559 return ret;
561 ret = uart_add_one_port(&mps2_uart_driver, &mps_port->port);
562 if (ret)
563 return ret;
565 platform_set_drvdata(pdev, mps_port);
567 return 0;
570 #ifdef CONFIG_OF
571 static const struct of_device_id mps2_match[] = {
572 { .compatible = "arm,mps2-uart", },
575 #endif
577 static struct platform_driver mps2_serial_driver = {
578 .probe = mps2_serial_probe,
580 .driver = {
581 .name = DRIVER_NAME,
582 .of_match_table = of_match_ptr(mps2_match),
583 .suppress_bind_attrs = true,
587 static int __init mps2_uart_init(void)
589 int ret;
591 ret = uart_register_driver(&mps2_uart_driver);
592 if (ret)
593 return ret;
595 ret = platform_driver_register(&mps2_serial_driver);
596 if (ret)
597 uart_unregister_driver(&mps2_uart_driver);
599 return ret;
601 arch_initcall(mps2_uart_init);