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 static int of_dev_node_match(struct device
*dev
, void *data
)
27 return dev
->of_node
== data
;
31 * of_find_device_by_node - Find the platform_device associated with a node
32 * @np: Pointer to device tree node
34 * Returns platform_device pointer, or NULL if not found
36 struct platform_device
*of_find_device_by_node(struct device_node
*np
)
40 dev
= bus_find_device(&platform_bus_type
, NULL
, np
, of_dev_node_match
);
41 return dev
? to_platform_device(dev
) : NULL
;
43 EXPORT_SYMBOL(of_find_device_by_node
);
45 #if defined(CONFIG_PPC_DCR)
49 #if !defined(CONFIG_SPARC)
51 * The following routines scan a subtree and registers a device for
52 * each applicable node.
54 * Note: sparc doesn't use these routines because it has a different
55 * mechanism for creating devices from device tree nodes.
59 * of_device_make_bus_id - Use the device node data to assign a unique name
60 * @dev: pointer to device structure that is linked to a device tree node
62 * This routine will first try using either the dcr-reg or the reg property
63 * value to derive a unique name. As a last resort it will use the node
64 * name followed by a unique number.
66 void of_device_make_bus_id(struct device
*dev
)
68 static atomic_t bus_no_reg_magic
;
69 struct device_node
*node
= dev
->of_node
;
76 * If it's a DCR based device, use 'd' for native DCRs
77 * and 'D' for MMIO DCRs.
79 reg
= of_get_property(node
, "dcr-reg", NULL
);
81 #ifdef CONFIG_PPC_DCR_NATIVE
82 dev_set_name(dev
, "d%x.%s", *reg
, node
->name
);
83 #else /* CONFIG_PPC_DCR_NATIVE */
84 u64 addr
= of_translate_dcr_address(node
, *reg
, NULL
);
85 if (addr
!= OF_BAD_ADDR
) {
86 dev_set_name(dev
, "D%llx.%s",
87 (unsigned long long)addr
, node
->name
);
90 #endif /* !CONFIG_PPC_DCR_NATIVE */
92 #endif /* CONFIG_PPC_DCR */
95 * For MMIO, get the physical address
97 reg
= of_get_property(node
, "reg", NULL
);
99 addr
= of_translate_address(node
, reg
);
100 if (addr
!= OF_BAD_ADDR
) {
101 dev_set_name(dev
, "%llx.%s",
102 (unsigned long long)addr
, node
->name
);
108 * No BusID, use the node name and add a globally incremented
109 * counter (and pray...)
111 magic
= atomic_add_return(1, &bus_no_reg_magic
);
112 dev_set_name(dev
, "%s.%d", node
->name
, magic
- 1);
116 * of_device_alloc - Allocate and initialize an of_device
117 * @np: device node to assign to device
118 * @bus_id: Name to assign to the device. May be null to use default name.
119 * @parent: Parent device.
121 struct platform_device
*of_device_alloc(struct device_node
*np
,
123 struct device
*parent
)
125 struct platform_device
*dev
;
126 int rc
, i
, num_reg
= 0, num_irq
;
127 struct resource
*res
, temp_res
;
129 dev
= platform_device_alloc("", -1);
133 /* count the io and irq resources */
134 while (of_address_to_resource(np
, num_reg
, &temp_res
) == 0)
136 num_irq
= of_irq_count(np
);
138 /* Populate the resource table */
139 if (num_irq
|| num_reg
) {
140 res
= kzalloc(sizeof(*res
) * (num_irq
+ num_reg
), GFP_KERNEL
);
142 platform_device_put(dev
);
146 dev
->num_resources
= num_reg
+ num_irq
;
148 for (i
= 0; i
< num_reg
; i
++, res
++) {
149 rc
= of_address_to_resource(np
, i
, res
);
152 WARN_ON(of_irq_to_resource_table(np
, res
, num_irq
) != num_irq
);
155 dev
->dev
.of_node
= of_node_get(np
);
156 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
157 dev
->dev
.dma_mask
= &dev
->archdata
.dma_mask
;
159 dev
->dev
.parent
= parent
;
162 dev_set_name(&dev
->dev
, "%s", bus_id
);
164 of_device_make_bus_id(&dev
->dev
);
168 EXPORT_SYMBOL(of_device_alloc
);
171 * of_platform_device_create - Alloc, initialize and register an of_device
172 * @np: pointer to node to create device for
173 * @bus_id: name to assign device
174 * @parent: Linux device model parent device.
176 * Returns pointer to created platform device, or NULL if a device was not
177 * registered. Unavailable devices will not get registered.
179 struct platform_device
*of_platform_device_create(struct device_node
*np
,
181 struct device
*parent
)
183 struct platform_device
*dev
;
185 if (!of_device_is_available(np
))
188 dev
= of_device_alloc(np
, bus_id
, parent
);
192 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
193 dev
->archdata
.dma_mask
= 0xffffffffUL
;
195 dev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
196 dev
->dev
.bus
= &platform_bus_type
;
198 /* We do not fill the DMA ops for platform devices by default.
199 * This is currently the responsibility of the platform code
200 * to do such, possibly using a device notifier
203 if (of_device_add(dev
) != 0) {
204 platform_device_put(dev
);
210 EXPORT_SYMBOL(of_platform_device_create
);
213 * of_platform_bus_create - Create an OF device for a bus node and all its
214 * children. Optionally recursively instantiate matching busses.
215 * @bus: device node of the bus to instantiate
216 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
217 * disallow recursive creation of child busses
219 static int of_platform_bus_create(const struct device_node
*bus
,
220 const struct of_device_id
*matches
,
221 struct device
*parent
)
223 struct device_node
*child
;
224 struct platform_device
*dev
;
227 for_each_child_of_node(bus
, child
) {
228 pr_debug(" create child: %s\n", child
->full_name
);
229 dev
= of_platform_device_create(child
, NULL
, parent
);
233 if (!of_match_node(matches
, child
))
236 pr_debug(" and sub busses\n");
237 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
248 * of_platform_bus_probe - Probe the device-tree for platform busses
249 * @root: parent of the first level to probe or NULL for the root of the tree
250 * @matches: match table, NULL to use the default
251 * @parent: parent to hook devices from, NULL for toplevel
253 * Note that children of the provided root are not instantiated as devices
254 * unless the specified root itself matches the bus list and is not NULL.
256 int of_platform_bus_probe(struct device_node
*root
,
257 const struct of_device_id
*matches
,
258 struct device
*parent
)
260 struct device_node
*child
;
261 struct platform_device
*dev
;
264 if (WARN_ON(!matches
|| matches
== OF_NO_DEEP_PROBE
))
267 root
= of_find_node_by_path("/");
273 pr_debug("of_platform_bus_probe()\n");
274 pr_debug(" starting at: %s\n", root
->full_name
);
276 /* Do a self check of bus type, if there's a match, create
279 if (of_match_node(matches
, root
)) {
280 pr_debug(" root match, create all sub devices\n");
281 dev
= of_platform_device_create(root
, NULL
, parent
);
285 pr_debug(" create all sub busses\n");
286 rc
= of_platform_bus_create(root
, matches
, &dev
->dev
);
289 for_each_child_of_node(root
, child
) {
290 if (!of_match_node(matches
, child
))
293 pr_debug(" match: %s\n", child
->full_name
);
294 dev
= of_platform_device_create(child
, NULL
, parent
);
298 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
308 EXPORT_SYMBOL(of_platform_bus_probe
);
309 #endif /* !CONFIG_SPARC */