1 /* 2 * IBM PowerPC IBM eBus Infrastructure Support. 3 * 4 * Copyright (c) 2005 IBM Corporation 5 * Joachim Fenkes <fenkes@de.ibm.com> 6 * Heiko J Schick <schickhj@de.ibm.com> 7 * 8 * All rights reserved. 9 * 10 * This source code is distributed under a dual license of GPL v2.0 and OpenIB 11 * BSD. 12 * 13 * OpenIB BSD License 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions are met: 17 * 18 * Redistributions of source code must retain the above copyright notice, this 19 * list of conditions and the following disclaimer. 20 * 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. 25 * 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. 37 */ 38 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> 45 #include <linux/irqdomain.h> 46 #include <linux/of.h> 47 #include <linux/slab.h> 48 #include <linux/stat.h> 49 #include <linux/sysfs.h> 50 #include <linux/of_platform.h> 51 #include <linux/platform_device.h> 52 #include <asm/ibmebus.h> 53 #include <asm/machdep.h> 54 55 static struct device ibmebus_bus_device = { /* fake "parent" device */ 56 .init_name = "ibmebus", 57 }; 58 59 const struct bus_type ibmebus_bus_type; 60 61 /* These devices will automatically be added to the bus during init */ 62 static const struct of_device_id ibmebus_matches[] __initconst = { 63 { .compatible = "IBM,lhca" }, 64 { .compatible = "IBM,lhea" }, 65 {}, 66 }; 67 68 static void *ibmebus_alloc_coherent(struct device *dev, 69 size_t size, 70 dma_addr_t *dma_handle, 71 gfp_t flag, 72 unsigned long attrs) 73 { 74 void *mem; 75 76 mem = kmalloc(size, flag); 77 *dma_handle = (dma_addr_t)mem; 78 79 return mem; 80 } 81 82 static void ibmebus_free_coherent(struct device *dev, 83 size_t size, void *vaddr, 84 dma_addr_t dma_handle, 85 unsigned long attrs) 86 { 87 kfree(vaddr); 88 } 89 90 static dma_addr_t ibmebus_map_phys(struct device *dev, phys_addr_t phys, 91 size_t size, 92 enum dma_data_direction direction, 93 unsigned long attrs) 94 { 95 if (attrs & DMA_ATTR_MMIO) 96 return DMA_MAPPING_ERROR; 97 98 return (dma_addr_t)(phys_to_virt(phys)); 99 } 100 101 static void ibmebus_unmap_phys(struct device *dev, 102 dma_addr_t dma_addr, 103 size_t size, 104 enum dma_data_direction direction, 105 unsigned long attrs) 106 { 107 return; 108 } 109 110 static int ibmebus_map_sg(struct device *dev, 111 struct scatterlist *sgl, 112 int nents, enum dma_data_direction direction, 113 unsigned long attrs) 114 { 115 struct scatterlist *sg; 116 int i; 117 118 for_each_sg(sgl, sg, nents, i) { 119 sg->dma_address = (dma_addr_t) sg_virt(sg); 120 sg->dma_length = sg->length; 121 } 122 123 return nents; 124 } 125 126 static void ibmebus_unmap_sg(struct device *dev, 127 struct scatterlist *sg, 128 int nents, enum dma_data_direction direction, 129 unsigned long attrs) 130 { 131 return; 132 } 133 134 static int ibmebus_dma_supported(struct device *dev, u64 mask) 135 { 136 return mask == DMA_BIT_MASK(64); 137 } 138 139 static u64 ibmebus_dma_get_required_mask(struct device *dev) 140 { 141 return DMA_BIT_MASK(64); 142 } 143 144 static const struct dma_map_ops ibmebus_dma_ops = { 145 .alloc = ibmebus_alloc_coherent, 146 .free = ibmebus_free_coherent, 147 .map_sg = ibmebus_map_sg, 148 .unmap_sg = ibmebus_unmap_sg, 149 .dma_supported = ibmebus_dma_supported, 150 .get_required_mask = ibmebus_dma_get_required_mask, 151 .map_phys = ibmebus_map_phys, 152 .unmap_phys = ibmebus_unmap_phys, 153 }; 154 155 static int ibmebus_match_path(struct device *dev, const void *data) 156 { 157 struct device_node *dn = to_platform_device(dev)->dev.of_node; 158 struct device_node *tn = of_find_node_by_path(data); 159 160 of_node_put(tn); 161 162 return (tn == dn); 163 } 164 165 static int ibmebus_match_node(struct device *dev, const void *data) 166 { 167 return to_platform_device(dev)->dev.of_node == data; 168 } 169 170 static int ibmebus_create_device(struct device_node *dn) 171 { 172 struct platform_device *dev; 173 int ret; 174 175 dev = of_device_alloc(dn, NULL, &ibmebus_bus_device); 176 if (!dev) 177 return -ENOMEM; 178 179 dev->dev.bus = &ibmebus_bus_type; 180 dev->dev.dma_ops = &ibmebus_dma_ops; 181 182 ret = of_device_add(dev); 183 if (ret) 184 platform_device_put(dev); 185 return ret; 186 } 187 188 static int ibmebus_create_devices(const struct of_device_id *matches) 189 { 190 struct device_node *root, *child; 191 struct device *dev; 192 int ret = 0; 193 194 root = of_find_node_by_path("/"); 195 196 for_each_child_of_node(root, child) { 197 if (!of_match_node(matches, child)) 198 continue; 199 200 dev = bus_find_device(&ibmebus_bus_type, NULL, child, 201 ibmebus_match_node); 202 if (dev) { 203 put_device(dev); 204 continue; 205 } 206 207 ret = ibmebus_create_device(child); 208 if (ret) { 209 printk(KERN_ERR "%s: failed to create device (%i)", 210 __func__, ret); 211 of_node_put(child); 212 break; 213 } 214 } 215 216 of_node_put(root); 217 return ret; 218 } 219 220 int ibmebus_register_driver(struct platform_driver *drv) 221 { 222 /* If the driver uses devices that ibmebus doesn't know, add them */ 223 ibmebus_create_devices(drv->driver.of_match_table); 224 225 drv->driver.bus = &ibmebus_bus_type; 226 return driver_register(&drv->driver); 227 } 228 EXPORT_SYMBOL(ibmebus_register_driver); 229 230 void ibmebus_unregister_driver(struct platform_driver *drv) 231 { 232 driver_unregister(&drv->driver); 233 } 234 EXPORT_SYMBOL(ibmebus_unregister_driver); 235 236 int ibmebus_request_irq(u32 ist, irq_handler_t handler, 237 unsigned long irq_flags, const char *devname, 238 void *dev_id) 239 { 240 unsigned int irq = irq_create_mapping(NULL, ist); 241 242 if (!irq) 243 return -EINVAL; 244 245 return request_irq(irq, handler, irq_flags, devname, dev_id); 246 } 247 EXPORT_SYMBOL(ibmebus_request_irq); 248 249 void ibmebus_free_irq(u32 ist, void *dev_id) 250 { 251 unsigned int irq = irq_find_mapping(NULL, ist); 252 253 free_irq(irq, dev_id); 254 irq_dispose_mapping(irq); 255 } 256 EXPORT_SYMBOL(ibmebus_free_irq); 257 258 static char *ibmebus_chomp(const char *in, size_t count) 259 { 260 char *out = kmalloc(count + 1, GFP_KERNEL); 261 262 if (!out) 263 return NULL; 264 265 memcpy(out, in, count); 266 out[count] = '\0'; 267 if (out[count - 1] == '\n') 268 out[count - 1] = '\0'; 269 270 return out; 271 } 272 273 static ssize_t probe_store(const struct bus_type *bus, const char *buf, size_t count) 274 { 275 struct device_node *dn = NULL; 276 struct device *dev; 277 char *path; 278 ssize_t rc = 0; 279 280 path = ibmebus_chomp(buf, count); 281 if (!path) 282 return -ENOMEM; 283 284 dev = bus_find_device(&ibmebus_bus_type, NULL, path, 285 ibmebus_match_path); 286 if (dev) { 287 put_device(dev); 288 printk(KERN_WARNING "%s: %s has already been probed\n", 289 __func__, path); 290 rc = -EEXIST; 291 goto out; 292 } 293 294 if ((dn = of_find_node_by_path(path))) { 295 rc = ibmebus_create_device(dn); 296 of_node_put(dn); 297 } else { 298 printk(KERN_WARNING "%s: no such device node: %s\n", 299 __func__, path); 300 rc = -ENODEV; 301 } 302 303 out: 304 kfree(path); 305 if (rc) 306 return rc; 307 return count; 308 } 309 static BUS_ATTR_WO(probe); 310 311 static ssize_t remove_store(const struct bus_type *bus, const char *buf, size_t count) 312 { 313 struct device *dev; 314 char *path; 315 316 path = ibmebus_chomp(buf, count); 317 if (!path) 318 return -ENOMEM; 319 320 if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path, 321 ibmebus_match_path))) { 322 of_device_unregister(to_platform_device(dev)); 323 put_device(dev); 324 325 kfree(path); 326 return count; 327 } else { 328 printk(KERN_WARNING "%s: %s not on the bus\n", 329 __func__, path); 330 331 kfree(path); 332 return -ENODEV; 333 } 334 } 335 static BUS_ATTR_WO(remove); 336 337 static struct attribute *ibmbus_bus_attrs[] = { 338 &bus_attr_probe.attr, 339 &bus_attr_remove.attr, 340 NULL, 341 }; 342 ATTRIBUTE_GROUPS(ibmbus_bus); 343 344 static int ibmebus_bus_bus_match(struct device *dev, const struct device_driver *drv) 345 { 346 const struct of_device_id *matches = drv->of_match_table; 347 348 if (!matches) 349 return 0; 350 351 return of_match_device(matches, dev) != NULL; 352 } 353 354 static int ibmebus_bus_device_probe(struct device *dev) 355 { 356 int error = -ENODEV; 357 struct platform_driver *drv; 358 struct platform_device *of_dev; 359 360 drv = to_platform_driver(dev->driver); 361 of_dev = to_platform_device(dev); 362 363 if (!drv->probe) 364 return error; 365 366 get_device(dev); 367 368 if (of_driver_match_device(dev, dev->driver)) 369 error = drv->probe(of_dev); 370 if (error) 371 put_device(dev); 372 373 return error; 374 } 375 376 static void ibmebus_bus_device_remove(struct device *dev) 377 { 378 struct platform_device *of_dev = to_platform_device(dev); 379 struct platform_driver *drv = to_platform_driver(dev->driver); 380 381 if (dev->driver && drv->remove) 382 drv->remove(of_dev); 383 } 384 385 static void ibmebus_bus_device_shutdown(struct device *dev) 386 { 387 struct platform_device *of_dev = to_platform_device(dev); 388 struct platform_driver *drv = to_platform_driver(dev->driver); 389 390 if (dev->driver && drv->shutdown) 391 drv->shutdown(of_dev); 392 } 393 394 /* 395 * ibmebus_bus_device_attrs 396 */ 397 static ssize_t devspec_show(struct device *dev, 398 struct device_attribute *attr, char *buf) 399 { 400 struct platform_device *ofdev; 401 402 ofdev = to_platform_device(dev); 403 return sysfs_emit(buf, "%pOF\n", ofdev->dev.of_node); 404 } 405 static DEVICE_ATTR_RO(devspec); 406 407 static ssize_t name_show(struct device *dev, 408 struct device_attribute *attr, char *buf) 409 { 410 struct platform_device *ofdev; 411 412 ofdev = to_platform_device(dev); 413 return sysfs_emit(buf, "%pOFn\n", ofdev->dev.of_node); 414 } 415 static DEVICE_ATTR_RO(name); 416 417 static ssize_t modalias_show(struct device *dev, 418 struct device_attribute *attr, char *buf) 419 { 420 return of_device_modalias(dev, buf, PAGE_SIZE); 421 } 422 static DEVICE_ATTR_RO(modalias); 423 424 static struct attribute *ibmebus_bus_device_attrs[] = { 425 &dev_attr_devspec.attr, 426 &dev_attr_name.attr, 427 &dev_attr_modalias.attr, 428 NULL, 429 }; 430 ATTRIBUTE_GROUPS(ibmebus_bus_device); 431 432 static int ibmebus_bus_modalias(const struct device *dev, struct kobj_uevent_env *env) 433 { 434 return of_device_uevent_modalias(dev, env); 435 } 436 437 const struct bus_type ibmebus_bus_type = { 438 .name = "ibmebus", 439 .uevent = ibmebus_bus_modalias, 440 .bus_groups = ibmbus_bus_groups, 441 .match = ibmebus_bus_bus_match, 442 .probe = ibmebus_bus_device_probe, 443 .remove = ibmebus_bus_device_remove, 444 .shutdown = ibmebus_bus_device_shutdown, 445 .dev_groups = ibmebus_bus_device_groups, 446 }; 447 EXPORT_SYMBOL(ibmebus_bus_type); 448 449 static int __init ibmebus_bus_init(void) 450 { 451 int err; 452 453 printk(KERN_INFO "IBM eBus Device Driver\n"); 454 455 err = bus_register(&ibmebus_bus_type); 456 if (err) { 457 printk(KERN_ERR "%s: failed to register IBM eBus.\n", 458 __func__); 459 return err; 460 } 461 462 err = device_register(&ibmebus_bus_device); 463 if (err) { 464 printk(KERN_WARNING "%s: device_register returned %i\n", 465 __func__, err); 466 put_device(&ibmebus_bus_device); 467 bus_unregister(&ibmebus_bus_type); 468 469 return err; 470 } 471 472 err = ibmebus_create_devices(ibmebus_matches); 473 if (err) { 474 device_unregister(&ibmebus_bus_device); 475 bus_unregister(&ibmebus_bus_type); 476 return err; 477 } 478 479 return 0; 480 } 481 machine_postcore_initcall(pseries, ibmebus_bus_init); 482