blob: 044546f00512886d0b5e411777dc4de95ca26858 [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>
Simon Glass9a148602017-05-17 17:18:10 -060023
Simon Glassc4fc5622017-05-18 20:08:58 -060024struct ofnode_phandle_args {
25 ofnode node;
26 int args_count;
27 uint32_t args[OF_MAX_PHANDLE_ARGS];
28};
29
30/**
Stefan Roesec14df8b2020-09-23 08:23:27 +020031 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -060032 *
33 * This cannot be called if the reference contains an offset.
34 *
35 * @node: Reference containing struct device_node * (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +010036 * Return: pointer to device node (can be NULL)
Simon Glassc4fc5622017-05-18 20:08:58 -060037 */
Simon Glass9036c5c2022-09-06 20:27:04 -060038static inline struct device_node *ofnode_to_np(ofnode node)
Simon Glassc4fc5622017-05-18 20:08:58 -060039{
40#ifdef OF_CHECKS
41 if (!of_live_active())
42 return NULL;
43#endif
44 return node.np;
45}
46
Simon Glass9a148602017-05-17 17:18:10 -060047/**
48 * ofnode_to_offset() - convert an ofnode to a flat DT offset
49 *
50 * This cannot be called if the reference contains a node pointer.
51 *
52 * @node: Reference containing offset (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +010053 * Return: DT offset (can be -1)
Simon Glass9a148602017-05-17 17:18:10 -060054 */
55static inline int ofnode_to_offset(ofnode node)
56{
Simon Glassc4fc5622017-05-18 20:08:58 -060057#ifdef OF_CHECKS
58 if (of_live_active())
59 return -1;
60#endif
Simon Glass9a148602017-05-17 17:18:10 -060061 return node.of_offset;
62}
63
64/**
65 * ofnode_valid() - check if an ofnode is valid
66 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +010067 * @node: Reference containing offset (possibly invalid)
68 * Return: true if the reference contains a valid ofnode, false if it is NULL
Simon Glass9a148602017-05-17 17:18:10 -060069 */
70static inline bool ofnode_valid(ofnode node)
71{
Simon Glassc4fc5622017-05-18 20:08:58 -060072 if (of_live_active())
73 return node.np != NULL;
74 else
Patrick Delaunay04fcfe72020-09-24 17:26:20 +020075 return node.of_offset >= 0;
Simon Glass9a148602017-05-17 17:18:10 -060076}
77
78/**
79 * offset_to_ofnode() - convert a DT offset to an ofnode
80 *
81 * @of_offset: DT offset (either valid, or -1)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +010082 * Return: reference to the associated DT offset
Simon Glass9a148602017-05-17 17:18:10 -060083 */
84static inline ofnode offset_to_ofnode(int of_offset)
85{
86 ofnode node;
87
Simon Glassc4fc5622017-05-18 20:08:58 -060088 if (of_live_active())
89 node.np = NULL;
90 else
Simon Glass1ab8b892019-12-06 21:41:36 -070091 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass9a148602017-05-17 17:18:10 -060092
93 return node;
94}
95
96/**
Simon Glassc4fc5622017-05-18 20:08:58 -060097 * np_to_ofnode() - convert a node pointer to an ofnode
98 *
99 * @np: Live node pointer (can be NULL)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100100 * Return: reference to the associated node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -0600101 */
Simon Glass9036c5c2022-09-06 20:27:04 -0600102static inline ofnode np_to_ofnode(struct device_node *np)
Simon Glassc4fc5622017-05-18 20:08:58 -0600103{
104 ofnode node;
105
106 node.np = np;
107
108 return node;
109}
110
111/**
112 * ofnode_is_np() - check if a reference is a node pointer
113 *
114 * This function associated that if there is a valid live tree then all
115 * references will use it. This is because using the flat DT when the live tree
116 * is valid is not permitted.
117 *
118 * @node: reference to check (possibly invalid)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100119 * Return: true if the reference is a live node pointer, false if it is a DT
Simon Glassc4fc5622017-05-18 20:08:58 -0600120 * offset
121 */
122static inline bool ofnode_is_np(ofnode node)
123{
124#ifdef OF_CHECKS
125 /*
126 * Check our assumption that flat tree offsets are not used when a
127 * live tree is in use.
128 */
129 assert(!ofnode_valid(node) ||
Stefan Roesec14df8b2020-09-23 08:23:27 +0200130 (of_live_active() ? ofnode_to_np(node)
131 : ofnode_to_np(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600132#endif
133 return of_live_active() && ofnode_valid(node);
134}
135
136/**
Simon Glass9a148602017-05-17 17:18:10 -0600137 * ofnode_equal() - check if two references are equal
138 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100139 * @ref1: first reference to check (possibly invalid)
140 * @ref2: second reference to check (possibly invalid)
141 * Return: true if equal, else false
Simon Glass9a148602017-05-17 17:18:10 -0600142 */
143static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
144{
145 /* We only need to compare the contents */
146 return ref1.of_offset == ref2.of_offset;
147}
148
Simon Glassc4fc5622017-05-18 20:08:58 -0600149/**
150 * ofnode_null() - Obtain a null ofnode
151 *
152 * This returns an ofnode which points to no node. It works both with the flat
153 * tree and livetree.
154 */
155static inline ofnode ofnode_null(void)
156{
157 ofnode node;
158
159 if (of_live_active())
160 node.np = NULL;
161 else
162 node.of_offset = -1;
163
164 return node;
165}
166
Simon Glass278ddba2020-11-28 17:50:07 -0700167static inline ofnode ofnode_root(void)
168{
169 ofnode node;
170
171 if (of_live_active())
172 node.np = gd_of_root();
173 else
174 node.of_offset = 0;
175
176 return node;
177}
178
Simon Glassc4fc5622017-05-18 20:08:58 -0600179/**
Simon Glassef75c592022-07-30 15:52:08 -0600180 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
181 *
182 * Returns: reference to the control FDT
183 */
184static inline oftree oftree_default(void)
185{
186 oftree tree;
187
188 if (of_live_active())
189 tree.np = gd_of_root();
190 else
191 tree.fdt = (void *)gd->fdt_blob;
192
193 return tree;
194}
195
196/**
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530197 * ofnode_name_eq() - Check if the node name is equivalent to a given name
198 * ignoring the unit address
199 *
200 * @node: valid node reference that has to be compared
201 * @name: name that has to be compared with the node name
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100202 * Return: true if matches, false if it doesn't match
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +0530203 */
204bool ofnode_name_eq(ofnode node, const char *name);
205
206/**
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200207 * ofnode_read_u8() - Read a 8-bit integer from a property
208 *
209 * @node: valid node reference to read property from
210 * @propname: name of the property to read from
211 * @outp: place to put value (if found)
212 * Return: 0 if OK, -ve on error
213 */
214int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
215
216/**
217 * ofnode_read_u8_default() - Read a 8-bit integer from a property
218 *
219 * @node: valid node reference to read property from
220 * @propname: name of the property to read from
221 * @def: default value to return if the property has no value
222 * Return: property value, or @def if not found
223 */
224u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
225
226/**
227 * ofnode_read_u16() - Read a 16-bit integer from a property
228 *
229 * @node: valid node reference to read property from
230 * @propname: name of the property to read from
231 * @outp: place to put value (if found)
232 * Return: 0 if OK, -ve on error
233 */
234int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
235
236/**
237 * ofnode_read_u16_default() - Read a 16-bit integer from a property
238 *
239 * @node: valid node reference to read property from
240 * @propname: name of the property to read from
241 * @def: default value to return if the property has no value
242 * Return: property value, or @def if not found
243 */
244u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
245
246/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600247 * ofnode_read_u32() - Read a 32-bit integer from a property
248 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100249 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600250 * @propname: name of the property to read from
251 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100252 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600253 */
254int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
255
256/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200257 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
258 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100259 * @node: valid node reference to read property from
Dario Binacchi81d80b52020-03-29 18:04:41 +0200260 * @propname: name of the property to read from
261 * @index: index of the integer to return
262 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100263 * Return: 0 if OK, -ve on error
Dario Binacchi81d80b52020-03-29 18:04:41 +0200264 */
265int ofnode_read_u32_index(ofnode node, const char *propname, int index,
266 u32 *outp);
267
268/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600269 * ofnode_read_s32() - Read a 32-bit integer from a property
270 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100271 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600272 * @propname: name of the property to read from
273 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100274 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600275 */
276static inline int ofnode_read_s32(ofnode node, const char *propname,
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100277 s32 *outp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600278{
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100279 return ofnode_read_u32(node, propname, (u32 *)outp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600280}
281
282/**
283 * ofnode_read_u32_default() - Read a 32-bit integer from a property
284 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100285 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600286 * @propname: name of the property to read from
287 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100288 * Return: property value, or @def if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600289 */
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100290u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600291
292/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200293 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
294 * property
295 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100296 * @node: valid node reference to read property from
Dario Binacchi81d80b52020-03-29 18:04:41 +0200297 * @propname: name of the property to read from
298 * @index: index of the integer to return
299 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100300 * Return: property value, or @def if not found
Dario Binacchi81d80b52020-03-29 18:04:41 +0200301 */
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100302u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
Dario Binacchi81d80b52020-03-29 18:04:41 +0200303 u32 def);
304
305/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600306 * ofnode_read_s32_default() - Read a 32-bit integer from a property
307 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100308 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600309 * @propname: name of the property to read from
310 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100311 * Return: property value, or @def if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600312 */
313int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
314
315/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100316 * ofnode_read_u64() - Read a 64-bit integer from a property
317 *
318 * @node: valid node reference to read property from
319 * @propname: name of the property to read from
320 * @outp: place to put value (if found)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100321 * Return: 0 if OK, -ve on error
Lukas Auerb03a60b2018-11-22 11:26:35 +0100322 */
323int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
324
325/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600326 * ofnode_read_u64_default() - Read a 64-bit integer from a property
327 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100328 * @node: valid node reference to read property from
Simon Glass9d54a7a2018-06-11 13:07:10 -0600329 * @propname: name of the property to read from
330 * @def: default value to return if the property has no value
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100331 * Return: property value, or @def if not found
Simon Glass9d54a7a2018-06-11 13:07:10 -0600332 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200333u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600334
335/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700336 * ofnode_read_prop() - Read a property from a node
337 *
338 * @node: valid node reference to read property from
339 * @propname: name of the property to read
340 * @sizep: if non-NULL, returns the size of the property, or an error code
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100341 * if not found
342 * Return: property value, or NULL if there is no such property
Simon Glass0c2e9802020-01-27 08:49:44 -0700343 */
344const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
345
346/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600347 * ofnode_read_string() - Read a string from a property
348 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700349 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600350 * @propname: name of the property to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100351 * Return: string from property value, or NULL if there is no such property
Simon Glassc4fc5622017-05-18 20:08:58 -0600352 */
353const char *ofnode_read_string(ofnode node, const char *propname);
354
355/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600356 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600357 *
358 * @node: valid node reference to read property from
359 * @propname: name of the property to read
360 * @out_values: pointer to return value, modified only if return value is 0
361 * @sz: number of array elements to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100362 * Return: 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600363 *
364 * Search for a property in a device node and read 32-bit value(s) from
365 * it. Returns 0 on success, -EINVAL if the property does not exist,
366 * -ENODATA if property does not have a value, and -EOVERFLOW if the
367 * property data isn't large enough.
368 *
369 * The out_values is modified only if a valid u32 value can be decoded.
370 */
371int ofnode_read_u32_array(ofnode node, const char *propname,
372 u32 *out_values, size_t sz);
373
374/**
375 * ofnode_read_bool() - read a boolean value from a property
376 *
377 * @node: valid node reference to read property from
378 * @propname: name of property to read
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100379 * Return: true if property is present (meaning true), false if not present
Simon Glassc4fc5622017-05-18 20:08:58 -0600380 */
381bool ofnode_read_bool(ofnode node, const char *propname);
382
383/**
384 * ofnode_find_subnode() - find a named subnode of a parent node
385 *
386 * @node: valid reference to parent node
387 * @subnode_name: name of subnode to find
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100388 * Return: reference to subnode (which can be invalid if there is no such
Simon Glassc4fc5622017-05-18 20:08:58 -0600389 * subnode)
390 */
391ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
392
Simon Glass39f1d282020-12-16 17:25:06 -0700393#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass3ba929a2020-10-30 21:38:53 -0600394#include <asm/global_data.h>
395
Simon Glass39f1d282020-12-16 17:25:06 -0700396static inline bool ofnode_is_enabled(ofnode node)
397{
398 if (ofnode_is_np(node)) {
399 return of_device_is_available(ofnode_to_np(node));
400 } else {
401 return fdtdec_get_is_enabled(gd->fdt_blob,
402 ofnode_to_offset(node));
403 }
404}
405
406static inline ofnode ofnode_first_subnode(ofnode node)
407{
408 assert(ofnode_valid(node));
409 if (ofnode_is_np(node))
410 return np_to_ofnode(node.np->child);
411
412 return offset_to_ofnode(
413 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
414}
415
416static inline ofnode ofnode_next_subnode(ofnode node)
417{
418 assert(ofnode_valid(node));
419 if (ofnode_is_np(node))
420 return np_to_ofnode(node.np->sibling);
421
422 return offset_to_ofnode(
423 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
424}
425#else
426/**
427 * ofnode_is_enabled() - Checks whether a node is enabled.
428 * This looks for a 'status' property. If this exists, then returns true if
429 * the status is 'okay' and false otherwise. If there is no status property,
430 * it returns true on the assumption that anything mentioned should be enabled
431 * by default.
432 *
433 * @node: node to examine
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100434 * Return: false (not enabled) or true (enabled)
Simon Glass39f1d282020-12-16 17:25:06 -0700435 */
436bool ofnode_is_enabled(ofnode node);
437
Simon Glassc4fc5622017-05-18 20:08:58 -0600438/**
439 * ofnode_first_subnode() - find the first subnode of a parent node
440 *
441 * @node: valid reference to a valid parent node
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100442 * Return: reference to the first subnode (which can be invalid if the parent
Simon Glassc4fc5622017-05-18 20:08:58 -0600443 * node has no subnodes)
444 */
445ofnode ofnode_first_subnode(ofnode node);
446
447/**
448 * ofnode_next_subnode() - find the next sibling of a subnode
449 *
450 * @node: valid reference to previous node (sibling)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100451 * Return: reference to the next subnode (which can be invalid if the node
Simon Glassc4fc5622017-05-18 20:08:58 -0600452 * has no more siblings)
453 */
454ofnode ofnode_next_subnode(ofnode node);
Simon Glass39f1d282020-12-16 17:25:06 -0700455#endif /* DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600456
457/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100458 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
459 *
460 * @node: valid node to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100461 * Return: ofnode reference of the parent node
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100462 */
463ofnode ofnode_get_parent(ofnode node);
464
465/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600466 * ofnode_get_name() - get the name of a node
467 *
468 * @node: valid node to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100469 * Return: name of node
Simon Glassc4fc5622017-05-18 20:08:58 -0600470 */
471const char *ofnode_get_name(ofnode node);
472
473/**
Marek Behúne897e3c2021-05-26 14:08:18 +0200474 * ofnode_get_path() - get the full path of a node
475 *
476 * @node: valid node to look up
477 * @buf: buffer to write the node path into
478 * @buflen: buffer size
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100479 * Return: 0 if OK, -ve on error
Marek Behúne897e3c2021-05-26 14:08:18 +0200480 */
481int ofnode_get_path(ofnode node, char *buf, int buflen);
482
483/**
Kever Yang37df0e02018-02-23 17:38:50 +0100484 * ofnode_get_by_phandle() - get ofnode from phandle
485 *
Simon Glass176dd432022-09-06 20:26:57 -0600486 * This uses the default (control) device tree
487 *
Kever Yang37df0e02018-02-23 17:38:50 +0100488 * @phandle: phandle to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100489 * Return: ofnode reference to the phandle
Kever Yang37df0e02018-02-23 17:38:50 +0100490 */
491ofnode ofnode_get_by_phandle(uint phandle);
492
493/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600494 * ofnode_read_size() - read the size of a property
495 *
496 * @node: node to check
497 * @propname: property to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100498 * Return: size of property if present, or -EINVAL if not
Simon Glassc4fc5622017-05-18 20:08:58 -0600499 */
500int ofnode_read_size(ofnode node, const char *propname);
501
502/**
Keerthyd332e6e2019-04-24 17:19:53 +0530503 * ofnode_get_addr_size_index() - get an address/size from a node
504 * based on index
505 *
506 * This reads the register address/size from a node based on index
507 *
508 * @node: node to read from
509 * @index: Index of address to read (0 for first)
510 * @size: Pointer to size of the address
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100511 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Keerthyd332e6e2019-04-24 17:19:53 +0530512 */
513phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
514 fdt_size_t *size);
515
516/**
Marek Behún177ab7f2021-05-26 14:08:17 +0200517 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
518 * based on index, without address
519 * translation
520 *
521 * This reads the register address/size from a node based on index.
522 * The resulting address is not translated. Useful for example for on-disk
523 * addresses.
524 *
525 * @node: node to read from
526 * @index: Index of address to read (0 for first)
527 * @size: Pointer to size of the address
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100528 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Marek Behún177ab7f2021-05-26 14:08:17 +0200529 */
530phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
531 fdt_size_t *size);
532
533/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600534 * ofnode_get_addr_index() - get an address from a node
535 *
536 * This reads the register address from a node
537 *
538 * @node: node to read from
539 * @index: Index of address to read (0 for first)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100540 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glass049ae1b2017-05-18 20:09:01 -0600541 */
542phys_addr_t ofnode_get_addr_index(ofnode node, int index);
543
544/**
545 * ofnode_get_addr() - get an address from a node
546 *
547 * This reads the register address from a node
548 *
549 * @node: node to read from
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100550 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
Simon Glass049ae1b2017-05-18 20:09:01 -0600551 */
552phys_addr_t ofnode_get_addr(ofnode node);
553
554/**
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800555 * ofnode_get_size() - get size from a node
556 *
557 * This reads the register size from a node
558 *
559 * @node: node to read from
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100560 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800561 */
562fdt_size_t ofnode_get_size(ofnode node);
563
564/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600565 * ofnode_stringlist_search() - find a string in a string list and return index
566 *
567 * Note that it is possible for this function to succeed on property values
568 * that are not NUL-terminated. That's because the function will stop after
569 * finding the first occurrence of @string. This can for example happen with
570 * small-valued cell properties, such as #address-cells, when searching for
571 * the empty string.
572 *
573 * @node: node to check
574 * @propname: name of the property containing the string list
575 * @string: string to look up in the string list
576 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100577 * Return:
Simon Glassc4fc5622017-05-18 20:08:58 -0600578 * the index of the string in the list of strings
579 * -ENODATA if the property is not found
580 * -EINVAL on some other error
581 */
582int ofnode_stringlist_search(ofnode node, const char *propname,
583 const char *string);
584
585/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600586 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600587 *
588 * Note that this will successfully extract strings from properties with
589 * non-NUL-terminated values. For example on small-valued cell properties
590 * this function will return the empty string.
591 *
592 * If non-NULL, the length of the string (on success) or a negative error-code
593 * (on failure) will be stored in the integer pointer to by lenp.
594 *
595 * @node: node to check
596 * @propname: name of the property containing the string list
Simon Glass7a3a1672021-10-23 17:26:06 -0600597 * @index: index of the string to return (cannot be negative)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100598 * @outp: return location for the string
Simon Glassc4fc5622017-05-18 20:08:58 -0600599 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100600 * Return:
Simon Glass7a3a1672021-10-23 17:26:06 -0600601 * 0 if found or -ve error value if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600602 */
603int ofnode_read_string_index(ofnode node, const char *propname, int index,
604 const char **outp);
605
606/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600607 * ofnode_read_string_count() - find the number of strings in a string list
608 *
609 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100610 * @property: name of the property containing the string list
611 * Return:
Simon Glass5fdb0052017-06-12 06:21:28 -0600612 * number of strings in the list, or -ve error value if not found
613 */
614int ofnode_read_string_count(ofnode node, const char *property);
615
616/**
Simon Glass9580bfc2021-10-23 17:26:07 -0600617 * ofnode_read_string_list() - read a list of strings
618 *
619 * This produces a list of string pointers with each one pointing to a string
620 * in the string list. If the property does not exist, it returns {NULL}.
621 *
622 * The data is allocated and the caller is reponsible for freeing the return
623 * value (the list of string pointers). The strings themselves may not be
624 * changed as they point directly into the devicetree property.
625 *
626 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100627 * @property: name of the property containing the string list
Simon Glass9580bfc2021-10-23 17:26:07 -0600628 * @listp: returns an allocated, NULL-terminated list of strings if the return
629 * value is > 0, else is set to NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100630 * Return:
631 * number of strings in list, 0 if none, -ENOMEM if out of memory,
632 * -EINVAL if no such property, -EENODATA if property is empty
Simon Glass9580bfc2021-10-23 17:26:07 -0600633 */
634int ofnode_read_string_list(ofnode node, const char *property,
635 const char ***listp);
636
637/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600638 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
639 *
640 * This function is useful to parse lists of phandles and their arguments.
641 * Returns 0 on success and fills out_args, on error returns appropriate
642 * errno value.
643 *
644 * Caller is responsible to call of_node_put() on the returned out_args->np
645 * pointer.
646 *
647 * Example:
648 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100649 * .. code-block::
Simon Glassc4fc5622017-05-18 20:08:58 -0600650 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100651 * phandle1: node1 {
652 * #list-cells = <2>;
653 * };
654 * phandle2: node2 {
655 * #list-cells = <1>;
656 * };
657 * node3 {
658 * list = <&phandle1 1 2 &phandle2 3>;
659 * };
Simon Glassc4fc5622017-05-18 20:08:58 -0600660 *
661 * To get a device_node of the `node2' node you may call this:
662 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
663 *
664 * @node: device tree node containing a list
665 * @list_name: property name that contains a list
666 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100667 * @cell_count: Cell count to use if @cells_name is NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600668 * @index: index of a phandle to parse out
669 * @out_args: optional pointer to output arguments structure (will be filled)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100670 * Return:
671 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
672 * @list_name does not exist, -EINVAL if a phandle was not found,
673 * @cells_name could not be found, the arguments were truncated or there
674 * were too many arguments.
Simon Glassc4fc5622017-05-18 20:08:58 -0600675 */
676int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
677 const char *cells_name, int cell_count,
678 int index,
679 struct ofnode_phandle_args *out_args);
680
681/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200682 * ofnode_count_phandle_with_args() - Count number of phandle in a list
683 *
684 * This function is useful to count phandles into a list.
685 * Returns number of phandle on success, on error returns appropriate
686 * errno value.
687 *
688 * @node: device tree node containing a list
689 * @list_name: property name that contains a list
690 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100691 * @cell_count: Cell count to use if @cells_name is NULL
692 * Return:
693 * number of phandle on success, -ENOENT if @list_name does not exist,
694 * -EINVAL if a phandle was not found, @cells_name could not be found.
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200695 */
696int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200697 const char *cells_name, int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200698
699/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600700 * ofnode_path() - find a node by full path
701 *
Simon Glassef75c592022-07-30 15:52:08 -0600702 * This uses the control FDT.
703 *
Simon Glassc4fc5622017-05-18 20:08:58 -0600704 * @path: Full path to node, e.g. "/bus/spi@1"
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100705 * Return: reference to the node found. Use ofnode_valid() to check if it exists
Simon Glassc4fc5622017-05-18 20:08:58 -0600706 */
707ofnode ofnode_path(const char *path);
708
709/**
Simon Glassef75c592022-07-30 15:52:08 -0600710 * ofnode_path_root() - find a node by full path from a root node
711 *
712 * @tree: Device tree to use
713 * @path: Full path to node, e.g. "/bus/spi@1"
714 * Return: reference to the node found. Use ofnode_valid() to check if it exists
715 */
716ofnode ofnode_path_root(oftree tree, const char *path);
717
718/**
Simon Glasse09223c2020-01-27 08:49:46 -0700719 * ofnode_read_chosen_prop() - get the value of a chosen property
720 *
721 * This looks for a property within the /chosen node and returns its value
722 *
723 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100724 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Simon Glasse09223c2020-01-27 08:49:46 -0700725 * returns NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100726 * Return: property value if found, else NULL
Simon Glasse09223c2020-01-27 08:49:46 -0700727 */
728const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
729
730/**
Simon Glassf3455962020-01-27 08:49:43 -0700731 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -0600732 *
Simon Glassf3455962020-01-27 08:49:43 -0700733 * This looks for a property within the /chosen node and returns its value,
734 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -0600735 *
736 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100737 * Return: string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600738 */
Simon Glassf3455962020-01-27 08:49:43 -0700739const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600740
741/**
Simon Glassc99ba912020-01-27 08:49:42 -0700742 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -0600743 *
Simon Glassc99ba912020-01-27 08:49:42 -0700744 * This looks up a named property in the chosen node and uses that as a path to
745 * look up a code.
746 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100747 * @propname: Property name to look for
748 * Return: the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -0600749 */
Simon Glassc99ba912020-01-27 08:49:42 -0700750ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600751
Michal Simek92a88622020-07-28 12:51:08 +0200752/**
753 * ofnode_read_aliases_prop() - get the value of a aliases property
754 *
755 * This looks for a property within the /aliases node and returns its value
756 *
757 * @propname: Property name to look for
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100758 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
Michal Simek92a88622020-07-28 12:51:08 +0200759 * returns NULL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100760 * Return: property value if found, else NULL
Michal Simek92a88622020-07-28 12:51:08 +0200761 */
762const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
763
764/**
765 * ofnode_get_aliases_node() - get a referenced node from the aliases node
766 *
767 * This looks up a named property in the aliases node and uses that as a path to
768 * look up a code.
769 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100770 * @propname: Property name to look for
771 * Return: the referenced node if present, else ofnode_null()
Michal Simek92a88622020-07-28 12:51:08 +0200772 */
773ofnode ofnode_get_aliases_node(const char *propname);
774
Simon Glassc4fc5622017-05-18 20:08:58 -0600775struct display_timing;
776/**
777 * ofnode_decode_display_timing() - decode display timings
778 *
779 * Decode display timings from the supplied 'display-timings' node.
780 * See doc/device-tree-bindings/video/display-timing.txt for binding
781 * information.
782 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100783 * @node: 'display-timing' node containing the timing subnodes
784 * @index: Index number to read (0=first timing subnode)
785 * @config: Place to put timings
786 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600787 */
788int ofnode_decode_display_timing(ofnode node, int index,
789 struct display_timing *config);
790
791/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100792 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -0600793 *
794 * @node: node to read
795 * @propname: property to read
796 * @lenp: place to put length on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100797 * Return: pointer to property, or NULL if not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600798 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900799const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600800
801/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100802 * ofnode_get_first_property()- get the reference of the first property
803 *
804 * Get reference to the first property of the node, it is used to iterate
805 * and read all the property with ofnode_get_property_by_prop().
806 *
807 * @node: node to read
808 * @prop: place to put argument reference
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100809 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100810 */
811int ofnode_get_first_property(ofnode node, struct ofprop *prop);
812
813/**
814 * ofnode_get_next_property() - get the reference of the next property
815 *
816 * Get reference to the next property of the node, it is used to iterate
817 * and read all the property with ofnode_get_property_by_prop().
818 *
819 * @prop: reference of current argument and place to put reference of next one
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100820 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100821 */
822int ofnode_get_next_property(struct ofprop *prop);
823
824/**
825 * ofnode_get_property_by_prop() - get a pointer to the value of a property
826 *
827 * Get value for the property identified by the provided reference.
828 *
829 * @prop: reference on property
830 * @propname: If non-NULL, place to property name on success,
831 * @lenp: If non-NULL, place to put length on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100832 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100833 */
834const void *ofnode_get_property_by_prop(const struct ofprop *prop,
835 const char **propname, int *lenp);
836
837/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600838 * ofnode_is_available() - check if a node is marked available
839 *
840 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100841 * Return: true if node's 'status' property is "okay" (or is missing)
Simon Glassc4fc5622017-05-18 20:08:58 -0600842 */
843bool ofnode_is_available(ofnode node);
844
845/**
846 * ofnode_get_addr_size() - get address and size from a property
847 *
848 * This does no address translation. It simply reads an property that contains
849 * an address and a size value, one after the other.
850 *
851 * @node: node to read from
852 * @propname: property to read
853 * @sizep: place to put size value (on success)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100854 * Return: address value, or FDT_ADDR_T_NONE on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600855 */
856phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
857 phys_size_t *sizep);
858
859/**
860 * ofnode_read_u8_array_ptr() - find an 8-bit array
861 *
862 * Look up a property in a node and return a pointer to its contents as a
863 * byte array of given length. The property must have at least enough data
864 * for the array (count bytes). It may have more, but this will be ignored.
865 * The data is not copied.
866 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100867 * @node: node to examine
868 * @propname: name of property to find
869 * @sz: number of array elements
870 * Return:
871 * pointer to byte array if found, or NULL if the property is not found or
872 * there is not enough data
Simon Glassc4fc5622017-05-18 20:08:58 -0600873 */
874const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
875 size_t sz);
876
877/**
878 * ofnode_read_pci_addr() - look up a PCI address
879 *
880 * Look at an address property in a node and return the PCI address which
881 * corresponds to the given type in the form of fdt_pci_addr.
882 * The property must hold one fdt_pci_addr with a lengh.
883 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100884 * @node: node to examine
885 * @type: pci address type (FDT_PCI_SPACE_xxx)
886 * @propname: name of property to find
887 * @addr: returns pci address in the form of fdt_pci_addr
888 * Return:
889 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
890 * format of the property was invalid, -ENXIO if the requested
891 * address type was not found
Simon Glassc4fc5622017-05-18 20:08:58 -0600892 */
893int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
894 const char *propname, struct fdt_pci_addr *addr);
895
896/**
Bin Mengfa157712018-08-03 01:14:35 -0700897 * ofnode_read_pci_vendev() - look up PCI vendor and device id
898 *
899 * Look at the compatible property of a device node that represents a PCI
900 * device and extract pci vendor id and device id from it.
901 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100902 * @node: node to examine
903 * @vendor: vendor id of the pci device
904 * @device: device id of the pci device
905 * Return: 0 if ok, negative on error
Bin Mengfa157712018-08-03 01:14:35 -0700906 */
907int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
908
909/**
Michal Simeka253c3b2022-02-23 15:45:40 +0100910 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
911 *
912 * Look at the compatible property of a device node that represents a eth phy
913 * device and extract phy vendor id and device id from it.
914 *
Heinrich Schuchardtc9ae4a82022-03-24 16:22:32 +0100915 * @node: node to examine
916 * @vendor: vendor id of the eth phy device
917 * @device: device id of the eth phy device
918 * Return: 0 if ok, negative on error
Michal Simeka253c3b2022-02-23 15:45:40 +0100919 */
920int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
921
922/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600923 * ofnode_read_addr_cells() - Get the number of address cells for a node
924 *
925 * This walks back up the tree to find the closest #address-cells property
926 * which controls the given node.
927 *
928 * @node: Node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100929 * Return: number of address cells this node uses
Simon Glassc4fc5622017-05-18 20:08:58 -0600930 */
931int ofnode_read_addr_cells(ofnode node);
932
933/**
934 * ofnode_read_size_cells() - Get the number of size cells for a node
935 *
936 * This walks back up the tree to find the closest #size-cells property
937 * which controls the given node.
938 *
939 * @node: Node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100940 * Return: number of size cells this node uses
Simon Glassc4fc5622017-05-18 20:08:58 -0600941 */
942int ofnode_read_size_cells(ofnode node);
943
944/**
Simon Glass4191dc12017-06-12 06:21:31 -0600945 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
946 *
947 * This function matches fdt_address_cells().
948 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100949 * @node: Node to check
950 * Return: value of #address-cells property in this node, or 2 if none
Simon Glass4191dc12017-06-12 06:21:31 -0600951 */
952int ofnode_read_simple_addr_cells(ofnode node);
953
954/**
955 * ofnode_read_simple_size_cells() - Get the size cells property in a node
956 *
957 * This function matches fdt_size_cells().
958 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100959 * @node: Node to check
960 * Return: value of #size-cells property in this node, or 2 if none
Simon Glass4191dc12017-06-12 06:21:31 -0600961 */
962int ofnode_read_simple_size_cells(ofnode node);
963
964/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600965 * ofnode_pre_reloc() - check if a node should be bound before relocation
966 *
967 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
968 * via special device tree properties.
969 *
970 * Before relocation this function can be used to check if nodes are required
971 * in either SPL or TPL stages.
972 *
973 * After relocation and jumping into the real U-Boot binary it is possible to
974 * determine if a node was bound in one of SPL/TPL stages.
975 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200976 * There are 4 settings currently in use
977 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glassc4fc5622017-05-18 20:08:58 -0600978 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100979 * Existing platforms only use it to indicate nodes needed in
980 * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200981 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
982 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glassc4fc5622017-05-18 20:08:58 -0600983 *
984 * @node: node to check
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100985 * Return: true if node is needed in SPL/TL, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -0600986 */
987bool ofnode_pre_reloc(ofnode node);
988
Simon Glassa8173d62018-06-11 13:07:12 -0600989/**
990 * ofnode_read_resource() - Read a resource from a node
991 *
992 * Read resource information from a node at the given index
993 *
994 * @node: Node to read from
995 * @index: Index of resource to read (0 = first)
996 * @res: Returns resource that was read, on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +0100997 * Return: 0 if OK, -ve on error
Simon Glassa8173d62018-06-11 13:07:12 -0600998 */
Simon Glassf7bfcc42017-07-25 08:29:55 -0600999int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -06001000
1001/**
1002 * ofnode_read_resource_byname() - Read a resource from a node by name
1003 *
1004 * Read resource information from a node matching the given name. This uses a
1005 * 'reg-names' string list property with the names matching the associated
1006 * 'reg' property list.
1007 *
1008 * @node: Node to read from
1009 * @name: Name of resource to read
1010 * @res: Returns resource that was read, on success
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001011 * Return: 0 if OK, -ve on error
Simon Glassa8173d62018-06-11 13:07:12 -06001012 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001013int ofnode_read_resource_byname(ofnode node, const char *name,
1014 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -06001015
Simon Glass28529762017-08-05 15:45:54 -06001016/**
Simon Glass954eeae2018-06-11 13:07:13 -06001017 * ofnode_by_compatible() - Find the next compatible node
1018 *
1019 * Find the next node after @from that is compatible with @compat
1020 *
1021 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1022 * @compat: Compatible string to match
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001023 * Return: ofnode found, or ofnode_null() if none
Simon Glass954eeae2018-06-11 13:07:13 -06001024 */
1025ofnode ofnode_by_compatible(ofnode from, const char *compat);
1026
1027/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001028 * ofnode_by_prop_value() - Find the next node with given property value
1029 *
1030 * Find the next node after @from that has a @propname with a value
1031 * @propval and a length @proplen.
1032 *
1033 * @from: ofnode to start from (use ofnode_null() to start at the
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001034 * beginning)
1035 * @propname: property name to check
1036 * @propval: property value to search for
1037 * @proplen: length of the value in propval
1038 * Return: ofnode found, or ofnode_null() if none
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001039 */
1040ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1041 const void *propval, int proplen);
1042
1043/**
Simon Glass28529762017-08-05 15:45:54 -06001044 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1045 *
1046 * @node: child node (ofnode, lvalue)
1047 * @parent: parent node (ofnode)
1048 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001049 * This is a wrapper around a for loop and is used like so::
Simon Glass28529762017-08-05 15:45:54 -06001050 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001051 * ofnode node;
1052 * ofnode_for_each_subnode(node, parent) {
1053 * Use node
1054 * ...
1055 * }
Simon Glass28529762017-08-05 15:45:54 -06001056 *
1057 * Note that this is implemented as a macro and @node is used as
1058 * iterator in the loop. The parent variable can be a constant or even a
1059 * literal.
1060 */
1061#define ofnode_for_each_subnode(node, parent) \
1062 for (node = ofnode_first_subnode(parent); \
1063 ofnode_valid(node); \
1064 node = ofnode_next_subnode(node))
1065
Mario Sixaefac062018-01-15 11:07:19 +01001066/**
Michael Wallea7b9df22021-10-15 15:15:17 +02001067 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1068 * compatible string
1069 *
1070 * @node: child node (ofnode, lvalue)
1071 * @compat: compatible string to match
1072 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001073 * This is a wrapper around a for loop and is used like so::
Michael Wallea7b9df22021-10-15 15:15:17 +02001074 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001075 * ofnode node;
1076 * ofnode_for_each_compatible_node(node, parent, compatible) {
1077 * Use node
1078 * ...
1079 * }
Michael Wallea7b9df22021-10-15 15:15:17 +02001080 *
1081 * Note that this is implemented as a macro and @node is used as
1082 * iterator in the loop.
1083 */
1084#define ofnode_for_each_compatible_node(node, compat) \
1085 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1086 ofnode_valid(node); \
1087 node = ofnode_by_compatible(node, compat))
1088
1089/**
developerd93c8b42020-05-02 11:35:09 +02001090 * ofnode_get_child_count() - get the child count of a ofnode
1091 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001092 * @parent: valid node to get its child count
1093 * Return: the number of subnodes
developerd93c8b42020-05-02 11:35:09 +02001094 */
1095int ofnode_get_child_count(ofnode parent);
1096
1097/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001098 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +01001099 *
1100 * Translate an address from the device-tree into a CPU physical address. This
1101 * function walks up the tree and applies the various bus mappings along the
1102 * way.
1103 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001104 * @node: Device tree node giving the context in which to translate the address
Mario Sixaefac062018-01-15 11:07:19 +01001105 * @in_addr: pointer to the address to translate
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001106 * Return: the translated address; OF_BAD_ADDR on error
Mario Sixaefac062018-01-15 11:07:19 +01001107 */
1108u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001109
1110/**
Fabien Dessenne22236e02019-05-31 15:11:30 +02001111 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1112 *
1113 * Translate a DMA address from the device-tree into a CPU physical address.
1114 * This function walks up the tree and applies the various bus mappings along
1115 * the way.
1116 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001117 * @node: Device tree node giving the context in which to translate the
1118 * DMA address
Fabien Dessenne22236e02019-05-31 15:11:30 +02001119 * @in_addr: pointer to the DMA address to translate
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001120 * Return: the translated DMA address; OF_BAD_ADDR on error
Fabien Dessenne22236e02019-05-31 15:11:30 +02001121 */
1122u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1123
1124/**
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001125 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1126 *
1127 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1128 * cpu->bus address translations
1129 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001130 * @node: Device tree node
1131 * @cpu: Pointer to variable storing the range's cpu address
1132 * @bus: Pointer to variable storing the range's bus address
1133 * @size: Pointer to variable storing the range's size
1134 * Return: translated DMA address or OF_BAD_ADDR on error
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001135 */
1136int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1137 u64 *size);
1138
1139/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001140 * ofnode_device_is_compatible() - check if the node is compatible with compat
1141 *
1142 * This allows to check whether the node is comaptible with the compat.
1143 *
1144 * @node: Device tree node for which compatible needs to be verified.
1145 * @compat: Compatible string which needs to verified in the given node.
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001146 * Return: true if OK, false if the compatible is not found
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001147 */
1148int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +02001149
1150/**
1151 * ofnode_write_prop() - Set a property of a ofnode
1152 *
1153 * Note that the value passed to the function is *not* allocated by the
Simon Glass1817f712022-07-30 15:52:07 -06001154 * function itself, but must be allocated by the caller if necessary. However
1155 * it does allocate memory for the property struct and name.
Mario Six047dafc2018-06-26 08:46:48 +02001156 *
1157 * @node: The node for whose property should be set
1158 * @propname: The name of the property to set
Mario Six047dafc2018-06-26 08:46:48 +02001159 * @value: The new value of the property (must be valid prior to calling
1160 * the function)
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001161 * @len: The length of the new value of the property
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001162 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001163 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -06001164int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1165 int len);
Mario Six047dafc2018-06-26 08:46:48 +02001166
1167/**
1168 * ofnode_write_string() - Set a string property of a ofnode
1169 *
1170 * Note that the value passed to the function is *not* allocated by the
1171 * function itself, but must be allocated by the caller if necessary.
1172 *
1173 * @node: The node for whose string property should be set
1174 * @propname: The name of the string property to set
1175 * @value: The new value of the string property (must be valid prior to
1176 * calling the function)
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001177 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001178 */
1179int ofnode_write_string(ofnode node, const char *propname, const char *value);
1180
1181/**
Simon Glassd28e31e2022-07-30 15:52:14 -06001182 * ofnode_write_u32() - Set an integer property of an ofnode
1183 *
1184 * @node: The node for whose string property should be set
1185 * @propname: The name of the string property to set
1186 * @value: The new value of the 32-bit integer property
1187 * Return: 0 if successful, -ve on error
1188 */
1189int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1190
1191/**
Mario Six047dafc2018-06-26 08:46:48 +02001192 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1193 * ofnode
1194 *
1195 * This function effectively sets the node's "status" property to either "okay"
1196 * or "disable", hence making it available for driver model initialization or
1197 * not.
1198 *
1199 * @node: The node to enable
1200 * @value: Flag that tells the function to either disable or enable the
1201 * node
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001202 * Return: 0 if successful, -ve on error
Mario Six047dafc2018-06-26 08:46:48 +02001203 */
1204int ofnode_set_enabled(ofnode node, bool value);
1205
Simon Glass0034d962021-08-07 07:24:01 -06001206/**
Sean Anderson9b3a6392022-03-28 18:14:37 -04001207 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1208 *
1209 * This function parses PHY handle from the Ethernet controller's ofnode
1210 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1211 *
1212 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1213 * if the result to that is true, this function should not be called.
1214 *
1215 * @eth_node: ofnode belonging to the Ethernet controller
1216 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1217 */
1218ofnode ofnode_get_phy_node(ofnode eth_node);
1219
1220/**
1221 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1222 *
1223 * This function parses the "phy-mode" / "phy-connection-type" property and
1224 * returns the corresponding PHY interface type.
1225 *
1226 * @mac_node: ofnode containing the property
1227 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1228 * error
1229 */
1230phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1231
1232#if CONFIG_IS_ENABLED(DM)
1233/**
Simon Glass0034d962021-08-07 07:24:01 -06001234 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1235 *
1236 * This reads a property from the /config node of the devicetree.
1237 *
1238 * See doc/config.txt for bindings
1239 *
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001240 * @prop_name: property name to look up
1241 * Return: true, if it exists, false if not
Simon Glass0034d962021-08-07 07:24:01 -06001242 */
1243bool ofnode_conf_read_bool(const char *prop_name);
1244
1245/**
1246 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1247 *
1248 * This reads a property from the /config node of the devicetree.
1249 *
1250 * See doc/config.txt for bindings
1251 *
1252 * @prop_name: property name to look up
1253 * @default_val: default value to return if the property is not found
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001254 * Return: integer value, if found, or @default_val if not
Simon Glass0034d962021-08-07 07:24:01 -06001255 */
1256int ofnode_conf_read_int(const char *prop_name, int default_val);
1257
1258/**
1259 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1260 *
1261 * This reads a property from the /config node of the devicetree.
1262 *
1263 * See doc/config.txt for bindings
1264 *
1265 * @prop_name: property name to look up
Patrick Delaunay92c1c5c2022-01-12 10:53:49 +01001266 * Return: string value, if found, or NULL if not
Simon Glass0034d962021-08-07 07:24:01 -06001267 */
1268const char *ofnode_conf_read_str(const char *prop_name);
1269
Sean Anderson9b3a6392022-03-28 18:14:37 -04001270#else /* CONFIG_DM */
1271static inline bool ofnode_conf_read_bool(const char *prop_name)
1272{
1273 return false;
1274}
Marek Behúnf4f1ddc2022-04-07 00:32:57 +02001275
Sean Anderson9b3a6392022-03-28 18:14:37 -04001276static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1277{
1278 return default_val;
1279}
1280
1281static inline const char *ofnode_conf_read_str(const char *prop_name)
1282{
1283 return NULL;
1284}
Simon Glass56bc3322022-09-06 20:27:02 -06001285
Sean Anderson9b3a6392022-03-28 18:14:37 -04001286#endif /* CONFIG_DM */
Marek Behúnbc194772022-04-07 00:33:01 +02001287
Simon Glass56bc3322022-09-06 20:27:02 -06001288/**
1289 * of_add_subnode() - add a new subnode to a node
1290 *
1291 * @parent: parent node to add to
1292 * @name: name of subnode
1293 * @nodep: returns pointer to new subnode (valid if the function returns 0
1294 * or -EEXIST)
1295 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1296 * -ve on other error
1297 */
1298int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1299
Simon Glass9a148602017-05-17 17:18:10 -06001300#endif