xref: /openbmc/u-boot/include/dm/read.h (revision e3963c09)
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_name() - Get the reg property of a device, indexed by name
129  *
130  * @dev: Device to read from
131  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
132  *	  'reg-names' property providing named-based identification. @index
133  *	  indicates the value to search for in 'reg-names'.
134  *
135  * @return address or FDT_ADDR_T_NONE if not found
136  */
137 fdt_addr_t dev_read_addr_name(struct udevice *dev, const char* name);
138 
139 /**
140  * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
141  *                         as a memory-mapped I/O pointer
142  *
143  * @dev: Device to read from
144  * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
145  *	  'reg-names' property providing named-based identification. @index
146  *	  indicates the value to search for in 'reg-names'.
147  *
148  * @return pointer or NULL if not found
149  */
150 void *dev_remap_addr_name(struct udevice *dev, const char* name);
151 
152 /**
153  * dev_read_addr() - Get the reg property of a device
154  *
155  * @dev: Device to read from
156  *
157  * @return address or FDT_ADDR_T_NONE if not found
158  */
159 fdt_addr_t dev_read_addr(struct udevice *dev);
160 
161 /**
162  * dev_read_addr_ptr() - Get the reg property of a device
163  *                       as a pointer
164  *
165  * @dev: Device to read from
166  *
167  * @return pointer or NULL if not found
168  */
169 void *dev_read_addr_ptr(struct udevice *dev);
170 
171 /**
172  * dev_remap_addr() - Get the reg property of a device as a
173  *                         memory-mapped I/O pointer
174  *
175  * @dev: Device to read from
176  *
177  * @return pointer or NULL if not found
178  */
179 void *dev_remap_addr(struct udevice *dev);
180 
181 /**
182  * dev_read_addr_size() - get address and size from a device property
183  *
184  * This does no address translation. It simply reads an property that contains
185  * an address and a size value, one after the other.
186  *
187  * @dev: Device to read from
188  * @propname: property to read
189  * @sizep: place to put size value (on success)
190  * @return address value, or FDT_ADDR_T_NONE on error
191  */
192 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
193 				fdt_size_t *sizep);
194 
195 /**
196  * dev_read_name() - get the name of a device's node
197  *
198  * @node: valid node to look up
199  * @return name of node
200  */
201 const char *dev_read_name(struct udevice *dev);
202 
203 /**
204  * dev_read_stringlist_search() - find string in a string list and return index
205  *
206  * Note that it is possible for this function to succeed on property values
207  * that are not NUL-terminated. That's because the function will stop after
208  * finding the first occurrence of @string. This can for example happen with
209  * small-valued cell properties, such as #address-cells, when searching for
210  * the empty string.
211  *
212  * @dev: device to check
213  * @propname: name of the property containing the string list
214  * @string: string to look up in the string list
215  *
216  * @return:
217  *   the index of the string in the list of strings
218  *   -ENODATA if the property is not found
219  *   -EINVAL on some other error
220  */
221 int dev_read_stringlist_search(struct udevice *dev, const char *property,
222 			  const char *string);
223 
224 /**
225  * dev_read_string_index() - obtain an indexed string from a string list
226  *
227  * @dev: device to examine
228  * @propname: name of the property containing the string list
229  * @index: index of the string to return
230  * @out: return location for the string
231  *
232  * @return:
233  *   length of string, if found or -ve error value if not found
234  */
235 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
236 			  const char **outp);
237 
238 /**
239  * dev_read_string_count() - find the number of strings in a string list
240  *
241  * @dev: device to examine
242  * @propname: name of the property containing the string list
243  * @return:
244  *   number of strings in the list, or -ve error value if not found
245  */
246 int dev_read_string_count(struct udevice *dev, const char *propname);
247 /**
248  * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
249  *
250  * This function is useful to parse lists of phandles and their arguments.
251  * Returns 0 on success and fills out_args, on error returns appropriate
252  * errno value.
253  *
254  * Caller is responsible to call of_node_put() on the returned out_args->np
255  * pointer.
256  *
257  * Example:
258  *
259  * phandle1: node1 {
260  *	#list-cells = <2>;
261  * }
262  *
263  * phandle2: node2 {
264  *	#list-cells = <1>;
265  * }
266  *
267  * node3 {
268  *	list = <&phandle1 1 2 &phandle2 3>;
269  * }
270  *
271  * To get a device_node of the `node2' node you may call this:
272  * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
273  *
274  * @dev:	device whose node containing a list
275  * @list_name:	property name that contains a list
276  * @cells_name:	property name that specifies phandles' arguments count
277  * @cells_count: Cell count to use if @cells_name is NULL
278  * @index:	index of a phandle to parse out
279  * @out_args:	optional pointer to output arguments structure (will be filled)
280  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
281  *	@list_name does not exist, -EINVAL if a phandle was not found,
282  *	@cells_name could not be found, the arguments were truncated or there
283  *	were too many arguments.
284  */
285 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
286 				const char *cells_name, int cell_count,
287 				int index,
288 				struct ofnode_phandle_args *out_args);
289 
290 /**
291  * dev_count_phandle_with_args() - Return phandle number in a list
292  *
293  * This function is usefull to get phandle number contained in a property list.
294  * For example, this allows to allocate the right amount of memory to keep
295  * clock's reference contained into the "clocks" property.
296  *
297  *
298  * @dev:	device whose node containing a list
299  * @list_name:	property name that contains a list
300  * @cells_name:	property name that specifies phandles' arguments count
301  * @Returns number of phandle found on success, on error returns appropriate
302  * errno value.
303  */
304 
305 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
306 				const char *cells_name);
307 
308 /**
309  * dev_read_addr_cells() - Get the number of address cells for a device's node
310  *
311  * This walks back up the tree to find the closest #address-cells property
312  * which controls the given node.
313  *
314  * @dev: device to check
315  * @return number of address cells this node uses
316  */
317 int dev_read_addr_cells(struct udevice *dev);
318 
319 /**
320  * dev_read_size_cells() - Get the number of size cells for a device's node
321  *
322  * This walks back up the tree to find the closest #size-cells property
323  * which controls the given node.
324  *
325  * @dev: device to check
326  * @return number of size cells this node uses
327  */
328 int dev_read_size_cells(struct udevice *dev);
329 
330 /**
331  * dev_read_addr_cells() - Get the address cells property in a node
332  *
333  * This function matches fdt_address_cells().
334  *
335  * @dev: device to check
336  * @return number of address cells this node uses
337  */
338 int dev_read_simple_addr_cells(struct udevice *dev);
339 
340 /**
341  * dev_read_size_cells() - Get the size cells property in a node
342  *
343  * This function matches fdt_size_cells().
344  *
345  * @dev: device to check
346  * @return number of size cells this node uses
347  */
348 int dev_read_simple_size_cells(struct udevice *dev);
349 
350 /**
351  * dev_read_phandle() - Get the phandle from a device
352  *
353  * @dev: device to check
354  * @return phandle (1 or greater), or 0 if no phandle or other error
355  */
356 int dev_read_phandle(struct udevice *dev);
357 
358 /**
359  * dev_read_prop()- - read a property from a device's node
360  *
361  * @dev: device to check
362  * @propname: property to read
363  * @lenp: place to put length on success
364  * @return pointer to property, or NULL if not found
365  */
366 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
367 
368 /**
369  * dev_read_alias_seq() - Get the alias sequence number of a node
370  *
371  * This works out whether a node is pointed to by an alias, and if so, the
372  * sequence number of that alias. Aliases are of the form <base><num> where
373  * <num> is the sequence number. For example spi2 would be sequence number 2.
374  *
375  * @dev: device to look up
376  * @devnump: set to the sequence number if one is found
377  * @return 0 if a sequence was found, -ve if not
378  */
379 int dev_read_alias_seq(struct udevice *dev, int *devnump);
380 
381 /**
382  * dev_read_u32_array() - Find and read an array of 32 bit integers
383  *
384  * Search for a property in a device node and read 32-bit value(s) from
385  * it.
386  *
387  * The out_values is modified only if a valid u32 value can be decoded.
388  *
389  * @dev: device to look up
390  * @propname:	name of the property to read
391  * @out_values:	pointer to return value, modified only if return value is 0
392  * @sz:		number of array elements to read
393  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
394  * property does not have a value, and -EOVERFLOW if the property data isn't
395  * large enough.
396  */
397 int dev_read_u32_array(struct udevice *dev, const char *propname,
398 		       u32 *out_values, size_t sz);
399 
400 /**
401  * dev_read_first_subnode() - find the first subnode of a device's node
402  *
403  * @dev: device to look up
404  * @return reference to the first subnode (which can be invalid if the device's
405  * node has no subnodes)
406  */
407 ofnode dev_read_first_subnode(struct udevice *dev);
408 
409 /**
410  * ofnode_next_subnode() - find the next sibling of a subnode
411  *
412  * @node:	valid reference to previous node (sibling)
413  * @return reference to the next subnode (which can be invalid if the node
414  * has no more siblings)
415  */
416 ofnode dev_read_next_subnode(ofnode node);
417 
418 /**
419  * dev_read_u8_array_ptr() - find an 8-bit array
420  *
421  * Look up a device's node property and return a pointer to its contents as a
422  * byte array of given length. The property must have at least enough data
423  * for the array (count bytes). It may have more, but this will be ignored.
424  * The data is not copied.
425  *
426  * @dev: device to look up
427  * @propname: name of property to find
428  * @sz: number of array elements
429  * @return pointer to byte array if found, or NULL if the property is not
430  *		found or there is not enough data
431  */
432 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
433 				     size_t sz);
434 
435 /**
436  * dev_read_enabled() - check whether a node is enabled
437  *
438  * This looks for a 'status' property. If this exists, then returns 1 if
439  * the status is 'ok' and 0 otherwise. If there is no status property,
440  * it returns 1 on the assumption that anything mentioned should be enabled
441  * by default.
442  *
443  * @dev: device to examine
444  * @return integer value 0 (not enabled) or 1 (enabled)
445  */
446 int dev_read_enabled(struct udevice *dev);
447 
448 /**
449  * dev_read_resource() - obtain an indexed resource from a device.
450  *
451  * @dev: device to examine
452  * @index index of the resource to retrieve (0 = first)
453  * @res returns the resource
454  * @return 0 if ok, negative on error
455  */
456 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
457 
458 /**
459  * dev_read_resource_byname() - obtain a named resource from a device.
460  *
461  * @dev: device to examine
462  * @name: name of the resource to retrieve
463  * @res: returns the resource
464  * @return 0 if ok, negative on error
465  */
466 int dev_read_resource_byname(struct udevice *dev, const char *name,
467 			     struct resource *res);
468 
469 /**
470  * dev_translate_address() - Tranlate a device-tree address
471  *
472  * Translate an address from the device-tree into a CPU physical address.  This
473  * function walks up the tree and applies the various bus mappings along the
474  * way.
475  *
476  * @dev: device giving the context in which to translate the address
477  * @in_addr: pointer to the address to translate
478  * @return the translated address; OF_BAD_ADDR on error
479  */
480 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
481 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
482 
483 static inline int dev_read_u32(struct udevice *dev,
484 			       const char *propname, u32 *outp)
485 {
486 	return ofnode_read_u32(dev_ofnode(dev), propname, outp);
487 }
488 
489 static inline int dev_read_u32_default(struct udevice *dev,
490 				       const char *propname, int def)
491 {
492 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
493 }
494 
495 static inline const char *dev_read_string(struct udevice *dev,
496 					  const char *propname)
497 {
498 	return ofnode_read_string(dev_ofnode(dev), propname);
499 }
500 
501 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
502 {
503 	return ofnode_read_bool(dev_ofnode(dev), propname);
504 }
505 
506 static inline ofnode dev_read_subnode(struct udevice *dev,
507 				      const char *subbnode_name)
508 {
509 	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
510 }
511 
512 static inline int dev_read_size(struct udevice *dev, const char *propname)
513 {
514 	return ofnode_read_size(dev_ofnode(dev), propname);
515 }
516 
517 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
518 {
519 	return devfdt_get_addr_index(dev, index);
520 }
521 
522 static inline fdt_addr_t dev_read_addr_name(struct udevice *dev,
523 					    const char *name)
524 {
525 	return devfdt_get_addr_name(dev, name);
526 }
527 
528 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
529 {
530 	return devfdt_get_addr(dev);
531 }
532 
533 static inline void *dev_read_addr_ptr(struct udevice *dev)
534 {
535 	return devfdt_get_addr_ptr(dev);
536 }
537 
538 static inline void *dev_remap_addr(struct udevice *dev)
539 {
540 	return devfdt_remap_addr(dev);
541 }
542 
543 static inline void *dev_remap_addr_index(struct udevice *dev, int index)
544 {
545 	return devfdt_remap_addr_index(dev, index);
546 }
547 
548 static inline void *dev_remap_addr_name(struct udevice *dev, const char *name)
549 {
550 	return devfdt_remap_addr_name(dev, name);
551 }
552 
553 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
554 					    const char *propname,
555 					    fdt_size_t *sizep)
556 {
557 	return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
558 }
559 
560 static inline const char *dev_read_name(struct udevice *dev)
561 {
562 	return ofnode_get_name(dev_ofnode(dev));
563 }
564 
565 static inline int dev_read_stringlist_search(struct udevice *dev,
566 					     const char *propname,
567 					     const char *string)
568 {
569 	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
570 }
571 
572 static inline int dev_read_string_index(struct udevice *dev,
573 					const char *propname, int index,
574 					const char **outp)
575 {
576 	return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
577 }
578 
579 static inline int dev_read_string_count(struct udevice *dev,
580 					const char *propname)
581 {
582 	return ofnode_read_string_count(dev_ofnode(dev), propname);
583 }
584 
585 static inline int dev_read_phandle_with_args(struct udevice *dev,
586 		const char *list_name, const char *cells_name, int cell_count,
587 		int index, struct ofnode_phandle_args *out_args)
588 {
589 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
590 					      cells_name, cell_count, index,
591 					      out_args);
592 }
593 
594 static inline int dev_count_phandle_with_args(struct udevice *dev,
595 		const char *list_name, const char *cells_name)
596 {
597 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
598 					      cells_name);
599 }
600 
601 static inline int dev_read_addr_cells(struct udevice *dev)
602 {
603 	/* NOTE: this call should walk up the parent stack */
604 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
605 }
606 
607 static inline int dev_read_size_cells(struct udevice *dev)
608 {
609 	/* NOTE: this call should walk up the parent stack */
610 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
611 }
612 
613 static inline int dev_read_simple_addr_cells(struct udevice *dev)
614 {
615 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
616 }
617 
618 static inline int dev_read_simple_size_cells(struct udevice *dev)
619 {
620 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
621 }
622 
623 static inline int dev_read_phandle(struct udevice *dev)
624 {
625 	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
626 }
627 
628 static inline const void *dev_read_prop(struct udevice *dev,
629 					const char *propname, int *lenp)
630 {
631 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
632 }
633 
634 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
635 {
636 	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
637 				    dev_of_offset(dev), devnump);
638 }
639 
640 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
641 				     u32 *out_values, size_t sz)
642 {
643 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
644 }
645 
646 static inline ofnode dev_read_first_subnode(struct udevice *dev)
647 {
648 	return ofnode_first_subnode(dev_ofnode(dev));
649 }
650 
651 static inline ofnode dev_read_next_subnode(ofnode node)
652 {
653 	return ofnode_next_subnode(node);
654 }
655 
656 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
657 					const char *propname, size_t sz)
658 {
659 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
660 }
661 
662 static inline int dev_read_enabled(struct udevice *dev)
663 {
664 	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
665 }
666 
667 static inline int dev_read_resource(struct udevice *dev, uint index,
668 				    struct resource *res)
669 {
670 	return ofnode_read_resource(dev_ofnode(dev), index, res);
671 }
672 
673 static inline int dev_read_resource_byname(struct udevice *dev,
674 					   const char *name,
675 					   struct resource *res)
676 {
677 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
678 }
679 
680 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
681 {
682 	return ofnode_translate_address(dev_ofnode(dev), in_addr);
683 }
684 
685 #endif /* CONFIG_DM_DEV_READ_INLINE */
686 
687 /**
688  * dev_for_each_subnode() - Helper function to iterate through subnodes
689  *
690  * This creates a for() loop which works through the subnodes in a device's
691  * device-tree node.
692  *
693  * @subnode: ofnode holding the current subnode
694  * @dev: device to use for interation (struct udevice *)
695  */
696 #define dev_for_each_subnode(subnode, dev) \
697 	for (subnode = dev_read_first_subnode(dev); \
698 	     ofnode_valid(subnode); \
699 	     subnode = ofnode_next_subnode(subnode))
700 
701 #endif
702