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>
30 #include <asm/atomic.h>
33 * The list of OF IDs below is used for matching bus types in the
34 * system whose devices are to be exposed as of_platform_devices.
36 * This is the default list valid for most platforms. This file provides
37 * functions who can take an explicit list if necessary though
39 * The search is always performed recursively looking for children of
40 * the provided device_node and recursively if such a children matches
41 * a bus type in the list
44 static struct of_device_id of_default_bus_ids[] = {
46 { .compatible = "soc", },
47 { .type = "spider", },
56 static atomic_t bus_no_reg_magic;
58 struct bus_type of_platform_bus_type = {
59 .uevent = of_device_uevent,
61 EXPORT_SYMBOL(of_platform_bus_type);
63 static int __init of_bus_driver_init(void)
65 return of_bus_type_init(&of_platform_bus_type, "of_platform");
68 postcore_initcall(of_bus_driver_init);
70 int of_register_platform_driver(struct of_platform_driver *drv)
72 /* initialize common driver fields */
73 drv->driver.name = drv->name;
74 drv->driver.bus = &of_platform_bus_type;
76 /* register with core */
77 return driver_register(&drv->driver);
79 EXPORT_SYMBOL(of_register_platform_driver);
81 void of_unregister_platform_driver(struct of_platform_driver *drv)
83 driver_unregister(&drv->driver);
85 EXPORT_SYMBOL(of_unregister_platform_driver);
87 static void of_platform_make_bus_id(struct of_device *dev)
89 struct device_node *node = dev->node;
90 char *name = dev->dev.bus_id;
96 * If it's a DCR based device, use 'd' for native DCRs
97 * and 'D' for MMIO DCRs.
100 reg = of_get_property(node, "dcr-reg", NULL);
102 #ifdef CONFIG_PPC_DCR_NATIVE
103 snprintf(name, BUS_ID_SIZE, "d%x.%s",
105 #else /* CONFIG_PPC_DCR_NATIVE */
106 addr = of_translate_dcr_address(node, *reg, NULL);
107 if (addr != OF_BAD_ADDR) {
108 snprintf(name, BUS_ID_SIZE,
109 "D%llx.%s", (unsigned long long)addr,
113 #endif /* !CONFIG_PPC_DCR_NATIVE */
115 #endif /* CONFIG_PPC_DCR */
118 * For MMIO, get the physical address
120 reg = of_get_property(node, "reg", NULL);
122 addr = of_translate_address(node, reg);
123 if (addr != OF_BAD_ADDR) {
124 snprintf(name, BUS_ID_SIZE,
125 "%llx.%s", (unsigned long long)addr,
132 * No BusID, use the node name and add a globally incremented
133 * counter (and pray...)
135 magic = atomic_add_return(1, &bus_no_reg_magic);
136 snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
139 struct of_device* of_platform_device_create(struct device_node *np,
141 struct device *parent)
143 struct of_device *dev;
145 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
149 dev->node = of_node_get(np);
150 dev->dma_mask = 0xffffffffUL;
151 dev->dev.dma_mask = &dev->dma_mask;
152 dev->dev.parent = parent;
153 dev->dev.bus = &of_platform_bus_type;
154 dev->dev.release = of_release_dev;
155 dev->dev.archdata.of_node = np;
156 dev->dev.archdata.numa_node = of_node_to_nid(np);
158 /* We do not fill the DMA ops for platform devices by default.
159 * This is currently the responsibility of the platform code
160 * to do such, possibly using a device notifier
164 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
166 of_platform_make_bus_id(dev);
168 if (of_device_register(dev) != 0) {
175 EXPORT_SYMBOL(of_platform_device_create);
180 * of_platform_bus_create - Create an OF device for a bus node and all its
181 * children. Optionally recursively instanciate matching busses.
182 * @bus: device node of the bus to instanciate
183 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
184 * disallow recursive creation of child busses
186 static int of_platform_bus_create(struct device_node *bus,
187 struct of_device_id *matches,
188 struct device *parent)
190 struct device_node *child;
191 struct of_device *dev;
194 for (child = NULL; (child = of_get_next_child(bus, child)); ) {
195 pr_debug(" create child: %s\n", child->full_name);
196 dev = of_platform_device_create(child, NULL, parent);
199 else if (!of_match_node(matches, child))
202 pr_debug(" and sub busses\n");
203 rc = of_platform_bus_create(child, matches, &dev->dev);
213 * of_platform_bus_probe - Probe the device-tree for platform busses
214 * @root: parent of the first level to probe or NULL for the root of the tree
215 * @matches: match table, NULL to use the default
216 * @parent: parent to hook devices from, NULL for toplevel
218 * Note that children of the provided root are not instanciated as devices
219 * unless the specified root itself matches the bus list and is not NULL.
222 int of_platform_bus_probe(struct device_node *root,
223 struct of_device_id *matches,
224 struct device *parent)
226 struct device_node *child;
227 struct of_device *dev;
231 matches = of_default_bus_ids;
232 if (matches == OF_NO_DEEP_PROBE)
235 root = of_find_node_by_path("/");
239 pr_debug("of_platform_bus_probe()\n");
240 pr_debug(" starting at: %s\n", root->full_name);
242 /* Do a self check of bus type, if there's a match, create
245 if (of_match_node(matches, root)) {
246 pr_debug(" root match, create all sub devices\n");
247 dev = of_platform_device_create(root, NULL, parent);
252 pr_debug(" create all sub busses\n");
253 rc = of_platform_bus_create(root, matches, &dev->dev);
256 for (child = NULL; (child = of_get_next_child(root, child)); ) {
257 if (!of_match_node(matches, child))
260 pr_debug(" match: %s\n", child->full_name);
261 dev = of_platform_device_create(child, NULL, parent);
265 rc = of_platform_bus_create(child, matches, &dev->dev);
275 EXPORT_SYMBOL(of_platform_bus_probe);
277 static int of_dev_node_match(struct device *dev, void *data)
279 return to_of_device(dev)->node == data;
282 struct of_device *of_find_device_by_node(struct device_node *np)
286 dev = bus_find_device(&of_platform_bus_type,
287 NULL, np, of_dev_node_match);
289 return to_of_device(dev);
292 EXPORT_SYMBOL(of_find_device_by_node);
294 static int of_dev_phandle_match(struct device *dev, void *data)
297 return to_of_device(dev)->node->linux_phandle == *ph;
300 struct of_device *of_find_device_by_phandle(phandle ph)
304 dev = bus_find_device(&of_platform_bus_type,
305 NULL, &ph, of_dev_phandle_match);
307 return to_of_device(dev);
310 EXPORT_SYMBOL(of_find_device_by_phandle);
313 #ifdef CONFIG_PPC_OF_PLATFORM_PCI
315 /* The probing of PCI controllers from of_platform is currently
316 * 64 bits only, mostly due to gratuitous differences between
317 * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
318 * lacking some bits needed here.
321 static int __devinit of_pci_phb_probe(struct of_device *dev,
322 const struct of_device_id *match)
324 struct pci_controller *phb;
326 /* Check if we can do that ... */
327 if (ppc_md.pci_setup_phb == NULL)
330 printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
332 /* Alloc and setup PHB data structure */
333 phb = pcibios_alloc_controller(dev->node);
337 /* Setup parent in sysfs */
338 phb->parent = &dev->dev;
340 /* Setup the PHB using arch provided callback */
341 if (ppc_md.pci_setup_phb(phb)) {
342 pcibios_free_controller(phb);
346 /* Process "ranges" property */
347 pci_process_bridge_OF_ranges(phb, dev->node, 0);
349 /* Init pci_dn data structures */
350 pci_devs_phb_init_dynamic(phb);
352 /* Register devices with EEH */
354 if (dev->node->child)
355 eeh_add_device_tree_early(dev->node);
356 #endif /* CONFIG_EEH */
361 /* Claim resources. This might need some rework as well depending
362 * wether we are doing probe-only or not, like assigning unassigned
365 pcibios_claim_one_bus(phb->bus);
367 /* Finish EEH setup */
369 eeh_add_device_tree_late(phb->bus);
372 /* Add probed PCI devices to the device model */
373 pci_bus_add_devices(phb->bus);
378 static struct of_device_id of_pci_phb_ids[] = {
382 { .type = "pciex", },
387 static struct of_platform_driver of_pci_phb_driver = {
389 .match_table = of_pci_phb_ids,
390 .probe = of_pci_phb_probe,
393 static __init int of_pci_phb_init(void)
395 return of_register_platform_driver(&of_pci_phb_driver);
398 device_initcall(of_pci_phb_init);
400 #endif /* CONFIG_PPC_OF_PLATFORM_PCI */