xref: /openbmc/u-boot/include/dm/read.h (revision 60d7c509)
1 /*
2  * Function to read values from the device tree node attached to a udevice.
3  *
4  * Copyright (c) 2017 Google, Inc
5  * Written by Simon Glass <sjg@chromium.org>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #ifndef _DM_READ_H
11 #define _DM_READ_H
12 
13 #include <dm/fdtaddr.h>
14 #include <dm/ofnode.h>
15 #include <dm/uclass.h>
16 
17 struct resource;
18 
19 #if CONFIG_IS_ENABLED(OF_LIVE)
20 static inline const struct device_node *dev_np(struct udevice *dev)
21 {
22 	return ofnode_to_np(dev->node);
23 }
24 #else
25 static inline const struct device_node *dev_np(struct udevice *dev)
26 {
27 	return NULL;
28 }
29 #endif
30 
31 /**
32  * dev_ofnode() - get the DT node reference associated with a udevice
33  *
34  * @dev:	device to check
35  * @return reference of the the device's DT node
36  */
37 static inline ofnode dev_ofnode(struct udevice *dev)
38 {
39 	return dev->node;
40 }
41 
42 static inline bool dev_of_valid(struct udevice *dev)
43 {
44 	return ofnode_valid(dev_ofnode(dev));
45 }
46 
47 #ifndef CONFIG_DM_DEV_READ_INLINE
48 /**
49  * dev_read_u32_default() - read a 32-bit integer from a device's DT property
50  *
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
55  */
56 int dev_read_u32_default(struct udevice *dev, const char *propname, int def);
57 
58 /**
59  * dev_read_string() - Read a string from a device's DT property
60  *
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
64  */
65 const char *dev_read_string(struct udevice *dev, const char *propname);
66 
67 /**
68  * dev_read_bool() - read a boolean value from a device's DT property
69  *
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
73  */
74 bool dev_read_bool(struct udevice *dev, const char *propname);
75 
76 /**
77  * dev_read_subnode() - find a named subnode of a device
78  *
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
82  * subnode)
83  */
84 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name);
85 
86 /**
87  * dev_read_size() - read the size of a property
88  *
89  * @dev: device to check
90  * @propname: property to check
91  * @return size of property if present, or -EINVAL if not
92  */
93 int dev_read_size(struct udevice *dev, const char *propname);
94 
95 /**
96  * dev_read_addr_index() - Get the indexed reg property of a device
97  *
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
101  *
102  * @return address or FDT_ADDR_T_NONE if not found
103  */
104 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index);
105 
106 /**
107  * dev_read_addr() - Get the reg property of a device
108  *
109  * @dev: Device to read from
110  *
111  * @return address or FDT_ADDR_T_NONE if not found
112  */
113 fdt_addr_t dev_read_addr(struct udevice *dev);
114 
115 /**
116  * dev_read_addr_ptr() - Get the reg property of a device
117  *                       as a pointer
118  *
119  * @dev: Device to read from
120  *
121  * @return pointer or NULL if not found
122  */
123 void *dev_read_addr_ptr(struct udevice *dev);
124 
125 /**
126  * dev_read_addr_size() - get address and size from a device property
127  *
128  * This does no address translation. It simply reads an property that contains
129  * an address and a size value, one after the other.
130  *
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
135  */
136 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
137 				fdt_size_t *sizep);
138 
139 /**
140  * dev_read_name() - get the name of a device's node
141  *
142  * @node: valid node to look up
143  * @return name of node
144  */
145 const char *dev_read_name(struct udevice *dev);
146 
147 /**
148  * dev_read_stringlist_search() - find string in a string list and return index
149  *
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
154  * the empty string.
155  *
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
159  *
160  * @return:
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
164  */
165 int dev_read_stringlist_search(struct udevice *dev, const char *property,
166 			  const char *string);
167 
168 /**
169  * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
170  *
171  * This function is useful to parse lists of phandles and their arguments.
172  * Returns 0 on success and fills out_args, on error returns appropriate
173  * errno value.
174  *
175  * Caller is responsible to call of_node_put() on the returned out_args->np
176  * pointer.
177  *
178  * Example:
179  *
180  * phandle1: node1 {
181  *	#list-cells = <2>;
182  * }
183  *
184  * phandle2: node2 {
185  *	#list-cells = <1>;
186  * }
187  *
188  * node3 {
189  *	list = <&phandle1 1 2 &phandle2 3>;
190  * }
191  *
192  * To get a device_node of the `node2' node you may call this:
193  * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
194  *
195  * @dev:	device whose node containing a list
196  * @list_name:	property name that contains a list
197  * @cells_name:	property name that specifies phandles' arguments count
198  * @cells_count: Cell count to use if @cells_name is NULL
199  * @index:	index of a phandle to parse out
200  * @out_args:	optional pointer to output arguments structure (will be filled)
201  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
202  *	@list_name does not exist, -EINVAL if a phandle was not found,
203  *	@cells_name could not be found, the arguments were truncated or there
204  *	were too many arguments.
205  */
206 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
207 				const char *cells_name, int cell_count,
208 				int index,
209 				struct ofnode_phandle_args *out_args);
210 
211 /**
212  * dev_count_phandle_with_args() - Return phandle number in a list
213  *
214  * This function is usefull to get phandle number contained in a property list.
215  * For example, this allows to allocate the right amount of memory to keep
216  * clock's reference contained into the "clocks" property.
217  *
218  *
219  * @dev:	device whose node containing a list
220  * @list_name:	property name that contains a list
221  * @cells_name:	property name that specifies phandles' arguments count
222  * @Returns number of phandle found on success, on error returns appropriate
223  * errno value.
224  */
225 
226 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
227 				const char *cells_name);
228 
229 /**
230  * dev_read_addr_cells() - Get the number of address cells for a device's node
231  *
232  * This walks back up the tree to find the closest #address-cells property
233  * which controls the given node.
234  *
235  * @dev: devioe to check
236  * @return number of address cells this node uses
237  */
238 int dev_read_addr_cells(struct udevice *dev);
239 
240 /**
241  * dev_read_size_cells() - Get the number of size cells for a device's node
242  *
243  * This walks back up the tree to find the closest #size-cells property
244  * which controls the given node.
245  *
246  * @dev: devioe to check
247  * @return number of size cells this node uses
248  */
249 int dev_read_size_cells(struct udevice *dev);
250 
251 /**
252  * dev_read_addr_cells() - Get the address cells property in a node
253  *
254  * This function matches fdt_address_cells().
255  *
256  * @dev: devioe to check
257  * @return number of address cells this node uses
258  */
259 int dev_read_simple_addr_cells(struct udevice *dev);
260 
261 /**
262  * dev_read_size_cells() - Get the size cells property in a node
263  *
264  * This function matches fdt_size_cells().
265  *
266  * @dev: devioe to check
267  * @return number of size cells this node uses
268  */
269 int dev_read_simple_size_cells(struct udevice *dev);
270 
271 /**
272  * dev_read_phandle() - Get the phandle from a device
273  *
274  * @dev: device to check
275  * @return phandle (1 or greater), or 0 if no phandle or other error
276  */
277 int dev_read_phandle(struct udevice *dev);
278 
279 /**
280  * dev_read_prop()- - read a property from a device's node
281  *
282  * @dev: device to check
283  * @propname: property to read
284  * @lenp: place to put length on success
285  * @return pointer to property, or NULL if not found
286  */
287 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
288 
289 /**
290  * dev_read_alias_seq() - Get the alias sequence number of a node
291  *
292  * This works out whether a node is pointed to by an alias, and if so, the
293  * sequence number of that alias. Aliases are of the form <base><num> where
294  * <num> is the sequence number. For example spi2 would be sequence number 2.
295  *
296  * @dev: device to look up
297  * @devnump: set to the sequence number if one is found
298  * @return 0 if a sequence was found, -ve if not
299  */
300 int dev_read_alias_seq(struct udevice *dev, int *devnump);
301 
302 /**
303  * dev_read_u32_array() - Find and read an array of 32 bit integers
304  *
305  * Search for a property in a device node and read 32-bit value(s) from
306  * it.
307  *
308  * The out_values is modified only if a valid u32 value can be decoded.
309  *
310  * @dev: device to look up
311  * @propname:	name of the property to read
312  * @out_values:	pointer to return value, modified only if return value is 0
313  * @sz:		number of array elements to read
314  * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if
315  * property does not have a value, and -EOVERFLOW if the property data isn't
316  * large enough.
317  */
318 int dev_read_u32_array(struct udevice *dev, const char *propname,
319 		       u32 *out_values, size_t sz);
320 
321 /**
322  * dev_read_first_subnode() - find the first subnode of a device's node
323  *
324  * @dev: device to look up
325  * @return reference to the first subnode (which can be invalid if the device's
326  * node has no subnodes)
327  */
328 ofnode dev_read_first_subnode(struct udevice *dev);
329 
330 /**
331  * ofnode_next_subnode() - find the next sibling of a subnode
332  *
333  * @node:	valid reference to previous node (sibling)
334  * @return reference to the next subnode (which can be invalid if the node
335  * has no more siblings)
336  */
337 ofnode dev_read_next_subnode(ofnode node);
338 
339 /**
340  * dev_read_u8_array_ptr() - find an 8-bit array
341  *
342  * Look up a device's node property and return a pointer to its contents as a
343  * byte array of given length. The property must have at least enough data
344  * for the array (count bytes). It may have more, but this will be ignored.
345  * The data is not copied.
346  *
347  * @dev: device to look up
348  * @propname: name of property to find
349  * @sz: number of array elements
350  * @return pointer to byte array if found, or NULL if the property is not
351  *		found or there is not enough data
352  */
353 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
354 				     size_t sz);
355 
356 /**
357  * dev_read_enabled() - check whether a node is enabled
358  *
359  * This looks for a 'status' property. If this exists, then returns 1 if
360  * the status is 'ok' and 0 otherwise. If there is no status property,
361  * it returns 1 on the assumption that anything mentioned should be enabled
362  * by default.
363  *
364  * @dev: device to examine
365  * @return integer value 0 (not enabled) or 1 (enabled)
366  */
367 int dev_read_enabled(struct udevice *dev);
368 
369 /**
370  * dev_read_resource() - obtain an indexed resource from a device.
371  *
372  * @dev: device to examine
373  * @index index of the resource to retrieve (0 = first)
374  * @res returns the resource
375  * @return 0 if ok, negative on error
376  */
377 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
378 
379 /**
380  * dev_read_resource_byname() - obtain a named resource from a device.
381  *
382  * @dev: device to examine
383  * @name: name of the resource to retrieve
384  * @res: returns the resource
385  * @return 0 if ok, negative on error
386  */
387 int dev_read_resource_byname(struct udevice *dev, const char *name,
388 			     struct resource *res);
389 
390 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
391 
392 static inline int dev_read_u32_default(struct udevice *dev,
393 				       const char *propname, int def)
394 {
395 	return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
396 }
397 
398 static inline const char *dev_read_string(struct udevice *dev,
399 					  const char *propname)
400 {
401 	return ofnode_read_string(dev_ofnode(dev), propname);
402 }
403 
404 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
405 {
406 	return ofnode_read_bool(dev_ofnode(dev), propname);
407 }
408 
409 static inline ofnode dev_read_subnode(struct udevice *dev,
410 				      const char *subbnode_name)
411 {
412 	return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
413 }
414 
415 static inline int dev_read_size(struct udevice *dev, const char *propname)
416 {
417 	return ofnode_read_size(dev_ofnode(dev), propname);
418 }
419 
420 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
421 {
422 	return devfdt_get_addr_index(dev, index);
423 }
424 
425 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
426 {
427 	return devfdt_get_addr(dev);
428 }
429 
430 static inline void *dev_read_addr_ptr(struct udevice *dev)
431 {
432 	return devfdt_get_addr_ptr(dev);
433 }
434 
435 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
436 					    const char *propname,
437 					    fdt_size_t *sizep)
438 {
439 	return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
440 }
441 
442 static inline const char *dev_read_name(struct udevice *dev)
443 {
444 	return ofnode_get_name(dev_ofnode(dev));
445 }
446 
447 static inline int dev_read_stringlist_search(struct udevice *dev,
448 					     const char *propname,
449 					     const char *string)
450 {
451 	return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
452 }
453 
454 static inline int dev_read_phandle_with_args(struct udevice *dev,
455 		const char *list_name, const char *cells_name, int cell_count,
456 		int index, struct ofnode_phandle_args *out_args)
457 {
458 	return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
459 					      cells_name, cell_count, index,
460 					      out_args);
461 }
462 
463 static inline int dev_count_phandle_with_args(struct udevice *dev,
464 		const char *list_name, const char *cells_name)
465 {
466 	return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
467 					      cells_name);
468 }
469 
470 static inline int dev_read_addr_cells(struct udevice *dev)
471 {
472 	/* NOTE: this call should walk up the parent stack */
473 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
474 }
475 
476 static inline int dev_read_size_cells(struct udevice *dev)
477 {
478 	/* NOTE: this call should walk up the parent stack */
479 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
480 }
481 
482 static inline int dev_read_simple_addr_cells(struct udevice *dev)
483 {
484 	return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
485 }
486 
487 static inline int dev_read_simple_size_cells(struct udevice *dev)
488 {
489 	return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
490 }
491 
492 static inline int dev_read_phandle(struct udevice *dev)
493 {
494 	return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
495 }
496 
497 static inline const void *dev_read_prop(struct udevice *dev,
498 					const char *propname, int *lenp)
499 {
500 	return ofnode_get_property(dev_ofnode(dev), propname, lenp);
501 }
502 
503 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
504 {
505 	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
506 				    dev_of_offset(dev), devnump);
507 }
508 
509 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
510 				     u32 *out_values, size_t sz)
511 {
512 	return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
513 }
514 
515 static inline ofnode dev_read_first_subnode(struct udevice *dev)
516 {
517 	return ofnode_first_subnode(dev_ofnode(dev));
518 }
519 
520 static inline ofnode dev_read_next_subnode(ofnode node)
521 {
522 	return ofnode_next_subnode(node);
523 }
524 
525 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
526 					const char *propname, size_t sz)
527 {
528 	return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
529 }
530 
531 static inline int dev_read_enabled(struct udevice *dev)
532 {
533 	return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
534 }
535 
536 static inline int dev_read_resource(struct udevice *dev, uint index,
537 				    struct resource *res)
538 {
539 	return ofnode_read_resource(dev_ofnode(dev), index, res);
540 }
541 
542 static inline int dev_read_resource_byname(struct udevice *dev,
543 					   const char *name,
544 					   struct resource *res)
545 {
546 	return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
547 }
548 
549 #endif /* CONFIG_DM_DEV_READ_INLINE */
550 
551 /**
552  * dev_for_each_subnode() - Helper function to iterate through subnodes
553  *
554  * This creates a for() loop which works through the subnodes in a device's
555  * device-tree node.
556  *
557  * @subnode: ofnode holding the current subnode
558  * @dev: device to use for interation (struct udevice *)
559  */
560 #define dev_for_each_subnode(subnode, dev) \
561 	for (subnode = dev_read_first_subnode(dev); \
562 	     ofnode_valid(subnode); \
563 	     subnode = ofnode_next_subnode(subnode))
564 
565 #endif
566