Merge branch 'move-drivers' of git://git.kernel.org/pub/scm/linux/kernel/git/davej...
[linux-2.6/next.git] / drivers / tty / serial / of_serial.c
blobc911b2419abb8a664f3bfad7d258a94c1363fa5b
1 /*
2 * Serial Port driver for Open Firmware platform devices
4 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
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.
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>
22 struct of_serial_info {
23 int type;
24 int line;
28 * Fill a struct uart_port for a given device node
30 static int __devinit of_platform_serial_setup(struct platform_device *ofdev,
31 int type, struct uart_port *port)
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;
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;
47 ret = of_address_to_resource(np, 0, &resource);
48 if (ret) {
49 dev_warn(&ofdev->dev, "invalid address\n");
50 return ret;
53 spin_lock_init(&port->lock);
54 port->mapbase = resource.start;
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);
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);
66 port->irq = irq_of_parse_and_map(np, 0);
67 port->iotype = UPIO_MEM;
68 port->type = type;
69 port->uartclk = be32_to_cpup(clk);
70 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
71 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
72 port->dev = &ofdev->dev;
73 /* If current-speed was set, then try not to change it. */
74 if (spd)
75 port->custom_divisor = be32_to_cpup(clk) / (16 * (be32_to_cpup(spd)));
77 return 0;
81 * Try to register a serial port
83 static struct of_device_id of_platform_serial_table[];
84 static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
86 const struct of_device_id *match;
87 struct of_serial_info *info;
88 struct uart_port port;
89 int port_type;
90 int ret;
92 match = of_match_device(of_platform_serial_table, &ofdev->dev);
93 if (!match)
94 return -EINVAL;
96 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
97 return -EBUSY;
99 info = kmalloc(sizeof(*info), GFP_KERNEL);
100 if (info == NULL)
101 return -ENOMEM;
103 port_type = (unsigned long)match->data;
104 ret = of_platform_serial_setup(ofdev, port_type, &port);
105 if (ret)
106 goto out;
108 switch (port_type) {
109 #ifdef CONFIG_SERIAL_8250
110 case PORT_8250 ... PORT_MAX_8250:
111 ret = serial8250_register_port(&port);
112 break;
113 #endif
114 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
115 case PORT_NWPSERIAL:
116 ret = nwpserial_register_port(&port);
117 break;
118 #endif
119 default:
120 /* need to add code for these */
121 case PORT_UNKNOWN:
122 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
123 ret = -ENODEV;
124 break;
126 if (ret < 0)
127 goto out;
129 info->type = port_type;
130 info->line = ret;
131 dev_set_drvdata(&ofdev->dev, info);
132 return 0;
133 out:
134 kfree(info);
135 irq_dispose_mapping(port.irq);
136 return ret;
140 * Release a line
142 static int of_platform_serial_remove(struct platform_device *ofdev)
144 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
145 switch (info->type) {
146 #ifdef CONFIG_SERIAL_8250
147 case PORT_8250 ... PORT_MAX_8250:
148 serial8250_unregister_port(info->line);
149 break;
150 #endif
151 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
152 case PORT_NWPSERIAL:
153 nwpserial_unregister_port(info->line);
154 break;
155 #endif
156 default:
157 /* need to add code for these */
158 break;
160 kfree(info);
161 return 0;
165 * A few common types, add more as needed.
167 static struct of_device_id __devinitdata of_platform_serial_table[] = {
168 { .compatible = "ns8250", .data = (void *)PORT_8250, },
169 { .compatible = "ns16450", .data = (void *)PORT_16450, },
170 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
171 { .compatible = "ns16550", .data = (void *)PORT_16550, },
172 { .compatible = "ns16750", .data = (void *)PORT_16750, },
173 { .compatible = "ns16850", .data = (void *)PORT_16850, },
174 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
175 { .compatible = "ibm,qpace-nwp-serial",
176 .data = (void *)PORT_NWPSERIAL, },
177 #endif
178 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
179 { /* end of list */ },
182 static struct platform_driver of_platform_serial_driver = {
183 .driver = {
184 .name = "of_serial",
185 .owner = THIS_MODULE,
186 .of_match_table = of_platform_serial_table,
188 .probe = of_platform_serial_probe,
189 .remove = of_platform_serial_remove,
192 static int __init of_platform_serial_init(void)
194 return platform_driver_register(&of_platform_serial_driver);
196 module_init(of_platform_serial_init);
198 static void __exit of_platform_serial_exit(void)
200 return platform_driver_unregister(&of_platform_serial_driver);
202 module_exit(of_platform_serial_exit);
204 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
205 MODULE_LICENSE("GPL");
206 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");