xref: /OK3568_Linux_fs/u-boot/include/dm/of_access.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Originally from Linux v4.9
3  * Copyright (C) 1996-2005 Paul Mackerras.
4  *
5  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
6  * Updates for SPARC64 by David S. Miller
7  * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
8  *
9  * Copyright (c) 2017 Google, Inc
10  * Written by Simon Glass <sjg@chromium.org>
11  *
12  * Modified for U-Boot
13  * Copyright (c) 2017 Google, Inc
14  *
15  * SPDX-License-Identifier:	GPL-2.0+
16  */
17 
18 #ifndef _DM_OF_ACCESS_H
19 #define _DM_OF_ACCESS_H
20 
21 #include <dm/of.h>
22 
23 /**
24  * of_find_all_nodes - Get next node in global list
25  * @prev:	Previous node or NULL to start iteration
26  *		of_node_put() will be called on it
27  *
28  * Returns a node pointer with refcount incremented, use
29  * of_node_put() on it when done.
30  */
31 struct device_node *of_find_all_nodes(struct device_node *prev);
32 
33 #define for_each_of_allnodes_from(from, dn) \
34 	for (dn = of_find_all_nodes(from); dn; dn = of_find_all_nodes(dn))
35 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
36 
37 /* Dummy functions to mirror Linux. These are not used in U-Boot */
38 #define of_node_get(x) (x)
of_node_put(const struct device_node * np)39 static inline void of_node_put(const struct device_node *np) { }
40 
41 /**
42  * of_n_addr_cells() - Get the number of address cells for a node
43  *
44  * This walks back up the tree to find the closest #address-cells property
45  * which controls the given node.
46  *
47  * @np: Node pointer to check
48  * @return number of address cells this node uses
49  */
50 int of_n_addr_cells(const struct device_node *np);
51 
52 /**
53  * of_n_size_cells() - Get the number of size cells for a node
54  *
55  * This walks back up the tree to find the closest #size-cells property
56  * which controls the given node.
57  *
58  * @np: Node pointer to check
59  * @return number of size cells this node uses
60  */
61 int of_n_size_cells(const struct device_node *np);
62 
63 /**
64  * of_simple_addr_cells() - Get the address cells property in a node
65  *
66  * This function matches fdt_address_cells().
67  *
68  * @np: Node pointer to check
69  * @return value of #address-cells property in this node, or 2 if none
70  */
71 int of_simple_addr_cells(const struct device_node *np);
72 
73 /**
74  * of_simple_size_cells() - Get the size cells property in a node
75  *
76  * This function matches fdt_size_cells().
77  *
78  * @np: Node pointer to check
79  * @return value of #size-cells property in this node, or 2 if none
80  */
81 int of_simple_size_cells(const struct device_node *np);
82 
83 /**
84  * of_find_property() - find a property in a node
85  *
86  * @np: Pointer to device node holding property
87  * @name: Name of property
88  * @lenp: If non-NULL, returns length of property
89  * @return pointer to property, or NULL if not found
90  */
91 struct property *of_find_property(const struct device_node *np,
92 				  const char *name, int *lenp);
93 
94 /**
95  * of_get_property() - get a property value
96  *
97  * Find a property with a given name for a given node and return the value.
98  *
99  * @np: Pointer to device node holding property
100  * @name: Name of property
101  * @lenp: If non-NULL, returns length of property
102  * @return pointer to property value, or NULL if not found
103  */
104 const void *of_get_property(const struct device_node *np, const char *name,
105 			    int *lenp);
106 
107 /**
108  * of_get_first_property()- get to the pointer of the first property
109  *
110  * Get pointer to the first property of the node, it is used to iterate
111  * and read all the property with of_get_next_property_by_prop().
112  *
113  * @np: Pointer to device node
114  * @return pointer to property or NULL if not found
115  */
116 const struct property *of_get_first_property(const struct device_node *np);
117 
118 /**
119  * of_get_next_property() - get to the pointer of the next property
120  *
121  * Get pointer to the next property of the node, it is used to iterate
122  * and read all the property with of_get_property_by_prop().
123  *
124  * @np: Pointer to device node
125  * @property: pointer of the current property
126  * @return pointer to next property or NULL if not found
127  */
128 const struct property *of_get_next_property(const struct device_node *np,
129 					    const struct property *property);
130 
131 /**
132  * of_get_property_by_prop() - get a property value of a node property
133  *
134  * Get value for the property identified by node and property pointer.
135  *
136  * @node: node to read
137  * @property: pointer of the property to read
138  * @propname: place to property name on success
139  * @lenp: place to put length on success
140  * @return pointer to property value or NULL if error
141  */
142 const void *of_get_property_by_prop(const struct device_node *np,
143 				    const struct property *property,
144 				    const char **name,
145 				    int *lenp);
146 
147 /**
148  * of_device_is_compatible() - Check if the node matches given constraints
149  * @device: pointer to node
150  * @compat: required compatible string, NULL or "" for any match
151  * @type: required device_type value, NULL or "" for any match
152  * @name: required node name, NULL or "" for any match
153  *
154  * Checks if the given @compat, @type and @name strings match the
155  * properties of the given @device. A constraints can be skipped by
156  * passing NULL or an empty string as the constraint.
157  *
158  * @return 0 for no match, and a positive integer on match. The return
159  * value is a relative score with larger values indicating better
160  * matches. The score is weighted for the most specific compatible value
161  * to get the highest score. Matching type is next, followed by matching
162  * name. Practically speaking, this results in the following priority
163  * order for matches:
164  *
165  * 1. specific compatible && type && name
166  * 2. specific compatible && type
167  * 3. specific compatible && name
168  * 4. specific compatible
169  * 5. general compatible && type && name
170  * 6. general compatible && type
171  * 7. general compatible && name
172  * 8. general compatible
173  * 9. type && name
174  * 10. type
175  * 11. name
176  */
177 int of_device_is_compatible(const struct device_node *np, const char *compat,
178 			    const char *type, const char *name);
179 
180 /**
181  * of_device_is_available() - check if a device is available for use
182  *
183  * @device: Node to check for availability
184  *
185  * @return true if the status property is absent or set to "okay", false
186  * otherwise
187  */
188 bool of_device_is_available(const struct device_node *np);
189 
190 /**
191  * of_get_parent() - Get a node's parent, if any
192  *
193  * @node: Node to check
194  * @eturns a node pointer, or NULL if none
195  */
196 struct device_node *of_get_parent(const struct device_node *np);
197 
198 /**
199  * of_find_node_opts_by_path() - Find a node matching a full OF path
200  *
201  * @path: Either the full path to match, or if the path does not start with
202  *	'/', the name of a property of the /aliases node (an alias). In the
203  *	case of an alias, the node matching the alias' value will be returned.
204  * @opts: Address of a pointer into which to store the start of an options
205  *	string appended to the end of the path with a ':' separator. Can be NULL
206  *
207  * Valid paths:
208  *	/foo/bar	Full path
209  *	foo		Valid alias
210  *	foo/bar		Valid alias + relative path
211  *
212  * @return a node pointer or NULL if not found
213  */
214 struct device_node *of_find_node_opts_by_path(const char *path,
215 					      const char **opts);
216 
of_find_node_by_path(const char * path)217 static inline struct device_node *of_find_node_by_path(const char *path)
218 {
219 	return of_find_node_opts_by_path(path, NULL);
220 }
221 
222 /**
223  * of_find_compatible_node() - find a node based on its compatible string
224  *
225  * Find a node based on type and one of the tokens in its "compatible" property
226  * @from: Node to start searching from or NULL. the node you pass will not be
227  *	searched, only the next one will; typically, you pass what the previous
228  *	call returned.
229  * @type: The type string to match "device_type" or NULL to ignore
230  * @compatible:	The string to match to one of the tokens in the device
231  *	"compatible" list.
232  * @return node pointer or NULL if not found
233  */
234 struct device_node *of_find_compatible_node(struct device_node *from,
235 				const char *type, const char *compatible);
236 
237 /**
238  * of_find_node_by_phandle() - Find a node given a phandle
239  *
240  * @handle:	phandle of the node to find
241  *
242  * @return node pointer, or NULL if not found
243  */
244 struct device_node *of_find_node_by_phandle(phandle handle);
245 
246 /**
247  * of_read_u32() - Find and read a 32-bit integer from a property
248  *
249  * Search for a property in a device node and read a 32-bit value from
250  * it.
251  *
252  * @np:		device node from which the property value is to be read.
253  * @propname:	name of the property to be searched.
254  * @outp:	pointer to return value, modified only if return value is 0.
255  *
256  * @return 0 on success, -EINVAL if the property does not exist,
257  * -ENODATA if property does not have a value, and -EOVERFLOW if the
258  * property data isn't large enough.
259  */
260 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
261 
262 /**
263  * of_property_read_u64 - Find and read a 64 bit integer from a property
264  * @np:         device node from which the property value is to be read.
265  * @propname:   name of the property to be searched.
266  * @out_value:  pointer to return value, modified only if return value is 0.
267  *
268  * Search for a property in a device node and read a 64-bit value from
269  * it. Returns 0 on success, -EINVAL if the property does not exist,
270  * -ENODATA if property does not have a value, and -EOVERFLOW if the
271  * property data isn't large enough.
272  *
273  * The out_value is modified only if a valid u64 value can be decoded.
274  */
275 int of_property_read_u64(const struct device_node *np, const char *propname,
276                          u64 *out_value);
277 
278 /**
279  * of_read_u32_array() - Find and read an array of 32 bit integers
280  *
281  * Search for a property in a device node and read 32-bit value(s) from
282  * it.
283  *
284  * @np:		device node from which the property value is to be read.
285  * @propname:	name of the property to be searched.
286  * @out_values:	pointer to return value, modified only if return value is 0.
287  * @sz:		number of array elements to read
288  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
289  * if property does not have a value, and -EOVERFLOW is longer than sz.
290  */
291 int of_read_u32_array(const struct device_node *np, const char *propname,
292 		      u32 *out_values, size_t sz);
293 
294 /**
295  * of_write_u32_array() - Find and write an array of 32 bit integers
296  *
297  * Search for a property in a device node and write 32-bit value(s) to
298  * it.
299  *
300  * @np:		device node from which the property value is to be read.
301  * @propname:	name of the property to be searched.
302  * @values:	pointer to update value, modified only if return value is 0.
303  * @sz:		number of array elements to read
304  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
305  * if property does not have a value, and -EOVERFLOW is longer than sz.
306  */
307 int of_write_u32_array(const struct device_node *np, const char *propname,
308 		       u32 *values, size_t sz);
309 
310 /**
311  * of_property_match_string() - Find string in a list and return index
312  *
313  * This function searches a string list property and returns the index
314  * of a specific string value.
315  *
316  * @np: pointer to node containing string list property
317  * @propname: string list property name
318  * @string: pointer to string to search for in string list
319  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
320  * if property does not have a value, and -EOVERFLOW is longer than sz.
321  */
322 int of_property_match_string(const struct device_node *np, const char *propname,
323 			     const char *string);
324 
325 int of_property_read_string_helper(const struct device_node *np,
326 				   const char *propname, const char **out_strs,
327 				   size_t sz, int index);
328 
329 /**
330  * of_property_read_string_index() - Find and read a string from a multiple
331  * strings property.
332  * @np:		device node from which the property value is to be read.
333  * @propname:	name of the property to be searched.
334  * @index:	index of the string in the list of strings
335  * @out_string:	pointer to null terminated return string, modified only if
336  *		return value is 0.
337  *
338  * Search for a property in a device tree node and retrieve a null
339  * terminated string value (pointer to data, not a copy) in the list of strings
340  * contained in that property.
341  * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
342  * property does not have a value, and -EILSEQ if the string is not
343  * null-terminated within the length of the property data.
344  *
345  * The out_string pointer is modified only if a valid string can be decoded.
346  */
of_property_read_string_index(const struct device_node * np,const char * propname,int index,const char ** output)347 static inline int of_property_read_string_index(const struct device_node *np,
348 						const char *propname,
349 						int index, const char **output)
350 {
351 	int rc = of_property_read_string_helper(np, propname, output, 1, index);
352 	return rc < 0 ? rc : 0;
353 }
354 
355 /**
356  * of_property_count_strings() - Find and return the number of strings from a
357  * multiple strings property.
358  * @np:		device node from which the property value is to be read.
359  * @propname:	name of the property to be searched.
360  *
361  * Search for a property in a device tree node and retrieve the number of null
362  * terminated string contain in it. Returns the number of strings on
363  * success, -EINVAL if the property does not exist, -ENODATA if property
364  * does not have a value, and -EILSEQ if the string is not null-terminated
365  * within the length of the property data.
366  */
of_property_count_strings(const struct device_node * np,const char * propname)367 static inline int of_property_count_strings(const struct device_node *np,
368 					    const char *propname)
369 {
370 	return of_property_read_string_helper(np, propname, NULL, 0, 0);
371 }
372 
373 /**
374  * of_parse_phandle - Resolve a phandle property to a device_node pointer
375  * @np: Pointer to device node holding phandle property
376  * @phandle_name: Name of property holding a phandle value
377  * @index: For properties holding a table of phandles, this is the index into
378  *         the table
379  *
380  * Returns the device_node pointer with refcount incremented.  Use
381  * of_node_put() on it when done.
382  */
383 struct device_node *of_parse_phandle(const struct device_node *np,
384 				     const char *phandle_name, int index);
385 
386 /**
387  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
388  *
389  * @np:		pointer to a device tree node containing a list
390  * @list_name:	property name that contains a list
391  * @cells_name:	property name that specifies phandles' arguments count
392  * @index:	index of a phandle to parse out
393  * @out_args:	optional pointer to output arguments structure (will be filled)
394  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
395  *	@list_name does not exist, -EINVAL if a phandle was not found,
396  *	@cells_name could not be found, the arguments were truncated or there
397  *	were too many arguments.
398  *
399  * This function is useful to parse lists of phandles and their arguments.
400  * Returns 0 on success and fills out_args, on error returns appropriate
401  * errno value.
402  *
403  * Caller is responsible to call of_node_put() on the returned out_args->np
404  * pointer.
405  *
406  * Example:
407  *
408  * phandle1: node1 {
409  *	#list-cells = <2>;
410  * }
411  *
412  * phandle2: node2 {
413  *	#list-cells = <1>;
414  * }
415  *
416  * node3 {
417  *	list = <&phandle1 1 2 &phandle2 3>;
418  * }
419  *
420  * To get a device_node of the `node2' node you may call this:
421  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
422  */
423 int of_parse_phandle_with_args(const struct device_node *np,
424 			       const char *list_name, const char *cells_name,
425 			       int index, struct of_phandle_args *out_args);
426 
427 /**
428  * of_count_phandle_with_args() - Count the number of phandle in a list
429  *
430  * @np:		pointer to a device tree node containing a list
431  * @list_name:	property name that contains a list
432  * @cells_name:	property name that specifies phandles' arguments count
433  * @return number of phandle found, -ENOENT if
434  *	@list_name does not exist, -EINVAL if a phandle was not found,
435  *	@cells_name could not be found, the arguments were truncated or there
436  *	were too many arguments.
437  *
438  * Returns number of phandle found on success, on error returns appropriate
439  * errno value.
440  *
441  */
442 int of_count_phandle_with_args(const struct device_node *np,
443 			       const char *list_name, const char *cells_name);
444 
445 /**
446  * of_alias_scan() - Scan all properties of the 'aliases' node
447  *
448  * The function scans all the properties of the 'aliases' node and populates
449  * the lookup table with the properties.  It returns the number of alias
450  * properties found, or an error code in case of failure.
451  *
452  * @return 9 if OK, -ENOMEM if not enough memory
453  */
454 int of_alias_scan(void);
455 
456 /**
457  * of_alias_get_id - Get alias id for the given device_node
458  *
459  * Travels the lookup table to get the alias id for the given device_node and
460  * alias stem.
461  *
462  * @np:		Pointer to the given device_node
463  * @stem:	Alias stem of the given device_node
464  * @return alias ID, if found, else -ENODEV
465  */
466 int of_alias_get_id(const struct device_node *np, const char *stem);
467 
468 /**
469  * of_alias_get_dev - Get device_node by given stem and alias id
470  *
471  * Travels the lookup table to get the device_node by given stem and alias id.
472  *
473  * @stem:	Alias stem of the given device_node
474  * @id:         Alias id of the given device_node
475  * @return device_node, if found, else NULL
476  */
477 struct device_node *of_alias_get_dev(const char *stem, int id);
478 
479 /**
480  * of_alias_dump - Dump of alias nodes added in aliases_lookup.
481  */
482 struct device_node *of_alias_dump(void);
483 
484 /**
485  * of_get_stdout() - Get node to use for stdout
486  *
487  * @return node referred to by stdout-path alias, or NULL if none
488  */
489 struct device_node *of_get_stdout(void);
490 
491 #endif
492