1 // SPDX-License-Identifier: GPL-2.0
3 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 * Copyright (C) 2004 Infineon IFAP DC COM CPE
6 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
7 * Copyright (C) 2007 John Crispin <john@phrozen.org>
8 * Copyright (C) 2010 Thomas Langer, <thomas.langer@lantiq.com>
11 #include <linux/slab.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/console.h>
15 #include <linux/sysrq.h>
16 #include <linux/device.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/of_platform.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
25 #include <linux/clk.h>
26 #include <linux/gpio.h>
28 #include <lantiq_soc.h>
30 #define PORT_LTQ_ASC 111
32 #define UART_DUMMY_UER_RX 1
33 #define DRVNAME "lantiq,asc"
35 #define LTQ_ASC_TBUF (0x0020 + 3)
36 #define LTQ_ASC_RBUF (0x0024 + 3)
38 #define LTQ_ASC_TBUF 0x0020
39 #define LTQ_ASC_RBUF 0x0024
41 #define LTQ_ASC_FSTAT 0x0048
42 #define LTQ_ASC_WHBSTATE 0x0018
43 #define LTQ_ASC_STATE 0x0014
44 #define LTQ_ASC_IRNCR 0x00F8
45 #define LTQ_ASC_CLC 0x0000
46 #define LTQ_ASC_ID 0x0008
47 #define LTQ_ASC_PISEL 0x0004
48 #define LTQ_ASC_TXFCON 0x0044
49 #define LTQ_ASC_RXFCON 0x0040
50 #define LTQ_ASC_CON 0x0010
51 #define LTQ_ASC_BG 0x0050
52 #define LTQ_ASC_IRNREN 0x00F4
54 #define ASC_IRNREN_TX 0x1
55 #define ASC_IRNREN_RX 0x2
56 #define ASC_IRNREN_ERR 0x4
57 #define ASC_IRNREN_TX_BUF 0x8
58 #define ASC_IRNCR_TIR 0x1
59 #define ASC_IRNCR_RIR 0x2
60 #define ASC_IRNCR_EIR 0x4
62 #define ASCOPT_CSIZE 0x3
65 #define ASCCLC_DISS 0x2
66 #define ASCCLC_RMCMASK 0x0000FF00
67 #define ASCCLC_RMCOFFSET 8
68 #define ASCCON_M_8ASYNC 0x0
69 #define ASCCON_M_7ASYNC 0x2
70 #define ASCCON_ODD 0x00000020
71 #define ASCCON_STP 0x00000080
72 #define ASCCON_BRS 0x00000100
73 #define ASCCON_FDE 0x00000200
74 #define ASCCON_R 0x00008000
75 #define ASCCON_FEN 0x00020000
76 #define ASCCON_ROEN 0x00080000
77 #define ASCCON_TOEN 0x00100000
78 #define ASCSTATE_PE 0x00010000
79 #define ASCSTATE_FE 0x00020000
80 #define ASCSTATE_ROE 0x00080000
81 #define ASCSTATE_ANY (ASCSTATE_ROE|ASCSTATE_PE|ASCSTATE_FE)
82 #define ASCWHBSTATE_CLRREN 0x00000001
83 #define ASCWHBSTATE_SETREN 0x00000002
84 #define ASCWHBSTATE_CLRPE 0x00000004
85 #define ASCWHBSTATE_CLRFE 0x00000008
86 #define ASCWHBSTATE_CLRROE 0x00000020
87 #define ASCTXFCON_TXFEN 0x0001
88 #define ASCTXFCON_TXFFLU 0x0002
89 #define ASCTXFCON_TXFITLMASK 0x3F00
90 #define ASCTXFCON_TXFITLOFF 8
91 #define ASCRXFCON_RXFEN 0x0001
92 #define ASCRXFCON_RXFFLU 0x0002
93 #define ASCRXFCON_RXFITLMASK 0x3F00
94 #define ASCRXFCON_RXFITLOFF 8
95 #define ASCFSTAT_RXFFLMASK 0x003F
96 #define ASCFSTAT_TXFFLMASK 0x3F00
97 #define ASCFSTAT_TXFREEMASK 0x3F000000
98 #define ASCFSTAT_TXFREEOFF 24
100 static void lqasc_tx_chars(struct uart_port
*port
);
101 static struct ltq_uart_port
*lqasc_port
[MAXPORTS
];
102 static struct uart_driver lqasc_reg
;
103 static DEFINE_SPINLOCK(ltq_asc_lock
);
105 struct ltq_uart_port
{
106 struct uart_port port
;
107 /* clock used to derive divider */
109 /* clock gating of the ASC core */
113 unsigned int err_irq
;
117 ltq_uart_port
*to_ltq_uart_port(struct uart_port
*port
)
119 return container_of(port
, struct ltq_uart_port
, port
);
123 lqasc_stop_tx(struct uart_port
*port
)
129 lqasc_start_tx(struct uart_port
*port
)
132 spin_lock_irqsave(<q_asc_lock
, flags
);
133 lqasc_tx_chars(port
);
134 spin_unlock_irqrestore(<q_asc_lock
, flags
);
139 lqasc_stop_rx(struct uart_port
*port
)
141 ltq_w32(ASCWHBSTATE_CLRREN
, port
->membase
+ LTQ_ASC_WHBSTATE
);
145 lqasc_rx_chars(struct uart_port
*port
)
147 struct tty_port
*tport
= &port
->state
->port
;
148 unsigned int ch
= 0, rsr
= 0, fifocnt
;
150 fifocnt
= ltq_r32(port
->membase
+ LTQ_ASC_FSTAT
) & ASCFSTAT_RXFFLMASK
;
152 u8 flag
= TTY_NORMAL
;
153 ch
= ltq_r8(port
->membase
+ LTQ_ASC_RBUF
);
154 rsr
= (ltq_r32(port
->membase
+ LTQ_ASC_STATE
)
155 & ASCSTATE_ANY
) | UART_DUMMY_UER_RX
;
156 tty_flip_buffer_push(tport
);
160 * Note that the error handling code is
161 * out of the main execution path
163 if (rsr
& ASCSTATE_ANY
) {
164 if (rsr
& ASCSTATE_PE
) {
165 port
->icount
.parity
++;
166 ltq_w32_mask(0, ASCWHBSTATE_CLRPE
,
167 port
->membase
+ LTQ_ASC_WHBSTATE
);
168 } else if (rsr
& ASCSTATE_FE
) {
169 port
->icount
.frame
++;
170 ltq_w32_mask(0, ASCWHBSTATE_CLRFE
,
171 port
->membase
+ LTQ_ASC_WHBSTATE
);
173 if (rsr
& ASCSTATE_ROE
) {
174 port
->icount
.overrun
++;
175 ltq_w32_mask(0, ASCWHBSTATE_CLRROE
,
176 port
->membase
+ LTQ_ASC_WHBSTATE
);
179 rsr
&= port
->read_status_mask
;
181 if (rsr
& ASCSTATE_PE
)
183 else if (rsr
& ASCSTATE_FE
)
187 if ((rsr
& port
->ignore_status_mask
) == 0)
188 tty_insert_flip_char(tport
, ch
, flag
);
190 if (rsr
& ASCSTATE_ROE
)
192 * Overrun is special, since it's reported
193 * immediately, and doesn't affect the current
196 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
200 tty_flip_buffer_push(tport
);
206 lqasc_tx_chars(struct uart_port
*port
)
208 struct circ_buf
*xmit
= &port
->state
->xmit
;
209 if (uart_tx_stopped(port
)) {
214 while (((ltq_r32(port
->membase
+ LTQ_ASC_FSTAT
) &
215 ASCFSTAT_TXFREEMASK
) >> ASCFSTAT_TXFREEOFF
) != 0) {
217 ltq_w8(port
->x_char
, port
->membase
+ LTQ_ASC_TBUF
);
223 if (uart_circ_empty(xmit
))
226 ltq_w8(port
->state
->xmit
.buf
[port
->state
->xmit
.tail
],
227 port
->membase
+ LTQ_ASC_TBUF
);
228 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
232 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
233 uart_write_wakeup(port
);
237 lqasc_tx_int(int irq
, void *_port
)
240 struct uart_port
*port
= (struct uart_port
*)_port
;
241 spin_lock_irqsave(<q_asc_lock
, flags
);
242 ltq_w32(ASC_IRNCR_TIR
, port
->membase
+ LTQ_ASC_IRNCR
);
243 spin_unlock_irqrestore(<q_asc_lock
, flags
);
244 lqasc_start_tx(port
);
249 lqasc_err_int(int irq
, void *_port
)
252 struct uart_port
*port
= (struct uart_port
*)_port
;
253 spin_lock_irqsave(<q_asc_lock
, flags
);
254 /* clear any pending interrupts */
255 ltq_w32_mask(0, ASCWHBSTATE_CLRPE
| ASCWHBSTATE_CLRFE
|
256 ASCWHBSTATE_CLRROE
, port
->membase
+ LTQ_ASC_WHBSTATE
);
257 spin_unlock_irqrestore(<q_asc_lock
, flags
);
262 lqasc_rx_int(int irq
, void *_port
)
265 struct uart_port
*port
= (struct uart_port
*)_port
;
266 spin_lock_irqsave(<q_asc_lock
, flags
);
267 ltq_w32(ASC_IRNCR_RIR
, port
->membase
+ LTQ_ASC_IRNCR
);
268 lqasc_rx_chars(port
);
269 spin_unlock_irqrestore(<q_asc_lock
, flags
);
274 lqasc_tx_empty(struct uart_port
*port
)
277 status
= ltq_r32(port
->membase
+ LTQ_ASC_FSTAT
) & ASCFSTAT_TXFFLMASK
;
278 return status
? 0 : TIOCSER_TEMT
;
282 lqasc_get_mctrl(struct uart_port
*port
)
284 return TIOCM_CTS
| TIOCM_CAR
| TIOCM_DSR
;
288 lqasc_set_mctrl(struct uart_port
*port
, u_int mctrl
)
293 lqasc_break_ctl(struct uart_port
*port
, int break_state
)
298 lqasc_startup(struct uart_port
*port
)
300 struct ltq_uart_port
*ltq_port
= to_ltq_uart_port(port
);
303 if (!IS_ERR(ltq_port
->clk
))
304 clk_enable(ltq_port
->clk
);
305 port
->uartclk
= clk_get_rate(ltq_port
->fpiclk
);
307 ltq_w32_mask(ASCCLC_DISS
| ASCCLC_RMCMASK
, (1 << ASCCLC_RMCOFFSET
),
308 port
->membase
+ LTQ_ASC_CLC
);
310 ltq_w32(0, port
->membase
+ LTQ_ASC_PISEL
);
312 ((TXFIFO_FL
<< ASCTXFCON_TXFITLOFF
) & ASCTXFCON_TXFITLMASK
) |
313 ASCTXFCON_TXFEN
| ASCTXFCON_TXFFLU
,
314 port
->membase
+ LTQ_ASC_TXFCON
);
316 ((RXFIFO_FL
<< ASCRXFCON_RXFITLOFF
) & ASCRXFCON_RXFITLMASK
)
317 | ASCRXFCON_RXFEN
| ASCRXFCON_RXFFLU
,
318 port
->membase
+ LTQ_ASC_RXFCON
);
319 /* make sure other settings are written to hardware before
320 * setting enable bits
323 ltq_w32_mask(0, ASCCON_M_8ASYNC
| ASCCON_FEN
| ASCCON_TOEN
|
324 ASCCON_ROEN
, port
->membase
+ LTQ_ASC_CON
);
326 retval
= request_irq(ltq_port
->tx_irq
, lqasc_tx_int
,
329 pr_err("failed to request lqasc_tx_int\n");
333 retval
= request_irq(ltq_port
->rx_irq
, lqasc_rx_int
,
336 pr_err("failed to request lqasc_rx_int\n");
340 retval
= request_irq(ltq_port
->err_irq
, lqasc_err_int
,
343 pr_err("failed to request lqasc_err_int\n");
347 ltq_w32(ASC_IRNREN_RX
| ASC_IRNREN_ERR
| ASC_IRNREN_TX
,
348 port
->membase
+ LTQ_ASC_IRNREN
);
352 free_irq(ltq_port
->rx_irq
, port
);
354 free_irq(ltq_port
->tx_irq
, port
);
359 lqasc_shutdown(struct uart_port
*port
)
361 struct ltq_uart_port
*ltq_port
= to_ltq_uart_port(port
);
362 free_irq(ltq_port
->tx_irq
, port
);
363 free_irq(ltq_port
->rx_irq
, port
);
364 free_irq(ltq_port
->err_irq
, port
);
366 ltq_w32(0, port
->membase
+ LTQ_ASC_CON
);
367 ltq_w32_mask(ASCRXFCON_RXFEN
, ASCRXFCON_RXFFLU
,
368 port
->membase
+ LTQ_ASC_RXFCON
);
369 ltq_w32_mask(ASCTXFCON_TXFEN
, ASCTXFCON_TXFFLU
,
370 port
->membase
+ LTQ_ASC_TXFCON
);
371 if (!IS_ERR(ltq_port
->clk
))
372 clk_disable(ltq_port
->clk
);
376 lqasc_set_termios(struct uart_port
*port
,
377 struct ktermios
*new, struct ktermios
*old
)
381 unsigned int divisor
;
383 unsigned int con
= 0;
386 cflag
= new->c_cflag
;
387 iflag
= new->c_iflag
;
389 switch (cflag
& CSIZE
) {
391 con
= ASCCON_M_7ASYNC
;
397 new->c_cflag
&= ~ CSIZE
;
399 con
= ASCCON_M_8ASYNC
;
403 cflag
&= ~CMSPAR
; /* Mark/Space parity is not supported */
408 if (cflag
& PARENB
) {
409 if (!(cflag
& PARODD
))
415 port
->read_status_mask
= ASCSTATE_ROE
;
417 port
->read_status_mask
|= ASCSTATE_FE
| ASCSTATE_PE
;
419 port
->ignore_status_mask
= 0;
421 port
->ignore_status_mask
|= ASCSTATE_FE
| ASCSTATE_PE
;
423 if (iflag
& IGNBRK
) {
425 * If we're ignoring parity and break indicators,
426 * ignore overruns too (for real raw support).
429 port
->ignore_status_mask
|= ASCSTATE_ROE
;
432 if ((cflag
& CREAD
) == 0)
433 port
->ignore_status_mask
|= UART_DUMMY_UER_RX
;
435 /* set error signals - framing, parity and overrun, enable receiver */
436 con
|= ASCCON_FEN
| ASCCON_TOEN
| ASCCON_ROEN
;
438 spin_lock_irqsave(<q_asc_lock
, flags
);
441 ltq_w32_mask(0, con
, port
->membase
+ LTQ_ASC_CON
);
443 /* Set baud rate - take a divider of 2 into account */
444 baud
= uart_get_baud_rate(port
, new, old
, 0, port
->uartclk
/ 16);
445 divisor
= uart_get_divisor(port
, baud
);
446 divisor
= divisor
/ 2 - 1;
448 /* disable the baudrate generator */
449 ltq_w32_mask(ASCCON_R
, 0, port
->membase
+ LTQ_ASC_CON
);
451 /* make sure the fractional divider is off */
452 ltq_w32_mask(ASCCON_FDE
, 0, port
->membase
+ LTQ_ASC_CON
);
454 /* set up to use divisor of 2 */
455 ltq_w32_mask(ASCCON_BRS
, 0, port
->membase
+ LTQ_ASC_CON
);
457 /* now we can write the new baudrate into the register */
458 ltq_w32(divisor
, port
->membase
+ LTQ_ASC_BG
);
460 /* turn the baudrate generator back on */
461 ltq_w32_mask(0, ASCCON_R
, port
->membase
+ LTQ_ASC_CON
);
464 ltq_w32(ASCWHBSTATE_SETREN
, port
->membase
+ LTQ_ASC_WHBSTATE
);
466 spin_unlock_irqrestore(<q_asc_lock
, flags
);
468 /* Don't rewrite B0 */
469 if (tty_termios_baud_rate(new))
470 tty_termios_encode_baud_rate(new, baud
, baud
);
472 uart_update_timeout(port
, cflag
, baud
);
476 lqasc_type(struct uart_port
*port
)
478 if (port
->type
== PORT_LTQ_ASC
)
485 lqasc_release_port(struct uart_port
*port
)
487 struct platform_device
*pdev
= to_platform_device(port
->dev
);
489 if (port
->flags
& UPF_IOREMAP
) {
490 devm_iounmap(&pdev
->dev
, port
->membase
);
491 port
->membase
= NULL
;
496 lqasc_request_port(struct uart_port
*port
)
498 struct platform_device
*pdev
= to_platform_device(port
->dev
);
499 struct resource
*res
;
502 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
504 dev_err(&pdev
->dev
, "cannot obtain I/O memory region");
507 size
= resource_size(res
);
509 res
= devm_request_mem_region(&pdev
->dev
, res
->start
,
510 size
, dev_name(&pdev
->dev
));
512 dev_err(&pdev
->dev
, "cannot request I/O memory region");
516 if (port
->flags
& UPF_IOREMAP
) {
517 port
->membase
= devm_ioremap_nocache(&pdev
->dev
,
518 port
->mapbase
, size
);
519 if (port
->membase
== NULL
)
526 lqasc_config_port(struct uart_port
*port
, int flags
)
528 if (flags
& UART_CONFIG_TYPE
) {
529 port
->type
= PORT_LTQ_ASC
;
530 lqasc_request_port(port
);
535 lqasc_verify_port(struct uart_port
*port
,
536 struct serial_struct
*ser
)
539 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_LTQ_ASC
)
541 if (ser
->irq
< 0 || ser
->irq
>= NR_IRQS
)
543 if (ser
->baud_base
< 9600)
548 static const struct uart_ops lqasc_pops
= {
549 .tx_empty
= lqasc_tx_empty
,
550 .set_mctrl
= lqasc_set_mctrl
,
551 .get_mctrl
= lqasc_get_mctrl
,
552 .stop_tx
= lqasc_stop_tx
,
553 .start_tx
= lqasc_start_tx
,
554 .stop_rx
= lqasc_stop_rx
,
555 .break_ctl
= lqasc_break_ctl
,
556 .startup
= lqasc_startup
,
557 .shutdown
= lqasc_shutdown
,
558 .set_termios
= lqasc_set_termios
,
560 .release_port
= lqasc_release_port
,
561 .request_port
= lqasc_request_port
,
562 .config_port
= lqasc_config_port
,
563 .verify_port
= lqasc_verify_port
,
567 lqasc_console_putchar(struct uart_port
*port
, int ch
)
575 fifofree
= (ltq_r32(port
->membase
+ LTQ_ASC_FSTAT
)
576 & ASCFSTAT_TXFREEMASK
) >> ASCFSTAT_TXFREEOFF
;
577 } while (fifofree
== 0);
578 ltq_w8(ch
, port
->membase
+ LTQ_ASC_TBUF
);
581 static void lqasc_serial_port_write(struct uart_port
*port
, const char *s
,
586 spin_lock_irqsave(<q_asc_lock
, flags
);
587 uart_console_write(port
, s
, count
, lqasc_console_putchar
);
588 spin_unlock_irqrestore(<q_asc_lock
, flags
);
592 lqasc_console_write(struct console
*co
, const char *s
, u_int count
)
594 struct ltq_uart_port
*ltq_port
;
596 if (co
->index
>= MAXPORTS
)
599 ltq_port
= lqasc_port
[co
->index
];
603 lqasc_serial_port_write(<q_port
->port
, s
, count
);
607 lqasc_console_setup(struct console
*co
, char *options
)
609 struct ltq_uart_port
*ltq_port
;
610 struct uart_port
*port
;
616 if (co
->index
>= MAXPORTS
)
619 ltq_port
= lqasc_port
[co
->index
];
623 port
= <q_port
->port
;
625 if (!IS_ERR(ltq_port
->clk
))
626 clk_enable(ltq_port
->clk
);
628 port
->uartclk
= clk_get_rate(ltq_port
->fpiclk
);
631 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
632 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
635 static struct console lqasc_console
= {
637 .write
= lqasc_console_write
,
638 .device
= uart_console_device
,
639 .setup
= lqasc_console_setup
,
640 .flags
= CON_PRINTBUFFER
,
646 lqasc_console_init(void)
648 register_console(&lqasc_console
);
651 console_initcall(lqasc_console_init
);
653 static void lqasc_serial_early_console_write(struct console
*co
,
657 struct earlycon_device
*dev
= co
->data
;
659 lqasc_serial_port_write(&dev
->port
, s
, count
);
663 lqasc_serial_early_console_setup(struct earlycon_device
*device
,
666 if (!device
->port
.membase
)
669 device
->con
->write
= lqasc_serial_early_console_write
;
672 OF_EARLYCON_DECLARE(lantiq
, DRVNAME
, lqasc_serial_early_console_setup
);
674 static struct uart_driver lqasc_reg
= {
675 .owner
= THIS_MODULE
,
676 .driver_name
= DRVNAME
,
677 .dev_name
= "ttyLTQ",
681 .cons
= &lqasc_console
,
685 lqasc_probe(struct platform_device
*pdev
)
687 struct device_node
*node
= pdev
->dev
.of_node
;
688 struct ltq_uart_port
*ltq_port
;
689 struct uart_port
*port
;
690 struct resource
*mmres
, irqres
[3];
694 mmres
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
695 ret
= of_irq_to_resource_table(node
, irqres
, 3);
696 if (!mmres
|| (ret
!= 3)) {
698 "failed to get memory/irq for serial port\n");
702 /* check if this is the console port */
703 if (mmres
->start
!= CPHYSADDR(LTQ_EARLY_ASC
))
706 if (lqasc_port
[line
]) {
707 dev_err(&pdev
->dev
, "port %d already allocated\n", line
);
711 ltq_port
= devm_kzalloc(&pdev
->dev
, sizeof(struct ltq_uart_port
),
716 port
= <q_port
->port
;
718 port
->iotype
= SERIAL_IO_MEM
;
719 port
->flags
= UPF_BOOT_AUTOCONF
| UPF_IOREMAP
;
720 port
->ops
= &lqasc_pops
;
722 port
->type
= PORT_LTQ_ASC
,
724 port
->dev
= &pdev
->dev
;
725 /* unused, just to be backward-compatible */
726 port
->irq
= irqres
[0].start
;
727 port
->mapbase
= mmres
->start
;
729 ltq_port
->fpiclk
= clk_get_fpi();
730 if (IS_ERR(ltq_port
->fpiclk
)) {
731 pr_err("failed to get fpi clk\n");
735 /* not all asc ports have clock gates, lets ignore the return code */
736 ltq_port
->clk
= clk_get(&pdev
->dev
, NULL
);
738 ltq_port
->tx_irq
= irqres
[0].start
;
739 ltq_port
->rx_irq
= irqres
[1].start
;
740 ltq_port
->err_irq
= irqres
[2].start
;
742 lqasc_port
[line
] = ltq_port
;
743 platform_set_drvdata(pdev
, ltq_port
);
745 ret
= uart_add_one_port(&lqasc_reg
, port
);
750 static const struct of_device_id ltq_asc_match
[] = {
751 { .compatible
= DRVNAME
},
755 static struct platform_driver lqasc_driver
= {
758 .of_match_table
= ltq_asc_match
,
767 ret
= uart_register_driver(&lqasc_reg
);
771 ret
= platform_driver_probe(&lqasc_driver
, lqasc_probe
);
773 uart_unregister_driver(&lqasc_reg
);
777 device_initcall(init_lqasc
);