SPDX: Convert all of our single license tags to Linux Kernel style
[platform/kernel/u-boot.git] / include / dm / read.h
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_read_addr() - Get the reg property of a device
117  *
118  * @dev: Device to read from
119  *
120  * @return address or FDT_ADDR_T_NONE if not found
121  */
122 fdt_addr_t dev_read_addr(struct udevice *dev);
123
124 /**
125  * dev_read_addr_ptr() - Get the reg property of a device
126  *                       as a pointer
127  *
128  * @dev: Device to read from
129  *
130  * @return pointer or NULL if not found
131  */
132 void *dev_read_addr_ptr(struct udevice *dev);
133
134 /**
135  * dev_read_addr_size() - get address and size from a device property
136  *
137  * This does no address translation. It simply reads an property that contains
138  * an address and a size value, one after the other.
139  *
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
144  */
145 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname,
146                                 fdt_size_t *sizep);
147
148 /**
149  * dev_read_name() - get the name of a device's node
150  *
151  * @node: valid node to look up
152  * @return name of node
153  */
154 const char *dev_read_name(struct udevice *dev);
155
156 /**
157  * dev_read_stringlist_search() - find string in a string list and return index
158  *
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
163  * the empty string.
164  *
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
168  *
169  * @return:
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
173  */
174 int dev_read_stringlist_search(struct udevice *dev, const char *property,
175                           const char *string);
176
177 /**
178  * dev_read_string_index() - obtain an indexed string from a string list
179  *
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
184  *
185  * @return:
186  *   length of string, if found or -ve error value if not found
187  */
188 int dev_read_string_index(struct udevice *dev, const char *propname, int index,
189                           const char **outp);
190
191 /**
192  * dev_read_string_count() - find the number of strings in a string list
193  *
194  * @dev: device to examine
195  * @propname: name of the property containing the string list
196  * @return:
197  *   number of strings in the list, or -ve error value if not found
198  */
199 int dev_read_string_count(struct udevice *dev, const char *propname);
200 /**
201  * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
202  *
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
205  * errno value.
206  *
207  * Caller is responsible to call of_node_put() on the returned out_args->np
208  * pointer.
209  *
210  * Example:
211  *
212  * phandle1: node1 {
213  *      #list-cells = <2>;
214  * }
215  *
216  * phandle2: node2 {
217  *      #list-cells = <1>;
218  * }
219  *
220  * node3 {
221  *      list = <&phandle1 1 2 &phandle2 3>;
222  * }
223  *
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);
226  *
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.
237  */
238 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name,
239                                 const char *cells_name, int cell_count,
240                                 int index,
241                                 struct ofnode_phandle_args *out_args);
242
243 /**
244  * dev_count_phandle_with_args() - Return phandle number in a list
245  *
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.
249  *
250  *
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
255  * errno value.
256  */
257
258 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name,
259                                 const char *cells_name);
260
261 /**
262  * dev_read_addr_cells() - Get the number of address cells for a device's node
263  *
264  * This walks back up the tree to find the closest #address-cells property
265  * which controls the given node.
266  *
267  * @dev: device to check
268  * @return number of address cells this node uses
269  */
270 int dev_read_addr_cells(struct udevice *dev);
271
272 /**
273  * dev_read_size_cells() - Get the number of size cells for a device's node
274  *
275  * This walks back up the tree to find the closest #size-cells property
276  * which controls the given node.
277  *
278  * @dev: device to check
279  * @return number of size cells this node uses
280  */
281 int dev_read_size_cells(struct udevice *dev);
282
283 /**
284  * dev_read_addr_cells() - Get the address cells property in a node
285  *
286  * This function matches fdt_address_cells().
287  *
288  * @dev: device to check
289  * @return number of address cells this node uses
290  */
291 int dev_read_simple_addr_cells(struct udevice *dev);
292
293 /**
294  * dev_read_size_cells() - Get the size cells property in a node
295  *
296  * This function matches fdt_size_cells().
297  *
298  * @dev: device to check
299  * @return number of size cells this node uses
300  */
301 int dev_read_simple_size_cells(struct udevice *dev);
302
303 /**
304  * dev_read_phandle() - Get the phandle from a device
305  *
306  * @dev: device to check
307  * @return phandle (1 or greater), or 0 if no phandle or other error
308  */
309 int dev_read_phandle(struct udevice *dev);
310
311 /**
312  * dev_read_prop()- - read a property from a device's node
313  *
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
318  */
319 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp);
320
321 /**
322  * dev_read_alias_seq() - Get the alias sequence number of a node
323  *
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.
327  *
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
331  */
332 int dev_read_alias_seq(struct udevice *dev, int *devnump);
333
334 /**
335  * dev_read_u32_array() - Find and read an array of 32 bit integers
336  *
337  * Search for a property in a device node and read 32-bit value(s) from
338  * it.
339  *
340  * The out_values is modified only if a valid u32 value can be decoded.
341  *
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
348  * large enough.
349  */
350 int dev_read_u32_array(struct udevice *dev, const char *propname,
351                        u32 *out_values, size_t sz);
352
353 /**
354  * dev_read_first_subnode() - find the first subnode of a device's node
355  *
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)
359  */
360 ofnode dev_read_first_subnode(struct udevice *dev);
361
362 /**
363  * ofnode_next_subnode() - find the next sibling of a subnode
364  *
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)
368  */
369 ofnode dev_read_next_subnode(ofnode node);
370
371 /**
372  * dev_read_u8_array_ptr() - find an 8-bit array
373  *
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.
378  *
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
384  */
385 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname,
386                                      size_t sz);
387
388 /**
389  * dev_read_enabled() - check whether a node is enabled
390  *
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
394  * by default.
395  *
396  * @dev: device to examine
397  * @return integer value 0 (not enabled) or 1 (enabled)
398  */
399 int dev_read_enabled(struct udevice *dev);
400
401 /**
402  * dev_read_resource() - obtain an indexed resource from a device.
403  *
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
408  */
409 int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
410
411 /**
412  * dev_read_resource_byname() - obtain a named resource from a device.
413  *
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
418  */
419 int dev_read_resource_byname(struct udevice *dev, const char *name,
420                              struct resource *res);
421
422 /**
423  * dev_translate_address() - Tranlate a device-tree address
424  *
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
427  * way.
428  *
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
432  */
433 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr);
434 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */
435
436 static inline int dev_read_u32(struct udevice *dev,
437                                const char *propname, u32 *outp)
438 {
439         return ofnode_read_u32(dev_ofnode(dev), propname, outp);
440 }
441
442 static inline int dev_read_u32_default(struct udevice *dev,
443                                        const char *propname, int def)
444 {
445         return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
446 }
447
448 static inline const char *dev_read_string(struct udevice *dev,
449                                           const char *propname)
450 {
451         return ofnode_read_string(dev_ofnode(dev), propname);
452 }
453
454 static inline bool dev_read_bool(struct udevice *dev, const char *propname)
455 {
456         return ofnode_read_bool(dev_ofnode(dev), propname);
457 }
458
459 static inline ofnode dev_read_subnode(struct udevice *dev,
460                                       const char *subbnode_name)
461 {
462         return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
463 }
464
465 static inline int dev_read_size(struct udevice *dev, const char *propname)
466 {
467         return ofnode_read_size(dev_ofnode(dev), propname);
468 }
469
470 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index)
471 {
472         return devfdt_get_addr_index(dev, index);
473 }
474
475 static inline fdt_addr_t dev_read_addr(struct udevice *dev)
476 {
477         return devfdt_get_addr(dev);
478 }
479
480 static inline void *dev_read_addr_ptr(struct udevice *dev)
481 {
482         return devfdt_get_addr_ptr(dev);
483 }
484
485 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev,
486                                             const char *propname,
487                                             fdt_size_t *sizep)
488 {
489         return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
490 }
491
492 static inline const char *dev_read_name(struct udevice *dev)
493 {
494         return ofnode_get_name(dev_ofnode(dev));
495 }
496
497 static inline int dev_read_stringlist_search(struct udevice *dev,
498                                              const char *propname,
499                                              const char *string)
500 {
501         return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
502 }
503
504 static inline int dev_read_string_index(struct udevice *dev,
505                                         const char *propname, int index,
506                                         const char **outp)
507 {
508         return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
509 }
510
511 static inline int dev_read_string_count(struct udevice *dev,
512                                         const char *propname)
513 {
514         return ofnode_read_string_count(dev_ofnode(dev), propname);
515 }
516
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)
520 {
521         return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
522                                               cells_name, cell_count, index,
523                                               out_args);
524 }
525
526 static inline int dev_count_phandle_with_args(struct udevice *dev,
527                 const char *list_name, const char *cells_name)
528 {
529         return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
530                                               cells_name);
531 }
532
533 static inline int dev_read_addr_cells(struct udevice *dev)
534 {
535         /* NOTE: this call should walk up the parent stack */
536         return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
537 }
538
539 static inline int dev_read_size_cells(struct udevice *dev)
540 {
541         /* NOTE: this call should walk up the parent stack */
542         return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
543 }
544
545 static inline int dev_read_simple_addr_cells(struct udevice *dev)
546 {
547         return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
548 }
549
550 static inline int dev_read_simple_size_cells(struct udevice *dev)
551 {
552         return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
553 }
554
555 static inline int dev_read_phandle(struct udevice *dev)
556 {
557         return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
558 }
559
560 static inline const void *dev_read_prop(struct udevice *dev,
561                                         const char *propname, int *lenp)
562 {
563         return ofnode_get_property(dev_ofnode(dev), propname, lenp);
564 }
565
566 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump)
567 {
568         return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
569                                     dev_of_offset(dev), devnump);
570 }
571
572 static inline int dev_read_u32_array(struct udevice *dev, const char *propname,
573                                      u32 *out_values, size_t sz)
574 {
575         return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
576 }
577
578 static inline ofnode dev_read_first_subnode(struct udevice *dev)
579 {
580         return ofnode_first_subnode(dev_ofnode(dev));
581 }
582
583 static inline ofnode dev_read_next_subnode(ofnode node)
584 {
585         return ofnode_next_subnode(node);
586 }
587
588 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev,
589                                         const char *propname, size_t sz)
590 {
591         return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
592 }
593
594 static inline int dev_read_enabled(struct udevice *dev)
595 {
596         return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
597 }
598
599 static inline int dev_read_resource(struct udevice *dev, uint index,
600                                     struct resource *res)
601 {
602         return ofnode_read_resource(dev_ofnode(dev), index, res);
603 }
604
605 static inline int dev_read_resource_byname(struct udevice *dev,
606                                            const char *name,
607                                            struct resource *res)
608 {
609         return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
610 }
611
612 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr)
613 {
614         return ofnode_translate_address(dev_ofnode(dev), in_addr);
615 }
616
617 #endif /* CONFIG_DM_DEV_READ_INLINE */
618
619 /**
620  * dev_for_each_subnode() - Helper function to iterate through subnodes
621  *
622  * This creates a for() loop which works through the subnodes in a device's
623  * device-tree node.
624  *
625  * @subnode: ofnode holding the current subnode
626  * @dev: device to use for interation (struct udevice *)
627  */
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))
632
633 #endif