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