]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
4984de2b SG |
2 | /* |
3 | * Copyright (c) 2017 Google, Inc | |
4 | * Written by Simon Glass <[email protected]> | |
4984de2b SG |
5 | */ |
6 | ||
7 | #ifndef _DM_OFNODE_H | |
8 | #define _DM_OFNODE_H | |
9 | ||
9e512045 SG |
10 | /* TODO([email protected]): Drop fdtdec.h include */ |
11 | #include <fdtdec.h> | |
12 | #include <dm/of.h> | |
ec1add1e | 13 | #include <dm/of_access.h> |
45dbe75d | 14 | #include <log.h> |
9e512045 SG |
15 | |
16 | /* Enable checks to protect against invalid calls */ | |
17 | #undef OF_CHECKS | |
18 | ||
dcf98852 SG |
19 | struct resource; |
20 | ||
4984de2b SG |
21 | /** |
22 | * ofnode - reference to a device tree node | |
23 | * | |
24 | * This union can hold either a straightforward pointer to a struct device_node | |
25 | * in the live device tree, or an offset within the flat device tree. In the | |
26 | * latter case, the pointer value is just the integer offset within the flat DT. | |
27 | * | |
28 | * Thus we can reference nodes in both the live tree (once available) and the | |
29 | * flat tree (until then). Functions are available to translate between an | |
30 | * ofnode and either an offset or a struct device_node *. | |
31 | * | |
32 | * The reference can also hold a null offset, in which case the pointer value | |
9e512045 | 33 | * here is NULL. This corresponds to a struct device_node * value of |
4984de2b SG |
34 | * NULL, or an offset of -1. |
35 | * | |
36 | * There is no ambiguity as to whether ofnode holds an offset or a node | |
37 | * pointer: when the live tree is active it holds a node pointer, otherwise it | |
38 | * holds an offset. The value itself does not need to be unique and in theory | |
39 | * the same value could point to a valid device node or a valid offset. We | |
40 | * could arrange for a unique value to be used (e.g. by making the pointer | |
41 | * point to an offset within the flat device tree in the case of an offset) but | |
42 | * this increases code size slightly due to the subtraction. Since it offers no | |
43 | * real benefit, the approach described here seems best. | |
44 | * | |
45 | * For now these points use constant types, since we don't allow writing | |
46 | * the DT. | |
47 | * | |
48 | * @np: Pointer to device node, used for live tree | |
afc1a78a | 49 | * @of_offset: Pointer into flat device tree, used for flat tree. Note that this |
4984de2b SG |
50 | * is not a really a pointer to a node: it is an offset value. See above. |
51 | */ | |
52 | typedef union ofnode_union { | |
b9390ce5 | 53 | const struct device_node *np; |
4984de2b SG |
54 | long of_offset; |
55 | } ofnode; | |
56 | ||
9e512045 SG |
57 | struct ofnode_phandle_args { |
58 | ofnode node; | |
59 | int args_count; | |
60 | uint32_t args[OF_MAX_PHANDLE_ARGS]; | |
61 | }; | |
62 | ||
ce891fca PD |
63 | /** |
64 | * ofprop - reference to a property of a device tree node | |
65 | * | |
66 | * This struct hold the reference on one property of one node, | |
67 | * using struct ofnode and an offset within the flat device tree or either | |
68 | * a pointer to a struct property in the live device tree. | |
69 | * | |
70 | * Thus we can reference arguments in both the live tree and the flat tree. | |
71 | * | |
72 | * The property reference can also hold a null reference. This corresponds to | |
73 | * a struct property NULL pointer or an offset of -1. | |
74 | * | |
75 | * @node: Pointer to device node | |
76 | * @offset: Pointer into flat device tree, used for flat tree. | |
77 | * @prop: Pointer to property, used for live treee. | |
78 | */ | |
79 | ||
80 | struct ofprop { | |
81 | ofnode node; | |
82 | union { | |
83 | int offset; | |
84 | const struct property *prop; | |
85 | }; | |
86 | }; | |
87 | ||
9e512045 | 88 | /** |
45dbe75d | 89 | * ofnode_to_np() - convert an ofnode to a live DT node pointer |
9e512045 SG |
90 | * |
91 | * This cannot be called if the reference contains an offset. | |
92 | * | |
93 | * @node: Reference containing struct device_node * (possibly invalid) | |
94 | * @return pointer to device node (can be NULL) | |
95 | */ | |
96 | static inline const struct device_node *ofnode_to_np(ofnode node) | |
97 | { | |
98 | #ifdef OF_CHECKS | |
99 | if (!of_live_active()) | |
100 | return NULL; | |
101 | #endif | |
102 | return node.np; | |
103 | } | |
104 | ||
4984de2b SG |
105 | /** |
106 | * ofnode_to_offset() - convert an ofnode to a flat DT offset | |
107 | * | |
108 | * This cannot be called if the reference contains a node pointer. | |
109 | * | |
110 | * @node: Reference containing offset (possibly invalid) | |
111 | * @return DT offset (can be -1) | |
112 | */ | |
113 | static inline int ofnode_to_offset(ofnode node) | |
114 | { | |
9e512045 SG |
115 | #ifdef OF_CHECKS |
116 | if (of_live_active()) | |
117 | return -1; | |
118 | #endif | |
4984de2b SG |
119 | return node.of_offset; |
120 | } | |
121 | ||
122 | /** | |
123 | * ofnode_valid() - check if an ofnode is valid | |
124 | * | |
125 | * @return true if the reference contains a valid ofnode, false if it is NULL | |
126 | */ | |
127 | static inline bool ofnode_valid(ofnode node) | |
128 | { | |
9e512045 SG |
129 | if (of_live_active()) |
130 | return node.np != NULL; | |
131 | else | |
6d9949fe | 132 | return node.of_offset >= 0; |
4984de2b SG |
133 | } |
134 | ||
135 | /** | |
136 | * offset_to_ofnode() - convert a DT offset to an ofnode | |
137 | * | |
138 | * @of_offset: DT offset (either valid, or -1) | |
139 | * @return reference to the associated DT offset | |
140 | */ | |
141 | static inline ofnode offset_to_ofnode(int of_offset) | |
142 | { | |
143 | ofnode node; | |
144 | ||
9e512045 SG |
145 | if (of_live_active()) |
146 | node.np = NULL; | |
147 | else | |
b14c5339 | 148 | node.of_offset = of_offset >= 0 ? of_offset : -1; |
9e512045 SG |
149 | |
150 | return node; | |
151 | } | |
152 | ||
153 | /** | |
154 | * np_to_ofnode() - convert a node pointer to an ofnode | |
155 | * | |
156 | * @np: Live node pointer (can be NULL) | |
157 | * @return reference to the associated node pointer | |
158 | */ | |
159 | static inline ofnode np_to_ofnode(const struct device_node *np) | |
160 | { | |
161 | ofnode node; | |
162 | ||
163 | node.np = np; | |
4984de2b SG |
164 | |
165 | return node; | |
166 | } | |
167 | ||
9e512045 SG |
168 | /** |
169 | * ofnode_is_np() - check if a reference is a node pointer | |
170 | * | |
171 | * This function associated that if there is a valid live tree then all | |
172 | * references will use it. This is because using the flat DT when the live tree | |
173 | * is valid is not permitted. | |
174 | * | |
175 | * @node: reference to check (possibly invalid) | |
176 | * @return true if the reference is a live node pointer, false if it is a DT | |
177 | * offset | |
178 | */ | |
179 | static inline bool ofnode_is_np(ofnode node) | |
180 | { | |
181 | #ifdef OF_CHECKS | |
182 | /* | |
183 | * Check our assumption that flat tree offsets are not used when a | |
184 | * live tree is in use. | |
185 | */ | |
186 | assert(!ofnode_valid(node) || | |
45dbe75d SR |
187 | (of_live_active() ? ofnode_to_np(node) |
188 | : ofnode_to_np(node))); | |
9e512045 SG |
189 | #endif |
190 | return of_live_active() && ofnode_valid(node); | |
191 | } | |
192 | ||
4984de2b SG |
193 | /** |
194 | * ofnode_equal() - check if two references are equal | |
195 | * | |
196 | * @return true if equal, else false | |
197 | */ | |
198 | static inline bool ofnode_equal(ofnode ref1, ofnode ref2) | |
199 | { | |
200 | /* We only need to compare the contents */ | |
201 | return ref1.of_offset == ref2.of_offset; | |
202 | } | |
203 | ||
9e512045 SG |
204 | /** |
205 | * ofnode_null() - Obtain a null ofnode | |
206 | * | |
207 | * This returns an ofnode which points to no node. It works both with the flat | |
208 | * tree and livetree. | |
209 | */ | |
210 | static inline ofnode ofnode_null(void) | |
211 | { | |
212 | ofnode node; | |
213 | ||
214 | if (of_live_active()) | |
215 | node.np = NULL; | |
216 | else | |
217 | node.of_offset = -1; | |
218 | ||
219 | return node; | |
220 | } | |
221 | ||
d0c20ce6 SG |
222 | static inline ofnode ofnode_root(void) |
223 | { | |
224 | ofnode node; | |
225 | ||
226 | if (of_live_active()) | |
227 | node.np = gd_of_root(); | |
228 | else | |
229 | node.of_offset = 0; | |
230 | ||
231 | return node; | |
232 | } | |
233 | ||
9e512045 SG |
234 | /** |
235 | * ofnode_read_u32() - Read a 32-bit integer from a property | |
236 | * | |
237 | * @ref: valid node reference to read property from | |
238 | * @propname: name of the property to read from | |
239 | * @outp: place to put value (if found) | |
240 | * @return 0 if OK, -ve on error | |
241 | */ | |
242 | int ofnode_read_u32(ofnode node, const char *propname, u32 *outp); | |
243 | ||
4bb7075c DB |
244 | /** |
245 | * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property | |
246 | * | |
247 | * @ref: valid node reference to read property from | |
248 | * @propname: name of the property to read from | |
249 | * @index: index of the integer to return | |
250 | * @outp: place to put value (if found) | |
251 | * @return 0 if OK, -ve on error | |
252 | */ | |
253 | int ofnode_read_u32_index(ofnode node, const char *propname, int index, | |
254 | u32 *outp); | |
255 | ||
9e512045 SG |
256 | /** |
257 | * ofnode_read_s32() - Read a 32-bit integer from a property | |
258 | * | |
259 | * @ref: valid node reference to read property from | |
260 | * @propname: name of the property to read from | |
261 | * @outp: place to put value (if found) | |
262 | * @return 0 if OK, -ve on error | |
263 | */ | |
264 | static inline int ofnode_read_s32(ofnode node, const char *propname, | |
265 | s32 *out_value) | |
266 | { | |
267 | return ofnode_read_u32(node, propname, (u32 *)out_value); | |
268 | } | |
269 | ||
270 | /** | |
271 | * ofnode_read_u32_default() - Read a 32-bit integer from a property | |
272 | * | |
273 | * @ref: valid node reference to read property from | |
274 | * @propname: name of the property to read from | |
275 | * @def: default value to return if the property has no value | |
276 | * @return property value, or @def if not found | |
277 | */ | |
b061ef39 | 278 | u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def); |
9e512045 | 279 | |
4bb7075c DB |
280 | /** |
281 | * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value | |
282 | * property | |
283 | * | |
284 | * @ref: valid node reference to read property from | |
285 | * @propname: name of the property to read from | |
286 | * @index: index of the integer to return | |
287 | * @def: default value to return if the property has no value | |
288 | * @return property value, or @def if not found | |
289 | */ | |
290 | u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index, | |
291 | u32 def); | |
292 | ||
9e512045 SG |
293 | /** |
294 | * ofnode_read_s32_default() - Read a 32-bit integer from a property | |
295 | * | |
296 | * @ref: valid node reference to read property from | |
297 | * @propname: name of the property to read from | |
298 | * @def: default value to return if the property has no value | |
299 | * @return property value, or @def if not found | |
300 | */ | |
301 | int ofnode_read_s32_default(ofnode node, const char *propname, s32 def); | |
302 | ||
afb30129 LA |
303 | /** |
304 | * ofnode_read_u64() - Read a 64-bit integer from a property | |
305 | * | |
306 | * @node: valid node reference to read property from | |
307 | * @propname: name of the property to read from | |
308 | * @outp: place to put value (if found) | |
309 | * @return 0 if OK, -ve on error | |
310 | */ | |
311 | int ofnode_read_u64(ofnode node, const char *propname, u64 *outp); | |
312 | ||
7e5196c4 SG |
313 | /** |
314 | * ofnode_read_u64_default() - Read a 64-bit integer from a property | |
315 | * | |
316 | * @ref: valid node reference to read property from | |
317 | * @propname: name of the property to read from | |
318 | * @def: default value to return if the property has no value | |
319 | * @return property value, or @def if not found | |
320 | */ | |
3f3d7715 | 321 | u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def); |
7e5196c4 | 322 | |
a8167d8e SG |
323 | /** |
324 | * ofnode_read_prop() - Read a property from a node | |
325 | * | |
326 | * @node: valid node reference to read property from | |
327 | * @propname: name of the property to read | |
328 | * @sizep: if non-NULL, returns the size of the property, or an error code | |
329 | if not found | |
330 | * @return property value, or NULL if there is no such property | |
331 | */ | |
332 | const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep); | |
333 | ||
9e512045 SG |
334 | /** |
335 | * ofnode_read_string() - Read a string from a property | |
336 | * | |
a8167d8e | 337 | * @node: valid node reference to read property from |
9e512045 SG |
338 | * @propname: name of the property to read |
339 | * @return string from property value, or NULL if there is no such property | |
340 | */ | |
341 | const char *ofnode_read_string(ofnode node, const char *propname); | |
342 | ||
343 | /** | |
bed77496 | 344 | * ofnode_read_u32_array() - Find and read an array of 32 bit integers |
9e512045 SG |
345 | * |
346 | * @node: valid node reference to read property from | |
347 | * @propname: name of the property to read | |
348 | * @out_values: pointer to return value, modified only if return value is 0 | |
349 | * @sz: number of array elements to read | |
fbe8d033 | 350 | * @return 0 if OK, -ve on error |
9e512045 SG |
351 | * |
352 | * Search for a property in a device node and read 32-bit value(s) from | |
353 | * it. Returns 0 on success, -EINVAL if the property does not exist, | |
354 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | |
355 | * property data isn't large enough. | |
356 | * | |
357 | * The out_values is modified only if a valid u32 value can be decoded. | |
358 | */ | |
359 | int ofnode_read_u32_array(ofnode node, const char *propname, | |
360 | u32 *out_values, size_t sz); | |
361 | ||
362 | /** | |
363 | * ofnode_read_bool() - read a boolean value from a property | |
364 | * | |
365 | * @node: valid node reference to read property from | |
366 | * @propname: name of property to read | |
367 | * @return true if property is present (meaning true), false if not present | |
368 | */ | |
369 | bool ofnode_read_bool(ofnode node, const char *propname); | |
370 | ||
371 | /** | |
372 | * ofnode_find_subnode() - find a named subnode of a parent node | |
373 | * | |
374 | * @node: valid reference to parent node | |
375 | * @subnode_name: name of subnode to find | |
376 | * @return reference to subnode (which can be invalid if there is no such | |
377 | * subnode) | |
378 | */ | |
379 | ofnode ofnode_find_subnode(ofnode node, const char *subnode_name); | |
380 | ||
ec1add1e | 381 | #if CONFIG_IS_ENABLED(DM_INLINE_OFNODE) |
401d1c4f SG |
382 | #include <asm/global_data.h> |
383 | ||
ec1add1e SG |
384 | static inline bool ofnode_is_enabled(ofnode node) |
385 | { | |
386 | if (ofnode_is_np(node)) { | |
387 | return of_device_is_available(ofnode_to_np(node)); | |
388 | } else { | |
389 | return fdtdec_get_is_enabled(gd->fdt_blob, | |
390 | ofnode_to_offset(node)); | |
391 | } | |
392 | } | |
393 | ||
394 | static inline ofnode ofnode_first_subnode(ofnode node) | |
395 | { | |
396 | assert(ofnode_valid(node)); | |
397 | if (ofnode_is_np(node)) | |
398 | return np_to_ofnode(node.np->child); | |
399 | ||
400 | return offset_to_ofnode( | |
401 | fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node))); | |
402 | } | |
403 | ||
404 | static inline ofnode ofnode_next_subnode(ofnode node) | |
405 | { | |
406 | assert(ofnode_valid(node)); | |
407 | if (ofnode_is_np(node)) | |
408 | return np_to_ofnode(node.np->sibling); | |
409 | ||
410 | return offset_to_ofnode( | |
411 | fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node))); | |
412 | } | |
413 | #else | |
414 | /** | |
415 | * ofnode_is_enabled() - Checks whether a node is enabled. | |
416 | * This looks for a 'status' property. If this exists, then returns true if | |
417 | * the status is 'okay' and false otherwise. If there is no status property, | |
418 | * it returns true on the assumption that anything mentioned should be enabled | |
419 | * by default. | |
420 | * | |
421 | * @node: node to examine | |
422 | * @return false (not enabled) or true (enabled) | |
423 | */ | |
424 | bool ofnode_is_enabled(ofnode node); | |
425 | ||
9e512045 SG |
426 | /** |
427 | * ofnode_first_subnode() - find the first subnode of a parent node | |
428 | * | |
429 | * @node: valid reference to a valid parent node | |
430 | * @return reference to the first subnode (which can be invalid if the parent | |
431 | * node has no subnodes) | |
432 | */ | |
433 | ofnode ofnode_first_subnode(ofnode node); | |
434 | ||
435 | /** | |
436 | * ofnode_next_subnode() - find the next sibling of a subnode | |
437 | * | |
438 | * @node: valid reference to previous node (sibling) | |
439 | * @return reference to the next subnode (which can be invalid if the node | |
440 | * has no more siblings) | |
441 | */ | |
442 | ofnode ofnode_next_subnode(ofnode node); | |
ec1add1e | 443 | #endif /* DM_INLINE_OFNODE */ |
9e512045 | 444 | |
e2d5997f PT |
445 | /** |
446 | * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode) | |
447 | * | |
448 | * @node: valid node to look up | |
449 | * @return ofnode reference of the parent node | |
450 | */ | |
451 | ofnode ofnode_get_parent(ofnode node); | |
452 | ||
9e512045 SG |
453 | /** |
454 | * ofnode_get_name() - get the name of a node | |
455 | * | |
456 | * @node: valid node to look up | |
33810b4e | 457 | * @return name of node |
9e512045 SG |
458 | */ |
459 | const char *ofnode_get_name(ofnode node); | |
460 | ||
b4f20767 KY |
461 | /** |
462 | * ofnode_get_by_phandle() - get ofnode from phandle | |
463 | * | |
464 | * @phandle: phandle to look up | |
465 | * @return ofnode reference to the phandle | |
466 | */ | |
467 | ofnode ofnode_get_by_phandle(uint phandle); | |
468 | ||
9e512045 SG |
469 | /** |
470 | * ofnode_read_size() - read the size of a property | |
471 | * | |
472 | * @node: node to check | |
473 | * @propname: property to check | |
474 | * @return size of property if present, or -EINVAL if not | |
475 | */ | |
476 | int ofnode_read_size(ofnode node, const char *propname); | |
477 | ||
e679d03b K |
478 | /** |
479 | * ofnode_get_addr_size_index() - get an address/size from a node | |
480 | * based on index | |
481 | * | |
482 | * This reads the register address/size from a node based on index | |
483 | * | |
484 | * @node: node to read from | |
485 | * @index: Index of address to read (0 for first) | |
486 | * @size: Pointer to size of the address | |
487 | * @return address, or FDT_ADDR_T_NONE if not present or invalid | |
488 | */ | |
489 | phys_addr_t ofnode_get_addr_size_index(ofnode node, int index, | |
490 | fdt_size_t *size); | |
491 | ||
bed77496 SG |
492 | /** |
493 | * ofnode_get_addr_index() - get an address from a node | |
494 | * | |
495 | * This reads the register address from a node | |
496 | * | |
497 | * @node: node to read from | |
498 | * @index: Index of address to read (0 for first) | |
499 | * @return address, or FDT_ADDR_T_NONE if not present or invalid | |
500 | */ | |
501 | phys_addr_t ofnode_get_addr_index(ofnode node, int index); | |
502 | ||
503 | /** | |
504 | * ofnode_get_addr() - get an address from a node | |
505 | * | |
506 | * This reads the register address from a node | |
507 | * | |
508 | * @node: node to read from | |
509 | * @return address, or FDT_ADDR_T_NONE if not present or invalid | |
510 | */ | |
511 | phys_addr_t ofnode_get_addr(ofnode node); | |
512 | ||
9e512045 SG |
513 | /** |
514 | * ofnode_stringlist_search() - find a string in a string list and return index | |
515 | * | |
516 | * Note that it is possible for this function to succeed on property values | |
517 | * that are not NUL-terminated. That's because the function will stop after | |
518 | * finding the first occurrence of @string. This can for example happen with | |
519 | * small-valued cell properties, such as #address-cells, when searching for | |
520 | * the empty string. | |
521 | * | |
522 | * @node: node to check | |
523 | * @propname: name of the property containing the string list | |
524 | * @string: string to look up in the string list | |
525 | * | |
526 | * @return: | |
527 | * the index of the string in the list of strings | |
528 | * -ENODATA if the property is not found | |
529 | * -EINVAL on some other error | |
530 | */ | |
531 | int ofnode_stringlist_search(ofnode node, const char *propname, | |
532 | const char *string); | |
533 | ||
534 | /** | |
8c293d6a | 535 | * ofnode_read_string_index() - obtain an indexed string from a string list |
9e512045 SG |
536 | * |
537 | * Note that this will successfully extract strings from properties with | |
538 | * non-NUL-terminated values. For example on small-valued cell properties | |
539 | * this function will return the empty string. | |
540 | * | |
541 | * If non-NULL, the length of the string (on success) or a negative error-code | |
542 | * (on failure) will be stored in the integer pointer to by lenp. | |
543 | * | |
544 | * @node: node to check | |
545 | * @propname: name of the property containing the string list | |
546 | * @index: index of the string to return | |
547 | * @lenp: return location for the string length or an error code on failure | |
548 | * | |
549 | * @return: | |
550 | * length of string, if found or -ve error value if not found | |
551 | */ | |
552 | int ofnode_read_string_index(ofnode node, const char *propname, int index, | |
553 | const char **outp); | |
554 | ||
8c293d6a SG |
555 | /** |
556 | * ofnode_read_string_count() - find the number of strings in a string list | |
557 | * | |
558 | * @node: node to check | |
559 | * @propname: name of the property containing the string list | |
560 | * @return: | |
561 | * number of strings in the list, or -ve error value if not found | |
562 | */ | |
563 | int ofnode_read_string_count(ofnode node, const char *property); | |
564 | ||
9e512045 SG |
565 | /** |
566 | * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list | |
567 | * | |
568 | * This function is useful to parse lists of phandles and their arguments. | |
569 | * Returns 0 on success and fills out_args, on error returns appropriate | |
570 | * errno value. | |
571 | * | |
572 | * Caller is responsible to call of_node_put() on the returned out_args->np | |
573 | * pointer. | |
574 | * | |
575 | * Example: | |
576 | * | |
577 | * phandle1: node1 { | |
578 | * #list-cells = <2>; | |
579 | * } | |
580 | * | |
581 | * phandle2: node2 { | |
582 | * #list-cells = <1>; | |
583 | * } | |
584 | * | |
585 | * node3 { | |
586 | * list = <&phandle1 1 2 &phandle2 3>; | |
587 | * } | |
588 | * | |
589 | * To get a device_node of the `node2' node you may call this: | |
590 | * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args); | |
591 | * | |
592 | * @node: device tree node containing a list | |
593 | * @list_name: property name that contains a list | |
594 | * @cells_name: property name that specifies phandles' arguments count | |
595 | * @cells_count: Cell count to use if @cells_name is NULL | |
596 | * @index: index of a phandle to parse out | |
597 | * @out_args: optional pointer to output arguments structure (will be filled) | |
598 | * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if | |
599 | * @list_name does not exist, -EINVAL if a phandle was not found, | |
600 | * @cells_name could not be found, the arguments were truncated or there | |
601 | * were too many arguments. | |
602 | */ | |
603 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, | |
604 | const char *cells_name, int cell_count, | |
605 | int index, | |
606 | struct ofnode_phandle_args *out_args); | |
607 | ||
642346ae PC |
608 | /** |
609 | * ofnode_count_phandle_with_args() - Count number of phandle in a list | |
610 | * | |
611 | * This function is useful to count phandles into a list. | |
612 | * Returns number of phandle on success, on error returns appropriate | |
613 | * errno value. | |
614 | * | |
615 | * @node: device tree node containing a list | |
616 | * @list_name: property name that contains a list | |
617 | * @cells_name: property name that specifies phandles' arguments count | |
89f68302 | 618 | * @cells_count: Cell count to use if @cells_name is NULL |
642346ae PC |
619 | * @return number of phandle on success, -ENOENT if @list_name does not |
620 | * exist, -EINVAL if a phandle was not found, @cells_name could not | |
621 | * be found. | |
622 | */ | |
623 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, | |
89f68302 | 624 | const char *cells_name, int cell_count); |
642346ae | 625 | |
9e512045 SG |
626 | /** |
627 | * ofnode_path() - find a node by full path | |
628 | * | |
629 | * @path: Full path to node, e.g. "/bus/spi@1" | |
630 | * @return reference to the node found. Use ofnode_valid() to check if it exists | |
631 | */ | |
632 | ofnode ofnode_path(const char *path); | |
633 | ||
bd933bfd SG |
634 | /** |
635 | * ofnode_read_chosen_prop() - get the value of a chosen property | |
636 | * | |
637 | * This looks for a property within the /chosen node and returns its value | |
638 | * | |
639 | * @propname: Property name to look for | |
640 | * @sizep: Returns size of property, or FDT_ERR_... error code if function | |
641 | * returns NULL | |
642 | * @return property value if found, else NULL | |
643 | */ | |
644 | const void *ofnode_read_chosen_prop(const char *propname, int *sizep); | |
645 | ||
9e512045 | 646 | /** |
14ca9f7f | 647 | * ofnode_read_chosen_string() - get the string value of a chosen property |
9e512045 | 648 | * |
14ca9f7f SG |
649 | * This looks for a property within the /chosen node and returns its value, |
650 | * checking that it is a valid nul-terminated string | |
9e512045 SG |
651 | * |
652 | * @propname: Property name to look for | |
14ca9f7f | 653 | * @return string value if found, else NULL |
9e512045 | 654 | */ |
14ca9f7f | 655 | const char *ofnode_read_chosen_string(const char *propname); |
9e512045 SG |
656 | |
657 | /** | |
74d594a2 | 658 | * ofnode_get_chosen_node() - get a referenced node from the chosen node |
9e512045 | 659 | * |
74d594a2 SG |
660 | * This looks up a named property in the chosen node and uses that as a path to |
661 | * look up a code. | |
662 | * | |
663 | * @return the referenced node if present, else ofnode_null() | |
9e512045 | 664 | */ |
74d594a2 | 665 | ofnode ofnode_get_chosen_node(const char *propname); |
9e512045 | 666 | |
305d3188 MS |
667 | /** |
668 | * ofnode_read_aliases_prop() - get the value of a aliases property | |
669 | * | |
670 | * This looks for a property within the /aliases node and returns its value | |
671 | * | |
672 | * @propname: Property name to look for | |
673 | * @sizep: Returns size of property, or FDT_ERR_... error code if function | |
674 | * returns NULL | |
675 | * @return property value if found, else NULL | |
676 | */ | |
677 | const void *ofnode_read_aliases_prop(const char *propname, int *sizep); | |
678 | ||
679 | /** | |
680 | * ofnode_get_aliases_node() - get a referenced node from the aliases node | |
681 | * | |
682 | * This looks up a named property in the aliases node and uses that as a path to | |
683 | * look up a code. | |
684 | * | |
685 | * @return the referenced node if present, else ofnode_null() | |
686 | */ | |
687 | ofnode ofnode_get_aliases_node(const char *propname); | |
688 | ||
9e512045 SG |
689 | struct display_timing; |
690 | /** | |
691 | * ofnode_decode_display_timing() - decode display timings | |
692 | * | |
693 | * Decode display timings from the supplied 'display-timings' node. | |
694 | * See doc/device-tree-bindings/video/display-timing.txt for binding | |
695 | * information. | |
696 | * | |
697 | * @node 'display-timing' node containing the timing subnodes | |
698 | * @index Index number to read (0=first timing subnode) | |
699 | * @config Place to put timings | |
700 | * @return 0 if OK, -FDT_ERR_NOTFOUND if not found | |
701 | */ | |
702 | int ofnode_decode_display_timing(ofnode node, int index, | |
703 | struct display_timing *config); | |
704 | ||
705 | /** | |
ce891fca | 706 | * ofnode_get_property() - get a pointer to the value of a node property |
9e512045 SG |
707 | * |
708 | * @node: node to read | |
709 | * @propname: property to read | |
710 | * @lenp: place to put length on success | |
711 | * @return pointer to property, or NULL if not found | |
712 | */ | |
61e51bab | 713 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp); |
9e512045 | 714 | |
ce891fca PD |
715 | /** |
716 | * ofnode_get_first_property()- get the reference of the first property | |
717 | * | |
718 | * Get reference to the first property of the node, it is used to iterate | |
719 | * and read all the property with ofnode_get_property_by_prop(). | |
720 | * | |
721 | * @node: node to read | |
722 | * @prop: place to put argument reference | |
723 | * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found | |
724 | */ | |
725 | int ofnode_get_first_property(ofnode node, struct ofprop *prop); | |
726 | ||
727 | /** | |
728 | * ofnode_get_next_property() - get the reference of the next property | |
729 | * | |
730 | * Get reference to the next property of the node, it is used to iterate | |
731 | * and read all the property with ofnode_get_property_by_prop(). | |
732 | * | |
733 | * @prop: reference of current argument and place to put reference of next one | |
734 | * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found | |
735 | */ | |
736 | int ofnode_get_next_property(struct ofprop *prop); | |
737 | ||
738 | /** | |
739 | * ofnode_get_property_by_prop() - get a pointer to the value of a property | |
740 | * | |
741 | * Get value for the property identified by the provided reference. | |
742 | * | |
743 | * @prop: reference on property | |
744 | * @propname: If non-NULL, place to property name on success, | |
745 | * @lenp: If non-NULL, place to put length on success | |
746 | * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found | |
747 | */ | |
748 | const void *ofnode_get_property_by_prop(const struct ofprop *prop, | |
749 | const char **propname, int *lenp); | |
750 | ||
9e512045 SG |
751 | /** |
752 | * ofnode_is_available() - check if a node is marked available | |
753 | * | |
754 | * @node: node to check | |
755 | * @return true if node's 'status' property is "okay" (or is missing) | |
756 | */ | |
757 | bool ofnode_is_available(ofnode node); | |
758 | ||
759 | /** | |
760 | * ofnode_get_addr_size() - get address and size from a property | |
761 | * | |
762 | * This does no address translation. It simply reads an property that contains | |
763 | * an address and a size value, one after the other. | |
764 | * | |
765 | * @node: node to read from | |
766 | * @propname: property to read | |
767 | * @sizep: place to put size value (on success) | |
768 | * @return address value, or FDT_ADDR_T_NONE on error | |
769 | */ | |
770 | phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname, | |
771 | phys_size_t *sizep); | |
772 | ||
773 | /** | |
774 | * ofnode_read_u8_array_ptr() - find an 8-bit array | |
775 | * | |
776 | * Look up a property in a node and return a pointer to its contents as a | |
777 | * byte array of given length. The property must have at least enough data | |
778 | * for the array (count bytes). It may have more, but this will be ignored. | |
779 | * The data is not copied. | |
780 | * | |
781 | * @node node to examine | |
782 | * @propname name of property to find | |
783 | * @sz number of array elements | |
784 | * @return pointer to byte array if found, or NULL if the property is not | |
785 | * found or there is not enough data | |
786 | */ | |
787 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, | |
788 | size_t sz); | |
789 | ||
790 | /** | |
791 | * ofnode_read_pci_addr() - look up a PCI address | |
792 | * | |
793 | * Look at an address property in a node and return the PCI address which | |
794 | * corresponds to the given type in the form of fdt_pci_addr. | |
795 | * The property must hold one fdt_pci_addr with a lengh. | |
796 | * | |
797 | * @node node to examine | |
798 | * @type pci address type (FDT_PCI_SPACE_xxx) | |
799 | * @propname name of property to find | |
800 | * @addr returns pci address in the form of fdt_pci_addr | |
801 | * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the | |
802 | * format of the property was invalid, -ENXIO if the requested | |
803 | * address type was not found | |
804 | */ | |
805 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, | |
806 | const char *propname, struct fdt_pci_addr *addr); | |
807 | ||
7b9cbadd BM |
808 | /** |
809 | * ofnode_read_pci_vendev() - look up PCI vendor and device id | |
810 | * | |
811 | * Look at the compatible property of a device node that represents a PCI | |
812 | * device and extract pci vendor id and device id from it. | |
813 | * | |
814 | * @param node node to examine | |
815 | * @param vendor vendor id of the pci device | |
816 | * @param device device id of the pci device | |
817 | * @return 0 if ok, negative on error | |
818 | */ | |
819 | int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device); | |
820 | ||
9e512045 SG |
821 | /** |
822 | * ofnode_read_addr_cells() - Get the number of address cells for a node | |
823 | * | |
824 | * This walks back up the tree to find the closest #address-cells property | |
825 | * which controls the given node. | |
826 | * | |
827 | * @node: Node to check | |
828 | * @return number of address cells this node uses | |
829 | */ | |
830 | int ofnode_read_addr_cells(ofnode node); | |
831 | ||
832 | /** | |
833 | * ofnode_read_size_cells() - Get the number of size cells for a node | |
834 | * | |
835 | * This walks back up the tree to find the closest #size-cells property | |
836 | * which controls the given node. | |
837 | * | |
838 | * @node: Node to check | |
839 | * @return number of size cells this node uses | |
840 | */ | |
841 | int ofnode_read_size_cells(ofnode node); | |
842 | ||
878d68c0 SG |
843 | /** |
844 | * ofnode_read_simple_addr_cells() - Get the address cells property in a node | |
845 | * | |
846 | * This function matches fdt_address_cells(). | |
847 | * | |
848 | * @np: Node pointer to check | |
849 | * @return value of #address-cells property in this node, or 2 if none | |
850 | */ | |
851 | int ofnode_read_simple_addr_cells(ofnode node); | |
852 | ||
853 | /** | |
854 | * ofnode_read_simple_size_cells() - Get the size cells property in a node | |
855 | * | |
856 | * This function matches fdt_size_cells(). | |
857 | * | |
858 | * @np: Node pointer to check | |
859 | * @return value of #size-cells property in this node, or 2 if none | |
860 | */ | |
861 | int ofnode_read_simple_size_cells(ofnode node); | |
862 | ||
9e512045 SG |
863 | /** |
864 | * ofnode_pre_reloc() - check if a node should be bound before relocation | |
865 | * | |
866 | * Device tree nodes can be marked as needing-to-be-bound in the loader stages | |
867 | * via special device tree properties. | |
868 | * | |
869 | * Before relocation this function can be used to check if nodes are required | |
870 | * in either SPL or TPL stages. | |
871 | * | |
872 | * After relocation and jumping into the real U-Boot binary it is possible to | |
873 | * determine if a node was bound in one of SPL/TPL stages. | |
874 | * | |
54e1223a PD |
875 | * There are 4 settings currently in use |
876 | * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only | |
9e512045 SG |
877 | * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL |
878 | * Existing platforms only use it to indicate nodes needed in | |
879 | * SPL. Should probably be replaced by u-boot,dm-spl for | |
880 | * new platforms. | |
54e1223a PD |
881 | * - u-boot,dm-spl: SPL and U-Boot pre-relocation |
882 | * - u-boot,dm-tpl: TPL and U-Boot pre-relocation | |
9e512045 SG |
883 | * |
884 | * @node: node to check | |
fbe8d033 | 885 | * @return true if node is needed in SPL/TL, false otherwise |
9e512045 SG |
886 | */ |
887 | bool ofnode_pre_reloc(ofnode node); | |
888 | ||
c98ad443 SG |
889 | /** |
890 | * ofnode_read_resource() - Read a resource from a node | |
891 | * | |
892 | * Read resource information from a node at the given index | |
893 | * | |
894 | * @node: Node to read from | |
895 | * @index: Index of resource to read (0 = first) | |
896 | * @res: Returns resource that was read, on success | |
897 | * @return 0 if OK, -ve on error | |
898 | */ | |
dcf98852 | 899 | int ofnode_read_resource(ofnode node, uint index, struct resource *res); |
c98ad443 SG |
900 | |
901 | /** | |
902 | * ofnode_read_resource_byname() - Read a resource from a node by name | |
903 | * | |
904 | * Read resource information from a node matching the given name. This uses a | |
905 | * 'reg-names' string list property with the names matching the associated | |
906 | * 'reg' property list. | |
907 | * | |
908 | * @node: Node to read from | |
909 | * @name: Name of resource to read | |
910 | * @res: Returns resource that was read, on success | |
911 | * @return 0 if OK, -ve on error | |
912 | */ | |
7b8b47bd MY |
913 | int ofnode_read_resource_byname(ofnode node, const char *name, |
914 | struct resource *res); | |
dcf98852 | 915 | |
c60f671b SG |
916 | /** |
917 | * ofnode_by_compatible() - Find the next compatible node | |
918 | * | |
919 | * Find the next node after @from that is compatible with @compat | |
920 | * | |
921 | * @from: ofnode to start from (use ofnode_null() to start at the beginning) | |
922 | * @compat: Compatible string to match | |
923 | * @return ofnode found, or ofnode_null() if none | |
924 | */ | |
925 | ofnode ofnode_by_compatible(ofnode from, const char *compat); | |
926 | ||
61fba0fa JW |
927 | /** |
928 | * ofnode_by_prop_value() - Find the next node with given property value | |
929 | * | |
930 | * Find the next node after @from that has a @propname with a value | |
931 | * @propval and a length @proplen. | |
932 | * | |
933 | * @from: ofnode to start from (use ofnode_null() to start at the | |
934 | * beginning) @propname: property name to check @propval: property value to | |
935 | * search for @proplen: length of the value in propval @return ofnode | |
936 | * found, or ofnode_null() if none | |
937 | */ | |
938 | ofnode ofnode_by_prop_value(ofnode from, const char *propname, | |
939 | const void *propval, int proplen); | |
940 | ||
3991f42e SG |
941 | /** |
942 | * ofnode_for_each_subnode() - iterate over all subnodes of a parent | |
943 | * | |
944 | * @node: child node (ofnode, lvalue) | |
945 | * @parent: parent node (ofnode) | |
946 | * | |
947 | * This is a wrapper around a for loop and is used like so: | |
948 | * | |
949 | * ofnode node; | |
950 | * | |
951 | * ofnode_for_each_subnode(node, parent) { | |
952 | * Use node | |
953 | * ... | |
954 | * } | |
955 | * | |
956 | * Note that this is implemented as a macro and @node is used as | |
957 | * iterator in the loop. The parent variable can be a constant or even a | |
958 | * literal. | |
959 | */ | |
960 | #define ofnode_for_each_subnode(node, parent) \ | |
961 | for (node = ofnode_first_subnode(parent); \ | |
962 | ofnode_valid(node); \ | |
963 | node = ofnode_next_subnode(node)) | |
964 | ||
89b84b85 CY |
965 | /** |
966 | * ofnode_get_child_count() - get the child count of a ofnode | |
967 | * | |
968 | * @node: valid node to get its child count | |
969 | * @return the number of subnodes | |
970 | */ | |
971 | int ofnode_get_child_count(ofnode parent); | |
972 | ||
147c6074 | 973 | /** |
641067fb | 974 | * ofnode_translate_address() - Translate a device-tree address |
147c6074 MS |
975 | * |
976 | * Translate an address from the device-tree into a CPU physical address. This | |
977 | * function walks up the tree and applies the various bus mappings along the | |
978 | * way. | |
979 | * | |
980 | * @ofnode: Device tree node giving the context in which to translate the | |
981 | * address | |
982 | * @in_addr: pointer to the address to translate | |
983 | * @return the translated address; OF_BAD_ADDR on error | |
984 | */ | |
985 | u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr); | |
5ccc2c21 | 986 | |
641067fb FD |
987 | /** |
988 | * ofnode_translate_dma_address() - Translate a device-tree DMA address | |
989 | * | |
990 | * Translate a DMA address from the device-tree into a CPU physical address. | |
991 | * This function walks up the tree and applies the various bus mappings along | |
992 | * the way. | |
993 | * | |
994 | * @ofnode: Device tree node giving the context in which to translate the | |
995 | * DMA address | |
996 | * @in_addr: pointer to the DMA address to translate | |
997 | * @return the translated DMA address; OF_BAD_ADDR on error | |
998 | */ | |
999 | u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr); | |
1000 | ||
5ccc2c21 MY |
1001 | /** |
1002 | * ofnode_device_is_compatible() - check if the node is compatible with compat | |
1003 | * | |
1004 | * This allows to check whether the node is comaptible with the compat. | |
1005 | * | |
1006 | * @node: Device tree node for which compatible needs to be verified. | |
1007 | * @compat: Compatible string which needs to verified in the given node. | |
1008 | * @return true if OK, false if the compatible is not found | |
1009 | */ | |
1010 | int ofnode_device_is_compatible(ofnode node, const char *compat); | |
e369e58d MS |
1011 | |
1012 | /** | |
1013 | * ofnode_write_prop() - Set a property of a ofnode | |
1014 | * | |
1015 | * Note that the value passed to the function is *not* allocated by the | |
1016 | * function itself, but must be allocated by the caller if necessary. | |
1017 | * | |
1018 | * @node: The node for whose property should be set | |
1019 | * @propname: The name of the property to set | |
1020 | * @len: The length of the new value of the property | |
1021 | * @value: The new value of the property (must be valid prior to calling | |
1022 | * the function) | |
1023 | * @return 0 if successful, -ve on error | |
1024 | */ | |
1025 | int ofnode_write_prop(ofnode node, const char *propname, int len, | |
1026 | const void *value); | |
1027 | ||
1028 | /** | |
1029 | * ofnode_write_string() - Set a string property of a ofnode | |
1030 | * | |
1031 | * Note that the value passed to the function is *not* allocated by the | |
1032 | * function itself, but must be allocated by the caller if necessary. | |
1033 | * | |
1034 | * @node: The node for whose string property should be set | |
1035 | * @propname: The name of the string property to set | |
1036 | * @value: The new value of the string property (must be valid prior to | |
1037 | * calling the function) | |
1038 | * @return 0 if successful, -ve on error | |
1039 | */ | |
1040 | int ofnode_write_string(ofnode node, const char *propname, const char *value); | |
1041 | ||
1042 | /** | |
1043 | * ofnode_set_enabled() - Enable or disable a device tree node given by its | |
1044 | * ofnode | |
1045 | * | |
1046 | * This function effectively sets the node's "status" property to either "okay" | |
1047 | * or "disable", hence making it available for driver model initialization or | |
1048 | * not. | |
1049 | * | |
1050 | * @node: The node to enable | |
1051 | * @value: Flag that tells the function to either disable or enable the | |
1052 | * node | |
1053 | * @return 0 if successful, -ve on error | |
1054 | */ | |
1055 | int ofnode_set_enabled(ofnode node, bool value); | |
1056 | ||
4984de2b | 1057 | #endif |