tty: of_serial: add support for the DesignWare 8250
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / tty / serial / of_serial.c
blob024926c3be575c0326e440efe7ccbb2b954079e7
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 u32 clk, spd, prop;
36 int ret;
38 memset(port, 0, sizeof *port);
39 if (of_property_read_u32(np, "clock-frequency", &clk)) {
40 dev_warn(&ofdev->dev, "no clock-frequency property set\n");
41 return -ENODEV;
43 /* If current-speed was set, then try not to change it. */
44 if (of_property_read_u32(np, "current-speed", &spd) == 0)
45 port->custom_divisor = clk / (16 * spd);
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 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
58 port->mapbase += prop;
60 /* Check for registers offset within the devices address range */
61 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
62 port->regshift = prop;
64 port->irq = irq_of_parse_and_map(np, 0);
65 port->iotype = UPIO_MEM;
66 if (of_property_read_u32(np, "reg-io-width", &prop) == 0) {
67 switch (prop) {
68 case 1:
69 port->iotype = UPIO_MEM;
70 break;
71 case 4:
72 port->iotype = UPIO_MEM32;
73 break;
74 default:
75 dev_warn(&ofdev->dev, "unsupported reg-io-width (%d)\n",
76 prop);
77 return -EINVAL;
81 if (of_device_is_compatible(np, "ns8250dw")) {
82 ret = serial8250_use_designware_io(port);
83 if (ret)
84 dev_warn(&ofdev->dev, "unable to register DesignWare 8250 helpers, continuing as a normal 8250\n");
87 port->type = type;
88 port->uartclk = clk;
89 port->flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
90 | UPF_FIXED_PORT | UPF_FIXED_TYPE;
91 port->dev = &ofdev->dev;
93 return 0;
97 * Try to register a serial port
99 static struct of_device_id of_platform_serial_table[];
100 static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
102 const struct of_device_id *match;
103 struct of_serial_info *info;
104 struct uart_port port;
105 int port_type;
106 int ret;
108 match = of_match_device(of_platform_serial_table, &ofdev->dev);
109 if (!match)
110 return -EINVAL;
112 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
113 return -EBUSY;
115 info = kmalloc(sizeof(*info), GFP_KERNEL);
116 if (info == NULL)
117 return -ENOMEM;
119 port_type = (unsigned long)match->data;
120 ret = of_platform_serial_setup(ofdev, port_type, &port);
121 if (ret)
122 goto out;
124 switch (port_type) {
125 #ifdef CONFIG_SERIAL_8250
126 case PORT_8250 ... PORT_MAX_8250:
127 ret = serial8250_register_port(&port);
128 break;
129 #endif
130 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
131 case PORT_NWPSERIAL:
132 ret = nwpserial_register_port(&port);
133 break;
134 #endif
135 default:
136 /* need to add code for these */
137 case PORT_UNKNOWN:
138 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
139 ret = -ENODEV;
140 break;
142 if (ret < 0)
143 goto out;
145 info->type = port_type;
146 info->line = ret;
147 dev_set_drvdata(&ofdev->dev, info);
148 return 0;
149 out:
150 kfree(info);
151 irq_dispose_mapping(port.irq);
152 return ret;
156 * Release a line
158 static int of_platform_serial_remove(struct platform_device *ofdev)
160 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
161 switch (info->type) {
162 #ifdef CONFIG_SERIAL_8250
163 case PORT_8250 ... PORT_MAX_8250:
164 serial8250_unregister_port(info->line);
165 break;
166 #endif
167 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
168 case PORT_NWPSERIAL:
169 nwpserial_unregister_port(info->line);
170 break;
171 #endif
172 default:
173 /* need to add code for these */
174 break;
176 kfree(info);
177 return 0;
181 * A few common types, add more as needed.
183 static struct of_device_id __devinitdata of_platform_serial_table[] = {
184 { .compatible = "ns8250", .data = (void *)PORT_8250, },
185 { .compatible = "ns8250dw", .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 { .compatible = "nvidia,tegra20-uart", .data = (void *)PORT_TEGRA, },
192 #ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
193 { .compatible = "ibm,qpace-nwp-serial",
194 .data = (void *)PORT_NWPSERIAL, },
195 #endif
196 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
197 { /* end of list */ },
200 static struct platform_driver of_platform_serial_driver = {
201 .driver = {
202 .name = "of_serial",
203 .owner = THIS_MODULE,
204 .of_match_table = of_platform_serial_table,
206 .probe = of_platform_serial_probe,
207 .remove = of_platform_serial_remove,
210 static int __init of_platform_serial_init(void)
212 return platform_driver_register(&of_platform_serial_driver);
214 module_init(of_platform_serial_init);
216 static void __exit of_platform_serial_exit(void)
218 return platform_driver_unregister(&of_platform_serial_driver);
220 module_exit(of_platform_serial_exit);
222 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
223 MODULE_LICENSE("GPL");
224 MODULE_DESCRIPTION("Serial Port driver for Open Firmware platform devices");