1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Socionext Inc.
6 #if defined(CONFIG_SERIAL_MILBEAUT_USIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
10 #include <linux/clk.h>
11 #include <linux/console.h>
12 #include <linux/module.h>
13 #include <linux/of_irq.h>
14 #include <linux/platform_device.h>
15 #include <linux/serial_core.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
19 #define USIO_NAME "mlb-usio-uart"
20 #define USIO_UART_DEV_NAME "ttyUSI"
22 static struct uart_port mlb_usio_ports[CONFIG_SERIAL_MILBEAUT_USIO_PORTS];
26 static int mlb_usio_irq[CONFIG_SERIAL_MILBEAUT_USIO_PORTS][2];
28 #define MLB_USIO_REG_SMR 0
29 #define MLB_USIO_REG_SCR 1
30 #define MLB_USIO_REG_ESCR 2
31 #define MLB_USIO_REG_SSR 3
32 #define MLB_USIO_REG_DR 4
33 #define MLB_USIO_REG_BGR 6
34 #define MLB_USIO_REG_FCR 12
35 #define MLB_USIO_REG_FBYTE 14
37 #define MLB_USIO_SMR_SOE BIT(0)
38 #define MLB_USIO_SMR_SBL BIT(3)
39 #define MLB_USIO_SCR_TXE BIT(0)
40 #define MLB_USIO_SCR_RXE BIT(1)
41 #define MLB_USIO_SCR_TBIE BIT(2)
42 #define MLB_USIO_SCR_TIE BIT(3)
43 #define MLB_USIO_SCR_RIE BIT(4)
44 #define MLB_USIO_SCR_UPCL BIT(7)
45 #define MLB_USIO_ESCR_L_8BIT 0
46 #define MLB_USIO_ESCR_L_5BIT 1
47 #define MLB_USIO_ESCR_L_6BIT 2
48 #define MLB_USIO_ESCR_L_7BIT 3
49 #define MLB_USIO_ESCR_P BIT(3)
50 #define MLB_USIO_ESCR_PEN BIT(4)
51 #define MLB_USIO_ESCR_FLWEN BIT(7)
52 #define MLB_USIO_SSR_TBI BIT(0)
53 #define MLB_USIO_SSR_TDRE BIT(1)
54 #define MLB_USIO_SSR_RDRF BIT(2)
55 #define MLB_USIO_SSR_ORE BIT(3)
56 #define MLB_USIO_SSR_FRE BIT(4)
57 #define MLB_USIO_SSR_PE BIT(5)
58 #define MLB_USIO_SSR_REC BIT(7)
59 #define MLB_USIO_SSR_BRK BIT(8)
60 #define MLB_USIO_FCR_FE1 BIT(0)
61 #define MLB_USIO_FCR_FE2 BIT(1)
62 #define MLB_USIO_FCR_FCL1 BIT(2)
63 #define MLB_USIO_FCR_FCL2 BIT(3)
64 #define MLB_USIO_FCR_FSET BIT(4)
65 #define MLB_USIO_FCR_FTIE BIT(9)
66 #define MLB_USIO_FCR_FDRQ BIT(10)
67 #define MLB_USIO_FCR_FRIIE BIT(11)
69 static void mlb_usio_stop_tx(struct uart_port *port)
71 writew(readw(port->membase + MLB_USIO_REG_FCR) & ~MLB_USIO_FCR_FTIE,
72 port->membase + MLB_USIO_REG_FCR);
73 writeb(readb(port->membase + MLB_USIO_REG_SCR) & ~MLB_USIO_SCR_TBIE,
74 port->membase + MLB_USIO_REG_SCR);
77 static void mlb_usio_tx_chars(struct uart_port *port)
79 struct circ_buf *xmit = &port->state->xmit;
82 writew(readw(port->membase + MLB_USIO_REG_FCR) & ~MLB_USIO_FCR_FTIE,
83 port->membase + MLB_USIO_REG_FCR);
84 writeb(readb(port->membase + MLB_USIO_REG_SCR) &
85 ~(MLB_USIO_SCR_TIE | MLB_USIO_SCR_TBIE),
86 port->membase + MLB_USIO_REG_SCR);
89 writew(port->x_char, port->membase + MLB_USIO_REG_DR);
94 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
95 mlb_usio_stop_tx(port);
99 count = port->fifosize -
100 (readw(port->membase + MLB_USIO_REG_FBYTE) & 0xff);
103 writew(xmit->buf[xmit->tail], port->membase + MLB_USIO_REG_DR);
105 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
107 if (uart_circ_empty(xmit))
110 } while (--count > 0);
112 writew(readw(port->membase + MLB_USIO_REG_FCR) & ~MLB_USIO_FCR_FDRQ,
113 port->membase + MLB_USIO_REG_FCR);
115 writeb(readb(port->membase + MLB_USIO_REG_SCR) | MLB_USIO_SCR_TBIE,
116 port->membase + MLB_USIO_REG_SCR);
118 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
119 uart_write_wakeup(port);
121 if (uart_circ_empty(xmit))
122 mlb_usio_stop_tx(port);
125 static void mlb_usio_start_tx(struct uart_port *port)
127 u16 fcr = readw(port->membase + MLB_USIO_REG_FCR);
129 writew(fcr | MLB_USIO_FCR_FTIE, port->membase + MLB_USIO_REG_FCR);
130 if (!(fcr & MLB_USIO_FCR_FDRQ))
133 writeb(readb(port->membase + MLB_USIO_REG_SCR) | MLB_USIO_SCR_TBIE,
134 port->membase + MLB_USIO_REG_SCR);
136 if (readb(port->membase + MLB_USIO_REG_SSR) & MLB_USIO_SSR_TBI)
137 mlb_usio_tx_chars(port);
140 static void mlb_usio_stop_rx(struct uart_port *port)
142 writeb(readb(port->membase + MLB_USIO_REG_SCR) & ~MLB_USIO_SCR_RIE,
143 port->membase + MLB_USIO_REG_SCR);
146 static void mlb_usio_enable_ms(struct uart_port *port)
148 writeb(readb(port->membase + MLB_USIO_REG_SCR) |
149 MLB_USIO_SCR_RIE | MLB_USIO_SCR_RXE,
150 port->membase + MLB_USIO_REG_SCR);
153 static void mlb_usio_rx_chars(struct uart_port *port)
155 struct tty_port *ttyport = &port->state->port;
156 unsigned long flag = 0;
161 while (max_count--) {
162 status = readb(port->membase + MLB_USIO_REG_SSR);
164 if (!(status & MLB_USIO_SSR_RDRF))
167 if (!(status & (MLB_USIO_SSR_ORE | MLB_USIO_SSR_FRE |
169 ch = readw(port->membase + MLB_USIO_REG_DR);
172 if (uart_handle_sysrq_char(port, ch))
174 uart_insert_char(port, status, MLB_USIO_SSR_ORE,
178 if (status & MLB_USIO_SSR_PE)
179 port->icount.parity++;
180 if (status & MLB_USIO_SSR_ORE)
181 port->icount.overrun++;
182 status &= port->read_status_mask;
183 if (status & MLB_USIO_SSR_BRK) {
187 if (status & MLB_USIO_SSR_PE) {
191 if (status & MLB_USIO_SSR_FRE) {
196 uart_insert_char(port, status, MLB_USIO_SSR_ORE,
199 writeb(readb(port->membase + MLB_USIO_REG_SSR) |
201 port->membase + MLB_USIO_REG_SSR);
203 max_count = readw(port->membase + MLB_USIO_REG_FBYTE) >> 8;
204 writew(readw(port->membase + MLB_USIO_REG_FCR) |
205 MLB_USIO_FCR_FE2 | MLB_USIO_FCR_FRIIE,
206 port->membase + MLB_USIO_REG_FCR);
209 tty_flip_buffer_push(ttyport);
212 static irqreturn_t mlb_usio_rx_irq(int irq, void *dev_id)
214 struct uart_port *port = dev_id;
216 spin_lock(&port->lock);
217 mlb_usio_rx_chars(port);
218 spin_unlock(&port->lock);
223 static irqreturn_t mlb_usio_tx_irq(int irq, void *dev_id)
225 struct uart_port *port = dev_id;
227 spin_lock(&port->lock);
228 if (readb(port->membase + MLB_USIO_REG_SSR) & MLB_USIO_SSR_TBI)
229 mlb_usio_tx_chars(port);
230 spin_unlock(&port->lock);
235 static unsigned int mlb_usio_tx_empty(struct uart_port *port)
237 return (readb(port->membase + MLB_USIO_REG_SSR) & MLB_USIO_SSR_TBI) ?
241 static void mlb_usio_set_mctrl(struct uart_port *port, unsigned int mctrl)
245 static unsigned int mlb_usio_get_mctrl(struct uart_port *port)
247 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
251 static void mlb_usio_break_ctl(struct uart_port *port, int break_state)
255 static int mlb_usio_startup(struct uart_port *port)
257 const char *portname = to_platform_device(port->dev)->name;
259 int ret, index = port->line;
262 ret = request_irq(mlb_usio_irq[index][RX], mlb_usio_rx_irq,
266 ret = request_irq(mlb_usio_irq[index][TX], mlb_usio_tx_irq,
269 free_irq(mlb_usio_irq[index][RX], port);
273 escr = readb(port->membase + MLB_USIO_REG_ESCR);
274 if (of_property_read_bool(port->dev->of_node, "auto-flow-control"))
275 escr |= MLB_USIO_ESCR_FLWEN;
276 spin_lock_irqsave(&port->lock, flags);
277 writeb(0, port->membase + MLB_USIO_REG_SCR);
278 writeb(escr, port->membase + MLB_USIO_REG_ESCR);
279 writeb(MLB_USIO_SCR_UPCL, port->membase + MLB_USIO_REG_SCR);
280 writeb(MLB_USIO_SSR_REC, port->membase + MLB_USIO_REG_SSR);
281 writew(0, port->membase + MLB_USIO_REG_FCR);
282 writew(MLB_USIO_FCR_FCL1 | MLB_USIO_FCR_FCL2,
283 port->membase + MLB_USIO_REG_FCR);
284 writew(MLB_USIO_FCR_FE1 | MLB_USIO_FCR_FE2 | MLB_USIO_FCR_FRIIE,
285 port->membase + MLB_USIO_REG_FCR);
286 writew(0, port->membase + MLB_USIO_REG_FBYTE);
287 writew(BIT(12), port->membase + MLB_USIO_REG_FBYTE);
289 writeb(MLB_USIO_SCR_TXE | MLB_USIO_SCR_RIE | MLB_USIO_SCR_TBIE |
290 MLB_USIO_SCR_RXE, port->membase + MLB_USIO_REG_SCR);
291 spin_unlock_irqrestore(&port->lock, flags);
296 static void mlb_usio_shutdown(struct uart_port *port)
298 int index = port->line;
300 free_irq(mlb_usio_irq[index][RX], port);
301 free_irq(mlb_usio_irq[index][TX], port);
304 static void mlb_usio_set_termios(struct uart_port *port,
305 struct ktermios *termios, struct ktermios *old)
307 unsigned int escr, smr = MLB_USIO_SMR_SOE;
308 unsigned long flags, baud, quot;
310 switch (termios->c_cflag & CSIZE) {
312 escr = MLB_USIO_ESCR_L_5BIT;
315 escr = MLB_USIO_ESCR_L_6BIT;
318 escr = MLB_USIO_ESCR_L_7BIT;
322 escr = MLB_USIO_ESCR_L_8BIT;
326 if (termios->c_cflag & CSTOPB)
327 smr |= MLB_USIO_SMR_SBL;
329 if (termios->c_cflag & PARENB) {
330 escr |= MLB_USIO_ESCR_PEN;
331 if (termios->c_cflag & PARODD)
332 escr |= MLB_USIO_ESCR_P;
334 /* Set hard flow control */
335 if (of_property_read_bool(port->dev->of_node, "auto-flow-control") ||
336 (termios->c_cflag & CRTSCTS))
337 escr |= MLB_USIO_ESCR_FLWEN;
339 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
341 quot = port->uartclk / baud - 1;
345 spin_lock_irqsave(&port->lock, flags);
346 uart_update_timeout(port, termios->c_cflag, baud);
347 port->read_status_mask = MLB_USIO_SSR_ORE | MLB_USIO_SSR_RDRF |
349 if (termios->c_iflag & INPCK)
350 port->read_status_mask |= MLB_USIO_SSR_FRE | MLB_USIO_SSR_PE;
352 port->ignore_status_mask = 0;
353 if (termios->c_iflag & IGNPAR)
354 port->ignore_status_mask |= MLB_USIO_SSR_FRE | MLB_USIO_SSR_PE;
355 if ((termios->c_iflag & IGNBRK) && (termios->c_iflag & IGNPAR))
356 port->ignore_status_mask |= MLB_USIO_SSR_ORE;
357 if ((termios->c_cflag & CREAD) == 0)
358 port->ignore_status_mask |= MLB_USIO_SSR_RDRF;
360 writeb(0, port->membase + MLB_USIO_REG_SCR);
361 writeb(MLB_USIO_SCR_UPCL, port->membase + MLB_USIO_REG_SCR);
362 writeb(MLB_USIO_SSR_REC, port->membase + MLB_USIO_REG_SSR);
363 writew(0, port->membase + MLB_USIO_REG_FCR);
364 writeb(smr, port->membase + MLB_USIO_REG_SMR);
365 writeb(escr, port->membase + MLB_USIO_REG_ESCR);
366 writew(quot, port->membase + MLB_USIO_REG_BGR);
367 writew(0, port->membase + MLB_USIO_REG_FCR);
368 writew(MLB_USIO_FCR_FCL1 | MLB_USIO_FCR_FCL2 | MLB_USIO_FCR_FE1 |
369 MLB_USIO_FCR_FE2 | MLB_USIO_FCR_FRIIE,
370 port->membase + MLB_USIO_REG_FCR);
371 writew(0, port->membase + MLB_USIO_REG_FBYTE);
372 writew(BIT(12), port->membase + MLB_USIO_REG_FBYTE);
373 writeb(MLB_USIO_SCR_RIE | MLB_USIO_SCR_RXE | MLB_USIO_SCR_TBIE |
374 MLB_USIO_SCR_TXE, port->membase + MLB_USIO_REG_SCR);
375 spin_unlock_irqrestore(&port->lock, flags);
378 static const char *mlb_usio_type(struct uart_port *port)
380 return ((port->type == PORT_MLB_USIO) ? USIO_NAME : NULL);
383 static void mlb_usio_config_port(struct uart_port *port, int flags)
385 if (flags & UART_CONFIG_TYPE)
386 port->type = PORT_MLB_USIO;
389 static const struct uart_ops mlb_usio_ops = {
390 .tx_empty = mlb_usio_tx_empty,
391 .set_mctrl = mlb_usio_set_mctrl,
392 .get_mctrl = mlb_usio_get_mctrl,
393 .stop_tx = mlb_usio_stop_tx,
394 .start_tx = mlb_usio_start_tx,
395 .stop_rx = mlb_usio_stop_rx,
396 .enable_ms = mlb_usio_enable_ms,
397 .break_ctl = mlb_usio_break_ctl,
398 .startup = mlb_usio_startup,
399 .shutdown = mlb_usio_shutdown,
400 .set_termios = mlb_usio_set_termios,
401 .type = mlb_usio_type,
402 .config_port = mlb_usio_config_port,
405 #ifdef CONFIG_SERIAL_MILBEAUT_USIO_CONSOLE
407 static void mlb_usio_console_putchar(struct uart_port *port, int c)
409 while (!(readb(port->membase + MLB_USIO_REG_SSR) & MLB_USIO_SSR_TDRE))
412 writew(c, port->membase + MLB_USIO_REG_DR);
415 static void mlb_usio_console_write(struct console *co, const char *s,
418 struct uart_port *port = &mlb_usio_ports[co->index];
420 uart_console_write(port, s, count, mlb_usio_console_putchar);
423 static int __init mlb_usio_console_setup(struct console *co, char *options)
425 struct uart_port *port;
431 if (co->index >= CONFIG_SERIAL_MILBEAUT_USIO_PORTS)
434 port = &mlb_usio_ports[co->index];
440 uart_parse_options(options, &baud, &parity, &bits, &flow);
442 if (of_property_read_bool(port->dev->of_node, "auto-flow-control"))
445 return uart_set_options(port, co, baud, parity, bits, flow);
449 static struct uart_driver mlb_usio_uart_driver;
450 static struct console mlb_usio_console = {
451 .name = USIO_UART_DEV_NAME,
452 .write = mlb_usio_console_write,
453 .device = uart_console_device,
454 .setup = mlb_usio_console_setup,
455 .flags = CON_PRINTBUFFER,
457 .data = &mlb_usio_uart_driver,
460 static int __init mlb_usio_console_init(void)
462 register_console(&mlb_usio_console);
465 console_initcall(mlb_usio_console_init);
468 static void mlb_usio_early_console_write(struct console *co, const char *s,
471 struct earlycon_device *dev = co->data;
473 uart_console_write(&dev->port, s, count, mlb_usio_console_putchar);
476 static int __init mlb_usio_early_console_setup(struct earlycon_device *device,
479 if (!device->port.membase)
481 device->con->write = mlb_usio_early_console_write;
485 OF_EARLYCON_DECLARE(mlb_usio, "socionext,milbeaut-usio-uart",
486 mlb_usio_early_console_setup);
488 #define USIO_CONSOLE (&mlb_usio_console)
490 #define USIO_CONSOLE NULL
493 static struct uart_driver mlb_usio_uart_driver = {
494 .owner = THIS_MODULE,
495 .driver_name = USIO_NAME,
496 .dev_name = USIO_UART_DEV_NAME,
497 .cons = USIO_CONSOLE,
498 .nr = CONFIG_SERIAL_MILBEAUT_USIO_PORTS,
501 static int mlb_usio_probe(struct platform_device *pdev)
503 struct clk *clk = devm_clk_get(&pdev->dev, NULL);
504 struct uart_port *port;
505 struct resource *res;
510 dev_err(&pdev->dev, "Missing clock\n");
513 ret = clk_prepare_enable(clk);
515 dev_err(&pdev->dev, "Clock enable failed: %d\n", ret);
518 of_property_read_u32(pdev->dev.of_node, "index", &index);
519 port = &mlb_usio_ports[index];
521 port->private_data = (void *)clk;
522 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
524 dev_err(&pdev->dev, "Missing regs\n");
528 port->membase = devm_ioremap(&pdev->dev, res->start,
531 ret = platform_get_irq_byname(pdev, "rx");
532 mlb_usio_irq[index][RX] = ret;
534 ret = platform_get_irq_byname(pdev, "tx");
535 mlb_usio_irq[index][TX] = ret;
537 port->irq = mlb_usio_irq[index][RX];
538 port->uartclk = clk_get_rate(clk);
539 port->fifosize = 128;
540 port->iotype = UPIO_MEM32;
541 port->flags = UPF_BOOT_AUTOCONF | UPF_SPD_VHI;
543 port->ops = &mlb_usio_ops;
544 port->dev = &pdev->dev;
546 ret = uart_add_one_port(&mlb_usio_uart_driver, port);
548 dev_err(&pdev->dev, "Adding port failed: %d\n", ret);
554 clk_disable_unprepare(clk);
559 static int mlb_usio_remove(struct platform_device *pdev)
561 struct uart_port *port = &mlb_usio_ports[pdev->id];
562 struct clk *clk = port->private_data;
564 uart_remove_one_port(&mlb_usio_uart_driver, port);
565 clk_disable_unprepare(clk);
570 static const struct of_device_id mlb_usio_dt_ids[] = {
571 { .compatible = "socionext,milbeaut-usio-uart" },
574 MODULE_DEVICE_TABLE(of, mlb_usio_dt_ids);
576 static struct platform_driver mlb_usio_driver = {
577 .probe = mlb_usio_probe,
578 .remove = mlb_usio_remove,
581 .of_match_table = mlb_usio_dt_ids,
585 static int __init mlb_usio_init(void)
587 int ret = uart_register_driver(&mlb_usio_uart_driver);
590 pr_err("%s: uart registration failed: %d\n", __func__, ret);
593 ret = platform_driver_register(&mlb_usio_driver);
595 uart_unregister_driver(&mlb_usio_uart_driver);
596 pr_err("%s: drv registration failed: %d\n", __func__, ret);
603 static void __exit mlb_usio_exit(void)
605 platform_driver_unregister(&mlb_usio_driver);
606 uart_unregister_driver(&mlb_usio_uart_driver);
609 module_init(mlb_usio_init);
610 module_exit(mlb_usio_exit);
612 MODULE_AUTHOR("SOCIONEXT");
613 MODULE_DESCRIPTION("MILBEAUT_USIO/UART Driver");
614 MODULE_LICENSE("GPL");