blob: d9fcd037ed266f73fa8e693c09a35bf2b32dadfa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass2d2af852011-10-24 19:15:32 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass2d2af852011-10-24 19:15:32 +00004 */
5
Simon Glass0a9d66f2012-07-12 05:25:02 +00006#ifndef __fdtdec_h
7#define __fdtdec_h
Simon Glass2d2af852011-10-24 19:15:32 +00008
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 Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Bin Meng779847e2014-12-31 16:05:11 +080018#include <pci.h>
Simon Glass2d2af852011-10-24 19:15:32 +000019
20/*
Johan Jonker02e3e062023-03-13 01:33:23 +010021 * 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 Glass2d2af852011-10-24 19:15:32 +000025 * endian even on a litle endian machine.
26 */
Thierry Reding3d4ffa02019-03-21 19:10:00 +010027
Chen Guanqiao223f17d2021-04-12 14:51:11 +080028#define FDT_SIZE_T_NONE (-1U)
29
Johan Jonker02e3e062023-03-13 01:33:23 +010030#ifdef CONFIG_FDT_64BIT
31typedef u64 fdt_addr_t;
32typedef u64 fdt_size_t;
Patrice Chotardb4c52112022-01-04 08:42:48 +010033#define FDT_ADDR_T_NONE ((ulong)(-1))
34
Simon Glass2d2af852011-10-24 19:15:32 +000035#define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
Simon Glassf909b9c2012-10-25 16:31:00 +000036#define fdt_size_to_cpu(reg) be64_to_cpu(reg)
Thierry Reding570a7932019-03-21 19:09:59 +010037#define cpu_to_fdt_addr(reg) cpu_to_be64(reg)
38#define cpu_to_fdt_size(reg) cpu_to_be64(reg)
Simon Glass1b1fe412017-08-29 14:15:50 -060039typedef fdt64_t fdt_val_t;
Simon Glass2d2af852011-10-24 19:15:32 +000040#else
Johan Jonker02e3e062023-03-13 01:33:23 +010041typedef u32 fdt_addr_t;
42typedef u32 fdt_size_t;
Patrice Chotardb4c52112022-01-04 08:42:48 +010043#define FDT_ADDR_T_NONE (-1U)
44
Simon Glass2d2af852011-10-24 19:15:32 +000045#define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
Simon Glassf909b9c2012-10-25 16:31:00 +000046#define fdt_size_to_cpu(reg) be32_to_cpu(reg)
Thierry Reding570a7932019-03-21 19:09:59 +010047#define cpu_to_fdt_addr(reg) cpu_to_be32(reg)
48#define cpu_to_fdt_size(reg) cpu_to_be32(reg)
Simon Glass1b1fe412017-08-29 14:15:50 -060049typedef fdt32_t fdt_val_t;
Simon Glass2d2af852011-10-24 19:15:32 +000050#endif
51
52/* Information obtained about memory from the FDT */
53struct fdt_memory {
54 fdt_addr_t start;
55 fdt_addr_t end;
56};
57
Michael Prattfa28d142018-06-11 13:07:09 -060058struct bd_info;
59
Simon Glassbed6bc72021-12-16 20:59:33 -070060/**
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
Simon Glass4a30a572024-01-03 18:49:19 -070075 * @FDTSRC_BLOBLIST: Provided by a bloblist from an earlier phase
Simon Glassbed6bc72021-12-16 20:59:33 -070076 */
77enum fdt_source_t {
78 FDTSRC_SEPARATE,
79 FDTSRC_FIT,
80 FDTSRC_BOARD,
81 FDTSRC_EMBED,
82 FDTSRC_ENV,
Simon Glass4a30a572024-01-03 18:49:19 -070083 FDTSRC_BLOBLIST,
Simon Glassbed6bc72021-12-16 20:59:33 -070084};
85
Thierry Redinga937b802014-08-26 17:33:53 +020086/*
87 * Information about a resource. start is the first address of the resource
88 * and end is the last address (inclusive). The length of the resource will
89 * be equal to: end - start + 1.
90 */
91struct fdt_resource {
92 fdt_addr_t start;
93 fdt_addr_t end;
94};
95
Bin Meng779847e2014-12-31 16:05:11 +080096enum fdt_pci_space {
97 FDT_PCI_SPACE_CONFIG = 0,
98 FDT_PCI_SPACE_IO = 0x01000000,
99 FDT_PCI_SPACE_MEM32 = 0x02000000,
100 FDT_PCI_SPACE_MEM64 = 0x03000000,
101 FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
102 FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
103};
104
105#define FDT_PCI_ADDR_CELLS 3
106#define FDT_PCI_SIZE_CELLS 2
107#define FDT_PCI_REG_SIZE \
108 ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
109
110/*
111 * The Open Firmware spec defines PCI physical address as follows:
112 *
113 * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
114 *
115 * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
116 * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
117 * phys.lo cell: llllllll llllllll llllllll llllllll
118 *
119 * where:
120 *
121 * n: is 0 if the address is relocatable, 1 otherwise
122 * p: is 1 if addressable region is prefetchable, 0 otherwise
123 * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
124 * (for Memory), or below 64KB (for relocatable I/O)
125 * ss: is the space code, denoting the address space
126 * bbbbbbbb: is the 8-bit Bus Number
127 * ddddd: is the 5-bit Device Number
128 * fff: is the 3-bit Function Number
129 * rrrrrrrr: is the 8-bit Register Number
130 * hhhhhhhh: is a 32-bit unsigned number
131 * llllllll: is a 32-bit unsigned number
132 */
133struct fdt_pci_addr {
134 u32 phys_hi;
135 u32 phys_mid;
136 u32 phys_lo;
137};
138
Thierry Redinga937b802014-08-26 17:33:53 +0200139/**
140 * Compute the size of a resource.
141 *
142 * @param res the resource to operate on
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100143 * Return: the size of the resource
Thierry Redinga937b802014-08-26 17:33:53 +0200144 */
145static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
146{
147 return res->end - res->start + 1;
148}
149
Simon Glass2d2af852011-10-24 19:15:32 +0000150/**
151 * Compat types that we know about and for which we might have drivers.
152 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
153 * within drivers.
154 */
155enum fdt_compat_id {
156 COMPAT_UNKNOWN,
Allen Martin55d98a12012-08-31 08:30:00 +0000157 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
158 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
Jim Lin5d309e62012-07-29 20:53:29 +0000159 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
Thierry Redingf202e022014-12-09 22:25:09 -0700160 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
161 /* Tegra124 XUSB pad controller */
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700162 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
163 /* Tegra210 XUSB pad controller */
Rajeshwari Shinded6e9f442013-01-07 23:35:05 +0000164 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
Vivek Gautamf8745232013-09-14 14:02:48 +0530165 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
Akshay Saraswataa773182013-02-25 01:13:01 +0000166 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
Piotr Wilczek24e368a2014-03-07 14:59:39 +0100167 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
Jaehoon Chunge3fb6fa2014-05-16 13:59:51 +0900168 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
Simon Glass609560942013-03-11 06:08:08 +0000169 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
Ajay Kumare24c5022014-09-05 16:53:33 +0530170 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
Simon Glassf79d5382014-11-12 22:42:21 -0700171 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
Bin Meng2bfd2642015-02-05 23:42:26 +0800172 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
Marek Vasutf3f8fe22015-07-25 19:33:56 +0200173 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
Marek Vasut17497232015-07-25 10:48:14 +0200174 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
Marek Vasutf26c1ae2015-12-05 19:28:44 +0100175 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
Andrew Bradford74fdb582015-08-07 08:36:35 -0400176 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
177 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
Bin Meng5ce2d8b2015-12-11 02:55:44 -0800178 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
Ley Foon Tanb25ae722017-04-05 17:32:47 +0800179 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
180 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
181 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
182 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
183 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
184 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
185 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
186 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
Tien Fong Cheebbfa5eb2017-09-25 16:40:03 +0800187 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
188 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
Marek Vasutee4e36b2018-05-12 11:56:10 +0200189 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
Simon Glass2d2af852011-10-24 19:15:32 +0000190
191 COMPAT_COUNT,
192};
193
Simon Glassb491e9c2015-01-05 20:05:26 -0700194#define MAX_PHANDLE_ARGS 16
195struct fdtdec_phandle_args {
196 int node;
197 int args_count;
198 uint32_t args[MAX_PHANDLE_ARGS];
199};
200
201/**
202 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
203 *
204 * This function is useful to parse lists of phandles and their arguments.
205 *
206 * Example:
207 *
208 * phandle1: node1 {
209 * #list-cells = <2>;
210 * }
211 *
212 * phandle2: node2 {
213 * #list-cells = <1>;
214 * }
215 *
216 * node3 {
217 * list = <&phandle1 1 2 &phandle2 3>;
218 * }
219 *
220 * To get a device_node of the `node2' node you may call this:
221 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
222 * &args);
223 *
224 * (This function is a modified version of __of_parse_phandle_with_args() from
225 * Linux 3.18)
226 *
227 * @blob: Pointer to device tree
228 * @src_node: Offset of device tree node containing a list
229 * @list_name: property name that contains a list
230 * @cells_name: property name that specifies the phandles' arguments count,
231 * or NULL to use @cells_count
232 * @cells_count: Cell count to use if @cells_name is NULL
233 * @index: index of a phandle to parse out
234 * @out_args: optional pointer to output arguments structure (will be filled)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100235 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
Simon Glassb491e9c2015-01-05 20:05:26 -0700236 * @list_name does not exist, a phandle was not found, @cells_name
237 * could not be found, the arguments were truncated or there were too
238 * many arguments.
239 *
240 */
241int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
242 const char *list_name,
243 const char *cells_name,
244 int cell_count, int index,
245 struct fdtdec_phandle_args *out_args);
246
Sean Pauladbcfc92012-10-25 16:31:06 +0000247/**
Simon Glass2d2af852011-10-24 19:15:32 +0000248 * Find the next numbered alias for a peripheral. This is used to enumerate
249 * all the peripherals of a certain type.
250 *
251 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
252 * this function will return a pointer to the node the alias points to, and
253 * then update *upto to 1. Next time you call this function, the next node
254 * will be returned.
255 *
256 * All nodes returned will match the compatible ID, as it is assumed that
257 * all peripherals use the same driver.
258 *
259 * @param blob FDT blob to use
260 * @param name Root name of alias to search for
261 * @param id Compatible ID to look for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100262 * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
Simon Glass2d2af852011-10-24 19:15:32 +0000263 */
264int fdtdec_next_alias(const void *blob, const char *name,
265 enum fdt_compat_id id, int *upto);
266
267/**
Gerald Van Barencdac3392012-11-12 23:13:54 -0500268 * Find the compatible ID for a given node.
269 *
270 * Generally each node has at least one compatible string attached to it.
271 * This function looks through our list of known compatible strings and
272 * returns the corresponding ID which matches the compatible string.
273 *
274 * @param blob FDT blob to use
275 * @param node Node containing compatible string to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100276 * Return: compatible ID, or COMPAT_UNKNOWN if we cannot find a match
Gerald Van Barencdac3392012-11-12 23:13:54 -0500277 */
278enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
279
280/**
Simon Glass65444982012-02-27 10:52:34 +0000281 * Find the next compatible node for a peripheral.
282 *
283 * Do the first call with node = 0. This function will return a pointer to
284 * the next compatible node. Next time you call this function, pass the
285 * value returned, and the next node will be provided.
286 *
287 * @param blob FDT blob to use
288 * @param node Start node for search
289 * @param id Compatible ID to look for (enum fdt_compat_id)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100290 * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
Simon Glass65444982012-02-27 10:52:34 +0000291 */
292int fdtdec_next_compatible(const void *blob, int node,
293 enum fdt_compat_id id);
294
295/**
Simon Glass2e979b12012-04-02 13:18:42 +0000296 * Find the next compatible subnode for a peripheral.
297 *
298 * Do the first call with node set to the parent and depth = 0. This
299 * function will return the offset of the next compatible node. Next time
300 * you call this function, pass the node value returned last time, with
301 * depth unchanged, 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)
306 * @param depthp Current depth (set to 0 before first call)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100307 * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
Simon Glass2e979b12012-04-02 13:18:42 +0000308 */
309int fdtdec_next_compatible_subnode(const void *blob, int node,
310 enum fdt_compat_id id, int *depthp);
311
Stephen Warren6dfe6182015-08-06 15:31:02 -0600312/*
313 * Look up an address property in a node and return the parsed address, and
314 * optionally the parsed size.
315 *
316 * This variant assumes a known and fixed number of cells are used to
317 * represent the address and size.
318 *
319 * You probably don't want to use this function directly except to parse
320 * non-standard properties, and never to parse the "reg" property. Instead,
321 * use one of the "auto" variants below, which automatically honor the
322 * #address-cells and #size-cells properties in the parent node.
323 *
324 * @param blob FDT blob
325 * @param node node to examine
326 * @param prop_name name of property to find
327 * @param index which address to retrieve from a list of addresses. Often 0.
328 * @param na the number of cells used to represent an address
329 * @param ns the number of cells used to represent a size
330 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren7d30e102016-08-05 09:47:51 -0600331 * @param translate Indicates whether to translate the returned value
332 * using the parent node's ranges property.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100333 * Return: address, if found, or FDT_ADDR_T_NONE if not
Stephen Warren6dfe6182015-08-06 15:31:02 -0600334 */
335fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
336 const char *prop_name, int index, int na, int ns,
Stephen Warren7d30e102016-08-05 09:47:51 -0600337 fdt_size_t *sizep, bool translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600338
339/*
340 * Look up an address property in a node and return the parsed address, and
341 * optionally the parsed size.
342 *
343 * This variant automatically determines the number of cells used to represent
344 * the address and size by parsing the provided parent node's #address-cells
345 * and #size-cells properties.
346 *
347 * @param blob FDT blob
348 * @param parent parent node of @node
349 * @param node node to examine
350 * @param prop_name name of property to find
351 * @param index which address to retrieve from a list of addresses. Often 0.
352 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren7d30e102016-08-05 09:47:51 -0600353 * @param translate Indicates whether to translate the returned value
354 * using the parent node's ranges property.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100355 * Return: address, if found, or FDT_ADDR_T_NONE if not
Stephen Warren6dfe6182015-08-06 15:31:02 -0600356 */
357fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
Stephen Warren7d30e102016-08-05 09:47:51 -0600358 int node, const char *prop_name, int index, fdt_size_t *sizep,
359 bool translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600360
361/*
362 * Look up an address property in a node and return the parsed address, and
363 * optionally the parsed size.
364 *
365 * This variant automatically determines the number of cells used to represent
366 * the address and size by parsing the parent node's #address-cells
367 * and #size-cells properties. The parent node is automatically found.
368 *
369 * The automatic parent lookup implemented by this function is slow.
370 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
371 * possible.
Simon Glass2d2af852011-10-24 19:15:32 +0000372 *
373 * @param blob FDT blob
Stephen Warren6dfe6182015-08-06 15:31:02 -0600374 * @param parent parent node of @node
Simon Glass2d2af852011-10-24 19:15:32 +0000375 * @param node node to examine
376 * @param prop_name name of property to find
Stephen Warren6dfe6182015-08-06 15:31:02 -0600377 * @param index which address to retrieve from a list of addresses. Often 0.
378 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren7d30e102016-08-05 09:47:51 -0600379 * @param translate Indicates whether to translate the returned value
380 * using the parent node's ranges property.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100381 * Return: address, if found, or FDT_ADDR_T_NONE if not
Simon Glass2d2af852011-10-24 19:15:32 +0000382 */
Stephen Warren6dfe6182015-08-06 15:31:02 -0600383fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
Stephen Warren7d30e102016-08-05 09:47:51 -0600384 const char *prop_name, int index, fdt_size_t *sizep,
385 bool translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600386
387/*
388 * Look up an address property in a node and return the parsed address.
389 *
390 * This variant hard-codes the number of cells used to represent the address
391 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
392 * always returns the first address value in the property (index 0).
393 *
394 * Use of this function is not recommended due to the hard-coding of cell
395 * counts. There is no programmatic validation that these hard-coded values
396 * actually match the device tree content in any way at all. This assumption
397 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
398 * set in the U-Boot build and exercising strict control over DT content to
399 * ensure use of matching #address-cells/#size-cells properties. However, this
400 * approach is error-prone; those familiar with DT will not expect the
401 * assumption to exist, and could easily invalidate it. If the assumption is
402 * invalidated, this function will not report the issue, and debugging will
403 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
404 *
405 * @param blob FDT blob
406 * @param node node to examine
407 * @param prop_name name of property to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100408 * Return: address, if found, or FDT_ADDR_T_NONE if not
Stephen Warren6dfe6182015-08-06 15:31:02 -0600409 */
Simon Glass2d2af852011-10-24 19:15:32 +0000410fdt_addr_t fdtdec_get_addr(const void *blob, int node,
411 const char *prop_name);
412
Stephen Warren6dfe6182015-08-06 15:31:02 -0600413/*
414 * Look up an address property in a node and return the parsed address, and
415 * optionally the parsed size.
416 *
417 * This variant hard-codes the number of cells used to represent the address
418 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
419 * always returns the first address value in the property (index 0).
420 *
421 * Use of this function is not recommended due to the hard-coding of cell
422 * counts. There is no programmatic validation that these hard-coded values
423 * actually match the device tree content in any way at all. This assumption
424 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
425 * set in the U-Boot build and exercising strict control over DT content to
426 * ensure use of matching #address-cells/#size-cells properties. However, this
427 * approach is error-prone; those familiar with DT will not expect the
428 * assumption to exist, and could easily invalidate it. If the assumption is
429 * invalidated, this function will not report the issue, and debugging will
430 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
Simon Glassf4299552013-03-19 04:58:51 +0000431 *
432 * @param blob FDT blob
433 * @param node node to examine
434 * @param prop_name name of property to find
Stephen Warren6dfe6182015-08-06 15:31:02 -0600435 * @param sizep a pointer to store the size into. Use NULL if not required
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100436 * Return: address, if found, or FDT_ADDR_T_NONE if not
Simon Glassf4299552013-03-19 04:58:51 +0000437 */
438fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
439 const char *prop_name, fdt_size_t *sizep);
440
441/**
Bin Meng779847e2014-12-31 16:05:11 +0800442 * Look at the compatible property of a device node that represents a PCI
443 * device and extract pci vendor id and device id from it.
444 *
445 * @param blob FDT blob
446 * @param node node to examine
447 * @param vendor vendor id of the pci device
448 * @param device device id of the pci device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100449 * Return: 0 if ok, negative on error
Bin Meng779847e2014-12-31 16:05:11 +0800450 */
451int fdtdec_get_pci_vendev(const void *blob, int node,
452 u16 *vendor, u16 *device);
453
454/**
455 * Look at the pci address of a device node that represents a PCI device
Bin Meng779847e2014-12-31 16:05:11 +0800456 * and return base address of the pci device's registers.
457 *
Simon Glassf1037ad2015-11-29 13:17:54 -0700458 * @param dev device to examine
Bin Meng779847e2014-12-31 16:05:11 +0800459 * @param addr pci address in the form of fdt_pci_addr
460 * @param bar returns base address of the pci device's registers
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100461 * Return: 0 if ok, negative on error
Bin Meng779847e2014-12-31 16:05:11 +0800462 */
Simon Glassc92aac12020-01-27 08:49:38 -0700463int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr,
Simon Glassf1037ad2015-11-29 13:17:54 -0700464 u32 *bar);
Bin Meng779847e2014-12-31 16:05:11 +0800465
466/**
Suneel Garapati78d0bea2019-10-19 15:19:35 -0700467 * Look at the bus range property of a device node and return the pci bus
468 * range for this node.
469 * The property must hold one fdt_pci_addr with a length.
470 * @param blob FDT blob
471 * @param node node to examine
472 * @param res the resource structure to return the bus range
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100473 * Return: 0 if ok, negative on error
Suneel Garapati78d0bea2019-10-19 15:19:35 -0700474 */
475
476int fdtdec_get_pci_bus_range(const void *blob, int node,
477 struct fdt_resource *res);
478
479/**
Simon Glass2d2af852011-10-24 19:15:32 +0000480 * Look up a 32-bit integer property in a node and return it. The property
481 * must have at least 4 bytes of data. The value of the first cell is
482 * returned.
483 *
484 * @param blob FDT blob
485 * @param node node to examine
486 * @param prop_name name of property to find
487 * @param default_val default value to return if the property is not found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100488 * Return: integer value, if found, or default_val if not
Simon Glass2d2af852011-10-24 19:15:32 +0000489 */
490s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
491 s32 default_val);
492
493/**
Chin Liang Seec97b0742015-10-17 08:30:32 -0500494 * Unsigned version of fdtdec_get_int. The property must have at least
495 * 4 bytes of data. The value of the first cell is returned.
496 *
497 * @param blob FDT blob
498 * @param node node to examine
499 * @param prop_name name of property to find
500 * @param default_val default value to return if the property is not found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100501 * Return: unsigned integer value, if found, or default_val if not
Chin Liang Seec97b0742015-10-17 08:30:32 -0500502 */
503unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
504 unsigned int default_val);
505
506/**
Simon Glass9ffbe332015-03-05 12:25:14 -0700507 * Get a variable-sized number from a property
508 *
509 * This reads a number from one or more cells.
510 *
511 * @param ptr Pointer to property
512 * @param cells Number of cells containing the number
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100513 * Return: the value in the cells
Simon Glass9ffbe332015-03-05 12:25:14 -0700514 */
515u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
516
517/**
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000518 * Look up a 64-bit integer property in a node and return it. The property
519 * must have at least 8 bytes of data (2 cells). The first two cells are
520 * concatenated to form a 8 bytes value, where the first cell is top half and
521 * the second cell is bottom half.
522 *
523 * @param blob FDT blob
524 * @param node node to examine
525 * @param prop_name name of property to find
526 * @param default_val default value to return if the property is not found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100527 * Return: integer value, if found, or default_val if not
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000528 */
529uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
530 uint64_t default_val);
531
532/**
Simon Glass2d2af852011-10-24 19:15:32 +0000533 * Checks whether a node is enabled.
534 * This looks for a 'status' property. If this exists, then returns 1 if
535 * the status is 'ok' and 0 otherwise. If there is no status property,
Simon Glass65444982012-02-27 10:52:34 +0000536 * it returns 1 on the assumption that anything mentioned should be enabled
537 * by default.
Simon Glass2d2af852011-10-24 19:15:32 +0000538 *
539 * @param blob FDT blob
540 * @param node node to examine
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100541 * Return: integer value 0 (not enabled) or 1 (enabled)
Simon Glass2d2af852011-10-24 19:15:32 +0000542 */
Simon Glass65444982012-02-27 10:52:34 +0000543int fdtdec_get_is_enabled(const void *blob, int node);
Simon Glass2d2af852011-10-24 19:15:32 +0000544
545/**
Simon Glass1691f692012-03-28 10:08:24 +0000546 * Checks that we have a valid fdt available to control U-Boot.
547
548 * However, if not then for the moment nothing is done, since this function
549 * is called too early to panic().
550 *
551 * @returns 0
Simon Glass2d2af852011-10-24 19:15:32 +0000552 */
553int fdtdec_check_fdt(void);
Simon Glassb022ead2012-01-17 08:20:50 +0000554
555/**
556 * Find the nodes for a peripheral and return a list of them in the correct
557 * order. This is used to enumerate all the peripherals of a certain type.
558 *
559 * To use this, optionally set up a /aliases node with alias properties for
560 * a peripheral. For example, for usb you could have:
561 *
562 * aliases {
563 * usb0 = "/ehci@c5008000";
564 * usb1 = "/ehci@c5000000";
565 * };
566 *
567 * Pass "usb" as the name to this function and will return a list of two
568 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
569 *
570 * All nodes returned will match the compatible ID, as it is assumed that
571 * all peripherals use the same driver.
572 *
573 * If no alias node is found, then the node list will be returned in the
574 * order found in the fdt. If the aliases mention a node which doesn't
575 * exist, then this will be ignored. If nodes are found with no aliases,
576 * they will be added in any order.
577 *
578 * If there is a gap in the aliases, then this function return a 0 node at
579 * that position. The return value will also count these gaps.
580 *
581 * This function checks node properties and will not return nodes which are
582 * marked disabled (status = "disabled").
583 *
584 * @param blob FDT blob to use
585 * @param name Root name of alias to search for
586 * @param id Compatible ID to look for
587 * @param node_list Place to put list of found nodes
588 * @param maxcount Maximum number of nodes to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100589 * Return: number of nodes found on success, FDT_ERR_... on error
Simon Glassb022ead2012-01-17 08:20:50 +0000590 */
591int fdtdec_find_aliases_for_id(const void *blob, const char *name,
592 enum fdt_compat_id id, int *node_list, int maxcount);
593
594/*
Simon Glass1f772662012-02-03 15:13:53 +0000595 * This function is similar to fdtdec_find_aliases_for_id() except that it
596 * adds to the node_list that is passed in. Any 0 elements are considered
597 * available for allocation - others are considered already used and are
598 * skipped.
599 *
600 * You can use this by calling fdtdec_find_aliases_for_id() with an
601 * uninitialised array, then setting the elements that are returned to -1,
602 * say, then calling this function, perhaps with a different compat id.
603 * Any elements you get back that are >0 are new nodes added by the call
604 * to this function.
605 *
606 * Note that if you have some nodes with aliases and some without, you are
607 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
608 * one compat_id may fill in positions for which you have aliases defined
609 * for another compat_id. When you later call *this* function with the second
610 * compat_id, the alias positions may already be used. A debug warning may
611 * be generated in this case, but it is safest to define aliases for all
612 * nodes when you care about the ordering.
613 */
614int fdtdec_add_aliases_for_id(const void *blob, const char *name,
615 enum fdt_compat_id id, int *node_list, int maxcount);
616
Simon Glass9908ad42014-07-23 06:55:09 -0600617/**
618 * Get the alias sequence number of a node
619 *
620 * This works out whether a node is pointed to by an alias, and if so, the
621 * sequence number of that alias. Aliases are of the form <base><num> where
622 * <num> is the sequence number. For example spi2 would be sequence number
623 * 2.
624 *
625 * @param blob Device tree blob (if NULL, then error is returned)
626 * @param base Base name for alias (before the underscore)
627 * @param node Node to look up
628 * @param seqp This is set to the sequence number if one is found,
629 * but otherwise the value is left alone
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100630 * Return: 0 if a sequence was found, -ve if not
Simon Glass9908ad42014-07-23 06:55:09 -0600631 */
632int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
633 int *seqp);
634
Simon Glassc95f6782014-07-23 06:55:16 -0600635/**
Michal Simek4b586672019-01-31 16:30:58 +0100636 * Get the highest alias number for susbystem.
637 *
638 * It parses all aliases and find out highest recorded alias for subsystem.
639 * Aliases are of the form <base><num> where <num> is the sequence number.
640 *
641 * @param blob Device tree blob (if NULL, then error is returned)
642 * @param base Base name for alias susbystem (before the number)
643 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100644 * Return: 0 highest alias ID, -1 if not found
Michal Simek4b586672019-01-31 16:30:58 +0100645 */
646int fdtdec_get_alias_highest_id(const void *blob, const char *base);
647
648/**
Simon Glass088dccb2015-10-17 19:41:14 -0600649 * Get a property from the /chosen node
650 *
651 * @param blob Device tree blob (if NULL, then NULL is returned)
652 * @param name Property name to look up
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100653 * Return: Value of property, or NULL if it does not exist
Simon Glass088dccb2015-10-17 19:41:14 -0600654 */
655const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
656
657/**
658 * Get the offset of the given /chosen node
Simon Glass1e374fb2014-09-04 16:27:24 -0600659 *
660 * This looks up a property in /chosen containing the path to another node,
661 * then finds the offset of that node.
662 *
663 * @param blob Device tree blob (if NULL, then error is returned)
664 * @param name Property name, e.g. "stdout-path"
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100665 * Return: Node offset referred to by that chosen node, or -ve FDT_ERR_...
Simon Glass1e374fb2014-09-04 16:27:24 -0600666 */
667int fdtdec_get_chosen_node(const void *blob, const char *name);
668
Simon Glass1f772662012-02-03 15:13:53 +0000669/*
Simon Glassb022ead2012-01-17 08:20:50 +0000670 * Get the name for a compatible ID
671 *
672 * @param id Compatible ID to look for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100673 * Return: compatible string for that id
Simon Glassb022ead2012-01-17 08:20:50 +0000674 */
675const char *fdtdec_get_compatible(enum fdt_compat_id id);
Simon Glassc4697362012-02-27 10:52:35 +0000676
677/* Look up a phandle and follow it to its node. Then return the offset
678 * of that node.
679 *
680 * @param blob FDT blob
681 * @param node node to examine
682 * @param prop_name name of property to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100683 * Return: node offset if found, -ve error code on error
Simon Glassc4697362012-02-27 10:52:35 +0000684 */
685int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
686
687/**
688 * Look up a property in a node and return its contents in an integer
689 * array of given length. The property must have at least enough data for
690 * the array (4*count bytes). It may have more, but this will be ignored.
691 *
692 * @param blob FDT blob
693 * @param node node to examine
694 * @param prop_name name of property to find
695 * @param array array to fill with data
696 * @param count number of array elements
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100697 * Return: 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
Simon Glassc4697362012-02-27 10:52:35 +0000698 * or -FDT_ERR_BADLAYOUT if not enough data
699 */
700int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
701 u32 *array, int count);
702
703/**
Simon Glasseda0c3f2014-11-10 18:00:19 -0700704 * Look up a property in a node and return its contents in an integer
705 * array of given length. The property must exist but may have less data that
706 * expected (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 Schuchardt47b4c022022-01-19 18:05:50 +0100713 * Return: number of array elements if ok, or -FDT_ERR_NOTFOUND if the
Simon Glasseda0c3f2014-11-10 18:00:19 -0700714 * property is not found
715 */
716int fdtdec_get_int_array_count(const void *blob, int node,
717 const char *prop_name, u32 *array, int count);
718
719/**
Simon Glass744ec232012-04-02 13:18:41 +0000720 * Look up a property in a node and return a pointer to its contents as a
721 * unsigned int array of given length. The property must have at least enough
722 * data for the array ('count' cells). It may have more, but this will be
723 * ignored. The data is not copied.
724 *
725 * Note that you must access elements of the array with fdt32_to_cpu(),
726 * since the elements will be big endian even on a little endian machine.
727 *
728 * @param blob FDT blob
729 * @param node node to examine
730 * @param prop_name name of property to find
731 * @param count number of array elements
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100732 * Return: pointer to array if found, or NULL if the property is not
Simon Glass744ec232012-04-02 13:18:41 +0000733 * found or there is not enough data
734 */
735const u32 *fdtdec_locate_array(const void *blob, int node,
736 const char *prop_name, int count);
737
738/**
Simon Glassc4697362012-02-27 10:52:35 +0000739 * Look up a boolean property in a node and return it.
740 *
741 * A boolean properly is true if present in the device tree and false if not
742 * present, regardless of its value.
743 *
744 * @param blob FDT blob
745 * @param node node to examine
746 * @param prop_name name of property to find
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100747 * Return: 1 if the properly is present; 0 if it isn't present
Simon Glassc4697362012-02-27 10:52:35 +0000748 */
749int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
Simon Glass0d83c4d2012-02-27 10:52:36 +0000750
Peng Fan23dd3a02016-02-01 13:31:15 +0800751/*
752 * Count child nodes of one parent node.
753 *
754 * @param blob FDT blob
755 * @param node parent node
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100756 * Return: number of child node; 0 if there is not child node
Peng Fan23dd3a02016-02-01 13:31:15 +0800757 */
758int fdtdec_get_child_count(const void *blob, int node);
759
Anton Stafff2305ec2012-04-17 09:01:28 +0000760/*
761 * Look up a property in a node and return its contents in a byte
762 * array of given length. The property must have at least enough data for
763 * the array (count bytes). It may have more, but this will be ignored.
764 *
765 * @param blob FDT blob
766 * @param node node to examine
767 * @param prop_name name of property to find
768 * @param array array to fill with data
769 * @param count number of array elements
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100770 * Return: 0 if ok, or -FDT_ERR_MISSING if the property is not found,
Anton Stafff2305ec2012-04-17 09:01:28 +0000771 * or -FDT_ERR_BADLAYOUT if not enough data
772 */
773int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
774 u8 *array, int count);
775
776/**
777 * Look up a property in a node and return a pointer to its contents as a
778 * byte array of given length. The property must have at least enough data
779 * for the array (count bytes). It may have more, but this will be ignored.
780 * The data is not copied.
781 *
782 * @param blob FDT blob
783 * @param node node to examine
784 * @param prop_name name of property to find
785 * @param count number of array elements
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100786 * Return: pointer to byte array if found, or NULL if the property is not
Anton Stafff2305ec2012-04-17 09:01:28 +0000787 * found or there is not enough data
788 */
789const u8 *fdtdec_locate_byte_array(const void *blob, int node,
790 const char *prop_name, int count);
Simon Glassf909b9c2012-10-25 16:31:00 +0000791
792/**
Thierry Redinga937b802014-08-26 17:33:53 +0200793 * Obtain an indexed resource from a device property.
794 *
795 * @param fdt FDT blob
796 * @param node node to examine
797 * @param property name of the property to parse
798 * @param index index of the resource to retrieve
799 * @param res returns the resource
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100800 * Return: 0 if ok, negative on error
Thierry Redinga937b802014-08-26 17:33:53 +0200801 */
802int fdt_get_resource(const void *fdt, int node, const char *property,
803 unsigned int index, struct fdt_resource *res);
804
805/**
806 * Obtain a named resource from a device property.
807 *
808 * Look up the index of the name in a list of strings and return the resource
809 * at that index.
810 *
811 * @param fdt FDT blob
812 * @param node node to examine
813 * @param property name of the property to parse
814 * @param prop_names name of the property containing the list of names
815 * @param name the name of the entry to look up
816 * @param res returns the resource
817 */
818int fdt_get_named_resource(const void *fdt, int node, const char *property,
819 const char *prop_names, const char *name,
820 struct fdt_resource *res);
821
Simon Glassb24e8642015-04-14 21:03:21 -0600822/* Display timings from linux include/video/display_timing.h */
823enum display_flags {
824 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
825 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
826 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
827 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
828
829 /* data enable flag */
830 DISPLAY_FLAGS_DE_LOW = 1 << 4,
831 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
832 /* drive data on pos. edge */
833 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
834 /* drive data on neg. edge */
835 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
836 DISPLAY_FLAGS_INTERLACED = 1 << 8,
837 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
838 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
839};
840
841/*
842 * A single signal can be specified via a range of minimal and maximal values
843 * with a typical value, that lies somewhere inbetween.
844 */
845struct timing_entry {
846 u32 min;
847 u32 typ;
848 u32 max;
849};
850
851/*
852 * Single "mode" entry. This describes one set of signal timings a display can
853 * have in one setting. This struct can later be converted to struct videomode
854 * (see include/video/videomode.h). As each timing_entry can be defined as a
855 * range, one struct display_timing may become multiple struct videomodes.
856 *
857 * Example: hsync active high, vsync active low
858 *
859 * Active Video
860 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
861 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
862 * | | porch | | porch |
863 *
864 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
865 *
866 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
867 */
868struct display_timing {
869 struct timing_entry pixelclock;
870
871 struct timing_entry hactive; /* hor. active video */
872 struct timing_entry hfront_porch; /* hor. front porch */
873 struct timing_entry hback_porch; /* hor. back porch */
874 struct timing_entry hsync_len; /* hor. sync len */
875
876 struct timing_entry vactive; /* ver. active video */
877 struct timing_entry vfront_porch; /* ver. front porch */
878 struct timing_entry vback_porch; /* ver. back porch */
879 struct timing_entry vsync_len; /* ver. sync len */
880
881 enum display_flags flags; /* display flags */
Jernej Skrabec55884e52017-04-29 14:43:36 +0200882 bool hdmi_monitor; /* is hdmi monitor? */
Simon Glassb24e8642015-04-14 21:03:21 -0600883};
884
885/**
886 * fdtdec_decode_display_timing() - decode display timings
887 *
888 * Decode display timings from the supplied 'display-timings' node.
889 * See doc/device-tree-bindings/video/display-timing.txt for binding
890 * information.
891 *
892 * @param blob FDT blob
893 * @param node 'display-timing' node containing the timing subnodes
894 * @param index Index number to read (0=first timing subnode)
895 * @param config Place to put timings
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100896 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glassb24e8642015-04-14 21:03:21 -0600897 */
898int fdtdec_decode_display_timing(const void *blob, int node, int index,
899 struct display_timing *config);
Nathan Rossiba97b722016-12-19 00:03:34 +1000900
901/**
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530902 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
903 * gd->ram_start
Nathan Rossiba97b722016-12-19 00:03:34 +1000904 *
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530905 * Decode the /memory 'reg' property to determine the size and start of the
906 * first memory bank, populate the global data with the size and start of the
907 * first bank of memory.
Nathan Rossiba97b722016-12-19 00:03:34 +1000908 *
909 * This function should be called from a boards dram_init(). This helper
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530910 * function allows for boards to query the device tree for DRAM size and start
911 * address instead of hard coding the value in the case where the memory size
912 * and start address cannot be detected automatically.
Nathan Rossiba97b722016-12-19 00:03:34 +1000913 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100914 * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
Nathan Rossiba97b722016-12-19 00:03:34 +1000915 * invalid
916 */
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530917int fdtdec_setup_mem_size_base(void);
Nathan Rossiba97b722016-12-19 00:03:34 +1000918
919/**
Michal Simek9d2cf072020-07-09 14:09:52 +0200920 * fdtdec_setup_mem_size_base_lowest() - decode and setup gd->ram_size and
921 * gd->ram_start by lowest available memory base
922 *
923 * Decode the /memory 'reg' property to determine the lowest start of the memory
924 * bank bank and populate the global data with it.
925 *
926 * This function should be called from a boards dram_init(). This helper
927 * function allows for boards to query the device tree for DRAM size and start
928 * address instead of hard coding the value in the case where the memory size
929 * and start address cannot be detected automatically.
930 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100931 * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
Michal Simek9d2cf072020-07-09 14:09:52 +0200932 * invalid
933 */
934int fdtdec_setup_mem_size_base_lowest(void);
935
936/**
Nathan Rossiba97b722016-12-19 00:03:34 +1000937 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
938 *
939 * Decode the /memory 'reg' property to determine the address and size of the
940 * memory banks. Use this data to populate the global data board info with the
941 * phys address and size of memory banks.
942 *
943 * This function should be called from a boards dram_init_banksize(). This
944 * helper function allows for boards to query the device tree for memory bank
945 * information instead of hard coding the information in cases where it cannot
946 * be detected automatically.
947 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100948 * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
Nathan Rossiba97b722016-12-19 00:03:34 +1000949 * invalid
950 */
951int fdtdec_setup_memory_banksize(void);
952
Simon Glass26b78b22015-02-27 22:06:34 -0700953/**
Thierry Reding1bb59a92019-04-15 11:32:13 +0200954 * fdtdec_set_ethernet_mac_address() - set MAC address for default interface
955 *
956 * Looks up the default interface via the "ethernet" alias (in the /aliases
957 * node) and stores the given MAC in its "local-mac-address" property. This
958 * is useful on platforms that store the MAC address in a custom location.
959 * Board code can call this in the late init stage to make sure that the
960 * interface device tree node has the right MAC address configured for the
961 * Ethernet uclass to pick it up.
962 *
963 * Typically the FDT passed into this function will be U-Boot's control DTB.
964 * Given that a lot of code may be holding offsets to various nodes in that
965 * tree, this code will only set the "local-mac-address" property in-place,
966 * which means that it needs to exist and have space for the 6-byte address.
967 * This ensures that the operation is non-destructive and does not invalidate
968 * offsets that other drivers may be using.
969 *
970 * @param fdt FDT blob
971 * @param mac buffer containing the MAC address to set
972 * @param size size of MAC address
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100973 * Return: 0 on success or a negative error code on failure
Thierry Reding1bb59a92019-04-15 11:32:13 +0200974 */
975int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size);
976
977/**
Thierry Reding15956ad2019-03-21 19:10:01 +0100978 * fdtdec_set_phandle() - sets the phandle of a given node
979 *
980 * @param blob FDT blob
981 * @param node offset in the FDT blob of the node whose phandle is to
982 * be set
983 * @param phandle phandle to set for the given node
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100984 * Return: 0 on success or a negative error code on failure
Thierry Reding15956ad2019-03-21 19:10:01 +0100985 */
Thierry Redinga3123092019-04-15 10:08:20 +0200986static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
987{
988 return fdt_setprop_u32(blob, node, "phandle", phandle);
989}
Thierry Reding15956ad2019-03-21 19:10:01 +0100990
Thierry Redingbedc4392021-09-03 15:16:21 +0200991/* add "no-map" property */
992#define FDTDEC_RESERVED_MEMORY_NO_MAP (1 << 0)
993
Thierry Reding15956ad2019-03-21 19:10:01 +0100994/**
Thierry Redinge8dfad42019-03-21 19:10:02 +0100995 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
996 *
997 * If a reserved-memory node already exists for the given carveout, a phandle
998 * for that node will be returned. Otherwise a new node will be created and a
999 * phandle corresponding to it will be returned.
1000 *
1001 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1002 * for details on how to use reserved memory regions.
1003 *
1004 * As an example, consider the following code snippet:
1005 *
1006 * struct fdt_memory fb = {
1007 * .start = 0x92cb3000,
1008 * .end = 0x934b2fff,
1009 * };
1010 * uint32_t phandle;
1011 *
Thierry Reding5e336912021-09-03 15:16:19 +02001012 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, NULL, 0, &phandle,
Thierry Redingbedc4392021-09-03 15:16:21 +02001013 * 0);
Thierry Redinge8dfad42019-03-21 19:10:02 +01001014 *
1015 * This results in the following subnode being added to the top-level
1016 * /reserved-memory node:
1017 *
1018 * reserved-memory {
1019 * #address-cells = <0x00000002>;
1020 * #size-cells = <0x00000002>;
1021 * ranges;
1022 *
1023 * framebuffer@92cb3000 {
1024 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1025 * phandle = <0x0000004d>;
1026 * };
1027 * };
1028 *
1029 * If the top-level /reserved-memory node does not exist, it will be created.
1030 * The phandle returned from the function call can be used to reference this
1031 * reserved memory region from other nodes.
1032 *
Thierry Redinge629b0d2019-03-21 19:10:03 +01001033 * See fdtdec_set_carveout() for a more elaborate example.
1034 *
Thierry Redinge8dfad42019-03-21 19:10:02 +01001035 * @param blob FDT blob
1036 * @param basename base name of the node to create
1037 * @param carveout information about the carveout region
Thierry Reding5e336912021-09-03 15:16:19 +02001038 * @param compatibles list of compatible strings for the carveout region
1039 * @param count number of compatible strings for the carveout region
Thierry Redinge8dfad42019-03-21 19:10:02 +01001040 * @param phandlep return location for the phandle of the carveout region
Heiko Stuebner754302f2019-10-23 16:46:39 +02001041 * can be NULL if no phandle should be added
Thierry Redingbedc4392021-09-03 15:16:21 +02001042 * @param flags bitmask of flags to set for the carveout region
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001043 * Return: 0 on success or a negative error code on failure
Thierry Redinge8dfad42019-03-21 19:10:02 +01001044 */
1045int fdtdec_add_reserved_memory(void *blob, const char *basename,
1046 const struct fdt_memory *carveout,
Thierry Reding5e336912021-09-03 15:16:19 +02001047 const char **compatibles, unsigned int count,
Thierry Redingbedc4392021-09-03 15:16:21 +02001048 uint32_t *phandlep, unsigned long flags);
Thierry Redinge8dfad42019-03-21 19:10:02 +01001049
1050/**
Thierry Redinge629b0d2019-03-21 19:10:03 +01001051 * fdtdec_get_carveout() - reads a carveout from an FDT
1052 *
1053 * Reads information about a carveout region from an FDT. The carveout is a
1054 * referenced by its phandle that is read from a given property in a given
1055 * node.
1056 *
1057 * @param blob FDT blob
1058 * @param node name of a node
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001059 * @param prop_name name of the property in the given node that contains
Thierry Redinge629b0d2019-03-21 19:10:03 +01001060 * the phandle for the carveout
1061 * @param index index of the phandle for which to read the carveout
1062 * @param carveout return location for the carveout information
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001063 * @param name return location for the carveout name
Thierry Reding5e336912021-09-03 15:16:19 +02001064 * @param compatiblesp return location for compatible strings
Thierry Redingbedc4392021-09-03 15:16:21 +02001065 * @param countp return location for the number of compatible strings
1066 * @param flags return location for the flags of the carveout
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001067 * Return: 0 on success or a negative error code on failure
Thierry Redinge629b0d2019-03-21 19:10:03 +01001068 */
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001069int fdtdec_get_carveout(const void *blob, const char *node,
1070 const char *prop_name, unsigned int index,
Thierry Reding5e336912021-09-03 15:16:19 +02001071 struct fdt_memory *carveout, const char **name,
Thierry Redingbedc4392021-09-03 15:16:21 +02001072 const char ***compatiblesp, unsigned int *countp,
1073 unsigned long *flags);
Thierry Redinge629b0d2019-03-21 19:10:03 +01001074
1075/**
1076 * fdtdec_set_carveout() - sets a carveout region for a given node
1077 *
1078 * Sets a carveout region for a given node. If a reserved-memory node already
1079 * exists for the carveout, the phandle for that node will be reused. If no
1080 * such node exists, a new one will be created and a phandle to it stored in
1081 * a specified property of the given node.
1082 *
1083 * As an example, consider the following code snippet:
1084 *
1085 * const char *node = "/host1x@50000000/dc@54240000";
1086 * struct fdt_memory fb = {
1087 * .start = 0x92cb3000,
1088 * .end = 0x934b2fff,
1089 * };
1090 *
Thierry Reding5e336912021-09-03 15:16:19 +02001091 * fdtdec_set_carveout(fdt, node, "memory-region", 0, "framebuffer", NULL,
Thierry Redingbedc4392021-09-03 15:16:21 +02001092 * 0, &fb, 0);
Thierry Redinge629b0d2019-03-21 19:10:03 +01001093 *
1094 * dc@54200000 is a display controller and was set up by the bootloader to
1095 * scan out the framebuffer specified by "fb". This would cause the following
1096 * reserved memory region to be added:
1097 *
1098 * reserved-memory {
1099 * #address-cells = <0x00000002>;
1100 * #size-cells = <0x00000002>;
1101 * ranges;
1102 *
1103 * framebuffer@92cb3000 {
1104 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1105 * phandle = <0x0000004d>;
1106 * };
1107 * };
1108 *
1109 * A "memory-region" property will also be added to the node referenced by the
1110 * offset parameter.
1111 *
1112 * host1x@50000000 {
1113 * ...
1114 *
1115 * dc@54240000 {
1116 * ...
1117 * memory-region = <0x0000004d>;
1118 * ...
1119 * };
1120 *
1121 * ...
1122 * };
1123 *
1124 * @param blob FDT blob
1125 * @param node name of the node to add the carveout to
1126 * @param prop_name name of the property in which to store the phandle of
1127 * the carveout
1128 * @param index index of the phandle to store
Thierry Redinge629b0d2019-03-21 19:10:03 +01001129 * @param carveout information about the carveout to add
Thierry Redingd6ad8072021-09-03 15:16:20 +02001130 * @param name base name of the reserved-memory node to create
Thierry Reding5e336912021-09-03 15:16:19 +02001131 * @param compatibles compatible strings to set for the carveout
1132 * @param count number of compatible strings
Thierry Redingbedc4392021-09-03 15:16:21 +02001133 * @param flags bitmask of flags to set for the carveout
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001134 * Return: 0 on success or a negative error code on failure
Thierry Redinge629b0d2019-03-21 19:10:03 +01001135 */
1136int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
Thierry Redingd6ad8072021-09-03 15:16:20 +02001137 unsigned int index, const struct fdt_memory *carveout,
1138 const char *name, const char **compatibles,
Thierry Redingbedc4392021-09-03 15:16:21 +02001139 unsigned int count, unsigned long flags);
Thierry Redinge629b0d2019-03-21 19:10:03 +01001140
1141/**
Evgeny Bachininc37ec642024-12-11 01:39:57 +03001142 * fdtdec_setup_embed - pick up embedded DTS
1143 *
1144 * Should be invoked under CONFIG_OF_EMBED guard.
1145 */
1146void fdtdec_setup_embed(void);
1147
1148/**
Simon Glass26b78b22015-02-27 22:06:34 -07001149 * Set up the device tree ready for use
1150 */
Simon Glassa0877672015-02-27 22:06:35 -07001151int fdtdec_setup(void);
Simon Glass26b78b22015-02-27 22:06:34 -07001152
Marek Vasutedeb6542020-04-11 21:18:59 +02001153/**
1154 * Perform board-specific early DT adjustments
1155 */
1156int fdtdec_board_setup(const void *fdt_blob);
1157
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001158/**
1159 * fdtdec_resetup() - Set up the device tree again
1160 *
1161 * The main difference with fdtdec_setup() is that it returns if the fdt has
1162 * changed because a better match has been found.
1163 * This is typically used for boards that rely on a DM driver to detect the
1164 * board type. This function sould be called by the board code after the stuff
1165 * needed by board_fit_config_name_match() to operate porperly is available.
1166 * If this functions signals that a rescan is necessary, the board code must
1167 * unbind all the drivers using dm_uninit() and then rescan the DT with
1168 * dm_init_and_scan().
1169 *
1170 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1171 * the fdt is required
1172 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001173 * Return: 0 if OK, -ve on error
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001174 */
1175int fdtdec_resetup(int *rescan);
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001176
Alex Deymo5b661ec2017-04-02 01:25:20 -07001177/**
1178 * Board-specific FDT initialization. Returns the address to a device tree blob.
Simon Glass28b76512021-12-16 20:59:31 -07001179 *
1180 * Called when CONFIG_OF_BOARD is defined.
1181 *
1182 * The existing devicetree is available at gd->fdt_blob
Ilias Apalodimasab5348a2021-10-26 09:12:33 +03001183 *
Simon Glass94086b22024-11-02 11:49:42 -06001184 * @fdtp: Existing devicetree blob pointer; update this and return 0 if a
1185 * different devicetree should be used
1186 * Return: 0 on success, -EEXIST if the existing FDT is OK, -ve error code if we
1187 * fail to setup a DTB
Alex Deymo5b661ec2017-04-02 01:25:20 -07001188 */
Simon Glass94086b22024-11-02 11:49:42 -06001189int board_fdt_blob_setup(void **fdtp);
Michael Prattfa28d142018-06-11 13:07:09 -06001190
1191/*
1192 * Decode the size of memory
1193 *
1194 * RAM size is normally set in a /memory node and consists of a list of
1195 * (base, size) cells in the 'reg' property. This information is used to
1196 * determine the total available memory as well as the address and size
1197 * of each bank.
1198 *
1199 * Optionally the memory configuration can vary depending on a board id,
1200 * typically read from strapping resistors or an EEPROM on the board.
1201 *
1202 * Finally, memory size can be detected (within certain limits) by probing
1203 * the available memory. It is safe to do so within the limits provides by
1204 * the board's device tree information. This makes it possible to produce
1205 * boards with different memory sizes, where the device tree specifies the
1206 * maximum memory configuration, and the smaller memory configuration is
1207 * probed.
1208 *
1209 * This function decodes that information, returning the memory base address,
1210 * size and bank information. See the memory.txt binding for full
1211 * documentation.
1212 *
1213 * @param blob Device tree blob
1214 * @param area Name of node to check (NULL means "/memory")
1215 * @param board_id Board ID to look up
1216 * @param basep Returns base address of first memory bank (NULL to
1217 * ignore)
1218 * @param sizep Returns total memory size (NULL to ignore)
1219 * @param bd Updated with the memory bank information (NULL to skip)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001220 * Return: 0 if OK, -ve on error
Michael Prattfa28d142018-06-11 13:07:09 -06001221 */
1222int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1223 phys_addr_t *basep, phys_size_t *sizep,
1224 struct bd_info *bd);
Alex Deymo5b661ec2017-04-02 01:25:20 -07001225
Simon Glassbed6bc72021-12-16 20:59:33 -07001226/**
1227 * fdtdec_get_srcname() - Get the name of where the devicetree comes from
1228 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001229 * Return: source name
Simon Glassbed6bc72021-12-16 20:59:33 -07001230 */
1231const char *fdtdec_get_srcname(void);
1232
Simon Glass0a9d66f2012-07-12 05:25:02 +00001233#endif