xref: /openbmc/linux/drivers/of/platform.c (revision 84d517f3)
1 /*
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>
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>
25 
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 */
32 };
33 
34 static int of_dev_node_match(struct device *dev, void *data)
35 {
36 	return dev->of_node == data;
37 }
38 
39 /**
40  * of_find_device_by_node - Find the platform_device associated with a node
41  * @np: Pointer to device tree node
42  *
43  * Returns platform_device pointer, or NULL if not found
44  */
45 struct platform_device *of_find_device_by_node(struct device_node *np)
46 {
47 	struct device *dev;
48 
49 	dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
50 	return dev ? to_platform_device(dev) : NULL;
51 }
52 EXPORT_SYMBOL(of_find_device_by_node);
53 
54 #ifdef CONFIG_OF_ADDRESS
55 /*
56  * The following routines scan a subtree and registers a device for
57  * each applicable node.
58  *
59  * Note: sparc doesn't use these routines because it has a different
60  * mechanism for creating devices from device tree nodes.
61  */
62 
63 /**
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
66  *
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.
70  */
71 void of_device_make_bus_id(struct device *dev)
72 {
73 	struct device_node *node = dev->of_node;
74 	const __be32 *reg;
75 	u64 addr;
76 
77 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
78 	while (node->parent) {
79 		/*
80 		 * If the address can be translated, then that is as much
81 		 * uniqueness as we need. Make it the first component and return
82 		 */
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,
87 				     dev_name(dev));
88 			return;
89 		}
90 
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));
94 		node = node->parent;
95 	}
96 }
97 
98 /**
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.
103  */
104 struct platform_device *of_device_alloc(struct device_node *np,
105 				  const char *bus_id,
106 				  struct device *parent)
107 {
108 	struct platform_device *dev;
109 	int rc, i, num_reg = 0, num_irq;
110 	struct resource *res, temp_res;
111 
112 	dev = platform_device_alloc("", -1);
113 	if (!dev)
114 		return NULL;
115 
116 	/* count the io and irq resources */
117 	while (of_address_to_resource(np, num_reg, &temp_res) == 0)
118 		num_reg++;
119 	num_irq = of_irq_count(np);
120 
121 	/* Populate the resource table */
122 	if (num_irq || num_reg) {
123 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
124 		if (!res) {
125 			platform_device_put(dev);
126 			return NULL;
127 		}
128 
129 		dev->num_resources = num_reg + num_irq;
130 		dev->resource = res;
131 		for (i = 0; i < num_reg; i++, res++) {
132 			rc = of_address_to_resource(np, i, res);
133 			WARN_ON(rc);
134 		}
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",
137 				 np->name);
138 	}
139 
140 	dev->dev.of_node = of_node_get(np);
141 #if defined(CONFIG_MICROBLAZE)
142 	dev->dev.dma_mask = &dev->archdata.dma_mask;
143 #endif
144 	dev->dev.parent = parent;
145 
146 	if (bus_id)
147 		dev_set_name(&dev->dev, "%s", bus_id);
148 	else
149 		of_device_make_bus_id(&dev->dev);
150 
151 	return dev;
152 }
153 EXPORT_SYMBOL(of_device_alloc);
154 
155 /**
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.
161  *
162  * Returns pointer to created platform device, or NULL if a device was not
163  * registered.  Unavailable devices will not get registered.
164  */
165 static struct platform_device *of_platform_device_create_pdata(
166 					struct device_node *np,
167 					const char *bus_id,
168 					void *platform_data,
169 					struct device *parent)
170 {
171 	struct platform_device *dev;
172 
173 	if (!of_device_is_available(np) ||
174 	    of_node_test_and_set_flag(np, OF_POPULATED))
175 		return NULL;
176 
177 	dev = of_device_alloc(np, bus_id, parent);
178 	if (!dev)
179 		goto err_clear_flag;
180 
181 #if defined(CONFIG_MICROBLAZE)
182 	dev->archdata.dma_mask = 0xffffffffUL;
183 #endif
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;
189 
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
193 	 */
194 
195 	if (of_device_add(dev) != 0) {
196 		platform_device_put(dev);
197 		goto err_clear_flag;
198 	}
199 
200 	return dev;
201 
202 err_clear_flag:
203 	of_node_clear_flag(np, OF_POPULATED);
204 	return NULL;
205 }
206 
207 /**
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.
212  *
213  * Returns pointer to created platform device, or NULL if a device was not
214  * registered.  Unavailable devices will not get registered.
215  */
216 struct platform_device *of_platform_device_create(struct device_node *np,
217 					    const char *bus_id,
218 					    struct device *parent)
219 {
220 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
221 }
222 EXPORT_SYMBOL(of_platform_device_create);
223 
224 #ifdef CONFIG_ARM_AMBA
225 static struct amba_device *of_amba_device_create(struct device_node *node,
226 						 const char *bus_id,
227 						 void *platform_data,
228 						 struct device *parent)
229 {
230 	struct amba_device *dev;
231 	const void *prop;
232 	int i, ret;
233 
234 	pr_debug("Creating amba device %s\n", node->full_name);
235 
236 	if (!of_device_is_available(node) ||
237 	    of_node_test_and_set_flag(node, OF_POPULATED))
238 		return NULL;
239 
240 	dev = amba_device_alloc(NULL, 0, 0);
241 	if (!dev) {
242 		pr_err("%s(): amba_device_alloc() failed for %s\n",
243 		       __func__, node->full_name);
244 		goto err_clear_flag;
245 	}
246 
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;
252 	if (bus_id)
253 		dev_set_name(&dev->dev, "%s", bus_id);
254 	else
255 		of_device_make_bus_id(&dev->dev);
256 
257 	/* Allow the HW Peripheral ID to be overridden */
258 	prop = of_get_property(node, "arm,primecell-periphid", NULL);
259 	if (prop)
260 		dev->periphid = of_read_ulong(prop, 1);
261 
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);
265 
266 	ret = of_address_to_resource(node, 0, &dev->res);
267 	if (ret) {
268 		pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
269 		       __func__, ret, node->full_name);
270 		goto err_free;
271 	}
272 
273 	ret = amba_device_add(dev, &iomem_resource);
274 	if (ret) {
275 		pr_err("%s(): amba_device_add() failed (%d) for %s\n",
276 		       __func__, ret, node->full_name);
277 		goto err_free;
278 	}
279 
280 	return dev;
281 
282 err_free:
283 	amba_device_put(dev);
284 err_clear_flag:
285 	of_node_clear_flag(node, OF_POPULATED);
286 	return NULL;
287 }
288 #else /* CONFIG_ARM_AMBA */
289 static struct amba_device *of_amba_device_create(struct device_node *node,
290 						 const char *bus_id,
291 						 void *platform_data,
292 						 struct device *parent)
293 {
294 	return NULL;
295 }
296 #endif /* CONFIG_ARM_AMBA */
297 
298 /**
299  * of_devname_lookup() - Given a device node, lookup the preferred Linux name
300  */
301 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
302 				 struct device_node *np)
303 {
304 	struct resource res;
305 
306 	if (!lookup)
307 		return NULL;
308 
309 	for(; lookup->compatible != NULL; lookup++) {
310 		if (!of_device_is_compatible(np, lookup->compatible))
311 			continue;
312 		if (!of_address_to_resource(np, 0, &res))
313 			if (res.start != lookup->phys_addr)
314 				continue;
315 		pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
316 		return lookup;
317 	}
318 
319 	return NULL;
320 }
321 
322 /**
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
329  *
330  * Creates a platform_device for the provided device_node, and optionally
331  * recursively create devices for all the child nodes.
332  */
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)
337 {
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;
343 	int rc = 0;
344 
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);
349 		return 0;
350 	}
351 
352 	auxdata = of_dev_lookup(lookup, bus);
353 	if (auxdata) {
354 		bus_id = auxdata->name;
355 		platform_data = auxdata->platform_data;
356 	}
357 
358 	if (of_device_is_compatible(bus, "arm,primecell")) {
359 		/*
360 		 * Don't return an error here to keep compatibility with older
361 		 * device tree files.
362 		 */
363 		of_amba_device_create(bus, bus_id, platform_data, parent);
364 		return 0;
365 	}
366 
367 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
368 	if (!dev || !of_match_node(matches, bus))
369 		return 0;
370 
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);
374 		if (rc) {
375 			of_node_put(child);
376 			break;
377 		}
378 	}
379 	return rc;
380 }
381 
382 /**
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
387  *
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.
390  */
391 int of_platform_bus_probe(struct device_node *root,
392 			  const struct of_device_id *matches,
393 			  struct device *parent)
394 {
395 	struct device_node *child;
396 	int rc = 0;
397 
398 	root = root ? of_node_get(root) : of_find_node_by_path("/");
399 	if (!root)
400 		return -EINVAL;
401 
402 	pr_debug("of_platform_bus_probe()\n");
403 	pr_debug(" starting at: %s\n", root->full_name);
404 
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))
410 			continue;
411 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
412 		if (rc)
413 			break;
414 	}
415 
416 	of_node_put(root);
417 	return rc;
418 }
419 EXPORT_SYMBOL(of_platform_bus_probe);
420 
421 /**
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
427  *
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).
434  *
435  * New board support should be using this function instead of
436  * of_platform_bus_probe().
437  *
438  * Returns 0 on success, < 0 on failure.
439  */
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)
444 {
445 	struct device_node *child;
446 	int rc = 0;
447 
448 	root = root ? of_node_get(root) : of_find_node_by_path("/");
449 	if (!root)
450 		return -EINVAL;
451 
452 	for_each_child_of_node(root, child) {
453 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
454 		if (rc)
455 			break;
456 	}
457 
458 	of_node_put(root);
459 	return rc;
460 }
461 EXPORT_SYMBOL_GPL(of_platform_populate);
462 
463 static int of_platform_device_destroy(struct device *dev, void *data)
464 {
465 	bool *children_left = data;
466 
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;
470 		return 0;
471 	}
472 
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;
476 		return 0;
477 	}
478 
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));
484 #endif
485 	else {
486 		*children_left = true;
487 		return 0;
488 	}
489 
490 	of_node_clear_flag(dev->of_node, OF_POPULATED);
491 
492 	return 0;
493 }
494 
495 /**
496  * of_platform_depopulate() - Remove devices populated from device tree
497  * @parent: device which childred will be removed
498  *
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).
503  *
504  * Returns 0 when all children devices have been removed or
505  * -EBUSY when some children remained.
506  */
507 int of_platform_depopulate(struct device *parent)
508 {
509 	bool children_left = false;
510 
511 	device_for_each_child(parent, &children_left,
512 			      of_platform_device_destroy);
513 
514 	return children_left ? -EBUSY : 0;
515 }
516 EXPORT_SYMBOL_GPL(of_platform_depopulate);
517 
518 #endif /* CONFIG_OF_ADDRESS */
519