2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
5 * Merged from powerpc/kernel/of_platform.c and
6 * sparc{,64}/kernel/of_device.c by Stephen Rothwell
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/slab.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
25 const struct of_device_id of_default_bus_match_table[] = {
26 { .compatible = "simple-bus", },
27 #ifdef CONFIG_ARM_AMBA
28 { .compatible = "arm,amba-bus", },
29 #endif /* CONFIG_ARM_AMBA */
30 {} /* Empty terminated list */
33 static int of_dev_node_match(struct device *dev, void *data)
35 return dev->of_node == data;
39 * of_find_device_by_node - Find the platform_device associated with a node
40 * @np: Pointer to device tree node
42 * Returns platform_device pointer, or NULL if not found
44 struct platform_device *of_find_device_by_node(struct device_node *np)
48 dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
49 return dev ? to_platform_device(dev) : NULL;
51 EXPORT_SYMBOL(of_find_device_by_node);
53 #if defined(CONFIG_PPC_DCR)
57 #if !defined(CONFIG_SPARC)
59 * The following routines scan a subtree and registers a device for
60 * each applicable node.
62 * Note: sparc doesn't use these routines because it has a different
63 * mechanism for creating devices from device tree nodes.
67 * of_device_make_bus_id - Use the device node data to assign a unique name
68 * @dev: pointer to device structure that is linked to a device tree node
70 * This routine will first try using either the dcr-reg or the reg property
71 * value to derive a unique name. As a last resort it will use the node
72 * name followed by a unique number.
74 void of_device_make_bus_id(struct device *dev)
76 static atomic_t bus_no_reg_magic;
77 struct device_node *node = dev->of_node;
84 * If it's a DCR based device, use 'd' for native DCRs
85 * and 'D' for MMIO DCRs.
87 reg = of_get_property(node, "dcr-reg", NULL);
89 #ifdef CONFIG_PPC_DCR_NATIVE
90 dev_set_name(dev, "d%x.%s", *reg, node->name);
91 #else /* CONFIG_PPC_DCR_NATIVE */
92 u64 addr = of_translate_dcr_address(node, *reg, NULL);
93 if (addr != OF_BAD_ADDR) {
94 dev_set_name(dev, "D%llx.%s",
95 (unsigned long long)addr, node->name);
98 #endif /* !CONFIG_PPC_DCR_NATIVE */
100 #endif /* CONFIG_PPC_DCR */
103 * For MMIO, get the physical address
105 reg = of_get_property(node, "reg", NULL);
107 addr = of_translate_address(node, reg);
108 if (addr != OF_BAD_ADDR) {
109 dev_set_name(dev, "%llx.%s",
110 (unsigned long long)addr, node->name);
116 * No BusID, use the node name and add a globally incremented
117 * counter (and pray...)
119 magic = atomic_add_return(1, &bus_no_reg_magic);
120 dev_set_name(dev, "%s.%d", node->name, magic - 1);
124 * of_device_alloc - Allocate and initialize an of_device
125 * @np: device node to assign to device
126 * @bus_id: Name to assign to the device. May be null to use default name.
127 * @parent: Parent device.
129 struct platform_device *of_device_alloc(struct device_node *np,
131 struct device *parent)
133 struct platform_device *dev;
134 int rc, i, num_reg = 0, num_irq;
135 struct resource *res, temp_res;
137 dev = platform_device_alloc("", -1);
141 /* count the io and irq resources */
142 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
144 num_irq = of_irq_count(np);
146 /* Populate the resource table */
147 if (num_irq || num_reg) {
148 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
150 platform_device_put(dev);
154 dev->num_resources = num_reg + num_irq;
156 for (i = 0; i < num_reg; i++, res++) {
157 rc = of_address_to_resource(np, i, res);
160 WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
163 dev->dev.of_node = of_node_get(np);
164 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
165 dev->dev.dma_mask = &dev->archdata.dma_mask;
167 dev->dev.parent = parent;
170 dev_set_name(&dev->dev, "%s", bus_id);
172 of_device_make_bus_id(&dev->dev);
176 EXPORT_SYMBOL(of_device_alloc);
179 * of_platform_device_create - Alloc, initialize and register an of_device
180 * @np: pointer to node to create device for
181 * @bus_id: name to assign device
182 * @parent: Linux device model parent device.
184 * Returns pointer to created platform device, or NULL if a device was not
185 * registered. Unavailable devices will not get registered.
187 struct platform_device *of_platform_device_create(struct device_node *np,
189 struct device *parent)
191 struct platform_device *dev;
193 if (!of_device_is_available(np))
196 dev = of_device_alloc(np, bus_id, parent);
200 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
201 dev->archdata.dma_mask = 0xffffffffUL;
203 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
204 dev->dev.bus = &platform_bus_type;
206 /* We do not fill the DMA ops for platform devices by default.
207 * This is currently the responsibility of the platform code
208 * to do such, possibly using a device notifier
211 if (of_device_add(dev) != 0) {
212 platform_device_put(dev);
218 EXPORT_SYMBOL(of_platform_device_create);
221 * of_platform_bus_create() - Create a device for a node and its children.
222 * @bus: device node of the bus to instantiate
223 * @matches: match table for bus nodes
224 * disallow recursive creation of child buses
225 * @parent: parent for new device, or NULL for top level.
227 * Creates a platform_device for the provided device_node, and optionally
228 * recursively create devices for all the child nodes.
230 static int of_platform_bus_create(struct device_node *bus,
231 const struct of_device_id *matches,
232 struct device *parent, bool strict)
234 struct device_node *child;
235 struct platform_device *dev;
238 /* Make sure it has a compatible property */
239 if (strict && (!of_get_property(bus, "compatible", NULL))) {
240 pr_debug("%s() - skipping %s, no compatible prop\n",
241 __func__, bus->full_name);
245 dev = of_platform_device_create(bus, NULL, parent);
246 if (!dev || !of_match_node(matches, bus))
249 for_each_child_of_node(bus, child) {
250 pr_debug(" create child: %s\n", child->full_name);
251 rc = of_platform_bus_create(child, matches, &dev->dev, strict);
261 * of_platform_bus_probe() - Probe the device-tree for platform buses
262 * @root: parent of the first level to probe or NULL for the root of the tree
263 * @matches: match table for bus nodes
264 * @parent: parent to hook devices from, NULL for toplevel
266 * Note that children of the provided root are not instantiated as devices
267 * unless the specified root itself matches the bus list and is not NULL.
269 int of_platform_bus_probe(struct device_node *root,
270 const struct of_device_id *matches,
271 struct device *parent)
273 struct device_node *child;
276 root = root ? of_node_get(root) : of_find_node_by_path("/");
280 pr_debug("of_platform_bus_probe()\n");
281 pr_debug(" starting at: %s\n", root->full_name);
283 /* Do a self check of bus type, if there's a match, create children */
284 if (of_match_node(matches, root)) {
285 rc = of_platform_bus_create(root, matches, parent, false);
286 } else for_each_child_of_node(root, child) {
287 if (!of_match_node(matches, child))
289 rc = of_platform_bus_create(child, matches, parent, false);
297 EXPORT_SYMBOL(of_platform_bus_probe);
300 * of_platform_populate() - Populate platform_devices from device tree data
301 * @root: parent of the first level to probe or NULL for the root of the tree
302 * @matches: match table, NULL to use the default
303 * @parent: parent to hook devices from, NULL for toplevel
305 * Similar to of_platform_bus_probe(), this function walks the device tree
306 * and creates devices from nodes. It differs in that it follows the modern
307 * convention of requiring all device nodes to have a 'compatible' property,
308 * and it is suitable for creating devices which are children of the root
309 * node (of_platform_bus_probe will only create children of the root which
310 * are selected by the @matches argument).
312 * New board support should be using this function instead of
313 * of_platform_bus_probe().
315 * Returns 0 on success, < 0 on failure.
317 int of_platform_populate(struct device_node *root,
318 const struct of_device_id *matches,
319 struct device *parent)
321 struct device_node *child;
324 root = root ? of_node_get(root) : of_find_node_by_path("/");
328 for_each_child_of_node(root, child) {
329 rc = of_platform_bus_create(child, matches, parent, true);
337 #endif /* !CONFIG_SPARC */