1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Function to read values from the device tree node attached to a udevice.
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
12 #include <linux/errno.h>
14 #include <dm/device.h>
15 #include <dm/fdtaddr.h>
16 #include <dm/ofnode.h>
17 #include <dm/uclass.h>
21 #if CONFIG_IS_ENABLED(OF_LIVE)
22 static inline const struct device_node *dev_np(const struct udevice *dev)
24 return ofnode_to_np(dev->node);
27 static inline const struct device_node *dev_np(const struct udevice *dev)
34 * dev_ofnode() - get the DT node reference associated with a udevice
36 * @dev: device to check
37 * @return reference of the the device's DT node
39 static inline ofnode dev_ofnode(const struct udevice *dev)
44 static inline bool dev_of_valid(const struct udevice *dev)
46 return ofnode_valid(dev_ofnode(dev));
49 #ifndef CONFIG_DM_DEV_READ_INLINE
52 * dev_read_u32() - read a 32-bit integer from a device's DT property
54 * @dev: device to read DT property from
55 * @propname: name of the property to read from
56 * @outp: place to put value (if found)
57 * @return 0 if OK, -ve on error
59 int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
62 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
64 * @dev: device to read DT property from
65 * @propname: name of the property to read from
66 * @def: default value to return if the property has no value
67 * @return property value, or @def if not found
69 int dev_read_u32_default(const struct udevice *dev, const char *propname,
73 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
76 * @dev: device to read DT property from
77 * @propname: name of the property to read from
78 * @index: index of the integer to return
79 * @outp: place to put value (if found)
80 * @return 0 if OK, -ve on error
82 int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
86 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
89 * @dev: device to read DT property from
90 * @propname: name of the property to read from
91 * @index: index of the integer to return
92 * @def: default value to return if the property has no value
93 * @return property value, or @def if not found
95 u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
99 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
101 * @dev: device to read DT property from
102 * @propname: name of the property to read from
103 * @outp: place to put value (if found)
104 * @return 0 if OK, -ve on error
106 int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
109 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
111 * @dev: device to read DT property from
112 * @propname: name of the property to read from
113 * @def: default value to return if the property has no value
114 * @return property value, or @def if not found
116 int dev_read_s32_default(const struct udevice *dev, const char *propname,
120 * dev_read_u32u() - read a 32-bit integer from a device's DT property
122 * This version uses a standard uint type.
124 * @dev: device to read DT property from
125 * @propname: name of the property to read from
126 * @outp: place to put value (if found)
127 * @return 0 if OK, -ve on error
129 int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
132 * dev_read_u64() - read a 64-bit integer from a device's DT property
134 * @dev: device to read DT property from
135 * @propname: name of the property to read from
136 * @outp: place to put value (if found)
137 * @return 0 if OK, -ve on error
139 int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
142 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
144 * @dev: device to read DT property from
145 * @propname: name of the property to read from
146 * @def: default value to return if the property has no value
147 * @return property value, or @def if not found
149 u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
153 * dev_read_string() - Read a string from a device's DT property
155 * @dev: device to read DT property from
156 * @propname: name of the property to read
157 * @return string from property value, or NULL if there is no such property
159 const char *dev_read_string(const struct udevice *dev, const char *propname);
162 * dev_read_bool() - read a boolean value from a device's DT property
164 * @dev: device to read DT property from
165 * @propname: name of property to read
166 * @return true if property is present (meaning true), false if not present
168 bool dev_read_bool(const struct udevice *dev, const char *propname);
171 * dev_read_subnode() - find a named subnode of a device
173 * @dev: device whose DT node contains the subnode
174 * @subnode_name: name of subnode to find
175 * @return reference to subnode (which can be invalid if there is no such
178 ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name);
181 * dev_read_size() - read the size of a property
183 * @dev: device to check
184 * @propname: property to check
185 * @return size of property if present, or -EINVAL if not
187 int dev_read_size(const struct udevice *dev, const char *propname);
190 * dev_read_addr_index() - Get the indexed reg property of a device
192 * @dev: Device to read from
193 * @index: the 'reg' property can hold a list of <addr, size> pairs
194 * and @index is used to select which one is required
196 * @return address or FDT_ADDR_T_NONE if not found
198 fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
201 * dev_read_addr_size_index() - Get the indexed reg property of a device
203 * @dev: Device to read from
204 * @index: the 'reg' property can hold a list of <addr, size> pairs
205 * and @index is used to select which one is required
206 * @size: place to put size value (on success)
208 * @return address or FDT_ADDR_T_NONE if not found
210 fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
214 * dev_remap_addr_index() - Get the indexed reg property of a device
215 * as a memory-mapped I/O pointer
217 * @dev: Device to read from
218 * @index: the 'reg' property can hold a list of <addr, size> pairs
219 * and @index is used to select which one is required
221 * @return pointer or NULL if not found
223 void *dev_remap_addr_index(const struct udevice *dev, int index);
226 * dev_read_addr_name() - Get the reg property of a device, indexed by name
228 * @dev: Device to read from
229 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
230 * 'reg-names' property providing named-based identification. @index
231 * indicates the value to search for in 'reg-names'.
233 * @return address or FDT_ADDR_T_NONE if not found
235 fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
238 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
240 * @dev: Device to read from
241 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
242 * 'reg-names' property providing named-based identification. @index
243 * indicates the value to search for in 'reg-names'.
244 * @size: place to put size value (on success)
246 * @return address or FDT_ADDR_T_NONE if not found
248 fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
252 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
253 * as a memory-mapped I/O pointer
255 * @dev: Device to read from
256 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
257 * 'reg-names' property providing named-based identification. @index
258 * indicates the value to search for in 'reg-names'.
260 * @return pointer or NULL if not found
262 void *dev_remap_addr_name(const struct udevice *dev, const char *name);
265 * dev_read_addr() - Get the reg property of a device
267 * @dev: Device to read from
269 * @return address or FDT_ADDR_T_NONE if not found
271 fdt_addr_t dev_read_addr(const struct udevice *dev);
274 * dev_read_addr_ptr() - Get the reg property of a device
277 * @dev: Device to read from
279 * @return pointer or NULL if not found
281 void *dev_read_addr_ptr(const struct udevice *dev);
284 * dev_read_addr_pci() - Read an address and handle PCI address translation
286 * At present U-Boot does not have address translation logic for PCI in the
287 * livetree implementation (of_addr.c). This special function supports this for
288 * the flat tree implementation.
290 * This function should be removed (and code should use dev_read() instead)
293 * 1. PCI address translation is added; and either
294 * 2. everything uses livetree where PCI translation is used (which is feasible
295 * in SPL and U-Boot proper) or PCI address translation is added to
296 * fdtdec_get_addr() and friends.
298 * @dev: Device to read from
299 * @return address or FDT_ADDR_T_NONE if not found
301 fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
304 * dev_remap_addr() - Get the reg property of a device as a
305 * memory-mapped I/O pointer
307 * @dev: Device to read from
309 * @return pointer or NULL if not found
311 void *dev_remap_addr(const struct udevice *dev);
314 * dev_read_addr_size() - get address and size from a device property
316 * This does no address translation. It simply reads an property that contains
317 * an address and a size value, one after the other.
319 * @dev: Device to read from
320 * @propname: property to read
321 * @sizep: place to put size value (on success)
322 * @return address value, or FDT_ADDR_T_NONE on error
324 fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
328 * dev_read_name() - get the name of a device's node
330 * @dev: Device to read from
331 * @return name of node
333 const char *dev_read_name(const struct udevice *dev);
336 * dev_read_stringlist_search() - find string in a string list and return index
338 * Note that it is possible for this function to succeed on property values
339 * that are not NUL-terminated. That's because the function will stop after
340 * finding the first occurrence of @string. This can for example happen with
341 * small-valued cell properties, such as #address-cells, when searching for
344 * @dev: device to check
345 * @propname: name of the property containing the string list
346 * @string: string to look up in the string list
349 * the index of the string in the list of strings
350 * -ENODATA if the property is not found
351 * -EINVAL on some other error
353 int dev_read_stringlist_search(const struct udevice *dev, const char *property,
357 * dev_read_string_index() - obtain an indexed string from a string list
359 * @dev: device to examine
360 * @propname: name of the property containing the string list
361 * @index: index of the string to return
362 * @out: return location for the string
365 * length of string, if found or -ve error value if not found
367 int dev_read_string_index(const struct udevice *dev, const char *propname,
368 int index, const char **outp);
371 * dev_read_string_count() - find the number of strings in a string list
373 * @dev: device to examine
374 * @propname: name of the property containing the string list
376 * number of strings in the list, or -ve error value if not found
378 int dev_read_string_count(const struct udevice *dev, const char *propname);
380 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
382 * This function is useful to parse lists of phandles and their arguments.
383 * Returns 0 on success and fills out_args, on error returns appropriate
386 * Caller is responsible to call of_node_put() on the returned out_args->np
400 * list = <&phandle1 1 2 &phandle2 3>;
403 * To get a device_node of the `node2' node you may call this:
404 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
406 * @dev: device whose node containing a list
407 * @list_name: property name that contains a list
408 * @cells_name: property name that specifies phandles' arguments count
409 * @cells_count: Cell count to use if @cells_name is NULL
410 * @index: index of a phandle to parse out
411 * @out_args: optional pointer to output arguments structure (will be filled)
412 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
413 * @list_name does not exist, -EINVAL if a phandle was not found,
414 * @cells_name could not be found, the arguments were truncated or there
415 * were too many arguments.
417 int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
418 const char *cells_name, int cell_count,
419 int index, struct ofnode_phandle_args *out_args);
422 * dev_count_phandle_with_args() - Return phandle number in a list
424 * This function is usefull to get phandle number contained in a property list.
425 * For example, this allows to allocate the right amount of memory to keep
426 * clock's reference contained into the "clocks" property.
429 * @dev: device whose node containing a list
430 * @list_name: property name that contains a list
431 * @cells_name: property name that specifies phandles' arguments count
432 * @cells_count: Cell count to use if @cells_name is NULL
433 * @Returns number of phandle found on success, on error returns appropriate
437 int dev_count_phandle_with_args(const struct udevice *dev,
438 const char *list_name, const char *cells_name,
442 * dev_read_addr_cells() - Get the number of address cells for a device's node
444 * This walks back up the tree to find the closest #address-cells property
445 * which controls the given node.
447 * @dev: device to check
448 * @return number of address cells this node uses
450 int dev_read_addr_cells(const struct udevice *dev);
453 * dev_read_size_cells() - Get the number of size cells for a device's node
455 * This walks back up the tree to find the closest #size-cells property
456 * which controls the given node.
458 * @dev: device to check
459 * @return number of size cells this node uses
461 int dev_read_size_cells(const struct udevice *dev);
464 * dev_read_addr_cells() - Get the address cells property in a node
466 * This function matches fdt_address_cells().
468 * @dev: device to check
469 * @return number of address cells this node uses
471 int dev_read_simple_addr_cells(const struct udevice *dev);
474 * dev_read_size_cells() - Get the size cells property in a node
476 * This function matches fdt_size_cells().
478 * @dev: device to check
479 * @return number of size cells this node uses
481 int dev_read_simple_size_cells(const struct udevice *dev);
484 * dev_read_phandle() - Get the phandle from a device
486 * @dev: device to check
487 * @return phandle (1 or greater), or 0 if no phandle or other error
489 int dev_read_phandle(const struct udevice *dev);
492 * dev_read_prop()- - read a property from a device's node
494 * @dev: device to check
495 * @propname: property to read
496 * @lenp: place to put length on success
497 * @return pointer to property, or NULL if not found
499 const void *dev_read_prop(const struct udevice *dev, const char *propname,
503 * dev_read_first_prop()- get the reference of the first property
505 * Get reference to the first property of the node, it is used to iterate
506 * and read all the property with dev_read_prop_by_prop().
508 * @dev: device to check
509 * @prop: place to put argument reference
510 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
512 int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
515 * ofnode_get_next_property() - get the reference of the next property
517 * Get reference to the next property of the node, it is used to iterate
518 * and read all the property with dev_read_prop_by_prop().
520 * @prop: reference of current argument and place to put reference of next one
521 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
523 int dev_read_next_prop(struct ofprop *prop);
526 * dev_read_prop_by_prop() - get a pointer to the value of a property
528 * Get value for the property identified by the provided reference.
530 * @prop: reference on property
531 * @propname: If non-NULL, place to property name on success,
532 * @lenp: If non-NULL, place to put length on success
533 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
535 const void *dev_read_prop_by_prop(struct ofprop *prop,
536 const char **propname, int *lenp);
539 * dev_read_alias_seq() - Get the alias sequence number of a node
541 * This works out whether a node is pointed to by an alias, and if so, the
542 * sequence number of that alias. Aliases are of the form <base><num> where
543 * <num> is the sequence number. For example spi2 would be sequence number 2.
545 * @dev: device to look up
546 * @devnump: set to the sequence number if one is found
547 * @return 0 if a sequence was found, -ve if not
549 int dev_read_alias_seq(const struct udevice *dev, int *devnump);
552 * dev_read_u32_array() - Find and read an array of 32 bit integers
554 * Search for a property in a device node and read 32-bit value(s) from
557 * The out_values is modified only if a valid u32 value can be decoded.
559 * @dev: device to look up
560 * @propname: name of the property to read
561 * @out_values: pointer to return value, modified only if return value is 0
562 * @sz: number of array elements to read
563 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
564 * property does not have a value, and -EOVERFLOW if the property data isn't
567 int dev_read_u32_array(const struct udevice *dev, const char *propname,
568 u32 *out_values, size_t sz);
571 * dev_read_first_subnode() - find the first subnode of a device's node
573 * @dev: device to look up
574 * @return reference to the first subnode (which can be invalid if the device's
575 * node has no subnodes)
577 ofnode dev_read_first_subnode(const struct udevice *dev);
580 * ofnode_next_subnode() - find the next sibling of a subnode
582 * @node: valid reference to previous node (sibling)
583 * @return reference to the next subnode (which can be invalid if the node
584 * has no more siblings)
586 ofnode dev_read_next_subnode(ofnode node);
589 * dev_read_u8_array_ptr() - find an 8-bit array
591 * Look up a device's node property and return a pointer to its contents as a
592 * byte array of given length. The property must have at least enough data
593 * for the array (count bytes). It may have more, but this will be ignored.
594 * The data is not copied.
596 * @dev: device to look up
597 * @propname: name of property to find
598 * @sz: number of array elements
599 * @return pointer to byte array if found, or NULL if the property is not
600 * found or there is not enough data
602 const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
603 const char *propname, size_t sz);
606 * dev_read_enabled() - check whether a node is enabled
608 * This looks for a 'status' property. If this exists, then returns 1 if
609 * the status is 'ok' and 0 otherwise. If there is no status property,
610 * it returns 1 on the assumption that anything mentioned should be enabled
613 * @dev: device to examine
614 * @return integer value 0 (not enabled) or 1 (enabled)
616 int dev_read_enabled(const struct udevice *dev);
619 * dev_read_resource() - obtain an indexed resource from a device.
621 * @dev: device to examine
622 * @index index of the resource to retrieve (0 = first)
623 * @res returns the resource
624 * @return 0 if ok, negative on error
626 int dev_read_resource(const struct udevice *dev, uint index,
627 struct resource *res);
630 * dev_read_resource_byname() - obtain a named resource from a device.
632 * @dev: device to examine
633 * @name: name of the resource to retrieve
634 * @res: returns the resource
635 * @return 0 if ok, negative on error
637 int dev_read_resource_byname(const struct udevice *dev, const char *name,
638 struct resource *res);
641 * dev_translate_address() - Translate a device-tree address
643 * Translate an address from the device-tree into a CPU physical address. This
644 * function walks up the tree and applies the various bus mappings along the
647 * @dev: device giving the context in which to translate the address
648 * @in_addr: pointer to the address to translate
649 * @return the translated address; OF_BAD_ADDR on error
651 u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
654 * dev_translate_dma_address() - Translate a device-tree DMA address
656 * Translate a DMA address from the device-tree into a CPU physical address.
657 * This function walks up the tree and applies the various bus mappings along
660 * @dev: device giving the context in which to translate the DMA address
661 * @in_addr: pointer to the DMA address to translate
662 * @return the translated DMA address; OF_BAD_ADDR on error
664 u64 dev_translate_dma_address(const struct udevice *dev,
665 const fdt32_t *in_addr);
668 * dev_read_alias_highest_id - Get highest alias id for the given stem
669 * @stem: Alias stem to be examined
671 * The function travels the lookup table to get the highest alias id for the
673 * @return alias ID, if found, else -1
675 int dev_read_alias_highest_id(const char *stem);
678 * dev_get_child_count() - get the child count of a device
680 * @dev: device to use for interation (struct udevice *)
681 * @return the count of child subnode
683 int dev_get_child_count(const struct udevice *dev);
686 * dev_read_pci_bus_range - Read PCI bus-range resource
688 * Look at the bus range property of a device node and return the pci bus
689 * range for this node.
691 * @dev: device to examine
692 * @res returns the resource
693 * @return 0 if ok, negative on error
695 int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
697 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
699 static inline int dev_read_u32(const struct udevice *dev,
700 const char *propname, u32 *outp)
702 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
705 static inline int dev_read_u32_default(const struct udevice *dev,
706 const char *propname, int def)
708 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
711 static inline int dev_read_u32_index(struct udevice *dev,
712 const char *propname, int index, u32 *outp)
714 return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
717 static inline u32 dev_read_u32_index_default(struct udevice *dev,
718 const char *propname, int index,
721 return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
725 static inline int dev_read_s32(const struct udevice *dev,
726 const char *propname, s32 *outp)
728 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
731 static inline int dev_read_s32_default(const struct udevice *dev,
732 const char *propname, int def)
734 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
737 static inline int dev_read_u32u(const struct udevice *dev,
738 const char *propname, uint *outp)
743 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
751 static inline int dev_read_u64(const struct udevice *dev,
752 const char *propname, u64 *outp)
754 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
757 static inline u64 dev_read_u64_default(const struct udevice *dev,
758 const char *propname, u64 def)
760 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
763 static inline const char *dev_read_string(const struct udevice *dev,
764 const char *propname)
766 return ofnode_read_string(dev_ofnode(dev), propname);
769 static inline bool dev_read_bool(const struct udevice *dev,
770 const char *propname)
772 return ofnode_read_bool(dev_ofnode(dev), propname);
775 static inline ofnode dev_read_subnode(const struct udevice *dev,
776 const char *subbnode_name)
778 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
781 static inline int dev_read_size(const struct udevice *dev, const char *propname)
783 return ofnode_read_size(dev_ofnode(dev), propname);
786 static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
789 return devfdt_get_addr_index(dev, index);
792 static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
796 return devfdt_get_addr_size_index(dev, index, size);
799 static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
802 return devfdt_get_addr_name(dev, name);
805 static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
809 return devfdt_get_addr_size_name(dev, name, size);
812 static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
814 return devfdt_get_addr(dev);
817 static inline void *dev_read_addr_ptr(const struct udevice *dev)
819 return devfdt_get_addr_ptr(dev);
822 static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
824 return devfdt_get_addr_pci(dev);
827 static inline void *dev_remap_addr(const struct udevice *dev)
829 return devfdt_remap_addr(dev);
832 static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
834 return devfdt_remap_addr_index(dev, index);
837 static inline void *dev_remap_addr_name(const struct udevice *dev,
840 return devfdt_remap_addr_name(dev, name);
843 static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
844 const char *propname,
847 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
850 static inline const char *dev_read_name(const struct udevice *dev)
852 return ofnode_get_name(dev_ofnode(dev));
855 static inline int dev_read_stringlist_search(const struct udevice *dev,
856 const char *propname,
859 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
862 static inline int dev_read_string_index(const struct udevice *dev,
863 const char *propname, int index,
866 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
869 static inline int dev_read_string_count(const struct udevice *dev,
870 const char *propname)
872 return ofnode_read_string_count(dev_ofnode(dev), propname);
875 static inline int dev_read_phandle_with_args(const struct udevice *dev,
876 const char *list_name, const char *cells_name, int cell_count,
877 int index, struct ofnode_phandle_args *out_args)
879 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
880 cells_name, cell_count, index,
884 static inline int dev_count_phandle_with_args(const struct udevice *dev,
885 const char *list_name, const char *cells_name, int cell_count)
887 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
888 cells_name, cell_count);
891 static inline int dev_read_addr_cells(const struct udevice *dev)
893 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
895 return fdt_address_cells(gd->fdt_blob, parent);
898 static inline int dev_read_size_cells(const struct udevice *dev)
900 int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
902 return fdt_size_cells(gd->fdt_blob, parent);
905 static inline int dev_read_simple_addr_cells(const struct udevice *dev)
907 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
910 static inline int dev_read_simple_size_cells(const struct udevice *dev)
912 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
915 static inline int dev_read_phandle(const struct udevice *dev)
917 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
920 static inline const void *dev_read_prop(const struct udevice *dev,
921 const char *propname, int *lenp)
923 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
926 static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
928 return ofnode_get_first_property(dev_ofnode(dev), prop);
931 static inline int dev_read_next_prop(struct ofprop *prop)
933 return ofnode_get_next_property(prop);
936 static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
937 const char **propname,
940 return ofnode_get_property_by_prop(prop, propname, lenp);
943 static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
945 #if CONFIG_IS_ENABLED(OF_CONTROL)
946 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
947 dev_of_offset(dev), devnump);
953 static inline int dev_read_u32_array(const struct udevice *dev,
954 const char *propname, u32 *out_values,
957 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
960 static inline ofnode dev_read_first_subnode(const struct udevice *dev)
962 return ofnode_first_subnode(dev_ofnode(dev));
965 static inline ofnode dev_read_next_subnode(ofnode node)
967 return ofnode_next_subnode(node);
970 static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
971 const char *propname,
974 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
977 static inline int dev_read_enabled(const struct udevice *dev)
979 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
982 static inline int dev_read_resource(const struct udevice *dev, uint index,
983 struct resource *res)
985 return ofnode_read_resource(dev_ofnode(dev), index, res);
988 static inline int dev_read_resource_byname(const struct udevice *dev,
990 struct resource *res)
992 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
995 static inline u64 dev_translate_address(const struct udevice *dev,
996 const fdt32_t *in_addr)
998 return ofnode_translate_address(dev_ofnode(dev), in_addr);
1001 static inline u64 dev_translate_dma_address(const struct udevice *dev,
1002 const fdt32_t *in_addr)
1004 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1007 static inline int dev_read_alias_highest_id(const char *stem)
1009 if (!CONFIG_IS_ENABLED(OF_LIBFDT))
1011 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1014 static inline int dev_get_child_count(const struct udevice *dev)
1016 return ofnode_get_child_count(dev_ofnode(dev));
1019 #endif /* CONFIG_DM_DEV_READ_INLINE */
1022 * dev_for_each_subnode() - Helper function to iterate through subnodes
1024 * This creates a for() loop which works through the subnodes in a device's
1027 * @subnode: ofnode holding the current subnode
1028 * @dev: device to use for interation (struct udevice *)
1030 #define dev_for_each_subnode(subnode, dev) \
1031 for (subnode = dev_read_first_subnode(dev); \
1032 ofnode_valid(subnode); \
1033 subnode = ofnode_next_subnode(subnode))
1036 * dev_for_each_property() - Helper function to iterate through property
1038 * This creates a for() loop which works through the property in a device's
1041 * @prop: struct ofprop holding the current property
1042 * @dev: device to use for interation (struct udevice *)
1044 #define dev_for_each_property(prop, dev) \
1045 for (int ret_prop = dev_read_first_prop(dev, &prop); \
1047 ret_prop = dev_read_next_prop(&prop))