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