1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
4*4882a593Smuzhiyun * <benh@kernel.crashing.org>
5*4882a593Smuzhiyun * and Arnd Bergmann, IBM Corp.
6*4882a593Smuzhiyun * Merged from powerpc/kernel/of_platform.c and
7*4882a593Smuzhiyun * sparc{,64}/kernel/of_device.c by Stephen Rothwell
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #define pr_fmt(fmt) "OF: " fmt
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/amba/bus.h>
15*4882a593Smuzhiyun #include <linux/device.h>
16*4882a593Smuzhiyun #include <linux/dma-mapping.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/of_address.h>
19*4882a593Smuzhiyun #include <linux/of_device.h>
20*4882a593Smuzhiyun #include <linux/of_iommu.h>
21*4882a593Smuzhiyun #include <linux/of_irq.h>
22*4882a593Smuzhiyun #include <linux/of_platform.h>
23*4882a593Smuzhiyun #include <linux/platform_device.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun const struct of_device_id of_default_bus_match_table[] = {
26*4882a593Smuzhiyun { .compatible = "simple-bus", },
27*4882a593Smuzhiyun { .compatible = "simple-mfd", },
28*4882a593Smuzhiyun { .compatible = "isa", },
29*4882a593Smuzhiyun #ifdef CONFIG_ARM_AMBA
30*4882a593Smuzhiyun { .compatible = "arm,amba-bus", },
31*4882a593Smuzhiyun #endif /* CONFIG_ARM_AMBA */
32*4882a593Smuzhiyun {} /* Empty terminated list */
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static const struct of_device_id of_skipped_node_table[] = {
36*4882a593Smuzhiyun { .compatible = "operating-points-v2", },
37*4882a593Smuzhiyun {} /* Empty terminated list */
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun * of_find_device_by_node - Find the platform_device associated with a node
42*4882a593Smuzhiyun * @np: Pointer to device tree node
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * Takes a reference to the embedded struct device which needs to be dropped
45*4882a593Smuzhiyun * after use.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * Returns platform_device pointer, or NULL if not found
48*4882a593Smuzhiyun */
of_find_device_by_node(struct device_node * np)49*4882a593Smuzhiyun struct platform_device *of_find_device_by_node(struct device_node *np)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun struct device *dev;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun dev = bus_find_device_by_of_node(&platform_bus_type, np);
54*4882a593Smuzhiyun return dev ? to_platform_device(dev) : NULL;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun EXPORT_SYMBOL(of_find_device_by_node);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #ifdef CONFIG_OF_ADDRESS
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * The following routines scan a subtree and registers a device for
61*4882a593Smuzhiyun * each applicable node.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Note: sparc doesn't use these routines because it has a different
64*4882a593Smuzhiyun * mechanism for creating devices from device tree nodes.
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun * of_device_make_bus_id - Use the device node data to assign a unique name
69*4882a593Smuzhiyun * @dev: pointer to device structure that is linked to a device tree node
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * This routine will first try using the translated bus address to
72*4882a593Smuzhiyun * derive a unique name. If it cannot, then it will prepend names from
73*4882a593Smuzhiyun * parent nodes until a unique name can be derived.
74*4882a593Smuzhiyun */
of_device_make_bus_id(struct device * dev)75*4882a593Smuzhiyun static void of_device_make_bus_id(struct device *dev)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun struct device_node *node = dev->of_node;
78*4882a593Smuzhiyun const __be32 *reg;
79*4882a593Smuzhiyun u64 addr;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* Construct the name, using parent nodes if necessary to ensure uniqueness */
82*4882a593Smuzhiyun while (node->parent) {
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun * If the address can be translated, then that is as much
85*4882a593Smuzhiyun * uniqueness as we need. Make it the first component and return
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun reg = of_get_property(node, "reg", NULL);
88*4882a593Smuzhiyun if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
89*4882a593Smuzhiyun dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
90*4882a593Smuzhiyun addr, node, dev_name(dev));
91*4882a593Smuzhiyun return;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* format arguments only used if dev_name() resolves to NULL */
95*4882a593Smuzhiyun dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
96*4882a593Smuzhiyun kbasename(node->full_name), dev_name(dev));
97*4882a593Smuzhiyun node = node->parent;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun * of_device_alloc - Allocate and initialize an of_device
103*4882a593Smuzhiyun * @np: device node to assign to device
104*4882a593Smuzhiyun * @bus_id: Name to assign to the device. May be null to use default name.
105*4882a593Smuzhiyun * @parent: Parent device.
106*4882a593Smuzhiyun */
of_device_alloc(struct device_node * np,const char * bus_id,struct device * parent)107*4882a593Smuzhiyun struct platform_device *of_device_alloc(struct device_node *np,
108*4882a593Smuzhiyun const char *bus_id,
109*4882a593Smuzhiyun struct device *parent)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun struct platform_device *dev;
112*4882a593Smuzhiyun int rc, i, num_reg = 0, num_irq;
113*4882a593Smuzhiyun struct resource *res, temp_res;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
116*4882a593Smuzhiyun if (!dev)
117*4882a593Smuzhiyun return NULL;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* count the io and irq resources */
120*4882a593Smuzhiyun while (of_address_to_resource(np, num_reg, &temp_res) == 0)
121*4882a593Smuzhiyun num_reg++;
122*4882a593Smuzhiyun num_irq = of_irq_count(np);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* Populate the resource table */
125*4882a593Smuzhiyun if (num_irq || num_reg) {
126*4882a593Smuzhiyun res = kcalloc(num_irq + num_reg, sizeof(*res), GFP_KERNEL);
127*4882a593Smuzhiyun if (!res) {
128*4882a593Smuzhiyun platform_device_put(dev);
129*4882a593Smuzhiyun return NULL;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun dev->num_resources = num_reg + num_irq;
133*4882a593Smuzhiyun dev->resource = res;
134*4882a593Smuzhiyun for (i = 0; i < num_reg; i++, res++) {
135*4882a593Smuzhiyun rc = of_address_to_resource(np, i, res);
136*4882a593Smuzhiyun WARN_ON(rc);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
139*4882a593Smuzhiyun pr_debug("not all legacy IRQ resources mapped for %pOFn\n",
140*4882a593Smuzhiyun np);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun dev->dev.of_node = of_node_get(np);
144*4882a593Smuzhiyun dev->dev.fwnode = &np->fwnode;
145*4882a593Smuzhiyun dev->dev.parent = parent ? : &platform_bus;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (bus_id)
148*4882a593Smuzhiyun dev_set_name(&dev->dev, "%s", bus_id);
149*4882a593Smuzhiyun else
150*4882a593Smuzhiyun of_device_make_bus_id(&dev->dev);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun return dev;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun EXPORT_SYMBOL(of_device_alloc);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /**
157*4882a593Smuzhiyun * of_platform_device_create_pdata - Alloc, initialize and register an of_device
158*4882a593Smuzhiyun * @np: pointer to node to create device for
159*4882a593Smuzhiyun * @bus_id: name to assign device
160*4882a593Smuzhiyun * @platform_data: pointer to populate platform_data pointer with
161*4882a593Smuzhiyun * @parent: Linux device model parent device.
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * Returns pointer to created platform device, or NULL if a device was not
164*4882a593Smuzhiyun * registered. Unavailable devices will not get registered.
165*4882a593Smuzhiyun */
of_platform_device_create_pdata(struct device_node * np,const char * bus_id,void * platform_data,struct device * parent)166*4882a593Smuzhiyun static struct platform_device *of_platform_device_create_pdata(
167*4882a593Smuzhiyun struct device_node *np,
168*4882a593Smuzhiyun const char *bus_id,
169*4882a593Smuzhiyun void *platform_data,
170*4882a593Smuzhiyun struct device *parent)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun struct platform_device *dev;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun if (!of_device_is_available(np) ||
175*4882a593Smuzhiyun of_node_test_and_set_flag(np, OF_POPULATED))
176*4882a593Smuzhiyun return NULL;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun dev = of_device_alloc(np, bus_id, parent);
179*4882a593Smuzhiyun if (!dev)
180*4882a593Smuzhiyun goto err_clear_flag;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
183*4882a593Smuzhiyun if (!dev->dev.dma_mask)
184*4882a593Smuzhiyun dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
185*4882a593Smuzhiyun dev->dev.bus = &platform_bus_type;
186*4882a593Smuzhiyun dev->dev.platform_data = platform_data;
187*4882a593Smuzhiyun of_msi_configure(&dev->dev, dev->dev.of_node);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (of_device_add(dev) != 0) {
190*4882a593Smuzhiyun platform_device_put(dev);
191*4882a593Smuzhiyun goto err_clear_flag;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun return dev;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun err_clear_flag:
197*4882a593Smuzhiyun of_node_clear_flag(np, OF_POPULATED);
198*4882a593Smuzhiyun return NULL;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun * of_platform_device_create - Alloc, initialize and register an of_device
203*4882a593Smuzhiyun * @np: pointer to node to create device for
204*4882a593Smuzhiyun * @bus_id: name to assign device
205*4882a593Smuzhiyun * @parent: Linux device model parent device.
206*4882a593Smuzhiyun *
207*4882a593Smuzhiyun * Returns pointer to created platform device, or NULL if a device was not
208*4882a593Smuzhiyun * registered. Unavailable devices will not get registered.
209*4882a593Smuzhiyun */
of_platform_device_create(struct device_node * np,const char * bus_id,struct device * parent)210*4882a593Smuzhiyun struct platform_device *of_platform_device_create(struct device_node *np,
211*4882a593Smuzhiyun const char *bus_id,
212*4882a593Smuzhiyun struct device *parent)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun return of_platform_device_create_pdata(np, bus_id, NULL, parent);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun EXPORT_SYMBOL(of_platform_device_create);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #ifdef CONFIG_ARM_AMBA
of_amba_device_create(struct device_node * node,const char * bus_id,void * platform_data,struct device * parent)219*4882a593Smuzhiyun static struct amba_device *of_amba_device_create(struct device_node *node,
220*4882a593Smuzhiyun const char *bus_id,
221*4882a593Smuzhiyun void *platform_data,
222*4882a593Smuzhiyun struct device *parent)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun struct amba_device *dev;
225*4882a593Smuzhiyun const void *prop;
226*4882a593Smuzhiyun int i, ret;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun pr_debug("Creating amba device %pOF\n", node);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun if (!of_device_is_available(node) ||
231*4882a593Smuzhiyun of_node_test_and_set_flag(node, OF_POPULATED))
232*4882a593Smuzhiyun return NULL;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun dev = amba_device_alloc(NULL, 0, 0);
235*4882a593Smuzhiyun if (!dev)
236*4882a593Smuzhiyun goto err_clear_flag;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* AMBA devices only support a single DMA mask */
239*4882a593Smuzhiyun dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
240*4882a593Smuzhiyun dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* setup generic device info */
243*4882a593Smuzhiyun dev->dev.of_node = of_node_get(node);
244*4882a593Smuzhiyun dev->dev.fwnode = &node->fwnode;
245*4882a593Smuzhiyun dev->dev.parent = parent ? : &platform_bus;
246*4882a593Smuzhiyun dev->dev.platform_data = platform_data;
247*4882a593Smuzhiyun if (bus_id)
248*4882a593Smuzhiyun dev_set_name(&dev->dev, "%s", bus_id);
249*4882a593Smuzhiyun else
250*4882a593Smuzhiyun of_device_make_bus_id(&dev->dev);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* Allow the HW Peripheral ID to be overridden */
253*4882a593Smuzhiyun prop = of_get_property(node, "arm,primecell-periphid", NULL);
254*4882a593Smuzhiyun if (prop)
255*4882a593Smuzhiyun dev->periphid = of_read_ulong(prop, 1);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /* Decode the IRQs and address ranges */
258*4882a593Smuzhiyun for (i = 0; i < AMBA_NR_IRQS; i++)
259*4882a593Smuzhiyun dev->irq[i] = irq_of_parse_and_map(node, i);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun ret = of_address_to_resource(node, 0, &dev->res);
262*4882a593Smuzhiyun if (ret) {
263*4882a593Smuzhiyun pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
264*4882a593Smuzhiyun ret, node);
265*4882a593Smuzhiyun goto err_free;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun ret = amba_device_add(dev, &iomem_resource);
269*4882a593Smuzhiyun if (ret) {
270*4882a593Smuzhiyun pr_err("amba_device_add() failed (%d) for %pOF\n",
271*4882a593Smuzhiyun ret, node);
272*4882a593Smuzhiyun goto err_free;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return dev;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun err_free:
278*4882a593Smuzhiyun amba_device_put(dev);
279*4882a593Smuzhiyun err_clear_flag:
280*4882a593Smuzhiyun of_node_clear_flag(node, OF_POPULATED);
281*4882a593Smuzhiyun return NULL;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun #else /* CONFIG_ARM_AMBA */
of_amba_device_create(struct device_node * node,const char * bus_id,void * platform_data,struct device * parent)284*4882a593Smuzhiyun static struct amba_device *of_amba_device_create(struct device_node *node,
285*4882a593Smuzhiyun const char *bus_id,
286*4882a593Smuzhiyun void *platform_data,
287*4882a593Smuzhiyun struct device *parent)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun return NULL;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun #endif /* CONFIG_ARM_AMBA */
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /**
294*4882a593Smuzhiyun * of_dev_lookup() - Given a device node, lookup the preferred Linux name
295*4882a593Smuzhiyun */
of_dev_lookup(const struct of_dev_auxdata * lookup,struct device_node * np)296*4882a593Smuzhiyun static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
297*4882a593Smuzhiyun struct device_node *np)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun const struct of_dev_auxdata *auxdata;
300*4882a593Smuzhiyun struct resource res;
301*4882a593Smuzhiyun int compatible = 0;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun if (!lookup)
304*4882a593Smuzhiyun return NULL;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun auxdata = lookup;
307*4882a593Smuzhiyun for (; auxdata->compatible; auxdata++) {
308*4882a593Smuzhiyun if (!of_device_is_compatible(np, auxdata->compatible))
309*4882a593Smuzhiyun continue;
310*4882a593Smuzhiyun compatible++;
311*4882a593Smuzhiyun if (!of_address_to_resource(np, 0, &res))
312*4882a593Smuzhiyun if (res.start != auxdata->phys_addr)
313*4882a593Smuzhiyun continue;
314*4882a593Smuzhiyun pr_debug("%pOF: devname=%s\n", np, auxdata->name);
315*4882a593Smuzhiyun return auxdata;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if (!compatible)
319*4882a593Smuzhiyun return NULL;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /* Try compatible match if no phys_addr and name are specified */
322*4882a593Smuzhiyun auxdata = lookup;
323*4882a593Smuzhiyun for (; auxdata->compatible; auxdata++) {
324*4882a593Smuzhiyun if (!of_device_is_compatible(np, auxdata->compatible))
325*4882a593Smuzhiyun continue;
326*4882a593Smuzhiyun if (!auxdata->phys_addr && !auxdata->name) {
327*4882a593Smuzhiyun pr_debug("%pOF: compatible match\n", np);
328*4882a593Smuzhiyun return auxdata;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun return NULL;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /**
336*4882a593Smuzhiyun * of_platform_bus_create() - Create a device for a node and its children.
337*4882a593Smuzhiyun * @bus: device node of the bus to instantiate
338*4882a593Smuzhiyun * @matches: match table for bus nodes
339*4882a593Smuzhiyun * @lookup: auxdata table for matching id and platform_data with device nodes
340*4882a593Smuzhiyun * @parent: parent for new device, or NULL for top level.
341*4882a593Smuzhiyun * @strict: require compatible property
342*4882a593Smuzhiyun *
343*4882a593Smuzhiyun * Creates a platform_device for the provided device_node, and optionally
344*4882a593Smuzhiyun * recursively create devices for all the child nodes.
345*4882a593Smuzhiyun */
of_platform_bus_create(struct device_node * bus,const struct of_device_id * matches,const struct of_dev_auxdata * lookup,struct device * parent,bool strict)346*4882a593Smuzhiyun static int of_platform_bus_create(struct device_node *bus,
347*4882a593Smuzhiyun const struct of_device_id *matches,
348*4882a593Smuzhiyun const struct of_dev_auxdata *lookup,
349*4882a593Smuzhiyun struct device *parent, bool strict)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun const struct of_dev_auxdata *auxdata;
352*4882a593Smuzhiyun struct device_node *child;
353*4882a593Smuzhiyun struct platform_device *dev;
354*4882a593Smuzhiyun const char *bus_id = NULL;
355*4882a593Smuzhiyun void *platform_data = NULL;
356*4882a593Smuzhiyun int rc = 0;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /* Make sure it has a compatible property */
359*4882a593Smuzhiyun if (strict && (!of_get_property(bus, "compatible", NULL))) {
360*4882a593Smuzhiyun pr_debug("%s() - skipping %pOF, no compatible prop\n",
361*4882a593Smuzhiyun __func__, bus);
362*4882a593Smuzhiyun return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* Skip nodes for which we don't want to create devices */
366*4882a593Smuzhiyun if (unlikely(of_match_node(of_skipped_node_table, bus))) {
367*4882a593Smuzhiyun pr_debug("%s() - skipping %pOF node\n", __func__, bus);
368*4882a593Smuzhiyun return 0;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
372*4882a593Smuzhiyun pr_debug("%s() - skipping %pOF, already populated\n",
373*4882a593Smuzhiyun __func__, bus);
374*4882a593Smuzhiyun return 0;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun auxdata = of_dev_lookup(lookup, bus);
378*4882a593Smuzhiyun if (auxdata) {
379*4882a593Smuzhiyun bus_id = auxdata->name;
380*4882a593Smuzhiyun platform_data = auxdata->platform_data;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun if (of_device_is_compatible(bus, "arm,primecell")) {
384*4882a593Smuzhiyun /*
385*4882a593Smuzhiyun * Don't return an error here to keep compatibility with older
386*4882a593Smuzhiyun * device tree files.
387*4882a593Smuzhiyun */
388*4882a593Smuzhiyun of_amba_device_create(bus, bus_id, platform_data, parent);
389*4882a593Smuzhiyun return 0;
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
393*4882a593Smuzhiyun if (!dev || !of_match_node(matches, bus))
394*4882a593Smuzhiyun return 0;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun for_each_child_of_node(bus, child) {
397*4882a593Smuzhiyun pr_debug(" create child: %pOF\n", child);
398*4882a593Smuzhiyun rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
399*4882a593Smuzhiyun if (rc) {
400*4882a593Smuzhiyun of_node_put(child);
401*4882a593Smuzhiyun break;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun of_node_set_flag(bus, OF_POPULATED_BUS);
405*4882a593Smuzhiyun return rc;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /**
409*4882a593Smuzhiyun * of_platform_bus_probe() - Probe the device-tree for platform buses
410*4882a593Smuzhiyun * @root: parent of the first level to probe or NULL for the root of the tree
411*4882a593Smuzhiyun * @matches: match table for bus nodes
412*4882a593Smuzhiyun * @parent: parent to hook devices from, NULL for toplevel
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * Note that children of the provided root are not instantiated as devices
415*4882a593Smuzhiyun * unless the specified root itself matches the bus list and is not NULL.
416*4882a593Smuzhiyun */
of_platform_bus_probe(struct device_node * root,const struct of_device_id * matches,struct device * parent)417*4882a593Smuzhiyun int of_platform_bus_probe(struct device_node *root,
418*4882a593Smuzhiyun const struct of_device_id *matches,
419*4882a593Smuzhiyun struct device *parent)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun struct device_node *child;
422*4882a593Smuzhiyun int rc = 0;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun root = root ? of_node_get(root) : of_find_node_by_path("/");
425*4882a593Smuzhiyun if (!root)
426*4882a593Smuzhiyun return -EINVAL;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun pr_debug("%s()\n", __func__);
429*4882a593Smuzhiyun pr_debug(" starting at: %pOF\n", root);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* Do a self check of bus type, if there's a match, create children */
432*4882a593Smuzhiyun if (of_match_node(matches, root)) {
433*4882a593Smuzhiyun rc = of_platform_bus_create(root, matches, NULL, parent, false);
434*4882a593Smuzhiyun } else for_each_child_of_node(root, child) {
435*4882a593Smuzhiyun if (!of_match_node(matches, child))
436*4882a593Smuzhiyun continue;
437*4882a593Smuzhiyun rc = of_platform_bus_create(child, matches, NULL, parent, false);
438*4882a593Smuzhiyun if (rc) {
439*4882a593Smuzhiyun of_node_put(child);
440*4882a593Smuzhiyun break;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun of_node_put(root);
445*4882a593Smuzhiyun return rc;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun EXPORT_SYMBOL(of_platform_bus_probe);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun * of_platform_populate() - Populate platform_devices from device tree data
451*4882a593Smuzhiyun * @root: parent of the first level to probe or NULL for the root of the tree
452*4882a593Smuzhiyun * @matches: match table, NULL to use the default
453*4882a593Smuzhiyun * @lookup: auxdata table for matching id and platform_data with device nodes
454*4882a593Smuzhiyun * @parent: parent to hook devices from, NULL for toplevel
455*4882a593Smuzhiyun *
456*4882a593Smuzhiyun * Similar to of_platform_bus_probe(), this function walks the device tree
457*4882a593Smuzhiyun * and creates devices from nodes. It differs in that it follows the modern
458*4882a593Smuzhiyun * convention of requiring all device nodes to have a 'compatible' property,
459*4882a593Smuzhiyun * and it is suitable for creating devices which are children of the root
460*4882a593Smuzhiyun * node (of_platform_bus_probe will only create children of the root which
461*4882a593Smuzhiyun * are selected by the @matches argument).
462*4882a593Smuzhiyun *
463*4882a593Smuzhiyun * New board support should be using this function instead of
464*4882a593Smuzhiyun * of_platform_bus_probe().
465*4882a593Smuzhiyun *
466*4882a593Smuzhiyun * Returns 0 on success, < 0 on failure.
467*4882a593Smuzhiyun */
of_platform_populate(struct device_node * root,const struct of_device_id * matches,const struct of_dev_auxdata * lookup,struct device * parent)468*4882a593Smuzhiyun int of_platform_populate(struct device_node *root,
469*4882a593Smuzhiyun const struct of_device_id *matches,
470*4882a593Smuzhiyun const struct of_dev_auxdata *lookup,
471*4882a593Smuzhiyun struct device *parent)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun struct device_node *child;
474*4882a593Smuzhiyun int rc = 0;
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun root = root ? of_node_get(root) : of_find_node_by_path("/");
477*4882a593Smuzhiyun if (!root)
478*4882a593Smuzhiyun return -EINVAL;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun pr_debug("%s()\n", __func__);
481*4882a593Smuzhiyun pr_debug(" starting at: %pOF\n", root);
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun device_links_supplier_sync_state_pause();
484*4882a593Smuzhiyun for_each_child_of_node(root, child) {
485*4882a593Smuzhiyun rc = of_platform_bus_create(child, matches, lookup, parent, true);
486*4882a593Smuzhiyun if (rc) {
487*4882a593Smuzhiyun of_node_put(child);
488*4882a593Smuzhiyun break;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun device_links_supplier_sync_state_resume();
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun of_node_set_flag(root, OF_POPULATED_BUS);
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun of_node_put(root);
496*4882a593Smuzhiyun return rc;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_platform_populate);
499*4882a593Smuzhiyun
of_platform_default_populate(struct device_node * root,const struct of_dev_auxdata * lookup,struct device * parent)500*4882a593Smuzhiyun int of_platform_default_populate(struct device_node *root,
501*4882a593Smuzhiyun const struct of_dev_auxdata *lookup,
502*4882a593Smuzhiyun struct device *parent)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun return of_platform_populate(root, of_default_bus_match_table, lookup,
505*4882a593Smuzhiyun parent);
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_platform_default_populate);
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun #ifndef CONFIG_PPC
510*4882a593Smuzhiyun static const struct of_device_id reserved_mem_matches[] = {
511*4882a593Smuzhiyun { .compatible = "qcom,rmtfs-mem" },
512*4882a593Smuzhiyun { .compatible = "qcom,cmd-db" },
513*4882a593Smuzhiyun { .compatible = "ramoops" },
514*4882a593Smuzhiyun {}
515*4882a593Smuzhiyun };
516*4882a593Smuzhiyun
of_platform_default_populate_init(void)517*4882a593Smuzhiyun static int __init of_platform_default_populate_init(void)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun struct device_node *node;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun device_links_supplier_sync_state_pause();
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun if (!of_have_populated_dt())
524*4882a593Smuzhiyun return -ENODEV;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun * Handle certain compatibles explicitly, since we don't want to create
528*4882a593Smuzhiyun * platform_devices for every node in /reserved-memory with a
529*4882a593Smuzhiyun * "compatible",
530*4882a593Smuzhiyun */
531*4882a593Smuzhiyun for_each_matching_node(node, reserved_mem_matches)
532*4882a593Smuzhiyun of_platform_device_create(node, NULL, NULL);
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun node = of_find_node_by_path("/firmware");
535*4882a593Smuzhiyun if (node) {
536*4882a593Smuzhiyun of_platform_populate(node, NULL, NULL, NULL);
537*4882a593Smuzhiyun of_node_put(node);
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /* Populate everything else. */
541*4882a593Smuzhiyun of_platform_default_populate(NULL, NULL, NULL);
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun return 0;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun arch_initcall_sync(of_platform_default_populate_init);
546*4882a593Smuzhiyun
of_platform_sync_state_init(void)547*4882a593Smuzhiyun static int __init of_platform_sync_state_init(void)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun device_links_supplier_sync_state_resume();
550*4882a593Smuzhiyun return 0;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun late_initcall_sync(of_platform_sync_state_init);
553*4882a593Smuzhiyun #endif
554*4882a593Smuzhiyun
of_platform_device_destroy(struct device * dev,void * data)555*4882a593Smuzhiyun int of_platform_device_destroy(struct device *dev, void *data)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun /* Do not touch devices not populated from the device tree */
558*4882a593Smuzhiyun if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
559*4882a593Smuzhiyun return 0;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /* Recurse for any nodes that were treated as busses */
562*4882a593Smuzhiyun if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
563*4882a593Smuzhiyun device_for_each_child(dev, NULL, of_platform_device_destroy);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun of_node_clear_flag(dev->of_node, OF_POPULATED);
566*4882a593Smuzhiyun of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun if (dev->bus == &platform_bus_type)
569*4882a593Smuzhiyun platform_device_unregister(to_platform_device(dev));
570*4882a593Smuzhiyun #ifdef CONFIG_ARM_AMBA
571*4882a593Smuzhiyun else if (dev->bus == &amba_bustype)
572*4882a593Smuzhiyun amba_device_unregister(to_amba_device(dev));
573*4882a593Smuzhiyun #endif
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun return 0;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_platform_device_destroy);
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun /**
580*4882a593Smuzhiyun * of_platform_depopulate() - Remove devices populated from device tree
581*4882a593Smuzhiyun * @parent: device which children will be removed
582*4882a593Smuzhiyun *
583*4882a593Smuzhiyun * Complementary to of_platform_populate(), this function removes children
584*4882a593Smuzhiyun * of the given device (and, recurrently, their children) that have been
585*4882a593Smuzhiyun * created from their respective device tree nodes (and only those,
586*4882a593Smuzhiyun * leaving others - eg. manually created - unharmed).
587*4882a593Smuzhiyun */
of_platform_depopulate(struct device * parent)588*4882a593Smuzhiyun void of_platform_depopulate(struct device *parent)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
591*4882a593Smuzhiyun device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
592*4882a593Smuzhiyun of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_platform_depopulate);
596*4882a593Smuzhiyun
devm_of_platform_populate_release(struct device * dev,void * res)597*4882a593Smuzhiyun static void devm_of_platform_populate_release(struct device *dev, void *res)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun of_platform_depopulate(*(struct device **)res);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /**
603*4882a593Smuzhiyun * devm_of_platform_populate() - Populate platform_devices from device tree data
604*4882a593Smuzhiyun * @dev: device that requested to populate from device tree data
605*4882a593Smuzhiyun *
606*4882a593Smuzhiyun * Similar to of_platform_populate(), but will automatically call
607*4882a593Smuzhiyun * of_platform_depopulate() when the device is unbound from the bus.
608*4882a593Smuzhiyun *
609*4882a593Smuzhiyun * Returns 0 on success, < 0 on failure.
610*4882a593Smuzhiyun */
devm_of_platform_populate(struct device * dev)611*4882a593Smuzhiyun int devm_of_platform_populate(struct device *dev)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun struct device **ptr;
614*4882a593Smuzhiyun int ret;
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun if (!dev)
617*4882a593Smuzhiyun return -EINVAL;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun ptr = devres_alloc(devm_of_platform_populate_release,
620*4882a593Smuzhiyun sizeof(*ptr), GFP_KERNEL);
621*4882a593Smuzhiyun if (!ptr)
622*4882a593Smuzhiyun return -ENOMEM;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
625*4882a593Smuzhiyun if (ret) {
626*4882a593Smuzhiyun devres_free(ptr);
627*4882a593Smuzhiyun } else {
628*4882a593Smuzhiyun *ptr = dev;
629*4882a593Smuzhiyun devres_add(dev, ptr);
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun return ret;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_of_platform_populate);
635*4882a593Smuzhiyun
devm_of_platform_match(struct device * dev,void * res,void * data)636*4882a593Smuzhiyun static int devm_of_platform_match(struct device *dev, void *res, void *data)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun struct device **ptr = res;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun if (!ptr) {
641*4882a593Smuzhiyun WARN_ON(!ptr);
642*4882a593Smuzhiyun return 0;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun return *ptr == data;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun /**
649*4882a593Smuzhiyun * devm_of_platform_depopulate() - Remove devices populated from device tree
650*4882a593Smuzhiyun * @dev: device that requested to depopulate from device tree data
651*4882a593Smuzhiyun *
652*4882a593Smuzhiyun * Complementary to devm_of_platform_populate(), this function removes children
653*4882a593Smuzhiyun * of the given device (and, recurrently, their children) that have been
654*4882a593Smuzhiyun * created from their respective device tree nodes (and only those,
655*4882a593Smuzhiyun * leaving others - eg. manually created - unharmed).
656*4882a593Smuzhiyun */
devm_of_platform_depopulate(struct device * dev)657*4882a593Smuzhiyun void devm_of_platform_depopulate(struct device *dev)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun int ret;
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun ret = devres_release(dev, devm_of_platform_populate_release,
662*4882a593Smuzhiyun devm_of_platform_match, dev);
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun WARN_ON(ret);
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun #ifdef CONFIG_OF_DYNAMIC
of_platform_notify(struct notifier_block * nb,unsigned long action,void * arg)669*4882a593Smuzhiyun static int of_platform_notify(struct notifier_block *nb,
670*4882a593Smuzhiyun unsigned long action, void *arg)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun struct of_reconfig_data *rd = arg;
673*4882a593Smuzhiyun struct platform_device *pdev_parent, *pdev;
674*4882a593Smuzhiyun bool children_left;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun switch (of_reconfig_get_state_change(action, rd)) {
677*4882a593Smuzhiyun case OF_RECONFIG_CHANGE_ADD:
678*4882a593Smuzhiyun /* verify that the parent is a bus */
679*4882a593Smuzhiyun if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
680*4882a593Smuzhiyun return NOTIFY_OK; /* not for us */
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /* already populated? (driver using of_populate manually) */
683*4882a593Smuzhiyun if (of_node_check_flag(rd->dn, OF_POPULATED))
684*4882a593Smuzhiyun return NOTIFY_OK;
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /* pdev_parent may be NULL when no bus platform device */
687*4882a593Smuzhiyun pdev_parent = of_find_device_by_node(rd->dn->parent);
688*4882a593Smuzhiyun pdev = of_platform_device_create(rd->dn, NULL,
689*4882a593Smuzhiyun pdev_parent ? &pdev_parent->dev : NULL);
690*4882a593Smuzhiyun of_dev_put(pdev_parent);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun if (pdev == NULL) {
693*4882a593Smuzhiyun pr_err("%s: failed to create for '%pOF'\n",
694*4882a593Smuzhiyun __func__, rd->dn);
695*4882a593Smuzhiyun /* of_platform_device_create tosses the error code */
696*4882a593Smuzhiyun return notifier_from_errno(-EINVAL);
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun break;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun case OF_RECONFIG_CHANGE_REMOVE:
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun /* already depopulated? */
703*4882a593Smuzhiyun if (!of_node_check_flag(rd->dn, OF_POPULATED))
704*4882a593Smuzhiyun return NOTIFY_OK;
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun /* find our device by node */
707*4882a593Smuzhiyun pdev = of_find_device_by_node(rd->dn);
708*4882a593Smuzhiyun if (pdev == NULL)
709*4882a593Smuzhiyun return NOTIFY_OK; /* no? not meant for us */
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /* unregister takes one ref away */
712*4882a593Smuzhiyun of_platform_device_destroy(&pdev->dev, &children_left);
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun /* and put the reference of the find */
715*4882a593Smuzhiyun of_dev_put(pdev);
716*4882a593Smuzhiyun break;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun return NOTIFY_OK;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun static struct notifier_block platform_of_notifier = {
723*4882a593Smuzhiyun .notifier_call = of_platform_notify,
724*4882a593Smuzhiyun };
725*4882a593Smuzhiyun
of_platform_register_reconfig_notifier(void)726*4882a593Smuzhiyun void of_platform_register_reconfig_notifier(void)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun #endif /* CONFIG_OF_DYNAMIC */
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun #endif /* CONFIG_OF_ADDRESS */
733