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