2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 /* TODO(sjg@chromium.org): Drop fdtdec.h include */
15 /* Enable checks to protect against invalid calls */
19 * ofnode - reference to a device tree node
21 * This union can hold either a straightforward pointer to a struct device_node
22 * in the live device tree, or an offset within the flat device tree. In the
23 * latter case, the pointer value is just the integer offset within the flat DT.
25 * Thus we can reference nodes in both the live tree (once available) and the
26 * flat tree (until then). Functions are available to translate between an
27 * ofnode and either an offset or a struct device_node *.
29 * The reference can also hold a null offset, in which case the pointer value
30 * here is NULL. This corresponds to a struct device_node * value of
31 * NULL, or an offset of -1.
33 * There is no ambiguity as to whether ofnode holds an offset or a node
34 * pointer: when the live tree is active it holds a node pointer, otherwise it
35 * holds an offset. The value itself does not need to be unique and in theory
36 * the same value could point to a valid device node or a valid offset. We
37 * could arrange for a unique value to be used (e.g. by making the pointer
38 * point to an offset within the flat device tree in the case of an offset) but
39 * this increases code size slightly due to the subtraction. Since it offers no
40 * real benefit, the approach described here seems best.
42 * For now these points use constant types, since we don't allow writing
45 * @np: Pointer to device node, used for live tree
46 * @flat_ptr: Pointer into flat device tree, used for flat tree. Note that this
47 * is not a really a pointer to a node: it is an offset value. See above.
49 typedef union ofnode_union {
50 const struct device_node *np; /* will be used for future live tree */
54 struct ofnode_phandle_args {
57 uint32_t args[OF_MAX_PHANDLE_ARGS];
61 * _ofnode_to_np() - convert an ofnode to a live DT node pointer
63 * This cannot be called if the reference contains an offset.
65 * @node: Reference containing struct device_node * (possibly invalid)
66 * @return pointer to device node (can be NULL)
68 static inline const struct device_node *ofnode_to_np(ofnode node)
71 if (!of_live_active())
78 * ofnode_to_offset() - convert an ofnode to a flat DT offset
80 * This cannot be called if the reference contains a node pointer.
82 * @node: Reference containing offset (possibly invalid)
83 * @return DT offset (can be -1)
85 static inline int ofnode_to_offset(ofnode node)
91 return node.of_offset;
95 * ofnode_valid() - check if an ofnode is valid
97 * @return true if the reference contains a valid ofnode, false if it is NULL
99 static inline bool ofnode_valid(ofnode node)
101 if (of_live_active())
102 return node.np != NULL;
104 return node.of_offset != -1;
108 * offset_to_ofnode() - convert a DT offset to an ofnode
110 * @of_offset: DT offset (either valid, or -1)
111 * @return reference to the associated DT offset
113 static inline ofnode offset_to_ofnode(int of_offset)
117 if (of_live_active())
120 node.of_offset = of_offset;
126 * np_to_ofnode() - convert a node pointer to an ofnode
128 * @np: Live node pointer (can be NULL)
129 * @return reference to the associated node pointer
131 static inline ofnode np_to_ofnode(const struct device_node *np)
141 * ofnode_is_np() - check if a reference is a node pointer
143 * This function associated that if there is a valid live tree then all
144 * references will use it. This is because using the flat DT when the live tree
145 * is valid is not permitted.
147 * @node: reference to check (possibly invalid)
148 * @return true if the reference is a live node pointer, false if it is a DT
151 static inline bool ofnode_is_np(ofnode node)
155 * Check our assumption that flat tree offsets are not used when a
156 * live tree is in use.
158 assert(!ofnode_valid(node) ||
159 (of_live_active() ? _ofnode_to_np(node)
160 : _ofnode_to_np(node)));
162 return of_live_active() && ofnode_valid(node);
166 * ofnode_equal() - check if two references are equal
168 * @return true if equal, else false
170 static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
172 /* We only need to compare the contents */
173 return ref1.of_offset == ref2.of_offset;
177 * ofnode_null() - Obtain a null ofnode
179 * This returns an ofnode which points to no node. It works both with the flat
182 static inline ofnode ofnode_null(void)
186 if (of_live_active())
195 * ofnode_read_u32() - Read a 32-bit integer from a property
197 * @ref: valid node reference to read property from
198 * @propname: name of the property to read from
199 * @outp: place to put value (if found)
200 * @return 0 if OK, -ve on error
202 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);
205 * ofnode_read_s32() - Read a 32-bit integer from a property
207 * @ref: valid node reference to read property from
208 * @propname: name of the property to read from
209 * @outp: place to put value (if found)
210 * @return 0 if OK, -ve on error
212 static inline int ofnode_read_s32(ofnode node, const char *propname,
215 return ofnode_read_u32(node, propname, (u32 *)out_value);
219 * ofnode_read_u32_default() - Read a 32-bit integer from a property
221 * @ref: valid node reference to read property from
222 * @propname: name of the property to read from
223 * @def: default value to return if the property has no value
224 * @return property value, or @def if not found
226 int ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);
229 * ofnode_read_s32_default() - Read a 32-bit integer from a property
231 * @ref: valid node reference to read property from
232 * @propname: name of the property to read from
233 * @def: default value to return if the property has no value
234 * @return property value, or @def if not found
236 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def);
239 * ofnode_read_string() - Read a string from a property
241 * @ref: valid node reference to read property from
242 * @propname: name of the property to read
243 * @return string from property value, or NULL if there is no such property
245 const char *ofnode_read_string(ofnode node, const char *propname);
248 * ofnode_read_u32_array() - Find and read an array of 32 bit integers
250 * @node: valid node reference to read property from
251 * @propname: name of the property to read
252 * @out_values: pointer to return value, modified only if return value is 0
253 * @sz: number of array elements to read
255 * Search for a property in a device node and read 32-bit value(s) from
256 * it. Returns 0 on success, -EINVAL if the property does not exist,
257 * -ENODATA if property does not have a value, and -EOVERFLOW if the
258 * property data isn't large enough.
260 * The out_values is modified only if a valid u32 value can be decoded.
262 int ofnode_read_u32_array(ofnode node, const char *propname,
263 u32 *out_values, size_t sz);
266 * ofnode_read_bool() - read a boolean value from a property
268 * @node: valid node reference to read property from
269 * @propname: name of property to read
270 * @return true if property is present (meaning true), false if not present
272 bool ofnode_read_bool(ofnode node, const char *propname);
275 * ofnode_find_subnode() - find a named subnode of a parent node
277 * @node: valid reference to parent node
278 * @subnode_name: name of subnode to find
279 * @return reference to subnode (which can be invalid if there is no such
282 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name);
285 * ofnode_first_subnode() - find the first subnode of a parent node
287 * @node: valid reference to a valid parent node
288 * @return reference to the first subnode (which can be invalid if the parent
289 * node has no subnodes)
291 ofnode ofnode_first_subnode(ofnode node);
294 * ofnode_next_subnode() - find the next sibling of a subnode
296 * @node: valid reference to previous node (sibling)
297 * @return reference to the next subnode (which can be invalid if the node
298 * has no more siblings)
300 ofnode ofnode_next_subnode(ofnode node);
303 * ofnode_get_name() - get the name of a node
305 * @node: valid node to look up
306 * @return name or node
308 const char *ofnode_get_name(ofnode node);
311 * ofnode_read_size() - read the size of a property
313 * @node: node to check
314 * @propname: property to check
315 * @return size of property if present, or -EINVAL if not
317 int ofnode_read_size(ofnode node, const char *propname);
320 * ofnode_get_addr_index() - get an address from a node
322 * This reads the register address from a node
324 * @node: node to read from
325 * @index: Index of address to read (0 for first)
326 * @return address, or FDT_ADDR_T_NONE if not present or invalid
328 phys_addr_t ofnode_get_addr_index(ofnode node, int index);
331 * ofnode_get_addr() - get an address from a node
333 * This reads the register address from a node
335 * @node: node to read from
336 * @return address, or FDT_ADDR_T_NONE if not present or invalid
338 phys_addr_t ofnode_get_addr(ofnode node);
341 * ofnode_stringlist_search() - find a string in a string list and return index
343 * Note that it is possible for this function to succeed on property values
344 * that are not NUL-terminated. That's because the function will stop after
345 * finding the first occurrence of @string. This can for example happen with
346 * small-valued cell properties, such as #address-cells, when searching for
349 * @node: node to check
350 * @propname: name of the property containing the string list
351 * @string: string to look up in the string list
354 * the index of the string in the list of strings
355 * -ENODATA if the property is not found
356 * -EINVAL on some other error
358 int ofnode_stringlist_search(ofnode node, const char *propname,
362 * ofnode_read_string_index() - obtain an indexed string from a string list
364 * Note that this will successfully extract strings from properties with
365 * non-NUL-terminated values. For example on small-valued cell properties
366 * this function will return the empty string.
368 * If non-NULL, the length of the string (on success) or a negative error-code
369 * (on failure) will be stored in the integer pointer to by lenp.
371 * @node: node to check
372 * @propname: name of the property containing the string list
373 * @index: index of the string to return
374 * @lenp: return location for the string length or an error code on failure
377 * length of string, if found or -ve error value if not found
379 int ofnode_read_string_index(ofnode node, const char *propname, int index,
383 * ofnode_read_string_count() - find the number of strings in a string list
385 * @node: node to check
386 * @propname: name of the property containing the string list
388 * number of strings in the list, or -ve error value if not found
390 int ofnode_read_string_count(ofnode node, const char *property);
393 * ofnode_parse_phandle_with_args() - Find a node pointed by phandle in a list
395 * This function is useful to parse lists of phandles and their arguments.
396 * Returns 0 on success and fills out_args, on error returns appropriate
399 * Caller is responsible to call of_node_put() on the returned out_args->np
413 * list = <&phandle1 1 2 &phandle2 3>;
416 * To get a device_node of the `node2' node you may call this:
417 * ofnode_parse_phandle_with_args(node3, "list", "#list-cells", 0, 1, &args);
419 * @node: device tree node containing a list
420 * @list_name: property name that contains a list
421 * @cells_name: property name that specifies phandles' arguments count
422 * @cells_count: Cell count to use if @cells_name is NULL
423 * @index: index of a phandle to parse out
424 * @out_args: optional pointer to output arguments structure (will be filled)
425 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
426 * @list_name does not exist, -EINVAL if a phandle was not found,
427 * @cells_name could not be found, the arguments were truncated or there
428 * were too many arguments.
430 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
431 const char *cells_name, int cell_count,
433 struct ofnode_phandle_args *out_args);
436 * ofnode_path() - find a node by full path
438 * @path: Full path to node, e.g. "/bus/spi@1"
439 * @return reference to the node found. Use ofnode_valid() to check if it exists
441 ofnode ofnode_path(const char *path);
444 * ofnode_get_chosen_prop() - get the value of a chosen property
446 * This looks for a property within the /chosen node and returns its value
448 * @propname: Property name to look for
450 const char *ofnode_get_chosen_prop(const char *propname);
453 * ofnode_get_chosen_node() - get the chosen node
455 * @return the chosen node if present, else ofnode_null()
457 ofnode ofnode_get_chosen_node(const char *name);
459 struct display_timing;
461 * ofnode_decode_display_timing() - decode display timings
463 * Decode display timings from the supplied 'display-timings' node.
464 * See doc/device-tree-bindings/video/display-timing.txt for binding
467 * @node 'display-timing' node containing the timing subnodes
468 * @index Index number to read (0=first timing subnode)
469 * @config Place to put timings
470 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
472 int ofnode_decode_display_timing(ofnode node, int index,
473 struct display_timing *config);
476 * ofnode_read_prop()- - read a node property
478 * @node: node to read
479 * @propname: property to read
480 * @lenp: place to put length on success
481 * @return pointer to property, or NULL if not found
483 const u32 *ofnode_read_prop(ofnode node, const char *propname, int *lenp);
486 * ofnode_is_available() - check if a node is marked available
488 * @node: node to check
489 * @return true if node's 'status' property is "okay" (or is missing)
491 bool ofnode_is_available(ofnode node);
494 * ofnode_get_addr_size() - get address and size from a property
496 * This does no address translation. It simply reads an property that contains
497 * an address and a size value, one after the other.
499 * @node: node to read from
500 * @propname: property to read
501 * @sizep: place to put size value (on success)
502 * @return address value, or FDT_ADDR_T_NONE on error
504 phys_addr_t ofnode_get_addr_size(ofnode node, const char *propname,
508 * ofnode_read_u8_array_ptr() - find an 8-bit array
510 * Look up a property in a node and return a pointer to its contents as a
511 * byte array of given length. The property must have at least enough data
512 * for the array (count bytes). It may have more, but this will be ignored.
513 * The data is not copied.
515 * @node node to examine
516 * @propname name of property to find
517 * @sz number of array elements
518 * @return pointer to byte array if found, or NULL if the property is not
519 * found or there is not enough data
521 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
525 * ofnode_read_pci_addr() - look up a PCI address
527 * Look at an address property in a node and return the PCI address which
528 * corresponds to the given type in the form of fdt_pci_addr.
529 * The property must hold one fdt_pci_addr with a lengh.
531 * @node node to examine
532 * @type pci address type (FDT_PCI_SPACE_xxx)
533 * @propname name of property to find
534 * @addr returns pci address in the form of fdt_pci_addr
535 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
536 * format of the property was invalid, -ENXIO if the requested
537 * address type was not found
539 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
540 const char *propname, struct fdt_pci_addr *addr);
543 * ofnode_read_addr_cells() - Get the number of address cells for a node
545 * This walks back up the tree to find the closest #address-cells property
546 * which controls the given node.
548 * @node: Node to check
549 * @return number of address cells this node uses
551 int ofnode_read_addr_cells(ofnode node);
554 * ofnode_read_size_cells() - Get the number of size cells for a node
556 * This walks back up the tree to find the closest #size-cells property
557 * which controls the given node.
559 * @node: Node to check
560 * @return number of size cells this node uses
562 int ofnode_read_size_cells(ofnode node);
565 * ofnode_pre_reloc() - check if a node should be bound before relocation
567 * Device tree nodes can be marked as needing-to-be-bound in the loader stages
568 * via special device tree properties.
570 * Before relocation this function can be used to check if nodes are required
571 * in either SPL or TPL stages.
573 * After relocation and jumping into the real U-Boot binary it is possible to
574 * determine if a node was bound in one of SPL/TPL stages.
576 * There are 3 settings currently in use
578 * - u-boot,dm-pre-reloc: legacy and indicates any of TPL or SPL
579 * Existing platforms only use it to indicate nodes needed in
580 * SPL. Should probably be replaced by u-boot,dm-spl for
583 * @node: node to check
584 * @eturns true if node is needed in SPL/TL, false otherwise
586 bool ofnode_pre_reloc(ofnode node);