1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 /* TODO(sjg@chromium.org): Drop fdtdec.h include */
13 #include <dm/of_access.h>
15 #include <phy_interface.h>
17 /* Enable checks to protect against invalid calls */
22 #include <dm/ofnode_decl.h>
24 struct ofnode_phandle_args {
27 uint32_t args[OF_MAX_PHANDLE_ARGS];
30 #if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)
32 * oftree_reset() - reset the state of the oftree list
34 * Reset the oftree list so it can be started again. This should be called
35 * once the control FDT is in place, but before the ofnode interface is used.
37 void oftree_reset(void);
40 * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
42 * This cannot be called if the reference contains a node pointer.
44 * @node: Reference containing offset (possibly invalid)
45 * Return: DT offset (can be NULL)
47 __attribute_const__ void *ofnode_to_fdt(ofnode node);
50 * ofnode_to_offset() - convert an ofnode to a flat DT offset
52 * This cannot be called if the reference contains a node pointer.
54 * @node: Reference containing offset (possibly invalid)
55 * Return: DT offset (can be -1)
57 __attribute_const__ int ofnode_to_offset(ofnode node);
60 * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
62 * If @fdt is not already registered in the list of current device trees, it is
65 * @fdt: Device tree to use
67 * Returns: reference to the given node
69 oftree oftree_from_fdt(void *fdt);
72 * noffset_to_ofnode() - convert a DT offset to an ofnode
74 * @other_node: Node in the same tree to use as a reference
75 * @of_offset: DT offset (either valid, or -1)
76 * Return: reference to the associated DT offset
78 ofnode noffset_to_ofnode(ofnode other_node, int of_offset);
80 #else /* !OFNODE_MULTI_TREE */
81 static inline void oftree_reset(void) {}
83 static inline void *ofnode_to_fdt(ofnode node)
89 /* Use the control FDT by default */
90 return (void *)gd->fdt_blob;
93 static inline __attribute_const__ int ofnode_to_offset(ofnode node)
99 return node.of_offset;
102 static inline oftree oftree_from_fdt(void *fdt)
106 /* we cannot access other trees without OFNODE_MULTI_TREE */
107 if (fdt == gd->fdt_blob)
115 static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
119 if (of_live_active())
122 node.of_offset = of_offset;
127 #endif /* OFNODE_MULTI_TREE */
130 * ofnode_to_np() - convert an ofnode to a live DT node pointer
132 * This cannot be called if the reference contains an offset.
134 * @node: Reference containing struct device_node * (possibly invalid)
135 * Return: pointer to device node (can be NULL)
137 static inline struct device_node *ofnode_to_np(ofnode node)
140 if (!of_live_active())
147 * ofnode_valid() - check if an ofnode is valid
149 * @node: Reference containing offset (possibly invalid)
150 * Return: true if the reference contains a valid ofnode, false if not
152 static inline bool ofnode_valid(ofnode node)
154 if (of_live_active())
155 return node.np != NULL;
157 return node.of_offset >= 0;
161 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
163 * This can only be called when flat tree is enabled
165 * @tree: Tree to look at
166 * @return FDT pointer from the tree
168 static inline void *oftree_lookup_fdt(oftree tree)
170 if (of_live_active())
177 * offset_to_ofnode() - convert a DT offset to an ofnode
179 * @of_offset: DT offset (either valid, or -1)
180 * Return: reference to the associated DT offset
182 static inline ofnode offset_to_ofnode(int of_offset)
186 if (of_live_active())
189 node.of_offset = of_offset >= 0 ? of_offset : -1;
195 * np_to_ofnode() - convert a node pointer to an ofnode
197 * @np: Live node pointer (can be NULL)
198 * Return: reference to the associated node pointer
200 static inline ofnode np_to_ofnode(struct device_node *np)
210 * ofnode_is_np() - check if a reference is a node pointer
212 * This function associated that if there is a valid live tree then all
213 * references will use it. This is because using the flat DT when the live tree
214 * is valid is not permitted.
216 * @node: reference to check (possibly invalid)
217 * Return: true if the reference is a live node pointer, false if it is a DT
220 static inline bool ofnode_is_np(ofnode node)
224 * Check our assumption that flat tree offsets are not used when a
225 * live tree is in use.
227 assert(!ofnode_valid(node) ||
228 (of_live_active() ? ofnode_to_np(node)
229 : ofnode_to_np(node)));
231 return of_live_active() && ofnode_valid(node);
235 * ofnode_equal() - check if two references are equal
237 * @ref1: first reference to check (possibly invalid)
238 * @ref2: second reference to check (possibly invalid)
239 * Return: true if equal, else false
241 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
243 /* We only need to compare the contents */
244 return ref1.of_offset == ref2.of_offset;
248 * oftree_valid() - check if an oftree is valid
250 * @tree: Reference containing oftree
251 * Return: true if the reference contains a valid oftree, false if node
253 static inline bool oftree_valid(oftree tree)
255 if (of_live_active())
262 * oftree_null() - Obtain a null oftree
264 * This returns an oftree which points to no tree. It works both with the flat
267 static inline oftree oftree_null(void)
271 if (of_live_active())
280 * ofnode_null() - Obtain a null ofnode
282 * This returns an ofnode which points to no node. It works both with the flat
285 static inline ofnode ofnode_null(void)
289 if (of_live_active())
297 static inline ofnode ofnode_root(void)
301 if (of_live_active())
302 node.np = gd_of_root();
310 * ofprop_valid() - check if an ofprop is valid
312 * @prop: Pointer to ofprop to check
313 * Return: true if the reference contains a valid ofprop, false if not
315 static inline bool ofprop_valid(struct ofprop *prop)
317 if (of_live_active())
320 return prop->offset >= 0;
324 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
326 * Returns: reference to the control FDT
328 static inline oftree oftree_default(void)
332 if (of_live_active())
333 tree.np = gd_of_root();
335 tree.fdt = (void *)gd->fdt_blob;
341 * oftree_from_np() - Returns an oftree from a node pointer
343 * @root: Root node of the tree
344 * Returns: reference to the given node
346 static inline oftree oftree_from_np(struct device_node *root)
356 * ofnode_name_eq() - Check if the node name is equivalent to a given name
357 * ignoring the unit address
359 * @node: valid node reference that has to be compared
360 * @name: name that has to be compared with the node name
361 * Return: true if matches, false if it doesn't match
363 bool ofnode_name_eq(ofnode node, const char *name);
366 * ofnode_read_u8() - Read a 8-bit integer from a property
368 * @node: valid node reference to read property from
369 * @propname: name of the property to read from
370 * @outp: place to put value (if found)
371 * Return: 0 if OK, -ve on error
373 int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
376 * ofnode_read_u8_default() - Read a 8-bit integer from a property
378 * @node: valid node reference to read property from
379 * @propname: name of the property to read from
380 * @def: default value to return if the property has no value
381 * Return: property value, or @def if not found
383 u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
386 * ofnode_read_u16() - Read a 16-bit integer from a property
388 * @node: valid node reference to read property from
389 * @propname: name of the property to read from
390 * @outp: place to put value (if found)
391 * Return: 0 if OK, -ve on error
393 int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
396 * ofnode_read_u16_default() - Read a 16-bit integer from a property
398 * @node: valid node reference to read property from
399 * @propname: name of the property to read from
400 * @def: default value to return if the property has no value
401 * Return: property value, or @def if not found
403 u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
406 * ofnode_read_u32() - Read a 32-bit integer from a property
408 * @node: valid node reference to read property from
409 * @propname: name of the property to read from
410 * @outp: place to put value (if found)
411 * Return: 0 if OK, -ve on error
413 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
416 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
418 * @node: valid node reference to read property from
419 * @propname: name of the property to read from
420 * @index: index of the integer to return
421 * @outp: place to put value (if found)
422 * Return: 0 if OK, -ve on error
424 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
428 * ofnode_read_s32() - Read a 32-bit integer from a property
430 * @node: valid node reference to read property from
431 * @propname: name of the property to read from
432 * @outp: place to put value (if found)
433 * Return: 0 if OK, -ve on error
435 static inline int ofnode_read_s32(ofnode node, const char *propname,
438 return ofnode_read_u32(node, propname, (u32 *)outp);
442 * ofnode_read_u32_default() - Read a 32-bit integer from a property
444 * @node: valid node reference to read property from
445 * @propname: name of the property to read from
446 * @def: default value to return if the property has no value
447 * Return: property value, or @def if not found
449 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
452 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
455 * @node: valid node reference to read property from
456 * @propname: name of the property to read from
457 * @index: index of the integer to return
458 * @def: default value to return if the property has no value
459 * Return: property value, or @def if not found
461 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
465 * ofnode_read_s32_default() - Read a 32-bit integer from a property
467 * @node: valid node reference to read property from
468 * @propname: name of the property to read from
469 * @def: default value to return if the property has no value
470 * Return: property value, or @def if not found
472 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
475 * ofnode_read_u64() - Read a 64-bit integer from a property
477 * @node: valid node reference to read property from
478 * @propname: name of the property to read from
479 * @outp: place to put value (if found)
480 * Return: 0 if OK, -ve on error
482 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
485 * ofnode_read_u64_default() - Read a 64-bit integer from a property
487 * @node: valid node reference to read property from
488 * @propname: name of the property to read from
489 * @def: default value to return if the property has no value
490 * Return: property value, or @def if not found
492 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
495 * ofnode_read_prop() - Read a property from a node
497 * @node: valid node reference to read property from
498 * @propname: name of the property to read
499 * @sizep: if non-NULL, returns the size of the property, or an error code
501 * Return: property value, or NULL if there is no such property
503 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
506 * ofnode_read_string() - Read a string from a property
508 * @node: valid node reference to read property from
509 * @propname: name of the property to read
510 * Return: string from property value, or NULL if there is no such property
512 const char *ofnode_read_string(ofnode node, const char *propname);
515 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
517 * @node: valid node reference to read property from
518 * @propname: name of the property to read
519 * @out_values: pointer to return value, modified only if return value is 0
520 * @sz: number of array elements to read
521 * Return: 0 on success, -EINVAL if the property does not exist,
522 * -ENODATA if property does not have a value, and -EOVERFLOW if the
523 * property data isn't large enough
525 * Search for a property in a device node and read 32-bit value(s) from
528 * The out_values is modified only if a valid u32 value can be decoded.
530 int ofnode_read_u32_array(ofnode node, const char *propname,
531 u32 *out_values, size_t sz);
534 * ofnode_read_bool() - read a boolean value from a property
536 * @node: valid node reference to read property from
537 * @propname: name of property to read
538 * Return: true if property is present (meaning true), false if not present
540 bool ofnode_read_bool(ofnode node, const char *propname);
543 * ofnode_find_subnode() - find a named subnode of a parent node
545 * @node: valid reference to parent node
546 * @subnode_name: name of subnode to find
547 * Return: reference to subnode (which can be invalid if there is no such
550 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
552 #if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
553 #include <asm/global_data.h>
555 static inline bool ofnode_is_enabled(ofnode node)
557 if (ofnode_is_np(node)) {
558 return of_device_is_available(ofnode_to_np(node));
560 return fdtdec_get_is_enabled(gd->fdt_blob,
561 ofnode_to_offset(node));
565 static inline ofnode ofnode_first_subnode(ofnode node)
567 assert(ofnode_valid(node));
568 if (ofnode_is_np(node))
569 return np_to_ofnode(node.np->child);
571 return offset_to_ofnode(
572 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
575 static inline ofnode ofnode_next_subnode(ofnode node)
577 assert(ofnode_valid(node));
578 if (ofnode_is_np(node))
579 return np_to_ofnode(node.np->sibling);
581 return offset_to_ofnode(
582 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
586 * ofnode_is_enabled() - Checks whether a node is enabled.
587 * This looks for a 'status' property. If this exists, then returns true if
588 * the status is 'okay' and false otherwise. If there is no status property,
589 * it returns true on the assumption that anything mentioned should be enabled
592 * @node: node to examine
593 * Return: false (not enabled) or true (enabled)
595 bool ofnode_is_enabled(ofnode node);
598 * ofnode_first_subnode() - find the first subnode of a parent node
600 * @node: valid reference to a valid parent node
601 * Return: reference to the first subnode (which can be invalid if the parent
602 * node has no subnodes)
604 ofnode ofnode_first_subnode(ofnode node);
607 * ofnode_next_subnode() - find the next sibling of a subnode
609 * @node: valid reference to previous node (sibling)
610 * Return: reference to the next subnode (which can be invalid if the node
611 * has no more siblings)
613 ofnode ofnode_next_subnode(ofnode node);
614 #endif /* DM_INLINE_OFNODE */
617 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
619 * @node: valid node to look up
620 * Return: ofnode reference of the parent node
622 ofnode ofnode_get_parent(ofnode node);
625 * ofnode_get_name() - get the name of a node
627 * @node: valid node to look up
628 * Return: name of node (for the root node this is "")
630 const char *ofnode_get_name(ofnode node);
633 * ofnode_get_path() - get the full path of a node
635 * @node: valid node to look up
636 * @buf: buffer to write the node path into
637 * @buflen: buffer size
638 * Return: 0 if OK, -ve on error
640 int ofnode_get_path(ofnode node, char *buf, int buflen);
643 * ofnode_get_by_phandle() - get ofnode from phandle
645 * This uses the default (control) device tree
647 * @phandle: phandle to look up
648 * Return: ofnode reference to the phandle
650 ofnode ofnode_get_by_phandle(uint phandle);
653 * oftree_get_by_phandle() - get ofnode from phandle
656 * @phandle: phandle to look up
657 * Return: ofnode reference to the phandle
659 ofnode oftree_get_by_phandle(oftree tree, uint phandle);
662 * ofnode_read_size() - read the size of a property
664 * @node: node to check
665 * @propname: property to check
666 * Return: size of property if present, or -EINVAL if not
668 int ofnode_read_size(ofnode node, const char *propname);
671 * ofnode_get_addr_size_index() - get an address/size from a node
674 * This reads the register address/size from a node based on index
676 * @node: node to read from
677 * @index: Index of address to read (0 for first)
678 * @size: Pointer to size of the address
679 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
681 phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
685 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
686 * based on index, without address
689 * This reads the register address/size from a node based on index.
690 * The resulting address is not translated. Useful for example for on-disk
693 * @node: node to read from
694 * @index: Index of address to read (0 for first)
695 * @size: Pointer to size of the address
696 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
698 phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
702 * ofnode_get_addr_index() - get an address from a node
704 * This reads the register address from a node
706 * @node: node to read from
707 * @index: Index of address to read (0 for first)
708 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
710 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
713 * ofnode_get_addr() - get an address from a node
715 * This reads the register address from a node
717 * @node: node to read from
718 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
720 phys_addr_t ofnode_get_addr(ofnode node);
723 * ofnode_get_size() - get size from a node
725 * This reads the register size from a node
727 * @node: node to read from
728 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
730 fdt_size_t ofnode_get_size(ofnode node);
733 * ofnode_stringlist_search() - find a string in a string list and return index
735 * Note that it is possible for this function to succeed on property values
736 * that are not NUL-terminated. That's because the function will stop after
737 * finding the first occurrence of @string. This can for example happen with
738 * small-valued cell properties, such as #address-cells, when searching for
741 * @node: node to check
742 * @propname: name of the property containing the string list
743 * @string: string to look up in the string list
746 * the index of the string in the list of strings
747 * -ENODATA if the property is not found
748 * -EINVAL on some other error
750 int ofnode_stringlist_search(ofnode node, const char *propname,
754 * ofnode_read_string_index() - obtain an indexed string from a string list
756 * Note that this will successfully extract strings from properties with
757 * non-NUL-terminated values. For example on small-valued cell properties
758 * this function will return the empty string.
760 * If non-NULL, the length of the string (on success) or a negative error-code
761 * (on failure) will be stored in the integer pointer to by lenp.
763 * @node: node to check
764 * @propname: name of the property containing the string list
765 * @index: index of the string to return (cannot be negative)
766 * @outp: return location for the string
769 * 0 if found or -ve error value if not found
771 int ofnode_read_string_index(ofnode node, const char *propname, int index,
775 * ofnode_read_string_count() - find the number of strings in a string list
777 * @node: node to check
778 * @property: name of the property containing the string list
780 * number of strings in the list, or -ve error value if not found
782 int ofnode_read_string_count(ofnode node, const char *property);
785 * ofnode_read_string_list() - read a list of strings
787 * This produces a list of string pointers with each one pointing to a string
788 * in the string list. If the property does not exist, it returns {NULL}.
790 * The data is allocated and the caller is reponsible for freeing the return
791 * value (the list of string pointers). The strings themselves may not be
792 * changed as they point directly into the devicetree property.
794 * @node: node to check
795 * @property: name of the property containing the string list
796 * @listp: returns an allocated, NULL-terminated list of strings if the return
797 * value is > 0, else is set to NULL
799 * number of strings in list, 0 if none, -ENOMEM if out of memory,
800 * -EINVAL if no such property, -EENODATA if property is empty
802 int ofnode_read_string_list(ofnode node, const char *property,
803 const char ***listp);
806 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
808 * This function is useful to parse lists of phandles and their arguments.
809 * Returns 0 on success and fills out_args, on error returns appropriate
812 * Caller is responsible to call of_node_put() on the returned out_args->np
826 * list = <&phandle1 1 2 &phandle2 3>;
829 * To get a device_node of the `node2' node you may call this:
830 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
832 * @node: device tree node containing a list
833 * @list_name: property name that contains a list
834 * @cells_name: property name that specifies phandles' arguments count
835 * @cell_count: Cell count to use if @cells_name is NULL
836 * @index: index of a phandle to parse out
837 * @out_args: optional pointer to output arguments structure (will be filled)
839 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
840 * @list_name does not exist, -EINVAL if a phandle was not found,
841 * @cells_name could not be found, the arguments were truncated or there
842 * were too many arguments.
844 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
845 const char *cells_name, int cell_count,
847 struct ofnode_phandle_args *out_args);
850 * ofnode_count_phandle_with_args() - Count number of phandle in a list
852 * This function is useful to count phandles into a list.
853 * Returns number of phandle on success, on error returns appropriate
856 * @node: device tree node containing a list
857 * @list_name: property name that contains a list
858 * @cells_name: property name that specifies phandles' arguments count
859 * @cell_count: Cell count to use if @cells_name is NULL
861 * number of phandle on success, -ENOENT if @list_name does not exist,
862 * -EINVAL if a phandle was not found, @cells_name could not be found.
864 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
865 const char *cells_name, int cell_count);
868 * ofnode_path() - find a node by full path
870 * This uses the control FDT.
872 * @path: Full path to node, e.g. "/bus/spi@1"
873 * Return: reference to the node found. Use ofnode_valid() to check if it exists
875 ofnode ofnode_path(const char *path);
878 * oftree_path() - find a node by full path from a root node
880 * @tree: Device tree to use
881 * @path: Full path to node, e.g. "/bus/spi@1"
882 * Return: reference to the node found. Use ofnode_valid() to check if it exists
884 ofnode oftree_path(oftree tree, const char *path);
887 * oftree_root() - get the root node of a tree
889 * @tree: Device tree to use
890 * Return: reference to the root node
892 ofnode oftree_root(oftree tree);
895 * ofnode_read_chosen_prop() - get the value of a chosen property
897 * This looks for a property within the /chosen node and returns its value.
899 * This only works with the control FDT.
901 * @propname: Property name to look for
902 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
904 * Return: property value if found, else NULL
906 const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
909 * ofnode_read_chosen_string() - get the string value of a chosen property
911 * This looks for a property within the /chosen node and returns its value,
912 * checking that it is a valid nul-terminated string
914 * This only works with the control FDT.
916 * @propname: Property name to look for
917 * Return: string value if found, else NULL
919 const char *ofnode_read_chosen_string(const char *propname);
922 * ofnode_get_chosen_node() - get a referenced node from the chosen node
924 * This looks up a named property in the chosen node and uses that as a path to
927 * This only works with the control FDT.
929 * @propname: Property name to look for
930 * Return: the referenced node if present, else ofnode_null()
932 ofnode ofnode_get_chosen_node(const char *propname);
935 * ofnode_read_aliases_prop() - get the value of a aliases property
937 * This looks for a property within the /aliases node and returns its value
939 * This only works with the control FDT.
941 * @propname: Property name to look for
942 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
944 * Return: property value if found, else NULL
946 const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
949 * ofnode_get_aliases_node() - get a referenced node from the aliases node
951 * This looks up a named property in the aliases node and uses that as a path to
954 * This only works with the control FDT.
956 * @propname: Property name to look for
957 * Return: the referenced node if present, else ofnode_null()
959 ofnode ofnode_get_aliases_node(const char *propname);
961 struct display_timing;
963 * ofnode_decode_display_timing() - decode display timings
965 * Decode display timings from the supplied 'display-timings' node.
966 * See doc/device-tree-bindings/video/display-timing.txt for binding
969 * @node: 'display-timing' node containing the timing subnodes
970 * @index: Index number to read (0=first timing subnode)
971 * @config: Place to put timings
972 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
974 int ofnode_decode_display_timing(ofnode node, int index,
975 struct display_timing *config);
978 * ofnode_get_property() - get a pointer to the value of a node property
980 * @node: node to read
981 * @propname: property to read
982 * @lenp: place to put length on success
983 * Return: pointer to property, or NULL if not found
985 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
988 * ofnode_first_property()- get the reference of the first property
990 * Get reference to the first property of the node, it is used to iterate
991 * and read all the property with ofprop_get_property().
993 * @node: node to read
994 * @prop: place to put argument reference
995 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
997 int ofnode_first_property(ofnode node, struct ofprop *prop);
1000 * ofnode_next_property() - get the reference of the next property
1002 * Get reference to the next property of the node, it is used to iterate
1003 * and read all the property with ofprop_get_property().
1005 * @prop: reference of current argument and place to put reference of next one
1006 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
1008 int ofnode_next_property(struct ofprop *prop);
1011 * ofnode_for_each_prop() - iterate over all properties of a node
1013 * @prop: struct ofprop
1014 * @node: node (lvalue, ofnode)
1016 * This is a wrapper around a for loop and is used like this::
1019 * struct ofprop prop;
1021 * ofnode_for_each_prop(prop, node) {
1025 * Note that this is implemented as a macro and @prop is used as
1026 * iterator in the loop. The parent variable can be a constant or even a
1029 #define ofnode_for_each_prop(prop, node) \
1030 for (ofnode_first_property(node, &prop); \
1031 ofprop_valid(&prop); \
1032 ofnode_next_property(&prop))
1035 * ofprop_get_property() - get a pointer to the value of a property
1037 * Get value for the property identified by the provided reference.
1039 * @prop: reference on property
1040 * @propname: If non-NULL, place to property name on success,
1041 * @lenp: If non-NULL, place to put length on success, or error code on failure
1042 * Return: pointer to property, or NULL if not found
1044 const void *ofprop_get_property(const struct ofprop *prop,
1045 const char **propname, int *lenp);
1048 * ofnode_get_addr_size() - get address and size from a property
1050 * This does no address translation. It simply reads an property that contains
1051 * an address and a size value, one after the other.
1053 * @node: node to read from
1054 * @propname: property to read
1055 * @sizep: place to put size value (on success)
1056 * Return: address value, or FDT_ADDR_T_NONE on error
1058 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1059 phys_size_t *sizep);
1062 * ofnode_read_u8_array_ptr() - find an 8-bit array
1064 * Look up a property in a node and return a pointer to its contents as a
1065 * byte array of given length. The property must have at least enough data
1066 * for the array (count bytes). It may have more, but this will be ignored.
1067 * The data is not copied.
1069 * @node: node to examine
1070 * @propname: name of property to find
1071 * @sz: number of array elements
1073 * pointer to byte array if found, or NULL if the property is not found or
1074 * there is not enough data
1076 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1080 * ofnode_read_pci_addr() - look up a PCI address
1082 * Look at an address property in a node and return the PCI address which
1083 * corresponds to the given type in the form of fdt_pci_addr.
1084 * The property must hold one fdt_pci_addr with a lengh.
1086 * @node: node to examine
1087 * @type: pci address type (FDT_PCI_SPACE_xxx)
1088 * @propname: name of property to find
1089 * @addr: returns pci address in the form of fdt_pci_addr
1091 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1092 * format of the property was invalid, -ENXIO if the requested
1093 * address type was not found
1095 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1096 const char *propname, struct fdt_pci_addr *addr);
1099 * ofnode_read_pci_vendev() - look up PCI vendor and device id
1101 * Look at the compatible property of a device node that represents a PCI
1102 * device and extract pci vendor id and device id from it.
1104 * @node: node to examine
1105 * @vendor: vendor id of the pci device
1106 * @device: device id of the pci device
1107 * Return: 0 if ok, negative on error
1109 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1112 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1114 * Look at the compatible property of a device node that represents a eth phy
1115 * device and extract phy vendor id and device id from it.
1117 * @node: node to examine
1118 * @vendor: vendor id of the eth phy device
1119 * @device: device id of the eth phy device
1120 * Return: 0 if ok, negative on error
1122 int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1125 * ofnode_read_addr_cells() - Get the number of address cells for a node
1127 * This walks back up the tree to find the closest #address-cells property
1128 * which controls the given node.
1130 * @node: Node to check
1131 * Return: number of address cells this node uses
1133 int ofnode_read_addr_cells(ofnode node);
1136 * ofnode_read_size_cells() - Get the number of size cells for a node
1138 * This walks back up the tree to find the closest #size-cells property
1139 * which controls the given node.
1141 * @node: Node to check
1142 * Return: number of size cells this node uses
1144 int ofnode_read_size_cells(ofnode node);
1147 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1149 * This function matches fdt_address_cells().
1151 * @node: Node to check
1152 * Return: value of #address-cells property in this node, or 2 if none
1154 int ofnode_read_simple_addr_cells(ofnode node);
1157 * ofnode_read_simple_size_cells() - Get the size cells property in a node
1159 * This function matches fdt_size_cells().
1161 * @node: Node to check
1162 * Return: value of #size-cells property in this node, or 2 if none
1164 int ofnode_read_simple_size_cells(ofnode node);
1167 * ofnode_pre_reloc() - check if a node should be bound before relocation
1169 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1170 * via special device tree properties.
1172 * Before relocation this function can be used to check if nodes are required
1173 * in either SPL or TPL stages.
1175 * After relocation and jumping into the real U-Boot binary it is possible to
1176 * determine if a node was bound in one of SPL/TPL stages.
1178 * There are 4 settings currently in use
1179 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
1180 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
1181 * Existing platforms only use it to indicate nodes needed in
1182 * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
1183 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
1184 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
1186 * @node: node to check
1187 * Return: true if node is needed in SPL/TL, false otherwise
1189 bool ofnode_pre_reloc(ofnode node);
1192 * ofnode_read_resource() - Read a resource from a node
1194 * Read resource information from a node at the given index
1196 * @node: Node to read from
1197 * @index: Index of resource to read (0 = first)
1198 * @res: Returns resource that was read, on success
1199 * Return: 0 if OK, -ve on error
1201 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
1204 * ofnode_read_resource_byname() - Read a resource from a node by name
1206 * Read resource information from a node matching the given name. This uses a
1207 * 'reg-names' string list property with the names matching the associated
1208 * 'reg' property list.
1210 * @node: Node to read from
1211 * @name: Name of resource to read
1212 * @res: Returns resource that was read, on success
1213 * Return: 0 if OK, -ve on error
1215 int ofnode_read_resource_byname(ofnode node, const char *name,
1216 struct resource *res);
1219 * ofnode_by_compatible() - Find the next compatible node
1221 * Find the next node after @from that is compatible with @compat
1223 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1224 * @compat: Compatible string to match
1225 * Return: ofnode found, or ofnode_null() if none
1227 ofnode ofnode_by_compatible(ofnode from, const char *compat);
1230 * ofnode_by_prop_value() - Find the next node with given property value
1232 * Find the next node after @from that has a @propname with a value
1233 * @propval and a length @proplen.
1235 * @from: ofnode to start from. Use ofnode_null() to start at the
1236 * beginning, or the return value from oftree_root() to start at the first
1238 * @propname: property name to check
1239 * @propval: property value to search for
1240 * @proplen: length of the value in propval
1241 * Return: ofnode found, or ofnode_null() if none
1243 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1244 const void *propval, int proplen);
1247 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1249 * @node: child node (ofnode, lvalue)
1250 * @parent: parent node (ofnode)
1252 * This is a wrapper around a for loop and is used like so::
1255 * ofnode_for_each_subnode(node, parent) {
1260 * Note that this is implemented as a macro and @node is used as
1261 * iterator in the loop. The parent variable can be a constant or even a
1264 #define ofnode_for_each_subnode(node, parent) \
1265 for (node = ofnode_first_subnode(parent); \
1266 ofnode_valid(node); \
1267 node = ofnode_next_subnode(node))
1270 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1273 * @node: child node (ofnode, lvalue)
1274 * @compat: compatible string to match
1276 * This is a wrapper around a for loop and is used like so::
1279 * ofnode_for_each_compatible_node(node, parent, compatible) {
1284 * Note that this is implemented as a macro and @node is used as
1285 * iterator in the loop.
1287 #define ofnode_for_each_compatible_node(node, compat) \
1288 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1289 ofnode_valid(node); \
1290 node = ofnode_by_compatible(node, compat))
1293 * ofnode_get_child_count() - get the child count of a ofnode
1295 * @parent: valid node to get its child count
1296 * Return: the number of subnodes
1298 int ofnode_get_child_count(ofnode parent);
1301 * ofnode_translate_address() - Translate a device-tree address
1303 * Translate an address from the device-tree into a CPU physical address. This
1304 * function walks up the tree and applies the various bus mappings along the
1307 * @node: Device tree node giving the context in which to translate the address
1308 * @in_addr: pointer to the address to translate
1309 * Return: the translated address; OF_BAD_ADDR on error
1311 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
1314 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1316 * Translate a DMA address from the device-tree into a CPU physical address.
1317 * This function walks up the tree and applies the various bus mappings along
1320 * @node: Device tree node giving the context in which to translate the
1322 * @in_addr: pointer to the DMA address to translate
1323 * Return: the translated DMA address; OF_BAD_ADDR on error
1325 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1328 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1330 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1331 * cpu->bus address translations
1333 * @node: Device tree node
1334 * @cpu: Pointer to variable storing the range's cpu address
1335 * @bus: Pointer to variable storing the range's bus address
1336 * @size: Pointer to variable storing the range's size
1337 * Return: translated DMA address or OF_BAD_ADDR on error
1339 int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1343 * ofnode_device_is_compatible() - check if the node is compatible with compat
1345 * This allows to check whether the node is comaptible with the compat.
1347 * @node: Device tree node for which compatible needs to be verified.
1348 * @compat: Compatible string which needs to verified in the given node.
1349 * Return: true if OK, false if the compatible is not found
1351 int ofnode_device_is_compatible(ofnode node, const char *compat);
1354 * ofnode_write_prop() - Set a property of a ofnode
1356 * Note that if @copy is false, the value passed to the function is *not*
1357 * allocated by the function itself, but must be allocated by the caller if
1358 * necessary. However it does allocate memory for the property struct and name.
1360 * @node: The node for whose property should be set
1361 * @propname: The name of the property to set
1362 * @value: The new value of the property (must be valid prior to calling
1364 * @len: The length of the new value of the property
1365 * @copy: true to allocate memory for the value. This only has any effect with
1366 * live tree, since flat tree handles this automatically. It allows a
1367 * node's value to be written to the tree, without requiring that the
1368 * caller allocate it
1369 * Return: 0 if successful, -ve on error
1371 int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1372 int len, bool copy);
1375 * ofnode_write_string() - Set a string property of a ofnode
1377 * Note that the value passed to the function is *not* allocated by the
1378 * function itself, but must be allocated by the caller if necessary.
1380 * @node: The node for whose string property should be set
1381 * @propname: The name of the string property to set
1382 * @value: The new value of the string property (must be valid prior to
1383 * calling the function)
1384 * Return: 0 if successful, -ve on error
1386 int ofnode_write_string(ofnode node, const char *propname, const char *value);
1389 * ofnode_write_u32() - Set an integer property of an ofnode
1391 * @node: The node for whose string property should be set
1392 * @propname: The name of the string property to set
1393 * @value: The new value of the 32-bit integer property
1394 * Return: 0 if successful, -ve on error
1396 int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1399 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1402 * This function effectively sets the node's "status" property to either "okay"
1403 * or "disable", hence making it available for driver model initialization or
1406 * @node: The node to enable
1407 * @value: Flag that tells the function to either disable or enable the
1409 * Return: 0 if successful, -ve on error
1411 int ofnode_set_enabled(ofnode node, bool value);
1414 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1416 * This function parses PHY handle from the Ethernet controller's ofnode
1417 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1419 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1420 * if the result to that is true, this function should not be called.
1422 * @eth_node: ofnode belonging to the Ethernet controller
1423 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1425 ofnode ofnode_get_phy_node(ofnode eth_node);
1428 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1430 * This function parses the "phy-mode" / "phy-connection-type" property and
1431 * returns the corresponding PHY interface type.
1433 * @mac_node: ofnode containing the property
1434 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1437 phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1439 #if CONFIG_IS_ENABLED(DM)
1441 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1443 * This reads a property from the /config node of the devicetree.
1445 * This only works with the control FDT.
1447 * See doc/device-tree-bindings/config.txt for bindings
1449 * @prop_name: property name to look up
1450 * Return: true, if it exists, false if not
1452 bool ofnode_conf_read_bool(const char *prop_name);
1455 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1457 * This reads a property from the /config node of the devicetree.
1459 * See doc/device-tree-bindings/config.txt for bindings
1461 * @prop_name: property name to look up
1462 * @default_val: default value to return if the property is not found
1463 * Return: integer value, if found, or @default_val if not
1465 int ofnode_conf_read_int(const char *prop_name, int default_val);
1468 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1470 * This reads a property from the /config node of the devicetree.
1472 * This only works with the control FDT.
1474 * See doc/device-tree-bindings/config.txt for bindings
1476 * @prop_name: property name to look up
1477 * Return: string value, if found, or NULL if not
1479 const char *ofnode_conf_read_str(const char *prop_name);
1481 #else /* CONFIG_DM */
1482 static inline bool ofnode_conf_read_bool(const char *prop_name)
1487 static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1492 static inline const char *ofnode_conf_read_str(const char *prop_name)
1497 #endif /* CONFIG_DM */
1500 * of_add_subnode() - add a new subnode to a node
1502 * @parent: parent node to add to
1503 * @name: name of subnode
1504 * @nodep: returns pointer to new subnode (valid if the function returns 0
1506 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1507 * -ve on other error
1509 int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1512 * ofnode_copy_props() - copy all properties from one node to another
1514 * Makes a copy of all properties from the source note in the destination node.
1515 * Existing properties in the destination node remain unchanged, except that
1516 * any with the same name are overwritten, including changing the size of the
1519 * For livetree, properties are copied / allocated, so the source tree does not
1520 * need to be present afterwards.
1522 * @src: Source node to read properties from
1523 * @dst: Destination node to write properties too
1525 int ofnode_copy_props(ofnode src, ofnode dst);