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