blob: 53f04ac91d064a0e00c1db6906678d174a0842e3 [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>
Stefan Roesec14df8b2020-09-23 08:23:27 +020013#include <log.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060014
15/* Enable checks to protect against invalid calls */
16#undef OF_CHECKS
17
Simon Glassf7bfcc42017-07-25 08:29:55 -060018struct resource;
19
Simon Glass9a148602017-05-17 17:18:10 -060020/**
21 * ofnode - reference to a device tree node
22 *
23 * This union can hold either a straightforward pointer to a struct device_node
24 * in the live device tree, or an offset within the flat device tree. In the
25 * latter case, the pointer value is just the integer offset within the flat DT.
26 *
27 * Thus we can reference nodes in both the live tree (once available) and the
28 * flat tree (until then). Functions are available to translate between an
29 * ofnode and either an offset or a struct device_node *.
30 *
31 * The reference can also hold a null offset, in which case the pointer value
Simon Glassc4fc5622017-05-18 20:08:58 -060032 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass9a148602017-05-17 17:18:10 -060033 * NULL, or an offset of -1.
34 *
35 * There is no ambiguity as to whether ofnode holds an offset or a node
36 * pointer: when the live tree is active it holds a node pointer, otherwise it
37 * holds an offset. The value itself does not need to be unique and in theory
38 * the same value could point to a valid device node or a valid offset. We
39 * could arrange for a unique value to be used (e.g. by making the pointer
40 * point to an offset within the flat device tree in the case of an offset) but
41 * this increases code size slightly due to the subtraction. Since it offers no
42 * real benefit, the approach described here seems best.
43 *
44 * For now these points use constant types, since we don't allow writing
45 * the DT.
46 *
47 * @np: Pointer to device node, used for live tree
Baruch Siach6bcca142017-11-09 13:44:28 +020048 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
Simon Glass9a148602017-05-17 17:18:10 -060049 * is not a really a pointer to a node: it is an offset value. See above.
50 */
51typedef union ofnode_union {
Heinrich Schuchardtb571d922020-07-24 18:39:43 +020052 const struct device_node *np;
Simon Glass9a148602017-05-17 17:18:10 -060053 long of_offset;
54} ofnode;
55
Simon Glassc4fc5622017-05-18 20:08:58 -060056struct ofnode_phandle_args {
57 ofnode node;
58 int args_count;
59 uint32_t args[OF_MAX_PHANDLE_ARGS];
60};
61
62/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +010063 * ofprop - reference to a property of a device tree node
64 *
65 * This struct hold the reference on one property of one node,
66 * using struct ofnode and an offset within the flat device tree or either
67 * a pointer to a struct property in the live device tree.
68 *
69 * Thus we can reference arguments in both the live tree and the flat tree.
70 *
71 * The property reference can also hold a null reference. This corresponds to
72 * a struct property NULL pointer or an offset of -1.
73 *
74 * @node: Pointer to device node
75 * @offset: Pointer into flat device tree, used for flat tree.
76 * @prop: Pointer to property, used for live treee.
77 */
78
79struct ofprop {
80 ofnode node;
81 union {
82 int offset;
83 const struct property *prop;
84 };
85};
86
87/**
Stefan Roesec14df8b2020-09-23 08:23:27 +020088 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glassc4fc5622017-05-18 20:08:58 -060089 *
90 * This cannot be called if the reference contains an offset.
91 *
92 * @node: Reference containing struct device_node * (possibly invalid)
93 * @return pointer to device node (can be NULL)
94 */
95static inline const struct device_node *ofnode_to_np(ofnode node)
96{
97#ifdef OF_CHECKS
98 if (!of_live_active())
99 return NULL;
100#endif
101 return node.np;
102}
103
Simon Glass9a148602017-05-17 17:18:10 -0600104/**
105 * ofnode_to_offset() - convert an ofnode to a flat DT offset
106 *
107 * This cannot be called if the reference contains a node pointer.
108 *
109 * @node: Reference containing offset (possibly invalid)
110 * @return DT offset (can be -1)
111 */
112static inline int ofnode_to_offset(ofnode node)
113{
Simon Glassc4fc5622017-05-18 20:08:58 -0600114#ifdef OF_CHECKS
115 if (of_live_active())
116 return -1;
117#endif
Simon Glass9a148602017-05-17 17:18:10 -0600118 return node.of_offset;
119}
120
121/**
122 * ofnode_valid() - check if an ofnode is valid
123 *
124 * @return true if the reference contains a valid ofnode, false if it is NULL
125 */
126static inline bool ofnode_valid(ofnode node)
127{
Simon Glassc4fc5622017-05-18 20:08:58 -0600128 if (of_live_active())
129 return node.np != NULL;
130 else
Patrick Delaunay04fcfe72020-09-24 17:26:20 +0200131 return node.of_offset >= 0;
Simon Glass9a148602017-05-17 17:18:10 -0600132}
133
134/**
135 * offset_to_ofnode() - convert a DT offset to an ofnode
136 *
137 * @of_offset: DT offset (either valid, or -1)
138 * @return reference to the associated DT offset
139 */
140static inline ofnode offset_to_ofnode(int of_offset)
141{
142 ofnode node;
143
Simon Glassc4fc5622017-05-18 20:08:58 -0600144 if (of_live_active())
145 node.np = NULL;
146 else
Simon Glass1ab8b892019-12-06 21:41:36 -0700147 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass9a148602017-05-17 17:18:10 -0600148
149 return node;
150}
151
152/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600153 * np_to_ofnode() - convert a node pointer to an ofnode
154 *
155 * @np: Live node pointer (can be NULL)
156 * @return reference to the associated node pointer
157 */
158static inline ofnode np_to_ofnode(const struct device_node *np)
159{
160 ofnode node;
161
162 node.np = np;
163
164 return node;
165}
166
167/**
168 * ofnode_is_np() - check if a reference is a node pointer
169 *
170 * This function associated that if there is a valid live tree then all
171 * references will use it. This is because using the flat DT when the live tree
172 * is valid is not permitted.
173 *
174 * @node: reference to check (possibly invalid)
175 * @return true if the reference is a live node pointer, false if it is a DT
176 * offset
177 */
178static inline bool ofnode_is_np(ofnode node)
179{
180#ifdef OF_CHECKS
181 /*
182 * Check our assumption that flat tree offsets are not used when a
183 * live tree is in use.
184 */
185 assert(!ofnode_valid(node) ||
Stefan Roesec14df8b2020-09-23 08:23:27 +0200186 (of_live_active() ? ofnode_to_np(node)
187 : ofnode_to_np(node)));
Simon Glassc4fc5622017-05-18 20:08:58 -0600188#endif
189 return of_live_active() && ofnode_valid(node);
190}
191
192/**
Simon Glass9a148602017-05-17 17:18:10 -0600193 * ofnode_equal() - check if two references are equal
194 *
195 * @return true if equal, else false
196 */
197static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
198{
199 /* We only need to compare the contents */
200 return ref1.of_offset == ref2.of_offset;
201}
202
Simon Glassc4fc5622017-05-18 20:08:58 -0600203/**
204 * ofnode_null() - Obtain a null ofnode
205 *
206 * This returns an ofnode which points to no node. It works both with the flat
207 * tree and livetree.
208 */
209static inline ofnode ofnode_null(void)
210{
211 ofnode node;
212
213 if (of_live_active())
214 node.np = NULL;
215 else
216 node.of_offset = -1;
217
218 return node;
219}
220
Simon Glass278ddba2020-11-28 17:50:07 -0700221static inline ofnode ofnode_root(void)
222{
223 ofnode node;
224
225 if (of_live_active())
226 node.np = gd_of_root();
227 else
228 node.of_offset = 0;
229
230 return node;
231}
232
Simon Glassc4fc5622017-05-18 20:08:58 -0600233/**
234 * ofnode_read_u32() - Read a 32-bit integer from a property
235 *
236 * @ref: valid node reference to read property from
237 * @propname: name of the property to read from
238 * @outp: place to put value (if found)
239 * @return 0 if OK, -ve on error
240 */
241int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
242
243/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200244 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
245 *
246 * @ref: valid node reference to read property from
247 * @propname: name of the property to read from
248 * @index: index of the integer to return
249 * @outp: place to put value (if found)
250 * @return 0 if OK, -ve on error
251 */
252int ofnode_read_u32_index(ofnode node, const char *propname, int index,
253 u32 *outp);
254
255/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600256 * ofnode_read_s32() - Read a 32-bit integer from a property
257 *
258 * @ref: valid node reference to read property from
259 * @propname: name of the property to read from
260 * @outp: place to put value (if found)
261 * @return 0 if OK, -ve on error
262 */
263static inline int ofnode_read_s32(ofnode node, const char *propname,
264 s32 *out_value)
265{
266 return ofnode_read_u32(node, propname, (u32 *)out_value);
267}
268
269/**
270 * ofnode_read_u32_default() - Read a 32-bit integer from a property
271 *
272 * @ref: valid node reference to read property from
273 * @propname: name of the property to read from
274 * @def: default value to return if the property has no value
275 * @return property value, or @def if not found
276 */
Trent Piepho5b775412019-05-10 17:48:20 +0000277u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glassc4fc5622017-05-18 20:08:58 -0600278
279/**
Dario Binacchi81d80b52020-03-29 18:04:41 +0200280 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
281 * property
282 *
283 * @ref: valid node reference to read property from
284 * @propname: name of the property to read from
285 * @index: index of the integer to return
286 * @def: default value to return if the property has no value
287 * @return property value, or @def if not found
288 */
289u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
290 u32 def);
291
292/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600293 * ofnode_read_s32_default() - Read a 32-bit integer from a property
294 *
295 * @ref: valid node reference to read property from
296 * @propname: name of the property to read from
297 * @def: default value to return if the property has no value
298 * @return property value, or @def if not found
299 */
300int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
301
302/**
Lukas Auerb03a60b2018-11-22 11:26:35 +0100303 * ofnode_read_u64() - Read a 64-bit integer from a property
304 *
305 * @node: valid node reference to read property from
306 * @propname: name of the property to read from
307 * @outp: place to put value (if found)
308 * @return 0 if OK, -ve on error
309 */
310int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
311
312/**
Simon Glass9d54a7a2018-06-11 13:07:10 -0600313 * ofnode_read_u64_default() - Read a 64-bit integer from a property
314 *
315 * @ref: valid node reference to read property from
316 * @propname: name of the property to read from
317 * @def: default value to return if the property has no value
318 * @return property value, or @def if not found
319 */
T Karthik Reddy478860d2019-09-02 16:34:30 +0200320u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass9d54a7a2018-06-11 13:07:10 -0600321
322/**
Simon Glass0c2e9802020-01-27 08:49:44 -0700323 * ofnode_read_prop() - Read a property from a node
324 *
325 * @node: valid node reference to read property from
326 * @propname: name of the property to read
327 * @sizep: if non-NULL, returns the size of the property, or an error code
328 if not found
329 * @return property value, or NULL if there is no such property
330 */
331const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
332
333/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600334 * ofnode_read_string() - Read a string from a property
335 *
Simon Glass0c2e9802020-01-27 08:49:44 -0700336 * @node: valid node reference to read property from
Simon Glassc4fc5622017-05-18 20:08:58 -0600337 * @propname: name of the property to read
338 * @return string from property value, or NULL if there is no such property
339 */
340const char *ofnode_read_string(ofnode node, const char *propname);
341
342/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600343 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glassc4fc5622017-05-18 20:08:58 -0600344 *
345 * @node: valid node reference to read property from
346 * @propname: name of the property to read
347 * @out_values: pointer to return value, modified only if return value is 0
348 * @sz: number of array elements to read
Simon Glass1c682342018-06-11 13:07:11 -0600349 * @return 0 if OK, -ve on error
Simon Glassc4fc5622017-05-18 20:08:58 -0600350 *
351 * Search for a property in a device node and read 32-bit value(s) from
352 * it. Returns 0 on success, -EINVAL if the property does not exist,
353 * -ENODATA if property does not have a value, and -EOVERFLOW if the
354 * property data isn't large enough.
355 *
356 * The out_values is modified only if a valid u32 value can be decoded.
357 */
358int ofnode_read_u32_array(ofnode node, const char *propname,
359 u32 *out_values, size_t sz);
Simon Glass5de5b3b2020-11-28 17:50:02 -0700360/**
361 * ofnode_is_enabled() - Checks whether a node is enabled.
362 * This looks for a 'status' property. If this exists, then returns true if
363 * the status is 'okay' and false otherwise. If there is no status property,
364 * it returns true on the assumption that anything mentioned should be enabled
365 * by default.
366 *
367 * @node: node to examine
368 * @return false (not enabled) or true (enabled)
369 */
370bool ofnode_is_enabled(ofnode node);
Simon Glassc4fc5622017-05-18 20:08:58 -0600371
372/**
373 * ofnode_read_bool() - read a boolean value from a property
374 *
375 * @node: valid node reference to read property from
376 * @propname: name of property to read
377 * @return true if property is present (meaning true), false if not present
378 */
379bool ofnode_read_bool(ofnode node, const char *propname);
380
381/**
382 * ofnode_find_subnode() - find a named subnode of a parent node
383 *
384 * @node: valid reference to parent node
385 * @subnode_name: name of subnode to find
386 * @return reference to subnode (which can be invalid if there is no such
387 * subnode)
388 */
389ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
390
391/**
392 * ofnode_first_subnode() - find the first subnode of a parent node
393 *
394 * @node: valid reference to a valid parent node
395 * @return reference to the first subnode (which can be invalid if the parent
396 * node has no subnodes)
397 */
398ofnode ofnode_first_subnode(ofnode node);
399
400/**
401 * ofnode_next_subnode() - find the next sibling of a subnode
402 *
403 * @node: valid reference to previous node (sibling)
404 * @return reference to the next subnode (which can be invalid if the node
405 * has no more siblings)
406 */
407ofnode ofnode_next_subnode(ofnode node);
408
409/**
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100410 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
411 *
412 * @node: valid node to look up
413 * @return ofnode reference of the parent node
414 */
415ofnode ofnode_get_parent(ofnode node);
416
417/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600418 * ofnode_get_name() - get the name of a node
419 *
420 * @node: valid node to look up
Baruch Siachf14cb212018-11-18 14:39:20 +0200421 * @return name of node
Simon Glassc4fc5622017-05-18 20:08:58 -0600422 */
423const char *ofnode_get_name(ofnode node);
424
425/**
Kever Yang37df0e02018-02-23 17:38:50 +0100426 * ofnode_get_by_phandle() - get ofnode from phandle
427 *
428 * @phandle: phandle to look up
429 * @return ofnode reference to the phandle
430 */
431ofnode ofnode_get_by_phandle(uint phandle);
432
433/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600434 * ofnode_read_size() - read the size of a property
435 *
436 * @node: node to check
437 * @propname: property to check
438 * @return size of property if present, or -EINVAL if not
439 */
440int ofnode_read_size(ofnode node, const char *propname);
441
442/**
Keerthyd332e6e2019-04-24 17:19:53 +0530443 * ofnode_get_addr_size_index() - get an address/size from a node
444 * based on index
445 *
446 * This reads the register address/size from a node based on index
447 *
448 * @node: node to read from
449 * @index: Index of address to read (0 for first)
450 * @size: Pointer to size of the address
451 * @return address, or FDT_ADDR_T_NONE if not present or invalid
452 */
453phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
454 fdt_size_t *size);
455
456/**
Simon Glass049ae1b2017-05-18 20:09:01 -0600457 * ofnode_get_addr_index() - get an address from a node
458 *
459 * This reads the register address from a node
460 *
461 * @node: node to read from
462 * @index: Index of address to read (0 for first)
463 * @return address, or FDT_ADDR_T_NONE if not present or invalid
464 */
465phys_addr_t ofnode_get_addr_index(ofnode node, int index);
466
467/**
468 * ofnode_get_addr() - get an address from a node
469 *
470 * This reads the register address from a node
471 *
472 * @node: node to read from
473 * @return address, or FDT_ADDR_T_NONE if not present or invalid
474 */
475phys_addr_t ofnode_get_addr(ofnode node);
476
477/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600478 * ofnode_stringlist_search() - find a string in a string list and return index
479 *
480 * Note that it is possible for this function to succeed on property values
481 * that are not NUL-terminated. That's because the function will stop after
482 * finding the first occurrence of @string. This can for example happen with
483 * small-valued cell properties, such as #address-cells, when searching for
484 * the empty string.
485 *
486 * @node: node to check
487 * @propname: name of the property containing the string list
488 * @string: string to look up in the string list
489 *
490 * @return:
491 * the index of the string in the list of strings
492 * -ENODATA if the property is not found
493 * -EINVAL on some other error
494 */
495int ofnode_stringlist_search(ofnode node, const char *propname,
496 const char *string);
497
498/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600499 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glassc4fc5622017-05-18 20:08:58 -0600500 *
501 * Note that this will successfully extract strings from properties with
502 * non-NUL-terminated values. For example on small-valued cell properties
503 * this function will return the empty string.
504 *
505 * If non-NULL, the length of the string (on success) or a negative error-code
506 * (on failure) will be stored in the integer pointer to by lenp.
507 *
508 * @node: node to check
509 * @propname: name of the property containing the string list
510 * @index: index of the string to return
511 * @lenp: return location for the string length or an error code on failure
512 *
513 * @return:
514 * length of string, if found or -ve error value if not found
515 */
516int ofnode_read_string_index(ofnode node, const char *propname, int index,
517 const char **outp);
518
519/**
Simon Glass5fdb0052017-06-12 06:21:28 -0600520 * ofnode_read_string_count() - find the number of strings in a string list
521 *
522 * @node: node to check
523 * @propname: name of the property containing the string list
524 * @return:
525 * number of strings in the list, or -ve error value if not found
526 */
527int ofnode_read_string_count(ofnode node, const char *property);
528
529/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600530 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
531 *
532 * This function is useful to parse lists of phandles and their arguments.
533 * Returns 0 on success and fills out_args, on error returns appropriate
534 * errno value.
535 *
536 * Caller is responsible to call of_node_put() on the returned out_args->np
537 * pointer.
538 *
539 * Example:
540 *
541 * phandle1: node1 {
542 * #list-cells = <2>;
543 * }
544 *
545 * phandle2: node2 {
546 * #list-cells = <1>;
547 * }
548 *
549 * node3 {
550 * list = <&phandle1 1 2 &phandle2 3>;
551 * }
552 *
553 * To get a device_node of the `node2' node you may call this:
554 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
555 *
556 * @node: device tree node containing a list
557 * @list_name: property name that contains a list
558 * @cells_name: property name that specifies phandles' arguments count
559 * @cells_count: Cell count to use if @cells_name is NULL
560 * @index: index of a phandle to parse out
561 * @out_args: optional pointer to output arguments structure (will be filled)
562 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
563 * @list_name does not exist, -EINVAL if a phandle was not found,
564 * @cells_name could not be found, the arguments were truncated or there
565 * were too many arguments.
566 */
567int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
568 const char *cells_name, int cell_count,
569 int index,
570 struct ofnode_phandle_args *out_args);
571
572/**
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200573 * ofnode_count_phandle_with_args() - Count number of phandle in a list
574 *
575 * This function is useful to count phandles into a list.
576 * Returns number of phandle on success, on error returns appropriate
577 * errno value.
578 *
579 * @node: device tree node containing a list
580 * @list_name: property name that contains a list
581 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunayd776a842020-09-25 09:41:14 +0200582 * @cells_count: Cell count to use if @cells_name is NULL
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200583 * @return number of phandle on success, -ENOENT if @list_name does not
584 * exist, -EINVAL if a phandle was not found, @cells_name could not
585 * be found.
586 */
587int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200588 const char *cells_name, int cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200589
590/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600591 * ofnode_path() - find a node by full path
592 *
593 * @path: Full path to node, e.g. "/bus/spi@1"
594 * @return reference to the node found. Use ofnode_valid() to check if it exists
595 */
596ofnode ofnode_path(const char *path);
597
598/**
Simon Glasse09223c2020-01-27 08:49:46 -0700599 * ofnode_read_chosen_prop() - get the value of a chosen property
600 *
601 * This looks for a property within the /chosen node and returns its value
602 *
603 * @propname: Property name to look for
604 * @sizep: Returns size of property, or FDT_ERR_... error code if function
605 * returns NULL
606 * @return property value if found, else NULL
607 */
608const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
609
610/**
Simon Glassf3455962020-01-27 08:49:43 -0700611 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glassc4fc5622017-05-18 20:08:58 -0600612 *
Simon Glassf3455962020-01-27 08:49:43 -0700613 * This looks for a property within the /chosen node and returns its value,
614 * checking that it is a valid nul-terminated string
Simon Glassc4fc5622017-05-18 20:08:58 -0600615 *
616 * @propname: Property name to look for
Simon Glassf3455962020-01-27 08:49:43 -0700617 * @return string value if found, else NULL
Simon Glassc4fc5622017-05-18 20:08:58 -0600618 */
Simon Glassf3455962020-01-27 08:49:43 -0700619const char *ofnode_read_chosen_string(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600620
621/**
Simon Glassc99ba912020-01-27 08:49:42 -0700622 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glassc4fc5622017-05-18 20:08:58 -0600623 *
Simon Glassc99ba912020-01-27 08:49:42 -0700624 * This looks up a named property in the chosen node and uses that as a path to
625 * look up a code.
626 *
627 * @return the referenced node if present, else ofnode_null()
Simon Glassc4fc5622017-05-18 20:08:58 -0600628 */
Simon Glassc99ba912020-01-27 08:49:42 -0700629ofnode ofnode_get_chosen_node(const char *propname);
Simon Glassc4fc5622017-05-18 20:08:58 -0600630
Michal Simek92a88622020-07-28 12:51:08 +0200631/**
632 * ofnode_read_aliases_prop() - get the value of a aliases property
633 *
634 * This looks for a property within the /aliases node and returns its value
635 *
636 * @propname: Property name to look for
637 * @sizep: Returns size of property, or FDT_ERR_... error code if function
638 * returns NULL
639 * @return property value if found, else NULL
640 */
641const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
642
643/**
644 * ofnode_get_aliases_node() - get a referenced node from the aliases node
645 *
646 * This looks up a named property in the aliases node and uses that as a path to
647 * look up a code.
648 *
649 * @return the referenced node if present, else ofnode_null()
650 */
651ofnode ofnode_get_aliases_node(const char *propname);
652
Simon Glassc4fc5622017-05-18 20:08:58 -0600653struct display_timing;
654/**
655 * ofnode_decode_display_timing() - decode display timings
656 *
657 * Decode display timings from the supplied 'display-timings' node.
658 * See doc/device-tree-bindings/video/display-timing.txt for binding
659 * information.
660 *
661 * @node 'display-timing' node containing the timing subnodes
662 * @index Index number to read (0=first timing subnode)
663 * @config Place to put timings
664 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
665 */
666int ofnode_decode_display_timing(ofnode node, int index,
667 struct display_timing *config);
668
669/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100670 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glassc4fc5622017-05-18 20:08:58 -0600671 *
672 * @node: node to read
673 * @propname: property to read
674 * @lenp: place to put length on success
675 * @return pointer to property, or NULL if not found
676 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900677const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600678
679/**
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100680 * ofnode_get_first_property()- get the reference of the first property
681 *
682 * Get reference to the first property of the node, it is used to iterate
683 * and read all the property with ofnode_get_property_by_prop().
684 *
685 * @node: node to read
686 * @prop: place to put argument reference
687 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
688 */
689int ofnode_get_first_property(ofnode node, struct ofprop *prop);
690
691/**
692 * ofnode_get_next_property() - get the reference of the next property
693 *
694 * Get reference to the next property of the node, it is used to iterate
695 * and read all the property with ofnode_get_property_by_prop().
696 *
697 * @prop: reference of current argument and place to put reference of next one
698 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
699 */
700int ofnode_get_next_property(struct ofprop *prop);
701
702/**
703 * ofnode_get_property_by_prop() - get a pointer to the value of a property
704 *
705 * Get value for the property identified by the provided reference.
706 *
707 * @prop: reference on property
708 * @propname: If non-NULL, place to property name on success,
709 * @lenp: If non-NULL, place to put length on success
710 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
711 */
712const void *ofnode_get_property_by_prop(const struct ofprop *prop,
713 const char **propname, int *lenp);
714
715/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600716 * ofnode_is_available() - check if a node is marked available
717 *
718 * @node: node to check
719 * @return true if node's 'status' property is "okay" (or is missing)
720 */
721bool ofnode_is_available(ofnode node);
722
723/**
724 * ofnode_get_addr_size() - get address and size from a property
725 *
726 * This does no address translation. It simply reads an property that contains
727 * an address and a size value, one after the other.
728 *
729 * @node: node to read from
730 * @propname: property to read
731 * @sizep: place to put size value (on success)
732 * @return address value, or FDT_ADDR_T_NONE on error
733 */
734phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
735 phys_size_t *sizep);
736
737/**
738 * ofnode_read_u8_array_ptr() - find an 8-bit array
739 *
740 * Look up a property in a node and return a pointer to its contents as a
741 * byte array of given length. The property must have at least enough data
742 * for the array (count bytes). It may have more, but this will be ignored.
743 * The data is not copied.
744 *
745 * @node node to examine
746 * @propname name of property to find
747 * @sz number of array elements
748 * @return pointer to byte array if found, or NULL if the property is not
749 * found or there is not enough data
750 */
751const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
752 size_t sz);
753
754/**
755 * ofnode_read_pci_addr() - look up a PCI address
756 *
757 * Look at an address property in a node and return the PCI address which
758 * corresponds to the given type in the form of fdt_pci_addr.
759 * The property must hold one fdt_pci_addr with a lengh.
760 *
761 * @node node to examine
762 * @type pci address type (FDT_PCI_SPACE_xxx)
763 * @propname name of property to find
764 * @addr returns pci address in the form of fdt_pci_addr
765 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
766 * format of the property was invalid, -ENXIO if the requested
767 * address type was not found
768 */
769int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
770 const char *propname, struct fdt_pci_addr *addr);
771
772/**
Bin Mengfa157712018-08-03 01:14:35 -0700773 * ofnode_read_pci_vendev() - look up PCI vendor and device id
774 *
775 * Look at the compatible property of a device node that represents a PCI
776 * device and extract pci vendor id and device id from it.
777 *
778 * @param node node to examine
779 * @param vendor vendor id of the pci device
780 * @param device device id of the pci device
781 * @return 0 if ok, negative on error
782 */
783int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
784
785/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600786 * ofnode_read_addr_cells() - Get the number of address cells for a node
787 *
788 * This walks back up the tree to find the closest #address-cells property
789 * which controls the given node.
790 *
791 * @node: Node to check
792 * @return number of address cells this node uses
793 */
794int ofnode_read_addr_cells(ofnode node);
795
796/**
797 * ofnode_read_size_cells() - Get the number of size cells for a node
798 *
799 * This walks back up the tree to find the closest #size-cells property
800 * which controls the given node.
801 *
802 * @node: Node to check
803 * @return number of size cells this node uses
804 */
805int ofnode_read_size_cells(ofnode node);
806
807/**
Simon Glass4191dc12017-06-12 06:21:31 -0600808 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
809 *
810 * This function matches fdt_address_cells().
811 *
812 * @np: Node pointer to check
813 * @return value of #address-cells property in this node, or 2 if none
814 */
815int ofnode_read_simple_addr_cells(ofnode node);
816
817/**
818 * ofnode_read_simple_size_cells() - Get the size cells property in a node
819 *
820 * This function matches fdt_size_cells().
821 *
822 * @np: Node pointer to check
823 * @return value of #size-cells property in this node, or 2 if none
824 */
825int ofnode_read_simple_size_cells(ofnode node);
826
827/**
Simon Glassc4fc5622017-05-18 20:08:58 -0600828 * ofnode_pre_reloc() - check if a node should be bound before relocation
829 *
830 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
831 * via special device tree properties.
832 *
833 * Before relocation this function can be used to check if nodes are required
834 * in either SPL or TPL stages.
835 *
836 * After relocation and jumping into the real U-Boot binary it is possible to
837 * determine if a node was bound in one of SPL/TPL stages.
838 *
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200839 * There are 4 settings currently in use
840 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glassc4fc5622017-05-18 20:08:58 -0600841 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
842 * Existing platforms only use it to indicate nodes needed in
843 * SPL. Should probably be replaced by u-boot,dm-spl for
844 * new platforms.
Patrick Delaunay63e4d112019-05-21 19:19:13 +0200845 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
846 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glassc4fc5622017-05-18 20:08:58 -0600847 *
848 * @node: node to check
Simon Glass1c682342018-06-11 13:07:11 -0600849 * @return true if node is needed in SPL/TL, false otherwise
Simon Glassc4fc5622017-05-18 20:08:58 -0600850 */
851bool ofnode_pre_reloc(ofnode node);
852
Simon Glassa8173d62018-06-11 13:07:12 -0600853/**
854 * ofnode_read_resource() - Read a resource from a node
855 *
856 * Read resource information from a node at the given index
857 *
858 * @node: Node to read from
859 * @index: Index of resource to read (0 = first)
860 * @res: Returns resource that was read, on success
861 * @return 0 if OK, -ve on error
862 */
Simon Glassf7bfcc42017-07-25 08:29:55 -0600863int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassa8173d62018-06-11 13:07:12 -0600864
865/**
866 * ofnode_read_resource_byname() - Read a resource from a node by name
867 *
868 * Read resource information from a node matching the given name. This uses a
869 * 'reg-names' string list property with the names matching the associated
870 * 'reg' property list.
871 *
872 * @node: Node to read from
873 * @name: Name of resource to read
874 * @res: Returns resource that was read, on success
875 * @return 0 if OK, -ve on error
876 */
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900877int ofnode_read_resource_byname(ofnode node, const char *name,
878 struct resource *res);
Simon Glassf7bfcc42017-07-25 08:29:55 -0600879
Simon Glass28529762017-08-05 15:45:54 -0600880/**
Simon Glass954eeae2018-06-11 13:07:13 -0600881 * ofnode_by_compatible() - Find the next compatible node
882 *
883 * Find the next node after @from that is compatible with @compat
884 *
885 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
886 * @compat: Compatible string to match
887 * @return ofnode found, or ofnode_null() if none
888 */
889ofnode ofnode_by_compatible(ofnode from, const char *compat);
890
891/**
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200892 * ofnode_by_prop_value() - Find the next node with given property value
893 *
894 * Find the next node after @from that has a @propname with a value
895 * @propval and a length @proplen.
896 *
897 * @from: ofnode to start from (use ofnode_null() to start at the
898 * beginning) @propname: property name to check @propval: property value to
899 * search for @proplen: length of the value in propval @return ofnode
900 * found, or ofnode_null() if none
901 */
902ofnode ofnode_by_prop_value(ofnode from, const char *propname,
903 const void *propval, int proplen);
904
905/**
Simon Glass28529762017-08-05 15:45:54 -0600906 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
907 *
908 * @node: child node (ofnode, lvalue)
909 * @parent: parent node (ofnode)
910 *
911 * This is a wrapper around a for loop and is used like so:
912 *
913 * ofnode node;
914 *
915 * ofnode_for_each_subnode(node, parent) {
916 * Use node
917 * ...
918 * }
919 *
920 * Note that this is implemented as a macro and @node is used as
921 * iterator in the loop. The parent variable can be a constant or even a
922 * literal.
923 */
924#define ofnode_for_each_subnode(node, parent) \
925 for (node = ofnode_first_subnode(parent); \
926 ofnode_valid(node); \
927 node = ofnode_next_subnode(node))
928
Mario Sixaefac062018-01-15 11:07:19 +0100929/**
developerd93c8b42020-05-02 11:35:09 +0200930 * ofnode_get_child_count() - get the child count of a ofnode
931 *
932 * @node: valid node to get its child count
933 * @return the number of subnodes
934 */
935int ofnode_get_child_count(ofnode parent);
936
937/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200938 * ofnode_translate_address() - Translate a device-tree address
Mario Sixaefac062018-01-15 11:07:19 +0100939 *
940 * Translate an address from the device-tree into a CPU physical address. This
941 * function walks up the tree and applies the various bus mappings along the
942 * way.
943 *
944 * @ofnode: Device tree node giving the context in which to translate the
945 * address
946 * @in_addr: pointer to the address to translate
947 * @return the translated address; OF_BAD_ADDR on error
948 */
949u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900950
951/**
Fabien Dessenne22236e02019-05-31 15:11:30 +0200952 * ofnode_translate_dma_address() - Translate a device-tree DMA address
953 *
954 * Translate a DMA address from the device-tree into a CPU physical address.
955 * This function walks up the tree and applies the various bus mappings along
956 * the way.
957 *
958 * @ofnode: Device tree node giving the context in which to translate the
959 * DMA address
960 * @in_addr: pointer to the DMA address to translate
961 * @return the translated DMA address; OF_BAD_ADDR on error
962 */
963u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
964
965/**
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900966 * ofnode_device_is_compatible() - check if the node is compatible with compat
967 *
968 * This allows to check whether the node is comaptible with the compat.
969 *
970 * @node: Device tree node for which compatible needs to be verified.
971 * @compat: Compatible string which needs to verified in the given node.
972 * @return true if OK, false if the compatible is not found
973 */
974int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Six047dafc2018-06-26 08:46:48 +0200975
976/**
977 * ofnode_write_prop() - Set a property of a ofnode
978 *
979 * Note that the value passed to the function is *not* allocated by the
980 * function itself, but must be allocated by the caller if necessary.
981 *
982 * @node: The node for whose property should be set
983 * @propname: The name of the property to set
984 * @len: The length of the new value of the property
985 * @value: The new value of the property (must be valid prior to calling
986 * the function)
987 * @return 0 if successful, -ve on error
988 */
989int ofnode_write_prop(ofnode node, const char *propname, int len,
990 const void *value);
991
992/**
993 * ofnode_write_string() - Set a string property of a ofnode
994 *
995 * Note that the value passed to the function is *not* allocated by the
996 * function itself, but must be allocated by the caller if necessary.
997 *
998 * @node: The node for whose string property should be set
999 * @propname: The name of the string property to set
1000 * @value: The new value of the string property (must be valid prior to
1001 * calling the function)
1002 * @return 0 if successful, -ve on error
1003 */
1004int ofnode_write_string(ofnode node, const char *propname, const char *value);
1005
1006/**
1007 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1008 * ofnode
1009 *
1010 * This function effectively sets the node's "status" property to either "okay"
1011 * or "disable", hence making it available for driver model initialization or
1012 * not.
1013 *
1014 * @node: The node to enable
1015 * @value: Flag that tells the function to either disable or enable the
1016 * node
1017 * @return 0 if successful, -ve on error
1018 */
1019int ofnode_set_enabled(ofnode node, bool value);
1020
Simon Glass9a148602017-05-17 17:18:10 -06001021#endif