2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 /* TODO(sjg@chromium.org): Drop fdtdec.h include */
15 /* Enable checks to protect against invalid calls */
21 * ofnode - reference to a device tree node
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.
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 *.
31 * The reference can also hold a null offset, in which case the pointer value
32 * here is NULL. This corresponds to a struct device_node * value of
33 * NULL, or an offset of -1.
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.
44 * For now these points use constant types, since we don't allow writing
47 * @np: Pointer to device node, used for live tree
48 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
49 * is not a really a pointer to a node: it is an offset value. See above.
51 typedef union ofnode_union {
52 const struct device_node *np; /* will be used for future live tree */
56 struct ofnode_phandle_args {
59 uint32_t args[OF_MAX_PHANDLE_ARGS];
63 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
65 * This cannot be called if the reference contains an offset.
67 * @node: Reference containing struct device_node * (possibly invalid)
68 * @return pointer to device node (can be NULL)
70 static inline const struct device_node *ofnode_to_np(ofnode node)
73 if (!of_live_active())
80 * ofnode_to_offset() - convert an ofnode to a flat DT offset
82 * This cannot be called if the reference contains a node pointer.
84 * @node: Reference containing offset (possibly invalid)
85 * @return DT offset (can be -1)
87 static inline int ofnode_to_offset(ofnode node)
93 return node.of_offset;
97 * ofnode_valid() - check if an ofnode is valid
99 * @return true if the reference contains a valid ofnode, false if it is NULL
101 static inline bool ofnode_valid(ofnode node)
103 if (of_live_active())
104 return node.np != NULL;
106 return node.of_offset != -1;
110 * offset_to_ofnode() - convert a DT offset to an ofnode
112 * @of_offset: DT offset (either valid, or -1)
113 * @return reference to the associated DT offset
115 static inline ofnode offset_to_ofnode(int of_offset)
119 if (of_live_active())
122 node.of_offset = of_offset;
128 * np_to_ofnode() - convert a node pointer to an ofnode
130 * @np: Live node pointer (can be NULL)
131 * @return reference to the associated node pointer
133 static inline ofnode np_to_ofnode(const struct device_node *np)
143 * ofnode_is_np() - check if a reference is a node pointer
145 * This function associated that if there is a valid live tree then all
146 * references will use it. This is because using the flat DT when the live tree
147 * is valid is not permitted.
149 * @node: reference to check (possibly invalid)
150 * @return true if the reference is a live node pointer, false if it is a DT
153 static inline bool ofnode_is_np(ofnode node)
157 * Check our assumption that flat tree offsets are not used when a
158 * live tree is in use.
160 assert(!ofnode_valid(node) ||
161 (of_live_active() ? _ofnode_to_np(node)
162 : _ofnode_to_np(node)));
164 return of_live_active() && ofnode_valid(node);
168 * ofnode_equal() - check if two references are equal
170 * @return true if equal, else false
172 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
174 /* We only need to compare the contents */
175 return ref1.of_offset == ref2.of_offset;
179 * ofnode_null() - Obtain a null ofnode
181 * This returns an ofnode which points to no node. It works both with the flat
184 static inline ofnode ofnode_null(void)
188 if (of_live_active())
197 * ofnode_read_u32() - Read a 32-bit integer from a property
199 * @ref: valid node reference to read property from
200 * @propname: name of the property to read from
201 * @outp: place to put value (if found)
202 * @return 0 if OK, -ve on error
204 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
207 * ofnode_read_s32() - Read a 32-bit integer from a property
209 * @ref: 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
214 static inline int ofnode_read_s32(ofnode node, const char *propname,
217 return ofnode_read_u32(node, propname, (u32 *)out_value);
221 * ofnode_read_u32_default() - Read a 32-bit integer from a property
223 * @ref: valid node reference to read property from
224 * @propname: name of the property to read from
225 * @def: default value to return if the property has no value
226 * @return property value, or @def if not found
228 int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
231 * ofnode_read_s32_default() - Read a 32-bit integer from a property
233 * @ref: valid node reference to read property from
234 * @propname: name of the property to read from
235 * @def: default value to return if the property has no value
236 * @return property value, or @def if not found
238 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
241 * ofnode_read_string() - Read a string from a property
243 * @ref: valid node reference to read property from
244 * @propname: name of the property to read
245 * @return string from property value, or NULL if there is no such property
247 const char *ofnode_read_string(ofnode node, const char *propname);
250 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
252 * @node: valid node reference to read property from
253 * @propname: name of the property to read
254 * @out_values: pointer to return value, modified only if return value is 0
255 * @sz: number of array elements to read
257 * Search for a property in a device node and read 32-bit value(s) from
258 * it. Returns 0 on success, -EINVAL if the property does not exist,
259 * -ENODATA if property does not have a value, and -EOVERFLOW if the
260 * property data isn't large enough.
262 * The out_values is modified only if a valid u32 value can be decoded.
264 int ofnode_read_u32_array(ofnode node, const char *propname,
265 u32 *out_values, size_t sz);
268 * ofnode_read_bool() - read a boolean value from a property
270 * @node: valid node reference to read property from
271 * @propname: name of property to read
272 * @return true if property is present (meaning true), false if not present
274 bool ofnode_read_bool(ofnode node, const char *propname);
277 * ofnode_find_subnode() - find a named subnode of a parent node
279 * @node: valid reference to parent node
280 * @subnode_name: name of subnode to find
281 * @return reference to subnode (which can be invalid if there is no such
284 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
287 * ofnode_first_subnode() - find the first subnode of a parent node
289 * @node: valid reference to a valid parent node
290 * @return reference to the first subnode (which can be invalid if the parent
291 * node has no subnodes)
293 ofnode ofnode_first_subnode(ofnode node);
296 * ofnode_next_subnode() - find the next sibling of a subnode
298 * @node: valid reference to previous node (sibling)
299 * @return reference to the next subnode (which can be invalid if the node
300 * has no more siblings)
302 ofnode ofnode_next_subnode(ofnode node);
305 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
307 * @node: valid node to look up
308 * @return ofnode reference of the parent node
310 ofnode ofnode_get_parent(ofnode node);
313 * ofnode_get_name() - get the name of a node
315 * @node: valid node to look up
316 * @return name or node
318 const char *ofnode_get_name(ofnode node);
321 * ofnode_get_by_phandle() - get ofnode from phandle
323 * @phandle: phandle to look up
324 * @return ofnode reference to the phandle
326 ofnode ofnode_get_by_phandle(uint phandle);
329 * ofnode_read_size() - read the size of a property
331 * @node: node to check
332 * @propname: property to check
333 * @return size of property if present, or -EINVAL if not
335 int ofnode_read_size(ofnode node, const char *propname);
338 * ofnode_get_addr_index() - get an address from a node
340 * This reads the register address from a node
342 * @node: node to read from
343 * @index: Index of address to read (0 for first)
344 * @return address, or FDT_ADDR_T_NONE if not present or invalid
346 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
349 * ofnode_get_addr() - get an address from a node
351 * This reads the register address from a node
353 * @node: node to read from
354 * @return address, or FDT_ADDR_T_NONE if not present or invalid
356 phys_addr_t ofnode_get_addr(ofnode node);
359 * ofnode_stringlist_search() - find a string in a string list and return index
361 * Note that it is possible for this function to succeed on property values
362 * that are not NUL-terminated. That's because the function will stop after
363 * finding the first occurrence of @string. This can for example happen with
364 * small-valued cell properties, such as #address-cells, when searching for
367 * @node: node to check
368 * @propname: name of the property containing the string list
369 * @string: string to look up in the string list
372 * the index of the string in the list of strings
373 * -ENODATA if the property is not found
374 * -EINVAL on some other error
376 int ofnode_stringlist_search(ofnode node, const char *propname,
380 * ofnode_read_string_index() - obtain an indexed string from a string list
382 * Note that this will successfully extract strings from properties with
383 * non-NUL-terminated values. For example on small-valued cell properties
384 * this function will return the empty string.
386 * If non-NULL, the length of the string (on success) or a negative error-code
387 * (on failure) will be stored in the integer pointer to by lenp.
389 * @node: node to check
390 * @propname: name of the property containing the string list
391 * @index: index of the string to return
392 * @lenp: return location for the string length or an error code on failure
395 * length of string, if found or -ve error value if not found
397 int ofnode_read_string_index(ofnode node, const char *propname, int index,
401 * ofnode_read_string_count() - find the number of strings in a string list
403 * @node: node to check
404 * @propname: name of the property containing the string list
406 * number of strings in the list, or -ve error value if not found
408 int ofnode_read_string_count(ofnode node, const char *property);
411 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
413 * This function is useful to parse lists of phandles and their arguments.
414 * Returns 0 on success and fills out_args, on error returns appropriate
417 * Caller is responsible to call of_node_put() on the returned out_args->np
431 * list = <&phandle1 1 2 &phandle2 3>;
434 * To get a device_node of the `node2' node you may call this:
435 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
437 * @node: device tree node containing a list
438 * @list_name: property name that contains a list
439 * @cells_name: property name that specifies phandles' arguments count
440 * @cells_count: Cell count to use if @cells_name is NULL
441 * @index: index of a phandle to parse out
442 * @out_args: optional pointer to output arguments structure (will be filled)
443 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
444 * @list_name does not exist, -EINVAL if a phandle was not found,
445 * @cells_name could not be found, the arguments were truncated or there
446 * were too many arguments.
448 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
449 const char *cells_name, int cell_count,
451 struct ofnode_phandle_args *out_args);
454 * ofnode_count_phandle_with_args() - Count number of phandle in a list
456 * This function is useful to count phandles into a list.
457 * Returns number of phandle on success, on error returns appropriate
460 * @node: device tree node containing a list
461 * @list_name: property name that contains a list
462 * @cells_name: property name that specifies phandles' arguments count
463 * @return number of phandle on success, -ENOENT if @list_name does not
464 * exist, -EINVAL if a phandle was not found, @cells_name could not
467 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
468 const char *cells_name);
471 * ofnode_path() - find a node by full path
473 * @path: Full path to node, e.g. "/bus/spi@1"
474 * @return reference to the node found. Use ofnode_valid() to check if it exists
476 ofnode ofnode_path(const char *path);
479 * ofnode_get_chosen_prop() - get the value of a chosen property
481 * This looks for a property within the /chosen node and returns its value
483 * @propname: Property name to look for
485 const char *ofnode_get_chosen_prop(const char *propname);
488 * ofnode_get_chosen_node() - get the chosen node
490 * @return the chosen node if present, else ofnode_null()
492 ofnode ofnode_get_chosen_node(const char *name);
494 struct display_timing;
496 * ofnode_decode_display_timing() - decode display timings
498 * Decode display timings from the supplied 'display-timings' node.
499 * See doc/device-tree-bindings/video/display-timing.txt for binding
502 * @node 'display-timing' node containing the timing subnodes
503 * @index Index number to read (0=first timing subnode)
504 * @config Place to put timings
505 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
507 int ofnode_decode_display_timing(ofnode node, int index,
508 struct display_timing *config);
511 * ofnode_get_property()- - get a pointer to the value of a node property
513 * @node: node to read
514 * @propname: property to read
515 * @lenp: place to put length on success
516 * @return pointer to property, or NULL if not found
518 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
521 * ofnode_is_available() - check if a node is marked available
523 * @node: node to check
524 * @return true if node's 'status' property is "okay" (or is missing)
526 bool ofnode_is_available(ofnode node);
529 * ofnode_get_addr_size() - get address and size from a property
531 * This does no address translation. It simply reads an property that contains
532 * an address and a size value, one after the other.
534 * @node: node to read from
535 * @propname: property to read
536 * @sizep: place to put size value (on success)
537 * @return address value, or FDT_ADDR_T_NONE on error
539 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
543 * ofnode_read_u8_array_ptr() - find an 8-bit array
545 * Look up a property in a node and return a pointer to its contents as a
546 * byte array of given length. The property must have at least enough data
547 * for the array (count bytes). It may have more, but this will be ignored.
548 * The data is not copied.
550 * @node node to examine
551 * @propname name of property to find
552 * @sz number of array elements
553 * @return pointer to byte array if found, or NULL if the property is not
554 * found or there is not enough data
556 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
560 * ofnode_read_pci_addr() - look up a PCI address
562 * Look at an address property in a node and return the PCI address which
563 * corresponds to the given type in the form of fdt_pci_addr.
564 * The property must hold one fdt_pci_addr with a lengh.
566 * @node node to examine
567 * @type pci address type (FDT_PCI_SPACE_xxx)
568 * @propname name of property to find
569 * @addr returns pci address in the form of fdt_pci_addr
570 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
571 * format of the property was invalid, -ENXIO if the requested
572 * address type was not found
574 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
575 const char *propname, struct fdt_pci_addr *addr);
578 * ofnode_read_addr_cells() - Get the number of address cells for a node
580 * This walks back up the tree to find the closest #address-cells property
581 * which controls the given node.
583 * @node: Node to check
584 * @return number of address cells this node uses
586 int ofnode_read_addr_cells(ofnode node);
589 * ofnode_read_size_cells() - Get the number of size cells for a node
591 * This walks back up the tree to find the closest #size-cells property
592 * which controls the given node.
594 * @node: Node to check
595 * @return number of size cells this node uses
597 int ofnode_read_size_cells(ofnode node);
600 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
602 * This function matches fdt_address_cells().
604 * @np: Node pointer to check
605 * @return value of #address-cells property in this node, or 2 if none
607 int ofnode_read_simple_addr_cells(ofnode node);
610 * ofnode_read_simple_size_cells() - Get the size cells property in a node
612 * This function matches fdt_size_cells().
614 * @np: Node pointer to check
615 * @return value of #size-cells property in this node, or 2 if none
617 int ofnode_read_simple_size_cells(ofnode node);
620 * ofnode_pre_reloc() - check if a node should be bound before relocation
622 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
623 * via special device tree properties.
625 * Before relocation this function can be used to check if nodes are required
626 * in either SPL or TPL stages.
628 * After relocation and jumping into the real U-Boot binary it is possible to
629 * determine if a node was bound in one of SPL/TPL stages.
631 * There are 3 settings currently in use
633 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
634 * Existing platforms only use it to indicate nodes needed in
635 * SPL. Should probably be replaced by u-boot,dm-spl for
638 * @node: node to check
639 * @eturns true if node is needed in SPL/TL, false otherwise
641 bool ofnode_pre_reloc(ofnode node);
643 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
644 int ofnode_read_resource_byname(ofnode node, const char *name,
645 struct resource *res);
648 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
650 * @node: child node (ofnode, lvalue)
651 * @parent: parent node (ofnode)
653 * This is a wrapper around a for loop and is used like so:
657 * ofnode_for_each_subnode(node, parent) {
662 * Note that this is implemented as a macro and @node is used as
663 * iterator in the loop. The parent variable can be a constant or even a
666 #define ofnode_for_each_subnode(node, parent) \
667 for (node = ofnode_first_subnode(parent); \
668 ofnode_valid(node); \
669 node = ofnode_next_subnode(node))
672 * ofnode_translate_address() - Tranlate a device-tree address
674 * Translate an address from the device-tree into a CPU physical address. This
675 * function walks up the tree and applies the various bus mappings along the
678 * @ofnode: Device tree node giving the context in which to translate the
680 * @in_addr: pointer to the address to translate
681 * @return the translated address; OF_BAD_ADDR on error
683 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);