blob: 9bd6a70587a67ebb474e3003c919869a3115d604 [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/*
21 * A typedef for a physical address. Note that fdt data is always big
22 * endian even on a litle endian machine.
23 */
York Sun56893722015-08-03 12:02:04 -070024typedef phys_addr_t fdt_addr_t;
25typedef phys_size_t fdt_size_t;
Thierry Reding3d4ffa02019-03-21 19:10:00 +010026
27static inline fdt32_t fdt_addr_unpack(fdt_addr_t addr, fdt32_t *upper)
28{
29 if (upper)
30#ifdef CONFIG_PHYS_64BIT
31 *upper = addr >> 32;
32#else
33 *upper = 0;
34#endif
35
36 return addr;
37}
38
39static inline fdt32_t fdt_size_unpack(fdt_size_t size, fdt32_t *upper)
40{
41 if (upper)
42#ifdef CONFIG_PHYS_64BIT
43 *upper = size >> 32;
44#else
45 *upper = 0;
46#endif
47
48 return size;
49}
50
Simon Glass2d2af852011-10-24 19:15:32 +000051#ifdef CONFIG_PHYS_64BIT
Mario Six61efece2018-02-12 08:05:57 +010052#define FDT_ADDR_T_NONE (-1U)
Simon Glass2d2af852011-10-24 19:15:32 +000053#define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
Simon Glassf909b9c2012-10-25 16:31:00 +000054#define fdt_size_to_cpu(reg) be64_to_cpu(reg)
Thierry Reding570a7932019-03-21 19:09:59 +010055#define cpu_to_fdt_addr(reg) cpu_to_be64(reg)
56#define cpu_to_fdt_size(reg) cpu_to_be64(reg)
Simon Glass1b1fe412017-08-29 14:15:50 -060057typedef fdt64_t fdt_val_t;
Simon Glass2d2af852011-10-24 19:15:32 +000058#else
Simon Glass2d2af852011-10-24 19:15:32 +000059#define FDT_ADDR_T_NONE (-1U)
60#define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
Simon Glassf909b9c2012-10-25 16:31:00 +000061#define fdt_size_to_cpu(reg) be32_to_cpu(reg)
Thierry Reding570a7932019-03-21 19:09:59 +010062#define cpu_to_fdt_addr(reg) cpu_to_be32(reg)
63#define cpu_to_fdt_size(reg) cpu_to_be32(reg)
Simon Glass1b1fe412017-08-29 14:15:50 -060064typedef fdt32_t fdt_val_t;
Simon Glass2d2af852011-10-24 19:15:32 +000065#endif
66
67/* Information obtained about memory from the FDT */
68struct fdt_memory {
69 fdt_addr_t start;
70 fdt_addr_t end;
71};
72
Michael Prattfa28d142018-06-11 13:07:09 -060073struct bd_info;
74
Simon Glass8579a512015-03-06 13:19:03 -070075#ifdef CONFIG_SPL_BUILD
76#define SPL_BUILD 1
77#else
78#define SPL_BUILD 0
79#endif
80
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -040081#if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
82extern phys_addr_t prior_stage_fdt_address;
83#endif
84
Thierry Redinga937b802014-08-26 17:33:53 +020085/*
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 */
90struct fdt_resource {
91 fdt_addr_t start;
92 fdt_addr_t end;
93};
94
Bin Meng779847e2014-12-31 16:05:11 +080095enum 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 */
132struct fdt_pci_addr {
133 u32 phys_hi;
134 u32 phys_mid;
135 u32 phys_lo;
136};
137
Thierry Redinga937b802014-08-26 17:33:53 +0200138/**
139 * Compute the size of a resource.
140 *
141 * @param res the resource to operate on
142 * @return the size of the resource
143 */
144static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
145{
146 return res->end - res->start + 1;
147}
148
Simon Glass2d2af852011-10-24 19:15:32 +0000149/**
150 * Compat types that we know about and for which we might have drivers.
151 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
152 * within drivers.
153 */
154enum fdt_compat_id {
155 COMPAT_UNKNOWN,
Allen Martin55d98a12012-08-31 08:30:00 +0000156 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
157 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
Jim Lin5d309e62012-07-29 20:53:29 +0000158 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
Thierry Redingf202e022014-12-09 22:25:09 -0700159 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
160 /* Tegra124 XUSB pad controller */
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700161 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
162 /* Tegra210 XUSB pad controller */
Hatim RVbe5ad5a2012-12-11 00:52:46 +0000163 COMPAT_SMSC_LAN9215, /* SMSC 10/100 Ethernet LAN9215 */
164 COMPAT_SAMSUNG_EXYNOS5_SROMC, /* Exynos5 SROMC */
Rajeshwari Shinded6e9f442013-01-07 23:35:05 +0000165 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
Vivek Gautamf8745232013-09-14 14:02:48 +0530166 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
Akshay Saraswataa773182013-02-25 01:13:01 +0000167 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
Piotr Wilczek24e368a2014-03-07 14:59:39 +0100168 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
Jaehoon Chunge3fb6fa2014-05-16 13:59:51 +0900169 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
Simon Glass609560942013-03-11 06:08:08 +0000170 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
Ajay Kumare24c5022014-09-05 16:53:33 +0530171 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
Simon Glassf79d5382014-11-12 22:42:21 -0700172 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
Bin Meng2bfd2642015-02-05 23:42:26 +0800173 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
Marek Vasutf3f8fe22015-07-25 19:33:56 +0200174 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
Marek Vasut17497232015-07-25 10:48:14 +0200175 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
Marek Vasutf26c1ae2015-12-05 19:28:44 +0100176 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
Andrew Bradford74fdb582015-08-07 08:36:35 -0400177 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
178 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
Bin Meng5ce2d8b2015-12-11 02:55:44 -0800179 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
Boris Brezillon57f20382016-06-15 21:09:23 +0200180 COMPAT_SUNXI_NAND, /* SUNXI NAND controller */
Ley Foon Tanb25ae722017-04-05 17:32:47 +0800181 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
182 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
183 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
184 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
185 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
186 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
187 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
188 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
Tien Fong Cheebbfa5eb2017-09-25 16:40:03 +0800189 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
190 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
Marek Vasutee4e36b2018-05-12 11:56:10 +0200191 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
Simon Glass2d2af852011-10-24 19:15:32 +0000192
193 COMPAT_COUNT,
194};
195
Simon Glassb491e9c2015-01-05 20:05:26 -0700196#define MAX_PHANDLE_ARGS 16
197struct fdtdec_phandle_args {
198 int node;
199 int args_count;
200 uint32_t args[MAX_PHANDLE_ARGS];
201};
202
203/**
204 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
205 *
206 * This function is useful to parse lists of phandles and their arguments.
207 *
208 * Example:
209 *
210 * phandle1: node1 {
211 * #list-cells = <2>;
212 * }
213 *
214 * phandle2: node2 {
215 * #list-cells = <1>;
216 * }
217 *
218 * node3 {
219 * list = <&phandle1 1 2 &phandle2 3>;
220 * }
221 *
222 * To get a device_node of the `node2' node you may call this:
223 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
224 * &args);
225 *
226 * (This function is a modified version of __of_parse_phandle_with_args() from
227 * Linux 3.18)
228 *
229 * @blob: Pointer to device tree
230 * @src_node: Offset of device tree node containing a list
231 * @list_name: property name that contains a list
232 * @cells_name: property name that specifies the phandles' arguments count,
233 * or NULL to use @cells_count
234 * @cells_count: Cell count to use if @cells_name is NULL
235 * @index: index of a phandle to parse out
236 * @out_args: optional pointer to output arguments structure (will be filled)
237 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
238 * @list_name does not exist, a phandle was not found, @cells_name
239 * could not be found, the arguments were truncated or there were too
240 * many arguments.
241 *
242 */
243int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
244 const char *list_name,
245 const char *cells_name,
246 int cell_count, int index,
247 struct fdtdec_phandle_args *out_args);
248
Sean Pauladbcfc92012-10-25 16:31:06 +0000249/**
Simon Glass2d2af852011-10-24 19:15:32 +0000250 * Find the next numbered alias for a peripheral. This is used to enumerate
251 * all the peripherals of a certain type.
252 *
253 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
254 * this function will return a pointer to the node the alias points to, and
255 * then update *upto to 1. Next time you call this function, the next node
256 * will be returned.
257 *
258 * All nodes returned will match the compatible ID, as it is assumed that
259 * all peripherals use the same driver.
260 *
261 * @param blob FDT blob to use
262 * @param name Root name of alias to search for
263 * @param id Compatible ID to look for
264 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
265 */
266int fdtdec_next_alias(const void *blob, const char *name,
267 enum fdt_compat_id id, int *upto);
268
269/**
Gerald Van Barencdac3392012-11-12 23:13:54 -0500270 * Find the compatible ID for a given node.
271 *
272 * Generally each node has at least one compatible string attached to it.
273 * This function looks through our list of known compatible strings and
274 * returns the corresponding ID which matches the compatible string.
275 *
276 * @param blob FDT blob to use
277 * @param node Node containing compatible string to find
278 * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
279 */
280enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
281
282/**
Simon Glass65444982012-02-27 10:52:34 +0000283 * Find the next compatible node for a peripheral.
284 *
285 * Do the first call with node = 0. This function will return a pointer to
286 * the next compatible node. Next time you call this function, pass the
287 * value returned, and the next node will be provided.
288 *
289 * @param blob FDT blob to use
290 * @param node Start node for search
291 * @param id Compatible ID to look for (enum fdt_compat_id)
292 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
293 */
294int fdtdec_next_compatible(const void *blob, int node,
295 enum fdt_compat_id id);
296
297/**
Simon Glass2e979b12012-04-02 13:18:42 +0000298 * Find the next compatible subnode for a peripheral.
299 *
300 * Do the first call with node set to the parent and depth = 0. This
301 * function will return the offset of the next compatible node. Next time
302 * you call this function, pass the node value returned last time, with
303 * depth unchanged, and the next node will be provided.
304 *
305 * @param blob FDT blob to use
306 * @param node Start node for search
307 * @param id Compatible ID to look for (enum fdt_compat_id)
308 * @param depthp Current depth (set to 0 before first call)
309 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
310 */
311int fdtdec_next_compatible_subnode(const void *blob, int node,
312 enum fdt_compat_id id, int *depthp);
313
Stephen Warren6dfe6182015-08-06 15:31:02 -0600314/*
315 * Look up an address property in a node and return the parsed address, and
316 * optionally the parsed size.
317 *
318 * This variant assumes a known and fixed number of cells are used to
319 * represent the address and size.
320 *
321 * You probably don't want to use this function directly except to parse
322 * non-standard properties, and never to parse the "reg" property. Instead,
323 * use one of the "auto" variants below, which automatically honor the
324 * #address-cells and #size-cells properties in the parent node.
325 *
326 * @param blob FDT blob
327 * @param node node to examine
328 * @param prop_name name of property to find
329 * @param index which address to retrieve from a list of addresses. Often 0.
330 * @param na the number of cells used to represent an address
331 * @param ns the number of cells used to represent a size
332 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren7d30e102016-08-05 09:47:51 -0600333 * @param translate Indicates whether to translate the returned value
334 * using the parent node's ranges property.
Stephen Warren6dfe6182015-08-06 15:31:02 -0600335 * @return address, if found, or FDT_ADDR_T_NONE if not
336 */
337fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
338 const char *prop_name, int index, int na, int ns,
Stephen Warren7d30e102016-08-05 09:47:51 -0600339 fdt_size_t *sizep, bool translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600340
341/*
342 * Look up an address property in a node and return the parsed address, and
343 * optionally the parsed size.
344 *
345 * This variant automatically determines the number of cells used to represent
346 * the address and size by parsing the provided parent node's #address-cells
347 * and #size-cells properties.
348 *
349 * @param blob FDT blob
350 * @param parent parent node of @node
351 * @param node node to examine
352 * @param prop_name name of property to find
353 * @param index which address to retrieve from a list of addresses. Often 0.
354 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren7d30e102016-08-05 09:47:51 -0600355 * @param translate Indicates whether to translate the returned value
356 * using the parent node's ranges property.
Stephen Warren6dfe6182015-08-06 15:31:02 -0600357 * @return address, if found, or FDT_ADDR_T_NONE if not
358 */
359fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
Stephen Warren7d30e102016-08-05 09:47:51 -0600360 int node, const char *prop_name, int index, fdt_size_t *sizep,
361 bool translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600362
363/*
364 * Look up an address property in a node and return the parsed address, and
365 * optionally the parsed size.
366 *
367 * This variant automatically determines the number of cells used to represent
368 * the address and size by parsing the parent node's #address-cells
369 * and #size-cells properties. The parent node is automatically found.
370 *
371 * The automatic parent lookup implemented by this function is slow.
372 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
373 * possible.
Simon Glass2d2af852011-10-24 19:15:32 +0000374 *
375 * @param blob FDT blob
Stephen Warren6dfe6182015-08-06 15:31:02 -0600376 * @param parent parent node of @node
Simon Glass2d2af852011-10-24 19:15:32 +0000377 * @param node node to examine
378 * @param prop_name name of property to find
Stephen Warren6dfe6182015-08-06 15:31:02 -0600379 * @param index which address to retrieve from a list of addresses. Often 0.
380 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren7d30e102016-08-05 09:47:51 -0600381 * @param translate Indicates whether to translate the returned value
382 * using the parent node's ranges property.
Simon Glass2d2af852011-10-24 19:15:32 +0000383 * @return address, if found, or FDT_ADDR_T_NONE if not
384 */
Stephen Warren6dfe6182015-08-06 15:31:02 -0600385fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
Stephen Warren7d30e102016-08-05 09:47:51 -0600386 const char *prop_name, int index, fdt_size_t *sizep,
387 bool translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600388
389/*
390 * Look up an address property in a node and return the parsed address.
391 *
392 * This variant hard-codes the number of cells used to represent the address
393 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
394 * always returns the first address value in the property (index 0).
395 *
396 * Use of this function is not recommended due to the hard-coding of cell
397 * counts. There is no programmatic validation that these hard-coded values
398 * actually match the device tree content in any way at all. This assumption
399 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
400 * set in the U-Boot build and exercising strict control over DT content to
401 * ensure use of matching #address-cells/#size-cells properties. However, this
402 * approach is error-prone; those familiar with DT will not expect the
403 * assumption to exist, and could easily invalidate it. If the assumption is
404 * invalidated, this function will not report the issue, and debugging will
405 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
406 *
407 * @param blob FDT blob
408 * @param node node to examine
409 * @param prop_name name of property to find
410 * @return address, if found, or FDT_ADDR_T_NONE if not
411 */
Simon Glass2d2af852011-10-24 19:15:32 +0000412fdt_addr_t fdtdec_get_addr(const void *blob, int node,
413 const char *prop_name);
414
Stephen Warren6dfe6182015-08-06 15:31:02 -0600415/*
416 * Look up an address property in a node and return the parsed address, and
417 * optionally the parsed size.
418 *
419 * This variant hard-codes the number of cells used to represent the address
420 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
421 * always returns the first address value in the property (index 0).
422 *
423 * Use of this function is not recommended due to the hard-coding of cell
424 * counts. There is no programmatic validation that these hard-coded values
425 * actually match the device tree content in any way at all. This assumption
426 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
427 * set in the U-Boot build and exercising strict control over DT content to
428 * ensure use of matching #address-cells/#size-cells properties. However, this
429 * approach is error-prone; those familiar with DT will not expect the
430 * assumption to exist, and could easily invalidate it. If the assumption is
431 * invalidated, this function will not report the issue, and debugging will
432 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
Simon Glassf4299552013-03-19 04:58:51 +0000433 *
434 * @param blob FDT blob
435 * @param node node to examine
436 * @param prop_name name of property to find
Stephen Warren6dfe6182015-08-06 15:31:02 -0600437 * @param sizep a pointer to store the size into. Use NULL if not required
Simon Glassf4299552013-03-19 04:58:51 +0000438 * @return address, if found, or FDT_ADDR_T_NONE if not
439 */
440fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
441 const char *prop_name, fdt_size_t *sizep);
442
443/**
Bin Meng779847e2014-12-31 16:05:11 +0800444 * Look at an address property in a node and return the pci address which
445 * corresponds to the given type in the form of fdt_pci_addr.
446 * The property must hold one fdt_pci_addr with a lengh.
447 *
448 * @param blob FDT blob
449 * @param node node to examine
450 * @param type pci address type (FDT_PCI_SPACE_xxx)
451 * @param prop_name name of property to find
452 * @param addr returns pci address in the form of fdt_pci_addr
Simon Glass2eb77f12015-03-05 12:25:19 -0700453 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
454 * format of the property was invalid, -ENXIO if the requested
455 * address type was not found
Bin Meng779847e2014-12-31 16:05:11 +0800456 */
457int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
458 const char *prop_name, struct fdt_pci_addr *addr);
459
460/**
461 * Look at the compatible property of a device node that represents a PCI
462 * device and extract pci vendor id and device id from it.
463 *
464 * @param blob FDT blob
465 * @param node node to examine
466 * @param vendor vendor id of the pci device
467 * @param device device id of the pci device
468 * @return 0 if ok, negative on error
469 */
470int fdtdec_get_pci_vendev(const void *blob, int node,
471 u16 *vendor, u16 *device);
472
473/**
474 * Look at the pci address of a device node that represents a PCI device
Bin Meng779847e2014-12-31 16:05:11 +0800475 * and return base address of the pci device's registers.
476 *
Simon Glassf1037ad2015-11-29 13:17:54 -0700477 * @param dev device to examine
Bin Meng779847e2014-12-31 16:05:11 +0800478 * @param addr pci address in the form of fdt_pci_addr
479 * @param bar returns base address of the pci device's registers
480 * @return 0 if ok, negative on error
481 */
Simon Glassf1037ad2015-11-29 13:17:54 -0700482int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
483 u32 *bar);
Bin Meng779847e2014-12-31 16:05:11 +0800484
485/**
Simon Glass2d2af852011-10-24 19:15:32 +0000486 * Look up a 32-bit integer property in a node and return it. The property
487 * must have at least 4 bytes of data. The value of the first cell is
488 * returned.
489 *
490 * @param blob FDT blob
491 * @param node node to examine
492 * @param prop_name name of property to find
493 * @param default_val default value to return if the property is not found
494 * @return integer value, if found, or default_val if not
495 */
496s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
497 s32 default_val);
498
499/**
Chin Liang Seec97b0742015-10-17 08:30:32 -0500500 * Unsigned version of fdtdec_get_int. The property must have at least
501 * 4 bytes of data. The value of the first cell is returned.
502 *
503 * @param blob FDT blob
504 * @param node node to examine
505 * @param prop_name name of property to find
506 * @param default_val default value to return if the property is not found
507 * @return unsigned integer value, if found, or default_val if not
508 */
509unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
510 unsigned int default_val);
511
512/**
Simon Glass9ffbe332015-03-05 12:25:14 -0700513 * Get a variable-sized number from a property
514 *
515 * This reads a number from one or more cells.
516 *
517 * @param ptr Pointer to property
518 * @param cells Number of cells containing the number
519 * @return the value in the cells
520 */
521u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
522
523/**
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000524 * Look up a 64-bit integer property in a node and return it. The property
525 * must have at least 8 bytes of data (2 cells). The first two cells are
526 * concatenated to form a 8 bytes value, where the first cell is top half and
527 * the second cell is bottom half.
528 *
529 * @param blob FDT blob
530 * @param node node to examine
531 * @param prop_name name of property to find
532 * @param default_val default value to return if the property is not found
533 * @return integer value, if found, or default_val if not
534 */
535uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
536 uint64_t default_val);
537
538/**
Simon Glass2d2af852011-10-24 19:15:32 +0000539 * Checks whether a node is enabled.
540 * This looks for a 'status' property. If this exists, then returns 1 if
541 * the status is 'ok' and 0 otherwise. If there is no status property,
Simon Glass65444982012-02-27 10:52:34 +0000542 * it returns 1 on the assumption that anything mentioned should be enabled
543 * by default.
Simon Glass2d2af852011-10-24 19:15:32 +0000544 *
545 * @param blob FDT blob
546 * @param node node to examine
Simon Glass65444982012-02-27 10:52:34 +0000547 * @return integer value 0 (not enabled) or 1 (enabled)
Simon Glass2d2af852011-10-24 19:15:32 +0000548 */
Simon Glass65444982012-02-27 10:52:34 +0000549int fdtdec_get_is_enabled(const void *blob, int node);
Simon Glass2d2af852011-10-24 19:15:32 +0000550
551/**
Simon Glass1691f692012-03-28 10:08:24 +0000552 * Make sure we have a valid fdt available to control U-Boot.
553 *
554 * If not, a message is printed to the console if the console is ready.
555 *
556 * @return 0 if all ok, -1 if not
557 */
558int fdtdec_prepare_fdt(void);
559
560/**
561 * Checks that we have a valid fdt available to control U-Boot.
562
563 * However, if not then for the moment nothing is done, since this function
564 * is called too early to panic().
565 *
566 * @returns 0
Simon Glass2d2af852011-10-24 19:15:32 +0000567 */
568int fdtdec_check_fdt(void);
Simon Glassb022ead2012-01-17 08:20:50 +0000569
570/**
571 * Find the nodes for a peripheral and return a list of them in the correct
572 * order. This is used to enumerate all the peripherals of a certain type.
573 *
574 * To use this, optionally set up a /aliases node with alias properties for
575 * a peripheral. For example, for usb you could have:
576 *
577 * aliases {
578 * usb0 = "/ehci@c5008000";
579 * usb1 = "/ehci@c5000000";
580 * };
581 *
582 * Pass "usb" as the name to this function and will return a list of two
583 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
584 *
585 * All nodes returned will match the compatible ID, as it is assumed that
586 * all peripherals use the same driver.
587 *
588 * If no alias node is found, then the node list will be returned in the
589 * order found in the fdt. If the aliases mention a node which doesn't
590 * exist, then this will be ignored. If nodes are found with no aliases,
591 * they will be added in any order.
592 *
593 * If there is a gap in the aliases, then this function return a 0 node at
594 * that position. The return value will also count these gaps.
595 *
596 * This function checks node properties and will not return nodes which are
597 * marked disabled (status = "disabled").
598 *
599 * @param blob FDT blob to use
600 * @param name Root name of alias to search for
601 * @param id Compatible ID to look for
602 * @param node_list Place to put list of found nodes
603 * @param maxcount Maximum number of nodes to find
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400604 * @return number of nodes found on success, FDT_ERR_... on error
Simon Glassb022ead2012-01-17 08:20:50 +0000605 */
606int fdtdec_find_aliases_for_id(const void *blob, const char *name,
607 enum fdt_compat_id id, int *node_list, int maxcount);
608
609/*
Simon Glass1f772662012-02-03 15:13:53 +0000610 * This function is similar to fdtdec_find_aliases_for_id() except that it
611 * adds to the node_list that is passed in. Any 0 elements are considered
612 * available for allocation - others are considered already used and are
613 * skipped.
614 *
615 * You can use this by calling fdtdec_find_aliases_for_id() with an
616 * uninitialised array, then setting the elements that are returned to -1,
617 * say, then calling this function, perhaps with a different compat id.
618 * Any elements you get back that are >0 are new nodes added by the call
619 * to this function.
620 *
621 * Note that if you have some nodes with aliases and some without, you are
622 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
623 * one compat_id may fill in positions for which you have aliases defined
624 * for another compat_id. When you later call *this* function with the second
625 * compat_id, the alias positions may already be used. A debug warning may
626 * be generated in this case, but it is safest to define aliases for all
627 * nodes when you care about the ordering.
628 */
629int fdtdec_add_aliases_for_id(const void *blob, const char *name,
630 enum fdt_compat_id id, int *node_list, int maxcount);
631
Simon Glass9908ad42014-07-23 06:55:09 -0600632/**
633 * Get the alias sequence number of a node
634 *
635 * This works out whether a node is pointed to by an alias, and if so, the
636 * sequence number of that alias. Aliases are of the form <base><num> where
637 * <num> is the sequence number. For example spi2 would be sequence number
638 * 2.
639 *
640 * @param blob Device tree blob (if NULL, then error is returned)
641 * @param base Base name for alias (before the underscore)
642 * @param node Node to look up
643 * @param seqp This is set to the sequence number if one is found,
644 * but otherwise the value is left alone
645 * @return 0 if a sequence was found, -ve if not
646 */
647int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
648 int *seqp);
649
Simon Glassc95f6782014-07-23 06:55:16 -0600650/**
Michal Simek4b586672019-01-31 16:30:58 +0100651 * Get the highest alias number for susbystem.
652 *
653 * It parses all aliases and find out highest recorded alias for subsystem.
654 * Aliases are of the form <base><num> where <num> is the sequence number.
655 *
656 * @param blob Device tree blob (if NULL, then error is returned)
657 * @param base Base name for alias susbystem (before the number)
658 *
659 * @return 0 highest alias ID, -1 if not found
660 */
661int fdtdec_get_alias_highest_id(const void *blob, const char *base);
662
663/**
Simon Glass088dccb2015-10-17 19:41:14 -0600664 * Get a property from the /chosen node
665 *
666 * @param blob Device tree blob (if NULL, then NULL is returned)
667 * @param name Property name to look up
668 * @return Value of property, or NULL if it does not exist
669 */
670const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
671
672/**
673 * Get the offset of the given /chosen node
Simon Glass1e374fb2014-09-04 16:27:24 -0600674 *
675 * This looks up a property in /chosen containing the path to another node,
676 * then finds the offset of that node.
677 *
678 * @param blob Device tree blob (if NULL, then error is returned)
679 * @param name Property name, e.g. "stdout-path"
680 * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
681 */
682int fdtdec_get_chosen_node(const void *blob, const char *name);
683
Simon Glass1f772662012-02-03 15:13:53 +0000684/*
Simon Glassb022ead2012-01-17 08:20:50 +0000685 * Get the name for a compatible ID
686 *
687 * @param id Compatible ID to look for
688 * @return compatible string for that id
689 */
690const char *fdtdec_get_compatible(enum fdt_compat_id id);
Simon Glassc4697362012-02-27 10:52:35 +0000691
692/* Look up a phandle and follow it to its node. Then return the offset
693 * of that node.
694 *
695 * @param blob FDT blob
696 * @param node node to examine
697 * @param prop_name name of property to find
698 * @return node offset if found, -ve error code on error
699 */
700int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
701
702/**
703 * Look up a property in a node and return its contents in an integer
704 * array of given length. The property must have at least enough data for
705 * the array (4*count bytes). It may have more, but this will be ignored.
706 *
707 * @param blob FDT blob
708 * @param node node to examine
709 * @param prop_name name of property to find
710 * @param array array to fill with data
711 * @param count number of array elements
712 * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
713 * or -FDT_ERR_BADLAYOUT if not enough data
714 */
715int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
716 u32 *array, int count);
717
718/**
Simon Glasseda0c3f2014-11-10 18:00:19 -0700719 * Look up a property in a node and return its contents in an integer
720 * array of given length. The property must exist but may have less data that
721 * expected (4*count bytes). It may have more, but this will be ignored.
722 *
723 * @param blob FDT blob
724 * @param node node to examine
725 * @param prop_name name of property to find
726 * @param array array to fill with data
727 * @param count number of array elements
728 * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
729 * property is not found
730 */
731int fdtdec_get_int_array_count(const void *blob, int node,
732 const char *prop_name, u32 *array, int count);
733
734/**
Simon Glass744ec232012-04-02 13:18:41 +0000735 * Look up a property in a node and return a pointer to its contents as a
736 * unsigned int array of given length. The property must have at least enough
737 * data for the array ('count' cells). It may have more, but this will be
738 * ignored. The data is not copied.
739 *
740 * Note that you must access elements of the array with fdt32_to_cpu(),
741 * since the elements will be big endian even on a little endian machine.
742 *
743 * @param blob FDT blob
744 * @param node node to examine
745 * @param prop_name name of property to find
746 * @param count number of array elements
747 * @return pointer to array if found, or NULL if the property is not
748 * found or there is not enough data
749 */
750const u32 *fdtdec_locate_array(const void *blob, int node,
751 const char *prop_name, int count);
752
753/**
Simon Glassc4697362012-02-27 10:52:35 +0000754 * Look up a boolean property in a node and return it.
755 *
756 * A boolean properly is true if present in the device tree and false if not
757 * present, regardless of its value.
758 *
759 * @param blob FDT blob
760 * @param node node to examine
761 * @param prop_name name of property to find
762 * @return 1 if the properly is present; 0 if it isn't present
763 */
764int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
Simon Glass0d83c4d2012-02-27 10:52:36 +0000765
Peng Fan23dd3a02016-02-01 13:31:15 +0800766/*
767 * Count child nodes of one parent node.
768 *
769 * @param blob FDT blob
770 * @param node parent node
771 * @return number of child node; 0 if there is not child node
772 */
773int fdtdec_get_child_count(const void *blob, int node);
774
Simon Glass0d83c4d2012-02-27 10:52:36 +0000775/**
Abhilash Kesavan67217af2012-10-25 16:30:58 +0000776 * Look in the FDT for a config item with the given name and return its value
777 * as a 32-bit integer. The property must have at least 4 bytes of data. The
778 * value of the first cell is returned.
779 *
780 * @param blob FDT blob to use
781 * @param prop_name Node property name
782 * @param default_val default value to return if the property is not found
783 * @return integer value, if found, or default_val if not
784 */
785int fdtdec_get_config_int(const void *blob, const char *prop_name,
786 int default_val);
787
Simon Glassb0494472012-10-25 16:30:59 +0000788/**
Gabe Black3e1c1512012-10-25 16:31:04 +0000789 * Look in the FDT for a config item with the given name
790 * and return whether it exists.
791 *
792 * @param blob FDT blob
793 * @param prop_name property name to look up
794 * @return 1, if it exists, or 0 if not
795 */
796int fdtdec_get_config_bool(const void *blob, const char *prop_name);
797
798/**
Simon Glassb0494472012-10-25 16:30:59 +0000799 * Look in the FDT for a config item with the given name and return its value
800 * as a string.
801 *
802 * @param blob FDT blob
803 * @param prop_name property name to look up
804 * @returns property string, NULL on error.
805 */
806char *fdtdec_get_config_string(const void *blob, const char *prop_name);
807
Anton Stafff2305ec2012-04-17 09:01:28 +0000808/*
809 * Look up a property in a node and return its contents in a byte
810 * array of given length. The property must have at least enough data for
811 * the array (count bytes). It may have more, but this will be ignored.
812 *
813 * @param blob FDT blob
814 * @param node node to examine
815 * @param prop_name name of property to find
816 * @param array array to fill with data
817 * @param count number of array elements
818 * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
819 * or -FDT_ERR_BADLAYOUT if not enough data
820 */
821int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
822 u8 *array, int count);
823
824/**
825 * Look up a property in a node and return a pointer to its contents as a
826 * byte array of given length. The property must have at least enough data
827 * for the array (count bytes). It may have more, but this will be ignored.
828 * The data is not copied.
829 *
830 * @param blob FDT blob
831 * @param node node to examine
832 * @param prop_name name of property to find
833 * @param count number of array elements
834 * @return pointer to byte array if found, or NULL if the property is not
835 * found or there is not enough data
836 */
837const u8 *fdtdec_locate_byte_array(const void *blob, int node,
838 const char *prop_name, int count);
Simon Glassf909b9c2012-10-25 16:31:00 +0000839
840/**
Thierry Redinga937b802014-08-26 17:33:53 +0200841 * Obtain an indexed resource from a device property.
842 *
843 * @param fdt FDT blob
844 * @param node node to examine
845 * @param property name of the property to parse
846 * @param index index of the resource to retrieve
847 * @param res returns the resource
848 * @return 0 if ok, negative on error
849 */
850int fdt_get_resource(const void *fdt, int node, const char *property,
851 unsigned int index, struct fdt_resource *res);
852
853/**
854 * Obtain a named resource from a device property.
855 *
856 * Look up the index of the name in a list of strings and return the resource
857 * at that index.
858 *
859 * @param fdt FDT blob
860 * @param node node to examine
861 * @param property name of the property to parse
862 * @param prop_names name of the property containing the list of names
863 * @param name the name of the entry to look up
864 * @param res returns the resource
865 */
866int fdt_get_named_resource(const void *fdt, int node, const char *property,
867 const char *prop_names, const char *name,
868 struct fdt_resource *res);
869
Simon Glassb24e8642015-04-14 21:03:21 -0600870/* Display timings from linux include/video/display_timing.h */
871enum display_flags {
872 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
873 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
874 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
875 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
876
877 /* data enable flag */
878 DISPLAY_FLAGS_DE_LOW = 1 << 4,
879 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
880 /* drive data on pos. edge */
881 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
882 /* drive data on neg. edge */
883 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
884 DISPLAY_FLAGS_INTERLACED = 1 << 8,
885 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
886 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
887};
888
889/*
890 * A single signal can be specified via a range of minimal and maximal values
891 * with a typical value, that lies somewhere inbetween.
892 */
893struct timing_entry {
894 u32 min;
895 u32 typ;
896 u32 max;
897};
898
899/*
900 * Single "mode" entry. This describes one set of signal timings a display can
901 * have in one setting. This struct can later be converted to struct videomode
902 * (see include/video/videomode.h). As each timing_entry can be defined as a
903 * range, one struct display_timing may become multiple struct videomodes.
904 *
905 * Example: hsync active high, vsync active low
906 *
907 * Active Video
908 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
909 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
910 * | | porch | | porch |
911 *
912 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
913 *
914 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
915 */
916struct display_timing {
917 struct timing_entry pixelclock;
918
919 struct timing_entry hactive; /* hor. active video */
920 struct timing_entry hfront_porch; /* hor. front porch */
921 struct timing_entry hback_porch; /* hor. back porch */
922 struct timing_entry hsync_len; /* hor. sync len */
923
924 struct timing_entry vactive; /* ver. active video */
925 struct timing_entry vfront_porch; /* ver. front porch */
926 struct timing_entry vback_porch; /* ver. back porch */
927 struct timing_entry vsync_len; /* ver. sync len */
928
929 enum display_flags flags; /* display flags */
Jernej Skrabec55884e52017-04-29 14:43:36 +0200930 bool hdmi_monitor; /* is hdmi monitor? */
Simon Glassb24e8642015-04-14 21:03:21 -0600931};
932
933/**
934 * fdtdec_decode_display_timing() - decode display timings
935 *
936 * Decode display timings from the supplied 'display-timings' node.
937 * See doc/device-tree-bindings/video/display-timing.txt for binding
938 * information.
939 *
940 * @param blob FDT blob
941 * @param node 'display-timing' node containing the timing subnodes
942 * @param index Index number to read (0=first timing subnode)
943 * @param config Place to put timings
944 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
945 */
946int fdtdec_decode_display_timing(const void *blob, int node, int index,
947 struct display_timing *config);
Nathan Rossiba97b722016-12-19 00:03:34 +1000948
949/**
Marek Vasutfa8df032019-03-05 04:25:54 +0100950 * fdtdec_setup_mem_size_base_fdt() - decode and setup gd->ram_size and
951 * gd->ram_start
952 *
953 * Decode the /memory 'reg' property to determine the size and start of the
954 * first memory bank, populate the global data with the size and start of the
955 * first bank of memory.
956 *
957 * This function should be called from a boards dram_init(). This helper
958 * function allows for boards to query the device tree for DRAM size and start
959 * address instead of hard coding the value in the case where the memory size
960 * and start address cannot be detected automatically.
961 *
962 * @param blob FDT blob
963 *
964 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
965 * invalid
966 */
967int fdtdec_setup_mem_size_base_fdt(const void *blob);
968
969/**
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530970 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
971 * gd->ram_start
Nathan Rossiba97b722016-12-19 00:03:34 +1000972 *
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530973 * Decode the /memory 'reg' property to determine the size and start of the
974 * first memory bank, populate the global data with the size and start of the
975 * first bank of memory.
Nathan Rossiba97b722016-12-19 00:03:34 +1000976 *
977 * This function should be called from a boards dram_init(). This helper
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530978 * function allows for boards to query the device tree for DRAM size and start
979 * address instead of hard coding the value in the case where the memory size
980 * and start address cannot be detected automatically.
Nathan Rossiba97b722016-12-19 00:03:34 +1000981 *
982 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
983 * invalid
984 */
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530985int fdtdec_setup_mem_size_base(void);
Nathan Rossiba97b722016-12-19 00:03:34 +1000986
987/**
Marek Vasut7c748422019-03-05 04:25:55 +0100988 * fdtdec_setup_memory_banksize_fdt() - decode and populate gd->bd->bi_dram
989 *
990 * Decode the /memory 'reg' property to determine the address and size of the
991 * memory banks. Use this data to populate the global data board info with the
992 * phys address and size of memory banks.
993 *
994 * This function should be called from a boards dram_init_banksize(). This
995 * helper function allows for boards to query the device tree for memory bank
996 * information instead of hard coding the information in cases where it cannot
997 * be detected automatically.
998 *
999 * @param blob FDT blob
1000 *
1001 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
1002 * invalid
1003 */
1004int fdtdec_setup_memory_banksize_fdt(const void *blob);
1005
1006/**
Nathan Rossiba97b722016-12-19 00:03:34 +10001007 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
1008 *
1009 * Decode the /memory 'reg' property to determine the address and size of the
1010 * memory banks. Use this data to populate the global data board info with the
1011 * phys address and size of memory banks.
1012 *
1013 * This function should be called from a boards dram_init_banksize(). This
1014 * helper function allows for boards to query the device tree for memory bank
1015 * information instead of hard coding the information in cases where it cannot
1016 * be detected automatically.
1017 *
1018 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
1019 * invalid
1020 */
1021int fdtdec_setup_memory_banksize(void);
1022
Simon Glass26b78b22015-02-27 22:06:34 -07001023/**
Thierry Reding15956ad2019-03-21 19:10:01 +01001024 * fdtdec_set_phandle() - sets the phandle of a given node
1025 *
1026 * @param blob FDT blob
1027 * @param node offset in the FDT blob of the node whose phandle is to
1028 * be set
1029 * @param phandle phandle to set for the given node
1030 * @return 0 on success or a negative error code on failure
1031 */
1032int fdtdec_set_phandle(void *blob, int node, uint32_t phandle);
1033
1034/**
Thierry Redinge8dfad42019-03-21 19:10:02 +01001035 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
1036 *
1037 * If a reserved-memory node already exists for the given carveout, a phandle
1038 * for that node will be returned. Otherwise a new node will be created and a
1039 * phandle corresponding to it will be returned.
1040 *
1041 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1042 * for details on how to use reserved memory regions.
1043 *
1044 * As an example, consider the following code snippet:
1045 *
1046 * struct fdt_memory fb = {
1047 * .start = 0x92cb3000,
1048 * .end = 0x934b2fff,
1049 * };
1050 * uint32_t phandle;
1051 *
1052 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, &phandle);
1053 *
1054 * This results in the following subnode being added to the top-level
1055 * /reserved-memory node:
1056 *
1057 * reserved-memory {
1058 * #address-cells = <0x00000002>;
1059 * #size-cells = <0x00000002>;
1060 * ranges;
1061 *
1062 * framebuffer@92cb3000 {
1063 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1064 * phandle = <0x0000004d>;
1065 * };
1066 * };
1067 *
1068 * If the top-level /reserved-memory node does not exist, it will be created.
1069 * The phandle returned from the function call can be used to reference this
1070 * reserved memory region from other nodes.
1071 *
1072 * @param blob FDT blob
1073 * @param basename base name of the node to create
1074 * @param carveout information about the carveout region
1075 * @param phandlep return location for the phandle of the carveout region
1076 * @return 0 on success or a negative error code on failure
1077 */
1078int fdtdec_add_reserved_memory(void *blob, const char *basename,
1079 const struct fdt_memory *carveout,
1080 uint32_t *phandlep);
1081
1082/**
Simon Glass26b78b22015-02-27 22:06:34 -07001083 * Set up the device tree ready for use
1084 */
Simon Glassa0877672015-02-27 22:06:35 -07001085int fdtdec_setup(void);
Simon Glass26b78b22015-02-27 22:06:34 -07001086
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001087#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1088/**
1089 * fdtdec_resetup() - Set up the device tree again
1090 *
1091 * The main difference with fdtdec_setup() is that it returns if the fdt has
1092 * changed because a better match has been found.
1093 * This is typically used for boards that rely on a DM driver to detect the
1094 * board type. This function sould be called by the board code after the stuff
1095 * needed by board_fit_config_name_match() to operate porperly is available.
1096 * If this functions signals that a rescan is necessary, the board code must
1097 * unbind all the drivers using dm_uninit() and then rescan the DT with
1098 * dm_init_and_scan().
1099 *
1100 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1101 * the fdt is required
1102 *
1103 * @return 0 if OK, -ve on error
1104 */
1105int fdtdec_resetup(int *rescan);
1106#endif
1107
Alex Deymo5b661ec2017-04-02 01:25:20 -07001108/**
1109 * Board-specific FDT initialization. Returns the address to a device tree blob.
Rob Clark974dcac2018-01-10 11:34:37 +01001110 * Called when CONFIG_OF_BOARD is defined, or if CONFIG_OF_SEPARATE is defined
1111 * and the board implements it.
Alex Deymo5b661ec2017-04-02 01:25:20 -07001112 */
Baruch Siach99a097d2018-10-28 14:41:14 +02001113void *board_fdt_blob_setup(void);
Michael Prattfa28d142018-06-11 13:07:09 -06001114
1115/*
1116 * Decode the size of memory
1117 *
1118 * RAM size is normally set in a /memory node and consists of a list of
1119 * (base, size) cells in the 'reg' property. This information is used to
1120 * determine the total available memory as well as the address and size
1121 * of each bank.
1122 *
1123 * Optionally the memory configuration can vary depending on a board id,
1124 * typically read from strapping resistors or an EEPROM on the board.
1125 *
1126 * Finally, memory size can be detected (within certain limits) by probing
1127 * the available memory. It is safe to do so within the limits provides by
1128 * the board's device tree information. This makes it possible to produce
1129 * boards with different memory sizes, where the device tree specifies the
1130 * maximum memory configuration, and the smaller memory configuration is
1131 * probed.
1132 *
1133 * This function decodes that information, returning the memory base address,
1134 * size and bank information. See the memory.txt binding for full
1135 * documentation.
1136 *
1137 * @param blob Device tree blob
1138 * @param area Name of node to check (NULL means "/memory")
1139 * @param board_id Board ID to look up
1140 * @param basep Returns base address of first memory bank (NULL to
1141 * ignore)
1142 * @param sizep Returns total memory size (NULL to ignore)
1143 * @param bd Updated with the memory bank information (NULL to skip)
1144 * @return 0 if OK, -ve on error
1145 */
1146int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1147 phys_addr_t *basep, phys_size_t *sizep,
1148 struct bd_info *bd);
Alex Deymo5b661ec2017-04-02 01:25:20 -07001149
Simon Glass0a9d66f2012-07-12 05:25:02 +00001150#endif