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/amba/bus.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/of_address.h>
21 #include <linux/of_device.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
26 const struct of_device_id of_default_bus_match_table[] = {
27 { .compatible = "simple-bus", },
28 #ifdef CONFIG_ARM_AMBA
29 { .compatible = "arm,amba-bus", },
30 #endif /* CONFIG_ARM_AMBA */
31 {} /* Empty terminated list */
34 static int of_dev_node_match(struct device *dev, void *data)
36 return dev->of_node == data;
40 * of_find_device_by_node - Find the platform_device associated with a node
41 * @np: Pointer to device tree node
43 * Returns platform_device pointer, or NULL if not found
45 struct platform_device *of_find_device_by_node(struct device_node *np)
49 dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
50 return dev ? to_platform_device(dev) : NULL;
52 EXPORT_SYMBOL(of_find_device_by_node);
54 #ifdef CONFIG_OF_ADDRESS
56 * The following routines scan a subtree and registers a device for
57 * each applicable node.
59 * Note: sparc doesn't use these routines because it has a different
60 * mechanism for creating devices from device tree nodes.
64 * of_device_make_bus_id - Use the device node data to assign a unique name
65 * @dev: pointer to device structure that is linked to a device tree node
67 * This routine will first try using the translated bus address to
68 * derive a unique name. If it cannot, then it will prepend names from
69 * parent nodes until a unique name can be derived.
71 void of_device_make_bus_id(struct device *dev)
73 struct device_node *node = dev->of_node;
77 /* Construct the name, using parent nodes if necessary to ensure uniqueness */
78 while (node->parent) {
80 * If the address can be translated, then that is as much
81 * uniqueness as we need. Make it the first component and return
83 reg = of_get_property(node, "reg", NULL);
84 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
85 dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
86 (unsigned long long)addr, node->name,
91 /* format arguments only used if dev_name() resolves to NULL */
92 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
93 strrchr(node->full_name, '/') + 1, dev_name(dev));
99 * of_device_alloc - Allocate and initialize an of_device
100 * @np: device node to assign to device
101 * @bus_id: Name to assign to the device. May be null to use default name.
102 * @parent: Parent device.
104 struct platform_device *of_device_alloc(struct device_node *np,
106 struct device *parent)
108 struct platform_device *dev;
109 int rc, i, num_reg = 0, num_irq;
110 struct resource *res, temp_res;
112 dev = platform_device_alloc("", -1);
116 /* count the io and irq resources */
117 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
119 num_irq = of_irq_count(np);
121 /* Populate the resource table */
122 if (num_irq || num_reg) {
123 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
125 platform_device_put(dev);
129 dev->num_resources = num_reg + num_irq;
131 for (i = 0; i < num_reg; i++, res++) {
132 rc = of_address_to_resource(np, i, res);
135 if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
136 pr_debug("not all legacy IRQ resources mapped for %s\n",
140 dev->dev.of_node = of_node_get(np);
141 #if defined(CONFIG_MICROBLAZE)
142 dev->dev.dma_mask = &dev->archdata.dma_mask;
144 dev->dev.parent = parent;
147 dev_set_name(&dev->dev, "%s", bus_id);
149 of_device_make_bus_id(&dev->dev);
153 EXPORT_SYMBOL(of_device_alloc);
156 * of_platform_device_create_pdata - Alloc, initialize and register an of_device
157 * @np: pointer to node to create device for
158 * @bus_id: name to assign device
159 * @platform_data: pointer to populate platform_data pointer with
160 * @parent: Linux device model parent device.
162 * Returns pointer to created platform device, or NULL if a device was not
163 * registered. Unavailable devices will not get registered.
165 static struct platform_device *of_platform_device_create_pdata(
166 struct device_node *np,
169 struct device *parent)
171 struct platform_device *dev;
173 if (!of_device_is_available(np) ||
174 of_node_test_and_set_flag(np, OF_POPULATED))
177 dev = of_device_alloc(np, bus_id, parent);
181 #if defined(CONFIG_MICROBLAZE)
182 dev->archdata.dma_mask = 0xffffffffUL;
184 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
185 if (!dev->dev.dma_mask)
186 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
187 dev->dev.bus = &platform_bus_type;
188 dev->dev.platform_data = platform_data;
190 /* We do not fill the DMA ops for platform devices by default.
191 * This is currently the responsibility of the platform code
192 * to do such, possibly using a device notifier
195 if (of_device_add(dev) != 0) {
196 platform_device_put(dev);
203 of_node_clear_flag(np, OF_POPULATED);
208 * of_platform_device_create - Alloc, initialize and register an of_device
209 * @np: pointer to node to create device for
210 * @bus_id: name to assign device
211 * @parent: Linux device model parent device.
213 * Returns pointer to created platform device, or NULL if a device was not
214 * registered. Unavailable devices will not get registered.
216 struct platform_device *of_platform_device_create(struct device_node *np,
218 struct device *parent)
220 return of_platform_device_create_pdata(np, bus_id, NULL, parent);
222 EXPORT_SYMBOL(of_platform_device_create);
224 #ifdef CONFIG_ARM_AMBA
225 static struct amba_device *of_amba_device_create(struct device_node *node,
228 struct device *parent)
230 struct amba_device *dev;
234 pr_debug("Creating amba device %s\n", node->full_name);
236 if (!of_device_is_available(node) ||
237 of_node_test_and_set_flag(node, OF_POPULATED))
240 dev = amba_device_alloc(NULL, 0, 0);
242 pr_err("%s(): amba_device_alloc() failed for %s\n",
243 __func__, node->full_name);
247 /* setup generic device info */
248 dev->dev.coherent_dma_mask = ~0;
249 dev->dev.of_node = of_node_get(node);
250 dev->dev.parent = parent;
251 dev->dev.platform_data = platform_data;
253 dev_set_name(&dev->dev, "%s", bus_id);
255 of_device_make_bus_id(&dev->dev);
257 /* Allow the HW Peripheral ID to be overridden */
258 prop = of_get_property(node, "arm,primecell-periphid", NULL);
260 dev->periphid = of_read_ulong(prop, 1);
262 /* Decode the IRQs and address ranges */
263 for (i = 0; i < AMBA_NR_IRQS; i++)
264 dev->irq[i] = irq_of_parse_and_map(node, i);
266 ret = of_address_to_resource(node, 0, &dev->res);
268 pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
269 __func__, ret, node->full_name);
273 ret = amba_device_add(dev, &iomem_resource);
275 pr_err("%s(): amba_device_add() failed (%d) for %s\n",
276 __func__, ret, node->full_name);
283 amba_device_put(dev);
285 of_node_clear_flag(node, OF_POPULATED);
288 #else /* CONFIG_ARM_AMBA */
289 static struct amba_device *of_amba_device_create(struct device_node *node,
292 struct device *parent)
296 #endif /* CONFIG_ARM_AMBA */
299 * of_devname_lookup() - Given a device node, lookup the preferred Linux name
301 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
302 struct device_node *np)
309 for(; lookup->compatible != NULL; lookup++) {
310 if (!of_device_is_compatible(np, lookup->compatible))
312 if (!of_address_to_resource(np, 0, &res))
313 if (res.start != lookup->phys_addr)
315 pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
323 * of_platform_bus_create() - Create a device for a node and its children.
324 * @bus: device node of the bus to instantiate
325 * @matches: match table for bus nodes
326 * @lookup: auxdata table for matching id and platform_data with device nodes
327 * @parent: parent for new device, or NULL for top level.
328 * @strict: require compatible property
330 * Creates a platform_device for the provided device_node, and optionally
331 * recursively create devices for all the child nodes.
333 static int of_platform_bus_create(struct device_node *bus,
334 const struct of_device_id *matches,
335 const struct of_dev_auxdata *lookup,
336 struct device *parent, bool strict)
338 const struct of_dev_auxdata *auxdata;
339 struct device_node *child;
340 struct platform_device *dev;
341 const char *bus_id = NULL;
342 void *platform_data = NULL;
345 /* Make sure it has a compatible property */
346 if (strict && (!of_get_property(bus, "compatible", NULL))) {
347 pr_debug("%s() - skipping %s, no compatible prop\n",
348 __func__, bus->full_name);
352 auxdata = of_dev_lookup(lookup, bus);
354 bus_id = auxdata->name;
355 platform_data = auxdata->platform_data;
358 if (of_device_is_compatible(bus, "arm,primecell")) {
360 * Don't return an error here to keep compatibility with older
363 of_amba_device_create(bus, bus_id, platform_data, parent);
367 dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
368 if (!dev || !of_match_node(matches, bus))
371 for_each_child_of_node(bus, child) {
372 pr_debug(" create child: %s\n", child->full_name);
373 rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
383 * of_platform_bus_probe() - Probe the device-tree for platform buses
384 * @root: parent of the first level to probe or NULL for the root of the tree
385 * @matches: match table for bus nodes
386 * @parent: parent to hook devices from, NULL for toplevel
388 * Note that children of the provided root are not instantiated as devices
389 * unless the specified root itself matches the bus list and is not NULL.
391 int of_platform_bus_probe(struct device_node *root,
392 const struct of_device_id *matches,
393 struct device *parent)
395 struct device_node *child;
398 root = root ? of_node_get(root) : of_find_node_by_path("/");
402 pr_debug("of_platform_bus_probe()\n");
403 pr_debug(" starting at: %s\n", root->full_name);
405 /* Do a self check of bus type, if there's a match, create children */
406 if (of_match_node(matches, root)) {
407 rc = of_platform_bus_create(root, matches, NULL, parent, false);
408 } else for_each_child_of_node(root, child) {
409 if (!of_match_node(matches, child))
411 rc = of_platform_bus_create(child, matches, NULL, parent, false);
419 EXPORT_SYMBOL(of_platform_bus_probe);
422 * of_platform_populate() - Populate platform_devices from device tree data
423 * @root: parent of the first level to probe or NULL for the root of the tree
424 * @matches: match table, NULL to use the default
425 * @lookup: auxdata table for matching id and platform_data with device nodes
426 * @parent: parent to hook devices from, NULL for toplevel
428 * Similar to of_platform_bus_probe(), this function walks the device tree
429 * and creates devices from nodes. It differs in that it follows the modern
430 * convention of requiring all device nodes to have a 'compatible' property,
431 * and it is suitable for creating devices which are children of the root
432 * node (of_platform_bus_probe will only create children of the root which
433 * are selected by the @matches argument).
435 * New board support should be using this function instead of
436 * of_platform_bus_probe().
438 * Returns 0 on success, < 0 on failure.
440 int of_platform_populate(struct device_node *root,
441 const struct of_device_id *matches,
442 const struct of_dev_auxdata *lookup,
443 struct device *parent)
445 struct device_node *child;
448 root = root ? of_node_get(root) : of_find_node_by_path("/");
452 for_each_child_of_node(root, child) {
453 rc = of_platform_bus_create(child, matches, lookup, parent, true);
461 EXPORT_SYMBOL_GPL(of_platform_populate);
463 static int of_platform_device_destroy(struct device *dev, void *data)
465 bool *children_left = data;
467 /* Do not touch devices not populated from the device tree */
468 if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED)) {
469 *children_left = true;
473 /* Recurse, but don't touch this device if it has any children left */
474 if (of_platform_depopulate(dev) != 0) {
475 *children_left = true;
479 if (dev->bus == &platform_bus_type)
480 platform_device_unregister(to_platform_device(dev));
481 #ifdef CONFIG_ARM_AMBA
482 else if (dev->bus == &amba_bustype)
483 amba_device_unregister(to_amba_device(dev));
486 *children_left = true;
490 of_node_clear_flag(dev->of_node, OF_POPULATED);
496 * of_platform_depopulate() - Remove devices populated from device tree
497 * @parent: device which childred will be removed
499 * Complementary to of_platform_populate(), this function removes children
500 * of the given device (and, recurrently, their children) that have been
501 * created from their respective device tree nodes (and only those,
502 * leaving others - eg. manually created - unharmed).
504 * Returns 0 when all children devices have been removed or
505 * -EBUSY when some children remained.
507 int of_platform_depopulate(struct device *parent)
509 bool children_left = false;
511 device_for_each_child(parent, &children_left,
512 of_platform_device_destroy);
514 return children_left ? -EBUSY : 0;
516 EXPORT_SYMBOL_GPL(of_platform_depopulate);
518 #endif /* CONFIG_OF_ADDRESS */