1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2017 Google, Inc
3*4882a593Smuzhiyun * Written by Simon Glass <sjg@chromium.org>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _DM_OFNODE_H
9*4882a593Smuzhiyun #define _DM_OFNODE_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /* TODO(sjg@chromium.org): Drop fdtdec.h include */
12*4882a593Smuzhiyun #include <fdtdec.h>
13*4882a593Smuzhiyun #include <dm/of.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /* Enable checks to protect against invalid calls */
16*4882a593Smuzhiyun #undef OF_CHECKS
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun struct resource;
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * ofnode - reference to a device tree node
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * This union can hold either a straightforward pointer to a struct device_node
24*4882a593Smuzhiyun * in the live device tree, or an offset within the flat device tree. In the
25*4882a593Smuzhiyun * latter case, the pointer value is just the integer offset within the flat DT.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * Thus we can reference nodes in both the live tree (once available) and the
28*4882a593Smuzhiyun * flat tree (until then). Functions are available to translate between an
29*4882a593Smuzhiyun * ofnode and either an offset or a struct device_node *.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * The reference can also hold a null offset, in which case the pointer value
32*4882a593Smuzhiyun * here is NULL. This corresponds to a struct device_node * value of
33*4882a593Smuzhiyun * NULL, or an offset of -1.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * There is no ambiguity as to whether ofnode holds an offset or a node
36*4882a593Smuzhiyun * pointer: when the live tree is active it holds a node pointer, otherwise it
37*4882a593Smuzhiyun * holds an offset. The value itself does not need to be unique and in theory
38*4882a593Smuzhiyun * the same value could point to a valid device node or a valid offset. We
39*4882a593Smuzhiyun * could arrange for a unique value to be used (e.g. by making the pointer
40*4882a593Smuzhiyun * point to an offset within the flat device tree in the case of an offset) but
41*4882a593Smuzhiyun * this increases code size slightly due to the subtraction. Since it offers no
42*4882a593Smuzhiyun * real benefit, the approach described here seems best.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * For now these points use constant types, since we don't allow writing
45*4882a593Smuzhiyun * the DT.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * @np: Pointer to device node, used for live tree
48*4882a593Smuzhiyun * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
49*4882a593Smuzhiyun * is not a really a pointer to a node: it is an offset value. See above.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun typedef union ofnode_union {
52*4882a593Smuzhiyun const struct device_node *np; /* will be used for future live tree */
53*4882a593Smuzhiyun long of_offset;
54*4882a593Smuzhiyun } ofnode;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun struct ofnode_phandle_args {
57*4882a593Smuzhiyun ofnode node;
58*4882a593Smuzhiyun int args_count;
59*4882a593Smuzhiyun uint32_t args[OF_MAX_PHANDLE_ARGS];
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun * ofprop - reference to a property of a device tree node
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * This struct hold the reference on one property of one node,
66*4882a593Smuzhiyun * using struct ofnode and an offset within the flat device tree or either
67*4882a593Smuzhiyun * a pointer to a struct property in the live device tree.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * Thus we can reference arguments in both the live tree and the flat tree.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * The property reference can also hold a null reference. This corresponds to
72*4882a593Smuzhiyun * a struct property NULL pointer or an offset of -1.
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * @node: Pointer to device node
75*4882a593Smuzhiyun * @offset: Pointer into flat device tree, used for flat tree.
76*4882a593Smuzhiyun * @prop: Pointer to property, used for live treee.
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun struct ofprop {
80*4882a593Smuzhiyun ofnode node;
81*4882a593Smuzhiyun union {
82*4882a593Smuzhiyun int offset;
83*4882a593Smuzhiyun const struct property *prop;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun * _ofnode_to_np() - convert an ofnode to a live DT node pointer
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * This cannot be called if the reference contains an offset.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * @node: Reference containing struct device_node * (possibly invalid)
93*4882a593Smuzhiyun * @return pointer to device node (can be NULL)
94*4882a593Smuzhiyun */
ofnode_to_np(ofnode node)95*4882a593Smuzhiyun static inline const struct device_node *ofnode_to_np(ofnode node)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun #ifdef OF_CHECKS
98*4882a593Smuzhiyun if (!of_live_active())
99*4882a593Smuzhiyun return NULL;
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun return node.np;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun * ofnode_to_offset() - convert an ofnode to a flat DT offset
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * This cannot be called if the reference contains a node pointer.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * @node: Reference containing offset (possibly invalid)
110*4882a593Smuzhiyun * @return DT offset (can be -1)
111*4882a593Smuzhiyun */
ofnode_to_offset(ofnode node)112*4882a593Smuzhiyun static inline int ofnode_to_offset(ofnode node)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun #ifdef OF_CHECKS
115*4882a593Smuzhiyun if (of_live_active())
116*4882a593Smuzhiyun return -1;
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun return node.of_offset;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun * ofnode_valid() - check if an ofnode is valid
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * @return true if the reference contains a valid ofnode, false if it is NULL
125*4882a593Smuzhiyun */
ofnode_valid(ofnode node)126*4882a593Smuzhiyun static inline bool ofnode_valid(ofnode node)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun if (of_live_active())
129*4882a593Smuzhiyun return node.np != NULL;
130*4882a593Smuzhiyun else
131*4882a593Smuzhiyun return node.of_offset != -1;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /**
135*4882a593Smuzhiyun * offset_to_ofnode() - convert a DT offset to an ofnode
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * @of_offset: DT offset (either valid, or -1)
138*4882a593Smuzhiyun * @return reference to the associated DT offset
139*4882a593Smuzhiyun */
offset_to_ofnode(int of_offset)140*4882a593Smuzhiyun static inline ofnode offset_to_ofnode(int of_offset)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun ofnode node;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun if (of_live_active())
145*4882a593Smuzhiyun node.np = NULL;
146*4882a593Smuzhiyun else
147*4882a593Smuzhiyun node.of_offset = of_offset;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun return node;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /**
153*4882a593Smuzhiyun * np_to_ofnode() - convert a node pointer to an ofnode
154*4882a593Smuzhiyun *
155*4882a593Smuzhiyun * @np: Live node pointer (can be NULL)
156*4882a593Smuzhiyun * @return reference to the associated node pointer
157*4882a593Smuzhiyun */
np_to_ofnode(const struct device_node * np)158*4882a593Smuzhiyun static inline ofnode np_to_ofnode(const struct device_node *np)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun ofnode node;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun node.np = np;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return node;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun * ofnode_is_np() - check if a reference is a node pointer
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * This function associated that if there is a valid live tree then all
171*4882a593Smuzhiyun * references will use it. This is because using the flat DT when the live tree
172*4882a593Smuzhiyun * is valid is not permitted.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * @node: reference to check (possibly invalid)
175*4882a593Smuzhiyun * @return true if the reference is a live node pointer, false if it is a DT
176*4882a593Smuzhiyun * offset
177*4882a593Smuzhiyun */
ofnode_is_np(ofnode node)178*4882a593Smuzhiyun static inline bool ofnode_is_np(ofnode node)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun #ifdef OF_CHECKS
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun * Check our assumption that flat tree offsets are not used when a
183*4882a593Smuzhiyun * live tree is in use.
184*4882a593Smuzhiyun */
185*4882a593Smuzhiyun assert(!ofnode_valid(node) ||
186*4882a593Smuzhiyun (of_live_active() ? _ofnode_to_np(node)
187*4882a593Smuzhiyun : _ofnode_to_np(node)));
188*4882a593Smuzhiyun #endif
189*4882a593Smuzhiyun return of_live_active() && ofnode_valid(node);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun * ofnode_equal() - check if two references are equal
194*4882a593Smuzhiyun *
195*4882a593Smuzhiyun * @return true if equal, else false
196*4882a593Smuzhiyun */
ofnode_equal(ofnode ref1,ofnode ref2)197*4882a593Smuzhiyun static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun /* We only need to compare the contents */
200*4882a593Smuzhiyun return ref1.of_offset == ref2.of_offset;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /**
204*4882a593Smuzhiyun * ofnode_null() - Obtain a null ofnode
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * This returns an ofnode which points to no node. It works both with the flat
207*4882a593Smuzhiyun * tree and livetree.
208*4882a593Smuzhiyun */
ofnode_null(void)209*4882a593Smuzhiyun static inline ofnode ofnode_null(void)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun ofnode node;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun if (of_live_active())
214*4882a593Smuzhiyun node.np = NULL;
215*4882a593Smuzhiyun else
216*4882a593Smuzhiyun node.of_offset = -1;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun return node;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /**
222*4882a593Smuzhiyun * ofnode_read_u32() - Read a 32-bit integer from a property
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * @ref: valid node reference to read property from
225*4882a593Smuzhiyun * @propname: name of the property to read from
226*4882a593Smuzhiyun * @outp: place to put value (if found)
227*4882a593Smuzhiyun * @return 0 if OK, -ve on error
228*4882a593Smuzhiyun */
229*4882a593Smuzhiyun int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * ofnode_read_s32() - Read a 32-bit integer from a property
233*4882a593Smuzhiyun *
234*4882a593Smuzhiyun * @ref: valid node reference to read property from
235*4882a593Smuzhiyun * @propname: name of the property to read from
236*4882a593Smuzhiyun * @outp: place to put value (if found)
237*4882a593Smuzhiyun * @return 0 if OK, -ve on error
238*4882a593Smuzhiyun */
ofnode_read_s32(ofnode node,const char * propname,s32 * out_value)239*4882a593Smuzhiyun static inline int ofnode_read_s32(ofnode node, const char *propname,
240*4882a593Smuzhiyun s32 *out_value)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun return ofnode_read_u32(node, propname, (u32 *)out_value);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /**
246*4882a593Smuzhiyun * ofnode_read_u32_default() - Read a 32-bit integer from a property
247*4882a593Smuzhiyun *
248*4882a593Smuzhiyun * @ref: valid node reference to read property from
249*4882a593Smuzhiyun * @propname: name of the property to read from
250*4882a593Smuzhiyun * @def: default value to return if the property has no value
251*4882a593Smuzhiyun * @return property value, or @def if not found
252*4882a593Smuzhiyun */
253*4882a593Smuzhiyun int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /**
256*4882a593Smuzhiyun * ofnode_read_u64() - Read a 64-bit integer from a property
257*4882a593Smuzhiyun *
258*4882a593Smuzhiyun * @ref: valid node reference to read property from
259*4882a593Smuzhiyun * @propname: name of the property to read from
260*4882a593Smuzhiyun * @outp: place to put value (if found)
261*4882a593Smuzhiyun * @return 0 if OK, -ve on error
262*4882a593Smuzhiyun */
263*4882a593Smuzhiyun int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /**
266*4882a593Smuzhiyun * ofnode_read_s32_default() - Read a 32-bit integer from a property
267*4882a593Smuzhiyun *
268*4882a593Smuzhiyun * @ref: valid node reference to read property from
269*4882a593Smuzhiyun * @propname: name of the property to read from
270*4882a593Smuzhiyun * @def: default value to return if the property has no value
271*4882a593Smuzhiyun * @return property value, or @def if not found
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /**
276*4882a593Smuzhiyun * ofnode_read_string() - Read a string from a property
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * @ref: valid node reference to read property from
279*4882a593Smuzhiyun * @propname: name of the property to read
280*4882a593Smuzhiyun * @return string from property value, or NULL if there is no such property
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun const char *ofnode_read_string(ofnode node, const char *propname);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun * ofnode_read_u32_array() - Find and read an array of 32 bit integers
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * @node: valid node reference to read property from
288*4882a593Smuzhiyun * @propname: name of the property to read
289*4882a593Smuzhiyun * @out_values: pointer to return value, modified only if return value is 0
290*4882a593Smuzhiyun * @sz: number of array elements to read
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * Search for a property in a device node and read 32-bit value(s) from
293*4882a593Smuzhiyun * it. Returns 0 on success, -EINVAL if the property does not exist,
294*4882a593Smuzhiyun * -ENODATA if property does not have a value, and -EOVERFLOW if the
295*4882a593Smuzhiyun * property data isn't large enough.
296*4882a593Smuzhiyun *
297*4882a593Smuzhiyun * The out_values is modified only if a valid u32 value can be decoded.
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun int ofnode_read_u32_array(ofnode node, const char *propname,
300*4882a593Smuzhiyun u32 *out_values, size_t sz);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /**
303*4882a593Smuzhiyun * ofnode_write_u32_array() - Find and write an array of 32 bit integers
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * @node: valid node reference to read property from
306*4882a593Smuzhiyun * @propname: name of the property to read
307*4882a593Smuzhiyun * @values: pointer to update value, modified only if return value is 0
308*4882a593Smuzhiyun * @sz: number of array elements to read
309*4882a593Smuzhiyun * @return 0 on success, -EINVAL if the property does not exist, -ENODATA
310*4882a593Smuzhiyun * if property does not have a value, and -EOVERFLOW is longer than sz.
311*4882a593Smuzhiyun */
312*4882a593Smuzhiyun int ofnode_write_u32_array(ofnode node, const char *propname,
313*4882a593Smuzhiyun u32 *values, size_t sz);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /**
316*4882a593Smuzhiyun * ofnode_read_bool() - read a boolean value from a property
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * @node: valid node reference to read property from
319*4882a593Smuzhiyun * @propname: name of property to read
320*4882a593Smuzhiyun * @return true if property is present (meaning true), false if not present
321*4882a593Smuzhiyun */
322*4882a593Smuzhiyun bool ofnode_read_bool(ofnode node, const char *propname);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun /**
325*4882a593Smuzhiyun * ofnode_find_subnode() - find a named subnode of a parent node
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * @node: valid reference to parent node
328*4882a593Smuzhiyun * @subnode_name: name of subnode to find
329*4882a593Smuzhiyun * @return reference to subnode (which can be invalid if there is no such
330*4882a593Smuzhiyun * subnode)
331*4882a593Smuzhiyun */
332*4882a593Smuzhiyun ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /**
335*4882a593Smuzhiyun * ofnode_first_subnode() - find the first subnode of a parent node
336*4882a593Smuzhiyun *
337*4882a593Smuzhiyun * @node: valid reference to a valid parent node
338*4882a593Smuzhiyun * @return reference to the first subnode (which can be invalid if the parent
339*4882a593Smuzhiyun * node has no subnodes)
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun ofnode ofnode_first_subnode(ofnode node);
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /**
344*4882a593Smuzhiyun * ofnode_next_subnode() - find the next sibling of a subnode
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * @node: valid reference to previous node (sibling)
347*4882a593Smuzhiyun * @return reference to the next subnode (which can be invalid if the node
348*4882a593Smuzhiyun * has no more siblings)
349*4882a593Smuzhiyun */
350*4882a593Smuzhiyun ofnode ofnode_next_subnode(ofnode node);
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /**
353*4882a593Smuzhiyun * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * @node: valid node to look up
356*4882a593Smuzhiyun * @return ofnode reference of the parent node
357*4882a593Smuzhiyun */
358*4882a593Smuzhiyun ofnode ofnode_get_parent(ofnode node);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /**
361*4882a593Smuzhiyun * ofnode_get_name() - get the name of a node
362*4882a593Smuzhiyun *
363*4882a593Smuzhiyun * @node: valid node to look up
364*4882a593Smuzhiyun * @return name or node
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun const char *ofnode_get_name(ofnode node);
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /**
369*4882a593Smuzhiyun * ofnode_get_by_phandle() - get ofnode from phandle
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * @phandle: phandle to look up
372*4882a593Smuzhiyun * @return ofnode reference to the phandle
373*4882a593Smuzhiyun */
374*4882a593Smuzhiyun ofnode ofnode_get_by_phandle(uint phandle);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun * ofnode_read_size() - read the size of a property
378*4882a593Smuzhiyun *
379*4882a593Smuzhiyun * @node: node to check
380*4882a593Smuzhiyun * @propname: property to check
381*4882a593Smuzhiyun * @return size of property if present, or -EINVAL if not
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun int ofnode_read_size(ofnode node, const char *propname);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /**
386*4882a593Smuzhiyun * ofnode_get_addr_index() - get an address from a node
387*4882a593Smuzhiyun *
388*4882a593Smuzhiyun * This reads the register address from a node
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * @node: node to read from
391*4882a593Smuzhiyun * @index: Index of address to read (0 for first)
392*4882a593Smuzhiyun * @return address, or FDT_ADDR_T_NONE if not present or invalid
393*4882a593Smuzhiyun */
394*4882a593Smuzhiyun phys_addr_t ofnode_get_addr_index(ofnode node, int index);
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun /**
397*4882a593Smuzhiyun * ofnode_get_addr() - get an address from a node
398*4882a593Smuzhiyun *
399*4882a593Smuzhiyun * This reads the register address from a node
400*4882a593Smuzhiyun *
401*4882a593Smuzhiyun * @node: node to read from
402*4882a593Smuzhiyun * @return address, or FDT_ADDR_T_NONE if not present or invalid
403*4882a593Smuzhiyun */
404*4882a593Smuzhiyun phys_addr_t ofnode_get_addr(ofnode node);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun * ofnode_stringlist_search() - find a string in a string list and return index
408*4882a593Smuzhiyun *
409*4882a593Smuzhiyun * Note that it is possible for this function to succeed on property values
410*4882a593Smuzhiyun * that are not NUL-terminated. That's because the function will stop after
411*4882a593Smuzhiyun * finding the first occurrence of @string. This can for example happen with
412*4882a593Smuzhiyun * small-valued cell properties, such as #address-cells, when searching for
413*4882a593Smuzhiyun * the empty string.
414*4882a593Smuzhiyun *
415*4882a593Smuzhiyun * @node: node to check
416*4882a593Smuzhiyun * @propname: name of the property containing the string list
417*4882a593Smuzhiyun * @string: string to look up in the string list
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * @return:
420*4882a593Smuzhiyun * the index of the string in the list of strings
421*4882a593Smuzhiyun * -ENODATA if the property is not found
422*4882a593Smuzhiyun * -EINVAL on some other error
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun int ofnode_stringlist_search(ofnode node, const char *propname,
425*4882a593Smuzhiyun const char *string);
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /**
428*4882a593Smuzhiyun * ofnode_read_string_index() - obtain an indexed string from a string list
429*4882a593Smuzhiyun *
430*4882a593Smuzhiyun * Note that this will successfully extract strings from properties with
431*4882a593Smuzhiyun * non-NUL-terminated values. For example on small-valued cell properties
432*4882a593Smuzhiyun * this function will return the empty string.
433*4882a593Smuzhiyun *
434*4882a593Smuzhiyun * If non-NULL, the length of the string (on success) or a negative error-code
435*4882a593Smuzhiyun * (on failure) will be stored in the integer pointer to by lenp.
436*4882a593Smuzhiyun *
437*4882a593Smuzhiyun * @node: node to check
438*4882a593Smuzhiyun * @propname: name of the property containing the string list
439*4882a593Smuzhiyun * @index: index of the string to return
440*4882a593Smuzhiyun * @lenp: return location for the string length or an error code on failure
441*4882a593Smuzhiyun *
442*4882a593Smuzhiyun * @return:
443*4882a593Smuzhiyun * length of string, if found or -ve error value if not found
444*4882a593Smuzhiyun */
445*4882a593Smuzhiyun int ofnode_read_string_index(ofnode node, const char *propname, int index,
446*4882a593Smuzhiyun const char **outp);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /**
449*4882a593Smuzhiyun * ofnode_read_string_count() - find the number of strings in a string list
450*4882a593Smuzhiyun *
451*4882a593Smuzhiyun * @node: node to check
452*4882a593Smuzhiyun * @propname: name of the property containing the string list
453*4882a593Smuzhiyun * @return:
454*4882a593Smuzhiyun * number of strings in the list, or -ve error value if not found
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun int ofnode_read_string_count(ofnode node, const char *property);
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /**
459*4882a593Smuzhiyun * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * This function is useful to parse lists of phandles and their arguments.
462*4882a593Smuzhiyun * Returns 0 on success and fills out_args, on error returns appropriate
463*4882a593Smuzhiyun * errno value.
464*4882a593Smuzhiyun *
465*4882a593Smuzhiyun * Caller is responsible to call of_node_put() on the returned out_args->np
466*4882a593Smuzhiyun * pointer.
467*4882a593Smuzhiyun *
468*4882a593Smuzhiyun * Example:
469*4882a593Smuzhiyun *
470*4882a593Smuzhiyun * phandle1: node1 {
471*4882a593Smuzhiyun * #list-cells = <2>;
472*4882a593Smuzhiyun * }
473*4882a593Smuzhiyun *
474*4882a593Smuzhiyun * phandle2: node2 {
475*4882a593Smuzhiyun * #list-cells = <1>;
476*4882a593Smuzhiyun * }
477*4882a593Smuzhiyun *
478*4882a593Smuzhiyun * node3 {
479*4882a593Smuzhiyun * list = <&phandle1 1 2 &phandle2 3>;
480*4882a593Smuzhiyun * }
481*4882a593Smuzhiyun *
482*4882a593Smuzhiyun * To get a device_node of the `node2' node you may call this:
483*4882a593Smuzhiyun * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
484*4882a593Smuzhiyun *
485*4882a593Smuzhiyun * @node: device tree node containing a list
486*4882a593Smuzhiyun * @list_name: property name that contains a list
487*4882a593Smuzhiyun * @cells_name: property name that specifies phandles' arguments count
488*4882a593Smuzhiyun * @cells_count: Cell count to use if @cells_name is NULL
489*4882a593Smuzhiyun * @index: index of a phandle to parse out
490*4882a593Smuzhiyun * @out_args: optional pointer to output arguments structure (will be filled)
491*4882a593Smuzhiyun * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
492*4882a593Smuzhiyun * @list_name does not exist, -EINVAL if a phandle was not found,
493*4882a593Smuzhiyun * @cells_name could not be found, the arguments were truncated or there
494*4882a593Smuzhiyun * were too many arguments.
495*4882a593Smuzhiyun */
496*4882a593Smuzhiyun int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
497*4882a593Smuzhiyun const char *cells_name, int cell_count,
498*4882a593Smuzhiyun int index,
499*4882a593Smuzhiyun struct ofnode_phandle_args *out_args);
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /**
502*4882a593Smuzhiyun * ofnode_count_phandle_with_args() - Count number of phandle in a list
503*4882a593Smuzhiyun *
504*4882a593Smuzhiyun * This function is useful to count phandles into a list.
505*4882a593Smuzhiyun * Returns number of phandle on success, on error returns appropriate
506*4882a593Smuzhiyun * errno value.
507*4882a593Smuzhiyun *
508*4882a593Smuzhiyun * @node: device tree node containing a list
509*4882a593Smuzhiyun * @list_name: property name that contains a list
510*4882a593Smuzhiyun * @cells_name: property name that specifies phandles' arguments count
511*4882a593Smuzhiyun * @return number of phandle on success, -ENOENT if @list_name does not
512*4882a593Smuzhiyun * exist, -EINVAL if a phandle was not found, @cells_name could not
513*4882a593Smuzhiyun * be found.
514*4882a593Smuzhiyun */
515*4882a593Smuzhiyun int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
516*4882a593Smuzhiyun const char *cells_name);
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun /**
519*4882a593Smuzhiyun * ofnode_path() - find a node by full path
520*4882a593Smuzhiyun *
521*4882a593Smuzhiyun * @path: Full path to node, e.g. "/bus/spi@1"
522*4882a593Smuzhiyun * @return reference to the node found. Use ofnode_valid() to check if it exists
523*4882a593Smuzhiyun */
524*4882a593Smuzhiyun ofnode ofnode_path(const char *path);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun /**
527*4882a593Smuzhiyun * ofnode_get_chosen_prop() - get the value of a chosen property
528*4882a593Smuzhiyun *
529*4882a593Smuzhiyun * This looks for a property within the /chosen node and returns its value
530*4882a593Smuzhiyun *
531*4882a593Smuzhiyun * @propname: Property name to look for
532*4882a593Smuzhiyun */
533*4882a593Smuzhiyun const char *ofnode_get_chosen_prop(const char *propname);
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /**
536*4882a593Smuzhiyun * ofnode_get_chosen_node() - get the chosen node
537*4882a593Smuzhiyun *
538*4882a593Smuzhiyun * @return the chosen node if present, else ofnode_null()
539*4882a593Smuzhiyun */
540*4882a593Smuzhiyun ofnode ofnode_get_chosen_node(const char *name);
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun struct display_timing;
543*4882a593Smuzhiyun /**
544*4882a593Smuzhiyun * ofnode_decode_display_timing() - decode display timings
545*4882a593Smuzhiyun *
546*4882a593Smuzhiyun * Decode display timings from the supplied 'display-timings' node.
547*4882a593Smuzhiyun * See doc/device-tree-bindings/video/display-timing.txt for binding
548*4882a593Smuzhiyun * information.
549*4882a593Smuzhiyun *
550*4882a593Smuzhiyun * @node 'display-timing' node containing the timing subnodes
551*4882a593Smuzhiyun * @index Index number to read (0=first timing subnode)
552*4882a593Smuzhiyun * @config Place to put timings
553*4882a593Smuzhiyun * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
554*4882a593Smuzhiyun */
555*4882a593Smuzhiyun int ofnode_decode_display_timing(ofnode node, int index,
556*4882a593Smuzhiyun struct display_timing *config);
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /**
559*4882a593Smuzhiyun * ofnode_get_property() - get a pointer to the value of a node property
560*4882a593Smuzhiyun *
561*4882a593Smuzhiyun * @node: node to read
562*4882a593Smuzhiyun * @propname: property to read
563*4882a593Smuzhiyun * @lenp: place to put length on success
564*4882a593Smuzhiyun * @return pointer to property, or NULL if not found
565*4882a593Smuzhiyun */
566*4882a593Smuzhiyun const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun /**
569*4882a593Smuzhiyun * ofnode_get_first_property()- get the reference of the first property
570*4882a593Smuzhiyun *
571*4882a593Smuzhiyun * Get reference to the first property of the node, it is used to iterate
572*4882a593Smuzhiyun * and read all the property with ofnode_get_property_by_prop().
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * @node: node to read
575*4882a593Smuzhiyun * @prop: place to put argument reference
576*4882a593Smuzhiyun * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
577*4882a593Smuzhiyun */
578*4882a593Smuzhiyun int ofnode_get_first_property(ofnode node, struct ofprop *prop);
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun /**
581*4882a593Smuzhiyun * ofnode_get_next_property() - get the reference of the next property
582*4882a593Smuzhiyun *
583*4882a593Smuzhiyun * Get reference to the next property of the node, it is used to iterate
584*4882a593Smuzhiyun * and read all the property with ofnode_get_property_by_prop().
585*4882a593Smuzhiyun *
586*4882a593Smuzhiyun * @prop: reference of current argument and place to put reference of next one
587*4882a593Smuzhiyun * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
588*4882a593Smuzhiyun */
589*4882a593Smuzhiyun int ofnode_get_next_property(struct ofprop *prop);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun /**
592*4882a593Smuzhiyun * ofnode_get_property_by_prop() - get a pointer to the value of a property
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * Get value for the property identified by the provided reference.
595*4882a593Smuzhiyun *
596*4882a593Smuzhiyun * @prop: reference on property
597*4882a593Smuzhiyun * @propname: If non-NULL, place to property name on success,
598*4882a593Smuzhiyun * @lenp: If non-NULL, place to put length on success
599*4882a593Smuzhiyun * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
600*4882a593Smuzhiyun */
601*4882a593Smuzhiyun const void *ofnode_get_property_by_prop(const struct ofprop *prop,
602*4882a593Smuzhiyun const char **propname, int *lenp);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /**
605*4882a593Smuzhiyun * ofnode_is_available() - check if a node is marked available
606*4882a593Smuzhiyun *
607*4882a593Smuzhiyun * @node: node to check
608*4882a593Smuzhiyun * @return true if node's 'status' property is "okay" (or is missing)
609*4882a593Smuzhiyun */
610*4882a593Smuzhiyun bool ofnode_is_available(ofnode node);
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun /**
613*4882a593Smuzhiyun * ofnode_get_addr_size() - get address and size from a property
614*4882a593Smuzhiyun *
615*4882a593Smuzhiyun * This does no address translation. It simply reads an property that contains
616*4882a593Smuzhiyun * an address and a size value, one after the other.
617*4882a593Smuzhiyun *
618*4882a593Smuzhiyun * @node: node to read from
619*4882a593Smuzhiyun * @propname: property to read
620*4882a593Smuzhiyun * @sizep: place to put size value (on success)
621*4882a593Smuzhiyun * @return address value, or FDT_ADDR_T_NONE on error
622*4882a593Smuzhiyun */
623*4882a593Smuzhiyun phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
624*4882a593Smuzhiyun phys_size_t *sizep);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /**
627*4882a593Smuzhiyun * ofnode_read_u8_array_ptr() - find an 8-bit array
628*4882a593Smuzhiyun *
629*4882a593Smuzhiyun * Look up a property in a node and return a pointer to its contents as a
630*4882a593Smuzhiyun * byte array of given length. The property must have at least enough data
631*4882a593Smuzhiyun * for the array (count bytes). It may have more, but this will be ignored.
632*4882a593Smuzhiyun * The data is not copied.
633*4882a593Smuzhiyun *
634*4882a593Smuzhiyun * @node node to examine
635*4882a593Smuzhiyun * @propname name of property to find
636*4882a593Smuzhiyun * @sz number of array elements
637*4882a593Smuzhiyun * @return pointer to byte array if found, or NULL if the property is not
638*4882a593Smuzhiyun * found or there is not enough data
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
641*4882a593Smuzhiyun size_t sz);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /**
644*4882a593Smuzhiyun * ofnode_read_pci_addr() - look up a PCI address
645*4882a593Smuzhiyun *
646*4882a593Smuzhiyun * Look at an address property in a node and return the PCI address which
647*4882a593Smuzhiyun * corresponds to the given type in the form of fdt_pci_addr.
648*4882a593Smuzhiyun * The property must hold one fdt_pci_addr with a lengh.
649*4882a593Smuzhiyun *
650*4882a593Smuzhiyun * @node node to examine
651*4882a593Smuzhiyun * @type pci address type (FDT_PCI_SPACE_xxx)
652*4882a593Smuzhiyun * @propname name of property to find
653*4882a593Smuzhiyun * @addr returns pci address in the form of fdt_pci_addr
654*4882a593Smuzhiyun * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
655*4882a593Smuzhiyun * format of the property was invalid, -ENXIO if the requested
656*4882a593Smuzhiyun * address type was not found
657*4882a593Smuzhiyun */
658*4882a593Smuzhiyun int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
659*4882a593Smuzhiyun const char *propname, struct fdt_pci_addr *addr);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /**
662*4882a593Smuzhiyun * ofnode_read_addr_cells() - Get the number of address cells for a node
663*4882a593Smuzhiyun *
664*4882a593Smuzhiyun * This walks back up the tree to find the closest #address-cells property
665*4882a593Smuzhiyun * which controls the given node.
666*4882a593Smuzhiyun *
667*4882a593Smuzhiyun * @node: Node to check
668*4882a593Smuzhiyun * @return number of address cells this node uses
669*4882a593Smuzhiyun */
670*4882a593Smuzhiyun int ofnode_read_addr_cells(ofnode node);
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /**
673*4882a593Smuzhiyun * ofnode_read_size_cells() - Get the number of size cells for a node
674*4882a593Smuzhiyun *
675*4882a593Smuzhiyun * This walks back up the tree to find the closest #size-cells property
676*4882a593Smuzhiyun * which controls the given node.
677*4882a593Smuzhiyun *
678*4882a593Smuzhiyun * @node: Node to check
679*4882a593Smuzhiyun * @return number of size cells this node uses
680*4882a593Smuzhiyun */
681*4882a593Smuzhiyun int ofnode_read_size_cells(ofnode node);
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun /**
684*4882a593Smuzhiyun * ofnode_read_simple_addr_cells() - Get the address cells property in a node
685*4882a593Smuzhiyun *
686*4882a593Smuzhiyun * This function matches fdt_address_cells().
687*4882a593Smuzhiyun *
688*4882a593Smuzhiyun * @np: Node pointer to check
689*4882a593Smuzhiyun * @return value of #address-cells property in this node, or 2 if none
690*4882a593Smuzhiyun */
691*4882a593Smuzhiyun int ofnode_read_simple_addr_cells(ofnode node);
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /**
694*4882a593Smuzhiyun * ofnode_read_simple_size_cells() - Get the size cells property in a node
695*4882a593Smuzhiyun *
696*4882a593Smuzhiyun * This function matches fdt_size_cells().
697*4882a593Smuzhiyun *
698*4882a593Smuzhiyun * @np: Node pointer to check
699*4882a593Smuzhiyun * @return value of #size-cells property in this node, or 2 if none
700*4882a593Smuzhiyun */
701*4882a593Smuzhiyun int ofnode_read_simple_size_cells(ofnode node);
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /**
704*4882a593Smuzhiyun * ofnode_pre_reloc() - check if a node should be bound before relocation
705*4882a593Smuzhiyun *
706*4882a593Smuzhiyun * Device tree nodes can be marked as needing-to-be-bound in the loader stages
707*4882a593Smuzhiyun * via special device tree properties.
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * Before relocation this function can be used to check if nodes are required
710*4882a593Smuzhiyun * in either SPL or TPL stages.
711*4882a593Smuzhiyun *
712*4882a593Smuzhiyun * After relocation and jumping into the real U-Boot binary it is possible to
713*4882a593Smuzhiyun * determine if a node was bound in one of SPL/TPL stages.
714*4882a593Smuzhiyun *
715*4882a593Smuzhiyun * There are 3 settings currently in use
716*4882a593Smuzhiyun * -
717*4882a593Smuzhiyun * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
718*4882a593Smuzhiyun * Existing platforms only use it to indicate nodes needed in
719*4882a593Smuzhiyun * SPL. Should probably be replaced by u-boot,dm-spl for
720*4882a593Smuzhiyun * new platforms.
721*4882a593Smuzhiyun *
722*4882a593Smuzhiyun * @node: node to check
723*4882a593Smuzhiyun * @eturns true if node is needed in SPL/TL, false otherwise
724*4882a593Smuzhiyun */
725*4882a593Smuzhiyun bool ofnode_pre_reloc(ofnode node);
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun int ofnode_read_resource(ofnode node, uint index, struct resource *res);
728*4882a593Smuzhiyun int ofnode_read_resource_byname(ofnode node, const char *name,
729*4882a593Smuzhiyun struct resource *res);
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun /**
732*4882a593Smuzhiyun * ofnode_for_each_subnode() - iterate over all subnodes of a parent
733*4882a593Smuzhiyun *
734*4882a593Smuzhiyun * @node: child node (ofnode, lvalue)
735*4882a593Smuzhiyun * @parent: parent node (ofnode)
736*4882a593Smuzhiyun *
737*4882a593Smuzhiyun * This is a wrapper around a for loop and is used like so:
738*4882a593Smuzhiyun *
739*4882a593Smuzhiyun * ofnode node;
740*4882a593Smuzhiyun *
741*4882a593Smuzhiyun * ofnode_for_each_subnode(node, parent) {
742*4882a593Smuzhiyun * Use node
743*4882a593Smuzhiyun * ...
744*4882a593Smuzhiyun * }
745*4882a593Smuzhiyun *
746*4882a593Smuzhiyun * Note that this is implemented as a macro and @node is used as
747*4882a593Smuzhiyun * iterator in the loop. The parent variable can be a constant or even a
748*4882a593Smuzhiyun * literal.
749*4882a593Smuzhiyun */
750*4882a593Smuzhiyun #define ofnode_for_each_subnode(node, parent) \
751*4882a593Smuzhiyun for (node = ofnode_first_subnode(parent); \
752*4882a593Smuzhiyun ofnode_valid(node); \
753*4882a593Smuzhiyun node = ofnode_next_subnode(node))
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun /**
756*4882a593Smuzhiyun * ofnode_translate_address() - Tranlate a device-tree address
757*4882a593Smuzhiyun *
758*4882a593Smuzhiyun * Translate an address from the device-tree into a CPU physical address. This
759*4882a593Smuzhiyun * function walks up the tree and applies the various bus mappings along the
760*4882a593Smuzhiyun * way.
761*4882a593Smuzhiyun *
762*4882a593Smuzhiyun * @ofnode: Device tree node giving the context in which to translate the
763*4882a593Smuzhiyun * address
764*4882a593Smuzhiyun * @in_addr: pointer to the address to translate
765*4882a593Smuzhiyun * @return the translated address; OF_BAD_ADDR on error
766*4882a593Smuzhiyun */
767*4882a593Smuzhiyun u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
768*4882a593Smuzhiyun #endif
769