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