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 #define MAX_LOC_CODE_LENGTH 80
49 static struct device ibmebus_bus_device = { /* fake "parent" device */
53 struct bus_type ibmebus_bus_type;
55 static void *ibmebus_alloc_coherent(struct device *dev,
57 dma_addr_t *dma_handle,
62 mem = kmalloc(size, flag);
63 *dma_handle = (dma_addr_t)mem;
68 static void ibmebus_free_coherent(struct device *dev,
69 size_t size, void *vaddr,
70 dma_addr_t dma_handle)
75 static dma_addr_t ibmebus_map_single(struct device *dev,
78 enum dma_data_direction direction)
80 return (dma_addr_t)(ptr);
83 static void ibmebus_unmap_single(struct device *dev,
86 enum dma_data_direction direction)
91 static int ibmebus_map_sg(struct device *dev,
92 struct scatterlist *sg,
93 int nents, enum dma_data_direction direction)
97 for (i = 0; i < nents; i++) {
98 sg[i].dma_address = (dma_addr_t)page_address(sg[i].page)
100 sg[i].dma_length = sg[i].length;
106 static void ibmebus_unmap_sg(struct device *dev,
107 struct scatterlist *sg,
108 int nents, enum dma_data_direction direction)
113 static int ibmebus_dma_supported(struct device *dev, u64 mask)
118 static struct dma_mapping_ops ibmebus_dma_ops = {
119 .alloc_coherent = ibmebus_alloc_coherent,
120 .free_coherent = ibmebus_free_coherent,
121 .map_single = ibmebus_map_single,
122 .unmap_single = ibmebus_unmap_single,
123 .map_sg = ibmebus_map_sg,
124 .unmap_sg = ibmebus_unmap_sg,
125 .dma_supported = ibmebus_dma_supported,
128 static int ibmebus_bus_probe(struct device *dev)
130 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
131 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
132 const struct of_device_id *id;
135 if (!ibmebusdrv->probe)
138 id = of_match_device(ibmebusdrv->id_table, &ibmebusdev->ofdev);
140 error = ibmebusdrv->probe(ibmebusdev, id);
146 static int ibmebus_bus_remove(struct device *dev)
148 struct ibmebus_dev *ibmebusdev = to_ibmebus_dev(dev);
149 struct ibmebus_driver *ibmebusdrv = to_ibmebus_driver(dev->driver);
151 if (ibmebusdrv->remove) {
152 return ibmebusdrv->remove(ibmebusdev);
158 static void __devinit ibmebus_dev_release(struct device *dev)
160 of_node_put(to_ibmebus_dev(dev)->ofdev.node);
161 kfree(to_ibmebus_dev(dev));
164 static int __devinit ibmebus_register_device_common(
165 struct ibmebus_dev *dev, const char *name)
169 dev->ofdev.dev.parent = &ibmebus_bus_device;
170 dev->ofdev.dev.bus = &ibmebus_bus_type;
171 dev->ofdev.dev.release = ibmebus_dev_release;
173 dev->ofdev.dev.archdata.of_node = dev->ofdev.node;
174 dev->ofdev.dev.archdata.dma_ops = &ibmebus_dma_ops;
175 dev->ofdev.dev.archdata.numa_node = of_node_to_nid(dev->ofdev.node);
177 /* An ibmebusdev is based on a of_device. We have to change the
178 * bus type to use our own DMA mapping operations.
180 if ((err = of_device_register(&dev->ofdev)) != 0) {
181 printk(KERN_ERR "%s: failed to register device (%d).\n",
189 static struct ibmebus_dev* __devinit ibmebus_register_device_node(
190 struct device_node *dn)
192 struct ibmebus_dev *dev;
193 const char *loc_code;
196 loc_code = of_get_property(dn, "ibm,loc-code", NULL);
198 printk(KERN_WARNING "%s: node %s missing 'ibm,loc-code'\n",
199 __FUNCTION__, dn->name ? dn->name : "<unknown>");
200 return ERR_PTR(-EINVAL);
203 if (strlen(loc_code) == 0) {
204 printk(KERN_WARNING "%s: 'ibm,loc-code' is invalid\n",
206 return ERR_PTR(-EINVAL);
209 dev = kzalloc(sizeof(struct ibmebus_dev), GFP_KERNEL);
211 return ERR_PTR(-ENOMEM);
214 dev->ofdev.node = of_node_get(dn);
216 length = strlen(loc_code);
217 memcpy(dev->ofdev.dev.bus_id, loc_code
218 + (length - min(length, BUS_ID_SIZE - 1)),
219 min(length, BUS_ID_SIZE - 1));
221 /* Register with generic device framework. */
222 if (ibmebus_register_device_common(dev, dn->name) != 0) {
224 return ERR_PTR(-ENODEV);
230 static void ibmebus_probe_of_nodes(char* name)
232 struct device_node *dn = NULL;
234 while ((dn = of_find_node_by_name(dn, name))) {
235 if (IS_ERR(ibmebus_register_device_node(dn))) {
246 static void ibmebus_add_devices_by_id(struct of_device_id *idt)
248 while (strlen(idt->name) > 0) {
249 ibmebus_probe_of_nodes(idt->name);
256 static int ibmebus_match_helper_name(struct device *dev, void *data)
258 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
261 name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
263 if (name && (strcmp(data, name) == 0))
269 static int ibmebus_unregister_device(struct device *dev)
271 of_device_unregister(to_of_device(dev));
276 static void ibmebus_remove_devices_by_id(struct of_device_id *idt)
280 while (strlen(idt->name) > 0) {
281 while ((dev = bus_find_device(&ibmebus_bus_type, NULL,
283 ibmebus_match_helper_name))) {
284 ibmebus_unregister_device(dev);
292 int ibmebus_register_driver(struct ibmebus_driver *drv)
296 drv->driver.name = drv->name;
297 drv->driver.bus = &ibmebus_bus_type;
298 drv->driver.probe = ibmebus_bus_probe;
299 drv->driver.remove = ibmebus_bus_remove;
301 if ((err = driver_register(&drv->driver) != 0))
304 /* remove all supported devices first, in case someone
305 * probed them manually before registering the driver */
306 ibmebus_remove_devices_by_id(drv->id_table);
307 ibmebus_add_devices_by_id(drv->id_table);
311 EXPORT_SYMBOL(ibmebus_register_driver);
313 void ibmebus_unregister_driver(struct ibmebus_driver *drv)
315 driver_unregister(&drv->driver);
316 ibmebus_remove_devices_by_id(drv->id_table);
318 EXPORT_SYMBOL(ibmebus_unregister_driver);
320 int ibmebus_request_irq(struct ibmebus_dev *dev,
322 irq_handler_t handler,
323 unsigned long irq_flags, const char * devname,
326 unsigned int irq = irq_create_mapping(NULL, ist);
331 return request_irq(irq, handler,
332 irq_flags, devname, dev_id);
334 EXPORT_SYMBOL(ibmebus_request_irq);
336 void ibmebus_free_irq(struct ibmebus_dev *dev, u32 ist, void *dev_id)
338 unsigned int irq = irq_find_mapping(NULL, ist);
340 free_irq(irq, dev_id);
342 EXPORT_SYMBOL(ibmebus_free_irq);
344 static int ibmebus_bus_match(struct device *dev, struct device_driver *drv)
346 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
347 struct ibmebus_driver *ebus_drv = to_ibmebus_driver(drv);
348 const struct of_device_id *ids = ebus_drv->id_table;
349 const struct of_device_id *found_id;
354 found_id = of_match_device(ids, &ebus_dev->ofdev);
361 static ssize_t name_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
364 struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
365 const char *name = of_get_property(ebus_dev->ofdev.node, "name", NULL);
366 return sprintf(buf, "%s\n", name);
369 static struct device_attribute ibmebus_dev_attrs[] = {
374 static int ibmebus_match_helper_loc_code(struct device *dev, void *data)
376 const struct ibmebus_dev *ebus_dev = to_ibmebus_dev(dev);
377 const char *loc_code;
379 loc_code = of_get_property(ebus_dev->ofdev.node, "ibm,loc-code", NULL);
381 if (loc_code && (strcmp(data, loc_code) == 0))
387 static ssize_t ibmebus_store_probe(struct bus_type *bus,
388 const char *buf, size_t count)
390 struct device_node *dn = NULL;
391 struct ibmebus_dev *dev;
392 const char *loc_code;
393 char parm[MAX_LOC_CODE_LENGTH];
395 if (count >= MAX_LOC_CODE_LENGTH)
397 memcpy(parm, buf, count);
399 if (parm[count-1] == '\n')
400 parm[count-1] = '\0';
402 if (bus_find_device(&ibmebus_bus_type, NULL, parm,
403 ibmebus_match_helper_loc_code)) {
404 printk(KERN_WARNING "%s: loc_code %s has already been probed\n",
409 while ((dn = of_find_all_nodes(dn))) {
410 loc_code = of_get_property(dn, "ibm,loc-code", NULL);
411 if (loc_code && (strncmp(loc_code, parm, count) == 0)) {
412 dev = ibmebus_register_device_node(dn);
417 return count; /* success */
421 /* if we drop out of the loop, the loc code was invalid */
422 printk(KERN_WARNING "%s: no device with loc_code %s found\n",
427 static ssize_t ibmebus_store_remove(struct bus_type *bus,
428 const char *buf, size_t count)
431 char parm[MAX_LOC_CODE_LENGTH];
433 if (count >= MAX_LOC_CODE_LENGTH)
435 memcpy(parm, buf, count);
437 if (parm[count-1] == '\n')
438 parm[count-1] = '\0';
440 /* The location code is unique, so we will find one device at most */
441 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, parm,
442 ibmebus_match_helper_loc_code))) {
443 ibmebus_unregister_device(dev);
445 printk(KERN_WARNING "%s: loc_code %s not on the bus\n",
453 static struct bus_attribute ibmebus_bus_attrs[] = {
454 __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
455 __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
459 struct bus_type ibmebus_bus_type = {
461 .match = ibmebus_bus_match,
462 .dev_attrs = ibmebus_dev_attrs,
463 .bus_attrs = ibmebus_bus_attrs
465 EXPORT_SYMBOL(ibmebus_bus_type);
467 static int __init ibmebus_bus_init(void)
471 printk(KERN_INFO "IBM eBus Device Driver\n");
473 err = bus_register(&ibmebus_bus_type);
475 printk(KERN_ERR ":%s: failed to register IBM eBus.\n",
480 err = device_register(&ibmebus_bus_device);
482 printk(KERN_WARNING "%s: device_register returned %i\n",
484 bus_unregister(&ibmebus_bus_type);
491 __initcall(ibmebus_bus_init);