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
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_read_addr() - Get the reg property of a device
118 * @dev: Device to read from
120 * @return address or FDT_ADDR_T_NONE if not found
122 fdt_addr_t dev_read_addr(struct udevice *dev);
125 * dev_read_addr_ptr() - Get the reg property of a device
128 * @dev: Device to read from
130 * @return pointer or NULL if not found
132 void *dev_read_addr_ptr(struct udevice *dev);
135 * dev_read_addr_size() - get address and size from a device property
137 * This does no address translation. It simply reads an property that contains
138 * an address and a size value, one after the other.
140 * @dev: Device to read from
141 * @propname: property to read
142 * @sizep: place to put size value (on success)
143 * @return address value, or FDT_ADDR_T_NONE on error
145 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
149 * dev_read_name() - get the name of a device's node
151 * @node: valid node to look up
152 * @return name of node
154 const char *dev_read_name(struct udevice *dev);
157 * dev_read_stringlist_search() - find string in a string list and return index
159 * Note that it is possible for this function to succeed on property values
160 * that are not NUL-terminated. That's because the function will stop after
161 * finding the first occurrence of @string. This can for example happen with
162 * small-valued cell properties, such as #address-cells, when searching for
165 * @dev: device to check
166 * @propname: name of the property containing the string list
167 * @string: string to look up in the string list
170 * the index of the string in the list of strings
171 * -ENODATA if the property is not found
172 * -EINVAL on some other error
174 int dev_read_stringlist_search(struct udevice *dev, const char *property,
178 * dev_read_string_index() - obtain an indexed string from a string list
180 * @dev: device to examine
181 * @propname: name of the property containing the string list
182 * @index: index of the string to return
183 * @out: return location for the string
186 * length of string, if found or -ve error value if not found
188 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
192 * dev_read_string_count() - find the number of strings in a string list
194 * @dev: device to examine
195 * @propname: name of the property containing the string list
197 * number of strings in the list, or -ve error value if not found
199 int dev_read_string_count(struct udevice *dev, const char *propname);
201 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
203 * This function is useful to parse lists of phandles and their arguments.
204 * Returns 0 on success and fills out_args, on error returns appropriate
207 * Caller is responsible to call of_node_put() on the returned out_args->np
221 * list = <&phandle1 1 2 &phandle2 3>;
224 * To get a device_node of the `node2' node you may call this:
225 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
227 * @dev: device whose node containing a list
228 * @list_name: property name that contains a list
229 * @cells_name: property name that specifies phandles' arguments count
230 * @cells_count: Cell count to use if @cells_name is NULL
231 * @index: index of a phandle to parse out
232 * @out_args: optional pointer to output arguments structure (will be filled)
233 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
234 * @list_name does not exist, -EINVAL if a phandle was not found,
235 * @cells_name could not be found, the arguments were truncated or there
236 * were too many arguments.
238 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
239 const char *cells_name, int cell_count,
241 struct ofnode_phandle_args *out_args);
244 * dev_count_phandle_with_args() - Return phandle number in a list
246 * This function is usefull to get phandle number contained in a property list.
247 * For example, this allows to allocate the right amount of memory to keep
248 * clock's reference contained into the "clocks" property.
251 * @dev: device whose node containing a list
252 * @list_name: property name that contains a list
253 * @cells_name: property name that specifies phandles' arguments count
254 * @Returns number of phandle found on success, on error returns appropriate
258 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
259 const char *cells_name);
262 * dev_read_addr_cells() - Get the number of address cells for a device's node
264 * This walks back up the tree to find the closest #address-cells property
265 * which controls the given node.
267 * @dev: device to check
268 * @return number of address cells this node uses
270 int dev_read_addr_cells(struct udevice *dev);
273 * dev_read_size_cells() - Get the number of size cells for a device's node
275 * This walks back up the tree to find the closest #size-cells property
276 * which controls the given node.
278 * @dev: device to check
279 * @return number of size cells this node uses
281 int dev_read_size_cells(struct udevice *dev);
284 * dev_read_addr_cells() - Get the address cells property in a node
286 * This function matches fdt_address_cells().
288 * @dev: device to check
289 * @return number of address cells this node uses
291 int dev_read_simple_addr_cells(struct udevice *dev);
294 * dev_read_size_cells() - Get the size cells property in a node
296 * This function matches fdt_size_cells().
298 * @dev: device to check
299 * @return number of size cells this node uses
301 int dev_read_simple_size_cells(struct udevice *dev);
304 * dev_read_phandle() - Get the phandle from a device
306 * @dev: device to check
307 * @return phandle (1 or greater), or 0 if no phandle or other error
309 int dev_read_phandle(struct udevice *dev);
312 * dev_read_prop()- - read a property from a device's node
314 * @dev: device to check
315 * @propname: property to read
316 * @lenp: place to put length on success
317 * @return pointer to property, or NULL if not found
319 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
322 * dev_read_alias_seq() - Get the alias sequence number of a node
324 * This works out whether a node is pointed to by an alias, and if so, the
325 * sequence number of that alias. Aliases are of the form <base><num> where
326 * <num> is the sequence number. For example spi2 would be sequence number 2.
328 * @dev: device to look up
329 * @devnump: set to the sequence number if one is found
330 * @return 0 if a sequence was found, -ve if not
332 int dev_read_alias_seq(struct udevice *dev, int *devnump);
335 * dev_read_u32_array() - Find and read an array of 32 bit integers
337 * Search for a property in a device node and read 32-bit value(s) from
340 * The out_values is modified only if a valid u32 value can be decoded.
342 * @dev: device to look up
343 * @propname: name of the property to read
344 * @out_values: pointer to return value, modified only if return value is 0
345 * @sz: number of array elements to read
346 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
347 * property does not have a value, and -EOVERFLOW if the property data isn't
350 int dev_read_u32_array(struct udevice *dev, const char *propname,
351 u32 *out_values, size_t sz);
354 * dev_read_first_subnode() - find the first subnode of a device's node
356 * @dev: device to look up
357 * @return reference to the first subnode (which can be invalid if the device's
358 * node has no subnodes)
360 ofnode dev_read_first_subnode(struct udevice *dev);
363 * ofnode_next_subnode() - find the next sibling of a subnode
365 * @node: valid reference to previous node (sibling)
366 * @return reference to the next subnode (which can be invalid if the node
367 * has no more siblings)
369 ofnode dev_read_next_subnode(ofnode node);
372 * dev_read_u8_array_ptr() - find an 8-bit array
374 * Look up a device's node property and return a pointer to its contents as a
375 * byte array of given length. The property must have at least enough data
376 * for the array (count bytes). It may have more, but this will be ignored.
377 * The data is not copied.
379 * @dev: device to look up
380 * @propname: name of property to find
381 * @sz: number of array elements
382 * @return pointer to byte array if found, or NULL if the property is not
383 * found or there is not enough data
385 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
389 * dev_read_enabled() - check whether a node is enabled
391 * This looks for a 'status' property. If this exists, then returns 1 if
392 * the status is 'ok' and 0 otherwise. If there is no status property,
393 * it returns 1 on the assumption that anything mentioned should be enabled
396 * @dev: device to examine
397 * @return integer value 0 (not enabled) or 1 (enabled)
399 int dev_read_enabled(struct udevice *dev);
402 * dev_read_resource() - obtain an indexed resource from a device.
404 * @dev: device to examine
405 * @index index of the resource to retrieve (0 = first)
406 * @res returns the resource
407 * @return 0 if ok, negative on error
409 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
412 * dev_read_resource_byname() - obtain a named resource from a device.
414 * @dev: device to examine
415 * @name: name of the resource to retrieve
416 * @res: returns the resource
417 * @return 0 if ok, negative on error
419 int dev_read_resource_byname(struct udevice *dev, const char *name,
420 struct resource *res);
423 * dev_translate_address() - Tranlate a device-tree address
425 * Translate an address from the device-tree into a CPU physical address. This
426 * function walks up the tree and applies the various bus mappings along the
429 * @dev: device giving the context in which to translate the address
430 * @in_addr: pointer to the address to translate
431 * @return the translated address; OF_BAD_ADDR on error
433 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
434 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
436 static inline int dev_read_u32(struct udevice *dev,
437 const char *propname, u32 *outp)
439 return ofnode_read_u32(dev_ofnode(dev), propname, outp);
442 static inline int dev_read_u32_default(struct udevice *dev,
443 const char *propname, int def)
445 return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
448 static inline const char *dev_read_string(struct udevice *dev,
449 const char *propname)
451 return ofnode_read_string(dev_ofnode(dev), propname);
454 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
456 return ofnode_read_bool(dev_ofnode(dev), propname);
459 static inline ofnode dev_read_subnode(struct udevice *dev,
460 const char *subbnode_name)
462 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
465 static inline int dev_read_size(struct udevice *dev, const char *propname)
467 return ofnode_read_size(dev_ofnode(dev), propname);
470 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
472 return devfdt_get_addr_index(dev, index);
475 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
477 return devfdt_get_addr(dev);
480 static inline void *dev_read_addr_ptr(struct udevice *dev)
482 return devfdt_get_addr_ptr(dev);
485 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
486 const char *propname,
489 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
492 static inline const char *dev_read_name(struct udevice *dev)
494 return ofnode_get_name(dev_ofnode(dev));
497 static inline int dev_read_stringlist_search(struct udevice *dev,
498 const char *propname,
501 return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
504 static inline int dev_read_string_index(struct udevice *dev,
505 const char *propname, int index,
508 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
511 static inline int dev_read_string_count(struct udevice *dev,
512 const char *propname)
514 return ofnode_read_string_count(dev_ofnode(dev), propname);
517 static inline int dev_read_phandle_with_args(struct udevice *dev,
518 const char *list_name, const char *cells_name, int cell_count,
519 int index, struct ofnode_phandle_args *out_args)
521 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
522 cells_name, cell_count, index,
526 static inline int dev_count_phandle_with_args(struct udevice *dev,
527 const char *list_name, const char *cells_name)
529 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
533 static inline int dev_read_addr_cells(struct udevice *dev)
535 /* NOTE: this call should walk up the parent stack */
536 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
539 static inline int dev_read_size_cells(struct udevice *dev)
541 /* NOTE: this call should walk up the parent stack */
542 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
545 static inline int dev_read_simple_addr_cells(struct udevice *dev)
547 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
550 static inline int dev_read_simple_size_cells(struct udevice *dev)
552 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
555 static inline int dev_read_phandle(struct udevice *dev)
557 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
560 static inline const void *dev_read_prop(struct udevice *dev,
561 const char *propname, int *lenp)
563 return ofnode_get_property(dev_ofnode(dev), propname, lenp);
566 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
568 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
569 dev_of_offset(dev), devnump);
572 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
573 u32 *out_values, size_t sz)
575 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
578 static inline ofnode dev_read_first_subnode(struct udevice *dev)
580 return ofnode_first_subnode(dev_ofnode(dev));
583 static inline ofnode dev_read_next_subnode(ofnode node)
585 return ofnode_next_subnode(node);
588 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
589 const char *propname, size_t sz)
591 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
594 static inline int dev_read_enabled(struct udevice *dev)
596 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
599 static inline int dev_read_resource(struct udevice *dev, uint index,
600 struct resource *res)
602 return ofnode_read_resource(dev_ofnode(dev), index, res);
605 static inline int dev_read_resource_byname(struct udevice *dev,
607 struct resource *res)
609 return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
612 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
614 return ofnode_translate_address(dev_ofnode(dev), in_addr);
617 #endif /* CONFIG_DM_DEV_READ_INLINE */
620 * dev_for_each_subnode() - Helper function to iterate through subnodes
622 * This creates a for() loop which works through the subnodes in a device's
625 * @subnode: ofnode holding the current subnode
626 * @dev: device to use for interation (struct udevice *)
628 #define dev_for_each_subnode(subnode, dev) \
629 for (subnode = dev_read_first_subnode(dev); \
630 ofnode_valid(subnode); \
631 subnode = ofnode_next_subnode(subnode))