Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / arch / powerpc / kernel / ibmebus.c
blobf636d417b28fafd5a1ff024b028a9805cff154b4
1 /*
2 * IBM PowerPC IBM eBus Infrastructure Support.
4 * Copyright (c) 2005 IBM Corporation
5 * Joachim Fenkes <fenkes@de.ibm.com>
6 * Heiko J Schick <schickhj@de.ibm.com>
8 * All rights reserved.
10 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
11 * BSD.
13 * OpenIB BSD License
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
18 * Redistributions of source code must retain the above copyright notice, this
19 * list of conditions and the following disclaimer.
21 * Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials
24 * provided with the distribution.
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
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
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
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/init.h>
40 #include <linux/export.h>
41 #include <linux/console.h>
42 #include <linux/kobject.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/interrupt.h>
45 #include <linux/of.h>
46 #include <linux/slab.h>
47 #include <linux/stat.h>
48 #include <linux/of_platform.h>
49 #include <asm/ibmebus.h>
50 #include <asm/abs_addr.h>
52 static struct device ibmebus_bus_device = { /* fake "parent" device */
53 .init_name = "ibmebus",
56 struct bus_type ibmebus_bus_type;
58 /* These devices will automatically be added to the bus during init */
59 static struct of_device_id __initdata ibmebus_matches[] = {
60 { .compatible = "IBM,lhca" },
61 { .compatible = "IBM,lhea" },
62 {},
65 static void *ibmebus_alloc_coherent(struct device *dev,
66 size_t size,
67 dma_addr_t *dma_handle,
68 gfp_t flag)
70 void *mem;
72 mem = kmalloc(size, flag);
73 *dma_handle = (dma_addr_t)mem;
75 return mem;
78 static void ibmebus_free_coherent(struct device *dev,
79 size_t size, void *vaddr,
80 dma_addr_t dma_handle)
82 kfree(vaddr);
85 static dma_addr_t ibmebus_map_page(struct device *dev,
86 struct page *page,
87 unsigned long offset,
88 size_t size,
89 enum dma_data_direction direction,
90 struct dma_attrs *attrs)
92 return (dma_addr_t)(page_address(page) + offset);
95 static void ibmebus_unmap_page(struct device *dev,
96 dma_addr_t dma_addr,
97 size_t size,
98 enum dma_data_direction direction,
99 struct dma_attrs *attrs)
101 return;
104 static int ibmebus_map_sg(struct device *dev,
105 struct scatterlist *sgl,
106 int nents, enum dma_data_direction direction,
107 struct dma_attrs *attrs)
109 struct scatterlist *sg;
110 int i;
112 for_each_sg(sgl, sg, nents, i) {
113 sg->dma_address = (dma_addr_t) sg_virt(sg);
114 sg->dma_length = sg->length;
117 return nents;
120 static void ibmebus_unmap_sg(struct device *dev,
121 struct scatterlist *sg,
122 int nents, enum dma_data_direction direction,
123 struct dma_attrs *attrs)
125 return;
128 static int ibmebus_dma_supported(struct device *dev, u64 mask)
130 return 1;
133 static struct dma_map_ops ibmebus_dma_ops = {
134 .alloc_coherent = ibmebus_alloc_coherent,
135 .free_coherent = ibmebus_free_coherent,
136 .map_sg = ibmebus_map_sg,
137 .unmap_sg = ibmebus_unmap_sg,
138 .dma_supported = ibmebus_dma_supported,
139 .map_page = ibmebus_map_page,
140 .unmap_page = ibmebus_unmap_page,
143 static int ibmebus_match_path(struct device *dev, void *data)
145 struct device_node *dn = to_platform_device(dev)->dev.of_node;
146 return (dn->full_name &&
147 (strcasecmp((char *)data, dn->full_name) == 0));
150 static int ibmebus_match_node(struct device *dev, void *data)
152 return to_platform_device(dev)->dev.of_node == data;
155 static int ibmebus_create_device(struct device_node *dn)
157 struct platform_device *dev;
158 int ret;
160 dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
161 if (!dev)
162 return -ENOMEM;
164 dev->dev.bus = &ibmebus_bus_type;
165 dev->dev.archdata.dma_ops = &ibmebus_dma_ops;
167 ret = of_device_add(dev);
168 if (ret)
169 platform_device_put(dev);
170 return ret;
173 static int ibmebus_create_devices(const struct of_device_id *matches)
175 struct device_node *root, *child;
176 int ret = 0;
178 root = of_find_node_by_path("/");
180 for_each_child_of_node(root, child) {
181 if (!of_match_node(matches, child))
182 continue;
184 if (bus_find_device(&ibmebus_bus_type, NULL, child,
185 ibmebus_match_node))
186 continue;
188 ret = ibmebus_create_device(child);
189 if (ret) {
190 printk(KERN_ERR "%s: failed to create device (%i)",
191 __func__, ret);
192 of_node_put(child);
193 break;
197 of_node_put(root);
198 return ret;
201 int ibmebus_register_driver(struct of_platform_driver *drv)
203 /* If the driver uses devices that ibmebus doesn't know, add them */
204 ibmebus_create_devices(drv->driver.of_match_table);
206 drv->driver.bus = &ibmebus_bus_type;
207 return driver_register(&drv->driver);
209 EXPORT_SYMBOL(ibmebus_register_driver);
211 void ibmebus_unregister_driver(struct of_platform_driver *drv)
213 driver_unregister(&drv->driver);
215 EXPORT_SYMBOL(ibmebus_unregister_driver);
217 int ibmebus_request_irq(u32 ist, irq_handler_t handler,
218 unsigned long irq_flags, const char *devname,
219 void *dev_id)
221 unsigned int irq = irq_create_mapping(NULL, ist);
223 if (irq == NO_IRQ)
224 return -EINVAL;
226 return request_irq(irq, handler, irq_flags, devname, dev_id);
228 EXPORT_SYMBOL(ibmebus_request_irq);
230 void ibmebus_free_irq(u32 ist, void *dev_id)
232 unsigned int irq = irq_find_mapping(NULL, ist);
234 free_irq(irq, dev_id);
235 irq_dispose_mapping(irq);
237 EXPORT_SYMBOL(ibmebus_free_irq);
239 static char *ibmebus_chomp(const char *in, size_t count)
241 char *out = kmalloc(count + 1, GFP_KERNEL);
243 if (!out)
244 return NULL;
246 memcpy(out, in, count);
247 out[count] = '\0';
248 if (out[count - 1] == '\n')
249 out[count - 1] = '\0';
251 return out;
254 static ssize_t ibmebus_store_probe(struct bus_type *bus,
255 const char *buf, size_t count)
257 struct device_node *dn = NULL;
258 char *path;
259 ssize_t rc = 0;
261 path = ibmebus_chomp(buf, count);
262 if (!path)
263 return -ENOMEM;
265 if (bus_find_device(&ibmebus_bus_type, NULL, path,
266 ibmebus_match_path)) {
267 printk(KERN_WARNING "%s: %s has already been probed\n",
268 __func__, path);
269 rc = -EEXIST;
270 goto out;
273 if ((dn = of_find_node_by_path(path))) {
274 rc = ibmebus_create_device(dn);
275 of_node_put(dn);
276 } else {
277 printk(KERN_WARNING "%s: no such device node: %s\n",
278 __func__, path);
279 rc = -ENODEV;
282 out:
283 kfree(path);
284 if (rc)
285 return rc;
286 return count;
289 static ssize_t ibmebus_store_remove(struct bus_type *bus,
290 const char *buf, size_t count)
292 struct device *dev;
293 char *path;
295 path = ibmebus_chomp(buf, count);
296 if (!path)
297 return -ENOMEM;
299 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
300 ibmebus_match_path))) {
301 of_device_unregister(to_platform_device(dev));
303 kfree(path);
304 return count;
305 } else {
306 printk(KERN_WARNING "%s: %s not on the bus\n",
307 __func__, path);
309 kfree(path);
310 return -ENODEV;
315 static struct bus_attribute ibmebus_bus_attrs[] = {
316 __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
317 __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
318 __ATTR_NULL
321 static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
323 const struct of_device_id *matches = drv->of_match_table;
325 if (!matches)
326 return 0;
328 return of_match_device(matches, dev) != NULL;
331 static int ibmebus_bus_device_probe(struct device *dev)
333 int error = -ENODEV;
334 struct of_platform_driver *drv;
335 struct platform_device *of_dev;
336 const struct of_device_id *match;
338 drv = to_of_platform_driver(dev->driver);
339 of_dev = to_platform_device(dev);
341 if (!drv->probe)
342 return error;
344 of_dev_get(of_dev);
346 match = of_match_device(drv->driver.of_match_table, dev);
347 if (match)
348 error = drv->probe(of_dev, match);
349 if (error)
350 of_dev_put(of_dev);
352 return error;
355 static int ibmebus_bus_device_remove(struct device *dev)
357 struct platform_device *of_dev = to_platform_device(dev);
358 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
360 if (dev->driver && drv->remove)
361 drv->remove(of_dev);
362 return 0;
365 static void ibmebus_bus_device_shutdown(struct device *dev)
367 struct platform_device *of_dev = to_platform_device(dev);
368 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
370 if (dev->driver && drv->shutdown)
371 drv->shutdown(of_dev);
375 * ibmebus_bus_device_attrs
377 static ssize_t devspec_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
380 struct platform_device *ofdev;
382 ofdev = to_platform_device(dev);
383 return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
386 static ssize_t name_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 struct platform_device *ofdev;
391 ofdev = to_platform_device(dev);
392 return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
395 static ssize_t modalias_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
398 ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
399 buf[len] = '\n';
400 buf[len+1] = 0;
401 return len+1;
404 struct device_attribute ibmebus_bus_device_attrs[] = {
405 __ATTR_RO(devspec),
406 __ATTR_RO(name),
407 __ATTR_RO(modalias),
408 __ATTR_NULL
411 #ifdef CONFIG_PM_SLEEP
412 static int ibmebus_bus_legacy_suspend(struct device *dev, pm_message_t mesg)
414 struct platform_device *of_dev = to_platform_device(dev);
415 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
416 int ret = 0;
418 if (dev->driver && drv->suspend)
419 ret = drv->suspend(of_dev, mesg);
420 return ret;
423 static int ibmebus_bus_legacy_resume(struct device *dev)
425 struct platform_device *of_dev = to_platform_device(dev);
426 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
427 int ret = 0;
429 if (dev->driver && drv->resume)
430 ret = drv->resume(of_dev);
431 return ret;
434 static int ibmebus_bus_pm_prepare(struct device *dev)
436 struct device_driver *drv = dev->driver;
437 int ret = 0;
439 if (drv && drv->pm && drv->pm->prepare)
440 ret = drv->pm->prepare(dev);
442 return ret;
445 static void ibmebus_bus_pm_complete(struct device *dev)
447 struct device_driver *drv = dev->driver;
449 if (drv && drv->pm && drv->pm->complete)
450 drv->pm->complete(dev);
453 #ifdef CONFIG_SUSPEND
455 static int ibmebus_bus_pm_suspend(struct device *dev)
457 struct device_driver *drv = dev->driver;
458 int ret = 0;
460 if (!drv)
461 return 0;
463 if (drv->pm) {
464 if (drv->pm->suspend)
465 ret = drv->pm->suspend(dev);
466 } else {
467 ret = ibmebus_bus_legacy_suspend(dev, PMSG_SUSPEND);
470 return ret;
473 static int ibmebus_bus_pm_suspend_noirq(struct device *dev)
475 struct device_driver *drv = dev->driver;
476 int ret = 0;
478 if (!drv)
479 return 0;
481 if (drv->pm) {
482 if (drv->pm->suspend_noirq)
483 ret = drv->pm->suspend_noirq(dev);
486 return ret;
489 static int ibmebus_bus_pm_resume(struct device *dev)
491 struct device_driver *drv = dev->driver;
492 int ret = 0;
494 if (!drv)
495 return 0;
497 if (drv->pm) {
498 if (drv->pm->resume)
499 ret = drv->pm->resume(dev);
500 } else {
501 ret = ibmebus_bus_legacy_resume(dev);
504 return ret;
507 static int ibmebus_bus_pm_resume_noirq(struct device *dev)
509 struct device_driver *drv = dev->driver;
510 int ret = 0;
512 if (!drv)
513 return 0;
515 if (drv->pm) {
516 if (drv->pm->resume_noirq)
517 ret = drv->pm->resume_noirq(dev);
520 return ret;
523 #else /* !CONFIG_SUSPEND */
525 #define ibmebus_bus_pm_suspend NULL
526 #define ibmebus_bus_pm_resume NULL
527 #define ibmebus_bus_pm_suspend_noirq NULL
528 #define ibmebus_bus_pm_resume_noirq NULL
530 #endif /* !CONFIG_SUSPEND */
532 #ifdef CONFIG_HIBERNATE_CALLBACKS
534 static int ibmebus_bus_pm_freeze(struct device *dev)
536 struct device_driver *drv = dev->driver;
537 int ret = 0;
539 if (!drv)
540 return 0;
542 if (drv->pm) {
543 if (drv->pm->freeze)
544 ret = drv->pm->freeze(dev);
545 } else {
546 ret = ibmebus_bus_legacy_suspend(dev, PMSG_FREEZE);
549 return ret;
552 static int ibmebus_bus_pm_freeze_noirq(struct device *dev)
554 struct device_driver *drv = dev->driver;
555 int ret = 0;
557 if (!drv)
558 return 0;
560 if (drv->pm) {
561 if (drv->pm->freeze_noirq)
562 ret = drv->pm->freeze_noirq(dev);
565 return ret;
568 static int ibmebus_bus_pm_thaw(struct device *dev)
570 struct device_driver *drv = dev->driver;
571 int ret = 0;
573 if (!drv)
574 return 0;
576 if (drv->pm) {
577 if (drv->pm->thaw)
578 ret = drv->pm->thaw(dev);
579 } else {
580 ret = ibmebus_bus_legacy_resume(dev);
583 return ret;
586 static int ibmebus_bus_pm_thaw_noirq(struct device *dev)
588 struct device_driver *drv = dev->driver;
589 int ret = 0;
591 if (!drv)
592 return 0;
594 if (drv->pm) {
595 if (drv->pm->thaw_noirq)
596 ret = drv->pm->thaw_noirq(dev);
599 return ret;
602 static int ibmebus_bus_pm_poweroff(struct device *dev)
604 struct device_driver *drv = dev->driver;
605 int ret = 0;
607 if (!drv)
608 return 0;
610 if (drv->pm) {
611 if (drv->pm->poweroff)
612 ret = drv->pm->poweroff(dev);
613 } else {
614 ret = ibmebus_bus_legacy_suspend(dev, PMSG_HIBERNATE);
617 return ret;
620 static int ibmebus_bus_pm_poweroff_noirq(struct device *dev)
622 struct device_driver *drv = dev->driver;
623 int ret = 0;
625 if (!drv)
626 return 0;
628 if (drv->pm) {
629 if (drv->pm->poweroff_noirq)
630 ret = drv->pm->poweroff_noirq(dev);
633 return ret;
636 static int ibmebus_bus_pm_restore(struct device *dev)
638 struct device_driver *drv = dev->driver;
639 int ret = 0;
641 if (!drv)
642 return 0;
644 if (drv->pm) {
645 if (drv->pm->restore)
646 ret = drv->pm->restore(dev);
647 } else {
648 ret = ibmebus_bus_legacy_resume(dev);
651 return ret;
654 static int ibmebus_bus_pm_restore_noirq(struct device *dev)
656 struct device_driver *drv = dev->driver;
657 int ret = 0;
659 if (!drv)
660 return 0;
662 if (drv->pm) {
663 if (drv->pm->restore_noirq)
664 ret = drv->pm->restore_noirq(dev);
667 return ret;
670 #else /* !CONFIG_HIBERNATE_CALLBACKS */
672 #define ibmebus_bus_pm_freeze NULL
673 #define ibmebus_bus_pm_thaw NULL
674 #define ibmebus_bus_pm_poweroff NULL
675 #define ibmebus_bus_pm_restore NULL
676 #define ibmebus_bus_pm_freeze_noirq NULL
677 #define ibmebus_bus_pm_thaw_noirq NULL
678 #define ibmebus_bus_pm_poweroff_noirq NULL
679 #define ibmebus_bus_pm_restore_noirq NULL
681 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
683 static struct dev_pm_ops ibmebus_bus_dev_pm_ops = {
684 .prepare = ibmebus_bus_pm_prepare,
685 .complete = ibmebus_bus_pm_complete,
686 .suspend = ibmebus_bus_pm_suspend,
687 .resume = ibmebus_bus_pm_resume,
688 .freeze = ibmebus_bus_pm_freeze,
689 .thaw = ibmebus_bus_pm_thaw,
690 .poweroff = ibmebus_bus_pm_poweroff,
691 .restore = ibmebus_bus_pm_restore,
692 .suspend_noirq = ibmebus_bus_pm_suspend_noirq,
693 .resume_noirq = ibmebus_bus_pm_resume_noirq,
694 .freeze_noirq = ibmebus_bus_pm_freeze_noirq,
695 .thaw_noirq = ibmebus_bus_pm_thaw_noirq,
696 .poweroff_noirq = ibmebus_bus_pm_poweroff_noirq,
697 .restore_noirq = ibmebus_bus_pm_restore_noirq,
700 #define IBMEBUS_BUS_PM_OPS_PTR (&ibmebus_bus_dev_pm_ops)
702 #else /* !CONFIG_PM_SLEEP */
704 #define IBMEBUS_BUS_PM_OPS_PTR NULL
706 #endif /* !CONFIG_PM_SLEEP */
708 struct bus_type ibmebus_bus_type = {
709 .name = "ibmebus",
710 .uevent = of_device_uevent,
711 .bus_attrs = ibmebus_bus_attrs,
712 .match = ibmebus_bus_bus_match,
713 .probe = ibmebus_bus_device_probe,
714 .remove = ibmebus_bus_device_remove,
715 .shutdown = ibmebus_bus_device_shutdown,
716 .dev_attrs = ibmebus_bus_device_attrs,
717 .pm = IBMEBUS_BUS_PM_OPS_PTR,
719 EXPORT_SYMBOL(ibmebus_bus_type);
721 static int __init ibmebus_bus_init(void)
723 int err;
725 printk(KERN_INFO "IBM eBus Device Driver\n");
727 err = bus_register(&ibmebus_bus_type);
728 if (err) {
729 printk(KERN_ERR "%s: failed to register IBM eBus.\n",
730 __func__);
731 return err;
734 err = device_register(&ibmebus_bus_device);
735 if (err) {
736 printk(KERN_WARNING "%s: device_register returned %i\n",
737 __func__, err);
738 bus_unregister(&ibmebus_bus_type);
740 return err;
743 err = ibmebus_create_devices(ibmebus_matches);
744 if (err) {
745 device_unregister(&ibmebus_bus_device);
746 bus_unregister(&ibmebus_bus_type);
747 return err;
750 return 0;
752 postcore_initcall(ibmebus_bus_init);