1 // SPDX-License-Identifier: GPL-2.0
5 * Based on msm_serial.c, which is:
6 * Copyright (C) 2007 Google, Inc.
10 #include <linux/hrtimer.h>
11 #include <linux/delay.h>
13 #include <linux/ioport.h>
14 #include <linux/irq.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/serial_core.h>
20 #include <linux/serial.h>
21 #include <linux/slab.h>
22 #include <linux/clk.h>
24 #include <linux/of_device.h>
25 #include <linux/err.h>
28 * UART Register offsets
31 #define VT8500_URTDR 0x0000 /* Transmit data */
32 #define VT8500_URRDR 0x0004 /* Receive data */
33 #define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
34 #define VT8500_URLCR 0x000C /* Line control */
35 #define VT8500_URICR 0x0010 /* IrDA control */
36 #define VT8500_URIER 0x0014 /* Interrupt enable */
37 #define VT8500_URISR 0x0018 /* Interrupt status */
38 #define VT8500_URUSR 0x001c /* UART status */
39 #define VT8500_URFCR 0x0020 /* FIFO control */
40 #define VT8500_URFIDX 0x0024 /* FIFO index */
41 #define VT8500_URBKR 0x0028 /* Break signal count */
42 #define VT8500_URTOD 0x002c /* Time out divisor */
43 #define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
44 #define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
47 * Interrupt enable and status bits
50 #define TXDE (1 << 0) /* Tx Data empty */
51 #define RXDF (1 << 1) /* Rx Data full */
52 #define TXFAE (1 << 2) /* Tx FIFO almost empty */
53 #define TXFE (1 << 3) /* Tx FIFO empty */
54 #define RXFAF (1 << 4) /* Rx FIFO almost full */
55 #define RXFF (1 << 5) /* Rx FIFO full */
56 #define TXUDR (1 << 6) /* Tx underrun */
57 #define RXOVER (1 << 7) /* Rx overrun */
58 #define PER (1 << 8) /* Parity error */
59 #define FER (1 << 9) /* Frame error */
60 #define TCTS (1 << 10) /* Toggle of CTS */
61 #define RXTOUT (1 << 11) /* Rx timeout */
62 #define BKDONE (1 << 12) /* Break signal done */
63 #define ERR (1 << 13) /* AHB error response */
65 #define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
66 #define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
72 #define VT8500_TXEN (1 << 0) /* Enable transmit logic */
73 #define VT8500_RXEN (1 << 1) /* Enable receive logic */
74 #define VT8500_CS8 (1 << 2) /* 8-bit data length (vs. 7-bit) */
75 #define VT8500_CSTOPB (1 << 3) /* 2 stop bits (vs. 1) */
76 #define VT8500_PARENB (1 << 4) /* Enable parity */
77 #define VT8500_PARODD (1 << 5) /* Odd parity (vs. even) */
78 #define VT8500_RTS (1 << 6) /* Ready to send */
79 #define VT8500_LOOPBK (1 << 7) /* Enable internal loopback */
80 #define VT8500_DMA (1 << 8) /* Enable DMA mode (needs FIFO) */
81 #define VT8500_BREAK (1 << 9) /* Initiate break signal */
82 #define VT8500_PSLVERR (1 << 10) /* APB error upon empty RX FIFO read */
83 #define VT8500_SWRTSCTS (1 << 11) /* Software-controlled RTS/CTS */
86 * Capability flags (driver-internal)
89 #define VT8500_HAS_SWRTSCTS_SWITCH (1 << 1)
91 #define VT8500_RECOMMENDED_CLK 12000000
92 #define VT8500_OVERSAMPLING_DIVISOR 13
93 #define VT8500_MAX_PORTS 6
96 struct uart_port uart;
99 unsigned int clk_predivisor;
101 unsigned int vt8500_uart_flags;
105 * we use this variable to keep track of which ports
106 * have been allocated as we can't use pdev->id in
109 static DECLARE_BITMAP(vt8500_ports_in_use, VT8500_MAX_PORTS);
111 static inline void vt8500_write(struct uart_port *port, unsigned int val,
114 writel(val, port->membase + off);
117 static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
119 return readl(port->membase + off);
122 static void vt8500_stop_tx(struct uart_port *port)
124 struct vt8500_port *vt8500_port = container_of(port,
128 vt8500_port->ier &= ~TX_FIFO_INTS;
129 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
132 static void vt8500_stop_rx(struct uart_port *port)
134 struct vt8500_port *vt8500_port = container_of(port,
138 vt8500_port->ier &= ~RX_FIFO_INTS;
139 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
142 static void vt8500_enable_ms(struct uart_port *port)
144 struct vt8500_port *vt8500_port = container_of(port,
148 vt8500_port->ier |= TCTS;
149 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
152 static void handle_rx(struct uart_port *port)
154 struct tty_port *tport = &port->state->port;
159 if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
160 port->icount.overrun++;
161 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
164 /* and now the main RX loop */
165 while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
167 char flag = TTY_NORMAL;
169 c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
171 /* Mask conditions we're ignorning. */
172 c &= ~port->read_status_mask;
175 port->icount.frame++;
177 } else if (c & PER) {
178 port->icount.parity++;
183 if (!uart_handle_sysrq_char(port, c))
184 tty_insert_flip_char(tport, c, flag);
187 tty_flip_buffer_push(tport);
190 static unsigned int vt8500_tx_empty(struct uart_port *port)
192 unsigned int idx = vt8500_read(port, VT8500_URFIDX) & 0x1f;
194 return idx < 16 ? TIOCSER_TEMT : 0;
197 static void handle_tx(struct uart_port *port)
199 struct circ_buf *xmit = &port->state->xmit;
202 writeb(port->x_char, port->membase + VT8500_TXFIFO);
206 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
207 vt8500_stop_tx(port);
211 while (vt8500_tx_empty(port)) {
212 if (uart_circ_empty(xmit))
215 writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
217 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
221 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
222 uart_write_wakeup(port);
224 if (uart_circ_empty(xmit))
225 vt8500_stop_tx(port);
228 static void vt8500_start_tx(struct uart_port *port)
230 struct vt8500_port *vt8500_port = container_of(port,
234 vt8500_port->ier &= ~TX_FIFO_INTS;
235 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
237 vt8500_port->ier |= TX_FIFO_INTS;
238 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
241 static void handle_delta_cts(struct uart_port *port)
244 wake_up_interruptible(&port->state->port.delta_msr_wait);
247 static irqreturn_t vt8500_irq(int irq, void *dev_id)
249 struct uart_port *port = dev_id;
252 spin_lock(&port->lock);
253 isr = vt8500_read(port, VT8500_URISR);
255 /* Acknowledge active status bits */
256 vt8500_write(port, isr, VT8500_URISR);
258 if (isr & RX_FIFO_INTS)
260 if (isr & TX_FIFO_INTS)
263 handle_delta_cts(port);
265 spin_unlock(&port->lock);
270 static unsigned int vt8500_get_mctrl(struct uart_port *port)
274 usr = vt8500_read(port, VT8500_URUSR);
281 static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
283 unsigned int lcr = vt8500_read(port, VT8500_URLCR);
285 if (mctrl & TIOCM_RTS)
290 vt8500_write(port, lcr, VT8500_URLCR);
293 static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
297 vt8500_read(port, VT8500_URLCR) | VT8500_BREAK,
301 static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
303 struct vt8500_port *vt8500_port =
304 container_of(port, struct vt8500_port, uart);
306 unsigned int loops = 1000;
308 div = ((vt8500_port->clk_predivisor - 1) & 0xf) << 16;
309 div |= (uart_get_divisor(port, baud) - 1) & 0x3ff;
311 /* Effective baud rate */
312 baud = port->uartclk / 16 / ((div & 0x3ff) + 1);
314 while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
317 vt8500_write(port, div, VT8500_URDIV);
319 /* Break signal timing depends on baud rate, update accordingly */
320 vt8500_write(port, mult_frac(baud, 4096, 1000000), VT8500_URBKR);
325 static int vt8500_startup(struct uart_port *port)
327 struct vt8500_port *vt8500_port =
328 container_of(port, struct vt8500_port, uart);
331 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
332 "vt8500_serial%d", port->line);
334 ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
335 vt8500_port->name, port);
339 vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
344 static void vt8500_shutdown(struct uart_port *port)
346 struct vt8500_port *vt8500_port =
347 container_of(port, struct vt8500_port, uart);
349 vt8500_port->ier = 0;
351 /* disable interrupts and FIFOs */
352 vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
353 vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
354 free_irq(port->irq, port);
357 static void vt8500_set_termios(struct uart_port *port,
358 struct ktermios *termios,
359 const struct ktermios *old)
361 struct vt8500_port *vt8500_port =
362 container_of(port, struct vt8500_port, uart);
364 unsigned int baud, lcr;
365 unsigned int loops = 1000;
367 spin_lock_irqsave(&port->lock, flags);
369 /* calculate and set baud rate */
370 baud = uart_get_baud_rate(port, termios, old, 900, 921600);
371 baud = vt8500_set_baud_rate(port, baud);
372 if (tty_termios_baud_rate(termios))
373 tty_termios_encode_baud_rate(termios, baud, baud);
375 /* calculate parity */
376 lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
377 lcr &= ~(VT8500_PARENB | VT8500_PARODD);
378 if (termios->c_cflag & PARENB) {
379 lcr |= VT8500_PARENB;
380 termios->c_cflag &= ~CMSPAR;
381 if (termios->c_cflag & PARODD)
382 lcr |= VT8500_PARODD;
385 /* calculate bits per char */
387 switch (termios->c_cflag & CSIZE) {
393 termios->c_cflag &= ~CSIZE;
394 termios->c_cflag |= CS8;
398 /* calculate stop bits */
399 lcr &= ~VT8500_CSTOPB;
400 if (termios->c_cflag & CSTOPB)
401 lcr |= VT8500_CSTOPB;
403 lcr &= ~VT8500_SWRTSCTS;
404 if (vt8500_port->vt8500_uart_flags & VT8500_HAS_SWRTSCTS_SWITCH)
405 lcr |= VT8500_SWRTSCTS;
407 /* set parity, bits per char, and stop bit */
408 vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
410 /* Configure status bits to ignore based on termio flags. */
411 port->read_status_mask = 0;
412 if (termios->c_iflag & IGNPAR)
413 port->read_status_mask = FER | PER;
415 uart_update_timeout(port, termios->c_cflag, baud);
418 vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
419 while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
423 /* Every possible FIFO-related interrupt */
424 vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
429 if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
430 vt8500_port->ier |= TCTS;
432 vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
433 vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
435 spin_unlock_irqrestore(&port->lock, flags);
438 static const char *vt8500_type(struct uart_port *port)
440 struct vt8500_port *vt8500_port =
441 container_of(port, struct vt8500_port, uart);
442 return vt8500_port->name;
445 static void vt8500_release_port(struct uart_port *port)
449 static int vt8500_request_port(struct uart_port *port)
454 static void vt8500_config_port(struct uart_port *port, int flags)
456 port->type = PORT_VT8500;
459 static int vt8500_verify_port(struct uart_port *port,
460 struct serial_struct *ser)
462 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
464 if (unlikely(port->irq != ser->irq))
469 static struct vt8500_port *vt8500_uart_ports[VT8500_MAX_PORTS];
470 static struct uart_driver vt8500_uart_driver;
472 #ifdef CONFIG_SERIAL_VT8500_CONSOLE
474 static void wait_for_xmitr(struct uart_port *port)
476 unsigned int status, tmout = 10000;
478 /* Wait up to 10ms for the character(s) to be sent. */
480 status = vt8500_read(port, VT8500_URFIDX);
485 } while (status & 0x10);
488 static void vt8500_console_putchar(struct uart_port *port, unsigned char c)
490 wait_for_xmitr(port);
491 writeb(c, port->membase + VT8500_TXFIFO);
494 static void vt8500_console_write(struct console *co, const char *s,
497 struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
500 BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
502 ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
503 vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
505 uart_console_write(&vt8500_port->uart, s, count,
506 vt8500_console_putchar);
509 * Finally, wait for transmitter to become empty
510 * and switch back to FIFO
512 wait_for_xmitr(&vt8500_port->uart);
513 vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
516 static int __init vt8500_console_setup(struct console *co, char *options)
518 struct vt8500_port *vt8500_port;
524 if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
527 vt8500_port = vt8500_uart_ports[co->index];
533 uart_parse_options(options, &baud, &parity, &bits, &flow);
535 return uart_set_options(&vt8500_port->uart,
536 co, baud, parity, bits, flow);
539 static struct console vt8500_console = {
541 .write = vt8500_console_write,
542 .device = uart_console_device,
543 .setup = vt8500_console_setup,
544 .flags = CON_PRINTBUFFER,
546 .data = &vt8500_uart_driver,
549 #define VT8500_CONSOLE (&vt8500_console)
552 #define VT8500_CONSOLE NULL
555 #ifdef CONFIG_CONSOLE_POLL
556 static int vt8500_get_poll_char(struct uart_port *port)
558 unsigned int status = vt8500_read(port, VT8500_URFIDX);
560 if (!(status & 0x1f00))
563 return vt8500_read(port, VT8500_RXFIFO) & 0xff;
566 static void vt8500_put_poll_char(struct uart_port *port, unsigned char c)
568 unsigned int status, tmout = 10000;
571 status = vt8500_read(port, VT8500_URFIDX);
576 } while (status & 0x10);
578 vt8500_write(port, c, VT8500_TXFIFO);
582 static const struct uart_ops vt8500_uart_pops = {
583 .tx_empty = vt8500_tx_empty,
584 .set_mctrl = vt8500_set_mctrl,
585 .get_mctrl = vt8500_get_mctrl,
586 .stop_tx = vt8500_stop_tx,
587 .start_tx = vt8500_start_tx,
588 .stop_rx = vt8500_stop_rx,
589 .enable_ms = vt8500_enable_ms,
590 .break_ctl = vt8500_break_ctl,
591 .startup = vt8500_startup,
592 .shutdown = vt8500_shutdown,
593 .set_termios = vt8500_set_termios,
595 .release_port = vt8500_release_port,
596 .request_port = vt8500_request_port,
597 .config_port = vt8500_config_port,
598 .verify_port = vt8500_verify_port,
599 #ifdef CONFIG_CONSOLE_POLL
600 .poll_get_char = vt8500_get_poll_char,
601 .poll_put_char = vt8500_put_poll_char,
605 static struct uart_driver vt8500_uart_driver = {
606 .owner = THIS_MODULE,
607 .driver_name = "vt8500_serial",
608 .dev_name = "ttyWMT",
610 .cons = VT8500_CONSOLE,
613 static unsigned int vt8500_flags; /* none required so far */
614 static unsigned int wm8880_flags = VT8500_HAS_SWRTSCTS_SWITCH;
616 static const struct of_device_id wmt_dt_ids[] = {
617 { .compatible = "via,vt8500-uart", .data = &vt8500_flags},
618 { .compatible = "wm,wm8880-uart", .data = &wm8880_flags},
622 static int vt8500_serial_probe(struct platform_device *pdev)
624 struct vt8500_port *vt8500_port;
625 struct resource *mmres;
626 struct device_node *np = pdev->dev.of_node;
627 const unsigned int *flags;
632 flags = of_device_get_match_data(&pdev->dev);
636 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
640 irq = platform_get_irq(pdev, 0);
645 port = of_alias_get_id(np, "serial");
646 if (port >= VT8500_MAX_PORTS)
653 /* calculate the port id */
654 port = find_first_zero_bit(vt8500_ports_in_use,
658 if (port >= VT8500_MAX_PORTS)
661 /* reserve the port id */
662 if (test_and_set_bit(port, vt8500_ports_in_use)) {
663 /* port already in use - shouldn't really happen */
667 vt8500_port = devm_kzalloc(&pdev->dev, sizeof(struct vt8500_port),
672 vt8500_port->uart.membase = devm_ioremap_resource(&pdev->dev, mmres);
673 if (IS_ERR(vt8500_port->uart.membase))
674 return PTR_ERR(vt8500_port->uart.membase);
676 vt8500_port->clk = of_clk_get(pdev->dev.of_node, 0);
677 if (IS_ERR(vt8500_port->clk)) {
678 dev_err(&pdev->dev, "failed to get clock\n");
682 ret = clk_prepare_enable(vt8500_port->clk);
684 dev_err(&pdev->dev, "failed to enable clock\n");
688 vt8500_port->vt8500_uart_flags = *flags;
689 vt8500_port->clk_predivisor = DIV_ROUND_CLOSEST(
690 clk_get_rate(vt8500_port->clk),
691 VT8500_RECOMMENDED_CLK
693 vt8500_port->uart.type = PORT_VT8500;
694 vt8500_port->uart.iotype = UPIO_MEM;
695 vt8500_port->uart.mapbase = mmres->start;
696 vt8500_port->uart.irq = irq;
697 vt8500_port->uart.fifosize = 16;
698 vt8500_port->uart.ops = &vt8500_uart_pops;
699 vt8500_port->uart.line = port;
700 vt8500_port->uart.dev = &pdev->dev;
701 vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
702 vt8500_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_VT8500_CONSOLE);
704 /* Serial core uses the magic "16" everywhere - adjust for it */
705 vt8500_port->uart.uartclk = 16 * clk_get_rate(vt8500_port->clk) /
706 vt8500_port->clk_predivisor /
707 VT8500_OVERSAMPLING_DIVISOR;
709 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
710 "VT8500 UART%d", pdev->id);
712 vt8500_uart_ports[port] = vt8500_port;
714 uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
716 platform_set_drvdata(pdev, vt8500_port);
721 static struct platform_driver vt8500_platform_driver = {
722 .probe = vt8500_serial_probe,
724 .name = "vt8500_serial",
725 .of_match_table = wmt_dt_ids,
726 .suppress_bind_attrs = true,
730 static int __init vt8500_serial_init(void)
734 ret = uart_register_driver(&vt8500_uart_driver);
738 ret = platform_driver_register(&vt8500_platform_driver);
741 uart_unregister_driver(&vt8500_uart_driver);
745 device_initcall(vt8500_serial_init);