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