1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * drivers/of/property.c - Procedures for accessing and interpreting
4  *			   Devicetree properties and graphs.
5  *
6  * Initially created by copying procedures from drivers/of/base.c. This
7  * file contains the OF property as well as the OF graph interface
8  * functions.
9  *
10  * Paul Mackerras	August 1996.
11  * Copyright (C) 1996-2005 Paul Mackerras.
12  *
13  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
14  *    {engebret|bergner}@us.ibm.com
15  *
16  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
17  *
18  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
19  *  Grant Likely.
20  */
21 
22 #define pr_fmt(fmt)	"OF: " fmt
23 
24 #include <linux/of.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/of_graph.h>
28 #include <linux/of_irq.h>
29 #include <linux/string.h>
30 #include <linux/moduleparam.h>
31 
32 #include "of_private.h"
33 
34 /**
35  * of_property_read_bool - Find a property
36  * @np:		device node from which the property value is to be read.
37  * @propname:	name of the property to be searched.
38  *
39  * Search for a boolean property in a device node. Usage on non-boolean
40  * property types is deprecated.
41  *
42  * Return: true if the property exists false otherwise.
43  */
of_property_read_bool(const struct device_node * np,const char * propname)44 bool of_property_read_bool(const struct device_node *np, const char *propname)
45 {
46 	struct property *prop = of_find_property(np, propname, NULL);
47 
48 	/*
49 	 * Boolean properties should not have a value. Testing for property
50 	 * presence should either use of_property_present() or just read the
51 	 * property value and check the returned error code.
52 	 */
53 	if (prop && prop->length)
54 		pr_warn("%pOF: Read of boolean property '%s' with a value.\n", np, propname);
55 
56 	return prop ? true : false;
57 }
58 EXPORT_SYMBOL(of_property_read_bool);
59 
60 /**
61  * of_graph_is_present() - check graph's presence
62  * @node: pointer to device_node containing graph port
63  *
64  * Return: True if @node has a port or ports (with a port) sub-node,
65  * false otherwise.
66  */
of_graph_is_present(const struct device_node * node)67 bool of_graph_is_present(const struct device_node *node)
68 {
69 	struct device_node *ports __free(device_node) = of_get_child_by_name(node, "ports");
70 
71 	if (ports)
72 		node = ports;
73 
74 	struct device_node *port __free(device_node) = of_get_child_by_name(node, "port");
75 
76 	return !!port;
77 }
78 EXPORT_SYMBOL(of_graph_is_present);
79 
80 /**
81  * of_property_count_elems_of_size - Count the number of elements in a property
82  *
83  * @np:		device node from which the property value is to be read.
84  * @propname:	name of the property to be searched.
85  * @elem_size:	size of the individual element
86  *
87  * Search for a property in a device node and count the number of elements of
88  * size elem_size in it.
89  *
90  * Return: The number of elements on sucess, -EINVAL if the property does not
91  * exist or its length does not match a multiple of elem_size and -ENODATA if
92  * the property does not have a value.
93  */
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)94 int of_property_count_elems_of_size(const struct device_node *np,
95 				const char *propname, int elem_size)
96 {
97 	const struct property *prop = of_find_property(np, propname, NULL);
98 
99 	if (!prop)
100 		return -EINVAL;
101 	if (!prop->value)
102 		return -ENODATA;
103 
104 	if (prop->length % elem_size != 0) {
105 		pr_err("size of %s in node %pOF is not a multiple of %d\n",
106 		       propname, np, elem_size);
107 		return -EINVAL;
108 	}
109 
110 	return prop->length / elem_size;
111 }
112 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
113 
114 /**
115  * of_find_property_value_of_size
116  *
117  * @np:		device node from which the property value is to be read.
118  * @propname:	name of the property to be searched.
119  * @min:	minimum allowed length of property value
120  * @max:	maximum allowed length of property value (0 means unlimited)
121  * @len:	if !=NULL, actual length is written to here
122  *
123  * Search for a property in a device node and valid the requested size.
124  *
125  * Return: The property value on success, -EINVAL if the property does not
126  * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
127  * property data is too small or too large.
128  *
129  */
of_find_property_value_of_size(const struct device_node * np,const char * propname,u32 min,u32 max,size_t * len)130 static void *of_find_property_value_of_size(const struct device_node *np,
131 			const char *propname, u32 min, u32 max, size_t *len)
132 {
133 	const struct property *prop = of_find_property(np, propname, NULL);
134 
135 	if (!prop)
136 		return ERR_PTR(-EINVAL);
137 	if (!prop->value)
138 		return ERR_PTR(-ENODATA);
139 	if (prop->length < min)
140 		return ERR_PTR(-EOVERFLOW);
141 	if (max && prop->length > max)
142 		return ERR_PTR(-EOVERFLOW);
143 
144 	if (len)
145 		*len = prop->length;
146 
147 	return prop->value;
148 }
149 
150 /**
151  * of_property_read_u16_index - Find and read a u16 from a multi-value property.
152  *
153  * @np:		device node from which the property value is to be read.
154  * @propname:	name of the property to be searched.
155  * @index:	index of the u16 in the list of values
156  * @out_value:	pointer to return value, modified only if no error.
157  *
158  * Search for a property in a device node and read nth 16-bit value from
159  * it.
160  *
161  * Return: 0 on success, -EINVAL if the property does not exist,
162  * -ENODATA if property does not have a value, and -EOVERFLOW if the
163  * property data isn't large enough.
164  *
165  * The out_value is modified only if a valid u16 value can be decoded.
166  */
of_property_read_u16_index(const struct device_node * np,const char * propname,u32 index,u16 * out_value)167 int of_property_read_u16_index(const struct device_node *np,
168 				       const char *propname,
169 				       u32 index, u16 *out_value)
170 {
171 	const u16 *val = of_find_property_value_of_size(np, propname,
172 					((index + 1) * sizeof(*out_value)),
173 					0, NULL);
174 
175 	if (IS_ERR(val))
176 		return PTR_ERR(val);
177 
178 	*out_value = be16_to_cpup(((__be16 *)val) + index);
179 	return 0;
180 }
181 EXPORT_SYMBOL_GPL(of_property_read_u16_index);
182 
183 /**
184  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
185  *
186  * @np:		device node from which the property value is to be read.
187  * @propname:	name of the property to be searched.
188  * @index:	index of the u32 in the list of values
189  * @out_value:	pointer to return value, modified only if no error.
190  *
191  * Search for a property in a device node and read nth 32-bit value from
192  * it.
193  *
194  * Return: 0 on success, -EINVAL if the property does not exist,
195  * -ENODATA if property does not have a value, and -EOVERFLOW if the
196  * property data isn't large enough.
197  *
198  * The out_value is modified only if a valid u32 value can be decoded.
199  */
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)200 int of_property_read_u32_index(const struct device_node *np,
201 				       const char *propname,
202 				       u32 index, u32 *out_value)
203 {
204 	const u32 *val = of_find_property_value_of_size(np, propname,
205 					((index + 1) * sizeof(*out_value)),
206 					0,
207 					NULL);
208 
209 	if (IS_ERR(val))
210 		return PTR_ERR(val);
211 
212 	*out_value = be32_to_cpup(((__be32 *)val) + index);
213 	return 0;
214 }
215 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
216 
217 /**
218  * of_property_read_u64_index - Find and read a u64 from a multi-value property.
219  *
220  * @np:		device node from which the property value is to be read.
221  * @propname:	name of the property to be searched.
222  * @index:	index of the u64 in the list of values
223  * @out_value:	pointer to return value, modified only if no error.
224  *
225  * Search for a property in a device node and read nth 64-bit value from
226  * it.
227  *
228  * Return: 0 on success, -EINVAL if the property does not exist,
229  * -ENODATA if property does not have a value, and -EOVERFLOW if the
230  * property data isn't large enough.
231  *
232  * The out_value is modified only if a valid u64 value can be decoded.
233  */
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)234 int of_property_read_u64_index(const struct device_node *np,
235 				       const char *propname,
236 				       u32 index, u64 *out_value)
237 {
238 	const u64 *val = of_find_property_value_of_size(np, propname,
239 					((index + 1) * sizeof(*out_value)),
240 					0, NULL);
241 
242 	if (IS_ERR(val))
243 		return PTR_ERR(val);
244 
245 	*out_value = be64_to_cpup(((__be64 *)val) + index);
246 	return 0;
247 }
248 EXPORT_SYMBOL_GPL(of_property_read_u64_index);
249 
250 /**
251  * of_property_read_variable_u8_array - Find and read an array of u8 from a
252  * property, with bounds on the minimum and maximum array size.
253  *
254  * @np:		device node from which the property value is to be read.
255  * @propname:	name of the property to be searched.
256  * @out_values:	pointer to found values.
257  * @sz_min:	minimum number of array elements to read
258  * @sz_max:	maximum number of array elements to read, if zero there is no
259  *		upper limit on the number of elements in the dts entry but only
260  *		sz_min will be read.
261  *
262  * Search for a property in a device node and read 8-bit value(s) from
263  * it.
264  *
265  * dts entry of array should be like:
266  *  ``property = /bits/ 8 <0x50 0x60 0x70>;``
267  *
268  * Return: The number of elements read on success, -EINVAL if the property
269  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
270  * if the property data is smaller than sz_min or longer than sz_max.
271  *
272  * The out_values is modified only if a valid u8 value can be decoded.
273  */
of_property_read_variable_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz_min,size_t sz_max)274 int of_property_read_variable_u8_array(const struct device_node *np,
275 					const char *propname, u8 *out_values,
276 					size_t sz_min, size_t sz_max)
277 {
278 	size_t sz, count;
279 	const u8 *val = of_find_property_value_of_size(np, propname,
280 						(sz_min * sizeof(*out_values)),
281 						(sz_max * sizeof(*out_values)),
282 						&sz);
283 
284 	if (IS_ERR(val))
285 		return PTR_ERR(val);
286 
287 	if (!sz_max)
288 		sz = sz_min;
289 	else
290 		sz /= sizeof(*out_values);
291 
292 	count = sz;
293 	while (count--)
294 		*out_values++ = *val++;
295 
296 	return sz;
297 }
298 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
299 
300 /**
301  * of_property_read_variable_u16_array - Find and read an array of u16 from a
302  * property, with bounds on the minimum and maximum array size.
303  *
304  * @np:		device node from which the property value is to be read.
305  * @propname:	name of the property to be searched.
306  * @out_values:	pointer to found values.
307  * @sz_min:	minimum number of array elements to read
308  * @sz_max:	maximum number of array elements to read, if zero there is no
309  *		upper limit on the number of elements in the dts entry but only
310  *		sz_min will be read.
311  *
312  * Search for a property in a device node and read 16-bit value(s) from
313  * it.
314  *
315  * dts entry of array should be like:
316  *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
317  *
318  * Return: The number of elements read on success, -EINVAL if the property
319  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
320  * if the property data is smaller than sz_min or longer than sz_max.
321  *
322  * The out_values is modified only if a valid u16 value can be decoded.
323  */
of_property_read_variable_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz_min,size_t sz_max)324 int of_property_read_variable_u16_array(const struct device_node *np,
325 					const char *propname, u16 *out_values,
326 					size_t sz_min, size_t sz_max)
327 {
328 	size_t sz, count;
329 	const __be16 *val = of_find_property_value_of_size(np, propname,
330 						(sz_min * sizeof(*out_values)),
331 						(sz_max * sizeof(*out_values)),
332 						&sz);
333 
334 	if (IS_ERR(val))
335 		return PTR_ERR(val);
336 
337 	if (!sz_max)
338 		sz = sz_min;
339 	else
340 		sz /= sizeof(*out_values);
341 
342 	count = sz;
343 	while (count--)
344 		*out_values++ = be16_to_cpup(val++);
345 
346 	return sz;
347 }
348 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
349 
350 /**
351  * of_property_read_variable_u32_array - Find and read an array of 32 bit
352  * integers from a property, with bounds on the minimum and maximum array size.
353  *
354  * @np:		device node from which the property value is to be read.
355  * @propname:	name of the property to be searched.
356  * @out_values:	pointer to return found values.
357  * @sz_min:	minimum number of array elements to read
358  * @sz_max:	maximum number of array elements to read, if zero there is no
359  *		upper limit on the number of elements in the dts entry but only
360  *		sz_min will be read.
361  *
362  * Search for a property in a device node and read 32-bit value(s) from
363  * it.
364  *
365  * Return: The number of elements read on success, -EINVAL if the property
366  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
367  * if the property data is smaller than sz_min or longer than sz_max.
368  *
369  * The out_values is modified only if a valid u32 value can be decoded.
370  */
of_property_read_variable_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz_min,size_t sz_max)371 int of_property_read_variable_u32_array(const struct device_node *np,
372 			       const char *propname, u32 *out_values,
373 			       size_t sz_min, size_t sz_max)
374 {
375 	size_t sz, count;
376 	const __be32 *val = of_find_property_value_of_size(np, propname,
377 						(sz_min * sizeof(*out_values)),
378 						(sz_max * sizeof(*out_values)),
379 						&sz);
380 
381 	if (IS_ERR(val))
382 		return PTR_ERR(val);
383 
384 	if (!sz_max)
385 		sz = sz_min;
386 	else
387 		sz /= sizeof(*out_values);
388 
389 	count = sz;
390 	while (count--)
391 		*out_values++ = be32_to_cpup(val++);
392 
393 	return sz;
394 }
395 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
396 
397 /**
398  * of_property_read_u64 - Find and read a 64 bit integer from a property
399  * @np:		device node from which the property value is to be read.
400  * @propname:	name of the property to be searched.
401  * @out_value:	pointer to return value, modified only if return value is 0.
402  *
403  * Search for a property in a device node and read a 64-bit value from
404  * it.
405  *
406  * Return: 0 on success, -EINVAL if the property does not exist,
407  * -ENODATA if property does not have a value, and -EOVERFLOW if the
408  * property data isn't large enough.
409  *
410  * The out_value is modified only if a valid u64 value can be decoded.
411  */
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)412 int of_property_read_u64(const struct device_node *np, const char *propname,
413 			 u64 *out_value)
414 {
415 	const __be32 *val = of_find_property_value_of_size(np, propname,
416 						sizeof(*out_value),
417 						0,
418 						NULL);
419 
420 	if (IS_ERR(val))
421 		return PTR_ERR(val);
422 
423 	*out_value = of_read_number(val, 2);
424 	return 0;
425 }
426 EXPORT_SYMBOL_GPL(of_property_read_u64);
427 
428 /**
429  * of_property_read_variable_u64_array - Find and read an array of 64 bit
430  * integers from a property, with bounds on the minimum and maximum array size.
431  *
432  * @np:		device node from which the property value is to be read.
433  * @propname:	name of the property to be searched.
434  * @out_values:	pointer to found values.
435  * @sz_min:	minimum number of array elements to read
436  * @sz_max:	maximum number of array elements to read, if zero there is no
437  *		upper limit on the number of elements in the dts entry but only
438  *		sz_min will be read.
439  *
440  * Search for a property in a device node and read 64-bit value(s) from
441  * it.
442  *
443  * Return: The number of elements read on success, -EINVAL if the property
444  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
445  * if the property data is smaller than sz_min or longer than sz_max.
446  *
447  * The out_values is modified only if a valid u64 value can be decoded.
448  */
of_property_read_variable_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz_min,size_t sz_max)449 int of_property_read_variable_u64_array(const struct device_node *np,
450 			       const char *propname, u64 *out_values,
451 			       size_t sz_min, size_t sz_max)
452 {
453 	size_t sz, count;
454 	const __be32 *val = of_find_property_value_of_size(np, propname,
455 						(sz_min * sizeof(*out_values)),
456 						(sz_max * sizeof(*out_values)),
457 						&sz);
458 
459 	if (IS_ERR(val))
460 		return PTR_ERR(val);
461 
462 	if (!sz_max)
463 		sz = sz_min;
464 	else
465 		sz /= sizeof(*out_values);
466 
467 	count = sz;
468 	while (count--) {
469 		*out_values++ = of_read_number(val, 2);
470 		val += 2;
471 	}
472 
473 	return sz;
474 }
475 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
476 
477 /**
478  * of_property_read_string - Find and read a string from a property
479  * @np:		device node from which the property value is to be read.
480  * @propname:	name of the property to be searched.
481  * @out_string:	pointer to null terminated return string, modified only if
482  *		return value is 0.
483  *
484  * Search for a property in a device tree node and retrieve a null
485  * terminated string value (pointer to data, not a copy).
486  *
487  * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
488  * property does not have a value, and -EILSEQ if the string is not
489  * null-terminated within the length of the property data.
490  *
491  * Note that the empty string "" has length of 1, thus -ENODATA cannot
492  * be interpreted as an empty string.
493  *
494  * The out_string pointer is modified only if a valid string can be decoded.
495  */
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)496 int of_property_read_string(const struct device_node *np, const char *propname,
497 				const char **out_string)
498 {
499 	const struct property *prop = of_find_property(np, propname, NULL);
500 
501 	if (!prop)
502 		return -EINVAL;
503 	if (!prop->length)
504 		return -ENODATA;
505 	if (strnlen(prop->value, prop->length) >= prop->length)
506 		return -EILSEQ;
507 	*out_string = prop->value;
508 	return 0;
509 }
510 EXPORT_SYMBOL_GPL(of_property_read_string);
511 
512 /**
513  * of_property_match_string() - Find string in a list and return index
514  * @np: pointer to the node containing the string list property
515  * @propname: string list property name
516  * @string: pointer to the string to search for in the string list
517  *
518  * Search for an exact match of string in a device node property which is a
519  * string of lists.
520  *
521  * Return: the index of the first occurrence of the string on success, -EINVAL
522  * if the property does not exist, -ENODATA if the property does not have a
523  * value, and -EILSEQ if the string is not null-terminated within the length of
524  * the property data.
525  */
of_property_match_string(const struct device_node * np,const char * propname,const char * string)526 int of_property_match_string(const struct device_node *np, const char *propname,
527 			     const char *string)
528 {
529 	const struct property *prop = of_find_property(np, propname, NULL);
530 	size_t l;
531 	int i;
532 	const char *p, *end;
533 
534 	if (!prop)
535 		return -EINVAL;
536 	if (!prop->value)
537 		return -ENODATA;
538 
539 	p = prop->value;
540 	end = p + prop->length;
541 
542 	for (i = 0; p < end; i++, p += l) {
543 		l = strnlen(p, end - p) + 1;
544 		if (p + l > end)
545 			return -EILSEQ;
546 		pr_debug("comparing %s with %s\n", string, p);
547 		if (strcmp(string, p) == 0)
548 			return i; /* Found it; return index */
549 	}
550 	return -ENODATA;
551 }
552 EXPORT_SYMBOL_GPL(of_property_match_string);
553 
554 /**
555  * of_property_read_string_helper() - Utility helper for parsing string properties
556  * @np:		device node from which the property value is to be read.
557  * @propname:	name of the property to be searched.
558  * @out_strs:	output array of string pointers.
559  * @sz:		number of array elements to read.
560  * @skip:	Number of strings to skip over at beginning of list.
561  *
562  * Don't call this function directly. It is a utility helper for the
563  * of_property_read_string*() family of functions.
564  */
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int skip)565 int of_property_read_string_helper(const struct device_node *np,
566 				   const char *propname, const char **out_strs,
567 				   size_t sz, int skip)
568 {
569 	const struct property *prop = of_find_property(np, propname, NULL);
570 	int l = 0, i = 0;
571 	const char *p, *end;
572 
573 	if (!prop)
574 		return -EINVAL;
575 	if (!prop->value)
576 		return -ENODATA;
577 	p = prop->value;
578 	end = p + prop->length;
579 
580 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
581 		l = strnlen(p, end - p) + 1;
582 		if (p + l > end)
583 			return -EILSEQ;
584 		if (out_strs && i >= skip)
585 			*out_strs++ = p;
586 	}
587 	i -= skip;
588 	return i <= 0 ? -ENODATA : i;
589 }
590 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
591 
of_prop_next_u32(const struct property * prop,const __be32 * cur,u32 * pu)592 const __be32 *of_prop_next_u32(const struct property *prop, const __be32 *cur,
593 			       u32 *pu)
594 {
595 	const void *curv = cur;
596 
597 	if (!prop)
598 		return NULL;
599 
600 	if (!cur) {
601 		curv = prop->value;
602 		goto out_val;
603 	}
604 
605 	curv += sizeof(*cur);
606 	if (curv >= prop->value + prop->length)
607 		return NULL;
608 
609 out_val:
610 	*pu = be32_to_cpup(curv);
611 	return curv;
612 }
613 EXPORT_SYMBOL_GPL(of_prop_next_u32);
614 
of_prop_next_string(const struct property * prop,const char * cur)615 const char *of_prop_next_string(const struct property *prop, const char *cur)
616 {
617 	const void *curv = cur;
618 
619 	if (!prop)
620 		return NULL;
621 
622 	if (!cur)
623 		return prop->value;
624 
625 	curv += strlen(cur) + 1;
626 	if (curv >= prop->value + prop->length)
627 		return NULL;
628 
629 	return curv;
630 }
631 EXPORT_SYMBOL_GPL(of_prop_next_string);
632 
633 /**
634  * of_graph_parse_endpoint() - parse common endpoint node properties
635  * @node: pointer to endpoint device_node
636  * @endpoint: pointer to the OF endpoint data structure
637  *
638  * The caller should hold a reference to @node.
639  */
of_graph_parse_endpoint(const struct device_node * node,struct of_endpoint * endpoint)640 int of_graph_parse_endpoint(const struct device_node *node,
641 			    struct of_endpoint *endpoint)
642 {
643 	struct device_node *port_node __free(device_node) =
644 			    of_get_parent(node);
645 
646 	WARN_ONCE(!port_node, "%s(): endpoint %pOF has no parent node\n",
647 		  __func__, node);
648 
649 	memset(endpoint, 0, sizeof(*endpoint));
650 
651 	endpoint->local_node = node;
652 	/*
653 	 * It doesn't matter whether the two calls below succeed.
654 	 * If they don't then the default value 0 is used.
655 	 */
656 	of_property_read_u32(port_node, "reg", &endpoint->port);
657 	of_property_read_u32(node, "reg", &endpoint->id);
658 
659 	return 0;
660 }
661 EXPORT_SYMBOL(of_graph_parse_endpoint);
662 
663 /**
664  * of_graph_get_port_by_id() - get the port matching a given id
665  * @parent: pointer to the parent device node
666  * @id: id of the port
667  *
668  * Return: A 'port' node pointer with refcount incremented. The caller
669  * has to use of_node_put() on it when done.
670  */
of_graph_get_port_by_id(struct device_node * parent,u32 id)671 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
672 {
673 	struct device_node *node __free(device_node) = of_get_child_by_name(parent, "ports");
674 
675 	if (node)
676 		parent = node;
677 
678 	for_each_child_of_node_scoped(parent, port) {
679 		u32 port_id = 0;
680 
681 		if (!of_node_name_eq(port, "port"))
682 			continue;
683 		of_property_read_u32(port, "reg", &port_id);
684 		if (id == port_id)
685 			return_ptr(port);
686 	}
687 
688 	return NULL;
689 }
690 EXPORT_SYMBOL(of_graph_get_port_by_id);
691 
692 /**
693  * of_graph_get_next_port() - get next port node.
694  * @parent: pointer to the parent device node, or parent ports node
695  * @prev: previous port node, or NULL to get first
696  *
697  * Parent device node can be used as @parent whether device node has ports node
698  * or not. It will work same as ports@0 node.
699  *
700  * Return: A 'port' node pointer with refcount incremented. Refcount
701  * of the passed @prev node is decremented.
702  */
of_graph_get_next_port(const struct device_node * parent,struct device_node * prev)703 struct device_node *of_graph_get_next_port(const struct device_node *parent,
704 					   struct device_node *prev)
705 {
706 	if (!parent)
707 		return NULL;
708 
709 	if (!prev) {
710 		struct device_node *node __free(device_node) =
711 			of_get_child_by_name(parent, "ports");
712 
713 		if (node)
714 			parent = node;
715 
716 		return of_get_child_by_name(parent, "port");
717 	}
718 
719 	do {
720 		prev = of_get_next_child(parent, prev);
721 		if (!prev)
722 			break;
723 	} while (!of_node_name_eq(prev, "port"));
724 
725 	return prev;
726 }
727 EXPORT_SYMBOL(of_graph_get_next_port);
728 
729 /**
730  * of_graph_get_next_port_endpoint() - get next endpoint node in port.
731  * If it reached to end of the port, it will return NULL.
732  * @port: pointer to the target port node
733  * @prev: previous endpoint node, or NULL to get first
734  *
735  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
736  * of the passed @prev node is decremented.
737  */
of_graph_get_next_port_endpoint(const struct device_node * port,struct device_node * prev)738 struct device_node *of_graph_get_next_port_endpoint(const struct device_node *port,
739 						    struct device_node *prev)
740 {
741 	while (1) {
742 		prev = of_get_next_child(port, prev);
743 		if (!prev)
744 			break;
745 		if (WARN(!of_node_name_eq(prev, "endpoint"),
746 			 "non endpoint node is used (%pOF)", prev))
747 			continue;
748 
749 		break;
750 	}
751 
752 	return prev;
753 }
754 EXPORT_SYMBOL(of_graph_get_next_port_endpoint);
755 
756 /**
757  * of_graph_get_next_endpoint() - get next endpoint node
758  * @parent: pointer to the parent device node
759  * @prev: previous endpoint node, or NULL to get first
760  *
761  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
762  * of the passed @prev node is decremented.
763  */
of_graph_get_next_endpoint(const struct device_node * parent,struct device_node * prev)764 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
765 					struct device_node *prev)
766 {
767 	struct device_node *endpoint;
768 	struct device_node *port;
769 
770 	if (!parent)
771 		return NULL;
772 
773 	/*
774 	 * Start by locating the port node. If no previous endpoint is specified
775 	 * search for the first port node, otherwise get the previous endpoint
776 	 * parent port node.
777 	 */
778 	if (!prev) {
779 		port = of_graph_get_next_port(parent, NULL);
780 		if (!port) {
781 			pr_debug("graph: no port node found in %pOF\n", parent);
782 			return NULL;
783 		}
784 	} else {
785 		port = of_get_parent(prev);
786 		if (WARN_ONCE(!port, "%s(): endpoint %pOF has no parent node\n",
787 			      __func__, prev))
788 			return NULL;
789 	}
790 
791 	while (1) {
792 		/*
793 		 * Now that we have a port node, get the next endpoint by
794 		 * getting the next child. If the previous endpoint is NULL this
795 		 * will return the first child.
796 		 */
797 		endpoint = of_graph_get_next_port_endpoint(port, prev);
798 		if (endpoint) {
799 			of_node_put(port);
800 			return endpoint;
801 		}
802 
803 		/* No more endpoints under this port, try the next one. */
804 		prev = NULL;
805 
806 		port = of_graph_get_next_port(parent, port);
807 		if (!port)
808 			return NULL;
809 	}
810 }
811 EXPORT_SYMBOL(of_graph_get_next_endpoint);
812 
813 /**
814  * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
815  * @parent: pointer to the parent device node
816  * @port_reg: identifier (value of reg property) of the parent port node
817  * @reg: identifier (value of reg property) of the endpoint node
818  *
819  * Return: An 'endpoint' node pointer which is identified by reg and at the same
820  * is the child of a port node identified by port_reg. reg and port_reg are
821  * ignored when they are -1. Use of_node_put() on the pointer when done.
822  */
of_graph_get_endpoint_by_regs(const struct device_node * parent,int port_reg,int reg)823 struct device_node *of_graph_get_endpoint_by_regs(
824 	const struct device_node *parent, int port_reg, int reg)
825 {
826 	struct of_endpoint endpoint;
827 	struct device_node *node = NULL;
828 
829 	for_each_endpoint_of_node(parent, node) {
830 		of_graph_parse_endpoint(node, &endpoint);
831 		if (((port_reg == -1) || (endpoint.port == port_reg)) &&
832 			((reg == -1) || (endpoint.id == reg)))
833 			return node;
834 	}
835 
836 	return NULL;
837 }
838 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
839 
840 /**
841  * of_graph_get_remote_endpoint() - get remote endpoint node
842  * @node: pointer to a local endpoint device_node
843  *
844  * Return: Remote endpoint node associated with remote endpoint node linked
845  *	   to @node. Use of_node_put() on it when done.
846  */
of_graph_get_remote_endpoint(const struct device_node * node)847 struct device_node *of_graph_get_remote_endpoint(const struct device_node *node)
848 {
849 	/* Get remote endpoint node. */
850 	return of_parse_phandle(node, "remote-endpoint", 0);
851 }
852 EXPORT_SYMBOL(of_graph_get_remote_endpoint);
853 
854 /**
855  * of_graph_get_port_parent() - get port's parent node
856  * @node: pointer to a local endpoint device_node
857  *
858  * Return: device node associated with endpoint node linked
859  *	   to @node. Use of_node_put() on it when done.
860  */
of_graph_get_port_parent(struct device_node * node)861 struct device_node *of_graph_get_port_parent(struct device_node *node)
862 {
863 	unsigned int depth;
864 
865 	if (!node)
866 		return NULL;
867 
868 	/*
869 	 * Preserve usecount for passed in node as of_get_next_parent()
870 	 * will do of_node_put() on it.
871 	 */
872 	of_node_get(node);
873 
874 	/* Walk 3 levels up only if there is 'ports' node. */
875 	for (depth = 3; depth && node; depth--) {
876 		node = of_get_next_parent(node);
877 		if (depth == 2 && !of_node_name_eq(node, "ports") &&
878 		    !of_node_name_eq(node, "in-ports") &&
879 		    !of_node_name_eq(node, "out-ports"))
880 			break;
881 	}
882 	return node;
883 }
884 EXPORT_SYMBOL(of_graph_get_port_parent);
885 
886 /**
887  * of_graph_get_remote_port_parent() - get remote port's parent node
888  * @node: pointer to a local endpoint device_node
889  *
890  * Return: Remote device node associated with remote endpoint node linked
891  *	   to @node. Use of_node_put() on it when done.
892  */
of_graph_get_remote_port_parent(const struct device_node * node)893 struct device_node *of_graph_get_remote_port_parent(
894 			       const struct device_node *node)
895 {
896 	/* Get remote endpoint node. */
897 	struct device_node *np __free(device_node) =
898 		of_graph_get_remote_endpoint(node);
899 
900 	return of_graph_get_port_parent(np);
901 }
902 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
903 
904 /**
905  * of_graph_get_remote_port() - get remote port node
906  * @node: pointer to a local endpoint device_node
907  *
908  * Return: Remote port node associated with remote endpoint node linked
909  * to @node. Use of_node_put() on it when done.
910  */
of_graph_get_remote_port(const struct device_node * node)911 struct device_node *of_graph_get_remote_port(const struct device_node *node)
912 {
913 	struct device_node *np;
914 
915 	/* Get remote endpoint node. */
916 	np = of_graph_get_remote_endpoint(node);
917 	if (!np)
918 		return NULL;
919 	return of_get_next_parent(np);
920 }
921 EXPORT_SYMBOL(of_graph_get_remote_port);
922 
923 /**
924  * of_graph_get_endpoint_count() - get the number of endpoints in a device node
925  * @np: parent device node containing ports and endpoints
926  *
927  * Return: count of endpoint of this device node
928  */
of_graph_get_endpoint_count(const struct device_node * np)929 unsigned int of_graph_get_endpoint_count(const struct device_node *np)
930 {
931 	struct device_node *endpoint;
932 	unsigned int num = 0;
933 
934 	for_each_endpoint_of_node(np, endpoint)
935 		num++;
936 
937 	return num;
938 }
939 EXPORT_SYMBOL(of_graph_get_endpoint_count);
940 
941 /**
942  * of_graph_get_port_count() - get the number of port in a device or ports node
943  * @np: pointer to the device or ports node
944  *
945  * Return: count of port of this device or ports node
946  */
of_graph_get_port_count(struct device_node * np)947 unsigned int of_graph_get_port_count(struct device_node *np)
948 {
949 	unsigned int num = 0;
950 
951 	for_each_of_graph_port(np, port)
952 		num++;
953 
954 	return num;
955 }
956 EXPORT_SYMBOL(of_graph_get_port_count);
957 
958 /**
959  * of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
960  * @node: pointer to parent device_node containing graph port/endpoint
961  * @port: identifier (value of reg property) of the parent port node
962  * @endpoint: identifier (value of reg property) of the endpoint node
963  *
964  * Return: Remote device node associated with remote endpoint node linked
965  * to @node. Use of_node_put() on it when done.
966  */
of_graph_get_remote_node(const struct device_node * node,u32 port,u32 endpoint)967 struct device_node *of_graph_get_remote_node(const struct device_node *node,
968 					     u32 port, u32 endpoint)
969 {
970 	struct device_node *endpoint_node, *remote;
971 
972 	endpoint_node = of_graph_get_endpoint_by_regs(node, port, endpoint);
973 	if (!endpoint_node) {
974 		pr_debug("no valid endpoint (%d, %d) for node %pOF\n",
975 			 port, endpoint, node);
976 		return NULL;
977 	}
978 
979 	remote = of_graph_get_remote_port_parent(endpoint_node);
980 	of_node_put(endpoint_node);
981 	if (!remote) {
982 		pr_debug("no valid remote node\n");
983 		return NULL;
984 	}
985 
986 	if (!of_device_is_available(remote)) {
987 		pr_debug("not available for remote node\n");
988 		of_node_put(remote);
989 		return NULL;
990 	}
991 
992 	return remote;
993 }
994 EXPORT_SYMBOL(of_graph_get_remote_node);
995 
of_fwnode_get(struct fwnode_handle * fwnode)996 static struct fwnode_handle *of_fwnode_get(struct fwnode_handle *fwnode)
997 {
998 	return of_fwnode_handle(of_node_get(to_of_node(fwnode)));
999 }
1000 
of_fwnode_put(struct fwnode_handle * fwnode)1001 static void of_fwnode_put(struct fwnode_handle *fwnode)
1002 {
1003 	of_node_put(to_of_node(fwnode));
1004 }
1005 
of_fwnode_device_is_available(const struct fwnode_handle * fwnode)1006 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1007 {
1008 	return of_device_is_available(to_of_node(fwnode));
1009 }
1010 
of_fwnode_device_dma_supported(const struct fwnode_handle * fwnode)1011 static bool of_fwnode_device_dma_supported(const struct fwnode_handle *fwnode)
1012 {
1013 	return true;
1014 }
1015 
1016 static enum dev_dma_attr
of_fwnode_device_get_dma_attr(const struct fwnode_handle * fwnode)1017 of_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode)
1018 {
1019 	if (of_dma_is_coherent(to_of_node(fwnode)))
1020 		return DEV_DMA_COHERENT;
1021 	else
1022 		return DEV_DMA_NON_COHERENT;
1023 }
1024 
of_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)1025 static bool of_fwnode_property_present(const struct fwnode_handle *fwnode,
1026 				       const char *propname)
1027 {
1028 	return of_property_present(to_of_node(fwnode), propname);
1029 }
1030 
of_fwnode_property_read_bool(const struct fwnode_handle * fwnode,const char * propname)1031 static bool of_fwnode_property_read_bool(const struct fwnode_handle *fwnode,
1032 					 const char *propname)
1033 {
1034 	return of_property_read_bool(to_of_node(fwnode), propname);
1035 }
1036 
of_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)1037 static int of_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1038 					     const char *propname,
1039 					     unsigned int elem_size, void *val,
1040 					     size_t nval)
1041 {
1042 	const struct device_node *node = to_of_node(fwnode);
1043 
1044 	if (!val)
1045 		return of_property_count_elems_of_size(node, propname,
1046 						       elem_size);
1047 
1048 	switch (elem_size) {
1049 	case sizeof(u8):
1050 		return of_property_read_u8_array(node, propname, val, nval);
1051 	case sizeof(u16):
1052 		return of_property_read_u16_array(node, propname, val, nval);
1053 	case sizeof(u32):
1054 		return of_property_read_u32_array(node, propname, val, nval);
1055 	case sizeof(u64):
1056 		return of_property_read_u64_array(node, propname, val, nval);
1057 	}
1058 
1059 	return -ENXIO;
1060 }
1061 
1062 static int
of_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)1063 of_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1064 				     const char *propname, const char **val,
1065 				     size_t nval)
1066 {
1067 	const struct device_node *node = to_of_node(fwnode);
1068 
1069 	return val ?
1070 		of_property_read_string_array(node, propname, val, nval) :
1071 		of_property_count_strings(node, propname);
1072 }
1073 
of_fwnode_get_name(const struct fwnode_handle * fwnode)1074 static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
1075 {
1076 	return kbasename(to_of_node(fwnode)->full_name);
1077 }
1078 
of_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)1079 static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1080 {
1081 	/* Root needs no prefix here (its name is "/"). */
1082 	if (!to_of_node(fwnode)->parent)
1083 		return "";
1084 
1085 	return "/";
1086 }
1087 
1088 static struct fwnode_handle *
of_fwnode_get_parent(const struct fwnode_handle * fwnode)1089 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
1090 {
1091 	return of_fwnode_handle(of_get_parent(to_of_node(fwnode)));
1092 }
1093 
1094 static struct fwnode_handle *
of_fwnode_get_next_child_node(const struct fwnode_handle * fwnode,struct fwnode_handle * child)1095 of_fwnode_get_next_child_node(const struct fwnode_handle *fwnode,
1096 			      struct fwnode_handle *child)
1097 {
1098 	return of_fwnode_handle(of_get_next_available_child(to_of_node(fwnode),
1099 							    to_of_node(child)));
1100 }
1101 
1102 static struct fwnode_handle *
of_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)1103 of_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
1104 			       const char *childname)
1105 {
1106 	const struct device_node *node = to_of_node(fwnode);
1107 	struct device_node *child;
1108 
1109 	for_each_available_child_of_node(node, child)
1110 		if (of_node_name_eq(child, childname))
1111 			return of_fwnode_handle(child);
1112 
1113 	return NULL;
1114 }
1115 
1116 static int
of_fwnode_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int nargs,unsigned int index,struct fwnode_reference_args * args)1117 of_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1118 			     const char *prop, const char *nargs_prop,
1119 			     unsigned int nargs, unsigned int index,
1120 			     struct fwnode_reference_args *args)
1121 {
1122 	struct of_phandle_args of_args;
1123 	unsigned int i;
1124 	int ret;
1125 
1126 	if (nargs_prop)
1127 		ret = of_parse_phandle_with_args(to_of_node(fwnode), prop,
1128 						 nargs_prop, index, &of_args);
1129 	else
1130 		ret = of_parse_phandle_with_fixed_args(to_of_node(fwnode), prop,
1131 						       nargs, index, &of_args);
1132 	if (ret < 0)
1133 		return ret;
1134 	if (!args) {
1135 		of_node_put(of_args.np);
1136 		return 0;
1137 	}
1138 
1139 	args->nargs = of_args.args_count;
1140 	args->fwnode = of_fwnode_handle(of_args.np);
1141 
1142 	for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
1143 		args->args[i] = i < of_args.args_count ? of_args.args[i] : 0;
1144 
1145 	return 0;
1146 }
1147 
1148 static struct fwnode_handle *
of_fwnode_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)1149 of_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
1150 				  struct fwnode_handle *prev)
1151 {
1152 	return of_fwnode_handle(of_graph_get_next_endpoint(to_of_node(fwnode),
1153 							   to_of_node(prev)));
1154 }
1155 
1156 static struct fwnode_handle *
of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle * fwnode)1157 of_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
1158 {
1159 	return of_fwnode_handle(
1160 		of_graph_get_remote_endpoint(to_of_node(fwnode)));
1161 }
1162 
1163 static struct fwnode_handle *
of_fwnode_graph_get_port_parent(struct fwnode_handle * fwnode)1164 of_fwnode_graph_get_port_parent(struct fwnode_handle *fwnode)
1165 {
1166 	struct device_node *np;
1167 
1168 	/* Get the parent of the port */
1169 	np = of_get_parent(to_of_node(fwnode));
1170 	if (!np)
1171 		return NULL;
1172 
1173 	/* Is this the "ports" node? If not, it's the port parent. */
1174 	if (!of_node_name_eq(np, "ports"))
1175 		return of_fwnode_handle(np);
1176 
1177 	return of_fwnode_handle(of_get_next_parent(np));
1178 }
1179 
of_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1180 static int of_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1181 					  struct fwnode_endpoint *endpoint)
1182 {
1183 	const struct device_node *node = to_of_node(fwnode);
1184 	struct device_node *port_node __free(device_node) = of_get_parent(node);
1185 
1186 	endpoint->local_fwnode = fwnode;
1187 
1188 	of_property_read_u32(port_node, "reg", &endpoint->port);
1189 	of_property_read_u32(node, "reg", &endpoint->id);
1190 
1191 	return 0;
1192 }
1193 
1194 static const void *
of_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)1195 of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1196 				const struct device *dev)
1197 {
1198 	return of_device_get_match_data(dev);
1199 }
1200 
of_link_to_phandle(struct device_node * con_np,struct device_node * sup_np,u8 flags)1201 static void of_link_to_phandle(struct device_node *con_np,
1202 			      struct device_node *sup_np,
1203 			      u8 flags)
1204 {
1205 	struct device_node *tmp_np __free(device_node) = of_node_get(sup_np);
1206 
1207 	/* Check that sup_np and its ancestors are available. */
1208 	while (tmp_np) {
1209 		if (of_fwnode_handle(tmp_np)->dev)
1210 			break;
1211 
1212 		if (!of_device_is_available(tmp_np))
1213 			return;
1214 
1215 		tmp_np = of_get_next_parent(tmp_np);
1216 	}
1217 
1218 	fwnode_link_add(of_fwnode_handle(con_np), of_fwnode_handle(sup_np), flags);
1219 }
1220 
1221 /**
1222  * parse_prop_cells - Property parsing function for suppliers
1223  *
1224  * @np:		Pointer to device tree node containing a list
1225  * @prop_name:	Name of property to be parsed. Expected to hold phandle values
1226  * @index:	For properties holding a list of phandles, this is the index
1227  *		into the list.
1228  * @list_name:	Property name that is known to contain list of phandle(s) to
1229  *		supplier(s)
1230  * @cells_name:	property name that specifies phandles' arguments count
1231  *
1232  * This is a helper function to parse properties that have a known fixed name
1233  * and are a list of phandles and phandle arguments.
1234  *
1235  * Returns:
1236  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1237  *   on it when done.
1238  * - NULL if no phandle found at index
1239  */
parse_prop_cells(struct device_node * np,const char * prop_name,int index,const char * list_name,const char * cells_name)1240 static struct device_node *parse_prop_cells(struct device_node *np,
1241 					    const char *prop_name, int index,
1242 					    const char *list_name,
1243 					    const char *cells_name)
1244 {
1245 	struct of_phandle_args sup_args;
1246 
1247 	if (strcmp(prop_name, list_name))
1248 		return NULL;
1249 
1250 	if (__of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1251 					 &sup_args))
1252 		return NULL;
1253 
1254 	return sup_args.np;
1255 }
1256 
1257 #define DEFINE_SIMPLE_PROP(fname, name, cells)				  \
1258 static struct device_node *parse_##fname(struct device_node *np,	  \
1259 					const char *prop_name, int index) \
1260 {									  \
1261 	return parse_prop_cells(np, prop_name, index, name, cells);	  \
1262 }
1263 
strcmp_suffix(const char * str,const char * suffix)1264 static int strcmp_suffix(const char *str, const char *suffix)
1265 {
1266 	unsigned int len, suffix_len;
1267 
1268 	len = strlen(str);
1269 	suffix_len = strlen(suffix);
1270 	if (len <= suffix_len)
1271 		return -1;
1272 	return strcmp(str + len - suffix_len, suffix);
1273 }
1274 
1275 /**
1276  * parse_suffix_prop_cells - Suffix property parsing function for suppliers
1277  *
1278  * @np:		Pointer to device tree node containing a list
1279  * @prop_name:	Name of property to be parsed. Expected to hold phandle values
1280  * @index:	For properties holding a list of phandles, this is the index
1281  *		into the list.
1282  * @suffix:	Property suffix that is known to contain list of phandle(s) to
1283  *		supplier(s)
1284  * @cells_name:	property name that specifies phandles' arguments count
1285  *
1286  * This is a helper function to parse properties that have a known fixed suffix
1287  * and are a list of phandles and phandle arguments.
1288  *
1289  * Returns:
1290  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1291  *   on it when done.
1292  * - NULL if no phandle found at index
1293  */
parse_suffix_prop_cells(struct device_node * np,const char * prop_name,int index,const char * suffix,const char * cells_name)1294 static struct device_node *parse_suffix_prop_cells(struct device_node *np,
1295 					    const char *prop_name, int index,
1296 					    const char *suffix,
1297 					    const char *cells_name)
1298 {
1299 	struct of_phandle_args sup_args;
1300 
1301 	if (strcmp_suffix(prop_name, suffix))
1302 		return NULL;
1303 
1304 	if (of_parse_phandle_with_args(np, prop_name, cells_name, index,
1305 				       &sup_args))
1306 		return NULL;
1307 
1308 	return sup_args.np;
1309 }
1310 
1311 #define DEFINE_SUFFIX_PROP(fname, suffix, cells)			     \
1312 static struct device_node *parse_##fname(struct device_node *np,	     \
1313 					const char *prop_name, int index)    \
1314 {									     \
1315 	return parse_suffix_prop_cells(np, prop_name, index, suffix, cells); \
1316 }
1317 
1318 /**
1319  * struct supplier_bindings - Property parsing functions for suppliers
1320  *
1321  * @parse_prop: function name
1322  *	parse_prop() finds the node corresponding to a supplier phandle
1323  *  parse_prop.np: Pointer to device node holding supplier phandle property
1324  *  parse_prop.prop_name: Name of property holding a phandle value
1325  *  parse_prop.index: For properties holding a list of phandles, this is the
1326  *		      index into the list
1327  * @get_con_dev: If the consumer node containing the property is never converted
1328  *		 to a struct device, implement this ops so fw_devlink can use it
1329  *		 to find the true consumer.
1330  * @optional: Describes whether a supplier is mandatory or not
1331  * @fwlink_flags: Optional fwnode link flags to use when creating a fwnode link
1332  *		  for this property.
1333  *
1334  * Returns:
1335  * parse_prop() return values are
1336  * - phandle node pointer with refcount incremented. Caller must of_node_put()
1337  *   on it when done.
1338  * - NULL if no phandle found at index
1339  */
1340 struct supplier_bindings {
1341 	struct device_node *(*parse_prop)(struct device_node *np,
1342 					  const char *prop_name, int index);
1343 	struct device_node *(*get_con_dev)(struct device_node *np);
1344 	bool optional;
1345 	u8 fwlink_flags;
1346 };
1347 
1348 DEFINE_SIMPLE_PROP(clocks, "clocks", "#clock-cells")
1349 DEFINE_SIMPLE_PROP(interconnects, "interconnects", "#interconnect-cells")
1350 DEFINE_SIMPLE_PROP(iommus, "iommus", "#iommu-cells")
1351 DEFINE_SIMPLE_PROP(mboxes, "mboxes", "#mbox-cells")
1352 DEFINE_SIMPLE_PROP(io_channels, "io-channels", "#io-channel-cells")
1353 DEFINE_SIMPLE_PROP(io_backends, "io-backends", "#io-backend-cells")
1354 DEFINE_SIMPLE_PROP(dmas, "dmas", "#dma-cells")
1355 DEFINE_SIMPLE_PROP(power_domains, "power-domains", "#power-domain-cells")
1356 DEFINE_SIMPLE_PROP(hwlocks, "hwlocks", "#hwlock-cells")
1357 DEFINE_SIMPLE_PROP(extcon, "extcon", NULL)
1358 DEFINE_SIMPLE_PROP(nvmem_cells, "nvmem-cells", "#nvmem-cell-cells")
1359 DEFINE_SIMPLE_PROP(phys, "phys", "#phy-cells")
1360 DEFINE_SIMPLE_PROP(wakeup_parent, "wakeup-parent", NULL)
1361 DEFINE_SIMPLE_PROP(pinctrl0, "pinctrl-0", NULL)
1362 DEFINE_SIMPLE_PROP(pinctrl1, "pinctrl-1", NULL)
1363 DEFINE_SIMPLE_PROP(pinctrl2, "pinctrl-2", NULL)
1364 DEFINE_SIMPLE_PROP(pinctrl3, "pinctrl-3", NULL)
1365 DEFINE_SIMPLE_PROP(pinctrl4, "pinctrl-4", NULL)
1366 DEFINE_SIMPLE_PROP(pinctrl5, "pinctrl-5", NULL)
1367 DEFINE_SIMPLE_PROP(pinctrl6, "pinctrl-6", NULL)
1368 DEFINE_SIMPLE_PROP(pinctrl7, "pinctrl-7", NULL)
1369 DEFINE_SIMPLE_PROP(pinctrl8, "pinctrl-8", NULL)
1370 DEFINE_SIMPLE_PROP(pwms, "pwms", "#pwm-cells")
1371 DEFINE_SIMPLE_PROP(resets, "resets", "#reset-cells")
1372 DEFINE_SIMPLE_PROP(leds, "leds", NULL)
1373 DEFINE_SIMPLE_PROP(backlight, "backlight", NULL)
1374 DEFINE_SIMPLE_PROP(panel, "panel", NULL)
1375 DEFINE_SIMPLE_PROP(msi_parent, "msi-parent", "#msi-cells")
1376 DEFINE_SIMPLE_PROP(post_init_providers, "post-init-providers", NULL)
1377 DEFINE_SIMPLE_PROP(access_controllers, "access-controllers", "#access-controller-cells")
1378 DEFINE_SIMPLE_PROP(pses, "pses", "#pse-cells")
1379 DEFINE_SIMPLE_PROP(power_supplies, "power-supplies", NULL)
1380 DEFINE_SUFFIX_PROP(regulators, "-supply", NULL)
1381 DEFINE_SUFFIX_PROP(gpio, "-gpio", "#gpio-cells")
1382 
parse_gpios(struct device_node * np,const char * prop_name,int index)1383 static struct device_node *parse_gpios(struct device_node *np,
1384 				       const char *prop_name, int index)
1385 {
1386 	if (!strcmp_suffix(prop_name, ",nr-gpios"))
1387 		return NULL;
1388 
1389 	return parse_suffix_prop_cells(np, prop_name, index, "-gpios",
1390 				       "#gpio-cells");
1391 }
1392 
parse_iommu_maps(struct device_node * np,const char * prop_name,int index)1393 static struct device_node *parse_iommu_maps(struct device_node *np,
1394 					    const char *prop_name, int index)
1395 {
1396 	if (strcmp(prop_name, "iommu-map"))
1397 		return NULL;
1398 
1399 	return of_parse_phandle(np, prop_name, (index * 4) + 1);
1400 }
1401 
parse_gpio_compat(struct device_node * np,const char * prop_name,int index)1402 static struct device_node *parse_gpio_compat(struct device_node *np,
1403 					     const char *prop_name, int index)
1404 {
1405 	struct of_phandle_args sup_args;
1406 
1407 	if (strcmp(prop_name, "gpio") && strcmp(prop_name, "gpios"))
1408 		return NULL;
1409 
1410 	/*
1411 	 * Ignore node with gpio-hog property since its gpios are all provided
1412 	 * by its parent.
1413 	 */
1414 	if (of_property_read_bool(np, "gpio-hog"))
1415 		return NULL;
1416 
1417 	if (of_parse_phandle_with_args(np, prop_name, "#gpio-cells", index,
1418 				       &sup_args))
1419 		return NULL;
1420 
1421 	return sup_args.np;
1422 }
1423 
parse_interrupts(struct device_node * np,const char * prop_name,int index)1424 static struct device_node *parse_interrupts(struct device_node *np,
1425 					    const char *prop_name, int index)
1426 {
1427 	struct of_phandle_args sup_args;
1428 
1429 	if (!IS_ENABLED(CONFIG_OF_IRQ) || IS_ENABLED(CONFIG_PPC))
1430 		return NULL;
1431 
1432 	if (strcmp(prop_name, "interrupts") &&
1433 	    strcmp(prop_name, "interrupts-extended"))
1434 		return NULL;
1435 
1436 	return of_irq_parse_one(np, index, &sup_args) ? NULL : sup_args.np;
1437 }
1438 
parse_interrupt_map(struct device_node * np,const char * prop_name,int index)1439 static struct device_node *parse_interrupt_map(struct device_node *np,
1440 					       const char *prop_name, int index)
1441 {
1442 	const __be32 *imap, *imap_end;
1443 	struct of_phandle_args sup_args;
1444 	u32 addrcells, intcells;
1445 	int imaplen;
1446 
1447 	if (!IS_ENABLED(CONFIG_OF_IRQ))
1448 		return NULL;
1449 
1450 	if (strcmp(prop_name, "interrupt-map"))
1451 		return NULL;
1452 
1453 	if (of_property_read_u32(np, "#interrupt-cells", &intcells))
1454 		return NULL;
1455 	addrcells = of_bus_n_addr_cells(np);
1456 
1457 	imap = of_get_property(np, "interrupt-map", &imaplen);
1458 	if (!imap)
1459 		return NULL;
1460 	imaplen /= sizeof(*imap);
1461 
1462 	imap_end = imap + imaplen;
1463 
1464 	for (int i = 0; imap + addrcells + intcells + 1 < imap_end; i++) {
1465 		imap += addrcells + intcells;
1466 
1467 		imap = of_irq_parse_imap_parent(imap, imap_end - imap, &sup_args);
1468 		if (!imap)
1469 			return NULL;
1470 
1471 		if (i == index)
1472 			return sup_args.np;
1473 
1474 		of_node_put(sup_args.np);
1475 	}
1476 
1477 	return NULL;
1478 }
1479 
parse_remote_endpoint(struct device_node * np,const char * prop_name,int index)1480 static struct device_node *parse_remote_endpoint(struct device_node *np,
1481 						 const char *prop_name,
1482 						 int index)
1483 {
1484 	/* Return NULL for index > 0 to signify end of remote-endpoints. */
1485 	if (index > 0 || strcmp(prop_name, "remote-endpoint"))
1486 		return NULL;
1487 
1488 	return of_graph_get_remote_port_parent(np);
1489 }
1490 
1491 static const struct supplier_bindings of_supplier_bindings[] = {
1492 	{ .parse_prop = parse_clocks, },
1493 	{ .parse_prop = parse_interconnects, },
1494 	{ .parse_prop = parse_iommus, .optional = true, },
1495 	{ .parse_prop = parse_iommu_maps, .optional = true, },
1496 	{ .parse_prop = parse_mboxes, },
1497 	{ .parse_prop = parse_io_channels, },
1498 	{ .parse_prop = parse_io_backends, },
1499 	{ .parse_prop = parse_dmas, .optional = true, },
1500 	{ .parse_prop = parse_power_domains, },
1501 	{ .parse_prop = parse_hwlocks, },
1502 	{ .parse_prop = parse_extcon, },
1503 	{ .parse_prop = parse_nvmem_cells, },
1504 	{ .parse_prop = parse_phys, },
1505 	{ .parse_prop = parse_wakeup_parent, },
1506 	{ .parse_prop = parse_pinctrl0, },
1507 	{ .parse_prop = parse_pinctrl1, },
1508 	{ .parse_prop = parse_pinctrl2, },
1509 	{ .parse_prop = parse_pinctrl3, },
1510 	{ .parse_prop = parse_pinctrl4, },
1511 	{ .parse_prop = parse_pinctrl5, },
1512 	{ .parse_prop = parse_pinctrl6, },
1513 	{ .parse_prop = parse_pinctrl7, },
1514 	{ .parse_prop = parse_pinctrl8, },
1515 	{
1516 		.parse_prop = parse_remote_endpoint,
1517 		.get_con_dev = of_graph_get_port_parent,
1518 	},
1519 	{ .parse_prop = parse_pwms, },
1520 	{ .parse_prop = parse_resets, },
1521 	{ .parse_prop = parse_leds, },
1522 	{ .parse_prop = parse_backlight, },
1523 	{ .parse_prop = parse_panel, },
1524 	{ .parse_prop = parse_msi_parent, },
1525 	{ .parse_prop = parse_pses, },
1526 	{ .parse_prop = parse_power_supplies, },
1527 	{ .parse_prop = parse_gpio_compat, },
1528 	{ .parse_prop = parse_interrupts, },
1529 	{ .parse_prop = parse_interrupt_map, },
1530 	{ .parse_prop = parse_access_controllers, },
1531 	{ .parse_prop = parse_regulators, },
1532 	{ .parse_prop = parse_gpio, },
1533 	{ .parse_prop = parse_gpios, },
1534 	{
1535 		.parse_prop = parse_post_init_providers,
1536 		.fwlink_flags = FWLINK_FLAG_IGNORE,
1537 	},
1538 	{}
1539 };
1540 
1541 /**
1542  * of_link_property - Create device links to suppliers listed in a property
1543  * @con_np: The consumer device tree node which contains the property
1544  * @prop_name: Name of property to be parsed
1545  *
1546  * This function checks if the property @prop_name that is present in the
1547  * @con_np device tree node is one of the known common device tree bindings
1548  * that list phandles to suppliers. If @prop_name isn't one, this function
1549  * doesn't do anything.
1550  *
1551  * If @prop_name is one, this function attempts to create fwnode links from the
1552  * consumer device tree node @con_np to all the suppliers device tree nodes
1553  * listed in @prop_name.
1554  *
1555  * Any failed attempt to create a fwnode link will NOT result in an immediate
1556  * return.  of_link_property() must create links to all the available supplier
1557  * device tree nodes even when attempts to create a link to one or more
1558  * suppliers fail.
1559  */
of_link_property(struct device_node * con_np,const char * prop_name)1560 static int of_link_property(struct device_node *con_np, const char *prop_name)
1561 {
1562 	struct device_node *phandle;
1563 	const struct supplier_bindings *s = of_supplier_bindings;
1564 	unsigned int i = 0;
1565 	bool matched = false;
1566 
1567 	/* Do not stop at first failed link, link all available suppliers. */
1568 	while (!matched && s->parse_prop) {
1569 		if (s->optional && !fw_devlink_is_strict()) {
1570 			s++;
1571 			continue;
1572 		}
1573 
1574 		while ((phandle = s->parse_prop(con_np, prop_name, i))) {
1575 			struct device_node *con_dev_np __free(device_node) =
1576 				s->get_con_dev ? s->get_con_dev(con_np) : of_node_get(con_np);
1577 
1578 			matched = true;
1579 			i++;
1580 			of_link_to_phandle(con_dev_np, phandle, s->fwlink_flags);
1581 			of_node_put(phandle);
1582 		}
1583 		s++;
1584 	}
1585 	return 0;
1586 }
1587 
of_fwnode_iomap(struct fwnode_handle * fwnode,int index)1588 static void __iomem *of_fwnode_iomap(struct fwnode_handle *fwnode, int index)
1589 {
1590 #ifdef CONFIG_OF_ADDRESS
1591 	return of_iomap(to_of_node(fwnode), index);
1592 #else
1593 	return NULL;
1594 #endif
1595 }
1596 
of_fwnode_irq_get(const struct fwnode_handle * fwnode,unsigned int index)1597 static int of_fwnode_irq_get(const struct fwnode_handle *fwnode,
1598 			     unsigned int index)
1599 {
1600 	return of_irq_get(to_of_node(fwnode), index);
1601 }
1602 
of_fwnode_add_links(struct fwnode_handle * fwnode)1603 static int of_fwnode_add_links(struct fwnode_handle *fwnode)
1604 {
1605 	const struct property *p;
1606 	struct device_node *con_np = to_of_node(fwnode);
1607 
1608 	if (IS_ENABLED(CONFIG_X86))
1609 		return 0;
1610 
1611 	if (!con_np)
1612 		return -EINVAL;
1613 
1614 	for_each_property_of_node(con_np, p)
1615 		of_link_property(con_np, p->name);
1616 
1617 	return 0;
1618 }
1619 
1620 const struct fwnode_operations of_fwnode_ops = {
1621 	.get = of_fwnode_get,
1622 	.put = of_fwnode_put,
1623 	.device_is_available = of_fwnode_device_is_available,
1624 	.device_get_match_data = of_fwnode_device_get_match_data,
1625 	.device_dma_supported = of_fwnode_device_dma_supported,
1626 	.device_get_dma_attr = of_fwnode_device_get_dma_attr,
1627 	.property_present = of_fwnode_property_present,
1628 	.property_read_bool = of_fwnode_property_read_bool,
1629 	.property_read_int_array = of_fwnode_property_read_int_array,
1630 	.property_read_string_array = of_fwnode_property_read_string_array,
1631 	.get_name = of_fwnode_get_name,
1632 	.get_name_prefix = of_fwnode_get_name_prefix,
1633 	.get_parent = of_fwnode_get_parent,
1634 	.get_next_child_node = of_fwnode_get_next_child_node,
1635 	.get_named_child_node = of_fwnode_get_named_child_node,
1636 	.get_reference_args = of_fwnode_get_reference_args,
1637 	.graph_get_next_endpoint = of_fwnode_graph_get_next_endpoint,
1638 	.graph_get_remote_endpoint = of_fwnode_graph_get_remote_endpoint,
1639 	.graph_get_port_parent = of_fwnode_graph_get_port_parent,
1640 	.graph_parse_endpoint = of_fwnode_graph_parse_endpoint,
1641 	.iomap = of_fwnode_iomap,
1642 	.irq_get = of_fwnode_irq_get,
1643 	.add_links = of_fwnode_add_links,
1644 };
1645 EXPORT_SYMBOL_GPL(of_fwnode_ops);
1646