Commit | Line | Data |
---|---|---|
d7a30103 HS |
1 | /* |
2 | * IBM PowerPC IBM eBus Infrastructure Support. | |
3 | * | |
4 | * Copyright (c) 2005 IBM Corporation | |
6bccf755 | 5 | * Joachim Fenkes <fenkes@de.ibm.com> |
d7a30103 | 6 | * Heiko J Schick <schickhj@de.ibm.com> |
a8308800 | 7 | * |
d7a30103 HS |
8 | * All rights reserved. |
9 | * | |
a8308800 JF |
10 | * This source code is distributed under a dual license of GPL v2.0 and OpenIB |
11 | * BSD. | |
d7a30103 HS |
12 | * |
13 | * OpenIB BSD License | |
14 | * | |
a8308800 JF |
15 | * Redistribution and use in source and binary forms, with or without |
16 | * modification, are permitted provided that the following conditions are met: | |
d7a30103 | 17 | * |
a8308800 JF |
18 | * Redistributions of source code must retain the above copyright notice, this |
19 | * list of conditions and the following disclaimer. | |
d7a30103 | 20 | * |
a8308800 JF |
21 | * Redistributions in binary form must reproduce the above copyright notice, |
22 | * this list of conditions and the following disclaimer in the documentation | |
d7a30103 | 23 | * and/or other materials |
a8308800 | 24 | * provided with the distribution. |
d7a30103 | 25 | * |
a8308800 JF |
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
30 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
d7a30103 HS |
32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
33 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
a8308800 JF |
34 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
d7a30103 HS |
36 | * POSSIBILITY OF SUCH DAMAGE. |
37 | */ | |
38 | ||
39 | #include <linux/init.h> | |
40 | #include <linux/console.h> | |
41 | #include <linux/kobject.h> | |
42 | #include <linux/dma-mapping.h> | |
43 | #include <linux/interrupt.h> | |
44 | #include <asm/ibmebus.h> | |
45 | #include <asm/abs_addr.h> | |
46 | ||
6bccf755 JF |
47 | static struct device ibmebus_bus_device = { /* fake "parent" device */ |
48 | .bus_id = "ibmebus", | |
d7a30103 HS |
49 | }; |
50 | ||
6bccf755 JF |
51 | struct bus_type ibmebus_bus_type; |
52 | ||
d7a30103 HS |
53 | static void *ibmebus_alloc_coherent(struct device *dev, |
54 | size_t size, | |
55 | dma_addr_t *dma_handle, | |
56 | gfp_t flag) | |
57 | { | |
58 | void *mem; | |
a8308800 | 59 | |
d7a30103 HS |
60 | mem = kmalloc(size, flag); |
61 | *dma_handle = (dma_addr_t)mem; | |
62 | ||
63 | return mem; | |
64 | } | |
65 | ||
66 | static void ibmebus_free_coherent(struct device *dev, | |
a8308800 | 67 | size_t size, void *vaddr, |
d7a30103 HS |
68 | dma_addr_t dma_handle) |
69 | { | |
70 | kfree(vaddr); | |
71 | } | |
72 | ||
73 | static dma_addr_t ibmebus_map_single(struct device *dev, | |
74 | void *ptr, | |
75 | size_t size, | |
76 | enum dma_data_direction direction) | |
77 | { | |
78 | return (dma_addr_t)(ptr); | |
79 | } | |
80 | ||
81 | static void ibmebus_unmap_single(struct device *dev, | |
82 | dma_addr_t dma_addr, | |
a8308800 | 83 | size_t size, |
d7a30103 HS |
84 | enum dma_data_direction direction) |
85 | { | |
86 | return; | |
87 | } | |
88 | ||
89 | static int ibmebus_map_sg(struct device *dev, | |
90 | struct scatterlist *sg, | |
91 | int nents, enum dma_data_direction direction) | |
92 | { | |
93 | int i; | |
a8308800 | 94 | |
d7a30103 | 95 | for (i = 0; i < nents; i++) { |
a8308800 | 96 | sg[i].dma_address = (dma_addr_t)page_address(sg[i].page) |
d7a30103 HS |
97 | + sg[i].offset; |
98 | sg[i].dma_length = sg[i].length; | |
99 | } | |
a8308800 | 100 | |
d7a30103 HS |
101 | return nents; |
102 | } | |
103 | ||
104 | static void ibmebus_unmap_sg(struct device *dev, | |
105 | struct scatterlist *sg, | |
106 | int nents, enum dma_data_direction direction) | |
107 | { | |
108 | return; | |
109 | } | |
110 | ||
111 | static int ibmebus_dma_supported(struct device *dev, u64 mask) | |
112 | { | |
113 | return 1; | |
114 | } | |
115 | ||
12d04eef | 116 | static struct dma_mapping_ops ibmebus_dma_ops = { |
d7a30103 HS |
117 | .alloc_coherent = ibmebus_alloc_coherent, |
118 | .free_coherent = ibmebus_free_coherent, | |
119 | .map_single = ibmebus_map_single, | |
120 | .unmap_single = ibmebus_unmap_single, | |
121 | .map_sg = ibmebus_map_sg, | |
122 | .unmap_sg = ibmebus_unmap_sg, | |
123 | .dma_supported = ibmebus_dma_supported, | |
124 | }; | |
125 | ||
126 | static int ibmebus_bus_probe(struct device *dev) | |
127 | { | |
128 | struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev); | |
129 | struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver); | |
130 | const struct of_device_id *id; | |
131 | int error = -ENODEV; | |
a8308800 | 132 | |
d7a30103 HS |
133 | if (!ibmebusdrv->probe) |
134 | return error; | |
a8308800 | 135 | |
d7a30103 HS |
136 | id = of_match_device(ibmebusdrv->id_table, &ibmebusdev->ofdev); |
137 | if (id) { | |
138 | error = ibmebusdrv->probe(ibmebusdev, id); | |
139 | } | |
a8308800 | 140 | |
d7a30103 HS |
141 | return error; |
142 | } | |
143 | ||
144 | static int ibmebus_bus_remove(struct device *dev) | |
145 | { | |
146 | struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev); | |
147 | struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver); | |
a8308800 | 148 | |
d7a30103 HS |
149 | if (ibmebusdrv->remove) { |
150 | return ibmebusdrv->remove(ibmebusdev); | |
151 | } | |
a8308800 | 152 | |
d7a30103 HS |
153 | return 0; |
154 | } | |
155 | ||
156 | static void __devinit ibmebus_dev_release(struct device *dev) | |
157 | { | |
158 | of_node_put(to_ibmebus_dev(dev)->ofdev.node); | |
159 | kfree(to_ibmebus_dev(dev)); | |
160 | } | |
161 | ||
6bccf755 | 162 | static int __devinit ibmebus_register_device_common( |
a7f67bdf | 163 | struct ibmebus_dev *dev, const char *name) |
d7a30103 HS |
164 | { |
165 | int err = 0; | |
166 | ||
6bccf755 | 167 | dev->ofdev.dev.parent = &ibmebus_bus_device; |
d7a30103 HS |
168 | dev->ofdev.dev.bus = &ibmebus_bus_type; |
169 | dev->ofdev.dev.release = ibmebus_dev_release; | |
170 | ||
12d04eef BH |
171 | dev->ofdev.dev.archdata.of_node = dev->ofdev.node; |
172 | dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops; | |
173 | dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node); | |
174 | ||
d7a30103 | 175 | /* An ibmebusdev is based on a of_device. We have to change the |
a8308800 JF |
176 | * bus type to use our own DMA mapping operations. |
177 | */ | |
d7a30103 HS |
178 | if ((err = of_device_register(&dev->ofdev)) != 0) { |
179 | printk(KERN_ERR "%s: failed to register device (%d).\n", | |
180 | __FUNCTION__, err); | |
6bccf755 | 181 | return -ENODEV; |
d7a30103 | 182 | } |
a8308800 | 183 | |
6bccf755 | 184 | return 0; |
d7a30103 HS |
185 | } |
186 | ||
187 | static struct ibmebus_dev* __devinit ibmebus_register_device_node( | |
188 | struct device_node *dn) | |
189 | { | |
190 | struct ibmebus_dev *dev; | |
d8612417 | 191 | int i, len, bus_len; |
d7a30103 | 192 | |
f8485350 | 193 | dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL); |
d8612417 | 194 | if (!dev) |
6bccf755 | 195 | return ERR_PTR(-ENOMEM); |
d7a30103 HS |
196 | |
197 | dev->ofdev.node = of_node_get(dn); | |
a8308800 | 198 | |
d8612417 JF |
199 | len = strlen(dn->full_name + 1); |
200 | bus_len = min(len, BUS_ID_SIZE - 1); | |
201 | memcpy(dev->ofdev.dev.bus_id, dn->full_name + 1 | |
202 | + (len - bus_len), bus_len); | |
203 | for (i = 0; i < bus_len; i++) | |
204 | if (dev->ofdev.dev.bus_id[i] == '/') | |
205 | dev->ofdev.dev.bus_id[i] = '_'; | |
d7a30103 HS |
206 | |
207 | /* Register with generic device framework. */ | |
6bccf755 | 208 | if (ibmebus_register_device_common(dev, dn->name) != 0) { |
d7a30103 | 209 | kfree(dev); |
6bccf755 | 210 | return ERR_PTR(-ENODEV); |
d7a30103 HS |
211 | } |
212 | ||
213 | return dev; | |
214 | } | |
215 | ||
216 | static void ibmebus_probe_of_nodes(char* name) | |
217 | { | |
218 | struct device_node *dn = NULL; | |
a8308800 | 219 | |
d7a30103 | 220 | while ((dn = of_find_node_by_name(dn, name))) { |
6bccf755 | 221 | if (IS_ERR(ibmebus_register_device_node(dn))) { |
d7a30103 | 222 | of_node_put(dn); |
d7a30103 HS |
223 | return; |
224 | } | |
225 | } | |
a8308800 | 226 | |
d7a30103 | 227 | of_node_put(dn); |
a8308800 | 228 | |
d7a30103 HS |
229 | return; |
230 | } | |
231 | ||
232 | static void ibmebus_add_devices_by_id(struct of_device_id *idt) | |
233 | { | |
234 | while (strlen(idt->name) > 0) { | |
235 | ibmebus_probe_of_nodes(idt->name); | |
236 | idt++; | |
237 | } | |
238 | ||
239 | return; | |
240 | } | |
241 | ||
0727702a | 242 | static int ibmebus_match_name(struct device *dev, void *data) |
d7a30103 | 243 | { |
6bccf755 | 244 | const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev); |
e2eb6392 | 245 | const char *name; |
6bccf755 | 246 | |
e2eb6392 | 247 | name = of_get_property(ebus_dev->ofdev.node, "name", NULL); |
6bccf755 | 248 | |
e2eb6392 | 249 | if (name && (strcmp(data, name) == 0)) |
d7a30103 | 250 | return 1; |
a8308800 | 251 | |
d7a30103 HS |
252 | return 0; |
253 | } | |
254 | ||
255 | static int ibmebus_unregister_device(struct device *dev) | |
256 | { | |
d7a30103 HS |
257 | of_device_unregister(to_of_device(dev)); |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | static void ibmebus_remove_devices_by_id(struct of_device_id *idt) | |
263 | { | |
264 | struct device *dev; | |
a8308800 | 265 | |
d7a30103 | 266 | while (strlen(idt->name) > 0) { |
a8308800 | 267 | while ((dev = bus_find_device(&ibmebus_bus_type, NULL, |
d7a30103 | 268 | (void*)idt->name, |
0727702a | 269 | ibmebus_match_name))) { |
d7a30103 HS |
270 | ibmebus_unregister_device(dev); |
271 | } | |
272 | idt++; | |
d7a30103 | 273 | } |
a8308800 | 274 | |
d7a30103 HS |
275 | return; |
276 | } | |
277 | ||
278 | int ibmebus_register_driver(struct ibmebus_driver *drv) | |
279 | { | |
280 | int err = 0; | |
281 | ||
282 | drv->driver.name = drv->name; | |
283 | drv->driver.bus = &ibmebus_bus_type; | |
284 | drv->driver.probe = ibmebus_bus_probe; | |
285 | drv->driver.remove = ibmebus_bus_remove; | |
286 | ||
287 | if ((err = driver_register(&drv->driver) != 0)) | |
288 | return err; | |
289 | ||
6bccf755 JF |
290 | /* remove all supported devices first, in case someone |
291 | * probed them manually before registering the driver */ | |
292 | ibmebus_remove_devices_by_id(drv->id_table); | |
d7a30103 | 293 | ibmebus_add_devices_by_id(drv->id_table); |
a8308800 | 294 | |
d7a30103 HS |
295 | return 0; |
296 | } | |
297 | EXPORT_SYMBOL(ibmebus_register_driver); | |
298 | ||
299 | void ibmebus_unregister_driver(struct ibmebus_driver *drv) | |
a8308800 | 300 | { |
d7a30103 HS |
301 | driver_unregister(&drv->driver); |
302 | ibmebus_remove_devices_by_id(drv->id_table); | |
303 | } | |
304 | EXPORT_SYMBOL(ibmebus_unregister_driver); | |
305 | ||
306 | int ibmebus_request_irq(struct ibmebus_dev *dev, | |
a8308800 | 307 | u32 ist, |
40220c1a | 308 | irq_handler_t handler, |
d7a30103 HS |
309 | unsigned long irq_flags, const char * devname, |
310 | void *dev_id) | |
311 | { | |
6e99e458 | 312 | unsigned int irq = irq_create_mapping(NULL, ist); |
a8308800 | 313 | |
d7a30103 HS |
314 | if (irq == NO_IRQ) |
315 | return -EINVAL; | |
a8308800 | 316 | |
d7a30103 HS |
317 | return request_irq(irq, handler, |
318 | irq_flags, devname, dev_id); | |
319 | } | |
320 | EXPORT_SYMBOL(ibmebus_request_irq); | |
321 | ||
322 | void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id) | |
323 | { | |
0ebfff14 | 324 | unsigned int irq = irq_find_mapping(NULL, ist); |
a8308800 | 325 | |
d7a30103 | 326 | free_irq(irq, dev_id); |
d7a30103 HS |
327 | } |
328 | EXPORT_SYMBOL(ibmebus_free_irq); | |
329 | ||
330 | static int ibmebus_bus_match(struct device *dev, struct device_driver *drv) | |
a8308800 | 331 | { |
d7a30103 HS |
332 | const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev); |
333 | struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv); | |
334 | const struct of_device_id *ids = ebus_drv->id_table; | |
335 | const struct of_device_id *found_id; | |
a8308800 | 336 | |
d7a30103 HS |
337 | if (!ids) |
338 | return 0; | |
a8308800 | 339 | |
d7a30103 HS |
340 | found_id = of_match_device(ids, &ebus_dev->ofdev); |
341 | if (found_id) | |
342 | return 1; | |
a8308800 | 343 | |
d7a30103 HS |
344 | return 0; |
345 | } | |
346 | ||
6bccf755 JF |
347 | static ssize_t name_show(struct device *dev, |
348 | struct device_attribute *attr, char *buf) | |
349 | { | |
350 | struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev); | |
e2eb6392 | 351 | const char *name = of_get_property(ebus_dev->ofdev.node, "name", NULL); |
6bccf755 JF |
352 | return sprintf(buf, "%s\n", name); |
353 | } | |
354 | ||
355 | static struct device_attribute ibmebus_dev_attrs[] = { | |
356 | __ATTR_RO(name), | |
357 | __ATTR_NULL | |
358 | }; | |
359 | ||
0727702a | 360 | static int ibmebus_match_path(struct device *dev, void *data) |
6bccf755 | 361 | { |
0727702a JF |
362 | int rc; |
363 | struct device_node *dn = | |
364 | of_node_get(to_ibmebus_dev(dev)->ofdev.node); | |
6bccf755 | 365 | |
0727702a | 366 | rc = (dn->full_name && (strcasecmp((char*)data, dn->full_name) == 0)); |
6bccf755 | 367 | |
0727702a JF |
368 | of_node_put(dn); |
369 | return rc; | |
370 | } | |
6bccf755 | 371 | |
0727702a JF |
372 | static char *ibmebus_chomp(const char *in, size_t count) |
373 | { | |
61a564fd JJ |
374 | char *out = kmalloc(count + 1, GFP_KERNEL); |
375 | ||
0727702a JF |
376 | if (!out) |
377 | return NULL; | |
378 | ||
379 | memcpy(out, in, count); | |
380 | out[count] = '\0'; | |
381 | if (out[count - 1] == '\n') | |
382 | out[count - 1] = '\0'; | |
383 | ||
384 | return out; | |
6bccf755 JF |
385 | } |
386 | ||
387 | static ssize_t ibmebus_store_probe(struct bus_type *bus, | |
388 | const char *buf, size_t count) | |
389 | { | |
390 | struct device_node *dn = NULL; | |
391 | struct ibmebus_dev *dev; | |
0727702a JF |
392 | char *path; |
393 | ssize_t rc; | |
394 | ||
395 | path = ibmebus_chomp(buf, count); | |
396 | if (!path) | |
397 | return -ENOMEM; | |
398 | ||
399 | if (bus_find_device(&ibmebus_bus_type, NULL, path, | |
400 | ibmebus_match_path)) { | |
401 | printk(KERN_WARNING "%s: %s has already been probed\n", | |
402 | __FUNCTION__, path); | |
403 | rc = -EINVAL; | |
404 | goto out; | |
6bccf755 JF |
405 | } |
406 | ||
0727702a JF |
407 | if ((dn = of_find_node_by_path(path))) { |
408 | dev = ibmebus_register_device_node(dn); | |
409 | of_node_put(dn); | |
410 | rc = IS_ERR(dev) ? PTR_ERR(dev) : count; | |
411 | } else { | |
412 | printk(KERN_WARNING "%s: no such device node: %s\n", | |
413 | __FUNCTION__, path); | |
414 | rc = -ENODEV; | |
6bccf755 JF |
415 | } |
416 | ||
0727702a JF |
417 | out: |
418 | kfree(path); | |
419 | return rc; | |
6bccf755 JF |
420 | } |
421 | ||
422 | static ssize_t ibmebus_store_remove(struct bus_type *bus, | |
423 | const char *buf, size_t count) | |
424 | { | |
425 | struct device *dev; | |
0727702a | 426 | char *path; |
6bccf755 | 427 | |
0727702a JF |
428 | path = ibmebus_chomp(buf, count); |
429 | if (!path) | |
430 | return -ENOMEM; | |
431 | ||
432 | if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path, | |
433 | ibmebus_match_path))) { | |
6bccf755 | 434 | ibmebus_unregister_device(dev); |
0727702a JF |
435 | |
436 | kfree(path); | |
437 | return count; | |
6bccf755 | 438 | } else { |
0727702a JF |
439 | printk(KERN_WARNING "%s: %s not on the bus\n", |
440 | __FUNCTION__, path); | |
441 | ||
442 | kfree(path); | |
6bccf755 JF |
443 | return -ENODEV; |
444 | } | |
6bccf755 JF |
445 | } |
446 | ||
447 | static struct bus_attribute ibmebus_bus_attrs[] = { | |
448 | __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe), | |
449 | __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove), | |
450 | __ATTR_NULL | |
451 | }; | |
452 | ||
d7a30103 | 453 | struct bus_type ibmebus_bus_type = { |
6bccf755 JF |
454 | .name = "ibmebus", |
455 | .match = ibmebus_bus_match, | |
456 | .dev_attrs = ibmebus_dev_attrs, | |
457 | .bus_attrs = ibmebus_bus_attrs | |
d7a30103 HS |
458 | }; |
459 | EXPORT_SYMBOL(ibmebus_bus_type); | |
460 | ||
461 | static int __init ibmebus_bus_init(void) | |
462 | { | |
463 | int err; | |
a8308800 | 464 | |
d7a30103 | 465 | printk(KERN_INFO "IBM eBus Device Driver\n"); |
a8308800 | 466 | |
d7a30103 HS |
467 | err = bus_register(&ibmebus_bus_type); |
468 | if (err) { | |
469 | printk(KERN_ERR ":%s: failed to register IBM eBus.\n", | |
470 | __FUNCTION__); | |
471 | return err; | |
472 | } | |
a8308800 | 473 | |
6bccf755 | 474 | err = device_register(&ibmebus_bus_device); |
d7a30103 | 475 | if (err) { |
a8308800 | 476 | printk(KERN_WARNING "%s: device_register returned %i\n", |
d7a30103 HS |
477 | __FUNCTION__, err); |
478 | bus_unregister(&ibmebus_bus_type); | |
479 | ||
480 | return err; | |
481 | } | |
a8308800 | 482 | |
d7a30103 HS |
483 | return 0; |
484 | } | |
485 | __initcall(ibmebus_bus_init); |