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