xref: /linux/drivers/of/platform.c (revision 29d4f8a4974aacf46b028fa92f9dd3ffdba3e614)
13f23de10SStephen Rothwell /*
23f23de10SStephen Rothwell  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
33f23de10SStephen Rothwell  *			 <benh@kernel.crashing.org>
43f23de10SStephen Rothwell  *    and		 Arnd Bergmann, IBM Corp.
53f23de10SStephen Rothwell  *    Merged from powerpc/kernel/of_platform.c and
63f23de10SStephen Rothwell  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
73f23de10SStephen Rothwell  *
83f23de10SStephen Rothwell  *  This program is free software; you can redistribute it and/or
93f23de10SStephen Rothwell  *  modify it under the terms of the GNU General Public License
103f23de10SStephen Rothwell  *  as published by the Free Software Foundation; either version
113f23de10SStephen Rothwell  *  2 of the License, or (at your option) any later version.
123f23de10SStephen Rothwell  *
133f23de10SStephen Rothwell  */
143f23de10SStephen Rothwell #include <linux/errno.h>
155c457083SStephen Rothwell #include <linux/module.h>
163f23de10SStephen Rothwell #include <linux/device.h>
175fd200f3SGrant Likely #include <linux/dma-mapping.h>
1850ef5284SGrant Likely #include <linux/slab.h>
199e3288dcSGrant Likely #include <linux/of_address.h>
203f23de10SStephen Rothwell #include <linux/of_device.h>
219e3288dcSGrant Likely #include <linux/of_irq.h>
223f23de10SStephen Rothwell #include <linux/of_platform.h>
23eca39301SGrant Likely #include <linux/platform_device.h>
24eca39301SGrant Likely 
25cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
26cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
27cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
28cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
29cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
30cbb49c26SGrant Likely 	{} /* Empty terminated list */
31cbb49c26SGrant Likely };
32cbb49c26SGrant Likely 
33c6085584SJonas Bonn static int of_dev_node_match(struct device *dev, void *data)
34c6085584SJonas Bonn {
35c6085584SJonas Bonn 	return dev->of_node == data;
36c6085584SJonas Bonn }
37c6085584SJonas Bonn 
38c6085584SJonas Bonn /**
39c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
40c6085584SJonas Bonn  * @np: Pointer to device tree node
41c6085584SJonas Bonn  *
42c6085584SJonas Bonn  * Returns platform_device pointer, or NULL if not found
43c6085584SJonas Bonn  */
44c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
45c6085584SJonas Bonn {
46c6085584SJonas Bonn 	struct device *dev;
47c6085584SJonas Bonn 
48c6085584SJonas Bonn 	dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
49c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
50c6085584SJonas Bonn }
51c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
52c6085584SJonas Bonn 
53596c955cSGrant Likely #if defined(CONFIG_PPC_DCR)
54596c955cSGrant Likely #include <asm/dcr.h>
55596c955cSGrant Likely #endif
56596c955cSGrant Likely 
575fd200f3SGrant Likely #if !defined(CONFIG_SPARC)
585fd200f3SGrant Likely /*
595fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
605fd200f3SGrant Likely  * each applicable node.
615fd200f3SGrant Likely  *
625fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
635fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
645fd200f3SGrant Likely  */
655fd200f3SGrant Likely 
665fd200f3SGrant Likely /**
6794c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
6894c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
6994c09319SGrant Likely  *
7094c09319SGrant Likely  * This routine will first try using either the dcr-reg or the reg property
7194c09319SGrant Likely  * value to derive a unique name.  As a last resort it will use the node
7294c09319SGrant Likely  * name followed by a unique number.
7394c09319SGrant Likely  */
74c6601225SGrant Likely void of_device_make_bus_id(struct device *dev)
7594c09319SGrant Likely {
7694c09319SGrant Likely 	static atomic_t bus_no_reg_magic;
7794c09319SGrant Likely 	struct device_node *node = dev->of_node;
7894c09319SGrant Likely 	const u32 *reg;
7994c09319SGrant Likely 	u64 addr;
8094c09319SGrant Likely 	int magic;
8194c09319SGrant Likely 
8294c09319SGrant Likely #ifdef CONFIG_PPC_DCR
8394c09319SGrant Likely 	/*
8494c09319SGrant Likely 	 * If it's a DCR based device, use 'd' for native DCRs
8594c09319SGrant Likely 	 * and 'D' for MMIO DCRs.
8694c09319SGrant Likely 	 */
8794c09319SGrant Likely 	reg = of_get_property(node, "dcr-reg", NULL);
8894c09319SGrant Likely 	if (reg) {
8994c09319SGrant Likely #ifdef CONFIG_PPC_DCR_NATIVE
9094c09319SGrant Likely 		dev_set_name(dev, "d%x.%s", *reg, node->name);
9194c09319SGrant Likely #else /* CONFIG_PPC_DCR_NATIVE */
9294c09319SGrant Likely 		u64 addr = of_translate_dcr_address(node, *reg, NULL);
9394c09319SGrant Likely 		if (addr != OF_BAD_ADDR) {
9494c09319SGrant Likely 			dev_set_name(dev, "D%llx.%s",
9594c09319SGrant Likely 				     (unsigned long long)addr, node->name);
9694c09319SGrant Likely 			return;
9794c09319SGrant Likely 		}
9894c09319SGrant Likely #endif /* !CONFIG_PPC_DCR_NATIVE */
9994c09319SGrant Likely 	}
10094c09319SGrant Likely #endif /* CONFIG_PPC_DCR */
10194c09319SGrant Likely 
10294c09319SGrant Likely 	/*
10394c09319SGrant Likely 	 * For MMIO, get the physical address
10494c09319SGrant Likely 	 */
10594c09319SGrant Likely 	reg = of_get_property(node, "reg", NULL);
10694c09319SGrant Likely 	if (reg) {
10794c09319SGrant Likely 		addr = of_translate_address(node, reg);
10894c09319SGrant Likely 		if (addr != OF_BAD_ADDR) {
10994c09319SGrant Likely 			dev_set_name(dev, "%llx.%s",
11094c09319SGrant Likely 				     (unsigned long long)addr, node->name);
11194c09319SGrant Likely 			return;
11294c09319SGrant Likely 		}
11394c09319SGrant Likely 	}
11494c09319SGrant Likely 
11594c09319SGrant Likely 	/*
11694c09319SGrant Likely 	 * No BusID, use the node name and add a globally incremented
11794c09319SGrant Likely 	 * counter (and pray...)
11894c09319SGrant Likely 	 */
11994c09319SGrant Likely 	magic = atomic_add_return(1, &bus_no_reg_magic);
12094c09319SGrant Likely 	dev_set_name(dev, "%s.%d", node->name, magic - 1);
12194c09319SGrant Likely }
12294c09319SGrant Likely 
12394c09319SGrant Likely /**
12494c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
12594c09319SGrant Likely  * @np: device node to assign to device
12694c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
12794c09319SGrant Likely  * @parent: Parent device.
12894c09319SGrant Likely  */
12994a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
13094c09319SGrant Likely 				  const char *bus_id,
13194c09319SGrant Likely 				  struct device *parent)
13294c09319SGrant Likely {
13394a0cb1fSGrant Likely 	struct platform_device *dev;
13452f6537cSAndres Salomon 	int rc, i, num_reg = 0, num_irq;
135ac80a51eSGrant Likely 	struct resource *res, temp_res;
13694c09319SGrant Likely 
1377096d042SGrant Likely 	dev = platform_device_alloc("", -1);
1387096d042SGrant Likely 	if (!dev)
1397096d042SGrant Likely 		return NULL;
1407096d042SGrant Likely 
1417096d042SGrant Likely 	/* count the io and irq resources */
142ac80a51eSGrant Likely 	while (of_address_to_resource(np, num_reg, &temp_res) == 0)
143ac80a51eSGrant Likely 		num_reg++;
14452f6537cSAndres Salomon 	num_irq = of_irq_count(np);
145ac80a51eSGrant Likely 
146ac80a51eSGrant Likely 	/* Populate the resource table */
147ac80a51eSGrant Likely 	if (num_irq || num_reg) {
1487096d042SGrant Likely 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
1497096d042SGrant Likely 		if (!res) {
1507096d042SGrant Likely 			platform_device_put(dev);
1517096d042SGrant Likely 			return NULL;
1527096d042SGrant Likely 		}
1537096d042SGrant Likely 
154ac80a51eSGrant Likely 		dev->num_resources = num_reg + num_irq;
155ac80a51eSGrant Likely 		dev->resource = res;
156ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
157ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
158ac80a51eSGrant Likely 			WARN_ON(rc);
159ac80a51eSGrant Likely 		}
16052f6537cSAndres Salomon 		WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
161ac80a51eSGrant Likely 	}
16294c09319SGrant Likely 
16394c09319SGrant Likely 	dev->dev.of_node = of_node_get(np);
1649e3288dcSGrant Likely #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
16594c09319SGrant Likely 	dev->dev.dma_mask = &dev->archdata.dma_mask;
1669e3288dcSGrant Likely #endif
16794c09319SGrant Likely 	dev->dev.parent = parent;
16894c09319SGrant Likely 
16994c09319SGrant Likely 	if (bus_id)
17094c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
17194c09319SGrant Likely 	else
17294c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
17394c09319SGrant Likely 
17494c09319SGrant Likely 	return dev;
17594c09319SGrant Likely }
17694c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
17794c09319SGrant Likely 
17894c09319SGrant Likely /**
1795fd200f3SGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
1805fd200f3SGrant Likely  * @np: pointer to node to create device for
1815fd200f3SGrant Likely  * @bus_id: name to assign device
1825fd200f3SGrant Likely  * @parent: Linux device model parent device.
183cd1e6504SGrant Likely  *
184cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
185cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1865fd200f3SGrant Likely  */
18794a0cb1fSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
1885fd200f3SGrant Likely 					    const char *bus_id,
1895fd200f3SGrant Likely 					    struct device *parent)
1905fd200f3SGrant Likely {
19194a0cb1fSGrant Likely 	struct platform_device *dev;
1925fd200f3SGrant Likely 
193cd1e6504SGrant Likely 	if (!of_device_is_available(np))
194cd1e6504SGrant Likely 		return NULL;
195cd1e6504SGrant Likely 
1965fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1975fd200f3SGrant Likely 	if (!dev)
1985fd200f3SGrant Likely 		return NULL;
1995fd200f3SGrant Likely 
2009e3288dcSGrant Likely #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
2015fd200f3SGrant Likely 	dev->archdata.dma_mask = 0xffffffffUL;
2029e3288dcSGrant Likely #endif
2035fd200f3SGrant Likely 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
204eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
2055fd200f3SGrant Likely 
2065fd200f3SGrant Likely 	/* We do not fill the DMA ops for platform devices by default.
2075fd200f3SGrant Likely 	 * This is currently the responsibility of the platform code
2085fd200f3SGrant Likely 	 * to do such, possibly using a device notifier
2095fd200f3SGrant Likely 	 */
2105fd200f3SGrant Likely 
2117096d042SGrant Likely 	if (of_device_add(dev) != 0) {
2127096d042SGrant Likely 		platform_device_put(dev);
2135fd200f3SGrant Likely 		return NULL;
2145fd200f3SGrant Likely 	}
2155fd200f3SGrant Likely 
2165fd200f3SGrant Likely 	return dev;
2175fd200f3SGrant Likely }
2185fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2195fd200f3SGrant Likely 
2205fd200f3SGrant Likely /**
22138e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
2225fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
2231eed4c07SGrant Likely  * @matches: match table for bus nodes
22438e9e21dSGrant Likely  * disallow recursive creation of child buses
22538e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
22638e9e21dSGrant Likely  *
22738e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
22838e9e21dSGrant Likely  * recursively create devices for all the child nodes.
2295fd200f3SGrant Likely  */
23038e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
2315fd200f3SGrant Likely 				  const struct of_device_id *matches,
232*29d4f8a4SGrant Likely 				  struct device *parent, bool strict)
2335fd200f3SGrant Likely {
2345fd200f3SGrant Likely 	struct device_node *child;
23594a0cb1fSGrant Likely 	struct platform_device *dev;
2365fd200f3SGrant Likely 	int rc = 0;
2375fd200f3SGrant Likely 
238*29d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
239*29d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
240*29d4f8a4SGrant Likely 		pr_debug("%s() - skipping %s, no compatible prop\n",
241*29d4f8a4SGrant Likely 			 __func__, bus->full_name);
242*29d4f8a4SGrant Likely 		return 0;
243*29d4f8a4SGrant Likely 	}
244*29d4f8a4SGrant Likely 
24538e9e21dSGrant Likely 	dev = of_platform_device_create(bus, NULL, parent);
24638e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
24738e9e21dSGrant Likely 		return 0;
24838e9e21dSGrant Likely 
2495fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
2505fd200f3SGrant Likely 		pr_debug("   create child: %s\n", child->full_name);
251*29d4f8a4SGrant Likely 		rc = of_platform_bus_create(child, matches, &dev->dev, strict);
2525fd200f3SGrant Likely 		if (rc) {
2535fd200f3SGrant Likely 			of_node_put(child);
2545fd200f3SGrant Likely 			break;
2555fd200f3SGrant Likely 		}
2565fd200f3SGrant Likely 	}
2575fd200f3SGrant Likely 	return rc;
2585fd200f3SGrant Likely }
2595fd200f3SGrant Likely 
2605fd200f3SGrant Likely /**
26138e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
2625fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
2631eed4c07SGrant Likely  * @matches: match table for bus nodes
2645fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
2655fd200f3SGrant Likely  *
2665fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
2675fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
2685fd200f3SGrant Likely  */
2695fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
2705fd200f3SGrant Likely 			  const struct of_device_id *matches,
2715fd200f3SGrant Likely 			  struct device *parent)
2725fd200f3SGrant Likely {
2735fd200f3SGrant Likely 	struct device_node *child;
2745fd200f3SGrant Likely 	int rc = 0;
2755fd200f3SGrant Likely 
2761eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
2771eed4c07SGrant Likely 	if (!root)
27860d59913SGrant Likely 		return -EINVAL;
2795fd200f3SGrant Likely 
2805fd200f3SGrant Likely 	pr_debug("of_platform_bus_probe()\n");
2815fd200f3SGrant Likely 	pr_debug(" starting at: %s\n", root->full_name);
2825fd200f3SGrant Likely 
2831eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
2845fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
285*29d4f8a4SGrant Likely 		rc = of_platform_bus_create(root, matches, parent, false);
28638e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
2875fd200f3SGrant Likely 		if (!of_match_node(matches, child))
2885fd200f3SGrant Likely 			continue;
289*29d4f8a4SGrant Likely 		rc = of_platform_bus_create(child, matches, parent, false);
29038e9e21dSGrant Likely 		if (rc)
2915fd200f3SGrant Likely 			break;
2925fd200f3SGrant Likely 	}
29338e9e21dSGrant Likely 
2945fd200f3SGrant Likely 	of_node_put(root);
2955fd200f3SGrant Likely 	return rc;
2965fd200f3SGrant Likely }
2975fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
298*29d4f8a4SGrant Likely 
299*29d4f8a4SGrant Likely /**
300*29d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
301*29d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
302*29d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
303*29d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
304*29d4f8a4SGrant Likely  *
305*29d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
306*29d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
307*29d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
308*29d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
309*29d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
310*29d4f8a4SGrant Likely  * are selected by the @matches argument).
311*29d4f8a4SGrant Likely  *
312*29d4f8a4SGrant Likely  * New board support should be using this function instead of
313*29d4f8a4SGrant Likely  * of_platform_bus_probe().
314*29d4f8a4SGrant Likely  *
315*29d4f8a4SGrant Likely  * Returns 0 on success, < 0 on failure.
316*29d4f8a4SGrant Likely  */
317*29d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
318*29d4f8a4SGrant Likely 			const struct of_device_id *matches,
319*29d4f8a4SGrant Likely 			struct device *parent)
320*29d4f8a4SGrant Likely {
321*29d4f8a4SGrant Likely 	struct device_node *child;
322*29d4f8a4SGrant Likely 	int rc = 0;
323*29d4f8a4SGrant Likely 
324*29d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
325*29d4f8a4SGrant Likely 	if (!root)
326*29d4f8a4SGrant Likely 		return -EINVAL;
327*29d4f8a4SGrant Likely 
328*29d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
329*29d4f8a4SGrant Likely 		rc = of_platform_bus_create(child, matches, parent, true);
330*29d4f8a4SGrant Likely 		if (rc)
331*29d4f8a4SGrant Likely 			break;
332*29d4f8a4SGrant Likely 	}
333*29d4f8a4SGrant Likely 
334*29d4f8a4SGrant Likely 	of_node_put(root);
335*29d4f8a4SGrant Likely 	return rc;
336*29d4f8a4SGrant Likely }
3375fd200f3SGrant Likely #endif /* !CONFIG_SPARC */
338