dm: core: Add ofnode_get_chosen_prop()
[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
14 /* Enable checks to protect against invalid calls */
15 #undef OF_CHECKS
16
17 struct resource;
18
19 /**
20  * ofnode - reference to a device tree node
21  *
22  * This union can hold either a straightforward pointer to a struct device_node
23  * in the live device tree, or an offset within the flat device tree. In the
24  * latter case, the pointer value is just the integer offset within the flat DT.
25  *
26  * Thus we can reference nodes in both the live tree (once available) and the
27  * flat tree (until then). Functions are available to translate between an
28  * ofnode and either an offset or a struct device_node *.
29  *
30  * The reference can also hold a null offset, in which case the pointer value
31  * here is NULL. This corresponds to a struct device_node * value of
32  * NULL, or an offset of -1.
33  *
34  * There is no ambiguity as to whether ofnode holds an offset or a node
35  * pointer: when the live tree is active it holds a node pointer, otherwise it
36  * holds an offset. The value itself does not need to be unique and in theory
37  * the same value could point to a valid device node or a valid offset. We
38  * could arrange for a unique value to be used (e.g. by making the pointer
39  * point to an offset within the flat device tree in the case of an offset) but
40  * this increases code size slightly due to the subtraction. Since it offers no
41  * real benefit, the approach described here seems best.
42  *
43  * For now these points use constant types, since we don't allow writing
44  * the DT.
45  *
46  * @np: Pointer to device node, used for live tree
47  * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
48  *      is not a really a pointer to a node: it is an offset value. See above.
49  */
50 typedef union ofnode_union {
51         const struct device_node *np;   /* will be used for future live tree */
52         long of_offset;
53 } ofnode;
54
55 struct ofnode_phandle_args {
56         ofnode node;
57         int args_count;
58         uint32_t args[OF_MAX_PHANDLE_ARGS];
59 };
60
61 /**
62  * _ofnode_to_np() - convert an ofnode to a live DT node pointer
63  *
64  * This cannot be called if the reference contains an offset.
65  *
66  * @node: Reference containing struct device_node * (possibly invalid)
67  * @return pointer to device node (can be NULL)
68  */
69 static inline const struct device_node *ofnode_to_np(ofnode node)
70 {
71 #ifdef OF_CHECKS
72         if (!of_live_active())
73                 return NULL;
74 #endif
75         return node.np;
76 }
77
78 /**
79  * ofnode_to_offset() - convert an ofnode to a flat DT offset
80  *
81  * This cannot be called if the reference contains a node pointer.
82  *
83  * @node: Reference containing offset (possibly invalid)
84  * @return DT offset (can be -1)
85  */
86 static inline int ofnode_to_offset(ofnode node)
87 {
88 #ifdef OF_CHECKS
89         if (of_live_active())
90                 return -1;
91 #endif
92         return node.of_offset;
93 }
94
95 /**
96  * ofnode_valid() - check if an ofnode is valid
97  *
98  * @return true if the reference contains a valid ofnode, false if it is NULL
99  */
100 static inline bool ofnode_valid(ofnode node)
101 {
102         if (of_live_active())
103                 return node.np != NULL;
104         else
105                 return node.of_offset != -1;
106 }
107
108 /**
109  * offset_to_ofnode() - convert a DT offset to an ofnode
110  *
111  * @of_offset: DT offset (either valid, or -1)
112  * @return reference to the associated DT offset
113  */
114 static inline ofnode offset_to_ofnode(int of_offset)
115 {
116         ofnode node;
117
118         if (of_live_active())
119                 node.np = NULL;
120         else
121                 node.of_offset = of_offset >= 0 ? of_offset : -1;
122
123         return node;
124 }
125
126 /**
127  * np_to_ofnode() - convert a node pointer to an ofnode
128  *
129  * @np: Live node pointer (can be NULL)
130  * @return reference to the associated node pointer
131  */
132 static inline ofnode np_to_ofnode(const struct device_node *np)
133 {
134         ofnode node;
135
136         node.np = np;
137
138         return node;
139 }
140
141 /**
142  * ofnode_is_np() - check if a reference is a node pointer
143  *
144  * This function associated that if there is a valid live tree then all
145  * references will use it. This is because using the flat DT when the live tree
146  * is valid is not permitted.
147  *
148  * @node: reference to check (possibly invalid)
149  * @return true if the reference is a live node pointer, false if it is a DT
150  * offset
151  */
152 static inline bool ofnode_is_np(ofnode node)
153 {
154 #ifdef OF_CHECKS
155         /*
156          * Check our assumption that flat tree offsets are not used when a
157          * live tree is in use.
158          */
159         assert(!ofnode_valid(node) ||
160                (of_live_active() ? _ofnode_to_np(node)
161                                   : _ofnode_to_np(node)));
162 #endif
163         return of_live_active() && ofnode_valid(node);
164 }
165
166 /**
167  * ofnode_equal() - check if two references are equal
168  *
169  * @return true if equal, else false
170  */
171 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
172 {
173         /* We only need to compare the contents */
174         return ref1.of_offset == ref2.of_offset;
175 }
176
177 /**
178  * ofnode_null() - Obtain a null ofnode
179  *
180  * This returns an ofnode which points to no node. It works both with the flat
181  * tree and livetree.
182  */
183 static inline ofnode ofnode_null(void)
184 {
185         ofnode node;
186
187         if (of_live_active())
188                 node.np = NULL;
189         else
190                 node.of_offset = -1;
191
192         return node;
193 }
194
195 /**
196  * ofnode_read_u32() - Read a 32-bit integer from a property
197  *
198  * @ref:        valid node reference to read property from
199  * @propname:   name of the property to read from
200  * @outp:       place to put value (if found)
201  * @return 0 if OK, -ve on error
202  */
203 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
204
205 /**
206  * ofnode_read_s32() - Read a 32-bit integer from a property
207  *
208  * @ref:        valid node reference to read property from
209  * @propname:   name of the property to read from
210  * @outp:       place to put value (if found)
211  * @return 0 if OK, -ve on error
212  */
213 static inline int ofnode_read_s32(ofnode node, const char *propname,
214                                   s32 *out_value)
215 {
216         return ofnode_read_u32(node, propname, (u32 *)out_value);
217 }
218
219 /**
220  * ofnode_read_u32_default() - Read a 32-bit integer from a property
221  *
222  * @ref:        valid node reference to read property from
223  * @propname:   name of the property to read from
224  * @def:        default value to return if the property has no value
225  * @return property value, or @def if not found
226  */
227 u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
228
229 /**
230  * ofnode_read_s32_default() - Read a 32-bit integer from a property
231  *
232  * @ref:        valid node reference to read property from
233  * @propname:   name of the property to read from
234  * @def:        default value to return if the property has no value
235  * @return property value, or @def if not found
236  */
237 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
238
239 /**
240  * ofnode_read_u64() - Read a 64-bit integer from a property
241  *
242  * @node:       valid node reference to read property from
243  * @propname:   name of the property to read from
244  * @outp:       place to put value (if found)
245  * @return 0 if OK, -ve on error
246  */
247 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp);
248
249 /**
250  * ofnode_read_u64_default() - Read a 64-bit integer from a property
251  *
252  * @ref:        valid node reference to read property from
253  * @propname:   name of the property to read from
254  * @def:        default value to return if the property has no value
255  * @return property value, or @def if not found
256  */
257 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def);
258
259 /**
260  * ofnode_read_prop() - Read a property from a node
261  *
262  * @node:       valid node reference to read property from
263  * @propname:   name of the property to read
264  * @sizep:      if non-NULL, returns the size of the property, or an error code
265                 if not found
266  * @return property value, or NULL if there is no such property
267  */
268 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);
269
270 /**
271  * ofnode_read_string() - Read a string from a property
272  *
273  * @node:       valid node reference to read property from
274  * @propname:   name of the property to read
275  * @return string from property value, or NULL if there is no such property
276  */
277 const char *ofnode_read_string(ofnode node, const char *propname);
278
279 /**
280  * ofnode_read_u32_array() - Find and read an array of 32 bit integers
281  *
282  * @node:       valid node reference to read property from
283  * @propname:   name of the property to read
284  * @out_values: pointer to return value, modified only if return value is 0
285  * @sz:         number of array elements to read
286  * @return 0 if OK, -ve on error
287  *
288  * Search for a property in a device node and read 32-bit value(s) from
289  * it. Returns 0 on success, -EINVAL if the property does not exist,
290  * -ENODATA if property does not have a value, and -EOVERFLOW if the
291  * property data isn't large enough.
292  *
293  * The out_values is modified only if a valid u32 value can be decoded.
294  */
295 int ofnode_read_u32_array(ofnode node, const char *propname,
296                           u32 *out_values, size_t sz);
297
298 /**
299  * ofnode_read_bool() - read a boolean value from a property
300  *
301  * @node:       valid node reference to read property from
302  * @propname:   name of property to read
303  * @return true if property is present (meaning true), false if not present
304  */
305 bool ofnode_read_bool(ofnode node, const char *propname);
306
307 /**
308  * ofnode_find_subnode() - find a named subnode of a parent node
309  *
310  * @node:       valid reference to parent node
311  * @subnode_name: name of subnode to find
312  * @return reference to subnode (which can be invalid if there is no such
313  * subnode)
314  */
315 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
316
317 /**
318  * ofnode_first_subnode() - find the first subnode of a parent node
319  *
320  * @node:       valid reference to a valid parent node
321  * @return reference to the first subnode (which can be invalid if the parent
322  * node has no subnodes)
323  */
324 ofnode ofnode_first_subnode(ofnode node);
325
326 /**
327  * ofnode_next_subnode() - find the next sibling of a subnode
328  *
329  * @node:       valid reference to previous node (sibling)
330  * @return reference to the next subnode (which can be invalid if the node
331  * has no more siblings)
332  */
333 ofnode ofnode_next_subnode(ofnode node);
334
335 /**
336  * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
337  *
338  * @node: valid node to look up
339  * @return ofnode reference of the parent node
340  */
341 ofnode ofnode_get_parent(ofnode node);
342
343 /**
344  * ofnode_get_name() - get the name of a node
345  *
346  * @node: valid node to look up
347  * @return name of node
348  */
349 const char *ofnode_get_name(ofnode node);
350
351 /**
352  * ofnode_get_by_phandle() - get ofnode from phandle
353  *
354  * @phandle:    phandle to look up
355  * @return ofnode reference to the phandle
356  */
357 ofnode ofnode_get_by_phandle(uint phandle);
358
359 /**
360  * ofnode_read_size() - read the size of a property
361  *
362  * @node: node to check
363  * @propname: property to check
364  * @return size of property if present, or -EINVAL if not
365  */
366 int ofnode_read_size(ofnode node, const char *propname);
367
368 /**
369  * ofnode_get_addr_size_index() - get an address/size from a node
370  *                                based on index
371  *
372  * This reads the register address/size from a node based on index
373  *
374  * @node: node to read from
375  * @index: Index of address to read (0 for first)
376  * @size: Pointer to size of the address
377  * @return address, or FDT_ADDR_T_NONE if not present or invalid
378  */
379 phys_addr_t ofnode_get_addr_size_index(ofnode node, int index,
380                                        fdt_size_t *size);
381
382 /**
383  * ofnode_get_addr_index() - get an address from a node
384  *
385  * This reads the register address from a node
386  *
387  * @node: node to read from
388  * @index: Index of address to read (0 for first)
389  * @return address, or FDT_ADDR_T_NONE if not present or invalid
390  */
391 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
392
393 /**
394  * ofnode_get_addr() - get an address from a node
395  *
396  * This reads the register address from a node
397  *
398  * @node: node to read from
399  * @return address, or FDT_ADDR_T_NONE if not present or invalid
400  */
401 phys_addr_t ofnode_get_addr(ofnode node);
402
403 /**
404  * ofnode_stringlist_search() - find a string in a string list and return index
405  *
406  * Note that it is possible for this function to succeed on property values
407  * that are not NUL-terminated. That's because the function will stop after
408  * finding the first occurrence of @string. This can for example happen with
409  * small-valued cell properties, such as #address-cells, when searching for
410  * the empty string.
411  *
412  * @node: node to check
413  * @propname: name of the property containing the string list
414  * @string: string to look up in the string list
415  *
416  * @return:
417  *   the index of the string in the list of strings
418  *   -ENODATA if the property is not found
419  *   -EINVAL on some other error
420  */
421 int ofnode_stringlist_search(ofnode node, const char *propname,
422                              const char *string);
423
424 /**
425  * ofnode_read_string_index() - obtain an indexed string from a string list
426  *
427  * Note that this will successfully extract strings from properties with
428  * non-NUL-terminated values. For example on small-valued cell properties
429  * this function will return the empty string.
430  *
431  * If non-NULL, the length of the string (on success) or a negative error-code
432  * (on failure) will be stored in the integer pointer to by lenp.
433  *
434  * @node: node to check
435  * @propname: name of the property containing the string list
436  * @index: index of the string to return
437  * @lenp: return location for the string length or an error code on failure
438  *
439  * @return:
440  *   length of string, if found or -ve error value if not found
441  */
442 int ofnode_read_string_index(ofnode node, const char *propname, int index,
443                              const char **outp);
444
445 /**
446  * ofnode_read_string_count() - find the number of strings in a string list
447  *
448  * @node: node to check
449  * @propname: name of the property containing the string list
450  * @return:
451  *   number of strings in the list, or -ve error value if not found
452  */
453 int ofnode_read_string_count(ofnode node, const char *property);
454
455 /**
456  * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
457  *
458  * This function is useful to parse lists of phandles and their arguments.
459  * Returns 0 on success and fills out_args, on error returns appropriate
460  * errno value.
461  *
462  * Caller is responsible to call of_node_put() on the returned out_args->np
463  * pointer.
464  *
465  * Example:
466  *
467  * phandle1: node1 {
468  *      #list-cells = <2>;
469  * }
470  *
471  * phandle2: node2 {
472  *      #list-cells = <1>;
473  * }
474  *
475  * node3 {
476  *      list = <&phandle1 1 2 &phandle2 3>;
477  * }
478  *
479  * To get a device_node of the `node2' node you may call this:
480  * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
481  *
482  * @node:       device tree node containing a list
483  * @list_name:  property name that contains a list
484  * @cells_name: property name that specifies phandles' arguments count
485  * @cells_count: Cell count to use if @cells_name is NULL
486  * @index:      index of a phandle to parse out
487  * @out_args:   optional pointer to output arguments structure (will be filled)
488  * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
489  *      @list_name does not exist, -EINVAL if a phandle was not found,
490  *      @cells_name could not be found, the arguments were truncated or there
491  *      were too many arguments.
492  */
493 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
494                                    const char *cells_name, int cell_count,
495                                    int index,
496                                    struct ofnode_phandle_args *out_args);
497
498 /**
499  * ofnode_count_phandle_with_args() - Count number of phandle in a list
500  *
501  * This function is useful to count phandles into a list.
502  * Returns number of phandle on success, on error returns appropriate
503  * errno value.
504  *
505  * @node:       device tree node containing a list
506  * @list_name:  property name that contains a list
507  * @cells_name: property name that specifies phandles' arguments count
508  * @return number of phandle on success, -ENOENT if @list_name does not
509  *      exist, -EINVAL if a phandle was not found, @cells_name could not
510  *      be found.
511  */
512 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
513                                    const char *cells_name);
514
515 /**
516  * ofnode_path() - find a node by full path
517  *
518  * @path: Full path to node, e.g. "/bus/spi@1"
519  * @return reference to the node found. Use ofnode_valid() to check if it exists
520  */
521 ofnode ofnode_path(const char *path);
522
523 /**
524  * ofnode_read_chosen_prop() - get the value of a chosen property
525  *
526  * This looks for a property within the /chosen node and returns its value
527  *
528  * @propname: Property name to look for
529  * @sizep: Returns size of property, or FDT_ERR_... error code if function
530  *      returns NULL
531  * @return property value if found, else NULL
532  */
533 const void *ofnode_read_chosen_prop(const char *propname, int *sizep);
534
535 /**
536  * ofnode_read_chosen_string() - get the string value of a chosen property
537  *
538  * This looks for a property within the /chosen node and returns its value,
539  * checking that it is a valid nul-terminated string
540  *
541  * @propname: Property name to look for
542  * @return string value if found, else NULL
543  */
544 const char *ofnode_read_chosen_string(const char *propname);
545
546 /**
547  * ofnode_get_chosen_node() - get a referenced node from the chosen node
548  *
549  * This looks up a named property in the chosen node and uses that as a path to
550  * look up a code.
551  *
552  * @return the referenced node if present, else ofnode_null()
553  */
554 ofnode ofnode_get_chosen_node(const char *propname);
555
556 struct display_timing;
557 /**
558  * ofnode_decode_display_timing() - decode display timings
559  *
560  * Decode display timings from the supplied 'display-timings' node.
561  * See doc/device-tree-bindings/video/display-timing.txt for binding
562  * information.
563  *
564  * @node        'display-timing' node containing the timing subnodes
565  * @index       Index number to read (0=first timing subnode)
566  * @config      Place to put timings
567  * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
568  */
569 int ofnode_decode_display_timing(ofnode node, int index,
570                                  struct display_timing *config);
571
572 /**
573  * ofnode_get_property()- - get a pointer to the value of a node property
574  *
575  * @node: node to read
576  * @propname: property to read
577  * @lenp: place to put length on success
578  * @return pointer to property, or NULL if not found
579  */
580 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
581
582 /**
583  * ofnode_is_available() - check if a node is marked available
584  *
585  * @node: node to check
586  * @return true if node's 'status' property is "okay" (or is missing)
587  */
588 bool ofnode_is_available(ofnode node);
589
590 /**
591  * ofnode_get_addr_size() - get address and size from a property
592  *
593  * This does no address translation. It simply reads an property that contains
594  * an address and a size value, one after the other.
595  *
596  * @node: node to read from
597  * @propname: property to read
598  * @sizep: place to put size value (on success)
599  * @return address value, or FDT_ADDR_T_NONE on error
600  */
601 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
602                                  phys_size_t *sizep);
603
604 /**
605  * ofnode_read_u8_array_ptr() - find an 8-bit array
606  *
607  * Look up a property in a node and return a pointer to its contents as a
608  * byte array of given length. The property must have at least enough data
609  * for the array (count bytes). It may have more, but this will be ignored.
610  * The data is not copied.
611  *
612  * @node        node to examine
613  * @propname    name of property to find
614  * @sz          number of array elements
615  * @return pointer to byte array if found, or NULL if the property is not
616  *              found or there is not enough data
617  */
618 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
619                                         size_t sz);
620
621 /**
622  * ofnode_read_pci_addr() - look up a PCI address
623  *
624  * Look at an address property in a node and return the PCI address which
625  * corresponds to the given type in the form of fdt_pci_addr.
626  * The property must hold one fdt_pci_addr with a lengh.
627  *
628  * @node        node to examine
629  * @type        pci address type (FDT_PCI_SPACE_xxx)
630  * @propname    name of property to find
631  * @addr        returns pci address in the form of fdt_pci_addr
632  * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
633  *              format of the property was invalid, -ENXIO if the requested
634  *              address type was not found
635  */
636 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
637                          const char *propname, struct fdt_pci_addr *addr);
638
639 /**
640  * ofnode_read_pci_vendev() - look up PCI vendor and device id
641  *
642  * Look at the compatible property of a device node that represents a PCI
643  * device and extract pci vendor id and device id from it.
644  *
645  * @param node          node to examine
646  * @param vendor        vendor id of the pci device
647  * @param device        device id of the pci device
648  * @return 0 if ok, negative on error
649  */
650 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
651
652 /**
653  * ofnode_read_addr_cells() - Get the number of address cells for a node
654  *
655  * This walks back up the tree to find the closest #address-cells property
656  * which controls the given node.
657  *
658  * @node: Node to check
659  * @return number of address cells this node uses
660  */
661 int ofnode_read_addr_cells(ofnode node);
662
663 /**
664  * ofnode_read_size_cells() - Get the number of size cells for a node
665  *
666  * This walks back up the tree to find the closest #size-cells property
667  * which controls the given node.
668  *
669  * @node: Node to check
670  * @return number of size cells this node uses
671  */
672 int ofnode_read_size_cells(ofnode node);
673
674 /**
675  * ofnode_read_simple_addr_cells() - Get the address cells property in a node
676  *
677  * This function matches fdt_address_cells().
678  *
679  * @np: Node pointer to check
680  * @return value of #address-cells property in this node, or 2 if none
681  */
682 int ofnode_read_simple_addr_cells(ofnode node);
683
684 /**
685  * ofnode_read_simple_size_cells() - Get the size cells property in a node
686  *
687  * This function matches fdt_size_cells().
688  *
689  * @np: Node pointer to check
690  * @return value of #size-cells property in this node, or 2 if none
691  */
692 int ofnode_read_simple_size_cells(ofnode node);
693
694 /**
695  * ofnode_pre_reloc() - check if a node should be bound before relocation
696  *
697  * Device tree nodes can be marked as needing-to-be-bound in the loader stages
698  * via special device tree properties.
699  *
700  * Before relocation this function can be used to check if nodes are required
701  * in either SPL or TPL stages.
702  *
703  * After relocation and jumping into the real U-Boot binary it is possible to
704  * determine if a node was bound in one of SPL/TPL stages.
705  *
706  * There are 4 settings currently in use
707  * - u-boot,dm-pre-proper: U-Boot proper pre-relocation only
708  * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
709  *   Existing platforms only use it to indicate nodes needed in
710  *   SPL. Should probably be replaced by u-boot,dm-spl for
711  *   new platforms.
712  * - u-boot,dm-spl: SPL and U-Boot pre-relocation
713  * - u-boot,dm-tpl: TPL and U-Boot pre-relocation
714  *
715  * @node: node to check
716  * @return true if node is needed in SPL/TL, false otherwise
717  */
718 bool ofnode_pre_reloc(ofnode node);
719
720 /**
721  * ofnode_read_resource() - Read a resource from a node
722  *
723  * Read resource information from a node at the given index
724  *
725  * @node: Node to read from
726  * @index: Index of resource to read (0 = first)
727  * @res: Returns resource that was read, on success
728  * @return 0 if OK, -ve on error
729  */
730 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
731
732 /**
733  * ofnode_read_resource_byname() - Read a resource from a node by name
734  *
735  * Read resource information from a node matching the given name. This uses a
736  * 'reg-names' string list property with the names matching the associated
737  * 'reg' property list.
738  *
739  * @node: Node to read from
740  * @name: Name of resource to read
741  * @res: Returns resource that was read, on success
742  * @return 0 if OK, -ve on error
743  */
744 int ofnode_read_resource_byname(ofnode node, const char *name,
745                                 struct resource *res);
746
747 /**
748  * ofnode_by_compatible() - Find the next compatible node
749  *
750  * Find the next node after @from that is compatible with @compat
751  *
752  * @from: ofnode to start from (use ofnode_null() to start at the beginning)
753  * @compat: Compatible string to match
754  * @return ofnode found, or ofnode_null() if none
755  */
756 ofnode ofnode_by_compatible(ofnode from, const char *compat);
757
758 /**
759  * ofnode_by_prop_value() - Find the next node with given property value
760  *
761  * Find the next node after @from that has a @propname with a value
762  * @propval and a length @proplen.
763  *
764  * @from: ofnode to start from (use ofnode_null() to start at the
765  * beginning) @propname: property name to check @propval: property value to
766  * search for @proplen: length of the value in propval @return ofnode
767  * found, or ofnode_null() if none
768  */
769 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
770                             const void *propval, int proplen);
771
772 /**
773  * ofnode_for_each_subnode() - iterate over all subnodes of a parent
774  *
775  * @node:       child node (ofnode, lvalue)
776  * @parent:     parent node (ofnode)
777  *
778  * This is a wrapper around a for loop and is used like so:
779  *
780  *      ofnode node;
781  *
782  *      ofnode_for_each_subnode(node, parent) {
783  *              Use node
784  *              ...
785  *      }
786  *
787  * Note that this is implemented as a macro and @node is used as
788  * iterator in the loop. The parent variable can be a constant or even a
789  * literal.
790  */
791 #define ofnode_for_each_subnode(node, parent) \
792         for (node = ofnode_first_subnode(parent); \
793              ofnode_valid(node); \
794              node = ofnode_next_subnode(node))
795
796 /**
797  * ofnode_translate_address() - Translate a device-tree address
798  *
799  * Translate an address from the device-tree into a CPU physical address. This
800  * function walks up the tree and applies the various bus mappings along the
801  * way.
802  *
803  * @ofnode: Device tree node giving the context in which to translate the
804  *          address
805  * @in_addr: pointer to the address to translate
806  * @return the translated address; OF_BAD_ADDR on error
807  */
808 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
809
810 /**
811  * ofnode_translate_dma_address() - Translate a device-tree DMA address
812  *
813  * Translate a DMA address from the device-tree into a CPU physical address.
814  * This function walks up the tree and applies the various bus mappings along
815  * the way.
816  *
817  * @ofnode: Device tree node giving the context in which to translate the
818  *          DMA address
819  * @in_addr: pointer to the DMA address to translate
820  * @return the translated DMA address; OF_BAD_ADDR on error
821  */
822 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr);
823
824 /**
825  * ofnode_device_is_compatible() - check if the node is compatible with compat
826  *
827  * This allows to check whether the node is comaptible with the compat.
828  *
829  * @node:       Device tree node for which compatible needs to be verified.
830  * @compat:     Compatible string which needs to verified in the given node.
831  * @return true if OK, false if the compatible is not found
832  */
833 int ofnode_device_is_compatible(ofnode node, const char *compat);
834
835 /**
836  * ofnode_write_prop() - Set a property of a ofnode
837  *
838  * Note that the value passed to the function is *not* allocated by the
839  * function itself, but must be allocated by the caller if necessary.
840  *
841  * @node:       The node for whose property should be set
842  * @propname:   The name of the property to set
843  * @len:        The length of the new value of the property
844  * @value:      The new value of the property (must be valid prior to calling
845  *              the function)
846  * @return 0 if successful, -ve on error
847  */
848 int ofnode_write_prop(ofnode node, const char *propname, int len,
849                       const void *value);
850
851 /**
852  * ofnode_write_string() - Set a string property of a ofnode
853  *
854  * Note that the value passed to the function is *not* allocated by the
855  * function itself, but must be allocated by the caller if necessary.
856  *
857  * @node:       The node for whose string property should be set
858  * @propname:   The name of the string property to set
859  * @value:      The new value of the string property (must be valid prior to
860  *              calling the function)
861  * @return 0 if successful, -ve on error
862  */
863 int ofnode_write_string(ofnode node, const char *propname, const char *value);
864
865 /**
866  * ofnode_set_enabled() - Enable or disable a device tree node given by its
867  *                        ofnode
868  *
869  * This function effectively sets the node's "status" property to either "okay"
870  * or "disable", hence making it available for driver model initialization or
871  * not.
872  *
873  * @node:       The node to enable
874  * @value:      Flag that tells the function to either disable or enable the
875  *              node
876  * @return 0 if successful, -ve on error
877  */
878 int ofnode_set_enabled(ofnode node, bool value);
879
880 #endif