blob: 32917f6615573d2367557511059a860e1b15456d [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 Glassf7bfcc42017-07-25 08:29:55 -060020struct resource;
21
Simon Glass9693c1d2022-07-30 15:52:06 -060022#include <dm/ofnode_decl.h>
Michal Simek43c42bd2023-08-31 08:59:05 +020023#include <linux/errno.h>
Simon Glass9a148602017-05-17 17:18:10 -060024
Simon Glassc4fc5622017-05-18 20:08:58 -060025struct ofnode_phandle_args {
26 ofnode node;
27 int args_count;
28 uint32_t args[OF_MAX_PHANDLE_ARGS];
29};
30
Simon Glasscb13a1b2022-09-06 20:27:26 -060031#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
Simon Glassc4fc5622017-05-18 20:08:58 -060032/**
Simon Glassba8457b2022-09-06 20:27:19 -060033 * oftree_reset() - reset the state of the oftree list
34 *
35 * Reset the oftree list so it can be started again. This should be called
36 * once the control FDT is in place, but before the ofnode interface is used.
37 */
Simon Glasscb13a1b2022-09-06 20:27:26 -060038void oftree_reset(void);
Simon Glassba8457b2022-09-06 20:27:19 -060039
40/**
Simon Glass04fa09a2022-09-06 20:27:20 -060041 * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
42 *
43 * This cannot be called if the reference contains a node pointer.
44 *
45 * @node: Reference containing offset (possibly invalid)
46 * Return: DT offset (can be NULL)
47 */
Simon Glasscb13a1b2022-09-06 20:27:26 -060048__attribute_const__ void *ofnode_to_fdt(ofnode node);
49
50/**
51 * ofnode_to_offset() - convert an ofnode to a flat DT offset
52 *
53 * This cannot be called if the reference contains a node pointer.
54 *
55 * @node: Reference containing offset (possibly invalid)
56 * Return: DT offset (can be -1)
57 */
58__attribute_const__ int ofnode_to_offset(ofnode node);
59
60/**
61 * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
62 *
Simon Glasse6a211c2022-10-11 09:47:19 -060063 * If @fdt is not already registered in the list of current device trees, it is
64 * added to the list.
65 *
Simon Glasscb13a1b2022-09-06 20:27:26 -060066 * @fdt: Device tree to use
67 *
68 * Returns: reference to the given node
69 */
70oftree oftree_from_fdt(void *fdt);
71
72/**
73 * noffset_to_ofnode() - convert a DT offset to an ofnode
74 *
75 * @other_node: Node in the same tree to use as a reference
76 * @of_offset: DT offset (either valid, or -1)
77 * Return: reference to the associated DT offset
78 */
79ofnode noffset_to_ofnode(ofnode other_node, int of_offset);
80
81#else /* !OFNODE_MULTI_TREE */
82static inline void oftree_reset(void) {}
83
Simon Glass04fa09a2022-09-06 20:27:20 -060084static inline void *ofnode_to_fdt(ofnode node)
85{
86#ifdef OF_CHECKS
87 if (of_live_active())
88 return NULL;
89#endif
Simon Glass04fa09a2022-09-06 20:27:20 -060090 /* Use the control FDT by default */
91 return (void *)gd->fdt_blob;
92}
93
Simon Glasscb13a1b2022-09-06 20:27:26 -060094static inline __attribute_const__ int ofnode_to_offset(ofnode node)
Simon Glass37dcd912022-09-06 20:27:23 -060095{
96#ifdef OF_CHECKS
97 if (of_live_active())
98 return -1;
99#endif
100 return node.of_offset;
101}
102
Simon Glasscb13a1b2022-09-06 20:27:26 -0600103static inline oftree oftree_from_fdt(void *fdt)
104{
105 oftree tree;
106
107 /* we cannot access other trees without OFNODE_MULTI_TREE */
108 if (fdt == gd->fdt_blob)
109 tree.fdt = fdt;
110 else
111 tree.fdt = NULL;
112
113 return tree;
114}
115
116static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
117{
118 ofnode node;
119
120 if (of_live_active())
121 node.np = NULL;
122 else
123 node.of_offset = of_offset;
124
125 return node;
126}
127
128#endif /* OFNODE_MULTI_TREE */
129
Simon Glass37dcd912022-09-06 20:27:23 -0600130/**
Stefan Roesec14df8b2020-09-23 08:23:27 +0200131 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -0600132 *
133 * This cannot be called if the reference contains an offset.
134 *
135 * @node: Reference containing struct device_node * (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100136 * Return: pointer to device node (can be NULL)
Simon Glassc4fc5622017-05-18 20:08:58 -0600137 */
Simon Glass9036c5c2022-09-06 20:27:04 -0600138static inline struct device_node *ofnode_to_np(ofnode node)
Simon Glassc4fc5622017-05-18 20:08:58 -0600139{
140#ifdef OF_CHECKS
141 if (!of_live_active())
142 return NULL;
143#endif
144 return node.np;
145}
146
Simon Glass9a148602017-05-17 17:18:10 -0600147/**
Simon Glass9a148602017-05-17 17:18:10 -0600148 * ofnode_valid() - check if an ofnode is valid
149 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100150 * @node: Reference containing offset (possibly invalid)
Simon Glasscb13a1b2022-09-06 20:27:26 -0600151 * Return: true if the reference contains a valid ofnode, false if not
Simon Glass9a148602017-05-17 17:18:10 -0600152 */
153static inline bool ofnode_valid(ofnode node)
154{
Simon Glassc4fc5622017-05-18 20:08:58 -0600155 if (of_live_active())
156 return node.np != NULL;
157 else
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200158 return node.of_offset >= 0;
Simon Glass9a148602017-05-17 17:18:10 -0600159}
160
161/**
Simon Glass95fd2092022-09-06 20:27:22 -0600162 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
163 *
164 * This can only be called when flat tree is enabled
165 *
166 * @tree: Tree to look at
167 * @return FDT pointer from the tree
168 */
169static inline void *oftree_lookup_fdt(oftree tree)
170{
171 if (of_live_active())
172 return NULL;
173 else
174 return tree.fdt;
175}
176
177/**
Simon Glass9a148602017-05-17 17:18:10 -0600178 * offset_to_ofnode() - convert a DT offset to an ofnode
179 *
180 * @of_offset: DT offset (either valid, or -1)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100181 * Return: reference to the associated DT offset
Simon Glass9a148602017-05-17 17:18:10 -0600182 */
183static inline ofnode offset_to_ofnode(int of_offset)
184{
185 ofnode node;
186
Simon Glassc4fc5622017-05-18 20:08:58 -0600187 if (of_live_active())
188 node.np = NULL;
189 else
Simon Glass1ab8b892019-12-06 21:41:36 -0700190 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass9a148602017-05-17 17:18:10 -0600191
192 return node;
193}
194
195/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600196 * np_to_ofnode() - convert a node pointer to an ofnode
197 *
198 * @np: Live node pointer (can be NULL)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100199 * Return: reference to the associated node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -0600200 */
Simon Glass9036c5c2022-09-06 20:27:04 -0600201static inline ofnode np_to_ofnode(struct device_node *np)
Simon Glassc4fc5622017-05-18 20:08:58 -0600202{
203 ofnode node;
204
205 node.np = np;
206
207 return node;
208}
209
210/**
211 * ofnode_is_np() - check if a reference is a node pointer
212 *
213 * This function associated that if there is a valid live tree then all
214 * references will use it. This is because using the flat DT when the live tree
215 * is valid is not permitted.
216 *
217 * @node: reference to check (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100218 * Return: true if the reference is a live node pointer, false if it is a DT
Simon Glassc4fc5622017-05-18 20:08:58 -0600219 * offset
220 */
221static inline bool ofnode_is_np(ofnode node)
222{
223#ifdef OF_CHECKS
224 /*
225 * Check our assumption that flat tree offsets are not used when a
226 * live tree is in use.
227 */
228 assert(!ofnode_valid(node) ||
Stefan Roesec14df8b2020-09-23 08:23:27 +0200229 (of_live_active() ? ofnode_to_np(node)
230 : ofnode_to_np(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600231#endif
232 return of_live_active() && ofnode_valid(node);
233}
234
235/**
Simon Glass9a148602017-05-17 17:18:10 -0600236 * ofnode_equal() - check if two references are equal
237 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100238 * @ref1: first reference to check (possibly invalid)
239 * @ref2: second reference to check (possibly invalid)
240 * Return: true if equal, else false
Simon Glass9a148602017-05-17 17:18:10 -0600241 */
242static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
243{
244 /* We only need to compare the contents */
245 return ref1.of_offset == ref2.of_offset;
246}
247
Simon Glassc4fc5622017-05-18 20:08:58 -0600248/**
Simon Glass2b9b14582022-09-06 20:27:21 -0600249 * oftree_valid() - check if an oftree is valid
250 *
251 * @tree: Reference containing oftree
252 * Return: true if the reference contains a valid oftree, false if node
253 */
254static inline bool oftree_valid(oftree tree)
255{
256 if (of_live_active())
257 return tree.np;
258 else
259 return tree.fdt;
260}
261
262/**
263 * oftree_null() - Obtain a null oftree
264 *
265 * This returns an oftree which points to no tree. It works both with the flat
266 * tree and livetree.
267 */
268static inline oftree oftree_null(void)
269{
270 oftree tree;
271
272 if (of_live_active())
273 tree.np = NULL;
274 else
275 tree.fdt = NULL;
276
277 return tree;
278}
279
280/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600281 * ofnode_null() - Obtain a null ofnode
282 *
283 * This returns an ofnode which points to no node. It works both with the flat
284 * tree and livetree.
285 */
286static inline ofnode ofnode_null(void)
287{
288 ofnode node;
289
290 if (of_live_active())
291 node.np = NULL;
292 else
293 node.of_offset = -1;
294
295 return node;
296}
297
Simon Glass278ddba2020-11-28 17:50:07 -0700298static inline ofnode ofnode_root(void)
299{
300 ofnode node;
301
302 if (of_live_active())
303 node.np = gd_of_root();
304 else
305 node.of_offset = 0;
306
307 return node;
308}
309
Simon Glassc4fc5622017-05-18 20:08:58 -0600310/**
Simon Glass4caa79a2022-09-06 20:27:16 -0600311 * ofprop_valid() - check if an ofprop is valid
312 *
313 * @prop: Pointer to ofprop to check
314 * Return: true if the reference contains a valid ofprop, false if not
315 */
316static inline bool ofprop_valid(struct ofprop *prop)
317{
318 if (of_live_active())
319 return prop->prop;
320 else
321 return prop->offset >= 0;
322}
323
324/**
Simon Glassef75c592022-07-30 15:52:08 -0600325 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
326 *
327 * Returns: reference to the control FDT
328 */
329static inline oftree oftree_default(void)
330{
331 oftree tree;
332
333 if (of_live_active())
334 tree.np = gd_of_root();
335 else
336 tree.fdt = (void *)gd->fdt_blob;
337
338 return tree;
339}
340
341/**
Simon Glass2b9b14582022-09-06 20:27:21 -0600342 * oftree_from_np() - Returns an oftree from a node pointer
343 *
344 * @root: Root node of the tree
345 * Returns: reference to the given node
346 */
347static inline oftree oftree_from_np(struct device_node *root)
348{
349 oftree tree;
350
351 tree.np = root;
352
353 return tree;
354}
355
356/**
Simon Glass722281b2023-06-01 10:22:42 -0600357 * oftree_dispose() - Dispose of an oftree
358 *
359 * This can be used to dispose of a tree that has been created (other than
360 * the control FDT which must not be disposed)
361 *
362 * @tree: Tree to dispose
363 */
364void oftree_dispose(oftree tree);
365
366/**
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530367 * ofnode_name_eq() - Check if the node name is equivalent to a given name
368 * ignoring the unit address
369 *
370 * @node: valid node reference that has to be compared
371 * @name: name that has to be compared with the node name
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100372 * Return: true if matches, false if it doesn't match
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530373 */
374bool ofnode_name_eq(ofnode node, const char *name);
375
376/**
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200377 * ofnode_read_u8() - Read a 8-bit integer from a property
378 *
379 * @node: valid node reference to read property from
380 * @propname: name of the property to read from
381 * @outp: place to put value (if found)
382 * Return: 0 if OK, -ve on error
383 */
384int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
385
386/**
387 * ofnode_read_u8_default() - Read a 8-bit integer from a property
388 *
389 * @node: valid node reference to read property from
390 * @propname: name of the property to read from
391 * @def: default value to return if the property has no value
392 * Return: property value, or @def if not found
393 */
394u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
395
396/**
397 * ofnode_read_u16() - Read a 16-bit integer from a property
398 *
399 * @node: valid node reference to read property from
400 * @propname: name of the property to read from
401 * @outp: place to put value (if found)
402 * Return: 0 if OK, -ve on error
403 */
404int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
405
406/**
407 * ofnode_read_u16_default() - Read a 16-bit integer from a property
408 *
409 * @node: valid node reference to read property from
410 * @propname: name of the property to read from
411 * @def: default value to return if the property has no value
412 * Return: property value, or @def if not found
413 */
414u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
415
416/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600417 * ofnode_read_u32() - Read a 32-bit integer from a property
418 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100419 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600420 * @propname: name of the property to read from
421 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100422 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600423 */
424int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
425
426/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200427 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
428 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100429 * @node: valid node reference to read property from
Dario Binacchi81d80b52020-03-29 18:04:41 +0200430 * @propname: name of the property to read from
431 * @index: index of the integer to return
432 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100433 * Return: 0 if OK, -ve on error
Dario Binacchi81d80b52020-03-29 18:04:41 +0200434 */
435int ofnode_read_u32_index(ofnode node, const char *propname, int index,
436 u32 *outp);
437
438/**
Michal Simek08a194e2023-08-25 11:37:46 +0200439 * ofnode_read_u64_index() - Read a 64-bit integer from a multi-value property
440 *
441 * @node: valid node reference to read property from
442 * @propname: name of the property to read from
443 * @index: index of the integer to return
444 * @outp: place to put value (if found)
445 * Return: 0 if OK, -ve on error
446 */
447int ofnode_read_u64_index(ofnode node, const char *propname, int index,
448 u64 *outp);
449
450/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600451 * ofnode_read_s32() - Read a 32-bit integer from a property
452 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100453 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600454 * @propname: name of the property to read from
455 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100456 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600457 */
458static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100459 s32 *outp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600460{
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100461 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600462}
463
464/**
465 * ofnode_read_u32_default() - Read a 32-bit integer from a property
466 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100467 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600468 * @propname: name of the property to read from
469 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100470 * Return: property value, or @def if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600471 */
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100472u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600473
474/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200475 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
476 * property
477 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100478 * @node: valid node reference to read property from
Dario Binacchi81d80b52020-03-29 18:04:41 +0200479 * @propname: name of the property to read from
480 * @index: index of the integer to return
481 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100482 * Return: property value, or @def if not found
Dario Binacchi81d80b52020-03-29 18:04:41 +0200483 */
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100484u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi81d80b52020-03-29 18:04:41 +0200485 u32 def);
486
487/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600488 * ofnode_read_s32_default() - Read a 32-bit integer from a property
489 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100490 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600491 * @propname: name of the property to read from
492 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100493 * Return: property value, or @def if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600494 */
495int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
496
497/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100498 * ofnode_read_u64() - Read a 64-bit integer from a property
499 *
500 * @node: valid node reference to read property from
501 * @propname: name of the property to read from
502 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100503 * Return: 0 if OK, -ve on error
Lukas Auerb03a60b2018-11-22 11:26:35 +0100504 */
505int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
506
507/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600508 * ofnode_read_u64_default() - Read a 64-bit integer from a property
509 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100510 * @node: valid node reference to read property from
Simon Glass9d54a7a2018-06-11 13:07:10 -0600511 * @propname: name of the property to read from
512 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100513 * Return: property value, or @def if not found
Simon Glass9d54a7a2018-06-11 13:07:10 -0600514 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200515u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600516
517/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700518 * ofnode_read_prop() - Read a property from a node
519 *
520 * @node: valid node reference to read property from
521 * @propname: name of the property to read
522 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100523 * if not found
524 * Return: property value, or NULL if there is no such property
Simon Glass0c2e9802020-01-27 08:49:44 -0700525 */
526const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
527
528/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600529 * ofnode_read_string() - Read a string from a property
530 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700531 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600532 * @propname: name of the property to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100533 * Return: string from property value, or NULL if there is no such property
Simon Glassc4fc5622017-05-18 20:08:58 -0600534 */
535const char *ofnode_read_string(ofnode node, const char *propname);
536
537/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600538 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600539 *
540 * @node: valid node reference to read property from
541 * @propname: name of the property to read
542 * @out_values: pointer to return value, modified only if return value is 0
543 * @sz: number of array elements to read
Simon Glasse3be5fc2022-09-06 20:27:18 -0600544 * Return: 0 on success, -EINVAL if the property does not exist,
545 * -ENODATA if property does not have a value, and -EOVERFLOW if the
546 * property data isn't large enough
Simon Glassc4fc5622017-05-18 20:08:58 -0600547 *
548 * Search for a property in a device node and read 32-bit value(s) from
Simon Glasse3be5fc2022-09-06 20:27:18 -0600549 * it.
Simon Glassc4fc5622017-05-18 20:08:58 -0600550 *
551 * The out_values is modified only if a valid u32 value can be decoded.
552 */
553int ofnode_read_u32_array(ofnode node, const char *propname,
554 u32 *out_values, size_t sz);
555
556/**
557 * ofnode_read_bool() - read a boolean value from a property
558 *
559 * @node: valid node reference to read property from
560 * @propname: name of property to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100561 * Return: true if property is present (meaning true), false if not present
Simon Glassc4fc5622017-05-18 20:08:58 -0600562 */
563bool ofnode_read_bool(ofnode node, const char *propname);
564
565/**
566 * ofnode_find_subnode() - find a named subnode of a parent node
567 *
568 * @node: valid reference to parent node
569 * @subnode_name: name of subnode to find
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100570 * Return: reference to subnode (which can be invalid if there is no such
Simon Glassc4fc5622017-05-18 20:08:58 -0600571 * subnode)
572 */
573ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
574
Simon Glass39f1d282020-12-16 17:25:06 -0700575#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass3ba929a2020-10-30 21:38:53 -0600576#include <asm/global_data.h>
577
Simon Glass39f1d282020-12-16 17:25:06 -0700578static inline bool ofnode_is_enabled(ofnode node)
579{
580 if (ofnode_is_np(node)) {
581 return of_device_is_available(ofnode_to_np(node));
582 } else {
583 return fdtdec_get_is_enabled(gd->fdt_blob,
584 ofnode_to_offset(node));
585 }
586}
587
588static inline ofnode ofnode_first_subnode(ofnode node)
589{
590 assert(ofnode_valid(node));
591 if (ofnode_is_np(node))
592 return np_to_ofnode(node.np->child);
593
594 return offset_to_ofnode(
595 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
596}
597
598static inline ofnode ofnode_next_subnode(ofnode node)
599{
600 assert(ofnode_valid(node));
601 if (ofnode_is_np(node))
602 return np_to_ofnode(node.np->sibling);
603
604 return offset_to_ofnode(
605 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
606}
607#else
608/**
609 * ofnode_is_enabled() - Checks whether a node is enabled.
610 * This looks for a 'status' property. If this exists, then returns true if
611 * the status is 'okay' and false otherwise. If there is no status property,
612 * it returns true on the assumption that anything mentioned should be enabled
613 * by default.
614 *
615 * @node: node to examine
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100616 * Return: false (not enabled) or true (enabled)
Simon Glass39f1d282020-12-16 17:25:06 -0700617 */
618bool ofnode_is_enabled(ofnode node);
619
Simon Glassc4fc5622017-05-18 20:08:58 -0600620/**
621 * ofnode_first_subnode() - find the first subnode of a parent node
622 *
623 * @node: valid reference to a valid parent node
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100624 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glassc4fc5622017-05-18 20:08:58 -0600625 * node has no subnodes)
626 */
627ofnode ofnode_first_subnode(ofnode node);
628
629/**
630 * ofnode_next_subnode() - find the next sibling of a subnode
631 *
632 * @node: valid reference to previous node (sibling)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100633 * Return: reference to the next subnode (which can be invalid if the node
Simon Glassc4fc5622017-05-18 20:08:58 -0600634 * has no more siblings)
635 */
636ofnode ofnode_next_subnode(ofnode node);
Simon Glass39f1d282020-12-16 17:25:06 -0700637#endif /* DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600638
639/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100640 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
641 *
642 * @node: valid node to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100643 * Return: ofnode reference of the parent node
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100644 */
645ofnode ofnode_get_parent(ofnode node);
646
647/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600648 * ofnode_get_name() - get the name of a node
649 *
650 * @node: valid node to look up
Simon Glass91d89a82022-09-06 20:27:15 -0600651 * Return: name of node (for the root node this is "")
Simon Glassc4fc5622017-05-18 20:08:58 -0600652 */
653const char *ofnode_get_name(ofnode node);
654
655/**
Marek Behúne897e3c2021-05-26 14:08:18 +0200656 * ofnode_get_path() - get the full path of a node
657 *
658 * @node: valid node to look up
659 * @buf: buffer to write the node path into
660 * @buflen: buffer size
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100661 * Return: 0 if OK, -ve on error
Marek Behúne897e3c2021-05-26 14:08:18 +0200662 */
663int ofnode_get_path(ofnode node, char *buf, int buflen);
664
665/**
Kever Yang37df0e02018-02-23 17:38:50 +0100666 * ofnode_get_by_phandle() - get ofnode from phandle
667 *
Simon Glass176dd432022-09-06 20:26:57 -0600668 * This uses the default (control) device tree
669 *
Kever Yang37df0e02018-02-23 17:38:50 +0100670 * @phandle: phandle to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100671 * Return: ofnode reference to the phandle
Kever Yang37df0e02018-02-23 17:38:50 +0100672 */
673ofnode ofnode_get_by_phandle(uint phandle);
674
675/**
Simon Glass95fd2092022-09-06 20:27:22 -0600676 * oftree_get_by_phandle() - get ofnode from phandle
677 *
678 * @tree: tree to use
679 * @phandle: phandle to look up
680 * Return: ofnode reference to the phandle
681 */
682ofnode oftree_get_by_phandle(oftree tree, uint phandle);
683
684/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600685 * ofnode_read_size() - read the size of a property
686 *
687 * @node: node to check
688 * @propname: property to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100689 * Return: size of property if present, or -EINVAL if not
Simon Glassc4fc5622017-05-18 20:08:58 -0600690 */
691int ofnode_read_size(ofnode node, const char *propname);
692
693/**
Keerthyd332e6e2019-04-24 17:19:53 +0530694 * ofnode_get_addr_size_index() - get an address/size from a node
695 * based on index
696 *
697 * This reads the register address/size from a node based on index
698 *
699 * @node: node to read from
700 * @index: Index of address to read (0 for first)
701 * @size: Pointer to size of the address
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100702 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthyd332e6e2019-04-24 17:19:53 +0530703 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100704fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index,
705 fdt_size_t *size);
Keerthyd332e6e2019-04-24 17:19:53 +0530706
707/**
Marek Behún177ab7f2021-05-26 14:08:17 +0200708 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
709 * based on index, without address
710 * translation
711 *
712 * This reads the register address/size from a node based on index.
713 * The resulting address is not translated. Useful for example for on-disk
714 * addresses.
715 *
716 * @node: node to read from
717 * @index: Index of address to read (0 for first)
718 * @size: Pointer to size of the address
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100719 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún177ab7f2021-05-26 14:08:17 +0200720 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100721fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
722 fdt_size_t *size);
Marek Behún177ab7f2021-05-26 14:08:17 +0200723
724/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600725 * ofnode_get_addr_index() - get an address from a node
726 *
727 * This reads the register address from a node
728 *
729 * @node: node to read from
730 * @index: Index of address to read (0 for first)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100731 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glass049ae1b2017-05-18 20:09:01 -0600732 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100733fdt_addr_t ofnode_get_addr_index(ofnode node, int index);
Simon Glass049ae1b2017-05-18 20:09:01 -0600734
735/**
736 * ofnode_get_addr() - get an address from a node
737 *
738 * This reads the register address from a node
739 *
740 * @node: node to read from
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100741 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glass049ae1b2017-05-18 20:09:01 -0600742 */
Johan Jonker9f6971f2023-03-13 01:30:33 +0100743fdt_addr_t ofnode_get_addr(ofnode node);
Simon Glass049ae1b2017-05-18 20:09:01 -0600744
745/**
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800746 * ofnode_get_size() - get size from a node
747 *
748 * This reads the register size from a node
749 *
750 * @node: node to read from
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100751 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800752 */
753fdt_size_t ofnode_get_size(ofnode node);
754
755/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600756 * ofnode_stringlist_search() - find a string in a string list and return index
757 *
758 * Note that it is possible for this function to succeed on property values
759 * that are not NUL-terminated. That's because the function will stop after
760 * finding the first occurrence of @string. This can for example happen with
761 * small-valued cell properties, such as #address-cells, when searching for
762 * the empty string.
763 *
764 * @node: node to check
765 * @propname: name of the property containing the string list
766 * @string: string to look up in the string list
767 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100768 * Return:
Simon Glassc4fc5622017-05-18 20:08:58 -0600769 * the index of the string in the list of strings
770 * -ENODATA if the property is not found
771 * -EINVAL on some other error
772 */
773int ofnode_stringlist_search(ofnode node, const char *propname,
774 const char *string);
775
776/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600777 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600778 *
779 * Note that this will successfully extract strings from properties with
780 * non-NUL-terminated values. For example on small-valued cell properties
781 * this function will return the empty string.
782 *
783 * If non-NULL, the length of the string (on success) or a negative error-code
784 * (on failure) will be stored in the integer pointer to by lenp.
785 *
786 * @node: node to check
787 * @propname: name of the property containing the string list
Simon Glass7a3a1672021-10-23 17:26:06 -0600788 * @index: index of the string to return (cannot be negative)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100789 * @outp: return location for the string
Simon Glassc4fc5622017-05-18 20:08:58 -0600790 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100791 * Return:
Simon Glass7a3a1672021-10-23 17:26:06 -0600792 * 0 if found or -ve error value if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600793 */
794int ofnode_read_string_index(ofnode node, const char *propname, int index,
795 const char **outp);
796
797/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600798 * ofnode_read_string_count() - find the number of strings in a string list
799 *
800 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100801 * @property: name of the property containing the string list
802 * Return:
Simon Glass5fdb0052017-06-12 06:21:28 -0600803 * number of strings in the list, or -ve error value if not found
804 */
805int ofnode_read_string_count(ofnode node, const char *property);
806
807/**
Simon Glass9580bfc2021-10-23 17:26:07 -0600808 * ofnode_read_string_list() - read a list of strings
809 *
810 * This produces a list of string pointers with each one pointing to a string
811 * in the string list. If the property does not exist, it returns {NULL}.
812 *
813 * The data is allocated and the caller is reponsible for freeing the return
814 * value (the list of string pointers). The strings themselves may not be
815 * changed as they point directly into the devicetree property.
816 *
817 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100818 * @property: name of the property containing the string list
Simon Glass9580bfc2021-10-23 17:26:07 -0600819 * @listp: returns an allocated, NULL-terminated list of strings if the return
820 * value is > 0, else is set to NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100821 * Return:
822 * number of strings in list, 0 if none, -ENOMEM if out of memory,
823 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass9580bfc2021-10-23 17:26:07 -0600824 */
825int ofnode_read_string_list(ofnode node, const char *property,
826 const char ***listp);
827
828/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600829 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
830 *
831 * This function is useful to parse lists of phandles and their arguments.
832 * Returns 0 on success and fills out_args, on error returns appropriate
833 * errno value.
834 *
835 * Caller is responsible to call of_node_put() on the returned out_args->np
836 * pointer.
837 *
838 * Example:
839 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100840 * .. code-block::
Simon Glassc4fc5622017-05-18 20:08:58 -0600841 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100842 * phandle1: node1 {
843 * #list-cells = <2>;
844 * };
845 * phandle2: node2 {
846 * #list-cells = <1>;
847 * };
848 * node3 {
849 * list = <&phandle1 1 2 &phandle2 3>;
850 * };
Simon Glassc4fc5622017-05-18 20:08:58 -0600851 *
852 * To get a device_node of the `node2' node you may call this:
853 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
854 *
855 * @node: device tree node containing a list
856 * @list_name: property name that contains a list
857 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100858 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600859 * @index: index of a phandle to parse out
860 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100861 * Return:
862 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
863 * @list_name does not exist, -EINVAL if a phandle was not found,
864 * @cells_name could not be found, the arguments were truncated or there
865 * were too many arguments.
Simon Glassc4fc5622017-05-18 20:08:58 -0600866 */
867int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
868 const char *cells_name, int cell_count,
869 int index,
870 struct ofnode_phandle_args *out_args);
871
872/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200873 * ofnode_count_phandle_with_args() - Count number of phandle in a list
874 *
875 * This function is useful to count phandles into a list.
876 * Returns number of phandle on success, on error returns appropriate
877 * errno value.
878 *
879 * @node: device tree node containing a list
880 * @list_name: property name that contains a list
881 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100882 * @cell_count: Cell count to use if @cells_name is NULL
883 * Return:
884 * number of phandle on success, -ENOENT if @list_name does not exist,
885 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200886 */
887int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200888 const char *cells_name, int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200889
890/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600891 * ofnode_path() - find a node by full path
892 *
Simon Glassef75c592022-07-30 15:52:08 -0600893 * This uses the control FDT.
894 *
Simon Glassc4fc5622017-05-18 20:08:58 -0600895 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100896 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glassc4fc5622017-05-18 20:08:58 -0600897 */
898ofnode ofnode_path(const char *path);
899
900/**
Simon Glass45ae59d2022-09-06 20:27:24 -0600901 * oftree_path() - find a node by full path from a root node
Simon Glassef75c592022-07-30 15:52:08 -0600902 *
903 * @tree: Device tree to use
904 * @path: Full path to node, e.g. "/bus/spi@1"
905 * Return: reference to the node found. Use ofnode_valid() to check if it exists
906 */
Simon Glass45ae59d2022-09-06 20:27:24 -0600907ofnode oftree_path(oftree tree, const char *path);
Simon Glassef75c592022-07-30 15:52:08 -0600908
909/**
Simon Glass45ae59d2022-09-06 20:27:24 -0600910 * oftree_root() - get the root node of a tree
911 *
912 * @tree: Device tree to use
913 * Return: reference to the root node
914 */
915ofnode oftree_root(oftree tree);
916
917/**
Simon Glasse09223c2020-01-27 08:49:46 -0700918 * ofnode_read_chosen_prop() - get the value of a chosen property
919 *
Simon Glass45ae59d2022-09-06 20:27:24 -0600920 * This looks for a property within the /chosen node and returns its value.
921 *
922 * This only works with the control FDT.
Simon Glasse09223c2020-01-27 08:49:46 -0700923 *
924 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100925 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glasse09223c2020-01-27 08:49:46 -0700926 * returns NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100927 * Return: property value if found, else NULL
Simon Glasse09223c2020-01-27 08:49:46 -0700928 */
929const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
930
931/**
Simon Glassf3455962020-01-27 08:49:43 -0700932 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -0600933 *
Simon Glassf3455962020-01-27 08:49:43 -0700934 * This looks for a property within the /chosen node and returns its value,
935 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -0600936 *
Simon Glassc63ffd72022-09-06 20:27:28 -0600937 * This only works with the control FDT.
938 *
Simon Glassc4fc5622017-05-18 20:08:58 -0600939 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100940 * Return: string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600941 */
Simon Glassf3455962020-01-27 08:49:43 -0700942const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600943
944/**
Simon Glassc99ba912020-01-27 08:49:42 -0700945 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -0600946 *
Simon Glassc99ba912020-01-27 08:49:42 -0700947 * This looks up a named property in the chosen node and uses that as a path to
948 * look up a code.
949 *
Simon Glassc63ffd72022-09-06 20:27:28 -0600950 * This only works with the control FDT.
951 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100952 * @propname: Property name to look for
953 * Return: the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -0600954 */
Simon Glassc99ba912020-01-27 08:49:42 -0700955ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600956
Michal Simek92a88622020-07-28 12:51:08 +0200957/**
958 * ofnode_read_aliases_prop() - get the value of a aliases property
959 *
960 * This looks for a property within the /aliases node and returns its value
961 *
Simon Glassc63ffd72022-09-06 20:27:28 -0600962 * This only works with the control FDT.
963 *
Michal Simek92a88622020-07-28 12:51:08 +0200964 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100965 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek92a88622020-07-28 12:51:08 +0200966 * returns NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100967 * Return: property value if found, else NULL
Michal Simek92a88622020-07-28 12:51:08 +0200968 */
969const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
970
971/**
972 * ofnode_get_aliases_node() - get a referenced node from the aliases node
973 *
974 * This looks up a named property in the aliases node and uses that as a path to
975 * look up a code.
976 *
Simon Glassc63ffd72022-09-06 20:27:28 -0600977 * This only works with the control FDT.
978 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100979 * @propname: Property name to look for
980 * Return: the referenced node if present, else ofnode_null()
Michal Simek92a88622020-07-28 12:51:08 +0200981 */
982ofnode ofnode_get_aliases_node(const char *propname);
983
Simon Glassc4fc5622017-05-18 20:08:58 -0600984struct display_timing;
985/**
986 * ofnode_decode_display_timing() - decode display timings
987 *
988 * Decode display timings from the supplied 'display-timings' node.
989 * See doc/device-tree-bindings/video/display-timing.txt for binding
990 * information.
991 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100992 * @node: 'display-timing' node containing the timing subnodes
993 * @index: Index number to read (0=first timing subnode)
994 * @config: Place to put timings
995 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600996 */
997int ofnode_decode_display_timing(ofnode node, int index,
998 struct display_timing *config);
999
1000/**
Nikhil M Jainff407062023-01-31 15:35:14 +05301001 * ofnode_decode_panel_timing() - decode display timings
1002 *
1003 * Decode panel timings from the supplied 'panel-timings' node.
1004 *
1005 * @node: 'display-timing' node containing the timing subnodes
1006 * @config: Place to put timings
1007 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
1008 */
1009int ofnode_decode_panel_timing(ofnode node,
1010 struct display_timing *config);
1011
1012/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001013 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -06001014 *
1015 * @node: node to read
1016 * @propname: property to read
1017 * @lenp: place to put length on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001018 * Return: pointer to property, or NULL if not found
Simon Glassc4fc5622017-05-18 20:08:58 -06001019 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +09001020const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -06001021
1022/**
Simon Glassfec058d2022-09-06 20:27:13 -06001023 * ofnode_first_property()- get the reference of the first property
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001024 *
1025 * Get reference to the first property of the node, it is used to iterate
Simon Glassd0aff8b2022-09-06 20:27:14 -06001026 * and read all the property with ofprop_get_property().
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001027 *
1028 * @node: node to read
1029 * @prop: place to put argument reference
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001030 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001031 */
Simon Glassfec058d2022-09-06 20:27:13 -06001032int ofnode_first_property(ofnode node, struct ofprop *prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001033
1034/**
Simon Glassfec058d2022-09-06 20:27:13 -06001035 * ofnode_next_property() - get the reference of the next property
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001036 *
1037 * Get reference to the next property of the node, it is used to iterate
Simon Glassd0aff8b2022-09-06 20:27:14 -06001038 * and read all the property with ofprop_get_property().
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001039 *
1040 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001041 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001042 */
Simon Glassfec058d2022-09-06 20:27:13 -06001043int ofnode_next_property(struct ofprop *prop);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001044
1045/**
Simon Glass4caa79a2022-09-06 20:27:16 -06001046 * ofnode_for_each_prop() - iterate over all properties of a node
1047 *
1048 * @prop: struct ofprop
1049 * @node: node (lvalue, ofnode)
1050 *
1051 * This is a wrapper around a for loop and is used like this::
1052 *
1053 * ofnode node;
1054 * struct ofprop prop;
1055 *
1056 * ofnode_for_each_prop(prop, node) {
1057 * ...use prop...
1058 * }
1059 *
1060 * Note that this is implemented as a macro and @prop is used as
1061 * iterator in the loop. The parent variable can be a constant or even a
1062 * literal.
1063 */
1064#define ofnode_for_each_prop(prop, node) \
1065 for (ofnode_first_property(node, &prop); \
1066 ofprop_valid(&prop); \
1067 ofnode_next_property(&prop))
1068
1069/**
Simon Glassd0aff8b2022-09-06 20:27:14 -06001070 * ofprop_get_property() - get a pointer to the value of a property
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001071 *
1072 * Get value for the property identified by the provided reference.
1073 *
1074 * @prop: reference on property
1075 * @propname: If non-NULL, place to property name on success,
Simon Glassd0aff8b2022-09-06 20:27:14 -06001076 * @lenp: If non-NULL, place to put length on success, or error code on failure
1077 * Return: pointer to property, or NULL if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001078 */
Simon Glassd0aff8b2022-09-06 20:27:14 -06001079const void *ofprop_get_property(const struct ofprop *prop,
1080 const char **propname, int *lenp);
Patrick Delaunaycaee1552020-01-13 11:34:56 +01001081
1082/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001083 * ofnode_get_addr_size() - get address and size from a property
1084 *
1085 * This does no address translation. It simply reads an property that contains
1086 * an address and a size value, one after the other.
1087 *
1088 * @node: node to read from
1089 * @propname: property to read
1090 * @sizep: place to put size value (on success)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001091 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glassc4fc5622017-05-18 20:08:58 -06001092 */
Johan Jonker9f6971f2023-03-13 01:30:33 +01001093fdt_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1094 fdt_size_t *sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -06001095
1096/**
1097 * ofnode_read_u8_array_ptr() - find an 8-bit array
1098 *
1099 * Look up a property in a node and return a pointer to its contents as a
1100 * byte array of given length. The property must have at least enough data
1101 * for the array (count bytes). It may have more, but this will be ignored.
1102 * The data is not copied.
1103 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001104 * @node: node to examine
1105 * @propname: name of property to find
1106 * @sz: number of array elements
1107 * Return:
1108 * pointer to byte array if found, or NULL if the property is not found or
1109 * there is not enough data
Simon Glassc4fc5622017-05-18 20:08:58 -06001110 */
1111const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1112 size_t sz);
1113
1114/**
1115 * ofnode_read_pci_addr() - look up a PCI address
1116 *
1117 * Look at an address property in a node and return the PCI address which
1118 * corresponds to the given type in the form of fdt_pci_addr.
1119 * The property must hold one fdt_pci_addr with a lengh.
1120 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001121 * @node: node to examine
1122 * @type: pci address type (FDT_PCI_SPACE_xxx)
1123 * @propname: name of property to find
1124 * @addr: returns pci address in the form of fdt_pci_addr
1125 * Return:
1126 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1127 * format of the property was invalid, -ENXIO if the requested
1128 * address type was not found
Simon Glassc4fc5622017-05-18 20:08:58 -06001129 */
1130int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1131 const char *propname, struct fdt_pci_addr *addr);
1132
1133/**
Bin Mengfa157712018-08-03 01:14:35 -07001134 * ofnode_read_pci_vendev() - look up PCI vendor and device id
1135 *
1136 * Look at the compatible property of a device node that represents a PCI
1137 * device and extract pci vendor id and device id from it.
1138 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001139 * @node: node to examine
1140 * @vendor: vendor id of the pci device
1141 * @device: device id of the pci device
1142 * Return: 0 if ok, negative on error
Bin Mengfa157712018-08-03 01:14:35 -07001143 */
1144int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1145
1146/**
Michal Simeka253c3b2022-02-23 15:45:40 +01001147 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1148 *
1149 * Look at the compatible property of a device node that represents a eth phy
1150 * device and extract phy vendor id and device id from it.
1151 *
Heinrich Schuchardtc9ae4a82022-03-24 16:22:32 +01001152 * @node: node to examine
1153 * @vendor: vendor id of the eth phy device
1154 * @device: device id of the eth phy device
1155 * Return: 0 if ok, negative on error
Michal Simeka253c3b2022-02-23 15:45:40 +01001156 */
1157int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1158
1159/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001160 * ofnode_read_addr_cells() - Get the number of address cells for a node
1161 *
1162 * This walks back up the tree to find the closest #address-cells property
1163 * which controls the given node.
1164 *
1165 * @node: Node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001166 * Return: number of address cells this node uses
Simon Glassc4fc5622017-05-18 20:08:58 -06001167 */
1168int ofnode_read_addr_cells(ofnode node);
1169
1170/**
1171 * ofnode_read_size_cells() - Get the number of size cells for a node
1172 *
1173 * This walks back up the tree to find the closest #size-cells property
1174 * which controls the given node.
1175 *
1176 * @node: Node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001177 * Return: number of size cells this node uses
Simon Glassc4fc5622017-05-18 20:08:58 -06001178 */
1179int ofnode_read_size_cells(ofnode node);
1180
1181/**
Simon Glass4191dc12017-06-12 06:21:31 -06001182 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1183 *
1184 * This function matches fdt_address_cells().
1185 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001186 * @node: Node to check
1187 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass4191dc12017-06-12 06:21:31 -06001188 */
1189int ofnode_read_simple_addr_cells(ofnode node);
1190
1191/**
1192 * ofnode_read_simple_size_cells() - Get the size cells property in a node
1193 *
1194 * This function matches fdt_size_cells().
1195 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001196 * @node: Node to check
1197 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass4191dc12017-06-12 06:21:31 -06001198 */
1199int ofnode_read_simple_size_cells(ofnode node);
1200
1201/**
Simon Glassc4fc5622017-05-18 20:08:58 -06001202 * ofnode_pre_reloc() - check if a node should be bound before relocation
1203 *
1204 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1205 * via special device tree properties.
1206 *
1207 * Before relocation this function can be used to check if nodes are required
1208 * in either SPL or TPL stages.
1209 *
1210 * After relocation and jumping into the real U-Boot binary it is possible to
1211 * determine if a node was bound in one of SPL/TPL stages.
1212 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +02001213 * There are 4 settings currently in use
Jonas Karlmanc43411b2023-08-20 22:03:18 +00001214 * - bootph-some-ram: U-Boot proper pre-relocation phase
Simon Glassfc1aa352023-02-13 08:56:34 -07001215 * - bootph-all: all phases
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001216 * Existing platforms only use it to indicate nodes needed in
Simon Glassfc1aa352023-02-13 08:56:34 -07001217 * SPL. Should probably be replaced by bootph-pre-ram for new platforms.
Jonas Karlmanc43411b2023-08-20 22:03:18 +00001218 * - bootph-pre-ram: SPL phase
1219 * - bootph-pre-sram: TPL phase
Simon Glassc4fc5622017-05-18 20:08:58 -06001220 *
1221 * @node: node to check
Jonas Karlmanc43411b2023-08-20 22:03:18 +00001222 * Return: true if node should be or was bound, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -06001223 */
1224bool ofnode_pre_reloc(ofnode node);
1225
Simon Glassa8173d62018-06-11 13:07:12 -06001226/**
1227 * ofnode_read_resource() - Read a resource from a node
1228 *
1229 * Read resource information from a node at the given index
1230 *
1231 * @node: Node to read from
1232 * @index: Index of resource to read (0 = first)
1233 * @res: Returns resource that was read, on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001234 * Return: 0 if OK, -ve on error
Simon Glassa8173d62018-06-11 13:07:12 -06001235 */
Simon Glassf7bfcc42017-07-25 08:29:55 -06001236int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -06001237
1238/**
1239 * ofnode_read_resource_byname() - Read a resource from a node by name
1240 *
1241 * Read resource information from a node matching the given name. This uses a
1242 * 'reg-names' string list property with the names matching the associated
1243 * 'reg' property list.
1244 *
1245 * @node: Node to read from
1246 * @name: Name of resource to read
1247 * @res: Returns resource that was read, on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001248 * Return: 0 if OK, -ve on error
Simon Glassa8173d62018-06-11 13:07:12 -06001249 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001250int ofnode_read_resource_byname(ofnode node, const char *name,
1251 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -06001252
Simon Glass28529762017-08-05 15:45:54 -06001253/**
Simon Glass954eeae2018-06-11 13:07:13 -06001254 * ofnode_by_compatible() - Find the next compatible node
1255 *
1256 * Find the next node after @from that is compatible with @compat
1257 *
1258 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1259 * @compat: Compatible string to match
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001260 * Return: ofnode found, or ofnode_null() if none
Simon Glass954eeae2018-06-11 13:07:13 -06001261 */
1262ofnode ofnode_by_compatible(ofnode from, const char *compat);
1263
1264/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001265 * ofnode_by_prop_value() - Find the next node with given property value
1266 *
1267 * Find the next node after @from that has a @propname with a value
1268 * @propval and a length @proplen.
1269 *
Simon Glass37dcd912022-09-06 20:27:23 -06001270 * @from: ofnode to start from. Use ofnode_null() to start at the
1271 * beginning, or the return value from oftree_root() to start at the first
1272 * child of the root
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001273 * @propname: property name to check
1274 * @propval: property value to search for
1275 * @proplen: length of the value in propval
1276 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001277 */
1278ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1279 const void *propval, int proplen);
1280
1281/**
Simon Glass28529762017-08-05 15:45:54 -06001282 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1283 *
1284 * @node: child node (ofnode, lvalue)
1285 * @parent: parent node (ofnode)
1286 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001287 * This is a wrapper around a for loop and is used like so::
Simon Glass28529762017-08-05 15:45:54 -06001288 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001289 * ofnode node;
1290 * ofnode_for_each_subnode(node, parent) {
1291 * Use node
1292 * ...
1293 * }
Simon Glass28529762017-08-05 15:45:54 -06001294 *
1295 * Note that this is implemented as a macro and @node is used as
1296 * iterator in the loop. The parent variable can be a constant or even a
1297 * literal.
1298 */
1299#define ofnode_for_each_subnode(node, parent) \
1300 for (node = ofnode_first_subnode(parent); \
1301 ofnode_valid(node); \
1302 node = ofnode_next_subnode(node))
1303
Mario Sixaefac062018-01-15 11:07:19 +01001304/**
Michael Wallea7b9df22021-10-15 15:15:17 +02001305 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1306 * compatible string
1307 *
1308 * @node: child node (ofnode, lvalue)
1309 * @compat: compatible string to match
1310 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001311 * This is a wrapper around a for loop and is used like so::
Michael Wallea7b9df22021-10-15 15:15:17 +02001312 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001313 * ofnode node;
1314 * ofnode_for_each_compatible_node(node, parent, compatible) {
1315 * Use node
1316 * ...
1317 * }
Michael Wallea7b9df22021-10-15 15:15:17 +02001318 *
1319 * Note that this is implemented as a macro and @node is used as
1320 * iterator in the loop.
1321 */
1322#define ofnode_for_each_compatible_node(node, compat) \
1323 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1324 ofnode_valid(node); \
1325 node = ofnode_by_compatible(node, compat))
1326
1327/**
developerd93c8b42020-05-02 11:35:09 +02001328 * ofnode_get_child_count() - get the child count of a ofnode
1329 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001330 * @parent: valid node to get its child count
1331 * Return: the number of subnodes
developerd93c8b42020-05-02 11:35:09 +02001332 */
1333int ofnode_get_child_count(ofnode parent);
1334
1335/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001336 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +01001337 *
1338 * Translate an address from the device-tree into a CPU physical address. This
1339 * function walks up the tree and applies the various bus mappings along the
1340 * way.
1341 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001342 * @node: Device tree node giving the context in which to translate the address
Mario Sixaefac062018-01-15 11:07:19 +01001343 * @in_addr: pointer to the address to translate
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001344 * Return: the translated address; OF_BAD_ADDR on error
Mario Sixaefac062018-01-15 11:07:19 +01001345 */
1346u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001347
1348/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001349 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1350 *
1351 * Translate a DMA address from the device-tree into a CPU physical address.
1352 * This function walks up the tree and applies the various bus mappings along
1353 * the way.
1354 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001355 * @node: Device tree node giving the context in which to translate the
1356 * DMA address
Fabien Dessenne22236e02019-05-31 15:11:30 +02001357 * @in_addr: pointer to the DMA address to translate
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001358 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne22236e02019-05-31 15:11:30 +02001359 */
1360u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1361
1362/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001363 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1364 *
1365 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1366 * cpu->bus address translations
1367 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001368 * @node: Device tree node
1369 * @cpu: Pointer to variable storing the range's cpu address
1370 * @bus: Pointer to variable storing the range's bus address
1371 * @size: Pointer to variable storing the range's size
1372 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001373 */
1374int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1375 u64 *size);
1376
1377/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001378 * ofnode_device_is_compatible() - check if the node is compatible with compat
1379 *
1380 * This allows to check whether the node is comaptible with the compat.
1381 *
1382 * @node: Device tree node for which compatible needs to be verified.
1383 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001384 * Return: true if OK, false if the compatible is not found
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001385 */
1386int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +02001387
1388/**
1389 * ofnode_write_prop() - Set a property of a ofnode
1390 *
Simon Glass17abed02022-09-06 20:27:32 -06001391 * Note that if @copy is false, the value passed to the function is *not*
1392 * allocated by the function itself, but must be allocated by the caller if
1393 * necessary. However it does allocate memory for the property struct and name.
Mario Six047dafc2018-06-26 08:46:48 +02001394 *
1395 * @node: The node for whose property should be set
1396 * @propname: The name of the property to set
Mario Six047dafc2018-06-26 08:46:48 +02001397 * @value: The new value of the property (must be valid prior to calling
1398 * the function)
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001399 * @len: The length of the new value of the property
Simon Glass17abed02022-09-06 20:27:32 -06001400 * @copy: true to allocate memory for the value. This only has any effect with
1401 * live tree, since flat tree handles this automatically. It allows a
1402 * node's value to be written to the tree, without requiring that the
1403 * caller allocate it
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001404 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001405 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001406int ofnode_write_prop(ofnode node, const char *propname, const void *value,
Simon Glass17abed02022-09-06 20:27:32 -06001407 int len, bool copy);
Mario Six047dafc2018-06-26 08:46:48 +02001408
1409/**
1410 * ofnode_write_string() - Set a string property of a ofnode
1411 *
1412 * Note that the value passed to the function is *not* allocated by the
1413 * function itself, but must be allocated by the caller if necessary.
1414 *
1415 * @node: The node for whose string property should be set
1416 * @propname: The name of the string property to set
1417 * @value: The new value of the string property (must be valid prior to
1418 * calling the function)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001419 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001420 */
1421int ofnode_write_string(ofnode node, const char *propname, const char *value);
1422
1423/**
Simon Glassd28e31e2022-07-30 15:52:14 -06001424 * ofnode_write_u32() - Set an integer property of an ofnode
1425 *
1426 * @node: The node for whose string property should be set
1427 * @propname: The name of the string property to set
1428 * @value: The new value of the 32-bit integer property
1429 * Return: 0 if successful, -ve on error
1430 */
1431int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1432
1433/**
Mario Six047dafc2018-06-26 08:46:48 +02001434 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1435 * ofnode
1436 *
1437 * This function effectively sets the node's "status" property to either "okay"
1438 * or "disable", hence making it available for driver model initialization or
1439 * not.
1440 *
1441 * @node: The node to enable
1442 * @value: Flag that tells the function to either disable or enable the
1443 * node
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001444 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001445 */
1446int ofnode_set_enabled(ofnode node, bool value);
1447
Simon Glass0034d962021-08-07 07:24:01 -06001448/**
Sean Anderson9b3a6392022-03-28 18:14:37 -04001449 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1450 *
1451 * This function parses PHY handle from the Ethernet controller's ofnode
1452 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1453 *
1454 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1455 * if the result to that is true, this function should not be called.
1456 *
1457 * @eth_node: ofnode belonging to the Ethernet controller
1458 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1459 */
1460ofnode ofnode_get_phy_node(ofnode eth_node);
1461
1462/**
1463 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1464 *
1465 * This function parses the "phy-mode" / "phy-connection-type" property and
1466 * returns the corresponding PHY interface type.
1467 *
1468 * @mac_node: ofnode containing the property
1469 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1470 * error
1471 */
1472phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1473
1474#if CONFIG_IS_ENABLED(DM)
1475/**
Simon Glass0034d962021-08-07 07:24:01 -06001476 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1477 *
1478 * This reads a property from the /config node of the devicetree.
1479 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001480 * This only works with the control FDT.
1481 *
1482 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass0034d962021-08-07 07:24:01 -06001483 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001484 * @prop_name: property name to look up
1485 * Return: true, if it exists, false if not
Simon Glass0034d962021-08-07 07:24:01 -06001486 */
1487bool ofnode_conf_read_bool(const char *prop_name);
1488
1489/**
1490 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1491 *
1492 * This reads a property from the /config node of the devicetree.
1493 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001494 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass0034d962021-08-07 07:24:01 -06001495 *
1496 * @prop_name: property name to look up
1497 * @default_val: default value to return if the property is not found
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001498 * Return: integer value, if found, or @default_val if not
Simon Glass0034d962021-08-07 07:24:01 -06001499 */
1500int ofnode_conf_read_int(const char *prop_name, int default_val);
1501
1502/**
1503 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1504 *
1505 * This reads a property from the /config node of the devicetree.
1506 *
Simon Glassc63ffd72022-09-06 20:27:28 -06001507 * This only works with the control FDT.
1508 *
1509 * See doc/device-tree-bindings/config.txt for bindings
Simon Glass0034d962021-08-07 07:24:01 -06001510 *
1511 * @prop_name: property name to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001512 * Return: string value, if found, or NULL if not
Simon Glass0034d962021-08-07 07:24:01 -06001513 */
1514const char *ofnode_conf_read_str(const char *prop_name);
1515
Michal Simek43c42bd2023-08-31 08:59:05 +02001516/**
1517 * ofnode_read_bootscript_address() - Read bootscr-address or bootscr-ram-offset
1518 *
1519 * @bootscr_address: pointer to 64bit address where bootscr-address property value
1520 * is stored
1521 * @bootscr_offset: pointer to 64bit offset address where bootscr-ram-offset
1522 * property value is stored
1523 *
1524 * This reads a bootscr-address or bootscr-ram-offset property from
1525 * the /options/u-boot/ node of the devicetree. bootscr-address holds the full
1526 * address of the boot script file. bootscr-ram-offset holds the boot script
1527 * file offset from the start of the ram base address. When bootscr-address is
1528 * defined, bootscr-ram-offset property is ignored.
1529 *
1530 * This only works with the control FDT.
1531 *
1532 * Return: 0 if OK, -EINVAL if property is not found.
1533 */
1534int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset);
1535
Michal Simek6a7c1ce2023-08-31 09:04:27 +02001536/**
1537 * ofnode_read_bootscript_flash() - Read bootscr-flash-offset/size
1538 *
1539 * @bootscr_flash_offset: pointer to 64bit offset where bootscr-flash-offset
1540 * property value is stored
1541 * @bootscr_flash_size: pointer to 64bit size where bootscr-flash-size property
1542 * value is stored
1543 *
1544 * This reads a bootscr-flash-offset and bootscr-flash-size properties from
1545 * the /options/u-boot/ node of the devicetree. bootscr-flash-offset holds
1546 * the offset of the boot script file from start of flash. bootscr-flash-size
1547 * holds the boot script size in flash. When bootscr-flash-size is not defined,
1548 * bootscr-flash-offset property is cleaned.
1549 *
1550 * This only works with the control FDT.
1551 *
1552 * Return: 0 if OK, -EINVAL if property is not found or incorrect.
1553 */
1554int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
1555 u64 *bootscr_flash_size);
1556
Sean Anderson9b3a6392022-03-28 18:14:37 -04001557#else /* CONFIG_DM */
1558static inline bool ofnode_conf_read_bool(const char *prop_name)
1559{
1560 return false;
1561}
Marek Behúnf4f1ddc2022-04-07 00:32:57 +02001562
Sean Anderson9b3a6392022-03-28 18:14:37 -04001563static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1564{
1565 return default_val;
1566}
1567
1568static inline const char *ofnode_conf_read_str(const char *prop_name)
1569{
1570 return NULL;
1571}
Simon Glass56bc3322022-09-06 20:27:02 -06001572
Michal Simek43c42bd2023-08-31 08:59:05 +02001573static inline int ofnode_read_bootscript_address(u64 *bootscr_address, u64 *bootscr_offset)
1574{
1575 return -EINVAL;
1576}
1577
Michal Simek6a7c1ce2023-08-31 09:04:27 +02001578static inline int ofnode_read_bootscript_flash(u64 *bootscr_flash_offset,
1579 u64 *bootscr_flash_size)
1580{
1581 return -EINVAL;
1582}
1583
Sean Anderson9b3a6392022-03-28 18:14:37 -04001584#endif /* CONFIG_DM */
Marek Behúnbc194772022-04-07 00:33:01 +02001585
Simon Glass56bc3322022-09-06 20:27:02 -06001586/**
1587 * of_add_subnode() - add a new subnode to a node
1588 *
1589 * @parent: parent node to add to
1590 * @name: name of subnode
1591 * @nodep: returns pointer to new subnode (valid if the function returns 0
1592 * or -EEXIST)
1593 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1594 * -ve on other error
1595 */
1596int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1597
Simon Glass7a7229a2022-09-06 20:27:33 -06001598/**
1599 * ofnode_copy_props() - copy all properties from one node to another
1600 *
Simon Glass68164892023-09-26 08:14:37 -06001601 * Makes a copy of all properties from the source node to the destination node.
Simon Glass7a7229a2022-09-06 20:27:33 -06001602 * Existing properties in the destination node remain unchanged, except that
1603 * any with the same name are overwritten, including changing the size of the
1604 * property.
1605 *
1606 * For livetree, properties are copied / allocated, so the source tree does not
1607 * need to be present afterwards.
1608 *
Simon Glass68164892023-09-26 08:14:37 -06001609 * @dst: Destination node to write properties to
Simon Glass7a7229a2022-09-06 20:27:33 -06001610 * @src: Source node to read properties from
Simon Glass7a7229a2022-09-06 20:27:33 -06001611 */
Simon Glass68164892023-09-26 08:14:37 -06001612int ofnode_copy_props(ofnode dst, ofnode src);
Simon Glass7a7229a2022-09-06 20:27:33 -06001613
Simon Glass9a148602017-05-17 17:18:10 -06001614#endif