blob: 6601bd831892312b09f4a3bcdd6e8ede5e757cc8 [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/**
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530235 * ofnode_name_eq() - Check if the node name is equivalent to a given name
236 * ignoring the unit address
237 *
238 * @node: valid node reference that has to be compared
239 * @name: name that has to be compared with the node name
240 * @return true if matches, false if it doesn't match
241 */
242bool ofnode_name_eq(ofnode node, const char *name);
243
244/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600245 * ofnode_read_u32() - Read a 32-bit integer from a property
246 *
247 * @ref: valid node reference to read property from
248 * @propname: name of the property to read from
249 * @outp: place to put value (if found)
250 * @return 0 if OK, -ve on error
251 */
252int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
253
254/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200255 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
256 *
257 * @ref: valid node reference to read property from
258 * @propname: name of the property to read from
259 * @index: index of the integer to return
260 * @outp: place to put value (if found)
261 * @return 0 if OK, -ve on error
262 */
263int ofnode_read_u32_index(ofnode node, const char *propname, int index,
264 u32 *outp);
265
266/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600267 * ofnode_read_s32() - Read a 32-bit integer from a property
268 *
269 * @ref: valid node reference to read property from
270 * @propname: name of the property to read from
271 * @outp: place to put value (if found)
272 * @return 0 if OK, -ve on error
273 */
274static inline int ofnode_read_s32(ofnode node, const char *propname,
275 s32 *out_value)
276{
277 return ofnode_read_u32(node, propname, (u32 *)out_value);
278}
279
280/**
281 * ofnode_read_u32_default() - Read a 32-bit integer from a property
282 *
283 * @ref: valid node reference to read property from
284 * @propname: name of the property to read from
285 * @def: default value to return if the property has no value
286 * @return property value, or @def if not found
287 */
Trent Piepho5b775412019-05-10 17:48:20 +0000288u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600289
290/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200291 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
292 * property
293 *
294 * @ref: valid node reference to read property from
295 * @propname: name of the property to read from
296 * @index: index of the integer to return
297 * @def: default value to return if the property has no value
298 * @return property value, or @def if not found
299 */
300u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
301 u32 def);
302
303/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600304 * ofnode_read_s32_default() - Read a 32-bit integer from a property
305 *
306 * @ref: valid node reference to read property from
307 * @propname: name of the property to read from
308 * @def: default value to return if the property has no value
309 * @return property value, or @def if not found
310 */
311int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
312
313/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100314 * ofnode_read_u64() - Read a 64-bit integer from a property
315 *
316 * @node: valid node reference to read property from
317 * @propname: name of the property to read from
318 * @outp: place to put value (if found)
319 * @return 0 if OK, -ve on error
320 */
321int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
322
323/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600324 * ofnode_read_u64_default() - Read a 64-bit integer from a property
325 *
326 * @ref: valid node reference to read property from
327 * @propname: name of the property to read from
328 * @def: default value to return if the property has no value
329 * @return property value, or @def if not found
330 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200331u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600332
333/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700334 * ofnode_read_prop() - Read a property from a node
335 *
336 * @node: valid node reference to read property from
337 * @propname: name of the property to read
338 * @sizep: if non-NULL, returns the size of the property, or an error code
339 if not found
340 * @return property value, or NULL if there is no such property
341 */
342const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
343
344/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600345 * ofnode_read_string() - Read a string from a property
346 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700347 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600348 * @propname: name of the property to read
349 * @return string from property value, or NULL if there is no such property
350 */
351const char *ofnode_read_string(ofnode node, const char *propname);
352
353/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600354 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600355 *
356 * @node: valid node reference to read property from
357 * @propname: name of the property to read
358 * @out_values: pointer to return value, modified only if return value is 0
359 * @sz: number of array elements to read
Simon Glass1c682342018-06-11 13:07:11 -0600360 * @return 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600361 *
362 * Search for a property in a device node and read 32-bit value(s) from
363 * it. Returns 0 on success, -EINVAL if the property does not exist,
364 * -ENODATA if property does not have a value, and -EOVERFLOW if the
365 * property data isn't large enough.
366 *
367 * The out_values is modified only if a valid u32 value can be decoded.
368 */
369int ofnode_read_u32_array(ofnode node, const char *propname,
370 u32 *out_values, size_t sz);
371
372/**
373 * ofnode_read_bool() - read a boolean value from a property
374 *
375 * @node: valid node reference to read property from
376 * @propname: name of property to read
377 * @return true if property is present (meaning true), false if not present
378 */
379bool ofnode_read_bool(ofnode node, const char *propname);
380
381/**
382 * ofnode_find_subnode() - find a named subnode of a parent node
383 *
384 * @node: valid reference to parent node
385 * @subnode_name: name of subnode to find
386 * @return reference to subnode (which can be invalid if there is no such
387 * subnode)
388 */
389ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
390
Simon Glass39f1d282020-12-16 17:25:06 -0700391#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass3ba929a2020-10-30 21:38:53 -0600392#include <asm/global_data.h>
393
Simon Glass39f1d282020-12-16 17:25:06 -0700394static inline bool ofnode_is_enabled(ofnode node)
395{
396 if (ofnode_is_np(node)) {
397 return of_device_is_available(ofnode_to_np(node));
398 } else {
399 return fdtdec_get_is_enabled(gd->fdt_blob,
400 ofnode_to_offset(node));
401 }
402}
403
404static inline ofnode ofnode_first_subnode(ofnode node)
405{
406 assert(ofnode_valid(node));
407 if (ofnode_is_np(node))
408 return np_to_ofnode(node.np->child);
409
410 return offset_to_ofnode(
411 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
412}
413
414static inline ofnode ofnode_next_subnode(ofnode node)
415{
416 assert(ofnode_valid(node));
417 if (ofnode_is_np(node))
418 return np_to_ofnode(node.np->sibling);
419
420 return offset_to_ofnode(
421 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
422}
423#else
424/**
425 * ofnode_is_enabled() - Checks whether a node is enabled.
426 * This looks for a 'status' property. If this exists, then returns true if
427 * the status is 'okay' and false otherwise. If there is no status property,
428 * it returns true on the assumption that anything mentioned should be enabled
429 * by default.
430 *
431 * @node: node to examine
432 * @return false (not enabled) or true (enabled)
433 */
434bool ofnode_is_enabled(ofnode node);
435
Simon Glassc4fc5622017-05-18 20:08:58 -0600436/**
437 * ofnode_first_subnode() - find the first subnode of a parent node
438 *
439 * @node: valid reference to a valid parent node
440 * @return reference to the first subnode (which can be invalid if the parent
441 * node has no subnodes)
442 */
443ofnode ofnode_first_subnode(ofnode node);
444
445/**
446 * ofnode_next_subnode() - find the next sibling of a subnode
447 *
448 * @node: valid reference to previous node (sibling)
449 * @return reference to the next subnode (which can be invalid if the node
450 * has no more siblings)
451 */
452ofnode ofnode_next_subnode(ofnode node);
Simon Glass39f1d282020-12-16 17:25:06 -0700453#endif /* DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600454
455/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100456 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
457 *
458 * @node: valid node to look up
459 * @return ofnode reference of the parent node
460 */
461ofnode ofnode_get_parent(ofnode node);
462
463/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600464 * ofnode_get_name() - get the name of a node
465 *
466 * @node: valid node to look up
Baruch Siachf14cb212018-11-18 14:39:20 +0200467 * @return name of node
Simon Glassc4fc5622017-05-18 20:08:58 -0600468 */
469const char *ofnode_get_name(ofnode node);
470
471/**
Marek Behúne897e3c2021-05-26 14:08:18 +0200472 * ofnode_get_path() - get the full path of a node
473 *
474 * @node: valid node to look up
475 * @buf: buffer to write the node path into
476 * @buflen: buffer size
477 * @return 0 if OK, -ve on error
478 */
479int ofnode_get_path(ofnode node, char *buf, int buflen);
480
481/**
Kever Yang37df0e02018-02-23 17:38:50 +0100482 * ofnode_get_by_phandle() - get ofnode from phandle
483 *
484 * @phandle: phandle to look up
485 * @return ofnode reference to the phandle
486 */
487ofnode ofnode_get_by_phandle(uint phandle);
488
489/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600490 * ofnode_read_size() - read the size of a property
491 *
492 * @node: node to check
493 * @propname: property to check
494 * @return size of property if present, or -EINVAL if not
495 */
496int ofnode_read_size(ofnode node, const char *propname);
497
498/**
Keerthyd332e6e2019-04-24 17:19:53 +0530499 * ofnode_get_addr_size_index() - get an address/size from a node
500 * based on index
501 *
502 * This reads the register address/size from a node based on index
503 *
504 * @node: node to read from
505 * @index: Index of address to read (0 for first)
506 * @size: Pointer to size of the address
507 * @return address, or FDT_ADDR_T_NONE if not present or invalid
508 */
509phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
510 fdt_size_t *size);
511
512/**
Marek Behún177ab7f2021-05-26 14:08:17 +0200513 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
514 * based on index, without address
515 * translation
516 *
517 * This reads the register address/size from a node based on index.
518 * The resulting address is not translated. Useful for example for on-disk
519 * addresses.
520 *
521 * @node: node to read from
522 * @index: Index of address to read (0 for first)
523 * @size: Pointer to size of the address
524 * @return address, or FDT_ADDR_T_NONE if not present or invalid
525 */
526phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
527 fdt_size_t *size);
528
529/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600530 * ofnode_get_addr_index() - get an address from a node
531 *
532 * This reads the register address from a node
533 *
534 * @node: node to read from
535 * @index: Index of address to read (0 for first)
536 * @return address, or FDT_ADDR_T_NONE if not present or invalid
537 */
538phys_addr_t ofnode_get_addr_index(ofnode node, int index);
539
540/**
541 * ofnode_get_addr() - get an address from a node
542 *
543 * This reads the register address from a node
544 *
545 * @node: node to read from
546 * @return address, or FDT_ADDR_T_NONE if not present or invalid
547 */
548phys_addr_t ofnode_get_addr(ofnode node);
549
550/**
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800551 * ofnode_get_size() - get size from a node
552 *
553 * This reads the register size from a node
554 *
555 * @node: node to read from
556 * @return size of the address, or FDT_SIZE_T_NONE if not present or invalid
557 */
558fdt_size_t ofnode_get_size(ofnode node);
559
560/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600561 * ofnode_stringlist_search() - find a string in a string list and return index
562 *
563 * Note that it is possible for this function to succeed on property values
564 * that are not NUL-terminated. That's because the function will stop after
565 * finding the first occurrence of @string. This can for example happen with
566 * small-valued cell properties, such as #address-cells, when searching for
567 * the empty string.
568 *
569 * @node: node to check
570 * @propname: name of the property containing the string list
571 * @string: string to look up in the string list
572 *
573 * @return:
574 * the index of the string in the list of strings
575 * -ENODATA if the property is not found
576 * -EINVAL on some other error
577 */
578int ofnode_stringlist_search(ofnode node, const char *propname,
579 const char *string);
580
581/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600582 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600583 *
584 * Note that this will successfully extract strings from properties with
585 * non-NUL-terminated values. For example on small-valued cell properties
586 * this function will return the empty string.
587 *
588 * If non-NULL, the length of the string (on success) or a negative error-code
589 * (on failure) will be stored in the integer pointer to by lenp.
590 *
591 * @node: node to check
592 * @propname: name of the property containing the string list
Simon Glass7a3a1672021-10-23 17:26:06 -0600593 * @index: index of the string to return (cannot be negative)
Simon Glassc4fc5622017-05-18 20:08:58 -0600594 * @lenp: return location for the string length or an error code on failure
595 *
596 * @return:
Simon Glass7a3a1672021-10-23 17:26:06 -0600597 * 0 if found or -ve error value if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600598 */
599int ofnode_read_string_index(ofnode node, const char *propname, int index,
600 const char **outp);
601
602/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600603 * ofnode_read_string_count() - find the number of strings in a string list
604 *
605 * @node: node to check
606 * @propname: name of the property containing the string list
607 * @return:
608 * number of strings in the list, or -ve error value if not found
609 */
610int ofnode_read_string_count(ofnode node, const char *property);
611
612/**
Simon Glass9580bfc2021-10-23 17:26:07 -0600613 * ofnode_read_string_list() - read a list of strings
614 *
615 * This produces a list of string pointers with each one pointing to a string
616 * in the string list. If the property does not exist, it returns {NULL}.
617 *
618 * The data is allocated and the caller is reponsible for freeing the return
619 * value (the list of string pointers). The strings themselves may not be
620 * changed as they point directly into the devicetree property.
621 *
622 * @node: node to check
623 * @listp: returns an allocated, NULL-terminated list of strings if the return
624 * value is > 0, else is set to NULL
625 * @return number of strings in list, 0 if none, -ENOMEM if out of memory,
626 * -EINVAL if no such property, -EENODATA if property is empty
627 * @return: NULL-terminated list of strings (NULL if no property or empty)
628 */
629int ofnode_read_string_list(ofnode node, const char *property,
630 const char ***listp);
631
632/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600633 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
634 *
635 * This function is useful to parse lists of phandles and their arguments.
636 * Returns 0 on success and fills out_args, on error returns appropriate
637 * errno value.
638 *
639 * Caller is responsible to call of_node_put() on the returned out_args->np
640 * pointer.
641 *
642 * Example:
643 *
644 * phandle1: node1 {
645 * #list-cells = <2>;
646 * }
647 *
648 * phandle2: node2 {
649 * #list-cells = <1>;
650 * }
651 *
652 * node3 {
653 * list = <&phandle1 1 2 &phandle2 3>;
654 * }
655 *
656 * To get a device_node of the `node2' node you may call this:
657 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
658 *
659 * @node: device tree node containing a list
660 * @list_name: property name that contains a list
661 * @cells_name: property name that specifies phandles' arguments count
662 * @cells_count: Cell count to use if @cells_name is NULL
663 * @index: index of a phandle to parse out
664 * @out_args: optional pointer to output arguments structure (will be filled)
665 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
666 * @list_name does not exist, -EINVAL if a phandle was not found,
667 * @cells_name could not be found, the arguments were truncated or there
668 * were too many arguments.
669 */
670int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
671 const char *cells_name, int cell_count,
672 int index,
673 struct ofnode_phandle_args *out_args);
674
675/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200676 * ofnode_count_phandle_with_args() - Count number of phandle in a list
677 *
678 * This function is useful to count phandles into a list.
679 * Returns number of phandle on success, on error returns appropriate
680 * errno value.
681 *
682 * @node: device tree node containing a list
683 * @list_name: property name that contains a list
684 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunayd776a842020-09-25 09:41:14 +0200685 * @cells_count: Cell count to use if @cells_name is NULL
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200686 * @return number of phandle on success, -ENOENT if @list_name does not
687 * exist, -EINVAL if a phandle was not found, @cells_name could not
688 * be found.
689 */
690int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200691 const char *cells_name, int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200692
693/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600694 * ofnode_path() - find a node by full path
695 *
696 * @path: Full path to node, e.g. "/bus/spi@1"
697 * @return reference to the node found. Use ofnode_valid() to check if it exists
698 */
699ofnode ofnode_path(const char *path);
700
701/**
Simon Glasse09223c2020-01-27 08:49:46 -0700702 * ofnode_read_chosen_prop() - get the value of a chosen property
703 *
704 * This looks for a property within the /chosen node and returns its value
705 *
706 * @propname: Property name to look for
707 * @sizep: Returns size of property, or FDT_ERR_... error code if function
708 * returns NULL
709 * @return property value if found, else NULL
710 */
711const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
712
713/**
Simon Glassf3455962020-01-27 08:49:43 -0700714 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -0600715 *
Simon Glassf3455962020-01-27 08:49:43 -0700716 * This looks for a property within the /chosen node and returns its value,
717 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -0600718 *
719 * @propname: Property name to look for
Simon Glassf3455962020-01-27 08:49:43 -0700720 * @return string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600721 */
Simon Glassf3455962020-01-27 08:49:43 -0700722const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600723
724/**
Simon Glassc99ba912020-01-27 08:49:42 -0700725 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -0600726 *
Simon Glassc99ba912020-01-27 08:49:42 -0700727 * This looks up a named property in the chosen node and uses that as a path to
728 * look up a code.
729 *
730 * @return the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -0600731 */
Simon Glassc99ba912020-01-27 08:49:42 -0700732ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600733
Michal Simek92a88622020-07-28 12:51:08 +0200734/**
735 * ofnode_read_aliases_prop() - get the value of a aliases property
736 *
737 * This looks for a property within the /aliases node and returns its value
738 *
739 * @propname: Property name to look for
740 * @sizep: Returns size of property, or FDT_ERR_... error code if function
741 * returns NULL
742 * @return property value if found, else NULL
743 */
744const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
745
746/**
747 * ofnode_get_aliases_node() - get a referenced node from the aliases node
748 *
749 * This looks up a named property in the aliases node and uses that as a path to
750 * look up a code.
751 *
752 * @return the referenced node if present, else ofnode_null()
753 */
754ofnode ofnode_get_aliases_node(const char *propname);
755
Simon Glassc4fc5622017-05-18 20:08:58 -0600756struct display_timing;
757/**
758 * ofnode_decode_display_timing() - decode display timings
759 *
760 * Decode display timings from the supplied 'display-timings' node.
761 * See doc/device-tree-bindings/video/display-timing.txt for binding
762 * information.
763 *
764 * @node 'display-timing' node containing the timing subnodes
765 * @index Index number to read (0=first timing subnode)
766 * @config Place to put timings
767 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
768 */
769int ofnode_decode_display_timing(ofnode node, int index,
770 struct display_timing *config);
771
772/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100773 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -0600774 *
775 * @node: node to read
776 * @propname: property to read
777 * @lenp: place to put length on success
778 * @return pointer to property, or NULL if not found
779 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900780const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600781
782/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100783 * ofnode_get_first_property()- get the reference of the first property
784 *
785 * Get reference to the first property of the node, it is used to iterate
786 * and read all the property with ofnode_get_property_by_prop().
787 *
788 * @node: node to read
789 * @prop: place to put argument reference
790 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
791 */
792int ofnode_get_first_property(ofnode node, struct ofprop *prop);
793
794/**
795 * ofnode_get_next_property() - get the reference of the next property
796 *
797 * Get reference to the next property of the node, it is used to iterate
798 * and read all the property with ofnode_get_property_by_prop().
799 *
800 * @prop: reference of current argument and place to put reference of next one
801 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
802 */
803int ofnode_get_next_property(struct ofprop *prop);
804
805/**
806 * ofnode_get_property_by_prop() - get a pointer to the value of a property
807 *
808 * Get value for the property identified by the provided reference.
809 *
810 * @prop: reference on property
811 * @propname: If non-NULL, place to property name on success,
812 * @lenp: If non-NULL, place to put length on success
813 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
814 */
815const void *ofnode_get_property_by_prop(const struct ofprop *prop,
816 const char **propname, int *lenp);
817
818/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600819 * ofnode_is_available() - check if a node is marked available
820 *
821 * @node: node to check
822 * @return true if node's 'status' property is "okay" (or is missing)
823 */
824bool ofnode_is_available(ofnode node);
825
826/**
827 * ofnode_get_addr_size() - get address and size from a property
828 *
829 * This does no address translation. It simply reads an property that contains
830 * an address and a size value, one after the other.
831 *
832 * @node: node to read from
833 * @propname: property to read
834 * @sizep: place to put size value (on success)
835 * @return address value, or FDT_ADDR_T_NONE on error
836 */
837phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
838 phys_size_t *sizep);
839
840/**
841 * ofnode_read_u8_array_ptr() - find an 8-bit array
842 *
843 * Look up a property in a node and return a pointer to its contents as a
844 * byte array of given length. The property must have at least enough data
845 * for the array (count bytes). It may have more, but this will be ignored.
846 * The data is not copied.
847 *
848 * @node node to examine
849 * @propname name of property to find
850 * @sz number of array elements
851 * @return pointer to byte array if found, or NULL if the property is not
852 * found or there is not enough data
853 */
854const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
855 size_t sz);
856
857/**
858 * ofnode_read_pci_addr() - look up a PCI address
859 *
860 * Look at an address property in a node and return the PCI address which
861 * corresponds to the given type in the form of fdt_pci_addr.
862 * The property must hold one fdt_pci_addr with a lengh.
863 *
864 * @node node to examine
865 * @type pci address type (FDT_PCI_SPACE_xxx)
866 * @propname name of property to find
867 * @addr returns pci address in the form of fdt_pci_addr
868 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
869 * format of the property was invalid, -ENXIO if the requested
870 * address type was not found
871 */
872int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
873 const char *propname, struct fdt_pci_addr *addr);
874
875/**
Bin Mengfa157712018-08-03 01:14:35 -0700876 * ofnode_read_pci_vendev() - look up PCI vendor and device id
877 *
878 * Look at the compatible property of a device node that represents a PCI
879 * device and extract pci vendor id and device id from it.
880 *
881 * @param node node to examine
882 * @param vendor vendor id of the pci device
883 * @param device device id of the pci device
884 * @return 0 if ok, negative on error
885 */
886int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
887
888/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600889 * ofnode_read_addr_cells() - Get the number of address cells for a node
890 *
891 * This walks back up the tree to find the closest #address-cells property
892 * which controls the given node.
893 *
894 * @node: Node to check
895 * @return number of address cells this node uses
896 */
897int ofnode_read_addr_cells(ofnode node);
898
899/**
900 * ofnode_read_size_cells() - Get the number of size cells for a node
901 *
902 * This walks back up the tree to find the closest #size-cells property
903 * which controls the given node.
904 *
905 * @node: Node to check
906 * @return number of size cells this node uses
907 */
908int ofnode_read_size_cells(ofnode node);
909
910/**
Simon Glass4191dc12017-06-12 06:21:31 -0600911 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
912 *
913 * This function matches fdt_address_cells().
914 *
915 * @np: Node pointer to check
916 * @return value of #address-cells property in this node, or 2 if none
917 */
918int ofnode_read_simple_addr_cells(ofnode node);
919
920/**
921 * ofnode_read_simple_size_cells() - Get the size cells property in a node
922 *
923 * This function matches fdt_size_cells().
924 *
925 * @np: Node pointer to check
926 * @return value of #size-cells property in this node, or 2 if none
927 */
928int ofnode_read_simple_size_cells(ofnode node);
929
930/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600931 * ofnode_pre_reloc() - check if a node should be bound before relocation
932 *
933 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
934 * via special device tree properties.
935 *
936 * Before relocation this function can be used to check if nodes are required
937 * in either SPL or TPL stages.
938 *
939 * After relocation and jumping into the real U-Boot binary it is possible to
940 * determine if a node was bound in one of SPL/TPL stages.
941 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200942 * There are 4 settings currently in use
943 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glassc4fc5622017-05-18 20:08:58 -0600944 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
945 * Existing platforms only use it to indicate nodes needed in
946 * SPL. Should probably be replaced by u-boot,dm-spl for
947 * new platforms.
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200948 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
949 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glassc4fc5622017-05-18 20:08:58 -0600950 *
951 * @node: node to check
Simon Glass1c682342018-06-11 13:07:11 -0600952 * @return true if node is needed in SPL/TL, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -0600953 */
954bool ofnode_pre_reloc(ofnode node);
955
Simon Glassa8173d62018-06-11 13:07:12 -0600956/**
957 * ofnode_read_resource() - Read a resource from a node
958 *
959 * Read resource information from a node at the given index
960 *
961 * @node: Node to read from
962 * @index: Index of resource to read (0 = first)
963 * @res: Returns resource that was read, on success
964 * @return 0 if OK, -ve on error
965 */
Simon Glassf7bfcc42017-07-25 08:29:55 -0600966int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -0600967
968/**
969 * ofnode_read_resource_byname() - Read a resource from a node by name
970 *
971 * Read resource information from a node matching the given name. This uses a
972 * 'reg-names' string list property with the names matching the associated
973 * 'reg' property list.
974 *
975 * @node: Node to read from
976 * @name: Name of resource to read
977 * @res: Returns resource that was read, on success
978 * @return 0 if OK, -ve on error
979 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900980int ofnode_read_resource_byname(ofnode node, const char *name,
981 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600982
Simon Glass28529762017-08-05 15:45:54 -0600983/**
Simon Glass954eeae2018-06-11 13:07:13 -0600984 * ofnode_by_compatible() - Find the next compatible node
985 *
986 * Find the next node after @from that is compatible with @compat
987 *
988 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
989 * @compat: Compatible string to match
990 * @return ofnode found, or ofnode_null() if none
991 */
992ofnode ofnode_by_compatible(ofnode from, const char *compat);
993
994/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200995 * ofnode_by_prop_value() - Find the next node with given property value
996 *
997 * Find the next node after @from that has a @propname with a value
998 * @propval and a length @proplen.
999 *
1000 * @from: ofnode to start from (use ofnode_null() to start at the
1001 * beginning) @propname: property name to check @propval: property value to
1002 * search for @proplen: length of the value in propval @return ofnode
1003 * found, or ofnode_null() if none
1004 */
1005ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1006 const void *propval, int proplen);
1007
1008/**
Simon Glass28529762017-08-05 15:45:54 -06001009 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1010 *
1011 * @node: child node (ofnode, lvalue)
1012 * @parent: parent node (ofnode)
1013 *
1014 * This is a wrapper around a for loop and is used like so:
1015 *
1016 * ofnode node;
1017 *
1018 * ofnode_for_each_subnode(node, parent) {
1019 * Use node
1020 * ...
1021 * }
1022 *
1023 * Note that this is implemented as a macro and @node is used as
1024 * iterator in the loop. The parent variable can be a constant or even a
1025 * literal.
1026 */
1027#define ofnode_for_each_subnode(node, parent) \
1028 for (node = ofnode_first_subnode(parent); \
1029 ofnode_valid(node); \
1030 node = ofnode_next_subnode(node))
1031
Mario Sixaefac062018-01-15 11:07:19 +01001032/**
Michael Wallea7b9df22021-10-15 15:15:17 +02001033 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1034 * compatible string
1035 *
1036 * @node: child node (ofnode, lvalue)
1037 * @compat: compatible string to match
1038 *
1039 * This is a wrapper around a for loop and is used like so:
1040 *
1041 * ofnode node;
1042 *
1043 * ofnode_for_each_compatible_node(node, parent, compatible) {
1044 * Use node
1045 * ...
1046 * }
1047 *
1048 * Note that this is implemented as a macro and @node is used as
1049 * iterator in the loop.
1050 */
1051#define ofnode_for_each_compatible_node(node, compat) \
1052 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1053 ofnode_valid(node); \
1054 node = ofnode_by_compatible(node, compat))
1055
1056/**
developerd93c8b42020-05-02 11:35:09 +02001057 * ofnode_get_child_count() - get the child count of a ofnode
1058 *
1059 * @node: valid node to get its child count
1060 * @return the number of subnodes
1061 */
1062int ofnode_get_child_count(ofnode parent);
1063
1064/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001065 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +01001066 *
1067 * Translate an address from the device-tree into a CPU physical address. This
1068 * function walks up the tree and applies the various bus mappings along the
1069 * way.
1070 *
1071 * @ofnode: Device tree node giving the context in which to translate the
1072 * address
1073 * @in_addr: pointer to the address to translate
1074 * @return the translated address; OF_BAD_ADDR on error
1075 */
1076u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001077
1078/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001079 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1080 *
1081 * Translate a DMA address from the device-tree into a CPU physical address.
1082 * This function walks up the tree and applies the various bus mappings along
1083 * the way.
1084 *
1085 * @ofnode: Device tree node giving the context in which to translate the
1086 * DMA address
1087 * @in_addr: pointer to the DMA address to translate
1088 * @return the translated DMA address; OF_BAD_ADDR on error
1089 */
1090u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1091
1092/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001093 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1094 *
1095 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1096 * cpu->bus address translations
1097 *
1098 * @param blob Pointer to device tree blob
1099 * @param node_offset Node DT offset
1100 * @param cpu Pointer to variable storing the range's cpu address
1101 * @param bus Pointer to variable storing the range's bus address
1102 * @param size Pointer to variable storing the range's size
1103 * @return translated DMA address or OF_BAD_ADDR on error
1104 */
1105int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1106 u64 *size);
1107
1108/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001109 * ofnode_device_is_compatible() - check if the node is compatible with compat
1110 *
1111 * This allows to check whether the node is comaptible with the compat.
1112 *
1113 * @node: Device tree node for which compatible needs to be verified.
1114 * @compat: Compatible string which needs to verified in the given node.
1115 * @return true if OK, false if the compatible is not found
1116 */
1117int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +02001118
1119/**
1120 * ofnode_write_prop() - Set a property of a ofnode
1121 *
1122 * Note that the value passed to the function is *not* allocated by the
1123 * function itself, but must be allocated by the caller if necessary.
1124 *
1125 * @node: The node for whose property should be set
1126 * @propname: The name of the property to set
1127 * @len: The length of the new value of the property
1128 * @value: The new value of the property (must be valid prior to calling
1129 * the function)
1130 * @return 0 if successful, -ve on error
1131 */
1132int ofnode_write_prop(ofnode node, const char *propname, int len,
1133 const void *value);
1134
1135/**
1136 * ofnode_write_string() - Set a string property of a ofnode
1137 *
1138 * Note that the value passed to the function is *not* allocated by the
1139 * function itself, but must be allocated by the caller if necessary.
1140 *
1141 * @node: The node for whose string property should be set
1142 * @propname: The name of the string property to set
1143 * @value: The new value of the string property (must be valid prior to
1144 * calling the function)
1145 * @return 0 if successful, -ve on error
1146 */
1147int ofnode_write_string(ofnode node, const char *propname, const char *value);
1148
1149/**
1150 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1151 * ofnode
1152 *
1153 * This function effectively sets the node's "status" property to either "okay"
1154 * or "disable", hence making it available for driver model initialization or
1155 * not.
1156 *
1157 * @node: The node to enable
1158 * @value: Flag that tells the function to either disable or enable the
1159 * node
1160 * @return 0 if successful, -ve on error
1161 */
1162int ofnode_set_enabled(ofnode node, bool value);
1163
Simon Glass0034d962021-08-07 07:24:01 -06001164/**
1165 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1166 *
1167 * This reads a property from the /config node of the devicetree.
1168 *
1169 * See doc/config.txt for bindings
1170 *
1171 * @prop_name property name to look up
1172 * @return true, if it exists, false if not
1173 */
1174bool ofnode_conf_read_bool(const char *prop_name);
1175
1176/**
1177 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1178 *
1179 * This reads a property from the /config node of the devicetree.
1180 *
1181 * See doc/config.txt for bindings
1182 *
1183 * @prop_name: property name to look up
1184 * @default_val: default value to return if the property is not found
1185 * @return integer value, if found, or @default_val if not
1186 */
1187int ofnode_conf_read_int(const char *prop_name, int default_val);
1188
1189/**
1190 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1191 *
1192 * This reads a property from the /config node of the devicetree.
1193 *
1194 * See doc/config.txt for bindings
1195 *
1196 * @prop_name: property name to look up
1197 * @return string value, if found, or NULL if not
1198 */
1199const char *ofnode_conf_read_str(const char *prop_name);
1200
Simon Glass9a148602017-05-17 17:18:10 -06001201#endif