]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/tty/serial/of_serial.c
36038ed8f09af8b382772dbfb5cbe957d10bbd2a
[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/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/serial_core.h>
16 #include <linux/serial_8250.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/nwpserial.h>
21
22 struct of_serial_info {
23         int type;
24         int line;
25 };
26
27 /*
28  * Fill a struct uart_port for a given device node
29  */
30 static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
31                                         int type, struct uart_port *port)
32 {
33         struct resource resource;
34         struct device_node *np = ofdev->dev.of_node;
35         const __be32 *clk, *spd;
36         const __be32 *prop;
37         int ret, prop_size;
38
39         memset(port, 0, sizeof *port);
40         spd = of_get_property(np, "current-speed", NULL);
41         clk = of_get_property(np, "clock-frequency", NULL);
42         if (!clk) {
43                 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
44                 return -ENODEV;
45         }
46
47         ret = of_address_to_resource(np, 0, &resource);
48         if (ret) {
49                 dev_warn(&ofdev->dev, "invalid address\n");
50                 return ret;
51         }
52
53         spin_lock_init(&port->lock);
54         port->mapbase = resource.start;
55
56         /* Check for shifted address mapping */
57         prop = of_get_property(np, "reg-offset", &prop_size);
58         if (prop && (prop_size == sizeof(u32)))
59                 port->mapbase += be32_to_cpup(prop);
60
61         /* Check for registers offset within the devices address range */
62         prop = of_get_property(np, "reg-shift", &prop_size);
63         if (prop && (prop_size == sizeof(u32)))
64                 port->regshift = be32_to_cpup(prop);
65
66         port->irq = irq_of_parse_and_map(np, 0);
67         port->iotype = UPIO_MEM;
68         prop = of_get_property(np, "reg-io-width", &prop_size);
69         if (prop && (prop_size == sizeof(u32))) {
70                 switch (be32_to_cpup(prop)) {
71                 case 1:
72                         port->iotype = UPIO_MEM;
73                         break;
74                 case 4:
75                         port->iotype = UPIO_MEM32;
76                         break;
77                 default:
78                         dev_warn(&ofdev->dev,
79                                  "unsupported io width (%d bytes)\n",
80                                  be32_to_cpup(prop));
81                         return -EINVAL;
82                 }
83         }
84
85         port->type = type;
86         port->uartclk = be32_to_cpup(clk);
87         port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
88                 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
89         port->dev = &ofdev->dev;
90         /* If current-speed was set, then try not to change it. */
91         if (spd)
92                 port->custom_divisor = be32_to_cpup(clk) / (16 * (be32_to_cpup(spd)));
93
94         return 0;
95 }
96
97 /*
98  * Try to register a serial port
99  */
100 static struct of_device_id of_platform_serial_table[];
101 static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
102 {
103         const struct of_device_id *match;
104         struct of_serial_info *info;
105         struct uart_port port;
106         int port_type;
107         int ret;
108
109         match = of_match_device(of_platform_serial_table, &ofdev->dev);
110         if (!match)
111                 return -EINVAL;
112
113         if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
114                 return -EBUSY;
115
116         info = kmalloc(sizeof(*info), GFP_KERNEL);
117         if (info == NULL)
118                 return -ENOMEM;
119
120         port_type = (unsigned long)match->data;
121         ret = of_platform_serial_setup(ofdev, port_type, &port);
122         if (ret)
123                 goto out;
124
125         switch (port_type) {
126 #ifdef CONFIG_SERIAL_8250
127         case PORT_8250 ... PORT_MAX_8250:
128                 ret = serial8250_register_port(&port);
129                 break;
130 #endif
131 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
132         case PORT_NWPSERIAL:
133                 ret = nwpserial_register_port(&port);
134                 break;
135 #endif
136         default:
137                 /* need to add code for these */
138         case PORT_UNKNOWN:
139                 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
140                 ret = -ENODEV;
141                 break;
142         }
143         if (ret < 0)
144                 goto out;
145
146         info->type = port_type;
147         info->line = ret;
148         dev_set_drvdata(&ofdev->dev, info);
149         return 0;
150 out:
151         kfree(info);
152         irq_dispose_mapping(port.irq);
153         return ret;
154 }
155
156 /*
157  * Release a line
158  */
159 static int of_platform_serial_remove(struct platform_device *ofdev)
160 {
161         struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
162         switch (info->type) {
163 #ifdef CONFIG_SERIAL_8250
164         case PORT_8250 ... PORT_MAX_8250:
165                 serial8250_unregister_port(info->line);
166                 break;
167 #endif
168 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
169         case PORT_NWPSERIAL:
170                 nwpserial_unregister_port(info->line);
171                 break;
172 #endif
173         default:
174                 /* need to add code for these */
175                 break;
176         }
177         kfree(info);
178         return 0;
179 }
180
181 /*
182  * A few common types, add more as needed.
183  */
184 static struct of_device_id __devinitdata of_platform_serial_table[] = {
185         { .compatible = "ns8250",   .data = (void *)PORT_8250, },
186         { .compatible = "ns16450",  .data = (void *)PORT_16450, },
187         { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
188         { .compatible = "ns16550",  .data = (void *)PORT_16550, },
189         { .compatible = "ns16750",  .data = (void *)PORT_16750, },
190         { .compatible = "ns16850",  .data = (void *)PORT_16850, },
191 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
192         { .compatible = "ibm,qpace-nwp-serial",
193                 .data = (void *)PORT_NWPSERIAL, },
194 #endif
195         { .type = "serial",         .data = (void *)PORT_UNKNOWN, },
196         { /* end of list */ },
197 };
198
199 static struct platform_driver of_platform_serial_driver = {
200         .driver = {
201                 .name = "of_serial",
202                 .owner = THIS_MODULE,
203                 .of_match_table = of_platform_serial_table,
204         },
205         .probe = of_platform_serial_probe,
206         .remove = of_platform_serial_remove,
207 };
208
209 static int __init of_platform_serial_init(void)
210 {
211         return platform_driver_register(&of_platform_serial_driver);
212 }
213 module_init(of_platform_serial_init);
214
215 static void __exit of_platform_serial_exit(void)
216 {
217         return platform_driver_unregister(&of_platform_serial_driver);
218 };
219 module_exit(of_platform_serial_exit);
220
221 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
222 MODULE_LICENSE("GPL");
223 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");