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