xref: /openbmc/u-boot/include/dm/read.h (revision afaea1f5)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Function to read values from the device tree node attached to a udevice.
4  *
5  * Copyright (c) 2017 Google, Inc
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8 
9 #ifndef _DM_READ_H
10 #define _DM_READ_H
11 
12 #include <dm/fdtaddr.h>
13 #include <dm/ofnode.h>
14 #include <dm/uclass.h>
15 
16 struct resource;
17 
18 #if CONFIG_IS_ENABLED(OF_LIVE)
19 static inline const struct device_node *dev_np(struct udevice *dev)
20 {
21 	return ofnode_to_np(dev->node);
22 }
23 #else
24 static inline const struct device_node *dev_np(struct udevice *dev)
25 {
26 	return NULL;
27 }
28 #endif
29 
30 /**
31  * dev_ofnode() - get the DT node reference associated with a udevice
32  *
33  * @dev:	device to check
34  * @return reference of the the device's DT node
35  */
36 static inline ofnode dev_ofnode(struct udevice *dev)
37 {
38 	return dev->node;
39 }
40 
41 static inline bool dev_of_valid(struct udevice *dev)
42 {
43 	return ofnode_valid(dev_ofnode(dev));
44 }
45 
46 #ifndef CONFIG_DM_DEV_READ_INLINE
47 /**
48  * dev_read_u32() - read a 32-bit integer from a device's DT property
49  *
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
54  */
55 int dev_read_u32(struct udevice *dev, const char *propname, u32 *outp);
56 
57 /**
58  * dev_read_u32_default() - read a 32-bit integer from a device's DT property
59  *
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
64  */
65 int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
66 
67 /**
68  * dev_read_string() - Read a string from a device's DT property
69  *
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
73  */
74 const char *dev_read_string(struct udevice *dev, const char *propname);
75 
76 /**
77  * dev_read_bool() - read a boolean value from a device's DT property
78  *
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
82  */
83 bool dev_read_bool(struct udevice *dev, const char *propname);
84 
85 /**
86  * dev_read_subnode() - find a named subnode of a device
87  *
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
91  * subnode)
92  */
93 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
94 
95 /**
96  * dev_read_size() - read the size of a property
97  *
98  * @dev: device to check
99  * @propname: property to check
100  * @return size of property if present, or -EINVAL if not
101  */
102 int dev_read_size(struct udevice *dev, const char *propname);
103 
104 /**
105  * dev_read_addr_index() - Get the indexed reg property of a device
106  *
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
110  *
111  * @return address or FDT_ADDR_T_NONE if not found
112  */
113 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
114 
115 /**
116  * dev_remap_addr_index() - Get the indexed reg property of a device
117  *                               as a memory-mapped I/O pointer
118  *
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
122  *
123  * @return pointer or NULL if not found
124  */
125 void *dev_remap_addr_index(struct udevice *dev, int index);
126 
127 /**
128  * dev_read_addr() - Get the reg property of a device
129  *
130  * @dev: Device to read from
131  *
132  * @return address or FDT_ADDR_T_NONE if not found
133  */
134 fdt_addr_t dev_read_addr(struct udevice *dev);
135 
136 /**
137  * dev_read_addr_ptr() - Get the reg property of a device
138  *                       as a pointer
139  *
140  * @dev: Device to read from
141  *
142  * @return pointer or NULL if not found
143  */
144 void *dev_read_addr_ptr(struct udevice *dev);
145 
146 /**
147  * dev_remap_addr() - Get the reg property of a device as a
148  *                         memory-mapped I/O pointer
149  *
150  * @dev: Device to read from
151  *
152  * @return pointer or NULL if not found
153  */
154 void *dev_remap_addr(struct udevice *dev);
155 
156 /**
157  * dev_read_addr_size() - get address and size from a device property
158  *
159  * This does no address translation. It simply reads an property that contains
160  * an address and a size value, one after the other.
161  *
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
166  */
167 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
168 				fdt_size_t *sizep);
169 
170 /**
171  * dev_read_name() - get the name of a device's node
172  *
173  * @node: valid node to look up
174  * @return name of node
175  */
176 const char *dev_read_name(struct udevice *dev);
177 
178 /**
179  * dev_read_stringlist_search() - find string in a string list and return index
180  *
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
185  * the empty string.
186  *
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
190  *
191  * @return:
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
195  */
196 int dev_read_stringlist_search(struct udevice *dev, const char *property,
197 			  const char *string);
198 
199 /**
200  * dev_read_string_index() - obtain an indexed string from a string list
201  *
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
206  *
207  * @return:
208  *   length of string, if found or -ve error value if not found
209  */
210 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
211 			  const char **outp);
212 
213 /**
214  * dev_read_string_count() - find the number of strings in a string list
215  *
216  * @dev: device to examine
217  * @propname: name of the property containing the string list
218  * @return:
219  *   number of strings in the list, or -ve error value if not found
220  */
221 int dev_read_string_count(struct udevice *dev, const char *propname);
222 /**
223  * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
224  *
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
227  * errno value.
228  *
229  * Caller is responsible to call of_node_put() on the returned out_args->np
230  * pointer.
231  *
232  * Example:
233  *
234  * phandle1: node1 {
235  *	#list-cells = <2>;
236  * }
237  *
238  * phandle2: node2 {
239  *	#list-cells = <1>;
240  * }
241  *
242  * node3 {
243  *	list = <&phandle1 1 2 &phandle2 3>;
244  * }
245  *
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);
248  *
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.
259  */
260 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
261 				const char *cells_name, int cell_count,
262 				int index,
263 				struct ofnode_phandle_args *out_args);
264 
265 /**
266  * dev_count_phandle_with_args() - Return phandle number in a list
267  *
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.
271  *
272  *
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
277  * errno value.
278  */
279 
280 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
281 				const char *cells_name);
282 
283 /**
284  * dev_read_addr_cells() - Get the number of address cells for a device's node
285  *
286  * This walks back up the tree to find the closest #address-cells property
287  * which controls the given node.
288  *
289  * @dev: device to check
290  * @return number of address cells this node uses
291  */
292 int dev_read_addr_cells(struct udevice *dev);
293 
294 /**
295  * dev_read_size_cells() - Get the number of size cells for a device's node
296  *
297  * This walks back up the tree to find the closest #size-cells property
298  * which controls the given node.
299  *
300  * @dev: device to check
301  * @return number of size cells this node uses
302  */
303 int dev_read_size_cells(struct udevice *dev);
304 
305 /**
306  * dev_read_addr_cells() - Get the address cells property in a node
307  *
308  * This function matches fdt_address_cells().
309  *
310  * @dev: device to check
311  * @return number of address cells this node uses
312  */
313 int dev_read_simple_addr_cells(struct udevice *dev);
314 
315 /**
316  * dev_read_size_cells() - Get the size cells property in a node
317  *
318  * This function matches fdt_size_cells().
319  *
320  * @dev: device to check
321  * @return number of size cells this node uses
322  */
323 int dev_read_simple_size_cells(struct udevice *dev);
324 
325 /**
326  * dev_read_phandle() - Get the phandle from a device
327  *
328  * @dev: device to check
329  * @return phandle (1 or greater), or 0 if no phandle or other error
330  */
331 int dev_read_phandle(struct udevice *dev);
332 
333 /**
334  * dev_read_prop()- - read a property from a device's node
335  *
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
340  */
341 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
342 
343 /**
344  * dev_read_alias_seq() - Get the alias sequence number of a node
345  *
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.
349  *
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
353  */
354 int dev_read_alias_seq(struct udevice *dev, int *devnump);
355 
356 /**
357  * dev_read_u32_array() - Find and read an array of 32 bit integers
358  *
359  * Search for a property in a device node and read 32-bit value(s) from
360  * it.
361  *
362  * The out_values is modified only if a valid u32 value can be decoded.
363  *
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
370  * large enough.
371  */
372 int dev_read_u32_array(struct udevice *dev, const char *propname,
373 		       u32 *out_values, size_t sz);
374 
375 /**
376  * dev_read_first_subnode() - find the first subnode of a device's node
377  *
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)
381  */
382 ofnode dev_read_first_subnode(struct udevice *dev);
383 
384 /**
385  * ofnode_next_subnode() - find the next sibling of a subnode
386  *
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)
390  */
391 ofnode dev_read_next_subnode(ofnode node);
392 
393 /**
394  * dev_read_u8_array_ptr() - find an 8-bit array
395  *
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.
400  *
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
406  */
407 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
408 				     size_t sz);
409 
410 /**
411  * dev_read_enabled() - check whether a node is enabled
412  *
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
416  * by default.
417  *
418  * @dev: device to examine
419  * @return integer value 0 (not enabled) or 1 (enabled)
420  */
421 int dev_read_enabled(struct udevice *dev);
422 
423 /**
424  * dev_read_resource() - obtain an indexed resource from a device.
425  *
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
430  */
431 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
432 
433 /**
434  * dev_read_resource_byname() - obtain a named resource from a device.
435  *
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
440  */
441 int dev_read_resource_byname(struct udevice *dev, const char *name,
442 			     struct resource *res);
443 
444 /**
445  * dev_translate_address() - Tranlate a device-tree address
446  *
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
449  * way.
450  *
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
454  */
455 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
456 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
457 
458 static inline int dev_read_u32(struct udevice *dev,
459 			       const char *propname, u32 *outp)
460 {
461 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
462 }
463 
464 static inline int dev_read_u32_default(struct udevice *dev,
465 				       const char *propname, int def)
466 {
467 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
468 }
469 
470 static inline const char *dev_read_string(struct udevice *dev,
471 					  const char *propname)
472 {
473 	return ofnode_read_string(dev_ofnode(dev), propname);
474 }
475 
476 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
477 {
478 	return ofnode_read_bool(dev_ofnode(dev), propname);
479 }
480 
481 static inline ofnode dev_read_subnode(struct udevice *dev,
482 				      const char *subbnode_name)
483 {
484 	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
485 }
486 
487 static inline int dev_read_size(struct udevice *dev, const char *propname)
488 {
489 	return ofnode_read_size(dev_ofnode(dev), propname);
490 }
491 
492 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
493 {
494 	return devfdt_get_addr_index(dev, index);
495 }
496 
497 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
498 {
499 	return devfdt_get_addr(dev);
500 }
501 
502 static inline void *dev_read_addr_ptr(struct udevice *dev)
503 {
504 	return devfdt_get_addr_ptr(dev);
505 }
506 
507 static inline void *dev_remap_addr(struct udevice *dev)
508 {
509 	return devfdt_remap_addr(dev);
510 }
511 
512 static inline void *dev_remap_addr_index(struct udevice *dev, int index)
513 {
514 	return devfdt_remap_addr_index(dev, index);
515 }
516 
517 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
518 					    const char *propname,
519 					    fdt_size_t *sizep)
520 {
521 	return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
522 }
523 
524 static inline const char *dev_read_name(struct udevice *dev)
525 {
526 	return ofnode_get_name(dev_ofnode(dev));
527 }
528 
529 static inline int dev_read_stringlist_search(struct udevice *dev,
530 					     const char *propname,
531 					     const char *string)
532 {
533 	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
534 }
535 
536 static inline int dev_read_string_index(struct udevice *dev,
537 					const char *propname, int index,
538 					const char **outp)
539 {
540 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
541 }
542 
543 static inline int dev_read_string_count(struct udevice *dev,
544 					const char *propname)
545 {
546 	return ofnode_read_string_count(dev_ofnode(dev), propname);
547 }
548 
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)
552 {
553 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
554 					      cells_name, cell_count, index,
555 					      out_args);
556 }
557 
558 static inline int dev_count_phandle_with_args(struct udevice *dev,
559 		const char *list_name, const char *cells_name)
560 {
561 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
562 					      cells_name);
563 }
564 
565 static inline int dev_read_addr_cells(struct udevice *dev)
566 {
567 	/* NOTE: this call should walk up the parent stack */
568 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
569 }
570 
571 static inline int dev_read_size_cells(struct udevice *dev)
572 {
573 	/* NOTE: this call should walk up the parent stack */
574 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
575 }
576 
577 static inline int dev_read_simple_addr_cells(struct udevice *dev)
578 {
579 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
580 }
581 
582 static inline int dev_read_simple_size_cells(struct udevice *dev)
583 {
584 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
585 }
586 
587 static inline int dev_read_phandle(struct udevice *dev)
588 {
589 	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
590 }
591 
592 static inline const void *dev_read_prop(struct udevice *dev,
593 					const char *propname, int *lenp)
594 {
595 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
596 }
597 
598 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
599 {
600 	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
601 				    dev_of_offset(dev), devnump);
602 }
603 
604 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
605 				     u32 *out_values, size_t sz)
606 {
607 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
608 }
609 
610 static inline ofnode dev_read_first_subnode(struct udevice *dev)
611 {
612 	return ofnode_first_subnode(dev_ofnode(dev));
613 }
614 
615 static inline ofnode dev_read_next_subnode(ofnode node)
616 {
617 	return ofnode_next_subnode(node);
618 }
619 
620 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
621 					const char *propname, size_t sz)
622 {
623 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
624 }
625 
626 static inline int dev_read_enabled(struct udevice *dev)
627 {
628 	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
629 }
630 
631 static inline int dev_read_resource(struct udevice *dev, uint index,
632 				    struct resource *res)
633 {
634 	return ofnode_read_resource(dev_ofnode(dev), index, res);
635 }
636 
637 static inline int dev_read_resource_byname(struct udevice *dev,
638 					   const char *name,
639 					   struct resource *res)
640 {
641 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
642 }
643 
644 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
645 {
646 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
647 }
648 
649 #endif /* CONFIG_DM_DEV_READ_INLINE */
650 
651 /**
652  * dev_for_each_subnode() - Helper function to iterate through subnodes
653  *
654  * This creates a for() loop which works through the subnodes in a device's
655  * device-tree node.
656  *
657  * @subnode: ofnode holding the current subnode
658  * @dev: device to use for interation (struct udevice *)
659  */
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))
664 
665 #endif
666