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 static inline fdt32_t fdt_addr_unpack(fdt_addr_t addr, fdt32_t *upper)
30 #ifdef CONFIG_PHYS_64BIT
39 static inline fdt32_t fdt_size_unpack(fdt_size_t size, fdt32_t *upper)
42 #ifdef CONFIG_PHYS_64BIT
51 #ifdef CONFIG_PHYS_64BIT
52 #define FDT_ADDR_T_NONE (-1U)
53 #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
54 #define fdt_size_to_cpu(reg) be64_to_cpu(reg)
55 #define cpu_to_fdt_addr(reg) cpu_to_be64(reg)
56 #define cpu_to_fdt_size(reg) cpu_to_be64(reg)
57 typedef fdt64_t fdt_val_t;
59 #define FDT_ADDR_T_NONE (-1U)
60 #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
61 #define fdt_size_to_cpu(reg) be32_to_cpu(reg)
62 #define cpu_to_fdt_addr(reg) cpu_to_be32(reg)
63 #define cpu_to_fdt_size(reg) cpu_to_be32(reg)
64 typedef fdt32_t fdt_val_t;
67 /* Information obtained about memory from the FDT */
75 #ifdef CONFIG_SPL_BUILD
81 #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
82 extern phys_addr_t prior_stage_fdt_address;
86 * Information about a resource. start is the first address of the resource
87 * and end is the last address (inclusive). The length of the resource will
88 * be equal to: end - start + 1.
96 FDT_PCI_SPACE_CONFIG = 0,
97 FDT_PCI_SPACE_IO = 0x01000000,
98 FDT_PCI_SPACE_MEM32 = 0x02000000,
99 FDT_PCI_SPACE_MEM64 = 0x03000000,
100 FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
101 FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
104 #define FDT_PCI_ADDR_CELLS 3
105 #define FDT_PCI_SIZE_CELLS 2
106 #define FDT_PCI_REG_SIZE \
107 ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
110 * The Open Firmware spec defines PCI physical address as follows:
112 * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
114 * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
115 * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
116 * phys.lo cell: llllllll llllllll llllllll llllllll
120 * n: is 0 if the address is relocatable, 1 otherwise
121 * p: is 1 if addressable region is prefetchable, 0 otherwise
122 * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
123 * (for Memory), or below 64KB (for relocatable I/O)
124 * ss: is the space code, denoting the address space
125 * bbbbbbbb: is the 8-bit Bus Number
126 * ddddd: is the 5-bit Device Number
127 * fff: is the 3-bit Function Number
128 * rrrrrrrr: is the 8-bit Register Number
129 * hhhhhhhh: is a 32-bit unsigned number
130 * llllllll: is a 32-bit unsigned number
132 struct fdt_pci_addr {
139 * Compute the size of a resource.
141 * @param res the resource to operate on
142 * @return the size of the resource
144 static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
146 return res->end - res->start + 1;
150 * Compat types that we know about and for which we might have drivers.
151 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
156 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
157 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
158 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
159 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
160 /* Tegra124 XUSB pad controller */
161 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
162 /* Tegra210 XUSB pad controller */
163 COMPAT_SMSC_LAN9215, /* SMSC 10/100 Ethernet LAN9215 */
164 COMPAT_SAMSUNG_EXYNOS5_SROMC, /* Exynos5 SROMC */
165 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
166 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
167 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
168 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
169 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
170 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
171 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
172 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
173 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
174 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
175 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
176 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
177 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
178 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
179 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
180 COMPAT_SUNXI_NAND, /* SUNXI NAND controller */
181 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
182 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
183 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
184 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
185 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
186 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
187 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
188 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
189 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
190 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
191 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
196 #define MAX_PHANDLE_ARGS 16
197 struct fdtdec_phandle_args {
200 uint32_t args[MAX_PHANDLE_ARGS];
204 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
206 * This function is useful to parse lists of phandles and their arguments.
219 * list = <&phandle1 1 2 &phandle2 3>;
222 * To get a device_node of the `node2' node you may call this:
223 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
226 * (This function is a modified version of __of_parse_phandle_with_args() from
229 * @blob: Pointer to device tree
230 * @src_node: Offset of device tree node containing a list
231 * @list_name: property name that contains a list
232 * @cells_name: property name that specifies the phandles' arguments count,
233 * or NULL to use @cells_count
234 * @cells_count: Cell count to use if @cells_name is NULL
235 * @index: index of a phandle to parse out
236 * @out_args: optional pointer to output arguments structure (will be filled)
237 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
238 * @list_name does not exist, a phandle was not found, @cells_name
239 * could not be found, the arguments were truncated or there were too
243 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
244 const char *list_name,
245 const char *cells_name,
246 int cell_count, int index,
247 struct fdtdec_phandle_args *out_args);
250 * Find the next numbered alias for a peripheral. This is used to enumerate
251 * all the peripherals of a certain type.
253 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
254 * this function will return a pointer to the node the alias points to, and
255 * then update *upto to 1. Next time you call this function, the next node
258 * All nodes returned will match the compatible ID, as it is assumed that
259 * all peripherals use the same driver.
261 * @param blob FDT blob to use
262 * @param name Root name of alias to search for
263 * @param id Compatible ID to look for
264 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
266 int fdtdec_next_alias(const void *blob, const char *name,
267 enum fdt_compat_id id, int *upto);
270 * Find the compatible ID for a given node.
272 * Generally each node has at least one compatible string attached to it.
273 * This function looks through our list of known compatible strings and
274 * returns the corresponding ID which matches the compatible string.
276 * @param blob FDT blob to use
277 * @param node Node containing compatible string to find
278 * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
280 enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
283 * Find the next compatible node for a peripheral.
285 * Do the first call with node = 0. This function will return a pointer to
286 * the next compatible node. Next time you call this function, pass the
287 * value returned, and the next node will be provided.
289 * @param blob FDT blob to use
290 * @param node Start node for search
291 * @param id Compatible ID to look for (enum fdt_compat_id)
292 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
294 int fdtdec_next_compatible(const void *blob, int node,
295 enum fdt_compat_id id);
298 * Find the next compatible subnode for a peripheral.
300 * Do the first call with node set to the parent and depth = 0. This
301 * function will return the offset of the next compatible node. Next time
302 * you call this function, pass the node value returned last time, with
303 * depth unchanged, and the next node will be provided.
305 * @param blob FDT blob to use
306 * @param node Start node for search
307 * @param id Compatible ID to look for (enum fdt_compat_id)
308 * @param depthp Current depth (set to 0 before first call)
309 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
311 int fdtdec_next_compatible_subnode(const void *blob, int node,
312 enum fdt_compat_id id, int *depthp);
315 * Look up an address property in a node and return the parsed address, and
316 * optionally the parsed size.
318 * This variant assumes a known and fixed number of cells are used to
319 * represent the address and size.
321 * You probably don't want to use this function directly except to parse
322 * non-standard properties, and never to parse the "reg" property. Instead,
323 * use one of the "auto" variants below, which automatically honor the
324 * #address-cells and #size-cells properties in the parent node.
326 * @param blob FDT blob
327 * @param node node to examine
328 * @param prop_name name of property to find
329 * @param index which address to retrieve from a list of addresses. Often 0.
330 * @param na the number of cells used to represent an address
331 * @param ns the number of cells used to represent a size
332 * @param sizep a pointer to store the size into. Use NULL if not required
333 * @param translate Indicates whether to translate the returned value
334 * using the parent node's ranges property.
335 * @return address, if found, or FDT_ADDR_T_NONE if not
337 fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
338 const char *prop_name, int index, int na, int ns,
339 fdt_size_t *sizep, bool translate);
342 * Look up an address property in a node and return the parsed address, and
343 * optionally the parsed size.
345 * This variant automatically determines the number of cells used to represent
346 * the address and size by parsing the provided parent node's #address-cells
347 * and #size-cells properties.
349 * @param blob FDT blob
350 * @param parent parent node of @node
351 * @param node node to examine
352 * @param prop_name name of property to find
353 * @param index which address to retrieve from a list of addresses. Often 0.
354 * @param sizep a pointer to store the size into. Use NULL if not required
355 * @param translate Indicates whether to translate the returned value
356 * using the parent node's ranges property.
357 * @return address, if found, or FDT_ADDR_T_NONE if not
359 fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
360 int node, const char *prop_name, int index, fdt_size_t *sizep,
364 * Look up an address property in a node and return the parsed address, and
365 * optionally the parsed size.
367 * This variant automatically determines the number of cells used to represent
368 * the address and size by parsing the parent node's #address-cells
369 * and #size-cells properties. The parent node is automatically found.
371 * The automatic parent lookup implemented by this function is slow.
372 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
375 * @param blob FDT blob
376 * @param parent parent node of @node
377 * @param node node to examine
378 * @param prop_name name of property to find
379 * @param index which address to retrieve from a list of addresses. Often 0.
380 * @param sizep a pointer to store the size into. Use NULL if not required
381 * @param translate Indicates whether to translate the returned value
382 * using the parent node's ranges property.
383 * @return address, if found, or FDT_ADDR_T_NONE if not
385 fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
386 const char *prop_name, int index, fdt_size_t *sizep,
390 * Look up an address property in a node and return the parsed address.
392 * This variant hard-codes the number of cells used to represent the address
393 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
394 * always returns the first address value in the property (index 0).
396 * Use of this function is not recommended due to the hard-coding of cell
397 * counts. There is no programmatic validation that these hard-coded values
398 * actually match the device tree content in any way at all. This assumption
399 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
400 * set in the U-Boot build and exercising strict control over DT content to
401 * ensure use of matching #address-cells/#size-cells properties. However, this
402 * approach is error-prone; those familiar with DT will not expect the
403 * assumption to exist, and could easily invalidate it. If the assumption is
404 * invalidated, this function will not report the issue, and debugging will
405 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
407 * @param blob FDT blob
408 * @param node node to examine
409 * @param prop_name name of property to find
410 * @return address, if found, or FDT_ADDR_T_NONE if not
412 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
413 const char *prop_name);
416 * Look up an address property in a node and return the parsed address, and
417 * optionally the parsed size.
419 * This variant hard-codes the number of cells used to represent the address
420 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
421 * always returns the first address value in the property (index 0).
423 * Use of this function is not recommended due to the hard-coding of cell
424 * counts. There is no programmatic validation that these hard-coded values
425 * actually match the device tree content in any way at all. This assumption
426 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
427 * set in the U-Boot build and exercising strict control over DT content to
428 * ensure use of matching #address-cells/#size-cells properties. However, this
429 * approach is error-prone; those familiar with DT will not expect the
430 * assumption to exist, and could easily invalidate it. If the assumption is
431 * invalidated, this function will not report the issue, and debugging will
432 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
434 * @param blob FDT blob
435 * @param node node to examine
436 * @param prop_name name of property to find
437 * @param sizep a pointer to store the size into. Use NULL if not required
438 * @return address, if found, or FDT_ADDR_T_NONE if not
440 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
441 const char *prop_name, fdt_size_t *sizep);
444 * Look at an address property in a node and return the pci address which
445 * corresponds to the given type in the form of fdt_pci_addr.
446 * The property must hold one fdt_pci_addr with a lengh.
448 * @param blob FDT blob
449 * @param node node to examine
450 * @param type pci address type (FDT_PCI_SPACE_xxx)
451 * @param prop_name name of property to find
452 * @param addr returns pci address in the form of fdt_pci_addr
453 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
454 * format of the property was invalid, -ENXIO if the requested
455 * address type was not found
457 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
458 const char *prop_name, struct fdt_pci_addr *addr);
461 * Look at the compatible property of a device node that represents a PCI
462 * device and extract pci vendor id and device id from it.
464 * @param blob FDT blob
465 * @param node node to examine
466 * @param vendor vendor id of the pci device
467 * @param device device id of the pci device
468 * @return 0 if ok, negative on error
470 int fdtdec_get_pci_vendev(const void *blob, int node,
471 u16 *vendor, u16 *device);
474 * Look at the pci address of a device node that represents a PCI device
475 * and return base address of the pci device's registers.
477 * @param dev device to examine
478 * @param addr pci address in the form of fdt_pci_addr
479 * @param bar returns base address of the pci device's registers
480 * @return 0 if ok, negative on error
482 int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
486 * Look up a 32-bit integer property in a node and return it. The property
487 * must have at least 4 bytes of data. The value of the first cell is
490 * @param blob FDT blob
491 * @param node node to examine
492 * @param prop_name name of property to find
493 * @param default_val default value to return if the property is not found
494 * @return integer value, if found, or default_val if not
496 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
500 * Unsigned version of fdtdec_get_int. The property must have at least
501 * 4 bytes of data. The value of the first cell is returned.
503 * @param blob FDT blob
504 * @param node node to examine
505 * @param prop_name name of property to find
506 * @param default_val default value to return if the property is not found
507 * @return unsigned integer value, if found, or default_val if not
509 unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
510 unsigned int default_val);
513 * Get a variable-sized number from a property
515 * This reads a number from one or more cells.
517 * @param ptr Pointer to property
518 * @param cells Number of cells containing the number
519 * @return the value in the cells
521 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
524 * Look up a 64-bit integer property in a node and return it. The property
525 * must have at least 8 bytes of data (2 cells). The first two cells are
526 * concatenated to form a 8 bytes value, where the first cell is top half and
527 * the second cell is bottom half.
529 * @param blob FDT blob
530 * @param node node to examine
531 * @param prop_name name of property to find
532 * @param default_val default value to return if the property is not found
533 * @return integer value, if found, or default_val if not
535 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
536 uint64_t default_val);
539 * Checks whether a node is enabled.
540 * This looks for a 'status' property. If this exists, then returns 1 if
541 * the status is 'ok' and 0 otherwise. If there is no status property,
542 * it returns 1 on the assumption that anything mentioned should be enabled
545 * @param blob FDT blob
546 * @param node node to examine
547 * @return integer value 0 (not enabled) or 1 (enabled)
549 int fdtdec_get_is_enabled(const void *blob, int node);
552 * Make sure we have a valid fdt available to control U-Boot.
554 * If not, a message is printed to the console if the console is ready.
556 * @return 0 if all ok, -1 if not
558 int fdtdec_prepare_fdt(void);
561 * Checks that we have a valid fdt available to control U-Boot.
563 * However, if not then for the moment nothing is done, since this function
564 * is called too early to panic().
568 int fdtdec_check_fdt(void);
571 * Find the nodes for a peripheral and return a list of them in the correct
572 * order. This is used to enumerate all the peripherals of a certain type.
574 * To use this, optionally set up a /aliases node with alias properties for
575 * a peripheral. For example, for usb you could have:
578 * usb0 = "/ehci@c5008000";
579 * usb1 = "/ehci@c5000000";
582 * Pass "usb" as the name to this function and will return a list of two
583 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
585 * All nodes returned will match the compatible ID, as it is assumed that
586 * all peripherals use the same driver.
588 * If no alias node is found, then the node list will be returned in the
589 * order found in the fdt. If the aliases mention a node which doesn't
590 * exist, then this will be ignored. If nodes are found with no aliases,
591 * they will be added in any order.
593 * If there is a gap in the aliases, then this function return a 0 node at
594 * that position. The return value will also count these gaps.
596 * This function checks node properties and will not return nodes which are
597 * marked disabled (status = "disabled").
599 * @param blob FDT blob to use
600 * @param name Root name of alias to search for
601 * @param id Compatible ID to look for
602 * @param node_list Place to put list of found nodes
603 * @param maxcount Maximum number of nodes to find
604 * @return number of nodes found on success, FDT_ERR_... on error
606 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
607 enum fdt_compat_id id, int *node_list, int maxcount);
610 * This function is similar to fdtdec_find_aliases_for_id() except that it
611 * adds to the node_list that is passed in. Any 0 elements are considered
612 * available for allocation - others are considered already used and are
615 * You can use this by calling fdtdec_find_aliases_for_id() with an
616 * uninitialised array, then setting the elements that are returned to -1,
617 * say, then calling this function, perhaps with a different compat id.
618 * Any elements you get back that are >0 are new nodes added by the call
621 * Note that if you have some nodes with aliases and some without, you are
622 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
623 * one compat_id may fill in positions for which you have aliases defined
624 * for another compat_id. When you later call *this* function with the second
625 * compat_id, the alias positions may already be used. A debug warning may
626 * be generated in this case, but it is safest to define aliases for all
627 * nodes when you care about the ordering.
629 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
630 enum fdt_compat_id id, int *node_list, int maxcount);
633 * Get the alias sequence number of a node
635 * This works out whether a node is pointed to by an alias, and if so, the
636 * sequence number of that alias. Aliases are of the form <base><num> where
637 * <num> is the sequence number. For example spi2 would be sequence number
640 * @param blob Device tree blob (if NULL, then error is returned)
641 * @param base Base name for alias (before the underscore)
642 * @param node Node to look up
643 * @param seqp This is set to the sequence number if one is found,
644 * but otherwise the value is left alone
645 * @return 0 if a sequence was found, -ve if not
647 int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
651 * Get the highest alias number for susbystem.
653 * It parses all aliases and find out highest recorded alias for subsystem.
654 * Aliases are of the form <base><num> where <num> is the sequence number.
656 * @param blob Device tree blob (if NULL, then error is returned)
657 * @param base Base name for alias susbystem (before the number)
659 * @return 0 highest alias ID, -1 if not found
661 int fdtdec_get_alias_highest_id(const void *blob, const char *base);
664 * Get a property from the /chosen node
666 * @param blob Device tree blob (if NULL, then NULL is returned)
667 * @param name Property name to look up
668 * @return Value of property, or NULL if it does not exist
670 const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
673 * Get the offset of the given /chosen node
675 * This looks up a property in /chosen containing the path to another node,
676 * then finds the offset of that node.
678 * @param blob Device tree blob (if NULL, then error is returned)
679 * @param name Property name, e.g. "stdout-path"
680 * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
682 int fdtdec_get_chosen_node(const void *blob, const char *name);
685 * Get the name for a compatible ID
687 * @param id Compatible ID to look for
688 * @return compatible string for that id
690 const char *fdtdec_get_compatible(enum fdt_compat_id id);
692 /* Look up a phandle and follow it to its node. Then return the offset
695 * @param blob FDT blob
696 * @param node node to examine
697 * @param prop_name name of property to find
698 * @return node offset if found, -ve error code on error
700 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
703 * Look up a property in a node and return its contents in an integer
704 * array of given length. The property must have at least enough data for
705 * the array (4*count bytes). It may have more, but this will be ignored.
707 * @param blob FDT blob
708 * @param node node to examine
709 * @param prop_name name of property to find
710 * @param array array to fill with data
711 * @param count number of array elements
712 * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
713 * or -FDT_ERR_BADLAYOUT if not enough data
715 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
716 u32 *array, int count);
719 * Look up a property in a node and return its contents in an integer
720 * array of given length. The property must exist but may have less data that
721 * expected (4*count bytes). It may have more, but this will be ignored.
723 * @param blob FDT blob
724 * @param node node to examine
725 * @param prop_name name of property to find
726 * @param array array to fill with data
727 * @param count number of array elements
728 * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
729 * property is not found
731 int fdtdec_get_int_array_count(const void *blob, int node,
732 const char *prop_name, u32 *array, int count);
735 * Look up a property in a node and return a pointer to its contents as a
736 * unsigned int array of given length. The property must have at least enough
737 * data for the array ('count' cells). It may have more, but this will be
738 * ignored. The data is not copied.
740 * Note that you must access elements of the array with fdt32_to_cpu(),
741 * since the elements will be big endian even on a little endian machine.
743 * @param blob FDT blob
744 * @param node node to examine
745 * @param prop_name name of property to find
746 * @param count number of array elements
747 * @return pointer to array if found, or NULL if the property is not
748 * found or there is not enough data
750 const u32 *fdtdec_locate_array(const void *blob, int node,
751 const char *prop_name, int count);
754 * Look up a boolean property in a node and return it.
756 * A boolean properly is true if present in the device tree and false if not
757 * present, regardless of its value.
759 * @param blob FDT blob
760 * @param node node to examine
761 * @param prop_name name of property to find
762 * @return 1 if the properly is present; 0 if it isn't present
764 int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
767 * Count child nodes of one parent node.
769 * @param blob FDT blob
770 * @param node parent node
771 * @return number of child node; 0 if there is not child node
773 int fdtdec_get_child_count(const void *blob, int node);
776 * Look in the FDT for a config item with the given name and return its value
777 * as a 32-bit integer. The property must have at least 4 bytes of data. The
778 * value of the first cell is returned.
780 * @param blob FDT blob to use
781 * @param prop_name Node property name
782 * @param default_val default value to return if the property is not found
783 * @return integer value, if found, or default_val if not
785 int fdtdec_get_config_int(const void *blob, const char *prop_name,
789 * Look in the FDT for a config item with the given name
790 * and return whether it exists.
792 * @param blob FDT blob
793 * @param prop_name property name to look up
794 * @return 1, if it exists, or 0 if not
796 int fdtdec_get_config_bool(const void *blob, const char *prop_name);
799 * Look in the FDT for a config item with the given name and return its value
802 * @param blob FDT blob
803 * @param prop_name property name to look up
804 * @returns property string, NULL on error.
806 char *fdtdec_get_config_string(const void *blob, const char *prop_name);
809 * Look up a property in a node and return its contents in a byte
810 * array of given length. The property must have at least enough data for
811 * the array (count bytes). It may have more, but this will be ignored.
813 * @param blob FDT blob
814 * @param node node to examine
815 * @param prop_name name of property to find
816 * @param array array to fill with data
817 * @param count number of array elements
818 * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
819 * or -FDT_ERR_BADLAYOUT if not enough data
821 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
822 u8 *array, int count);
825 * Look up a property in a node and return a pointer to its contents as a
826 * byte array of given length. The property must have at least enough data
827 * for the array (count bytes). It may have more, but this will be ignored.
828 * The data is not copied.
830 * @param blob FDT blob
831 * @param node node to examine
832 * @param prop_name name of property to find
833 * @param count number of array elements
834 * @return pointer to byte array if found, or NULL if the property is not
835 * found or there is not enough data
837 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
838 const char *prop_name, int count);
841 * Obtain an indexed resource from a device property.
843 * @param fdt FDT blob
844 * @param node node to examine
845 * @param property name of the property to parse
846 * @param index index of the resource to retrieve
847 * @param res returns the resource
848 * @return 0 if ok, negative on error
850 int fdt_get_resource(const void *fdt, int node, const char *property,
851 unsigned int index, struct fdt_resource *res);
854 * Obtain a named resource from a device property.
856 * Look up the index of the name in a list of strings and return the resource
859 * @param fdt FDT blob
860 * @param node node to examine
861 * @param property name of the property to parse
862 * @param prop_names name of the property containing the list of names
863 * @param name the name of the entry to look up
864 * @param res returns the resource
866 int fdt_get_named_resource(const void *fdt, int node, const char *property,
867 const char *prop_names, const char *name,
868 struct fdt_resource *res);
870 /* Display timings from linux include/video/display_timing.h */
872 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
873 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
874 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
875 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
877 /* data enable flag */
878 DISPLAY_FLAGS_DE_LOW = 1 << 4,
879 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
880 /* drive data on pos. edge */
881 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
882 /* drive data on neg. edge */
883 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
884 DISPLAY_FLAGS_INTERLACED = 1 << 8,
885 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
886 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
890 * A single signal can be specified via a range of minimal and maximal values
891 * with a typical value, that lies somewhere inbetween.
893 struct timing_entry {
900 * Single "mode" entry. This describes one set of signal timings a display can
901 * have in one setting. This struct can later be converted to struct videomode
902 * (see include/video/videomode.h). As each timing_entry can be defined as a
903 * range, one struct display_timing may become multiple struct videomodes.
905 * Example: hsync active high, vsync active low
908 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
909 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
910 * | | porch | | porch |
912 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
914 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
916 struct display_timing {
917 struct timing_entry pixelclock;
919 struct timing_entry hactive; /* hor. active video */
920 struct timing_entry hfront_porch; /* hor. front porch */
921 struct timing_entry hback_porch; /* hor. back porch */
922 struct timing_entry hsync_len; /* hor. sync len */
924 struct timing_entry vactive; /* ver. active video */
925 struct timing_entry vfront_porch; /* ver. front porch */
926 struct timing_entry vback_porch; /* ver. back porch */
927 struct timing_entry vsync_len; /* ver. sync len */
929 enum display_flags flags; /* display flags */
930 bool hdmi_monitor; /* is hdmi monitor? */
934 * fdtdec_decode_display_timing() - decode display timings
936 * Decode display timings from the supplied 'display-timings' node.
937 * See doc/device-tree-bindings/video/display-timing.txt for binding
940 * @param blob FDT blob
941 * @param node 'display-timing' node containing the timing subnodes
942 * @param index Index number to read (0=first timing subnode)
943 * @param config Place to put timings
944 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
946 int fdtdec_decode_display_timing(const void *blob, int node, int index,
947 struct display_timing *config);
950 * fdtdec_setup_mem_size_base_fdt() - decode and setup gd->ram_size and
953 * Decode the /memory 'reg' property to determine the size and start of the
954 * first memory bank, populate the global data with the size and start of the
955 * first bank of memory.
957 * This function should be called from a boards dram_init(). This helper
958 * function allows for boards to query the device tree for DRAM size and start
959 * address instead of hard coding the value in the case where the memory size
960 * and start address cannot be detected automatically.
962 * @param blob FDT blob
964 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
967 int fdtdec_setup_mem_size_base_fdt(const void *blob);
970 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
973 * Decode the /memory 'reg' property to determine the size and start of the
974 * first memory bank, populate the global data with the size and start of the
975 * first bank of memory.
977 * This function should be called from a boards dram_init(). This helper
978 * function allows for boards to query the device tree for DRAM size and start
979 * address instead of hard coding the value in the case where the memory size
980 * and start address cannot be detected automatically.
982 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
985 int fdtdec_setup_mem_size_base(void);
988 * fdtdec_setup_memory_banksize_fdt() - decode and populate gd->bd->bi_dram
990 * Decode the /memory 'reg' property to determine the address and size of the
991 * memory banks. Use this data to populate the global data board info with the
992 * phys address and size of memory banks.
994 * This function should be called from a boards dram_init_banksize(). This
995 * helper function allows for boards to query the device tree for memory bank
996 * information instead of hard coding the information in cases where it cannot
997 * be detected automatically.
999 * @param blob FDT blob
1001 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
1004 int fdtdec_setup_memory_banksize_fdt(const void *blob);
1007 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
1009 * Decode the /memory 'reg' property to determine the address and size of the
1010 * memory banks. Use this data to populate the global data board info with the
1011 * phys address and size of memory banks.
1013 * This function should be called from a boards dram_init_banksize(). This
1014 * helper function allows for boards to query the device tree for memory bank
1015 * information instead of hard coding the information in cases where it cannot
1016 * be detected automatically.
1018 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
1021 int fdtdec_setup_memory_banksize(void);
1024 * fdtdec_set_phandle() - sets the phandle of a given node
1026 * @param blob FDT blob
1027 * @param node offset in the FDT blob of the node whose phandle is to
1029 * @param phandle phandle to set for the given node
1030 * @return 0 on success or a negative error code on failure
1032 static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
1034 return fdt_setprop_u32(blob, node, "phandle", phandle);
1038 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
1040 * If a reserved-memory node already exists for the given carveout, a phandle
1041 * for that node will be returned. Otherwise a new node will be created and a
1042 * phandle corresponding to it will be returned.
1044 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1045 * for details on how to use reserved memory regions.
1047 * As an example, consider the following code snippet:
1049 * struct fdt_memory fb = {
1050 * .start = 0x92cb3000,
1051 * .end = 0x934b2fff,
1055 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, &phandle);
1057 * This results in the following subnode being added to the top-level
1058 * /reserved-memory node:
1061 * #address-cells = <0x00000002>;
1062 * #size-cells = <0x00000002>;
1065 * framebuffer@92cb3000 {
1066 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1067 * phandle = <0x0000004d>;
1071 * If the top-level /reserved-memory node does not exist, it will be created.
1072 * The phandle returned from the function call can be used to reference this
1073 * reserved memory region from other nodes.
1075 * See fdtdec_set_carveout() for a more elaborate example.
1077 * @param blob FDT blob
1078 * @param basename base name of the node to create
1079 * @param carveout information about the carveout region
1080 * @param phandlep return location for the phandle of the carveout region
1081 * @return 0 on success or a negative error code on failure
1083 int fdtdec_add_reserved_memory(void *blob, const char *basename,
1084 const struct fdt_memory *carveout,
1085 uint32_t *phandlep);
1088 * fdtdec_get_carveout() - reads a carveout from an FDT
1090 * Reads information about a carveout region from an FDT. The carveout is a
1091 * referenced by its phandle that is read from a given property in a given
1094 * @param blob FDT blob
1095 * @param node name of a node
1096 * @param name name of the property in the given node that contains
1097 * the phandle for the carveout
1098 * @param index index of the phandle for which to read the carveout
1099 * @param carveout return location for the carveout information
1100 * @return 0 on success or a negative error code on failure
1102 int fdtdec_get_carveout(const void *blob, const char *node, const char *name,
1103 unsigned int index, struct fdt_memory *carveout);
1106 * fdtdec_set_carveout() - sets a carveout region for a given node
1108 * Sets a carveout region for a given node. If a reserved-memory node already
1109 * exists for the carveout, the phandle for that node will be reused. If no
1110 * such node exists, a new one will be created and a phandle to it stored in
1111 * a specified property of the given node.
1113 * As an example, consider the following code snippet:
1115 * const char *node = "/host1x@50000000/dc@54240000";
1116 * struct fdt_memory fb = {
1117 * .start = 0x92cb3000,
1118 * .end = 0x934b2fff,
1121 * fdtdec_set_carveout(fdt, node, "memory-region", 0, "framebuffer", &fb);
1123 * dc@54200000 is a display controller and was set up by the bootloader to
1124 * scan out the framebuffer specified by "fb". This would cause the following
1125 * reserved memory region to be added:
1128 * #address-cells = <0x00000002>;
1129 * #size-cells = <0x00000002>;
1132 * framebuffer@92cb3000 {
1133 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1134 * phandle = <0x0000004d>;
1138 * A "memory-region" property will also be added to the node referenced by the
1146 * memory-region = <0x0000004d>;
1153 * @param blob FDT blob
1154 * @param node name of the node to add the carveout to
1155 * @param prop_name name of the property in which to store the phandle of
1157 * @param index index of the phandle to store
1158 * @param name base name of the reserved-memory node to create
1159 * @param carveout information about the carveout to add
1160 * @return 0 on success or a negative error code on failure
1162 int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
1163 unsigned int index, const char *name,
1164 const struct fdt_memory *carveout);
1167 * Set up the device tree ready for use
1169 int fdtdec_setup(void);
1171 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1173 * fdtdec_resetup() - Set up the device tree again
1175 * The main difference with fdtdec_setup() is that it returns if the fdt has
1176 * changed because a better match has been found.
1177 * This is typically used for boards that rely on a DM driver to detect the
1178 * board type. This function sould be called by the board code after the stuff
1179 * needed by board_fit_config_name_match() to operate porperly is available.
1180 * If this functions signals that a rescan is necessary, the board code must
1181 * unbind all the drivers using dm_uninit() and then rescan the DT with
1182 * dm_init_and_scan().
1184 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1185 * the fdt is required
1187 * @return 0 if OK, -ve on error
1189 int fdtdec_resetup(int *rescan);
1193 * Board-specific FDT initialization. Returns the address to a device tree blob.
1194 * Called when CONFIG_OF_BOARD is defined, or if CONFIG_OF_SEPARATE is defined
1195 * and the board implements it.
1197 void *board_fdt_blob_setup(void);
1200 * Decode the size of memory
1202 * RAM size is normally set in a /memory node and consists of a list of
1203 * (base, size) cells in the 'reg' property. This information is used to
1204 * determine the total available memory as well as the address and size
1207 * Optionally the memory configuration can vary depending on a board id,
1208 * typically read from strapping resistors or an EEPROM on the board.
1210 * Finally, memory size can be detected (within certain limits) by probing
1211 * the available memory. It is safe to do so within the limits provides by
1212 * the board's device tree information. This makes it possible to produce
1213 * boards with different memory sizes, where the device tree specifies the
1214 * maximum memory configuration, and the smaller memory configuration is
1217 * This function decodes that information, returning the memory base address,
1218 * size and bank information. See the memory.txt binding for full
1221 * @param blob Device tree blob
1222 * @param area Name of node to check (NULL means "/memory")
1223 * @param board_id Board ID to look up
1224 * @param basep Returns base address of first memory bank (NULL to
1226 * @param sizep Returns total memory size (NULL to ignore)
1227 * @param bd Updated with the memory bank information (NULL to skip)
1228 * @return 0 if OK, -ve on error
1230 int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1231 phys_addr_t *basep, phys_size_t *sizep,
1232 struct bd_info *bd);