xref: /openbmc/linux/drivers/tty/serial/8250/8250_fsl.c (revision 6db6b729)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Freescale 16550 UART "driver", Copyright (C) 2011 Paul Gortmaker.
4  * Copyright 2020 NXP
5  * Copyright 2020 Puresoftware Ltd.
6  *
7  * This isn't a full driver; it just provides an alternate IRQ
8  * handler to deal with an errata and provide ACPI wrapper.
9  * Everything else is just using the bog standard 8250 support.
10  *
11  * We follow code flow of serial8250_default_handle_irq() but add
12  * a check for a break and insert a dummy read on the Rx for the
13  * immediately following IRQ event.
14  *
15  * We re-use the already existing "bug handling" lsr_saved_flags
16  * field to carry the "what we just did" information from the one
17  * IRQ event to the next one.
18  */
19 
20 #include <linux/acpi.h>
21 #include <linux/serial_reg.h>
22 #include <linux/serial_8250.h>
23 
24 #include "8250.h"
25 
26 int fsl8250_handle_irq(struct uart_port *port)
27 {
28 	unsigned long flags;
29 	u16 lsr, orig_lsr;
30 	unsigned int iir;
31 	struct uart_8250_port *up = up_to_u8250p(port);
32 
33 	spin_lock_irqsave(&up->port.lock, flags);
34 
35 	iir = port->serial_in(port, UART_IIR);
36 	if (iir & UART_IIR_NO_INT) {
37 		spin_unlock_irqrestore(&up->port.lock, flags);
38 		return 0;
39 	}
40 
41 	/*
42 	 * For a single break the hardware reports LSR.BI for each character
43 	 * time. This is described in the MPC8313E chip errata as "General17".
44 	 * A typical break has a duration of 0.3s, with a 115200n8 configuration
45 	 * that (theoretically) corresponds to ~3500 interrupts in these 0.3s.
46 	 * In practise it's less (around 500) because of hardware
47 	 * and software latencies. The workaround recommended by the vendor is
48 	 * to read the RX register (to clear LSR.DR and thus prevent a FIFO
49 	 * aging interrupt). To prevent the irq from retriggering LSR must not be
50 	 * read. (This would clear LSR.BI, hardware would reassert the BI event
51 	 * immediately and interrupt the CPU again. The hardware clears LSR.BI
52 	 * when the next valid char is read.)
53 	 */
54 	if (unlikely(up->lsr_saved_flags & UART_LSR_BI)) {
55 		up->lsr_saved_flags &= ~UART_LSR_BI;
56 		port->serial_in(port, UART_RX);
57 		spin_unlock_irqrestore(&up->port.lock, flags);
58 		return 1;
59 	}
60 
61 	lsr = orig_lsr = up->port.serial_in(&up->port, UART_LSR);
62 
63 	/* Process incoming characters first */
64 	if ((lsr & (UART_LSR_DR | UART_LSR_BI)) &&
65 	    (up->ier & (UART_IER_RLSI | UART_IER_RDI))) {
66 		lsr = serial8250_rx_chars(up, lsr);
67 	}
68 
69 	/* Stop processing interrupts on input overrun */
70 	if ((orig_lsr & UART_LSR_OE) && (up->overrun_backoff_time_ms > 0)) {
71 		unsigned long delay;
72 
73 		up->ier = port->serial_in(port, UART_IER);
74 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
75 			port->ops->stop_rx(port);
76 		} else {
77 			/* Keep restarting the timer until
78 			 * the input overrun subsides.
79 			 */
80 			cancel_delayed_work(&up->overrun_backoff);
81 		}
82 
83 		delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
84 		schedule_delayed_work(&up->overrun_backoff, delay);
85 	}
86 
87 	serial8250_modem_status(up);
88 
89 	if ((lsr & UART_LSR_THRE) && (up->ier & UART_IER_THRI))
90 		serial8250_tx_chars(up);
91 
92 	up->lsr_saved_flags |= orig_lsr & UART_LSR_BI;
93 
94 	uart_unlock_and_check_sysrq_irqrestore(&up->port, flags);
95 
96 	return 1;
97 }
98 EXPORT_SYMBOL_GPL(fsl8250_handle_irq);
99 
100 #ifdef CONFIG_ACPI
101 struct fsl8250_data {
102 	int	line;
103 };
104 
105 static int fsl8250_acpi_probe(struct platform_device *pdev)
106 {
107 	struct fsl8250_data *data;
108 	struct uart_8250_port port8250;
109 	struct device *dev = &pdev->dev;
110 	struct resource *regs;
111 
112 	int ret, irq;
113 
114 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115 	if (!regs) {
116 		dev_err(dev, "no registers defined\n");
117 		return -EINVAL;
118 	}
119 
120 	irq = platform_get_irq(pdev, 0);
121 	if (irq < 0)
122 		return irq;
123 
124 	memset(&port8250, 0, sizeof(port8250));
125 
126 	ret = device_property_read_u32(dev, "clock-frequency",
127 					&port8250.port.uartclk);
128 	if (ret)
129 		return ret;
130 
131 	spin_lock_init(&port8250.port.lock);
132 
133 	port8250.port.mapbase           = regs->start;
134 	port8250.port.irq               = irq;
135 	port8250.port.handle_irq        = fsl8250_handle_irq;
136 	port8250.port.type              = PORT_16550A;
137 	port8250.port.flags             = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF
138 						| UPF_FIXED_PORT | UPF_IOREMAP
139 						| UPF_FIXED_TYPE;
140 	port8250.port.dev               = dev;
141 	port8250.port.mapsize           = resource_size(regs);
142 	port8250.port.iotype            = UPIO_MEM;
143 	port8250.port.irqflags          = IRQF_SHARED;
144 
145 	port8250.port.membase = devm_ioremap(dev,  port8250.port.mapbase,
146 							port8250.port.mapsize);
147 	if (!port8250.port.membase)
148 		return -ENOMEM;
149 
150 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
151 	if (!data)
152 		return -ENOMEM;
153 
154 	data->line = serial8250_register_8250_port(&port8250);
155 	if (data->line < 0)
156 		return data->line;
157 
158 	platform_set_drvdata(pdev, data);
159 	return 0;
160 }
161 
162 static int fsl8250_acpi_remove(struct platform_device *pdev)
163 {
164 	struct fsl8250_data *data = platform_get_drvdata(pdev);
165 
166 	serial8250_unregister_port(data->line);
167 	return 0;
168 }
169 
170 static const struct acpi_device_id fsl_8250_acpi_id[] = {
171 	{ "NXP0018", 0 },
172 	{ },
173 };
174 MODULE_DEVICE_TABLE(acpi, fsl_8250_acpi_id);
175 
176 static struct platform_driver fsl8250_platform_driver = {
177 	.driver = {
178 		.name			= "fsl-16550-uart",
179 		.acpi_match_table	= ACPI_PTR(fsl_8250_acpi_id),
180 	},
181 	.probe			= fsl8250_acpi_probe,
182 	.remove			= fsl8250_acpi_remove,
183 };
184 
185 module_platform_driver(fsl8250_platform_driver);
186 #endif
187 
188 MODULE_LICENSE("GPL");
189 MODULE_DESCRIPTION("Handling of Freescale specific 8250 variants");
190