xref: /openbmc/linux/drivers/tty/serial/8250/8250_of.c (revision f220d3eb)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Serial Port driver for Open Firmware platform devices
4  *
5  *    Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
6  */
7 #include <linux/console.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/delay.h>
11 #include <linux/serial_core.h>
12 #include <linux/serial_reg.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/clk.h>
18 #include <linux/reset.h>
19 
20 #include "8250.h"
21 
22 struct of_serial_info {
23 	struct clk *clk;
24 	struct reset_control *rst;
25 	int type;
26 	int line;
27 };
28 
29 #ifdef CONFIG_ARCH_TEGRA
30 static void tegra_serial_handle_break(struct uart_port *p)
31 {
32 	unsigned int status, tmout = 10000;
33 
34 	do {
35 		status = p->serial_in(p, UART_LSR);
36 		if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
37 			status = p->serial_in(p, UART_RX);
38 		else
39 			break;
40 		if (--tmout == 0)
41 			break;
42 		udelay(1);
43 	} while (1);
44 }
45 #else
46 static inline void tegra_serial_handle_break(struct uart_port *port)
47 {
48 }
49 #endif
50 
51 /*
52  * Fill a struct uart_port for a given device node
53  */
54 static int of_platform_serial_setup(struct platform_device *ofdev,
55 			int type, struct uart_port *port,
56 			struct of_serial_info *info)
57 {
58 	struct resource resource;
59 	struct device_node *np = ofdev->dev.of_node;
60 	u32 clk, spd, prop;
61 	int ret;
62 
63 	memset(port, 0, sizeof *port);
64 
65 	pm_runtime_enable(&ofdev->dev);
66 	pm_runtime_get_sync(&ofdev->dev);
67 
68 	if (of_property_read_u32(np, "clock-frequency", &clk)) {
69 
70 		/* Get clk rate through clk driver if present */
71 		info->clk = devm_clk_get(&ofdev->dev, NULL);
72 		if (IS_ERR(info->clk)) {
73 			dev_warn(&ofdev->dev,
74 				"clk or clock-frequency not defined\n");
75 			ret = PTR_ERR(info->clk);
76 			goto err_pmruntime;
77 		}
78 
79 		ret = clk_prepare_enable(info->clk);
80 		if (ret < 0)
81 			goto err_pmruntime;
82 
83 		clk = clk_get_rate(info->clk);
84 	}
85 	/* If current-speed was set, then try not to change it. */
86 	if (of_property_read_u32(np, "current-speed", &spd) == 0)
87 		port->custom_divisor = clk / (16 * spd);
88 
89 	ret = of_address_to_resource(np, 0, &resource);
90 	if (ret) {
91 		dev_warn(&ofdev->dev, "invalid address\n");
92 		goto err_unprepare;
93 	}
94 
95 	port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT |
96 				  UPF_FIXED_TYPE;
97 	spin_lock_init(&port->lock);
98 
99 	if (resource_type(&resource) == IORESOURCE_IO) {
100 		port->iotype = UPIO_PORT;
101 		port->iobase = resource.start;
102 	} else {
103 		port->mapbase = resource.start;
104 		port->mapsize = resource_size(&resource);
105 
106 		/* Check for shifted address mapping */
107 		if (of_property_read_u32(np, "reg-offset", &prop) == 0)
108 			port->mapbase += prop;
109 
110 		port->iotype = UPIO_MEM;
111 		if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
112 			switch (prop) {
113 			case 1:
114 				port->iotype = UPIO_MEM;
115 				break;
116 			case 2:
117 				port->iotype = UPIO_MEM16;
118 				break;
119 			case 4:
120 				port->iotype = of_device_is_big_endian(np) ?
121 					       UPIO_MEM32BE : UPIO_MEM32;
122 				break;
123 			default:
124 				dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
125 					 prop);
126 				ret = -EINVAL;
127 				goto err_unprepare;
128 			}
129 		}
130 		port->flags |= UPF_IOREMAP;
131 	}
132 
133 	/* Check for registers offset within the devices address range */
134 	if (of_property_read_u32(np, "reg-shift", &prop) == 0)
135 		port->regshift = prop;
136 
137 	/* Check for fifo size */
138 	if (of_property_read_u32(np, "fifo-size", &prop) == 0)
139 		port->fifosize = prop;
140 
141 	/* Check for a fixed line number */
142 	ret = of_alias_get_id(np, "serial");
143 	if (ret >= 0)
144 		port->line = ret;
145 
146 	port->irq = irq_of_parse_and_map(np, 0);
147 	if (!port->irq) {
148 		ret = -EPROBE_DEFER;
149 		goto err_unprepare;
150 	}
151 
152 	info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL);
153 	if (IS_ERR(info->rst)) {
154 		ret = PTR_ERR(info->rst);
155 		goto err_dispose;
156 	}
157 
158 	ret = reset_control_deassert(info->rst);
159 	if (ret)
160 		goto err_dispose;
161 
162 	port->type = type;
163 	port->uartclk = clk;
164 	port->irqflags |= IRQF_SHARED;
165 
166 	if (of_property_read_bool(np, "no-loopback-test"))
167 		port->flags |= UPF_SKIP_TEST;
168 
169 	port->dev = &ofdev->dev;
170 
171 	switch (type) {
172 	case PORT_TEGRA:
173 		port->handle_break = tegra_serial_handle_break;
174 		break;
175 
176 	case PORT_RT2880:
177 		port->iotype = UPIO_AU;
178 		break;
179 	}
180 
181 	if (IS_ENABLED(CONFIG_SERIAL_8250_FSL) &&
182 	    (of_device_is_compatible(np, "fsl,ns16550") ||
183 	     of_device_is_compatible(np, "fsl,16550-FIFO64")))
184 		port->handle_irq = fsl8250_handle_irq;
185 
186 	return 0;
187 err_dispose:
188 	irq_dispose_mapping(port->irq);
189 err_unprepare:
190 	clk_disable_unprepare(info->clk);
191 err_pmruntime:
192 	pm_runtime_put_sync(&ofdev->dev);
193 	pm_runtime_disable(&ofdev->dev);
194 	return ret;
195 }
196 
197 /*
198  * Try to register a serial port
199  */
200 static const struct of_device_id of_platform_serial_table[];
201 static int of_platform_serial_probe(struct platform_device *ofdev)
202 {
203 	const struct of_device_id *match;
204 	struct of_serial_info *info;
205 	struct uart_8250_port port8250;
206 	u32 tx_threshold;
207 	int port_type;
208 	int ret;
209 
210 	match = of_match_device(of_platform_serial_table, &ofdev->dev);
211 	if (!match)
212 		return -EINVAL;
213 
214 	if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
215 		return -EBUSY;
216 
217 	info = kzalloc(sizeof(*info), GFP_KERNEL);
218 	if (info == NULL)
219 		return -ENOMEM;
220 
221 	port_type = (unsigned long)match->data;
222 	memset(&port8250, 0, sizeof(port8250));
223 	ret = of_platform_serial_setup(ofdev, port_type, &port8250.port, info);
224 	if (ret)
225 		goto err_free;
226 
227 	if (port8250.port.fifosize)
228 		port8250.capabilities = UART_CAP_FIFO;
229 
230 	/* Check for TX FIFO threshold & set tx_loadsz */
231 	if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
232 				  &tx_threshold) == 0) &&
233 	    (tx_threshold < port8250.port.fifosize))
234 		port8250.tx_loadsz = port8250.port.fifosize - tx_threshold;
235 
236 	if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control"))
237 		port8250.capabilities |= UART_CAP_AFE;
238 
239 	ret = serial8250_register_8250_port(&port8250);
240 	if (ret < 0)
241 		goto err_dispose;
242 
243 	info->type = port_type;
244 	info->line = ret;
245 	platform_set_drvdata(ofdev, info);
246 	return 0;
247 err_dispose:
248 	irq_dispose_mapping(port8250.port.irq);
249 	pm_runtime_put_sync(&ofdev->dev);
250 	pm_runtime_disable(&ofdev->dev);
251 	clk_disable_unprepare(info->clk);
252 err_free:
253 	kfree(info);
254 	return ret;
255 }
256 
257 /*
258  * Release a line
259  */
260 static int of_platform_serial_remove(struct platform_device *ofdev)
261 {
262 	struct of_serial_info *info = platform_get_drvdata(ofdev);
263 
264 	serial8250_unregister_port(info->line);
265 
266 	reset_control_assert(info->rst);
267 	pm_runtime_put_sync(&ofdev->dev);
268 	pm_runtime_disable(&ofdev->dev);
269 	clk_disable_unprepare(info->clk);
270 	kfree(info);
271 	return 0;
272 }
273 
274 #ifdef CONFIG_PM_SLEEP
275 static int of_serial_suspend(struct device *dev)
276 {
277 	struct of_serial_info *info = dev_get_drvdata(dev);
278 	struct uart_8250_port *port8250 = serial8250_get_port(info->line);
279 	struct uart_port *port = &port8250->port;
280 
281 	serial8250_suspend_port(info->line);
282 
283 	if (!uart_console(port) || console_suspend_enabled) {
284 		pm_runtime_put_sync(dev);
285 		clk_disable_unprepare(info->clk);
286 	}
287 	return 0;
288 }
289 
290 static int of_serial_resume(struct device *dev)
291 {
292 	struct of_serial_info *info = dev_get_drvdata(dev);
293 	struct uart_8250_port *port8250 = serial8250_get_port(info->line);
294 	struct uart_port *port = &port8250->port;
295 
296 	if (!uart_console(port) || console_suspend_enabled) {
297 		pm_runtime_get_sync(dev);
298 		clk_prepare_enable(info->clk);
299 	}
300 
301 	serial8250_resume_port(info->line);
302 
303 	return 0;
304 }
305 #endif
306 static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
307 
308 /*
309  * A few common types, add more as needed.
310  */
311 static const struct of_device_id of_platform_serial_table[] = {
312 	{ .compatible = "ns8250",   .data = (void *)PORT_8250, },
313 	{ .compatible = "ns16450",  .data = (void *)PORT_16450, },
314 	{ .compatible = "ns16550a", .data = (void *)PORT_16550A, },
315 	{ .compatible = "ns16550",  .data = (void *)PORT_16550, },
316 	{ .compatible = "ns16750",  .data = (void *)PORT_16750, },
317 	{ .compatible = "ns16850",  .data = (void *)PORT_16850, },
318 	{ .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
319 	{ .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
320 	{ .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
321 	{ .compatible = "altr,16550-FIFO32",
322 		.data = (void *)PORT_ALTR_16550_F32, },
323 	{ .compatible = "altr,16550-FIFO64",
324 		.data = (void *)PORT_ALTR_16550_F64, },
325 	{ .compatible = "altr,16550-FIFO128",
326 		.data = (void *)PORT_ALTR_16550_F128, },
327 	{ .compatible = "mediatek,mtk-btif",
328 		.data = (void *)PORT_MTK_BTIF, },
329 	{ .compatible = "mrvl,mmp-uart",
330 		.data = (void *)PORT_XSCALE, },
331 	{ .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
332 	{ .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
333 	{ /* end of list */ },
334 };
335 MODULE_DEVICE_TABLE(of, of_platform_serial_table);
336 
337 static struct platform_driver of_platform_serial_driver = {
338 	.driver = {
339 		.name = "of_serial",
340 		.of_match_table = of_platform_serial_table,
341 		.pm = &of_serial_pm_ops,
342 	},
343 	.probe = of_platform_serial_probe,
344 	.remove = of_platform_serial_remove,
345 };
346 
347 module_platform_driver(of_platform_serial_driver);
348 
349 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
350 MODULE_LICENSE("GPL");
351 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");
352