2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
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.
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>
21 #include <linux/pci.h>
23 #include <asm/errno.h>
25 #include <asm/of_device.h>
26 #include <asm/of_platform.h>
27 #include <asm/topology.h>
28 #include <asm/pci-bridge.h>
29 #include <asm/ppc-pci.h>
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.
35 * This is the default list valid for most platforms. This file provides
36 * functions who can take an explicit list if necessary though
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
43 static struct of_device_id of_default_bus_ids[] = {
45 { .compatible = "soc", },
46 { .type = "spider", },
56 * OF platform device type definition & base infrastructure
60 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
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;
69 return of_match_device(matches, of_dev) != NULL;
72 static int of_platform_device_probe(struct device *dev)
75 struct of_platform_driver *drv;
76 struct of_device *of_dev;
77 const struct of_device_id *match;
79 drv = to_of_platform_driver(dev->driver);
80 of_dev = to_of_device(dev);
87 match = of_match_device(drv->match_table, of_dev);
89 error = drv->probe(of_dev, match);
96 static int of_platform_device_remove(struct device *dev)
98 struct of_device * of_dev = to_of_device(dev);
99 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
101 if (dev->driver && drv->remove)
106 static int of_platform_device_suspend(struct device *dev, pm_message_t state)
108 struct of_device * of_dev = to_of_device(dev);
109 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
112 if (dev->driver && drv->suspend)
113 error = drv->suspend(of_dev, state);
117 static int of_platform_device_resume(struct device * dev)
119 struct of_device * of_dev = to_of_device(dev);
120 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
123 if (dev->driver && drv->resume)
124 error = drv->resume(of_dev);
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,
136 EXPORT_SYMBOL(of_platform_bus_type);
138 static int __init of_bus_driver_init(void)
140 return bus_register(&of_platform_bus_type);
143 postcore_initcall(of_bus_driver_init);
145 int of_register_platform_driver(struct of_platform_driver *drv)
147 /* initialize common driver fields */
148 drv->driver.name = drv->name;
149 drv->driver.bus = &of_platform_bus_type;
151 /* register with core */
152 return driver_register(&drv->driver);
154 EXPORT_SYMBOL(of_register_platform_driver);
156 void of_unregister_platform_driver(struct of_platform_driver *drv)
158 driver_unregister(&drv->driver);
160 EXPORT_SYMBOL(of_unregister_platform_driver);
162 static void of_platform_make_bus_id(struct of_device *dev)
164 struct device_node *node = dev->node;
165 char *name = dev->dev.bus_id;
170 * If it's a DCR based device, use 'd' for native DCRs
171 * and 'D' for MMIO DCRs.
173 #ifdef CONFIG_PPC_DCR
174 reg = get_property(node, "dcr-reg", NULL);
176 #ifdef CONFIG_PPC_DCR_NATIVE
177 snprintf(name, BUS_ID_SIZE, "d%x.%s",
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,
187 #endif /* !CONFIG_PPC_DCR_NATIVE */
189 #endif /* CONFIG_PPC_DCR */
192 * For MMIO, get the physical address
194 reg = get_property(node, "reg", NULL);
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,
206 * No BusID, use the node name and pray
208 snprintf(name, BUS_ID_SIZE, "%s", node->name);
211 struct of_device* of_platform_device_create(struct device_node *np,
213 struct device *parent)
215 struct of_device *dev;
217 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
220 memset(dev, 0, sizeof(*dev));
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;
228 dev->dev.archdata.of_node = np;
229 dev->dev.archdata.numa_node = of_node_to_nid(np);
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
237 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
239 of_platform_make_bus_id(dev);
241 if (of_device_register(dev) != 0) {
248 EXPORT_SYMBOL(of_platform_device_create);
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
259 static int of_platform_bus_create(struct device_node *bus,
260 struct of_device_id *matches,
261 struct device *parent)
263 struct device_node *child;
264 struct of_device *dev;
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);
272 else if (!of_match_node(matches, child))
275 pr_debug(" and sub busses\n");
276 rc = of_platform_bus_create(child, matches, &dev->dev);
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
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.
295 int of_platform_bus_probe(struct device_node *root,
296 struct of_device_id *matches,
297 struct device *parent)
299 struct device_node *child;
300 struct of_device *dev;
304 matches = of_default_bus_ids;
305 if (matches == OF_NO_DEEP_PROBE)
308 root = of_find_node_by_path("/");
312 pr_debug("of_platform_bus_probe()\n");
313 pr_debug(" starting at: %s\n", root->full_name);
315 /* Do a self check of bus type, if there's a match, create
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);
325 pr_debug(" create all sub busses\n");
326 rc = of_platform_bus_create(root, matches, &dev->dev);
329 for (child = NULL; (child = of_get_next_child(root, child)); ) {
330 if (!of_match_node(matches, child))
333 pr_debug(" match: %s\n", child->full_name);
334 dev = of_platform_device_create(child, NULL, parent);
338 rc = of_platform_bus_create(child, matches, &dev->dev);
348 EXPORT_SYMBOL(of_platform_bus_probe);
350 static int of_dev_node_match(struct device *dev, void *data)
352 return to_of_device(dev)->node == data;
355 struct of_device *of_find_device_by_node(struct device_node *np)
359 dev = bus_find_device(&of_platform_bus_type,
360 NULL, np, of_dev_node_match);
362 return to_of_device(dev);
365 EXPORT_SYMBOL(of_find_device_by_node);
367 static int of_dev_phandle_match(struct device *dev, void *data)
370 return to_of_device(dev)->node->linux_phandle == *ph;
373 struct of_device *of_find_device_by_phandle(phandle ph)
377 dev = bus_find_device(&of_platform_bus_type,
378 NULL, &ph, of_dev_phandle_match);
380 return to_of_device(dev);
383 EXPORT_SYMBOL(of_find_device_by_phandle);
386 #ifdef CONFIG_PPC_OF_PLATFORM_PCI
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.
394 static int __devinit of_pci_phb_probe(struct of_device *dev,
395 const struct of_device_id *match)
397 struct pci_controller *phb;
399 /* Check if we can do that ... */
400 if (ppc_md.pci_setup_phb == NULL)
403 printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
405 /* Alloc and setup PHB data structure */
406 phb = pcibios_alloc_controller(dev->node);
410 /* Setup parent in sysfs */
411 phb->parent = &dev->dev;
413 /* Setup the PHB using arch provided callback */
414 if (ppc_md.pci_setup_phb(phb)) {
415 pcibios_free_controller(phb);
419 /* Process "ranges" property */
420 pci_process_bridge_OF_ranges(phb, dev->node, 0);
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
426 pci_setup_phb_io_dynamic(phb, 0);
428 /* Init pci_dn data structures */
429 pci_devs_phb_init_dynamic(phb);
431 /* Register devices with EEH */
433 if (dev->node->child)
434 eeh_add_device_tree_early(dev->node);
435 #endif /* CONFIG_EEH */
440 /* Claim resources. This might need some rework as well depending
441 * wether we are doing probe-only or not, like assigning unassigned
444 pcibios_claim_one_bus(phb->bus);
446 /* Finish EEH setup */
448 eeh_add_device_tree_late(phb->bus);
451 /* Add probed PCI devices to the device model */
452 pci_bus_add_devices(phb->bus);
457 static struct of_device_id of_pci_phb_ids[] = {
461 { .type = "pciex", },
466 static struct of_platform_driver of_pci_phb_driver = {
468 .match_table = of_pci_phb_ids,
469 .probe = of_pci_phb_probe,
471 .multithread_probe = 1,
475 static __init int of_pci_phb_init(void)
477 return of_register_platform_driver(&of_pci_phb_driver);
480 device_initcall(of_pci_phb_init);
482 #endif /* CONFIG_PPC_OF_PLATFORM_PCI */