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 <dm/fdtaddr.h>
13 #include <dm/ofnode.h>
14 #include <dm/uclass.h>
18 #if CONFIG_IS_ENABLED(OF_LIVE)
19 static inline const struct device_node *dev_np(struct udevice *dev)
21 return ofnode_to_np(dev->node);
24 static inline const struct device_node *dev_np(struct udevice *dev)
31 * dev_ofnode() - get the DT node reference associated with a udevice
33 * @dev: device to check
34 * @return reference of the the device's DT node
36 static inline ofnode dev_ofnode(struct udevice *dev)
41 static inline bool dev_of_valid(struct udevice *dev)
43 return ofnode_valid(dev_ofnode(dev));
46 #ifndef CONFIG_DM_DEV_READ_INLINE
49 * dev_read_u32() - read a 32-bit integer from a device's DT property
51 * @dev: device to read DT property from
52 * @propname: name of the property to read from
53 * @outp: place to put value (if found)
54 * @return 0 if OK, -ve on error
56 int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp);
59 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
61 * @dev: device to read DT property from
62 * @propname: name of the property to read from
63 * @def: default value to return if the property has no value
64 * @return property value, or @def if not found
66 int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
69 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
71 * @dev: device to read DT property from
72 * @propname: name of the property to read from
73 * @outp: place to put value (if found)
74 * @return 0 if OK, -ve on error
76 int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp);
79 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
81 * @dev: device to read DT property from
82 * @propname: name of the property to read from
83 * @def: default value to return if the property has no value
84 * @return property value, or @def if not found
86 int dev_read_s32_default(struct udevice *dev, const char *propname, int def);
89 * dev_read_u32u() - read a 32-bit integer from a device's DT property
91 * This version uses a standard uint type.
93 * @dev: device to read DT property from
94 * @propname: name of the property to read from
95 * @outp: place to put value (if found)
96 * @return 0 if OK, -ve on error
98 int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp);
101 * dev_read_u64() - read a 64-bit integer from a device's DT property
103 * @dev: device to read DT property from
104 * @propname: name of the property to read from
105 * @outp: place to put value (if found)
106 * @return 0 if OK, -ve on error
108 int dev_read_u64(struct udevice *dev, const char *propname, u64 *outp);
111 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
113 * @dev: device to read DT property from
114 * @propname: name of the property to read from
115 * @def: default value to return if the property has no value
116 * @return property value, or @def if not found
118 u64 dev_read_u64_default(struct udevice *dev, const char *propname, u64 def);
121 * dev_read_string() - Read a string from a device's DT property
123 * @dev: device to read DT property from
124 * @propname: name of the property to read
125 * @return string from property value, or NULL if there is no such property
127 const char *dev_read_string(struct udevice *dev, const char *propname);
130 * dev_read_bool() - read a boolean value from a device's DT property
132 * @dev: device to read DT property from
133 * @propname: name of property to read
134 * @return true if property is present (meaning true), false if not present
136 bool dev_read_bool(struct udevice *dev, const char *propname);
139 * dev_read_subnode() - find a named subnode of a device
141 * @dev: device whose DT node contains the subnode
142 * @subnode_name: name of subnode to find
143 * @return reference to subnode (which can be invalid if there is no such
146 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
149 * dev_read_size() - read the size of a property
151 * @dev: device to check
152 * @propname: property to check
153 * @return size of property if present, or -EINVAL if not
155 int dev_read_size(struct udevice *dev, const char *propname);
158 * dev_read_addr_index() - Get the indexed reg property of a device
160 * @dev: Device to read from
161 * @index: the 'reg' property can hold a list of <addr, size> pairs
162 * and @index is used to select which one is required
164 * @return address or FDT_ADDR_T_NONE if not found
166 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
169 * dev_read_addr_size_index() - Get the indexed reg property of a device
171 * @dev: Device to read from
172 * @index: the 'reg' property can hold a list of <addr, size> pairs
173 * and @index is used to select which one is required
174 * @size: place to put size value (on success)
176 * @return address or FDT_ADDR_T_NONE if not found
178 fdt_addr_t dev_read_addr_size_index(struct udevice *dev, int index,
182 * dev_remap_addr_index() - Get the indexed reg property of a device
183 * as a memory-mapped I/O pointer
185 * @dev: Device to read from
186 * @index: the 'reg' property can hold a list of <addr, size> pairs
187 * and @index is used to select which one is required
189 * @return pointer or NULL if not found
191 void *dev_remap_addr_index(struct udevice *dev, int index);
194 * dev_read_addr_name() - Get the reg property of a device, indexed by name
196 * @dev: Device to read from
197 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
198 * 'reg-names' property providing named-based identification. @index
199 * indicates the value to search for in 'reg-names'.
201 * @return address or FDT_ADDR_T_NONE if not found
203 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
206 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
208 * @dev: Device to read from
209 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
210 * 'reg-names' property providing named-based identification. @index
211 * indicates the value to search for in 'reg-names'.
212 * @size: place to put size value (on success)
214 * @return address or FDT_ADDR_T_NONE if not found
216 fdt_addr_t dev_read_addr_size_name(struct udevice *dev, const char *name,
220 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
221 * as a memory-mapped I/O pointer
223 * @dev: Device to read from
224 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
225 * 'reg-names' property providing named-based identification. @index
226 * indicates the value to search for in 'reg-names'.
228 * @return pointer or NULL if not found
230 void *dev_remap_addr_name(struct udevice *dev, const char* name);
233 * dev_read_addr() - Get the reg property of a device
235 * @dev: Device to read from
237 * @return address or FDT_ADDR_T_NONE if not found
239 fdt_addr_t dev_read_addr(struct udevice *dev);
242 * dev_read_addr_ptr() - Get the reg property of a device
245 * @dev: Device to read from
247 * @return pointer or NULL if not found
249 void *dev_read_addr_ptr(struct udevice *dev);
252 * dev_remap_addr() - Get the reg property of a device as a
253 * memory-mapped I/O pointer
255 * @dev: Device to read from
257 * @return pointer or NULL if not found
259 void *dev_remap_addr(struct udevice *dev);
262 * dev_read_addr_size() - get address and size from a device property
264 * This does no address translation. It simply reads an property that contains
265 * an address and a size value, one after the other.
267 * @dev: Device to read from
268 * @propname: property to read
269 * @sizep: place to put size value (on success)
270 * @return address value, or FDT_ADDR_T_NONE on error
272 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
276 * dev_read_name() - get the name of a device's node
278 * @dev: Device to read from
279 * @return name of node
281 const char *dev_read_name(struct udevice *dev);
284 * dev_read_stringlist_search() - find string in a string list and return index
286 * Note that it is possible for this function to succeed on property values
287 * that are not NUL-terminated. That's because the function will stop after
288 * finding the first occurrence of @string. This can for example happen with
289 * small-valued cell properties, such as #address-cells, when searching for
292 * @dev: device to check
293 * @propname: name of the property containing the string list
294 * @string: string to look up in the string list
297 * the index of the string in the list of strings
298 * -ENODATA if the property is not found
299 * -EINVAL on some other error
301 int dev_read_stringlist_search(struct udevice *dev, const char *property,
305 * dev_read_string_index() - obtain an indexed string from a string list
307 * @dev: device to examine
308 * @propname: name of the property containing the string list
309 * @index: index of the string to return
310 * @out: return location for the string
313 * length of string, if found or -ve error value if not found
315 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
319 * dev_read_string_count() - find the number of strings in a string list
321 * @dev: device to examine
322 * @propname: name of the property containing the string list
324 * number of strings in the list, or -ve error value if not found
326 int dev_read_string_count(struct udevice *dev, const char *propname);
328 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
330 * This function is useful to parse lists of phandles and their arguments.
331 * Returns 0 on success and fills out_args, on error returns appropriate
334 * Caller is responsible to call of_node_put() on the returned out_args->np
348 * list = <&phandle1 1 2 &phandle2 3>;
351 * To get a device_node of the `node2' node you may call this:
352 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
354 * @dev: device whose node containing a list
355 * @list_name: property name that contains a list
356 * @cells_name: property name that specifies phandles' arguments count
357 * @cells_count: Cell count to use if @cells_name is NULL
358 * @index: index of a phandle to parse out
359 * @out_args: optional pointer to output arguments structure (will be filled)
360 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
361 * @list_name does not exist, -EINVAL if a phandle was not found,
362 * @cells_name could not be found, the arguments were truncated or there
363 * were too many arguments.
365 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
366 const char *cells_name, int cell_count,
368 struct ofnode_phandle_args *out_args);
371 * dev_count_phandle_with_args() - Return phandle number in a list
373 * This function is usefull to get phandle number contained in a property list.
374 * For example, this allows to allocate the right amount of memory to keep
375 * clock's reference contained into the "clocks" property.
378 * @dev: device whose node containing a list
379 * @list_name: property name that contains a list
380 * @cells_name: property name that specifies phandles' arguments count
381 * @Returns number of phandle found on success, on error returns appropriate
385 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
386 const char *cells_name);
389 * dev_read_addr_cells() - Get the number of address cells for a device's node
391 * This walks back up the tree to find the closest #address-cells property
392 * which controls the given node.
394 * @dev: device to check
395 * @return number of address cells this node uses
397 int dev_read_addr_cells(struct udevice *dev);
400 * dev_read_size_cells() - Get the number of size cells for a device's node
402 * This walks back up the tree to find the closest #size-cells property
403 * which controls the given node.
405 * @dev: device to check
406 * @return number of size cells this node uses
408 int dev_read_size_cells(struct udevice *dev);
411 * dev_read_addr_cells() - Get the address cells property in a node
413 * This function matches fdt_address_cells().
415 * @dev: device to check
416 * @return number of address cells this node uses
418 int dev_read_simple_addr_cells(struct udevice *dev);
421 * dev_read_size_cells() - Get the size cells property in a node
423 * This function matches fdt_size_cells().
425 * @dev: device to check
426 * @return number of size cells this node uses
428 int dev_read_simple_size_cells(struct udevice *dev);
431 * dev_read_phandle() - Get the phandle from a device
433 * @dev: device to check
434 * @return phandle (1 or greater), or 0 if no phandle or other error
436 int dev_read_phandle(struct udevice *dev);
439 * dev_read_prop()- - read a property from a device's node
441 * @dev: device to check
442 * @propname: property to read
443 * @lenp: place to put length on success
444 * @return pointer to property, or NULL if not found
446 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
449 * dev_read_alias_seq() - Get the alias sequence number of a node
451 * This works out whether a node is pointed to by an alias, and if so, the
452 * sequence number of that alias. Aliases are of the form <base><num> where
453 * <num> is the sequence number. For example spi2 would be sequence number 2.
455 * @dev: device to look up
456 * @devnump: set to the sequence number if one is found
457 * @return 0 if a sequence was found, -ve if not
459 int dev_read_alias_seq(struct udevice *dev, int *devnump);
462 * dev_read_u32_array() - Find and read an array of 32 bit integers
464 * Search for a property in a device node and read 32-bit value(s) from
467 * The out_values is modified only if a valid u32 value can be decoded.
469 * @dev: device to look up
470 * @propname: name of the property to read
471 * @out_values: pointer to return value, modified only if return value is 0
472 * @sz: number of array elements to read
473 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
474 * property does not have a value, and -EOVERFLOW if the property data isn't
477 int dev_read_u32_array(struct udevice *dev, const char *propname,
478 u32 *out_values, size_t sz);
481 * dev_read_first_subnode() - find the first subnode of a device's node
483 * @dev: device to look up
484 * @return reference to the first subnode (which can be invalid if the device's
485 * node has no subnodes)
487 ofnode dev_read_first_subnode(struct udevice *dev);
490 * ofnode_next_subnode() - find the next sibling of a subnode
492 * @node: valid reference to previous node (sibling)
493 * @return reference to the next subnode (which can be invalid if the node
494 * has no more siblings)
496 ofnode dev_read_next_subnode(ofnode node);
499 * dev_read_u8_array_ptr() - find an 8-bit array
501 * Look up a device's node property and return a pointer to its contents as a
502 * byte array of given length. The property must have at least enough data
503 * for the array (count bytes). It may have more, but this will be ignored.
504 * The data is not copied.
506 * @dev: device to look up
507 * @propname: name of property to find
508 * @sz: number of array elements
509 * @return pointer to byte array if found, or NULL if the property is not
510 * found or there is not enough data
512 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
516 * dev_read_enabled() - check whether a node is enabled
518 * This looks for a 'status' property. If this exists, then returns 1 if
519 * the status is 'ok' and 0 otherwise. If there is no status property,
520 * it returns 1 on the assumption that anything mentioned should be enabled
523 * @dev: device to examine
524 * @return integer value 0 (not enabled) or 1 (enabled)
526 int dev_read_enabled(struct udevice *dev);
529 * dev_read_resource() - obtain an indexed resource from a device.
531 * @dev: device to examine
532 * @index index of the resource to retrieve (0 = first)
533 * @res returns the resource
534 * @return 0 if ok, negative on error
536 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
539 * dev_read_resource_byname() - obtain a named resource from a device.
541 * @dev: device to examine
542 * @name: name of the resource to retrieve
543 * @res: returns the resource
544 * @return 0 if ok, negative on error
546 int dev_read_resource_byname(struct udevice *dev, const char *name,
547 struct resource *res);
550 * dev_translate_address() - Translate a device-tree address
552 * Translate an address from the device-tree into a CPU physical address. This
553 * function walks up the tree and applies the various bus mappings along the
556 * @dev: device giving the context in which to translate the address
557 * @in_addr: pointer to the address to translate
558 * @return the translated address; OF_BAD_ADDR on error
560 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
563 * dev_translate_dma_address() - Translate a device-tree DMA address
565 * Translate a DMA address from the device-tree into a CPU physical address.
566 * This function walks up the tree and applies the various bus mappings along
569 * @dev: device giving the context in which to translate the DMA address
570 * @in_addr: pointer to the DMA address to translate
571 * @return the translated DMA address; OF_BAD_ADDR on error
573 u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr);
576 * dev_read_alias_highest_id - Get highest alias id for the given stem
577 * @stem: Alias stem to be examined
579 * The function travels the lookup table to get the highest alias id for the
581 * @return alias ID, if found, else -1
583 int dev_read_alias_highest_id(const char *stem);
585 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
587 static inline int dev_read_u32(struct udevice *dev,
588 const char *propname, u32 *outp)
590 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
593 static inline int dev_read_u32_default(struct udevice *dev,
594 const char *propname, int def)
596 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
599 static inline int dev_read_s32(struct udevice *dev,
600 const char *propname, s32 *outp)
602 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
605 static inline int dev_read_s32_default(struct udevice *dev,
606 const char *propname, int def)
608 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
611 static inline int dev_read_u32u(struct udevice *dev,
612 const char *propname, uint *outp)
617 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
625 static inline int dev_read_u64(struct udevice *dev,
626 const char *propname, u64 *outp)
628 return ofnode_read_u64(dev_ofnode(dev), propname, outp);
631 static inline u64 dev_read_u64_default(struct udevice *dev,
632 const char *propname, u64 def)
634 return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
637 static inline const char *dev_read_string(struct udevice *dev,
638 const char *propname)
640 return ofnode_read_string(dev_ofnode(dev), propname);
643 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
645 return ofnode_read_bool(dev_ofnode(dev), propname);
648 static inline ofnode dev_read_subnode(struct udevice *dev,
649 const char *subbnode_name)
651 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
654 static inline int dev_read_size(struct udevice *dev, const char *propname)
656 return ofnode_read_size(dev_ofnode(dev), propname);
659 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
661 return devfdt_get_addr_index(dev, index);
664 static inline fdt_addr_t dev_read_addr_size_index(struct udevice *dev,
668 return devfdt_get_addr_size_index(dev, index, size);
671 static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
674 return devfdt_get_addr_name(dev, name);
677 static inline fdt_addr_t dev_read_addr_size_name(struct udevice *dev,
681 return devfdt_get_addr_size_name(dev, name, size);
684 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
686 return devfdt_get_addr(dev);
689 static inline void *dev_read_addr_ptr(struct udevice *dev)
691 return devfdt_get_addr_ptr(dev);
694 static inline void *dev_remap_addr(struct udevice *dev)
696 return devfdt_remap_addr(dev);
699 static inline void *dev_remap_addr_index(struct udevice *dev, int index)
701 return devfdt_remap_addr_index(dev, index);
704 static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
706 return devfdt_remap_addr_name(dev, name);
709 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
710 const char *propname,
713 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
716 static inline const char *dev_read_name(struct udevice *dev)
718 return ofnode_get_name(dev_ofnode(dev));
721 static inline int dev_read_stringlist_search(struct udevice *dev,
722 const char *propname,
725 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
728 static inline int dev_read_string_index(struct udevice *dev,
729 const char *propname, int index,
732 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
735 static inline int dev_read_string_count(struct udevice *dev,
736 const char *propname)
738 return ofnode_read_string_count(dev_ofnode(dev), propname);
741 static inline int dev_read_phandle_with_args(struct udevice *dev,
742 const char *list_name, const char *cells_name, int cell_count,
743 int index, struct ofnode_phandle_args *out_args)
745 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
746 cells_name, cell_count, index,
750 static inline int dev_count_phandle_with_args(struct udevice *dev,
751 const char *list_name, const char *cells_name)
753 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
757 static inline int dev_read_addr_cells(struct udevice *dev)
759 /* NOTE: this call should walk up the parent stack */
760 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
763 static inline int dev_read_size_cells(struct udevice *dev)
765 /* NOTE: this call should walk up the parent stack */
766 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
769 static inline int dev_read_simple_addr_cells(struct udevice *dev)
771 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
774 static inline int dev_read_simple_size_cells(struct udevice *dev)
776 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
779 static inline int dev_read_phandle(struct udevice *dev)
781 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
784 static inline const void *dev_read_prop(struct udevice *dev,
785 const char *propname, int *lenp)
787 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
790 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
792 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
793 dev_of_offset(dev), devnump);
796 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
797 u32 *out_values, size_t sz)
799 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
802 static inline ofnode dev_read_first_subnode(struct udevice *dev)
804 return ofnode_first_subnode(dev_ofnode(dev));
807 static inline ofnode dev_read_next_subnode(ofnode node)
809 return ofnode_next_subnode(node);
812 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
813 const char *propname, size_t sz)
815 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
818 static inline int dev_read_enabled(struct udevice *dev)
820 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
823 static inline int dev_read_resource(struct udevice *dev, uint index,
824 struct resource *res)
826 return ofnode_read_resource(dev_ofnode(dev), index, res);
829 static inline int dev_read_resource_byname(struct udevice *dev,
831 struct resource *res)
833 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
836 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
838 return ofnode_translate_address(dev_ofnode(dev), in_addr);
841 static inline u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
843 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
846 static inline int dev_read_alias_highest_id(const char *stem)
848 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
851 #endif /* CONFIG_DM_DEV_READ_INLINE */
854 * dev_for_each_subnode() - Helper function to iterate through subnodes
856 * This creates a for() loop which works through the subnodes in a device's
859 * @subnode: ofnode holding the current subnode
860 * @dev: device to use for interation (struct udevice *)
862 #define dev_for_each_subnode(subnode, dev) \
863 for (subnode = dev_read_first_subnode(dev); \
864 ofnode_valid(subnode); \
865 subnode = ofnode_next_subnode(subnode))