1 // SPDX-License-Identifier: GPL-2.0
3 * uartlite.c: Serial driver for Xilinx uartlite serial controller
6 * Copyright (C) 2007 Secret Lab Technologies Ltd.
9 #include <linux/platform_device.h>
10 #include <linux/module.h>
11 #include <linux/console.h>
12 #include <linux/serial.h>
13 #include <linux/serial_core.h>
14 #include <linux/tty.h>
15 #include <linux/tty_flip.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/init.h>
20 #include <linux/iopoll.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
25 #include <linux/clk.h>
26 #include <linux/pm_runtime.h>
28 #define ULITE_NAME "ttyUL"
29 #define ULITE_MAJOR 204
30 #define ULITE_MINOR 187
31 #define ULITE_NR_UARTS CONFIG_SERIAL_UARTLITE_NR_UARTS
33 /* ---------------------------------------------------------------------
34 * Register definitions
36 * For register details see datasheet:
37 * https://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
42 #define ULITE_STATUS 0x08
43 #define ULITE_CONTROL 0x0c
45 #define ULITE_REGION 16
47 #define ULITE_STATUS_RXVALID 0x01
48 #define ULITE_STATUS_RXFULL 0x02
49 #define ULITE_STATUS_TXEMPTY 0x04
50 #define ULITE_STATUS_TXFULL 0x08
51 #define ULITE_STATUS_IE 0x10
52 #define ULITE_STATUS_OVERRUN 0x20
53 #define ULITE_STATUS_FRAME 0x40
54 #define ULITE_STATUS_PARITY 0x80
56 #define ULITE_CONTROL_RST_TX 0x01
57 #define ULITE_CONTROL_RST_RX 0x02
58 #define ULITE_CONTROL_IE 0x10
59 #define UART_AUTOSUSPEND_TIMEOUT 3000 /* ms */
61 /* Static pointer to console port */
62 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
63 static struct uart_port *console_port;
66 struct uartlite_data {
67 const struct uartlite_reg_ops *reg_ops;
71 struct uartlite_reg_ops {
72 u32 (*in)(void __iomem *addr);
73 void (*out)(u32 val, void __iomem *addr);
76 static u32 uartlite_inbe32(void __iomem *addr)
78 return ioread32be(addr);
81 static void uartlite_outbe32(u32 val, void __iomem *addr)
83 iowrite32be(val, addr);
86 static const struct uartlite_reg_ops uartlite_be = {
87 .in = uartlite_inbe32,
88 .out = uartlite_outbe32,
91 static u32 uartlite_inle32(void __iomem *addr)
93 return ioread32(addr);
96 static void uartlite_outle32(u32 val, void __iomem *addr)
101 static const struct uartlite_reg_ops uartlite_le = {
102 .in = uartlite_inle32,
103 .out = uartlite_outle32,
106 static inline u32 uart_in32(u32 offset, struct uart_port *port)
108 struct uartlite_data *pdata = port->private_data;
110 return pdata->reg_ops->in(port->membase + offset);
113 static inline void uart_out32(u32 val, u32 offset, struct uart_port *port)
115 struct uartlite_data *pdata = port->private_data;
117 pdata->reg_ops->out(val, port->membase + offset);
120 static struct uart_port ulite_ports[ULITE_NR_UARTS];
122 /* ---------------------------------------------------------------------
123 * Core UART driver operations
126 static int ulite_receive(struct uart_port *port, int stat)
128 struct tty_port *tport = &port->state->port;
129 unsigned char ch = 0;
130 char flag = TTY_NORMAL;
132 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
133 | ULITE_STATUS_FRAME)) == 0)
137 if (stat & ULITE_STATUS_RXVALID) {
139 ch = uart_in32(ULITE_RX, port);
141 if (stat & ULITE_STATUS_PARITY)
142 port->icount.parity++;
145 if (stat & ULITE_STATUS_OVERRUN)
146 port->icount.overrun++;
148 if (stat & ULITE_STATUS_FRAME)
149 port->icount.frame++;
152 /* drop byte with parity error if IGNPAR specificed */
153 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
154 stat &= ~ULITE_STATUS_RXVALID;
156 stat &= port->read_status_mask;
158 if (stat & ULITE_STATUS_PARITY)
162 stat &= ~port->ignore_status_mask;
164 if (stat & ULITE_STATUS_RXVALID)
165 tty_insert_flip_char(tport, ch, flag);
167 if (stat & ULITE_STATUS_FRAME)
168 tty_insert_flip_char(tport, 0, TTY_FRAME);
170 if (stat & ULITE_STATUS_OVERRUN)
171 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
176 static int ulite_transmit(struct uart_port *port, int stat)
178 struct circ_buf *xmit = &port->state->xmit;
180 if (stat & ULITE_STATUS_TXFULL)
184 uart_out32(port->x_char, ULITE_TX, port);
190 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
193 uart_out32(xmit->buf[xmit->tail], ULITE_TX, port);
194 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
198 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
199 uart_write_wakeup(port);
204 static irqreturn_t ulite_isr(int irq, void *dev_id)
206 struct uart_port *port = dev_id;
207 int stat, busy, n = 0;
211 spin_lock_irqsave(&port->lock, flags);
212 stat = uart_in32(ULITE_STATUS, port);
213 busy = ulite_receive(port, stat);
214 busy |= ulite_transmit(port, stat);
215 spin_unlock_irqrestore(&port->lock, flags);
221 tty_flip_buffer_push(&port->state->port);
228 static unsigned int ulite_tx_empty(struct uart_port *port)
233 spin_lock_irqsave(&port->lock, flags);
234 ret = uart_in32(ULITE_STATUS, port);
235 spin_unlock_irqrestore(&port->lock, flags);
237 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
240 static unsigned int ulite_get_mctrl(struct uart_port *port)
242 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
245 static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
250 static void ulite_stop_tx(struct uart_port *port)
255 static void ulite_start_tx(struct uart_port *port)
257 ulite_transmit(port, uart_in32(ULITE_STATUS, port));
260 static void ulite_stop_rx(struct uart_port *port)
262 /* don't forward any more data (like !CREAD) */
263 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
264 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
267 static void ulite_break_ctl(struct uart_port *port, int ctl)
272 static int ulite_startup(struct uart_port *port)
274 struct uartlite_data *pdata = port->private_data;
277 ret = clk_enable(pdata->clk);
279 dev_err(port->dev, "Failed to enable clock\n");
283 ret = request_irq(port->irq, ulite_isr, IRQF_SHARED | IRQF_TRIGGER_RISING,
288 uart_out32(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
289 ULITE_CONTROL, port);
290 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
295 static void ulite_shutdown(struct uart_port *port)
297 struct uartlite_data *pdata = port->private_data;
299 uart_out32(0, ULITE_CONTROL, port);
300 uart_in32(ULITE_CONTROL, port); /* dummy */
301 free_irq(port->irq, port);
302 clk_disable(pdata->clk);
305 static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
306 struct ktermios *old)
311 spin_lock_irqsave(&port->lock, flags);
313 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
314 | ULITE_STATUS_TXFULL;
316 if (termios->c_iflag & INPCK)
317 port->read_status_mask |=
318 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
320 port->ignore_status_mask = 0;
321 if (termios->c_iflag & IGNPAR)
322 port->ignore_status_mask |= ULITE_STATUS_PARITY
323 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
325 /* ignore all characters if CREAD is not set */
326 if ((termios->c_cflag & CREAD) == 0)
327 port->ignore_status_mask |=
328 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
329 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
332 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
333 uart_update_timeout(port, termios->c_cflag, baud);
335 spin_unlock_irqrestore(&port->lock, flags);
338 static const char *ulite_type(struct uart_port *port)
340 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
343 static void ulite_release_port(struct uart_port *port)
345 release_mem_region(port->mapbase, ULITE_REGION);
346 iounmap(port->membase);
347 port->membase = NULL;
350 static int ulite_request_port(struct uart_port *port)
352 struct uartlite_data *pdata = port->private_data;
355 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
356 port, (unsigned long long) port->mapbase);
358 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
359 dev_err(port->dev, "Memory region busy\n");
363 port->membase = ioremap(port->mapbase, ULITE_REGION);
364 if (!port->membase) {
365 dev_err(port->dev, "Unable to map registers\n");
366 release_mem_region(port->mapbase, ULITE_REGION);
370 pdata->reg_ops = &uartlite_be;
371 ret = uart_in32(ULITE_CONTROL, port);
372 uart_out32(ULITE_CONTROL_RST_TX, ULITE_CONTROL, port);
373 ret = uart_in32(ULITE_STATUS, port);
374 /* Endianess detection */
375 if ((ret & ULITE_STATUS_TXEMPTY) != ULITE_STATUS_TXEMPTY)
376 pdata->reg_ops = &uartlite_le;
381 static void ulite_config_port(struct uart_port *port, int flags)
383 if (!ulite_request_port(port))
384 port->type = PORT_UARTLITE;
387 static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
389 /* we don't want the core code to modify any port params */
393 static void ulite_pm(struct uart_port *port, unsigned int state,
394 unsigned int oldstate)
399 ret = pm_runtime_get_sync(port->dev);
401 dev_err(port->dev, "Failed to enable clocks\n");
403 pm_runtime_mark_last_busy(port->dev);
404 pm_runtime_put_autosuspend(port->dev);
408 #ifdef CONFIG_CONSOLE_POLL
409 static int ulite_get_poll_char(struct uart_port *port)
411 if (!(uart_in32(ULITE_STATUS, port) & ULITE_STATUS_RXVALID))
414 return uart_in32(ULITE_RX, port);
417 static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
419 while (uart_in32(ULITE_STATUS, port) & ULITE_STATUS_TXFULL)
422 /* write char to device */
423 uart_out32(ch, ULITE_TX, port);
427 static const struct uart_ops ulite_ops = {
428 .tx_empty = ulite_tx_empty,
429 .set_mctrl = ulite_set_mctrl,
430 .get_mctrl = ulite_get_mctrl,
431 .stop_tx = ulite_stop_tx,
432 .start_tx = ulite_start_tx,
433 .stop_rx = ulite_stop_rx,
434 .break_ctl = ulite_break_ctl,
435 .startup = ulite_startup,
436 .shutdown = ulite_shutdown,
437 .set_termios = ulite_set_termios,
439 .release_port = ulite_release_port,
440 .request_port = ulite_request_port,
441 .config_port = ulite_config_port,
442 .verify_port = ulite_verify_port,
444 #ifdef CONFIG_CONSOLE_POLL
445 .poll_get_char = ulite_get_poll_char,
446 .poll_put_char = ulite_put_poll_char,
450 /* ---------------------------------------------------------------------
451 * Console driver operations
454 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
455 static void ulite_console_wait_tx(struct uart_port *port)
460 * Spin waiting for TX fifo to have space available.
461 * When using the Microblaze Debug Module this can take up to 1s
463 if (read_poll_timeout_atomic(uart_in32, val, !(val & ULITE_STATUS_TXFULL),
464 0, 1000000, false, ULITE_STATUS, port))
466 "timeout waiting for TX buffer empty\n");
469 static void ulite_console_putchar(struct uart_port *port, int ch)
471 ulite_console_wait_tx(port);
472 uart_out32(ch, ULITE_TX, port);
475 static void ulite_console_write(struct console *co, const char *s,
478 struct uart_port *port = console_port;
483 if (oops_in_progress) {
484 locked = spin_trylock_irqsave(&port->lock, flags);
486 spin_lock_irqsave(&port->lock, flags);
488 /* save and disable interrupt */
489 ier = uart_in32(ULITE_STATUS, port) & ULITE_STATUS_IE;
490 uart_out32(0, ULITE_CONTROL, port);
492 uart_console_write(port, s, count, ulite_console_putchar);
494 ulite_console_wait_tx(port);
496 /* restore interrupt state */
498 uart_out32(ULITE_CONTROL_IE, ULITE_CONTROL, port);
501 spin_unlock_irqrestore(&port->lock, flags);
504 static int ulite_console_setup(struct console *co, char *options)
506 struct uart_port *port = NULL;
512 if (co->index >= 0 && co->index < ULITE_NR_UARTS)
513 port = ulite_ports + co->index;
515 /* Has the device been initialized yet? */
516 if (!port || !port->mapbase) {
517 pr_debug("console on ttyUL%i not present\n", co->index);
523 /* not initialized yet? */
524 if (!port->membase) {
525 if (ulite_request_port(port))
530 uart_parse_options(options, &baud, &parity, &bits, &flow);
532 return uart_set_options(port, co, baud, parity, bits, flow);
535 static struct uart_driver ulite_uart_driver;
537 static struct console ulite_console = {
539 .write = ulite_console_write,
540 .device = uart_console_device,
541 .setup = ulite_console_setup,
542 .flags = CON_PRINTBUFFER,
543 .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
544 .data = &ulite_uart_driver,
547 static void early_uartlite_putc(struct uart_port *port, int c)
550 * Limit how many times we'll spin waiting for TX FIFO status.
551 * This will prevent lockups if the base address is incorrectly
552 * set, or any other issue on the UARTLITE.
553 * This limit is pretty arbitrary, unless we are at about 10 baud
554 * we'll never timeout on a working UART.
556 unsigned retries = 1000000;
559 (readl(port->membase + ULITE_STATUS) & ULITE_STATUS_TXFULL))
562 /* Only attempt the iowrite if we didn't timeout */
564 writel(c & 0xff, port->membase + ULITE_TX);
567 static void early_uartlite_write(struct console *console,
568 const char *s, unsigned n)
570 struct earlycon_device *device = console->data;
571 uart_console_write(&device->port, s, n, early_uartlite_putc);
574 static int __init early_uartlite_setup(struct earlycon_device *device,
577 if (!device->port.membase)
580 device->con->write = early_uartlite_write;
583 EARLYCON_DECLARE(uartlite, early_uartlite_setup);
584 OF_EARLYCON_DECLARE(uartlite_b, "xlnx,opb-uartlite-1.00.b", early_uartlite_setup);
585 OF_EARLYCON_DECLARE(uartlite_a, "xlnx,xps-uartlite-1.00.a", early_uartlite_setup);
587 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
589 static struct uart_driver ulite_uart_driver = {
590 .owner = THIS_MODULE,
591 .driver_name = "uartlite",
592 .dev_name = ULITE_NAME,
593 .major = ULITE_MAJOR,
594 .minor = ULITE_MINOR,
595 .nr = ULITE_NR_UARTS,
596 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
597 .cons = &ulite_console,
601 /* ---------------------------------------------------------------------
602 * Port assignment functions (mapping devices to uart_port structures)
605 /** ulite_assign: register a uartlite device with the driver
607 * @dev: pointer to device structure
608 * @id: requested id number. Pass -1 for automatic port assignment
609 * @base: base address of uartlite registers
610 * @irq: irq number for uartlite
611 * @pdata: private data for uartlite
613 * Returns: 0 on success, <0 otherwise
615 static int ulite_assign(struct device *dev, int id, u32 base, int irq,
616 struct uartlite_data *pdata)
618 struct uart_port *port;
621 /* if id = -1; then scan for a free id and use that */
623 for (id = 0; id < ULITE_NR_UARTS; id++)
624 if (ulite_ports[id].mapbase == 0)
627 if (id < 0 || id >= ULITE_NR_UARTS) {
628 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
632 if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
633 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
638 port = &ulite_ports[id];
640 spin_lock_init(&port->lock);
643 port->iotype = UPIO_MEM;
644 port->iobase = 1; /* mark port in use */
645 port->mapbase = base;
646 port->membase = NULL;
647 port->ops = &ulite_ops;
649 port->flags = UPF_BOOT_AUTOCONF;
651 port->type = PORT_UNKNOWN;
653 port->private_data = pdata;
655 dev_set_drvdata(dev, port);
657 /* Register the port */
658 rc = uart_add_one_port(&ulite_uart_driver, port);
660 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
662 dev_set_drvdata(dev, NULL);
669 /** ulite_release: register a uartlite device with the driver
671 * @dev: pointer to device structure
673 static int ulite_release(struct device *dev)
675 struct uart_port *port = dev_get_drvdata(dev);
679 rc = uart_remove_one_port(&ulite_uart_driver, port);
680 dev_set_drvdata(dev, NULL);
688 * ulite_suspend - Stop the device.
690 * @dev: handle to the device structure.
693 static int __maybe_unused ulite_suspend(struct device *dev)
695 struct uart_port *port = dev_get_drvdata(dev);
698 uart_suspend_port(&ulite_uart_driver, port);
704 * ulite_resume - Resume the device.
706 * @dev: handle to the device structure.
707 * Return: 0 on success, errno otherwise.
709 static int __maybe_unused ulite_resume(struct device *dev)
711 struct uart_port *port = dev_get_drvdata(dev);
714 uart_resume_port(&ulite_uart_driver, port);
719 static int __maybe_unused ulite_runtime_suspend(struct device *dev)
721 struct uart_port *port = dev_get_drvdata(dev);
722 struct uartlite_data *pdata = port->private_data;
724 clk_disable(pdata->clk);
728 static int __maybe_unused ulite_runtime_resume(struct device *dev)
730 struct uart_port *port = dev_get_drvdata(dev);
731 struct uartlite_data *pdata = port->private_data;
734 ret = clk_enable(pdata->clk);
736 dev_err(dev, "Cannot enable clock.\n");
742 /* ---------------------------------------------------------------------
743 * Platform bus binding
746 static const struct dev_pm_ops ulite_pm_ops = {
747 SET_SYSTEM_SLEEP_PM_OPS(ulite_suspend, ulite_resume)
748 SET_RUNTIME_PM_OPS(ulite_runtime_suspend,
749 ulite_runtime_resume, NULL)
752 #if defined(CONFIG_OF)
753 /* Match table for of_platform binding */
754 static const struct of_device_id ulite_of_match[] = {
755 { .compatible = "xlnx,opb-uartlite-1.00.b", },
756 { .compatible = "xlnx,xps-uartlite-1.00.a", },
759 MODULE_DEVICE_TABLE(of, ulite_of_match);
760 #endif /* CONFIG_OF */
762 static int ulite_probe(struct platform_device *pdev)
764 struct resource *res;
765 struct uartlite_data *pdata;
771 prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
773 id = be32_to_cpup(prop);
775 pdata = devm_kzalloc(&pdev->dev, sizeof(struct uartlite_data),
780 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
784 irq = platform_get_irq(pdev, 0);
788 pdata->clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
789 if (IS_ERR(pdata->clk)) {
790 if (PTR_ERR(pdata->clk) != -ENOENT)
791 return PTR_ERR(pdata->clk);
794 * Clock framework support is optional, continue on
795 * anyways if we don't find a matching clock.
800 ret = clk_prepare_enable(pdata->clk);
802 dev_err(&pdev->dev, "Failed to prepare clock\n");
806 pm_runtime_use_autosuspend(&pdev->dev);
807 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT);
808 pm_runtime_set_active(&pdev->dev);
809 pm_runtime_enable(&pdev->dev);
811 if (!ulite_uart_driver.state) {
812 dev_dbg(&pdev->dev, "uartlite: calling uart_register_driver()\n");
813 ret = uart_register_driver(&ulite_uart_driver);
815 dev_err(&pdev->dev, "Failed to register driver\n");
816 clk_disable_unprepare(pdata->clk);
821 ret = ulite_assign(&pdev->dev, id, res->start, irq, pdata);
823 pm_runtime_mark_last_busy(&pdev->dev);
824 pm_runtime_put_autosuspend(&pdev->dev);
829 static int ulite_remove(struct platform_device *pdev)
831 struct uart_port *port = dev_get_drvdata(&pdev->dev);
832 struct uartlite_data *pdata = port->private_data;
835 clk_disable_unprepare(pdata->clk);
836 rc = ulite_release(&pdev->dev);
837 pm_runtime_disable(&pdev->dev);
838 pm_runtime_set_suspended(&pdev->dev);
839 pm_runtime_dont_use_autosuspend(&pdev->dev);
843 /* work with hotplug and coldplug */
844 MODULE_ALIAS("platform:uartlite");
846 static struct platform_driver ulite_platform_driver = {
847 .probe = ulite_probe,
848 .remove = ulite_remove,
851 .of_match_table = of_match_ptr(ulite_of_match),
856 /* ---------------------------------------------------------------------
857 * Module setup/teardown
860 static int __init ulite_init(void)
863 pr_debug("uartlite: calling platform_driver_register()\n");
864 return platform_driver_register(&ulite_platform_driver);
867 static void __exit ulite_exit(void)
869 platform_driver_unregister(&ulite_platform_driver);
870 if (ulite_uart_driver.state)
871 uart_unregister_driver(&ulite_uart_driver);
874 module_init(ulite_init);
875 module_exit(ulite_exit);
878 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
879 MODULE_LICENSE("GPL");