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>
44 #include <asm/ibmebus.h>
45 #include <asm/abs_addr.h>
47 static struct device ibmebus_bus_device = { /* fake "parent" device */
51 struct bus_type ibmebus_bus_type;
53 static void *ibmebus_alloc_coherent(struct device *dev,
55 dma_addr_t *dma_handle,
60 mem = kmalloc(size, flag);
61 *dma_handle = (dma_addr_t)mem;
66 static void ibmebus_free_coherent(struct device *dev,
67 size_t size, void *vaddr,
68 dma_addr_t dma_handle)
73 static dma_addr_t ibmebus_map_single(struct device *dev,
76 enum dma_data_direction direction)
78 return (dma_addr_t)(ptr);
81 static void ibmebus_unmap_single(struct device *dev,
84 enum dma_data_direction direction)
89 static int ibmebus_map_sg(struct device *dev,
90 struct scatterlist *sgl,
91 int nents, enum dma_data_direction direction)
93 struct scatterlist *sg;
96 for_each_sg(sgl, sg, nents, i) {
97 sg->dma_address = (dma_addr_t)page_address(sg->page)
99 sg->dma_length = sg->length;
105 static void ibmebus_unmap_sg(struct device *dev,
106 struct scatterlist *sg,
107 int nents, enum dma_data_direction direction)
112 static int ibmebus_dma_supported(struct device *dev, u64 mask)
117 static struct dma_mapping_ops ibmebus_dma_ops = {
118 .alloc_coherent = ibmebus_alloc_coherent,
119 .free_coherent = ibmebus_free_coherent,
120 .map_single = ibmebus_map_single,
121 .unmap_single = ibmebus_unmap_single,
122 .map_sg = ibmebus_map_sg,
123 .unmap_sg = ibmebus_unmap_sg,
124 .dma_supported = ibmebus_dma_supported,
127 static int ibmebus_bus_probe(struct device *dev)
129 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
130 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
131 const struct of_device_id *id;
134 if (!ibmebusdrv->probe)
137 id = of_match_device(ibmebusdrv->id_table, &ibmebusdev->ofdev);
139 error = ibmebusdrv->probe(ibmebusdev, id);
145 static int ibmebus_bus_remove(struct device *dev)
147 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
148 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
150 if (ibmebusdrv->remove) {
151 return ibmebusdrv->remove(ibmebusdev);
157 static void __devinit ibmebus_dev_release(struct device *dev)
159 of_node_put(to_ibmebus_dev(dev)->ofdev.node);
160 kfree(to_ibmebus_dev(dev));
163 static int __devinit ibmebus_register_device_common(
164 struct ibmebus_dev *dev, const char *name)
168 dev->ofdev.dev.parent = &ibmebus_bus_device;
169 dev->ofdev.dev.bus = &ibmebus_bus_type;
170 dev->ofdev.dev.release = ibmebus_dev_release;
172 dev->ofdev.dev.archdata.of_node = dev->ofdev.node;
173 dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops;
174 dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node);
176 /* An ibmebusdev is based on a of_device. We have to change the
177 * bus type to use our own DMA mapping operations.
179 if ((err = of_device_register(&dev->ofdev)) != 0) {
180 printk(KERN_ERR "%s: failed to register device (%d).\n",
188 static struct ibmebus_dev* __devinit ibmebus_register_device_node(
189 struct device_node *dn)
191 struct ibmebus_dev *dev;
194 dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL);
196 return ERR_PTR(-ENOMEM);
198 dev->ofdev.node = of_node_get(dn);
200 len = strlen(dn->full_name + 1);
201 bus_len = min(len, BUS_ID_SIZE - 1);
202 memcpy(dev->ofdev.dev.bus_id, dn->full_name + 1
203 + (len - bus_len), bus_len);
204 for (i = 0; i < bus_len; i++)
205 if (dev->ofdev.dev.bus_id[i] == '/')
206 dev->ofdev.dev.bus_id[i] = '_';
208 /* Register with generic device framework. */
209 if (ibmebus_register_device_common(dev, dn->name) != 0) {
211 return ERR_PTR(-ENODEV);
217 static void ibmebus_probe_of_nodes(char* name)
219 struct device_node *dn = NULL;
221 while ((dn = of_find_node_by_name(dn, name))) {
222 if (IS_ERR(ibmebus_register_device_node(dn))) {
233 static void ibmebus_add_devices_by_id(struct of_device_id *idt)
235 while (strlen(idt->name) > 0) {
236 ibmebus_probe_of_nodes(idt->name);
243 static int ibmebus_match_name(struct device *dev, void *data)
245 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
248 name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
250 if (name && (strcmp(data, name) == 0))
256 static int ibmebus_unregister_device(struct device *dev)
258 of_device_unregister(to_of_device(dev));
263 static void ibmebus_remove_devices_by_id(struct of_device_id *idt)
267 while (strlen(idt->name) > 0) {
268 while ((dev = bus_find_device(&ibmebus_bus_type, NULL,
270 ibmebus_match_name))) {
271 ibmebus_unregister_device(dev);
279 int ibmebus_register_driver(struct ibmebus_driver *drv)
283 drv->driver.name = drv->name;
284 drv->driver.bus = &ibmebus_bus_type;
285 drv->driver.probe = ibmebus_bus_probe;
286 drv->driver.remove = ibmebus_bus_remove;
288 if ((err = driver_register(&drv->driver) != 0))
291 /* remove all supported devices first, in case someone
292 * probed them manually before registering the driver */
293 ibmebus_remove_devices_by_id(drv->id_table);
294 ibmebus_add_devices_by_id(drv->id_table);
298 EXPORT_SYMBOL(ibmebus_register_driver);
300 void ibmebus_unregister_driver(struct ibmebus_driver *drv)
302 driver_unregister(&drv->driver);
303 ibmebus_remove_devices_by_id(drv->id_table);
305 EXPORT_SYMBOL(ibmebus_unregister_driver);
307 int ibmebus_request_irq(struct ibmebus_dev *dev,
309 irq_handler_t handler,
310 unsigned long irq_flags, const char * devname,
313 unsigned int irq = irq_create_mapping(NULL, ist);
318 return request_irq(irq, handler,
319 irq_flags, devname, dev_id);
321 EXPORT_SYMBOL(ibmebus_request_irq);
323 void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id)
325 unsigned int irq = irq_find_mapping(NULL, ist);
327 free_irq(irq, dev_id);
329 EXPORT_SYMBOL(ibmebus_free_irq);
331 static int ibmebus_bus_match(struct device *dev, struct device_driver *drv)
333 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
334 struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv);
335 const struct of_device_id *ids = ebus_drv->id_table;
336 const struct of_device_id *found_id;
341 found_id = of_match_device(ids, &ebus_dev->ofdev);
348 static ssize_t name_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
351 struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
352 const char *name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
353 return sprintf(buf, "%s\n", name);
356 static struct device_attribute ibmebus_dev_attrs[] = {
361 static int ibmebus_match_path(struct device *dev, void *data)
364 struct device_node *dn =
365 of_node_get(to_ibmebus_dev(dev)->ofdev.node);
367 rc = (dn->full_name && (strcasecmp((char*)data, dn->full_name) == 0));
373 static char *ibmebus_chomp(const char *in, size_t count)
375 char *out = kmalloc(count + 1, GFP_KERNEL);
380 memcpy(out, in, count);
382 if (out[count - 1] == '\n')
383 out[count - 1] = '\0';
388 static ssize_t ibmebus_store_probe(struct bus_type *bus,
389 const char *buf, size_t count)
391 struct device_node *dn = NULL;
392 struct ibmebus_dev *dev;
396 path = ibmebus_chomp(buf, count);
400 if (bus_find_device(&ibmebus_bus_type, NULL, path,
401 ibmebus_match_path)) {
402 printk(KERN_WARNING "%s: %s has already been probed\n",
408 if ((dn = of_find_node_by_path(path))) {
409 dev = ibmebus_register_device_node(dn);
411 rc = IS_ERR(dev) ? PTR_ERR(dev) : count;
413 printk(KERN_WARNING "%s: no such device node: %s\n",
423 static ssize_t ibmebus_store_remove(struct bus_type *bus,
424 const char *buf, size_t count)
429 path = ibmebus_chomp(buf, count);
433 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
434 ibmebus_match_path))) {
435 ibmebus_unregister_device(dev);
440 printk(KERN_WARNING "%s: %s not on the bus\n",
448 static struct bus_attribute ibmebus_bus_attrs[] = {
449 __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
450 __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
454 struct bus_type ibmebus_bus_type = {
456 .match = ibmebus_bus_match,
457 .dev_attrs = ibmebus_dev_attrs,
458 .bus_attrs = ibmebus_bus_attrs
460 EXPORT_SYMBOL(ibmebus_bus_type);
462 static int __init ibmebus_bus_init(void)
466 printk(KERN_INFO "IBM eBus Device Driver\n");
468 err = bus_register(&ibmebus_bus_type);
470 printk(KERN_ERR ":%s: failed to register IBM eBus.\n",
475 err = device_register(&ibmebus_bus_device);
477 printk(KERN_WARNING "%s: device_register returned %i\n",
479 bus_unregister(&ibmebus_bus_type);
486 __initcall(ibmebus_bus_init);