1 // SPDX-License-Identifier: GPL-2.0
3 * LiteUART serial controller (LiteX) Driver
5 * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
8 #include <linux/console.h>
9 #include <linux/litex.h>
10 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_platform.h>
14 #include <linux/serial.h>
15 #include <linux/serial_core.h>
16 #include <linux/slab.h>
17 #include <linux/timer.h>
18 #include <linux/tty_flip.h>
19 #include <linux/xarray.h>
22 * CSRs definitions (base address offsets + width)
24 * The definitions below are true for LiteX SoC configured for 8-bit CSR Bus,
27 * Supporting other configurations might require new definitions or a more
28 * generic way of indexing the LiteX CSRs.
30 * For more details on how CSRs are defined and handled in LiteX, see comments
31 * in the LiteX SoC Driver: drivers/soc/litex/litex_soc_ctrl.c
34 #define OFF_TXFULL 0x04
35 #define OFF_RXEMPTY 0x08
36 #define OFF_EV_STATUS 0x0c
37 #define OFF_EV_PENDING 0x10
38 #define OFF_EV_ENABLE 0x14
44 struct liteuart_port {
45 struct uart_port port;
46 struct timer_list timer;
50 #define to_liteuart_port(port) container_of(port, struct liteuart_port, port)
52 static DEFINE_XARRAY_FLAGS(liteuart_array, XA_FLAGS_ALLOC);
54 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
55 static struct console liteuart_console;
58 static struct uart_driver liteuart_driver = {
60 .driver_name = "liteuart",
64 .nr = CONFIG_SERIAL_LITEUART_MAX_PORTS,
65 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
66 .cons = &liteuart_console,
70 static void liteuart_timer(struct timer_list *t)
72 struct liteuart_port *uart = from_timer(uart, t, timer);
73 struct uart_port *port = &uart->port;
74 unsigned char __iomem *membase = port->membase;
75 unsigned int flg = TTY_NORMAL;
79 while ((status = !litex_read8(membase + OFF_RXEMPTY)) == 1) {
80 ch = litex_read8(membase + OFF_RXTX);
83 /* necessary for RXEMPTY to refresh its value */
84 litex_write8(membase + OFF_EV_PENDING, EV_TX | EV_RX);
86 /* no overflow bits in status */
87 if (!(uart_handle_sysrq_char(port, ch)))
88 uart_insert_char(port, status, 0, ch, flg);
90 tty_flip_buffer_push(&port->state->port);
93 mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
96 static void liteuart_putchar(struct uart_port *port, unsigned char ch)
98 while (litex_read8(port->membase + OFF_TXFULL))
101 litex_write8(port->membase + OFF_RXTX, ch);
104 static unsigned int liteuart_tx_empty(struct uart_port *port)
106 /* not really tx empty, just checking if tx is not full */
107 if (!litex_read8(port->membase + OFF_TXFULL))
113 static void liteuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
115 /* modem control register is not present in LiteUART */
118 static unsigned int liteuart_get_mctrl(struct uart_port *port)
120 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
123 static void liteuart_stop_tx(struct uart_port *port)
127 static void liteuart_start_tx(struct uart_port *port)
129 struct circ_buf *xmit = &port->state->xmit;
132 if (unlikely(port->x_char)) {
133 litex_write8(port->membase + OFF_RXTX, port->x_char);
136 } else if (!uart_circ_empty(xmit)) {
137 while (xmit->head != xmit->tail) {
138 ch = xmit->buf[xmit->tail];
139 uart_xmit_advance(port, 1);
140 liteuart_putchar(port, ch);
144 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
145 uart_write_wakeup(port);
148 static void liteuart_stop_rx(struct uart_port *port)
150 struct liteuart_port *uart = to_liteuart_port(port);
152 /* just delete timer */
153 del_timer(&uart->timer);
156 static void liteuart_break_ctl(struct uart_port *port, int break_state)
158 /* LiteUART doesn't support sending break signal */
161 static int liteuart_startup(struct uart_port *port)
163 struct liteuart_port *uart = to_liteuart_port(port);
166 litex_write8(port->membase + OFF_EV_ENABLE, 0);
168 /* prepare timer for polling */
169 timer_setup(&uart->timer, liteuart_timer, 0);
170 mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
175 static void liteuart_shutdown(struct uart_port *port)
179 static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,
180 const struct ktermios *old)
185 spin_lock_irqsave(&port->lock, flags);
187 /* update baudrate */
188 baud = uart_get_baud_rate(port, new, old, 0, 460800);
189 uart_update_timeout(port, new->c_cflag, baud);
191 spin_unlock_irqrestore(&port->lock, flags);
194 static const char *liteuart_type(struct uart_port *port)
199 static void liteuart_release_port(struct uart_port *port)
203 static int liteuart_request_port(struct uart_port *port)
208 static void liteuart_config_port(struct uart_port *port, int flags)
211 * Driver core for serial ports forces a non-zero value for port type.
212 * Write an arbitrary value here to accommodate the serial core driver,
213 * as ID part of UAPI is redundant.
218 static int liteuart_verify_port(struct uart_port *port,
219 struct serial_struct *ser)
221 if (port->type != PORT_UNKNOWN && ser->type != 1)
227 static const struct uart_ops liteuart_ops = {
228 .tx_empty = liteuart_tx_empty,
229 .set_mctrl = liteuart_set_mctrl,
230 .get_mctrl = liteuart_get_mctrl,
231 .stop_tx = liteuart_stop_tx,
232 .start_tx = liteuart_start_tx,
233 .stop_rx = liteuart_stop_rx,
234 .break_ctl = liteuart_break_ctl,
235 .startup = liteuart_startup,
236 .shutdown = liteuart_shutdown,
237 .set_termios = liteuart_set_termios,
238 .type = liteuart_type,
239 .release_port = liteuart_release_port,
240 .request_port = liteuart_request_port,
241 .config_port = liteuart_config_port,
242 .verify_port = liteuart_verify_port,
245 static int liteuart_probe(struct platform_device *pdev)
247 struct liteuart_port *uart;
248 struct uart_port *port;
249 struct xa_limit limit;
252 /* look for aliases; auto-enumerate for free index if not found */
253 dev_id = of_alias_get_id(pdev->dev.of_node, "serial");
255 limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
257 limit = XA_LIMIT(dev_id, dev_id);
259 uart = devm_kzalloc(&pdev->dev, sizeof(struct liteuart_port), GFP_KERNEL);
263 ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);
271 port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
272 if (IS_ERR(port->membase)) {
273 ret = PTR_ERR(port->membase);
277 /* values not from device tree */
278 port->dev = &pdev->dev;
279 port->iotype = UPIO_MEM;
280 port->flags = UPF_BOOT_AUTOCONF;
281 port->ops = &liteuart_ops;
285 port->type = PORT_UNKNOWN;
287 spin_lock_init(&port->lock);
289 platform_set_drvdata(pdev, port);
291 ret = uart_add_one_port(&liteuart_driver, &uart->port);
298 xa_erase(&liteuart_array, uart->id);
303 static int liteuart_remove(struct platform_device *pdev)
305 struct uart_port *port = platform_get_drvdata(pdev);
306 struct liteuart_port *uart = to_liteuart_port(port);
308 uart_remove_one_port(&liteuart_driver, port);
309 xa_erase(&liteuart_array, uart->id);
314 static const struct of_device_id liteuart_of_match[] = {
315 { .compatible = "litex,liteuart" },
318 MODULE_DEVICE_TABLE(of, liteuart_of_match);
320 static struct platform_driver liteuart_platform_driver = {
321 .probe = liteuart_probe,
322 .remove = liteuart_remove,
325 .of_match_table = liteuart_of_match,
329 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
331 static void liteuart_console_write(struct console *co, const char *s,
334 struct liteuart_port *uart;
335 struct uart_port *port;
338 uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
341 spin_lock_irqsave(&port->lock, flags);
342 uart_console_write(port, s, count, liteuart_putchar);
343 spin_unlock_irqrestore(&port->lock, flags);
346 static int liteuart_console_setup(struct console *co, char *options)
348 struct liteuart_port *uart;
349 struct uart_port *port;
355 uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
364 uart_parse_options(options, &baud, &parity, &bits, &flow);
366 return uart_set_options(port, co, baud, parity, bits, flow);
369 static struct console liteuart_console = {
371 .write = liteuart_console_write,
372 .device = uart_console_device,
373 .setup = liteuart_console_setup,
374 .flags = CON_PRINTBUFFER,
376 .data = &liteuart_driver,
379 static int __init liteuart_console_init(void)
381 register_console(&liteuart_console);
385 console_initcall(liteuart_console_init);
387 static void early_liteuart_write(struct console *console, const char *s,
390 struct earlycon_device *device = console->data;
391 struct uart_port *port = &device->port;
393 uart_console_write(port, s, count, liteuart_putchar);
396 static int __init early_liteuart_setup(struct earlycon_device *device,
399 if (!device->port.membase)
402 device->con->write = early_liteuart_write;
406 OF_EARLYCON_DECLARE(liteuart, "litex,liteuart", early_liteuart_setup);
407 #endif /* CONFIG_SERIAL_LITEUART_CONSOLE */
409 static int __init liteuart_init(void)
413 res = uart_register_driver(&liteuart_driver);
417 res = platform_driver_register(&liteuart_platform_driver);
419 uart_unregister_driver(&liteuart_driver);
426 static void __exit liteuart_exit(void)
428 platform_driver_unregister(&liteuart_platform_driver);
429 uart_unregister_driver(&liteuart_driver);
432 module_init(liteuart_init);
433 module_exit(liteuart_exit);
435 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
436 MODULE_DESCRIPTION("LiteUART serial driver");
437 MODULE_LICENSE("GPL v2");
438 MODULE_ALIAS("platform:liteuart");