Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp. |
| 3 | * <benh@kernel.crashing.org> |
Benjamin Herrenschmidt | 4c9d280 | 2006-11-11 17:25:08 +1100 | [diff] [blame^] | 4 | * and Arnd Bergmann, IBM Corp. |
Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #undef DEBUG |
| 14 | |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mod_devicetable.h> |
| 20 | #include <linux/slab.h> |
Benjamin Herrenschmidt | 4c9d280 | 2006-11-11 17:25:08 +1100 | [diff] [blame^] | 21 | #include <linux/pci.h> |
Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 22 | |
| 23 | #include <asm/errno.h> |
| 24 | #include <asm/dcr.h> |
| 25 | #include <asm/of_device.h> |
| 26 | #include <asm/of_platform.h> |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 27 | #include <asm/topology.h> |
Benjamin Herrenschmidt | 4c9d280 | 2006-11-11 17:25:08 +1100 | [diff] [blame^] | 28 | #include <asm/pci-bridge.h> |
| 29 | #include <asm/ppc-pci.h> |
Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * The list of OF IDs below is used for matching bus types in the |
| 33 | * system whose devices are to be exposed as of_platform_devices. |
| 34 | * |
| 35 | * This is the default list valid for most platforms. This file provides |
| 36 | * functions who can take an explicit list if necessary though |
| 37 | * |
| 38 | * The search is always performed recursively looking for children of |
| 39 | * the provided device_node and recursively if such a children matches |
| 40 | * a bus type in the list |
| 41 | */ |
| 42 | |
| 43 | static struct of_device_id of_default_bus_ids[] = { |
| 44 | { .type = "soc", }, |
| 45 | { .compatible = "soc", }, |
| 46 | { .type = "spider", }, |
| 47 | { .type = "axon", }, |
| 48 | { .type = "plb5", }, |
| 49 | { .type = "plb4", }, |
| 50 | { .type = "opb", }, |
| 51 | {}, |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * |
| 56 | * OF platform device type definition & base infrastructure |
| 57 | * |
| 58 | */ |
| 59 | |
| 60 | static int of_platform_bus_match(struct device *dev, struct device_driver *drv) |
| 61 | { |
| 62 | struct of_device * of_dev = to_of_device(dev); |
| 63 | struct of_platform_driver * of_drv = to_of_platform_driver(drv); |
| 64 | const struct of_device_id * matches = of_drv->match_table; |
| 65 | |
| 66 | if (!matches) |
| 67 | return 0; |
| 68 | |
| 69 | return of_match_device(matches, of_dev) != NULL; |
| 70 | } |
| 71 | |
| 72 | static int of_platform_device_probe(struct device *dev) |
| 73 | { |
| 74 | int error = -ENODEV; |
| 75 | struct of_platform_driver *drv; |
| 76 | struct of_device *of_dev; |
| 77 | const struct of_device_id *match; |
| 78 | |
| 79 | drv = to_of_platform_driver(dev->driver); |
| 80 | of_dev = to_of_device(dev); |
| 81 | |
| 82 | if (!drv->probe) |
| 83 | return error; |
| 84 | |
| 85 | of_dev_get(of_dev); |
| 86 | |
| 87 | match = of_match_device(drv->match_table, of_dev); |
| 88 | if (match) |
| 89 | error = drv->probe(of_dev, match); |
| 90 | if (error) |
| 91 | of_dev_put(of_dev); |
| 92 | |
| 93 | return error; |
| 94 | } |
| 95 | |
| 96 | static int of_platform_device_remove(struct device *dev) |
| 97 | { |
| 98 | struct of_device * of_dev = to_of_device(dev); |
| 99 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 100 | |
| 101 | if (dev->driver && drv->remove) |
| 102 | drv->remove(of_dev); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int of_platform_device_suspend(struct device *dev, pm_message_t state) |
| 107 | { |
| 108 | struct of_device * of_dev = to_of_device(dev); |
| 109 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 110 | int error = 0; |
| 111 | |
| 112 | if (dev->driver && drv->suspend) |
| 113 | error = drv->suspend(of_dev, state); |
| 114 | return error; |
| 115 | } |
| 116 | |
| 117 | static int of_platform_device_resume(struct device * dev) |
| 118 | { |
| 119 | struct of_device * of_dev = to_of_device(dev); |
| 120 | struct of_platform_driver * drv = to_of_platform_driver(dev->driver); |
| 121 | int error = 0; |
| 122 | |
| 123 | if (dev->driver && drv->resume) |
| 124 | error = drv->resume(of_dev); |
| 125 | return error; |
| 126 | } |
| 127 | |
| 128 | struct bus_type of_platform_bus_type = { |
| 129 | .name = "of_platform", |
| 130 | .match = of_platform_bus_match, |
| 131 | .probe = of_platform_device_probe, |
| 132 | .remove = of_platform_device_remove, |
| 133 | .suspend = of_platform_device_suspend, |
| 134 | .resume = of_platform_device_resume, |
| 135 | }; |
| 136 | EXPORT_SYMBOL(of_platform_bus_type); |
| 137 | |
| 138 | static int __init of_bus_driver_init(void) |
| 139 | { |
| 140 | return bus_register(&of_platform_bus_type); |
| 141 | } |
| 142 | |
| 143 | postcore_initcall(of_bus_driver_init); |
| 144 | |
| 145 | int of_register_platform_driver(struct of_platform_driver *drv) |
| 146 | { |
| 147 | /* initialize common driver fields */ |
| 148 | drv->driver.name = drv->name; |
| 149 | drv->driver.bus = &of_platform_bus_type; |
| 150 | |
| 151 | /* register with core */ |
| 152 | return driver_register(&drv->driver); |
| 153 | } |
| 154 | EXPORT_SYMBOL(of_register_platform_driver); |
| 155 | |
| 156 | void of_unregister_platform_driver(struct of_platform_driver *drv) |
| 157 | { |
| 158 | driver_unregister(&drv->driver); |
| 159 | } |
| 160 | EXPORT_SYMBOL(of_unregister_platform_driver); |
| 161 | |
| 162 | static void of_platform_make_bus_id(struct of_device *dev) |
| 163 | { |
| 164 | struct device_node *node = dev->node; |
| 165 | char *name = dev->dev.bus_id; |
| 166 | const u32 *reg; |
| 167 | u64 addr; |
| 168 | |
| 169 | /* |
| 170 | * If it's a DCR based device, use 'd' for native DCRs |
| 171 | * and 'D' for MMIO DCRs. |
| 172 | */ |
| 173 | #ifdef CONFIG_PPC_DCR |
| 174 | reg = get_property(node, "dcr-reg", NULL); |
| 175 | if (reg) { |
| 176 | #ifdef CONFIG_PPC_DCR_NATIVE |
| 177 | snprintf(name, BUS_ID_SIZE, "d%x.%s", |
| 178 | *reg, node->name); |
| 179 | #else /* CONFIG_PPC_DCR_NATIVE */ |
| 180 | addr = of_translate_dcr_address(node, *reg, NULL); |
| 181 | if (addr != OF_BAD_ADDR) { |
| 182 | snprintf(name, BUS_ID_SIZE, |
| 183 | "D%llx.%s", (unsigned long long)addr, |
| 184 | node->name); |
| 185 | return; |
| 186 | } |
| 187 | #endif /* !CONFIG_PPC_DCR_NATIVE */ |
| 188 | } |
| 189 | #endif /* CONFIG_PPC_DCR */ |
| 190 | |
| 191 | /* |
| 192 | * For MMIO, get the physical address |
| 193 | */ |
| 194 | reg = get_property(node, "reg", NULL); |
| 195 | if (reg) { |
| 196 | addr = of_translate_address(node, reg); |
| 197 | if (addr != OF_BAD_ADDR) { |
| 198 | snprintf(name, BUS_ID_SIZE, |
| 199 | "%llx.%s", (unsigned long long)addr, |
| 200 | node->name); |
| 201 | return; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * No BusID, use the node name and pray |
| 207 | */ |
| 208 | snprintf(name, BUS_ID_SIZE, "%s", node->name); |
| 209 | } |
| 210 | |
| 211 | struct of_device* of_platform_device_create(struct device_node *np, |
| 212 | const char *bus_id, |
| 213 | struct device *parent) |
| 214 | { |
| 215 | struct of_device *dev; |
| 216 | |
| 217 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); |
| 218 | if (!dev) |
| 219 | return NULL; |
| 220 | memset(dev, 0, sizeof(*dev)); |
| 221 | |
| 222 | dev->node = of_node_get(np); |
| 223 | dev->dma_mask = 0xffffffffUL; |
| 224 | dev->dev.dma_mask = &dev->dma_mask; |
| 225 | dev->dev.parent = parent; |
| 226 | dev->dev.bus = &of_platform_bus_type; |
| 227 | dev->dev.release = of_release_dev; |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 228 | dev->dev.archdata.of_node = np; |
| 229 | dev->dev.archdata.numa_node = of_node_to_nid(np); |
| 230 | |
| 231 | /* We do not fill the DMA ops for platform devices by default. |
| 232 | * This is currently the responsibility of the platform code |
| 233 | * to do such, possibly using a device notifier |
| 234 | */ |
Benjamin Herrenschmidt | 7eebde7 | 2006-11-11 17:24:59 +1100 | [diff] [blame] | 235 | |
| 236 | if (bus_id) |
| 237 | strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE); |
| 238 | else |
| 239 | of_platform_make_bus_id(dev); |
| 240 | |
| 241 | if (of_device_register(dev) != 0) { |
| 242 | kfree(dev); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | return dev; |
| 247 | } |
| 248 | EXPORT_SYMBOL(of_platform_device_create); |
| 249 | |
| 250 | |
| 251 | |
| 252 | /** |
| 253 | * of_platform_bus_create - Create an OF device for a bus node and all its |
| 254 | * children. Optionally recursively instanciate matching busses. |
| 255 | * @bus: device node of the bus to instanciate |
| 256 | * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to |
| 257 | * disallow recursive creation of child busses |
| 258 | */ |
| 259 | static int of_platform_bus_create(struct device_node *bus, |
| 260 | struct of_device_id *matches, |
| 261 | struct device *parent) |
| 262 | { |
| 263 | struct device_node *child; |
| 264 | struct of_device *dev; |
| 265 | int rc = 0; |
| 266 | |
| 267 | for (child = NULL; (child = of_get_next_child(bus, child)); ) { |
| 268 | pr_debug(" create child: %s\n", child->full_name); |
| 269 | dev = of_platform_device_create(child, NULL, parent); |
| 270 | if (dev == NULL) |
| 271 | rc = -ENOMEM; |
| 272 | else if (!of_match_node(matches, child)) |
| 273 | continue; |
| 274 | if (rc == 0) { |
| 275 | pr_debug(" and sub busses\n"); |
| 276 | rc = of_platform_bus_create(child, matches, &dev->dev); |
| 277 | } if (rc) { |
| 278 | of_node_put(child); |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | return rc; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * of_platform_bus_probe - Probe the device-tree for platform busses |
| 287 | * @root: parent of the first level to probe or NULL for the root of the tree |
| 288 | * @matches: match table, NULL to use the default |
| 289 | * @parent: parent to hook devices from, NULL for toplevel |
| 290 | * |
| 291 | * Note that children of the provided root are not instanciated as devices |
| 292 | * unless the specified root itself matches the bus list and is not NULL. |
| 293 | */ |
| 294 | |
| 295 | int of_platform_bus_probe(struct device_node *root, |
| 296 | struct of_device_id *matches, |
| 297 | struct device *parent) |
| 298 | { |
| 299 | struct device_node *child; |
| 300 | struct of_device *dev; |
| 301 | int rc = 0; |
| 302 | |
| 303 | if (matches == NULL) |
| 304 | matches = of_default_bus_ids; |
| 305 | if (matches == OF_NO_DEEP_PROBE) |
| 306 | return -EINVAL; |
| 307 | if (root == NULL) |
| 308 | root = of_find_node_by_path("/"); |
| 309 | else |
| 310 | of_node_get(root); |
| 311 | |
| 312 | pr_debug("of_platform_bus_probe()\n"); |
| 313 | pr_debug(" starting at: %s\n", root->full_name); |
| 314 | |
| 315 | /* Do a self check of bus type, if there's a match, create |
| 316 | * children |
| 317 | */ |
| 318 | if (of_match_node(matches, root)) { |
| 319 | pr_debug(" root match, create all sub devices\n"); |
| 320 | dev = of_platform_device_create(root, NULL, parent); |
| 321 | if (dev == NULL) { |
| 322 | rc = -ENOMEM; |
| 323 | goto bail; |
| 324 | } |
| 325 | pr_debug(" create all sub busses\n"); |
| 326 | rc = of_platform_bus_create(root, matches, &dev->dev); |
| 327 | goto bail; |
| 328 | } |
| 329 | for (child = NULL; (child = of_get_next_child(root, child)); ) { |
| 330 | if (!of_match_node(matches, child)) |
| 331 | continue; |
| 332 | |
| 333 | pr_debug(" match: %s\n", child->full_name); |
| 334 | dev = of_platform_device_create(child, NULL, parent); |
| 335 | if (dev == NULL) |
| 336 | rc = -ENOMEM; |
| 337 | else |
| 338 | rc = of_platform_bus_create(child, matches, &dev->dev); |
| 339 | if (rc) { |
| 340 | of_node_put(child); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | bail: |
| 345 | of_node_put(root); |
| 346 | return rc; |
| 347 | } |
| 348 | EXPORT_SYMBOL(of_platform_bus_probe); |
| 349 | |
| 350 | static int of_dev_node_match(struct device *dev, void *data) |
| 351 | { |
| 352 | return to_of_device(dev)->node == data; |
| 353 | } |
| 354 | |
| 355 | struct of_device *of_find_device_by_node(struct device_node *np) |
| 356 | { |
| 357 | struct device *dev; |
| 358 | |
| 359 | dev = bus_find_device(&of_platform_bus_type, |
| 360 | NULL, np, of_dev_node_match); |
| 361 | if (dev) |
| 362 | return to_of_device(dev); |
| 363 | return NULL; |
| 364 | } |
| 365 | EXPORT_SYMBOL(of_find_device_by_node); |
| 366 | |
| 367 | static int of_dev_phandle_match(struct device *dev, void *data) |
| 368 | { |
| 369 | phandle *ph = data; |
| 370 | return to_of_device(dev)->node->linux_phandle == *ph; |
| 371 | } |
| 372 | |
| 373 | struct of_device *of_find_device_by_phandle(phandle ph) |
| 374 | { |
| 375 | struct device *dev; |
| 376 | |
| 377 | dev = bus_find_device(&of_platform_bus_type, |
| 378 | NULL, &ph, of_dev_phandle_match); |
| 379 | if (dev) |
| 380 | return to_of_device(dev); |
| 381 | return NULL; |
| 382 | } |
| 383 | EXPORT_SYMBOL(of_find_device_by_phandle); |
Benjamin Herrenschmidt | 4c9d280 | 2006-11-11 17:25:08 +1100 | [diff] [blame^] | 384 | |
| 385 | |
| 386 | #ifdef CONFIG_PPC_OF_PLATFORM_PCI |
| 387 | |
| 388 | /* The probing of PCI controllers from of_platform is currently |
| 389 | * 64 bits only, mostly due to gratuitous differences between |
| 390 | * the 32 and 64 bits PCI code on PowerPC and the 32 bits one |
| 391 | * lacking some bits needed here. |
| 392 | */ |
| 393 | |
| 394 | static int __devinit of_pci_phb_probe(struct of_device *dev, |
| 395 | const struct of_device_id *match) |
| 396 | { |
| 397 | struct pci_controller *phb; |
| 398 | |
| 399 | /* Check if we can do that ... */ |
| 400 | if (ppc_md.pci_setup_phb == NULL) |
| 401 | return -ENODEV; |
| 402 | |
| 403 | printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name); |
| 404 | |
| 405 | /* Alloc and setup PHB data structure */ |
| 406 | phb = pcibios_alloc_controller(dev->node); |
| 407 | if (!phb) |
| 408 | return -ENODEV; |
| 409 | |
| 410 | /* Setup parent in sysfs */ |
| 411 | phb->parent = &dev->dev; |
| 412 | |
| 413 | /* Setup the PHB using arch provided callback */ |
| 414 | if (ppc_md.pci_setup_phb(phb)) { |
| 415 | pcibios_free_controller(phb); |
| 416 | return -ENODEV; |
| 417 | } |
| 418 | |
| 419 | /* Process "ranges" property */ |
| 420 | pci_process_bridge_OF_ranges(phb, dev->node, 0); |
| 421 | |
| 422 | /* Setup IO space. |
| 423 | * This will not work properly for ISA IOs, something needs to be done |
| 424 | * about it if we ever generalize that way of probing PCI brigdes |
| 425 | */ |
| 426 | pci_setup_phb_io_dynamic(phb, 0); |
| 427 | |
| 428 | /* Init pci_dn data structures */ |
| 429 | pci_devs_phb_init_dynamic(phb); |
| 430 | |
| 431 | /* Register devices with EEH */ |
| 432 | #ifdef CONFIG_EEH |
| 433 | if (dev->node->child) |
| 434 | eeh_add_device_tree_early(dev->node); |
| 435 | #endif /* CONFIG_EEH */ |
| 436 | |
| 437 | /* Scan the bus */ |
| 438 | scan_phb(phb); |
| 439 | |
| 440 | /* Claim resources. This might need some rework as well depending |
| 441 | * wether we are doing probe-only or not, like assigning unassigned |
| 442 | * resources etc... |
| 443 | */ |
| 444 | pcibios_claim_one_bus(phb->bus); |
| 445 | |
| 446 | /* Finish EEH setup */ |
| 447 | #ifdef CONFIG_EEH |
| 448 | eeh_add_device_tree_late(phb->bus); |
| 449 | #endif |
| 450 | |
| 451 | /* Add probed PCI devices to the device model */ |
| 452 | pci_bus_add_devices(phb->bus); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static struct of_device_id of_pci_phb_ids[] = { |
| 458 | { .type = "pci", }, |
| 459 | { .type = "pcix", }, |
| 460 | { .type = "pcie", }, |
| 461 | { .type = "pciex", }, |
| 462 | { .type = "ht", }, |
| 463 | {} |
| 464 | }; |
| 465 | |
| 466 | static struct of_platform_driver of_pci_phb_driver = { |
| 467 | .name = "of-pci", |
| 468 | .match_table = of_pci_phb_ids, |
| 469 | .probe = of_pci_phb_probe, |
| 470 | .driver = { |
| 471 | .multithread_probe = 1, |
| 472 | }, |
| 473 | }; |
| 474 | |
| 475 | static __init int of_pci_phb_init(void) |
| 476 | { |
| 477 | return of_register_platform_driver(&of_pci_phb_driver); |
| 478 | } |
| 479 | |
| 480 | device_initcall(of_pci_phb_init); |
| 481 | |
| 482 | #endif /* CONFIG_PPC_OF_PLATFORM_PCI */ |