fa72bb9effbb9526ecb6bf90e6a5254ef80a9079
[platform/kernel/u-boot.git] / include / dm / ofnode.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2017 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #ifndef _DM_OFNODE_H
8 #define _DM_OFNODE_H
9
10 /* TODO(sjg@chromium.org): Drop fdtdec.h include */
11 #include <fdtdec.h>
12 #include <dm/of.h>
13 #include <dm/of_access.h>
14 #include <log.h>
15 #include <phy_interface.h>
16
17 /* Enable checks to protect against invalid calls */
18 #undef OF_CHECKS
19
20 struct resource;
21
22 #include <dm/ofnode_decl.h>
23
24 struct ofnode_phandle_args {
25         ofnode node;
26         int args_count;
27         uint32_t args[OF_MAX_PHANDLE_ARGS];
28 };
29
30 /**
31  * oftree_reset() - reset the state of the oftree list
32  *
33  * Reset the oftree list so it can be started again. This should be called
34  * once the control FDT is in place, but before the ofnode interface is used.
35  */
36 static inline void oftree_reset(void) {}
37
38 /**
39  * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
40  *
41  * This cannot be called if the reference contains a node pointer.
42  *
43  * @node: Reference containing offset (possibly invalid)
44  * Return: DT offset (can be NULL)
45  */
46 static inline void *ofnode_to_fdt(ofnode node)
47 {
48 #ifdef OF_CHECKS
49         if (of_live_active())
50                 return NULL;
51 #endif
52
53         /* Use the control FDT by default */
54         return (void *)gd->fdt_blob;
55 }
56
57 /**
58  * ofnode_to_np() - convert an ofnode to a live DT node pointer
59  *
60  * This cannot be called if the reference contains an offset.
61  *
62  * @node: Reference containing struct device_node * (possibly invalid)
63  * Return: pointer to device node (can be NULL)
64  */
65 static inline struct device_node *ofnode_to_np(ofnode node)
66 {
67 #ifdef OF_CHECKS
68         if (!of_live_active())
69                 return NULL;
70 #endif
71         return node.np;
72 }
73
74 /**
75  * ofnode_to_offset() - convert an ofnode to a flat DT offset
76  *
77  * This cannot be called if the reference contains a node pointer.
78  *
79  * @node: Reference containing offset (possibly invalid)
80  * Return: DT offset (can be -1)
81  */
82 static inline int ofnode_to_offset(ofnode node)
83 {
84 #ifdef OF_CHECKS
85         if (of_live_active())
86                 return -1;
87 #endif
88         return node.of_offset;
89 }
90
91 /**
92  * ofnode_valid() - check if an ofnode is valid
93  *
94  * @node: Reference containing offset (possibly invalid)
95  * Return: true if the reference contains a valid ofnode, false if it is NULL
96  */
97 static inline bool ofnode_valid(ofnode node)
98 {
99         if (of_live_active())
100                 return node.np != NULL;
101         else
102                 return node.of_offset >= 0;
103 }
104
105 /**
106  * oftree_lookup_fdt() - obtain the FDT pointer from an oftree
107  *
108  * This can only be called when flat tree is enabled
109  *
110  * @tree: Tree to look at
111  * @return FDT pointer from the tree
112  */
113 static inline void *oftree_lookup_fdt(oftree tree)
114 {
115         if (of_live_active())
116                 return NULL;
117         else
118                 return tree.fdt;
119 }
120
121 /**
122  * offset_to_ofnode() - convert a DT offset to an ofnode
123  *
124  * @of_offset: DT offset (either valid, or -1)
125  * Return: reference to the associated DT offset
126  */
127 static inline ofnode offset_to_ofnode(int of_offset)
128 {
129         ofnode node;
130
131         if (of_live_active())
132                 node.np = NULL;
133         else
134                 node.of_offset = of_offset >= 0 ? of_offset : -1;
135
136         return node;
137 }
138
139 /**
140  * np_to_ofnode() - convert a node pointer to an ofnode
141  *
142  * @np: Live node pointer (can be NULL)
143  * Return: reference to the associated node pointer
144  */
145 static inline ofnode np_to_ofnode(struct device_node *np)
146 {
147         ofnode node;
148
149         node.np = np;
150
151         return node;
152 }
153
154 /**
155  * ofnode_is_np() - check if a reference is a node pointer
156  *
157  * This function associated that if there is a valid live tree then all
158  * references will use it. This is because using the flat DT when the live tree
159  * is valid is not permitted.
160  *
161  * @node: reference to check (possibly invalid)
162  * Return: true if the reference is a live node pointer, false if it is a DT
163  * offset
164  */
165 static inline bool ofnode_is_np(ofnode node)
166 {
167 #ifdef OF_CHECKS
168         /*
169          * Check our assumption that flat tree offsets are not used when a
170          * live tree is in use.
171          */
172         assert(!ofnode_valid(node) ||
173                (of_live_active() ? ofnode_to_np(node)
174                                   : ofnode_to_np(node)));
175 #endif
176         return of_live_active() && ofnode_valid(node);
177 }
178
179 /**
180  * ofnode_equal() - check if two references are equal
181  *
182  * @ref1: first reference to check (possibly invalid)
183  * @ref2: second reference to check (possibly invalid)
184  * Return: true if equal, else false
185  */
186 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
187 {
188         /* We only need to compare the contents */
189         return ref1.of_offset == ref2.of_offset;
190 }
191
192 /**
193  * oftree_valid() - check if an oftree is valid
194  *
195  * @tree: Reference containing oftree
196  * Return: true if the reference contains a valid oftree, false if node
197  */
198 static inline bool oftree_valid(oftree tree)
199 {
200         if (of_live_active())
201                 return tree.np;
202         else
203                 return tree.fdt;
204 }
205
206 /**
207  * oftree_null() - Obtain a null oftree
208  *
209  * This returns an oftree which points to no tree. It works both with the flat
210  * tree and livetree.
211  */
212 static inline oftree oftree_null(void)
213 {
214         oftree tree;
215
216         if (of_live_active())
217                 tree.np = NULL;
218         else
219                 tree.fdt = NULL;
220
221         return tree;
222 }
223
224 /**
225  * ofnode_null() - Obtain a null ofnode
226  *
227  * This returns an ofnode which points to no node. It works both with the flat
228  * tree and livetree.
229  */
230 static inline ofnode ofnode_null(void)
231 {
232         ofnode node;
233
234         if (of_live_active())
235                 node.np = NULL;
236         else
237                 node.of_offset = -1;
238
239         return node;
240 }
241
242 static inline ofnode ofnode_root(void)
243 {
244         ofnode node;
245
246         if (of_live_active())
247                 node.np = gd_of_root();
248         else
249                 node.of_offset = 0;
250
251         return node;
252 }
253
254 /**
255  * ofprop_valid() - check if an ofprop is valid
256  *
257  * @prop: Pointer to ofprop to check
258  * Return: true if the reference contains a valid ofprop, false if not
259  */
260 static inline bool ofprop_valid(struct ofprop *prop)
261 {
262         if (of_live_active())
263                 return prop->prop;
264         else
265                 return prop->offset >= 0;
266 }
267
268 /**
269  * oftree_default() - Returns the default device tree (U-Boot's control FDT)
270  *
271  * Returns: reference to the control FDT
272  */
273 static inline oftree oftree_default(void)
274 {
275         oftree tree;
276
277         if (of_live_active())
278                 tree.np = gd_of_root();
279         else
280                 tree.fdt = (void *)gd->fdt_blob;
281
282         return tree;
283 }
284
285 /**
286  * oftree_from_np() - Returns an oftree from a node pointer
287  *
288  * @root: Root node of the tree
289  * Returns: reference to the given node
290  */
291 static inline oftree oftree_from_np(struct device_node *root)
292 {
293         oftree tree;
294
295         tree.np = root;
296
297         return tree;
298 }
299
300 /**
301  * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
302  *
303  * @fdt: Device tree to use
304  *
305  * Returns: reference to the given node
306  */
307 static inline oftree oftree_from_fdt(void *fdt)
308 {
309         oftree tree;
310
311         tree.fdt = fdt;
312
313         return tree;
314 }
315
316 /**
317  * ofnode_name_eq() - Check if the node name is equivalent to a given name
318  *                    ignoring the unit address
319  *
320  * @node:       valid node reference that has to be compared
321  * @name:       name that has to be compared with the node name
322  * Return: true if matches, false if it doesn't match
323  */
324 bool ofnode_name_eq(ofnode node, const char *name);
325
326 /**
327  * ofnode_read_u8() - Read a 8-bit integer from a property
328  *
329  * @node:       valid node reference to read property from
330  * @propname:   name of the property to read from
331  * @outp:       place to put value (if found)
332  * Return: 0 if OK, -ve on error
333  */
334 int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
335
336 /**
337  * ofnode_read_u8_default() - Read a 8-bit integer from a property
338  *
339  * @node:       valid node reference to read property from
340  * @propname:   name of the property to read from
341  * @def:        default value to return if the property has no value
342  * Return: property value, or @def if not found
343  */
344 u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
345
346 /**
347  * ofnode_read_u16() - Read a 16-bit integer from a property
348  *
349  * @node:       valid node reference to read property from
350  * @propname:   name of the property to read from
351  * @outp:       place to put value (if found)
352  * Return: 0 if OK, -ve on error
353  */
354 int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
355
356 /**
357  * ofnode_read_u16_default() - Read a 16-bit integer from a property
358  *
359  * @node:       valid node reference to read property from
360  * @propname:   name of the property to read from
361  * @def:        default value to return if the property has no value
362  * Return: property value, or @def if not found
363  */
364 u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
365
366 /**
367  * ofnode_read_u32() - Read a 32-bit integer from a property
368  *
369  * @node:       valid node reference to read property from
370  * @propname:   name of the property to read from
371  * @outp:       place to put value (if found)
372  * Return: 0 if OK, -ve on error
373  */
374 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
375
376 /**
377  * ofnode_read_u32_index() - Read a 32-bit integer from a multi-value property
378  *
379  * @node:       valid node reference to read property from
380  * @propname:   name of the property to read from
381  * @index:      index of the integer to return
382  * @outp:       place to put value (if found)
383  * Return: 0 if OK, -ve on error
384  */
385 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
386                           u32 *outp);
387
388 /**
389  * ofnode_read_s32() - Read a 32-bit integer from a property
390  *
391  * @node:       valid node reference to read property from
392  * @propname:   name of the property to read from
393  * @outp:       place to put value (if found)
394  * Return: 0 if OK, -ve on error
395  */
396 static inline int ofnode_read_s32(ofnode node, const char *propname,
397                                   s32 *outp)
398 {
399         return ofnode_read_u32(node, propname, (u32 *)outp);
400 }
401
402 /**
403  * ofnode_read_u32_default() - Read a 32-bit integer from a property
404  *
405  * @node:       valid node reference to read property from
406  * @propname:   name of the property to read from
407  * @def:        default value to return if the property has no value
408  * Return: property value, or @def if not found
409  */
410 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def);
411
412 /**
413  * ofnode_read_u32_index_default() - Read a 32-bit integer from a multi-value
414  *                                   property
415  *
416  * @node:       valid node reference to read property from
417  * @propname:   name of the property to read from
418  * @index:      index of the integer to return
419  * @def:        default value to return if the property has no value
420  * Return: property value, or @def if not found
421  */
422 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
423                                   u32 def);
424
425 /**
426  * ofnode_read_s32_default() - Read a 32-bit integer from a property
427  *
428  * @node:       valid node reference to read property from
429  * @propname:   name of the property to read from
430  * @def:        default value to return if the property has no value
431  * Return: property value, or @def if not found
432  */
433 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
434
435 /**
436  * ofnode_read_u64() - Read a 64-bit integer from a property
437  *
438  * @node:       valid node reference to read property from
439  * @propname:   name of the property to read from
440  * @outp:       place to put value (if found)
441  * Return: 0 if OK, -ve on error
442  */
443 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
444
445 /**
446  * ofnode_read_u64_default() - Read a 64-bit integer from a property
447  *
448  * @node:       valid node reference to read property from
449  * @propname:   name of the property to read from
450  * @def:        default value to return if the property has no value
451  * Return: property value, or @def if not found
452  */
453 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
454
455 /**
456  * ofnode_read_prop() - Read a property from a node
457  *
458  * @node:       valid node reference to read property from
459  * @propname:   name of the property to read
460  * @sizep:      if non-NULL, returns the size of the property, or an error code
461  *              if not found
462  * Return: property value, or NULL if there is no such property
463  */
464 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
465
466 /**
467  * ofnode_read_string() - Read a string from a property
468  *
469  * @node:       valid node reference to read property from
470  * @propname:   name of the property to read
471  * Return: string from property value, or NULL if there is no such property
472  */
473 const char *ofnode_read_string(ofnode node, const char *propname);
474
475 /**
476  * ofnode_read_u32_array() - Find and read an array of 32 bit integers
477  *
478  * @node:       valid node reference to read property from
479  * @propname:   name of the property to read
480  * @out_values: pointer to return value, modified only if return value is 0
481  * @sz:         number of array elements to read
482  * Return: 0 on success, -EINVAL if the property does not exist,
483  * -ENODATA if property does not have a value, and -EOVERFLOW if the
484  * property data isn't large enough
485  *
486  * Search for a property in a device node and read 32-bit value(s) from
487  * it.
488  *
489  * The out_values is modified only if a valid u32 value can be decoded.
490  */
491 int ofnode_read_u32_array(ofnode node, const char *propname,
492                           u32 *out_values, size_t sz);
493
494 /**
495  * ofnode_read_bool() - read a boolean value from a property
496  *
497  * @node:       valid node reference to read property from
498  * @propname:   name of property to read
499  * Return: true if property is present (meaning true), false if not present
500  */
501 bool ofnode_read_bool(ofnode node, const char *propname);
502
503 /**
504  * ofnode_find_subnode() - find a named subnode of a parent node
505  *
506  * @node:       valid reference to parent node
507  * @subnode_name: name of subnode to find
508  * Return: reference to subnode (which can be invalid if there is no such
509  * subnode)
510  */
511 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
512
513 #if CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
514 #include <asm/global_data.h>
515
516 static inline bool ofnode_is_enabled(ofnode node)
517 {
518         if (ofnode_is_np(node)) {
519                 return of_device_is_available(ofnode_to_np(node));
520         } else {
521                 return fdtdec_get_is_enabled(gd->fdt_blob,
522                                              ofnode_to_offset(node));
523         }
524 }
525
526 static inline ofnode ofnode_first_subnode(ofnode node)
527 {
528         assert(ofnode_valid(node));
529         if (ofnode_is_np(node))
530                 return np_to_ofnode(node.np->child);
531
532         return offset_to_ofnode(
533                 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
534 }
535
536 static inline ofnode ofnode_next_subnode(ofnode node)
537 {
538         assert(ofnode_valid(node));
539         if (ofnode_is_np(node))
540                 return np_to_ofnode(node.np->sibling);
541
542         return offset_to_ofnode(
543                 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
544 }
545 #else
546 /**
547  * ofnode_is_enabled() - Checks whether a node is enabled.
548  * This looks for a 'status' property. If this exists, then returns true if
549  * the status is 'okay' and false otherwise. If there is no status property,
550  * it returns true on the assumption that anything mentioned should be enabled
551  * by default.
552  *
553  * @node: node to examine
554  * Return: false (not enabled) or true (enabled)
555  */
556 bool ofnode_is_enabled(ofnode node);
557
558 /**
559  * ofnode_first_subnode() - find the first subnode of a parent node
560  *
561  * @node:       valid reference to a valid parent node
562  * Return: reference to the first subnode (which can be invalid if the parent
563  * node has no subnodes)
564  */
565 ofnode ofnode_first_subnode(ofnode node);
566
567 /**
568  * ofnode_next_subnode() - find the next sibling of a subnode
569  *
570  * @node:       valid reference to previous node (sibling)
571  * Return: reference to the next subnode (which can be invalid if the node
572  * has no more siblings)
573  */
574 ofnode ofnode_next_subnode(ofnode node);
575 #endif /* DM_INLINE_OFNODE */
576
577 /**
578  * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
579  *
580  * @node: valid node to look up
581  * Return: ofnode reference of the parent node
582  */
583 ofnode ofnode_get_parent(ofnode node);
584
585 /**
586  * ofnode_get_name() - get the name of a node
587  *
588  * @node: valid node to look up
589  * Return: name of node (for the root node this is "")
590  */
591 const char *ofnode_get_name(ofnode node);
592
593 /**
594  * ofnode_get_path() - get the full path of a node
595  *
596  * @node: valid node to look up
597  * @buf: buffer to write the node path into
598  * @buflen: buffer size
599  * Return: 0 if OK, -ve on error
600  */
601 int ofnode_get_path(ofnode node, char *buf, int buflen);
602
603 /**
604  * ofnode_get_by_phandle() - get ofnode from phandle
605  *
606  * This uses the default (control) device tree
607  *
608  * @phandle:    phandle to look up
609  * Return: ofnode reference to the phandle
610  */
611 ofnode ofnode_get_by_phandle(uint phandle);
612
613 /**
614  * oftree_get_by_phandle() - get ofnode from phandle
615  *
616  * @tree:       tree to use
617  * @phandle:    phandle to look up
618  * Return: ofnode reference to the phandle
619  */
620 ofnode oftree_get_by_phandle(oftree tree, uint phandle);
621
622 /**
623  * ofnode_read_size() - read the size of a property
624  *
625  * @node: node to check
626  * @propname: property to check
627  * Return: size of property if present, or -EINVAL if not
628  */
629 int ofnode_read_size(ofnode node, const char *propname);
630
631 /**
632  * ofnode_get_addr_size_index() - get an address/size from a node
633  *                                based on index
634  *
635  * This reads the register address/size from a node based on index
636  *
637  * @node: node to read from
638  * @index: Index of address to read (0 for first)
639  * @size: Pointer to size of the address
640  * Return: address, or FDT_ADDR_T_NONE if not present or invalid
641  */
642 phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
643                                        fdt_size_t *size);
644
645 /**
646  * ofnode_get_addr_size_index_notrans() - get an address/size from a node
647  *                                        based on index, without address
648  *                                        translation
649  *
650  * This reads the register address/size from a node based on index.
651  * The resulting address is not translated. Useful for example for on-disk
652  * addresses.
653  *
654  * @node: node to read from
655  * @index: Index of address to read (0 for first)
656  * @size: Pointer to size of the address
657  * Return: address, or FDT_ADDR_T_NONE if not present or invalid
658  */
659 phys_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
660                                                fdt_size_t *size);
661
662 /**
663  * ofnode_get_addr_index() - get an address from a node
664  *
665  * This reads the register address from a node
666  *
667  * @node: node to read from
668  * @index: Index of address to read (0 for first)
669  * Return: address, or FDT_ADDR_T_NONE if not present or invalid
670  */
671 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
672
673 /**
674  * ofnode_get_addr() - get an address from a node
675  *
676  * This reads the register address from a node
677  *
678  * @node: node to read from
679  * Return: address, or FDT_ADDR_T_NONE if not present or invalid
680  */
681 phys_addr_t ofnode_get_addr(ofnode node);
682
683 /**
684  * ofnode_get_size() - get size from a node
685  *
686  * This reads the register size from a node
687  *
688  * @node: node to read from
689  * Return: size of the address, or FDT_SIZE_T_NONE if not present or invalid
690  */
691 fdt_size_t ofnode_get_size(ofnode node);
692
693 /**
694  * ofnode_stringlist_search() - find a string in a string list and return index
695  *
696  * Note that it is possible for this function to succeed on property values
697  * that are not NUL-terminated. That's because the function will stop after
698  * finding the first occurrence of @string. This can for example happen with
699  * small-valued cell properties, such as #address-cells, when searching for
700  * the empty string.
701  *
702  * @node: node to check
703  * @propname: name of the property containing the string list
704  * @string: string to look up in the string list
705  *
706  * Return:
707  *   the index of the string in the list of strings
708  *   -ENODATA if the property is not found
709  *   -EINVAL on some other error
710  */
711 int ofnode_stringlist_search(ofnode node, const char *propname,
712                              const char *string);
713
714 /**
715  * ofnode_read_string_index() - obtain an indexed string from a string list
716  *
717  * Note that this will successfully extract strings from properties with
718  * non-NUL-terminated values. For example on small-valued cell properties
719  * this function will return the empty string.
720  *
721  * If non-NULL, the length of the string (on success) or a negative error-code
722  * (on failure) will be stored in the integer pointer to by lenp.
723  *
724  * @node: node to check
725  * @propname: name of the property containing the string list
726  * @index: index of the string to return (cannot be negative)
727  * @outp: return location for the string
728  *
729  * Return:
730  *   0 if found or -ve error value if not found
731  */
732 int ofnode_read_string_index(ofnode node, const char *propname, int index,
733                              const char **outp);
734
735 /**
736  * ofnode_read_string_count() - find the number of strings in a string list
737  *
738  * @node: node to check
739  * @property: name of the property containing the string list
740  * Return:
741  *   number of strings in the list, or -ve error value if not found
742  */
743 int ofnode_read_string_count(ofnode node, const char *property);
744
745 /**
746  * ofnode_read_string_list() - read a list of strings
747  *
748  * This produces a list of string pointers with each one pointing to a string
749  * in the string list. If the property does not exist, it returns {NULL}.
750  *
751  * The data is allocated and the caller is reponsible for freeing the return
752  * value (the list of string pointers). The strings themselves may not be
753  * changed as they point directly into the devicetree property.
754  *
755  * @node: node to check
756  * @property: name of the property containing the string list
757  * @listp: returns an allocated, NULL-terminated list of strings if the return
758  *      value is > 0, else is set to NULL
759  * Return:
760  * number of strings in list, 0 if none, -ENOMEM if out of memory,
761  * -EINVAL if no such property, -EENODATA if property is empty
762  */
763 int ofnode_read_string_list(ofnode node, const char *property,
764                             const char ***listp);
765
766 /**
767  * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
768  *
769  * This function is useful to parse lists of phandles and their arguments.
770  * Returns 0 on success and fills out_args, on error returns appropriate
771  * errno value.
772  *
773  * Caller is responsible to call of_node_put() on the returned out_args->np
774  * pointer.
775  *
776  * Example:
777  *
778  * .. code-block::
779  *
780  *   phandle1: node1 {
781  *       #list-cells = <2>;
782  *   };
783  *   phandle2: node2 {
784  *       #list-cells = <1>;
785  *   };
786  *   node3 {
787  *       list = <&phandle1 1 2 &phandle2 3>;
788  *   };
789  *
790  * To get a device_node of the `node2' node you may call this:
791  * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
792  *
793  * @node:       device tree node containing a list
794  * @list_name:  property name that contains a list
795  * @cells_name: property name that specifies phandles' arguments count
796  * @cell_count: Cell count to use if @cells_name is NULL
797  * @index:      index of a phandle to parse out
798  * @out_args:   optional pointer to output arguments structure (will be filled)
799  * Return:
800  *   0 on success (with @out_args filled out if not NULL), -ENOENT if
801  *   @list_name does not exist, -EINVAL if a phandle was not found,
802  *   @cells_name could not be found, the arguments were truncated or there
803  *   were too many arguments.
804  */
805 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
806                                    const char *cells_name, int cell_count,
807                                    int index,
808                                    struct ofnode_phandle_args *out_args);
809
810 /**
811  * ofnode_count_phandle_with_args() - Count number of phandle in a list
812  *
813  * This function is useful to count phandles into a list.
814  * Returns number of phandle on success, on error returns appropriate
815  * errno value.
816  *
817  * @node:       device tree node containing a list
818  * @list_name:  property name that contains a list
819  * @cells_name: property name that specifies phandles' arguments count
820  * @cell_count: Cell count to use if @cells_name is NULL
821  * Return:
822  *   number of phandle on success, -ENOENT if @list_name does not exist,
823  *   -EINVAL if a phandle was not found, @cells_name could not be found.
824  */
825 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
826                                    const char *cells_name, int cell_count);
827
828 /**
829  * ofnode_path() - find a node by full path
830  *
831  * This uses the control FDT.
832  *
833  * @path: Full path to node, e.g. "/bus/spi@1"
834  * Return: reference to the node found. Use ofnode_valid() to check if it exists
835  */
836 ofnode ofnode_path(const char *path);
837
838 /**
839  * ofnode_path_root() - find a node by full path from a root node
840  *
841  * @tree: Device tree to use
842  * @path: Full path to node, e.g. "/bus/spi@1"
843  * Return: reference to the node found. Use ofnode_valid() to check if it exists
844  */
845 ofnode ofnode_path_root(oftree tree, const char *path);
846
847 /**
848  * ofnode_read_chosen_prop() - get the value of a chosen property
849  *
850  * This looks for a property within the /chosen node and returns its value
851  *
852  * @propname: Property name to look for
853  * @sizep: Returns size of property, or  `FDT_ERR_...` error code if function
854  *      returns NULL
855  * Return: property value if found, else NULL
856  */
857 const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
858
859 /**
860  * ofnode_read_chosen_string() - get the string value of a chosen property
861  *
862  * This looks for a property within the /chosen node and returns its value,
863  * checking that it is a valid nul-terminated string
864  *
865  * @propname: Property name to look for
866  * Return: string value if found, else NULL
867  */
868 const char *ofnode_read_chosen_string(const char *propname);
869
870 /**
871  * ofnode_get_chosen_node() - get a referenced node from the chosen node
872  *
873  * This looks up a named property in the chosen node and uses that as a path to
874  * look up a code.
875  *
876  * @propname: Property name to look for
877  * Return: the referenced node if present, else ofnode_null()
878  */
879 ofnode ofnode_get_chosen_node(const char *propname);
880
881 /**
882  * ofnode_read_aliases_prop() - get the value of a aliases property
883  *
884  * This looks for a property within the /aliases node and returns its value
885  *
886  * @propname: Property name to look for
887  * @sizep: Returns size of property, or `FDT_ERR_...` error code if function
888  *      returns NULL
889  * Return: property value if found, else NULL
890  */
891 const void *ofnode_read_aliases_prop(const char *propname, int *sizep);
892
893 /**
894  * ofnode_get_aliases_node() - get a referenced node from the aliases node
895  *
896  * This looks up a named property in the aliases node and uses that as a path to
897  * look up a code.
898  *
899  * @propname: Property name to look for
900  * Return: the referenced node if present, else ofnode_null()
901  */
902 ofnode ofnode_get_aliases_node(const char *propname);
903
904 struct display_timing;
905 /**
906  * ofnode_decode_display_timing() - decode display timings
907  *
908  * Decode display timings from the supplied 'display-timings' node.
909  * See doc/device-tree-bindings/video/display-timing.txt for binding
910  * information.
911  *
912  * @node:       'display-timing' node containing the timing subnodes
913  * @index:      Index number to read (0=first timing subnode)
914  * @config:     Place to put timings
915  * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
916  */
917 int ofnode_decode_display_timing(ofnode node, int index,
918                                  struct display_timing *config);
919
920 /**
921  * ofnode_get_property() - get a pointer to the value of a node property
922  *
923  * @node: node to read
924  * @propname: property to read
925  * @lenp: place to put length on success
926  * Return: pointer to property, or NULL if not found
927  */
928 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
929
930 /**
931  * ofnode_first_property()- get the reference of the first property
932  *
933  * Get reference to the first property of the node, it is used to iterate
934  * and read all the property with ofprop_get_property().
935  *
936  * @node: node to read
937  * @prop: place to put argument reference
938  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
939  */
940 int ofnode_first_property(ofnode node, struct ofprop *prop);
941
942 /**
943  * ofnode_next_property() - get the reference of the next property
944  *
945  * Get reference to the next property of the node, it is used to iterate
946  * and read all the property with ofprop_get_property().
947  *
948  * @prop: reference of current argument and place to put reference of next one
949  * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
950  */
951 int ofnode_next_property(struct ofprop *prop);
952
953 /**
954  * ofnode_for_each_prop() - iterate over all properties of a node
955  *
956  * @prop:       struct ofprop
957  * @node:       node (lvalue, ofnode)
958  *
959  * This is a wrapper around a for loop and is used like this::
960  *
961  *   ofnode node;
962  *   struct ofprop prop;
963  *
964  *   ofnode_for_each_prop(prop, node) {
965  *       ...use prop...
966  *   }
967  *
968  * Note that this is implemented as a macro and @prop is used as
969  * iterator in the loop. The parent variable can be a constant or even a
970  * literal.
971  */
972 #define ofnode_for_each_prop(prop, node) \
973         for (ofnode_first_property(node, &prop); \
974              ofprop_valid(&prop); \
975              ofnode_next_property(&prop))
976
977 /**
978  * ofprop_get_property() - get a pointer to the value of a property
979  *
980  * Get value for the property identified by the provided reference.
981  *
982  * @prop: reference on property
983  * @propname: If non-NULL, place to property name on success,
984  * @lenp: If non-NULL, place to put length on success, or error code on failure
985  * Return: pointer to property, or NULL if not found
986  */
987 const void *ofprop_get_property(const struct ofprop *prop,
988                                 const char **propname, int *lenp);
989
990 /**
991  * ofnode_get_addr_size() - get address and size from a property
992  *
993  * This does no address translation. It simply reads an property that contains
994  * an address and a size value, one after the other.
995  *
996  * @node: node to read from
997  * @propname: property to read
998  * @sizep: place to put size value (on success)
999  * Return: address value, or FDT_ADDR_T_NONE on error
1000  */
1001 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
1002                                  phys_size_t *sizep);
1003
1004 /**
1005  * ofnode_read_u8_array_ptr() - find an 8-bit array
1006  *
1007  * Look up a property in a node and return a pointer to its contents as a
1008  * byte array of given length. The property must have at least enough data
1009  * for the array (count bytes). It may have more, but this will be ignored.
1010  * The data is not copied.
1011  *
1012  * @node:       node to examine
1013  * @propname:   name of property to find
1014  * @sz:         number of array elements
1015  * Return:
1016  * pointer to byte array if found, or NULL if the property is not found or
1017  * there is not enough data
1018  */
1019 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
1020                                         size_t sz);
1021
1022 /**
1023  * ofnode_read_pci_addr() - look up a PCI address
1024  *
1025  * Look at an address property in a node and return the PCI address which
1026  * corresponds to the given type in the form of fdt_pci_addr.
1027  * The property must hold one fdt_pci_addr with a lengh.
1028  *
1029  * @node:       node to examine
1030  * @type:       pci address type (FDT_PCI_SPACE_xxx)
1031  * @propname:   name of property to find
1032  * @addr:       returns pci address in the form of fdt_pci_addr
1033  * Return:
1034  * 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
1035  * format of the property was invalid, -ENXIO if the requested
1036  * address type was not found
1037  */
1038 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
1039                          const char *propname, struct fdt_pci_addr *addr);
1040
1041 /**
1042  * ofnode_read_pci_vendev() - look up PCI vendor and device id
1043  *
1044  * Look at the compatible property of a device node that represents a PCI
1045  * device and extract pci vendor id and device id from it.
1046  *
1047  * @node:       node to examine
1048  * @vendor:     vendor id of the pci device
1049  * @device:     device id of the pci device
1050  * Return: 0 if ok, negative on error
1051  */
1052 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
1053
1054 /**
1055  * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
1056  *
1057  * Look at the compatible property of a device node that represents a eth phy
1058  * device and extract phy vendor id and device id from it.
1059  *
1060  * @node:       node to examine
1061  * @vendor:     vendor id of the eth phy device
1062  * @device:     device id of the eth phy device
1063  * Return:       0 if ok, negative on error
1064  */
1065 int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
1066
1067 /**
1068  * ofnode_read_addr_cells() - Get the number of address cells for a node
1069  *
1070  * This walks back up the tree to find the closest #address-cells property
1071  * which controls the given node.
1072  *
1073  * @node: Node to check
1074  * Return: number of address cells this node uses
1075  */
1076 int ofnode_read_addr_cells(ofnode node);
1077
1078 /**
1079  * ofnode_read_size_cells() - Get the number of size cells for a node
1080  *
1081  * This walks back up the tree to find the closest #size-cells property
1082  * which controls the given node.
1083  *
1084  * @node: Node to check
1085  * Return: number of size cells this node uses
1086  */
1087 int ofnode_read_size_cells(ofnode node);
1088
1089 /**
1090  * ofnode_read_simple_addr_cells() - Get the address cells property in a node
1091  *
1092  * This function matches fdt_address_cells().
1093  *
1094  * @node: Node to check
1095  * Return: value of #address-cells property in this node, or 2 if none
1096  */
1097 int ofnode_read_simple_addr_cells(ofnode node);
1098
1099 /**
1100  * ofnode_read_simple_size_cells() - Get the size cells property in a node
1101  *
1102  * This function matches fdt_size_cells().
1103  *
1104  * @node: Node to check
1105  * Return: value of #size-cells property in this node, or 2 if none
1106  */
1107 int ofnode_read_simple_size_cells(ofnode node);
1108
1109 /**
1110  * ofnode_pre_reloc() - check if a node should be bound before relocation
1111  *
1112  * Device tree nodes can be marked as needing-to-be-bound in the loader stages
1113  * via special device tree properties.
1114  *
1115  * Before relocation this function can be used to check if nodes are required
1116  * in either SPL or TPL stages.
1117  *
1118  * After relocation and jumping into the real U-Boot binary it is possible to
1119  * determine if a node was bound in one of SPL/TPL stages.
1120  *
1121  * There are 4 settings currently in use
1122  * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
1123  * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
1124  * Existing platforms only use it to indicate nodes needed in
1125  * SPL. Should probably be replaced by u-boot,dm-spl for new platforms.
1126  * - u-boot,dm-spl: SPL and U-Boot pre-relocation
1127  * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
1128  *
1129  * @node: node to check
1130  * Return: true if node is needed in SPL/TL, false otherwise
1131  */
1132 bool ofnode_pre_reloc(ofnode node);
1133
1134 /**
1135  * ofnode_read_resource() - Read a resource from a node
1136  *
1137  * Read resource information from a node at the given index
1138  *
1139  * @node: Node to read from
1140  * @index: Index of resource to read (0 = first)
1141  * @res: Returns resource that was read, on success
1142  * Return: 0 if OK, -ve on error
1143  */
1144 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
1145
1146 /**
1147  * ofnode_read_resource_byname() - Read a resource from a node by name
1148  *
1149  * Read resource information from a node matching the given name. This uses a
1150  * 'reg-names' string list property with the names matching the associated
1151  * 'reg' property list.
1152  *
1153  * @node: Node to read from
1154  * @name: Name of resource to read
1155  * @res: Returns resource that was read, on success
1156  * Return: 0 if OK, -ve on error
1157  */
1158 int ofnode_read_resource_byname(ofnode node, const char *name,
1159                                 struct resource *res);
1160
1161 /**
1162  * ofnode_by_compatible() - Find the next compatible node
1163  *
1164  * Find the next node after @from that is compatible with @compat
1165  *
1166  * @from: ofnode to start from (use ofnode_null() to start at the beginning)
1167  * @compat: Compatible string to match
1168  * Return: ofnode found, or ofnode_null() if none
1169  */
1170 ofnode ofnode_by_compatible(ofnode from, const char *compat);
1171
1172 /**
1173  * ofnode_by_prop_value() - Find the next node with given property value
1174  *
1175  * Find the next node after @from that has a @propname with a value
1176  * @propval and a length @proplen.
1177  *
1178  * @from: ofnode to start from (use ofnode_null() to start at the
1179  * beginning)
1180  * @propname: property name to check
1181  * @propval: property value to search for
1182  * @proplen: length of the value in propval
1183  * Return: ofnode found, or ofnode_null() if none
1184  */
1185 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1186                             const void *propval, int proplen);
1187
1188 /**
1189  * ofnode_for_each_subnode() - iterate over all subnodes of a parent
1190  *
1191  * @node:       child node (ofnode, lvalue)
1192  * @parent:     parent node (ofnode)
1193  *
1194  * This is a wrapper around a for loop and is used like so::
1195  *
1196  *   ofnode node;
1197  *   ofnode_for_each_subnode(node, parent) {
1198  *       Use node
1199  *       ...
1200  *   }
1201  *
1202  * Note that this is implemented as a macro and @node is used as
1203  * iterator in the loop. The parent variable can be a constant or even a
1204  * literal.
1205  */
1206 #define ofnode_for_each_subnode(node, parent) \
1207         for (node = ofnode_first_subnode(parent); \
1208              ofnode_valid(node); \
1209              node = ofnode_next_subnode(node))
1210
1211 /**
1212  * ofnode_for_each_compatible_node() - iterate over all nodes with a given
1213  *                                     compatible string
1214  *
1215  * @node:       child node (ofnode, lvalue)
1216  * @compat:     compatible string to match
1217  *
1218  * This is a wrapper around a for loop and is used like so::
1219  *
1220  *   ofnode node;
1221  *   ofnode_for_each_compatible_node(node, parent, compatible) {
1222  *      Use node
1223  *      ...
1224  *   }
1225  *
1226  * Note that this is implemented as a macro and @node is used as
1227  * iterator in the loop.
1228  */
1229 #define ofnode_for_each_compatible_node(node, compat) \
1230         for (node = ofnode_by_compatible(ofnode_null(), compat); \
1231              ofnode_valid(node); \
1232              node = ofnode_by_compatible(node, compat))
1233
1234 /**
1235  * ofnode_get_child_count() - get the child count of a ofnode
1236  *
1237  * @parent: valid node to get its child count
1238  * Return: the number of subnodes
1239  */
1240 int ofnode_get_child_count(ofnode parent);
1241
1242 /**
1243  * ofnode_translate_address() - Translate a device-tree address
1244  *
1245  * Translate an address from the device-tree into a CPU physical address. This
1246  * function walks up the tree and applies the various bus mappings along the
1247  * way.
1248  *
1249  * @node: Device tree node giving the context in which to translate the address
1250  * @in_addr: pointer to the address to translate
1251  * Return: the translated address; OF_BAD_ADDR on error
1252  */
1253 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
1254
1255 /**
1256  * ofnode_translate_dma_address() - Translate a device-tree DMA address
1257  *
1258  * Translate a DMA address from the device-tree into a CPU physical address.
1259  * This function walks up the tree and applies the various bus mappings along
1260  * the way.
1261  *
1262  * @node: Device tree node giving the context in which to translate the
1263  *        DMA address
1264  * @in_addr: pointer to the DMA address to translate
1265  * Return: the translated DMA address; OF_BAD_ADDR on error
1266  */
1267 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
1268
1269 /**
1270  * ofnode_get_dma_range() - get dma-ranges for a specific DT node
1271  *
1272  * Get DMA ranges for a specifc node, this is useful to perform bus->cpu and
1273  * cpu->bus address translations
1274  *
1275  * @node: Device tree node
1276  * @cpu: Pointer to variable storing the range's cpu address
1277  * @bus: Pointer to variable storing the range's bus address
1278  * @size: Pointer to variable storing the range's size
1279  * Return: translated DMA address or OF_BAD_ADDR on error
1280  */
1281 int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus,
1282                          u64 *size);
1283
1284 /**
1285  * ofnode_device_is_compatible() - check if the node is compatible with compat
1286  *
1287  * This allows to check whether the node is comaptible with the compat.
1288  *
1289  * @node:       Device tree node for which compatible needs to be verified.
1290  * @compat:     Compatible string which needs to verified in the given node.
1291  * Return: true if OK, false if the compatible is not found
1292  */
1293 int ofnode_device_is_compatible(ofnode node, const char *compat);
1294
1295 /**
1296  * ofnode_write_prop() - Set a property of a ofnode
1297  *
1298  * Note that the value passed to the function is *not* allocated by the
1299  * function itself, but must be allocated by the caller if necessary. However
1300  * it does allocate memory for the property struct and name.
1301  *
1302  * @node:       The node for whose property should be set
1303  * @propname:   The name of the property to set
1304  * @value:      The new value of the property (must be valid prior to calling
1305  *              the function)
1306  * @len:        The length of the new value of the property
1307  * Return: 0 if successful, -ve on error
1308  */
1309 int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1310                       int len);
1311
1312 /**
1313  * ofnode_write_string() - Set a string property of a ofnode
1314  *
1315  * Note that the value passed to the function is *not* allocated by the
1316  * function itself, but must be allocated by the caller if necessary.
1317  *
1318  * @node:       The node for whose string property should be set
1319  * @propname:   The name of the string property to set
1320  * @value:      The new value of the string property (must be valid prior to
1321  *              calling the function)
1322  * Return: 0 if successful, -ve on error
1323  */
1324 int ofnode_write_string(ofnode node, const char *propname, const char *value);
1325
1326 /**
1327  * ofnode_write_u32() - Set an integer property of an ofnode
1328  *
1329  * @node:       The node for whose string property should be set
1330  * @propname:   The name of the string property to set
1331  * @value:      The new value of the 32-bit integer property
1332  * Return: 0 if successful, -ve on error
1333  */
1334 int ofnode_write_u32(ofnode node, const char *propname, u32 value);
1335
1336 /**
1337  * ofnode_set_enabled() - Enable or disable a device tree node given by its
1338  *                        ofnode
1339  *
1340  * This function effectively sets the node's "status" property to either "okay"
1341  * or "disable", hence making it available for driver model initialization or
1342  * not.
1343  *
1344  * @node:       The node to enable
1345  * @value:      Flag that tells the function to either disable or enable the
1346  *              node
1347  * Return: 0 if successful, -ve on error
1348  */
1349 int ofnode_set_enabled(ofnode node, bool value);
1350
1351 /**
1352  * ofnode_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
1353  *
1354  * This function parses PHY handle from the Ethernet controller's ofnode
1355  * (trying all possible PHY handle property names), and returns the PHY ofnode.
1356  *
1357  * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
1358  * if the result to that is true, this function should not be called.
1359  *
1360  * @eth_node:   ofnode belonging to the Ethernet controller
1361  * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
1362  */
1363 ofnode ofnode_get_phy_node(ofnode eth_node);
1364
1365 /**
1366  * ofnode_read_phy_mode() - Read PHY connection type from a MAC node
1367  *
1368  * This function parses the "phy-mode" / "phy-connection-type" property and
1369  * returns the corresponding PHY interface type.
1370  *
1371  * @mac_node:   ofnode containing the property
1372  * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
1373  *         error
1374  */
1375 phy_interface_t ofnode_read_phy_mode(ofnode mac_node);
1376
1377 #if CONFIG_IS_ENABLED(DM)
1378 /**
1379  * ofnode_conf_read_bool() - Read a boolean value from the U-Boot config
1380  *
1381  * This reads a property from the /config node of the devicetree.
1382  *
1383  * See doc/config.txt for bindings
1384  *
1385  * @prop_name:  property name to look up
1386  * Return: true, if it exists, false if not
1387  */
1388 bool ofnode_conf_read_bool(const char *prop_name);
1389
1390 /**
1391  * ofnode_conf_read_int() - Read an integer value from the U-Boot config
1392  *
1393  * This reads a property from the /config node of the devicetree.
1394  *
1395  * See doc/config.txt for bindings
1396  *
1397  * @prop_name: property name to look up
1398  * @default_val: default value to return if the property is not found
1399  * Return: integer value, if found, or @default_val if not
1400  */
1401 int ofnode_conf_read_int(const char *prop_name, int default_val);
1402
1403 /**
1404  * ofnode_conf_read_str() - Read a string value from the U-Boot config
1405  *
1406  * This reads a property from the /config node of the devicetree.
1407  *
1408  * See doc/config.txt for bindings
1409  *
1410  * @prop_name: property name to look up
1411  * Return: string value, if found, or NULL if not
1412  */
1413 const char *ofnode_conf_read_str(const char *prop_name);
1414
1415 #else /* CONFIG_DM */
1416 static inline bool ofnode_conf_read_bool(const char *prop_name)
1417 {
1418         return false;
1419 }
1420
1421 static inline int ofnode_conf_read_int(const char *prop_name, int default_val)
1422 {
1423         return default_val;
1424 }
1425
1426 static inline const char *ofnode_conf_read_str(const char *prop_name)
1427 {
1428         return NULL;
1429 }
1430
1431 #endif /* CONFIG_DM */
1432
1433 /**
1434  * of_add_subnode() - add a new subnode to a node
1435  *
1436  * @parent:     parent node to add to
1437  * @name:       name of subnode
1438  * @nodep:      returns pointer to new subnode (valid if the function returns 0
1439  *      or -EEXIST)
1440  * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other
1441  * -ve on other error
1442  */
1443 int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep);
1444
1445 #endif