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
8 #include <linux/bits.h>
9 #include <linux/console.h>
10 #include <linux/math.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/serial_core.h>
14 #include <linux/serial_reg.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_platform.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/clk.h>
20 #include <linux/reset.h>
21 #include <linux/notifier.h>
22
23 #include "8250.h"
24
25 struct of_serial_info {
26 struct clk *clk;
27 struct clk *bus_clk;
28 struct reset_control *rst;
29 int type;
30 int line;
31 struct notifier_block clk_notifier;
32 };
33
34 /* Nuvoton NPCM timeout register */
35 #define UART_NPCM_TOR 7
36 #define UART_NPCM_TOIE BIT(7) /* Timeout Interrupt Enable */
37
npcm_startup(struct uart_port * port)38 static int npcm_startup(struct uart_port *port)
39 {
40 /*
41 * Nuvoton calls the scratch register 'UART_TOR' (timeout
42 * register). Enable it, and set TIOC (timeout interrupt
43 * comparator) to be 0x20 for correct operation.
44 */
45 serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
46
47 return serial8250_do_startup(port);
48 }
49
50 /* Nuvoton NPCM UARTs have a custom divisor calculation */
npcm_get_divisor(struct uart_port * port,unsigned int baud,unsigned int * frac)51 static unsigned int npcm_get_divisor(struct uart_port *port, unsigned int baud,
52 unsigned int *frac)
53 {
54 return DIV_ROUND_CLOSEST(port->uartclk, 16 * baud + 2) - 2;
55 }
56
npcm_setup(struct uart_port * port)57 static int npcm_setup(struct uart_port *port)
58 {
59 port->get_divisor = npcm_get_divisor;
60 port->startup = npcm_startup;
61 return 0;
62 }
63
clk_nb_to_info(struct notifier_block * nb)64 static inline struct of_serial_info *clk_nb_to_info(struct notifier_block *nb)
65 {
66 return container_of(nb, struct of_serial_info, clk_notifier);
67 }
68
of_platform_serial_clk_notifier_cb(struct notifier_block * nb,unsigned long event,void * data)69 static int of_platform_serial_clk_notifier_cb(struct notifier_block *nb, unsigned long event,
70 void *data)
71 {
72 struct of_serial_info *info = clk_nb_to_info(nb);
73 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
74 struct clk_notifier_data *ndata = data;
75
76 if (event == POST_RATE_CHANGE) {
77 serial8250_update_uartclk(&port8250->port, ndata->new_rate);
78 return NOTIFY_OK;
79 }
80
81 return NOTIFY_DONE;
82 }
83
84 /*
85 * Fill a struct uart_port for a given device node
86 */
of_platform_serial_setup(struct platform_device * ofdev,int type,struct uart_8250_port * up,struct of_serial_info * info)87 static int of_platform_serial_setup(struct platform_device *ofdev,
88 int type, struct uart_8250_port *up,
89 struct of_serial_info *info)
90 {
91 struct resource resource;
92 struct device *dev = &ofdev->dev;
93 struct device_node *np = dev->of_node;
94 struct uart_port *port = &up->port;
95 u32 spd;
96 int ret;
97
98 memset(port, 0, sizeof *port);
99
100 pm_runtime_enable(&ofdev->dev);
101 pm_runtime_get_sync(&ofdev->dev);
102
103 ret = of_address_to_resource(np, 0, &resource);
104 if (ret) {
105 dev_err_probe(dev, ret, "invalid address\n");
106 goto err_pmruntime;
107 }
108
109 port->dev = &ofdev->dev;
110 port->flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_FIXED_TYPE;
111 spin_lock_init(&port->lock);
112
113 if (resource_type(&resource) == IORESOURCE_IO) {
114 port->iobase = resource.start;
115 } else {
116 port->mapbase = resource.start;
117 port->mapsize = resource_size(&resource);
118 port->flags |= UPF_IOREMAP;
119 }
120
121 ret = uart_read_and_validate_port_properties(port);
122 if (ret)
123 goto err_pmruntime;
124
125 /* Get clk rate through clk driver if present */
126 if (!port->uartclk) {
127 struct clk *bus_clk;
128
129 bus_clk = devm_clk_get_optional_enabled(dev, "bus");
130 if (IS_ERR(bus_clk)) {
131 ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
132 goto err_pmruntime;
133 }
134
135 /* If the bus clock is required, core clock must be named */
136 info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
137 if (IS_ERR(info->clk)) {
138 ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
139 goto err_pmruntime;
140 }
141
142 info->bus_clk = bus_clk;
143 port->uartclk = clk_get_rate(info->clk);
144 }
145 /* If current-speed was set, then try not to change it. */
146 if (of_property_read_u32(np, "current-speed", &spd) == 0)
147 port->custom_divisor = port->uartclk / (16 * spd);
148
149 /* Compatibility with the deprecated pxa driver and 8250_pxa drivers. */
150 if (of_device_is_compatible(np, "mrvl,mmp-uart"))
151 port->regshift = 2;
152
153 info->rst = devm_reset_control_get_optional_shared(&ofdev->dev, NULL);
154 if (IS_ERR(info->rst)) {
155 ret = PTR_ERR(info->rst);
156 goto err_pmruntime;
157 }
158
159 ret = reset_control_deassert(info->rst);
160 if (ret)
161 goto err_pmruntime;
162
163 port->type = type;
164 port->rs485_config = serial8250_em485_config;
165 port->rs485_supported = serial8250_em485_supported;
166 up->rs485_start_tx = serial8250_em485_start_tx;
167 up->rs485_stop_tx = serial8250_em485_stop_tx;
168
169 switch (type) {
170 case PORT_RT2880:
171 ret = rt288x_setup(port);
172 break;
173 case PORT_NPCM:
174 ret = npcm_setup(port);
175 break;
176 default:
177 /* Nothing to do */
178 ret = 0;
179 break;
180 }
181 if (ret)
182 goto err_pmruntime;
183
184 if (IS_REACHABLE(CONFIG_SERIAL_8250_FSL) &&
185 (of_device_is_compatible(np, "fsl,ns16550") ||
186 of_device_is_compatible(np, "fsl,16550-FIFO64"))) {
187 port->handle_irq = fsl8250_handle_irq;
188 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
189 }
190
191 return 0;
192 err_pmruntime:
193 pm_runtime_put_sync(&ofdev->dev);
194 pm_runtime_disable(&ofdev->dev);
195 return ret;
196 }
197
198 /*
199 * Try to register a serial port
200 */
of_platform_serial_probe(struct platform_device * ofdev)201 static int of_platform_serial_probe(struct platform_device *ofdev)
202 {
203 struct of_serial_info *info;
204 struct uart_8250_port port8250;
205 unsigned int port_type;
206 u32 tx_threshold;
207 int ret;
208
209 if (IS_ENABLED(CONFIG_SERIAL_8250_BCM7271) &&
210 of_device_is_compatible(ofdev->dev.of_node, "brcm,bcm7271-uart"))
211 return -ENODEV;
212
213 port_type = (unsigned long)of_device_get_match_data(&ofdev->dev);
214 if (port_type == PORT_UNKNOWN)
215 return -EINVAL;
216
217 if (of_property_read_bool(ofdev->dev.of_node, "used-by-rtas"))
218 return -EBUSY;
219
220 info = kzalloc(sizeof(*info), GFP_KERNEL);
221 if (info == NULL)
222 return -ENOMEM;
223
224 memset(&port8250, 0, sizeof(port8250));
225 ret = of_platform_serial_setup(ofdev, port_type, &port8250, info);
226 if (ret)
227 goto err_free;
228
229 if (port8250.port.fifosize)
230 port8250.capabilities = UART_CAP_FIFO;
231
232 /* Check for TX FIFO threshold & set tx_loadsz */
233 if ((of_property_read_u32(ofdev->dev.of_node, "tx-threshold",
234 &tx_threshold) == 0) &&
235 (tx_threshold < port8250.port.fifosize))
236 port8250.tx_loadsz = port8250.port.fifosize - tx_threshold;
237
238 if (of_property_read_bool(ofdev->dev.of_node, "auto-flow-control"))
239 port8250.capabilities |= UART_CAP_AFE;
240
241 if (of_property_read_u32(ofdev->dev.of_node,
242 "overrun-throttle-ms",
243 &port8250.overrun_backoff_time_ms) != 0)
244 port8250.overrun_backoff_time_ms = 0;
245
246 ret = serial8250_register_8250_port(&port8250);
247 if (ret < 0)
248 goto err_dispose;
249
250 info->type = port_type;
251 info->line = ret;
252 platform_set_drvdata(ofdev, info);
253
254 if (info->clk) {
255 info->clk_notifier.notifier_call = of_platform_serial_clk_notifier_cb;
256 ret = clk_notifier_register(info->clk, &info->clk_notifier);
257 if (ret) {
258 dev_err_probe(port8250.port.dev, ret, "Failed to set the clock notifier\n");
259 goto err_unregister;
260 }
261 }
262
263 return 0;
264 err_unregister:
265 serial8250_unregister_port(info->line);
266 err_dispose:
267 pm_runtime_put_sync(&ofdev->dev);
268 pm_runtime_disable(&ofdev->dev);
269 err_free:
270 kfree(info);
271 return ret;
272 }
273
274 /*
275 * Release a line
276 */
of_platform_serial_remove(struct platform_device * ofdev)277 static void of_platform_serial_remove(struct platform_device *ofdev)
278 {
279 struct of_serial_info *info = platform_get_drvdata(ofdev);
280
281 if (info->clk)
282 clk_notifier_unregister(info->clk, &info->clk_notifier);
283
284 serial8250_unregister_port(info->line);
285
286 reset_control_assert(info->rst);
287 pm_runtime_put_sync(&ofdev->dev);
288 pm_runtime_disable(&ofdev->dev);
289 kfree(info);
290 }
291
292 #ifdef CONFIG_PM_SLEEP
of_serial_suspend(struct device * dev)293 static int of_serial_suspend(struct device *dev)
294 {
295 struct of_serial_info *info = dev_get_drvdata(dev);
296 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
297 struct uart_port *port = &port8250->port;
298
299 serial8250_suspend_port(info->line);
300
301 if (!uart_console(port) || console_suspend_enabled) {
302 pm_runtime_put_sync(dev);
303 clk_disable_unprepare(info->clk);
304 clk_disable_unprepare(info->bus_clk);
305 }
306 return 0;
307 }
308
of_serial_resume(struct device * dev)309 static int of_serial_resume(struct device *dev)
310 {
311 struct of_serial_info *info = dev_get_drvdata(dev);
312 struct uart_8250_port *port8250 = serial8250_get_port(info->line);
313 struct uart_port *port = &port8250->port;
314
315 if (!uart_console(port) || console_suspend_enabled) {
316 pm_runtime_get_sync(dev);
317 clk_prepare_enable(info->bus_clk);
318 clk_prepare_enable(info->clk);
319 }
320
321 serial8250_resume_port(info->line);
322
323 return 0;
324 }
325 #endif
326 static SIMPLE_DEV_PM_OPS(of_serial_pm_ops, of_serial_suspend, of_serial_resume);
327
328 /*
329 * A few common types, add more as needed.
330 */
331 static const struct of_device_id of_platform_serial_table[] = {
332 { .compatible = "ns8250", .data = (void *)PORT_8250, },
333 { .compatible = "ns16450", .data = (void *)PORT_16450, },
334 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
335 { .compatible = "ns16550", .data = (void *)PORT_16550, },
336 { .compatible = "ns16750", .data = (void *)PORT_16750, },
337 { .compatible = "ns16850", .data = (void *)PORT_16850, },
338 { .compatible = "nxp,lpc3220-uart", .data = (void *)PORT_LPC3220, },
339 { .compatible = "ralink,rt2880-uart", .data = (void *)PORT_RT2880, },
340 { .compatible = "intel,xscale-uart", .data = (void *)PORT_XSCALE, },
341 { .compatible = "altr,16550-FIFO32",
342 .data = (void *)PORT_ALTR_16550_F32, },
343 { .compatible = "altr,16550-FIFO64",
344 .data = (void *)PORT_ALTR_16550_F64, },
345 { .compatible = "altr,16550-FIFO128",
346 .data = (void *)PORT_ALTR_16550_F128, },
347 { .compatible = "fsl,16550-FIFO64",
348 .data = (void *)PORT_16550A_FSL64, },
349 { .compatible = "mediatek,mtk-btif",
350 .data = (void *)PORT_MTK_BTIF, },
351 { .compatible = "mrvl,mmp-uart",
352 .data = (void *)PORT_XSCALE, },
353 { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
354 { .compatible = "nuvoton,wpcm450-uart", .data = (void *)PORT_NPCM, },
355 { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
356 { /* end of list */ },
357 };
358 MODULE_DEVICE_TABLE(of, of_platform_serial_table);
359
360 static struct platform_driver of_platform_serial_driver = {
361 .driver = {
362 .name = "of_serial",
363 .of_match_table = of_platform_serial_table,
364 .pm = &of_serial_pm_ops,
365 },
366 .probe = of_platform_serial_probe,
367 .remove = of_platform_serial_remove,
368 };
369
370 module_platform_driver(of_platform_serial_driver);
371
372 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
373 MODULE_LICENSE("GPL");
374 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");
375