1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012-2015 Spreadtrum Communications Inc.
6 #if defined(CONFIG_SERIAL_SPRD_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
10 #include <linux/clk.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
14 #include <linux/ioport.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/serial_core.h>
20 #include <linux/serial.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
27 #define SPRD_TTY_NAME "ttyS"
28 #define SPRD_FIFO_SIZE 128
29 #define SPRD_DEF_RATE 26000000
30 #define SPRD_BAUD_IO_LIMIT 3000000
31 #define SPRD_TIMEOUT 256000
33 /* the offset of serial registers and BITs for them */
35 #define SPRD_TXD 0x0000
36 #define SPRD_RXD 0x0004
38 /* line status register and its BITs */
39 #define SPRD_LSR 0x0008
40 #define SPRD_LSR_OE BIT(4)
41 #define SPRD_LSR_FE BIT(3)
42 #define SPRD_LSR_PE BIT(2)
43 #define SPRD_LSR_BI BIT(7)
44 #define SPRD_LSR_TX_OVER BIT(15)
46 /* data number in TX and RX fifo */
47 #define SPRD_STS1 0x000C
49 /* interrupt enable register and its BITs */
50 #define SPRD_IEN 0x0010
51 #define SPRD_IEN_RX_FULL BIT(0)
52 #define SPRD_IEN_TX_EMPTY BIT(1)
53 #define SPRD_IEN_BREAK_DETECT BIT(7)
54 #define SPRD_IEN_TIMEOUT BIT(13)
56 /* interrupt clear register */
57 #define SPRD_ICLR 0x0014
58 #define SPRD_ICLR_TIMEOUT BIT(13)
60 /* line control register */
61 #define SPRD_LCR 0x0018
62 #define SPRD_LCR_STOP_1BIT 0x10
63 #define SPRD_LCR_STOP_2BIT 0x30
64 #define SPRD_LCR_DATA_LEN (BIT(2) | BIT(3))
65 #define SPRD_LCR_DATA_LEN5 0x0
66 #define SPRD_LCR_DATA_LEN6 0x4
67 #define SPRD_LCR_DATA_LEN7 0x8
68 #define SPRD_LCR_DATA_LEN8 0xc
69 #define SPRD_LCR_PARITY (BIT(0) | BIT(1))
70 #define SPRD_LCR_PARITY_EN 0x2
71 #define SPRD_LCR_EVEN_PAR 0x0
72 #define SPRD_LCR_ODD_PAR 0x1
74 /* control register 1 */
75 #define SPRD_CTL1 0x001C
76 #define RX_HW_FLOW_CTL_THLD BIT(6)
77 #define RX_HW_FLOW_CTL_EN BIT(7)
78 #define TX_HW_FLOW_CTL_EN BIT(8)
79 #define RX_TOUT_THLD_DEF 0x3E00
80 #define RX_HFC_THLD_DEF 0x40
82 /* fifo threshold register */
83 #define SPRD_CTL2 0x0020
84 #define THLD_TX_EMPTY 0x40
85 #define THLD_RX_FULL 0x40
87 /* config baud rate register */
88 #define SPRD_CLKD0 0x0024
89 #define SPRD_CLKD1 0x0028
91 /* interrupt mask status register */
92 #define SPRD_IMSR 0x002C
93 #define SPRD_IMSR_RX_FIFO_FULL BIT(0)
94 #define SPRD_IMSR_TX_FIFO_EMPTY BIT(1)
95 #define SPRD_IMSR_BREAK_DETECT BIT(7)
96 #define SPRD_IMSR_TIMEOUT BIT(13)
108 struct sprd_uart_port
{
109 struct uart_port port
;
110 struct reg_backup reg_bak
;
114 static struct sprd_uart_port
*sprd_port
[UART_NR_MAX
];
115 static int sprd_ports_num
;
117 static inline unsigned int serial_in(struct uart_port
*port
, int offset
)
119 return readl_relaxed(port
->membase
+ offset
);
122 static inline void serial_out(struct uart_port
*port
, int offset
, int value
)
124 writel_relaxed(value
, port
->membase
+ offset
);
127 static unsigned int sprd_tx_empty(struct uart_port
*port
)
129 if (serial_in(port
, SPRD_STS1
) & 0xff00)
135 static unsigned int sprd_get_mctrl(struct uart_port
*port
)
137 return TIOCM_DSR
| TIOCM_CTS
;
140 static void sprd_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
145 static void sprd_stop_tx(struct uart_port
*port
)
147 unsigned int ien
, iclr
;
149 iclr
= serial_in(port
, SPRD_ICLR
);
150 ien
= serial_in(port
, SPRD_IEN
);
152 iclr
|= SPRD_IEN_TX_EMPTY
;
153 ien
&= ~SPRD_IEN_TX_EMPTY
;
155 serial_out(port
, SPRD_ICLR
, iclr
);
156 serial_out(port
, SPRD_IEN
, ien
);
159 static void sprd_start_tx(struct uart_port
*port
)
163 ien
= serial_in(port
, SPRD_IEN
);
164 if (!(ien
& SPRD_IEN_TX_EMPTY
)) {
165 ien
|= SPRD_IEN_TX_EMPTY
;
166 serial_out(port
, SPRD_IEN
, ien
);
170 static void sprd_stop_rx(struct uart_port
*port
)
172 unsigned int ien
, iclr
;
174 iclr
= serial_in(port
, SPRD_ICLR
);
175 ien
= serial_in(port
, SPRD_IEN
);
177 ien
&= ~(SPRD_IEN_RX_FULL
| SPRD_IEN_BREAK_DETECT
);
178 iclr
|= SPRD_IEN_RX_FULL
| SPRD_IEN_BREAK_DETECT
;
180 serial_out(port
, SPRD_IEN
, ien
);
181 serial_out(port
, SPRD_ICLR
, iclr
);
184 /* The Sprd serial does not support this function. */
185 static void sprd_break_ctl(struct uart_port
*port
, int break_state
)
190 static int handle_lsr_errors(struct uart_port
*port
,
197 if (*lsr
& SPRD_LSR_BI
) {
198 *lsr
&= ~(SPRD_LSR_FE
| SPRD_LSR_PE
);
200 ret
= uart_handle_break(port
);
203 } else if (*lsr
& SPRD_LSR_PE
)
204 port
->icount
.parity
++;
205 else if (*lsr
& SPRD_LSR_FE
)
206 port
->icount
.frame
++;
207 if (*lsr
& SPRD_LSR_OE
)
208 port
->icount
.overrun
++;
210 /* mask off conditions which should be ignored */
211 *lsr
&= port
->read_status_mask
;
212 if (*lsr
& SPRD_LSR_BI
)
214 else if (*lsr
& SPRD_LSR_PE
)
216 else if (*lsr
& SPRD_LSR_FE
)
222 static inline void sprd_rx(struct uart_port
*port
)
224 struct tty_port
*tty
= &port
->state
->port
;
225 unsigned int ch
, flag
, lsr
, max_count
= SPRD_TIMEOUT
;
227 while ((serial_in(port
, SPRD_STS1
) & 0x00ff) && max_count
--) {
228 lsr
= serial_in(port
, SPRD_LSR
);
229 ch
= serial_in(port
, SPRD_RXD
);
233 if (lsr
& (SPRD_LSR_BI
| SPRD_LSR_PE
|
234 SPRD_LSR_FE
| SPRD_LSR_OE
))
235 if (handle_lsr_errors(port
, &lsr
, &flag
))
237 if (uart_handle_sysrq_char(port
, ch
))
240 uart_insert_char(port
, lsr
, SPRD_LSR_OE
, ch
, flag
);
243 tty_flip_buffer_push(tty
);
246 static inline void sprd_tx(struct uart_port
*port
)
248 struct circ_buf
*xmit
= &port
->state
->xmit
;
252 serial_out(port
, SPRD_TXD
, port
->x_char
);
258 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
263 count
= THLD_TX_EMPTY
;
265 serial_out(port
, SPRD_TXD
, xmit
->buf
[xmit
->tail
]);
266 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
268 if (uart_circ_empty(xmit
))
270 } while (--count
> 0);
272 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
273 uart_write_wakeup(port
);
275 if (uart_circ_empty(xmit
))
279 /* this handles the interrupt from one port */
280 static irqreturn_t
sprd_handle_irq(int irq
, void *dev_id
)
282 struct uart_port
*port
= dev_id
;
285 spin_lock(&port
->lock
);
287 ims
= serial_in(port
, SPRD_IMSR
);
290 spin_unlock(&port
->lock
);
294 if (ims
& SPRD_IMSR_TIMEOUT
)
295 serial_out(port
, SPRD_ICLR
, SPRD_ICLR_TIMEOUT
);
297 if (ims
& (SPRD_IMSR_RX_FIFO_FULL
|
298 SPRD_IMSR_BREAK_DETECT
| SPRD_IMSR_TIMEOUT
))
301 if (ims
& SPRD_IMSR_TX_FIFO_EMPTY
)
304 spin_unlock(&port
->lock
);
309 static int sprd_startup(struct uart_port
*port
)
312 unsigned int ien
, fc
;
313 unsigned int timeout
;
314 struct sprd_uart_port
*sp
;
317 serial_out(port
, SPRD_CTL2
, ((THLD_TX_EMPTY
<< 8) | THLD_RX_FULL
));
320 timeout
= SPRD_TIMEOUT
;
321 while (timeout
-- && serial_in(port
, SPRD_STS1
) & 0x00ff)
322 serial_in(port
, SPRD_RXD
);
325 timeout
= SPRD_TIMEOUT
;
326 while (timeout
-- && serial_in(port
, SPRD_STS1
) & 0xff00)
329 /* clear interrupt */
330 serial_out(port
, SPRD_IEN
, 0);
331 serial_out(port
, SPRD_ICLR
, ~0);
334 sp
= container_of(port
, struct sprd_uart_port
, port
);
335 snprintf(sp
->name
, sizeof(sp
->name
), "sprd_serial%d", port
->line
);
336 ret
= devm_request_irq(port
->dev
, port
->irq
, sprd_handle_irq
,
337 IRQF_SHARED
, sp
->name
, port
);
339 dev_err(port
->dev
, "fail to request serial irq %d, ret=%d\n",
343 fc
= serial_in(port
, SPRD_CTL1
);
344 fc
|= RX_TOUT_THLD_DEF
| RX_HFC_THLD_DEF
;
345 serial_out(port
, SPRD_CTL1
, fc
);
347 /* enable interrupt */
348 spin_lock_irqsave(&port
->lock
, flags
);
349 ien
= serial_in(port
, SPRD_IEN
);
350 ien
|= SPRD_IEN_RX_FULL
| SPRD_IEN_BREAK_DETECT
| SPRD_IEN_TIMEOUT
;
351 serial_out(port
, SPRD_IEN
, ien
);
352 spin_unlock_irqrestore(&port
->lock
, flags
);
357 static void sprd_shutdown(struct uart_port
*port
)
359 serial_out(port
, SPRD_IEN
, 0);
360 serial_out(port
, SPRD_ICLR
, ~0);
361 devm_free_irq(port
->dev
, port
->irq
, port
);
364 static void sprd_set_termios(struct uart_port
*port
,
365 struct ktermios
*termios
,
366 struct ktermios
*old
)
368 unsigned int baud
, quot
;
369 unsigned int lcr
= 0, fc
;
372 /* ask the core to calculate the divisor for us */
373 baud
= uart_get_baud_rate(port
, termios
, old
, 0, SPRD_BAUD_IO_LIMIT
);
375 quot
= (unsigned int)((port
->uartclk
+ baud
/ 2) / baud
);
377 /* set data length */
378 switch (termios
->c_cflag
& CSIZE
) {
380 lcr
|= SPRD_LCR_DATA_LEN5
;
383 lcr
|= SPRD_LCR_DATA_LEN6
;
386 lcr
|= SPRD_LCR_DATA_LEN7
;
390 lcr
|= SPRD_LCR_DATA_LEN8
;
394 /* calculate stop bits */
395 lcr
&= ~(SPRD_LCR_STOP_1BIT
| SPRD_LCR_STOP_2BIT
);
396 if (termios
->c_cflag
& CSTOPB
)
397 lcr
|= SPRD_LCR_STOP_2BIT
;
399 lcr
|= SPRD_LCR_STOP_1BIT
;
401 /* calculate parity */
402 lcr
&= ~SPRD_LCR_PARITY
;
403 termios
->c_cflag
&= ~CMSPAR
; /* no support mark/space */
404 if (termios
->c_cflag
& PARENB
) {
405 lcr
|= SPRD_LCR_PARITY_EN
;
406 if (termios
->c_cflag
& PARODD
)
407 lcr
|= SPRD_LCR_ODD_PAR
;
409 lcr
|= SPRD_LCR_EVEN_PAR
;
412 spin_lock_irqsave(&port
->lock
, flags
);
414 /* update the per-port timeout */
415 uart_update_timeout(port
, termios
->c_cflag
, baud
);
417 port
->read_status_mask
= SPRD_LSR_OE
;
418 if (termios
->c_iflag
& INPCK
)
419 port
->read_status_mask
|= SPRD_LSR_FE
| SPRD_LSR_PE
;
420 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
421 port
->read_status_mask
|= SPRD_LSR_BI
;
423 /* characters to ignore */
424 port
->ignore_status_mask
= 0;
425 if (termios
->c_iflag
& IGNPAR
)
426 port
->ignore_status_mask
|= SPRD_LSR_PE
| SPRD_LSR_FE
;
427 if (termios
->c_iflag
& IGNBRK
) {
428 port
->ignore_status_mask
|= SPRD_LSR_BI
;
430 * If we're ignoring parity and break indicators,
431 * ignore overruns too (for real raw support).
433 if (termios
->c_iflag
& IGNPAR
)
434 port
->ignore_status_mask
|= SPRD_LSR_OE
;
438 fc
= serial_in(port
, SPRD_CTL1
);
439 fc
&= ~(RX_HW_FLOW_CTL_THLD
| RX_HW_FLOW_CTL_EN
| TX_HW_FLOW_CTL_EN
);
440 if (termios
->c_cflag
& CRTSCTS
) {
441 fc
|= RX_HW_FLOW_CTL_THLD
;
442 fc
|= RX_HW_FLOW_CTL_EN
;
443 fc
|= TX_HW_FLOW_CTL_EN
;
446 /* clock divider bit0~bit15 */
447 serial_out(port
, SPRD_CLKD0
, quot
& 0xffff);
449 /* clock divider bit16~bit20 */
450 serial_out(port
, SPRD_CLKD1
, (quot
& 0x1f0000) >> 16);
451 serial_out(port
, SPRD_LCR
, lcr
);
452 fc
|= RX_TOUT_THLD_DEF
| RX_HFC_THLD_DEF
;
453 serial_out(port
, SPRD_CTL1
, fc
);
455 spin_unlock_irqrestore(&port
->lock
, flags
);
457 /* Don't rewrite B0 */
458 if (tty_termios_baud_rate(termios
))
459 tty_termios_encode_baud_rate(termios
, baud
, baud
);
462 static const char *sprd_type(struct uart_port
*port
)
467 static void sprd_release_port(struct uart_port
*port
)
472 static int sprd_request_port(struct uart_port
*port
)
477 static void sprd_config_port(struct uart_port
*port
, int flags
)
479 if (flags
& UART_CONFIG_TYPE
)
480 port
->type
= PORT_SPRD
;
483 static int sprd_verify_port(struct uart_port
*port
,
484 struct serial_struct
*ser
)
486 if (ser
->type
!= PORT_SPRD
)
488 if (port
->irq
!= ser
->irq
)
490 if (port
->iotype
!= ser
->io_type
)
495 static const struct uart_ops serial_sprd_ops
= {
496 .tx_empty
= sprd_tx_empty
,
497 .get_mctrl
= sprd_get_mctrl
,
498 .set_mctrl
= sprd_set_mctrl
,
499 .stop_tx
= sprd_stop_tx
,
500 .start_tx
= sprd_start_tx
,
501 .stop_rx
= sprd_stop_rx
,
502 .break_ctl
= sprd_break_ctl
,
503 .startup
= sprd_startup
,
504 .shutdown
= sprd_shutdown
,
505 .set_termios
= sprd_set_termios
,
507 .release_port
= sprd_release_port
,
508 .request_port
= sprd_request_port
,
509 .config_port
= sprd_config_port
,
510 .verify_port
= sprd_verify_port
,
513 #ifdef CONFIG_SERIAL_SPRD_CONSOLE
514 static void wait_for_xmitr(struct uart_port
*port
)
516 unsigned int status
, tmout
= 10000;
518 /* wait up to 10ms for the character(s) to be sent */
520 status
= serial_in(port
, SPRD_STS1
);
524 } while (status
& 0xff00);
527 static void sprd_console_putchar(struct uart_port
*port
, int ch
)
529 wait_for_xmitr(port
);
530 serial_out(port
, SPRD_TXD
, ch
);
533 static void sprd_console_write(struct console
*co
, const char *s
,
536 struct uart_port
*port
= &sprd_port
[co
->index
]->port
;
542 else if (oops_in_progress
)
543 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
545 spin_lock_irqsave(&port
->lock
, flags
);
547 uart_console_write(port
, s
, count
, sprd_console_putchar
);
549 /* wait for transmitter to become empty */
550 wait_for_xmitr(port
);
553 spin_unlock_irqrestore(&port
->lock
, flags
);
556 static int __init
sprd_console_setup(struct console
*co
, char *options
)
558 struct uart_port
*port
;
564 if (co
->index
>= UART_NR_MAX
|| co
->index
< 0)
567 port
= &sprd_port
[co
->index
]->port
;
569 pr_info("serial port %d not yet initialized\n", co
->index
);
573 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
575 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
578 static struct uart_driver sprd_uart_driver
;
579 static struct console sprd_console
= {
580 .name
= SPRD_TTY_NAME
,
581 .write
= sprd_console_write
,
582 .device
= uart_console_device
,
583 .setup
= sprd_console_setup
,
584 .flags
= CON_PRINTBUFFER
,
586 .data
= &sprd_uart_driver
,
589 #define SPRD_CONSOLE (&sprd_console)
591 /* Support for earlycon */
592 static void sprd_putc(struct uart_port
*port
, int c
)
594 unsigned int timeout
= SPRD_TIMEOUT
;
597 !(readl(port
->membase
+ SPRD_LSR
) & SPRD_LSR_TX_OVER
))
600 writeb(c
, port
->membase
+ SPRD_TXD
);
603 static void sprd_early_write(struct console
*con
, const char *s
,
606 struct earlycon_device
*dev
= con
->data
;
608 uart_console_write(&dev
->port
, s
, n
, sprd_putc
);
611 static int __init
sprd_early_console_setup(
612 struct earlycon_device
*device
,
615 if (!device
->port
.membase
)
618 device
->con
->write
= sprd_early_write
;
621 OF_EARLYCON_DECLARE(sprd_serial
, "sprd,sc9836-uart",
622 sprd_early_console_setup
);
624 #else /* !CONFIG_SERIAL_SPRD_CONSOLE */
625 #define SPRD_CONSOLE NULL
628 static struct uart_driver sprd_uart_driver
= {
629 .owner
= THIS_MODULE
,
630 .driver_name
= "sprd_serial",
631 .dev_name
= SPRD_TTY_NAME
,
635 .cons
= SPRD_CONSOLE
,
638 static int sprd_probe_dt_alias(int index
, struct device
*dev
)
640 struct device_node
*np
;
643 if (!IS_ENABLED(CONFIG_OF
))
650 ret
= of_alias_get_id(np
, "serial");
653 else if (ret
>= ARRAY_SIZE(sprd_port
) || sprd_port
[ret
] != NULL
) {
654 dev_warn(dev
, "requested serial port %d not available.\n", ret
);
661 static int sprd_remove(struct platform_device
*dev
)
663 struct sprd_uart_port
*sup
= platform_get_drvdata(dev
);
666 uart_remove_one_port(&sprd_uart_driver
, &sup
->port
);
667 sprd_port
[sup
->port
.line
] = NULL
;
672 uart_unregister_driver(&sprd_uart_driver
);
677 static int sprd_probe(struct platform_device
*pdev
)
679 struct resource
*res
;
680 struct uart_port
*up
;
686 for (index
= 0; index
< ARRAY_SIZE(sprd_port
); index
++)
687 if (sprd_port
[index
] == NULL
)
690 if (index
== ARRAY_SIZE(sprd_port
))
693 index
= sprd_probe_dt_alias(index
, &pdev
->dev
);
695 sprd_port
[index
] = devm_kzalloc(&pdev
->dev
,
696 sizeof(*sprd_port
[index
]), GFP_KERNEL
);
697 if (!sprd_port
[index
])
700 up
= &sprd_port
[index
]->port
;
701 up
->dev
= &pdev
->dev
;
703 up
->type
= PORT_SPRD
;
704 up
->iotype
= UPIO_MEM
;
705 up
->uartclk
= SPRD_DEF_RATE
;
706 up
->fifosize
= SPRD_FIFO_SIZE
;
707 up
->ops
= &serial_sprd_ops
;
708 up
->flags
= UPF_BOOT_AUTOCONF
;
710 clk
= devm_clk_get(&pdev
->dev
, NULL
);
711 if (!IS_ERR_OR_NULL(clk
))
712 up
->uartclk
= clk_get_rate(clk
);
714 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
716 dev_err(&pdev
->dev
, "not provide mem resource\n");
719 up
->mapbase
= res
->start
;
720 up
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
721 if (IS_ERR(up
->membase
))
722 return PTR_ERR(up
->membase
);
724 irq
= platform_get_irq(pdev
, 0);
726 dev_err(&pdev
->dev
, "not provide irq resource: %d\n", irq
);
731 if (!sprd_ports_num
) {
732 ret
= uart_register_driver(&sprd_uart_driver
);
734 pr_err("Failed to register SPRD-UART driver\n");
740 ret
= uart_add_one_port(&sprd_uart_driver
, up
);
742 sprd_port
[index
] = NULL
;
746 platform_set_drvdata(pdev
, up
);
751 #ifdef CONFIG_PM_SLEEP
752 static int sprd_suspend(struct device
*dev
)
754 struct sprd_uart_port
*sup
= dev_get_drvdata(dev
);
756 uart_suspend_port(&sprd_uart_driver
, &sup
->port
);
761 static int sprd_resume(struct device
*dev
)
763 struct sprd_uart_port
*sup
= dev_get_drvdata(dev
);
765 uart_resume_port(&sprd_uart_driver
, &sup
->port
);
771 static SIMPLE_DEV_PM_OPS(sprd_pm_ops
, sprd_suspend
, sprd_resume
);
773 static const struct of_device_id serial_ids
[] = {
774 {.compatible
= "sprd,sc9836-uart",},
777 MODULE_DEVICE_TABLE(of
, serial_ids
);
779 static struct platform_driver sprd_platform_driver
= {
781 .remove
= sprd_remove
,
783 .name
= "sprd_serial",
784 .of_match_table
= of_match_ptr(serial_ids
),
789 module_platform_driver(sprd_platform_driver
);
791 MODULE_LICENSE("GPL v2");
792 MODULE_DESCRIPTION("Spreadtrum SoC serial driver series");