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_string() - Read a string from a device's DT property
70 * @dev: device to read DT property from
71 * @propname: name of the property to read
72 * @return string from property value, or NULL if there is no such property
74 const char *dev_read_string(struct udevice *dev, const char *propname);
77 * dev_read_bool() - read a boolean value from a device's DT property
79 * @dev: device to read DT property from
80 * @propname: name of property to read
81 * @return true if property is present (meaning true), false if not present
83 bool dev_read_bool(struct udevice *dev, const char *propname);
86 * dev_read_subnode() - find a named subnode of a device
88 * @dev: device whose DT node contains the subnode
89 * @subnode_name: name of subnode to find
90 * @return reference to subnode (which can be invalid if there is no such
93 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
96 * dev_read_size() - read the size of a property
98 * @dev: device to check
99 * @propname: property to check
100 * @return size of property if present, or -EINVAL if not
102 int dev_read_size(struct udevice *dev, const char *propname);
105 * dev_read_addr_index() - Get the indexed reg property of a device
107 * @dev: Device to read from
108 * @index: the 'reg' property can hold a list of <addr, size> pairs
109 * and @index is used to select which one is required
111 * @return address or FDT_ADDR_T_NONE if not found
113 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
116 * dev_remap_addr_index() - Get the indexed reg property of a device
117 * as a memory-mapped I/O pointer
119 * @dev: Device to read from
120 * @index: the 'reg' property can hold a list of <addr, size> pairs
121 * and @index is used to select which one is required
123 * @return pointer or NULL if not found
125 void *dev_remap_addr_index(struct udevice *dev, int index);
128 * dev_read_addr() - Get the reg property of a device
130 * @dev: Device to read from
132 * @return address or FDT_ADDR_T_NONE if not found
134 fdt_addr_t dev_read_addr(struct udevice *dev);
137 * dev_read_addr_ptr() - Get the reg property of a device
140 * @dev: Device to read from
142 * @return pointer or NULL if not found
144 void *dev_read_addr_ptr(struct udevice *dev);
147 * dev_remap_addr() - Get the reg property of a device as a
148 * memory-mapped I/O pointer
150 * @dev: Device to read from
152 * @return pointer or NULL if not found
154 void *dev_remap_addr(struct udevice *dev);
157 * dev_read_addr_size() - get address and size from a device property
159 * This does no address translation. It simply reads an property that contains
160 * an address and a size value, one after the other.
162 * @dev: Device to read from
163 * @propname: property to read
164 * @sizep: place to put size value (on success)
165 * @return address value, or FDT_ADDR_T_NONE on error
167 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
171 * dev_read_name() - get the name of a device's node
173 * @node: valid node to look up
174 * @return name of node
176 const char *dev_read_name(struct udevice *dev);
179 * dev_read_stringlist_search() - find string in a string list and return index
181 * Note that it is possible for this function to succeed on property values
182 * that are not NUL-terminated. That's because the function will stop after
183 * finding the first occurrence of @string. This can for example happen with
184 * small-valued cell properties, such as #address-cells, when searching for
187 * @dev: device to check
188 * @propname: name of the property containing the string list
189 * @string: string to look up in the string list
192 * the index of the string in the list of strings
193 * -ENODATA if the property is not found
194 * -EINVAL on some other error
196 int dev_read_stringlist_search(struct udevice *dev, const char *property,
200 * dev_read_string_index() - obtain an indexed string from a string list
202 * @dev: device to examine
203 * @propname: name of the property containing the string list
204 * @index: index of the string to return
205 * @out: return location for the string
208 * length of string, if found or -ve error value if not found
210 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
214 * dev_read_string_count() - find the number of strings in a string list
216 * @dev: device to examine
217 * @propname: name of the property containing the string list
219 * number of strings in the list, or -ve error value if not found
221 int dev_read_string_count(struct udevice *dev, const char *propname);
223 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
225 * This function is useful to parse lists of phandles and their arguments.
226 * Returns 0 on success and fills out_args, on error returns appropriate
229 * Caller is responsible to call of_node_put() on the returned out_args->np
243 * list = <&phandle1 1 2 &phandle2 3>;
246 * To get a device_node of the `node2' node you may call this:
247 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
249 * @dev: device whose node containing a list
250 * @list_name: property name that contains a list
251 * @cells_name: property name that specifies phandles' arguments count
252 * @cells_count: Cell count to use if @cells_name is NULL
253 * @index: index of a phandle to parse out
254 * @out_args: optional pointer to output arguments structure (will be filled)
255 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
256 * @list_name does not exist, -EINVAL if a phandle was not found,
257 * @cells_name could not be found, the arguments were truncated or there
258 * were too many arguments.
260 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
261 const char *cells_name, int cell_count,
263 struct ofnode_phandle_args *out_args);
266 * dev_count_phandle_with_args() - Return phandle number in a list
268 * This function is usefull to get phandle number contained in a property list.
269 * For example, this allows to allocate the right amount of memory to keep
270 * clock's reference contained into the "clocks" property.
273 * @dev: device whose node containing a list
274 * @list_name: property name that contains a list
275 * @cells_name: property name that specifies phandles' arguments count
276 * @Returns number of phandle found on success, on error returns appropriate
280 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
281 const char *cells_name);
284 * dev_read_addr_cells() - Get the number of address cells for a device's node
286 * This walks back up the tree to find the closest #address-cells property
287 * which controls the given node.
289 * @dev: device to check
290 * @return number of address cells this node uses
292 int dev_read_addr_cells(struct udevice *dev);
295 * dev_read_size_cells() - Get the number of size cells for a device's node
297 * This walks back up the tree to find the closest #size-cells property
298 * which controls the given node.
300 * @dev: device to check
301 * @return number of size cells this node uses
303 int dev_read_size_cells(struct udevice *dev);
306 * dev_read_addr_cells() - Get the address cells property in a node
308 * This function matches fdt_address_cells().
310 * @dev: device to check
311 * @return number of address cells this node uses
313 int dev_read_simple_addr_cells(struct udevice *dev);
316 * dev_read_size_cells() - Get the size cells property in a node
318 * This function matches fdt_size_cells().
320 * @dev: device to check
321 * @return number of size cells this node uses
323 int dev_read_simple_size_cells(struct udevice *dev);
326 * dev_read_phandle() - Get the phandle from a device
328 * @dev: device to check
329 * @return phandle (1 or greater), or 0 if no phandle or other error
331 int dev_read_phandle(struct udevice *dev);
334 * dev_read_prop()- - read a property from a device's node
336 * @dev: device to check
337 * @propname: property to read
338 * @lenp: place to put length on success
339 * @return pointer to property, or NULL if not found
341 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
344 * dev_read_alias_seq() - Get the alias sequence number of a node
346 * This works out whether a node is pointed to by an alias, and if so, the
347 * sequence number of that alias. Aliases are of the form <base><num> where
348 * <num> is the sequence number. For example spi2 would be sequence number 2.
350 * @dev: device to look up
351 * @devnump: set to the sequence number if one is found
352 * @return 0 if a sequence was found, -ve if not
354 int dev_read_alias_seq(struct udevice *dev, int *devnump);
357 * dev_read_u32_array() - Find and read an array of 32 bit integers
359 * Search for a property in a device node and read 32-bit value(s) from
362 * The out_values is modified only if a valid u32 value can be decoded.
364 * @dev: device to look up
365 * @propname: name of the property to read
366 * @out_values: pointer to return value, modified only if return value is 0
367 * @sz: number of array elements to read
368 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
369 * property does not have a value, and -EOVERFLOW if the property data isn't
372 int dev_read_u32_array(struct udevice *dev, const char *propname,
373 u32 *out_values, size_t sz);
376 * dev_read_first_subnode() - find the first subnode of a device's node
378 * @dev: device to look up
379 * @return reference to the first subnode (which can be invalid if the device's
380 * node has no subnodes)
382 ofnode dev_read_first_subnode(struct udevice *dev);
385 * ofnode_next_subnode() - find the next sibling of a subnode
387 * @node: valid reference to previous node (sibling)
388 * @return reference to the next subnode (which can be invalid if the node
389 * has no more siblings)
391 ofnode dev_read_next_subnode(ofnode node);
394 * dev_read_u8_array_ptr() - find an 8-bit array
396 * Look up a device's node property and return a pointer to its contents as a
397 * byte array of given length. The property must have at least enough data
398 * for the array (count bytes). It may have more, but this will be ignored.
399 * The data is not copied.
401 * @dev: device to look up
402 * @propname: name of property to find
403 * @sz: number of array elements
404 * @return pointer to byte array if found, or NULL if the property is not
405 * found or there is not enough data
407 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
411 * dev_read_enabled() - check whether a node is enabled
413 * This looks for a 'status' property. If this exists, then returns 1 if
414 * the status is 'ok' and 0 otherwise. If there is no status property,
415 * it returns 1 on the assumption that anything mentioned should be enabled
418 * @dev: device to examine
419 * @return integer value 0 (not enabled) or 1 (enabled)
421 int dev_read_enabled(struct udevice *dev);
424 * dev_read_resource() - obtain an indexed resource from a device.
426 * @dev: device to examine
427 * @index index of the resource to retrieve (0 = first)
428 * @res returns the resource
429 * @return 0 if ok, negative on error
431 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
434 * dev_read_resource_byname() - obtain a named resource from a device.
436 * @dev: device to examine
437 * @name: name of the resource to retrieve
438 * @res: returns the resource
439 * @return 0 if ok, negative on error
441 int dev_read_resource_byname(struct udevice *dev, const char *name,
442 struct resource *res);
445 * dev_translate_address() - Tranlate a device-tree address
447 * Translate an address from the device-tree into a CPU physical address. This
448 * function walks up the tree and applies the various bus mappings along the
451 * @dev: device giving the context in which to translate the address
452 * @in_addr: pointer to the address to translate
453 * @return the translated address; OF_BAD_ADDR on error
455 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
456 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
458 static inline int dev_read_u32(struct udevice *dev,
459 const char *propname, u32 *outp)
461 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
464 static inline int dev_read_u32_default(struct udevice *dev,
465 const char *propname, int def)
467 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
470 static inline const char *dev_read_string(struct udevice *dev,
471 const char *propname)
473 return ofnode_read_string(dev_ofnode(dev), propname);
476 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
478 return ofnode_read_bool(dev_ofnode(dev), propname);
481 static inline ofnode dev_read_subnode(struct udevice *dev,
482 const char *subbnode_name)
484 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
487 static inline int dev_read_size(struct udevice *dev, const char *propname)
489 return ofnode_read_size(dev_ofnode(dev), propname);
492 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
494 return devfdt_get_addr_index(dev, index);
497 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
499 return devfdt_get_addr(dev);
502 static inline void *dev_read_addr_ptr(struct udevice *dev)
504 return devfdt_get_addr_ptr(dev);
507 static inline void *dev_remap_addr(struct udevice *dev)
509 return devfdt_remap_addr(dev);
512 static inline void *dev_remap_addr_index(struct udevice *dev, int index)
514 return devfdt_remap_addr_index(dev, index);
517 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
518 const char *propname,
521 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
524 static inline const char *dev_read_name(struct udevice *dev)
526 return ofnode_get_name(dev_ofnode(dev));
529 static inline int dev_read_stringlist_search(struct udevice *dev,
530 const char *propname,
533 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
536 static inline int dev_read_string_index(struct udevice *dev,
537 const char *propname, int index,
540 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
543 static inline int dev_read_string_count(struct udevice *dev,
544 const char *propname)
546 return ofnode_read_string_count(dev_ofnode(dev), propname);
549 static inline int dev_read_phandle_with_args(struct udevice *dev,
550 const char *list_name, const char *cells_name, int cell_count,
551 int index, struct ofnode_phandle_args *out_args)
553 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
554 cells_name, cell_count, index,
558 static inline int dev_count_phandle_with_args(struct udevice *dev,
559 const char *list_name, const char *cells_name)
561 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
565 static inline int dev_read_addr_cells(struct udevice *dev)
567 /* NOTE: this call should walk up the parent stack */
568 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
571 static inline int dev_read_size_cells(struct udevice *dev)
573 /* NOTE: this call should walk up the parent stack */
574 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
577 static inline int dev_read_simple_addr_cells(struct udevice *dev)
579 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
582 static inline int dev_read_simple_size_cells(struct udevice *dev)
584 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
587 static inline int dev_read_phandle(struct udevice *dev)
589 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
592 static inline const void *dev_read_prop(struct udevice *dev,
593 const char *propname, int *lenp)
595 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
598 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
600 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
601 dev_of_offset(dev), devnump);
604 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
605 u32 *out_values, size_t sz)
607 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
610 static inline ofnode dev_read_first_subnode(struct udevice *dev)
612 return ofnode_first_subnode(dev_ofnode(dev));
615 static inline ofnode dev_read_next_subnode(ofnode node)
617 return ofnode_next_subnode(node);
620 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
621 const char *propname, size_t sz)
623 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
626 static inline int dev_read_enabled(struct udevice *dev)
628 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
631 static inline int dev_read_resource(struct udevice *dev, uint index,
632 struct resource *res)
634 return ofnode_read_resource(dev_ofnode(dev), index, res);
637 static inline int dev_read_resource_byname(struct udevice *dev,
639 struct resource *res)
641 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
644 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
646 return ofnode_translate_address(dev_ofnode(dev), in_addr);
649 #endif /* CONFIG_DM_DEV_READ_INLINE */
652 * dev_for_each_subnode() - Helper function to iterate through subnodes
654 * This creates a for() loop which works through the subnodes in a device's
657 * @subnode: ofnode holding the current subnode
658 * @dev: device to use for interation (struct udevice *)
660 #define dev_for_each_subnode(subnode, dev) \
661 for (subnode = dev_read_first_subnode(dev); \
662 ofnode_valid(subnode); \
663 subnode = ofnode_next_subnode(subnode))