blob: 1f4a5d3af76e656b0223c7823e517700ae27fc6d [file] [log] [blame]
Stephen Rothwell3f23de12007-05-03 02:38:57 +10001/*
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
7 *
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.
12 *
13 */
14#include <linux/errno.h>
Stephen Rothwell5c457082007-10-17 21:17:42 -070015#include <linux/module.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100016#include <linux/device.h>
Grant Likely5fd200f2010-06-08 07:48:13 -060017#include <linux/dma-mapping.h>
Grant Likely50ef5282010-06-08 07:48:20 -060018#include <linux/slab.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060019#include <linux/of_address.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100020#include <linux/of_device.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060021#include <linux/of_irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100022#include <linux/of_platform.h>
Grant Likelyeca39302010-06-08 07:48:21 -060023#include <linux/platform_device.h>
24
Grant Likelycbb49c22011-06-21 10:59:34 -060025const 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 */
31};
32
Jonas Bonnc6085582010-07-23 19:19:35 +020033static int of_dev_node_match(struct device *dev, void *data)
34{
35 return dev->of_node == data;
36}
37
38/**
39 * of_find_device_by_node - Find the platform_device associated with a node
40 * @np: Pointer to device tree node
41 *
42 * Returns platform_device pointer, or NULL if not found
43 */
44struct platform_device *of_find_device_by_node(struct device_node *np)
45{
46 struct device *dev;
47
48 dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
49 return dev ? to_platform_device(dev) : NULL;
50}
51EXPORT_SYMBOL(of_find_device_by_node);
52
Grant Likely596c9552010-07-14 23:51:43 -060053#if defined(CONFIG_PPC_DCR)
54#include <asm/dcr.h>
55#endif
56
Grant Likely5fd200f2010-06-08 07:48:13 -060057#if !defined(CONFIG_SPARC)
58/*
59 * The following routines scan a subtree and registers a device for
60 * each applicable node.
61 *
62 * Note: sparc doesn't use these routines because it has a different
63 * mechanism for creating devices from device tree nodes.
64 */
65
66/**
Grant Likely94c09312010-06-08 07:48:14 -060067 * 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
69 *
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.
73 */
Grant Likelyc6601222010-07-23 15:04:01 -060074void of_device_make_bus_id(struct device *dev)
Grant Likely94c09312010-06-08 07:48:14 -060075{
76 static atomic_t bus_no_reg_magic;
77 struct device_node *node = dev->of_node;
78 const u32 *reg;
79 u64 addr;
80 int magic;
81
82#ifdef CONFIG_PPC_DCR
83 /*
84 * If it's a DCR based device, use 'd' for native DCRs
85 * and 'D' for MMIO DCRs.
86 */
87 reg = of_get_property(node, "dcr-reg", NULL);
88 if (reg) {
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);
96 return;
97 }
98#endif /* !CONFIG_PPC_DCR_NATIVE */
99 }
100#endif /* CONFIG_PPC_DCR */
101
102 /*
103 * For MMIO, get the physical address
104 */
105 reg = of_get_property(node, "reg", NULL);
106 if (reg) {
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);
111 return;
112 }
113 }
114
115 /*
116 * No BusID, use the node name and add a globally incremented
117 * counter (and pray...)
118 */
119 magic = atomic_add_return(1, &bus_no_reg_magic);
120 dev_set_name(dev, "%s.%d", node->name, magic - 1);
121}
122
123/**
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.
128 */
Grant Likely94a0cb12010-07-22 13:59:23 -0600129struct platform_device *of_device_alloc(struct device_node *np,
Grant Likely94c09312010-06-08 07:48:14 -0600130 const char *bus_id,
131 struct device *parent)
132{
Grant Likely94a0cb12010-07-22 13:59:23 -0600133 struct platform_device *dev;
Andres Salomon52f65372010-10-10 21:35:05 -0600134 int rc, i, num_reg = 0, num_irq;
Grant Likelyac80a512010-06-08 07:48:15 -0600135 struct resource *res, temp_res;
Grant Likely94c09312010-06-08 07:48:14 -0600136
Grant Likely7096d042010-10-20 11:45:13 -0600137 dev = platform_device_alloc("", -1);
138 if (!dev)
139 return NULL;
140
141 /* count the io and irq resources */
Grant Likelyac80a512010-06-08 07:48:15 -0600142 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
143 num_reg++;
Andres Salomon52f65372010-10-10 21:35:05 -0600144 num_irq = of_irq_count(np);
Grant Likelyac80a512010-06-08 07:48:15 -0600145
Grant Likelyac80a512010-06-08 07:48:15 -0600146 /* Populate the resource table */
147 if (num_irq || num_reg) {
Grant Likely7096d042010-10-20 11:45:13 -0600148 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
149 if (!res) {
150 platform_device_put(dev);
151 return NULL;
152 }
153
Grant Likelyac80a512010-06-08 07:48:15 -0600154 dev->num_resources = num_reg + num_irq;
155 dev->resource = res;
156 for (i = 0; i < num_reg; i++, res++) {
157 rc = of_address_to_resource(np, i, res);
158 WARN_ON(rc);
159 }
Andres Salomon52f65372010-10-10 21:35:05 -0600160 WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
Grant Likelyac80a512010-06-08 07:48:15 -0600161 }
Grant Likely94c09312010-06-08 07:48:14 -0600162
163 dev->dev.of_node = of_node_get(np);
Grant Likely9e3288d2010-06-08 07:48:26 -0600164#if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
Grant Likely94c09312010-06-08 07:48:14 -0600165 dev->dev.dma_mask = &dev->archdata.dma_mask;
Grant Likely9e3288d2010-06-08 07:48:26 -0600166#endif
Grant Likely94c09312010-06-08 07:48:14 -0600167 dev->dev.parent = parent;
Grant Likely94c09312010-06-08 07:48:14 -0600168
169 if (bus_id)
170 dev_set_name(&dev->dev, "%s", bus_id);
171 else
172 of_device_make_bus_id(&dev->dev);
173
174 return dev;
175}
176EXPORT_SYMBOL(of_device_alloc);
177
178/**
Grant Likely5fd200f2010-06-08 07:48:13 -0600179 * 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.
Grant Likelycd1e6502011-01-03 15:51:11 -0700183 *
184 * Returns pointer to created platform device, or NULL if a device was not
185 * registered. Unavailable devices will not get registered.
Grant Likely5fd200f2010-06-08 07:48:13 -0600186 */
Grant Likely94a0cb12010-07-22 13:59:23 -0600187struct platform_device *of_platform_device_create(struct device_node *np,
Grant Likely5fd200f2010-06-08 07:48:13 -0600188 const char *bus_id,
189 struct device *parent)
190{
Grant Likely94a0cb12010-07-22 13:59:23 -0600191 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600192
Grant Likelycd1e6502011-01-03 15:51:11 -0700193 if (!of_device_is_available(np))
194 return NULL;
195
Grant Likely5fd200f2010-06-08 07:48:13 -0600196 dev = of_device_alloc(np, bus_id, parent);
197 if (!dev)
198 return NULL;
199
Grant Likely9e3288d2010-06-08 07:48:26 -0600200#if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
Grant Likely5fd200f2010-06-08 07:48:13 -0600201 dev->archdata.dma_mask = 0xffffffffUL;
Grant Likely9e3288d2010-06-08 07:48:26 -0600202#endif
Grant Likely5fd200f2010-06-08 07:48:13 -0600203 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Grant Likelyeca39302010-06-08 07:48:21 -0600204 dev->dev.bus = &platform_bus_type;
Grant Likely5fd200f2010-06-08 07:48:13 -0600205
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
209 */
210
Grant Likely7096d042010-10-20 11:45:13 -0600211 if (of_device_add(dev) != 0) {
212 platform_device_put(dev);
Grant Likely5fd200f2010-06-08 07:48:13 -0600213 return NULL;
214 }
215
216 return dev;
217}
218EXPORT_SYMBOL(of_platform_device_create);
219
220/**
Grant Likely38e9e212011-03-18 10:21:28 -0600221 * of_platform_bus_create() - Create a device for a node and its children.
Grant Likely5fd200f2010-06-08 07:48:13 -0600222 * @bus: device node of the bus to instantiate
Grant Likely1eed4c02011-03-18 10:21:29 -0600223 * @matches: match table for bus nodes
Grant Likely38e9e212011-03-18 10:21:28 -0600224 * disallow recursive creation of child buses
225 * @parent: parent for new device, or NULL for top level.
226 *
227 * Creates a platform_device for the provided device_node, and optionally
228 * recursively create devices for all the child nodes.
Grant Likely5fd200f2010-06-08 07:48:13 -0600229 */
Grant Likely38e9e212011-03-18 10:21:28 -0600230static int of_platform_bus_create(struct device_node *bus,
Grant Likely5fd200f2010-06-08 07:48:13 -0600231 const struct of_device_id *matches,
Grant Likely29d4f8a2011-06-21 10:59:34 -0600232 struct device *parent, bool strict)
Grant Likely5fd200f2010-06-08 07:48:13 -0600233{
234 struct device_node *child;
Grant Likely94a0cb12010-07-22 13:59:23 -0600235 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600236 int rc = 0;
237
Grant Likely29d4f8a2011-06-21 10:59:34 -0600238 /* 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);
242 return 0;
243 }
244
Grant Likely38e9e212011-03-18 10:21:28 -0600245 dev = of_platform_device_create(bus, NULL, parent);
246 if (!dev || !of_match_node(matches, bus))
247 return 0;
248
Grant Likely5fd200f2010-06-08 07:48:13 -0600249 for_each_child_of_node(bus, child) {
250 pr_debug(" create child: %s\n", child->full_name);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600251 rc = of_platform_bus_create(child, matches, &dev->dev, strict);
Grant Likely5fd200f2010-06-08 07:48:13 -0600252 if (rc) {
253 of_node_put(child);
254 break;
255 }
256 }
257 return rc;
258}
259
260/**
Grant Likely38e9e212011-03-18 10:21:28 -0600261 * of_platform_bus_probe() - Probe the device-tree for platform buses
Grant Likely5fd200f2010-06-08 07:48:13 -0600262 * @root: parent of the first level to probe or NULL for the root of the tree
Grant Likely1eed4c02011-03-18 10:21:29 -0600263 * @matches: match table for bus nodes
Grant Likely5fd200f2010-06-08 07:48:13 -0600264 * @parent: parent to hook devices from, NULL for toplevel
265 *
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.
268 */
269int of_platform_bus_probe(struct device_node *root,
270 const struct of_device_id *matches,
271 struct device *parent)
272{
273 struct device_node *child;
Grant Likely5fd200f2010-06-08 07:48:13 -0600274 int rc = 0;
275
Grant Likely1eed4c02011-03-18 10:21:29 -0600276 root = root ? of_node_get(root) : of_find_node_by_path("/");
277 if (!root)
Grant Likely60d59912010-06-08 07:48:25 -0600278 return -EINVAL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600279
280 pr_debug("of_platform_bus_probe()\n");
281 pr_debug(" starting at: %s\n", root->full_name);
282
Grant Likely1eed4c02011-03-18 10:21:29 -0600283 /* Do a self check of bus type, if there's a match, create children */
Grant Likely5fd200f2010-06-08 07:48:13 -0600284 if (of_match_node(matches, root)) {
Grant Likely29d4f8a2011-06-21 10:59:34 -0600285 rc = of_platform_bus_create(root, matches, parent, false);
Grant Likely38e9e212011-03-18 10:21:28 -0600286 } else for_each_child_of_node(root, child) {
Grant Likely5fd200f2010-06-08 07:48:13 -0600287 if (!of_match_node(matches, child))
288 continue;
Grant Likely29d4f8a2011-06-21 10:59:34 -0600289 rc = of_platform_bus_create(child, matches, parent, false);
Grant Likely38e9e212011-03-18 10:21:28 -0600290 if (rc)
Grant Likely5fd200f2010-06-08 07:48:13 -0600291 break;
Grant Likely5fd200f2010-06-08 07:48:13 -0600292 }
Grant Likely38e9e212011-03-18 10:21:28 -0600293
Grant Likely5fd200f2010-06-08 07:48:13 -0600294 of_node_put(root);
295 return rc;
296}
297EXPORT_SYMBOL(of_platform_bus_probe);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600298
299/**
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
304 *
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).
311 *
312 * New board support should be using this function instead of
313 * of_platform_bus_probe().
314 *
315 * Returns 0 on success, < 0 on failure.
316 */
317int of_platform_populate(struct device_node *root,
318 const struct of_device_id *matches,
319 struct device *parent)
320{
321 struct device_node *child;
322 int rc = 0;
323
324 root = root ? of_node_get(root) : of_find_node_by_path("/");
325 if (!root)
326 return -EINVAL;
327
328 for_each_child_of_node(root, child) {
329 rc = of_platform_bus_create(child, matches, parent, true);
330 if (rc)
331 break;
332 }
333
334 of_node_put(root);
335 return rc;
336}
Grant Likely5fd200f2010-06-08 07:48:13 -0600337#endif /* !CONFIG_SPARC */