]>
git.ipfire.org Git - people/ms/u-boot.git/blob - include/dm/read.h
2 * Function to read values from the device tree node attached to a udevice.
4 * Copyright (c) 2017 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/fdtaddr.h>
14 #include <dm/ofnode.h>
15 #include <dm/uclass.h>
19 #if CONFIG_IS_ENABLED(OF_LIVE)
20 static inline const struct device_node
*dev_np(struct udevice
*dev
)
22 return ofnode_to_np(dev
->node
);
25 static inline const struct device_node
*dev_np(struct udevice
*dev
)
32 * dev_ofnode() - get the DT node reference associated with a udevice
34 * @dev: device to check
35 * @return reference of the the device's DT node
37 static inline ofnode
dev_ofnode(struct udevice
*dev
)
42 static inline bool dev_of_valid(struct udevice
*dev
)
44 return ofnode_valid(dev_ofnode(dev
));
47 #ifndef CONFIG_DM_DEV_READ_INLINE
49 * dev_read_u32_default() - 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 * @def: default value to return if the property has no value
54 * @return property value, or @def if not found
56 int dev_read_u32_default(struct udevice
*dev
, const char *propname
, int def
);
59 * dev_read_string() - Read a string from a device's DT property
61 * @dev: device to read DT property from
62 * @propname: name of the property to read
63 * @return string from property value, or NULL if there is no such property
65 const char *dev_read_string(struct udevice
*dev
, const char *propname
);
68 * dev_read_bool() - read a boolean value from a device's DT property
70 * @dev: device to read DT property from
71 * @propname: name of property to read
72 * @return true if property is present (meaning true), false if not present
74 bool dev_read_bool(struct udevice
*dev
, const char *propname
);
77 * dev_read_subnode() - find a named subnode of a device
79 * @dev: device whose DT node contains the subnode
80 * @subnode_name: name of subnode to find
81 * @return reference to subnode (which can be invalid if there is no such
84 ofnode
dev_read_subnode(struct udevice
*dev
, const char *subbnode_name
);
87 * dev_read_size() - read the size of a property
89 * @dev: device to check
90 * @propname: property to check
91 * @return size of property if present, or -EINVAL if not
93 int dev_read_size(struct udevice
*dev
, const char *propname
);
96 * dev_read_addr_index() - Get the indexed reg property of a device
98 * @dev: Device to read from
99 * @index: the 'reg' property can hold a list of <addr, size> pairs
100 * and @index is used to select which one is required
102 * @return address or FDT_ADDR_T_NONE if not found
104 fdt_addr_t
dev_read_addr_index(struct udevice
*dev
, int index
);
107 * dev_read_addr() - Get the reg property of a device
109 * @dev: Device to read from
111 * @return address or FDT_ADDR_T_NONE if not found
113 fdt_addr_t
dev_read_addr(struct udevice
*dev
);
116 * dev_read_addr_ptr() - Get the reg property of a device
119 * @dev: Device to read from
121 * @return pointer or NULL if not found
123 void *dev_read_addr_ptr(struct udevice
*dev
);
126 * dev_read_addr_size() - get address and size from a device property
128 * This does no address translation. It simply reads an property that contains
129 * an address and a size value, one after the other.
131 * @dev: Device to read from
132 * @propname: property to read
133 * @sizep: place to put size value (on success)
134 * @return address value, or FDT_ADDR_T_NONE on error
136 fdt_addr_t
dev_read_addr_size(struct udevice
*dev
, const char *propname
,
140 * dev_read_name() - get the name of a device's node
142 * @node: valid node to look up
143 * @return name of node
145 const char *dev_read_name(struct udevice
*dev
);
148 * dev_read_stringlist_search() - find string in a string list and return index
150 * Note that it is possible for this function to succeed on property values
151 * that are not NUL-terminated. That's because the function will stop after
152 * finding the first occurrence of @string. This can for example happen with
153 * small-valued cell properties, such as #address-cells, when searching for
156 * @dev: device to check
157 * @propname: name of the property containing the string list
158 * @string: string to look up in the string list
161 * the index of the string in the list of strings
162 * -ENODATA if the property is not found
163 * -EINVAL on some other error
165 int dev_read_stringlist_search(struct udevice
*dev
, const char *property
,
169 * dev_read_string_index() - obtain an indexed string from a string list
171 * @dev: device to examine
172 * @propname: name of the property containing the string list
173 * @index: index of the string to return
174 * @out: return location for the string
177 * length of string, if found or -ve error value if not found
179 int dev_read_string_index(struct udevice
*dev
, const char *propname
, int index
,
183 * dev_read_string_count() - find the number of strings in a string list
185 * @dev: device to examine
186 * @propname: name of the property containing the string list
188 * number of strings in the list, or -ve error value if not found
190 int dev_read_string_count(struct udevice
*dev
, const char *propname
);
192 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
194 * This function is useful to parse lists of phandles and their arguments.
195 * Returns 0 on success and fills out_args, on error returns appropriate
198 * Caller is responsible to call of_node_put() on the returned out_args->np
212 * list = <&phandle1 1 2 &phandle2 3>;
215 * To get a device_node of the `node2' node you may call this:
216 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
218 * @dev: device whose node containing a list
219 * @list_name: property name that contains a list
220 * @cells_name: property name that specifies phandles' arguments count
221 * @cells_count: Cell count to use if @cells_name is NULL
222 * @index: index of a phandle to parse out
223 * @out_args: optional pointer to output arguments structure (will be filled)
224 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
225 * @list_name does not exist, -EINVAL if a phandle was not found,
226 * @cells_name could not be found, the arguments were truncated or there
227 * were too many arguments.
229 int dev_read_phandle_with_args(struct udevice
*dev
, const char *list_name
,
230 const char *cells_name
, int cell_count
,
232 struct ofnode_phandle_args
*out_args
);
235 * dev_count_phandle_with_args() - Return phandle number in a list
237 * This function is usefull to get phandle number contained in a property list.
238 * For example, this allows to allocate the right amount of memory to keep
239 * clock's reference contained into the "clocks" property.
242 * @dev: device whose node containing a list
243 * @list_name: property name that contains a list
244 * @cells_name: property name that specifies phandles' arguments count
245 * @Returns number of phandle found on success, on error returns appropriate
249 int dev_count_phandle_with_args(struct udevice
*dev
, const char *list_name
,
250 const char *cells_name
);
253 * dev_read_addr_cells() - Get the number of address cells for a device's node
255 * This walks back up the tree to find the closest #address-cells property
256 * which controls the given node.
258 * @dev: devioe to check
259 * @return number of address cells this node uses
261 int dev_read_addr_cells(struct udevice
*dev
);
264 * dev_read_size_cells() - Get the number of size cells for a device's node
266 * This walks back up the tree to find the closest #size-cells property
267 * which controls the given node.
269 * @dev: devioe to check
270 * @return number of size cells this node uses
272 int dev_read_size_cells(struct udevice
*dev
);
275 * dev_read_addr_cells() - Get the address cells property in a node
277 * This function matches fdt_address_cells().
279 * @dev: devioe to check
280 * @return number of address cells this node uses
282 int dev_read_simple_addr_cells(struct udevice
*dev
);
285 * dev_read_size_cells() - Get the size cells property in a node
287 * This function matches fdt_size_cells().
289 * @dev: devioe to check
290 * @return number of size cells this node uses
292 int dev_read_simple_size_cells(struct udevice
*dev
);
295 * dev_read_phandle() - Get the phandle from a device
297 * @dev: device to check
298 * @return phandle (1 or greater), or 0 if no phandle or other error
300 int dev_read_phandle(struct udevice
*dev
);
303 * dev_read_prop()- - read a property from a device's node
305 * @dev: device to check
306 * @propname: property to read
307 * @lenp: place to put length on success
308 * @return pointer to property, or NULL if not found
310 const void *dev_read_prop(struct udevice
*dev
, const char *propname
, int *lenp
);
313 * dev_read_alias_seq() - Get the alias sequence number of a node
315 * This works out whether a node is pointed to by an alias, and if so, the
316 * sequence number of that alias. Aliases are of the form <base><num> where
317 * <num> is the sequence number. For example spi2 would be sequence number 2.
319 * @dev: device to look up
320 * @devnump: set to the sequence number if one is found
321 * @return 0 if a sequence was found, -ve if not
323 int dev_read_alias_seq(struct udevice
*dev
, int *devnump
);
326 * dev_read_u32_array() - Find and read an array of 32 bit integers
328 * Search for a property in a device node and read 32-bit value(s) from
331 * The out_values is modified only if a valid u32 value can be decoded.
333 * @dev: device to look up
334 * @propname: name of the property to read
335 * @out_values: pointer to return value, modified only if return value is 0
336 * @sz: number of array elements to read
337 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
338 * property does not have a value, and -EOVERFLOW if the property data isn't
341 int dev_read_u32_array(struct udevice
*dev
, const char *propname
,
342 u32
*out_values
, size_t sz
);
345 * dev_read_first_subnode() - find the first subnode of a device's node
347 * @dev: device to look up
348 * @return reference to the first subnode (which can be invalid if the device's
349 * node has no subnodes)
351 ofnode
dev_read_first_subnode(struct udevice
*dev
);
354 * ofnode_next_subnode() - find the next sibling of a subnode
356 * @node: valid reference to previous node (sibling)
357 * @return reference to the next subnode (which can be invalid if the node
358 * has no more siblings)
360 ofnode
dev_read_next_subnode(ofnode node
);
363 * dev_read_u8_array_ptr() - find an 8-bit array
365 * Look up a device's node property and return a pointer to its contents as a
366 * byte array of given length. The property must have at least enough data
367 * for the array (count bytes). It may have more, but this will be ignored.
368 * The data is not copied.
370 * @dev: device to look up
371 * @propname: name of property to find
372 * @sz: number of array elements
373 * @return pointer to byte array if found, or NULL if the property is not
374 * found or there is not enough data
376 const uint8_t *dev_read_u8_array_ptr(struct udevice
*dev
, const char *propname
,
380 * dev_read_enabled() - check whether a node is enabled
382 * This looks for a 'status' property. If this exists, then returns 1 if
383 * the status is 'ok' and 0 otherwise. If there is no status property,
384 * it returns 1 on the assumption that anything mentioned should be enabled
387 * @dev: device to examine
388 * @return integer value 0 (not enabled) or 1 (enabled)
390 int dev_read_enabled(struct udevice
*dev
);
393 * dev_read_resource() - obtain an indexed resource from a device.
395 * @dev: device to examine
396 * @index index of the resource to retrieve (0 = first)
397 * @res returns the resource
398 * @return 0 if ok, negative on error
400 int dev_read_resource(struct udevice
*dev
, uint index
, struct resource
*res
);
403 * dev_read_resource_byname() - obtain a named resource from a device.
405 * @dev: device to examine
406 * @name: name of the resource to retrieve
407 * @res: returns the resource
408 * @return 0 if ok, negative on error
410 int dev_read_resource_byname(struct udevice
*dev
, const char *name
,
411 struct resource
*res
);
413 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
415 static inline int dev_read_u32_default(struct udevice
*dev
,
416 const char *propname
, int def
)
418 return ofnode_read_u32_default(dev_ofnode(dev
), propname
, def
);
421 static inline const char *dev_read_string(struct udevice
*dev
,
422 const char *propname
)
424 return ofnode_read_string(dev_ofnode(dev
), propname
);
427 static inline bool dev_read_bool(struct udevice
*dev
, const char *propname
)
429 return ofnode_read_bool(dev_ofnode(dev
), propname
);
432 static inline ofnode
dev_read_subnode(struct udevice
*dev
,
433 const char *subbnode_name
)
435 return ofnode_find_subnode(dev_ofnode(dev
), subbnode_name
);
438 static inline int dev_read_size(struct udevice
*dev
, const char *propname
)
440 return ofnode_read_size(dev_ofnode(dev
), propname
);
443 static inline fdt_addr_t
dev_read_addr_index(struct udevice
*dev
, int index
)
445 return devfdt_get_addr_index(dev
, index
);
448 static inline fdt_addr_t
dev_read_addr(struct udevice
*dev
)
450 return devfdt_get_addr(dev
);
453 static inline void *dev_read_addr_ptr(struct udevice
*dev
)
455 return devfdt_get_addr_ptr(dev
);
458 static inline fdt_addr_t
dev_read_addr_size(struct udevice
*dev
,
459 const char *propname
,
462 return ofnode_get_addr_size(dev_ofnode(dev
), propname
, sizep
);
465 static inline const char *dev_read_name(struct udevice
*dev
)
467 return ofnode_get_name(dev_ofnode(dev
));
470 static inline int dev_read_stringlist_search(struct udevice
*dev
,
471 const char *propname
,
474 return ofnode_stringlist_search(dev_ofnode(dev
), propname
, string
);
477 static inline int dev_read_string_index(struct udevice
*dev
,
478 const char *propname
, int index
,
481 return ofnode_read_string_index(dev_ofnode(dev
), propname
, index
, outp
);
484 static inline int dev_read_string_count(struct udevice
*dev
,
485 const char *propname
)
487 return ofnode_read_string_count(dev_ofnode(dev
), propname
);
490 static inline int dev_read_phandle_with_args(struct udevice
*dev
,
491 const char *list_name
, const char *cells_name
, int cell_count
,
492 int index
, struct ofnode_phandle_args
*out_args
)
494 return ofnode_parse_phandle_with_args(dev_ofnode(dev
), list_name
,
495 cells_name
, cell_count
, index
,
499 static inline int dev_count_phandle_with_args(struct udevice
*dev
,
500 const char *list_name
, const char *cells_name
)
502 return ofnode_count_phandle_with_args(dev_ofnode(dev
), list_name
,
506 static inline int dev_read_addr_cells(struct udevice
*dev
)
508 /* NOTE: this call should walk up the parent stack */
509 return fdt_address_cells(gd
->fdt_blob
, dev_of_offset(dev
));
512 static inline int dev_read_size_cells(struct udevice
*dev
)
514 /* NOTE: this call should walk up the parent stack */
515 return fdt_size_cells(gd
->fdt_blob
, dev_of_offset(dev
));
518 static inline int dev_read_simple_addr_cells(struct udevice
*dev
)
520 return fdt_address_cells(gd
->fdt_blob
, dev_of_offset(dev
));
523 static inline int dev_read_simple_size_cells(struct udevice
*dev
)
525 return fdt_size_cells(gd
->fdt_blob
, dev_of_offset(dev
));
528 static inline int dev_read_phandle(struct udevice
*dev
)
530 return fdt_get_phandle(gd
->fdt_blob
, dev_of_offset(dev
));
533 static inline const void *dev_read_prop(struct udevice
*dev
,
534 const char *propname
, int *lenp
)
536 return ofnode_get_property(dev_ofnode(dev
), propname
, lenp
);
539 static inline int dev_read_alias_seq(struct udevice
*dev
, int *devnump
)
541 return fdtdec_get_alias_seq(gd
->fdt_blob
, dev
->uclass
->uc_drv
->name
,
542 dev_of_offset(dev
), devnump
);
545 static inline int dev_read_u32_array(struct udevice
*dev
, const char *propname
,
546 u32
*out_values
, size_t sz
)
548 return ofnode_read_u32_array(dev_ofnode(dev
), propname
, out_values
, sz
);
551 static inline ofnode
dev_read_first_subnode(struct udevice
*dev
)
553 return ofnode_first_subnode(dev_ofnode(dev
));
556 static inline ofnode
dev_read_next_subnode(ofnode node
)
558 return ofnode_next_subnode(node
);
561 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice
*dev
,
562 const char *propname
, size_t sz
)
564 return ofnode_read_u8_array_ptr(dev_ofnode(dev
), propname
, sz
);
567 static inline int dev_read_enabled(struct udevice
*dev
)
569 return fdtdec_get_is_enabled(gd
->fdt_blob
, dev_of_offset(dev
));
572 static inline int dev_read_resource(struct udevice
*dev
, uint index
,
573 struct resource
*res
)
575 return ofnode_read_resource(dev_ofnode(dev
), index
, res
);
578 static inline int dev_read_resource_byname(struct udevice
*dev
,
580 struct resource
*res
)
582 return ofnode_read_resource_byname(dev_ofnode(dev
), name
, res
);
585 #endif /* CONFIG_DM_DEV_READ_INLINE */
588 * dev_for_each_subnode() - Helper function to iterate through subnodes
590 * This creates a for() loop which works through the subnodes in a device's
593 * @subnode: ofnode holding the current subnode
594 * @dev: device to use for interation (struct udevice *)
596 #define dev_for_each_subnode(subnode, dev) \
597 for (subnode = dev_read_first_subnode(dev); \
598 ofnode_valid(subnode); \
599 subnode = ofnode_next_subnode(subnode))