1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Originally from Linux v4.9
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
7 * Updates for SPARC64 by David S. Miller
8 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
10 * Copyright (c) 2017 Google, Inc
11 * Written by Simon Glass <sjg@chromium.org>
14 * Copyright (c) 2017 Google, Inc
17 #ifndef _DM_OF_ACCESS_H
18 #define _DM_OF_ACCESS_H
23 * of_find_all_nodes - Get next node in global list
24 * @prev: Previous node or NULL to start iteration
25 * of_node_put() will be called on it
27 * Returns a node pointer with refcount incremented, use
28 * of_node_put() on it when done.
30 struct device_node *of_find_all_nodes(struct device_node *prev);
32 #define for_each_of_allnodes_from(from, dn) \
33 for (dn = of_find_all_nodes(from); dn; dn = of_find_all_nodes(dn))
34 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
36 /* Dummy functions to mirror Linux. These are not used in U-Boot */
37 #define of_node_get(x) (x)
38 static inline void of_node_put(const struct device_node *np) { }
41 * of_n_addr_cells() - Get the number of address cells for a node
43 * This walks back up the tree to find the closest #address-cells property
44 * which controls the given node.
46 * @np: Node pointer to check
47 * @return number of address cells this node uses
49 int of_n_addr_cells(const struct device_node *np);
52 * of_n_size_cells() - Get the number of size cells for a node
54 * This walks back up the tree to find the closest #size-cells property
55 * which controls the given node.
57 * @np: Node pointer to check
58 * @return number of size cells this node uses
60 int of_n_size_cells(const struct device_node *np);
63 * of_simple_addr_cells() - Get the address cells property in a node
65 * This function matches fdt_address_cells().
67 * @np: Node pointer to check
68 * @return value of #address-cells property in this node, or 2 if none
70 int of_simple_addr_cells(const struct device_node *np);
73 * of_simple_size_cells() - Get the size cells property in a node
75 * This function matches fdt_size_cells().
77 * @np: Node pointer to check
78 * @return value of #size-cells property in this node, or 2 if none
80 int of_simple_size_cells(const struct device_node *np);
83 * of_find_property() - find a property in a node
85 * @np: Pointer to device node holding property
86 * @name: Name of property
87 * @lenp: If non-NULL, returns length of property
88 * @return pointer to property, or NULL if not found
90 struct property *of_find_property(const struct device_node *np,
91 const char *name, int *lenp);
94 * of_get_property() - get a property value
96 * Find a property with a given name for a given node and return the value.
98 * @np: Pointer to device node holding property
99 * @name: Name of property
100 * @lenp: If non-NULL, returns length of property
101 * @return pointer to property value, or NULL if not found
103 const void *of_get_property(const struct device_node *np, const char *name,
107 * of_get_first_property()- get to the pointer of the first property
109 * Get pointer to the first property of the node, it is used to iterate
110 * and read all the property with of_get_next_property_by_prop().
112 * @np: Pointer to device node
113 * @return pointer to property or NULL if not found
115 const struct property *of_get_first_property(const struct device_node *np);
118 * of_get_next_property() - get to the pointer of the next property
120 * Get pointer to the next property of the node, it is used to iterate
121 * and read all the property with of_get_property_by_prop().
123 * @np: Pointer to device node
124 * @property: pointer of the current property
125 * @return pointer to next property or NULL if not found
127 const struct property *of_get_next_property(const struct device_node *np,
128 const struct property *property);
131 * of_get_property_by_prop() - get a property value of a node property
133 * Get value for the property identified by node and property pointer.
135 * @node: node to read
136 * @property: pointer of the property to read
137 * @propname: place to property name on success
138 * @lenp: place to put length on success
139 * @return pointer to property value or NULL if error
141 const void *of_get_property_by_prop(const struct device_node *np,
142 const struct property *property,
147 * of_device_is_compatible() - Check if the node matches given constraints
148 * @device: pointer to node
149 * @compat: required compatible string, NULL or "" for any match
150 * @type: required device_type value, NULL or "" for any match
151 * @name: required node name, NULL or "" for any match
153 * Checks if the given @compat, @type and @name strings match the
154 * properties of the given @device. A constraints can be skipped by
155 * passing NULL or an empty string as the constraint.
157 * @return 0 for no match, and a positive integer on match. The return
158 * value is a relative score with larger values indicating better
159 * matches. The score is weighted for the most specific compatible value
160 * to get the highest score. Matching type is next, followed by matching
161 * name. Practically speaking, this results in the following priority
164 * 1. specific compatible && type && name
165 * 2. specific compatible && type
166 * 3. specific compatible && name
167 * 4. specific compatible
168 * 5. general compatible && type && name
169 * 6. general compatible && type
170 * 7. general compatible && name
171 * 8. general compatible
176 int of_device_is_compatible(const struct device_node *np, const char *compat,
177 const char *type, const char *name);
180 * of_device_is_available() - check if a device is available for use
182 * @device: Node to check for availability
184 * @return true if the status property is absent or set to "okay", false
187 bool of_device_is_available(const struct device_node *np);
190 * of_get_parent() - Get a node's parent, if any
192 * @node: Node to check
193 * @eturns a node pointer, or NULL if none
195 struct device_node *of_get_parent(const struct device_node *np);
198 * of_find_node_opts_by_path() - Find a node matching a full OF path
200 * @path: Either the full path to match, or if the path does not start with
201 * '/', the name of a property of the /aliases node (an alias). In the
202 * case of an alias, the node matching the alias' value will be returned.
203 * @opts: Address of a pointer into which to store the start of an options
204 * string appended to the end of the path with a ':' separator. Can be NULL
209 * foo/bar Valid alias + relative path
211 * @return a node pointer or NULL if not found
213 struct device_node *of_find_node_opts_by_path(const char *path,
216 static inline struct device_node *of_find_node_by_path(const char *path)
218 return of_find_node_opts_by_path(path, NULL);
222 * of_find_compatible_node() - find a node based on its compatible string
224 * Find a node based on type and one of the tokens in its "compatible" property
225 * @from: Node to start searching from or NULL. the node you pass will not be
226 * searched, only the next one will; typically, you pass what the previous
228 * @type: The type string to match "device_type" or NULL to ignore
229 * @compatible: The string to match to one of the tokens in the device
231 * @return node pointer or NULL if not found
233 struct device_node *of_find_compatible_node(struct device_node *from,
234 const char *type, const char *compatible);
237 * of_find_node_by_prop_value() - find a node with a given property value
239 * Find a node based on a property value.
240 * @from: Node to start searching from or NULL. the node you pass will not be
241 * searched, only the next one will; typically, you pass what the previous
243 * @propname: property name to check
244 * @propval: property value to search for
245 * @proplen: length of the value in propval
246 * @return node pointer or NULL if not found
248 struct device_node *of_find_node_by_prop_value(struct device_node *from,
249 const char *propname,
253 * of_find_node_by_phandle() - Find a node given a phandle
255 * @handle: phandle of the node to find
257 * @return node pointer, or NULL if not found
259 struct device_node *of_find_node_by_phandle(phandle handle);
262 * of_read_u32() - Find and read a 32-bit integer from a property
264 * Search for a property in a device node and read a 32-bit value from
267 * @np: device node from which the property value is to be read.
268 * @propname: name of the property to be searched.
269 * @outp: pointer to return value, modified only if return value is 0.
271 * @return 0 on success, -EINVAL if the property does not exist,
272 * -ENODATA if property does not have a value, and -EOVERFLOW if the
273 * property data isn't large enough.
275 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
278 * of_read_u32_index() - Find and read a 32-bit value from a multi-value
281 * Search for a property in a device node and read a 32-bit value from
284 * @np: device node from which the property value is to be read.
285 * @propname: name of the property to be searched.
286 * @index: index of the u32 in the list of values
287 * @outp: pointer to return value, modified only if return value is 0.
289 * @return 0 on success, -EINVAL if the property does not exist,
290 * -ENODATA if property does not have a value, and -EOVERFLOW if the
291 * property data isn't large enough.
293 int of_read_u32_index(const struct device_node *np, const char *propname,
294 int index, u32 *outp);
297 * of_read_u64() - Find and read a 64-bit integer from a property
299 * Search for a property in a device node and read a 64-bit value from
302 * @np: device node from which the property value is to be read.
303 * @propname: name of the property to be searched.
304 * @outp: pointer to return value, modified only if return value is 0.
306 * @return 0 on success, -EINVAL if the property does not exist,
307 * -ENODATA if property does not have a value, and -EOVERFLOW if the
308 * property data isn't large enough.
310 int of_read_u64(const struct device_node *np, const char *propname, u64 *outp);
313 * of_read_u32_array() - Find and read an array of 32 bit integers
315 * Search for a property in a device node and read 32-bit value(s) from
318 * @np: device node from which the property value is to be read.
319 * @propname: name of the property to be searched.
320 * @out_values: pointer to return value, modified only if return value is 0.
321 * @sz: number of array elements to read
322 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
323 * if property does not have a value, and -EOVERFLOW is longer than sz.
325 int of_read_u32_array(const struct device_node *np, const char *propname,
326 u32 *out_values, size_t sz);
329 * of_property_match_string() - Find string in a list and return index
331 * This function searches a string list property and returns the index
332 * of a specific string value.
334 * @np: pointer to node containing string list property
335 * @propname: string list property name
336 * @string: pointer to string to search for in string list
337 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
338 * if property does not have a value, and -EOVERFLOW is longer than sz.
340 int of_property_match_string(const struct device_node *np, const char *propname,
343 int of_property_read_string_helper(const struct device_node *np,
344 const char *propname, const char **out_strs,
345 size_t sz, int index);
348 * of_property_read_string_index() - Find and read a string from a multiple
350 * @np: device node from which the property value is to be read.
351 * @propname: name of the property to be searched.
352 * @index: index of the string in the list of strings
353 * @out_string: pointer to null terminated return string, modified only if
356 * Search for a property in a device tree node and retrieve a null
357 * terminated string value (pointer to data, not a copy) in the list of strings
358 * contained in that property.
359 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
360 * property does not have a value, and -EILSEQ if the string is not
361 * null-terminated within the length of the property data.
363 * The out_string pointer is modified only if a valid string can be decoded.
365 static inline int of_property_read_string_index(const struct device_node *np,
366 const char *propname,
367 int index, const char **output)
369 int rc = of_property_read_string_helper(np, propname, output, 1, index);
370 return rc < 0 ? rc : 0;
374 * of_property_count_strings() - Find and return the number of strings from a
375 * multiple strings property.
376 * @np: device node from which the property value is to be read.
377 * @propname: name of the property to be searched.
379 * Search for a property in a device tree node and retrieve the number of null
380 * terminated string contain in it. Returns the number of strings on
381 * success, -EINVAL if the property does not exist, -ENODATA if property
382 * does not have a value, and -EILSEQ if the string is not null-terminated
383 * within the length of the property data.
385 static inline int of_property_count_strings(const struct device_node *np,
386 const char *propname)
388 return of_property_read_string_helper(np, propname, NULL, 0, 0);
392 * of_parse_phandle - Resolve a phandle property to a device_node pointer
393 * @np: Pointer to device node holding phandle property
394 * @phandle_name: Name of property holding a phandle value
395 * @index: For properties holding a table of phandles, this is the index into
398 * Returns the device_node pointer with refcount incremented. Use
399 * of_node_put() on it when done.
401 struct device_node *of_parse_phandle(const struct device_node *np,
402 const char *phandle_name, int index);
405 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
407 * @np: pointer to a device tree node containing a list
408 * @list_name: property name that contains a list
409 * @cells_name: property name that specifies phandles' arguments count
410 * @cells_count: Cell count to use if @cells_name is NULL
411 * @index: index of a phandle to parse out
412 * @out_args: optional pointer to output arguments structure (will be filled)
413 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
414 * @list_name does not exist, -EINVAL if a phandle was not found,
415 * @cells_name could not be found, the arguments were truncated or there
416 * were too many arguments.
418 * This function is useful to parse lists of phandles and their arguments.
419 * Returns 0 on success and fills out_args, on error returns appropriate
422 * Caller is responsible to call of_node_put() on the returned out_args->np
436 * list = <&phandle1 1 2 &phandle2 3>;
439 * To get a device_node of the `node2' node you may call this:
440 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
442 int of_parse_phandle_with_args(const struct device_node *np,
443 const char *list_name, const char *cells_name,
444 int cells_count, int index,
445 struct of_phandle_args *out_args);
448 * of_count_phandle_with_args() - Count the number of phandle in a list
450 * @np: pointer to a device tree node containing a list
451 * @list_name: property name that contains a list
452 * @cells_name: property name that specifies phandles' arguments count
453 * @cells_count: Cell count to use if @cells_name is NULL
454 * @return number of phandle found, -ENOENT if
455 * @list_name does not exist, -EINVAL if a phandle was not found,
456 * @cells_name could not be found, the arguments were truncated or there
457 * were too many arguments.
459 * Returns number of phandle found on success, on error returns appropriate
463 int of_count_phandle_with_args(const struct device_node *np,
464 const char *list_name, const char *cells_name,
468 * of_alias_scan() - Scan all properties of the 'aliases' node
470 * The function scans all the properties of the 'aliases' node and populates
471 * the lookup table with the properties. It returns the number of alias
472 * properties found, or an error code in case of failure.
474 * @return 9 if OK, -ENOMEM if not enough memory
476 int of_alias_scan(void);
479 * of_alias_get_id - Get alias id for the given device_node
481 * Travels the lookup table to get the alias id for the given device_node and
484 * @np: Pointer to the given device_node
485 * @stem: Alias stem of the given device_node
486 * @return alias ID, if found, else -ENODEV
488 int of_alias_get_id(const struct device_node *np, const char *stem);
491 * of_alias_get_highest_id - Get highest alias id for the given stem
492 * @stem: Alias stem to be examined
494 * The function travels the lookup table to get the highest alias id for the
496 * @return alias ID, if found, else -1
498 int of_alias_get_highest_id(const char *stem);
501 * of_get_stdout() - Get node to use for stdout
503 * @return node referred to by stdout-path alias, or NULL if none
505 struct device_node *of_get_stdout(void);