xref: /OK3568_Linux_fs/u-boot/drivers/core/of_access.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Originally from Linux v4.9
3*4882a593Smuzhiyun  * Paul Mackerras	August 1996.
4*4882a593Smuzhiyun  * Copyright (C) 1996-2005 Paul Mackerras.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7*4882a593Smuzhiyun  *   {engebret|bergner}@us.ibm.com
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
12*4882a593Smuzhiyun  * Grant Likely.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Modified for U-Boot
15*4882a593Smuzhiyun  * Copyright (c) 2017 Google, Inc
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * This file follows drivers/of/base.c with functions in the same order as the
18*4882a593Smuzhiyun  * Linux version.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <common.h>
24*4882a593Smuzhiyun #include <linux/libfdt.h>
25*4882a593Smuzhiyun #include <dm/of_access.h>
26*4882a593Smuzhiyun #include <linux/ctype.h>
27*4882a593Smuzhiyun #include <linux/err.h>
28*4882a593Smuzhiyun #include <linux/ioport.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* list of struct alias_prop aliases */
33*4882a593Smuzhiyun LIST_HEAD(aliases_lookup);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* "/aliaes" node */
36*4882a593Smuzhiyun static struct device_node *of_aliases;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* "/chosen" node */
39*4882a593Smuzhiyun static struct device_node *of_chosen;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /* node pointed to by the stdout-path alias */
42*4882a593Smuzhiyun static struct device_node *of_stdout;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /* pointer to options given after the alias (separated by :) or NULL if none */
45*4882a593Smuzhiyun static const char *of_stdout_options;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun  * struct alias_prop - Alias property in 'aliases' node
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * The structure represents one alias property of 'aliases' node as
51*4882a593Smuzhiyun  * an entry in aliases_lookup list.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * @link:	List node to link the structure in aliases_lookup list
54*4882a593Smuzhiyun  * @alias:	Alias property name
55*4882a593Smuzhiyun  * @np:		Pointer to device_node that the alias stands for
56*4882a593Smuzhiyun  * @id:		Index value from end of alias name
57*4882a593Smuzhiyun  * @stem:	Alias string without the index
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun struct alias_prop {
60*4882a593Smuzhiyun 	struct list_head link;
61*4882a593Smuzhiyun 	const char *alias;
62*4882a593Smuzhiyun 	struct device_node *np;
63*4882a593Smuzhiyun 	int id;
64*4882a593Smuzhiyun 	char stem[0];
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun 
of_n_addr_cells(const struct device_node * np)67*4882a593Smuzhiyun int of_n_addr_cells(const struct device_node *np)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	const __be32 *ip;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	do {
72*4882a593Smuzhiyun 		if (np->parent)
73*4882a593Smuzhiyun 			np = np->parent;
74*4882a593Smuzhiyun 		ip = of_get_property(np, "#address-cells", NULL);
75*4882a593Smuzhiyun 		if (ip)
76*4882a593Smuzhiyun 			return be32_to_cpup(ip);
77*4882a593Smuzhiyun 	} while (np->parent);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* No #address-cells property for the root node */
80*4882a593Smuzhiyun 	return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
of_n_size_cells(const struct device_node * np)83*4882a593Smuzhiyun int of_n_size_cells(const struct device_node *np)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	const __be32 *ip;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	do {
88*4882a593Smuzhiyun 		if (np->parent)
89*4882a593Smuzhiyun 			np = np->parent;
90*4882a593Smuzhiyun 		ip = of_get_property(np, "#size-cells", NULL);
91*4882a593Smuzhiyun 		if (ip)
92*4882a593Smuzhiyun 			return be32_to_cpup(ip);
93*4882a593Smuzhiyun 	} while (np->parent);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* No #size-cells property for the root node */
96*4882a593Smuzhiyun 	return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
of_simple_addr_cells(const struct device_node * np)99*4882a593Smuzhiyun int of_simple_addr_cells(const struct device_node *np)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	const __be32 *ip;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	ip = of_get_property(np, "#address-cells", NULL);
104*4882a593Smuzhiyun 	if (ip)
105*4882a593Smuzhiyun 		return be32_to_cpup(ip);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* Return a default of 2 to match fdt_address_cells()*/
108*4882a593Smuzhiyun 	return 2;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
of_simple_size_cells(const struct device_node * np)111*4882a593Smuzhiyun int of_simple_size_cells(const struct device_node *np)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	const __be32 *ip;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	ip = of_get_property(np, "#size-cells", NULL);
116*4882a593Smuzhiyun 	if (ip)
117*4882a593Smuzhiyun 		return be32_to_cpup(ip);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/* Return a default of 2 to match fdt_size_cells()*/
120*4882a593Smuzhiyun 	return 2;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
of_find_property(const struct device_node * np,const char * name,int * lenp)123*4882a593Smuzhiyun struct property *of_find_property(const struct device_node *np,
124*4882a593Smuzhiyun 				  const char *name, int *lenp)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct property *pp;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	if (!np)
129*4882a593Smuzhiyun 		return NULL;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	for (pp = np->properties; pp; pp = pp->next) {
132*4882a593Smuzhiyun 		if (strcmp(pp->name, name) == 0) {
133*4882a593Smuzhiyun 			if (lenp)
134*4882a593Smuzhiyun 				*lenp = pp->length;
135*4882a593Smuzhiyun 			break;
136*4882a593Smuzhiyun 		}
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 	if (!pp && lenp)
139*4882a593Smuzhiyun 		*lenp = -FDT_ERR_NOTFOUND;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	return pp;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
of_find_all_nodes(struct device_node * prev)144*4882a593Smuzhiyun struct device_node *of_find_all_nodes(struct device_node *prev)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	struct device_node *np;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	if (!prev) {
149*4882a593Smuzhiyun 		np = gd->of_root;
150*4882a593Smuzhiyun 	} else if (prev->child) {
151*4882a593Smuzhiyun 		np = prev->child;
152*4882a593Smuzhiyun 	} else {
153*4882a593Smuzhiyun 		/*
154*4882a593Smuzhiyun 		 * Walk back up looking for a sibling, or the end of the
155*4882a593Smuzhiyun 		 * structure
156*4882a593Smuzhiyun 		 */
157*4882a593Smuzhiyun 		np = prev;
158*4882a593Smuzhiyun 		while (np->parent && !np->sibling)
159*4882a593Smuzhiyun 			np = np->parent;
160*4882a593Smuzhiyun 		np = np->sibling; /* Might be null at the end of the tree */
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	return np;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
of_get_property(const struct device_node * np,const char * name,int * lenp)166*4882a593Smuzhiyun const void *of_get_property(const struct device_node *np, const char *name,
167*4882a593Smuzhiyun 			    int *lenp)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct property *pp = of_find_property(np, name, lenp);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	return pp ? pp->value : NULL;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
of_get_first_property(const struct device_node * np)174*4882a593Smuzhiyun const struct property *of_get_first_property(const struct device_node *np)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	if (!np)
177*4882a593Smuzhiyun 		return NULL;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return  np->properties;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
of_get_next_property(const struct device_node * np,const struct property * property)182*4882a593Smuzhiyun const struct property *of_get_next_property(const struct device_node *np,
183*4882a593Smuzhiyun 					    const struct property *property)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	if (!np)
186*4882a593Smuzhiyun 		return NULL;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	return property->next;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
of_get_property_by_prop(const struct device_node * np,const struct property * property,const char ** name,int * lenp)191*4882a593Smuzhiyun const void *of_get_property_by_prop(const struct device_node *np,
192*4882a593Smuzhiyun 				    const struct property *property,
193*4882a593Smuzhiyun 				    const char **name,
194*4882a593Smuzhiyun 				    int *lenp)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	if (!np || !property)
197*4882a593Smuzhiyun 		return NULL;
198*4882a593Smuzhiyun 	if (name)
199*4882a593Smuzhiyun 		*name = property->name;
200*4882a593Smuzhiyun 	if (lenp)
201*4882a593Smuzhiyun 		*lenp = property->length;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	return property->value;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
of_prop_next_string(struct property * prop,const char * cur)206*4882a593Smuzhiyun static const char *of_prop_next_string(struct property *prop, const char *cur)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	const void *curv = cur;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	if (!prop)
211*4882a593Smuzhiyun 		return NULL;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (!cur)
214*4882a593Smuzhiyun 		return prop->value;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	curv += strlen(cur) + 1;
217*4882a593Smuzhiyun 	if (curv >= prop->value + prop->length)
218*4882a593Smuzhiyun 		return NULL;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	return curv;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
of_device_is_compatible(const struct device_node * device,const char * compat,const char * type,const char * name)223*4882a593Smuzhiyun int of_device_is_compatible(const struct device_node *device,
224*4882a593Smuzhiyun 			    const char *compat, const char *type,
225*4882a593Smuzhiyun 			    const char *name)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	struct property *prop;
228*4882a593Smuzhiyun 	const char *cp;
229*4882a593Smuzhiyun 	int index = 0, score = 0;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* Compatible match has highest priority */
232*4882a593Smuzhiyun 	if (compat && compat[0]) {
233*4882a593Smuzhiyun 		prop = of_find_property(device, "compatible", NULL);
234*4882a593Smuzhiyun 		for (cp = of_prop_next_string(prop, NULL); cp;
235*4882a593Smuzhiyun 		     cp = of_prop_next_string(prop, cp), index++) {
236*4882a593Smuzhiyun 			if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
237*4882a593Smuzhiyun 				score = INT_MAX/2 - (index << 2);
238*4882a593Smuzhiyun 				break;
239*4882a593Smuzhiyun 			}
240*4882a593Smuzhiyun 		}
241*4882a593Smuzhiyun 		if (!score)
242*4882a593Smuzhiyun 			return 0;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/* Matching type is better than matching name */
246*4882a593Smuzhiyun 	if (type && type[0]) {
247*4882a593Smuzhiyun 		if (!device->type || of_node_cmp(type, device->type))
248*4882a593Smuzhiyun 			return 0;
249*4882a593Smuzhiyun 		score += 2;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* Matching name is a bit better than not */
253*4882a593Smuzhiyun 	if (name && name[0]) {
254*4882a593Smuzhiyun 		if (!device->name || of_node_cmp(name, device->name))
255*4882a593Smuzhiyun 			return 0;
256*4882a593Smuzhiyun 		score++;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return score;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
of_device_is_available(const struct device_node * device)262*4882a593Smuzhiyun bool of_device_is_available(const struct device_node *device)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	const char *status;
265*4882a593Smuzhiyun 	int statlen;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	if (!device)
268*4882a593Smuzhiyun 		return false;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	status = of_get_property(device, "status", &statlen);
271*4882a593Smuzhiyun 	if (status == NULL)
272*4882a593Smuzhiyun 		return true;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	if (statlen > 0) {
275*4882a593Smuzhiyun 		if (!strcmp(status, "okay"))
276*4882a593Smuzhiyun 			return true;
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	return false;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
of_get_parent(const struct device_node * node)282*4882a593Smuzhiyun struct device_node *of_get_parent(const struct device_node *node)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	const struct device_node *np;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	if (!node)
287*4882a593Smuzhiyun 		return NULL;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	np = of_node_get(node->parent);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	return (struct device_node *)np;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
__of_get_next_child(const struct device_node * node,struct device_node * prev)294*4882a593Smuzhiyun static struct device_node *__of_get_next_child(const struct device_node *node,
295*4882a593Smuzhiyun 					       struct device_node *prev)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	struct device_node *next;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	if (!node)
300*4882a593Smuzhiyun 		return NULL;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	next = prev ? prev->sibling : node->child;
303*4882a593Smuzhiyun 	/*
304*4882a593Smuzhiyun 	 * coverity[dead_error_line : FALSE]
305*4882a593Smuzhiyun 	 * Dead code here since our current implementation of of_node_get()
306*4882a593Smuzhiyun 	 * always returns NULL (Coverity CID 163245). But we leave it as is
307*4882a593Smuzhiyun 	 * since we may want to implement get/put later.
308*4882a593Smuzhiyun 	 */
309*4882a593Smuzhiyun 	for (; next; next = next->sibling)
310*4882a593Smuzhiyun 		if (of_node_get(next))
311*4882a593Smuzhiyun 			break;
312*4882a593Smuzhiyun 	of_node_put(prev);
313*4882a593Smuzhiyun 	return next;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun #define __for_each_child_of_node(parent, child) \
317*4882a593Smuzhiyun 	for (child = __of_get_next_child(parent, NULL); child != NULL; \
318*4882a593Smuzhiyun 	     child = __of_get_next_child(parent, child))
319*4882a593Smuzhiyun 
__of_find_node_by_path(struct device_node * parent,const char * path)320*4882a593Smuzhiyun static struct device_node *__of_find_node_by_path(struct device_node *parent,
321*4882a593Smuzhiyun 						  const char *path)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	struct device_node *child;
324*4882a593Smuzhiyun 	int len;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	len = strcspn(path, "/:");
327*4882a593Smuzhiyun 	if (!len)
328*4882a593Smuzhiyun 		return NULL;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	__for_each_child_of_node(parent, child) {
331*4882a593Smuzhiyun 		const char *name = strrchr(child->full_name, '/');
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 		name++;
334*4882a593Smuzhiyun 		if (strncmp(path, name, len) == 0 && (strlen(name) == len))
335*4882a593Smuzhiyun 			return child;
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 	return NULL;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun #define for_each_property_of_node(dn, pp) \
341*4882a593Smuzhiyun 	for (pp = dn->properties; pp != NULL; pp = pp->next)
342*4882a593Smuzhiyun 
of_find_node_opts_by_path(const char * path,const char ** opts)343*4882a593Smuzhiyun struct device_node *of_find_node_opts_by_path(const char *path,
344*4882a593Smuzhiyun 					      const char **opts)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	struct device_node *np = NULL;
347*4882a593Smuzhiyun 	struct property *pp;
348*4882a593Smuzhiyun 	const char *separator = strchr(path, ':');
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	if (opts)
351*4882a593Smuzhiyun 		*opts = separator ? separator + 1 : NULL;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	if (strcmp(path, "/") == 0)
354*4882a593Smuzhiyun 		return of_node_get(gd->of_root);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* The path could begin with an alias */
357*4882a593Smuzhiyun 	if (*path != '/') {
358*4882a593Smuzhiyun 		int len;
359*4882a593Smuzhiyun 		const char *p = separator;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 		if (!p)
362*4882a593Smuzhiyun 			p = strchrnul(path, '/');
363*4882a593Smuzhiyun 		len = p - path;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 		/* of_aliases must not be NULL */
366*4882a593Smuzhiyun 		if (!of_aliases)
367*4882a593Smuzhiyun 			return NULL;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 		for_each_property_of_node(of_aliases, pp) {
370*4882a593Smuzhiyun 			if (strlen(pp->name) == len && !strncmp(pp->name, path,
371*4882a593Smuzhiyun 								len)) {
372*4882a593Smuzhiyun 				np = of_find_node_by_path(pp->value);
373*4882a593Smuzhiyun 				break;
374*4882a593Smuzhiyun 			}
375*4882a593Smuzhiyun 		}
376*4882a593Smuzhiyun 		if (!np)
377*4882a593Smuzhiyun 			return NULL;
378*4882a593Smuzhiyun 		path = p;
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/* Step down the tree matching path components */
382*4882a593Smuzhiyun 	if (!np)
383*4882a593Smuzhiyun 		np = of_node_get(gd->of_root);
384*4882a593Smuzhiyun 	while (np && *path == '/') {
385*4882a593Smuzhiyun 		struct device_node *tmp = np;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		path++; /* Increment past '/' delimiter */
388*4882a593Smuzhiyun 		np = __of_find_node_by_path(np, path);
389*4882a593Smuzhiyun 		of_node_put(tmp);
390*4882a593Smuzhiyun 		path = strchrnul(path, '/');
391*4882a593Smuzhiyun 		if (separator && separator < path)
392*4882a593Smuzhiyun 			break;
393*4882a593Smuzhiyun 	}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	return np;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
of_find_compatible_node(struct device_node * from,const char * type,const char * compatible)398*4882a593Smuzhiyun struct device_node *of_find_compatible_node(struct device_node *from,
399*4882a593Smuzhiyun 		const char *type, const char *compatible)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	struct device_node *np;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	for_each_of_allnodes_from(from, np)
404*4882a593Smuzhiyun 		if (of_device_is_compatible(np, compatible, type, NULL) &&
405*4882a593Smuzhiyun 		    of_node_get(np))
406*4882a593Smuzhiyun 			break;
407*4882a593Smuzhiyun 	of_node_put(from);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	return np;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
of_find_node_by_phandle(phandle handle)412*4882a593Smuzhiyun struct device_node *of_find_node_by_phandle(phandle handle)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	struct device_node *np;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	if (!handle)
417*4882a593Smuzhiyun 		return NULL;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	for_each_of_allnodes(np)
420*4882a593Smuzhiyun 		if (np->phandle == handle)
421*4882a593Smuzhiyun 			break;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun #ifdef CONFIG_USING_KERNEL_DTB_V2
424*4882a593Smuzhiyun 	/* If not find in kernel fdt, traverse u-boot fdt */
425*4882a593Smuzhiyun 	if (!np) {
426*4882a593Smuzhiyun 		for (np = gd->of_root_f; np; np = of_find_all_nodes(np)) {
427*4882a593Smuzhiyun 			if (np->phandle == handle)
428*4882a593Smuzhiyun 				break;
429*4882a593Smuzhiyun 		}
430*4882a593Smuzhiyun 	}
431*4882a593Smuzhiyun #endif
432*4882a593Smuzhiyun 	(void)of_node_get(np);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	return np;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /**
438*4882a593Smuzhiyun  * of_find_property_value_of_size() - find property of given size
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * Search for a property in a device node and validate the requested size.
441*4882a593Smuzhiyun  *
442*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
443*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
444*4882a593Smuzhiyun  * @len:	requested length of property value
445*4882a593Smuzhiyun  *
446*4882a593Smuzhiyun  * @return the property value on success, -EINVAL if the property does not
447*4882a593Smuzhiyun  * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
448*4882a593Smuzhiyun  * property data isn't large enough.
449*4882a593Smuzhiyun  */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 len)450*4882a593Smuzhiyun static void *of_find_property_value_of_size(const struct device_node *np,
451*4882a593Smuzhiyun 					    const char *propname, u32 len)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun 	struct property *prop = of_find_property(np, propname, NULL);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	if (!prop)
456*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
457*4882a593Smuzhiyun 	if (!prop->value)
458*4882a593Smuzhiyun 		return ERR_PTR(-ENODATA);
459*4882a593Smuzhiyun 	if (len > prop->length)
460*4882a593Smuzhiyun 		return ERR_PTR(-EOVERFLOW);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	return prop->value;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
of_read_u32(const struct device_node * np,const char * propname,u32 * outp)465*4882a593Smuzhiyun int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun 	const __be32 *val;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	debug("%s: %s: ", __func__, propname);
470*4882a593Smuzhiyun 	if (!np)
471*4882a593Smuzhiyun 		return -EINVAL;
472*4882a593Smuzhiyun 	val = of_find_property_value_of_size(np, propname, sizeof(*outp));
473*4882a593Smuzhiyun 	if (IS_ERR(val)) {
474*4882a593Smuzhiyun 		debug("(not found)\n");
475*4882a593Smuzhiyun 		return PTR_ERR(val);
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	*outp = be32_to_cpup(val);
479*4882a593Smuzhiyun 	debug("%#x (%d)\n", *outp, *outp);
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun /**
485*4882a593Smuzhiyun  * of_property_read_u64 - Find and read a 64 bit integer from a property
486*4882a593Smuzhiyun  * @np:         device node from which the property value is to be read.
487*4882a593Smuzhiyun  * @propname:   name of the property to be searched.
488*4882a593Smuzhiyun  * @out_value:  pointer to return value, modified only if return value is 0.
489*4882a593Smuzhiyun  *
490*4882a593Smuzhiyun  * Search for a property in a device node and read a 64-bit value from
491*4882a593Smuzhiyun  * it. Returns 0 on success, -EINVAL if the property does not exist,
492*4882a593Smuzhiyun  * -ENODATA if property does not have a value, and -EOVERFLOW if the
493*4882a593Smuzhiyun  * property data isn't large enough.
494*4882a593Smuzhiyun  *
495*4882a593Smuzhiyun  * The out_value is modified only if a valid u64 value can be decoded.
496*4882a593Smuzhiyun  */
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)497*4882a593Smuzhiyun int of_property_read_u64(const struct device_node *np, const char *propname,
498*4882a593Smuzhiyun                          u64 *out_value)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun 	const __be32 *val = of_find_property_value_of_size(np, propname,
501*4882a593Smuzhiyun 							   sizeof(*out_value));
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	if (IS_ERR(val))
504*4882a593Smuzhiyun 		return PTR_ERR(val);
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	*out_value = of_read_number(val, 2);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	return 0;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
of_read_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz)511*4882a593Smuzhiyun int of_read_u32_array(const struct device_node *np, const char *propname,
512*4882a593Smuzhiyun 		      u32 *out_values, size_t sz)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	const __be32 *val;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	debug("%s: %s: ", __func__, propname);
517*4882a593Smuzhiyun 	val = of_find_property_value_of_size(np, propname,
518*4882a593Smuzhiyun 					     sz * sizeof(*out_values));
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	if (IS_ERR(val))
521*4882a593Smuzhiyun 		return PTR_ERR(val);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	debug("size %zd\n", sz);
524*4882a593Smuzhiyun 	while (sz--)
525*4882a593Smuzhiyun 		*out_values++ = be32_to_cpup(val++);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	return 0;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
of_write_u32_array(const struct device_node * np,const char * propname,u32 * values,size_t sz)530*4882a593Smuzhiyun int of_write_u32_array(const struct device_node *np, const char *propname,
531*4882a593Smuzhiyun 		       u32 *values, size_t sz)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	__be32 *val;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	debug("%s: %s: ", __func__, propname);
536*4882a593Smuzhiyun 	val = of_find_property_value_of_size(np, propname,
537*4882a593Smuzhiyun 					     sz * sizeof(*values));
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	if (IS_ERR(val))
540*4882a593Smuzhiyun 		return PTR_ERR(val);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	debug("size %zd\n", sz);
543*4882a593Smuzhiyun 	while (sz--)
544*4882a593Smuzhiyun 		*val++ = cpu_to_be32p(values++);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	return 0;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
of_property_match_string(const struct device_node * np,const char * propname,const char * string)549*4882a593Smuzhiyun int of_property_match_string(const struct device_node *np, const char *propname,
550*4882a593Smuzhiyun 			     const char *string)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	const struct property *prop = of_find_property(np, propname, NULL);
553*4882a593Smuzhiyun 	size_t l;
554*4882a593Smuzhiyun 	int i;
555*4882a593Smuzhiyun 	const char *p, *end;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	if (!prop)
558*4882a593Smuzhiyun 		return -EINVAL;
559*4882a593Smuzhiyun 	if (!prop->value)
560*4882a593Smuzhiyun 		return -ENODATA;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	p = prop->value;
563*4882a593Smuzhiyun 	end = p + prop->length;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	for (i = 0; p < end; i++, p += l) {
566*4882a593Smuzhiyun 		l = strnlen(p, end - p) + 1;
567*4882a593Smuzhiyun 		if (p + l > end)
568*4882a593Smuzhiyun 			return -EILSEQ;
569*4882a593Smuzhiyun 		debug("comparing %s with %s\n", string, p);
570*4882a593Smuzhiyun 		if (strcmp(string, p) == 0)
571*4882a593Smuzhiyun 			return i; /* Found it; return index */
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 	return -ENODATA;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun /**
577*4882a593Smuzhiyun  * of_property_read_string_helper() - Utility helper for parsing string properties
578*4882a593Smuzhiyun  * @np:		device node from which the property value is to be read.
579*4882a593Smuzhiyun  * @propname:	name of the property to be searched.
580*4882a593Smuzhiyun  * @out_strs:	output array of string pointers.
581*4882a593Smuzhiyun  * @sz:		number of array elements to read.
582*4882a593Smuzhiyun  * @skip:	Number of strings to skip over at beginning of list.
583*4882a593Smuzhiyun  *
584*4882a593Smuzhiyun  * Don't call this function directly. It is a utility helper for the
585*4882a593Smuzhiyun  * of_property_read_string*() family of functions.
586*4882a593Smuzhiyun  */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)587*4882a593Smuzhiyun int of_property_read_string_helper(const struct device_node *np,
588*4882a593Smuzhiyun 				   const char *propname, const char **out_strs,
589*4882a593Smuzhiyun 				   size_t sz, int skip)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	const struct property *prop = of_find_property(np, propname, NULL);
592*4882a593Smuzhiyun 	int l = 0, i = 0;
593*4882a593Smuzhiyun 	const char *p, *end;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	if (!prop)
596*4882a593Smuzhiyun 		return -EINVAL;
597*4882a593Smuzhiyun 	if (!prop->value)
598*4882a593Smuzhiyun 		return -ENODATA;
599*4882a593Smuzhiyun 	p = prop->value;
600*4882a593Smuzhiyun 	end = p + prop->length;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
603*4882a593Smuzhiyun 		l = strnlen(p, end - p) + 1;
604*4882a593Smuzhiyun 		if (p + l > end)
605*4882a593Smuzhiyun 			return -EILSEQ;
606*4882a593Smuzhiyun 		if (out_strs && i >= skip)
607*4882a593Smuzhiyun 			*out_strs++ = p;
608*4882a593Smuzhiyun 	}
609*4882a593Smuzhiyun 	i -= skip;
610*4882a593Smuzhiyun 	return i <= 0 ? -ENODATA : i;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
__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)613*4882a593Smuzhiyun static int __of_parse_phandle_with_args(const struct device_node *np,
614*4882a593Smuzhiyun 					const char *list_name,
615*4882a593Smuzhiyun 					const char *cells_name,
616*4882a593Smuzhiyun 					int cell_count, int index,
617*4882a593Smuzhiyun 					struct of_phandle_args *out_args)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun 	const __be32 *list, *list_end;
620*4882a593Smuzhiyun 	int rc = 0, cur_index = 0;
621*4882a593Smuzhiyun 	uint32_t count = 0;
622*4882a593Smuzhiyun 	struct device_node *node = NULL;
623*4882a593Smuzhiyun 	phandle phandle;
624*4882a593Smuzhiyun 	int size;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	/* Retrieve the phandle list property */
627*4882a593Smuzhiyun 	list = of_get_property(np, list_name, &size);
628*4882a593Smuzhiyun 	if (!list)
629*4882a593Smuzhiyun 		return -ENOENT;
630*4882a593Smuzhiyun 	list_end = list + size / sizeof(*list);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	/* Loop over the phandles until all the requested entry is found */
633*4882a593Smuzhiyun 	while (list < list_end) {
634*4882a593Smuzhiyun 		rc = -EINVAL;
635*4882a593Smuzhiyun 		count = 0;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 		/*
638*4882a593Smuzhiyun 		 * If phandle is 0, then it is an empty entry with no
639*4882a593Smuzhiyun 		 * arguments.  Skip forward to the next entry.
640*4882a593Smuzhiyun 		 */
641*4882a593Smuzhiyun 		phandle = be32_to_cpup(list++);
642*4882a593Smuzhiyun 		if (phandle) {
643*4882a593Smuzhiyun 			/*
644*4882a593Smuzhiyun 			 * Find the provider node and parse the #*-cells
645*4882a593Smuzhiyun 			 * property to determine the argument length.
646*4882a593Smuzhiyun 			 *
647*4882a593Smuzhiyun 			 * This is not needed if the cell count is hard-coded
648*4882a593Smuzhiyun 			 * (i.e. cells_name not set, but cell_count is set),
649*4882a593Smuzhiyun 			 * except when we're going to return the found node
650*4882a593Smuzhiyun 			 * below.
651*4882a593Smuzhiyun 			 */
652*4882a593Smuzhiyun 			if (cells_name || cur_index == index) {
653*4882a593Smuzhiyun 				node = of_find_node_by_phandle(phandle);
654*4882a593Smuzhiyun 				if (!node) {
655*4882a593Smuzhiyun 					debug("%s: could not find phandle\n",
656*4882a593Smuzhiyun 					      np->full_name);
657*4882a593Smuzhiyun 					goto err;
658*4882a593Smuzhiyun 				}
659*4882a593Smuzhiyun 			}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 			if (cells_name) {
662*4882a593Smuzhiyun 				if (of_read_u32(node, cells_name, &count)) {
663*4882a593Smuzhiyun 					debug("%s: could not get %s for %s\n",
664*4882a593Smuzhiyun 					      np->full_name, cells_name,
665*4882a593Smuzhiyun 					      node->full_name);
666*4882a593Smuzhiyun 					goto err;
667*4882a593Smuzhiyun 				}
668*4882a593Smuzhiyun 			} else {
669*4882a593Smuzhiyun 				count = cell_count;
670*4882a593Smuzhiyun 			}
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 			/*
673*4882a593Smuzhiyun 			 * Make sure that the arguments actually fit in the
674*4882a593Smuzhiyun 			 * remaining property data length
675*4882a593Smuzhiyun 			 */
676*4882a593Smuzhiyun 			if (list + count > list_end) {
677*4882a593Smuzhiyun 				debug("%s: arguments longer than property\n",
678*4882a593Smuzhiyun 				      np->full_name);
679*4882a593Smuzhiyun 				goto err;
680*4882a593Smuzhiyun 			}
681*4882a593Smuzhiyun 		}
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 		/*
684*4882a593Smuzhiyun 		 * All of the error cases above bail out of the loop, so at
685*4882a593Smuzhiyun 		 * this point, the parsing is successful. If the requested
686*4882a593Smuzhiyun 		 * index matches, then fill the out_args structure and return,
687*4882a593Smuzhiyun 		 * or return -ENOENT for an empty entry.
688*4882a593Smuzhiyun 		 */
689*4882a593Smuzhiyun 		rc = -ENOENT;
690*4882a593Smuzhiyun 		if (cur_index == index) {
691*4882a593Smuzhiyun 			if (!phandle)
692*4882a593Smuzhiyun 				goto err;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 			if (out_args) {
695*4882a593Smuzhiyun 				int i;
696*4882a593Smuzhiyun 				if (WARN_ON(count > OF_MAX_PHANDLE_ARGS))
697*4882a593Smuzhiyun 					count = OF_MAX_PHANDLE_ARGS;
698*4882a593Smuzhiyun 				out_args->np = node;
699*4882a593Smuzhiyun 				out_args->args_count = count;
700*4882a593Smuzhiyun 				for (i = 0; i < count; i++)
701*4882a593Smuzhiyun 					out_args->args[i] =
702*4882a593Smuzhiyun 							be32_to_cpup(list++);
703*4882a593Smuzhiyun 			} else {
704*4882a593Smuzhiyun 				of_node_put(node);
705*4882a593Smuzhiyun 			}
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 			/* Found it! return success */
708*4882a593Smuzhiyun 			return 0;
709*4882a593Smuzhiyun 		}
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		of_node_put(node);
712*4882a593Smuzhiyun 		node = NULL;
713*4882a593Smuzhiyun 		list += count;
714*4882a593Smuzhiyun 		cur_index++;
715*4882a593Smuzhiyun 	}
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	/*
718*4882a593Smuzhiyun 	 * Unlock node before returning result; will be one of:
719*4882a593Smuzhiyun 	 * -ENOENT : index is for empty phandle
720*4882a593Smuzhiyun 	 * -EINVAL : parsing error on data
721*4882a593Smuzhiyun 	 * [1..n]  : Number of phandle (count mode; when index = -1)
722*4882a593Smuzhiyun 	 */
723*4882a593Smuzhiyun 	rc = index < 0 ? cur_index : -ENOENT;
724*4882a593Smuzhiyun  err:
725*4882a593Smuzhiyun 	if (node)
726*4882a593Smuzhiyun 		of_node_put(node);
727*4882a593Smuzhiyun 	return rc;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
of_parse_phandle(const struct device_node * np,const char * phandle_name,int index)730*4882a593Smuzhiyun struct device_node *of_parse_phandle(const struct device_node *np,
731*4882a593Smuzhiyun 				     const char *phandle_name, int index)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	struct of_phandle_args args;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	if (index < 0)
736*4882a593Smuzhiyun 		return NULL;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index,
739*4882a593Smuzhiyun 					 &args))
740*4882a593Smuzhiyun 		return NULL;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	return args.np;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun 
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)745*4882a593Smuzhiyun int of_parse_phandle_with_args(const struct device_node *np,
746*4882a593Smuzhiyun 			       const char *list_name, const char *cells_name,
747*4882a593Smuzhiyun 			       int index, struct of_phandle_args *out_args)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	if (index < 0)
750*4882a593Smuzhiyun 		return -EINVAL;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
753*4882a593Smuzhiyun 					    index, out_args);
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
of_count_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name)756*4882a593Smuzhiyun int of_count_phandle_with_args(const struct device_node *np,
757*4882a593Smuzhiyun 			       const char *list_name, const char *cells_name)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun 	return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
760*4882a593Smuzhiyun 					    -1, NULL);
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
of_alias_add(struct alias_prop * ap,struct device_node * np,int id,const char * stem,int stem_len)763*4882a593Smuzhiyun static void of_alias_add(struct alias_prop *ap, struct device_node *np,
764*4882a593Smuzhiyun 			 int id, const char *stem, int stem_len)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	struct alias_prop *oldap;
767*4882a593Smuzhiyun 	ap->np = np;
768*4882a593Smuzhiyun 	ap->id = id;
769*4882a593Smuzhiyun 	strncpy(ap->stem, stem, stem_len);
770*4882a593Smuzhiyun 	ap->stem[stem_len] = 0;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	/* Delete U-Boot alias which is same with kernel */
773*4882a593Smuzhiyun 	mutex_lock(&of_mutex);
774*4882a593Smuzhiyun 	list_for_each_entry(oldap, &aliases_lookup, link) {
775*4882a593Smuzhiyun 		if (stem && !strcmp(stem, oldap->alias) && (id == oldap->id)) {
776*4882a593Smuzhiyun 			/* Always use from U-Boot aliase */
777*4882a593Smuzhiyun 			if (strcmp(stem, "mmc"))
778*4882a593Smuzhiyun 				continue;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 			list_del(&oldap->link);
781*4882a593Smuzhiyun 			break;
782*4882a593Smuzhiyun 		}
783*4882a593Smuzhiyun 	}
784*4882a593Smuzhiyun 	mutex_unlock(&of_mutex);
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	list_add_tail(&ap->link, &aliases_lookup);
787*4882a593Smuzhiyun 	debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
788*4882a593Smuzhiyun 	      ap->alias, ap->stem, ap->id, of_node_full_name(np));
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun 
of_alias_scan(void)791*4882a593Smuzhiyun int of_alias_scan(void)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun 	struct property *pp;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	of_aliases = of_find_node_by_path("/aliases");
796*4882a593Smuzhiyun 	of_chosen = of_find_node_by_path("/chosen");
797*4882a593Smuzhiyun 	if (of_chosen == NULL)
798*4882a593Smuzhiyun 		of_chosen = of_find_node_by_path("/chosen@0");
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	if (of_chosen) {
801*4882a593Smuzhiyun 		const char *name;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 		name = of_get_property(of_chosen, "stdout-path", NULL);
804*4882a593Smuzhiyun 		if (name)
805*4882a593Smuzhiyun 			of_stdout = of_find_node_opts_by_path(name,
806*4882a593Smuzhiyun 							&of_stdout_options);
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (!of_aliases)
810*4882a593Smuzhiyun 		return 0;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	for_each_property_of_node(of_aliases, pp) {
813*4882a593Smuzhiyun 		const char *start = pp->name;
814*4882a593Smuzhiyun 		const char *end = start + strlen(start);
815*4882a593Smuzhiyun 		struct device_node *np;
816*4882a593Smuzhiyun 		struct alias_prop *ap;
817*4882a593Smuzhiyun 		ulong id;
818*4882a593Smuzhiyun 		int len;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 		/* Skip those we do not want to proceed */
821*4882a593Smuzhiyun 		if (!strcmp(pp->name, "name") ||
822*4882a593Smuzhiyun 		    !strcmp(pp->name, "phandle") ||
823*4882a593Smuzhiyun 		    !strcmp(pp->name, "linux,phandle"))
824*4882a593Smuzhiyun 			continue;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 		np = of_find_node_by_path(pp->value);
827*4882a593Smuzhiyun 		if (!np)
828*4882a593Smuzhiyun 			continue;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 		/*
831*4882a593Smuzhiyun 		 * walk the alias backwards to extract the id and work out
832*4882a593Smuzhiyun 		 * the 'stem' string
833*4882a593Smuzhiyun 		 */
834*4882a593Smuzhiyun 		while (isdigit(*(end-1)) && end > start)
835*4882a593Smuzhiyun 			end--;
836*4882a593Smuzhiyun 		len = end - start;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 		if (strict_strtoul(end, 10, &id) < 0)
839*4882a593Smuzhiyun 			continue;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 		/* Allocate an alias_prop with enough space for the stem */
842*4882a593Smuzhiyun 		ap = malloc(sizeof(*ap) + len + 1);
843*4882a593Smuzhiyun 		if (!ap)
844*4882a593Smuzhiyun 			return -ENOMEM;
845*4882a593Smuzhiyun 		memset(ap, 0, sizeof(*ap) + len + 1);
846*4882a593Smuzhiyun 		ap->alias = start;
847*4882a593Smuzhiyun 		of_alias_add(ap, np, id, start, len);
848*4882a593Smuzhiyun 	}
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	return 0;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
of_alias_get_id(const struct device_node * np,const char * stem)853*4882a593Smuzhiyun int of_alias_get_id(const struct device_node *np, const char *stem)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	struct alias_prop *app;
856*4882a593Smuzhiyun 	int id = -ENODEV;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	mutex_lock(&of_mutex);
859*4882a593Smuzhiyun 	list_for_each_entry(app, &aliases_lookup, link) {
860*4882a593Smuzhiyun 		if (strcmp(app->stem, stem) != 0)
861*4882a593Smuzhiyun 			continue;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 		if (np == app->np) {
864*4882a593Smuzhiyun 			id = app->id;
865*4882a593Smuzhiyun 			break;
866*4882a593Smuzhiyun 		}
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 	mutex_unlock(&of_mutex);
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	return id;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
of_alias_get_dev(const char * stem,int id)873*4882a593Smuzhiyun struct device_node *of_alias_get_dev(const char *stem, int id)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun 	struct alias_prop *app;
876*4882a593Smuzhiyun 	struct device_node *np = NULL;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	mutex_lock(&of_mutex);
879*4882a593Smuzhiyun 	list_for_each_entry(app, &aliases_lookup, link) {
880*4882a593Smuzhiyun 		if (strcmp(app->stem, stem) != 0)
881*4882a593Smuzhiyun 			continue;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 		if (id == app->id) {
884*4882a593Smuzhiyun 			np = app->np;
885*4882a593Smuzhiyun 			break;
886*4882a593Smuzhiyun 		}
887*4882a593Smuzhiyun 	}
888*4882a593Smuzhiyun 	mutex_unlock(&of_mutex);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	return np;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun 
of_alias_dump(void)893*4882a593Smuzhiyun struct device_node *of_alias_dump(void)
894*4882a593Smuzhiyun {
895*4882a593Smuzhiyun 	struct alias_prop *app;
896*4882a593Smuzhiyun 	struct device_node *np = NULL;
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	mutex_lock(&of_mutex);
899*4882a593Smuzhiyun 	list_for_each_entry(app, &aliases_lookup, link) {
900*4882a593Smuzhiyun 		printf("%10s%d: %20s, phandle=%d %4s\n",
901*4882a593Smuzhiyun 		       app->stem, app->id,
902*4882a593Smuzhiyun 		       app->np->full_name, app->np->phandle,
903*4882a593Smuzhiyun 		       of_get_property(app->np, "u-boot,dm-pre-reloc", NULL) ||
904*4882a593Smuzhiyun 		       of_get_property(app->np, "u-boot,dm-spl", NULL) ? "*" : "");
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 	mutex_unlock(&of_mutex);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	return np;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun 
of_get_stdout(void)911*4882a593Smuzhiyun struct device_node *of_get_stdout(void)
912*4882a593Smuzhiyun {
913*4882a593Smuzhiyun 	return of_stdout;
914*4882a593Smuzhiyun }
915