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 * ofnode_to_np() - convert an ofnode to a live DT node pointer
33 * This cannot be called if the reference contains an offset.
35 * @node: Reference containing struct device_node * (possibly invalid)
36 * Return: pointer to device node (can be NULL)
38 static inline struct device_node *ofnode_to_np(ofnode node)
41 if (!of_live_active())
48 * ofnode_to_offset() - convert an ofnode to a flat DT offset
50 * This cannot be called if the reference contains a node pointer.
52 * @node: Reference containing offset (possibly invalid)
53 * Return: DT offset (can be -1)
55 static inline int ofnode_to_offset(ofnode node)
61 return node.of_offset;
65 * ofnode_valid() - check if an ofnode is valid
67 * @node: Reference containing offset (possibly invalid)
68 * Return: true if the reference contains a valid ofnode, false if it is NULL
70 static inline bool ofnode_valid(ofnode node)
73 return node.np != NULL;
75 return node.of_offset >= 0;
79 * offset_to_ofnode() - convert a DT offset to an ofnode
81 * @of_offset: DT offset (either valid, or -1)
82 * Return: reference to the associated DT offset
84 static inline ofnode offset_to_ofnode(int of_offset)
91 node.of_offset = of_offset >= 0 ? of_offset : -1;
97 * np_to_ofnode() - convert a node pointer to an ofnode
99 * @np: Live node pointer (can be NULL)
100 * Return: reference to the associated node pointer
102 static inline ofnode np_to_ofnode(struct device_node *np)
112 * ofnode_is_np() - check if a reference is a node pointer
114 * This function associated that if there is a valid live tree then all
115 * references will use it. This is because using the flat DT when the live tree
116 * is valid is not permitted.
118 * @node: reference to check (possibly invalid)
119 * Return: true if the reference is a live node pointer, false if it is a DT
122 static inline bool ofnode_is_np(ofnode node)
126 * Check our assumption that flat tree offsets are not used when a
127 * live tree is in use.
129 assert(!ofnode_valid(node) ||
130 (of_live_active() ? ofnode_to_np(node)
131 : ofnode_to_np(node)));
133 return of_live_active() && ofnode_valid(node);
137 * ofnode_equal() - check if two references are equal
139 * @ref1: first reference to check (possibly invalid)
140 * @ref2: second reference to check (possibly invalid)
141 * Return: true if equal, else false
143 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
145 /* We only need to compare the contents */
146 return ref1.of_offset == ref2.of_offset;
150 * ofnode_null() - Obtain a null ofnode
152 * This returns an ofnode which points to no node. It works both with the flat
155 static inline ofnode ofnode_null(void)
159 if (of_live_active())
167 static inline ofnode ofnode_root(void)
171 if (of_live_active())
172 node.np = gd_of_root();
180 * ofprop_valid() - check if an ofprop is valid
182 * @prop: Pointer to ofprop to check
183 * Return: true if the reference contains a valid ofprop, false if not
185 static inline bool ofprop_valid(struct ofprop *prop)
187 if (of_live_active())
190 return prop->offset >= 0;
194 * oftree_default() - Returns the default device tree (U-Boot's control FDT)
196 * Returns: reference to the control FDT
198 static inline oftree oftree_default(void)
202 if (of_live_active())
203 tree.np = gd_of_root();
205 tree.fdt = (void *)gd->fdt_blob;
211 * ofnode_name_eq() - Check if the node name is equivalent to a given name
212 * ignoring the unit address
214 * @node: valid node reference that has to be compared
215 * @name: name that has to be compared with the node name
216 * Return: true if matches, false if it doesn't match
218 bool ofnode_name_eq(ofnode node, const char *name);
221 * ofnode_read_u8() - Read a 8-bit integer from a property
223 * @node: valid node reference to read property from
224 * @propname: name of the property to read from
225 * @outp: place to put value (if found)
226 * Return: 0 if OK, -ve on error
228 int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
231 * ofnode_read_u8_default() - Read a 8-bit integer from a property
233 * @node: 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 u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
241 * ofnode_read_u16() - Read a 16-bit integer from a property
243 * @node: valid node reference to read property from
244 * @propname: name of the property to read from
245 * @outp: place to put value (if found)
246 * Return: 0 if OK, -ve on error
248 int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
251 * ofnode_read_u16_default() - Read a 16-bit integer from a property
253 * @node: valid node reference to read property from
254 * @propname: name of the property to read from
255 * @def: default value to return if the property has no value
256 * Return: property value, or @def if not found
258 u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
261 * ofnode_read_u32() - Read a 32-bit integer from a property
263 * @node: valid node reference to read property from
264 * @propname: name of the property to read from
265 * @outp: place to put value (if found)
266 * Return: 0 if OK, -ve on error
268 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
271 * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
273 * @node: valid node reference to read property from
274 * @propname: name of the property to read from
275 * @index: index of the integer to return
276 * @outp: place to put value (if found)
277 * Return: 0 if OK, -ve on error
279 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
283 * ofnode_read_s32() - Read a 32-bit integer from a property
285 * @node: valid node reference to read property from
286 * @propname: name of the property to read from
287 * @outp: place to put value (if found)
288 * Return: 0 if OK, -ve on error
290 static inline int ofnode_read_s32(ofnode node, const char *propname,
293 return ofnode_read_u32(node, propname, (u32 *)outp);
297 * ofnode_read_u32_default() - Read a 32-bit integer from a property
299 * @node: valid node reference to read property from
300 * @propname: name of the property to read from
301 * @def: default value to return if the property has no value
302 * Return: property value, or @def if not found
304 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
307 * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
310 * @node: valid node reference to read property from
311 * @propname: name of the property to read from
312 * @index: index of the integer to return
313 * @def: default value to return if the property has no value
314 * Return: property value, or @def if not found
316 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
320 * ofnode_read_s32_default() - Read a 32-bit integer from a property
322 * @node: valid node reference to read property from
323 * @propname: name of the property to read from
324 * @def: default value to return if the property has no value
325 * Return: property value, or @def if not found
327 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
330 * ofnode_read_u64() - Read a 64-bit integer from a property
332 * @node: valid node reference to read property from
333 * @propname: name of the property to read from
334 * @outp: place to put value (if found)
335 * Return: 0 if OK, -ve on error
337 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
340 * ofnode_read_u64_default() - Read a 64-bit integer from a property
342 * @node: valid node reference to read property from
343 * @propname: name of the property to read from
344 * @def: default value to return if the property has no value
345 * Return: property value, or @def if not found
347 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
350 * ofnode_read_prop() - Read a property from a node
352 * @node: valid node reference to read property from
353 * @propname: name of the property to read
354 * @sizep: if non-NULL, returns the size of the property, or an error code
356 * Return: property value, or NULL if there is no such property
358 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
361 * ofnode_read_string() - Read a string from a property
363 * @node: valid node reference to read property from
364 * @propname: name of the property to read
365 * Return: string from property value, or NULL if there is no such property
367 const char *ofnode_read_string(ofnode node, const char *propname);
370 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
372 * @node: valid node reference to read property from
373 * @propname: name of the property to read
374 * @out_values: pointer to return value, modified only if return value is 0
375 * @sz: number of array elements to read
376 * Return: 0 on success, -EINVAL if the property does not exist,
377 * -ENODATA if property does not have a value, and -EOVERFLOW if the
378 * property data isn't large enough
380 * Search for a property in a device node and read 32-bit value(s) from
383 * The out_values is modified only if a valid u32 value can be decoded.
385 int ofnode_read_u32_array(ofnode node, const char *propname,
386 u32 *out_values, size_t sz);
389 * ofnode_read_bool() - read a boolean value from a property
391 * @node: valid node reference to read property from
392 * @propname: name of property to read
393 * Return: true if property is present (meaning true), false if not present
395 bool ofnode_read_bool(ofnode node, const char *propname);
398 * ofnode_find_subnode() - find a named subnode of a parent node
400 * @node: valid reference to parent node
401 * @subnode_name: name of subnode to find
402 * Return: reference to subnode (which can be invalid if there is no such
405 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
407 #if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
408 #include <asm/global_data.h>
410 static inline bool ofnode_is_enabled(ofnode node)
412 if (ofnode_is_np(node)) {
413 return of_device_is_available(ofnode_to_np(node));
415 return fdtdec_get_is_enabled(gd->fdt_blob,
416 ofnode_to_offset(node));
420 static inline ofnode ofnode_first_subnode(ofnode node)
422 assert(ofnode_valid(node));
423 if (ofnode_is_np(node))
424 return np_to_ofnode(node.np->child);
426 return offset_to_ofnode(
427 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
430 static inline ofnode ofnode_next_subnode(ofnode node)
432 assert(ofnode_valid(node));
433 if (ofnode_is_np(node))
434 return np_to_ofnode(node.np->sibling);
436 return offset_to_ofnode(
437 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
441 * ofnode_is_enabled() - Checks whether a node is enabled.
442 * This looks for a 'status' property. If this exists, then returns true if
443 * the status is 'okay' and false otherwise. If there is no status property,
444 * it returns true on the assumption that anything mentioned should be enabled
447 * @node: node to examine
448 * Return: false (not enabled) or true (enabled)
450 bool ofnode_is_enabled(ofnode node);
453 * ofnode_first_subnode() - find the first subnode of a parent node
455 * @node: valid reference to a valid parent node
456 * Return: reference to the first subnode (which can be invalid if the parent
457 * node has no subnodes)
459 ofnode ofnode_first_subnode(ofnode node);
462 * ofnode_next_subnode() - find the next sibling of a subnode
464 * @node: valid reference to previous node (sibling)
465 * Return: reference to the next subnode (which can be invalid if the node
466 * has no more siblings)
468 ofnode ofnode_next_subnode(ofnode node);
469 #endif /* DM_INLINE_OFNODE */
472 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
474 * @node: valid node to look up
475 * Return: ofnode reference of the parent node
477 ofnode ofnode_get_parent(ofnode node);
480 * ofnode_get_name() - get the name of a node
482 * @node: valid node to look up
483 * Return: name of node (for the root node this is "")
485 const char *ofnode_get_name(ofnode node);
488 * ofnode_get_path() - get the full path of a node
490 * @node: valid node to look up
491 * @buf: buffer to write the node path into
492 * @buflen: buffer size
493 * Return: 0 if OK, -ve on error
495 int ofnode_get_path(ofnode node, char *buf, int buflen);
498 * ofnode_get_by_phandle() - get ofnode from phandle
500 * This uses the default (control) device tree
502 * @phandle: phandle to look up
503 * Return: ofnode reference to the phandle
505 ofnode ofnode_get_by_phandle(uint phandle);
508 * ofnode_read_size() - read the size of a property
510 * @node: node to check
511 * @propname: property to check
512 * Return: size of property if present, or -EINVAL if not
514 int ofnode_read_size(ofnode node, const char *propname);
517 * ofnode_get_addr_size_index() - get an address/size from a node
520 * This reads the register address/size from a node based on index
522 * @node: node to read from
523 * @index: Index of address to read (0 for first)
524 * @size: Pointer to size of the address
525 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
527 phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
531 * ofnode_get_addr_size_index_notrans() - get an address/size from a node
532 * based on index, without address
535 * This reads the register address/size from a node based on index.
536 * The resulting address is not translated. Useful for example for on-disk
539 * @node: node to read from
540 * @index: Index of address to read (0 for first)
541 * @size: Pointer to size of the address
542 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
544 phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
548 * ofnode_get_addr_index() - get an address from a node
550 * This reads the register address from a node
552 * @node: node to read from
553 * @index: Index of address to read (0 for first)
554 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
556 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
559 * ofnode_get_addr() - get an address from a node
561 * This reads the register address from a node
563 * @node: node to read from
564 * Return: address, or FDT_ADDR_T_NONE if not present or invalid
566 phys_addr_t ofnode_get_addr(ofnode node);
569 * ofnode_get_size() - get size from a node
571 * This reads the register size from a node
573 * @node: node to read from
574 * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
576 fdt_size_t ofnode_get_size(ofnode node);
579 * ofnode_stringlist_search() - find a string in a string list and return index
581 * Note that it is possible for this function to succeed on property values
582 * that are not NUL-terminated. That's because the function will stop after
583 * finding the first occurrence of @string. This can for example happen with
584 * small-valued cell properties, such as #address-cells, when searching for
587 * @node: node to check
588 * @propname: name of the property containing the string list
589 * @string: string to look up in the string list
592 * the index of the string in the list of strings
593 * -ENODATA if the property is not found
594 * -EINVAL on some other error
596 int ofnode_stringlist_search(ofnode node, const char *propname,
600 * ofnode_read_string_index() - obtain an indexed string from a string list
602 * Note that this will successfully extract strings from properties with
603 * non-NUL-terminated values. For example on small-valued cell properties
604 * this function will return the empty string.
606 * If non-NULL, the length of the string (on success) or a negative error-code
607 * (on failure) will be stored in the integer pointer to by lenp.
609 * @node: node to check
610 * @propname: name of the property containing the string list
611 * @index: index of the string to return (cannot be negative)
612 * @outp: return location for the string
615 * 0 if found or -ve error value if not found
617 int ofnode_read_string_index(ofnode node, const char *propname, int index,
621 * ofnode_read_string_count() - find the number of strings in a string list
623 * @node: node to check
624 * @property: name of the property containing the string list
626 * number of strings in the list, or -ve error value if not found
628 int ofnode_read_string_count(ofnode node, const char *property);
631 * ofnode_read_string_list() - read a list of strings
633 * This produces a list of string pointers with each one pointing to a string
634 * in the string list. If the property does not exist, it returns {NULL}.
636 * The data is allocated and the caller is reponsible for freeing the return
637 * value (the list of string pointers). The strings themselves may not be
638 * changed as they point directly into the devicetree property.
640 * @node: node to check
641 * @property: name of the property containing the string list
642 * @listp: returns an allocated, NULL-terminated list of strings if the return
643 * value is > 0, else is set to NULL
645 * number of strings in list, 0 if none, -ENOMEM if out of memory,
646 * -EINVAL if no such property, -EENODATA if property is empty
648 int ofnode_read_string_list(ofnode node, const char *property,
649 const char ***listp);
652 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
654 * This function is useful to parse lists of phandles and their arguments.
655 * Returns 0 on success and fills out_args, on error returns appropriate
658 * Caller is responsible to call of_node_put() on the returned out_args->np
672 * list = <&phandle1 1 2 &phandle2 3>;
675 * To get a device_node of the `node2' node you may call this:
676 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
678 * @node: device tree node containing a list
679 * @list_name: property name that contains a list
680 * @cells_name: property name that specifies phandles' arguments count
681 * @cell_count: Cell count to use if @cells_name is NULL
682 * @index: index of a phandle to parse out
683 * @out_args: optional pointer to output arguments structure (will be filled)
685 * 0 on success (with @out_args filled out if not NULL), -ENOENT if
686 * @list_name does not exist, -EINVAL if a phandle was not found,
687 * @cells_name could not be found, the arguments were truncated or there
688 * were too many arguments.
690 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
691 const char *cells_name, int cell_count,
693 struct ofnode_phandle_args *out_args);
696 * ofnode_count_phandle_with_args() - Count number of phandle in a list
698 * This function is useful to count phandles into a list.
699 * Returns number of phandle on success, on error returns appropriate
702 * @node: device tree node containing a list
703 * @list_name: property name that contains a list
704 * @cells_name: property name that specifies phandles' arguments count
705 * @cell_count: Cell count to use if @cells_name is NULL
707 * number of phandle on success, -ENOENT if @list_name does not exist,
708 * -EINVAL if a phandle was not found, @cells_name could not be found.
710 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
711 const char *cells_name, int cell_count);
714 * ofnode_path() - find a node by full path
716 * This uses the control FDT.
718 * @path: Full path to node, e.g. "/bus/spi@1"
719 * Return: reference to the node found. Use ofnode_valid() to check if it exists
721 ofnode ofnode_path(const char *path);
724 * ofnode_path_root() - find a node by full path from a root node
726 * @tree: Device tree to use
727 * @path: Full path to node, e.g. "/bus/spi@1"
728 * Return: reference to the node found. Use ofnode_valid() to check if it exists
730 ofnode ofnode_path_root(oftree tree, const char *path);
733 * ofnode_read_chosen_prop() - get the value of a chosen property
735 * This looks for a property within the /chosen node and returns its value
737 * @propname: Property name to look for
738 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
740 * Return: property value if found, else NULL
742 const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
745 * ofnode_read_chosen_string() - get the string value of a chosen property
747 * This looks for a property within the /chosen node and returns its value,
748 * checking that it is a valid nul-terminated string
750 * @propname: Property name to look for
751 * Return: string value if found, else NULL
753 const char *ofnode_read_chosen_string(const char *propname);
756 * ofnode_get_chosen_node() - get a referenced node from the chosen node
758 * This looks up a named property in the chosen node and uses that as a path to
761 * @propname: Property name to look for
762 * Return: the referenced node if present, else ofnode_null()
764 ofnode ofnode_get_chosen_node(const char *propname);
767 * ofnode_read_aliases_prop() - get the value of a aliases property
769 * This looks for a property within the /aliases node and returns its value
771 * @propname: Property name to look for
772 * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
774 * Return: property value if found, else NULL
776 const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
779 * ofnode_get_aliases_node() - get a referenced node from the aliases node
781 * This looks up a named property in the aliases node and uses that as a path to
784 * @propname: Property name to look for
785 * Return: the referenced node if present, else ofnode_null()
787 ofnode ofnode_get_aliases_node(const char *propname);
789 struct display_timing;
791 * ofnode_decode_display_timing() - decode display timings
793 * Decode display timings from the supplied 'display-timings' node.
794 * See doc/device-tree-bindings/video/display-timing.txt for binding
797 * @node: 'display-timing' node containing the timing subnodes
798 * @index: Index number to read (0=first timing subnode)
799 * @config: Place to put timings
800 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
802 int ofnode_decode_display_timing(ofnode node, int index,
803 struct display_timing *config);
806 * ofnode_get_property() - get a pointer to the value of a node property
808 * @node: node to read
809 * @propname: property to read
810 * @lenp: place to put length on success
811 * Return: pointer to property, or NULL if not found
813 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
816 * ofnode_first_property()- get the reference of the first property
818 * Get reference to the first property of the node, it is used to iterate
819 * and read all the property with ofprop_get_property().
821 * @node: node to read
822 * @prop: place to put argument reference
823 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
825 int ofnode_first_property(ofnode node, struct ofprop *prop);
828 * ofnode_next_property() - get the reference of the next property
830 * Get reference to the next property of the node, it is used to iterate
831 * and read all the property with ofprop_get_property().
833 * @prop: reference of current argument and place to put reference of next one
834 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
836 int ofnode_next_property(struct ofprop *prop);
839 * ofnode_for_each_prop() - iterate over all properties of a node
841 * @prop: struct ofprop
842 * @node: node (lvalue, ofnode)
844 * This is a wrapper around a for loop and is used like this::
847 * struct ofprop prop;
849 * ofnode_for_each_prop(prop, node) {
853 * Note that this is implemented as a macro and @prop is used as
854 * iterator in the loop. The parent variable can be a constant or even a
857 #define ofnode_for_each_prop(prop, node) \
858 for (ofnode_first_property(node, &prop); \
859 ofprop_valid(&prop); \
860 ofnode_next_property(&prop))
863 * ofprop_get_property() - get a pointer to the value of a property
865 * Get value for the property identified by the provided reference.
867 * @prop: reference on property
868 * @propname: If non-NULL, place to property name on success,
869 * @lenp: If non-NULL, place to put length on success, or error code on failure
870 * Return: pointer to property, or NULL if not found
872 const void *ofprop_get_property(const struct ofprop *prop,
873 const char **propname, int *lenp);
876 * ofnode_get_addr_size() - get address and size from a property
878 * This does no address translation. It simply reads an property that contains
879 * an address and a size value, one after the other.
881 * @node: node to read from
882 * @propname: property to read
883 * @sizep: place to put size value (on success)
884 * Return: address value, or FDT_ADDR_T_NONE on error
886 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
890 * ofnode_read_u8_array_ptr() - find an 8-bit array
892 * Look up a property in a node and return a pointer to its contents as a
893 * byte array of given length. The property must have at least enough data
894 * for the array (count bytes). It may have more, but this will be ignored.
895 * The data is not copied.
897 * @node: node to examine
898 * @propname: name of property to find
899 * @sz: number of array elements
901 * pointer to byte array if found, or NULL if the property is not found or
902 * there is not enough data
904 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
908 * ofnode_read_pci_addr() - look up a PCI address
910 * Look at an address property in a node and return the PCI address which
911 * corresponds to the given type in the form of fdt_pci_addr.
912 * The property must hold one fdt_pci_addr with a lengh.
914 * @node: node to examine
915 * @type: pci address type (FDT_PCI_SPACE_xxx)
916 * @propname: name of property to find
917 * @addr: returns pci address in the form of fdt_pci_addr
919 * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
920 * format of the property was invalid, -ENXIO if the requested
921 * address type was not found
923 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
924 const char *propname, struct fdt_pci_addr *addr);
927 * ofnode_read_pci_vendev() - look up PCI vendor and device id
929 * Look at the compatible property of a device node that represents a PCI
930 * device and extract pci vendor id and device id from it.
932 * @node: node to examine
933 * @vendor: vendor id of the pci device
934 * @device: device id of the pci device
935 * Return: 0 if ok, negative on error
937 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
940 * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
942 * Look at the compatible property of a device node that represents a eth phy
943 * device and extract phy vendor id and device id from it.
945 * @node: node to examine
946 * @vendor: vendor id of the eth phy device
947 * @device: device id of the eth phy device
948 * Return: 0 if ok, negative on error
950 int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
953 * ofnode_read_addr_cells() - Get the number of address cells for a node
955 * This walks back up the tree to find the closest #address-cells property
956 * which controls the given node.
958 * @node: Node to check
959 * Return: number of address cells this node uses
961 int ofnode_read_addr_cells(ofnode node);
964 * ofnode_read_size_cells() - Get the number of size cells for a node
966 * This walks back up the tree to find the closest #size-cells property
967 * which controls the given node.
969 * @node: Node to check
970 * Return: number of size cells this node uses
972 int ofnode_read_size_cells(ofnode node);
975 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
977 * This function matches fdt_address_cells().
979 * @node: Node to check
980 * Return: value of #address-cells property in this node, or 2 if none
982 int ofnode_read_simple_addr_cells(ofnode node);
985 * ofnode_read_simple_size_cells() - Get the size cells property in a node
987 * This function matches fdt_size_cells().
989 * @node: Node to check
990 * Return: value of #size-cells property in this node, or 2 if none
992 int ofnode_read_simple_size_cells(ofnode node);
995 * ofnode_pre_reloc() - check if a node should be bound before relocation
997 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
998 * via special device tree properties.
1000 * Before relocation this function can be used to check if nodes are required
1001 * in either SPL or TPL stages.
1003 * After relocation and jumping into the real U-Boot binary it is possible to
1004 * determine if a node was bound in one of SPL/TPL stages.
1006 * There are 4 settings currently in use
1007 * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
1008 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
1009 * Existing platforms only use it to indicate nodes needed in
1010 * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
1011 * - u-boot,dm-spl: SPL and U-Boot pre-relocation
1012 * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
1014 * @node: node to check
1015 * Return: true if node is needed in SPL/TL, false otherwise
1017 bool ofnode_pre_reloc(ofnode node);
1020 * ofnode_read_resource() - Read a resource from a node
1022 * Read resource information from a node at the given index
1024 * @node: Node to read from
1025 * @index: Index of resource to read (0 = first)
1026 * @res: Returns resource that was read, on success
1027 * Return: 0 if OK, -ve on error
1029 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
1032 * ofnode_read_resource_byname() - Read a resource from a node by name
1034 * Read resource information from a node matching the given name. This uses a
1035 * 'reg-names' string list property with the names matching the associated
1036 * 'reg' property list.
1038 * @node: Node to read from
1039 * @name: Name of resource to read
1040 * @res: Returns resource that was read, on success
1041 * Return: 0 if OK, -ve on error
1043 int ofnode_read_resource_byname(ofnode node, const char *name,
1044 struct resource *res);
1047 * ofnode_by_compatible() - Find the next compatible node
1049 * Find the next node after @from that is compatible with @compat
1051 * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1052 * @compat: Compatible string to match
1053 * Return: ofnode found, or ofnode_null() if none
1055 ofnode ofnode_by_compatible(ofnode from, const char *compat);
1058 * ofnode_by_prop_value() - Find the next node with given property value
1060 * Find the next node after @from that has a @propname with a value
1061 * @propval and a length @proplen.
1063 * @from: ofnode to start from (use ofnode_null() to start at the
1065 * @propname: property name to check
1066 * @propval: property value to search for
1067 * @proplen: length of the value in propval
1068 * Return: ofnode found, or ofnode_null() if none
1070 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1071 const void *propval, int proplen);
1074 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1076 * @node: child node (ofnode, lvalue)
1077 * @parent: parent node (ofnode)
1079 * This is a wrapper around a for loop and is used like so::
1082 * ofnode_for_each_subnode(node, parent) {
1087 * Note that this is implemented as a macro and @node is used as
1088 * iterator in the loop. The parent variable can be a constant or even a
1091 #define ofnode_for_each_subnode(node, parent) \
1092 for (node = ofnode_first_subnode(parent); \
1093 ofnode_valid(node); \
1094 node = ofnode_next_subnode(node))
1097 * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1100 * @node: child node (ofnode, lvalue)
1101 * @compat: compatible string to match
1103 * This is a wrapper around a for loop and is used like so::
1106 * ofnode_for_each_compatible_node(node, parent, compatible) {
1111 * Note that this is implemented as a macro and @node is used as
1112 * iterator in the loop.
1114 #define ofnode_for_each_compatible_node(node, compat) \
1115 for (node = ofnode_by_compatible(ofnode_null(), compat); \
1116 ofnode_valid(node); \
1117 node = ofnode_by_compatible(node, compat))
1120 * ofnode_get_child_count() - get the child count of a ofnode
1122 * @parent: valid node to get its child count
1123 * Return: the number of subnodes
1125 int ofnode_get_child_count(ofnode parent);
1128 * ofnode_translate_address() - Translate a device-tree address
1130 * Translate an address from the device-tree into a CPU physical address. This
1131 * function walks up the tree and applies the various bus mappings along the
1134 * @node: Device tree node giving the context in which to translate the address
1135 * @in_addr: pointer to the address to translate
1136 * Return: the translated address; OF_BAD_ADDR on error
1138 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
1141 * ofnode_translate_dma_address() - Translate a device-tree DMA address
1143 * Translate a DMA address from the device-tree into a CPU physical address.
1144 * This function walks up the tree and applies the various bus mappings along
1147 * @node: Device tree node giving the context in which to translate the
1149 * @in_addr: pointer to the DMA address to translate
1150 * Return: the translated DMA address; OF_BAD_ADDR on error
1152 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1155 * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1157 * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1158 * cpu->bus address translations
1160 * @node: Device tree node
1161 * @cpu: Pointer to variable storing the range's cpu address
1162 * @bus: Pointer to variable storing the range's bus address
1163 * @size: Pointer to variable storing the range's size
1164 * Return: translated DMA address or OF_BAD_ADDR on error
1166 int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1170 * ofnode_device_is_compatible() - check if the node is compatible with compat
1172 * This allows to check whether the node is comaptible with the compat.
1174 * @node: Device tree node for which compatible needs to be verified.
1175 * @compat: Compatible string which needs to verified in the given node.
1176 * Return: true if OK, false if the compatible is not found
1178 int ofnode_device_is_compatible(ofnode node, const char *compat);
1181 * ofnode_write_prop() - Set a property of a ofnode
1183 * Note that the value passed to the function is *not* allocated by the
1184 * function itself, but must be allocated by the caller if necessary. However
1185 * it does allocate memory for the property struct and name.
1187 * @node: The node for whose property should be set
1188 * @propname: The name of the property to set
1189 * @value: The new value of the property (must be valid prior to calling
1191 * @len: The length of the new value of the property
1192 * Return: 0 if successful, -ve on error
1194 int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1198 * ofnode_write_string() - Set a string property of a ofnode
1200 * Note that the value passed to the function is *not* allocated by the
1201 * function itself, but must be allocated by the caller if necessary.
1203 * @node: The node for whose string property should be set
1204 * @propname: The name of the string property to set
1205 * @value: The new value of the string property (must be valid prior to
1206 * calling the function)
1207 * Return: 0 if successful, -ve on error
1209 int ofnode_write_string(ofnode node, const char *propname, const char *value);
1212 * ofnode_write_u32() - Set an integer property of an ofnode
1214 * @node: The node for whose string property should be set
1215 * @propname: The name of the string property to set
1216 * @value: The new value of the 32-bit integer property
1217 * Return: 0 if successful, -ve on error
1219 int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1222 * ofnode_set_enabled() - Enable or disable a device tree node given by its
1225 * This function effectively sets the node's "status" property to either "okay"
1226 * or "disable", hence making it available for driver model initialization or
1229 * @node: The node to enable
1230 * @value: Flag that tells the function to either disable or enable the
1232 * Return: 0 if successful, -ve on error
1234 int ofnode_set_enabled(ofnode node, bool value);
1237 * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1239 * This function parses PHY handle from the Ethernet controller's ofnode
1240 * (trying all possible PHY handle property names), and returns the PHY ofnode.
1242 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1243 * if the result to that is true, this function should not be called.
1245 * @eth_node: ofnode belonging to the Ethernet controller
1246 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1248 ofnode ofnode_get_phy_node(ofnode eth_node);
1251 * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1253 * This function parses the "phy-mode" / "phy-connection-type" property and
1254 * returns the corresponding PHY interface type.
1256 * @mac_node: ofnode containing the property
1257 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1260 phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1262 #if CONFIG_IS_ENABLED(DM)
1264 * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1266 * This reads a property from the /config node of the devicetree.
1268 * See doc/config.txt for bindings
1270 * @prop_name: property name to look up
1271 * Return: true, if it exists, false if not
1273 bool ofnode_conf_read_bool(const char *prop_name);
1276 * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1278 * This reads a property from the /config node of the devicetree.
1280 * See doc/config.txt for bindings
1282 * @prop_name: property name to look up
1283 * @default_val: default value to return if the property is not found
1284 * Return: integer value, if found, or @default_val if not
1286 int ofnode_conf_read_int(const char *prop_name, int default_val);
1289 * ofnode_conf_read_str() - Read a string value from the U-Boot config
1291 * This reads a property from the /config node of the devicetree.
1293 * See doc/config.txt for bindings
1295 * @prop_name: property name to look up
1296 * Return: string value, if found, or NULL if not
1298 const char *ofnode_conf_read_str(const char *prop_name);
1300 #else /* CONFIG_DM */
1301 static inline bool ofnode_conf_read_bool(const char *prop_name)
1306 static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1311 static inline const char *ofnode_conf_read_str(const char *prop_name)
1316 #endif /* CONFIG_DM */
1319 * of_add_subnode() - add a new subnode to a node
1321 * @parent: parent node to add to
1322 * @name: name of subnode
1323 * @nodep: returns pointer to new subnode (valid if the function returns 0
1325 * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1326 * -ve on other error
1328 int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);