xref: /rk3399_rockchip-uboot/include/dm/ofnode.h (revision 9e51204527dcae59a326c51a71c9b80effd8db05)
14984de2bSSimon Glass /*
24984de2bSSimon Glass  * Copyright (c) 2017 Google, Inc
34984de2bSSimon Glass  * Written by Simon Glass <sjg@chromium.org>
44984de2bSSimon Glass  *
54984de2bSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
64984de2bSSimon Glass  */
74984de2bSSimon Glass 
84984de2bSSimon Glass #ifndef _DM_OFNODE_H
94984de2bSSimon Glass #define _DM_OFNODE_H
104984de2bSSimon Glass 
11*9e512045SSimon Glass /* TODO(sjg@chromium.org): Drop fdtdec.h include */
12*9e512045SSimon Glass #include <fdtdec.h>
13*9e512045SSimon Glass #include <dm/of.h>
14*9e512045SSimon Glass 
15*9e512045SSimon Glass /* Enable checks to protect against invalid calls */
16*9e512045SSimon Glass #undef OF_CHECKS
17*9e512045SSimon Glass 
184984de2bSSimon Glass /**
194984de2bSSimon Glass  * ofnode - reference to a device tree node
204984de2bSSimon Glass  *
214984de2bSSimon Glass  * This union can hold either a straightforward pointer to a struct device_node
224984de2bSSimon Glass  * in the live device tree, or an offset within the flat device tree. In the
234984de2bSSimon Glass  * latter case, the pointer value is just the integer offset within the flat DT.
244984de2bSSimon Glass  *
254984de2bSSimon Glass  * Thus we can reference nodes in both the live tree (once available) and the
264984de2bSSimon Glass  * flat tree (until then). Functions are available to translate between an
274984de2bSSimon Glass  * ofnode and either an offset or a struct device_node *.
284984de2bSSimon Glass  *
294984de2bSSimon Glass  * The reference can also hold a null offset, in which case the pointer value
30*9e512045SSimon Glass  * here is NULL. This corresponds to a struct device_node * value of
314984de2bSSimon Glass  * NULL, or an offset of -1.
324984de2bSSimon Glass  *
334984de2bSSimon Glass  * There is no ambiguity as to whether ofnode holds an offset or a node
344984de2bSSimon Glass  * pointer: when the live tree is active it holds a node pointer, otherwise it
354984de2bSSimon Glass  * holds an offset. The value itself does not need to be unique and in theory
364984de2bSSimon Glass  * the same value could point to a valid device node or a valid offset. We
374984de2bSSimon Glass  * could arrange for a unique value to be used (e.g. by making the pointer
384984de2bSSimon Glass  * point to an offset within the flat device tree in the case of an offset) but
394984de2bSSimon Glass  * this increases code size slightly due to the subtraction. Since it offers no
404984de2bSSimon Glass  * real benefit, the approach described here seems best.
414984de2bSSimon Glass  *
424984de2bSSimon Glass  * For now these points use constant types, since we don't allow writing
434984de2bSSimon Glass  * the DT.
444984de2bSSimon Glass  *
454984de2bSSimon Glass  * @np: Pointer to device node, used for live tree
464984de2bSSimon Glass  * @flat_ptr: Pointer into flat device tree, used for flat tree. Note that this
474984de2bSSimon Glass  *	is not a really a pointer to a node: it is an offset value. See above.
484984de2bSSimon Glass  */
494984de2bSSimon Glass typedef union ofnode_union {
504984de2bSSimon Glass 	const struct device_node *np;	/* will be used for future live tree */
514984de2bSSimon Glass 	long of_offset;
524984de2bSSimon Glass } ofnode;
534984de2bSSimon Glass 
54*9e512045SSimon Glass struct ofnode_phandle_args {
55*9e512045SSimon Glass 	ofnode node;
56*9e512045SSimon Glass 	int args_count;
57*9e512045SSimon Glass 	uint32_t args[OF_MAX_PHANDLE_ARGS];
58*9e512045SSimon Glass };
59*9e512045SSimon Glass 
60*9e512045SSimon Glass /**
61*9e512045SSimon Glass  * _ofnode_to_np() - convert an ofnode to a live DT node pointer
62*9e512045SSimon Glass  *
63*9e512045SSimon Glass  * This cannot be called if the reference contains an offset.
64*9e512045SSimon Glass  *
65*9e512045SSimon Glass  * @node: Reference containing struct device_node * (possibly invalid)
66*9e512045SSimon Glass  * @return pointer to device node (can be NULL)
67*9e512045SSimon Glass  */
68*9e512045SSimon Glass static inline const struct device_node *ofnode_to_np(ofnode node)
69*9e512045SSimon Glass {
70*9e512045SSimon Glass #ifdef OF_CHECKS
71*9e512045SSimon Glass 	if (!of_live_active())
72*9e512045SSimon Glass 		return NULL;
73*9e512045SSimon Glass #endif
74*9e512045SSimon Glass 	return node.np;
75*9e512045SSimon Glass }
76*9e512045SSimon Glass 
774984de2bSSimon Glass /**
784984de2bSSimon Glass  * ofnode_to_offset() - convert an ofnode to a flat DT offset
794984de2bSSimon Glass  *
804984de2bSSimon Glass  * This cannot be called if the reference contains a node pointer.
814984de2bSSimon Glass  *
824984de2bSSimon Glass  * @node: Reference containing offset (possibly invalid)
834984de2bSSimon Glass  * @return DT offset (can be -1)
844984de2bSSimon Glass  */
854984de2bSSimon Glass static inline int ofnode_to_offset(ofnode node)
864984de2bSSimon Glass {
87*9e512045SSimon Glass #ifdef OF_CHECKS
88*9e512045SSimon Glass 	if (of_live_active())
89*9e512045SSimon Glass 		return -1;
90*9e512045SSimon Glass #endif
914984de2bSSimon Glass 	return node.of_offset;
924984de2bSSimon Glass }
934984de2bSSimon Glass 
944984de2bSSimon Glass /**
954984de2bSSimon Glass  * ofnode_valid() - check if an ofnode is valid
964984de2bSSimon Glass  *
974984de2bSSimon Glass  * @return true if the reference contains a valid ofnode, false if it is NULL
984984de2bSSimon Glass  */
994984de2bSSimon Glass static inline bool ofnode_valid(ofnode node)
1004984de2bSSimon Glass {
101*9e512045SSimon Glass 	if (of_live_active())
102*9e512045SSimon Glass 		return node.np != NULL;
103*9e512045SSimon Glass 	else
1044984de2bSSimon Glass 		return node.of_offset != -1;
1054984de2bSSimon Glass }
1064984de2bSSimon Glass 
1074984de2bSSimon Glass /**
1084984de2bSSimon Glass  * offset_to_ofnode() - convert a DT offset to an ofnode
1094984de2bSSimon Glass  *
1104984de2bSSimon Glass  * @of_offset: DT offset (either valid, or -1)
1114984de2bSSimon Glass  * @return reference to the associated DT offset
1124984de2bSSimon Glass  */
1134984de2bSSimon Glass static inline ofnode offset_to_ofnode(int of_offset)
1144984de2bSSimon Glass {
1154984de2bSSimon Glass 	ofnode node;
1164984de2bSSimon Glass 
117*9e512045SSimon Glass 	if (of_live_active())
118*9e512045SSimon Glass 		node.np = NULL;
119*9e512045SSimon Glass 	else
1204984de2bSSimon Glass 		node.of_offset = of_offset;
1214984de2bSSimon Glass 
1224984de2bSSimon Glass 	return node;
1234984de2bSSimon Glass }
1244984de2bSSimon Glass 
1254984de2bSSimon Glass /**
126*9e512045SSimon Glass  * np_to_ofnode() - convert a node pointer to an ofnode
127*9e512045SSimon Glass  *
128*9e512045SSimon Glass  * @np: Live node pointer (can be NULL)
129*9e512045SSimon Glass  * @return reference to the associated node pointer
130*9e512045SSimon Glass  */
131*9e512045SSimon Glass static inline ofnode np_to_ofnode(const struct device_node *np)
132*9e512045SSimon Glass {
133*9e512045SSimon Glass 	ofnode node;
134*9e512045SSimon Glass 
135*9e512045SSimon Glass 	node.np = np;
136*9e512045SSimon Glass 
137*9e512045SSimon Glass 	return node;
138*9e512045SSimon Glass }
139*9e512045SSimon Glass 
140*9e512045SSimon Glass /**
141*9e512045SSimon Glass  * ofnode_is_np() - check if a reference is a node pointer
142*9e512045SSimon Glass  *
143*9e512045SSimon Glass  * This function associated that if there is a valid live tree then all
144*9e512045SSimon Glass  * references will use it. This is because using the flat DT when the live tree
145*9e512045SSimon Glass  * is valid is not permitted.
146*9e512045SSimon Glass  *
147*9e512045SSimon Glass  * @node: reference to check (possibly invalid)
148*9e512045SSimon Glass  * @return true if the reference is a live node pointer, false if it is a DT
149*9e512045SSimon Glass  * offset
150*9e512045SSimon Glass  */
151*9e512045SSimon Glass static inline bool ofnode_is_np(ofnode node)
152*9e512045SSimon Glass {
153*9e512045SSimon Glass #ifdef OF_CHECKS
154*9e512045SSimon Glass 	/*
155*9e512045SSimon Glass 	 * Check our assumption that flat tree offsets are not used when a
156*9e512045SSimon Glass 	 * live tree is in use.
157*9e512045SSimon Glass 	 */
158*9e512045SSimon Glass 	assert(!ofnode_valid(node) ||
159*9e512045SSimon Glass 	       (of_live_active() ? _ofnode_to_np(node)
160*9e512045SSimon Glass 				  : _ofnode_to_np(node)));
161*9e512045SSimon Glass #endif
162*9e512045SSimon Glass 	return of_live_active() && ofnode_valid(node);
163*9e512045SSimon Glass }
164*9e512045SSimon Glass 
165*9e512045SSimon Glass /**
1664984de2bSSimon Glass  * ofnode_equal() - check if two references are equal
1674984de2bSSimon Glass  *
1684984de2bSSimon Glass  * @return true if equal, else false
1694984de2bSSimon Glass  */
1704984de2bSSimon Glass static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
1714984de2bSSimon Glass {
1724984de2bSSimon Glass 	/* We only need to compare the contents */
1734984de2bSSimon Glass 	return ref1.of_offset == ref2.of_offset;
1744984de2bSSimon Glass }
1754984de2bSSimon Glass 
176*9e512045SSimon Glass /**
177*9e512045SSimon Glass  * ofnode_null() - Obtain a null ofnode
178*9e512045SSimon Glass  *
179*9e512045SSimon Glass  * This returns an ofnode which points to no node. It works both with the flat
180*9e512045SSimon Glass  * tree and livetree.
181*9e512045SSimon Glass  */
182*9e512045SSimon Glass static inline ofnode ofnode_null(void)
183*9e512045SSimon Glass {
184*9e512045SSimon Glass 	ofnode node;
185*9e512045SSimon Glass 
186*9e512045SSimon Glass 	if (of_live_active())
187*9e512045SSimon Glass 		node.np = NULL;
188*9e512045SSimon Glass 	else
189*9e512045SSimon Glass 		node.of_offset = -1;
190*9e512045SSimon Glass 
191*9e512045SSimon Glass 	return node;
192*9e512045SSimon Glass }
193*9e512045SSimon Glass 
194*9e512045SSimon Glass /**
195*9e512045SSimon Glass  * ofnode_read_u32() - Read a 32-bit integer from a property
196*9e512045SSimon Glass  *
197*9e512045SSimon Glass  * @ref:	valid node reference to read property from
198*9e512045SSimon Glass  * @propname:	name of the property to read from
199*9e512045SSimon Glass  * @outp:	place to put value (if found)
200*9e512045SSimon Glass  * @return 0 if OK, -ve on error
201*9e512045SSimon Glass  */
202*9e512045SSimon Glass int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
203*9e512045SSimon Glass 
204*9e512045SSimon Glass /**
205*9e512045SSimon Glass  * ofnode_read_s32() - Read a 32-bit integer from a property
206*9e512045SSimon Glass  *
207*9e512045SSimon Glass  * @ref:	valid node reference to read property from
208*9e512045SSimon Glass  * @propname:	name of the property to read from
209*9e512045SSimon Glass  * @outp:	place to put value (if found)
210*9e512045SSimon Glass  * @return 0 if OK, -ve on error
211*9e512045SSimon Glass  */
212*9e512045SSimon Glass static inline int ofnode_read_s32(ofnode node, const char *propname,
213*9e512045SSimon Glass 				  s32 *out_value)
214*9e512045SSimon Glass {
215*9e512045SSimon Glass 	return ofnode_read_u32(node, propname, (u32 *)out_value);
216*9e512045SSimon Glass }
217*9e512045SSimon Glass 
218*9e512045SSimon Glass /**
219*9e512045SSimon Glass  * ofnode_read_u32_default() - Read a 32-bit integer from a property
220*9e512045SSimon Glass  *
221*9e512045SSimon Glass  * @ref:	valid node reference to read property from
222*9e512045SSimon Glass  * @propname:	name of the property to read from
223*9e512045SSimon Glass  * @def:	default value to return if the property has no value
224*9e512045SSimon Glass  * @return property value, or @def if not found
225*9e512045SSimon Glass  */
226*9e512045SSimon Glass int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
227*9e512045SSimon Glass 
228*9e512045SSimon Glass /**
229*9e512045SSimon Glass  * ofnode_read_s32_default() - Read a 32-bit integer from a property
230*9e512045SSimon Glass  *
231*9e512045SSimon Glass  * @ref:	valid node reference to read property from
232*9e512045SSimon Glass  * @propname:	name of the property to read from
233*9e512045SSimon Glass  * @def:	default value to return if the property has no value
234*9e512045SSimon Glass  * @return property value, or @def if not found
235*9e512045SSimon Glass  */
236*9e512045SSimon Glass int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
237*9e512045SSimon Glass 
238*9e512045SSimon Glass /**
239*9e512045SSimon Glass  * ofnode_read_string() - Read a string from a property
240*9e512045SSimon Glass  *
241*9e512045SSimon Glass  * @ref:	valid node reference to read property from
242*9e512045SSimon Glass  * @propname:	name of the property to read
243*9e512045SSimon Glass  * @return string from property value, or NULL if there is no such property
244*9e512045SSimon Glass  */
245*9e512045SSimon Glass const char *ofnode_read_string(ofnode node, const char *propname);
246*9e512045SSimon Glass 
247*9e512045SSimon Glass /**
248*9e512045SSimon Glass  * ofnode_read_u32_array - Find and read an array of 32 bit integers
249*9e512045SSimon Glass  *
250*9e512045SSimon Glass  * @node:	valid node reference to read property from
251*9e512045SSimon Glass  * @propname:	name of the property to read
252*9e512045SSimon Glass  * @out_values:	pointer to return value, modified only if return value is 0
253*9e512045SSimon Glass  * @sz:		number of array elements to read
254*9e512045SSimon Glass  *
255*9e512045SSimon Glass  * Search for a property in a device node and read 32-bit value(s) from
256*9e512045SSimon Glass  * it. Returns 0 on success, -EINVAL if the property does not exist,
257*9e512045SSimon Glass  * -ENODATA if property does not have a value, and -EOVERFLOW if the
258*9e512045SSimon Glass  * property data isn't large enough.
259*9e512045SSimon Glass  *
260*9e512045SSimon Glass  * The out_values is modified only if a valid u32 value can be decoded.
261*9e512045SSimon Glass  */
262*9e512045SSimon Glass int ofnode_read_u32_array(ofnode node, const char *propname,
263*9e512045SSimon Glass 			  u32 *out_values, size_t sz);
264*9e512045SSimon Glass 
265*9e512045SSimon Glass /**
266*9e512045SSimon Glass  * ofnode_read_bool() - read a boolean value from a property
267*9e512045SSimon Glass  *
268*9e512045SSimon Glass  * @node:	valid node reference to read property from
269*9e512045SSimon Glass  * @propname:	name of property to read
270*9e512045SSimon Glass  * @return true if property is present (meaning true), false if not present
271*9e512045SSimon Glass  */
272*9e512045SSimon Glass bool ofnode_read_bool(ofnode node, const char *propname);
273*9e512045SSimon Glass 
274*9e512045SSimon Glass /**
275*9e512045SSimon Glass  * ofnode_find_subnode() - find a named subnode of a parent node
276*9e512045SSimon Glass  *
277*9e512045SSimon Glass  * @node:	valid reference to parent node
278*9e512045SSimon Glass  * @subnode_name: name of subnode to find
279*9e512045SSimon Glass  * @return reference to subnode (which can be invalid if there is no such
280*9e512045SSimon Glass  * subnode)
281*9e512045SSimon Glass  */
282*9e512045SSimon Glass ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
283*9e512045SSimon Glass 
284*9e512045SSimon Glass /**
285*9e512045SSimon Glass  * ofnode_first_subnode() - find the first subnode of a parent node
286*9e512045SSimon Glass  *
287*9e512045SSimon Glass  * @node:	valid reference to a valid parent node
288*9e512045SSimon Glass  * @return reference to the first subnode (which can be invalid if the parent
289*9e512045SSimon Glass  * node has no subnodes)
290*9e512045SSimon Glass  */
291*9e512045SSimon Glass ofnode ofnode_first_subnode(ofnode node);
292*9e512045SSimon Glass 
293*9e512045SSimon Glass /**
294*9e512045SSimon Glass  * ofnode_next_subnode() - find the next sibling of a subnode
295*9e512045SSimon Glass  *
296*9e512045SSimon Glass  * @node:	valid reference to previous node (sibling)
297*9e512045SSimon Glass  * @return reference to the next subnode (which can be invalid if the node
298*9e512045SSimon Glass  * has no more siblings)
299*9e512045SSimon Glass  */
300*9e512045SSimon Glass ofnode ofnode_next_subnode(ofnode node);
301*9e512045SSimon Glass 
302*9e512045SSimon Glass /**
303*9e512045SSimon Glass  * ofnode_get_name() - get the name of a node
304*9e512045SSimon Glass  *
305*9e512045SSimon Glass  * @node: valid node to look up
306*9e512045SSimon Glass  * @return name or node
307*9e512045SSimon Glass  */
308*9e512045SSimon Glass const char *ofnode_get_name(ofnode node);
309*9e512045SSimon Glass 
310*9e512045SSimon Glass /**
311*9e512045SSimon Glass  * ofnode_read_size() - read the size of a property
312*9e512045SSimon Glass  *
313*9e512045SSimon Glass  * @node: node to check
314*9e512045SSimon Glass  * @propname: property to check
315*9e512045SSimon Glass  * @return size of property if present, or -EINVAL if not
316*9e512045SSimon Glass  */
317*9e512045SSimon Glass int ofnode_read_size(ofnode node, const char *propname);
318*9e512045SSimon Glass 
319*9e512045SSimon Glass /**
320*9e512045SSimon Glass  * ofnode_stringlist_search() - find a string in a string list and return index
321*9e512045SSimon Glass  *
322*9e512045SSimon Glass  * Note that it is possible for this function to succeed on property values
323*9e512045SSimon Glass  * that are not NUL-terminated. That's because the function will stop after
324*9e512045SSimon Glass  * finding the first occurrence of @string. This can for example happen with
325*9e512045SSimon Glass  * small-valued cell properties, such as #address-cells, when searching for
326*9e512045SSimon Glass  * the empty string.
327*9e512045SSimon Glass  *
328*9e512045SSimon Glass  * @node: node to check
329*9e512045SSimon Glass  * @propname: name of the property containing the string list
330*9e512045SSimon Glass  * @string: string to look up in the string list
331*9e512045SSimon Glass  *
332*9e512045SSimon Glass  * @return:
333*9e512045SSimon Glass  *   the index of the string in the list of strings
334*9e512045SSimon Glass  *   -ENODATA if the property is not found
335*9e512045SSimon Glass  *   -EINVAL on some other error
336*9e512045SSimon Glass  */
337*9e512045SSimon Glass int ofnode_stringlist_search(ofnode node, const char *propname,
338*9e512045SSimon Glass 			     const char *string);
339*9e512045SSimon Glass 
340*9e512045SSimon Glass /**
341*9e512045SSimon Glass  * fdt_stringlist_get() - obtain the string at a given index in a string list
342*9e512045SSimon Glass  *
343*9e512045SSimon Glass  * Note that this will successfully extract strings from properties with
344*9e512045SSimon Glass  * non-NUL-terminated values. For example on small-valued cell properties
345*9e512045SSimon Glass  * this function will return the empty string.
346*9e512045SSimon Glass  *
347*9e512045SSimon Glass  * If non-NULL, the length of the string (on success) or a negative error-code
348*9e512045SSimon Glass  * (on failure) will be stored in the integer pointer to by lenp.
349*9e512045SSimon Glass  *
350*9e512045SSimon Glass  * @node: node to check
351*9e512045SSimon Glass  * @propname: name of the property containing the string list
352*9e512045SSimon Glass  * @index: index of the string to return
353*9e512045SSimon Glass  * @lenp: return location for the string length or an error code on failure
354*9e512045SSimon Glass  *
355*9e512045SSimon Glass  * @return:
356*9e512045SSimon Glass  *   length of string, if found or -ve error value if not found
357*9e512045SSimon Glass  */
358*9e512045SSimon Glass int ofnode_read_string_index(ofnode node, const char *propname, int index,
359*9e512045SSimon Glass 			     const char **outp);
360*9e512045SSimon Glass 
361*9e512045SSimon Glass /**
362*9e512045SSimon Glass  * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
363*9e512045SSimon Glass  *
364*9e512045SSimon Glass  * This function is useful to parse lists of phandles and their arguments.
365*9e512045SSimon Glass  * Returns 0 on success and fills out_args, on error returns appropriate
366*9e512045SSimon Glass  * errno value.
367*9e512045SSimon Glass  *
368*9e512045SSimon Glass  * Caller is responsible to call of_node_put() on the returned out_args->np
369*9e512045SSimon Glass  * pointer.
370*9e512045SSimon Glass  *
371*9e512045SSimon Glass  * Example:
372*9e512045SSimon Glass  *
373*9e512045SSimon Glass  * phandle1: node1 {
374*9e512045SSimon Glass  *	#list-cells = <2>;
375*9e512045SSimon Glass  * }
376*9e512045SSimon Glass  *
377*9e512045SSimon Glass  * phandle2: node2 {
378*9e512045SSimon Glass  *	#list-cells = <1>;
379*9e512045SSimon Glass  * }
380*9e512045SSimon Glass  *
381*9e512045SSimon Glass  * node3 {
382*9e512045SSimon Glass  *	list = <&phandle1 1 2 &phandle2 3>;
383*9e512045SSimon Glass  * }
384*9e512045SSimon Glass  *
385*9e512045SSimon Glass  * To get a device_node of the `node2' node you may call this:
386*9e512045SSimon Glass  * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
387*9e512045SSimon Glass  *
388*9e512045SSimon Glass  * @node:	device tree node containing a list
389*9e512045SSimon Glass  * @list_name:	property name that contains a list
390*9e512045SSimon Glass  * @cells_name:	property name that specifies phandles' arguments count
391*9e512045SSimon Glass  * @cells_count: Cell count to use if @cells_name is NULL
392*9e512045SSimon Glass  * @index:	index of a phandle to parse out
393*9e512045SSimon Glass  * @out_args:	optional pointer to output arguments structure (will be filled)
394*9e512045SSimon Glass  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
395*9e512045SSimon Glass  *	@list_name does not exist, -EINVAL if a phandle was not found,
396*9e512045SSimon Glass  *	@cells_name could not be found, the arguments were truncated or there
397*9e512045SSimon Glass  *	were too many arguments.
398*9e512045SSimon Glass  */
399*9e512045SSimon Glass int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
400*9e512045SSimon Glass 				   const char *cells_name, int cell_count,
401*9e512045SSimon Glass 				   int index,
402*9e512045SSimon Glass 				   struct ofnode_phandle_args *out_args);
403*9e512045SSimon Glass 
404*9e512045SSimon Glass /**
405*9e512045SSimon Glass  * ofnode_path() - find a node by full path
406*9e512045SSimon Glass  *
407*9e512045SSimon Glass  * @path: Full path to node, e.g. "/bus/spi@1"
408*9e512045SSimon Glass  * @return reference to the node found. Use ofnode_valid() to check if it exists
409*9e512045SSimon Glass  */
410*9e512045SSimon Glass ofnode ofnode_path(const char *path);
411*9e512045SSimon Glass 
412*9e512045SSimon Glass /**
413*9e512045SSimon Glass  * ofnode_get_chosen_prop() - get the value of a chosen property
414*9e512045SSimon Glass  *
415*9e512045SSimon Glass  * This looks for a property within the /chosen node and returns its value
416*9e512045SSimon Glass  *
417*9e512045SSimon Glass  * @propname: Property name to look for
418*9e512045SSimon Glass  */
419*9e512045SSimon Glass const char *ofnode_get_chosen_prop(const char *propname);
420*9e512045SSimon Glass 
421*9e512045SSimon Glass /**
422*9e512045SSimon Glass  * ofnode_get_chosen_node() - get the chosen node
423*9e512045SSimon Glass  *
424*9e512045SSimon Glass  * @return the chosen node if present, else ofnode_null()
425*9e512045SSimon Glass  */
426*9e512045SSimon Glass ofnode ofnode_get_chosen_node(const char *name);
427*9e512045SSimon Glass 
428*9e512045SSimon Glass struct display_timing;
429*9e512045SSimon Glass /**
430*9e512045SSimon Glass  * ofnode_decode_display_timing() - decode display timings
431*9e512045SSimon Glass  *
432*9e512045SSimon Glass  * Decode display timings from the supplied 'display-timings' node.
433*9e512045SSimon Glass  * See doc/device-tree-bindings/video/display-timing.txt for binding
434*9e512045SSimon Glass  * information.
435*9e512045SSimon Glass  *
436*9e512045SSimon Glass  * @node	'display-timing' node containing the timing subnodes
437*9e512045SSimon Glass  * @index	Index number to read (0=first timing subnode)
438*9e512045SSimon Glass  * @config	Place to put timings
439*9e512045SSimon Glass  * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
440*9e512045SSimon Glass  */
441*9e512045SSimon Glass int ofnode_decode_display_timing(ofnode node, int index,
442*9e512045SSimon Glass 				 struct display_timing *config);
443*9e512045SSimon Glass 
444*9e512045SSimon Glass /**
445*9e512045SSimon Glass  * ofnode_read_prop()- - read a node property
446*9e512045SSimon Glass  *
447*9e512045SSimon Glass  * @node: node to read
448*9e512045SSimon Glass  * @propname: property to read
449*9e512045SSimon Glass  * @lenp: place to put length on success
450*9e512045SSimon Glass  * @return pointer to property, or NULL if not found
451*9e512045SSimon Glass  */
452*9e512045SSimon Glass const u32 *ofnode_read_prop(ofnode node, const char *propname, int *lenp);
453*9e512045SSimon Glass 
454*9e512045SSimon Glass /**
455*9e512045SSimon Glass  * ofnode_is_available() - check if a node is marked available
456*9e512045SSimon Glass  *
457*9e512045SSimon Glass  * @node: node to check
458*9e512045SSimon Glass  * @return true if node's 'status' property is "okay" (or is missing)
459*9e512045SSimon Glass  */
460*9e512045SSimon Glass bool ofnode_is_available(ofnode node);
461*9e512045SSimon Glass 
462*9e512045SSimon Glass /**
463*9e512045SSimon Glass  * ofnode_get_addr_size() - get address and size from a property
464*9e512045SSimon Glass  *
465*9e512045SSimon Glass  * This does no address translation. It simply reads an property that contains
466*9e512045SSimon Glass  * an address and a size value, one after the other.
467*9e512045SSimon Glass  *
468*9e512045SSimon Glass  * @node: node to read from
469*9e512045SSimon Glass  * @propname: property to read
470*9e512045SSimon Glass  * @sizep: place to put size value (on success)
471*9e512045SSimon Glass  * @return address value, or FDT_ADDR_T_NONE on error
472*9e512045SSimon Glass  */
473*9e512045SSimon Glass phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
474*9e512045SSimon Glass 				 phys_size_t *sizep);
475*9e512045SSimon Glass 
476*9e512045SSimon Glass /**
477*9e512045SSimon Glass  * ofnode_read_u8_array_ptr() - find an 8-bit array
478*9e512045SSimon Glass  *
479*9e512045SSimon Glass  * Look up a property in a node and return a pointer to its contents as a
480*9e512045SSimon Glass  * byte array of given length. The property must have at least enough data
481*9e512045SSimon Glass  * for the array (count bytes). It may have more, but this will be ignored.
482*9e512045SSimon Glass  * The data is not copied.
483*9e512045SSimon Glass  *
484*9e512045SSimon Glass  * @node	node to examine
485*9e512045SSimon Glass  * @propname	name of property to find
486*9e512045SSimon Glass  * @sz		number of array elements
487*9e512045SSimon Glass  * @return pointer to byte array if found, or NULL if the property is not
488*9e512045SSimon Glass  *		found or there is not enough data
489*9e512045SSimon Glass  */
490*9e512045SSimon Glass const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
491*9e512045SSimon Glass 					size_t sz);
492*9e512045SSimon Glass 
493*9e512045SSimon Glass /**
494*9e512045SSimon Glass  * ofnode_read_pci_addr() - look up a PCI address
495*9e512045SSimon Glass  *
496*9e512045SSimon Glass  * Look at an address property in a node and return the PCI address which
497*9e512045SSimon Glass  * corresponds to the given type in the form of fdt_pci_addr.
498*9e512045SSimon Glass  * The property must hold one fdt_pci_addr with a lengh.
499*9e512045SSimon Glass  *
500*9e512045SSimon Glass  * @node	node to examine
501*9e512045SSimon Glass  * @type	pci address type (FDT_PCI_SPACE_xxx)
502*9e512045SSimon Glass  * @propname	name of property to find
503*9e512045SSimon Glass  * @addr	returns pci address in the form of fdt_pci_addr
504*9e512045SSimon Glass  * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
505*9e512045SSimon Glass  *		format of the property was invalid, -ENXIO if the requested
506*9e512045SSimon Glass  *		address type was not found
507*9e512045SSimon Glass  */
508*9e512045SSimon Glass int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
509*9e512045SSimon Glass 			 const char *propname, struct fdt_pci_addr *addr);
510*9e512045SSimon Glass 
511*9e512045SSimon Glass /**
512*9e512045SSimon Glass  * ofnode_read_addr_cells() - Get the number of address cells for a node
513*9e512045SSimon Glass  *
514*9e512045SSimon Glass  * This walks back up the tree to find the closest #address-cells property
515*9e512045SSimon Glass  * which controls the given node.
516*9e512045SSimon Glass  *
517*9e512045SSimon Glass  * @node: Node to check
518*9e512045SSimon Glass  * @return number of address cells this node uses
519*9e512045SSimon Glass  */
520*9e512045SSimon Glass int ofnode_read_addr_cells(ofnode node);
521*9e512045SSimon Glass 
522*9e512045SSimon Glass /**
523*9e512045SSimon Glass  * ofnode_read_size_cells() - Get the number of size cells for a node
524*9e512045SSimon Glass  *
525*9e512045SSimon Glass  * This walks back up the tree to find the closest #size-cells property
526*9e512045SSimon Glass  * which controls the given node.
527*9e512045SSimon Glass  *
528*9e512045SSimon Glass  * @node: Node to check
529*9e512045SSimon Glass  * @return number of size cells this node uses
530*9e512045SSimon Glass  */
531*9e512045SSimon Glass int ofnode_read_size_cells(ofnode node);
532*9e512045SSimon Glass 
533*9e512045SSimon Glass /**
534*9e512045SSimon Glass  * ofnode_pre_reloc() - check if a node should be bound before relocation
535*9e512045SSimon Glass  *
536*9e512045SSimon Glass  * Device tree nodes can be marked as needing-to-be-bound in the loader stages
537*9e512045SSimon Glass  * via special device tree properties.
538*9e512045SSimon Glass  *
539*9e512045SSimon Glass  * Before relocation this function can be used to check if nodes are required
540*9e512045SSimon Glass  * in either SPL or TPL stages.
541*9e512045SSimon Glass  *
542*9e512045SSimon Glass  * After relocation and jumping into the real U-Boot binary it is possible to
543*9e512045SSimon Glass  * determine if a node was bound in one of SPL/TPL stages.
544*9e512045SSimon Glass  *
545*9e512045SSimon Glass  * There are 3 settings currently in use
546*9e512045SSimon Glass  * -
547*9e512045SSimon Glass  * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
548*9e512045SSimon Glass  *   Existing platforms only use it to indicate nodes needed in
549*9e512045SSimon Glass  *   SPL. Should probably be replaced by u-boot,dm-spl for
550*9e512045SSimon Glass  *   new platforms.
551*9e512045SSimon Glass  *
552*9e512045SSimon Glass  * @node: node to check
553*9e512045SSimon Glass  * @eturns true if node is needed in SPL/TL, false otherwise
554*9e512045SSimon Glass  */
555*9e512045SSimon Glass bool ofnode_pre_reloc(ofnode node);
556*9e512045SSimon Glass 
5574984de2bSSimon Glass #endif
558