1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
5 * Based on msm_serial.c, which is:
6 * Copyright (C) 2007 Google, Inc.
7 * Author: Robert Love <rlove@google.com>
10 #if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
11 # define SUPPORT_SYSRQ
14 #include <linux/hrtimer.h>
15 #include <linux/delay.h>
17 #include <linux/ioport.h>
18 #include <linux/irq.h>
19 #include <linux/init.h>
20 #include <linux/console.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial_core.h>
24 #include <linux/serial.h>
25 #include <linux/slab.h>
26 #include <linux/clk.h>
28 #include <linux/of_device.h>
29 #include <linux/err.h>
32 * UART Register offsets
35 #define VT8500_URTDR 0x0000 /* Transmit data */
36 #define VT8500_URRDR 0x0004 /* Receive data */
37 #define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
38 #define VT8500_URLCR 0x000C /* Line control */
39 #define VT8500_URICR 0x0010 /* IrDA control */
40 #define VT8500_URIER 0x0014 /* Interrupt enable */
41 #define VT8500_URISR 0x0018 /* Interrupt status */
42 #define VT8500_URUSR 0x001c /* UART status */
43 #define VT8500_URFCR 0x0020 /* FIFO control */
44 #define VT8500_URFIDX 0x0024 /* FIFO index */
45 #define VT8500_URBKR 0x0028 /* Break signal count */
46 #define VT8500_URTOD 0x002c /* Time out divisor */
47 #define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
48 #define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
51 * Interrupt enable and status bits
54 #define TXDE (1 << 0) /* Tx Data empty */
55 #define RXDF (1 << 1) /* Rx Data full */
56 #define TXFAE (1 << 2) /* Tx FIFO almost empty */
57 #define TXFE (1 << 3) /* Tx FIFO empty */
58 #define RXFAF (1 << 4) /* Rx FIFO almost full */
59 #define RXFF (1 << 5) /* Rx FIFO full */
60 #define TXUDR (1 << 6) /* Tx underrun */
61 #define RXOVER (1 << 7) /* Rx overrun */
62 #define PER (1 << 8) /* Parity error */
63 #define FER (1 << 9) /* Frame error */
64 #define TCTS (1 << 10) /* Toggle of CTS */
65 #define RXTOUT (1 << 11) /* Rx timeout */
66 #define BKDONE (1 << 12) /* Break signal done */
67 #define ERR (1 << 13) /* AHB error response */
69 #define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
70 #define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
76 #define VT8500_TXEN (1 << 0) /* Enable transmit logic */
77 #define VT8500_RXEN (1 << 1) /* Enable receive logic */
78 #define VT8500_CS8 (1 << 2) /* 8-bit data length (vs. 7-bit) */
79 #define VT8500_CSTOPB (1 << 3) /* 2 stop bits (vs. 1) */
80 #define VT8500_PARENB (1 << 4) /* Enable parity */
81 #define VT8500_PARODD (1 << 5) /* Odd parity (vs. even) */
82 #define VT8500_RTS (1 << 6) /* Ready to send */
83 #define VT8500_LOOPBK (1 << 7) /* Enable internal loopback */
84 #define VT8500_DMA (1 << 8) /* Enable DMA mode (needs FIFO) */
85 #define VT8500_BREAK (1 << 9) /* Initiate break signal */
86 #define VT8500_PSLVERR (1 << 10) /* APB error upon empty RX FIFO read */
87 #define VT8500_SWRTSCTS (1 << 11) /* Software-controlled RTS/CTS */
90 * Capability flags (driver-internal)
93 #define VT8500_HAS_SWRTSCTS_SWITCH (1 << 1)
95 #define VT8500_RECOMMENDED_CLK 12000000
96 #define VT8500_OVERSAMPLING_DIVISOR 13
97 #define VT8500_MAX_PORTS 6
100 struct uart_port uart
;
103 unsigned int clk_predivisor
;
105 unsigned int vt8500_uart_flags
;
109 * we use this variable to keep track of which ports
110 * have been allocated as we can't use pdev->id in
113 static DECLARE_BITMAP(vt8500_ports_in_use
, VT8500_MAX_PORTS
);
115 static inline void vt8500_write(struct uart_port
*port
, unsigned int val
,
118 writel(val
, port
->membase
+ off
);
121 static inline unsigned int vt8500_read(struct uart_port
*port
, unsigned int off
)
123 return readl(port
->membase
+ off
);
126 static void vt8500_stop_tx(struct uart_port
*port
)
128 struct vt8500_port
*vt8500_port
= container_of(port
,
132 vt8500_port
->ier
&= ~TX_FIFO_INTS
;
133 vt8500_write(port
, vt8500_port
->ier
, VT8500_URIER
);
136 static void vt8500_stop_rx(struct uart_port
*port
)
138 struct vt8500_port
*vt8500_port
= container_of(port
,
142 vt8500_port
->ier
&= ~RX_FIFO_INTS
;
143 vt8500_write(port
, vt8500_port
->ier
, VT8500_URIER
);
146 static void vt8500_enable_ms(struct uart_port
*port
)
148 struct vt8500_port
*vt8500_port
= container_of(port
,
152 vt8500_port
->ier
|= TCTS
;
153 vt8500_write(port
, vt8500_port
->ier
, VT8500_URIER
);
156 static void handle_rx(struct uart_port
*port
)
158 struct tty_port
*tport
= &port
->state
->port
;
163 if ((vt8500_read(port
, VT8500_URISR
) & RXOVER
)) {
164 port
->icount
.overrun
++;
165 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
168 /* and now the main RX loop */
169 while (vt8500_read(port
, VT8500_URFIDX
) & 0x1f00) {
171 char flag
= TTY_NORMAL
;
173 c
= readw(port
->membase
+ VT8500_RXFIFO
) & 0x3ff;
175 /* Mask conditions we're ignorning. */
176 c
&= ~port
->read_status_mask
;
179 port
->icount
.frame
++;
181 } else if (c
& PER
) {
182 port
->icount
.parity
++;
187 if (!uart_handle_sysrq_char(port
, c
))
188 tty_insert_flip_char(tport
, c
, flag
);
191 spin_unlock(&port
->lock
);
192 tty_flip_buffer_push(tport
);
193 spin_lock(&port
->lock
);
196 static void handle_tx(struct uart_port
*port
)
198 struct circ_buf
*xmit
= &port
->state
->xmit
;
201 writeb(port
->x_char
, port
->membase
+ VT8500_TXFIFO
);
205 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
206 vt8500_stop_tx(port
);
210 while ((vt8500_read(port
, VT8500_URFIDX
) & 0x1f) < 16) {
211 if (uart_circ_empty(xmit
))
214 writeb(xmit
->buf
[xmit
->tail
], port
->membase
+ VT8500_TXFIFO
);
216 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
220 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
221 uart_write_wakeup(port
);
223 if (uart_circ_empty(xmit
))
224 vt8500_stop_tx(port
);
227 static void vt8500_start_tx(struct uart_port
*port
)
229 struct vt8500_port
*vt8500_port
= container_of(port
,
233 vt8500_port
->ier
&= ~TX_FIFO_INTS
;
234 vt8500_write(port
, vt8500_port
->ier
, VT8500_URIER
);
236 vt8500_port
->ier
|= TX_FIFO_INTS
;
237 vt8500_write(port
, vt8500_port
->ier
, VT8500_URIER
);
240 static void handle_delta_cts(struct uart_port
*port
)
243 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
246 static irqreturn_t
vt8500_irq(int irq
, void *dev_id
)
248 struct uart_port
*port
= dev_id
;
251 spin_lock(&port
->lock
);
252 isr
= vt8500_read(port
, VT8500_URISR
);
254 /* Acknowledge active status bits */
255 vt8500_write(port
, isr
, VT8500_URISR
);
257 if (isr
& RX_FIFO_INTS
)
259 if (isr
& TX_FIFO_INTS
)
262 handle_delta_cts(port
);
264 spin_unlock(&port
->lock
);
269 static unsigned int vt8500_tx_empty(struct uart_port
*port
)
271 return (vt8500_read(port
, VT8500_URFIDX
) & 0x1f) < 16 ?
275 static unsigned int vt8500_get_mctrl(struct uart_port
*port
)
279 usr
= vt8500_read(port
, VT8500_URUSR
);
286 static void vt8500_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
288 unsigned int lcr
= vt8500_read(port
, VT8500_URLCR
);
290 if (mctrl
& TIOCM_RTS
)
295 vt8500_write(port
, lcr
, VT8500_URLCR
);
298 static void vt8500_break_ctl(struct uart_port
*port
, int break_ctl
)
302 vt8500_read(port
, VT8500_URLCR
) | VT8500_BREAK
,
306 static int vt8500_set_baud_rate(struct uart_port
*port
, unsigned int baud
)
308 struct vt8500_port
*vt8500_port
=
309 container_of(port
, struct vt8500_port
, uart
);
311 unsigned int loops
= 1000;
313 div
= ((vt8500_port
->clk_predivisor
- 1) & 0xf) << 16;
314 div
|= (uart_get_divisor(port
, baud
) - 1) & 0x3ff;
316 /* Effective baud rate */
317 baud
= port
->uartclk
/ 16 / ((div
& 0x3ff) + 1);
319 while ((vt8500_read(port
, VT8500_URUSR
) & (1 << 5)) && --loops
)
322 vt8500_write(port
, div
, VT8500_URDIV
);
324 /* Break signal timing depends on baud rate, update accordingly */
325 vt8500_write(port
, mult_frac(baud
, 4096, 1000000), VT8500_URBKR
);
330 static int vt8500_startup(struct uart_port
*port
)
332 struct vt8500_port
*vt8500_port
=
333 container_of(port
, struct vt8500_port
, uart
);
336 snprintf(vt8500_port
->name
, sizeof(vt8500_port
->name
),
337 "vt8500_serial%d", port
->line
);
339 ret
= request_irq(port
->irq
, vt8500_irq
, IRQF_TRIGGER_HIGH
,
340 vt8500_port
->name
, port
);
344 vt8500_write(port
, 0x03, VT8500_URLCR
); /* enable TX & RX */
349 static void vt8500_shutdown(struct uart_port
*port
)
351 struct vt8500_port
*vt8500_port
=
352 container_of(port
, struct vt8500_port
, uart
);
354 vt8500_port
->ier
= 0;
356 /* disable interrupts and FIFOs */
357 vt8500_write(&vt8500_port
->uart
, 0, VT8500_URIER
);
358 vt8500_write(&vt8500_port
->uart
, 0x880, VT8500_URFCR
);
359 free_irq(port
->irq
, port
);
362 static void vt8500_set_termios(struct uart_port
*port
,
363 struct ktermios
*termios
,
364 struct ktermios
*old
)
366 struct vt8500_port
*vt8500_port
=
367 container_of(port
, struct vt8500_port
, uart
);
369 unsigned int baud
, lcr
;
370 unsigned int loops
= 1000;
372 spin_lock_irqsave(&port
->lock
, flags
);
374 /* calculate and set baud rate */
375 baud
= uart_get_baud_rate(port
, termios
, old
, 900, 921600);
376 baud
= vt8500_set_baud_rate(port
, baud
);
377 if (tty_termios_baud_rate(termios
))
378 tty_termios_encode_baud_rate(termios
, baud
, baud
);
380 /* calculate parity */
381 lcr
= vt8500_read(&vt8500_port
->uart
, VT8500_URLCR
);
382 lcr
&= ~(VT8500_PARENB
| VT8500_PARODD
);
383 if (termios
->c_cflag
& PARENB
) {
384 lcr
|= VT8500_PARENB
;
385 termios
->c_cflag
&= ~CMSPAR
;
386 if (termios
->c_cflag
& PARODD
)
387 lcr
|= VT8500_PARODD
;
390 /* calculate bits per char */
392 switch (termios
->c_cflag
& CSIZE
) {
398 termios
->c_cflag
&= ~CSIZE
;
399 termios
->c_cflag
|= CS8
;
403 /* calculate stop bits */
404 lcr
&= ~VT8500_CSTOPB
;
405 if (termios
->c_cflag
& CSTOPB
)
406 lcr
|= VT8500_CSTOPB
;
408 lcr
&= ~VT8500_SWRTSCTS
;
409 if (vt8500_port
->vt8500_uart_flags
& VT8500_HAS_SWRTSCTS_SWITCH
)
410 lcr
|= VT8500_SWRTSCTS
;
412 /* set parity, bits per char, and stop bit */
413 vt8500_write(&vt8500_port
->uart
, lcr
, VT8500_URLCR
);
415 /* Configure status bits to ignore based on termio flags. */
416 port
->read_status_mask
= 0;
417 if (termios
->c_iflag
& IGNPAR
)
418 port
->read_status_mask
= FER
| PER
;
420 uart_update_timeout(port
, termios
->c_cflag
, baud
);
423 vt8500_write(&vt8500_port
->uart
, 0x88c, VT8500_URFCR
);
424 while ((vt8500_read(&vt8500_port
->uart
, VT8500_URFCR
) & 0xc)
428 /* Every possible FIFO-related interrupt */
429 vt8500_port
->ier
= RX_FIFO_INTS
| TX_FIFO_INTS
;
434 if (UART_ENABLE_MS(&vt8500_port
->uart
, termios
->c_cflag
))
435 vt8500_port
->ier
|= TCTS
;
437 vt8500_write(&vt8500_port
->uart
, 0x881, VT8500_URFCR
);
438 vt8500_write(&vt8500_port
->uart
, vt8500_port
->ier
, VT8500_URIER
);
440 spin_unlock_irqrestore(&port
->lock
, flags
);
443 static const char *vt8500_type(struct uart_port
*port
)
445 struct vt8500_port
*vt8500_port
=
446 container_of(port
, struct vt8500_port
, uart
);
447 return vt8500_port
->name
;
450 static void vt8500_release_port(struct uart_port
*port
)
454 static int vt8500_request_port(struct uart_port
*port
)
459 static void vt8500_config_port(struct uart_port
*port
, int flags
)
461 port
->type
= PORT_VT8500
;
464 static int vt8500_verify_port(struct uart_port
*port
,
465 struct serial_struct
*ser
)
467 if (unlikely(ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_VT8500
))
469 if (unlikely(port
->irq
!= ser
->irq
))
474 static struct vt8500_port
*vt8500_uart_ports
[VT8500_MAX_PORTS
];
475 static struct uart_driver vt8500_uart_driver
;
477 #ifdef CONFIG_SERIAL_VT8500_CONSOLE
479 static void wait_for_xmitr(struct uart_port
*port
)
481 unsigned int status
, tmout
= 10000;
483 /* Wait up to 10ms for the character(s) to be sent. */
485 status
= vt8500_read(port
, VT8500_URFIDX
);
490 } while (status
& 0x10);
493 static void vt8500_console_putchar(struct uart_port
*port
, int c
)
495 wait_for_xmitr(port
);
496 writeb(c
, port
->membase
+ VT8500_TXFIFO
);
499 static void vt8500_console_write(struct console
*co
, const char *s
,
502 struct vt8500_port
*vt8500_port
= vt8500_uart_ports
[co
->index
];
505 BUG_ON(co
->index
< 0 || co
->index
>= vt8500_uart_driver
.nr
);
507 ier
= vt8500_read(&vt8500_port
->uart
, VT8500_URIER
);
508 vt8500_write(&vt8500_port
->uart
, VT8500_URIER
, 0);
510 uart_console_write(&vt8500_port
->uart
, s
, count
,
511 vt8500_console_putchar
);
514 * Finally, wait for transmitter to become empty
515 * and switch back to FIFO
517 wait_for_xmitr(&vt8500_port
->uart
);
518 vt8500_write(&vt8500_port
->uart
, VT8500_URIER
, ier
);
521 static int __init
vt8500_console_setup(struct console
*co
, char *options
)
523 struct vt8500_port
*vt8500_port
;
529 if (unlikely(co
->index
>= vt8500_uart_driver
.nr
|| co
->index
< 0))
532 vt8500_port
= vt8500_uart_ports
[co
->index
];
538 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
540 return uart_set_options(&vt8500_port
->uart
,
541 co
, baud
, parity
, bits
, flow
);
544 static struct console vt8500_console
= {
546 .write
= vt8500_console_write
,
547 .device
= uart_console_device
,
548 .setup
= vt8500_console_setup
,
549 .flags
= CON_PRINTBUFFER
,
551 .data
= &vt8500_uart_driver
,
554 #define VT8500_CONSOLE (&vt8500_console)
557 #define VT8500_CONSOLE NULL
560 #ifdef CONFIG_CONSOLE_POLL
561 static int vt8500_get_poll_char(struct uart_port
*port
)
563 unsigned int status
= vt8500_read(port
, VT8500_URFIDX
);
565 if (!(status
& 0x1f00))
568 return vt8500_read(port
, VT8500_RXFIFO
) & 0xff;
571 static void vt8500_put_poll_char(struct uart_port
*port
, unsigned char c
)
573 unsigned int status
, tmout
= 10000;
576 status
= vt8500_read(port
, VT8500_URFIDX
);
581 } while (status
& 0x10);
583 vt8500_write(port
, c
, VT8500_TXFIFO
);
587 static const struct uart_ops vt8500_uart_pops
= {
588 .tx_empty
= vt8500_tx_empty
,
589 .set_mctrl
= vt8500_set_mctrl
,
590 .get_mctrl
= vt8500_get_mctrl
,
591 .stop_tx
= vt8500_stop_tx
,
592 .start_tx
= vt8500_start_tx
,
593 .stop_rx
= vt8500_stop_rx
,
594 .enable_ms
= vt8500_enable_ms
,
595 .break_ctl
= vt8500_break_ctl
,
596 .startup
= vt8500_startup
,
597 .shutdown
= vt8500_shutdown
,
598 .set_termios
= vt8500_set_termios
,
600 .release_port
= vt8500_release_port
,
601 .request_port
= vt8500_request_port
,
602 .config_port
= vt8500_config_port
,
603 .verify_port
= vt8500_verify_port
,
604 #ifdef CONFIG_CONSOLE_POLL
605 .poll_get_char
= vt8500_get_poll_char
,
606 .poll_put_char
= vt8500_put_poll_char
,
610 static struct uart_driver vt8500_uart_driver
= {
611 .owner
= THIS_MODULE
,
612 .driver_name
= "vt8500_serial",
613 .dev_name
= "ttyWMT",
615 .cons
= VT8500_CONSOLE
,
618 static unsigned int vt8500_flags
; /* none required so far */
619 static unsigned int wm8880_flags
= VT8500_HAS_SWRTSCTS_SWITCH
;
621 static const struct of_device_id wmt_dt_ids
[] = {
622 { .compatible
= "via,vt8500-uart", .data
= &vt8500_flags
},
623 { .compatible
= "wm,wm8880-uart", .data
= &wm8880_flags
},
627 static int vt8500_serial_probe(struct platform_device
*pdev
)
629 struct vt8500_port
*vt8500_port
;
630 struct resource
*mmres
, *irqres
;
631 struct device_node
*np
= pdev
->dev
.of_node
;
632 const struct of_device_id
*match
;
633 const unsigned int *flags
;
637 match
= of_match_device(wmt_dt_ids
, &pdev
->dev
);
643 mmres
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
644 irqres
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
645 if (!mmres
|| !irqres
)
649 port
= of_alias_get_id(np
, "serial");
650 if (port
>= VT8500_MAX_PORTS
)
657 /* calculate the port id */
658 port
= find_first_zero_bit(vt8500_ports_in_use
,
662 if (port
>= VT8500_MAX_PORTS
)
665 /* reserve the port id */
666 if (test_and_set_bit(port
, vt8500_ports_in_use
)) {
667 /* port already in use - shouldn't really happen */
671 vt8500_port
= devm_kzalloc(&pdev
->dev
, sizeof(struct vt8500_port
),
676 vt8500_port
->uart
.membase
= devm_ioremap_resource(&pdev
->dev
, mmres
);
677 if (IS_ERR(vt8500_port
->uart
.membase
))
678 return PTR_ERR(vt8500_port
->uart
.membase
);
680 vt8500_port
->clk
= of_clk_get(pdev
->dev
.of_node
, 0);
681 if (IS_ERR(vt8500_port
->clk
)) {
682 dev_err(&pdev
->dev
, "failed to get clock\n");
686 ret
= clk_prepare_enable(vt8500_port
->clk
);
688 dev_err(&pdev
->dev
, "failed to enable clock\n");
692 vt8500_port
->vt8500_uart_flags
= *flags
;
693 vt8500_port
->clk_predivisor
= DIV_ROUND_CLOSEST(
694 clk_get_rate(vt8500_port
->clk
),
695 VT8500_RECOMMENDED_CLK
697 vt8500_port
->uart
.type
= PORT_VT8500
;
698 vt8500_port
->uart
.iotype
= UPIO_MEM
;
699 vt8500_port
->uart
.mapbase
= mmres
->start
;
700 vt8500_port
->uart
.irq
= irqres
->start
;
701 vt8500_port
->uart
.fifosize
= 16;
702 vt8500_port
->uart
.ops
= &vt8500_uart_pops
;
703 vt8500_port
->uart
.line
= port
;
704 vt8500_port
->uart
.dev
= &pdev
->dev
;
705 vt8500_port
->uart
.flags
= UPF_IOREMAP
| UPF_BOOT_AUTOCONF
;
707 /* Serial core uses the magic "16" everywhere - adjust for it */
708 vt8500_port
->uart
.uartclk
= 16 * clk_get_rate(vt8500_port
->clk
) /
709 vt8500_port
->clk_predivisor
/
710 VT8500_OVERSAMPLING_DIVISOR
;
712 snprintf(vt8500_port
->name
, sizeof(vt8500_port
->name
),
713 "VT8500 UART%d", pdev
->id
);
715 vt8500_uart_ports
[port
] = vt8500_port
;
717 uart_add_one_port(&vt8500_uart_driver
, &vt8500_port
->uart
);
719 platform_set_drvdata(pdev
, vt8500_port
);
724 static struct platform_driver vt8500_platform_driver
= {
725 .probe
= vt8500_serial_probe
,
727 .name
= "vt8500_serial",
728 .of_match_table
= wmt_dt_ids
,
729 .suppress_bind_attrs
= true,
733 static int __init
vt8500_serial_init(void)
737 ret
= uart_register_driver(&vt8500_uart_driver
);
741 ret
= platform_driver_register(&vt8500_platform_driver
);
744 uart_unregister_driver(&vt8500_uart_driver
);
748 device_initcall(vt8500_serial_init
);