blob: 3da05d8b2178e2b47dd382d478ce0eae1c0dbf29 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass9a148602017-05-17 17:18:10 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass9a148602017-05-17 17:18:10 -06005 */
6
7#ifndef _DM_OFNODE_H
8#define _DM_OFNODE_H
9
Simon Glassc4fc5622017-05-18 20:08:58 -060010/* TODO(sjg@chromium.org): Drop fdtdec.h include */
11#include <fdtdec.h>
12#include <dm/of.h>
Simon Glass39f1d282020-12-16 17:25:06 -070013#include <dm/of_access.h>
Stefan Roesec14df8b2020-09-23 08:23:27 +020014#include <log.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060015
16/* Enable checks to protect against invalid calls */
17#undef OF_CHECKS
18
Simon Glassf7bfcc42017-07-25 08:29:55 -060019struct resource;
20
Simon Glass9a148602017-05-17 17:18:10 -060021/**
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
Simon Glassc4fc5622017-05-18 20:08:58 -060033 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass9a148602017-05-17 17:18:10 -060034 * 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
Baruch Siach6bcca142017-11-09 13:44:28 +020049 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
Simon Glass9a148602017-05-17 17:18:10 -060050 * is not a really a pointer to a node: it is an offset value. See above.
51 */
52typedef union ofnode_union {
Heinrich Schuchardtb571d922020-07-24 18:39:43 +020053 const struct device_node *np;
Simon Glass9a148602017-05-17 17:18:10 -060054 long of_offset;
55} ofnode;
56
Simon Glassc4fc5622017-05-18 20:08:58 -060057struct ofnode_phandle_args {
58 ofnode node;
59 int args_count;
60 uint32_t args[OF_MAX_PHANDLE_ARGS];
61};
62
63/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +010064 * 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
80struct ofprop {
81 ofnode node;
82 union {
83 int offset;
84 const struct property *prop;
85 };
86};
87
88/**
Stefan Roesec14df8b2020-09-23 08:23:27 +020089 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -060090 *
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 */
96static 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
Simon Glass9a148602017-05-17 17:18:10 -0600105/**
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 */
113static inline int ofnode_to_offset(ofnode node)
114{
Simon Glassc4fc5622017-05-18 20:08:58 -0600115#ifdef OF_CHECKS
116 if (of_live_active())
117 return -1;
118#endif
Simon Glass9a148602017-05-17 17:18:10 -0600119 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 */
127static inline bool ofnode_valid(ofnode node)
128{
Simon Glassc4fc5622017-05-18 20:08:58 -0600129 if (of_live_active())
130 return node.np != NULL;
131 else
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200132 return node.of_offset >= 0;
Simon Glass9a148602017-05-17 17:18:10 -0600133}
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 */
141static inline ofnode offset_to_ofnode(int of_offset)
142{
143 ofnode node;
144
Simon Glassc4fc5622017-05-18 20:08:58 -0600145 if (of_live_active())
146 node.np = NULL;
147 else
Simon Glass1ab8b892019-12-06 21:41:36 -0700148 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass9a148602017-05-17 17:18:10 -0600149
150 return node;
151}
152
153/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600154 * 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 */
159static inline ofnode np_to_ofnode(const struct device_node *np)
160{
161 ofnode node;
162
163 node.np = np;
164
165 return node;
166}
167
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 */
179static 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) ||
Stefan Roesec14df8b2020-09-23 08:23:27 +0200187 (of_live_active() ? ofnode_to_np(node)
188 : ofnode_to_np(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600189#endif
190 return of_live_active() && ofnode_valid(node);
191}
192
193/**
Simon Glass9a148602017-05-17 17:18:10 -0600194 * ofnode_equal() - check if two references are equal
195 *
196 * @return true if equal, else false
197 */
198static 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
Simon Glassc4fc5622017-05-18 20:08:58 -0600204/**
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 */
210static 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
Simon Glass278ddba2020-11-28 17:50:07 -0700222static 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
Simon Glassc4fc5622017-05-18 20:08:58 -0600234/**
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 */
242int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
243
244/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200245 * 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 */
253int ofnode_read_u32_index(ofnode node, const char *propname, int index,
254 u32 *outp);
255
256/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600257 * 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 */
264static 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 */
Trent Piepho5b775412019-05-10 17:48:20 +0000278u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600279
280/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200281 * 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 */
290u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
291 u32 def);
292
293/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600294 * 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 */
301int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
302
303/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100304 * 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 */
311int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
312
313/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600314 * 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 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200321u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600322
323/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700324 * 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 */
332const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
333
334/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600335 * ofnode_read_string() - Read a string from a property
336 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700337 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600338 * @propname: name of the property to read
339 * @return string from property value, or NULL if there is no such property
340 */
341const char *ofnode_read_string(ofnode node, const char *propname);
342
343/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600344 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600345 *
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
Simon Glass1c682342018-06-11 13:07:11 -0600350 * @return 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600351 *
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 */
359int 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 */
369bool 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 */
379ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
380
Simon Glass39f1d282020-12-16 17:25:06 -0700381#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass3ba929a2020-10-30 21:38:53 -0600382#include <asm/global_data.h>
383
Simon Glass39f1d282020-12-16 17:25:06 -0700384static 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
394static 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
404static 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 */
424bool ofnode_is_enabled(ofnode node);
425
Simon Glassc4fc5622017-05-18 20:08:58 -0600426/**
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 */
433ofnode 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 */
442ofnode ofnode_next_subnode(ofnode node);
Simon Glass39f1d282020-12-16 17:25:06 -0700443#endif /* DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600444
445/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100446 * 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 */
451ofnode ofnode_get_parent(ofnode node);
452
453/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600454 * ofnode_get_name() - get the name of a node
455 *
456 * @node: valid node to look up
Baruch Siachf14cb212018-11-18 14:39:20 +0200457 * @return name of node
Simon Glassc4fc5622017-05-18 20:08:58 -0600458 */
459const char *ofnode_get_name(ofnode node);
460
461/**
Marek Behúne897e3c2021-05-26 14:08:18 +0200462 * ofnode_get_path() - get the full path of a node
463 *
464 * @node: valid node to look up
465 * @buf: buffer to write the node path into
466 * @buflen: buffer size
467 * @return 0 if OK, -ve on error
468 */
469int ofnode_get_path(ofnode node, char *buf, int buflen);
470
471/**
Kever Yang37df0e02018-02-23 17:38:50 +0100472 * ofnode_get_by_phandle() - get ofnode from phandle
473 *
474 * @phandle: phandle to look up
475 * @return ofnode reference to the phandle
476 */
477ofnode ofnode_get_by_phandle(uint phandle);
478
479/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600480 * ofnode_read_size() - read the size of a property
481 *
482 * @node: node to check
483 * @propname: property to check
484 * @return size of property if present, or -EINVAL if not
485 */
486int ofnode_read_size(ofnode node, const char *propname);
487
488/**
Keerthyd332e6e2019-04-24 17:19:53 +0530489 * ofnode_get_addr_size_index() - get an address/size from a node
490 * based on index
491 *
492 * This reads the register address/size from a node based on index
493 *
494 * @node: node to read from
495 * @index: Index of address to read (0 for first)
496 * @size: Pointer to size of the address
497 * @return address, or FDT_ADDR_T_NONE if not present or invalid
498 */
499phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
500 fdt_size_t *size);
501
502/**
Marek Behún177ab7f2021-05-26 14:08:17 +0200503 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
504 * based on index, without address
505 * translation
506 *
507 * This reads the register address/size from a node based on index.
508 * The resulting address is not translated. Useful for example for on-disk
509 * addresses.
510 *
511 * @node: node to read from
512 * @index: Index of address to read (0 for first)
513 * @size: Pointer to size of the address
514 * @return address, or FDT_ADDR_T_NONE if not present or invalid
515 */
516phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
517 fdt_size_t *size);
518
519/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600520 * ofnode_get_addr_index() - get an address from a node
521 *
522 * This reads the register address from a node
523 *
524 * @node: node to read from
525 * @index: Index of address to read (0 for first)
526 * @return address, or FDT_ADDR_T_NONE if not present or invalid
527 */
528phys_addr_t ofnode_get_addr_index(ofnode node, int index);
529
530/**
531 * ofnode_get_addr() - get an address from a node
532 *
533 * This reads the register address from a node
534 *
535 * @node: node to read from
536 * @return address, or FDT_ADDR_T_NONE if not present or invalid
537 */
538phys_addr_t ofnode_get_addr(ofnode node);
539
540/**
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800541 * ofnode_get_size() - get size from a node
542 *
543 * This reads the register size from a node
544 *
545 * @node: node to read from
546 * @return size of the address, or FDT_SIZE_T_NONE if not present or invalid
547 */
548fdt_size_t ofnode_get_size(ofnode node);
549
550/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600551 * ofnode_stringlist_search() - find a string in a string list and return index
552 *
553 * Note that it is possible for this function to succeed on property values
554 * that are not NUL-terminated. That's because the function will stop after
555 * finding the first occurrence of @string. This can for example happen with
556 * small-valued cell properties, such as #address-cells, when searching for
557 * the empty string.
558 *
559 * @node: node to check
560 * @propname: name of the property containing the string list
561 * @string: string to look up in the string list
562 *
563 * @return:
564 * the index of the string in the list of strings
565 * -ENODATA if the property is not found
566 * -EINVAL on some other error
567 */
568int ofnode_stringlist_search(ofnode node, const char *propname,
569 const char *string);
570
571/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600572 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600573 *
574 * Note that this will successfully extract strings from properties with
575 * non-NUL-terminated values. For example on small-valued cell properties
576 * this function will return the empty string.
577 *
578 * If non-NULL, the length of the string (on success) or a negative error-code
579 * (on failure) will be stored in the integer pointer to by lenp.
580 *
581 * @node: node to check
582 * @propname: name of the property containing the string list
583 * @index: index of the string to return
584 * @lenp: return location for the string length or an error code on failure
585 *
586 * @return:
587 * length of string, if found or -ve error value if not found
588 */
589int ofnode_read_string_index(ofnode node, const char *propname, int index,
590 const char **outp);
591
592/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600593 * ofnode_read_string_count() - find the number of strings in a string list
594 *
595 * @node: node to check
596 * @propname: name of the property containing the string list
597 * @return:
598 * number of strings in the list, or -ve error value if not found
599 */
600int ofnode_read_string_count(ofnode node, const char *property);
601
602/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600603 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
604 *
605 * This function is useful to parse lists of phandles and their arguments.
606 * Returns 0 on success and fills out_args, on error returns appropriate
607 * errno value.
608 *
609 * Caller is responsible to call of_node_put() on the returned out_args->np
610 * pointer.
611 *
612 * Example:
613 *
614 * phandle1: node1 {
615 * #list-cells = <2>;
616 * }
617 *
618 * phandle2: node2 {
619 * #list-cells = <1>;
620 * }
621 *
622 * node3 {
623 * list = <&phandle1 1 2 &phandle2 3>;
624 * }
625 *
626 * To get a device_node of the `node2' node you may call this:
627 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
628 *
629 * @node: device tree node containing a list
630 * @list_name: property name that contains a list
631 * @cells_name: property name that specifies phandles' arguments count
632 * @cells_count: Cell count to use if @cells_name is NULL
633 * @index: index of a phandle to parse out
634 * @out_args: optional pointer to output arguments structure (will be filled)
635 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
636 * @list_name does not exist, -EINVAL if a phandle was not found,
637 * @cells_name could not be found, the arguments were truncated or there
638 * were too many arguments.
639 */
640int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
641 const char *cells_name, int cell_count,
642 int index,
643 struct ofnode_phandle_args *out_args);
644
645/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200646 * ofnode_count_phandle_with_args() - Count number of phandle in a list
647 *
648 * This function is useful to count phandles into a list.
649 * Returns number of phandle on success, on error returns appropriate
650 * errno value.
651 *
652 * @node: device tree node containing a list
653 * @list_name: property name that contains a list
654 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunayd776a842020-09-25 09:41:14 +0200655 * @cells_count: Cell count to use if @cells_name is NULL
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200656 * @return number of phandle on success, -ENOENT if @list_name does not
657 * exist, -EINVAL if a phandle was not found, @cells_name could not
658 * be found.
659 */
660int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200661 const char *cells_name, int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200662
663/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600664 * ofnode_path() - find a node by full path
665 *
666 * @path: Full path to node, e.g. "/bus/spi@1"
667 * @return reference to the node found. Use ofnode_valid() to check if it exists
668 */
669ofnode ofnode_path(const char *path);
670
671/**
Simon Glasse09223c2020-01-27 08:49:46 -0700672 * ofnode_read_chosen_prop() - get the value of a chosen property
673 *
674 * This looks for a property within the /chosen node and returns its value
675 *
676 * @propname: Property name to look for
677 * @sizep: Returns size of property, or FDT_ERR_... error code if function
678 * returns NULL
679 * @return property value if found, else NULL
680 */
681const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
682
683/**
Simon Glassf3455962020-01-27 08:49:43 -0700684 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -0600685 *
Simon Glassf3455962020-01-27 08:49:43 -0700686 * This looks for a property within the /chosen node and returns its value,
687 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -0600688 *
689 * @propname: Property name to look for
Simon Glassf3455962020-01-27 08:49:43 -0700690 * @return string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600691 */
Simon Glassf3455962020-01-27 08:49:43 -0700692const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600693
694/**
Simon Glassc99ba912020-01-27 08:49:42 -0700695 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -0600696 *
Simon Glassc99ba912020-01-27 08:49:42 -0700697 * This looks up a named property in the chosen node and uses that as a path to
698 * look up a code.
699 *
700 * @return the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -0600701 */
Simon Glassc99ba912020-01-27 08:49:42 -0700702ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600703
Michal Simek92a88622020-07-28 12:51:08 +0200704/**
705 * ofnode_read_aliases_prop() - get the value of a aliases property
706 *
707 * This looks for a property within the /aliases node and returns its value
708 *
709 * @propname: Property name to look for
710 * @sizep: Returns size of property, or FDT_ERR_... error code if function
711 * returns NULL
712 * @return property value if found, else NULL
713 */
714const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
715
716/**
717 * ofnode_get_aliases_node() - get a referenced node from the aliases node
718 *
719 * This looks up a named property in the aliases node and uses that as a path to
720 * look up a code.
721 *
722 * @return the referenced node if present, else ofnode_null()
723 */
724ofnode ofnode_get_aliases_node(const char *propname);
725
Simon Glassc4fc5622017-05-18 20:08:58 -0600726struct display_timing;
727/**
728 * ofnode_decode_display_timing() - decode display timings
729 *
730 * Decode display timings from the supplied 'display-timings' node.
731 * See doc/device-tree-bindings/video/display-timing.txt for binding
732 * information.
733 *
734 * @node 'display-timing' node containing the timing subnodes
735 * @index Index number to read (0=first timing subnode)
736 * @config Place to put timings
737 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
738 */
739int ofnode_decode_display_timing(ofnode node, int index,
740 struct display_timing *config);
741
742/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100743 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -0600744 *
745 * @node: node to read
746 * @propname: property to read
747 * @lenp: place to put length on success
748 * @return pointer to property, or NULL if not found
749 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900750const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600751
752/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100753 * ofnode_get_first_property()- get the reference of the first property
754 *
755 * Get reference to the first property of the node, it is used to iterate
756 * and read all the property with ofnode_get_property_by_prop().
757 *
758 * @node: node to read
759 * @prop: place to put argument reference
760 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
761 */
762int ofnode_get_first_property(ofnode node, struct ofprop *prop);
763
764/**
765 * ofnode_get_next_property() - get the reference of the next property
766 *
767 * Get reference to the next property of the node, it is used to iterate
768 * and read all the property with ofnode_get_property_by_prop().
769 *
770 * @prop: reference of current argument and place to put reference of next one
771 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
772 */
773int ofnode_get_next_property(struct ofprop *prop);
774
775/**
776 * ofnode_get_property_by_prop() - get a pointer to the value of a property
777 *
778 * Get value for the property identified by the provided reference.
779 *
780 * @prop: reference on property
781 * @propname: If non-NULL, place to property name on success,
782 * @lenp: If non-NULL, place to put length on success
783 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
784 */
785const void *ofnode_get_property_by_prop(const struct ofprop *prop,
786 const char **propname, int *lenp);
787
788/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600789 * ofnode_is_available() - check if a node is marked available
790 *
791 * @node: node to check
792 * @return true if node's 'status' property is "okay" (or is missing)
793 */
794bool ofnode_is_available(ofnode node);
795
796/**
797 * ofnode_get_addr_size() - get address and size from a property
798 *
799 * This does no address translation. It simply reads an property that contains
800 * an address and a size value, one after the other.
801 *
802 * @node: node to read from
803 * @propname: property to read
804 * @sizep: place to put size value (on success)
805 * @return address value, or FDT_ADDR_T_NONE on error
806 */
807phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
808 phys_size_t *sizep);
809
810/**
811 * ofnode_read_u8_array_ptr() - find an 8-bit array
812 *
813 * Look up a property in a node and return a pointer to its contents as a
814 * byte array of given length. The property must have at least enough data
815 * for the array (count bytes). It may have more, but this will be ignored.
816 * The data is not copied.
817 *
818 * @node node to examine
819 * @propname name of property to find
820 * @sz number of array elements
821 * @return pointer to byte array if found, or NULL if the property is not
822 * found or there is not enough data
823 */
824const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
825 size_t sz);
826
827/**
828 * ofnode_read_pci_addr() - look up a PCI address
829 *
830 * Look at an address property in a node and return the PCI address which
831 * corresponds to the given type in the form of fdt_pci_addr.
832 * The property must hold one fdt_pci_addr with a lengh.
833 *
834 * @node node to examine
835 * @type pci address type (FDT_PCI_SPACE_xxx)
836 * @propname name of property to find
837 * @addr returns pci address in the form of fdt_pci_addr
838 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
839 * format of the property was invalid, -ENXIO if the requested
840 * address type was not found
841 */
842int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
843 const char *propname, struct fdt_pci_addr *addr);
844
845/**
Bin Mengfa157712018-08-03 01:14:35 -0700846 * ofnode_read_pci_vendev() - look up PCI vendor and device id
847 *
848 * Look at the compatible property of a device node that represents a PCI
849 * device and extract pci vendor id and device id from it.
850 *
851 * @param node node to examine
852 * @param vendor vendor id of the pci device
853 * @param device device id of the pci device
854 * @return 0 if ok, negative on error
855 */
856int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
857
858/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600859 * ofnode_read_addr_cells() - Get the number of address cells for a node
860 *
861 * This walks back up the tree to find the closest #address-cells property
862 * which controls the given node.
863 *
864 * @node: Node to check
865 * @return number of address cells this node uses
866 */
867int ofnode_read_addr_cells(ofnode node);
868
869/**
870 * ofnode_read_size_cells() - Get the number of size cells for a node
871 *
872 * This walks back up the tree to find the closest #size-cells property
873 * which controls the given node.
874 *
875 * @node: Node to check
876 * @return number of size cells this node uses
877 */
878int ofnode_read_size_cells(ofnode node);
879
880/**
Simon Glass4191dc12017-06-12 06:21:31 -0600881 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
882 *
883 * This function matches fdt_address_cells().
884 *
885 * @np: Node pointer to check
886 * @return value of #address-cells property in this node, or 2 if none
887 */
888int ofnode_read_simple_addr_cells(ofnode node);
889
890/**
891 * ofnode_read_simple_size_cells() - Get the size cells property in a node
892 *
893 * This function matches fdt_size_cells().
894 *
895 * @np: Node pointer to check
896 * @return value of #size-cells property in this node, or 2 if none
897 */
898int ofnode_read_simple_size_cells(ofnode node);
899
900/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600901 * ofnode_pre_reloc() - check if a node should be bound before relocation
902 *
903 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
904 * via special device tree properties.
905 *
906 * Before relocation this function can be used to check if nodes are required
907 * in either SPL or TPL stages.
908 *
909 * After relocation and jumping into the real U-Boot binary it is possible to
910 * determine if a node was bound in one of SPL/TPL stages.
911 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200912 * There are 4 settings currently in use
913 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glassc4fc5622017-05-18 20:08:58 -0600914 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
915 * Existing platforms only use it to indicate nodes needed in
916 * SPL. Should probably be replaced by u-boot,dm-spl for
917 * new platforms.
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200918 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
919 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glassc4fc5622017-05-18 20:08:58 -0600920 *
921 * @node: node to check
Simon Glass1c682342018-06-11 13:07:11 -0600922 * @return true if node is needed in SPL/TL, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -0600923 */
924bool ofnode_pre_reloc(ofnode node);
925
Simon Glassa8173d62018-06-11 13:07:12 -0600926/**
927 * ofnode_read_resource() - Read a resource from a node
928 *
929 * Read resource information from a node at the given index
930 *
931 * @node: Node to read from
932 * @index: Index of resource to read (0 = first)
933 * @res: Returns resource that was read, on success
934 * @return 0 if OK, -ve on error
935 */
Simon Glassf7bfcc42017-07-25 08:29:55 -0600936int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -0600937
938/**
939 * ofnode_read_resource_byname() - Read a resource from a node by name
940 *
941 * Read resource information from a node matching the given name. This uses a
942 * 'reg-names' string list property with the names matching the associated
943 * 'reg' property list.
944 *
945 * @node: Node to read from
946 * @name: Name of resource to read
947 * @res: Returns resource that was read, on success
948 * @return 0 if OK, -ve on error
949 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900950int ofnode_read_resource_byname(ofnode node, const char *name,
951 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600952
Simon Glass28529762017-08-05 15:45:54 -0600953/**
Simon Glass954eeae2018-06-11 13:07:13 -0600954 * ofnode_by_compatible() - Find the next compatible node
955 *
956 * Find the next node after @from that is compatible with @compat
957 *
958 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
959 * @compat: Compatible string to match
960 * @return ofnode found, or ofnode_null() if none
961 */
962ofnode ofnode_by_compatible(ofnode from, const char *compat);
963
964/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200965 * ofnode_by_prop_value() - Find the next node with given property value
966 *
967 * Find the next node after @from that has a @propname with a value
968 * @propval and a length @proplen.
969 *
970 * @from: ofnode to start from (use ofnode_null() to start at the
971 * beginning) @propname: property name to check @propval: property value to
972 * search for @proplen: length of the value in propval @return ofnode
973 * found, or ofnode_null() if none
974 */
975ofnode ofnode_by_prop_value(ofnode from, const char *propname,
976 const void *propval, int proplen);
977
978/**
Simon Glass28529762017-08-05 15:45:54 -0600979 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
980 *
981 * @node: child node (ofnode, lvalue)
982 * @parent: parent node (ofnode)
983 *
984 * This is a wrapper around a for loop and is used like so:
985 *
986 * ofnode node;
987 *
988 * ofnode_for_each_subnode(node, parent) {
989 * Use node
990 * ...
991 * }
992 *
993 * Note that this is implemented as a macro and @node is used as
994 * iterator in the loop. The parent variable can be a constant or even a
995 * literal.
996 */
997#define ofnode_for_each_subnode(node, parent) \
998 for (node = ofnode_first_subnode(parent); \
999 ofnode_valid(node); \
1000 node = ofnode_next_subnode(node))
1001
Mario Sixaefac062018-01-15 11:07:19 +01001002/**
developerd93c8b42020-05-02 11:35:09 +02001003 * ofnode_get_child_count() - get the child count of a ofnode
1004 *
1005 * @node: valid node to get its child count
1006 * @return the number of subnodes
1007 */
1008int ofnode_get_child_count(ofnode parent);
1009
1010/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001011 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +01001012 *
1013 * Translate an address from the device-tree into a CPU physical address. This
1014 * function walks up the tree and applies the various bus mappings along the
1015 * way.
1016 *
1017 * @ofnode: Device tree node giving the context in which to translate the
1018 * address
1019 * @in_addr: pointer to the address to translate
1020 * @return the translated address; OF_BAD_ADDR on error
1021 */
1022u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001023
1024/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001025 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1026 *
1027 * Translate a DMA address from the device-tree into a CPU physical address.
1028 * This function walks up the tree and applies the various bus mappings along
1029 * the way.
1030 *
1031 * @ofnode: Device tree node giving the context in which to translate the
1032 * DMA address
1033 * @in_addr: pointer to the DMA address to translate
1034 * @return the translated DMA address; OF_BAD_ADDR on error
1035 */
1036u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1037
1038/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001039 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1040 *
1041 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1042 * cpu->bus address translations
1043 *
1044 * @param blob Pointer to device tree blob
1045 * @param node_offset Node DT offset
1046 * @param cpu Pointer to variable storing the range's cpu address
1047 * @param bus Pointer to variable storing the range's bus address
1048 * @param size Pointer to variable storing the range's size
1049 * @return translated DMA address or OF_BAD_ADDR on error
1050 */
1051int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1052 u64 *size);
1053
1054/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001055 * ofnode_device_is_compatible() - check if the node is compatible with compat
1056 *
1057 * This allows to check whether the node is comaptible with the compat.
1058 *
1059 * @node: Device tree node for which compatible needs to be verified.
1060 * @compat: Compatible string which needs to verified in the given node.
1061 * @return true if OK, false if the compatible is not found
1062 */
1063int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +02001064
1065/**
1066 * ofnode_write_prop() - Set a property of a ofnode
1067 *
1068 * Note that the value passed to the function is *not* allocated by the
1069 * function itself, but must be allocated by the caller if necessary.
1070 *
1071 * @node: The node for whose property should be set
1072 * @propname: The name of the property to set
1073 * @len: The length of the new value of the property
1074 * @value: The new value of the property (must be valid prior to calling
1075 * the function)
1076 * @return 0 if successful, -ve on error
1077 */
1078int ofnode_write_prop(ofnode node, const char *propname, int len,
1079 const void *value);
1080
1081/**
1082 * ofnode_write_string() - Set a string property of a ofnode
1083 *
1084 * Note that the value passed to the function is *not* allocated by the
1085 * function itself, but must be allocated by the caller if necessary.
1086 *
1087 * @node: The node for whose string property should be set
1088 * @propname: The name of the string property to set
1089 * @value: The new value of the string property (must be valid prior to
1090 * calling the function)
1091 * @return 0 if successful, -ve on error
1092 */
1093int ofnode_write_string(ofnode node, const char *propname, const char *value);
1094
1095/**
1096 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1097 * ofnode
1098 *
1099 * This function effectively sets the node's "status" property to either "okay"
1100 * or "disable", hence making it available for driver model initialization or
1101 * not.
1102 *
1103 * @node: The node to enable
1104 * @value: Flag that tells the function to either disable or enable the
1105 * node
1106 * @return 0 if successful, -ve on error
1107 */
1108int ofnode_set_enabled(ofnode node, bool value);
1109
Simon Glass9a148602017-05-17 17:18:10 -06001110#endif