xref: /openbmc/linux/drivers/tty/serial/8250/8250_fsl.c (revision 249592bf)
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 struct fsl8250_data {
27 	int	line;
28 };
29 
30 int fsl8250_handle_irq(struct uart_port *port)
31 {
32 	unsigned char lsr, orig_lsr;
33 	unsigned int iir;
34 	struct uart_8250_port *up = up_to_u8250p(port);
35 
36 	spin_lock(&up->port.lock);
37 
38 	iir = port->serial_in(port, UART_IIR);
39 	if (iir & UART_IIR_NO_INT) {
40 		spin_unlock(&up->port.lock);
41 		return 0;
42 	}
43 
44 	/* This is the WAR; if last event was BRK, then read and return */
45 	if (unlikely(up->lsr_saved_flags & UART_LSR_BI)) {
46 		up->lsr_saved_flags &= ~UART_LSR_BI;
47 		port->serial_in(port, UART_RX);
48 		spin_unlock(&up->port.lock);
49 		return 1;
50 	}
51 
52 	lsr = orig_lsr = up->port.serial_in(&up->port, UART_LSR);
53 
54 	/* Process incoming characters first */
55 	if ((lsr & (UART_LSR_DR | UART_LSR_BI)) &&
56 	    (up->ier & (UART_IER_RLSI | UART_IER_RDI))) {
57 		lsr = serial8250_rx_chars(up, lsr);
58 	}
59 
60 	/* Stop processing interrupts on input overrun */
61 	if ((orig_lsr & UART_LSR_OE) && (up->overrun_backoff_time_ms > 0)) {
62 		unsigned long delay;
63 
64 		up->ier = port->serial_in(port, UART_IER);
65 		if (up->ier & (UART_IER_RLSI | UART_IER_RDI)) {
66 			port->ops->stop_rx(port);
67 		} else {
68 			/* Keep restarting the timer until
69 			 * the input overrun subsides.
70 			 */
71 			cancel_delayed_work(&up->overrun_backoff);
72 		}
73 
74 		delay = msecs_to_jiffies(up->overrun_backoff_time_ms);
75 		schedule_delayed_work(&up->overrun_backoff, delay);
76 	}
77 
78 	serial8250_modem_status(up);
79 
80 	if ((lsr & UART_LSR_THRE) && (up->ier & UART_IER_THRI))
81 		serial8250_tx_chars(up);
82 
83 	up->lsr_saved_flags = orig_lsr;
84 
85 	uart_unlock_and_check_sysrq(&up->port);
86 
87 	return 1;
88 }
89 EXPORT_SYMBOL_GPL(fsl8250_handle_irq);
90 
91 #ifdef CONFIG_ACPI
92 static int fsl8250_acpi_probe(struct platform_device *pdev)
93 {
94 	struct fsl8250_data *data;
95 	struct uart_8250_port port8250;
96 	struct device *dev = &pdev->dev;
97 	struct resource *regs;
98 
99 	int ret, irq;
100 
101 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
102 	if (!regs) {
103 		dev_err(dev, "no registers defined\n");
104 		return -EINVAL;
105 	}
106 
107 	irq = platform_get_irq(pdev, 0);
108 	if (irq < 0)
109 		return irq;
110 
111 	memset(&port8250, 0, sizeof(port8250));
112 
113 	ret = device_property_read_u32(dev, "clock-frequency",
114 					&port8250.port.uartclk);
115 	if (ret)
116 		return ret;
117 
118 	spin_lock_init(&port8250.port.lock);
119 
120 	port8250.port.mapbase           = regs->start;
121 	port8250.port.irq               = irq;
122 	port8250.port.handle_irq        = fsl8250_handle_irq;
123 	port8250.port.type              = PORT_16550A;
124 	port8250.port.flags             = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF
125 						| UPF_FIXED_PORT | UPF_IOREMAP
126 						| UPF_FIXED_TYPE;
127 	port8250.port.dev               = dev;
128 	port8250.port.mapsize           = resource_size(regs);
129 	port8250.port.iotype            = UPIO_MEM;
130 	port8250.port.irqflags          = IRQF_SHARED;
131 
132 	port8250.port.membase = devm_ioremap(dev,  port8250.port.mapbase,
133 							port8250.port.mapsize);
134 	if (!port8250.port.membase)
135 		return -ENOMEM;
136 
137 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
138 	if (!data)
139 		return -ENOMEM;
140 
141 	data->line = serial8250_register_8250_port(&port8250);
142 	if (data->line < 0)
143 		return data->line;
144 
145 	platform_set_drvdata(pdev, data);
146 	return 0;
147 }
148 
149 static int fsl8250_acpi_remove(struct platform_device *pdev)
150 {
151 	struct fsl8250_data *data = platform_get_drvdata(pdev);
152 
153 	serial8250_unregister_port(data->line);
154 	return 0;
155 }
156 
157 static const struct acpi_device_id fsl_8250_acpi_id[] = {
158 	{ "NXP0018", 0 },
159 	{ },
160 };
161 MODULE_DEVICE_TABLE(acpi, fsl_8250_acpi_id);
162 
163 static struct platform_driver fsl8250_platform_driver = {
164 	.driver = {
165 		.name			= "fsl-16550-uart",
166 		.acpi_match_table	= ACPI_PTR(fsl_8250_acpi_id),
167 	},
168 	.probe			= fsl8250_acpi_probe,
169 	.remove			= fsl8250_acpi_remove,
170 };
171 
172 module_platform_driver(fsl8250_platform_driver);
173 #endif
174