blob: 8e4c3574df8c3d2fad6cdcdb09a8a79d1a17b4e1 [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>
Marek Behúnbc194772022-04-07 00:33:01 +020015#include <phy_interface.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060016
17/* Enable checks to protect against invalid calls */
18#undef OF_CHECKS
19
Simon Glassebbe90b2023-09-26 08:14:43 -060020struct abuf;
Simon Glassf7bfcc42017-07-25 08:29:55 -060021struct resource;
22
Simon Glass9693c1d2022-07-30 15:52:06 -060023#include <dm/ofnode_decl.h>
Michal Simek43c42bd2023-08-31 08:59:05 +020024#include <linux/errno.h>
Simon Glass9a148602017-05-17 17:18:10 -060025
Simon Glassc4fc5622017-05-18 20:08:58 -060026struct ofnode_phandle_args {
27 ofnode node;
28 int args_count;
29 uint32_t args[OF_MAX_PHANDLE_ARGS];
30};
31
Simon Glasscb13a1b2022-09-06 20:27:26 -060032#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
Simon Glassc4fc5622017-05-18 20:08:58 -060033/**
Simon Glassba8457b2022-09-06 20:27:19 -060034 * oftree_reset() - reset the state of the oftree list
35 *
36 * Reset the oftree list so it can be started again. This should be called
37 * once the control FDT is in place, but before the ofnode interface is used.
38 */
Simon Glasscb13a1b2022-09-06 20:27:26 -060039void oftree_reset(void);
Simon Glassba8457b2022-09-06 20:27:19 -060040
41/**
Simon Glass04fa09a2022-09-06 20:27:20 -060042 * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
43 *
44 * This cannot be called if the reference contains a node pointer.
45 *
46 * @node: Reference containing offset (possibly invalid)
47 * Return: DT offset (can be NULL)
48 */
Simon Glasscb13a1b2022-09-06 20:27:26 -060049__attribute_const__ void *ofnode_to_fdt(ofnode node);
50
51/**
52 * ofnode_to_offset() - convert an ofnode to a flat DT offset
53 *
54 * This cannot be called if the reference contains a node pointer.
55 *
56 * @node: Reference containing offset (possibly invalid)
57 * Return: DT offset (can be -1)
58 */
59__attribute_const__ int ofnode_to_offset(ofnode node);
60
61/**
62 * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
63 *
Simon Glasse6a211c2022-10-11 09:47:19 -060064 * If @fdt is not already registered in the list of current device trees, it is
65 * added to the list.
66 *
Simon Glasscb13a1b2022-09-06 20:27:26 -060067 * @fdt: Device tree to use
68 *
69 * Returns: reference to the given node
70 */
71oftree oftree_from_fdt(void *fdt);
72
73/**
74 * noffset_to_ofnode() - convert a DT offset to an ofnode
75 *
76 * @other_node: Node in the same tree to use as a reference
77 * @of_offset: DT offset (either valid, or -1)
78 * Return: reference to the associated DT offset
79 */
80ofnode noffset_to_ofnode(ofnode other_node, int of_offset);
81
82#else /* !OFNODE_MULTI_TREE */
83static inline void oftree_reset(void) {}
84
Simon Glass04fa09a2022-09-06 20:27:20 -060085static inline void *ofnode_to_fdt(ofnode node)
86{
87#ifdef OF_CHECKS
88 if (of_live_active())
89 return NULL;
90#endif
Simon Glass04fa09a2022-09-06 20:27:20 -060091 /* Use the control FDT by default */
92 return (void *)gd->fdt_blob;
93}
94
Simon Glasscb13a1b2022-09-06 20:27:26 -060095static inline __attribute_const__ int ofnode_to_offset(ofnode node)
Simon Glass37dcd912022-09-06 20:27:23 -060096{
97#ifdef OF_CHECKS
98 if (of_live_active())
99 return -1;
100#endif
101 return node.of_offset;
102}
103
Simon Glasscb13a1b2022-09-06 20:27:26 -0600104static inline oftree oftree_from_fdt(void *fdt)
105{
106 oftree tree;
107
108 /* we cannot access other trees without OFNODE_MULTI_TREE */
109 if (fdt == gd->fdt_blob)
110 tree.fdt = fdt;
111 else
112 tree.fdt = NULL;
113
114 return tree;
115}
116
117static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
118{
119 ofnode node;
120
121 if (of_live_active())
122 node.np = NULL;
123 else
124 node.of_offset = of_offset;
125
126 return node;
127}
128
129#endif /* OFNODE_MULTI_TREE */
130
Simon Glass37dcd912022-09-06 20:27:23 -0600131/**
Simon Glassa869a1b2023-09-26 08:14:40 -0600132 * oftree_new() - Create a new, empty tree
133 *
134 * @treep: Returns a pointer to the tree, on success
135 * Returns: 0 on success, -ENOMEM if out of memory, -E2BIG if !OF_LIVE and
136 * there are too many (flattrees) already
137 */
138int oftree_new(oftree *treep);
139
140/**
Simon Glassebbe90b2023-09-26 08:14:43 -0600141 * oftree_to_fdt() - Convert an oftree to a flat FDT
142 *
143 * @tree: tree to flatten (if livetree) or copy (if not)
144 * @buf: Returns inited buffer containing the newly created flat tree. Note
145 * that for flat tree the buffer is not allocated. In either case the caller
146 * must call abut_uninit() to free any memory used by @buf
147 * Return: 0 on success, -ENOMEM if out of memory, other -ve value for any other
148 * error
149 */
150int oftree_to_fdt(oftree tree, struct abuf *buf);
151
152/**
Stefan Roesec14df8b2020-09-23 08:23:27 +0200153 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -0600154 *
155 * This cannot be called if the reference contains an offset.
156 *
157 * @node: Reference containing struct device_node * (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100158 * Return: pointer to device node (can be NULL)
Simon Glassc4fc5622017-05-18 20:08:58 -0600159 */
Simon Glass9036c5c2022-09-06 20:27:04 -0600160static inline struct device_node *ofnode_to_np(ofnode node)
Simon Glassc4fc5622017-05-18 20:08:58 -0600161{
162#ifdef OF_CHECKS
163 if (!of_live_active())
164 return NULL;
165#endif
166 return node.np;
167}
168
Simon Glass9a148602017-05-17 17:18:10 -0600169/**
Simon Glass9a148602017-05-17 17:18:10 -0600170 * ofnode_valid() - check if an ofnode is valid
171 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100172 * @node: Reference containing offset (possibly invalid)
Simon Glasscb13a1b2022-09-06 20:27:26 -0600173 * Return: true if the reference contains a valid ofnode, false if not
Simon Glass9a148602017-05-17 17:18:10 -0600174 */
175static inline bool ofnode_valid(ofnode node)
176{
Simon Glassc4fc5622017-05-18 20:08:58 -0600177 if (of_live_active())
178 return node.np != NULL;
179 else
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200180 return node.of_offset >= 0;
Simon Glass9a148602017-05-17 17:18:10 -0600181}
182
183/**
Simon Glass95fd2092022-09-06 20:27:22 -0600184 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
185 *
186 * This can only be called when flat tree is enabled
187 *
188 * @tree: Tree to look at
189 * @return FDT pointer from the tree
190 */
191static inline void *oftree_lookup_fdt(oftree tree)
192{
193 if (of_live_active())
194 return NULL;
195 else
196 return tree.fdt;
197}
198
199/**
Simon Glass9a148602017-05-17 17:18:10 -0600200 * offset_to_ofnode() - convert a DT offset to an ofnode
201 *
202 * @of_offset: DT offset (either valid, or -1)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100203 * Return: reference to the associated DT offset
Simon Glass9a148602017-05-17 17:18:10 -0600204 */
205static inline ofnode offset_to_ofnode(int of_offset)
206{
207 ofnode node;
208
Simon Glassc4fc5622017-05-18 20:08:58 -0600209 if (of_live_active())
210 node.np = NULL;
211 else
Simon Glass1ab8b892019-12-06 21:41:36 -0700212 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass9a148602017-05-17 17:18:10 -0600213
214 return node;
215}
216
217/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600218 * np_to_ofnode() - convert a node pointer to an ofnode
219 *
220 * @np: Live node pointer (can be NULL)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100221 * Return: reference to the associated node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -0600222 */
Simon Glass9036c5c2022-09-06 20:27:04 -0600223static inline ofnode np_to_ofnode(struct device_node *np)
Simon Glassc4fc5622017-05-18 20:08:58 -0600224{
225 ofnode node;
226
227 node.np = np;
228
229 return node;
230}
231
232/**
233 * ofnode_is_np() - check if a reference is a node pointer
234 *
235 * This function associated that if there is a valid live tree then all
236 * references will use it. This is because using the flat DT when the live tree
237 * is valid is not permitted.
238 *
239 * @node: reference to check (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100240 * Return: true if the reference is a live node pointer, false if it is a DT
Simon Glassc4fc5622017-05-18 20:08:58 -0600241 * offset
242 */
243static inline bool ofnode_is_np(ofnode node)
244{
245#ifdef OF_CHECKS
246 /*
247 * Check our assumption that flat tree offsets are not used when a
248 * live tree is in use.
249 */
250 assert(!ofnode_valid(node) ||
Stefan Roesec14df8b2020-09-23 08:23:27 +0200251 (of_live_active() ? ofnode_to_np(node)
252 : ofnode_to_np(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600253#endif
254 return of_live_active() && ofnode_valid(node);
255}
256
257/**
Simon Glass9a148602017-05-17 17:18:10 -0600258 * ofnode_equal() - check if two references are equal
259 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100260 * @ref1: first reference to check (possibly invalid)
261 * @ref2: second reference to check (possibly invalid)
262 * Return: true if equal, else false
Simon Glass9a148602017-05-17 17:18:10 -0600263 */
264static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
265{
266 /* We only need to compare the contents */
267 return ref1.of_offset == ref2.of_offset;
268}
269
Simon Glassc4fc5622017-05-18 20:08:58 -0600270/**
Simon Glass2b9b14582022-09-06 20:27:21 -0600271 * oftree_valid() - check if an oftree is valid
272 *
273 * @tree: Reference containing oftree
274 * Return: true if the reference contains a valid oftree, false if node
275 */
276static inline bool oftree_valid(oftree tree)
277{
278 if (of_live_active())
279 return tree.np;
280 else
281 return tree.fdt;
282}
283
284/**
285 * oftree_null() - Obtain a null oftree
286 *
287 * This returns an oftree which points to no tree. It works both with the flat
288 * tree and livetree.
289 */
290static inline oftree oftree_null(void)
291{
292 oftree tree;
293
294 if (of_live_active())
295 tree.np = NULL;
296 else
297 tree.fdt = NULL;
298
299 return tree;
300}
301
302/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600303 * ofnode_null() - Obtain a null ofnode
304 *
305 * This returns an ofnode which points to no node. It works both with the flat
306 * tree and livetree.
307 */
308static inline ofnode ofnode_null(void)
309{
310 ofnode node;
311
312 if (of_live_active())
313 node.np = NULL;
314 else
315 node.of_offset = -1;
316
317 return node;
318}
319
Simon Glass278ddba2020-11-28 17:50:07 -0700320static inline ofnode ofnode_root(void)
321{
322 ofnode node;
323
324 if (of_live_active())
325 node.np = gd_of_root();
326 else
327 node.of_offset = 0;
328
329 return node;
330}
331
Simon Glassc4fc5622017-05-18 20:08:58 -0600332/**
Simon Glass4caa79a2022-09-06 20:27:16 -0600333 * ofprop_valid() - check if an ofprop is valid
334 *
335 * @prop: Pointer to ofprop to check
336 * Return: true if the reference contains a valid ofprop, false if not
337 */
338static inline bool ofprop_valid(struct ofprop *prop)
339{
340 if (of_live_active())
341 return prop->prop;
342 else
343 return prop->offset >= 0;
344}
345
346/**
Simon Glassef75c592022-07-30 15:52:08 -0600347 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
348 *
349 * Returns: reference to the control FDT
350 */
351static inline oftree oftree_default(void)
352{
353 oftree tree;
354
355 if (of_live_active())
356 tree.np = gd_of_root();
357 else
358 tree.fdt = (void *)gd->fdt_blob;
359
360 return tree;
361}
362
363/**
Simon Glass2b9b14582022-09-06 20:27:21 -0600364 * oftree_from_np() - Returns an oftree from a node pointer
365 *
366 * @root: Root node of the tree
367 * Returns: reference to the given node
368 */
369static inline oftree oftree_from_np(struct device_node *root)
370{
371 oftree tree;
372
373 tree.np = root;
374
375 return tree;
376}
377
378/**
Simon Glass722281b2023-06-01 10:22:42 -0600379 * oftree_dispose() - Dispose of an oftree
380 *
381 * This can be used to dispose of a tree that has been created (other than
382 * the control FDT which must not be disposed)
383 *
384 * @tree: Tree to dispose
385 */
386void oftree_dispose(oftree tree);
387
388/**
Simon Glass6dc1eb32025-01-10 17:00:27 -0700389 * ofnode_name_eq() - Check a node name ignoring its unit address
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530390 *
Simon Glass6dc1eb32025-01-10 17:00:27 -0700391 * @node: valid node to compared, which may have a unit address
392 * @name: name (without unit address) to compare with the node name
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100393 * Return: true if matches, false if it doesn't match
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530394 */
395bool ofnode_name_eq(ofnode node, const char *name);
396
397/**
Simon Glass9d300ce2025-01-10 17:00:28 -0700398 * ofnode_name_eq_unit() - Check a node name ignoring its unit address
399 *
400 * This is separate from ofnode_name_eq() to avoid code-size increase for
401 * boards which don't need this function
402 *
403 * @node: valid node to compared, which may have a unit address
404 * @name: name to compare with the node name. If this contains a unit
405 * address, it is matched, otherwise the unit address is ignored
406 * when searching for matches
407 * Return: true if matches, false if it doesn't match
408 */
409bool ofnode_name_eq_unit(ofnode node, const char *name);
410
411/**
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200412 * ofnode_read_u8() - Read a 8-bit integer from a property
413 *
414 * @node: valid node reference to read property from
415 * @propname: name of the property to read from
416 * @outp: place to put value (if found)
417 * Return: 0 if OK, -ve on error
418 */
419int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
420
421/**
422 * ofnode_read_u8_default() - Read a 8-bit integer from a property
423 *
424 * @node: valid node reference to read property from
425 * @propname: name of the property to read from
426 * @def: default value to return if the property has no value
427 * Return: property value, or @def if not found
428 */
429u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
430
431/**
432 * ofnode_read_u16() - Read a 16-bit integer from a property
433 *
434 * @node: valid node reference to read property from
435 * @propname: name of the property to read from
436 * @outp: place to put value (if found)
437 * Return: 0 if OK, -ve on error
438 */
439int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
440
441/**
442 * ofnode_read_u16_default() - Read a 16-bit integer from a property
443 *
444 * @node: valid node reference to read property from
445 * @propname: name of the property to read from
446 * @def: default value to return if the property has no value
447 * Return: property value, or @def if not found
448 */
449u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
450
451/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600452 * ofnode_read_u32() - Read a 32-bit integer from a property
453 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100454 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600455 * @propname: name of the property to read from
456 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100457 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600458 */
459int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
460
461/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200462 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
463 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100464 * @node: valid node reference to read property from
Dario Binacchi81d80b52020-03-29 18:04:41 +0200465 * @propname: name of the property to read from
466 * @index: index of the integer to return
467 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100468 * Return: 0 if OK, -ve on error
Dario Binacchi81d80b52020-03-29 18:04:41 +0200469 */
470int ofnode_read_u32_index(ofnode node, const char *propname, int index,
471 u32 *outp);
472
473/**
Michal Simek08a194e2023-08-25 11:37:46 +0200474 * ofnode_read_u64_index() - Read a 64-bit integer from a multi-value property
475 *
476 * @node: valid node reference to read property from
477 * @propname: name of the property to read from
478 * @index: index of the integer to return
479 * @outp: place to put value (if found)
480 * Return: 0 if OK, -ve on error
481 */
482int ofnode_read_u64_index(ofnode node, const char *propname, int index,
483 u64 *outp);
484
485/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600486 * ofnode_read_s32() - Read a 32-bit integer from a property
487 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100488 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600489 * @propname: name of the property to read from
490 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100491 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600492 */
493static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100494 s32 *outp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600495{
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100496 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600497}
498
499/**
500 * ofnode_read_u32_default() - Read a 32-bit integer from a property
501 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100502 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600503 * @propname: name of the property to read from
504 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100505 * Return: property value, or @def if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600506 */
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100507u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600508
509/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200510 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
511 * property
512 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100513 * @node: valid node reference to read property from
Dario Binacchi81d80b52020-03-29 18:04:41 +0200514 * @propname: name of the property to read from
515 * @index: index of the integer to return
516 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100517 * Return: property value, or @def if not found
Dario Binacchi81d80b52020-03-29 18:04:41 +0200518 */
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100519u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi81d80b52020-03-29 18:04:41 +0200520 u32 def);
521
522/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600523 * ofnode_read_s32_default() - Read a 32-bit integer from a property
524 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100525 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600526 * @propname: name of the property to read from
527 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100528 * Return: property value, or @def if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600529 */
530int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
531
532/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100533 * ofnode_read_u64() - Read a 64-bit integer from a property
534 *
535 * @node: valid node reference to read property from
536 * @propname: name of the property to read from
537 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100538 * Return: 0 if OK, -ve on error
Lukas Auerb03a60b2018-11-22 11:26:35 +0100539 */
540int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
541
542/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600543 * ofnode_read_u64_default() - Read a 64-bit integer from a property
544 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100545 * @node: valid node reference to read property from
Simon Glass9d54a7a2018-06-11 13:07:10 -0600546 * @propname: name of the property to read from
547 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100548 * Return: property value, or @def if not found
Simon Glass9d54a7a2018-06-11 13:07:10 -0600549 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200550u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600551
552/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700553 * ofnode_read_prop() - Read a property from a node
554 *
555 * @node: valid node reference to read property from
556 * @propname: name of the property to read
557 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100558 * if not found
559 * Return: property value, or NULL if there is no such property
Simon Glass0c2e9802020-01-27 08:49:44 -0700560 */
561const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
562
563/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600564 * ofnode_read_string() - Read a string from a property
565 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700566 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600567 * @propname: name of the property to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100568 * Return: string from property value, or NULL if there is no such property
Simon Glassc4fc5622017-05-18 20:08:58 -0600569 */
570const char *ofnode_read_string(ofnode node, const char *propname);
571
572/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600573 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600574 *
575 * @node: valid node reference to read property from
576 * @propname: name of the property to read
577 * @out_values: pointer to return value, modified only if return value is 0
578 * @sz: number of array elements to read
Simon Glasse3be5fc2022-09-06 20:27:18 -0600579 * Return: 0 on success, -EINVAL if the property does not exist,
580 * -ENODATA if property does not have a value, and -EOVERFLOW if the
581 * property data isn't large enough
Simon Glassc4fc5622017-05-18 20:08:58 -0600582 *
583 * Search for a property in a device node and read 32-bit value(s) from
Simon Glasse3be5fc2022-09-06 20:27:18 -0600584 * it.
Simon Glassc4fc5622017-05-18 20:08:58 -0600585 *
586 * The out_values is modified only if a valid u32 value can be decoded.
587 */
588int ofnode_read_u32_array(ofnode node, const char *propname,
589 u32 *out_values, size_t sz);
590
591/**
592 * ofnode_read_bool() - read a boolean value from a property
593 *
594 * @node: valid node reference to read property from
595 * @propname: name of property to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100596 * Return: true if property is present (meaning true), false if not present
Simon Glassc4fc5622017-05-18 20:08:58 -0600597 */
598bool ofnode_read_bool(ofnode node, const char *propname);
599
600/**
601 * ofnode_find_subnode() - find a named subnode of a parent node
602 *
603 * @node: valid reference to parent node
604 * @subnode_name: name of subnode to find
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100605 * Return: reference to subnode (which can be invalid if there is no such
Simon Glassc4fc5622017-05-18 20:08:58 -0600606 * subnode)
607 */
608ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
609
Simon Glass39f1d282020-12-16 17:25:06 -0700610#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass3ba929a2020-10-30 21:38:53 -0600611#include <asm/global_data.h>
612
Simon Glass39f1d282020-12-16 17:25:06 -0700613static inline bool ofnode_is_enabled(ofnode node)
614{
615 if (ofnode_is_np(node)) {
616 return of_device_is_available(ofnode_to_np(node));
617 } else {
618 return fdtdec_get_is_enabled(gd->fdt_blob,
619 ofnode_to_offset(node));
620 }
621}
622
623static inline ofnode ofnode_first_subnode(ofnode node)
624{
625 assert(ofnode_valid(node));
626 if (ofnode_is_np(node))
627 return np_to_ofnode(node.np->child);
628
629 return offset_to_ofnode(
630 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
631}
632
633static inline ofnode ofnode_next_subnode(ofnode node)
634{
635 assert(ofnode_valid(node));
636 if (ofnode_is_np(node))
637 return np_to_ofnode(node.np->sibling);
638
639 return offset_to_ofnode(
640 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
641}
642#else
643/**
644 * ofnode_is_enabled() - Checks whether a node is enabled.
645 * This looks for a 'status' property. If this exists, then returns true if
646 * the status is 'okay' and false otherwise. If there is no status property,
647 * it returns true on the assumption that anything mentioned should be enabled
648 * by default.
649 *
650 * @node: node to examine
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100651 * Return: false (not enabled) or true (enabled)
Simon Glass39f1d282020-12-16 17:25:06 -0700652 */
653bool ofnode_is_enabled(ofnode node);
654
Simon Glassc4fc5622017-05-18 20:08:58 -0600655/**
656 * ofnode_first_subnode() - find the first subnode of a parent node
657 *
658 * @node: valid reference to a valid parent node
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100659 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glassc4fc5622017-05-18 20:08:58 -0600660 * node has no subnodes)
661 */
662ofnode ofnode_first_subnode(ofnode node);
663
664/**
665 * ofnode_next_subnode() - find the next sibling of a subnode
666 *
667 * @node: valid reference to previous node (sibling)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100668 * Return: reference to the next subnode (which can be invalid if the node
Simon Glassc4fc5622017-05-18 20:08:58 -0600669 * has no more siblings)
670 */
671ofnode ofnode_next_subnode(ofnode node);
Simon Glass39f1d282020-12-16 17:25:06 -0700672#endif /* DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600673
674/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100675 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
676 *
677 * @node: valid node to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100678 * Return: ofnode reference of the parent node
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100679 */
680ofnode ofnode_get_parent(ofnode node);
681
682/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600683 * ofnode_get_name() - get the name of a node
684 *
685 * @node: valid node to look up
Simon Glass91d89a82022-09-06 20:27:15 -0600686 * Return: name of node (for the root node this is "")
Simon Glassc4fc5622017-05-18 20:08:58 -0600687 */
688const char *ofnode_get_name(ofnode node);
689
690/**
Marek Behúne897e3c2021-05-26 14:08:18 +0200691 * ofnode_get_path() - get the full path of a node
692 *
693 * @node: valid node to look up
694 * @buf: buffer to write the node path into
695 * @buflen: buffer size
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100696 * Return: 0 if OK, -ve on error
Marek Behúne897e3c2021-05-26 14:08:18 +0200697 */
698int ofnode_get_path(ofnode node, char *buf, int buflen);
699
700/**
Kever Yang37df0e02018-02-23 17:38:50 +0100701 * ofnode_get_by_phandle() - get ofnode from phandle
702 *
Simon Glass176dd432022-09-06 20:26:57 -0600703 * This uses the default (control) device tree
704 *
Kever Yang37df0e02018-02-23 17:38:50 +0100705 * @phandle: phandle to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100706 * Return: ofnode reference to the phandle
Kever Yang37df0e02018-02-23 17:38:50 +0100707 */
708ofnode ofnode_get_by_phandle(uint phandle);
709
710/**
Simon Glass95fd2092022-09-06 20:27:22 -0600711 * oftree_get_by_phandle() - get ofnode from phandle
712 *
713 * @tree: tree to use
714 * @phandle: phandle to look up
715 * Return: ofnode reference to the phandle
716 */
717ofnode oftree_get_by_phandle(oftree tree, uint phandle);
718
719/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600720 * ofnode_read_size() - read the size of a property
721 *
722 * @node: node to check
723 * @propname: property to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100724 * Return: size of property if present, or -EINVAL if not
Simon Glassc4fc5622017-05-18 20:08:58 -0600725 */
726int ofnode_read_size(ofnode node, const char *propname);
727
728/**
Keerthyd332e6e2019-04-24 17:19:53 +0530729 * ofnode_get_addr_size_index() - get an address/size from a node
730 * based on index
731 *
732 * This reads the register address/size from a node based on index
733 *
734 * @node: node to read from
735 * @index: Index of address to read (0 for first)
736 * @size: Pointer to size of the address
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100737 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthyd332e6e2019-04-24 17:19:53 +0530738 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100739fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index,
740 fdt_size_t *size);
Keerthyd332e6e2019-04-24 17:19:53 +0530741
742/**
Marek Behún177ab7f2021-05-26 14:08:17 +0200743 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
744 * based on index, without address
745 * translation
746 *
747 * This reads the register address/size from a node based on index.
748 * The resulting address is not translated. Useful for example for on-disk
749 * addresses.
750 *
751 * @node: node to read from
752 * @index: Index of address to read (0 for first)
753 * @size: Pointer to size of the address
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100754 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún177ab7f2021-05-26 14:08:17 +0200755 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100756fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
757 fdt_size_t *size);
Marek Behún177ab7f2021-05-26 14:08:17 +0200758
759/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600760 * ofnode_get_addr_index() - get an address from a node
761 *
762 * This reads the register address from a node
763 *
764 * @node: node to read from
765 * @index: Index of address to read (0 for first)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100766 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glass049ae1b2017-05-18 20:09:01 -0600767 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100768fdt_addr_t ofnode_get_addr_index(ofnode node, int index);
Simon Glass049ae1b2017-05-18 20:09:01 -0600769
770/**
771 * ofnode_get_addr() - get an address from a node
772 *
773 * This reads the register address from a node
774 *
775 * @node: node to read from
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100776 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glass049ae1b2017-05-18 20:09:01 -0600777 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100778fdt_addr_t ofnode_get_addr(ofnode node);
Simon Glass049ae1b2017-05-18 20:09:01 -0600779
780/**
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800781 * ofnode_get_size() - get size from a node
782 *
783 * This reads the register size from a node
784 *
785 * @node: node to read from
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100786 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800787 */
788fdt_size_t ofnode_get_size(ofnode node);
789
790/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600791 * ofnode_stringlist_search() - find a string in a string list and return index
792 *
793 * Note that it is possible for this function to succeed on property values
794 * that are not NUL-terminated. That's because the function will stop after
795 * finding the first occurrence of @string. This can for example happen with
796 * small-valued cell properties, such as #address-cells, when searching for
797 * the empty string.
798 *
799 * @node: node to check
800 * @propname: name of the property containing the string list
801 * @string: string to look up in the string list
802 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100803 * Return:
Simon Glassc4fc5622017-05-18 20:08:58 -0600804 * the index of the string in the list of strings
805 * -ENODATA if the property is not found
806 * -EINVAL on some other error
807 */
808int ofnode_stringlist_search(ofnode node, const char *propname,
809 const char *string);
810
811/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600812 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600813 *
814 * Note that this will successfully extract strings from properties with
815 * non-NUL-terminated values. For example on small-valued cell properties
816 * this function will return the empty string.
817 *
818 * If non-NULL, the length of the string (on success) or a negative error-code
819 * (on failure) will be stored in the integer pointer to by lenp.
820 *
821 * @node: node to check
822 * @propname: name of the property containing the string list
Simon Glass7a3a1672021-10-23 17:26:06 -0600823 * @index: index of the string to return (cannot be negative)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100824 * @outp: return location for the string
Simon Glassc4fc5622017-05-18 20:08:58 -0600825 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100826 * Return:
Simon Glass7a3a1672021-10-23 17:26:06 -0600827 * 0 if found or -ve error value if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600828 */
829int ofnode_read_string_index(ofnode node, const char *propname, int index,
830 const char **outp);
831
832/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600833 * ofnode_read_string_count() - find the number of strings in a string list
834 *
835 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100836 * @property: name of the property containing the string list
837 * Return:
Simon Glass5fdb0052017-06-12 06:21:28 -0600838 * number of strings in the list, or -ve error value if not found
839 */
840int ofnode_read_string_count(ofnode node, const char *property);
841
842/**
Simon Glass9580bfc2021-10-23 17:26:07 -0600843 * ofnode_read_string_list() - read a list of strings
844 *
845 * This produces a list of string pointers with each one pointing to a string
846 * in the string list. If the property does not exist, it returns {NULL}.
847 *
848 * The data is allocated and the caller is reponsible for freeing the return
849 * value (the list of string pointers). The strings themselves may not be
850 * changed as they point directly into the devicetree property.
851 *
852 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100853 * @property: name of the property containing the string list
Simon Glass9580bfc2021-10-23 17:26:07 -0600854 * @listp: returns an allocated, NULL-terminated list of strings if the return
855 * value is > 0, else is set to NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100856 * Return:
857 * number of strings in list, 0 if none, -ENOMEM if out of memory,
858 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass9580bfc2021-10-23 17:26:07 -0600859 */
860int ofnode_read_string_list(ofnode node, const char *property,
861 const char ***listp);
862
863/**
Christian Marangie810bcd2024-11-10 12:50:22 +0100864 * ofnode_parse_phandle() - Resolve a phandle property to an ofnode
865 *
866 * @node: node to check
867 * @phandle_name: Name of property holding a phandle value
868 * @index: For properties holding a table of phandles, this is the index into
869 * the table
870 * Return: ofnode that the phandle points to or ofnode_null() on error.
871 */
872ofnode ofnode_parse_phandle(ofnode node, const char *phandle_name,
873 int index);
874
875/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600876 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
877 *
878 * This function is useful to parse lists of phandles and their arguments.
879 * Returns 0 on success and fills out_args, on error returns appropriate
880 * errno value.
881 *
882 * Caller is responsible to call of_node_put() on the returned out_args->np
883 * pointer.
884 *
885 * Example:
886 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100887 * .. code-block::
Simon Glassc4fc5622017-05-18 20:08:58 -0600888 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100889 * phandle1: node1 {
890 * #list-cells = <2>;
891 * };
892 * phandle2: node2 {
893 * #list-cells = <1>;
894 * };
895 * node3 {
896 * list = <&phandle1 1 2 &phandle2 3>;
897 * };
Simon Glassc4fc5622017-05-18 20:08:58 -0600898 *
899 * To get a device_node of the `node2' node you may call this:
900 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
901 *
902 * @node: device tree node containing a list
903 * @list_name: property name that contains a list
904 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100905 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600906 * @index: index of a phandle to parse out
907 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100908 * Return:
909 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
910 * @list_name does not exist, -EINVAL if a phandle was not found,
911 * @cells_name could not be found, the arguments were truncated or there
912 * were too many arguments.
Simon Glassc4fc5622017-05-18 20:08:58 -0600913 */
914int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
915 const char *cells_name, int cell_count,
916 int index,
917 struct ofnode_phandle_args *out_args);
918
919/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200920 * ofnode_count_phandle_with_args() - Count number of phandle in a list
921 *
922 * This function is useful to count phandles into a list.
923 * Returns number of phandle on success, on error returns appropriate
924 * errno value.
925 *
926 * @node: device tree node containing a list
927 * @list_name: property name that contains a list
928 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100929 * @cell_count: Cell count to use if @cells_name is NULL
930 * Return:
931 * number of phandle on success, -ENOENT if @list_name does not exist,
932 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200933 */
934int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200935 const char *cells_name, int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200936
937/**
Christian Marangie810bcd2024-11-10 12:50:22 +0100938 * oftree_parse_phandle() - Resolve a phandle property to an ofnode
939 * from a root node
940 *
941 * @tree: device tree to use
942 * @node: node to check
943 * @phandle_name: Name of property holding a phandle value
944 * @index: For properties holding a table of phandles, this is the index into
945 * the table
946 * Return: ofnode that the phandle points to or ofnode_null() on error.
947 */
948ofnode oftree_parse_phandle(oftree tree, ofnode node, const char *phandle_name,
949 int index);
950
951/**
Christian Marangi98715ac2024-11-10 12:50:20 +0100952 * oftree_parse_phandle_with_args() - Find a node pointed by phandle in a list
953 * from a root node
954 *
955 * This function is useful to parse lists of phandles and their arguments.
956 * Returns 0 on success and fills out_args, on error returns appropriate
957 * errno value.
958 *
959 * Caller is responsible to call of_node_put() on the returned out_args->np
960 * pointer.
961 *
962 * Example:
963 *
964 * .. code-block::
965 *
966 * phandle1: node1 {
967 * #list-cells = <2>;
968 * };
969 * phandle2: node2 {
970 * #list-cells = <1>;
971 * };
972 * node3 {
973 * list = <&phandle1 1 2 &phandle2 3>;
974 * };
975 *
976 * To get a device_node of the `node2' node you may call this:
977 * oftree_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
978 *
979 * @tree: device tree to use
980 * @node: device tree node containing a list
981 * @list_name: property name that contains a list
982 * @cells_name: property name that specifies phandles' arguments count
983 * @cell_count: Cell count to use if @cells_name is NULL
984 * @index: index of a phandle to parse out
985 * @out_args: optional pointer to output arguments structure (will be filled)
986 * Return:
987 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
988 * @list_name does not exist, -EINVAL if a phandle was not found,
989 * @cells_name could not be found, the arguments were truncated or there
990 * were too many arguments.
991 */
992int oftree_parse_phandle_with_args(oftree tree, ofnode node, const char *list_name,
993 const char *cells_name, int cell_count,
994 int index,
995 struct ofnode_phandle_args *out_args);
996
997/**
998 * oftree_count_phandle_with_args() - Count number of phandle in a list
999 * from a root node
1000 *
1001 * This function is useful to count phandles into a list.
1002 * Returns number of phandle on success, on error returns appropriate
1003 * errno value.
1004 *
1005 * @tree: device tree to use
1006 * @node: device tree node containing a list
1007 * @list_name: property name that contains a list
1008 * @cells_name: property name that specifies phandles' arguments count
1009 * @cell_count: Cell count to use if @cells_name is NULL
1010 * Return:
1011 * number of phandle on success, -ENOENT if @list_name does not exist,
1012 * -EINVAL if a phandle was not found, @cells_name could not be found.
1013 */
1014int oftree_count_phandle_with_args(oftree tree, ofnode node, const char *list_name,
1015 const char *cells_name, int cell_count);
1016
1017/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001018 * ofnode_path() - find a node by full path
1019 *
Simon Glassef75c592022-07-30 15:52:08 -06001020 * This uses the control FDT.
1021 *
Simon Glassc4fc5622017-05-18 20:08:58 -06001022 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001023 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glassc4fc5622017-05-18 20:08:58 -06001024 */
1025ofnode ofnode_path(const char *path);
1026
1027/**
Simon Glass45ae59d2022-09-06 20:27:24 -06001028 * oftree_path() - find a node by full path from a root node
Simon Glassef75c592022-07-30 15:52:08 -06001029 *
1030 * @tree: Device tree to use
1031 * @path: Full path to node, e.g. "/bus/spi@1"
1032 * Return: reference to the node found. Use ofnode_valid() to check if it exists
1033 */
Simon Glass45ae59d2022-09-06 20:27:24 -06001034ofnode oftree_path(oftree tree, const char *path);
Simon Glassef75c592022-07-30 15:52:08 -06001035
1036/**
Simon Glass45ae59d2022-09-06 20:27:24 -06001037 * oftree_root() - get the root node of a tree
1038 *
1039 * @tree: Device tree to use
1040 * Return: reference to the root node
1041 */
1042ofnode oftree_root(oftree tree);
1043
1044/**
Simon Glasse09223c2020-01-27 08:49:46 -07001045 * ofnode_read_chosen_prop() - get the value of a chosen property
1046 *
Simon Glass45ae59d2022-09-06 20:27:24 -06001047 * This looks for a property within the /chosen node and returns its value.
1048 *
1049 * This only works with the control FDT.
Simon Glasse09223c2020-01-27 08:49:46 -07001050 *
1051 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001052 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glasse09223c2020-01-27 08:49:46 -07001053 * returns NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001054 * Return: property value if found, else NULL
Simon Glasse09223c2020-01-27 08:49:46 -07001055 */
1056const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
1057
1058/**
Simon Glassf3455962020-01-27 08:49:43 -07001059 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -06001060 *
Simon Glassf3455962020-01-27 08:49:43 -07001061 * This looks for a property within the /chosen node and returns its value,
1062 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -06001063 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001064 * This only works with the control FDT.
1065 *
Simon Glassc4fc5622017-05-18 20:08:58 -06001066 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001067 * Return: string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -06001068 */
Simon Glassf3455962020-01-27 08:49:43 -07001069const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -06001070
1071/**
Simon Glassc99ba912020-01-27 08:49:42 -07001072 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -06001073 *
Simon Glassc99ba912020-01-27 08:49:42 -07001074 * This looks up a named property in the chosen node and uses that as a path to
1075 * look up a code.
1076 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001077 * This only works with the control FDT.
1078 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001079 * @propname: Property name to look for
1080 * Return: the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -06001081 */
Simon Glassc99ba912020-01-27 08:49:42 -07001082ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -06001083
Michal Simek92a88622020-07-28 12:51:08 +02001084/**
Algapally Santosh Sagardf178992023-09-21 16:50:43 +05301085 * ofnode_read_baud() - get the baudrate from string value of chosen property
Michal Simek92a88622020-07-28 12:51:08 +02001086 *
Algapally Santosh Sagardf178992023-09-21 16:50:43 +05301087 * This looks for stdout-path property within the /chosen node and parses its
1088 * value to return baudrate.
Michal Simek92a88622020-07-28 12:51:08 +02001089 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001090 * This only works with the control FDT.
1091 *
Algapally Santosh Sagardf178992023-09-21 16:50:43 +05301092 * Return: baudrate value if found, else -ve error code
1093 */
1094int ofnode_read_baud(void);
1095
1096/**
1097 * ofnode_read_aliases_prop() - get the value of a aliases property
1098 *
1099 * This looks for a property within the /aliases node and returns its value
1100 *
Michal Simek92a88622020-07-28 12:51:08 +02001101 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001102 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek92a88622020-07-28 12:51:08 +02001103 * returns NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001104 * Return: property value if found, else NULL
Michal Simek92a88622020-07-28 12:51:08 +02001105 */
1106const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
1107
1108/**
1109 * ofnode_get_aliases_node() - get a referenced node from the aliases node
1110 *
1111 * This looks up a named property in the aliases node and uses that as a path to
1112 * look up a code.
1113 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001114 * This only works with the control FDT.
1115 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001116 * @propname: Property name to look for
1117 * Return: the referenced node if present, else ofnode_null()
Michal Simek92a88622020-07-28 12:51:08 +02001118 */
1119ofnode ofnode_get_aliases_node(const char *propname);
1120
Simon Glassc4fc5622017-05-18 20:08:58 -06001121struct display_timing;
1122/**
1123 * ofnode_decode_display_timing() - decode display timings
1124 *
1125 * Decode display timings from the supplied 'display-timings' node.
1126 * See doc/device-tree-bindings/video/display-timing.txt for binding
1127 * information.
1128 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001129 * @node: 'display-timing' node containing the timing subnodes
1130 * @index: Index number to read (0=first timing subnode)
1131 * @config: Place to put timings
1132 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glassc4fc5622017-05-18 20:08:58 -06001133 */
1134int ofnode_decode_display_timing(ofnode node, int index,
1135 struct display_timing *config);
1136
1137/**
Nikhil M Jainff407062023-01-31 15:35:14 +05301138 * ofnode_decode_panel_timing() - decode display timings
1139 *
1140 * Decode panel timings from the supplied 'panel-timings' node.
1141 *
1142 * @node: 'display-timing' node containing the timing subnodes
1143 * @config: Place to put timings
1144 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
1145 */
1146int ofnode_decode_panel_timing(ofnode node,
1147 struct display_timing *config);
1148
1149/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001150 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -06001151 *
1152 * @node: node to read
1153 * @propname: property to read
1154 * @lenp: place to put length on success
Simon Glass86ef3992023-09-26 08:14:44 -06001155 * Return: pointer to property value, or NULL if not found or empty
Simon Glassc4fc5622017-05-18 20:08:58 -06001156 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +09001157const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -06001158
1159/**
Simon Glass86ef3992023-09-26 08:14:44 -06001160 * ofnode_has_property() - check if a node has a named property
1161 *
1162 * @node: node to read
1163 * @propname: property to read
1164 * Return: true if the property exists in the node, false if not
1165 */
1166bool ofnode_has_property(ofnode node, const char *propname);
1167
1168/**
Simon Glassfec058d2022-09-06 20:27:13 -06001169 * ofnode_first_property()- get the reference of the first property
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001170 *
1171 * Get reference to the first property of the node, it is used to iterate
Simon Glassd0aff8b2022-09-06 20:27:14 -06001172 * and read all the property with ofprop_get_property().
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001173 *
1174 * @node: node to read
1175 * @prop: place to put argument reference
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001176 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001177 */
Simon Glassfec058d2022-09-06 20:27:13 -06001178int ofnode_first_property(ofnode node, struct ofprop *prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001179
1180/**
Simon Glassfec058d2022-09-06 20:27:13 -06001181 * ofnode_next_property() - get the reference of the next property
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001182 *
1183 * Get reference to the next property of the node, it is used to iterate
Simon Glassd0aff8b2022-09-06 20:27:14 -06001184 * and read all the property with ofprop_get_property().
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001185 *
1186 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001187 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001188 */
Simon Glassfec058d2022-09-06 20:27:13 -06001189int ofnode_next_property(struct ofprop *prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001190
1191/**
Simon Glass4caa79a2022-09-06 20:27:16 -06001192 * ofnode_for_each_prop() - iterate over all properties of a node
1193 *
1194 * @prop: struct ofprop
1195 * @node: node (lvalue, ofnode)
1196 *
1197 * This is a wrapper around a for loop and is used like this::
1198 *
1199 * ofnode node;
1200 * struct ofprop prop;
1201 *
1202 * ofnode_for_each_prop(prop, node) {
1203 * ...use prop...
1204 * }
1205 *
1206 * Note that this is implemented as a macro and @prop is used as
1207 * iterator in the loop. The parent variable can be a constant or even a
1208 * literal.
1209 */
1210#define ofnode_for_each_prop(prop, node) \
1211 for (ofnode_first_property(node, &prop); \
1212 ofprop_valid(&prop); \
1213 ofnode_next_property(&prop))
1214
1215/**
Simon Glassd0aff8b2022-09-06 20:27:14 -06001216 * ofprop_get_property() - get a pointer to the value of a property
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001217 *
1218 * Get value for the property identified by the provided reference.
1219 *
1220 * @prop: reference on property
1221 * @propname: If non-NULL, place to property name on success,
Simon Glassd0aff8b2022-09-06 20:27:14 -06001222 * @lenp: If non-NULL, place to put length on success, or error code on failure
1223 * Return: pointer to property, or NULL if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001224 */
Simon Glassd0aff8b2022-09-06 20:27:14 -06001225const void *ofprop_get_property(const struct ofprop *prop,
1226 const char **propname, int *lenp);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001227
1228/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001229 * ofnode_get_addr_size() - get address and size from a property
1230 *
1231 * This does no address translation. It simply reads an property that contains
1232 * an address and a size value, one after the other.
1233 *
1234 * @node: node to read from
1235 * @propname: property to read
1236 * @sizep: place to put size value (on success)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001237 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glassc4fc5622017-05-18 20:08:58 -06001238 */
Johan Jonker9f6971f2023-03-13 01:30:33 +01001239fdt_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1240 fdt_size_t *sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -06001241
1242/**
1243 * ofnode_read_u8_array_ptr() - find an 8-bit array
1244 *
1245 * Look up a property in a node and return a pointer to its contents as a
1246 * byte array of given length. The property must have at least enough data
1247 * for the array (count bytes). It may have more, but this will be ignored.
1248 * The data is not copied.
1249 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001250 * @node: node to examine
1251 * @propname: name of property to find
1252 * @sz: number of array elements
1253 * Return:
1254 * pointer to byte array if found, or NULL if the property is not found or
1255 * there is not enough data
Simon Glassc4fc5622017-05-18 20:08:58 -06001256 */
1257const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1258 size_t sz);
1259
1260/**
1261 * ofnode_read_pci_addr() - look up a PCI address
1262 *
1263 * Look at an address property in a node and return the PCI address which
1264 * corresponds to the given type in the form of fdt_pci_addr.
1265 * The property must hold one fdt_pci_addr with a lengh.
1266 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001267 * @node: node to examine
1268 * @type: pci address type (FDT_PCI_SPACE_xxx)
1269 * @propname: name of property to find
1270 * @addr: returns pci address in the form of fdt_pci_addr
Simon Glass4289c262023-09-26 08:14:58 -06001271 * @size: if non-null, returns register-space size
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001272 * Return:
1273 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1274 * format of the property was invalid, -ENXIO if the requested
1275 * address type was not found
Simon Glassc4fc5622017-05-18 20:08:58 -06001276 */
1277int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
Simon Glass4289c262023-09-26 08:14:58 -06001278 const char *propname, struct fdt_pci_addr *addr,
1279 fdt_size_t *size);
Simon Glassc4fc5622017-05-18 20:08:58 -06001280
1281/**
Bin Mengfa157712018-08-03 01:14:35 -07001282 * ofnode_read_pci_vendev() - look up PCI vendor and device id
1283 *
1284 * Look at the compatible property of a device node that represents a PCI
1285 * device and extract pci vendor id and device id from it.
1286 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001287 * @node: node to examine
1288 * @vendor: vendor id of the pci device
1289 * @device: device id of the pci device
1290 * Return: 0 if ok, negative on error
Bin Mengfa157712018-08-03 01:14:35 -07001291 */
1292int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1293
1294/**
Michal Simeka253c3b2022-02-23 15:45:40 +01001295 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1296 *
1297 * Look at the compatible property of a device node that represents a eth phy
1298 * device and extract phy vendor id and device id from it.
1299 *
Heinrich Schuchardtc9ae4a82022-03-24 16:22:32 +01001300 * @node: node to examine
1301 * @vendor: vendor id of the eth phy device
1302 * @device: device id of the eth phy device
1303 * Return: 0 if ok, negative on error
Michal Simeka253c3b2022-02-23 15:45:40 +01001304 */
1305int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1306
1307/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001308 * ofnode_read_addr_cells() - Get the number of address cells for a node
1309 *
1310 * This walks back up the tree to find the closest #address-cells property
1311 * which controls the given node.
1312 *
1313 * @node: Node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001314 * Return: number of address cells this node uses
Simon Glassc4fc5622017-05-18 20:08:58 -06001315 */
1316int ofnode_read_addr_cells(ofnode node);
1317
1318/**
1319 * ofnode_read_size_cells() - Get the number of size cells for a node
1320 *
1321 * This walks back up the tree to find the closest #size-cells property
1322 * which controls the given node.
1323 *
1324 * @node: Node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001325 * Return: number of size cells this node uses
Simon Glassc4fc5622017-05-18 20:08:58 -06001326 */
1327int ofnode_read_size_cells(ofnode node);
1328
1329/**
Simon Glass4191dc12017-06-12 06:21:31 -06001330 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1331 *
1332 * This function matches fdt_address_cells().
1333 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001334 * @node: Node to check
1335 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass4191dc12017-06-12 06:21:31 -06001336 */
1337int ofnode_read_simple_addr_cells(ofnode node);
1338
1339/**
1340 * ofnode_read_simple_size_cells() - Get the size cells property in a node
1341 *
1342 * This function matches fdt_size_cells().
1343 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001344 * @node: Node to check
1345 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass4191dc12017-06-12 06:21:31 -06001346 */
1347int ofnode_read_simple_size_cells(ofnode node);
1348
1349/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001350 * ofnode_pre_reloc() - check if a node should be bound before relocation
1351 *
1352 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1353 * via special device tree properties.
1354 *
1355 * Before relocation this function can be used to check if nodes are required
1356 * in either SPL or TPL stages.
1357 *
1358 * After relocation and jumping into the real U-Boot binary it is possible to
1359 * determine if a node was bound in one of SPL/TPL stages.
1360 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +02001361 * There are 4 settings currently in use
Jonas Karlmanc43411b2023-08-20 22:03:18 +00001362 * - bootph-some-ram: U-Boot proper pre-relocation phase
Simon Glassfc1aa352023-02-13 08:56:34 -07001363 * - bootph-all: all phases
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001364 * Existing platforms only use it to indicate nodes needed in
Simon Glassfc1aa352023-02-13 08:56:34 -07001365 * SPL. Should probably be replaced by bootph-pre-ram for new platforms.
Jonas Karlmanc43411b2023-08-20 22:03:18 +00001366 * - bootph-pre-ram: SPL phase
1367 * - bootph-pre-sram: TPL phase
Simon Glassc4fc5622017-05-18 20:08:58 -06001368 *
1369 * @node: node to check
Jonas Karlmanc43411b2023-08-20 22:03:18 +00001370 * Return: true if node should be or was bound, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -06001371 */
1372bool ofnode_pre_reloc(ofnode node);
1373
Simon Glassa8173d62018-06-11 13:07:12 -06001374/**
1375 * ofnode_read_resource() - Read a resource from a node
1376 *
1377 * Read resource information from a node at the given index
1378 *
1379 * @node: Node to read from
1380 * @index: Index of resource to read (0 = first)
1381 * @res: Returns resource that was read, on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001382 * Return: 0 if OK, -ve on error
Simon Glassa8173d62018-06-11 13:07:12 -06001383 */
Simon Glassf7bfcc42017-07-25 08:29:55 -06001384int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -06001385
1386/**
1387 * ofnode_read_resource_byname() - Read a resource from a node by name
1388 *
1389 * Read resource information from a node matching the given name. This uses a
1390 * 'reg-names' string list property with the names matching the associated
1391 * 'reg' property list.
1392 *
1393 * @node: Node to read from
1394 * @name: Name of resource to read
1395 * @res: Returns resource that was read, on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001396 * Return: 0 if OK, -ve on error
Simon Glassa8173d62018-06-11 13:07:12 -06001397 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001398int ofnode_read_resource_byname(ofnode node, const char *name,
1399 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -06001400
Simon Glass28529762017-08-05 15:45:54 -06001401/**
Simon Glass954eeae2018-06-11 13:07:13 -06001402 * ofnode_by_compatible() - Find the next compatible node
1403 *
1404 * Find the next node after @from that is compatible with @compat
1405 *
1406 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1407 * @compat: Compatible string to match
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001408 * Return: ofnode found, or ofnode_null() if none
Simon Glass954eeae2018-06-11 13:07:13 -06001409 */
1410ofnode ofnode_by_compatible(ofnode from, const char *compat);
1411
1412/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001413 * ofnode_by_prop_value() - Find the next node with given property value
1414 *
1415 * Find the next node after @from that has a @propname with a value
1416 * @propval and a length @proplen.
1417 *
Simon Glass37dcd912022-09-06 20:27:23 -06001418 * @from: ofnode to start from. Use ofnode_null() to start at the
1419 * beginning, or the return value from oftree_root() to start at the first
1420 * child of the root
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001421 * @propname: property name to check
1422 * @propval: property value to search for
1423 * @proplen: length of the value in propval
1424 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001425 */
1426ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1427 const void *propval, int proplen);
1428
1429/**
Simon Glass28529762017-08-05 15:45:54 -06001430 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1431 *
1432 * @node: child node (ofnode, lvalue)
1433 * @parent: parent node (ofnode)
1434 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001435 * This is a wrapper around a for loop and is used like so::
Simon Glass28529762017-08-05 15:45:54 -06001436 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001437 * ofnode node;
1438 * ofnode_for_each_subnode(node, parent) {
1439 * Use node
1440 * ...
1441 * }
Simon Glass28529762017-08-05 15:45:54 -06001442 *
1443 * Note that this is implemented as a macro and @node is used as
1444 * iterator in the loop. The parent variable can be a constant or even a
1445 * literal.
1446 */
1447#define ofnode_for_each_subnode(node, parent) \
1448 for (node = ofnode_first_subnode(parent); \
1449 ofnode_valid(node); \
1450 node = ofnode_next_subnode(node))
1451
Mario Sixaefac062018-01-15 11:07:19 +01001452/**
Michael Wallea7b9df22021-10-15 15:15:17 +02001453 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1454 * compatible string
1455 *
1456 * @node: child node (ofnode, lvalue)
1457 * @compat: compatible string to match
1458 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001459 * This is a wrapper around a for loop and is used like so::
Michael Wallea7b9df22021-10-15 15:15:17 +02001460 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001461 * ofnode node;
1462 * ofnode_for_each_compatible_node(node, parent, compatible) {
1463 * Use node
1464 * ...
1465 * }
Michael Wallea7b9df22021-10-15 15:15:17 +02001466 *
1467 * Note that this is implemented as a macro and @node is used as
1468 * iterator in the loop.
1469 */
1470#define ofnode_for_each_compatible_node(node, compat) \
1471 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1472 ofnode_valid(node); \
1473 node = ofnode_by_compatible(node, compat))
1474
1475/**
developerd93c8b42020-05-02 11:35:09 +02001476 * ofnode_get_child_count() - get the child count of a ofnode
1477 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001478 * @parent: valid node to get its child count
1479 * Return: the number of subnodes
developerd93c8b42020-05-02 11:35:09 +02001480 */
1481int ofnode_get_child_count(ofnode parent);
1482
1483/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001484 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +01001485 *
1486 * Translate an address from the device-tree into a CPU physical address. This
1487 * function walks up the tree and applies the various bus mappings along the
1488 * way.
1489 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001490 * @node: Device tree node giving the context in which to translate the address
Mario Sixaefac062018-01-15 11:07:19 +01001491 * @in_addr: pointer to the address to translate
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001492 * Return: the translated address; OF_BAD_ADDR on error
Mario Sixaefac062018-01-15 11:07:19 +01001493 */
1494u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001495
1496/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001497 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1498 *
1499 * Translate a DMA address from the device-tree into a CPU physical address.
1500 * This function walks up the tree and applies the various bus mappings along
1501 * the way.
1502 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001503 * @node: Device tree node giving the context in which to translate the
1504 * DMA address
Fabien Dessenne22236e02019-05-31 15:11:30 +02001505 * @in_addr: pointer to the DMA address to translate
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001506 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne22236e02019-05-31 15:11:30 +02001507 */
1508u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1509
1510/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001511 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1512 *
1513 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1514 * cpu->bus address translations
1515 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001516 * @node: Device tree node
1517 * @cpu: Pointer to variable storing the range's cpu address
1518 * @bus: Pointer to variable storing the range's bus address
1519 * @size: Pointer to variable storing the range's size
1520 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001521 */
1522int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1523 u64 *size);
1524
1525/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001526 * ofnode_device_is_compatible() - check if the node is compatible with compat
1527 *
1528 * This allows to check whether the node is comaptible with the compat.
1529 *
1530 * @node: Device tree node for which compatible needs to be verified.
1531 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001532 * Return: true if OK, false if the compatible is not found
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001533 */
1534int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +02001535
1536/**
1537 * ofnode_write_prop() - Set a property of a ofnode
1538 *
Simon Glass17abed02022-09-06 20:27:32 -06001539 * Note that if @copy is false, the value passed to the function is *not*
1540 * allocated by the function itself, but must be allocated by the caller if
1541 * necessary. However it does allocate memory for the property struct and name.
Mario Six047dafc2018-06-26 08:46:48 +02001542 *
1543 * @node: The node for whose property should be set
1544 * @propname: The name of the property to set
Mario Six047dafc2018-06-26 08:46:48 +02001545 * @value: The new value of the property (must be valid prior to calling
1546 * the function)
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001547 * @len: The length of the new value of the property
Simon Glass17abed02022-09-06 20:27:32 -06001548 * @copy: true to allocate memory for the value. This only has any effect with
1549 * live tree, since flat tree handles this automatically. It allows a
1550 * node's value to be written to the tree, without requiring that the
1551 * caller allocate it
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001552 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001553 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001554int ofnode_write_prop(ofnode node, const char *propname, const void *value,
Simon Glass17abed02022-09-06 20:27:32 -06001555 int len, bool copy);
Mario Six047dafc2018-06-26 08:46:48 +02001556
1557/**
1558 * ofnode_write_string() - Set a string property of a ofnode
1559 *
1560 * Note that the value passed to the function is *not* allocated by the
1561 * function itself, but must be allocated by the caller if necessary.
1562 *
1563 * @node: The node for whose string property should be set
1564 * @propname: The name of the string property to set
1565 * @value: The new value of the string property (must be valid prior to
1566 * calling the function)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001567 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001568 */
1569int ofnode_write_string(ofnode node, const char *propname, const char *value);
1570
1571/**
Simon Glassd28e31e2022-07-30 15:52:14 -06001572 * ofnode_write_u32() - Set an integer property of an ofnode
1573 *
1574 * @node: The node for whose string property should be set
1575 * @propname: The name of the string property to set
1576 * @value: The new value of the 32-bit integer property
1577 * Return: 0 if successful, -ve on error
1578 */
1579int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1580
1581/**
Simon Glassc681e092023-09-26 08:14:45 -06001582 * ofnode_write_u64() - Set an integer property of an ofnode
1583 *
1584 * @node: The node for whose string property should be set
1585 * @propname: The name of the string property to set
1586 * @value: The new value of the 64-bit integer property
1587 * Return: 0 if successful, -ve on error
1588 */
1589int ofnode_write_u64(ofnode node, const char *propname, u64 value);
1590
1591/**
Simon Glass86ef3992023-09-26 08:14:44 -06001592 * ofnode_write_bool() - Set a boolean property of an ofnode
1593 *
1594 * This either adds or deleted a property with a zero-length value
1595 *
1596 * @node: The node for whose string property should be set
1597 * @propname: The name of the string property to set
1598 * @value: The new value of the boolean property
1599 * Return: 0 if successful, -ve on error
1600 */
1601int ofnode_write_bool(ofnode node, const char *propname, bool value);
1602
1603/**
1604 * ofnode_delete_prop() - Delete a property
1605 *
1606 * @node: Node containing the property to delete
1607 * @propname: Name of property to delete
1608 * Return: 0 if successful, -ve on error
1609 */
1610int ofnode_delete_prop(ofnode node, const char *propname);
1611
1612/**
Mario Six047dafc2018-06-26 08:46:48 +02001613 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1614 * ofnode
1615 *
1616 * This function effectively sets the node's "status" property to either "okay"
1617 * or "disable", hence making it available for driver model initialization or
1618 * not.
1619 *
1620 * @node: The node to enable
1621 * @value: Flag that tells the function to either disable or enable the
1622 * node
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001623 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001624 */
1625int ofnode_set_enabled(ofnode node, bool value);
1626
Simon Glass0034d962021-08-07 07:24:01 -06001627/**
Sean Anderson9b3a6392022-03-28 18:14:37 -04001628 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1629 *
1630 * This function parses PHY handle from the Ethernet controller's ofnode
1631 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1632 *
1633 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1634 * if the result to that is true, this function should not be called.
1635 *
1636 * @eth_node: ofnode belonging to the Ethernet controller
1637 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1638 */
1639ofnode ofnode_get_phy_node(ofnode eth_node);
1640
1641/**
1642 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1643 *
1644 * This function parses the "phy-mode" / "phy-connection-type" property and
1645 * returns the corresponding PHY interface type.
1646 *
1647 * @mac_node: ofnode containing the property
1648 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1649 * error
1650 */
1651phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1652
1653#if CONFIG_IS_ENABLED(DM)
1654/**
Simon Glass0034d962021-08-07 07:24:01 -06001655 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1656 *
1657 * This reads a property from the /config node of the devicetree.
1658 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001659 * This only works with the control FDT.
1660 *
1661 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass0034d962021-08-07 07:24:01 -06001662 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001663 * @prop_name: property name to look up
1664 * Return: true, if it exists, false if not
Simon Glass0034d962021-08-07 07:24:01 -06001665 */
1666bool ofnode_conf_read_bool(const char *prop_name);
1667
1668/**
1669 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1670 *
1671 * This reads a property from the /config node of the devicetree.
1672 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001673 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass0034d962021-08-07 07:24:01 -06001674 *
1675 * @prop_name: property name to look up
1676 * @default_val: default value to return if the property is not found
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001677 * Return: integer value, if found, or @default_val if not
Simon Glass0034d962021-08-07 07:24:01 -06001678 */
1679int ofnode_conf_read_int(const char *prop_name, int default_val);
1680
1681/**
1682 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1683 *
1684 * This reads a property from the /config node of the devicetree.
1685 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001686 * This only works with the control FDT.
1687 *
1688 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass0034d962021-08-07 07:24:01 -06001689 *
1690 * @prop_name: property name to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001691 * Return: string value, if found, or NULL if not
Simon Glass0034d962021-08-07 07:24:01 -06001692 */
1693const char *ofnode_conf_read_str(const char *prop_name);
1694
Michal Simek43c42bd2023-08-31 08:59:05 +02001695/**
Christian Marangi72544732024-10-01 14:24:35 +02001696 * ofnode_options_read_bool() - Read a boolean value from the U-Boot options
1697 *
1698 * This reads a property from the /options/u-boot/ node of the devicetree.
1699 *
1700 * This only works with the control FDT.
1701 *
1702 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
1703 *
1704 * @prop_name: property name to look up
1705 * Return: true, if it exists, false if not
1706 */
1707bool ofnode_options_read_bool(const char *prop_name);
1708
1709/**
1710 * ofnode_options_read_int() - Read an integer value from the U-Boot options
1711 *
1712 * This reads a property from the /options/u-boot/ node of the devicetree.
1713 *
1714 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
1715 *
1716 * @prop_name: property name to look up
1717 * @default_val: default value to return if the property is not found
1718 * Return: integer value, if found, or @default_val if not
1719 */
1720int ofnode_options_read_int(const char *prop_name, int default_val);
1721
1722/**
1723 * ofnode_options_read_str() - Read a string value from the U-Boot options
1724 *
1725 * This reads a property from the /options/u-boot/ node of the devicetree.
1726 *
1727 * This only works with the control FDT.
1728 *
1729 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
1730 *
1731 * @prop_name: property name to look up
1732 * Return: string value, if found, or NULL if not
1733 */
1734const char *ofnode_options_read_str(const char *prop_name);
1735
1736/**
Christian Marangicdaa9952024-11-10 12:50:24 +01001737 * ofnode_options_get_by_phandle() - Get a ofnode from phandle from the U-Boot options
1738 *
1739 * This reads a property from the /options/u-boot/ node of the devicetree.
1740 *
1741 * This only works with the control FDT.
1742 *
1743 * See dtschema/schemas/options/u-boot.yaml in dt-schema project for bindings
1744 *
1745 * @prop_name: property name to look up
1746 * @nodep: pointer to ofnode where node is stored
1747 * Return: 0, if found, or negative error if not
1748 */
1749int ofnode_options_get_by_phandle(const char *prop_name, ofnode *nodep);
1750
1751/**
Michal Simek43c42bd2023-08-31 08:59:05 +02001752 * ofnode_read_bootscript_address() - Read bootscr-address or bootscr-ram-offset
1753 *
1754 * @bootscr_address: pointer to 64bit address where bootscr-address property value
1755 * is stored
1756 * @bootscr_offset: pointer to 64bit offset address where bootscr-ram-offset
1757 * property value is stored
1758 *
1759 * This reads a bootscr-address or bootscr-ram-offset property from
1760 * the /options/u-boot/ node of the devicetree. bootscr-address holds the full
1761 * address of the boot script file. bootscr-ram-offset holds the boot script
1762 * file offset from the start of the ram base address. When bootscr-address is
1763 * defined, bootscr-ram-offset property is ignored.
1764 *
1765 * This only works with the control FDT.
1766 *
1767 * Return: 0 if OK, -EINVAL if property is not found.
1768 */
1769int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset);
1770
Michal Simek6a7c1ce2023-08-31 09:04:27 +02001771/**
1772 * ofnode_read_bootscript_flash() - Read bootscr-flash-offset/size
1773 *
1774 * @bootscr_flash_offset: pointer to 64bit offset where bootscr-flash-offset
1775 * property value is stored
1776 * @bootscr_flash_size: pointer to 64bit size where bootscr-flash-size property
1777 * value is stored
1778 *
1779 * This reads a bootscr-flash-offset and bootscr-flash-size properties from
1780 * the /options/u-boot/ node of the devicetree. bootscr-flash-offset holds
1781 * the offset of the boot script file from start of flash. bootscr-flash-size
1782 * holds the boot script size in flash. When bootscr-flash-size is not defined,
1783 * bootscr-flash-offset property is cleaned.
1784 *
1785 * This only works with the control FDT.
1786 *
1787 * Return: 0 if OK, -EINVAL if property is not found or incorrect.
1788 */
1789int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
1790 u64 *bootscr_flash_size);
1791
Sean Anderson9b3a6392022-03-28 18:14:37 -04001792#else /* CONFIG_DM */
1793static inline bool ofnode_conf_read_bool(const char *prop_name)
1794{
1795 return false;
1796}
Marek Behúnf4f1ddc2022-04-07 00:32:57 +02001797
Sean Anderson9b3a6392022-03-28 18:14:37 -04001798static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1799{
1800 return default_val;
1801}
1802
1803static inline const char *ofnode_conf_read_str(const char *prop_name)
1804{
1805 return NULL;
1806}
Simon Glass56bc3322022-09-06 20:27:02 -06001807
Michal Simek43c42bd2023-08-31 08:59:05 +02001808static inline int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset)
1809{
1810 return -EINVAL;
1811}
1812
Michal Simek6a7c1ce2023-08-31 09:04:27 +02001813static inline int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
1814 u64 *bootscr_flash_size)
1815{
1816 return -EINVAL;
1817}
1818
Sean Anderson9b3a6392022-03-28 18:14:37 -04001819#endif /* CONFIG_DM */
Marek Behúnbc194772022-04-07 00:33:01 +02001820
Simon Glass56bc3322022-09-06 20:27:02 -06001821/**
1822 * of_add_subnode() - add a new subnode to a node
1823 *
1824 * @parent: parent node to add to
Simon Glassbdbd96a2025-01-10 17:00:11 -07001825 * @name: name of subnode (allocated by this function)
Simon Glass56bc3322022-09-06 20:27:02 -06001826 * @nodep: returns pointer to new subnode (valid if the function returns 0
1827 * or -EEXIST)
1828 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1829 * -ve on other error
1830 */
1831int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1832
Simon Glass7a7229a2022-09-06 20:27:33 -06001833/**
1834 * ofnode_copy_props() - copy all properties from one node to another
1835 *
Simon Glass68164892023-09-26 08:14:37 -06001836 * Makes a copy of all properties from the source node to the destination node.
Simon Glass7a7229a2022-09-06 20:27:33 -06001837 * Existing properties in the destination node remain unchanged, except that
1838 * any with the same name are overwritten, including changing the size of the
1839 * property.
1840 *
1841 * For livetree, properties are copied / allocated, so the source tree does not
1842 * need to be present afterwards.
1843 *
Simon Glass68164892023-09-26 08:14:37 -06001844 * @dst: Destination node to write properties to
Simon Glass7a7229a2022-09-06 20:27:33 -06001845 * @src: Source node to read properties from
Simon Glass7a7229a2022-09-06 20:27:33 -06001846 */
Simon Glass68164892023-09-26 08:14:37 -06001847int ofnode_copy_props(ofnode dst, ofnode src);
Simon Glass7a7229a2022-09-06 20:27:33 -06001848
Simon Glass7c33c962023-09-26 08:14:41 -06001849/**
1850 * ofnode_copy_node() - Copy a node to another place
1851 *
1852 * If a node with this name already exists in dst_parent, this returns an
1853 * .error
1854 *
1855 * @dst_parent: Parent of the newly copied node
1856 * @name: Name to give the new node
1857 * @src: Source node to copy
1858 * @nodep: Returns the new node, or the existing node if there is one
1859 * Return: 0 if OK, -EEXIST if dst_parent already has a node with this parent
1860 */
1861int ofnode_copy_node(ofnode dst_parent, const char *name, ofnode src,
1862 ofnode *nodep);
1863
Simon Glass45448772023-09-26 08:14:42 -06001864/**
1865 * ofnode_delete() - Delete a node
1866 *
1867 * Delete a node from the tree
1868 *
1869 * @nodep: Pointer to node to delete (set to ofnode_null() on success)
1870 * Return: 0 if OK, -ENOENT if the node does not exist, -EPERM if it is the root
1871 * node (wWhich cannot be removed), -EFAULT if the tree is broken (to_remove is
1872 * not a child of its parent),
1873 *
1874 */
1875int ofnode_delete(ofnode *nodep);
1876
Simon Glass9a148602017-05-17 17:18:10 -06001877#endif