2 * Originally from Linux v4.9
3 * Copyright (C) 1996-2005 Paul Mackerras.
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.
9 * Copyright (c) 2017 Google, Inc
10 * Written by Simon Glass <sjg@chromium.org>
13 * Copyright (c) 2017 Google, Inc
15 * SPDX-License-Identifier: GPL-2.0+
18 #ifndef _DM_OF_ACCESS_H
19 #define _DM_OF_ACCESS_H
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
28 * Returns a node pointer with refcount incremented, use
29 * of_node_put() on it when done.
31 struct device_node *of_find_all_nodes(struct device_node *prev);
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)
37 /* Dummy functions to mirror Linux. These are not used in U-Boot */
38 #define of_node_get(x) (x)
39 static inline void of_node_put(const struct device_node *np) { }
42 * of_n_addr_cells() - Get the number of address cells for a node
44 * This walks back up the tree to find the closest #address-cells property
45 * which controls the given node.
47 * @np: Node pointer to check
48 * @return number of address cells this node uses
50 int of_n_addr_cells(const struct device_node *np);
53 * of_n_size_cells() - Get the number of size cells for a node
55 * This walks back up the tree to find the closest #size-cells property
56 * which controls the given node.
58 * @np: Node pointer to check
59 * @return number of size cells this node uses
61 int of_n_size_cells(const struct device_node *np);
64 * of_simple_addr_cells() - Get the address cells property in a node
66 * This function matches fdt_address_cells().
68 * @np: Node pointer to check
69 * @return value of #address-cells property in this node, or 2 if none
71 int of_simple_addr_cells(const struct device_node *np);
74 * of_simple_size_cells() - Get the size cells property in a node
76 * This function matches fdt_size_cells().
78 * @np: Node pointer to check
79 * @return value of #size-cells property in this node, or 2 if none
81 int of_simple_size_cells(const struct device_node *np);
84 * of_find_property() - find a property in a node
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
91 struct property *of_find_property(const struct device_node *np,
92 const char *name, int *lenp);
95 * of_get_property() - get a property value
97 * Find a property with a given name for a given node and return the value.
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
104 const void *of_get_property(const struct device_node *np, const char *name,
108 * of_device_is_compatible() - Check if the node matches given constraints
109 * @device: pointer to node
110 * @compat: required compatible string, NULL or "" for any match
111 * @type: required device_type value, NULL or "" for any match
112 * @name: required node name, NULL or "" for any match
114 * Checks if the given @compat, @type and @name strings match the
115 * properties of the given @device. A constraints can be skipped by
116 * passing NULL or an empty string as the constraint.
118 * @return 0 for no match, and a positive integer on match. The return
119 * value is a relative score with larger values indicating better
120 * matches. The score is weighted for the most specific compatible value
121 * to get the highest score. Matching type is next, followed by matching
122 * name. Practically speaking, this results in the following priority
125 * 1. specific compatible && type && name
126 * 2. specific compatible && type
127 * 3. specific compatible && name
128 * 4. specific compatible
129 * 5. general compatible && type && name
130 * 6. general compatible && type
131 * 7. general compatible && name
132 * 8. general compatible
137 int of_device_is_compatible(const struct device_node *np, const char *compat,
138 const char *type, const char *name);
141 * of_device_is_available() - check if a device is available for use
143 * @device: Node to check for availability
145 * @return true if the status property is absent or set to "okay", false
148 bool of_device_is_available(const struct device_node *np);
151 * of_get_parent() - Get a node's parent, if any
153 * @node: Node to check
154 * @eturns a node pointer, or NULL if none
156 struct device_node *of_get_parent(const struct device_node *np);
159 * of_find_node_opts_by_path() - Find a node matching a full OF path
161 * @path: Either the full path to match, or if the path does not start with
162 * '/', the name of a property of the /aliases node (an alias). In the
163 * case of an alias, the node matching the alias' value will be returned.
164 * @opts: Address of a pointer into which to store the start of an options
165 * string appended to the end of the path with a ':' separator. Can be NULL
170 * foo/bar Valid alias + relative path
172 * @return a node pointer or NULL if not found
174 struct device_node *of_find_node_opts_by_path(const char *path,
177 static inline struct device_node *of_find_node_by_path(const char *path)
179 return of_find_node_opts_by_path(path, NULL);
183 * of_find_compatible_node() - find a node based on its compatible string
185 * Find a node based on type and one of the tokens in its "compatible" property
186 * @from: Node to start searching from or NULL. the node you pass will not be
187 * searched, only the next one will; typically, you pass what the previous
189 * @type: The type string to match "device_type" or NULL to ignore
190 * @compatible: The string to match to one of the tokens in the device
192 * @return node pointer or NULL if not found
194 struct device_node *of_find_compatible_node(struct device_node *from,
195 const char *type, const char *compatible);
198 * of_find_node_by_phandle() - Find a node given a phandle
200 * @handle: phandle of the node to find
202 * @return node pointer, or NULL if not found
204 struct device_node *of_find_node_by_phandle(phandle handle);
207 * of_read_u32() - Find and read a 32-bit integer from a property
209 * Search for a property in a device node and read a 32-bit value from
212 * @np: device node from which the property value is to be read.
213 * @propname: name of the property to be searched.
214 * @outp: pointer to return value, modified only if return value is 0.
216 * @return 0 on success, -EINVAL if the property does not exist,
217 * -ENODATA if property does not have a value, and -EOVERFLOW if the
218 * property data isn't large enough.
220 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
223 * of_read_u32_array() - Find and read an array of 32 bit integers
225 * Search for a property in a device node and read 32-bit value(s) from
228 * @np: device node from which the property value is to be read.
229 * @propname: name of the property to be searched.
230 * @out_values: pointer to return value, modified only if return value is 0.
231 * @sz: number of array elements to read
232 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
233 * if property does not have a value, and -EOVERFLOW is longer than sz.
235 int of_read_u32_array(const struct device_node *np, const char *propname,
236 u32 *out_values, size_t sz);
239 * of_property_match_string() - Find string in a list and return index
241 * This function searches a string list property and returns the index
242 * of a specific string value.
244 * @np: pointer to node containing string list property
245 * @propname: string list property name
246 * @string: pointer to string to search for in string list
247 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
248 * if property does not have a value, and -EOVERFLOW is longer than sz.
250 int of_property_match_string(const struct device_node *np, const char *propname,
253 int of_property_read_string_helper(const struct device_node *np,
254 const char *propname, const char **out_strs,
255 size_t sz, int index);
258 * of_property_read_string_index() - Find and read a string from a multiple
260 * @np: device node from which the property value is to be read.
261 * @propname: name of the property to be searched.
262 * @index: index of the string in the list of strings
263 * @out_string: pointer to null terminated return string, modified only if
266 * Search for a property in a device tree node and retrieve a null
267 * terminated string value (pointer to data, not a copy) in the list of strings
268 * contained in that property.
269 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
270 * property does not have a value, and -EILSEQ if the string is not
271 * null-terminated within the length of the property data.
273 * The out_string pointer is modified only if a valid string can be decoded.
275 static inline int of_property_read_string_index(const struct device_node *np,
276 const char *propname,
277 int index, const char **output)
279 int rc = of_property_read_string_helper(np, propname, output, 1, index);
280 return rc < 0 ? rc : 0;
284 * of_property_count_strings() - Find and return the number of strings from a
285 * multiple strings property.
286 * @np: device node from which the property value is to be read.
287 * @propname: name of the property to be searched.
289 * Search for a property in a device tree node and retrieve the number of null
290 * terminated string contain in it. Returns the number of strings on
291 * success, -EINVAL if the property does not exist, -ENODATA if property
292 * does not have a value, and -EILSEQ if the string is not null-terminated
293 * within the length of the property data.
295 static inline int of_property_count_strings(const struct device_node *np,
296 const char *propname)
298 return of_property_read_string_helper(np, propname, NULL, 0, 0);
302 * of_parse_phandle - Resolve a phandle property to a device_node pointer
303 * @np: Pointer to device node holding phandle property
304 * @phandle_name: Name of property holding a phandle value
305 * @index: For properties holding a table of phandles, this is the index into
308 * Returns the device_node pointer with refcount incremented. Use
309 * of_node_put() on it when done.
311 struct device_node *of_parse_phandle(const struct device_node *np,
312 const char *phandle_name, int index);
315 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
317 * @np: pointer to a device tree node containing a list
318 * @list_name: property name that contains a list
319 * @cells_name: property name that specifies phandles' arguments count
320 * @index: index of a phandle to parse out
321 * @out_args: optional pointer to output arguments structure (will be filled)
322 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
323 * @list_name does not exist, -EINVAL if a phandle was not found,
324 * @cells_name could not be found, the arguments were truncated or there
325 * were too many arguments.
327 * This function is useful to parse lists of phandles and their arguments.
328 * Returns 0 on success and fills out_args, on error returns appropriate
331 * Caller is responsible to call of_node_put() on the returned out_args->np
345 * list = <&phandle1 1 2 &phandle2 3>;
348 * To get a device_node of the `node2' node you may call this:
349 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
351 int of_parse_phandle_with_args(const struct device_node *np,
352 const char *list_name, const char *cells_name,
353 int index, struct of_phandle_args *out_args);
356 * of_count_phandle_with_args() - Count the number of phandle in a list
358 * @np: pointer to a device tree node containing a list
359 * @list_name: property name that contains a list
360 * @cells_name: property name that specifies phandles' arguments count
361 * @return number of phandle found, -ENOENT if
362 * @list_name does not exist, -EINVAL if a phandle was not found,
363 * @cells_name could not be found, the arguments were truncated or there
364 * were too many arguments.
366 * Returns number of phandle found on success, on error returns appropriate
370 int of_count_phandle_with_args(const struct device_node *np,
371 const char *list_name, const char *cells_name);
374 * of_alias_scan() - Scan all properties of the 'aliases' node
376 * The function scans all the properties of the 'aliases' node and populates
377 * the lookup table with the properties. It returns the number of alias
378 * properties found, or an error code in case of failure.
380 * @return 9 if OK, -ENOMEM if not enough memory
382 int of_alias_scan(void);
385 * of_alias_get_id - Get alias id for the given device_node
387 * Travels the lookup table to get the alias id for the given device_node and
390 * @np: Pointer to the given device_node
391 * @stem: Alias stem of the given device_node
392 * @return alias ID, if found, else -ENODEV
394 int of_alias_get_id(const struct device_node *np, const char *stem);
397 * of_get_stdout() - Get node to use for stdout
399 * @return node referred to by stdout-path alias, or NULL if none
401 struct device_node *of_get_stdout(void);