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