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-map-ops.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/machdep.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 const struct of_device_id ibmebus_matches[] __initconst = {
60 { .compatible = "IBM,lhca" },
61 { .compatible = "IBM,lhea" },
65 static void *ibmebus_alloc_coherent(struct device *dev,
67 dma_addr_t *dma_handle,
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,
87 static dma_addr_t ibmebus_map_page(struct device *dev,
91 enum dma_data_direction direction,
94 return (dma_addr_t)(page_address(page) + offset);
97 static void ibmebus_unmap_page(struct device *dev,
100 enum dma_data_direction direction,
106 static int ibmebus_map_sg(struct device *dev,
107 struct scatterlist *sgl,
108 int nents, enum dma_data_direction direction,
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,
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 const 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, const void *data)
153 struct device_node *dn = to_platform_device(dev)->dev.of_node;
154 return (of_find_node_by_path(data) == dn);
157 static int ibmebus_match_node(struct device *dev, const void *data)
159 return to_platform_device(dev)->dev.of_node == data;
162 static int ibmebus_create_device(struct device_node *dn)
164 struct platform_device *dev;
167 dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
171 dev->dev.bus = &ibmebus_bus_type;
172 dev->dev.dma_ops = &ibmebus_dma_ops;
174 ret = of_device_add(dev);
176 platform_device_put(dev);
180 static int ibmebus_create_devices(const struct of_device_id *matches)
182 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 dev = bus_find_device(&ibmebus_bus_type, NULL, child,
199 ret = ibmebus_create_device(child);
201 printk(KERN_ERR "%s: failed to create device (%i)",
212 int ibmebus_register_driver(struct platform_driver *drv)
214 /* If the driver uses devices that ibmebus doesn't know, add them */
215 ibmebus_create_devices(drv->driver.of_match_table);
217 drv->driver.bus = &ibmebus_bus_type;
218 return driver_register(&drv->driver);
220 EXPORT_SYMBOL(ibmebus_register_driver);
222 void ibmebus_unregister_driver(struct platform_driver *drv)
224 driver_unregister(&drv->driver);
226 EXPORT_SYMBOL(ibmebus_unregister_driver);
228 int ibmebus_request_irq(u32 ist, irq_handler_t handler,
229 unsigned long irq_flags, const char *devname,
232 unsigned int irq = irq_create_mapping(NULL, ist);
237 return request_irq(irq, handler, irq_flags, devname, dev_id);
239 EXPORT_SYMBOL(ibmebus_request_irq);
241 void ibmebus_free_irq(u32 ist, void *dev_id)
243 unsigned int irq = irq_find_mapping(NULL, ist);
245 free_irq(irq, dev_id);
246 irq_dispose_mapping(irq);
248 EXPORT_SYMBOL(ibmebus_free_irq);
250 static char *ibmebus_chomp(const char *in, size_t count)
252 char *out = kmalloc(count + 1, GFP_KERNEL);
257 memcpy(out, in, count);
259 if (out[count - 1] == '\n')
260 out[count - 1] = '\0';
265 static ssize_t probe_store(struct bus_type *bus, const char *buf, size_t count)
267 struct device_node *dn = NULL;
272 path = ibmebus_chomp(buf, count);
276 dev = bus_find_device(&ibmebus_bus_type, NULL, path,
280 printk(KERN_WARNING "%s: %s has already been probed\n",
286 if ((dn = of_find_node_by_path(path))) {
287 rc = ibmebus_create_device(dn);
290 printk(KERN_WARNING "%s: no such device node: %s\n",
301 static BUS_ATTR_WO(probe);
303 static ssize_t remove_store(struct bus_type *bus, const char *buf, size_t count)
308 path = ibmebus_chomp(buf, count);
312 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
313 ibmebus_match_path))) {
314 of_device_unregister(to_platform_device(dev));
320 printk(KERN_WARNING "%s: %s not on the bus\n",
327 static BUS_ATTR_WO(remove);
329 static struct attribute *ibmbus_bus_attrs[] = {
330 &bus_attr_probe.attr,
331 &bus_attr_remove.attr,
334 ATTRIBUTE_GROUPS(ibmbus_bus);
336 static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
338 const struct of_device_id *matches = drv->of_match_table;
343 return of_match_device(matches, dev) != NULL;
346 static int ibmebus_bus_device_probe(struct device *dev)
349 struct platform_driver *drv;
350 struct platform_device *of_dev;
352 drv = to_platform_driver(dev->driver);
353 of_dev = to_platform_device(dev);
360 if (of_driver_match_device(dev, dev->driver))
361 error = drv->probe(of_dev);
368 static int ibmebus_bus_device_remove(struct device *dev)
370 struct platform_device *of_dev = to_platform_device(dev);
371 struct platform_driver *drv = to_platform_driver(dev->driver);
373 if (dev->driver && drv->remove)
378 static void ibmebus_bus_device_shutdown(struct device *dev)
380 struct platform_device *of_dev = to_platform_device(dev);
381 struct platform_driver *drv = to_platform_driver(dev->driver);
383 if (dev->driver && drv->shutdown)
384 drv->shutdown(of_dev);
388 * ibmebus_bus_device_attrs
390 static ssize_t devspec_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct platform_device *ofdev;
395 ofdev = to_platform_device(dev);
396 return sprintf(buf, "%pOF\n", ofdev->dev.of_node);
398 static DEVICE_ATTR_RO(devspec);
400 static ssize_t name_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 struct platform_device *ofdev;
405 ofdev = to_platform_device(dev);
406 return sprintf(buf, "%pOFn\n", ofdev->dev.of_node);
408 static DEVICE_ATTR_RO(name);
410 static ssize_t modalias_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 return of_device_modalias(dev, buf, PAGE_SIZE);
415 static DEVICE_ATTR_RO(modalias);
417 static struct attribute *ibmebus_bus_device_attrs[] = {
418 &dev_attr_devspec.attr,
420 &dev_attr_modalias.attr,
423 ATTRIBUTE_GROUPS(ibmebus_bus_device);
425 struct bus_type ibmebus_bus_type = {
427 .uevent = of_device_uevent_modalias,
428 .bus_groups = ibmbus_bus_groups,
429 .match = ibmebus_bus_bus_match,
430 .probe = ibmebus_bus_device_probe,
431 .remove = ibmebus_bus_device_remove,
432 .shutdown = ibmebus_bus_device_shutdown,
433 .dev_groups = ibmebus_bus_device_groups,
435 EXPORT_SYMBOL(ibmebus_bus_type);
437 static int __init ibmebus_bus_init(void)
441 printk(KERN_INFO "IBM eBus Device Driver\n");
443 err = bus_register(&ibmebus_bus_type);
445 printk(KERN_ERR "%s: failed to register IBM eBus.\n",
450 err = device_register(&ibmebus_bus_device);
452 printk(KERN_WARNING "%s: device_register returned %i\n",
454 put_device(&ibmebus_bus_device);
455 bus_unregister(&ibmebus_bus_type);
460 err = ibmebus_create_devices(ibmebus_matches);
462 device_unregister(&ibmebus_bus_device);
463 bus_unregister(&ibmebus_bus_type);
469 machine_postcore_initcall(pseries, ibmebus_bus_init);