Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 0a9d66f | 2012-07-12 05:25:02 +0000 | [diff] [blame] | 6 | #ifndef __fdtdec_h |
| 7 | #define __fdtdec_h |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 8 | |
| 9 | /* |
| 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. |
| 15 | */ |
| 16 | |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 18 | #include <pci.h> |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
Johan Jonker | 02e3e06 | 2023-03-13 01:33:23 +0100 | [diff] [blame] | 21 | * Support for 64bit fdt addresses. |
| 22 | * This can be used not only for 64bit SoCs, but also |
| 23 | * for large address extensions on 32bit SoCs. |
| 24 | * Note that fdt data is always big |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 25 | * endian even on a litle endian machine. |
| 26 | */ |
Thierry Reding | 3d4ffa0 | 2019-03-21 19:10:00 +0100 | [diff] [blame] | 27 | |
Chen Guanqiao | 223f17d | 2021-04-12 14:51:11 +0800 | [diff] [blame] | 28 | #define FDT_SIZE_T_NONE (-1U) |
| 29 | |
Johan Jonker | 02e3e06 | 2023-03-13 01:33:23 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_FDT_64BIT |
| 31 | typedef u64 fdt_addr_t; |
| 32 | typedef u64 fdt_size_t; |
Patrice Chotard | b4c5211 | 2022-01-04 08:42:48 +0100 | [diff] [blame] | 33 | #define FDT_ADDR_T_NONE ((ulong)(-1)) |
| 34 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 35 | #define fdt_addr_to_cpu(reg) be64_to_cpu(reg) |
Simon Glass | f909b9c | 2012-10-25 16:31:00 +0000 | [diff] [blame] | 36 | #define fdt_size_to_cpu(reg) be64_to_cpu(reg) |
Thierry Reding | 570a793 | 2019-03-21 19:09:59 +0100 | [diff] [blame] | 37 | #define cpu_to_fdt_addr(reg) cpu_to_be64(reg) |
| 38 | #define cpu_to_fdt_size(reg) cpu_to_be64(reg) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 39 | typedef fdt64_t fdt_val_t; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 40 | #else |
Johan Jonker | 02e3e06 | 2023-03-13 01:33:23 +0100 | [diff] [blame] | 41 | typedef u32 fdt_addr_t; |
| 42 | typedef u32 fdt_size_t; |
Patrice Chotard | b4c5211 | 2022-01-04 08:42:48 +0100 | [diff] [blame] | 43 | #define FDT_ADDR_T_NONE (-1U) |
| 44 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 45 | #define fdt_addr_to_cpu(reg) be32_to_cpu(reg) |
Simon Glass | f909b9c | 2012-10-25 16:31:00 +0000 | [diff] [blame] | 46 | #define fdt_size_to_cpu(reg) be32_to_cpu(reg) |
Thierry Reding | 570a793 | 2019-03-21 19:09:59 +0100 | [diff] [blame] | 47 | #define cpu_to_fdt_addr(reg) cpu_to_be32(reg) |
| 48 | #define cpu_to_fdt_size(reg) cpu_to_be32(reg) |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 49 | typedef fdt32_t fdt_val_t; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | /* Information obtained about memory from the FDT */ |
| 53 | struct fdt_memory { |
| 54 | fdt_addr_t start; |
| 55 | fdt_addr_t end; |
| 56 | }; |
| 57 | |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 58 | struct bd_info; |
| 59 | |
Simon Glass | bed6bc7 | 2021-12-16 20:59:33 -0700 | [diff] [blame] | 60 | /** |
| 61 | * enum fdt_source_t - indicates where the devicetree came from |
| 62 | * |
| 63 | * These are listed in approximate order of desirability after FDTSRC_NONE |
| 64 | * |
| 65 | * @FDTSRC_SEPARATE: Appended to U-Boot. This is the normal approach if U-Boot |
| 66 | * is the only firmware being booted |
| 67 | * @FDTSRC_FIT: Found in a multi-dtb FIT. This should be used when U-Boot must |
| 68 | * select a devicetree from many options |
| 69 | * @FDTSRC_BOARD: Located by custom board code. This should only be used when |
| 70 | * the prior stage does not support FDTSRC_PASSAGE |
| 71 | * @FDTSRC_EMBED: Embedded into U-Boot executable. This should onyl be used when |
| 72 | * U-Boot is packaged as an ELF file, e.g. for debugging purposes |
| 73 | * @FDTSRC_ENV: Provided by the fdtcontroladdr environment variable. This should |
| 74 | * be used for debugging/development only |
| 75 | * @FDTSRC_NONE: No devicetree at all |
| 76 | */ |
| 77 | enum fdt_source_t { |
| 78 | FDTSRC_SEPARATE, |
| 79 | FDTSRC_FIT, |
| 80 | FDTSRC_BOARD, |
| 81 | FDTSRC_EMBED, |
| 82 | FDTSRC_ENV, |
| 83 | }; |
| 84 | |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 85 | /* |
| 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. |
| 89 | */ |
| 90 | struct fdt_resource { |
| 91 | fdt_addr_t start; |
| 92 | fdt_addr_t end; |
| 93 | }; |
| 94 | |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 95 | enum fdt_pci_space { |
| 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, |
| 102 | }; |
| 103 | |
| 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)) |
| 108 | |
| 109 | /* |
| 110 | * The Open Firmware spec defines PCI physical address as follows: |
| 111 | * |
| 112 | * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00 |
| 113 | * |
| 114 | * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr |
| 115 | * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh |
| 116 | * phys.lo cell: llllllll llllllll llllllll llllllll |
| 117 | * |
| 118 | * where: |
| 119 | * |
| 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 |
| 131 | */ |
| 132 | struct fdt_pci_addr { |
| 133 | u32 phys_hi; |
| 134 | u32 phys_mid; |
| 135 | u32 phys_lo; |
| 136 | }; |
| 137 | |
Simon Glass | e16f4c4 | 2019-12-28 10:44:42 -0700 | [diff] [blame] | 138 | extern u8 __dtb_dt_begin[]; /* embedded device tree blob */ |
| 139 | extern u8 __dtb_dt_spl_begin[]; /* embedded device tree blob for SPL/TPL */ |
| 140 | |
Simon Glass | bbe5797 | 2021-12-16 20:59:26 -0700 | [diff] [blame] | 141 | /* Get a pointer to the embedded devicetree, if there is one, else NULL */ |
| 142 | static inline u8 *dtb_dt_embedded(void) |
| 143 | { |
| 144 | #ifdef CONFIG_OF_EMBED |
| 145 | # ifdef CONFIG_SPL_BUILD |
| 146 | return __dtb_dt_spl_begin; |
| 147 | # else |
| 148 | return __dtb_dt_begin; |
| 149 | # endif |
| 150 | #else |
| 151 | return NULL; |
| 152 | #endif |
| 153 | } |
| 154 | |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 155 | /** |
| 156 | * Compute the size of a resource. |
| 157 | * |
| 158 | * @param res the resource to operate on |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 159 | * Return: the size of the resource |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 160 | */ |
| 161 | static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res) |
| 162 | { |
| 163 | return res->end - res->start + 1; |
| 164 | } |
| 165 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 166 | /** |
| 167 | * Compat types that we know about and for which we might have drivers. |
| 168 | * Each is named COMPAT_<dir>_<filename> where <dir> is the directory |
| 169 | * within drivers. |
| 170 | */ |
| 171 | enum fdt_compat_id { |
| 172 | COMPAT_UNKNOWN, |
Allen Martin | 55d98a1 | 2012-08-31 08:30:00 +0000 | [diff] [blame] | 173 | COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */ |
| 174 | COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */ |
Jim Lin | 5d309e6 | 2012-07-29 20:53:29 +0000 | [diff] [blame] | 175 | COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */ |
Thierry Reding | f202e02 | 2014-12-09 22:25:09 -0700 | [diff] [blame] | 176 | COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL, |
| 177 | /* Tegra124 XUSB pad controller */ |
Tom Warren | ab0cc6b | 2015-03-04 16:36:00 -0700 | [diff] [blame] | 178 | COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL, |
| 179 | /* Tegra210 XUSB pad controller */ |
Rajeshwari Shinde | d6e9f44 | 2013-01-07 23:35:05 +0000 | [diff] [blame] | 180 | COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */ |
Vivek Gautam | f874523 | 2013-09-14 14:02:48 +0530 | [diff] [blame] | 181 | COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */ |
Akshay Saraswat | aa77318 | 2013-02-25 01:13:01 +0000 | [diff] [blame] | 182 | COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */ |
Piotr Wilczek | 24e368a | 2014-03-07 14:59:39 +0100 | [diff] [blame] | 183 | COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */ |
Jaehoon Chung | e3fb6fa | 2014-05-16 13:59:51 +0900 | [diff] [blame] | 184 | COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */ |
Simon Glass | 60956094 | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 185 | COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */ |
Ajay Kumar | e24c502 | 2014-09-05 16:53:33 +0530 | [diff] [blame] | 186 | COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */ |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 187 | COMPAT_INTEL_MICROCODE, /* Intel microcode update */ |
Bin Meng | 2bfd264 | 2015-02-05 23:42:26 +0800 | [diff] [blame] | 188 | COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */ |
Marek Vasut | f3f8fe2 | 2015-07-25 19:33:56 +0200 | [diff] [blame] | 189 | COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */ |
Marek Vasut | 1749723 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 190 | COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */ |
Marek Vasut | f26c1ae | 2015-12-05 19:28:44 +0100 | [diff] [blame] | 191 | COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */ |
Andrew Bradford | 74fdb58 | 2015-08-07 08:36:35 -0400 | [diff] [blame] | 192 | COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */ |
| 193 | COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */ |
Bin Meng | 5ce2d8b | 2015-12-11 02:55:44 -0800 | [diff] [blame] | 194 | COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */ |
Ley Foon Tan | b25ae72 | 2017-04-05 17:32:47 +0800 | [diff] [blame] | 195 | COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */ |
| 196 | COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */ |
| 197 | COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */ |
| 198 | COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */ |
| 199 | COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */ |
| 200 | COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */ |
| 201 | COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */ |
| 202 | COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */ |
Tien Fong Chee | bbfa5eb | 2017-09-25 16:40:03 +0800 | [diff] [blame] | 203 | COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */ |
| 204 | COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */ |
Marek Vasut | ee4e36b | 2018-05-12 11:56:10 +0200 | [diff] [blame] | 205 | COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */ |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 206 | |
| 207 | COMPAT_COUNT, |
| 208 | }; |
| 209 | |
Simon Glass | b491e9c | 2015-01-05 20:05:26 -0700 | [diff] [blame] | 210 | #define MAX_PHANDLE_ARGS 16 |
| 211 | struct fdtdec_phandle_args { |
| 212 | int node; |
| 213 | int args_count; |
| 214 | uint32_t args[MAX_PHANDLE_ARGS]; |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list |
| 219 | * |
| 220 | * This function is useful to parse lists of phandles and their arguments. |
| 221 | * |
| 222 | * Example: |
| 223 | * |
| 224 | * phandle1: node1 { |
| 225 | * #list-cells = <2>; |
| 226 | * } |
| 227 | * |
| 228 | * phandle2: node2 { |
| 229 | * #list-cells = <1>; |
| 230 | * } |
| 231 | * |
| 232 | * node3 { |
| 233 | * list = <&phandle1 1 2 &phandle2 3>; |
| 234 | * } |
| 235 | * |
| 236 | * To get a device_node of the `node2' node you may call this: |
| 237 | * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1, |
| 238 | * &args); |
| 239 | * |
| 240 | * (This function is a modified version of __of_parse_phandle_with_args() from |
| 241 | * Linux 3.18) |
| 242 | * |
| 243 | * @blob: Pointer to device tree |
| 244 | * @src_node: Offset of device tree node containing a list |
| 245 | * @list_name: property name that contains a list |
| 246 | * @cells_name: property name that specifies the phandles' arguments count, |
| 247 | * or NULL to use @cells_count |
| 248 | * @cells_count: Cell count to use if @cells_name is NULL |
| 249 | * @index: index of a phandle to parse out |
| 250 | * @out_args: optional pointer to output arguments structure (will be filled) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 251 | * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if |
Simon Glass | b491e9c | 2015-01-05 20:05:26 -0700 | [diff] [blame] | 252 | * @list_name does not exist, a phandle was not found, @cells_name |
| 253 | * could not be found, the arguments were truncated or there were too |
| 254 | * many arguments. |
| 255 | * |
| 256 | */ |
| 257 | int fdtdec_parse_phandle_with_args(const void *blob, int src_node, |
| 258 | const char *list_name, |
| 259 | const char *cells_name, |
| 260 | int cell_count, int index, |
| 261 | struct fdtdec_phandle_args *out_args); |
| 262 | |
Sean Paul | adbcfc9 | 2012-10-25 16:31:06 +0000 | [diff] [blame] | 263 | /** |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 264 | * Find the next numbered alias for a peripheral. This is used to enumerate |
| 265 | * all the peripherals of a certain type. |
| 266 | * |
| 267 | * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then |
| 268 | * this function will return a pointer to the node the alias points to, and |
| 269 | * then update *upto to 1. Next time you call this function, the next node |
| 270 | * will be returned. |
| 271 | * |
| 272 | * All nodes returned will match the compatible ID, as it is assumed that |
| 273 | * all peripherals use the same driver. |
| 274 | * |
| 275 | * @param blob FDT blob to use |
| 276 | * @param name Root name of alias to search for |
| 277 | * @param id Compatible ID to look for |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 278 | * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 279 | */ |
| 280 | int fdtdec_next_alias(const void *blob, const char *name, |
| 281 | enum fdt_compat_id id, int *upto); |
| 282 | |
| 283 | /** |
Gerald Van Baren | cdac339 | 2012-11-12 23:13:54 -0500 | [diff] [blame] | 284 | * Find the compatible ID for a given node. |
| 285 | * |
| 286 | * Generally each node has at least one compatible string attached to it. |
| 287 | * This function looks through our list of known compatible strings and |
| 288 | * returns the corresponding ID which matches the compatible string. |
| 289 | * |
| 290 | * @param blob FDT blob to use |
| 291 | * @param node Node containing compatible string to find |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 292 | * Return: compatible ID, or COMPAT_UNKNOWN if we cannot find a match |
Gerald Van Baren | cdac339 | 2012-11-12 23:13:54 -0500 | [diff] [blame] | 293 | */ |
| 294 | enum fdt_compat_id fdtdec_lookup(const void *blob, int node); |
| 295 | |
| 296 | /** |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 297 | * Find the next compatible node for a peripheral. |
| 298 | * |
| 299 | * Do the first call with node = 0. This function will return a pointer to |
| 300 | * the next compatible node. Next time you call this function, pass the |
| 301 | * value returned, and the next node will be provided. |
| 302 | * |
| 303 | * @param blob FDT blob to use |
| 304 | * @param node Start node for search |
| 305 | * @param id Compatible ID to look for (enum fdt_compat_id) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 306 | * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 307 | */ |
| 308 | int fdtdec_next_compatible(const void *blob, int node, |
| 309 | enum fdt_compat_id id); |
| 310 | |
| 311 | /** |
Simon Glass | 2e979b1 | 2012-04-02 13:18:42 +0000 | [diff] [blame] | 312 | * Find the next compatible subnode for a peripheral. |
| 313 | * |
| 314 | * Do the first call with node set to the parent and depth = 0. This |
| 315 | * function will return the offset of the next compatible node. Next time |
| 316 | * you call this function, pass the node value returned last time, with |
| 317 | * depth unchanged, and the next node will be provided. |
| 318 | * |
| 319 | * @param blob FDT blob to use |
| 320 | * @param node Start node for search |
| 321 | * @param id Compatible ID to look for (enum fdt_compat_id) |
| 322 | * @param depthp Current depth (set to 0 before first call) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 323 | * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more |
Simon Glass | 2e979b1 | 2012-04-02 13:18:42 +0000 | [diff] [blame] | 324 | */ |
| 325 | int fdtdec_next_compatible_subnode(const void *blob, int node, |
| 326 | enum fdt_compat_id id, int *depthp); |
| 327 | |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 328 | /* |
| 329 | * Look up an address property in a node and return the parsed address, and |
| 330 | * optionally the parsed size. |
| 331 | * |
| 332 | * This variant assumes a known and fixed number of cells are used to |
| 333 | * represent the address and size. |
| 334 | * |
| 335 | * You probably don't want to use this function directly except to parse |
| 336 | * non-standard properties, and never to parse the "reg" property. Instead, |
| 337 | * use one of the "auto" variants below, which automatically honor the |
| 338 | * #address-cells and #size-cells properties in the parent node. |
| 339 | * |
| 340 | * @param blob FDT blob |
| 341 | * @param node node to examine |
| 342 | * @param prop_name name of property to find |
| 343 | * @param index which address to retrieve from a list of addresses. Often 0. |
| 344 | * @param na the number of cells used to represent an address |
| 345 | * @param ns the number of cells used to represent a size |
| 346 | * @param sizep a pointer to store the size into. Use NULL if not required |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 347 | * @param translate Indicates whether to translate the returned value |
| 348 | * using the parent node's ranges property. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 349 | * Return: address, if found, or FDT_ADDR_T_NONE if not |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 350 | */ |
| 351 | fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node, |
| 352 | const char *prop_name, int index, int na, int ns, |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 353 | fdt_size_t *sizep, bool translate); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * Look up an address property in a node and return the parsed address, and |
| 357 | * optionally the parsed size. |
| 358 | * |
| 359 | * This variant automatically determines the number of cells used to represent |
| 360 | * the address and size by parsing the provided parent node's #address-cells |
| 361 | * and #size-cells properties. |
| 362 | * |
| 363 | * @param blob FDT blob |
| 364 | * @param parent parent node of @node |
| 365 | * @param node node to examine |
| 366 | * @param prop_name name of property to find |
| 367 | * @param index which address to retrieve from a list of addresses. Often 0. |
| 368 | * @param sizep a pointer to store the size into. Use NULL if not required |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 369 | * @param translate Indicates whether to translate the returned value |
| 370 | * using the parent node's ranges property. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 371 | * Return: address, if found, or FDT_ADDR_T_NONE if not |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 372 | */ |
| 373 | fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent, |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 374 | int node, const char *prop_name, int index, fdt_size_t *sizep, |
| 375 | bool translate); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * Look up an address property in a node and return the parsed address, and |
| 379 | * optionally the parsed size. |
| 380 | * |
| 381 | * This variant automatically determines the number of cells used to represent |
| 382 | * the address and size by parsing the parent node's #address-cells |
| 383 | * and #size-cells properties. The parent node is automatically found. |
| 384 | * |
| 385 | * The automatic parent lookup implemented by this function is slow. |
| 386 | * Consequently, fdtdec_get_addr_size_auto_parent() should be used where |
| 387 | * possible. |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 388 | * |
| 389 | * @param blob FDT blob |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 390 | * @param parent parent node of @node |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 391 | * @param node node to examine |
| 392 | * @param prop_name name of property to find |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 393 | * @param index which address to retrieve from a list of addresses. Often 0. |
| 394 | * @param sizep a pointer to store the size into. Use NULL if not required |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 395 | * @param translate Indicates whether to translate the returned value |
| 396 | * using the parent node's ranges property. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 397 | * Return: address, if found, or FDT_ADDR_T_NONE if not |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 398 | */ |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 399 | fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node, |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 400 | const char *prop_name, int index, fdt_size_t *sizep, |
| 401 | bool translate); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * Look up an address property in a node and return the parsed address. |
| 405 | * |
| 406 | * This variant hard-codes the number of cells used to represent the address |
| 407 | * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also |
| 408 | * always returns the first address value in the property (index 0). |
| 409 | * |
| 410 | * Use of this function is not recommended due to the hard-coding of cell |
| 411 | * counts. There is no programmatic validation that these hard-coded values |
| 412 | * actually match the device tree content in any way at all. This assumption |
| 413 | * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately |
| 414 | * set in the U-Boot build and exercising strict control over DT content to |
| 415 | * ensure use of matching #address-cells/#size-cells properties. However, this |
| 416 | * approach is error-prone; those familiar with DT will not expect the |
| 417 | * assumption to exist, and could easily invalidate it. If the assumption is |
| 418 | * invalidated, this function will not report the issue, and debugging will |
| 419 | * be required. Instead, use fdtdec_get_addr_size_auto_parent(). |
| 420 | * |
| 421 | * @param blob FDT blob |
| 422 | * @param node node to examine |
| 423 | * @param prop_name name of property to find |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 424 | * Return: address, if found, or FDT_ADDR_T_NONE if not |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 425 | */ |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 426 | fdt_addr_t fdtdec_get_addr(const void *blob, int node, |
| 427 | const char *prop_name); |
| 428 | |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 429 | /* |
| 430 | * Look up an address property in a node and return the parsed address, and |
| 431 | * optionally the parsed size. |
| 432 | * |
| 433 | * This variant hard-codes the number of cells used to represent the address |
| 434 | * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also |
| 435 | * always returns the first address value in the property (index 0). |
| 436 | * |
| 437 | * Use of this function is not recommended due to the hard-coding of cell |
| 438 | * counts. There is no programmatic validation that these hard-coded values |
| 439 | * actually match the device tree content in any way at all. This assumption |
| 440 | * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately |
| 441 | * set in the U-Boot build and exercising strict control over DT content to |
| 442 | * ensure use of matching #address-cells/#size-cells properties. However, this |
| 443 | * approach is error-prone; those familiar with DT will not expect the |
| 444 | * assumption to exist, and could easily invalidate it. If the assumption is |
| 445 | * invalidated, this function will not report the issue, and debugging will |
| 446 | * be required. Instead, use fdtdec_get_addr_size_auto_parent(). |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 447 | * |
| 448 | * @param blob FDT blob |
| 449 | * @param node node to examine |
| 450 | * @param prop_name name of property to find |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 451 | * @param sizep a pointer to store the size into. Use NULL if not required |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 452 | * Return: address, if found, or FDT_ADDR_T_NONE if not |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 453 | */ |
| 454 | fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, |
| 455 | const char *prop_name, fdt_size_t *sizep); |
| 456 | |
| 457 | /** |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 458 | * Look at the compatible property of a device node that represents a PCI |
| 459 | * device and extract pci vendor id and device id from it. |
| 460 | * |
| 461 | * @param blob FDT blob |
| 462 | * @param node node to examine |
| 463 | * @param vendor vendor id of the pci device |
| 464 | * @param device device id of the pci device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 465 | * Return: 0 if ok, negative on error |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 466 | */ |
| 467 | int fdtdec_get_pci_vendev(const void *blob, int node, |
| 468 | u16 *vendor, u16 *device); |
| 469 | |
| 470 | /** |
| 471 | * Look at the pci address of a device node that represents a PCI device |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 472 | * and return base address of the pci device's registers. |
| 473 | * |
Simon Glass | f1037ad | 2015-11-29 13:17:54 -0700 | [diff] [blame] | 474 | * @param dev device to examine |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 475 | * @param addr pci address in the form of fdt_pci_addr |
| 476 | * @param bar returns base address of the pci device's registers |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 477 | * Return: 0 if ok, negative on error |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 478 | */ |
Simon Glass | c92aac1 | 2020-01-27 08:49:38 -0700 | [diff] [blame] | 479 | int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr, |
Simon Glass | f1037ad | 2015-11-29 13:17:54 -0700 | [diff] [blame] | 480 | u32 *bar); |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 481 | |
| 482 | /** |
Suneel Garapati | 78d0bea | 2019-10-19 15:19:35 -0700 | [diff] [blame] | 483 | * Look at the bus range property of a device node and return the pci bus |
| 484 | * range for this node. |
| 485 | * The property must hold one fdt_pci_addr with a length. |
| 486 | * @param blob FDT blob |
| 487 | * @param node node to examine |
| 488 | * @param res the resource structure to return the bus range |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 489 | * Return: 0 if ok, negative on error |
Suneel Garapati | 78d0bea | 2019-10-19 15:19:35 -0700 | [diff] [blame] | 490 | */ |
| 491 | |
| 492 | int fdtdec_get_pci_bus_range(const void *blob, int node, |
| 493 | struct fdt_resource *res); |
| 494 | |
| 495 | /** |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 496 | * Look up a 32-bit integer property in a node and return it. The property |
| 497 | * must have at least 4 bytes of data. The value of the first cell is |
| 498 | * returned. |
| 499 | * |
| 500 | * @param blob FDT blob |
| 501 | * @param node node to examine |
| 502 | * @param prop_name name of property to find |
| 503 | * @param default_val default value to return if the property is not found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 504 | * Return: integer value, if found, or default_val if not |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 505 | */ |
| 506 | s32 fdtdec_get_int(const void *blob, int node, const char *prop_name, |
| 507 | s32 default_val); |
| 508 | |
| 509 | /** |
Chin Liang See | c97b074 | 2015-10-17 08:30:32 -0500 | [diff] [blame] | 510 | * Unsigned version of fdtdec_get_int. The property must have at least |
| 511 | * 4 bytes of data. The value of the first cell is returned. |
| 512 | * |
| 513 | * @param blob FDT blob |
| 514 | * @param node node to examine |
| 515 | * @param prop_name name of property to find |
| 516 | * @param default_val default value to return if the property is not found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 517 | * Return: unsigned integer value, if found, or default_val if not |
Chin Liang See | c97b074 | 2015-10-17 08:30:32 -0500 | [diff] [blame] | 518 | */ |
| 519 | unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name, |
| 520 | unsigned int default_val); |
| 521 | |
| 522 | /** |
Simon Glass | 9ffbe33 | 2015-03-05 12:25:14 -0700 | [diff] [blame] | 523 | * Get a variable-sized number from a property |
| 524 | * |
| 525 | * This reads a number from one or more cells. |
| 526 | * |
| 527 | * @param ptr Pointer to property |
| 528 | * @param cells Number of cells containing the number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 529 | * Return: the value in the cells |
Simon Glass | 9ffbe33 | 2015-03-05 12:25:14 -0700 | [diff] [blame] | 530 | */ |
| 531 | u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells); |
| 532 | |
| 533 | /** |
Che-Liang Chiou | d81d017 | 2012-10-25 16:31:05 +0000 | [diff] [blame] | 534 | * Look up a 64-bit integer property in a node and return it. The property |
| 535 | * must have at least 8 bytes of data (2 cells). The first two cells are |
| 536 | * concatenated to form a 8 bytes value, where the first cell is top half and |
| 537 | * the second cell is bottom half. |
| 538 | * |
| 539 | * @param blob FDT blob |
| 540 | * @param node node to examine |
| 541 | * @param prop_name name of property to find |
| 542 | * @param default_val default value to return if the property is not found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 543 | * Return: integer value, if found, or default_val if not |
Che-Liang Chiou | d81d017 | 2012-10-25 16:31:05 +0000 | [diff] [blame] | 544 | */ |
| 545 | uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name, |
| 546 | uint64_t default_val); |
| 547 | |
| 548 | /** |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 549 | * Checks whether a node is enabled. |
| 550 | * This looks for a 'status' property. If this exists, then returns 1 if |
| 551 | * the status is 'ok' and 0 otherwise. If there is no status property, |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 552 | * it returns 1 on the assumption that anything mentioned should be enabled |
| 553 | * by default. |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 554 | * |
| 555 | * @param blob FDT blob |
| 556 | * @param node node to examine |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 557 | * Return: integer value 0 (not enabled) or 1 (enabled) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 558 | */ |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 559 | int fdtdec_get_is_enabled(const void *blob, int node); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 560 | |
| 561 | /** |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 562 | * Checks that we have a valid fdt available to control U-Boot. |
| 563 | |
| 564 | * However, if not then for the moment nothing is done, since this function |
| 565 | * is called too early to panic(). |
| 566 | * |
| 567 | * @returns 0 |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 568 | */ |
| 569 | int fdtdec_check_fdt(void); |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 570 | |
| 571 | /** |
| 572 | * Find the nodes for a peripheral and return a list of them in the correct |
| 573 | * order. This is used to enumerate all the peripherals of a certain type. |
| 574 | * |
| 575 | * To use this, optionally set up a /aliases node with alias properties for |
| 576 | * a peripheral. For example, for usb you could have: |
| 577 | * |
| 578 | * aliases { |
| 579 | * usb0 = "/ehci@c5008000"; |
| 580 | * usb1 = "/ehci@c5000000"; |
| 581 | * }; |
| 582 | * |
| 583 | * Pass "usb" as the name to this function and will return a list of two |
| 584 | * nodes offsets: /ehci@c5008000 and ehci@c5000000. |
| 585 | * |
| 586 | * All nodes returned will match the compatible ID, as it is assumed that |
| 587 | * all peripherals use the same driver. |
| 588 | * |
| 589 | * If no alias node is found, then the node list will be returned in the |
| 590 | * order found in the fdt. If the aliases mention a node which doesn't |
| 591 | * exist, then this will be ignored. If nodes are found with no aliases, |
| 592 | * they will be added in any order. |
| 593 | * |
| 594 | * If there is a gap in the aliases, then this function return a 0 node at |
| 595 | * that position. The return value will also count these gaps. |
| 596 | * |
| 597 | * This function checks node properties and will not return nodes which are |
| 598 | * marked disabled (status = "disabled"). |
| 599 | * |
| 600 | * @param blob FDT blob to use |
| 601 | * @param name Root name of alias to search for |
| 602 | * @param id Compatible ID to look for |
| 603 | * @param node_list Place to put list of found nodes |
| 604 | * @param maxcount Maximum number of nodes to find |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 605 | * Return: number of nodes found on success, FDT_ERR_... on error |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 606 | */ |
| 607 | int fdtdec_find_aliases_for_id(const void *blob, const char *name, |
| 608 | enum fdt_compat_id id, int *node_list, int maxcount); |
| 609 | |
| 610 | /* |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 611 | * This function is similar to fdtdec_find_aliases_for_id() except that it |
| 612 | * adds to the node_list that is passed in. Any 0 elements are considered |
| 613 | * available for allocation - others are considered already used and are |
| 614 | * skipped. |
| 615 | * |
| 616 | * You can use this by calling fdtdec_find_aliases_for_id() with an |
| 617 | * uninitialised array, then setting the elements that are returned to -1, |
| 618 | * say, then calling this function, perhaps with a different compat id. |
| 619 | * Any elements you get back that are >0 are new nodes added by the call |
| 620 | * to this function. |
| 621 | * |
| 622 | * Note that if you have some nodes with aliases and some without, you are |
| 623 | * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with |
| 624 | * one compat_id may fill in positions for which you have aliases defined |
| 625 | * for another compat_id. When you later call *this* function with the second |
| 626 | * compat_id, the alias positions may already be used. A debug warning may |
| 627 | * be generated in this case, but it is safest to define aliases for all |
| 628 | * nodes when you care about the ordering. |
| 629 | */ |
| 630 | int fdtdec_add_aliases_for_id(const void *blob, const char *name, |
| 631 | enum fdt_compat_id id, int *node_list, int maxcount); |
| 632 | |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 633 | /** |
| 634 | * Get the alias sequence number of a node |
| 635 | * |
| 636 | * This works out whether a node is pointed to by an alias, and if so, the |
| 637 | * sequence number of that alias. Aliases are of the form <base><num> where |
| 638 | * <num> is the sequence number. For example spi2 would be sequence number |
| 639 | * 2. |
| 640 | * |
| 641 | * @param blob Device tree blob (if NULL, then error is returned) |
| 642 | * @param base Base name for alias (before the underscore) |
| 643 | * @param node Node to look up |
| 644 | * @param seqp This is set to the sequence number if one is found, |
| 645 | * but otherwise the value is left alone |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 646 | * Return: 0 if a sequence was found, -ve if not |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 647 | */ |
| 648 | int fdtdec_get_alias_seq(const void *blob, const char *base, int node, |
| 649 | int *seqp); |
| 650 | |
Simon Glass | c95f678 | 2014-07-23 06:55:16 -0600 | [diff] [blame] | 651 | /** |
Michal Simek | 4b58667 | 2019-01-31 16:30:58 +0100 | [diff] [blame] | 652 | * Get the highest alias number for susbystem. |
| 653 | * |
| 654 | * It parses all aliases and find out highest recorded alias for subsystem. |
| 655 | * Aliases are of the form <base><num> where <num> is the sequence number. |
| 656 | * |
| 657 | * @param blob Device tree blob (if NULL, then error is returned) |
| 658 | * @param base Base name for alias susbystem (before the number) |
| 659 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 660 | * Return: 0 highest alias ID, -1 if not found |
Michal Simek | 4b58667 | 2019-01-31 16:30:58 +0100 | [diff] [blame] | 661 | */ |
| 662 | int fdtdec_get_alias_highest_id(const void *blob, const char *base); |
| 663 | |
| 664 | /** |
Simon Glass | 088dccb | 2015-10-17 19:41:14 -0600 | [diff] [blame] | 665 | * Get a property from the /chosen node |
| 666 | * |
| 667 | * @param blob Device tree blob (if NULL, then NULL is returned) |
| 668 | * @param name Property name to look up |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 669 | * Return: Value of property, or NULL if it does not exist |
Simon Glass | 088dccb | 2015-10-17 19:41:14 -0600 | [diff] [blame] | 670 | */ |
| 671 | const char *fdtdec_get_chosen_prop(const void *blob, const char *name); |
| 672 | |
| 673 | /** |
| 674 | * Get the offset of the given /chosen node |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 675 | * |
| 676 | * This looks up a property in /chosen containing the path to another node, |
| 677 | * then finds the offset of that node. |
| 678 | * |
| 679 | * @param blob Device tree blob (if NULL, then error is returned) |
| 680 | * @param name Property name, e.g. "stdout-path" |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 681 | * Return: Node offset referred to by that chosen node, or -ve FDT_ERR_... |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 682 | */ |
| 683 | int fdtdec_get_chosen_node(const void *blob, const char *name); |
| 684 | |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 685 | /* |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 686 | * Get the name for a compatible ID |
| 687 | * |
| 688 | * @param id Compatible ID to look for |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 689 | * Return: compatible string for that id |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 690 | */ |
| 691 | const char *fdtdec_get_compatible(enum fdt_compat_id id); |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 692 | |
| 693 | /* Look up a phandle and follow it to its node. Then return the offset |
| 694 | * of that node. |
| 695 | * |
| 696 | * @param blob FDT blob |
| 697 | * @param node node to examine |
| 698 | * @param prop_name name of property to find |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 699 | * Return: node offset if found, -ve error code on error |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 700 | */ |
| 701 | int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name); |
| 702 | |
| 703 | /** |
| 704 | * Look up a property in a node and return its contents in an integer |
| 705 | * array of given length. The property must have at least enough data for |
| 706 | * the array (4*count bytes). It may have more, but this will be ignored. |
| 707 | * |
| 708 | * @param blob FDT blob |
| 709 | * @param node node to examine |
| 710 | * @param prop_name name of property to find |
| 711 | * @param array array to fill with data |
| 712 | * @param count number of array elements |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 713 | * Return: 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found, |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 714 | * or -FDT_ERR_BADLAYOUT if not enough data |
| 715 | */ |
| 716 | int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, |
| 717 | u32 *array, int count); |
| 718 | |
| 719 | /** |
Simon Glass | eda0c3f | 2014-11-10 18:00:19 -0700 | [diff] [blame] | 720 | * Look up a property in a node and return its contents in an integer |
| 721 | * array of given length. The property must exist but may have less data that |
| 722 | * expected (4*count bytes). It may have more, but this will be ignored. |
| 723 | * |
| 724 | * @param blob FDT blob |
| 725 | * @param node node to examine |
| 726 | * @param prop_name name of property to find |
| 727 | * @param array array to fill with data |
| 728 | * @param count number of array elements |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 729 | * Return: number of array elements if ok, or -FDT_ERR_NOTFOUND if the |
Simon Glass | eda0c3f | 2014-11-10 18:00:19 -0700 | [diff] [blame] | 730 | * property is not found |
| 731 | */ |
| 732 | int fdtdec_get_int_array_count(const void *blob, int node, |
| 733 | const char *prop_name, u32 *array, int count); |
| 734 | |
| 735 | /** |
Simon Glass | 744ec23 | 2012-04-02 13:18:41 +0000 | [diff] [blame] | 736 | * Look up a property in a node and return a pointer to its contents as a |
| 737 | * unsigned int array of given length. The property must have at least enough |
| 738 | * data for the array ('count' cells). It may have more, but this will be |
| 739 | * ignored. The data is not copied. |
| 740 | * |
| 741 | * Note that you must access elements of the array with fdt32_to_cpu(), |
| 742 | * since the elements will be big endian even on a little endian machine. |
| 743 | * |
| 744 | * @param blob FDT blob |
| 745 | * @param node node to examine |
| 746 | * @param prop_name name of property to find |
| 747 | * @param count number of array elements |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 748 | * Return: pointer to array if found, or NULL if the property is not |
Simon Glass | 744ec23 | 2012-04-02 13:18:41 +0000 | [diff] [blame] | 749 | * found or there is not enough data |
| 750 | */ |
| 751 | const u32 *fdtdec_locate_array(const void *blob, int node, |
| 752 | const char *prop_name, int count); |
| 753 | |
| 754 | /** |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 755 | * Look up a boolean property in a node and return it. |
| 756 | * |
| 757 | * A boolean properly is true if present in the device tree and false if not |
| 758 | * present, regardless of its value. |
| 759 | * |
| 760 | * @param blob FDT blob |
| 761 | * @param node node to examine |
| 762 | * @param prop_name name of property to find |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 763 | * Return: 1 if the properly is present; 0 if it isn't present |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 764 | */ |
| 765 | int fdtdec_get_bool(const void *blob, int node, const char *prop_name); |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 766 | |
Peng Fan | 23dd3a0 | 2016-02-01 13:31:15 +0800 | [diff] [blame] | 767 | /* |
| 768 | * Count child nodes of one parent node. |
| 769 | * |
| 770 | * @param blob FDT blob |
| 771 | * @param node parent node |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 772 | * Return: number of child node; 0 if there is not child node |
Peng Fan | 23dd3a0 | 2016-02-01 13:31:15 +0800 | [diff] [blame] | 773 | */ |
| 774 | int fdtdec_get_child_count(const void *blob, int node); |
| 775 | |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 776 | /* |
| 777 | * Look up a property in a node and return its contents in a byte |
| 778 | * array of given length. The property must have at least enough data for |
| 779 | * the array (count bytes). It may have more, but this will be ignored. |
| 780 | * |
| 781 | * @param blob FDT blob |
| 782 | * @param node node to examine |
| 783 | * @param prop_name name of property to find |
| 784 | * @param array array to fill with data |
| 785 | * @param count number of array elements |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 786 | * Return: 0 if ok, or -FDT_ERR_MISSING if the property is not found, |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 787 | * or -FDT_ERR_BADLAYOUT if not enough data |
| 788 | */ |
| 789 | int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name, |
| 790 | u8 *array, int count); |
| 791 | |
| 792 | /** |
| 793 | * Look up a property in a node and return a pointer to its contents as a |
| 794 | * byte array of given length. The property must have at least enough data |
| 795 | * for the array (count bytes). It may have more, but this will be ignored. |
| 796 | * The data is not copied. |
| 797 | * |
| 798 | * @param blob FDT blob |
| 799 | * @param node node to examine |
| 800 | * @param prop_name name of property to find |
| 801 | * @param count number of array elements |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 802 | * Return: pointer to byte array if found, or NULL if the property is not |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 803 | * found or there is not enough data |
| 804 | */ |
| 805 | const u8 *fdtdec_locate_byte_array(const void *blob, int node, |
| 806 | const char *prop_name, int count); |
Simon Glass | f909b9c | 2012-10-25 16:31:00 +0000 | [diff] [blame] | 807 | |
| 808 | /** |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 809 | * Obtain an indexed resource from a device property. |
| 810 | * |
| 811 | * @param fdt FDT blob |
| 812 | * @param node node to examine |
| 813 | * @param property name of the property to parse |
| 814 | * @param index index of the resource to retrieve |
| 815 | * @param res returns the resource |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 816 | * Return: 0 if ok, negative on error |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 817 | */ |
| 818 | int fdt_get_resource(const void *fdt, int node, const char *property, |
| 819 | unsigned int index, struct fdt_resource *res); |
| 820 | |
| 821 | /** |
| 822 | * Obtain a named resource from a device property. |
| 823 | * |
| 824 | * Look up the index of the name in a list of strings and return the resource |
| 825 | * at that index. |
| 826 | * |
| 827 | * @param fdt FDT blob |
| 828 | * @param node node to examine |
| 829 | * @param property name of the property to parse |
| 830 | * @param prop_names name of the property containing the list of names |
| 831 | * @param name the name of the entry to look up |
| 832 | * @param res returns the resource |
| 833 | */ |
| 834 | int fdt_get_named_resource(const void *fdt, int node, const char *property, |
| 835 | const char *prop_names, const char *name, |
| 836 | struct fdt_resource *res); |
| 837 | |
Simon Glass | b24e864 | 2015-04-14 21:03:21 -0600 | [diff] [blame] | 838 | /* Display timings from linux include/video/display_timing.h */ |
| 839 | enum display_flags { |
| 840 | DISPLAY_FLAGS_HSYNC_LOW = 1 << 0, |
| 841 | DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1, |
| 842 | DISPLAY_FLAGS_VSYNC_LOW = 1 << 2, |
| 843 | DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3, |
| 844 | |
| 845 | /* data enable flag */ |
| 846 | DISPLAY_FLAGS_DE_LOW = 1 << 4, |
| 847 | DISPLAY_FLAGS_DE_HIGH = 1 << 5, |
| 848 | /* drive data on pos. edge */ |
| 849 | DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6, |
| 850 | /* drive data on neg. edge */ |
| 851 | DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7, |
| 852 | DISPLAY_FLAGS_INTERLACED = 1 << 8, |
| 853 | DISPLAY_FLAGS_DOUBLESCAN = 1 << 9, |
| 854 | DISPLAY_FLAGS_DOUBLECLK = 1 << 10, |
| 855 | }; |
| 856 | |
| 857 | /* |
| 858 | * A single signal can be specified via a range of minimal and maximal values |
| 859 | * with a typical value, that lies somewhere inbetween. |
| 860 | */ |
| 861 | struct timing_entry { |
| 862 | u32 min; |
| 863 | u32 typ; |
| 864 | u32 max; |
| 865 | }; |
| 866 | |
| 867 | /* |
| 868 | * Single "mode" entry. This describes one set of signal timings a display can |
| 869 | * have in one setting. This struct can later be converted to struct videomode |
| 870 | * (see include/video/videomode.h). As each timing_entry can be defined as a |
| 871 | * range, one struct display_timing may become multiple struct videomodes. |
| 872 | * |
| 873 | * Example: hsync active high, vsync active low |
| 874 | * |
| 875 | * Active Video |
| 876 | * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________ |
| 877 | * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync.. |
| 878 | * | | porch | | porch | |
| 879 | * |
| 880 | * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯ |
| 881 | * |
| 882 | * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________ |
| 883 | */ |
| 884 | struct display_timing { |
| 885 | struct timing_entry pixelclock; |
| 886 | |
| 887 | struct timing_entry hactive; /* hor. active video */ |
| 888 | struct timing_entry hfront_porch; /* hor. front porch */ |
| 889 | struct timing_entry hback_porch; /* hor. back porch */ |
| 890 | struct timing_entry hsync_len; /* hor. sync len */ |
| 891 | |
| 892 | struct timing_entry vactive; /* ver. active video */ |
| 893 | struct timing_entry vfront_porch; /* ver. front porch */ |
| 894 | struct timing_entry vback_porch; /* ver. back porch */ |
| 895 | struct timing_entry vsync_len; /* ver. sync len */ |
| 896 | |
| 897 | enum display_flags flags; /* display flags */ |
Jernej Skrabec | 55884e5 | 2017-04-29 14:43:36 +0200 | [diff] [blame] | 898 | bool hdmi_monitor; /* is hdmi monitor? */ |
Simon Glass | b24e864 | 2015-04-14 21:03:21 -0600 | [diff] [blame] | 899 | }; |
| 900 | |
| 901 | /** |
| 902 | * fdtdec_decode_display_timing() - decode display timings |
| 903 | * |
| 904 | * Decode display timings from the supplied 'display-timings' node. |
| 905 | * See doc/device-tree-bindings/video/display-timing.txt for binding |
| 906 | * information. |
| 907 | * |
| 908 | * @param blob FDT blob |
| 909 | * @param node 'display-timing' node containing the timing subnodes |
| 910 | * @param index Index number to read (0=first timing subnode) |
| 911 | * @param config Place to put timings |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 912 | * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found |
Simon Glass | b24e864 | 2015-04-14 21:03:21 -0600 | [diff] [blame] | 913 | */ |
| 914 | int fdtdec_decode_display_timing(const void *blob, int node, int index, |
| 915 | struct display_timing *config); |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 916 | |
| 917 | /** |
Siva Durga Prasad Paladugu | b3d55ea | 2018-07-16 15:56:11 +0530 | [diff] [blame] | 918 | * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and |
| 919 | * gd->ram_start |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 920 | * |
Siva Durga Prasad Paladugu | b3d55ea | 2018-07-16 15:56:11 +0530 | [diff] [blame] | 921 | * Decode the /memory 'reg' property to determine the size and start of the |
| 922 | * first memory bank, populate the global data with the size and start of the |
| 923 | * first bank of memory. |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 924 | * |
| 925 | * This function should be called from a boards dram_init(). This helper |
Siva Durga Prasad Paladugu | b3d55ea | 2018-07-16 15:56:11 +0530 | [diff] [blame] | 926 | * function allows for boards to query the device tree for DRAM size and start |
| 927 | * address instead of hard coding the value in the case where the memory size |
| 928 | * and start address cannot be detected automatically. |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 929 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 930 | * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 931 | * invalid |
| 932 | */ |
Siva Durga Prasad Paladugu | b3d55ea | 2018-07-16 15:56:11 +0530 | [diff] [blame] | 933 | int fdtdec_setup_mem_size_base(void); |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 934 | |
| 935 | /** |
Michal Simek | 9d2cf07 | 2020-07-09 14:09:52 +0200 | [diff] [blame] | 936 | * fdtdec_setup_mem_size_base_lowest() - decode and setup gd->ram_size and |
| 937 | * gd->ram_start by lowest available memory base |
| 938 | * |
| 939 | * Decode the /memory 'reg' property to determine the lowest start of the memory |
| 940 | * bank bank and populate the global data with it. |
| 941 | * |
| 942 | * This function should be called from a boards dram_init(). This helper |
| 943 | * function allows for boards to query the device tree for DRAM size and start |
| 944 | * address instead of hard coding the value in the case where the memory size |
| 945 | * and start address cannot be detected automatically. |
| 946 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 947 | * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or |
Michal Simek | 9d2cf07 | 2020-07-09 14:09:52 +0200 | [diff] [blame] | 948 | * invalid |
| 949 | */ |
| 950 | int fdtdec_setup_mem_size_base_lowest(void); |
| 951 | |
| 952 | /** |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 953 | * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram |
| 954 | * |
| 955 | * Decode the /memory 'reg' property to determine the address and size of the |
| 956 | * memory banks. Use this data to populate the global data board info with the |
| 957 | * phys address and size of memory banks. |
| 958 | * |
| 959 | * This function should be called from a boards dram_init_banksize(). This |
| 960 | * helper function allows for boards to query the device tree for memory bank |
| 961 | * information instead of hard coding the information in cases where it cannot |
| 962 | * be detected automatically. |
| 963 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 964 | * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 965 | * invalid |
| 966 | */ |
| 967 | int fdtdec_setup_memory_banksize(void); |
| 968 | |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 969 | /** |
Thierry Reding | 1bb59a9 | 2019-04-15 11:32:13 +0200 | [diff] [blame] | 970 | * fdtdec_set_ethernet_mac_address() - set MAC address for default interface |
| 971 | * |
| 972 | * Looks up the default interface via the "ethernet" alias (in the /aliases |
| 973 | * node) and stores the given MAC in its "local-mac-address" property. This |
| 974 | * is useful on platforms that store the MAC address in a custom location. |
| 975 | * Board code can call this in the late init stage to make sure that the |
| 976 | * interface device tree node has the right MAC address configured for the |
| 977 | * Ethernet uclass to pick it up. |
| 978 | * |
| 979 | * Typically the FDT passed into this function will be U-Boot's control DTB. |
| 980 | * Given that a lot of code may be holding offsets to various nodes in that |
| 981 | * tree, this code will only set the "local-mac-address" property in-place, |
| 982 | * which means that it needs to exist and have space for the 6-byte address. |
| 983 | * This ensures that the operation is non-destructive and does not invalidate |
| 984 | * offsets that other drivers may be using. |
| 985 | * |
| 986 | * @param fdt FDT blob |
| 987 | * @param mac buffer containing the MAC address to set |
| 988 | * @param size size of MAC address |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 989 | * Return: 0 on success or a negative error code on failure |
Thierry Reding | 1bb59a9 | 2019-04-15 11:32:13 +0200 | [diff] [blame] | 990 | */ |
| 991 | int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size); |
| 992 | |
| 993 | /** |
Thierry Reding | 15956ad | 2019-03-21 19:10:01 +0100 | [diff] [blame] | 994 | * fdtdec_set_phandle() - sets the phandle of a given node |
| 995 | * |
| 996 | * @param blob FDT blob |
| 997 | * @param node offset in the FDT blob of the node whose phandle is to |
| 998 | * be set |
| 999 | * @param phandle phandle to set for the given node |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1000 | * Return: 0 on success or a negative error code on failure |
Thierry Reding | 15956ad | 2019-03-21 19:10:01 +0100 | [diff] [blame] | 1001 | */ |
Thierry Reding | a312309 | 2019-04-15 10:08:20 +0200 | [diff] [blame] | 1002 | static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle) |
| 1003 | { |
| 1004 | return fdt_setprop_u32(blob, node, "phandle", phandle); |
| 1005 | } |
Thierry Reding | 15956ad | 2019-03-21 19:10:01 +0100 | [diff] [blame] | 1006 | |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1007 | /* add "no-map" property */ |
| 1008 | #define FDTDEC_RESERVED_MEMORY_NO_MAP (1 << 0) |
| 1009 | |
Thierry Reding | 15956ad | 2019-03-21 19:10:01 +0100 | [diff] [blame] | 1010 | /** |
Thierry Reding | e8dfad4 | 2019-03-21 19:10:02 +0100 | [diff] [blame] | 1011 | * fdtdec_add_reserved_memory() - add or find a reserved-memory node |
| 1012 | * |
| 1013 | * If a reserved-memory node already exists for the given carveout, a phandle |
| 1014 | * for that node will be returned. Otherwise a new node will be created and a |
| 1015 | * phandle corresponding to it will be returned. |
| 1016 | * |
| 1017 | * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt |
| 1018 | * for details on how to use reserved memory regions. |
| 1019 | * |
| 1020 | * As an example, consider the following code snippet: |
| 1021 | * |
| 1022 | * struct fdt_memory fb = { |
| 1023 | * .start = 0x92cb3000, |
| 1024 | * .end = 0x934b2fff, |
| 1025 | * }; |
| 1026 | * uint32_t phandle; |
| 1027 | * |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1028 | * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, NULL, 0, &phandle, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1029 | * 0); |
Thierry Reding | e8dfad4 | 2019-03-21 19:10:02 +0100 | [diff] [blame] | 1030 | * |
| 1031 | * This results in the following subnode being added to the top-level |
| 1032 | * /reserved-memory node: |
| 1033 | * |
| 1034 | * reserved-memory { |
| 1035 | * #address-cells = <0x00000002>; |
| 1036 | * #size-cells = <0x00000002>; |
| 1037 | * ranges; |
| 1038 | * |
| 1039 | * framebuffer@92cb3000 { |
| 1040 | * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>; |
| 1041 | * phandle = <0x0000004d>; |
| 1042 | * }; |
| 1043 | * }; |
| 1044 | * |
| 1045 | * If the top-level /reserved-memory node does not exist, it will be created. |
| 1046 | * The phandle returned from the function call can be used to reference this |
| 1047 | * reserved memory region from other nodes. |
| 1048 | * |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1049 | * See fdtdec_set_carveout() for a more elaborate example. |
| 1050 | * |
Thierry Reding | e8dfad4 | 2019-03-21 19:10:02 +0100 | [diff] [blame] | 1051 | * @param blob FDT blob |
| 1052 | * @param basename base name of the node to create |
| 1053 | * @param carveout information about the carveout region |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1054 | * @param compatibles list of compatible strings for the carveout region |
| 1055 | * @param count number of compatible strings for the carveout region |
Thierry Reding | e8dfad4 | 2019-03-21 19:10:02 +0100 | [diff] [blame] | 1056 | * @param phandlep return location for the phandle of the carveout region |
Heiko Stuebner | 754302f | 2019-10-23 16:46:39 +0200 | [diff] [blame] | 1057 | * can be NULL if no phandle should be added |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1058 | * @param flags bitmask of flags to set for the carveout region |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1059 | * Return: 0 on success or a negative error code on failure |
Thierry Reding | e8dfad4 | 2019-03-21 19:10:02 +0100 | [diff] [blame] | 1060 | */ |
| 1061 | int fdtdec_add_reserved_memory(void *blob, const char *basename, |
| 1062 | const struct fdt_memory *carveout, |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1063 | const char **compatibles, unsigned int count, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1064 | uint32_t *phandlep, unsigned long flags); |
Thierry Reding | e8dfad4 | 2019-03-21 19:10:02 +0100 | [diff] [blame] | 1065 | |
| 1066 | /** |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1067 | * fdtdec_get_carveout() - reads a carveout from an FDT |
| 1068 | * |
| 1069 | * Reads information about a carveout region from an FDT. The carveout is a |
| 1070 | * referenced by its phandle that is read from a given property in a given |
| 1071 | * node. |
| 1072 | * |
| 1073 | * @param blob FDT blob |
| 1074 | * @param node name of a node |
Thierry Reding | 8a3c9fc | 2021-09-03 15:16:18 +0200 | [diff] [blame] | 1075 | * @param prop_name name of the property in the given node that contains |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1076 | * the phandle for the carveout |
| 1077 | * @param index index of the phandle for which to read the carveout |
| 1078 | * @param carveout return location for the carveout information |
Thierry Reding | 8a3c9fc | 2021-09-03 15:16:18 +0200 | [diff] [blame] | 1079 | * @param name return location for the carveout name |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1080 | * @param compatiblesp return location for compatible strings |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1081 | * @param countp return location for the number of compatible strings |
| 1082 | * @param flags return location for the flags of the carveout |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1083 | * Return: 0 on success or a negative error code on failure |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1084 | */ |
Thierry Reding | 8a3c9fc | 2021-09-03 15:16:18 +0200 | [diff] [blame] | 1085 | int fdtdec_get_carveout(const void *blob, const char *node, |
| 1086 | const char *prop_name, unsigned int index, |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1087 | struct fdt_memory *carveout, const char **name, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1088 | const char ***compatiblesp, unsigned int *countp, |
| 1089 | unsigned long *flags); |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1090 | |
| 1091 | /** |
| 1092 | * fdtdec_set_carveout() - sets a carveout region for a given node |
| 1093 | * |
| 1094 | * Sets a carveout region for a given node. If a reserved-memory node already |
| 1095 | * exists for the carveout, the phandle for that node will be reused. If no |
| 1096 | * such node exists, a new one will be created and a phandle to it stored in |
| 1097 | * a specified property of the given node. |
| 1098 | * |
| 1099 | * As an example, consider the following code snippet: |
| 1100 | * |
| 1101 | * const char *node = "/host1x@50000000/dc@54240000"; |
| 1102 | * struct fdt_memory fb = { |
| 1103 | * .start = 0x92cb3000, |
| 1104 | * .end = 0x934b2fff, |
| 1105 | * }; |
| 1106 | * |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1107 | * fdtdec_set_carveout(fdt, node, "memory-region", 0, "framebuffer", NULL, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1108 | * 0, &fb, 0); |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1109 | * |
| 1110 | * dc@54200000 is a display controller and was set up by the bootloader to |
| 1111 | * scan out the framebuffer specified by "fb". This would cause the following |
| 1112 | * reserved memory region to be added: |
| 1113 | * |
| 1114 | * reserved-memory { |
| 1115 | * #address-cells = <0x00000002>; |
| 1116 | * #size-cells = <0x00000002>; |
| 1117 | * ranges; |
| 1118 | * |
| 1119 | * framebuffer@92cb3000 { |
| 1120 | * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>; |
| 1121 | * phandle = <0x0000004d>; |
| 1122 | * }; |
| 1123 | * }; |
| 1124 | * |
| 1125 | * A "memory-region" property will also be added to the node referenced by the |
| 1126 | * offset parameter. |
| 1127 | * |
| 1128 | * host1x@50000000 { |
| 1129 | * ... |
| 1130 | * |
| 1131 | * dc@54240000 { |
| 1132 | * ... |
| 1133 | * memory-region = <0x0000004d>; |
| 1134 | * ... |
| 1135 | * }; |
| 1136 | * |
| 1137 | * ... |
| 1138 | * }; |
| 1139 | * |
| 1140 | * @param blob FDT blob |
| 1141 | * @param node name of the node to add the carveout to |
| 1142 | * @param prop_name name of the property in which to store the phandle of |
| 1143 | * the carveout |
| 1144 | * @param index index of the phandle to store |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1145 | * @param carveout information about the carveout to add |
Thierry Reding | d6ad807 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 1146 | * @param name base name of the reserved-memory node to create |
Thierry Reding | 5e33691 | 2021-09-03 15:16:19 +0200 | [diff] [blame] | 1147 | * @param compatibles compatible strings to set for the carveout |
| 1148 | * @param count number of compatible strings |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1149 | * @param flags bitmask of flags to set for the carveout |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1150 | * Return: 0 on success or a negative error code on failure |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1151 | */ |
| 1152 | int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name, |
Thierry Reding | d6ad807 | 2021-09-03 15:16:20 +0200 | [diff] [blame] | 1153 | unsigned int index, const struct fdt_memory *carveout, |
| 1154 | const char *name, const char **compatibles, |
Thierry Reding | bedc439 | 2021-09-03 15:16:21 +0200 | [diff] [blame] | 1155 | unsigned int count, unsigned long flags); |
Thierry Reding | e629b0d | 2019-03-21 19:10:03 +0100 | [diff] [blame] | 1156 | |
| 1157 | /** |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1158 | * Set up the device tree ready for use |
| 1159 | */ |
Simon Glass | a087767 | 2015-02-27 22:06:35 -0700 | [diff] [blame] | 1160 | int fdtdec_setup(void); |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1161 | |
Marek Vasut | edeb654 | 2020-04-11 21:18:59 +0200 | [diff] [blame] | 1162 | /** |
| 1163 | * Perform board-specific early DT adjustments |
| 1164 | */ |
| 1165 | int fdtdec_board_setup(const void *fdt_blob); |
| 1166 | |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 1167 | /** |
| 1168 | * fdtdec_resetup() - Set up the device tree again |
| 1169 | * |
| 1170 | * The main difference with fdtdec_setup() is that it returns if the fdt has |
| 1171 | * changed because a better match has been found. |
| 1172 | * This is typically used for boards that rely on a DM driver to detect the |
| 1173 | * board type. This function sould be called by the board code after the stuff |
| 1174 | * needed by board_fit_config_name_match() to operate porperly is available. |
| 1175 | * If this functions signals that a rescan is necessary, the board code must |
| 1176 | * unbind all the drivers using dm_uninit() and then rescan the DT with |
| 1177 | * dm_init_and_scan(). |
| 1178 | * |
| 1179 | * @param rescan Returns a flag indicating that fdt has changed and rescanning |
| 1180 | * the fdt is required |
| 1181 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1182 | * Return: 0 if OK, -ve on error |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 1183 | */ |
| 1184 | int fdtdec_resetup(int *rescan); |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 1185 | |
Alex Deymo | 5b661ec | 2017-04-02 01:25:20 -0700 | [diff] [blame] | 1186 | /** |
| 1187 | * Board-specific FDT initialization. Returns the address to a device tree blob. |
Simon Glass | 28b7651 | 2021-12-16 20:59:31 -0700 | [diff] [blame] | 1188 | * |
| 1189 | * Called when CONFIG_OF_BOARD is defined. |
| 1190 | * |
| 1191 | * The existing devicetree is available at gd->fdt_blob |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 1192 | * |
| 1193 | * @err internal error code if we fail to setup a DTB |
Simon Glass | 28b7651 | 2021-12-16 20:59:31 -0700 | [diff] [blame] | 1194 | * @returns new devicetree blob pointer |
Alex Deymo | 5b661ec | 2017-04-02 01:25:20 -0700 | [diff] [blame] | 1195 | */ |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 1196 | void *board_fdt_blob_setup(int *err); |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 1197 | |
| 1198 | /* |
| 1199 | * Decode the size of memory |
| 1200 | * |
| 1201 | * RAM size is normally set in a /memory node and consists of a list of |
| 1202 | * (base, size) cells in the 'reg' property. This information is used to |
| 1203 | * determine the total available memory as well as the address and size |
| 1204 | * of each bank. |
| 1205 | * |
| 1206 | * Optionally the memory configuration can vary depending on a board id, |
| 1207 | * typically read from strapping resistors or an EEPROM on the board. |
| 1208 | * |
| 1209 | * Finally, memory size can be detected (within certain limits) by probing |
| 1210 | * the available memory. It is safe to do so within the limits provides by |
| 1211 | * the board's device tree information. This makes it possible to produce |
| 1212 | * boards with different memory sizes, where the device tree specifies the |
| 1213 | * maximum memory configuration, and the smaller memory configuration is |
| 1214 | * probed. |
| 1215 | * |
| 1216 | * This function decodes that information, returning the memory base address, |
| 1217 | * size and bank information. See the memory.txt binding for full |
| 1218 | * documentation. |
| 1219 | * |
| 1220 | * @param blob Device tree blob |
| 1221 | * @param area Name of node to check (NULL means "/memory") |
| 1222 | * @param board_id Board ID to look up |
| 1223 | * @param basep Returns base address of first memory bank (NULL to |
| 1224 | * ignore) |
| 1225 | * @param sizep Returns total memory size (NULL to ignore) |
| 1226 | * @param bd Updated with the memory bank information (NULL to skip) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1227 | * Return: 0 if OK, -ve on error |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 1228 | */ |
| 1229 | int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id, |
| 1230 | phys_addr_t *basep, phys_size_t *sizep, |
| 1231 | struct bd_info *bd); |
Alex Deymo | 5b661ec | 2017-04-02 01:25:20 -0700 | [diff] [blame] | 1232 | |
Simon Glass | bed6bc7 | 2021-12-16 20:59:33 -0700 | [diff] [blame] | 1233 | /** |
| 1234 | * fdtdec_get_srcname() - Get the name of where the devicetree comes from |
| 1235 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1236 | * Return: source name |
Simon Glass | bed6bc7 | 2021-12-16 20:59:33 -0700 | [diff] [blame] | 1237 | */ |
| 1238 | const char *fdtdec_get_srcname(void); |
| 1239 | |
Simon Glass | 0a9d66f | 2012-07-12 05:25:02 +0000 | [diff] [blame] | 1240 | #endif |