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
48 * dev_read_u32() - read a 32-bit integer from a device's DT property
50 * @dev: device to read DT property from
51 * @propname: name of the property to read from
52 * @outp: place to put value (if found)
53 * @return 0 if OK, -ve on error
55 int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp);
58 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
60 * @dev: device to read DT property from
61 * @propname: name of the property to read from
62 * @def: default value to return if the property has no value
63 * @return property value, or @def if not found
65 int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
68 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
70 * @dev: device to read DT property from
71 * @propname: name of the property to read from
72 * @outp: place to put value (if found)
73 * @return 0 if OK, -ve on error
75 int dev_read_s32(struct udevice *dev, const char *propname, s32 *outp);
78 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
80 * @dev: device to read DT property from
81 * @propname: name of the property to read from
82 * @def: default value to return if the property has no value
83 * @return property value, or @def if not found
85 int dev_read_s32_default(struct udevice *dev, const char *propname, int def);
88 * dev_read_u32u() - read a 32-bit integer from a device's DT property
90 * This version uses a standard uint type.
92 * @dev: device to read DT property from
93 * @propname: name of the property to read from
94 * @outp: place to put value (if found)
95 * @return 0 if OK, -ve on error
97 int dev_read_u32u(struct udevice *dev, const char *propname, uint *outp);
100 * dev_read_string() - Read a string from a device's DT property
102 * @dev: device to read DT property from
103 * @propname: name of the property to read
104 * @return string from property value, or NULL if there is no such property
106 const char *dev_read_string(struct udevice *dev, const char *propname);
109 * dev_read_bool() - read a boolean value from a device's DT property
111 * @dev: device to read DT property from
112 * @propname: name of property to read
113 * @return true if property is present (meaning true), false if not present
115 bool dev_read_bool(struct udevice *dev, const char *propname);
118 * dev_read_subnode() - find a named subnode of a device
120 * @dev: device whose DT node contains the subnode
121 * @subnode_name: name of subnode to find
122 * @return reference to subnode (which can be invalid if there is no such
125 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
128 * dev_read_size() - read the size of a property
130 * @dev: device to check
131 * @propname: property to check
132 * @return size of property if present, or -EINVAL if not
134 int dev_read_size(struct udevice *dev, const char *propname);
137 * dev_read_addr_index() - Get the indexed reg property of a device
139 * @dev: Device to read from
140 * @index: the 'reg' property can hold a list of <addr, size> pairs
141 * and @index is used to select which one is required
143 * @return address or FDT_ADDR_T_NONE if not found
145 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
148 * dev_remap_addr_index() - Get the indexed reg property of a device
149 * as a memory-mapped I/O pointer
151 * @dev: Device to read from
152 * @index: the 'reg' property can hold a list of <addr, size> pairs
153 * and @index is used to select which one is required
155 * @return pointer or NULL if not found
157 void *dev_remap_addr_index(struct udevice *dev, int index);
160 * dev_read_addr_name() - Get the reg property of a device, indexed by name
162 * @dev: Device to read from
163 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
164 * 'reg-names' property providing named-based identification. @index
165 * indicates the value to search for in 'reg-names'.
167 * @return address or FDT_ADDR_T_NONE if not found
169 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
172 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
173 * as a memory-mapped I/O pointer
175 * @dev: Device to read from
176 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
177 * 'reg-names' property providing named-based identification. @index
178 * indicates the value to search for in 'reg-names'.
180 * @return pointer or NULL if not found
182 void *dev_remap_addr_name(struct udevice *dev, const char* name);
185 * dev_read_addr() - Get the reg property of a device
187 * @dev: Device to read from
189 * @return address or FDT_ADDR_T_NONE if not found
191 fdt_addr_t dev_read_addr(struct udevice *dev);
194 * dev_read_addr_ptr() - Get the reg property of a device
197 * @dev: Device to read from
199 * @return pointer or NULL if not found
201 void *dev_read_addr_ptr(struct udevice *dev);
204 * dev_remap_addr() - Get the reg property of a device as a
205 * memory-mapped I/O pointer
207 * @dev: Device to read from
209 * @return pointer or NULL if not found
211 void *dev_remap_addr(struct udevice *dev);
214 * dev_read_addr_size() - get address and size from a device property
216 * This does no address translation. It simply reads an property that contains
217 * an address and a size value, one after the other.
219 * @dev: Device to read from
220 * @propname: property to read
221 * @sizep: place to put size value (on success)
222 * @return address value, or FDT_ADDR_T_NONE on error
224 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
228 * dev_read_name() - get the name of a device's node
230 * @node: valid node to look up
231 * @return name of node
233 const char *dev_read_name(struct udevice *dev);
236 * dev_read_stringlist_search() - find string in a string list and return index
238 * Note that it is possible for this function to succeed on property values
239 * that are not NUL-terminated. That's because the function will stop after
240 * finding the first occurrence of @string. This can for example happen with
241 * small-valued cell properties, such as #address-cells, when searching for
244 * @dev: device to check
245 * @propname: name of the property containing the string list
246 * @string: string to look up in the string list
249 * the index of the string in the list of strings
250 * -ENODATA if the property is not found
251 * -EINVAL on some other error
253 int dev_read_stringlist_search(struct udevice *dev, const char *property,
257 * dev_read_string_index() - obtain an indexed string from a string list
259 * @dev: device to examine
260 * @propname: name of the property containing the string list
261 * @index: index of the string to return
262 * @out: return location for the string
265 * length of string, if found or -ve error value if not found
267 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
271 * dev_read_string_count() - find the number of strings in a string list
273 * @dev: device to examine
274 * @propname: name of the property containing the string list
276 * number of strings in the list, or -ve error value if not found
278 int dev_read_string_count(struct udevice *dev, const char *propname);
280 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
282 * This function is useful to parse lists of phandles and their arguments.
283 * Returns 0 on success and fills out_args, on error returns appropriate
286 * Caller is responsible to call of_node_put() on the returned out_args->np
300 * list = <&phandle1 1 2 &phandle2 3>;
303 * To get a device_node of the `node2' node you may call this:
304 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
306 * @dev: device whose node containing a list
307 * @list_name: property name that contains a list
308 * @cells_name: property name that specifies phandles' arguments count
309 * @cells_count: Cell count to use if @cells_name is NULL
310 * @index: index of a phandle to parse out
311 * @out_args: optional pointer to output arguments structure (will be filled)
312 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
313 * @list_name does not exist, -EINVAL if a phandle was not found,
314 * @cells_name could not be found, the arguments were truncated or there
315 * were too many arguments.
317 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
318 const char *cells_name, int cell_count,
320 struct ofnode_phandle_args *out_args);
323 * dev_count_phandle_with_args() - Return phandle number in a list
325 * This function is usefull to get phandle number contained in a property list.
326 * For example, this allows to allocate the right amount of memory to keep
327 * clock's reference contained into the "clocks" property.
330 * @dev: device whose node containing a list
331 * @list_name: property name that contains a list
332 * @cells_name: property name that specifies phandles' arguments count
333 * @Returns number of phandle found on success, on error returns appropriate
337 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
338 const char *cells_name);
341 * dev_read_addr_cells() - Get the number of address cells for a device's node
343 * This walks back up the tree to find the closest #address-cells property
344 * which controls the given node.
346 * @dev: device to check
347 * @return number of address cells this node uses
349 int dev_read_addr_cells(struct udevice *dev);
352 * dev_read_size_cells() - Get the number of size cells for a device's node
354 * This walks back up the tree to find the closest #size-cells property
355 * which controls the given node.
357 * @dev: device to check
358 * @return number of size cells this node uses
360 int dev_read_size_cells(struct udevice *dev);
363 * dev_read_addr_cells() - Get the address cells property in a node
365 * This function matches fdt_address_cells().
367 * @dev: device to check
368 * @return number of address cells this node uses
370 int dev_read_simple_addr_cells(struct udevice *dev);
373 * dev_read_size_cells() - Get the size cells property in a node
375 * This function matches fdt_size_cells().
377 * @dev: device to check
378 * @return number of size cells this node uses
380 int dev_read_simple_size_cells(struct udevice *dev);
383 * dev_read_phandle() - Get the phandle from a device
385 * @dev: device to check
386 * @return phandle (1 or greater), or 0 if no phandle or other error
388 int dev_read_phandle(struct udevice *dev);
391 * dev_read_prop()- - read a property from a device's node
393 * @dev: device to check
394 * @propname: property to read
395 * @lenp: place to put length on success
396 * @return pointer to property, or NULL if not found
398 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
401 * dev_read_alias_seq() - Get the alias sequence number of a node
403 * This works out whether a node is pointed to by an alias, and if so, the
404 * sequence number of that alias. Aliases are of the form <base><num> where
405 * <num> is the sequence number. For example spi2 would be sequence number 2.
407 * @dev: device to look up
408 * @devnump: set to the sequence number if one is found
409 * @return 0 if a sequence was found, -ve if not
411 int dev_read_alias_seq(struct udevice *dev, int *devnump);
414 * dev_read_u32_array() - Find and read an array of 32 bit integers
416 * Search for a property in a device node and read 32-bit value(s) from
419 * The out_values is modified only if a valid u32 value can be decoded.
421 * @dev: device to look up
422 * @propname: name of the property to read
423 * @out_values: pointer to return value, modified only if return value is 0
424 * @sz: number of array elements to read
425 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
426 * property does not have a value, and -EOVERFLOW if the property data isn't
429 int dev_read_u32_array(struct udevice *dev, const char *propname,
430 u32 *out_values, size_t sz);
433 * dev_read_first_subnode() - find the first subnode of a device's node
435 * @dev: device to look up
436 * @return reference to the first subnode (which can be invalid if the device's
437 * node has no subnodes)
439 ofnode dev_read_first_subnode(struct udevice *dev);
442 * ofnode_next_subnode() - find the next sibling of a subnode
444 * @node: valid reference to previous node (sibling)
445 * @return reference to the next subnode (which can be invalid if the node
446 * has no more siblings)
448 ofnode dev_read_next_subnode(ofnode node);
451 * dev_read_u8_array_ptr() - find an 8-bit array
453 * Look up a device's node property and return a pointer to its contents as a
454 * byte array of given length. The property must have at least enough data
455 * for the array (count bytes). It may have more, but this will be ignored.
456 * The data is not copied.
458 * @dev: device to look up
459 * @propname: name of property to find
460 * @sz: number of array elements
461 * @return pointer to byte array if found, or NULL if the property is not
462 * found or there is not enough data
464 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
468 * dev_read_enabled() - check whether a node is enabled
470 * This looks for a 'status' property. If this exists, then returns 1 if
471 * the status is 'ok' and 0 otherwise. If there is no status property,
472 * it returns 1 on the assumption that anything mentioned should be enabled
475 * @dev: device to examine
476 * @return integer value 0 (not enabled) or 1 (enabled)
478 int dev_read_enabled(struct udevice *dev);
481 * dev_read_resource() - obtain an indexed resource from a device.
483 * @dev: device to examine
484 * @index index of the resource to retrieve (0 = first)
485 * @res returns the resource
486 * @return 0 if ok, negative on error
488 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
491 * dev_read_resource_byname() - obtain a named resource from a device.
493 * @dev: device to examine
494 * @name: name of the resource to retrieve
495 * @res: returns the resource
496 * @return 0 if ok, negative on error
498 int dev_read_resource_byname(struct udevice *dev, const char *name,
499 struct resource *res);
502 * dev_translate_address() - Translate a device-tree address
504 * Translate an address from the device-tree into a CPU physical address. This
505 * function walks up the tree and applies the various bus mappings along the
508 * @dev: device giving the context in which to translate the address
509 * @in_addr: pointer to the address to translate
510 * @return the translated address; OF_BAD_ADDR on error
512 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
515 * dev_translate_dma_address() - Translate a device-tree DMA address
517 * Translate a DMA address from the device-tree into a CPU physical address.
518 * This function walks up the tree and applies the various bus mappings along
521 * @dev: device giving the context in which to translate the DMA address
522 * @in_addr: pointer to the DMA address to translate
523 * @return the translated DMA address; OF_BAD_ADDR on error
525 u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr);
528 * dev_read_alias_highest_id - Get highest alias id for the given stem
529 * @stem: Alias stem to be examined
531 * The function travels the lookup table to get the highest alias id for the
533 * @return alias ID, if found, else -1
535 int dev_read_alias_highest_id(const char *stem);
537 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
539 static inline int dev_read_u32(struct udevice *dev,
540 const char *propname, u32 *outp)
542 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
545 static inline int dev_read_u32_default(struct udevice *dev,
546 const char *propname, int def)
548 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
551 static inline int dev_read_s32(struct udevice *dev,
552 const char *propname, s32 *outp)
554 return ofnode_read_s32(dev_ofnode(dev), propname, outp);
557 static inline int dev_read_s32_default(struct udevice *dev,
558 const char *propname, int def)
560 return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
563 static inline int dev_read_u32u(struct udevice *dev,
564 const char *propname, uint *outp)
569 ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
577 static inline const char *dev_read_string(struct udevice *dev,
578 const char *propname)
580 return ofnode_read_string(dev_ofnode(dev), propname);
583 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
585 return ofnode_read_bool(dev_ofnode(dev), propname);
588 static inline ofnode dev_read_subnode(struct udevice *dev,
589 const char *subbnode_name)
591 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
594 static inline int dev_read_size(struct udevice *dev, const char *propname)
596 return ofnode_read_size(dev_ofnode(dev), propname);
599 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
601 return devfdt_get_addr_index(dev, index);
604 static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
607 return devfdt_get_addr_name(dev, name);
610 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
612 return devfdt_get_addr(dev);
615 static inline void *dev_read_addr_ptr(struct udevice *dev)
617 return devfdt_get_addr_ptr(dev);
620 static inline void *dev_remap_addr(struct udevice *dev)
622 return devfdt_remap_addr(dev);
625 static inline void *dev_remap_addr_index(struct udevice *dev, int index)
627 return devfdt_remap_addr_index(dev, index);
630 static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
632 return devfdt_remap_addr_name(dev, name);
635 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
636 const char *propname,
639 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
642 static inline const char *dev_read_name(struct udevice *dev)
644 return ofnode_get_name(dev_ofnode(dev));
647 static inline int dev_read_stringlist_search(struct udevice *dev,
648 const char *propname,
651 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
654 static inline int dev_read_string_index(struct udevice *dev,
655 const char *propname, int index,
658 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
661 static inline int dev_read_string_count(struct udevice *dev,
662 const char *propname)
664 return ofnode_read_string_count(dev_ofnode(dev), propname);
667 static inline int dev_read_phandle_with_args(struct udevice *dev,
668 const char *list_name, const char *cells_name, int cell_count,
669 int index, struct ofnode_phandle_args *out_args)
671 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
672 cells_name, cell_count, index,
676 static inline int dev_count_phandle_with_args(struct udevice *dev,
677 const char *list_name, const char *cells_name)
679 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
683 static inline int dev_read_addr_cells(struct udevice *dev)
685 /* NOTE: this call should walk up the parent stack */
686 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
689 static inline int dev_read_size_cells(struct udevice *dev)
691 /* NOTE: this call should walk up the parent stack */
692 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
695 static inline int dev_read_simple_addr_cells(struct udevice *dev)
697 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
700 static inline int dev_read_simple_size_cells(struct udevice *dev)
702 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
705 static inline int dev_read_phandle(struct udevice *dev)
707 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
710 static inline const void *dev_read_prop(struct udevice *dev,
711 const char *propname, int *lenp)
713 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
716 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
718 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
719 dev_of_offset(dev), devnump);
722 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
723 u32 *out_values, size_t sz)
725 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
728 static inline ofnode dev_read_first_subnode(struct udevice *dev)
730 return ofnode_first_subnode(dev_ofnode(dev));
733 static inline ofnode dev_read_next_subnode(ofnode node)
735 return ofnode_next_subnode(node);
738 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
739 const char *propname, size_t sz)
741 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
744 static inline int dev_read_enabled(struct udevice *dev)
746 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
749 static inline int dev_read_resource(struct udevice *dev, uint index,
750 struct resource *res)
752 return ofnode_read_resource(dev_ofnode(dev), index, res);
755 static inline int dev_read_resource_byname(struct udevice *dev,
757 struct resource *res)
759 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
762 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
764 return ofnode_translate_address(dev_ofnode(dev), in_addr);
767 static inline u64 dev_translate_dma_address(struct udevice *dev, const fdt32_t *in_addr)
769 return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
772 static inline int dev_read_alias_highest_id(const char *stem)
774 return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
777 #endif /* CONFIG_DM_DEV_READ_INLINE */
780 * dev_for_each_subnode() - Helper function to iterate through subnodes
782 * This creates a for() loop which works through the subnodes in a device's
785 * @subnode: ofnode holding the current subnode
786 * @dev: device to use for interation (struct udevice *)
788 #define dev_for_each_subnode(subnode, dev) \
789 for (subnode = dev_read_first_subnode(dev); \
790 ofnode_valid(subnode); \
791 subnode = ofnode_next_subnode(subnode))