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>
10 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
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/console.h>
41 #include <linux/kobject.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/interrupt.h>
45 #include <linux/slab.h>
46 #include <linux/of_platform.h>
47 #include <asm/ibmebus.h>
48 #include <asm/abs_addr.h>
50 static struct device ibmebus_bus_device
= { /* fake "parent" device */
51 .init_name
= "ibmebus",
54 struct bus_type ibmebus_bus_type
;
56 /* These devices will automatically be added to the bus during init */
57 static struct of_device_id __initdata ibmebus_matches
[] = {
58 { .compatible
= "IBM,lhca" },
59 { .compatible
= "IBM,lhea" },
63 static void *ibmebus_alloc_coherent(struct device
*dev
,
65 dma_addr_t
*dma_handle
,
70 mem
= kmalloc(size
, flag
);
71 *dma_handle
= (dma_addr_t
)mem
;
76 static void ibmebus_free_coherent(struct device
*dev
,
77 size_t size
, void *vaddr
,
78 dma_addr_t dma_handle
)
83 static dma_addr_t
ibmebus_map_page(struct device
*dev
,
87 enum dma_data_direction direction
,
88 struct dma_attrs
*attrs
)
90 return (dma_addr_t
)(page_address(page
) + offset
);
93 static void ibmebus_unmap_page(struct device
*dev
,
96 enum dma_data_direction direction
,
97 struct dma_attrs
*attrs
)
102 static int ibmebus_map_sg(struct device
*dev
,
103 struct scatterlist
*sgl
,
104 int nents
, enum dma_data_direction direction
,
105 struct dma_attrs
*attrs
)
107 struct scatterlist
*sg
;
110 for_each_sg(sgl
, sg
, nents
, i
) {
111 sg
->dma_address
= (dma_addr_t
) sg_virt(sg
);
112 sg
->dma_length
= sg
->length
;
118 static void ibmebus_unmap_sg(struct device
*dev
,
119 struct scatterlist
*sg
,
120 int nents
, enum dma_data_direction direction
,
121 struct dma_attrs
*attrs
)
126 static int ibmebus_dma_supported(struct device
*dev
, u64 mask
)
131 static struct dma_map_ops ibmebus_dma_ops
= {
132 .alloc_coherent
= ibmebus_alloc_coherent
,
133 .free_coherent
= ibmebus_free_coherent
,
134 .map_sg
= ibmebus_map_sg
,
135 .unmap_sg
= ibmebus_unmap_sg
,
136 .dma_supported
= ibmebus_dma_supported
,
137 .map_page
= ibmebus_map_page
,
138 .unmap_page
= ibmebus_unmap_page
,
141 static int ibmebus_match_path(struct device
*dev
, void *data
)
143 struct device_node
*dn
= to_platform_device(dev
)->dev
.of_node
;
144 return (dn
->full_name
&&
145 (strcasecmp((char *)data
, dn
->full_name
) == 0));
148 static int ibmebus_match_node(struct device
*dev
, void *data
)
150 return to_platform_device(dev
)->dev
.of_node
== data
;
153 static int ibmebus_create_device(struct device_node
*dn
)
155 struct platform_device
*dev
;
158 dev
= of_device_alloc(dn
, NULL
, &ibmebus_bus_device
);
162 dev
->dev
.bus
= &ibmebus_bus_type
;
163 dev
->dev
.archdata
.dma_ops
= &ibmebus_dma_ops
;
165 ret
= of_device_add(dev
);
167 platform_device_put(dev
);
171 static int ibmebus_create_devices(const struct of_device_id
*matches
)
173 struct device_node
*root
, *child
;
176 root
= of_find_node_by_path("/");
178 for_each_child_of_node(root
, child
) {
179 if (!of_match_node(matches
, child
))
182 if (bus_find_device(&ibmebus_bus_type
, NULL
, child
,
186 ret
= ibmebus_create_device(child
);
188 printk(KERN_ERR
"%s: failed to create device (%i)",
199 int ibmebus_register_driver(struct of_platform_driver
*drv
)
201 /* If the driver uses devices that ibmebus doesn't know, add them */
202 ibmebus_create_devices(drv
->driver
.of_match_table
);
204 drv
->driver
.bus
= &ibmebus_bus_type
;
205 return driver_register(&drv
->driver
);
207 EXPORT_SYMBOL(ibmebus_register_driver
);
209 void ibmebus_unregister_driver(struct of_platform_driver
*drv
)
211 driver_unregister(&drv
->driver
);
213 EXPORT_SYMBOL(ibmebus_unregister_driver
);
215 int ibmebus_request_irq(u32 ist
, irq_handler_t handler
,
216 unsigned long irq_flags
, const char *devname
,
219 unsigned int irq
= irq_create_mapping(NULL
, ist
);
224 return request_irq(irq
, handler
, irq_flags
, devname
, dev_id
);
226 EXPORT_SYMBOL(ibmebus_request_irq
);
228 void ibmebus_free_irq(u32 ist
, void *dev_id
)
230 unsigned int irq
= irq_find_mapping(NULL
, ist
);
232 free_irq(irq
, dev_id
);
233 irq_dispose_mapping(irq
);
235 EXPORT_SYMBOL(ibmebus_free_irq
);
237 static char *ibmebus_chomp(const char *in
, size_t count
)
239 char *out
= kmalloc(count
+ 1, GFP_KERNEL
);
244 memcpy(out
, in
, count
);
246 if (out
[count
- 1] == '\n')
247 out
[count
- 1] = '\0';
252 static ssize_t
ibmebus_store_probe(struct bus_type
*bus
,
253 const char *buf
, size_t count
)
255 struct device_node
*dn
= NULL
;
259 path
= ibmebus_chomp(buf
, count
);
263 if (bus_find_device(&ibmebus_bus_type
, NULL
, path
,
264 ibmebus_match_path
)) {
265 printk(KERN_WARNING
"%s: %s has already been probed\n",
271 if ((dn
= of_find_node_by_path(path
))) {
272 rc
= ibmebus_create_device(dn
);
275 printk(KERN_WARNING
"%s: no such device node: %s\n",
287 static ssize_t
ibmebus_store_remove(struct bus_type
*bus
,
288 const char *buf
, size_t count
)
293 path
= ibmebus_chomp(buf
, count
);
297 if ((dev
= bus_find_device(&ibmebus_bus_type
, NULL
, path
,
298 ibmebus_match_path
))) {
299 of_device_unregister(to_platform_device(dev
));
304 printk(KERN_WARNING
"%s: %s not on the bus\n",
313 static struct bus_attribute ibmebus_bus_attrs
[] = {
314 __ATTR(probe
, S_IWUSR
, NULL
, ibmebus_store_probe
),
315 __ATTR(remove
, S_IWUSR
, NULL
, ibmebus_store_remove
),
319 static int ibmebus_bus_bus_match(struct device
*dev
, struct device_driver
*drv
)
321 const struct of_device_id
*matches
= drv
->of_match_table
;
326 return of_match_device(matches
, dev
) != NULL
;
329 static int ibmebus_bus_device_probe(struct device
*dev
)
332 struct of_platform_driver
*drv
;
333 struct platform_device
*of_dev
;
334 const struct of_device_id
*match
;
336 drv
= to_of_platform_driver(dev
->driver
);
337 of_dev
= to_platform_device(dev
);
344 match
= of_match_device(drv
->driver
.of_match_table
, dev
);
346 error
= drv
->probe(of_dev
, match
);
353 static int ibmebus_bus_device_remove(struct device
*dev
)
355 struct platform_device
*of_dev
= to_platform_device(dev
);
356 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
358 if (dev
->driver
&& drv
->remove
)
363 static void ibmebus_bus_device_shutdown(struct device
*dev
)
365 struct platform_device
*of_dev
= to_platform_device(dev
);
366 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
368 if (dev
->driver
&& drv
->shutdown
)
369 drv
->shutdown(of_dev
);
373 * ibmebus_bus_device_attrs
375 static ssize_t
devspec_show(struct device
*dev
,
376 struct device_attribute
*attr
, char *buf
)
378 struct platform_device
*ofdev
;
380 ofdev
= to_platform_device(dev
);
381 return sprintf(buf
, "%s\n", ofdev
->dev
.of_node
->full_name
);
384 static ssize_t
name_show(struct device
*dev
,
385 struct device_attribute
*attr
, char *buf
)
387 struct platform_device
*ofdev
;
389 ofdev
= to_platform_device(dev
);
390 return sprintf(buf
, "%s\n", ofdev
->dev
.of_node
->name
);
393 static ssize_t
modalias_show(struct device
*dev
,
394 struct device_attribute
*attr
, char *buf
)
396 ssize_t len
= of_device_get_modalias(dev
, buf
, PAGE_SIZE
- 2);
402 struct device_attribute ibmebus_bus_device_attrs
[] = {
409 #ifdef CONFIG_PM_SLEEP
410 static int ibmebus_bus_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
412 struct platform_device
*of_dev
= to_platform_device(dev
);
413 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
416 if (dev
->driver
&& drv
->suspend
)
417 ret
= drv
->suspend(of_dev
, mesg
);
421 static int ibmebus_bus_legacy_resume(struct device
*dev
)
423 struct platform_device
*of_dev
= to_platform_device(dev
);
424 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
427 if (dev
->driver
&& drv
->resume
)
428 ret
= drv
->resume(of_dev
);
432 static int ibmebus_bus_pm_prepare(struct device
*dev
)
434 struct device_driver
*drv
= dev
->driver
;
437 if (drv
&& drv
->pm
&& drv
->pm
->prepare
)
438 ret
= drv
->pm
->prepare(dev
);
443 static void ibmebus_bus_pm_complete(struct device
*dev
)
445 struct device_driver
*drv
= dev
->driver
;
447 if (drv
&& drv
->pm
&& drv
->pm
->complete
)
448 drv
->pm
->complete(dev
);
451 #ifdef CONFIG_SUSPEND
453 static int ibmebus_bus_pm_suspend(struct device
*dev
)
455 struct device_driver
*drv
= dev
->driver
;
462 if (drv
->pm
->suspend
)
463 ret
= drv
->pm
->suspend(dev
);
465 ret
= ibmebus_bus_legacy_suspend(dev
, PMSG_SUSPEND
);
471 static int ibmebus_bus_pm_suspend_noirq(struct device
*dev
)
473 struct device_driver
*drv
= dev
->driver
;
480 if (drv
->pm
->suspend_noirq
)
481 ret
= drv
->pm
->suspend_noirq(dev
);
487 static int ibmebus_bus_pm_resume(struct device
*dev
)
489 struct device_driver
*drv
= dev
->driver
;
497 ret
= drv
->pm
->resume(dev
);
499 ret
= ibmebus_bus_legacy_resume(dev
);
505 static int ibmebus_bus_pm_resume_noirq(struct device
*dev
)
507 struct device_driver
*drv
= dev
->driver
;
514 if (drv
->pm
->resume_noirq
)
515 ret
= drv
->pm
->resume_noirq(dev
);
521 #else /* !CONFIG_SUSPEND */
523 #define ibmebus_bus_pm_suspend NULL
524 #define ibmebus_bus_pm_resume NULL
525 #define ibmebus_bus_pm_suspend_noirq NULL
526 #define ibmebus_bus_pm_resume_noirq NULL
528 #endif /* !CONFIG_SUSPEND */
530 #ifdef CONFIG_HIBERNATION
532 static int ibmebus_bus_pm_freeze(struct device
*dev
)
534 struct device_driver
*drv
= dev
->driver
;
542 ret
= drv
->pm
->freeze(dev
);
544 ret
= ibmebus_bus_legacy_suspend(dev
, PMSG_FREEZE
);
550 static int ibmebus_bus_pm_freeze_noirq(struct device
*dev
)
552 struct device_driver
*drv
= dev
->driver
;
559 if (drv
->pm
->freeze_noirq
)
560 ret
= drv
->pm
->freeze_noirq(dev
);
566 static int ibmebus_bus_pm_thaw(struct device
*dev
)
568 struct device_driver
*drv
= dev
->driver
;
576 ret
= drv
->pm
->thaw(dev
);
578 ret
= ibmebus_bus_legacy_resume(dev
);
584 static int ibmebus_bus_pm_thaw_noirq(struct device
*dev
)
586 struct device_driver
*drv
= dev
->driver
;
593 if (drv
->pm
->thaw_noirq
)
594 ret
= drv
->pm
->thaw_noirq(dev
);
600 static int ibmebus_bus_pm_poweroff(struct device
*dev
)
602 struct device_driver
*drv
= dev
->driver
;
609 if (drv
->pm
->poweroff
)
610 ret
= drv
->pm
->poweroff(dev
);
612 ret
= ibmebus_bus_legacy_suspend(dev
, PMSG_HIBERNATE
);
618 static int ibmebus_bus_pm_poweroff_noirq(struct device
*dev
)
620 struct device_driver
*drv
= dev
->driver
;
627 if (drv
->pm
->poweroff_noirq
)
628 ret
= drv
->pm
->poweroff_noirq(dev
);
634 static int ibmebus_bus_pm_restore(struct device
*dev
)
636 struct device_driver
*drv
= dev
->driver
;
643 if (drv
->pm
->restore
)
644 ret
= drv
->pm
->restore(dev
);
646 ret
= ibmebus_bus_legacy_resume(dev
);
652 static int ibmebus_bus_pm_restore_noirq(struct device
*dev
)
654 struct device_driver
*drv
= dev
->driver
;
661 if (drv
->pm
->restore_noirq
)
662 ret
= drv
->pm
->restore_noirq(dev
);
668 #else /* !CONFIG_HIBERNATION */
670 #define ibmebus_bus_pm_freeze NULL
671 #define ibmebus_bus_pm_thaw NULL
672 #define ibmebus_bus_pm_poweroff NULL
673 #define ibmebus_bus_pm_restore NULL
674 #define ibmebus_bus_pm_freeze_noirq NULL
675 #define ibmebus_bus_pm_thaw_noirq NULL
676 #define ibmebus_bus_pm_poweroff_noirq NULL
677 #define ibmebus_bus_pm_restore_noirq NULL
679 #endif /* !CONFIG_HIBERNATION */
681 static struct dev_pm_ops ibmebus_bus_dev_pm_ops
= {
682 .prepare
= ibmebus_bus_pm_prepare
,
683 .complete
= ibmebus_bus_pm_complete
,
684 .suspend
= ibmebus_bus_pm_suspend
,
685 .resume
= ibmebus_bus_pm_resume
,
686 .freeze
= ibmebus_bus_pm_freeze
,
687 .thaw
= ibmebus_bus_pm_thaw
,
688 .poweroff
= ibmebus_bus_pm_poweroff
,
689 .restore
= ibmebus_bus_pm_restore
,
690 .suspend_noirq
= ibmebus_bus_pm_suspend_noirq
,
691 .resume_noirq
= ibmebus_bus_pm_resume_noirq
,
692 .freeze_noirq
= ibmebus_bus_pm_freeze_noirq
,
693 .thaw_noirq
= ibmebus_bus_pm_thaw_noirq
,
694 .poweroff_noirq
= ibmebus_bus_pm_poweroff_noirq
,
695 .restore_noirq
= ibmebus_bus_pm_restore_noirq
,
698 #define IBMEBUS_BUS_PM_OPS_PTR (&ibmebus_bus_dev_pm_ops)
700 #else /* !CONFIG_PM_SLEEP */
702 #define IBMEBUS_BUS_PM_OPS_PTR NULL
704 #endif /* !CONFIG_PM_SLEEP */
706 struct bus_type ibmebus_bus_type
= {
708 .uevent
= of_device_uevent
,
709 .bus_attrs
= ibmebus_bus_attrs
,
710 .match
= ibmebus_bus_bus_match
,
711 .probe
= ibmebus_bus_device_probe
,
712 .remove
= ibmebus_bus_device_remove
,
713 .shutdown
= ibmebus_bus_device_shutdown
,
714 .dev_attrs
= ibmebus_bus_device_attrs
,
715 .pm
= IBMEBUS_BUS_PM_OPS_PTR
,
717 EXPORT_SYMBOL(ibmebus_bus_type
);
719 static int __init
ibmebus_bus_init(void)
723 printk(KERN_INFO
"IBM eBus Device Driver\n");
725 err
= bus_register(&ibmebus_bus_type
);
727 printk(KERN_ERR
"%s: failed to register IBM eBus.\n",
732 err
= device_register(&ibmebus_bus_device
);
734 printk(KERN_WARNING
"%s: device_register returned %i\n",
736 bus_unregister(&ibmebus_bus_type
);
741 err
= ibmebus_create_devices(ibmebus_matches
);
743 device_unregister(&ibmebus_bus_device
);
744 bus_unregister(&ibmebus_bus_type
);
750 postcore_initcall(ibmebus_bus_init
);