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];
31 * oftree_reset() - reset the state of the oftree list
33 * Reset the oftree list so it can be started again. This should be called
34 * once the control FDT is in place, but before the ofnode interface is used.
36 static inline void oftree_reset(void) {}
39 * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
41 * This cannot be called if the reference contains a node pointer.
43 * @node: Reference containing offset (possibly invalid)
44 * Return: DT offset (can be NULL)
46 static inline void *ofnode_to_fdt(ofnode node)
53 /* Use the control FDT by default */
54 return (void *)gd->fdt_blob;
58 * ofnode_to_offset() - convert an ofnode to a flat DT offset
60 * This cannot be called if the reference contains a node pointer.
62 * @node: Reference containing offset (possibly invalid)
63 * Return: DT offset (can be -1)
65 static inline int ofnode_to_offset(ofnode node)
71 return node.of_offset;
75 * ofnode_to_np() - convert an ofnode to a live DT node pointer
77 * This cannot be called if the reference contains an offset.
79 * @node: Reference containing struct device_node * (possibly invalid)
80 * Return: pointer to device node (can be NULL)
82 static inline struct device_node *ofnode_to_np(ofnode node)
85 if (!of_live_active())
92 * noffset_to_ofnode() - convert a DT offset to an ofnode
94 * @other_node: Node in the same tree to use as a reference
95 * @of_offset: DT offset (either valid, or -1)
96 * Return: reference to the associated DT offset
98 static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
102 if (of_live_active())
105 node.of_offset = of_offset;
111 * ofnode_valid() - check if an ofnode is valid
113 * @node: Reference containing offset (possibly invalid)
114 * Return: true if the reference contains a valid ofnode, false if it is NULL
116 static inline bool ofnode_valid(ofnode node)
118 if (of_live_active())
119 return node.np != NULL;
121 return node.of_offset >= 0;
125 * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
127 * This can only be called when flat tree is enabled
129 * @tree: Tree to look at
130 * @return FDT pointer from the tree
132 static inline void *oftree_lookup_fdt(oftree tree)
134 if (of_live_active())
141 * offset_to_ofnode() - convert a DT offset to an ofnode
143 * @of_offset: DT offset (either valid, or -1)
144 * Return: reference to the associated DT offset
146 static inline ofnode offset_to_ofnode(int of_offset)
150 if (of_live_active())
153 node.of_offset = of_offset >= 0 ? of_offset : -1;
159 * np_to_ofnode() - convert a node pointer to an ofnode
161 * @np: Live node pointer (can be NULL)
162 * Return: reference to the associated node pointer
164 static inline ofnode np_to_ofnode(struct device_node *np)
174 * ofnode_is_np() - check if a reference is a node pointer
176 * This function associated that if there is a valid live tree then all
177 * references will use it. This is because using the flat DT when the live tree
178 * is valid is not permitted.
180 * @node: reference to check (possibly invalid)
181 * Return: true if the reference is a live node pointer, false if it is a DT
184 static inline bool ofnode_is_np(ofnode node)
188 * Check our assumption that flat tree offsets are not used when a
189 * live tree is in use.
191 assert(!ofnode_valid(node) ||
192 (of_live_active() ? ofnode_to_np(node)
193 : ofnode_to_np(node)));
195 return of_live_active() && ofnode_valid(node);
199 * ofnode_equal() - check if two references are equal
201 * @ref1: first reference to check (possibly invalid)
202 * @ref2: second reference to check (possibly invalid)
203 * Return: true if equal, else false
205 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
207 /* We only need to compare the contents */
208 return ref1.of_offset == ref2.of_offset;
212 * oftree_valid() - check if an oftree is valid
214 * @tree: Reference containing oftree
215 * Return: true if the reference contains a valid oftree, false if node
217 static inline bool oftree_valid(oftree tree)
219 if (of_live_active())
226 * oftree_null() - Obtain a null oftree
228 * This returns an oftree which points to no tree. It works both with the flat
231 static inline oftree oftree_null(void)
235 if (of_live_active())
244 * ofnode_null() - Obtain a null ofnode
246 * This returns an ofnode which points to no node. It works both with the flat
249 static inline ofnode ofnode_null(void)
253 if (of_live_active())
261 static inline ofnode ofnode_root(void)
265 if (of_live_active())
266 node.np = gd_of_root();
274 * ofprop_valid() - check if an ofprop is valid
276 * @prop: Pointer to ofprop to check
277 * Return: true if the reference contains a valid ofprop, false if not
279 static inline bool ofprop_valid(struct ofprop *prop)
281 if (of_live_active())
284 return prop->offset >= 0;
288 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
290 * Returns: reference to the control FDT
292 static inline oftree oftree_default(void)
296 if (of_live_active())
297 tree.np = gd_of_root();
299 tree.fdt = (void *)gd->fdt_blob;
305 * oftree_from_np() - Returns an oftree from a node pointer
307 * @root: Root node of the tree
308 * Returns: reference to the given node
310 static inline oftree oftree_from_np(struct device_node *root)
320 * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
322 * @fdt: Device tree to use
324 * Returns: reference to the given node
326 static inline oftree oftree_from_fdt(void *fdt)
336 * ofnode_name_eq() - Check if the node name is equivalent to a given name
337 * ignoring the unit address
339 * @node: valid node reference that has to be compared
340 * @name: name that has to be compared with the node name
341 * Return: true if matches, false if it doesn't match
343 bool ofnode_name_eq(ofnode node, const char *name);
346 * ofnode_read_u8() - Read a 8-bit integer from a property
348 * @node: valid node reference to read property from
349 * @propname: name of the property to read from
350 * @outp: place to put value (if found)
351 * Return: 0 if OK, -ve on error
353 int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
356 * ofnode_read_u8_default() - Read a 8-bit integer from a property
358 * @node: valid node reference to read property from
359 * @propname: name of the property to read from
360 * @def: default value to return if the property has no value
361 * Return: property value, or @def if not found
363 u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
366 * ofnode_read_u16() - Read a 16-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_u16(ofnode node, const char *propname, u16 *outp);
376 * ofnode_read_u16_default() - Read a 16-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 u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
386 * ofnode_read_u32() - Read a 32-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_u32(ofnode node, const char *propname, u32 *outp);
396 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
398 * @node: valid node reference to read property from
399 * @propname: name of the property to read from
400 * @index: index of the integer to return
401 * @outp: place to put value (if found)
402 * Return: 0 if OK, -ve on error
404 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
408 * ofnode_read_s32() - Read a 32-bit integer from a property
410 * @node: valid node reference to read property from
411 * @propname: name of the property to read from
412 * @outp: place to put value (if found)
413 * Return: 0 if OK, -ve on error
415 static inline int ofnode_read_s32(ofnode node, const char *propname,
418 return ofnode_read_u32(node, propname, (u32 *)outp);
422 * ofnode_read_u32_default() - Read a 32-bit integer from a property
424 * @node: valid node reference to read property from
425 * @propname: name of the property to read from
426 * @def: default value to return if the property has no value
427 * Return: property value, or @def if not found
429 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
432 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
435 * @node: valid node reference to read property from
436 * @propname: name of the property to read from
437 * @index: index of the integer to return
438 * @def: default value to return if the property has no value
439 * Return: property value, or @def if not found
441 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
445 * ofnode_read_s32_default() - Read a 32-bit integer from a property
447 * @node: valid node reference to read property from
448 * @propname: name of the property to read from
449 * @def: default value to return if the property has no value
450 * Return: property value, or @def if not found
452 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
455 * ofnode_read_u64() - Read a 64-bit integer from a property
457 * @node: valid node reference to read property from
458 * @propname: name of the property to read from
459 * @outp: place to put value (if found)
460 * Return: 0 if OK, -ve on error
462 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
465 * ofnode_read_u64_default() - Read a 64-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 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
475 * ofnode_read_prop() - Read a property from a node
477 * @node: valid node reference to read property from
478 * @propname: name of the property to read
479 * @sizep: if non-NULL, returns the size of the property, or an error code
481 * Return: property value, or NULL if there is no such property
483 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
486 * ofnode_read_string() - Read a string from a property
488 * @node: valid node reference to read property from
489 * @propname: name of the property to read
490 * Return: string from property value, or NULL if there is no such property
492 const char *ofnode_read_string(ofnode node, const char *propname);
495 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
497 * @node: valid node reference to read property from
498 * @propname: name of the property to read
499 * @out_values: pointer to return value, modified only if return value is 0
500 * @sz: number of array elements to read
501 * Return: 0 on success, -EINVAL if the property does not exist,
502 * -ENODATA if property does not have a value, and -EOVERFLOW if the
503 * property data isn't large enough
505 * Search for a property in a device node and read 32-bit value(s) from
508 * The out_values is modified only if a valid u32 value can be decoded.
510 int ofnode_read_u32_array(ofnode node, const char *propname,
511 u32 *out_values, size_t sz);
514 * ofnode_read_bool() - read a boolean value from a property
516 * @node: valid node reference to read property from
517 * @propname: name of property to read
518 * Return: true if property is present (meaning true), false if not present
520 bool ofnode_read_bool(ofnode node, const char *propname);
523 * ofnode_find_subnode() - find a named subnode of a parent node
525 * @node: valid reference to parent node
526 * @subnode_name: name of subnode to find
527 * Return: reference to subnode (which can be invalid if there is no such
530 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
532 #if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
533 #include <asm/global_data.h>
535 static inline bool ofnode_is_enabled(ofnode node)
537 if (ofnode_is_np(node)) {
538 return of_device_is_available(ofnode_to_np(node));
540 return fdtdec_get_is_enabled(gd->fdt_blob,
541 ofnode_to_offset(node));
545 static inline ofnode ofnode_first_subnode(ofnode node)
547 assert(ofnode_valid(node));
548 if (ofnode_is_np(node))
549 return np_to_ofnode(node.np->child);
551 return offset_to_ofnode(
552 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
555 static inline ofnode ofnode_next_subnode(ofnode node)
557 assert(ofnode_valid(node));
558 if (ofnode_is_np(node))
559 return np_to_ofnode(node.np->sibling);
561 return offset_to_ofnode(
562 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
566 * ofnode_is_enabled() - Checks whether a node is enabled.
567 * This looks for a 'status' property. If this exists, then returns true if
568 * the status is 'okay' and false otherwise. If there is no status property,
569 * it returns true on the assumption that anything mentioned should be enabled
572 * @node: node to examine
573 * Return: false (not enabled) or true (enabled)
575 bool ofnode_is_enabled(ofnode node);
578 * ofnode_first_subnode() - find the first subnode of a parent node
580 * @node: valid reference to a valid parent node
581 * Return: reference to the first subnode (which can be invalid if the parent
582 * node has no subnodes)
584 ofnode ofnode_first_subnode(ofnode node);
587 * ofnode_next_subnode() - find the next sibling of a subnode
589 * @node: valid reference to previous node (sibling)
590 * Return: reference to the next subnode (which can be invalid if the node
591 * has no more siblings)
593 ofnode ofnode_next_subnode(ofnode node);
594 #endif /* DM_INLINE_OFNODE */
597 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
599 * @node: valid node to look up
600 * Return: ofnode reference of the parent node
602 ofnode ofnode_get_parent(ofnode node);
605 * ofnode_get_name() - get the name of a node
607 * @node: valid node to look up
608 * Return: name of node (for the root node this is "")
610 const char *ofnode_get_name(ofnode node);
613 * ofnode_get_path() - get the full path of a node
615 * @node: valid node to look up
616 * @buf: buffer to write the node path into
617 * @buflen: buffer size
618 * Return: 0 if OK, -ve on error
620 int ofnode_get_path(ofnode node, char *buf, int buflen);
623 * ofnode_get_by_phandle() - get ofnode from phandle
625 * This uses the default (control) device tree
627 * @phandle: phandle to look up
628 * Return: ofnode reference to the phandle
630 ofnode ofnode_get_by_phandle(uint phandle);
633 * oftree_get_by_phandle() - get ofnode from phandle
636 * @phandle: phandle to look up
637 * Return: ofnode reference to the phandle
639 ofnode oftree_get_by_phandle(oftree tree, uint phandle);
642 * ofnode_read_size() - read the size of a property
644 * @node: node to check
645 * @propname: property to check
646 * Return: size of property if present, or -EINVAL if not
648 int ofnode_read_size(ofnode node, const char *propname);
651 * ofnode_get_addr_size_index() - get an address/size from a node
654 * This reads the register address/size from a node based on index
656 * @node: node to read from
657 * @index: Index of address to read (0 for first)
658 * @size: Pointer to size of the address
659 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
661 phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
665 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
666 * based on index, without address
669 * This reads the register address/size from a node based on index.
670 * The resulting address is not translated. Useful for example for on-disk
673 * @node: node to read from
674 * @index: Index of address to read (0 for first)
675 * @size: Pointer to size of the address
676 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
678 phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
682 * ofnode_get_addr_index() - get an address from a node
684 * This reads the register address from a node
686 * @node: node to read from
687 * @index: Index of address to read (0 for first)
688 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
690 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
693 * ofnode_get_addr() - get an address from a node
695 * This reads the register address from a node
697 * @node: node to read from
698 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
700 phys_addr_t ofnode_get_addr(ofnode node);
703 * ofnode_get_size() - get size from a node
705 * This reads the register size from a node
707 * @node: node to read from
708 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
710 fdt_size_t ofnode_get_size(ofnode node);
713 * ofnode_stringlist_search() - find a string in a string list and return index
715 * Note that it is possible for this function to succeed on property values
716 * that are not NUL-terminated. That's because the function will stop after
717 * finding the first occurrence of @string. This can for example happen with
718 * small-valued cell properties, such as #address-cells, when searching for
721 * @node: node to check
722 * @propname: name of the property containing the string list
723 * @string: string to look up in the string list
726 * the index of the string in the list of strings
727 * -ENODATA if the property is not found
728 * -EINVAL on some other error
730 int ofnode_stringlist_search(ofnode node, const char *propname,
734 * ofnode_read_string_index() - obtain an indexed string from a string list
736 * Note that this will successfully extract strings from properties with
737 * non-NUL-terminated values. For example on small-valued cell properties
738 * this function will return the empty string.
740 * If non-NULL, the length of the string (on success) or a negative error-code
741 * (on failure) will be stored in the integer pointer to by lenp.
743 * @node: node to check
744 * @propname: name of the property containing the string list
745 * @index: index of the string to return (cannot be negative)
746 * @outp: return location for the string
749 * 0 if found or -ve error value if not found
751 int ofnode_read_string_index(ofnode node, const char *propname, int index,
755 * ofnode_read_string_count() - find the number of strings in a string list
757 * @node: node to check
758 * @property: name of the property containing the string list
760 * number of strings in the list, or -ve error value if not found
762 int ofnode_read_string_count(ofnode node, const char *property);
765 * ofnode_read_string_list() - read a list of strings
767 * This produces a list of string pointers with each one pointing to a string
768 * in the string list. If the property does not exist, it returns {NULL}.
770 * The data is allocated and the caller is reponsible for freeing the return
771 * value (the list of string pointers). The strings themselves may not be
772 * changed as they point directly into the devicetree property.
774 * @node: node to check
775 * @property: name of the property containing the string list
776 * @listp: returns an allocated, NULL-terminated list of strings if the return
777 * value is > 0, else is set to NULL
779 * number of strings in list, 0 if none, -ENOMEM if out of memory,
780 * -EINVAL if no such property, -EENODATA if property is empty
782 int ofnode_read_string_list(ofnode node, const char *property,
783 const char ***listp);
786 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
788 * This function is useful to parse lists of phandles and their arguments.
789 * Returns 0 on success and fills out_args, on error returns appropriate
792 * Caller is responsible to call of_node_put() on the returned out_args->np
806 * list = <&phandle1 1 2 &phandle2 3>;
809 * To get a device_node of the `node2' node you may call this:
810 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
812 * @node: device tree node containing a list
813 * @list_name: property name that contains a list
814 * @cells_name: property name that specifies phandles' arguments count
815 * @cell_count: Cell count to use if @cells_name is NULL
816 * @index: index of a phandle to parse out
817 * @out_args: optional pointer to output arguments structure (will be filled)
819 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
820 * @list_name does not exist, -EINVAL if a phandle was not found,
821 * @cells_name could not be found, the arguments were truncated or there
822 * were too many arguments.
824 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
825 const char *cells_name, int cell_count,
827 struct ofnode_phandle_args *out_args);
830 * ofnode_count_phandle_with_args() - Count number of phandle in a list
832 * This function is useful to count phandles into a list.
833 * Returns number of phandle on success, on error returns appropriate
836 * @node: device tree node containing a list
837 * @list_name: property name that contains a list
838 * @cells_name: property name that specifies phandles' arguments count
839 * @cell_count: Cell count to use if @cells_name is NULL
841 * number of phandle on success, -ENOENT if @list_name does not exist,
842 * -EINVAL if a phandle was not found, @cells_name could not be found.
844 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
845 const char *cells_name, int cell_count);
848 * ofnode_path() - find a node by full path
850 * This uses the control FDT.
852 * @path: Full path to node, e.g. "/bus/spi@1"
853 * Return: reference to the node found. Use ofnode_valid() to check if it exists
855 ofnode ofnode_path(const char *path);
858 * ofnode_path_root() - find a node by full path from a root node
860 * @tree: Device tree to use
861 * @path: Full path to node, e.g. "/bus/spi@1"
862 * Return: reference to the node found. Use ofnode_valid() to check if it exists
864 ofnode ofnode_path_root(oftree tree, const char *path);
867 * ofnode_read_chosen_prop() - get the value of a chosen property
869 * This looks for a property within the /chosen node and returns its value
871 * @propname: Property name to look for
872 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
874 * Return: property value if found, else NULL
876 const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
879 * ofnode_read_chosen_string() - get the string value of a chosen property
881 * This looks for a property within the /chosen node and returns its value,
882 * checking that it is a valid nul-terminated string
884 * @propname: Property name to look for
885 * Return: string value if found, else NULL
887 const char *ofnode_read_chosen_string(const char *propname);
890 * ofnode_get_chosen_node() - get a referenced node from the chosen node
892 * This looks up a named property in the chosen node and uses that as a path to
895 * @propname: Property name to look for
896 * Return: the referenced node if present, else ofnode_null()
898 ofnode ofnode_get_chosen_node(const char *propname);
901 * ofnode_read_aliases_prop() - get the value of a aliases property
903 * This looks for a property within the /aliases node and returns its value
905 * @propname: Property name to look for
906 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
908 * Return: property value if found, else NULL
910 const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
913 * ofnode_get_aliases_node() - get a referenced node from the aliases node
915 * This looks up a named property in the aliases node and uses that as a path to
918 * @propname: Property name to look for
919 * Return: the referenced node if present, else ofnode_null()
921 ofnode ofnode_get_aliases_node(const char *propname);
923 struct display_timing;
925 * ofnode_decode_display_timing() - decode display timings
927 * Decode display timings from the supplied 'display-timings' node.
928 * See doc/device-tree-bindings/video/display-timing.txt for binding
931 * @node: 'display-timing' node containing the timing subnodes
932 * @index: Index number to read (0=first timing subnode)
933 * @config: Place to put timings
934 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
936 int ofnode_decode_display_timing(ofnode node, int index,
937 struct display_timing *config);
940 * ofnode_get_property() - get a pointer to the value of a node property
942 * @node: node to read
943 * @propname: property to read
944 * @lenp: place to put length on success
945 * Return: pointer to property, or NULL if not found
947 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
950 * ofnode_first_property()- get the reference of the first property
952 * Get reference to the first property of the node, it is used to iterate
953 * and read all the property with ofprop_get_property().
955 * @node: node to read
956 * @prop: place to put argument reference
957 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
959 int ofnode_first_property(ofnode node, struct ofprop *prop);
962 * ofnode_next_property() - get the reference of the next property
964 * Get reference to the next property of the node, it is used to iterate
965 * and read all the property with ofprop_get_property().
967 * @prop: reference of current argument and place to put reference of next one
968 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
970 int ofnode_next_property(struct ofprop *prop);
973 * ofnode_for_each_prop() - iterate over all properties of a node
975 * @prop: struct ofprop
976 * @node: node (lvalue, ofnode)
978 * This is a wrapper around a for loop and is used like this::
981 * struct ofprop prop;
983 * ofnode_for_each_prop(prop, node) {
987 * Note that this is implemented as a macro and @prop is used as
988 * iterator in the loop. The parent variable can be a constant or even a
991 #define ofnode_for_each_prop(prop, node) \
992 for (ofnode_first_property(node, &prop); \
993 ofprop_valid(&prop); \
994 ofnode_next_property(&prop))
997 * ofprop_get_property() - get a pointer to the value of a property
999 * Get value for the property identified by the provided reference.
1001 * @prop: reference on property
1002 * @propname: If non-NULL, place to property name on success,
1003 * @lenp: If non-NULL, place to put length on success, or error code on failure
1004 * Return: pointer to property, or NULL if not found
1006 const void *ofprop_get_property(const struct ofprop *prop,
1007 const char **propname, int *lenp);
1010 * ofnode_get_addr_size() - get address and size from a property
1012 * This does no address translation. It simply reads an property that contains
1013 * an address and a size value, one after the other.
1015 * @node: node to read from
1016 * @propname: property to read
1017 * @sizep: place to put size value (on success)
1018 * Return: address value, or FDT_ADDR_T_NONE on error
1020 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1021 phys_size_t *sizep);
1024 * ofnode_read_u8_array_ptr() - find an 8-bit array
1026 * Look up a property in a node and return a pointer to its contents as a
1027 * byte array of given length. The property must have at least enough data
1028 * for the array (count bytes). It may have more, but this will be ignored.
1029 * The data is not copied.
1031 * @node: node to examine
1032 * @propname: name of property to find
1033 * @sz: number of array elements
1035 * pointer to byte array if found, or NULL if the property is not found or
1036 * there is not enough data
1038 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1042 * ofnode_read_pci_addr() - look up a PCI address
1044 * Look at an address property in a node and return the PCI address which
1045 * corresponds to the given type in the form of fdt_pci_addr.
1046 * The property must hold one fdt_pci_addr with a lengh.
1048 * @node: node to examine
1049 * @type: pci address type (FDT_PCI_SPACE_xxx)
1050 * @propname: name of property to find
1051 * @addr: returns pci address in the form of fdt_pci_addr
1053 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1054 * format of the property was invalid, -ENXIO if the requested
1055 * address type was not found
1057 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1058 const char *propname, struct fdt_pci_addr *addr);
1061 * ofnode_read_pci_vendev() - look up PCI vendor and device id
1063 * Look at the compatible property of a device node that represents a PCI
1064 * device and extract pci vendor id and device id from it.
1066 * @node: node to examine
1067 * @vendor: vendor id of the pci device
1068 * @device: device id of the pci device
1069 * Return: 0 if ok, negative on error
1071 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1074 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1076 * Look at the compatible property of a device node that represents a eth phy
1077 * device and extract phy vendor id and device id from it.
1079 * @node: node to examine
1080 * @vendor: vendor id of the eth phy device
1081 * @device: device id of the eth phy device
1082 * Return: 0 if ok, negative on error
1084 int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1087 * ofnode_read_addr_cells() - Get the number of address cells for a node
1089 * This walks back up the tree to find the closest #address-cells property
1090 * which controls the given node.
1092 * @node: Node to check
1093 * Return: number of address cells this node uses
1095 int ofnode_read_addr_cells(ofnode node);
1098 * ofnode_read_size_cells() - Get the number of size cells for a node
1100 * This walks back up the tree to find the closest #size-cells property
1101 * which controls the given node.
1103 * @node: Node to check
1104 * Return: number of size cells this node uses
1106 int ofnode_read_size_cells(ofnode node);
1109 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1111 * This function matches fdt_address_cells().
1113 * @node: Node to check
1114 * Return: value of #address-cells property in this node, or 2 if none
1116 int ofnode_read_simple_addr_cells(ofnode node);
1119 * ofnode_read_simple_size_cells() - Get the size cells property in a node
1121 * This function matches fdt_size_cells().
1123 * @node: Node to check
1124 * Return: value of #size-cells property in this node, or 2 if none
1126 int ofnode_read_simple_size_cells(ofnode node);
1129 * ofnode_pre_reloc() - check if a node should be bound before relocation
1131 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1132 * via special device tree properties.
1134 * Before relocation this function can be used to check if nodes are required
1135 * in either SPL or TPL stages.
1137 * After relocation and jumping into the real U-Boot binary it is possible to
1138 * determine if a node was bound in one of SPL/TPL stages.
1140 * There are 4 settings currently in use
1141 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
1142 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
1143 * Existing platforms only use it to indicate nodes needed in
1144 * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
1145 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
1146 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
1148 * @node: node to check
1149 * Return: true if node is needed in SPL/TL, false otherwise
1151 bool ofnode_pre_reloc(ofnode node);
1154 * ofnode_read_resource() - Read a resource from a node
1156 * Read resource information from a node at the given index
1158 * @node: Node to read from
1159 * @index: Index of resource to read (0 = first)
1160 * @res: Returns resource that was read, on success
1161 * Return: 0 if OK, -ve on error
1163 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
1166 * ofnode_read_resource_byname() - Read a resource from a node by name
1168 * Read resource information from a node matching the given name. This uses a
1169 * 'reg-names' string list property with the names matching the associated
1170 * 'reg' property list.
1172 * @node: Node to read from
1173 * @name: Name of resource to read
1174 * @res: Returns resource that was read, on success
1175 * Return: 0 if OK, -ve on error
1177 int ofnode_read_resource_byname(ofnode node, const char *name,
1178 struct resource *res);
1181 * ofnode_by_compatible() - Find the next compatible node
1183 * Find the next node after @from that is compatible with @compat
1185 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1186 * @compat: Compatible string to match
1187 * Return: ofnode found, or ofnode_null() if none
1189 ofnode ofnode_by_compatible(ofnode from, const char *compat);
1192 * ofnode_by_prop_value() - Find the next node with given property value
1194 * Find the next node after @from that has a @propname with a value
1195 * @propval and a length @proplen.
1197 * @from: ofnode to start from. Use ofnode_null() to start at the
1198 * beginning, or the return value from oftree_root() to start at the first
1200 * @propname: property name to check
1201 * @propval: property value to search for
1202 * @proplen: length of the value in propval
1203 * Return: ofnode found, or ofnode_null() if none
1205 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1206 const void *propval, int proplen);
1209 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1211 * @node: child node (ofnode, lvalue)
1212 * @parent: parent node (ofnode)
1214 * This is a wrapper around a for loop and is used like so::
1217 * ofnode_for_each_subnode(node, parent) {
1222 * Note that this is implemented as a macro and @node is used as
1223 * iterator in the loop. The parent variable can be a constant or even a
1226 #define ofnode_for_each_subnode(node, parent) \
1227 for (node = ofnode_first_subnode(parent); \
1228 ofnode_valid(node); \
1229 node = ofnode_next_subnode(node))
1232 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1235 * @node: child node (ofnode, lvalue)
1236 * @compat: compatible string to match
1238 * This is a wrapper around a for loop and is used like so::
1241 * ofnode_for_each_compatible_node(node, parent, compatible) {
1246 * Note that this is implemented as a macro and @node is used as
1247 * iterator in the loop.
1249 #define ofnode_for_each_compatible_node(node, compat) \
1250 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1251 ofnode_valid(node); \
1252 node = ofnode_by_compatible(node, compat))
1255 * ofnode_get_child_count() - get the child count of a ofnode
1257 * @parent: valid node to get its child count
1258 * Return: the number of subnodes
1260 int ofnode_get_child_count(ofnode parent);
1263 * ofnode_translate_address() - Translate a device-tree address
1265 * Translate an address from the device-tree into a CPU physical address. This
1266 * function walks up the tree and applies the various bus mappings along the
1269 * @node: Device tree node giving the context in which to translate the address
1270 * @in_addr: pointer to the address to translate
1271 * Return: the translated address; OF_BAD_ADDR on error
1273 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
1276 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1278 * Translate a DMA address from the device-tree into a CPU physical address.
1279 * This function walks up the tree and applies the various bus mappings along
1282 * @node: Device tree node giving the context in which to translate the
1284 * @in_addr: pointer to the DMA address to translate
1285 * Return: the translated DMA address; OF_BAD_ADDR on error
1287 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1290 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1292 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1293 * cpu->bus address translations
1295 * @node: Device tree node
1296 * @cpu: Pointer to variable storing the range's cpu address
1297 * @bus: Pointer to variable storing the range's bus address
1298 * @size: Pointer to variable storing the range's size
1299 * Return: translated DMA address or OF_BAD_ADDR on error
1301 int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1305 * ofnode_device_is_compatible() - check if the node is compatible with compat
1307 * This allows to check whether the node is comaptible with the compat.
1309 * @node: Device tree node for which compatible needs to be verified.
1310 * @compat: Compatible string which needs to verified in the given node.
1311 * Return: true if OK, false if the compatible is not found
1313 int ofnode_device_is_compatible(ofnode node, const char *compat);
1316 * ofnode_write_prop() - Set a property of a ofnode
1318 * Note that the value passed to the function is *not* allocated by the
1319 * function itself, but must be allocated by the caller if necessary. However
1320 * it does allocate memory for the property struct and name.
1322 * @node: The node for whose property should be set
1323 * @propname: The name of the property to set
1324 * @value: The new value of the property (must be valid prior to calling
1326 * @len: The length of the new value of the property
1327 * Return: 0 if successful, -ve on error
1329 int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1333 * ofnode_write_string() - Set a string property of a ofnode
1335 * Note that the value passed to the function is *not* allocated by the
1336 * function itself, but must be allocated by the caller if necessary.
1338 * @node: The node for whose string property should be set
1339 * @propname: The name of the string property to set
1340 * @value: The new value of the string property (must be valid prior to
1341 * calling the function)
1342 * Return: 0 if successful, -ve on error
1344 int ofnode_write_string(ofnode node, const char *propname, const char *value);
1347 * ofnode_write_u32() - Set an integer property of an ofnode
1349 * @node: The node for whose string property should be set
1350 * @propname: The name of the string property to set
1351 * @value: The new value of the 32-bit integer property
1352 * Return: 0 if successful, -ve on error
1354 int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1357 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1360 * This function effectively sets the node's "status" property to either "okay"
1361 * or "disable", hence making it available for driver model initialization or
1364 * @node: The node to enable
1365 * @value: Flag that tells the function to either disable or enable the
1367 * Return: 0 if successful, -ve on error
1369 int ofnode_set_enabled(ofnode node, bool value);
1372 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1374 * This function parses PHY handle from the Ethernet controller's ofnode
1375 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1377 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1378 * if the result to that is true, this function should not be called.
1380 * @eth_node: ofnode belonging to the Ethernet controller
1381 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1383 ofnode ofnode_get_phy_node(ofnode eth_node);
1386 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1388 * This function parses the "phy-mode" / "phy-connection-type" property and
1389 * returns the corresponding PHY interface type.
1391 * @mac_node: ofnode containing the property
1392 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1395 phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1397 #if CONFIG_IS_ENABLED(DM)
1399 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1401 * This reads a property from the /config node of the devicetree.
1403 * See doc/config.txt for bindings
1405 * @prop_name: property name to look up
1406 * Return: true, if it exists, false if not
1408 bool ofnode_conf_read_bool(const char *prop_name);
1411 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1413 * This reads a property from the /config node of the devicetree.
1415 * See doc/config.txt for bindings
1417 * @prop_name: property name to look up
1418 * @default_val: default value to return if the property is not found
1419 * Return: integer value, if found, or @default_val if not
1421 int ofnode_conf_read_int(const char *prop_name, int default_val);
1424 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1426 * This reads a property from the /config node of the devicetree.
1428 * See doc/config.txt for bindings
1430 * @prop_name: property name to look up
1431 * Return: string value, if found, or NULL if not
1433 const char *ofnode_conf_read_str(const char *prop_name);
1435 #else /* CONFIG_DM */
1436 static inline bool ofnode_conf_read_bool(const char *prop_name)
1441 static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1446 static inline const char *ofnode_conf_read_str(const char *prop_name)
1451 #endif /* CONFIG_DM */
1454 * of_add_subnode() - add a new subnode to a node
1456 * @parent: parent node to add to
1457 * @name: name of subnode
1458 * @nodep: returns pointer to new subnode (valid if the function returns 0
1460 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1461 * -ve on other error
1463 int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);