blob: d2827001e2be7e211a572594ac1e00199962c66b [file] [log] [blame]
Simon Glass644ec0a2017-05-18 20:08:54 -06001/*
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 */
31struct 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)
39static 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 */
50int 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 */
61int of_n_size_cells(const struct device_node *np);
62
63/**
64 * of_find_property() - find a property in a node
65 *
66 * @np: Pointer to device node holding property
67 * @name: Name of property
68 * @lenp: If non-NULL, returns length of property
69 * @return pointer to property, or NULL if not found
70 */
71struct property *of_find_property(const struct device_node *np,
72 const char *name, int *lenp);
73
74/**
75 * of_get_property() - get a property value
76 *
77 * Find a property with a given name for a given node and return the value.
78 *
79 * @np: Pointer to device node holding property
80 * @name: Name of property
81 * @lenp: If non-NULL, returns length of property
82 * @return pointer to property value, or NULL if not found
83 */
84const void *of_get_property(const struct device_node *np, const char *name,
85 int *lenp);
86
87/**
88 * of_device_is_compatible() - Check if the node matches given constraints
89 * @device: pointer to node
90 * @compat: required compatible string, NULL or "" for any match
91 * @type: required device_type value, NULL or "" for any match
92 * @name: required node name, NULL or "" for any match
93 *
94 * Checks if the given @compat, @type and @name strings match the
95 * properties of the given @device. A constraints can be skipped by
96 * passing NULL or an empty string as the constraint.
97 *
98 * @return 0 for no match, and a positive integer on match. The return
99 * value is a relative score with larger values indicating better
100 * matches. The score is weighted for the most specific compatible value
101 * to get the highest score. Matching type is next, followed by matching
102 * name. Practically speaking, this results in the following priority
103 * order for matches:
104 *
105 * 1. specific compatible && type && name
106 * 2. specific compatible && type
107 * 3. specific compatible && name
108 * 4. specific compatible
109 * 5. general compatible && type && name
110 * 6. general compatible && type
111 * 7. general compatible && name
112 * 8. general compatible
113 * 9. type && name
114 * 10. type
115 * 11. name
116 */
117int of_device_is_compatible(const struct device_node *np, const char *compat,
118 const char *type, const char *name);
119
120/**
121 * of_device_is_available() - check if a device is available for use
122 *
123 * @device: Node to check for availability
124 *
125 * @return true if the status property is absent or set to "okay", false
126 * otherwise
127 */
128bool of_device_is_available(const struct device_node *np);
129
130/**
131 * of_get_parent() - Get a node's parent, if any
132 *
133 * @node: Node to check
134 * @eturns a node pointer, or NULL if none
135 */
136struct device_node *of_get_parent(const struct device_node *np);
137
138/**
139 * of_find_node_opts_by_path() - Find a node matching a full OF path
140 *
141 * @path: Either the full path to match, or if the path does not start with
142 * '/', the name of a property of the /aliases node (an alias). In the
143 * case of an alias, the node matching the alias' value will be returned.
144 * @opts: Address of a pointer into which to store the start of an options
145 * string appended to the end of the path with a ':' separator. Can be NULL
146 *
147 * Valid paths:
148 * /foo/bar Full path
149 * foo Valid alias
150 * foo/bar Valid alias + relative path
151 *
152 * @return a node pointer or NULL if not found
153 */
154struct device_node *of_find_node_opts_by_path(const char *path,
155 const char **opts);
156
157static inline struct device_node *of_find_node_by_path(const char *path)
158{
159 return of_find_node_opts_by_path(path, NULL);
160}
161
162/**
163 * of_find_compatible_node() - find a node based on its compatible string
164 *
165 * Find a node based on type and one of the tokens in its "compatible" property
166 * @from: Node to start searching from or NULL. the node you pass will not be
167 * searched, only the next one will; typically, you pass what the previous
168 * call returned.
169 * @type: The type string to match "device_type" or NULL to ignore
170 * @compatible: The string to match to one of the tokens in the device
171 * "compatible" list.
172 * @return node pointer or NULL if not found
173 */
174struct device_node *of_find_compatible_node(struct device_node *from,
175 const char *type, const char *compatible);
176
177/**
178 * of_find_node_by_phandle() - Find a node given a phandle
179 *
180 * @handle: phandle of the node to find
181 *
182 * @return node pointer, or NULL if not found
183 */
184struct device_node *of_find_node_by_phandle(phandle handle);
185
186/**
187 * of_read_u32() - Find and read a 32-bit integer from a property
188 *
189 * Search for a property in a device node and read a 32-bit value from
190 * it.
191 *
192 * @np: device node from which the property value is to be read.
193 * @propname: name of the property to be searched.
194 * @outp: pointer to return value, modified only if return value is 0.
195 *
196 * @return 0 on success, -EINVAL if the property does not exist,
197 * -ENODATA if property does not have a value, and -EOVERFLOW if the
198 * property data isn't large enough.
199 */
200int of_read_u32(const struct device_node *np, const char *propname, u32 *outp);
201
202/**
203 * of_read_u32_array() - Find and read an array of 32 bit integers
204 *
205 * Search for a property in a device node and read 32-bit value(s) from
206 * it.
207 *
208 * @np: device node from which the property value is to be read.
209 * @propname: name of the property to be searched.
210 * @out_values: pointer to return value, modified only if return value is 0.
211 * @sz: number of array elements to read
212 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
213 * if property does not have a value, and -EOVERFLOW is longer than sz.
214 */
215int of_read_u32_array(const struct device_node *np, const char *propname,
216 u32 *out_values, size_t sz);
217
218/**
219 * of_property_match_string() - Find string in a list and return index
220 *
221 * This function searches a string list property and returns the index
222 * of a specific string value.
223 *
224 * @np: pointer to node containing string list property
225 * @propname: string list property name
226 * @string: pointer to string to search for in string list
227 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
228 * if property does not have a value, and -EOVERFLOW is longer than sz.
229 */
230int of_property_match_string(const struct device_node *np, const char *propname,
231 const char *string);
232
233int of_property_read_string_helper(const struct device_node *np,
234 const char *propname, const char **out_strs,
235 size_t sz, int index);
236
237/**
238 * of_property_read_string_index() - Find and read a string from a multiple
239 * strings property.
240 * @np: device node from which the property value is to be read.
241 * @propname: name of the property to be searched.
242 * @index: index of the string in the list of strings
243 * @out_string: pointer to null terminated return string, modified only if
244 * return value is 0.
245 *
246 * Search for a property in a device tree node and retrieve a null
247 * terminated string value (pointer to data, not a copy) in the list of strings
248 * contained in that property.
249 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
250 * property does not have a value, and -EILSEQ if the string is not
251 * null-terminated within the length of the property data.
252 *
253 * The out_string pointer is modified only if a valid string can be decoded.
254 */
255static inline int of_property_read_string_index(const struct device_node *np,
256 const char *propname,
257 int index, const char **output)
258{
259 int rc = of_property_read_string_helper(np, propname, output, 1, index);
260 return rc < 0 ? rc : 0;
261}
262
263/**
Simon Glass8c293d62017-06-12 06:21:28 -0600264 * of_property_count_strings() - Find and return the number of strings from a
265 * multiple strings property.
266 * @np: device node from which the property value is to be read.
267 * @propname: name of the property to be searched.
268 *
269 * Search for a property in a device tree node and retrieve the number of null
270 * terminated string contain in it. Returns the number of strings on
271 * success, -EINVAL if the property does not exist, -ENODATA if property
272 * does not have a value, and -EILSEQ if the string is not null-terminated
273 * within the length of the property data.
274 */
275static inline int of_property_count_strings(const struct device_node *np,
276 const char *propname)
277{
278 return of_property_read_string_helper(np, propname, NULL, 0, 0);
279}
280
281/**
Simon Glass644ec0a2017-05-18 20:08:54 -0600282 * of_parse_phandle - Resolve a phandle property to a device_node pointer
283 * @np: Pointer to device node holding phandle property
284 * @phandle_name: Name of property holding a phandle value
285 * @index: For properties holding a table of phandles, this is the index into
286 * the table
287 *
288 * Returns the device_node pointer with refcount incremented. Use
289 * of_node_put() on it when done.
290 */
291struct device_node *of_parse_phandle(const struct device_node *np,
292 const char *phandle_name, int index);
293
294/**
295 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
296 *
297 * @np: pointer to a device tree node containing a list
298 * @list_name: property name that contains a list
299 * @cells_name: property name that specifies phandles' arguments count
300 * @index: index of a phandle to parse out
301 * @out_args: optional pointer to output arguments structure (will be filled)
302 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
303 * @list_name does not exist, -EINVAL if a phandle was not found,
304 * @cells_name could not be found, the arguments were truncated or there
305 * were too many arguments.
306 *
307 * This function is useful to parse lists of phandles and their arguments.
308 * Returns 0 on success and fills out_args, on error returns appropriate
309 * errno value.
310 *
311 * Caller is responsible to call of_node_put() on the returned out_args->np
312 * pointer.
313 *
314 * Example:
315 *
316 * phandle1: node1 {
317 * #list-cells = <2>;
318 * }
319 *
320 * phandle2: node2 {
321 * #list-cells = <1>;
322 * }
323 *
324 * node3 {
325 * list = <&phandle1 1 2 &phandle2 3>;
326 * }
327 *
328 * To get a device_node of the `node2' node you may call this:
329 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
330 */
331int of_parse_phandle_with_args(const struct device_node *np,
332 const char *list_name, const char *cells_name,
333 int index, struct of_phandle_args *out_args);
334
335/**
336 * of_alias_scan() - Scan all properties of the 'aliases' node
337 *
338 * The function scans all the properties of the 'aliases' node and populates
339 * the lookup table with the properties. It returns the number of alias
340 * properties found, or an error code in case of failure.
341 *
342 * @return 9 if OK, -ENOMEM if not enough memory
343 */
344int of_alias_scan(void);
345
346/**
347 * of_alias_get_id - Get alias id for the given device_node
348 *
349 * Travels the lookup table to get the alias id for the given device_node and
350 * alias stem.
351 *
352 * @np: Pointer to the given device_node
353 * @stem: Alias stem of the given device_node
354 * @return alias ID, if found, else -ENODEV
355 */
356int of_alias_get_id(const struct device_node *np, const char *stem);
357
358/**
359 * of_get_stdout() - Get node to use for stdout
360 *
361 * @return node referred to by stdout-path alias, or NULL if none
362 */
363struct device_node *of_get_stdout(void);
364
365#endif