1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 /* TODO(sjg@chromium.org): Drop fdtdec.h include */
14 /* Enable checks to protect against invalid calls */
20 * ofnode - reference to a device tree node
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.
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 *.
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.
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.
43 * For now these points use constant types, since we don't allow writing
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.
50 typedef union ofnode_union {
51 const struct device_node *np; /* will be used for future live tree */
55 struct ofnode_phandle_args {
58 uint32_t args[OF_MAX_PHANDLE_ARGS];
62 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
64 * This cannot be called if the reference contains an offset.
66 * @node: Reference containing struct device_node * (possibly invalid)
67 * @return pointer to device node (can be NULL)
69 static inline const struct device_node *ofnode_to_np(ofnode node)
72 if (!of_live_active())
79 * ofnode_to_offset() - convert an ofnode to a flat DT offset
81 * This cannot be called if the reference contains a node pointer.
83 * @node: Reference containing offset (possibly invalid)
84 * @return DT offset (can be -1)
86 static inline int ofnode_to_offset(ofnode node)
92 return node.of_offset;
96 * ofnode_valid() - check if an ofnode is valid
98 * @return true if the reference contains a valid ofnode, false if it is NULL
100 static inline bool ofnode_valid(ofnode node)
102 if (of_live_active())
103 return node.np != NULL;
105 return node.of_offset != -1;
109 * offset_to_ofnode() - convert a DT offset to an ofnode
111 * @of_offset: DT offset (either valid, or -1)
112 * @return reference to the associated DT offset
114 static inline ofnode offset_to_ofnode(int of_offset)
118 if (of_live_active())
121 node.of_offset = of_offset;
127 * np_to_ofnode() - convert a node pointer to an ofnode
129 * @np: Live node pointer (can be NULL)
130 * @return reference to the associated node pointer
132 static inline ofnode np_to_ofnode(const struct device_node *np)
142 * ofnode_is_np() - check if a reference is a node pointer
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.
148 * @node: reference to check (possibly invalid)
149 * @return true if the reference is a live node pointer, false if it is a DT
152 static inline bool ofnode_is_np(ofnode node)
156 * Check our assumption that flat tree offsets are not used when a
157 * live tree is in use.
159 assert(!ofnode_valid(node) ||
160 (of_live_active() ? _ofnode_to_np(node)
161 : _ofnode_to_np(node)));
163 return of_live_active() && ofnode_valid(node);
167 * ofnode_equal() - check if two references are equal
169 * @return true if equal, else false
171 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
173 /* We only need to compare the contents */
174 return ref1.of_offset == ref2.of_offset;
178 * ofnode_null() - Obtain a null ofnode
180 * This returns an ofnode which points to no node. It works both with the flat
183 static inline ofnode ofnode_null(void)
187 if (of_live_active())
196 * ofnode_read_u32() - Read a 32-bit integer from a property
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
203 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
206 * ofnode_read_s32() - Read a 32-bit integer from a property
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
213 static inline int ofnode_read_s32(ofnode node, const char *propname,
216 return ofnode_read_u32(node, propname, (u32 *)out_value);
220 * ofnode_read_u32_default() - Read a 32-bit integer from a property
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
227 int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
230 * ofnode_read_s32_default() - Read a 32-bit integer from a property
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
237 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
240 * ofnode_read_string() - Read a string from a property
242 * @ref: valid node reference to read property from
243 * @propname: name of the property to read
244 * @return string from property value, or NULL if there is no such property
246 const char *ofnode_read_string(ofnode node, const char *propname);
249 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
251 * @node: valid node reference to read property from
252 * @propname: name of the property to read
253 * @out_values: pointer to return value, modified only if return value is 0
254 * @sz: number of array elements to read
256 * Search for a property in a device node and read 32-bit value(s) from
257 * it. Returns 0 on success, -EINVAL if the property does not exist,
258 * -ENODATA if property does not have a value, and -EOVERFLOW if the
259 * property data isn't large enough.
261 * The out_values is modified only if a valid u32 value can be decoded.
263 int ofnode_read_u32_array(ofnode node, const char *propname,
264 u32 *out_values, size_t sz);
267 * ofnode_read_bool() - read a boolean value from a property
269 * @node: valid node reference to read property from
270 * @propname: name of property to read
271 * @return true if property is present (meaning true), false if not present
273 bool ofnode_read_bool(ofnode node, const char *propname);
276 * ofnode_find_subnode() - find a named subnode of a parent node
278 * @node: valid reference to parent node
279 * @subnode_name: name of subnode to find
280 * @return reference to subnode (which can be invalid if there is no such
283 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
286 * ofnode_first_subnode() - find the first subnode of a parent node
288 * @node: valid reference to a valid parent node
289 * @return reference to the first subnode (which can be invalid if the parent
290 * node has no subnodes)
292 ofnode ofnode_first_subnode(ofnode node);
295 * ofnode_next_subnode() - find the next sibling of a subnode
297 * @node: valid reference to previous node (sibling)
298 * @return reference to the next subnode (which can be invalid if the node
299 * has no more siblings)
301 ofnode ofnode_next_subnode(ofnode node);
304 * ofnode_get_parent() - get the ofnode's parent (enclosing ofnode)
306 * @node: valid node to look up
307 * @return ofnode reference of the parent node
309 ofnode ofnode_get_parent(ofnode node);
312 * ofnode_get_name() - get the name of a node
314 * @node: valid node to look up
315 * @return name or node
317 const char *ofnode_get_name(ofnode node);
320 * ofnode_get_by_phandle() - get ofnode from phandle
322 * @phandle: phandle to look up
323 * @return ofnode reference to the phandle
325 ofnode ofnode_get_by_phandle(uint phandle);
328 * ofnode_read_size() - read the size of a property
330 * @node: node to check
331 * @propname: property to check
332 * @return size of property if present, or -EINVAL if not
334 int ofnode_read_size(ofnode node, const char *propname);
337 * ofnode_get_addr_index() - get an address from a node
339 * This reads the register address from a node
341 * @node: node to read from
342 * @index: Index of address to read (0 for first)
343 * @return address, or FDT_ADDR_T_NONE if not present or invalid
345 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
348 * ofnode_get_addr() - get an address from a node
350 * This reads the register address from a node
352 * @node: node to read from
353 * @return address, or FDT_ADDR_T_NONE if not present or invalid
355 phys_addr_t ofnode_get_addr(ofnode node);
358 * ofnode_stringlist_search() - find a string in a string list and return index
360 * Note that it is possible for this function to succeed on property values
361 * that are not NUL-terminated. That's because the function will stop after
362 * finding the first occurrence of @string. This can for example happen with
363 * small-valued cell properties, such as #address-cells, when searching for
366 * @node: node to check
367 * @propname: name of the property containing the string list
368 * @string: string to look up in the string list
371 * the index of the string in the list of strings
372 * -ENODATA if the property is not found
373 * -EINVAL on some other error
375 int ofnode_stringlist_search(ofnode node, const char *propname,
379 * ofnode_read_string_index() - obtain an indexed string from a string list
381 * Note that this will successfully extract strings from properties with
382 * non-NUL-terminated values. For example on small-valued cell properties
383 * this function will return the empty string.
385 * If non-NULL, the length of the string (on success) or a negative error-code
386 * (on failure) will be stored in the integer pointer to by lenp.
388 * @node: node to check
389 * @propname: name of the property containing the string list
390 * @index: index of the string to return
391 * @lenp: return location for the string length or an error code on failure
394 * length of string, if found or -ve error value if not found
396 int ofnode_read_string_index(ofnode node, const char *propname, int index,
400 * ofnode_read_string_count() - find the number of strings in a string list
402 * @node: node to check
403 * @propname: name of the property containing the string list
405 * number of strings in the list, or -ve error value if not found
407 int ofnode_read_string_count(ofnode node, const char *property);
410 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
412 * This function is useful to parse lists of phandles and their arguments.
413 * Returns 0 on success and fills out_args, on error returns appropriate
416 * Caller is responsible to call of_node_put() on the returned out_args->np
430 * list = <&phandle1 1 2 &phandle2 3>;
433 * To get a device_node of the `node2' node you may call this:
434 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
436 * @node: device tree node containing a list
437 * @list_name: property name that contains a list
438 * @cells_name: property name that specifies phandles' arguments count
439 * @cells_count: Cell count to use if @cells_name is NULL
440 * @index: index of a phandle to parse out
441 * @out_args: optional pointer to output arguments structure (will be filled)
442 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
443 * @list_name does not exist, -EINVAL if a phandle was not found,
444 * @cells_name could not be found, the arguments were truncated or there
445 * were too many arguments.
447 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
448 const char *cells_name, int cell_count,
450 struct ofnode_phandle_args *out_args);
453 * ofnode_count_phandle_with_args() - Count number of phandle in a list
455 * This function is useful to count phandles into a list.
456 * Returns number of phandle on success, on error returns appropriate
459 * @node: device tree node containing a list
460 * @list_name: property name that contains a list
461 * @cells_name: property name that specifies phandles' arguments count
462 * @return number of phandle on success, -ENOENT if @list_name does not
463 * exist, -EINVAL if a phandle was not found, @cells_name could not
466 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
467 const char *cells_name);
470 * ofnode_path() - find a node by full path
472 * @path: Full path to node, e.g. "/bus/spi@1"
473 * @return reference to the node found. Use ofnode_valid() to check if it exists
475 ofnode ofnode_path(const char *path);
478 * ofnode_get_chosen_prop() - get the value of a chosen property
480 * This looks for a property within the /chosen node and returns its value
482 * @propname: Property name to look for
484 const char *ofnode_get_chosen_prop(const char *propname);
487 * ofnode_get_chosen_node() - get the chosen node
489 * @return the chosen node if present, else ofnode_null()
491 ofnode ofnode_get_chosen_node(const char *name);
493 struct display_timing;
495 * ofnode_decode_display_timing() - decode display timings
497 * Decode display timings from the supplied 'display-timings' node.
498 * See doc/device-tree-bindings/video/display-timing.txt for binding
501 * @node 'display-timing' node containing the timing subnodes
502 * @index Index number to read (0=first timing subnode)
503 * @config Place to put timings
504 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
506 int ofnode_decode_display_timing(ofnode node, int index,
507 struct display_timing *config);
510 * ofnode_get_property()- - get a pointer to the value of a node property
512 * @node: node to read
513 * @propname: property to read
514 * @lenp: place to put length on success
515 * @return pointer to property, or NULL if not found
517 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp);
520 * ofnode_is_available() - check if a node is marked available
522 * @node: node to check
523 * @return true if node's 'status' property is "okay" (or is missing)
525 bool ofnode_is_available(ofnode node);
528 * ofnode_get_addr_size() - get address and size from a property
530 * This does no address translation. It simply reads an property that contains
531 * an address and a size value, one after the other.
533 * @node: node to read from
534 * @propname: property to read
535 * @sizep: place to put size value (on success)
536 * @return address value, or FDT_ADDR_T_NONE on error
538 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
542 * ofnode_read_u8_array_ptr() - find an 8-bit array
544 * Look up a property in a node and return a pointer to its contents as a
545 * byte array of given length. The property must have at least enough data
546 * for the array (count bytes). It may have more, but this will be ignored.
547 * The data is not copied.
549 * @node node to examine
550 * @propname name of property to find
551 * @sz number of array elements
552 * @return pointer to byte array if found, or NULL if the property is not
553 * found or there is not enough data
555 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
559 * ofnode_read_pci_addr() - look up a PCI address
561 * Look at an address property in a node and return the PCI address which
562 * corresponds to the given type in the form of fdt_pci_addr.
563 * The property must hold one fdt_pci_addr with a lengh.
565 * @node node to examine
566 * @type pci address type (FDT_PCI_SPACE_xxx)
567 * @propname name of property to find
568 * @addr returns pci address in the form of fdt_pci_addr
569 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
570 * format of the property was invalid, -ENXIO if the requested
571 * address type was not found
573 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
574 const char *propname, struct fdt_pci_addr *addr);
577 * ofnode_read_addr_cells() - Get the number of address cells for a node
579 * This walks back up the tree to find the closest #address-cells property
580 * which controls the given node.
582 * @node: Node to check
583 * @return number of address cells this node uses
585 int ofnode_read_addr_cells(ofnode node);
588 * ofnode_read_size_cells() - Get the number of size cells for a node
590 * This walks back up the tree to find the closest #size-cells property
591 * which controls the given node.
593 * @node: Node to check
594 * @return number of size cells this node uses
596 int ofnode_read_size_cells(ofnode node);
599 * ofnode_read_simple_addr_cells() - Get the address cells property in a node
601 * This function matches fdt_address_cells().
603 * @np: Node pointer to check
604 * @return value of #address-cells property in this node, or 2 if none
606 int ofnode_read_simple_addr_cells(ofnode node);
609 * ofnode_read_simple_size_cells() - Get the size cells property in a node
611 * This function matches fdt_size_cells().
613 * @np: Node pointer to check
614 * @return value of #size-cells property in this node, or 2 if none
616 int ofnode_read_simple_size_cells(ofnode node);
619 * ofnode_pre_reloc() - check if a node should be bound before relocation
621 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
622 * via special device tree properties.
624 * Before relocation this function can be used to check if nodes are required
625 * in either SPL or TPL stages.
627 * After relocation and jumping into the real U-Boot binary it is possible to
628 * determine if a node was bound in one of SPL/TPL stages.
630 * There are 3 settings currently in use
632 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
633 * Existing platforms only use it to indicate nodes needed in
634 * SPL. Should probably be replaced by u-boot,dm-spl for
637 * @node: node to check
638 * @eturns true if node is needed in SPL/TL, false otherwise
640 bool ofnode_pre_reloc(ofnode node);
642 int ofnode_read_resource(ofnode node, uint index, struct resource *res);
643 int ofnode_read_resource_byname(ofnode node, const char *name,
644 struct resource *res);
647 * ofnode_for_each_subnode() - iterate over all subnodes of a parent
649 * @node: child node (ofnode, lvalue)
650 * @parent: parent node (ofnode)
652 * This is a wrapper around a for loop and is used like so:
656 * ofnode_for_each_subnode(node, parent) {
661 * Note that this is implemented as a macro and @node is used as
662 * iterator in the loop. The parent variable can be a constant or even a
665 #define ofnode_for_each_subnode(node, parent) \
666 for (node = ofnode_first_subnode(parent); \
667 ofnode_valid(node); \
668 node = ofnode_next_subnode(node))
671 * ofnode_translate_address() - Tranlate a device-tree address
673 * Translate an address from the device-tree into a CPU physical address. This
674 * function walks up the tree and applies the various bus mappings along the
677 * @ofnode: Device tree node giving the context in which to translate the
679 * @in_addr: pointer to the address to translate
680 * @return the translated address; OF_BAD_ADDR on error
682 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr);
685 * ofnode_device_is_compatible() - check if the node is compatible with compat
687 * This allows to check whether the node is comaptible with the compat.
689 * @node: Device tree node for which compatible needs to be verified.
690 * @compat: Compatible string which needs to verified in the given node.
691 * @return true if OK, false if the compatible is not found
693 int ofnode_device_is_compatible(ofnode node, const char *compat);