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