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/export.h>
41 #include <linux/console.h>
42 #include <linux/kobject.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/interrupt.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" },
65 static void *ibmebus_alloc_coherent(struct device
*dev
,
67 dma_addr_t
*dma_handle
,
69 struct dma_attrs
*attrs
)
73 mem
= kmalloc(size
, flag
);
74 *dma_handle
= (dma_addr_t
)mem
;
79 static void ibmebus_free_coherent(struct device
*dev
,
80 size_t size
, void *vaddr
,
81 dma_addr_t dma_handle
,
82 struct dma_attrs
*attrs
)
87 static dma_addr_t
ibmebus_map_page(struct device
*dev
,
91 enum dma_data_direction direction
,
92 struct dma_attrs
*attrs
)
94 return (dma_addr_t
)(page_address(page
) + offset
);
97 static void ibmebus_unmap_page(struct device
*dev
,
100 enum dma_data_direction direction
,
101 struct dma_attrs
*attrs
)
106 static int ibmebus_map_sg(struct device
*dev
,
107 struct scatterlist
*sgl
,
108 int nents
, enum dma_data_direction direction
,
109 struct dma_attrs
*attrs
)
111 struct scatterlist
*sg
;
114 for_each_sg(sgl
, sg
, nents
, i
) {
115 sg
->dma_address
= (dma_addr_t
) sg_virt(sg
);
116 sg
->dma_length
= sg
->length
;
122 static void ibmebus_unmap_sg(struct device
*dev
,
123 struct scatterlist
*sg
,
124 int nents
, enum dma_data_direction direction
,
125 struct dma_attrs
*attrs
)
130 static int ibmebus_dma_supported(struct device
*dev
, u64 mask
)
132 return mask
== DMA_BIT_MASK(64);
135 static u64
ibmebus_dma_get_required_mask(struct device
*dev
)
137 return DMA_BIT_MASK(64);
140 static struct dma_map_ops ibmebus_dma_ops
= {
141 .alloc
= ibmebus_alloc_coherent
,
142 .free
= ibmebus_free_coherent
,
143 .map_sg
= ibmebus_map_sg
,
144 .unmap_sg
= ibmebus_unmap_sg
,
145 .dma_supported
= ibmebus_dma_supported
,
146 .get_required_mask
= ibmebus_dma_get_required_mask
,
147 .map_page
= ibmebus_map_page
,
148 .unmap_page
= ibmebus_unmap_page
,
151 static int ibmebus_match_path(struct device
*dev
, void *data
)
153 struct device_node
*dn
= to_platform_device(dev
)->dev
.of_node
;
154 return (dn
->full_name
&&
155 (strcasecmp((char *)data
, dn
->full_name
) == 0));
158 static int ibmebus_match_node(struct device
*dev
, void *data
)
160 return to_platform_device(dev
)->dev
.of_node
== data
;
163 static int ibmebus_create_device(struct device_node
*dn
)
165 struct platform_device
*dev
;
168 dev
= of_device_alloc(dn
, NULL
, &ibmebus_bus_device
);
172 dev
->dev
.bus
= &ibmebus_bus_type
;
173 dev
->dev
.archdata
.dma_ops
= &ibmebus_dma_ops
;
175 ret
= of_device_add(dev
);
177 platform_device_put(dev
);
181 static int ibmebus_create_devices(const struct of_device_id
*matches
)
183 struct device_node
*root
, *child
;
186 root
= of_find_node_by_path("/");
188 for_each_child_of_node(root
, child
) {
189 if (!of_match_node(matches
, child
))
192 if (bus_find_device(&ibmebus_bus_type
, NULL
, child
,
196 ret
= ibmebus_create_device(child
);
198 printk(KERN_ERR
"%s: failed to create device (%i)",
209 int ibmebus_register_driver(struct of_platform_driver
*drv
)
211 /* If the driver uses devices that ibmebus doesn't know, add them */
212 ibmebus_create_devices(drv
->driver
.of_match_table
);
214 drv
->driver
.bus
= &ibmebus_bus_type
;
215 return driver_register(&drv
->driver
);
217 EXPORT_SYMBOL(ibmebus_register_driver
);
219 void ibmebus_unregister_driver(struct of_platform_driver
*drv
)
221 driver_unregister(&drv
->driver
);
223 EXPORT_SYMBOL(ibmebus_unregister_driver
);
225 int ibmebus_request_irq(u32 ist
, irq_handler_t handler
,
226 unsigned long irq_flags
, const char *devname
,
229 unsigned int irq
= irq_create_mapping(NULL
, ist
);
234 return request_irq(irq
, handler
, irq_flags
, devname
, dev_id
);
236 EXPORT_SYMBOL(ibmebus_request_irq
);
238 void ibmebus_free_irq(u32 ist
, void *dev_id
)
240 unsigned int irq
= irq_find_mapping(NULL
, ist
);
242 free_irq(irq
, dev_id
);
243 irq_dispose_mapping(irq
);
245 EXPORT_SYMBOL(ibmebus_free_irq
);
247 static char *ibmebus_chomp(const char *in
, size_t count
)
249 char *out
= kmalloc(count
+ 1, GFP_KERNEL
);
254 memcpy(out
, in
, count
);
256 if (out
[count
- 1] == '\n')
257 out
[count
- 1] = '\0';
262 static ssize_t
ibmebus_store_probe(struct bus_type
*bus
,
263 const char *buf
, size_t count
)
265 struct device_node
*dn
= NULL
;
269 path
= ibmebus_chomp(buf
, count
);
273 if (bus_find_device(&ibmebus_bus_type
, NULL
, path
,
274 ibmebus_match_path
)) {
275 printk(KERN_WARNING
"%s: %s has already been probed\n",
281 if ((dn
= of_find_node_by_path(path
))) {
282 rc
= ibmebus_create_device(dn
);
285 printk(KERN_WARNING
"%s: no such device node: %s\n",
297 static ssize_t
ibmebus_store_remove(struct bus_type
*bus
,
298 const char *buf
, size_t count
)
303 path
= ibmebus_chomp(buf
, count
);
307 if ((dev
= bus_find_device(&ibmebus_bus_type
, NULL
, path
,
308 ibmebus_match_path
))) {
309 of_device_unregister(to_platform_device(dev
));
314 printk(KERN_WARNING
"%s: %s not on the bus\n",
323 static struct bus_attribute ibmebus_bus_attrs
[] = {
324 __ATTR(probe
, S_IWUSR
, NULL
, ibmebus_store_probe
),
325 __ATTR(remove
, S_IWUSR
, NULL
, ibmebus_store_remove
),
329 static int ibmebus_bus_bus_match(struct device
*dev
, struct device_driver
*drv
)
331 const struct of_device_id
*matches
= drv
->of_match_table
;
336 return of_match_device(matches
, dev
) != NULL
;
339 static int ibmebus_bus_device_probe(struct device
*dev
)
342 struct of_platform_driver
*drv
;
343 struct platform_device
*of_dev
;
344 const struct of_device_id
*match
;
346 drv
= to_of_platform_driver(dev
->driver
);
347 of_dev
= to_platform_device(dev
);
354 match
= of_match_device(drv
->driver
.of_match_table
, dev
);
356 error
= drv
->probe(of_dev
, match
);
363 static int ibmebus_bus_device_remove(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
->remove
)
373 static void ibmebus_bus_device_shutdown(struct device
*dev
)
375 struct platform_device
*of_dev
= to_platform_device(dev
);
376 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
378 if (dev
->driver
&& drv
->shutdown
)
379 drv
->shutdown(of_dev
);
383 * ibmebus_bus_device_attrs
385 static ssize_t
devspec_show(struct device
*dev
,
386 struct device_attribute
*attr
, char *buf
)
388 struct platform_device
*ofdev
;
390 ofdev
= to_platform_device(dev
);
391 return sprintf(buf
, "%s\n", ofdev
->dev
.of_node
->full_name
);
394 static ssize_t
name_show(struct device
*dev
,
395 struct device_attribute
*attr
, char *buf
)
397 struct platform_device
*ofdev
;
399 ofdev
= to_platform_device(dev
);
400 return sprintf(buf
, "%s\n", ofdev
->dev
.of_node
->name
);
403 static ssize_t
modalias_show(struct device
*dev
,
404 struct device_attribute
*attr
, char *buf
)
406 ssize_t len
= of_device_get_modalias(dev
, buf
, PAGE_SIZE
- 2);
412 struct device_attribute ibmebus_bus_device_attrs
[] = {
419 #ifdef CONFIG_PM_SLEEP
420 static int ibmebus_bus_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
422 struct platform_device
*of_dev
= to_platform_device(dev
);
423 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
426 if (dev
->driver
&& drv
->suspend
)
427 ret
= drv
->suspend(of_dev
, mesg
);
431 static int ibmebus_bus_legacy_resume(struct device
*dev
)
433 struct platform_device
*of_dev
= to_platform_device(dev
);
434 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
437 if (dev
->driver
&& drv
->resume
)
438 ret
= drv
->resume(of_dev
);
442 static int ibmebus_bus_pm_prepare(struct device
*dev
)
444 struct device_driver
*drv
= dev
->driver
;
447 if (drv
&& drv
->pm
&& drv
->pm
->prepare
)
448 ret
= drv
->pm
->prepare(dev
);
453 static void ibmebus_bus_pm_complete(struct device
*dev
)
455 struct device_driver
*drv
= dev
->driver
;
457 if (drv
&& drv
->pm
&& drv
->pm
->complete
)
458 drv
->pm
->complete(dev
);
461 #ifdef CONFIG_SUSPEND
463 static int ibmebus_bus_pm_suspend(struct device
*dev
)
465 struct device_driver
*drv
= dev
->driver
;
472 if (drv
->pm
->suspend
)
473 ret
= drv
->pm
->suspend(dev
);
475 ret
= ibmebus_bus_legacy_suspend(dev
, PMSG_SUSPEND
);
481 static int ibmebus_bus_pm_suspend_noirq(struct device
*dev
)
483 struct device_driver
*drv
= dev
->driver
;
490 if (drv
->pm
->suspend_noirq
)
491 ret
= drv
->pm
->suspend_noirq(dev
);
497 static int ibmebus_bus_pm_resume(struct device
*dev
)
499 struct device_driver
*drv
= dev
->driver
;
507 ret
= drv
->pm
->resume(dev
);
509 ret
= ibmebus_bus_legacy_resume(dev
);
515 static int ibmebus_bus_pm_resume_noirq(struct device
*dev
)
517 struct device_driver
*drv
= dev
->driver
;
524 if (drv
->pm
->resume_noirq
)
525 ret
= drv
->pm
->resume_noirq(dev
);
531 #else /* !CONFIG_SUSPEND */
533 #define ibmebus_bus_pm_suspend NULL
534 #define ibmebus_bus_pm_resume NULL
535 #define ibmebus_bus_pm_suspend_noirq NULL
536 #define ibmebus_bus_pm_resume_noirq NULL
538 #endif /* !CONFIG_SUSPEND */
540 #ifdef CONFIG_HIBERNATE_CALLBACKS
542 static int ibmebus_bus_pm_freeze(struct device
*dev
)
544 struct device_driver
*drv
= dev
->driver
;
552 ret
= drv
->pm
->freeze(dev
);
554 ret
= ibmebus_bus_legacy_suspend(dev
, PMSG_FREEZE
);
560 static int ibmebus_bus_pm_freeze_noirq(struct device
*dev
)
562 struct device_driver
*drv
= dev
->driver
;
569 if (drv
->pm
->freeze_noirq
)
570 ret
= drv
->pm
->freeze_noirq(dev
);
576 static int ibmebus_bus_pm_thaw(struct device
*dev
)
578 struct device_driver
*drv
= dev
->driver
;
586 ret
= drv
->pm
->thaw(dev
);
588 ret
= ibmebus_bus_legacy_resume(dev
);
594 static int ibmebus_bus_pm_thaw_noirq(struct device
*dev
)
596 struct device_driver
*drv
= dev
->driver
;
603 if (drv
->pm
->thaw_noirq
)
604 ret
= drv
->pm
->thaw_noirq(dev
);
610 static int ibmebus_bus_pm_poweroff(struct device
*dev
)
612 struct device_driver
*drv
= dev
->driver
;
619 if (drv
->pm
->poweroff
)
620 ret
= drv
->pm
->poweroff(dev
);
622 ret
= ibmebus_bus_legacy_suspend(dev
, PMSG_HIBERNATE
);
628 static int ibmebus_bus_pm_poweroff_noirq(struct device
*dev
)
630 struct device_driver
*drv
= dev
->driver
;
637 if (drv
->pm
->poweroff_noirq
)
638 ret
= drv
->pm
->poweroff_noirq(dev
);
644 static int ibmebus_bus_pm_restore(struct device
*dev
)
646 struct device_driver
*drv
= dev
->driver
;
653 if (drv
->pm
->restore
)
654 ret
= drv
->pm
->restore(dev
);
656 ret
= ibmebus_bus_legacy_resume(dev
);
662 static int ibmebus_bus_pm_restore_noirq(struct device
*dev
)
664 struct device_driver
*drv
= dev
->driver
;
671 if (drv
->pm
->restore_noirq
)
672 ret
= drv
->pm
->restore_noirq(dev
);
678 #else /* !CONFIG_HIBERNATE_CALLBACKS */
680 #define ibmebus_bus_pm_freeze NULL
681 #define ibmebus_bus_pm_thaw NULL
682 #define ibmebus_bus_pm_poweroff NULL
683 #define ibmebus_bus_pm_restore NULL
684 #define ibmebus_bus_pm_freeze_noirq NULL
685 #define ibmebus_bus_pm_thaw_noirq NULL
686 #define ibmebus_bus_pm_poweroff_noirq NULL
687 #define ibmebus_bus_pm_restore_noirq NULL
689 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
691 static struct dev_pm_ops ibmebus_bus_dev_pm_ops
= {
692 .prepare
= ibmebus_bus_pm_prepare
,
693 .complete
= ibmebus_bus_pm_complete
,
694 .suspend
= ibmebus_bus_pm_suspend
,
695 .resume
= ibmebus_bus_pm_resume
,
696 .freeze
= ibmebus_bus_pm_freeze
,
697 .thaw
= ibmebus_bus_pm_thaw
,
698 .poweroff
= ibmebus_bus_pm_poweroff
,
699 .restore
= ibmebus_bus_pm_restore
,
700 .suspend_noirq
= ibmebus_bus_pm_suspend_noirq
,
701 .resume_noirq
= ibmebus_bus_pm_resume_noirq
,
702 .freeze_noirq
= ibmebus_bus_pm_freeze_noirq
,
703 .thaw_noirq
= ibmebus_bus_pm_thaw_noirq
,
704 .poweroff_noirq
= ibmebus_bus_pm_poweroff_noirq
,
705 .restore_noirq
= ibmebus_bus_pm_restore_noirq
,
708 #define IBMEBUS_BUS_PM_OPS_PTR (&ibmebus_bus_dev_pm_ops)
710 #else /* !CONFIG_PM_SLEEP */
712 #define IBMEBUS_BUS_PM_OPS_PTR NULL
714 #endif /* !CONFIG_PM_SLEEP */
716 struct bus_type ibmebus_bus_type
= {
718 .uevent
= of_device_uevent
,
719 .bus_attrs
= ibmebus_bus_attrs
,
720 .match
= ibmebus_bus_bus_match
,
721 .probe
= ibmebus_bus_device_probe
,
722 .remove
= ibmebus_bus_device_remove
,
723 .shutdown
= ibmebus_bus_device_shutdown
,
724 .dev_attrs
= ibmebus_bus_device_attrs
,
725 .pm
= IBMEBUS_BUS_PM_OPS_PTR
,
727 EXPORT_SYMBOL(ibmebus_bus_type
);
729 static int __init
ibmebus_bus_init(void)
733 printk(KERN_INFO
"IBM eBus Device Driver\n");
735 err
= bus_register(&ibmebus_bus_type
);
737 printk(KERN_ERR
"%s: failed to register IBM eBus.\n",
742 err
= device_register(&ibmebus_bus_device
);
744 printk(KERN_WARNING
"%s: device_register returned %i\n",
746 bus_unregister(&ibmebus_bus_type
);
751 err
= ibmebus_create_devices(ibmebus_matches
);
753 device_unregister(&ibmebus_bus_device
);
754 bus_unregister(&ibmebus_bus_type
);
760 postcore_initcall(ibmebus_bus_init
);