xref: /openbmc/u-boot/drivers/core/of_access.c (revision f94fa0e94f36c740d3c7aa314c89a750c742185b)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2644ec0a9SSimon Glass /*
3644ec0a9SSimon Glass  * Originally from Linux v4.9
4644ec0a9SSimon Glass  * Paul Mackerras	August 1996.
5644ec0a9SSimon Glass  * Copyright (C) 1996-2005 Paul Mackerras.
6644ec0a9SSimon Glass  *
7644ec0a9SSimon Glass  * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8644ec0a9SSimon Glass  *   {engebret|bergner}@us.ibm.com
9644ec0a9SSimon Glass  *
10644ec0a9SSimon Glass  * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11644ec0a9SSimon Glass  *
12644ec0a9SSimon Glass  * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13644ec0a9SSimon Glass  * Grant Likely.
14644ec0a9SSimon Glass  *
15644ec0a9SSimon Glass  * Modified for U-Boot
16644ec0a9SSimon Glass  * Copyright (c) 2017 Google, Inc
17644ec0a9SSimon Glass  *
18644ec0a9SSimon Glass  * This file follows drivers/of/base.c with functions in the same order as the
19644ec0a9SSimon Glass  * Linux version.
20644ec0a9SSimon Glass  */
21644ec0a9SSimon Glass 
22644ec0a9SSimon Glass #include <common.h>
23b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
24644ec0a9SSimon Glass #include <dm/of_access.h>
25644ec0a9SSimon Glass #include <linux/ctype.h>
26644ec0a9SSimon Glass #include <linux/err.h>
27644ec0a9SSimon Glass #include <linux/ioport.h>
28644ec0a9SSimon Glass 
29644ec0a9SSimon Glass DECLARE_GLOBAL_DATA_PTR;
30644ec0a9SSimon Glass 
31644ec0a9SSimon Glass /* list of struct alias_prop aliases */
32644ec0a9SSimon Glass LIST_HEAD(aliases_lookup);
33644ec0a9SSimon Glass 
34644ec0a9SSimon Glass /* "/aliaes" node */
35644ec0a9SSimon Glass static struct device_node *of_aliases;
36644ec0a9SSimon Glass 
37644ec0a9SSimon Glass /* "/chosen" node */
38644ec0a9SSimon Glass static struct device_node *of_chosen;
39644ec0a9SSimon Glass 
40644ec0a9SSimon Glass /* node pointed to by the stdout-path alias */
41644ec0a9SSimon Glass static struct device_node *of_stdout;
42644ec0a9SSimon Glass 
43644ec0a9SSimon Glass /* pointer to options given after the alias (separated by :) or NULL if none */
44644ec0a9SSimon Glass static const char *of_stdout_options;
45644ec0a9SSimon Glass 
46644ec0a9SSimon Glass /**
47644ec0a9SSimon Glass  * struct alias_prop - Alias property in 'aliases' node
48644ec0a9SSimon Glass  *
49644ec0a9SSimon Glass  * The structure represents one alias property of 'aliases' node as
50644ec0a9SSimon Glass  * an entry in aliases_lookup list.
51644ec0a9SSimon Glass  *
52644ec0a9SSimon Glass  * @link:	List node to link the structure in aliases_lookup list
53644ec0a9SSimon Glass  * @alias:	Alias property name
54644ec0a9SSimon Glass  * @np:		Pointer to device_node that the alias stands for
55644ec0a9SSimon Glass  * @id:		Index value from end of alias name
56644ec0a9SSimon Glass  * @stem:	Alias string without the index
57644ec0a9SSimon Glass  */
58644ec0a9SSimon Glass struct alias_prop {
59644ec0a9SSimon Glass 	struct list_head link;
60644ec0a9SSimon Glass 	const char *alias;
61644ec0a9SSimon Glass 	struct device_node *np;
62644ec0a9SSimon Glass 	int id;
63644ec0a9SSimon Glass 	char stem[0];
64644ec0a9SSimon Glass };
65644ec0a9SSimon Glass 
of_n_addr_cells(const struct device_node * np)66644ec0a9SSimon Glass int of_n_addr_cells(const struct device_node *np)
67644ec0a9SSimon Glass {
68644ec0a9SSimon Glass 	const __be32 *ip;
69644ec0a9SSimon Glass 
70644ec0a9SSimon Glass 	do {
71644ec0a9SSimon Glass 		if (np->parent)
72644ec0a9SSimon Glass 			np = np->parent;
73644ec0a9SSimon Glass 		ip = of_get_property(np, "#address-cells", NULL);
74644ec0a9SSimon Glass 		if (ip)
75644ec0a9SSimon Glass 			return be32_to_cpup(ip);
76644ec0a9SSimon Glass 	} while (np->parent);
77644ec0a9SSimon Glass 
78644ec0a9SSimon Glass 	/* No #address-cells property for the root node */
79644ec0a9SSimon Glass 	return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
80644ec0a9SSimon Glass }
81644ec0a9SSimon Glass 
of_n_size_cells(const struct device_node * np)82644ec0a9SSimon Glass int of_n_size_cells(const struct device_node *np)
83644ec0a9SSimon Glass {
84644ec0a9SSimon Glass 	const __be32 *ip;
85644ec0a9SSimon Glass 
86644ec0a9SSimon Glass 	do {
87644ec0a9SSimon Glass 		if (np->parent)
88644ec0a9SSimon Glass 			np = np->parent;
89644ec0a9SSimon Glass 		ip = of_get_property(np, "#size-cells", NULL);
90644ec0a9SSimon Glass 		if (ip)
91644ec0a9SSimon Glass 			return be32_to_cpup(ip);
92644ec0a9SSimon Glass 	} while (np->parent);
93644ec0a9SSimon Glass 
94644ec0a9SSimon Glass 	/* No #size-cells property for the root node */
95644ec0a9SSimon Glass 	return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
96644ec0a9SSimon Glass }
97644ec0a9SSimon Glass 
of_simple_addr_cells(const struct device_node * np)98878d68c0SSimon Glass int of_simple_addr_cells(const struct device_node *np)
99878d68c0SSimon Glass {
100878d68c0SSimon Glass 	const __be32 *ip;
101878d68c0SSimon Glass 
102878d68c0SSimon Glass 	ip = of_get_property(np, "#address-cells", NULL);
103878d68c0SSimon Glass 	if (ip)
104878d68c0SSimon Glass 		return be32_to_cpup(ip);
105878d68c0SSimon Glass 
106878d68c0SSimon Glass 	/* Return a default of 2 to match fdt_address_cells()*/
107878d68c0SSimon Glass 	return 2;
108878d68c0SSimon Glass }
109878d68c0SSimon Glass 
of_simple_size_cells(const struct device_node * np)110878d68c0SSimon Glass int of_simple_size_cells(const struct device_node *np)
111878d68c0SSimon Glass {
112878d68c0SSimon Glass 	const __be32 *ip;
113878d68c0SSimon Glass 
114878d68c0SSimon Glass 	ip = of_get_property(np, "#size-cells", NULL);
115878d68c0SSimon Glass 	if (ip)
116878d68c0SSimon Glass 		return be32_to_cpup(ip);
117878d68c0SSimon Glass 
118878d68c0SSimon Glass 	/* Return a default of 2 to match fdt_size_cells()*/
119878d68c0SSimon Glass 	return 2;
120878d68c0SSimon Glass }
121878d68c0SSimon Glass 
of_find_property(const struct device_node * np,const char * name,int * lenp)122644ec0a9SSimon Glass struct property *of_find_property(const struct device_node *np,
123644ec0a9SSimon Glass 				  const char *name, int *lenp)
124644ec0a9SSimon Glass {
125644ec0a9SSimon Glass 	struct property *pp;
126644ec0a9SSimon Glass 
127644ec0a9SSimon Glass 	if (!np)
128644ec0a9SSimon Glass 		return NULL;
129644ec0a9SSimon Glass 
130644ec0a9SSimon Glass 	for (pp = np->properties; pp; pp = pp->next) {
131644ec0a9SSimon Glass 		if (strcmp(pp->name, name) == 0) {
132644ec0a9SSimon Glass 			if (lenp)
133644ec0a9SSimon Glass 				*lenp = pp->length;
134644ec0a9SSimon Glass 			break;
135644ec0a9SSimon Glass 		}
136644ec0a9SSimon Glass 	}
137644ec0a9SSimon Glass 	if (!pp && lenp)
138644ec0a9SSimon Glass 		*lenp = -FDT_ERR_NOTFOUND;
139644ec0a9SSimon Glass 
140644ec0a9SSimon Glass 	return pp;
141644ec0a9SSimon Glass }
142644ec0a9SSimon Glass 
of_find_all_nodes(struct device_node * prev)143644ec0a9SSimon Glass struct device_node *of_find_all_nodes(struct device_node *prev)
144644ec0a9SSimon Glass {
145644ec0a9SSimon Glass 	struct device_node *np;
146644ec0a9SSimon Glass 
147644ec0a9SSimon Glass 	if (!prev) {
148644ec0a9SSimon Glass 		np = gd->of_root;
149644ec0a9SSimon Glass 	} else if (prev->child) {
150644ec0a9SSimon Glass 		np = prev->child;
151644ec0a9SSimon Glass 	} else {
152644ec0a9SSimon Glass 		/*
153644ec0a9SSimon Glass 		 * Walk back up looking for a sibling, or the end of the
154644ec0a9SSimon Glass 		 * structure
155644ec0a9SSimon Glass 		 */
156644ec0a9SSimon Glass 		np = prev;
157644ec0a9SSimon Glass 		while (np->parent && !np->sibling)
158644ec0a9SSimon Glass 			np = np->parent;
159644ec0a9SSimon Glass 		np = np->sibling; /* Might be null at the end of the tree */
160644ec0a9SSimon Glass 	}
161644ec0a9SSimon Glass 
162644ec0a9SSimon Glass 	return np;
163644ec0a9SSimon Glass }
164644ec0a9SSimon Glass 
of_get_property(const struct device_node * np,const char * name,int * lenp)165644ec0a9SSimon Glass const void *of_get_property(const struct device_node *np, const char *name,
166644ec0a9SSimon Glass 			    int *lenp)
167644ec0a9SSimon Glass {
168644ec0a9SSimon Glass 	struct property *pp = of_find_property(np, name, lenp);
169644ec0a9SSimon Glass 
170644ec0a9SSimon Glass 	return pp ? pp->value : NULL;
171644ec0a9SSimon Glass }
172644ec0a9SSimon Glass 
of_prop_next_string(struct property * prop,const char * cur)173644ec0a9SSimon Glass static const char *of_prop_next_string(struct property *prop, const char *cur)
174644ec0a9SSimon Glass {
175644ec0a9SSimon Glass 	const void *curv = cur;
176644ec0a9SSimon Glass 
177644ec0a9SSimon Glass 	if (!prop)
178644ec0a9SSimon Glass 		return NULL;
179644ec0a9SSimon Glass 
180644ec0a9SSimon Glass 	if (!cur)
181644ec0a9SSimon Glass 		return prop->value;
182644ec0a9SSimon Glass 
183644ec0a9SSimon Glass 	curv += strlen(cur) + 1;
184644ec0a9SSimon Glass 	if (curv >= prop->value + prop->length)
185644ec0a9SSimon Glass 		return NULL;
186644ec0a9SSimon Glass 
187644ec0a9SSimon Glass 	return curv;
188644ec0a9SSimon Glass }
189644ec0a9SSimon Glass 
of_device_is_compatible(const struct device_node * device,const char * compat,const char * type,const char * name)190644ec0a9SSimon Glass int of_device_is_compatible(const struct device_node *device,
191644ec0a9SSimon Glass 			    const char *compat, const char *type,
192644ec0a9SSimon Glass 			    const char *name)
193644ec0a9SSimon Glass {
194644ec0a9SSimon Glass 	struct property *prop;
195644ec0a9SSimon Glass 	const char *cp;
196644ec0a9SSimon Glass 	int index = 0, score = 0;
197644ec0a9SSimon Glass 
198644ec0a9SSimon Glass 	/* Compatible match has highest priority */
199644ec0a9SSimon Glass 	if (compat && compat[0]) {
200644ec0a9SSimon Glass 		prop = of_find_property(device, "compatible", NULL);
201644ec0a9SSimon Glass 		for (cp = of_prop_next_string(prop, NULL); cp;
202644ec0a9SSimon Glass 		     cp = of_prop_next_string(prop, cp), index++) {
203644ec0a9SSimon Glass 			if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
204644ec0a9SSimon Glass 				score = INT_MAX/2 - (index << 2);
205644ec0a9SSimon Glass 				break;
206644ec0a9SSimon Glass 			}
207644ec0a9SSimon Glass 		}
208644ec0a9SSimon Glass 		if (!score)
209644ec0a9SSimon Glass 			return 0;
210644ec0a9SSimon Glass 	}
211644ec0a9SSimon Glass 
212644ec0a9SSimon Glass 	/* Matching type is better than matching name */
213644ec0a9SSimon Glass 	if (type && type[0]) {
214644ec0a9SSimon Glass 		if (!device->type || of_node_cmp(type, device->type))
215644ec0a9SSimon Glass 			return 0;
216644ec0a9SSimon Glass 		score += 2;
217644ec0a9SSimon Glass 	}
218644ec0a9SSimon Glass 
219644ec0a9SSimon Glass 	/* Matching name is a bit better than not */
220644ec0a9SSimon Glass 	if (name && name[0]) {
221644ec0a9SSimon Glass 		if (!device->name || of_node_cmp(name, device->name))
222644ec0a9SSimon Glass 			return 0;
223644ec0a9SSimon Glass 		score++;
224644ec0a9SSimon Glass 	}
225644ec0a9SSimon Glass 
226644ec0a9SSimon Glass 	return score;
227644ec0a9SSimon Glass }
228644ec0a9SSimon Glass 
of_device_is_available(const struct device_node * device)229644ec0a9SSimon Glass bool of_device_is_available(const struct device_node *device)
230644ec0a9SSimon Glass {
231644ec0a9SSimon Glass 	const char *status;
232644ec0a9SSimon Glass 	int statlen;
233644ec0a9SSimon Glass 
234644ec0a9SSimon Glass 	if (!device)
235644ec0a9SSimon Glass 		return false;
236644ec0a9SSimon Glass 
237644ec0a9SSimon Glass 	status = of_get_property(device, "status", &statlen);
238644ec0a9SSimon Glass 	if (status == NULL)
239644ec0a9SSimon Glass 		return true;
240644ec0a9SSimon Glass 
241644ec0a9SSimon Glass 	if (statlen > 0) {
242644ec0a9SSimon Glass 		if (!strcmp(status, "okay"))
243644ec0a9SSimon Glass 			return true;
244644ec0a9SSimon Glass 	}
245644ec0a9SSimon Glass 
246644ec0a9SSimon Glass 	return false;
247644ec0a9SSimon Glass }
248644ec0a9SSimon Glass 
of_get_parent(const struct device_node * node)249644ec0a9SSimon Glass struct device_node *of_get_parent(const struct device_node *node)
250644ec0a9SSimon Glass {
251644ec0a9SSimon Glass 	const struct device_node *np;
252644ec0a9SSimon Glass 
253644ec0a9SSimon Glass 	if (!node)
254644ec0a9SSimon Glass 		return NULL;
255644ec0a9SSimon Glass 
256644ec0a9SSimon Glass 	np = of_node_get(node->parent);
257644ec0a9SSimon Glass 
258644ec0a9SSimon Glass 	return (struct device_node *)np;
259644ec0a9SSimon Glass }
260644ec0a9SSimon Glass 
__of_get_next_child(const struct device_node * node,struct device_node * prev)261644ec0a9SSimon Glass static struct device_node *__of_get_next_child(const struct device_node *node,
262644ec0a9SSimon Glass 					       struct device_node *prev)
263644ec0a9SSimon Glass {
264644ec0a9SSimon Glass 	struct device_node *next;
265644ec0a9SSimon Glass 
266644ec0a9SSimon Glass 	if (!node)
267644ec0a9SSimon Glass 		return NULL;
268644ec0a9SSimon Glass 
269644ec0a9SSimon Glass 	next = prev ? prev->sibling : node->child;
27073027a85SSimon Glass 	/*
27173027a85SSimon Glass 	 * coverity[dead_error_line : FALSE]
27273027a85SSimon Glass 	 * Dead code here since our current implementation of of_node_get()
27373027a85SSimon Glass 	 * always returns NULL (Coverity CID 163245). But we leave it as is
27473027a85SSimon Glass 	 * since we may want to implement get/put later.
27573027a85SSimon Glass 	 */
276644ec0a9SSimon Glass 	for (; next; next = next->sibling)
277644ec0a9SSimon Glass 		if (of_node_get(next))
278644ec0a9SSimon Glass 			break;
279644ec0a9SSimon Glass 	of_node_put(prev);
280644ec0a9SSimon Glass 	return next;
281644ec0a9SSimon Glass }
282644ec0a9SSimon Glass 
283644ec0a9SSimon Glass #define __for_each_child_of_node(parent, child) \
284644ec0a9SSimon Glass 	for (child = __of_get_next_child(parent, NULL); child != NULL; \
285644ec0a9SSimon Glass 	     child = __of_get_next_child(parent, child))
286644ec0a9SSimon Glass 
__of_find_node_by_path(struct device_node * parent,const char * path)287644ec0a9SSimon Glass static struct device_node *__of_find_node_by_path(struct device_node *parent,
288644ec0a9SSimon Glass 						  const char *path)
289644ec0a9SSimon Glass {
290644ec0a9SSimon Glass 	struct device_node *child;
291644ec0a9SSimon Glass 	int len;
292644ec0a9SSimon Glass 
293644ec0a9SSimon Glass 	len = strcspn(path, "/:");
294644ec0a9SSimon Glass 	if (!len)
295644ec0a9SSimon Glass 		return NULL;
296644ec0a9SSimon Glass 
297644ec0a9SSimon Glass 	__for_each_child_of_node(parent, child) {
298644ec0a9SSimon Glass 		const char *name = strrchr(child->full_name, '/');
299644ec0a9SSimon Glass 
300644ec0a9SSimon Glass 		name++;
301644ec0a9SSimon Glass 		if (strncmp(path, name, len) == 0 && (strlen(name) == len))
302644ec0a9SSimon Glass 			return child;
303644ec0a9SSimon Glass 	}
304644ec0a9SSimon Glass 	return NULL;
305644ec0a9SSimon Glass }
306644ec0a9SSimon Glass 
307644ec0a9SSimon Glass #define for_each_property_of_node(dn, pp) \
308644ec0a9SSimon Glass 	for (pp = dn->properties; pp != NULL; pp = pp->next)
309644ec0a9SSimon Glass 
of_find_node_opts_by_path(const char * path,const char ** opts)310644ec0a9SSimon Glass struct device_node *of_find_node_opts_by_path(const char *path,
311644ec0a9SSimon Glass 					      const char **opts)
312644ec0a9SSimon Glass {
313644ec0a9SSimon Glass 	struct device_node *np = NULL;
314644ec0a9SSimon Glass 	struct property *pp;
315644ec0a9SSimon Glass 	const char *separator = strchr(path, ':');
316644ec0a9SSimon Glass 
317644ec0a9SSimon Glass 	if (opts)
318644ec0a9SSimon Glass 		*opts = separator ? separator + 1 : NULL;
319644ec0a9SSimon Glass 
320644ec0a9SSimon Glass 	if (strcmp(path, "/") == 0)
321644ec0a9SSimon Glass 		return of_node_get(gd->of_root);
322644ec0a9SSimon Glass 
323644ec0a9SSimon Glass 	/* The path could begin with an alias */
324644ec0a9SSimon Glass 	if (*path != '/') {
325644ec0a9SSimon Glass 		int len;
326644ec0a9SSimon Glass 		const char *p = separator;
327644ec0a9SSimon Glass 
328644ec0a9SSimon Glass 		if (!p)
329644ec0a9SSimon Glass 			p = strchrnul(path, '/');
330644ec0a9SSimon Glass 		len = p - path;
331644ec0a9SSimon Glass 
332644ec0a9SSimon Glass 		/* of_aliases must not be NULL */
333644ec0a9SSimon Glass 		if (!of_aliases)
334644ec0a9SSimon Glass 			return NULL;
335644ec0a9SSimon Glass 
336644ec0a9SSimon Glass 		for_each_property_of_node(of_aliases, pp) {
337644ec0a9SSimon Glass 			if (strlen(pp->name) == len && !strncmp(pp->name, path,
338644ec0a9SSimon Glass 								len)) {
339644ec0a9SSimon Glass 				np = of_find_node_by_path(pp->value);
340644ec0a9SSimon Glass 				break;
341644ec0a9SSimon Glass 			}
342644ec0a9SSimon Glass 		}
343644ec0a9SSimon Glass 		if (!np)
344644ec0a9SSimon Glass 			return NULL;
345644ec0a9SSimon Glass 		path = p;
346644ec0a9SSimon Glass 	}
347644ec0a9SSimon Glass 
348644ec0a9SSimon Glass 	/* Step down the tree matching path components */
349644ec0a9SSimon Glass 	if (!np)
350644ec0a9SSimon Glass 		np = of_node_get(gd->of_root);
351644ec0a9SSimon Glass 	while (np && *path == '/') {
352644ec0a9SSimon Glass 		struct device_node *tmp = np;
353644ec0a9SSimon Glass 
354644ec0a9SSimon Glass 		path++; /* Increment past '/' delimiter */
355644ec0a9SSimon Glass 		np = __of_find_node_by_path(np, path);
356644ec0a9SSimon Glass 		of_node_put(tmp);
357644ec0a9SSimon Glass 		path = strchrnul(path, '/');
358644ec0a9SSimon Glass 		if (separator && separator < path)
359644ec0a9SSimon Glass 			break;
360644ec0a9SSimon Glass 	}
361644ec0a9SSimon Glass 
362644ec0a9SSimon Glass 	return np;
363644ec0a9SSimon Glass }
364644ec0a9SSimon Glass 
of_find_compatible_node(struct device_node * from,const char * type,const char * compatible)365644ec0a9SSimon Glass struct device_node *of_find_compatible_node(struct device_node *from,
366644ec0a9SSimon Glass 		const char *type, const char *compatible)
367644ec0a9SSimon Glass {
368644ec0a9SSimon Glass 	struct device_node *np;
369644ec0a9SSimon Glass 
370644ec0a9SSimon Glass 	for_each_of_allnodes_from(from, np)
371644ec0a9SSimon Glass 		if (of_device_is_compatible(np, compatible, type, NULL) &&
372644ec0a9SSimon Glass 		    of_node_get(np))
373644ec0a9SSimon Glass 			break;
374644ec0a9SSimon Glass 	of_node_put(from);
375644ec0a9SSimon Glass 
376644ec0a9SSimon Glass 	return np;
377644ec0a9SSimon Glass }
378644ec0a9SSimon Glass 
of_device_has_prop_value(const struct device_node * device,const char * propname,const void * propval,int proplen)37961fba0faSJens Wiklander static int of_device_has_prop_value(const struct device_node *device,
38061fba0faSJens Wiklander 				    const char *propname, const void *propval,
38161fba0faSJens Wiklander 				    int proplen)
38261fba0faSJens Wiklander {
38361fba0faSJens Wiklander 	struct property *prop = of_find_property(device, propname, NULL);
38461fba0faSJens Wiklander 
38561fba0faSJens Wiklander 	if (!prop || !prop->value || prop->length != proplen)
38661fba0faSJens Wiklander 		return 0;
38761fba0faSJens Wiklander 	return !memcmp(prop->value, propval, proplen);
38861fba0faSJens Wiklander }
38961fba0faSJens Wiklander 
of_find_node_by_prop_value(struct device_node * from,const char * propname,const void * propval,int proplen)39061fba0faSJens Wiklander struct device_node *of_find_node_by_prop_value(struct device_node *from,
39161fba0faSJens Wiklander 					       const char *propname,
39261fba0faSJens Wiklander 					       const void *propval, int proplen)
39361fba0faSJens Wiklander {
39461fba0faSJens Wiklander 	struct device_node *np;
39561fba0faSJens Wiklander 
39661fba0faSJens Wiklander 	for_each_of_allnodes_from(from, np) {
39761fba0faSJens Wiklander 		if (of_device_has_prop_value(np, propname, propval, proplen) &&
39861fba0faSJens Wiklander 		    of_node_get(np))
39961fba0faSJens Wiklander 			break;
40061fba0faSJens Wiklander 	}
40161fba0faSJens Wiklander 	of_node_put(from);
40261fba0faSJens Wiklander 
40361fba0faSJens Wiklander 	return np;
40461fba0faSJens Wiklander }
40561fba0faSJens Wiklander 
of_find_node_by_phandle(phandle handle)406644ec0a9SSimon Glass struct device_node *of_find_node_by_phandle(phandle handle)
407644ec0a9SSimon Glass {
408644ec0a9SSimon Glass 	struct device_node *np;
409644ec0a9SSimon Glass 
410644ec0a9SSimon Glass 	if (!handle)
411644ec0a9SSimon Glass 		return NULL;
412644ec0a9SSimon Glass 
413644ec0a9SSimon Glass 	for_each_of_allnodes(np)
414644ec0a9SSimon Glass 		if (np->phandle == handle)
415644ec0a9SSimon Glass 			break;
416644ec0a9SSimon Glass 	(void)of_node_get(np);
417644ec0a9SSimon Glass 
418644ec0a9SSimon Glass 	return np;
419644ec0a9SSimon Glass }
420644ec0a9SSimon Glass 
421644ec0a9SSimon Glass /**
422644ec0a9SSimon Glass  * of_find_property_value_of_size() - find property of given size
423644ec0a9SSimon Glass  *
424644ec0a9SSimon Glass  * Search for a property in a device node and validate the requested size.
425644ec0a9SSimon Glass  *
426644ec0a9SSimon Glass  * @np:		device node from which the property value is to be read.
427644ec0a9SSimon Glass  * @propname:	name of the property to be searched.
428644ec0a9SSimon Glass  * @len:	requested length of property value
429644ec0a9SSimon Glass  *
430644ec0a9SSimon Glass  * @return the property value on success, -EINVAL if the property does not
431644ec0a9SSimon Glass  * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
432644ec0a9SSimon Glass  * property data isn't large enough.
433644ec0a9SSimon Glass  */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 len)434644ec0a9SSimon Glass static void *of_find_property_value_of_size(const struct device_node *np,
435644ec0a9SSimon Glass 					    const char *propname, u32 len)
436644ec0a9SSimon Glass {
437644ec0a9SSimon Glass 	struct property *prop = of_find_property(np, propname, NULL);
438644ec0a9SSimon Glass 
439644ec0a9SSimon Glass 	if (!prop)
440644ec0a9SSimon Glass 		return ERR_PTR(-EINVAL);
441644ec0a9SSimon Glass 	if (!prop->value)
442644ec0a9SSimon Glass 		return ERR_PTR(-ENODATA);
443644ec0a9SSimon Glass 	if (len > prop->length)
444644ec0a9SSimon Glass 		return ERR_PTR(-EOVERFLOW);
445644ec0a9SSimon Glass 
446644ec0a9SSimon Glass 	return prop->value;
447644ec0a9SSimon Glass }
448644ec0a9SSimon Glass 
of_read_u32(const struct device_node * np,const char * propname,u32 * outp)449644ec0a9SSimon Glass int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
450644ec0a9SSimon Glass {
451644ec0a9SSimon Glass 	const __be32 *val;
452644ec0a9SSimon Glass 
453644ec0a9SSimon Glass 	debug("%s: %s: ", __func__, propname);
454644ec0a9SSimon Glass 	if (!np)
455644ec0a9SSimon Glass 		return -EINVAL;
456644ec0a9SSimon Glass 	val = of_find_property_value_of_size(np, propname, sizeof(*outp));
457644ec0a9SSimon Glass 	if (IS_ERR(val)) {
458644ec0a9SSimon Glass 		debug("(not found)\n");
459644ec0a9SSimon Glass 		return PTR_ERR(val);
460644ec0a9SSimon Glass 	}
461644ec0a9SSimon Glass 
462644ec0a9SSimon Glass 	*outp = be32_to_cpup(val);
463644ec0a9SSimon Glass 	debug("%#x (%d)\n", *outp, *outp);
464644ec0a9SSimon Glass 
465644ec0a9SSimon Glass 	return 0;
466644ec0a9SSimon Glass }
467644ec0a9SSimon Glass 
of_read_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz)468644ec0a9SSimon Glass int of_read_u32_array(const struct device_node *np, const char *propname,
469644ec0a9SSimon Glass 		      u32 *out_values, size_t sz)
470644ec0a9SSimon Glass {
471644ec0a9SSimon Glass 	const __be32 *val;
472644ec0a9SSimon Glass 
473644ec0a9SSimon Glass 	debug("%s: %s: ", __func__, propname);
474644ec0a9SSimon Glass 	val = of_find_property_value_of_size(np, propname,
475644ec0a9SSimon Glass 					     sz * sizeof(*out_values));
476644ec0a9SSimon Glass 
477644ec0a9SSimon Glass 	if (IS_ERR(val))
478644ec0a9SSimon Glass 		return PTR_ERR(val);
479644ec0a9SSimon Glass 
480644ec0a9SSimon Glass 	debug("size %zd\n", sz);
481644ec0a9SSimon Glass 	while (sz--)
482644ec0a9SSimon Glass 		*out_values++ = be32_to_cpup(val++);
483644ec0a9SSimon Glass 
484644ec0a9SSimon Glass 	return 0;
485644ec0a9SSimon Glass }
486644ec0a9SSimon Glass 
of_read_u64(const struct device_node * np,const char * propname,u64 * outp)4877e5196c4SSimon Glass int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
4887e5196c4SSimon Glass {
4897e5196c4SSimon Glass 	const __be64 *val;
4907e5196c4SSimon Glass 
4917e5196c4SSimon Glass 	debug("%s: %s: ", __func__, propname);
4927e5196c4SSimon Glass 	if (!np)
4937e5196c4SSimon Glass 		return -EINVAL;
4947e5196c4SSimon Glass 	val = of_find_property_value_of_size(np, propname, sizeof(*outp));
4957e5196c4SSimon Glass 	if (IS_ERR(val)) {
4967e5196c4SSimon Glass 		debug("(not found)\n");
4977e5196c4SSimon Glass 		return PTR_ERR(val);
4987e5196c4SSimon Glass 	}
4997e5196c4SSimon Glass 
5007e5196c4SSimon Glass 	*outp = be64_to_cpup(val);
5017e5196c4SSimon Glass 	debug("%#llx (%lld)\n", (unsigned long long)*outp,
5027e5196c4SSimon Glass               (unsigned long long)*outp);
5037e5196c4SSimon Glass 
5047e5196c4SSimon Glass 	return 0;
5057e5196c4SSimon Glass }
5067e5196c4SSimon Glass 
of_property_match_string(const struct device_node * np,const char * propname,const char * string)507644ec0a9SSimon Glass int of_property_match_string(const struct device_node *np, const char *propname,
508644ec0a9SSimon Glass 			     const char *string)
509644ec0a9SSimon Glass {
510644ec0a9SSimon Glass 	const struct property *prop = of_find_property(np, propname, NULL);
511644ec0a9SSimon Glass 	size_t l;
512644ec0a9SSimon Glass 	int i;
513644ec0a9SSimon Glass 	const char *p, *end;
514644ec0a9SSimon Glass 
515644ec0a9SSimon Glass 	if (!prop)
516644ec0a9SSimon Glass 		return -EINVAL;
517644ec0a9SSimon Glass 	if (!prop->value)
518644ec0a9SSimon Glass 		return -ENODATA;
519644ec0a9SSimon Glass 
520644ec0a9SSimon Glass 	p = prop->value;
521644ec0a9SSimon Glass 	end = p + prop->length;
522644ec0a9SSimon Glass 
523644ec0a9SSimon Glass 	for (i = 0; p < end; i++, p += l) {
524644ec0a9SSimon Glass 		l = strnlen(p, end - p) + 1;
525644ec0a9SSimon Glass 		if (p + l > end)
526644ec0a9SSimon Glass 			return -EILSEQ;
527644ec0a9SSimon Glass 		debug("comparing %s with %s\n", string, p);
528644ec0a9SSimon Glass 		if (strcmp(string, p) == 0)
529644ec0a9SSimon Glass 			return i; /* Found it; return index */
530644ec0a9SSimon Glass 	}
531644ec0a9SSimon Glass 	return -ENODATA;
532644ec0a9SSimon Glass }
533644ec0a9SSimon Glass 
534644ec0a9SSimon Glass /**
535644ec0a9SSimon Glass  * of_property_read_string_helper() - Utility helper for parsing string properties
536644ec0a9SSimon Glass  * @np:		device node from which the property value is to be read.
537644ec0a9SSimon Glass  * @propname:	name of the property to be searched.
538644ec0a9SSimon Glass  * @out_strs:	output array of string pointers.
539644ec0a9SSimon Glass  * @sz:		number of array elements to read.
540644ec0a9SSimon Glass  * @skip:	Number of strings to skip over at beginning of list.
541644ec0a9SSimon Glass  *
542644ec0a9SSimon Glass  * Don't call this function directly. It is a utility helper for the
543644ec0a9SSimon Glass  * of_property_read_string*() family of functions.
544644ec0a9SSimon Glass  */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)545644ec0a9SSimon Glass int of_property_read_string_helper(const struct device_node *np,
546644ec0a9SSimon Glass 				   const char *propname, const char **out_strs,
547644ec0a9SSimon Glass 				   size_t sz, int skip)
548644ec0a9SSimon Glass {
549644ec0a9SSimon Glass 	const struct property *prop = of_find_property(np, propname, NULL);
550644ec0a9SSimon Glass 	int l = 0, i = 0;
551644ec0a9SSimon Glass 	const char *p, *end;
552644ec0a9SSimon Glass 
553644ec0a9SSimon Glass 	if (!prop)
554644ec0a9SSimon Glass 		return -EINVAL;
555644ec0a9SSimon Glass 	if (!prop->value)
556644ec0a9SSimon Glass 		return -ENODATA;
557644ec0a9SSimon Glass 	p = prop->value;
558644ec0a9SSimon Glass 	end = p + prop->length;
559644ec0a9SSimon Glass 
560644ec0a9SSimon Glass 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
561644ec0a9SSimon Glass 		l = strnlen(p, end - p) + 1;
562644ec0a9SSimon Glass 		if (p + l > end)
563644ec0a9SSimon Glass 			return -EILSEQ;
564644ec0a9SSimon Glass 		if (out_strs && i >= skip)
565644ec0a9SSimon Glass 			*out_strs++ = p;
566644ec0a9SSimon Glass 	}
567644ec0a9SSimon Glass 	i -= skip;
568644ec0a9SSimon Glass 	return i <= 0 ? -ENODATA : i;
569644ec0a9SSimon Glass }
570644ec0a9SSimon Glass 
__of_parse_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name,int cell_count,int index,struct of_phandle_args * out_args)571644ec0a9SSimon Glass static int __of_parse_phandle_with_args(const struct device_node *np,
572644ec0a9SSimon Glass 					const char *list_name,
573644ec0a9SSimon Glass 					const char *cells_name,
574644ec0a9SSimon Glass 					int cell_count, int index,
575644ec0a9SSimon Glass 					struct of_phandle_args *out_args)
576644ec0a9SSimon Glass {
577644ec0a9SSimon Glass 	const __be32 *list, *list_end;
578644ec0a9SSimon Glass 	int rc = 0, cur_index = 0;
579644ec0a9SSimon Glass 	uint32_t count = 0;
580644ec0a9SSimon Glass 	struct device_node *node = NULL;
581644ec0a9SSimon Glass 	phandle phandle;
582644ec0a9SSimon Glass 	int size;
583644ec0a9SSimon Glass 
584644ec0a9SSimon Glass 	/* Retrieve the phandle list property */
585644ec0a9SSimon Glass 	list = of_get_property(np, list_name, &size);
586644ec0a9SSimon Glass 	if (!list)
587644ec0a9SSimon Glass 		return -ENOENT;
588644ec0a9SSimon Glass 	list_end = list + size / sizeof(*list);
589644ec0a9SSimon Glass 
590644ec0a9SSimon Glass 	/* Loop over the phandles until all the requested entry is found */
591644ec0a9SSimon Glass 	while (list < list_end) {
592644ec0a9SSimon Glass 		rc = -EINVAL;
593644ec0a9SSimon Glass 		count = 0;
594644ec0a9SSimon Glass 
595644ec0a9SSimon Glass 		/*
596644ec0a9SSimon Glass 		 * If phandle is 0, then it is an empty entry with no
597644ec0a9SSimon Glass 		 * arguments.  Skip forward to the next entry.
598644ec0a9SSimon Glass 		 */
599644ec0a9SSimon Glass 		phandle = be32_to_cpup(list++);
600644ec0a9SSimon Glass 		if (phandle) {
601644ec0a9SSimon Glass 			/*
602644ec0a9SSimon Glass 			 * Find the provider node and parse the #*-cells
603644ec0a9SSimon Glass 			 * property to determine the argument length.
604644ec0a9SSimon Glass 			 *
605644ec0a9SSimon Glass 			 * This is not needed if the cell count is hard-coded
606644ec0a9SSimon Glass 			 * (i.e. cells_name not set, but cell_count is set),
607644ec0a9SSimon Glass 			 * except when we're going to return the found node
608644ec0a9SSimon Glass 			 * below.
609644ec0a9SSimon Glass 			 */
610644ec0a9SSimon Glass 			if (cells_name || cur_index == index) {
611644ec0a9SSimon Glass 				node = of_find_node_by_phandle(phandle);
612644ec0a9SSimon Glass 				if (!node) {
613644ec0a9SSimon Glass 					debug("%s: could not find phandle\n",
614644ec0a9SSimon Glass 					      np->full_name);
615644ec0a9SSimon Glass 					goto err;
616644ec0a9SSimon Glass 				}
617644ec0a9SSimon Glass 			}
618644ec0a9SSimon Glass 
619644ec0a9SSimon Glass 			if (cells_name) {
620644ec0a9SSimon Glass 				if (of_read_u32(node, cells_name, &count)) {
621644ec0a9SSimon Glass 					debug("%s: could not get %s for %s\n",
622644ec0a9SSimon Glass 					      np->full_name, cells_name,
623644ec0a9SSimon Glass 					      node->full_name);
624644ec0a9SSimon Glass 					goto err;
625644ec0a9SSimon Glass 				}
626644ec0a9SSimon Glass 			} else {
627644ec0a9SSimon Glass 				count = cell_count;
628644ec0a9SSimon Glass 			}
629644ec0a9SSimon Glass 
630644ec0a9SSimon Glass 			/*
631644ec0a9SSimon Glass 			 * Make sure that the arguments actually fit in the
632644ec0a9SSimon Glass 			 * remaining property data length
633644ec0a9SSimon Glass 			 */
634644ec0a9SSimon Glass 			if (list + count > list_end) {
635644ec0a9SSimon Glass 				debug("%s: arguments longer than property\n",
636644ec0a9SSimon Glass 				      np->full_name);
637644ec0a9SSimon Glass 				goto err;
638644ec0a9SSimon Glass 			}
639644ec0a9SSimon Glass 		}
640644ec0a9SSimon Glass 
641644ec0a9SSimon Glass 		/*
642644ec0a9SSimon Glass 		 * All of the error cases above bail out of the loop, so at
643644ec0a9SSimon Glass 		 * this point, the parsing is successful. If the requested
644644ec0a9SSimon Glass 		 * index matches, then fill the out_args structure and return,
645644ec0a9SSimon Glass 		 * or return -ENOENT for an empty entry.
646644ec0a9SSimon Glass 		 */
647644ec0a9SSimon Glass 		rc = -ENOENT;
648644ec0a9SSimon Glass 		if (cur_index == index) {
649644ec0a9SSimon Glass 			if (!phandle)
650644ec0a9SSimon Glass 				goto err;
651644ec0a9SSimon Glass 
652644ec0a9SSimon Glass 			if (out_args) {
653644ec0a9SSimon Glass 				int i;
654644ec0a9SSimon Glass 				if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
655644ec0a9SSimon Glass 					count = OF_MAX_PHANDLE_ARGS;
656644ec0a9SSimon Glass 				out_args->np = node;
657644ec0a9SSimon Glass 				out_args->args_count = count;
658644ec0a9SSimon Glass 				for (i = 0; i < count; i++)
659644ec0a9SSimon Glass 					out_args->args[i] =
660644ec0a9SSimon Glass 							be32_to_cpup(list++);
661644ec0a9SSimon Glass 			} else {
662644ec0a9SSimon Glass 				of_node_put(node);
663644ec0a9SSimon Glass 			}
664644ec0a9SSimon Glass 
665644ec0a9SSimon Glass 			/* Found it! return success */
666644ec0a9SSimon Glass 			return 0;
667644ec0a9SSimon Glass 		}
668644ec0a9SSimon Glass 
669644ec0a9SSimon Glass 		of_node_put(node);
670644ec0a9SSimon Glass 		node = NULL;
671644ec0a9SSimon Glass 		list += count;
672644ec0a9SSimon Glass 		cur_index++;
673644ec0a9SSimon Glass 	}
674644ec0a9SSimon Glass 
675644ec0a9SSimon Glass 	/*
676644ec0a9SSimon Glass 	 * Unlock node before returning result; will be one of:
677644ec0a9SSimon Glass 	 * -ENOENT : index is for empty phandle
678644ec0a9SSimon Glass 	 * -EINVAL : parsing error on data
679644ec0a9SSimon Glass 	 * [1..n]  : Number of phandle (count mode; when index = -1)
680644ec0a9SSimon Glass 	 */
681644ec0a9SSimon Glass 	rc = index < 0 ? cur_index : -ENOENT;
682644ec0a9SSimon Glass  err:
683644ec0a9SSimon Glass 	if (node)
684644ec0a9SSimon Glass 		of_node_put(node);
685644ec0a9SSimon Glass 	return rc;
686644ec0a9SSimon Glass }
687644ec0a9SSimon Glass 
of_parse_phandle(const struct device_node * np,const char * phandle_name,int index)688644ec0a9SSimon Glass struct device_node *of_parse_phandle(const struct device_node *np,
689644ec0a9SSimon Glass 				     const char *phandle_name, int index)
690644ec0a9SSimon Glass {
691644ec0a9SSimon Glass 	struct of_phandle_args args;
692644ec0a9SSimon Glass 
693644ec0a9SSimon Glass 	if (index < 0)
694644ec0a9SSimon Glass 		return NULL;
695644ec0a9SSimon Glass 
696644ec0a9SSimon Glass 	if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
697644ec0a9SSimon Glass 					 &args))
698644ec0a9SSimon Glass 		return NULL;
699644ec0a9SSimon Glass 
700644ec0a9SSimon Glass 	return args.np;
701644ec0a9SSimon Glass }
702644ec0a9SSimon Glass 
of_parse_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name,int index,struct of_phandle_args * out_args)703644ec0a9SSimon Glass int of_parse_phandle_with_args(const struct device_node *np,
704644ec0a9SSimon Glass 			       const char *list_name, const char *cells_name,
705644ec0a9SSimon Glass 			       int index, struct of_phandle_args *out_args)
706644ec0a9SSimon Glass {
707644ec0a9SSimon Glass 	if (index < 0)
708644ec0a9SSimon Glass 		return -EINVAL;
709644ec0a9SSimon Glass 
710644ec0a9SSimon Glass 	return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
711644ec0a9SSimon Glass 					    index, out_args);
712644ec0a9SSimon Glass }
713644ec0a9SSimon Glass 
of_count_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name)714642346aeSPatrice Chotard int of_count_phandle_with_args(const struct device_node *np,
715642346aeSPatrice Chotard 			       const char *list_name, const char *cells_name)
716642346aeSPatrice Chotard {
717642346aeSPatrice Chotard 	return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
718642346aeSPatrice Chotard 					    -1, NULL);
719642346aeSPatrice Chotard }
720642346aeSPatrice Chotard 
of_alias_add(struct alias_prop * ap,struct device_node * np,int id,const char * stem,int stem_len)721644ec0a9SSimon Glass static void of_alias_add(struct alias_prop *ap, struct device_node *np,
722644ec0a9SSimon Glass 			 int id, const char *stem, int stem_len)
723644ec0a9SSimon Glass {
724644ec0a9SSimon Glass 	ap->np = np;
725644ec0a9SSimon Glass 	ap->id = id;
726644ec0a9SSimon Glass 	strncpy(ap->stem, stem, stem_len);
727644ec0a9SSimon Glass 	ap->stem[stem_len] = 0;
728644ec0a9SSimon Glass 	list_add_tail(&ap->link, &aliases_lookup);
729644ec0a9SSimon Glass 	debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
730644ec0a9SSimon Glass 	      ap->alias, ap->stem, ap->id, of_node_full_name(np));
731644ec0a9SSimon Glass }
732644ec0a9SSimon Glass 
of_alias_scan(void)733644ec0a9SSimon Glass int of_alias_scan(void)
734644ec0a9SSimon Glass {
735644ec0a9SSimon Glass 	struct property *pp;
736644ec0a9SSimon Glass 
737644ec0a9SSimon Glass 	of_aliases = of_find_node_by_path("/aliases");
738644ec0a9SSimon Glass 	of_chosen = of_find_node_by_path("/chosen");
739644ec0a9SSimon Glass 	if (of_chosen == NULL)
740644ec0a9SSimon Glass 		of_chosen = of_find_node_by_path("/chosen@0");
741644ec0a9SSimon Glass 
742644ec0a9SSimon Glass 	if (of_chosen) {
743644ec0a9SSimon Glass 		const char *name;
744644ec0a9SSimon Glass 
745644ec0a9SSimon Glass 		name = of_get_property(of_chosen, "stdout-path", NULL);
746644ec0a9SSimon Glass 		if (name)
747644ec0a9SSimon Glass 			of_stdout = of_find_node_opts_by_path(name,
748644ec0a9SSimon Glass 							&of_stdout_options);
749644ec0a9SSimon Glass 	}
750644ec0a9SSimon Glass 
751644ec0a9SSimon Glass 	if (!of_aliases)
752644ec0a9SSimon Glass 		return 0;
753644ec0a9SSimon Glass 
754644ec0a9SSimon Glass 	for_each_property_of_node(of_aliases, pp) {
755644ec0a9SSimon Glass 		const char *start = pp->name;
756644ec0a9SSimon Glass 		const char *end = start + strlen(start);
757644ec0a9SSimon Glass 		struct device_node *np;
758644ec0a9SSimon Glass 		struct alias_prop *ap;
759644ec0a9SSimon Glass 		ulong id;
760644ec0a9SSimon Glass 		int len;
761644ec0a9SSimon Glass 
762644ec0a9SSimon Glass 		/* Skip those we do not want to proceed */
763644ec0a9SSimon Glass 		if (!strcmp(pp->name, "name") ||
764644ec0a9SSimon Glass 		    !strcmp(pp->name, "phandle") ||
765644ec0a9SSimon Glass 		    !strcmp(pp->name, "linux,phandle"))
766644ec0a9SSimon Glass 			continue;
767644ec0a9SSimon Glass 
768644ec0a9SSimon Glass 		np = of_find_node_by_path(pp->value);
769644ec0a9SSimon Glass 		if (!np)
770644ec0a9SSimon Glass 			continue;
771644ec0a9SSimon Glass 
772644ec0a9SSimon Glass 		/*
773644ec0a9SSimon Glass 		 * walk the alias backwards to extract the id and work out
774644ec0a9SSimon Glass 		 * the 'stem' string
775644ec0a9SSimon Glass 		 */
776644ec0a9SSimon Glass 		while (isdigit(*(end-1)) && end > start)
777644ec0a9SSimon Glass 			end--;
778644ec0a9SSimon Glass 		len = end - start;
779644ec0a9SSimon Glass 
780644ec0a9SSimon Glass 		if (strict_strtoul(end, 10, &id) < 0)
781644ec0a9SSimon Glass 			continue;
782644ec0a9SSimon Glass 
783644ec0a9SSimon Glass 		/* Allocate an alias_prop with enough space for the stem */
784644ec0a9SSimon Glass 		ap = malloc(sizeof(*ap) + len + 1);
785644ec0a9SSimon Glass 		if (!ap)
786644ec0a9SSimon Glass 			return -ENOMEM;
787644ec0a9SSimon Glass 		memset(ap, 0, sizeof(*ap) + len + 1);
788644ec0a9SSimon Glass 		ap->alias = start;
789644ec0a9SSimon Glass 		of_alias_add(ap, np, id, start, len);
790644ec0a9SSimon Glass 	}
791644ec0a9SSimon Glass 
792644ec0a9SSimon Glass 	return 0;
793644ec0a9SSimon Glass }
794644ec0a9SSimon Glass 
of_alias_get_id(const struct device_node * np,const char * stem)795644ec0a9SSimon Glass int of_alias_get_id(const struct device_node *np, const char *stem)
796644ec0a9SSimon Glass {
797644ec0a9SSimon Glass 	struct alias_prop *app;
798644ec0a9SSimon Glass 	int id = -ENODEV;
799644ec0a9SSimon Glass 
800644ec0a9SSimon Glass 	mutex_lock(&of_mutex);
801644ec0a9SSimon Glass 	list_for_each_entry(app, &aliases_lookup, link) {
802644ec0a9SSimon Glass 		if (strcmp(app->stem, stem) != 0)
803644ec0a9SSimon Glass 			continue;
804644ec0a9SSimon Glass 
805644ec0a9SSimon Glass 		if (np == app->np) {
806644ec0a9SSimon Glass 			id = app->id;
807644ec0a9SSimon Glass 			break;
808644ec0a9SSimon Glass 		}
809644ec0a9SSimon Glass 	}
810644ec0a9SSimon Glass 	mutex_unlock(&of_mutex);
811644ec0a9SSimon Glass 
812644ec0a9SSimon Glass 	return id;
813644ec0a9SSimon Glass }
814644ec0a9SSimon Glass 
of_alias_get_highest_id(const char * stem)815*5ebc7c7eSMichal Simek int of_alias_get_highest_id(const char *stem)
816*5ebc7c7eSMichal Simek {
817*5ebc7c7eSMichal Simek 	struct alias_prop *app;
818*5ebc7c7eSMichal Simek 	int id = -1;
819*5ebc7c7eSMichal Simek 
820*5ebc7c7eSMichal Simek 	mutex_lock(&of_mutex);
821*5ebc7c7eSMichal Simek 	list_for_each_entry(app, &aliases_lookup, link) {
822*5ebc7c7eSMichal Simek 		if (strcmp(app->stem, stem) != 0)
823*5ebc7c7eSMichal Simek 			continue;
824*5ebc7c7eSMichal Simek 
825*5ebc7c7eSMichal Simek 		if (app->id > id)
826*5ebc7c7eSMichal Simek 			id = app->id;
827*5ebc7c7eSMichal Simek 	}
828*5ebc7c7eSMichal Simek 	mutex_unlock(&of_mutex);
829*5ebc7c7eSMichal Simek 
830*5ebc7c7eSMichal Simek 	return id;
831*5ebc7c7eSMichal Simek }
832*5ebc7c7eSMichal Simek 
of_get_stdout(void)833644ec0a9SSimon Glass struct device_node *of_get_stdout(void)
834644ec0a9SSimon Glass {
835644ec0a9SSimon Glass 	return of_stdout;
836644ec0a9SSimon Glass }
837