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>
22 #include <linux/of_device.h>
23 #include <linux/of_platform.h>
25 #include <asm/errno.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 if (!drv->driver.name)
74 drv->driver.name = drv->name;
75 if (!drv->driver.owner)
76 drv->driver.owner = drv->owner;
77 drv->driver.bus = &of_platform_bus_type;
79 /* register with core */
80 return driver_register(&drv->driver);
82 EXPORT_SYMBOL(of_register_platform_driver);
84 void of_unregister_platform_driver(struct of_platform_driver *drv)
86 driver_unregister(&drv->driver);
88 EXPORT_SYMBOL(of_unregister_platform_driver);
90 static void of_platform_make_bus_id(struct of_device *dev)
92 struct device_node *node = dev->node;
93 char *name = dev->dev.bus_id;
99 * If it's a DCR based device, use 'd' for native DCRs
100 * and 'D' for MMIO DCRs.
102 #ifdef CONFIG_PPC_DCR
103 reg = of_get_property(node, "dcr-reg", NULL);
105 #ifdef CONFIG_PPC_DCR_NATIVE
106 snprintf(name, BUS_ID_SIZE, "d%x.%s",
108 #else /* CONFIG_PPC_DCR_NATIVE */
109 addr = of_translate_dcr_address(node, *reg, NULL);
110 if (addr != OF_BAD_ADDR) {
111 snprintf(name, BUS_ID_SIZE,
112 "D%llx.%s", (unsigned long long)addr,
116 #endif /* !CONFIG_PPC_DCR_NATIVE */
118 #endif /* CONFIG_PPC_DCR */
121 * For MMIO, get the physical address
123 reg = of_get_property(node, "reg", NULL);
125 addr = of_translate_address(node, reg);
126 if (addr != OF_BAD_ADDR) {
127 snprintf(name, BUS_ID_SIZE,
128 "%llx.%s", (unsigned long long)addr,
135 * No BusID, use the node name and add a globally incremented
136 * counter (and pray...)
138 magic = atomic_add_return(1, &bus_no_reg_magic);
139 snprintf(name, BUS_ID_SIZE, "%s.%d", node->name, magic - 1);
142 struct of_device* of_platform_device_create(struct device_node *np,
144 struct device *parent)
146 struct of_device *dev;
148 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
152 dev->node = of_node_get(np);
153 dev->dma_mask = 0xffffffffUL;
154 dev->dev.dma_mask = &dev->dma_mask;
155 dev->dev.parent = parent;
156 dev->dev.bus = &of_platform_bus_type;
157 dev->dev.release = of_release_dev;
158 dev->dev.archdata.of_node = np;
159 dev->dev.archdata.numa_node = of_node_to_nid(np);
161 /* We do not fill the DMA ops for platform devices by default.
162 * This is currently the responsibility of the platform code
163 * to do such, possibly using a device notifier
167 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
169 of_platform_make_bus_id(dev);
171 if (of_device_register(dev) != 0) {
178 EXPORT_SYMBOL(of_platform_device_create);
183 * of_platform_bus_create - Create an OF device for a bus node and all its
184 * children. Optionally recursively instanciate matching busses.
185 * @bus: device node of the bus to instanciate
186 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
187 * disallow recursive creation of child busses
189 static int of_platform_bus_create(struct device_node *bus,
190 struct of_device_id *matches,
191 struct device *parent)
193 struct device_node *child;
194 struct of_device *dev;
197 for (child = NULL; (child = of_get_next_child(bus, child)); ) {
198 pr_debug(" create child: %s\n", child->full_name);
199 dev = of_platform_device_create(child, NULL, parent);
202 else if (!of_match_node(matches, child))
205 pr_debug(" and sub busses\n");
206 rc = of_platform_bus_create(child, matches, &dev->dev);
216 * of_platform_bus_probe - Probe the device-tree for platform busses
217 * @root: parent of the first level to probe or NULL for the root of the tree
218 * @matches: match table, NULL to use the default
219 * @parent: parent to hook devices from, NULL for toplevel
221 * Note that children of the provided root are not instanciated as devices
222 * unless the specified root itself matches the bus list and is not NULL.
225 int of_platform_bus_probe(struct device_node *root,
226 struct of_device_id *matches,
227 struct device *parent)
229 struct device_node *child;
230 struct of_device *dev;
234 matches = of_default_bus_ids;
235 if (matches == OF_NO_DEEP_PROBE)
238 root = of_find_node_by_path("/");
242 pr_debug("of_platform_bus_probe()\n");
243 pr_debug(" starting at: %s\n", root->full_name);
245 /* Do a self check of bus type, if there's a match, create
248 if (of_match_node(matches, root)) {
249 pr_debug(" root match, create all sub devices\n");
250 dev = of_platform_device_create(root, NULL, parent);
255 pr_debug(" create all sub busses\n");
256 rc = of_platform_bus_create(root, matches, &dev->dev);
259 for (child = NULL; (child = of_get_next_child(root, child)); ) {
260 if (!of_match_node(matches, child))
263 pr_debug(" match: %s\n", child->full_name);
264 dev = of_platform_device_create(child, NULL, parent);
268 rc = of_platform_bus_create(child, matches, &dev->dev);
278 EXPORT_SYMBOL(of_platform_bus_probe);
280 static int of_dev_node_match(struct device *dev, void *data)
282 return to_of_device(dev)->node == data;
285 struct of_device *of_find_device_by_node(struct device_node *np)
289 dev = bus_find_device(&of_platform_bus_type,
290 NULL, np, of_dev_node_match);
292 return to_of_device(dev);
295 EXPORT_SYMBOL(of_find_device_by_node);
297 static int of_dev_phandle_match(struct device *dev, void *data)
300 return to_of_device(dev)->node->linux_phandle == *ph;
303 struct of_device *of_find_device_by_phandle(phandle ph)
307 dev = bus_find_device(&of_platform_bus_type,
308 NULL, &ph, of_dev_phandle_match);
310 return to_of_device(dev);
313 EXPORT_SYMBOL(of_find_device_by_phandle);
316 #ifdef CONFIG_PPC_OF_PLATFORM_PCI
318 /* The probing of PCI controllers from of_platform is currently
319 * 64 bits only, mostly due to gratuitous differences between
320 * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
321 * lacking some bits needed here.
324 static int __devinit of_pci_phb_probe(struct of_device *dev,
325 const struct of_device_id *match)
327 struct pci_controller *phb;
329 /* Check if we can do that ... */
330 if (ppc_md.pci_setup_phb == NULL)
333 printk(KERN_INFO "Setting up PCI bus %s\n", dev->node->full_name);
335 /* Alloc and setup PHB data structure */
336 phb = pcibios_alloc_controller(dev->node);
340 /* Setup parent in sysfs */
341 phb->parent = &dev->dev;
343 /* Setup the PHB using arch provided callback */
344 if (ppc_md.pci_setup_phb(phb)) {
345 pcibios_free_controller(phb);
349 /* Process "ranges" property */
350 pci_process_bridge_OF_ranges(phb, dev->node, 0);
352 /* Init pci_dn data structures */
353 pci_devs_phb_init_dynamic(phb);
355 /* Register devices with EEH */
357 if (dev->node->child)
358 eeh_add_device_tree_early(dev->node);
359 #endif /* CONFIG_EEH */
364 /* Claim resources. This might need some rework as well depending
365 * wether we are doing probe-only or not, like assigning unassigned
368 pcibios_claim_one_bus(phb->bus);
370 /* Finish EEH setup */
372 eeh_add_device_tree_late(phb->bus);
375 /* Add probed PCI devices to the device model */
376 pci_bus_add_devices(phb->bus);
381 static struct of_device_id of_pci_phb_ids[] = {
385 { .type = "pciex", },
390 static struct of_platform_driver of_pci_phb_driver = {
391 .match_table = of_pci_phb_ids,
392 .probe = of_pci_phb_probe,
398 static __init int of_pci_phb_init(void)
400 return of_register_platform_driver(&of_pci_phb_driver);
403 device_initcall(of_pci_phb_init);
405 #endif /* CONFIG_PPC_OF_PLATFORM_PCI */