blob: 5b088650d3bc85b914429b0df7af53d198610143 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass4984de22017-05-17 17:18:10 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass4984de22017-05-17 17:18:10 -06005 */
6
7#ifndef _DM_OFNODE_H
8#define _DM_OFNODE_H
9
Simon Glass9e512042017-05-18 20:08:58 -060010/* TODO(sjg@chromium.org): Drop fdtdec.h include */
11#include <fdtdec.h>
12#include <dm/of.h>
Simon Glassec1add12020-12-16 17:25:06 -070013#include <dm/of_access.h>
Stefan Roese45dbe752020-09-23 08:23:27 +020014#include <log.h>
Simon Glass9e512042017-05-18 20:08:58 -060015
16/* Enable checks to protect against invalid calls */
17#undef OF_CHECKS
18
Simon Glassdcf98852017-07-25 08:29:55 -060019struct resource;
20
Simon Glass4984de22017-05-17 17:18:10 -060021/**
22 * ofnode - reference to a device tree node
23 *
24 * This union can hold either a straightforward pointer to a struct device_node
25 * in the live device tree, or an offset within the flat device tree. In the
26 * latter case, the pointer value is just the integer offset within the flat DT.
27 *
28 * Thus we can reference nodes in both the live tree (once available) and the
29 * flat tree (until then). Functions are available to translate between an
30 * ofnode and either an offset or a struct device_node *.
31 *
32 * The reference can also hold a null offset, in which case the pointer value
Simon Glass9e512042017-05-18 20:08:58 -060033 * here is NULL. This corresponds to a struct device_node * value of
Simon Glass4984de22017-05-17 17:18:10 -060034 * NULL, or an offset of -1.
35 *
36 * There is no ambiguity as to whether ofnode holds an offset or a node
37 * pointer: when the live tree is active it holds a node pointer, otherwise it
38 * holds an offset. The value itself does not need to be unique and in theory
39 * the same value could point to a valid device node or a valid offset. We
40 * could arrange for a unique value to be used (e.g. by making the pointer
41 * point to an offset within the flat device tree in the case of an offset) but
42 * this increases code size slightly due to the subtraction. Since it offers no
43 * real benefit, the approach described here seems best.
44 *
45 * For now these points use constant types, since we don't allow writing
46 * the DT.
47 *
48 * @np: Pointer to device node, used for live tree
Baruch Siachafc1a782017-11-09 13:44:28 +020049 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
Simon Glass4984de22017-05-17 17:18:10 -060050 * is not a really a pointer to a node: it is an offset value. See above.
51 */
52typedef union ofnode_union {
Heinrich Schuchardtb9390ce2020-07-24 18:39:43 +020053 const struct device_node *np;
Simon Glass4984de22017-05-17 17:18:10 -060054 long of_offset;
55} ofnode;
56
Simon Glass9e512042017-05-18 20:08:58 -060057struct ofnode_phandle_args {
58 ofnode node;
59 int args_count;
60 uint32_t args[OF_MAX_PHANDLE_ARGS];
61};
62
63/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +010064 * ofprop - reference to a property of a device tree node
65 *
66 * This struct hold the reference on one property of one node,
67 * using struct ofnode and an offset within the flat device tree or either
68 * a pointer to a struct property in the live device tree.
69 *
70 * Thus we can reference arguments in both the live tree and the flat tree.
71 *
72 * The property reference can also hold a null reference. This corresponds to
73 * a struct property NULL pointer or an offset of -1.
74 *
75 * @node: Pointer to device node
76 * @offset: Pointer into flat device tree, used for flat tree.
77 * @prop: Pointer to property, used for live treee.
78 */
79
80struct ofprop {
81 ofnode node;
82 union {
83 int offset;
84 const struct property *prop;
85 };
86};
87
88/**
Stefan Roese45dbe752020-09-23 08:23:27 +020089 * ofnode_to_np() - convert an ofnode to a live DT node pointer
Simon Glass9e512042017-05-18 20:08:58 -060090 *
91 * This cannot be called if the reference contains an offset.
92 *
93 * @node: Reference containing struct device_node * (possibly invalid)
94 * @return pointer to device node (can be NULL)
95 */
96static inline const struct device_node *ofnode_to_np(ofnode node)
97{
98#ifdef OF_CHECKS
99 if (!of_live_active())
100 return NULL;
101#endif
102 return node.np;
103}
104
Simon Glass4984de22017-05-17 17:18:10 -0600105/**
106 * ofnode_to_offset() - convert an ofnode to a flat DT offset
107 *
108 * This cannot be called if the reference contains a node pointer.
109 *
110 * @node: Reference containing offset (possibly invalid)
111 * @return DT offset (can be -1)
112 */
113static inline int ofnode_to_offset(ofnode node)
114{
Simon Glass9e512042017-05-18 20:08:58 -0600115#ifdef OF_CHECKS
116 if (of_live_active())
117 return -1;
118#endif
Simon Glass4984de22017-05-17 17:18:10 -0600119 return node.of_offset;
120}
121
122/**
123 * ofnode_valid() - check if an ofnode is valid
124 *
125 * @return true if the reference contains a valid ofnode, false if it is NULL
126 */
127static inline bool ofnode_valid(ofnode node)
128{
Simon Glass9e512042017-05-18 20:08:58 -0600129 if (of_live_active())
130 return node.np != NULL;
131 else
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200132 return node.of_offset >= 0;
Simon Glass4984de22017-05-17 17:18:10 -0600133}
134
135/**
136 * offset_to_ofnode() - convert a DT offset to an ofnode
137 *
138 * @of_offset: DT offset (either valid, or -1)
139 * @return reference to the associated DT offset
140 */
141static inline ofnode offset_to_ofnode(int of_offset)
142{
143 ofnode node;
144
Simon Glass9e512042017-05-18 20:08:58 -0600145 if (of_live_active())
146 node.np = NULL;
147 else
Simon Glassb14c5332019-12-06 21:41:36 -0700148 node.of_offset = of_offset >= 0 ? of_offset : -1;
Simon Glass4984de22017-05-17 17:18:10 -0600149
150 return node;
151}
152
153/**
Simon Glass9e512042017-05-18 20:08:58 -0600154 * np_to_ofnode() - convert a node pointer to an ofnode
155 *
156 * @np: Live node pointer (can be NULL)
157 * @return reference to the associated node pointer
158 */
159static inline ofnode np_to_ofnode(const struct device_node *np)
160{
161 ofnode node;
162
163 node.np = np;
164
165 return node;
166}
167
168/**
169 * ofnode_is_np() - check if a reference is a node pointer
170 *
171 * This function associated that if there is a valid live tree then all
172 * references will use it. This is because using the flat DT when the live tree
173 * is valid is not permitted.
174 *
175 * @node: reference to check (possibly invalid)
176 * @return true if the reference is a live node pointer, false if it is a DT
177 * offset
178 */
179static inline bool ofnode_is_np(ofnode node)
180{
181#ifdef OF_CHECKS
182 /*
183 * Check our assumption that flat tree offsets are not used when a
184 * live tree is in use.
185 */
186 assert(!ofnode_valid(node) ||
Stefan Roese45dbe752020-09-23 08:23:27 +0200187 (of_live_active() ? ofnode_to_np(node)
188 : ofnode_to_np(node)));
Simon Glass9e512042017-05-18 20:08:58 -0600189#endif
190 return of_live_active() && ofnode_valid(node);
191}
192
193/**
Simon Glass4984de22017-05-17 17:18:10 -0600194 * ofnode_equal() - check if two references are equal
195 *
196 * @return true if equal, else false
197 */
198static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
199{
200 /* We only need to compare the contents */
201 return ref1.of_offset == ref2.of_offset;
202}
203
Simon Glass9e512042017-05-18 20:08:58 -0600204/**
205 * ofnode_null() - Obtain a null ofnode
206 *
207 * This returns an ofnode which points to no node. It works both with the flat
208 * tree and livetree.
209 */
210static inline ofnode ofnode_null(void)
211{
212 ofnode node;
213
214 if (of_live_active())
215 node.np = NULL;
216 else
217 node.of_offset = -1;
218
219 return node;
220}
221
Simon Glassd0c20ce2020-11-28 17:50:07 -0700222static inline ofnode ofnode_root(void)
223{
224 ofnode node;
225
226 if (of_live_active())
227 node.np = gd_of_root();
228 else
229 node.of_offset = 0;
230
231 return node;
232}
233
Simon Glass9e512042017-05-18 20:08:58 -0600234/**
235 * ofnode_read_u32() - Read a 32-bit integer from a property
236 *
237 * @ref: valid node reference to read property from
238 * @propname: name of the property to read from
239 * @outp: place to put value (if found)
240 * @return 0 if OK, -ve on error
241 */
242int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
243
244/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200245 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
246 *
247 * @ref: valid node reference to read property from
248 * @propname: name of the property to read from
249 * @index: index of the integer to return
250 * @outp: place to put value (if found)
251 * @return 0 if OK, -ve on error
252 */
253int ofnode_read_u32_index(ofnode node, const char *propname, int index,
254 u32 *outp);
255
256/**
Simon Glass9e512042017-05-18 20:08:58 -0600257 * ofnode_read_s32() - Read a 32-bit integer from a property
258 *
259 * @ref: valid node reference to read property from
260 * @propname: name of the property to read from
261 * @outp: place to put value (if found)
262 * @return 0 if OK, -ve on error
263 */
264static inline int ofnode_read_s32(ofnode node, const char *propname,
265 s32 *out_value)
266{
267 return ofnode_read_u32(node, propname, (u32 *)out_value);
268}
269
270/**
271 * ofnode_read_u32_default() - Read a 32-bit integer from a property
272 *
273 * @ref: valid node reference to read property from
274 * @propname: name of the property to read from
275 * @def: default value to return if the property has no value
276 * @return property value, or @def if not found
277 */
Trent Piephob061ef32019-05-10 17:48:20 +0000278u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
Simon Glass9e512042017-05-18 20:08:58 -0600279
280/**
Dario Binacchi4bb70752020-03-29 18:04:41 +0200281 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
282 * property
283 *
284 * @ref: valid node reference to read property from
285 * @propname: name of the property to read from
286 * @index: index of the integer to return
287 * @def: default value to return if the property has no value
288 * @return property value, or @def if not found
289 */
290u32 ofnode_read_u32_index_default(ofnode ref, const char *propname, int index,
291 u32 def);
292
293/**
Simon Glass9e512042017-05-18 20:08:58 -0600294 * ofnode_read_s32_default() - Read a 32-bit integer from a property
295 *
296 * @ref: valid node reference to read property from
297 * @propname: name of the property to read from
298 * @def: default value to return if the property has no value
299 * @return property value, or @def if not found
300 */
301int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
302
303/**
Lukas Auerafb30122018-11-22 11:26:35 +0100304 * ofnode_read_u64() - Read a 64-bit integer from a property
305 *
306 * @node: valid node reference to read property from
307 * @propname: name of the property to read from
308 * @outp: place to put value (if found)
309 * @return 0 if OK, -ve on error
310 */
311int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
312
313/**
Simon Glass7e5196c2018-06-11 13:07:10 -0600314 * ofnode_read_u64_default() - Read a 64-bit integer from a property
315 *
316 * @ref: valid node reference to read property from
317 * @propname: name of the property to read from
318 * @def: default value to return if the property has no value
319 * @return property value, or @def if not found
320 */
T Karthik Reddy3f3d7712019-09-02 16:34:30 +0200321u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
Simon Glass7e5196c2018-06-11 13:07:10 -0600322
323/**
Simon Glassa8167d82020-01-27 08:49:44 -0700324 * ofnode_read_prop() - Read a property from a node
325 *
326 * @node: valid node reference to read property from
327 * @propname: name of the property to read
328 * @sizep: if non-NULL, returns the size of the property, or an error code
329 if not found
330 * @return property value, or NULL if there is no such property
331 */
332const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
333
334/**
Simon Glass9e512042017-05-18 20:08:58 -0600335 * ofnode_read_string() - Read a string from a property
336 *
Simon Glassa8167d82020-01-27 08:49:44 -0700337 * @node: valid node reference to read property from
Simon Glass9e512042017-05-18 20:08:58 -0600338 * @propname: name of the property to read
339 * @return string from property value, or NULL if there is no such property
340 */
341const char *ofnode_read_string(ofnode node, const char *propname);
342
343/**
Simon Glassbed77492017-05-18 20:09:01 -0600344 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
Simon Glass9e512042017-05-18 20:08:58 -0600345 *
346 * @node: valid node reference to read property from
347 * @propname: name of the property to read
348 * @out_values: pointer to return value, modified only if return value is 0
349 * @sz: number of array elements to read
Simon Glassfbe8d032018-06-11 13:07:11 -0600350 * @return 0 if OK, -ve on error
Simon Glass9e512042017-05-18 20:08:58 -0600351 *
352 * Search for a property in a device node and read 32-bit value(s) from
353 * it. Returns 0 on success, -EINVAL if the property does not exist,
354 * -ENODATA if property does not have a value, and -EOVERFLOW if the
355 * property data isn't large enough.
356 *
357 * The out_values is modified only if a valid u32 value can be decoded.
358 */
359int ofnode_read_u32_array(ofnode node, const char *propname,
360 u32 *out_values, size_t sz);
361
362/**
363 * ofnode_read_bool() - read a boolean value from a property
364 *
365 * @node: valid node reference to read property from
366 * @propname: name of property to read
367 * @return true if property is present (meaning true), false if not present
368 */
369bool ofnode_read_bool(ofnode node, const char *propname);
370
371/**
372 * ofnode_find_subnode() - find a named subnode of a parent node
373 *
374 * @node: valid reference to parent node
375 * @subnode_name: name of subnode to find
376 * @return reference to subnode (which can be invalid if there is no such
377 * subnode)
378 */
379ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
380
Simon Glassec1add12020-12-16 17:25:06 -0700381#if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
382static inline bool ofnode_is_enabled(ofnode node)
383{
384 if (ofnode_is_np(node)) {
385 return of_device_is_available(ofnode_to_np(node));
386 } else {
387 return fdtdec_get_is_enabled(gd->fdt_blob,
388 ofnode_to_offset(node));
389 }
390}
391
392static inline ofnode ofnode_first_subnode(ofnode node)
393{
394 assert(ofnode_valid(node));
395 if (ofnode_is_np(node))
396 return np_to_ofnode(node.np->child);
397
398 return offset_to_ofnode(
399 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
400}
401
402static inline ofnode ofnode_next_subnode(ofnode node)
403{
404 assert(ofnode_valid(node));
405 if (ofnode_is_np(node))
406 return np_to_ofnode(node.np->sibling);
407
408 return offset_to_ofnode(
409 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
410}
411#else
412/**
413 * ofnode_is_enabled() - Checks whether a node is enabled.
414 * This looks for a 'status' property. If this exists, then returns true if
415 * the status is 'okay' and false otherwise. If there is no status property,
416 * it returns true on the assumption that anything mentioned should be enabled
417 * by default.
418 *
419 * @node: node to examine
420 * @return false (not enabled) or true (enabled)
421 */
422bool ofnode_is_enabled(ofnode node);
423
Simon Glass9e512042017-05-18 20:08:58 -0600424/**
425 * ofnode_first_subnode() - find the first subnode of a parent node
426 *
427 * @node: valid reference to a valid parent node
428 * @return reference to the first subnode (which can be invalid if the parent
429 * node has no subnodes)
430 */
431ofnode ofnode_first_subnode(ofnode node);
432
433/**
434 * ofnode_next_subnode() - find the next sibling of a subnode
435 *
436 * @node: valid reference to previous node (sibling)
437 * @return reference to the next subnode (which can be invalid if the node
438 * has no more siblings)
439 */
440ofnode ofnode_next_subnode(ofnode node);
Simon Glassec1add12020-12-16 17:25:06 -0700441#endif /* DM_INLINE_OFNODE */
Simon Glass9e512042017-05-18 20:08:58 -0600442
443/**
Philipp Tomsiche2d59972018-02-23 17:38:49 +0100444 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
445 *
446 * @node: valid node to look up
447 * @return ofnode reference of the parent node
448 */
449ofnode ofnode_get_parent(ofnode node);
450
451/**
Simon Glass9e512042017-05-18 20:08:58 -0600452 * ofnode_get_name() - get the name of a node
453 *
454 * @node: valid node to look up
Baruch Siach33810b42018-11-18 14:39:20 +0200455 * @return name of node
Simon Glass9e512042017-05-18 20:08:58 -0600456 */
457const char *ofnode_get_name(ofnode node);
458
459/**
Kever Yangb4f20762018-02-23 17:38:50 +0100460 * ofnode_get_by_phandle() - get ofnode from phandle
461 *
462 * @phandle: phandle to look up
463 * @return ofnode reference to the phandle
464 */
465ofnode ofnode_get_by_phandle(uint phandle);
466
467/**
Simon Glass9e512042017-05-18 20:08:58 -0600468 * ofnode_read_size() - read the size of a property
469 *
470 * @node: node to check
471 * @propname: property to check
472 * @return size of property if present, or -EINVAL if not
473 */
474int ofnode_read_size(ofnode node, const char *propname);
475
476/**
Keerthye679d032019-04-24 17:19:53 +0530477 * ofnode_get_addr_size_index() - get an address/size from a node
478 * based on index
479 *
480 * This reads the register address/size from a node based on index
481 *
482 * @node: node to read from
483 * @index: Index of address to read (0 for first)
484 * @size: Pointer to size of the address
485 * @return address, or FDT_ADDR_T_NONE if not present or invalid
486 */
487phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
488 fdt_size_t *size);
489
490/**
Simon Glassbed77492017-05-18 20:09:01 -0600491 * ofnode_get_addr_index() - get an address from a node
492 *
493 * This reads the register address from a node
494 *
495 * @node: node to read from
496 * @index: Index of address to read (0 for first)
497 * @return address, or FDT_ADDR_T_NONE if not present or invalid
498 */
499phys_addr_t ofnode_get_addr_index(ofnode node, int index);
500
501/**
502 * ofnode_get_addr() - get an address from a node
503 *
504 * This reads the register address from a node
505 *
506 * @node: node to read from
507 * @return address, or FDT_ADDR_T_NONE if not present or invalid
508 */
509phys_addr_t ofnode_get_addr(ofnode node);
510
511/**
Simon Glass9e512042017-05-18 20:08:58 -0600512 * ofnode_stringlist_search() - find a string in a string list and return index
513 *
514 * Note that it is possible for this function to succeed on property values
515 * that are not NUL-terminated. That's because the function will stop after
516 * finding the first occurrence of @string. This can for example happen with
517 * small-valued cell properties, such as #address-cells, when searching for
518 * the empty string.
519 *
520 * @node: node to check
521 * @propname: name of the property containing the string list
522 * @string: string to look up in the string list
523 *
524 * @return:
525 * the index of the string in the list of strings
526 * -ENODATA if the property is not found
527 * -EINVAL on some other error
528 */
529int ofnode_stringlist_search(ofnode node, const char *propname,
530 const char *string);
531
532/**
Simon Glass8c293d62017-06-12 06:21:28 -0600533 * ofnode_read_string_index() - obtain an indexed string from a string list
Simon Glass9e512042017-05-18 20:08:58 -0600534 *
535 * Note that this will successfully extract strings from properties with
536 * non-NUL-terminated values. For example on small-valued cell properties
537 * this function will return the empty string.
538 *
539 * If non-NULL, the length of the string (on success) or a negative error-code
540 * (on failure) will be stored in the integer pointer to by lenp.
541 *
542 * @node: node to check
543 * @propname: name of the property containing the string list
544 * @index: index of the string to return
545 * @lenp: return location for the string length or an error code on failure
546 *
547 * @return:
548 * length of string, if found or -ve error value if not found
549 */
550int ofnode_read_string_index(ofnode node, const char *propname, int index,
551 const char **outp);
552
553/**
Simon Glass8c293d62017-06-12 06:21:28 -0600554 * ofnode_read_string_count() - find the number of strings in a string list
555 *
556 * @node: node to check
557 * @propname: name of the property containing the string list
558 * @return:
559 * number of strings in the list, or -ve error value if not found
560 */
561int ofnode_read_string_count(ofnode node, const char *property);
562
563/**
Simon Glass9e512042017-05-18 20:08:58 -0600564 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
565 *
566 * This function is useful to parse lists of phandles and their arguments.
567 * Returns 0 on success and fills out_args, on error returns appropriate
568 * errno value.
569 *
570 * Caller is responsible to call of_node_put() on the returned out_args->np
571 * pointer.
572 *
573 * Example:
574 *
575 * phandle1: node1 {
576 * #list-cells = <2>;
577 * }
578 *
579 * phandle2: node2 {
580 * #list-cells = <1>;
581 * }
582 *
583 * node3 {
584 * list = <&phandle1 1 2 &phandle2 3>;
585 * }
586 *
587 * To get a device_node of the `node2' node you may call this:
588 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
589 *
590 * @node: device tree node containing a list
591 * @list_name: property name that contains a list
592 * @cells_name: property name that specifies phandles' arguments count
593 * @cells_count: Cell count to use if @cells_name is NULL
594 * @index: index of a phandle to parse out
595 * @out_args: optional pointer to output arguments structure (will be filled)
596 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
597 * @list_name does not exist, -EINVAL if a phandle was not found,
598 * @cells_name could not be found, the arguments were truncated or there
599 * were too many arguments.
600 */
601int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
602 const char *cells_name, int cell_count,
603 int index,
604 struct ofnode_phandle_args *out_args);
605
606/**
Patrice Chotard642346a2017-07-18 11:57:08 +0200607 * ofnode_count_phandle_with_args() - Count number of phandle in a list
608 *
609 * This function is useful to count phandles into a list.
610 * Returns number of phandle on success, on error returns appropriate
611 * errno value.
612 *
613 * @node: device tree node containing a list
614 * @list_name: property name that contains a list
615 * @cells_name: property name that specifies phandles' arguments count
Patrick Delaunay89f68302020-09-25 09:41:14 +0200616 * @cells_count: Cell count to use if @cells_name is NULL
Patrice Chotard642346a2017-07-18 11:57:08 +0200617 * @return number of phandle on success, -ENOENT if @list_name does not
618 * exist, -EINVAL if a phandle was not found, @cells_name could not
619 * be found.
620 */
621int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunay89f68302020-09-25 09:41:14 +0200622 const char *cells_name, int cell_count);
Patrice Chotard642346a2017-07-18 11:57:08 +0200623
624/**
Simon Glass9e512042017-05-18 20:08:58 -0600625 * ofnode_path() - find a node by full path
626 *
627 * @path: Full path to node, e.g. "/bus/spi@1"
628 * @return reference to the node found. Use ofnode_valid() to check if it exists
629 */
630ofnode ofnode_path(const char *path);
631
632/**
Simon Glassbd933bf2020-01-27 08:49:46 -0700633 * ofnode_read_chosen_prop() - get the value of a chosen property
634 *
635 * This looks for a property within the /chosen node and returns its value
636 *
637 * @propname: Property name to look for
638 * @sizep: Returns size of property, or FDT_ERR_... error code if function
639 * returns NULL
640 * @return property value if found, else NULL
641 */
642const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
643
644/**
Simon Glass14ca9f72020-01-27 08:49:43 -0700645 * ofnode_read_chosen_string() - get the string value of a chosen property
Simon Glass9e512042017-05-18 20:08:58 -0600646 *
Simon Glass14ca9f72020-01-27 08:49:43 -0700647 * This looks for a property within the /chosen node and returns its value,
648 * checking that it is a valid nul-terminated string
Simon Glass9e512042017-05-18 20:08:58 -0600649 *
650 * @propname: Property name to look for
Simon Glass14ca9f72020-01-27 08:49:43 -0700651 * @return string value if found, else NULL
Simon Glass9e512042017-05-18 20:08:58 -0600652 */
Simon Glass14ca9f72020-01-27 08:49:43 -0700653const char *ofnode_read_chosen_string(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600654
655/**
Simon Glass74d594a2020-01-27 08:49:42 -0700656 * ofnode_get_chosen_node() - get a referenced node from the chosen node
Simon Glass9e512042017-05-18 20:08:58 -0600657 *
Simon Glass74d594a2020-01-27 08:49:42 -0700658 * This looks up a named property in the chosen node and uses that as a path to
659 * look up a code.
660 *
661 * @return the referenced node if present, else ofnode_null()
Simon Glass9e512042017-05-18 20:08:58 -0600662 */
Simon Glass74d594a2020-01-27 08:49:42 -0700663ofnode ofnode_get_chosen_node(const char *propname);
Simon Glass9e512042017-05-18 20:08:58 -0600664
Michal Simek305d3182020-07-28 12:51:08 +0200665/**
666 * ofnode_read_aliases_prop() - get the value of a aliases property
667 *
668 * This looks for a property within the /aliases node and returns its value
669 *
670 * @propname: Property name to look for
671 * @sizep: Returns size of property, or FDT_ERR_... error code if function
672 * returns NULL
673 * @return property value if found, else NULL
674 */
675const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
676
677/**
678 * ofnode_get_aliases_node() - get a referenced node from the aliases node
679 *
680 * This looks up a named property in the aliases node and uses that as a path to
681 * look up a code.
682 *
683 * @return the referenced node if present, else ofnode_null()
684 */
685ofnode ofnode_get_aliases_node(const char *propname);
686
Simon Glass9e512042017-05-18 20:08:58 -0600687struct display_timing;
688/**
689 * ofnode_decode_display_timing() - decode display timings
690 *
691 * Decode display timings from the supplied 'display-timings' node.
692 * See doc/device-tree-bindings/video/display-timing.txt for binding
693 * information.
694 *
695 * @node 'display-timing' node containing the timing subnodes
696 * @index Index number to read (0=first timing subnode)
697 * @config Place to put timings
698 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
699 */
700int ofnode_decode_display_timing(ofnode node, int index,
701 struct display_timing *config);
702
703/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100704 * ofnode_get_property() - get a pointer to the value of a node property
Simon Glass9e512042017-05-18 20:08:58 -0600705 *
706 * @node: node to read
707 * @propname: property to read
708 * @lenp: place to put length on success
709 * @return pointer to property, or NULL if not found
710 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900711const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
Simon Glass9e512042017-05-18 20:08:58 -0600712
713/**
Patrick Delaunayce891fca2020-01-13 11:34:56 +0100714 * ofnode_get_first_property()- get the reference of the first property
715 *
716 * Get reference to the first property of the node, it is used to iterate
717 * and read all the property with ofnode_get_property_by_prop().
718 *
719 * @node: node to read
720 * @prop: place to put argument reference
721 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
722 */
723int ofnode_get_first_property(ofnode node, struct ofprop *prop);
724
725/**
726 * ofnode_get_next_property() - get the reference of the next property
727 *
728 * Get reference to the next property of the node, it is used to iterate
729 * and read all the property with ofnode_get_property_by_prop().
730 *
731 * @prop: reference of current argument and place to put reference of next one
732 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
733 */
734int ofnode_get_next_property(struct ofprop *prop);
735
736/**
737 * ofnode_get_property_by_prop() - get a pointer to the value of a property
738 *
739 * Get value for the property identified by the provided reference.
740 *
741 * @prop: reference on property
742 * @propname: If non-NULL, place to property name on success,
743 * @lenp: If non-NULL, place to put length on success
744 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
745 */
746const void *ofnode_get_property_by_prop(const struct ofprop *prop,
747 const char **propname, int *lenp);
748
749/**
Simon Glass9e512042017-05-18 20:08:58 -0600750 * ofnode_is_available() - check if a node is marked available
751 *
752 * @node: node to check
753 * @return true if node's 'status' property is "okay" (or is missing)
754 */
755bool ofnode_is_available(ofnode node);
756
757/**
758 * ofnode_get_addr_size() - get address and size from a property
759 *
760 * This does no address translation. It simply reads an property that contains
761 * an address and a size value, one after the other.
762 *
763 * @node: node to read from
764 * @propname: property to read
765 * @sizep: place to put size value (on success)
766 * @return address value, or FDT_ADDR_T_NONE on error
767 */
768phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
769 phys_size_t *sizep);
770
771/**
772 * ofnode_read_u8_array_ptr() - find an 8-bit array
773 *
774 * Look up a property in a node and return a pointer to its contents as a
775 * byte array of given length. The property must have at least enough data
776 * for the array (count bytes). It may have more, but this will be ignored.
777 * The data is not copied.
778 *
779 * @node node to examine
780 * @propname name of property to find
781 * @sz number of array elements
782 * @return pointer to byte array if found, or NULL if the property is not
783 * found or there is not enough data
784 */
785const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
786 size_t sz);
787
788/**
789 * ofnode_read_pci_addr() - look up a PCI address
790 *
791 * Look at an address property in a node and return the PCI address which
792 * corresponds to the given type in the form of fdt_pci_addr.
793 * The property must hold one fdt_pci_addr with a lengh.
794 *
795 * @node node to examine
796 * @type pci address type (FDT_PCI_SPACE_xxx)
797 * @propname name of property to find
798 * @addr returns pci address in the form of fdt_pci_addr
799 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
800 * format of the property was invalid, -ENXIO if the requested
801 * address type was not found
802 */
803int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
804 const char *propname, struct fdt_pci_addr *addr);
805
806/**
Bin Meng7b9cbad2018-08-03 01:14:35 -0700807 * ofnode_read_pci_vendev() - look up PCI vendor and device id
808 *
809 * Look at the compatible property of a device node that represents a PCI
810 * device and extract pci vendor id and device id from it.
811 *
812 * @param node node to examine
813 * @param vendor vendor id of the pci device
814 * @param device device id of the pci device
815 * @return 0 if ok, negative on error
816 */
817int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
818
819/**
Simon Glass9e512042017-05-18 20:08:58 -0600820 * ofnode_read_addr_cells() - Get the number of address cells for a node
821 *
822 * This walks back up the tree to find the closest #address-cells property
823 * which controls the given node.
824 *
825 * @node: Node to check
826 * @return number of address cells this node uses
827 */
828int ofnode_read_addr_cells(ofnode node);
829
830/**
831 * ofnode_read_size_cells() - Get the number of size cells for a node
832 *
833 * This walks back up the tree to find the closest #size-cells property
834 * which controls the given node.
835 *
836 * @node: Node to check
837 * @return number of size cells this node uses
838 */
839int ofnode_read_size_cells(ofnode node);
840
841/**
Simon Glass878d68c2017-06-12 06:21:31 -0600842 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
843 *
844 * This function matches fdt_address_cells().
845 *
846 * @np: Node pointer to check
847 * @return value of #address-cells property in this node, or 2 if none
848 */
849int ofnode_read_simple_addr_cells(ofnode node);
850
851/**
852 * ofnode_read_simple_size_cells() - Get the size cells property in a node
853 *
854 * This function matches fdt_size_cells().
855 *
856 * @np: Node pointer to check
857 * @return value of #size-cells property in this node, or 2 if none
858 */
859int ofnode_read_simple_size_cells(ofnode node);
860
861/**
Simon Glass9e512042017-05-18 20:08:58 -0600862 * ofnode_pre_reloc() - check if a node should be bound before relocation
863 *
864 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
865 * via special device tree properties.
866 *
867 * Before relocation this function can be used to check if nodes are required
868 * in either SPL or TPL stages.
869 *
870 * After relocation and jumping into the real U-Boot binary it is possible to
871 * determine if a node was bound in one of SPL/TPL stages.
872 *
Patrick Delaunay54e12232019-05-21 19:19:13 +0200873 * There are 4 settings currently in use
874 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
Simon Glass9e512042017-05-18 20:08:58 -0600875 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
876 * Existing platforms only use it to indicate nodes needed in
877 * SPL. Should probably be replaced by u-boot,dm-spl for
878 * new platforms.
Patrick Delaunay54e12232019-05-21 19:19:13 +0200879 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
880 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
Simon Glass9e512042017-05-18 20:08:58 -0600881 *
882 * @node: node to check
Simon Glassfbe8d032018-06-11 13:07:11 -0600883 * @return true if node is needed in SPL/TL, false otherwise
Simon Glass9e512042017-05-18 20:08:58 -0600884 */
885bool ofnode_pre_reloc(ofnode node);
886
Simon Glassc98ad442018-06-11 13:07:12 -0600887/**
888 * ofnode_read_resource() - Read a resource from a node
889 *
890 * Read resource information from a node at the given index
891 *
892 * @node: Node to read from
893 * @index: Index of resource to read (0 = first)
894 * @res: Returns resource that was read, on success
895 * @return 0 if OK, -ve on error
896 */
Simon Glassdcf98852017-07-25 08:29:55 -0600897int ofnode_read_resource(ofnode node, uint index, struct resource *res);
Simon Glassc98ad442018-06-11 13:07:12 -0600898
899/**
900 * ofnode_read_resource_byname() - Read a resource from a node by name
901 *
902 * Read resource information from a node matching the given name. This uses a
903 * 'reg-names' string list property with the names matching the associated
904 * 'reg' property list.
905 *
906 * @node: Node to read from
907 * @name: Name of resource to read
908 * @res: Returns resource that was read, on success
909 * @return 0 if OK, -ve on error
910 */
Masahiro Yamada7b8b47b2017-08-26 01:12:30 +0900911int ofnode_read_resource_byname(ofnode node, const char *name,
912 struct resource *res);
Simon Glassdcf98852017-07-25 08:29:55 -0600913
Simon Glass3991f422017-08-05 15:45:54 -0600914/**
Simon Glassc60f6712018-06-11 13:07:13 -0600915 * ofnode_by_compatible() - Find the next compatible node
916 *
917 * Find the next node after @from that is compatible with @compat
918 *
919 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
920 * @compat: Compatible string to match
921 * @return ofnode found, or ofnode_null() if none
922 */
923ofnode ofnode_by_compatible(ofnode from, const char *compat);
924
925/**
Jens Wiklander61fba0f2018-08-20 11:09:58 +0200926 * ofnode_by_prop_value() - Find the next node with given property value
927 *
928 * Find the next node after @from that has a @propname with a value
929 * @propval and a length @proplen.
930 *
931 * @from: ofnode to start from (use ofnode_null() to start at the
932 * beginning) @propname: property name to check @propval: property value to
933 * search for @proplen: length of the value in propval @return ofnode
934 * found, or ofnode_null() if none
935 */
936ofnode ofnode_by_prop_value(ofnode from, const char *propname,
937 const void *propval, int proplen);
938
939/**
Simon Glass3991f422017-08-05 15:45:54 -0600940 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
941 *
942 * @node: child node (ofnode, lvalue)
943 * @parent: parent node (ofnode)
944 *
945 * This is a wrapper around a for loop and is used like so:
946 *
947 * ofnode node;
948 *
949 * ofnode_for_each_subnode(node, parent) {
950 * Use node
951 * ...
952 * }
953 *
954 * Note that this is implemented as a macro and @node is used as
955 * iterator in the loop. The parent variable can be a constant or even a
956 * literal.
957 */
958#define ofnode_for_each_subnode(node, parent) \
959 for (node = ofnode_first_subnode(parent); \
960 ofnode_valid(node); \
961 node = ofnode_next_subnode(node))
962
Mario Six147c6072018-01-15 11:07:19 +0100963/**
Chunfeng Yun89b84b82020-05-02 11:35:09 +0200964 * ofnode_get_child_count() - get the child count of a ofnode
965 *
966 * @node: valid node to get its child count
967 * @return the number of subnodes
968 */
969int ofnode_get_child_count(ofnode parent);
970
971/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200972 * ofnode_translate_address() - Translate a device-tree address
Mario Six147c6072018-01-15 11:07:19 +0100973 *
974 * Translate an address from the device-tree into a CPU physical address. This
975 * function walks up the tree and applies the various bus mappings along the
976 * way.
977 *
978 * @ofnode: Device tree node giving the context in which to translate the
979 * address
980 * @in_addr: pointer to the address to translate
981 * @return the translated address; OF_BAD_ADDR on error
982 */
983u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +0900984
985/**
Fabien Dessenne641067f2019-05-31 15:11:30 +0200986 * ofnode_translate_dma_address() - Translate a device-tree DMA address
987 *
988 * Translate a DMA address from the device-tree into a CPU physical address.
989 * This function walks up the tree and applies the various bus mappings along
990 * the way.
991 *
992 * @ofnode: Device tree node giving the context in which to translate the
993 * DMA address
994 * @in_addr: pointer to the DMA address to translate
995 * @return the translated DMA address; OF_BAD_ADDR on error
996 */
997u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
998
999/**
Masahiro Yamada5ccc2c22018-04-19 12:14:02 +09001000 * ofnode_device_is_compatible() - check if the node is compatible with compat
1001 *
1002 * This allows to check whether the node is comaptible with the compat.
1003 *
1004 * @node: Device tree node for which compatible needs to be verified.
1005 * @compat: Compatible string which needs to verified in the given node.
1006 * @return true if OK, false if the compatible is not found
1007 */
1008int ofnode_device_is_compatible(ofnode node, const char *compat);
Mario Sixe369e582018-06-26 08:46:48 +02001009
1010/**
1011 * ofnode_write_prop() - Set a property of a ofnode
1012 *
1013 * Note that the value passed to the function is *not* allocated by the
1014 * function itself, but must be allocated by the caller if necessary.
1015 *
1016 * @node: The node for whose property should be set
1017 * @propname: The name of the property to set
1018 * @len: The length of the new value of the property
1019 * @value: The new value of the property (must be valid prior to calling
1020 * the function)
1021 * @return 0 if successful, -ve on error
1022 */
1023int ofnode_write_prop(ofnode node, const char *propname, int len,
1024 const void *value);
1025
1026/**
1027 * ofnode_write_string() - Set a string property of a ofnode
1028 *
1029 * Note that the value passed to the function is *not* allocated by the
1030 * function itself, but must be allocated by the caller if necessary.
1031 *
1032 * @node: The node for whose string property should be set
1033 * @propname: The name of the string property to set
1034 * @value: The new value of the string property (must be valid prior to
1035 * calling the function)
1036 * @return 0 if successful, -ve on error
1037 */
1038int ofnode_write_string(ofnode node, const char *propname, const char *value);
1039
1040/**
1041 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1042 * ofnode
1043 *
1044 * This function effectively sets the node's "status" property to either "okay"
1045 * or "disable", hence making it available for driver model initialization or
1046 * not.
1047 *
1048 * @node: The node to enable
1049 * @value: Flag that tells the function to either disable or enable the
1050 * node
1051 * @return 0 if successful, -ve on error
1052 */
1053int ofnode_set_enabled(ofnode node, bool value);
1054
Simon Glass4984de22017-05-17 17:18:10 -06001055#endif