1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2011 The Chromium OS Authors.
10 * This file contains convenience functions for decoding useful and
11 * enlightening information from FDTs. It is intended to be used by device
12 * drivers and board-specific code within U-Boot. It aims to reduce the
13 * amount of FDT munging required within U-Boot itself, so that driver code
14 * changes to support FDT are minimized.
17 #include <linux/libfdt.h>
21 * A typedef for a physical address. Note that fdt data is always big
22 * endian even on a litle endian machine.
24 typedef phys_addr_t fdt_addr_t;
25 typedef phys_size_t fdt_size_t;
27 #define FDT_ADDR_T_NONE (-1U)
28 #define FDT_SIZE_T_NONE (-1U)
30 #ifdef CONFIG_PHYS_64BIT
31 #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
32 #define fdt_size_to_cpu(reg) be64_to_cpu(reg)
33 #define cpu_to_fdt_addr(reg) cpu_to_be64(reg)
34 #define cpu_to_fdt_size(reg) cpu_to_be64(reg)
35 typedef fdt64_t fdt_val_t;
37 #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
38 #define fdt_size_to_cpu(reg) be32_to_cpu(reg)
39 #define cpu_to_fdt_addr(reg) cpu_to_be32(reg)
40 #define cpu_to_fdt_size(reg) cpu_to_be32(reg)
41 typedef fdt32_t fdt_val_t;
44 /* Information obtained about memory from the FDT */
52 #ifdef CONFIG_SPL_BUILD
59 * enum fdt_source_t - indicates where the devicetree came from
61 * These are listed in approximate order of desirability after FDTSRC_NONE
63 * @FDTSRC_SEPARATE: Appended to U-Boot. This is the normal approach if U-Boot
64 * is the only firmware being booted
65 * @FDTSRC_FIT: Found in a multi-dtb FIT. This should be used when U-Boot must
66 * select a devicetree from many options
67 * @FDTSRC_BOARD: Located by custom board code. This should only be used when
68 * the prior stage does not support FDTSRC_PASSAGE
69 * @FDTSRC_EMBED: Embedded into U-Boot executable. This should onyl be used when
70 * U-Boot is packaged as an ELF file, e.g. for debugging purposes
71 * @FDTSRC_ENV: Provided by the fdtcontroladdr environment variable. This should
72 * be used for debugging/development only
73 * @FDTSRC_NONE: No devicetree at all
84 * Information about a resource. start is the first address of the resource
85 * and end is the last address (inclusive). The length of the resource will
86 * be equal to: end - start + 1.
94 FDT_PCI_SPACE_CONFIG = 0,
95 FDT_PCI_SPACE_IO = 0x01000000,
96 FDT_PCI_SPACE_MEM32 = 0x02000000,
97 FDT_PCI_SPACE_MEM64 = 0x03000000,
98 FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
99 FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
102 #define FDT_PCI_ADDR_CELLS 3
103 #define FDT_PCI_SIZE_CELLS 2
104 #define FDT_PCI_REG_SIZE \
105 ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
108 * The Open Firmware spec defines PCI physical address as follows:
110 * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
112 * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
113 * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
114 * phys.lo cell: llllllll llllllll llllllll llllllll
118 * n: is 0 if the address is relocatable, 1 otherwise
119 * p: is 1 if addressable region is prefetchable, 0 otherwise
120 * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
121 * (for Memory), or below 64KB (for relocatable I/O)
122 * ss: is the space code, denoting the address space
123 * bbbbbbbb: is the 8-bit Bus Number
124 * ddddd: is the 5-bit Device Number
125 * fff: is the 3-bit Function Number
126 * rrrrrrrr: is the 8-bit Register Number
127 * hhhhhhhh: is a 32-bit unsigned number
128 * llllllll: is a 32-bit unsigned number
130 struct fdt_pci_addr {
136 extern u8 __dtb_dt_begin[]; /* embedded device tree blob */
137 extern u8 __dtb_dt_spl_begin[]; /* embedded device tree blob for SPL/TPL */
139 /* Get a pointer to the embedded devicetree, if there is one, else NULL */
140 static inline u8 *dtb_dt_embedded(void)
142 #ifdef CONFIG_OF_EMBED
143 # ifdef CONFIG_SPL_BUILD
144 return __dtb_dt_spl_begin;
146 return __dtb_dt_begin;
154 * Compute the size of a resource.
156 * @param res the resource to operate on
157 * @return the size of the resource
159 static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
161 return res->end - res->start + 1;
165 * Compat types that we know about and for which we might have drivers.
166 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
171 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
172 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
173 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
174 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
175 /* Tegra124 XUSB pad controller */
176 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
177 /* Tegra210 XUSB pad controller */
178 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
179 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
180 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
181 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
182 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
183 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
184 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
185 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
186 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
187 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
188 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
189 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
190 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
191 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
192 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
193 COMPAT_SUNXI_NAND, /* SUNXI NAND controller */
194 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
195 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
196 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
197 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
198 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
199 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
200 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
201 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
202 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
203 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
204 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
209 #define MAX_PHANDLE_ARGS 16
210 struct fdtdec_phandle_args {
213 uint32_t args[MAX_PHANDLE_ARGS];
217 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
219 * This function is useful to parse lists of phandles and their arguments.
232 * list = <&phandle1 1 2 &phandle2 3>;
235 * To get a device_node of the `node2' node you may call this:
236 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
239 * (This function is a modified version of __of_parse_phandle_with_args() from
242 * @blob: Pointer to device tree
243 * @src_node: Offset of device tree node containing a list
244 * @list_name: property name that contains a list
245 * @cells_name: property name that specifies the phandles' arguments count,
246 * or NULL to use @cells_count
247 * @cells_count: Cell count to use if @cells_name is NULL
248 * @index: index of a phandle to parse out
249 * @out_args: optional pointer to output arguments structure (will be filled)
250 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
251 * @list_name does not exist, a phandle was not found, @cells_name
252 * could not be found, the arguments were truncated or there were too
256 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
257 const char *list_name,
258 const char *cells_name,
259 int cell_count, int index,
260 struct fdtdec_phandle_args *out_args);
263 * Find the next numbered alias for a peripheral. This is used to enumerate
264 * all the peripherals of a certain type.
266 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
267 * this function will return a pointer to the node the alias points to, and
268 * then update *upto to 1. Next time you call this function, the next node
271 * All nodes returned will match the compatible ID, as it is assumed that
272 * all peripherals use the same driver.
274 * @param blob FDT blob to use
275 * @param name Root name of alias to search for
276 * @param id Compatible ID to look for
277 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
279 int fdtdec_next_alias(const void *blob, const char *name,
280 enum fdt_compat_id id, int *upto);
283 * Find the compatible ID for a given node.
285 * Generally each node has at least one compatible string attached to it.
286 * This function looks through our list of known compatible strings and
287 * returns the corresponding ID which matches the compatible string.
289 * @param blob FDT blob to use
290 * @param node Node containing compatible string to find
291 * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
293 enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
296 * Find the next compatible node for a peripheral.
298 * Do the first call with node = 0. This function will return a pointer to
299 * the next compatible node. Next time you call this function, pass the
300 * value returned, and the next node will be provided.
302 * @param blob FDT blob to use
303 * @param node Start node for search
304 * @param id Compatible ID to look for (enum fdt_compat_id)
305 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
307 int fdtdec_next_compatible(const void *blob, int node,
308 enum fdt_compat_id id);
311 * Find the next compatible subnode for a peripheral.
313 * Do the first call with node set to the parent and depth = 0. This
314 * function will return the offset of the next compatible node. Next time
315 * you call this function, pass the node value returned last time, with
316 * depth unchanged, and the next node will be provided.
318 * @param blob FDT blob to use
319 * @param node Start node for search
320 * @param id Compatible ID to look for (enum fdt_compat_id)
321 * @param depthp Current depth (set to 0 before first call)
322 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
324 int fdtdec_next_compatible_subnode(const void *blob, int node,
325 enum fdt_compat_id id, int *depthp);
328 * Look up an address property in a node and return the parsed address, and
329 * optionally the parsed size.
331 * This variant assumes a known and fixed number of cells are used to
332 * represent the address and size.
334 * You probably don't want to use this function directly except to parse
335 * non-standard properties, and never to parse the "reg" property. Instead,
336 * use one of the "auto" variants below, which automatically honor the
337 * #address-cells and #size-cells properties in the parent node.
339 * @param blob FDT blob
340 * @param node node to examine
341 * @param prop_name name of property to find
342 * @param index which address to retrieve from a list of addresses. Often 0.
343 * @param na the number of cells used to represent an address
344 * @param ns the number of cells used to represent a size
345 * @param sizep a pointer to store the size into. Use NULL if not required
346 * @param translate Indicates whether to translate the returned value
347 * using the parent node's ranges property.
348 * @return address, if found, or FDT_ADDR_T_NONE if not
350 fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
351 const char *prop_name, int index, int na, int ns,
352 fdt_size_t *sizep, bool translate);
355 * Look up an address property in a node and return the parsed address, and
356 * optionally the parsed size.
358 * This variant automatically determines the number of cells used to represent
359 * the address and size by parsing the provided parent node's #address-cells
360 * and #size-cells properties.
362 * @param blob FDT blob
363 * @param parent parent node of @node
364 * @param node node to examine
365 * @param prop_name name of property to find
366 * @param index which address to retrieve from a list of addresses. Often 0.
367 * @param sizep a pointer to store the size into. Use NULL if not required
368 * @param translate Indicates whether to translate the returned value
369 * using the parent node's ranges property.
370 * @return address, if found, or FDT_ADDR_T_NONE if not
372 fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
373 int node, const char *prop_name, int index, fdt_size_t *sizep,
377 * Look up an address property in a node and return the parsed address, and
378 * optionally the parsed size.
380 * This variant automatically determines the number of cells used to represent
381 * the address and size by parsing the parent node's #address-cells
382 * and #size-cells properties. The parent node is automatically found.
384 * The automatic parent lookup implemented by this function is slow.
385 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
388 * @param blob FDT blob
389 * @param parent parent node of @node
390 * @param node node to examine
391 * @param prop_name name of property to find
392 * @param index which address to retrieve from a list of addresses. Often 0.
393 * @param sizep a pointer to store the size into. Use NULL if not required
394 * @param translate Indicates whether to translate the returned value
395 * using the parent node's ranges property.
396 * @return address, if found, or FDT_ADDR_T_NONE if not
398 fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
399 const char *prop_name, int index, fdt_size_t *sizep,
403 * Look up an address property in a node and return the parsed address.
405 * This variant hard-codes the number of cells used to represent the address
406 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
407 * always returns the first address value in the property (index 0).
409 * Use of this function is not recommended due to the hard-coding of cell
410 * counts. There is no programmatic validation that these hard-coded values
411 * actually match the device tree content in any way at all. This assumption
412 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
413 * set in the U-Boot build and exercising strict control over DT content to
414 * ensure use of matching #address-cells/#size-cells properties. However, this
415 * approach is error-prone; those familiar with DT will not expect the
416 * assumption to exist, and could easily invalidate it. If the assumption is
417 * invalidated, this function will not report the issue, and debugging will
418 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
420 * @param blob FDT blob
421 * @param node node to examine
422 * @param prop_name name of property to find
423 * @return address, if found, or FDT_ADDR_T_NONE if not
425 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
426 const char *prop_name);
429 * Look up an address property in a node and return the parsed address, and
430 * optionally the parsed size.
432 * This variant hard-codes the number of cells used to represent the address
433 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
434 * always returns the first address value in the property (index 0).
436 * Use of this function is not recommended due to the hard-coding of cell
437 * counts. There is no programmatic validation that these hard-coded values
438 * actually match the device tree content in any way at all. This assumption
439 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
440 * set in the U-Boot build and exercising strict control over DT content to
441 * ensure use of matching #address-cells/#size-cells properties. However, this
442 * approach is error-prone; those familiar with DT will not expect the
443 * assumption to exist, and could easily invalidate it. If the assumption is
444 * invalidated, this function will not report the issue, and debugging will
445 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
447 * @param blob FDT blob
448 * @param node node to examine
449 * @param prop_name name of property to find
450 * @param sizep a pointer to store the size into. Use NULL if not required
451 * @return address, if found, or FDT_ADDR_T_NONE if not
453 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
454 const char *prop_name, fdt_size_t *sizep);
457 * Look at the compatible property of a device node that represents a PCI
458 * device and extract pci vendor id and device id from it.
460 * @param blob FDT blob
461 * @param node node to examine
462 * @param vendor vendor id of the pci device
463 * @param device device id of the pci device
464 * @return 0 if ok, negative on error
466 int fdtdec_get_pci_vendev(const void *blob, int node,
467 u16 *vendor, u16 *device);
470 * Look at the pci address of a device node that represents a PCI device
471 * and return base address of the pci device's registers.
473 * @param dev device to examine
474 * @param addr pci address in the form of fdt_pci_addr
475 * @param bar returns base address of the pci device's registers
476 * @return 0 if ok, negative on error
478 int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr,
482 * Look at the bus range property of a device node and return the pci bus
483 * range for this node.
484 * The property must hold one fdt_pci_addr with a length.
485 * @param blob FDT blob
486 * @param node node to examine
487 * @param res the resource structure to return the bus range
488 * @return 0 if ok, negative on error
491 int fdtdec_get_pci_bus_range(const void *blob, int node,
492 struct fdt_resource *res);
495 * Look up a 32-bit integer property in a node and return it. The property
496 * must have at least 4 bytes of data. The value of the first cell is
499 * @param blob FDT blob
500 * @param node node to examine
501 * @param prop_name name of property to find
502 * @param default_val default value to return if the property is not found
503 * @return integer value, if found, or default_val if not
505 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
509 * Unsigned version of fdtdec_get_int. The property must have at least
510 * 4 bytes of data. The value of the first cell is returned.
512 * @param blob FDT blob
513 * @param node node to examine
514 * @param prop_name name of property to find
515 * @param default_val default value to return if the property is not found
516 * @return unsigned integer value, if found, or default_val if not
518 unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
519 unsigned int default_val);
522 * Get a variable-sized number from a property
524 * This reads a number from one or more cells.
526 * @param ptr Pointer to property
527 * @param cells Number of cells containing the number
528 * @return the value in the cells
530 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
533 * Look up a 64-bit integer property in a node and return it. The property
534 * must have at least 8 bytes of data (2 cells). The first two cells are
535 * concatenated to form a 8 bytes value, where the first cell is top half and
536 * the second cell is bottom half.
538 * @param blob FDT blob
539 * @param node node to examine
540 * @param prop_name name of property to find
541 * @param default_val default value to return if the property is not found
542 * @return integer value, if found, or default_val if not
544 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
545 uint64_t default_val);
548 * Checks whether a node is enabled.
549 * This looks for a 'status' property. If this exists, then returns 1 if
550 * the status is 'ok' and 0 otherwise. If there is no status property,
551 * it returns 1 on the assumption that anything mentioned should be enabled
554 * @param blob FDT blob
555 * @param node node to examine
556 * @return integer value 0 (not enabled) or 1 (enabled)
558 int fdtdec_get_is_enabled(const void *blob, int node);
561 * Make sure we have a valid fdt available to control U-Boot.
563 * If not, a message is printed to the console if the console is ready.
565 * @return 0 if all ok, -1 if not
567 int fdtdec_prepare_fdt(void);
570 * Checks that we have a valid fdt available to control U-Boot.
572 * However, if not then for the moment nothing is done, since this function
573 * is called too early to panic().
577 int fdtdec_check_fdt(void);
580 * Find the nodes for a peripheral and return a list of them in the correct
581 * order. This is used to enumerate all the peripherals of a certain type.
583 * To use this, optionally set up a /aliases node with alias properties for
584 * a peripheral. For example, for usb you could have:
587 * usb0 = "/ehci@c5008000";
588 * usb1 = "/ehci@c5000000";
591 * Pass "usb" as the name to this function and will return a list of two
592 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
594 * All nodes returned will match the compatible ID, as it is assumed that
595 * all peripherals use the same driver.
597 * If no alias node is found, then the node list will be returned in the
598 * order found in the fdt. If the aliases mention a node which doesn't
599 * exist, then this will be ignored. If nodes are found with no aliases,
600 * they will be added in any order.
602 * If there is a gap in the aliases, then this function return a 0 node at
603 * that position. The return value will also count these gaps.
605 * This function checks node properties and will not return nodes which are
606 * marked disabled (status = "disabled").
608 * @param blob FDT blob to use
609 * @param name Root name of alias to search for
610 * @param id Compatible ID to look for
611 * @param node_list Place to put list of found nodes
612 * @param maxcount Maximum number of nodes to find
613 * @return number of nodes found on success, FDT_ERR_... on error
615 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
616 enum fdt_compat_id id, int *node_list, int maxcount);
619 * This function is similar to fdtdec_find_aliases_for_id() except that it
620 * adds to the node_list that is passed in. Any 0 elements are considered
621 * available for allocation - others are considered already used and are
624 * You can use this by calling fdtdec_find_aliases_for_id() with an
625 * uninitialised array, then setting the elements that are returned to -1,
626 * say, then calling this function, perhaps with a different compat id.
627 * Any elements you get back that are >0 are new nodes added by the call
630 * Note that if you have some nodes with aliases and some without, you are
631 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
632 * one compat_id may fill in positions for which you have aliases defined
633 * for another compat_id. When you later call *this* function with the second
634 * compat_id, the alias positions may already be used. A debug warning may
635 * be generated in this case, but it is safest to define aliases for all
636 * nodes when you care about the ordering.
638 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
639 enum fdt_compat_id id, int *node_list, int maxcount);
642 * Get the alias sequence number of a node
644 * This works out whether a node is pointed to by an alias, and if so, the
645 * sequence number of that alias. Aliases are of the form <base><num> where
646 * <num> is the sequence number. For example spi2 would be sequence number
649 * @param blob Device tree blob (if NULL, then error is returned)
650 * @param base Base name for alias (before the underscore)
651 * @param node Node to look up
652 * @param seqp This is set to the sequence number if one is found,
653 * but otherwise the value is left alone
654 * @return 0 if a sequence was found, -ve if not
656 int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
660 * Get the highest alias number for susbystem.
662 * It parses all aliases and find out highest recorded alias for subsystem.
663 * Aliases are of the form <base><num> where <num> is the sequence number.
665 * @param blob Device tree blob (if NULL, then error is returned)
666 * @param base Base name for alias susbystem (before the number)
668 * @return 0 highest alias ID, -1 if not found
670 int fdtdec_get_alias_highest_id(const void *blob, const char *base);
673 * Get a property from the /chosen node
675 * @param blob Device tree blob (if NULL, then NULL is returned)
676 * @param name Property name to look up
677 * @return Value of property, or NULL if it does not exist
679 const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
682 * Get the offset of the given /chosen node
684 * This looks up a property in /chosen containing the path to another node,
685 * then finds the offset of that node.
687 * @param blob Device tree blob (if NULL, then error is returned)
688 * @param name Property name, e.g. "stdout-path"
689 * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
691 int fdtdec_get_chosen_node(const void *blob, const char *name);
694 * Get the name for a compatible ID
696 * @param id Compatible ID to look for
697 * @return compatible string for that id
699 const char *fdtdec_get_compatible(enum fdt_compat_id id);
701 /* Look up a phandle and follow it to its node. Then return the offset
704 * @param blob FDT blob
705 * @param node node to examine
706 * @param prop_name name of property to find
707 * @return node offset if found, -ve error code on error
709 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
712 * Look up a property in a node and return its contents in an integer
713 * array of given length. The property must have at least enough data for
714 * the array (4*count bytes). It may have more, but this will be ignored.
716 * @param blob FDT blob
717 * @param node node to examine
718 * @param prop_name name of property to find
719 * @param array array to fill with data
720 * @param count number of array elements
721 * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
722 * or -FDT_ERR_BADLAYOUT if not enough data
724 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
725 u32 *array, int count);
728 * Look up a property in a node and return its contents in an integer
729 * array of given length. The property must exist but may have less data that
730 * expected (4*count bytes). It may have more, but this will be ignored.
732 * @param blob FDT blob
733 * @param node node to examine
734 * @param prop_name name of property to find
735 * @param array array to fill with data
736 * @param count number of array elements
737 * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
738 * property is not found
740 int fdtdec_get_int_array_count(const void *blob, int node,
741 const char *prop_name, u32 *array, int count);
744 * Look up a property in a node and return a pointer to its contents as a
745 * unsigned int array of given length. The property must have at least enough
746 * data for the array ('count' cells). It may have more, but this will be
747 * ignored. The data is not copied.
749 * Note that you must access elements of the array with fdt32_to_cpu(),
750 * since the elements will be big endian even on a little endian machine.
752 * @param blob FDT blob
753 * @param node node to examine
754 * @param prop_name name of property to find
755 * @param count number of array elements
756 * @return pointer to array if found, or NULL if the property is not
757 * found or there is not enough data
759 const u32 *fdtdec_locate_array(const void *blob, int node,
760 const char *prop_name, int count);
763 * Look up a boolean property in a node and return it.
765 * A boolean properly is true if present in the device tree and false if not
766 * present, regardless of its value.
768 * @param blob FDT blob
769 * @param node node to examine
770 * @param prop_name name of property to find
771 * @return 1 if the properly is present; 0 if it isn't present
773 int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
776 * Count child nodes of one parent node.
778 * @param blob FDT blob
779 * @param node parent node
780 * @return number of child node; 0 if there is not child node
782 int fdtdec_get_child_count(const void *blob, int node);
785 * Look up a property in a node and return its contents in a byte
786 * array of given length. The property must have at least enough data for
787 * the array (count bytes). It may have more, but this will be ignored.
789 * @param blob FDT blob
790 * @param node node to examine
791 * @param prop_name name of property to find
792 * @param array array to fill with data
793 * @param count number of array elements
794 * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
795 * or -FDT_ERR_BADLAYOUT if not enough data
797 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
798 u8 *array, int count);
801 * Look up a property in a node and return a pointer to its contents as a
802 * byte array of given length. The property must have at least enough data
803 * for the array (count bytes). It may have more, but this will be ignored.
804 * The data is not copied.
806 * @param blob FDT blob
807 * @param node node to examine
808 * @param prop_name name of property to find
809 * @param count number of array elements
810 * @return pointer to byte array if found, or NULL if the property is not
811 * found or there is not enough data
813 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
814 const char *prop_name, int count);
817 * Obtain an indexed resource from a device property.
819 * @param fdt FDT blob
820 * @param node node to examine
821 * @param property name of the property to parse
822 * @param index index of the resource to retrieve
823 * @param res returns the resource
824 * @return 0 if ok, negative on error
826 int fdt_get_resource(const void *fdt, int node, const char *property,
827 unsigned int index, struct fdt_resource *res);
830 * Obtain a named resource from a device property.
832 * Look up the index of the name in a list of strings and return the resource
835 * @param fdt FDT blob
836 * @param node node to examine
837 * @param property name of the property to parse
838 * @param prop_names name of the property containing the list of names
839 * @param name the name of the entry to look up
840 * @param res returns the resource
842 int fdt_get_named_resource(const void *fdt, int node, const char *property,
843 const char *prop_names, const char *name,
844 struct fdt_resource *res);
846 /* Display timings from linux include/video/display_timing.h */
848 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
849 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
850 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
851 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
853 /* data enable flag */
854 DISPLAY_FLAGS_DE_LOW = 1 << 4,
855 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
856 /* drive data on pos. edge */
857 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
858 /* drive data on neg. edge */
859 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
860 DISPLAY_FLAGS_INTERLACED = 1 << 8,
861 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
862 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
866 * A single signal can be specified via a range of minimal and maximal values
867 * with a typical value, that lies somewhere inbetween.
869 struct timing_entry {
876 * Single "mode" entry. This describes one set of signal timings a display can
877 * have in one setting. This struct can later be converted to struct videomode
878 * (see include/video/videomode.h). As each timing_entry can be defined as a
879 * range, one struct display_timing may become multiple struct videomodes.
881 * Example: hsync active high, vsync active low
884 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
885 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
886 * | | porch | | porch |
888 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
890 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
892 struct display_timing {
893 struct timing_entry pixelclock;
895 struct timing_entry hactive; /* hor. active video */
896 struct timing_entry hfront_porch; /* hor. front porch */
897 struct timing_entry hback_porch; /* hor. back porch */
898 struct timing_entry hsync_len; /* hor. sync len */
900 struct timing_entry vactive; /* ver. active video */
901 struct timing_entry vfront_porch; /* ver. front porch */
902 struct timing_entry vback_porch; /* ver. back porch */
903 struct timing_entry vsync_len; /* ver. sync len */
905 enum display_flags flags; /* display flags */
906 bool hdmi_monitor; /* is hdmi monitor? */
910 * fdtdec_decode_display_timing() - decode display timings
912 * Decode display timings from the supplied 'display-timings' node.
913 * See doc/device-tree-bindings/video/display-timing.txt for binding
916 * @param blob FDT blob
917 * @param node 'display-timing' node containing the timing subnodes
918 * @param index Index number to read (0=first timing subnode)
919 * @param config Place to put timings
920 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
922 int fdtdec_decode_display_timing(const void *blob, int node, int index,
923 struct display_timing *config);
926 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
929 * Decode the /memory 'reg' property to determine the size and start of the
930 * first memory bank, populate the global data with the size and start of the
931 * first bank of memory.
933 * This function should be called from a boards dram_init(). This helper
934 * function allows for boards to query the device tree for DRAM size and start
935 * address instead of hard coding the value in the case where the memory size
936 * and start address cannot be detected automatically.
938 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
941 int fdtdec_setup_mem_size_base(void);
944 * fdtdec_setup_mem_size_base_lowest() - decode and setup gd->ram_size and
945 * gd->ram_start by lowest available memory base
947 * Decode the /memory 'reg' property to determine the lowest start of the memory
948 * bank bank and populate the global data with it.
950 * This function should be called from a boards dram_init(). This helper
951 * function allows for boards to query the device tree for DRAM size and start
952 * address instead of hard coding the value in the case where the memory size
953 * and start address cannot be detected automatically.
955 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
958 int fdtdec_setup_mem_size_base_lowest(void);
961 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
963 * Decode the /memory 'reg' property to determine the address and size of the
964 * memory banks. Use this data to populate the global data board info with the
965 * phys address and size of memory banks.
967 * This function should be called from a boards dram_init_banksize(). This
968 * helper function allows for boards to query the device tree for memory bank
969 * information instead of hard coding the information in cases where it cannot
970 * be detected automatically.
972 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
975 int fdtdec_setup_memory_banksize(void);
978 * fdtdec_set_ethernet_mac_address() - set MAC address for default interface
980 * Looks up the default interface via the "ethernet" alias (in the /aliases
981 * node) and stores the given MAC in its "local-mac-address" property. This
982 * is useful on platforms that store the MAC address in a custom location.
983 * Board code can call this in the late init stage to make sure that the
984 * interface device tree node has the right MAC address configured for the
985 * Ethernet uclass to pick it up.
987 * Typically the FDT passed into this function will be U-Boot's control DTB.
988 * Given that a lot of code may be holding offsets to various nodes in that
989 * tree, this code will only set the "local-mac-address" property in-place,
990 * which means that it needs to exist and have space for the 6-byte address.
991 * This ensures that the operation is non-destructive and does not invalidate
992 * offsets that other drivers may be using.
994 * @param fdt FDT blob
995 * @param mac buffer containing the MAC address to set
996 * @param size size of MAC address
997 * @return 0 on success or a negative error code on failure
999 int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size);
1002 * fdtdec_set_phandle() - sets the phandle of a given node
1004 * @param blob FDT blob
1005 * @param node offset in the FDT blob of the node whose phandle is to
1007 * @param phandle phandle to set for the given node
1008 * @return 0 on success or a negative error code on failure
1010 static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
1012 return fdt_setprop_u32(blob, node, "phandle", phandle);
1015 /* add "no-map" property */
1016 #define FDTDEC_RESERVED_MEMORY_NO_MAP (1 << 0)
1019 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
1021 * If a reserved-memory node already exists for the given carveout, a phandle
1022 * for that node will be returned. Otherwise a new node will be created and a
1023 * phandle corresponding to it will be returned.
1025 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1026 * for details on how to use reserved memory regions.
1028 * As an example, consider the following code snippet:
1030 * struct fdt_memory fb = {
1031 * .start = 0x92cb3000,
1032 * .end = 0x934b2fff,
1036 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, NULL, 0, &phandle,
1039 * This results in the following subnode being added to the top-level
1040 * /reserved-memory node:
1043 * #address-cells = <0x00000002>;
1044 * #size-cells = <0x00000002>;
1047 * framebuffer@92cb3000 {
1048 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1049 * phandle = <0x0000004d>;
1053 * If the top-level /reserved-memory node does not exist, it will be created.
1054 * The phandle returned from the function call can be used to reference this
1055 * reserved memory region from other nodes.
1057 * See fdtdec_set_carveout() for a more elaborate example.
1059 * @param blob FDT blob
1060 * @param basename base name of the node to create
1061 * @param carveout information about the carveout region
1062 * @param compatibles list of compatible strings for the carveout region
1063 * @param count number of compatible strings for the carveout region
1064 * @param phandlep return location for the phandle of the carveout region
1065 * can be NULL if no phandle should be added
1066 * @param flags bitmask of flags to set for the carveout region
1067 * @return 0 on success or a negative error code on failure
1069 int fdtdec_add_reserved_memory(void *blob, const char *basename,
1070 const struct fdt_memory *carveout,
1071 const char **compatibles, unsigned int count,
1072 uint32_t *phandlep, unsigned long flags);
1075 * fdtdec_get_carveout() - reads a carveout from an FDT
1077 * Reads information about a carveout region from an FDT. The carveout is a
1078 * referenced by its phandle that is read from a given property in a given
1081 * @param blob FDT blob
1082 * @param node name of a node
1083 * @param prop_name name of the property in the given node that contains
1084 * the phandle for the carveout
1085 * @param index index of the phandle for which to read the carveout
1086 * @param carveout return location for the carveout information
1087 * @param name return location for the carveout name
1088 * @param compatiblesp return location for compatible strings
1089 * @param countp return location for the number of compatible strings
1090 * @param flags return location for the flags of the carveout
1091 * @return 0 on success or a negative error code on failure
1093 int fdtdec_get_carveout(const void *blob, const char *node,
1094 const char *prop_name, unsigned int index,
1095 struct fdt_memory *carveout, const char **name,
1096 const char ***compatiblesp, unsigned int *countp,
1097 unsigned long *flags);
1100 * fdtdec_set_carveout() - sets a carveout region for a given node
1102 * Sets a carveout region for a given node. If a reserved-memory node already
1103 * exists for the carveout, the phandle for that node will be reused. If no
1104 * such node exists, a new one will be created and a phandle to it stored in
1105 * a specified property of the given node.
1107 * As an example, consider the following code snippet:
1109 * const char *node = "/host1x@50000000/dc@54240000";
1110 * struct fdt_memory fb = {
1111 * .start = 0x92cb3000,
1112 * .end = 0x934b2fff,
1115 * fdtdec_set_carveout(fdt, node, "memory-region", 0, "framebuffer", NULL,
1118 * dc@54200000 is a display controller and was set up by the bootloader to
1119 * scan out the framebuffer specified by "fb". This would cause the following
1120 * reserved memory region to be added:
1123 * #address-cells = <0x00000002>;
1124 * #size-cells = <0x00000002>;
1127 * framebuffer@92cb3000 {
1128 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1129 * phandle = <0x0000004d>;
1133 * A "memory-region" property will also be added to the node referenced by the
1141 * memory-region = <0x0000004d>;
1148 * @param blob FDT blob
1149 * @param node name of the node to add the carveout to
1150 * @param prop_name name of the property in which to store the phandle of
1152 * @param index index of the phandle to store
1153 * @param carveout information about the carveout to add
1154 * @param name base name of the reserved-memory node to create
1155 * @param compatibles compatible strings to set for the carveout
1156 * @param count number of compatible strings
1157 * @param flags bitmask of flags to set for the carveout
1158 * @return 0 on success or a negative error code on failure
1160 int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
1161 unsigned int index, const struct fdt_memory *carveout,
1162 const char *name, const char **compatibles,
1163 unsigned int count, unsigned long flags);
1166 * Set up the device tree ready for use
1168 int fdtdec_setup(void);
1171 * Perform board-specific early DT adjustments
1173 int fdtdec_board_setup(const void *fdt_blob);
1175 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1177 * fdtdec_resetup() - Set up the device tree again
1179 * The main difference with fdtdec_setup() is that it returns if the fdt has
1180 * changed because a better match has been found.
1181 * This is typically used for boards that rely on a DM driver to detect the
1182 * board type. This function sould be called by the board code after the stuff
1183 * needed by board_fit_config_name_match() to operate porperly is available.
1184 * If this functions signals that a rescan is necessary, the board code must
1185 * unbind all the drivers using dm_uninit() and then rescan the DT with
1186 * dm_init_and_scan().
1188 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1189 * the fdt is required
1191 * @return 0 if OK, -ve on error
1193 int fdtdec_resetup(int *rescan);
1197 * Board-specific FDT initialization. Returns the address to a device tree blob.
1199 * Called when CONFIG_OF_BOARD is defined.
1201 * The existing devicetree is available at gd->fdt_blob
1203 * @err internal error code if we fail to setup a DTB
1204 * @returns new devicetree blob pointer
1206 void *board_fdt_blob_setup(int *err);
1209 * Decode the size of memory
1211 * RAM size is normally set in a /memory node and consists of a list of
1212 * (base, size) cells in the 'reg' property. This information is used to
1213 * determine the total available memory as well as the address and size
1216 * Optionally the memory configuration can vary depending on a board id,
1217 * typically read from strapping resistors or an EEPROM on the board.
1219 * Finally, memory size can be detected (within certain limits) by probing
1220 * the available memory. It is safe to do so within the limits provides by
1221 * the board's device tree information. This makes it possible to produce
1222 * boards with different memory sizes, where the device tree specifies the
1223 * maximum memory configuration, and the smaller memory configuration is
1226 * This function decodes that information, returning the memory base address,
1227 * size and bank information. See the memory.txt binding for full
1230 * @param blob Device tree blob
1231 * @param area Name of node to check (NULL means "/memory")
1232 * @param board_id Board ID to look up
1233 * @param basep Returns base address of first memory bank (NULL to
1235 * @param sizep Returns total memory size (NULL to ignore)
1236 * @param bd Updated with the memory bank information (NULL to skip)
1237 * @return 0 if OK, -ve on error
1239 int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1240 phys_addr_t *basep, phys_size_t *sizep,
1241 struct bd_info *bd);
1244 * fdtdec_get_srcname() - Get the name of where the devicetree comes from
1246 * @return source name
1248 const char *fdtdec_get_srcname(void);