1 // SPDX-License-Identifier: GPL-2.0
3 * Sunplus SoC UART driver
5 * Author: Hammer Hsieh <hammerh0314@gmail.com>
7 * Note1: This driver is 8250-like uart, but are not register compatible.
9 * Note2: On some buses, for preventing data incoherence, must do a read
10 * for ensure write made it to hardware. In this driver, function startup
11 * and shutdown did not do a read but only do a write directly. For what?
12 * In Sunplus bus communication between memory bus and peripheral bus with
13 * posted write, it will send a specific command after last write command
14 * to make sure write done. Then memory bus identify the specific command
15 * and send done signal back to master device. After master device received
16 * done signal, then proceed next write command. It is no need to do a read
19 #include <linux/clk.h>
20 #include <linux/console.h>
21 #include <linux/interrupt.h>
23 #include <linux/iopoll.h>
24 #include <linux/module.h>
26 #include <linux/of_platform.h>
27 #include <linux/platform_device.h>
28 #include <linux/reset.h>
29 #include <linux/serial_core.h>
30 #include <linux/serial_reg.h>
31 #include <linux/sysrq.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
36 /* Register offsets */
37 #define SUP_UART_DATA 0x00
38 #define SUP_UART_LSR 0x04
39 #define SUP_UART_MSR 0x08
40 #define SUP_UART_LCR 0x0C
41 #define SUP_UART_MCR 0x10
42 #define SUP_UART_DIV_L 0x14
43 #define SUP_UART_DIV_H 0x18
44 #define SUP_UART_ISC 0x1C
45 #define SUP_UART_TX_RESIDUE 0x20
46 #define SUP_UART_RX_RESIDUE 0x24
48 /* Line Status Register bits */
49 #define SUP_UART_LSR_BC BIT(5) /* break condition status */
50 #define SUP_UART_LSR_FE BIT(4) /* frame error status */
51 #define SUP_UART_LSR_OE BIT(3) /* overrun error status */
52 #define SUP_UART_LSR_PE BIT(2) /* parity error status */
53 #define SUP_UART_LSR_RX BIT(1) /* 1: receive fifo not empty */
54 #define SUP_UART_LSR_TX BIT(0) /* 1: transmit fifo is not full */
55 #define SUP_UART_LSR_TX_NOT_FULL 1
56 #define SUP_UART_LSR_BRK_ERROR_BITS GENMASK(5, 2)
58 /* Line Control Register bits */
59 #define SUP_UART_LCR_SBC BIT(5) /* select break condition */
61 /* Modem Control Register bits */
62 #define SUP_UART_MCR_RI BIT(3) /* ring indicator */
63 #define SUP_UART_MCR_DCD BIT(2) /* data carrier detect */
65 /* Interrupt Status/Control Register bits */
66 #define SUP_UART_ISC_RXM BIT(5) /* RX interrupt enable */
67 #define SUP_UART_ISC_TXM BIT(4) /* TX interrupt enable */
68 #define SUP_UART_ISC_RX BIT(1) /* RX interrupt status */
69 #define SUP_UART_ISC_TX BIT(0) /* TX interrupt status */
71 #define SUP_DUMMY_READ BIT(16) /* drop bytes received on a !CREAD port */
74 struct sunplus_uart_port
{
75 struct uart_port port
;
77 struct reset_control
*rstc
;
80 static void sp_uart_put_char(struct uart_port
*port
, unsigned int ch
)
82 writel(ch
, port
->membase
+ SUP_UART_DATA
);
85 static u32
sunplus_tx_buf_not_full(struct uart_port
*port
)
87 unsigned int lsr
= readl(port
->membase
+ SUP_UART_LSR
);
89 return (lsr
& SUP_UART_LSR_TX
) ? SUP_UART_LSR_TX_NOT_FULL
: 0;
92 static unsigned int sunplus_tx_empty(struct uart_port
*port
)
94 unsigned int lsr
= readl(port
->membase
+ SUP_UART_LSR
);
96 return (lsr
& UART_LSR_TEMT
) ? TIOCSER_TEMT
: 0;
99 static void sunplus_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
101 unsigned int mcr
= readl(port
->membase
+ SUP_UART_MCR
);
103 if (mctrl
& TIOCM_DTR
)
106 mcr
&= ~UART_MCR_DTR
;
108 if (mctrl
& TIOCM_RTS
)
111 mcr
&= ~UART_MCR_RTS
;
113 if (mctrl
& TIOCM_CAR
)
114 mcr
|= SUP_UART_MCR_DCD
;
116 mcr
&= ~SUP_UART_MCR_DCD
;
118 if (mctrl
& TIOCM_RI
)
119 mcr
|= SUP_UART_MCR_RI
;
121 mcr
&= ~SUP_UART_MCR_RI
;
123 if (mctrl
& TIOCM_LOOP
)
124 mcr
|= UART_MCR_LOOP
;
126 mcr
&= ~UART_MCR_LOOP
;
128 writel(mcr
, port
->membase
+ SUP_UART_MCR
);
131 static unsigned int sunplus_get_mctrl(struct uart_port
*port
)
133 unsigned int mcr
, ret
= 0;
135 mcr
= readl(port
->membase
+ SUP_UART_MCR
);
137 if (mcr
& UART_MCR_DTR
)
140 if (mcr
& UART_MCR_RTS
)
143 if (mcr
& SUP_UART_MCR_DCD
)
146 if (mcr
& SUP_UART_MCR_RI
)
149 if (mcr
& UART_MCR_LOOP
)
155 static void sunplus_stop_tx(struct uart_port
*port
)
159 isc
= readl(port
->membase
+ SUP_UART_ISC
);
160 isc
&= ~SUP_UART_ISC_TXM
;
161 writel(isc
, port
->membase
+ SUP_UART_ISC
);
164 static void sunplus_start_tx(struct uart_port
*port
)
168 isc
= readl(port
->membase
+ SUP_UART_ISC
);
169 isc
|= SUP_UART_ISC_TXM
;
170 writel(isc
, port
->membase
+ SUP_UART_ISC
);
173 static void sunplus_stop_rx(struct uart_port
*port
)
177 isc
= readl(port
->membase
+ SUP_UART_ISC
);
178 isc
&= ~SUP_UART_ISC_RXM
;
179 writel(isc
, port
->membase
+ SUP_UART_ISC
);
182 static void sunplus_break_ctl(struct uart_port
*port
, int ctl
)
187 uart_port_lock_irqsave(port
, &flags
);
189 lcr
= readl(port
->membase
+ SUP_UART_LCR
);
192 lcr
|= SUP_UART_LCR_SBC
; /* start break */
194 lcr
&= ~SUP_UART_LCR_SBC
; /* stop break */
196 writel(lcr
, port
->membase
+ SUP_UART_LCR
);
198 uart_port_unlock_irqrestore(port
, flags
);
201 static void transmit_chars(struct uart_port
*port
)
203 struct tty_port
*tport
= &port
->state
->port
;
206 sp_uart_put_char(port
, port
->x_char
);
212 if (kfifo_is_empty(&tport
->xmit_fifo
) || uart_tx_stopped(port
)) {
213 sunplus_stop_tx(port
);
220 if (!uart_fifo_get(port
, &ch
))
223 sp_uart_put_char(port
, ch
);
224 } while (sunplus_tx_buf_not_full(port
));
226 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
227 uart_write_wakeup(port
);
229 if (kfifo_is_empty(&tport
->xmit_fifo
))
230 sunplus_stop_tx(port
);
233 static void receive_chars(struct uart_port
*port
)
235 unsigned int lsr
= readl(port
->membase
+ SUP_UART_LSR
);
239 ch
= readl(port
->membase
+ SUP_UART_DATA
);
243 if (unlikely(lsr
& SUP_UART_LSR_BRK_ERROR_BITS
)) {
244 if (lsr
& SUP_UART_LSR_BC
) {
245 lsr
&= ~(SUP_UART_LSR_FE
| SUP_UART_LSR_PE
);
248 if (uart_handle_break(port
))
250 } else if (lsr
& SUP_UART_LSR_PE
) {
251 port
->icount
.parity
++;
253 } else if (lsr
& SUP_UART_LSR_FE
) {
254 port
->icount
.frame
++;
258 if (lsr
& SUP_UART_LSR_OE
)
259 port
->icount
.overrun
++;
262 if (port
->ignore_status_mask
& SUP_DUMMY_READ
)
265 if (uart_prepare_sysrq_char(port
, ch
))
268 uart_insert_char(port
, lsr
, SUP_UART_LSR_OE
, ch
, flag
);
271 lsr
= readl(port
->membase
+ SUP_UART_LSR
);
272 } while (lsr
& SUP_UART_LSR_RX
);
274 tty_flip_buffer_push(&port
->state
->port
);
277 static irqreturn_t
sunplus_uart_irq(int irq
, void *args
)
279 struct uart_port
*port
= args
;
282 uart_port_lock(port
);
284 isc
= readl(port
->membase
+ SUP_UART_ISC
);
286 if (isc
& SUP_UART_ISC_RX
)
289 if (isc
& SUP_UART_ISC_TX
)
290 transmit_chars(port
);
292 uart_unlock_and_check_sysrq(port
);
297 static int sunplus_startup(struct uart_port
*port
)
300 unsigned int isc
= 0;
303 ret
= request_irq(port
->irq
, sunplus_uart_irq
, 0, "sunplus_uart", port
);
307 uart_port_lock_irqsave(port
, &flags
);
308 /* isc define Bit[7:4] int setting, Bit[3:0] int status
309 * isc register will clean Bit[3:0] int status after read
310 * only do a write to Bit[7:4] int setting
312 isc
|= SUP_UART_ISC_RXM
;
313 writel(isc
, port
->membase
+ SUP_UART_ISC
);
314 uart_port_unlock_irqrestore(port
, flags
);
319 static void sunplus_shutdown(struct uart_port
*port
)
323 uart_port_lock_irqsave(port
, &flags
);
324 /* isc define Bit[7:4] int setting, Bit[3:0] int status
325 * isc register will clean Bit[3:0] int status after read
326 * only do a write to Bit[7:4] int setting
328 writel(0, port
->membase
+ SUP_UART_ISC
); /* disable all interrupt */
329 uart_port_unlock_irqrestore(port
, flags
);
331 free_irq(port
->irq
, port
);
334 static void sunplus_set_termios(struct uart_port
*port
,
335 struct ktermios
*termios
,
336 const struct ktermios
*oldtermios
)
338 u32 ext
, div
, div_l
, div_h
, baud
, lcr
;
339 u32 clk
= port
->uartclk
;
342 baud
= uart_get_baud_rate(port
, termios
, oldtermios
, 0, port
->uartclk
/ 16);
344 /* baud rate = uartclk / ((16 * divisor + 1) + divisor_ext) */
348 div
= (div
>> 4) - 1;
349 div_l
= (div
& 0xFF) | (ext
<< 12);
352 switch (termios
->c_cflag
& CSIZE
) {
354 lcr
= UART_LCR_WLEN5
;
357 lcr
= UART_LCR_WLEN6
;
360 lcr
= UART_LCR_WLEN7
;
363 lcr
= UART_LCR_WLEN8
;
367 if (termios
->c_cflag
& CSTOPB
)
368 lcr
|= UART_LCR_STOP
;
370 if (termios
->c_cflag
& PARENB
) {
371 lcr
|= UART_LCR_PARITY
;
373 if (!(termios
->c_cflag
& PARODD
))
374 lcr
|= UART_LCR_EPAR
;
377 uart_port_lock_irqsave(port
, &flags
);
379 uart_update_timeout(port
, termios
->c_cflag
, baud
);
381 port
->read_status_mask
= 0;
382 if (termios
->c_iflag
& INPCK
)
383 port
->read_status_mask
|= SUP_UART_LSR_PE
| SUP_UART_LSR_FE
;
385 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
386 port
->read_status_mask
|= SUP_UART_LSR_BC
;
388 /* Characters to ignore */
389 port
->ignore_status_mask
= 0;
390 if (termios
->c_iflag
& IGNPAR
)
391 port
->ignore_status_mask
|= SUP_UART_LSR_FE
| SUP_UART_LSR_PE
;
393 if (termios
->c_iflag
& IGNBRK
) {
394 port
->ignore_status_mask
|= SUP_UART_LSR_BC
;
396 if (termios
->c_iflag
& IGNPAR
)
397 port
->ignore_status_mask
|= SUP_UART_LSR_OE
;
400 /* Ignore all characters if CREAD is not set */
401 if ((termios
->c_cflag
& CREAD
) == 0) {
402 port
->ignore_status_mask
|= SUP_DUMMY_READ
;
403 /* flush rx data FIFO */
404 writel(0, port
->membase
+ SUP_UART_RX_RESIDUE
);
407 /* Settings for baud rate divisor and lcr */
408 writel(div_h
, port
->membase
+ SUP_UART_DIV_H
);
409 writel(div_l
, port
->membase
+ SUP_UART_DIV_L
);
410 writel(lcr
, port
->membase
+ SUP_UART_LCR
);
412 uart_port_unlock_irqrestore(port
, flags
);
415 static void sunplus_set_ldisc(struct uart_port
*port
, struct ktermios
*termios
)
417 int new = termios
->c_line
;
420 port
->flags
|= UPF_HARDPPS_CD
;
422 port
->flags
&= ~UPF_HARDPPS_CD
;
425 static const char *sunplus_type(struct uart_port
*port
)
427 return port
->type
== PORT_SUNPLUS
? "sunplus_uart" : NULL
;
430 static void sunplus_config_port(struct uart_port
*port
, int type
)
432 if (type
& UART_CONFIG_TYPE
)
433 port
->type
= PORT_SUNPLUS
;
436 static int sunplus_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
438 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_SUNPLUS
)
444 #if defined(CONFIG_SERIAL_SUNPLUS_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
445 static void wait_for_xmitr(struct uart_port
*port
)
450 /* Wait while FIFO is full or timeout */
451 ret
= readl_poll_timeout_atomic(port
->membase
+ SUP_UART_LSR
, val
,
452 (val
& SUP_UART_LSR_TX
), 1, 10000);
454 if (ret
== -ETIMEDOUT
) {
455 dev_err(port
->dev
, "Timeout waiting while UART TX FULL\n");
461 #ifdef CONFIG_CONSOLE_POLL
462 static void sunplus_poll_put_char(struct uart_port
*port
, unsigned char data
)
464 wait_for_xmitr(port
);
465 sp_uart_put_char(port
, data
);
468 static int sunplus_poll_get_char(struct uart_port
*port
)
470 unsigned int lsr
= readl(port
->membase
+ SUP_UART_LSR
);
472 if (!(lsr
& SUP_UART_LSR_RX
))
475 return readl(port
->membase
+ SUP_UART_DATA
);
479 static const struct uart_ops sunplus_uart_ops
= {
480 .tx_empty
= sunplus_tx_empty
,
481 .set_mctrl
= sunplus_set_mctrl
,
482 .get_mctrl
= sunplus_get_mctrl
,
483 .stop_tx
= sunplus_stop_tx
,
484 .start_tx
= sunplus_start_tx
,
485 .stop_rx
= sunplus_stop_rx
,
486 .break_ctl
= sunplus_break_ctl
,
487 .startup
= sunplus_startup
,
488 .shutdown
= sunplus_shutdown
,
489 .set_termios
= sunplus_set_termios
,
490 .set_ldisc
= sunplus_set_ldisc
,
491 .type
= sunplus_type
,
492 .config_port
= sunplus_config_port
,
493 .verify_port
= sunplus_verify_port
,
494 #ifdef CONFIG_CONSOLE_POLL
495 .poll_put_char
= sunplus_poll_put_char
,
496 .poll_get_char
= sunplus_poll_get_char
,
500 #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
501 static struct sunplus_uart_port
*sunplus_console_ports
[SUP_UART_NR
];
503 static void sunplus_uart_console_putchar(struct uart_port
*port
,
506 wait_for_xmitr(port
);
507 sp_uart_put_char(port
, ch
);
510 static void sunplus_console_write(struct console
*co
,
517 if (oops_in_progress
)
518 locked
= uart_port_trylock_irqsave(&sunplus_console_ports
[co
->index
]->port
, &flags
);
520 uart_port_lock_irqsave(&sunplus_console_ports
[co
->index
]->port
, &flags
);
522 uart_console_write(&sunplus_console_ports
[co
->index
]->port
, s
, count
,
523 sunplus_uart_console_putchar
);
526 uart_port_unlock_irqrestore(&sunplus_console_ports
[co
->index
]->port
, flags
);
529 static int __init
sunplus_console_setup(struct console
*co
, char *options
)
531 struct sunplus_uart_port
*sup
;
537 if (co
->index
< 0 || co
->index
>= SUP_UART_NR
)
540 sup
= sunplus_console_ports
[co
->index
];
545 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
547 return uart_set_options(&sup
->port
, co
, baud
, parity
, bits
, flow
);
550 static struct uart_driver sunplus_uart_driver
;
551 static struct console sunplus_uart_console
= {
553 .write
= sunplus_console_write
,
554 .device
= uart_console_device
,
555 .setup
= sunplus_console_setup
,
556 .flags
= CON_PRINTBUFFER
,
558 .data
= &sunplus_uart_driver
561 #define SERIAL_SUNPLUS_CONSOLE (&sunplus_uart_console)
563 #define SERIAL_SUNPLUS_CONSOLE NULL
566 static struct uart_driver sunplus_uart_driver
= {
567 .owner
= THIS_MODULE
,
568 .driver_name
= "sunplus_uart",
569 .dev_name
= "ttySUP",
573 .cons
= SERIAL_SUNPLUS_CONSOLE
,
576 static void sunplus_uart_disable_unprepare(void *data
)
578 clk_disable_unprepare(data
);
581 static void sunplus_uart_reset_control_assert(void *data
)
583 reset_control_assert(data
);
586 static int sunplus_uart_probe(struct platform_device
*pdev
)
588 struct sunplus_uart_port
*sup
;
589 struct uart_port
*port
;
590 struct resource
*res
;
593 pdev
->id
= of_alias_get_id(pdev
->dev
.of_node
, "serial");
595 if (pdev
->id
< 0 || pdev
->id
>= SUP_UART_NR
)
598 sup
= devm_kzalloc(&pdev
->dev
, sizeof(*sup
), GFP_KERNEL
);
602 sup
->clk
= devm_clk_get_optional(&pdev
->dev
, NULL
);
603 if (IS_ERR(sup
->clk
))
604 return dev_err_probe(&pdev
->dev
, PTR_ERR(sup
->clk
), "clk not found\n");
606 ret
= clk_prepare_enable(sup
->clk
);
610 ret
= devm_add_action_or_reset(&pdev
->dev
, sunplus_uart_disable_unprepare
, sup
->clk
);
614 sup
->rstc
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
615 if (IS_ERR(sup
->rstc
))
616 return dev_err_probe(&pdev
->dev
, PTR_ERR(sup
->rstc
), "rstc not found\n");
620 port
->membase
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
621 if (IS_ERR(port
->membase
))
622 return dev_err_probe(&pdev
->dev
, PTR_ERR(port
->membase
), "membase not found\n");
624 irq
= platform_get_irq(pdev
, 0);
628 port
->mapbase
= res
->start
;
629 port
->uartclk
= clk_get_rate(sup
->clk
);
630 port
->line
= pdev
->id
;
632 port
->dev
= &pdev
->dev
;
633 port
->iotype
= UPIO_MEM
;
634 port
->ops
= &sunplus_uart_ops
;
635 port
->flags
= UPF_BOOT_AUTOCONF
;
636 port
->fifosize
= 128;
638 ret
= reset_control_deassert(sup
->rstc
);
642 ret
= devm_add_action_or_reset(&pdev
->dev
, sunplus_uart_reset_control_assert
, sup
->rstc
);
646 #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
647 sunplus_console_ports
[sup
->port
.line
] = sup
;
650 platform_set_drvdata(pdev
, &sup
->port
);
652 ret
= uart_add_one_port(&sunplus_uart_driver
, &sup
->port
);
653 #ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
655 sunplus_console_ports
[sup
->port
.line
] = NULL
;
661 static void sunplus_uart_remove(struct platform_device
*pdev
)
663 struct sunplus_uart_port
*sup
= platform_get_drvdata(pdev
);
665 uart_remove_one_port(&sunplus_uart_driver
, &sup
->port
);
668 static int __maybe_unused
sunplus_uart_suspend(struct device
*dev
)
670 struct sunplus_uart_port
*sup
= dev_get_drvdata(dev
);
672 if (!uart_console(&sup
->port
))
673 uart_suspend_port(&sunplus_uart_driver
, &sup
->port
);
678 static int __maybe_unused
sunplus_uart_resume(struct device
*dev
)
680 struct sunplus_uart_port
*sup
= dev_get_drvdata(dev
);
682 if (!uart_console(&sup
->port
))
683 uart_resume_port(&sunplus_uart_driver
, &sup
->port
);
688 static const struct dev_pm_ops sunplus_uart_pm_ops
= {
689 SET_SYSTEM_SLEEP_PM_OPS(sunplus_uart_suspend
, sunplus_uart_resume
)
692 static const struct of_device_id sp_uart_of_match
[] = {
693 { .compatible
= "sunplus,sp7021-uart" },
696 MODULE_DEVICE_TABLE(of
, sp_uart_of_match
);
698 static struct platform_driver sunplus_uart_platform_driver
= {
699 .probe
= sunplus_uart_probe
,
700 .remove
= sunplus_uart_remove
,
702 .name
= "sunplus_uart",
703 .of_match_table
= sp_uart_of_match
,
704 .pm
= &sunplus_uart_pm_ops
,
708 static int __init
sunplus_uart_init(void)
712 ret
= uart_register_driver(&sunplus_uart_driver
);
716 ret
= platform_driver_register(&sunplus_uart_platform_driver
);
718 uart_unregister_driver(&sunplus_uart_driver
);
722 module_init(sunplus_uart_init
);
724 static void __exit
sunplus_uart_exit(void)
726 platform_driver_unregister(&sunplus_uart_platform_driver
);
727 uart_unregister_driver(&sunplus_uart_driver
);
729 module_exit(sunplus_uart_exit
);
731 #ifdef CONFIG_SERIAL_EARLYCON
732 static void sunplus_uart_putc(struct uart_port
*port
, unsigned char c
)
737 ret
= readl_poll_timeout_atomic(port
->membase
+ SUP_UART_LSR
, val
,
738 (val
& UART_LSR_TEMT
), 1, 10000);
742 writel(c
, port
->membase
+ SUP_UART_DATA
);
745 static void sunplus_uart_early_write(struct console
*con
, const char *s
, unsigned int n
)
747 struct earlycon_device
*dev
= con
->data
;
749 uart_console_write(&dev
->port
, s
, n
, sunplus_uart_putc
);
753 sunplus_uart_early_setup(struct earlycon_device
*dev
, const char *opt
)
755 if (!(dev
->port
.membase
|| dev
->port
.iobase
))
758 dev
->con
->write
= sunplus_uart_early_write
;
762 OF_EARLYCON_DECLARE(sunplus_uart
, "sunplus,sp7021-uart", sunplus_uart_early_setup
);
765 MODULE_DESCRIPTION("Sunplus UART driver");
766 MODULE_AUTHOR("Hammer Hsieh <hammerh0314@gmail.com>");
767 MODULE_LICENSE("GPL v2");