blob: bd1149f46d08d00d0ec186be923e9f8b899a0391 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassb5220bc2011-10-24 19:15:32 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glassb5220bc2011-10-24 19:15:32 +00004 */
5
Simon Glass5bfa78d2012-07-12 05:25:02 +00006#ifndef __fdtdec_h
7#define __fdtdec_h
Simon Glassb5220bc2011-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 Yamadab08c8c42018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Bin Menga62e84d2014-12-31 16:05:11 +080018#include <pci.h>
Simon Glassb5220bc2011-10-24 19:15:32 +000019
20/*
Johan Jonker53c50932023-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 Glassb5220bc2011-10-24 19:15:32 +000025 * endian even on a litle endian machine.
26 */
Thierry Reding4f253ad2019-03-21 19:10:00 +010027
Chen Guanqiaoaa351a12021-04-12 14:51:11 +080028#define FDT_SIZE_T_NONE (-1U)
29
Johan Jonker53c50932023-03-13 01:33:23 +010030#ifdef CONFIG_FDT_64BIT
31typedef u64 fdt_addr_t;
32typedef u64 fdt_size_t;
Patrice Chotard9876ae72022-01-04 08:42:48 +010033#define FDT_ADDR_T_NONE ((ulong)(-1))
34
Simon Glassb5220bc2011-10-24 19:15:32 +000035#define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
Simon Glassf20c4612012-10-25 16:31:00 +000036#define fdt_size_to_cpu(reg) be64_to_cpu(reg)
Thierry Reding155d0a02019-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 Glassc20ee0e2017-08-29 14:15:50 -060039typedef fdt64_t fdt_val_t;
Simon Glassb5220bc2011-10-24 19:15:32 +000040#else
Johan Jonker53c50932023-03-13 01:33:23 +010041typedef u32 fdt_addr_t;
42typedef u32 fdt_size_t;
Patrice Chotard9876ae72022-01-04 08:42:48 +010043#define FDT_ADDR_T_NONE (-1U)
44
Simon Glassb5220bc2011-10-24 19:15:32 +000045#define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
Simon Glassf20c4612012-10-25 16:31:00 +000046#define fdt_size_to_cpu(reg) be32_to_cpu(reg)
Thierry Reding155d0a02019-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 Glassc20ee0e2017-08-29 14:15:50 -060049typedef fdt32_t fdt_val_t;
Simon Glassb5220bc2011-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 Pratt90c08fa2018-06-11 13:07:09 -060058struct bd_info;
59
Simon Glass39605c62021-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
75 * @FDTSRC_NONE: No devicetree at all
76 */
77enum fdt_source_t {
78 FDTSRC_SEPARATE,
79 FDTSRC_FIT,
80 FDTSRC_BOARD,
81 FDTSRC_EMBED,
82 FDTSRC_ENV,
83};
84
Thierry Reding56f42242014-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 Menga62e84d2014-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
Simon Glass2ebebb92019-12-28 10:44:42 -0700138extern u8 __dtb_dt_begin[]; /* embedded device tree blob */
139extern u8 __dtb_dt_spl_begin[]; /* embedded device tree blob for SPL/TPL */
140
Simon Glassd893b8a2021-12-16 20:59:26 -0700141/* Get a pointer to the embedded devicetree, if there is one, else NULL */
142static inline u8 *dtb_dt_embedded(void)
143{
144#ifdef CONFIG_OF_EMBED
145# ifdef CONFIG_SPL_BUILD
146 return __dtb_dt_spl_begin;
147# else
148 return __dtb_dt_begin;
149# endif
150#else
151 return NULL;
152#endif
153}
154
Thierry Reding56f42242014-08-26 17:33:53 +0200155/**
156 * Compute the size of a resource.
157 *
158 * @param res the resource to operate on
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100159 * Return: the size of the resource
Thierry Reding56f42242014-08-26 17:33:53 +0200160 */
161static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
162{
163 return res->end - res->start + 1;
164}
165
Simon Glassb5220bc2011-10-24 19:15:32 +0000166/**
167 * Compat types that we know about and for which we might have drivers.
168 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
169 * within drivers.
170 */
171enum fdt_compat_id {
172 COMPAT_UNKNOWN,
Allen Martin00a27492012-08-31 08:30:00 +0000173 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
174 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
Jim Lin312693c2012-07-29 20:53:29 +0000175 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
Thierry Reding79c7a902014-12-09 22:25:09 -0700176 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
177 /* Tegra124 XUSB pad controller */
Tom Warren7aaa5a62015-03-04 16:36:00 -0700178 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
179 /* Tegra210 XUSB pad controller */
Rajeshwari Shinde6abd1622013-01-07 23:35:05 +0000180 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
Vivek Gautam108b85b2013-09-14 14:02:48 +0530181 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
Akshay Saraswat618766c2013-02-25 01:13:01 +0000182 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
Piotr Wilczekde461c52014-03-07 14:59:39 +0100183 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
Jaehoon Chung7d3ca0f2014-05-16 13:59:51 +0900184 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
Simon Glassbb8215f2013-03-11 06:08:08 +0000185 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
Ajay Kumar45c480c2014-09-05 16:53:33 +0530186 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
Simon Glass77f9b1f2014-11-12 22:42:21 -0700187 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
Bin Mengc89ada02015-02-05 23:42:26 +0800188 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
Marek Vasut6ab00db2015-07-25 19:33:56 +0200189 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
Marek Vasut129adf52015-07-25 10:48:14 +0200190 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
Marek Vasutef4b01b2015-12-05 19:28:44 +0100191 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
Andrew Bradfordf3b84a32015-08-07 08:36:35 -0400192 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
193 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
Bin Meng394e0b62015-12-11 02:55:44 -0800194 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
Ley Foon Tane11b5e82017-04-05 17:32:47 +0800195 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
196 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
197 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
198 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
199 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
200 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
201 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
202 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
Tien Fong Cheeeb57c0b2017-09-25 16:40:03 +0800203 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
204 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
Marek Vasut19c8fc72018-05-12 11:56:10 +0200205 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
Simon Glassb5220bc2011-10-24 19:15:32 +0000206
207 COMPAT_COUNT,
208};
209
Simon Glass57068a72015-01-05 20:05:26 -0700210#define MAX_PHANDLE_ARGS 16
211struct fdtdec_phandle_args {
212 int node;
213 int args_count;
214 uint32_t args[MAX_PHANDLE_ARGS];
215};
216
217/**
218 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
219 *
220 * This function is useful to parse lists of phandles and their arguments.
221 *
222 * Example:
223 *
224 * phandle1: node1 {
225 * #list-cells = <2>;
226 * }
227 *
228 * phandle2: node2 {
229 * #list-cells = <1>;
230 * }
231 *
232 * node3 {
233 * list = <&phandle1 1 2 &phandle2 3>;
234 * }
235 *
236 * To get a device_node of the `node2' node you may call this:
237 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
238 * &args);
239 *
240 * (This function is a modified version of __of_parse_phandle_with_args() from
241 * Linux 3.18)
242 *
243 * @blob: Pointer to device tree
244 * @src_node: Offset of device tree node containing a list
245 * @list_name: property name that contains a list
246 * @cells_name: property name that specifies the phandles' arguments count,
247 * or NULL to use @cells_count
248 * @cells_count: Cell count to use if @cells_name is NULL
249 * @index: index of a phandle to parse out
250 * @out_args: optional pointer to output arguments structure (will be filled)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100251 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
Simon Glass57068a72015-01-05 20:05:26 -0700252 * @list_name does not exist, a phandle was not found, @cells_name
253 * could not be found, the arguments were truncated or there were too
254 * many arguments.
255 *
256 */
257int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
258 const char *list_name,
259 const char *cells_name,
260 int cell_count, int index,
261 struct fdtdec_phandle_args *out_args);
262
Sean Paul202ff752012-10-25 16:31:06 +0000263/**
Simon Glassb5220bc2011-10-24 19:15:32 +0000264 * Find the next numbered alias for a peripheral. This is used to enumerate
265 * all the peripherals of a certain type.
266 *
267 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
268 * this function will return a pointer to the node the alias points to, and
269 * then update *upto to 1. Next time you call this function, the next node
270 * will be returned.
271 *
272 * All nodes returned will match the compatible ID, as it is assumed that
273 * all peripherals use the same driver.
274 *
275 * @param blob FDT blob to use
276 * @param name Root name of alias to search for
277 * @param id Compatible ID to look for
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100278 * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
Simon Glassb5220bc2011-10-24 19:15:32 +0000279 */
280int fdtdec_next_alias(const void *blob, const char *name,
281 enum fdt_compat_id id, int *upto);
282
283/**
Gerald Van Baren7cde3972012-11-12 23:13:54 -0500284 * Find the compatible ID for a given node.
285 *
286 * Generally each node has at least one compatible string attached to it.
287 * This function looks through our list of known compatible strings and
288 * returns the corresponding ID which matches the compatible string.
289 *
290 * @param blob FDT blob to use
291 * @param node Node containing compatible string to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100292 * Return: compatible ID, or COMPAT_UNKNOWN if we cannot find a match
Gerald Van Baren7cde3972012-11-12 23:13:54 -0500293 */
294enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
295
296/**
Simon Glassf88fe2d2012-02-27 10:52:34 +0000297 * Find the next compatible node for a peripheral.
298 *
299 * Do the first call with node = 0. This function will return a pointer to
300 * the next compatible node. Next time you call this function, pass the
301 * value returned, and the next node will be provided.
302 *
303 * @param blob FDT blob to use
304 * @param node Start node for search
305 * @param id Compatible ID to look for (enum fdt_compat_id)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100306 * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
Simon Glassf88fe2d2012-02-27 10:52:34 +0000307 */
308int fdtdec_next_compatible(const void *blob, int node,
309 enum fdt_compat_id id);
310
311/**
Simon Glass3ddecfc2012-04-02 13:18:42 +0000312 * Find the next compatible subnode for a peripheral.
313 *
314 * Do the first call with node set to the parent and depth = 0. This
315 * function will return the offset of the next compatible node. Next time
316 * you call this function, pass the node value returned last time, with
317 * depth unchanged, and the next node will be provided.
318 *
319 * @param blob FDT blob to use
320 * @param node Start node for search
321 * @param id Compatible ID to look for (enum fdt_compat_id)
322 * @param depthp Current depth (set to 0 before first call)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100323 * Return: offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
Simon Glass3ddecfc2012-04-02 13:18:42 +0000324 */
325int fdtdec_next_compatible_subnode(const void *blob, int node,
326 enum fdt_compat_id id, int *depthp);
327
Stephen Warren02464e32015-08-06 15:31:02 -0600328/*
329 * Look up an address property in a node and return the parsed address, and
330 * optionally the parsed size.
331 *
332 * This variant assumes a known and fixed number of cells are used to
333 * represent the address and size.
334 *
335 * You probably don't want to use this function directly except to parse
336 * non-standard properties, and never to parse the "reg" property. Instead,
337 * use one of the "auto" variants below, which automatically honor the
338 * #address-cells and #size-cells properties in the parent node.
339 *
340 * @param blob FDT blob
341 * @param node node to examine
342 * @param prop_name name of property to find
343 * @param index which address to retrieve from a list of addresses. Often 0.
344 * @param na the number of cells used to represent an address
345 * @param ns the number of cells used to represent a size
346 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren6e06acb2016-08-05 09:47:51 -0600347 * @param translate Indicates whether to translate the returned value
348 * using the parent node's ranges property.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100349 * Return: address, if found, or FDT_ADDR_T_NONE if not
Stephen Warren02464e32015-08-06 15:31:02 -0600350 */
351fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
352 const char *prop_name, int index, int na, int ns,
Stephen Warren6e06acb2016-08-05 09:47:51 -0600353 fdt_size_t *sizep, bool translate);
Stephen Warren02464e32015-08-06 15:31:02 -0600354
355/*
356 * Look up an address property in a node and return the parsed address, and
357 * optionally the parsed size.
358 *
359 * This variant automatically determines the number of cells used to represent
360 * the address and size by parsing the provided parent node's #address-cells
361 * and #size-cells properties.
362 *
363 * @param blob FDT blob
364 * @param parent parent node of @node
365 * @param node node to examine
366 * @param prop_name name of property to find
367 * @param index which address to retrieve from a list of addresses. Often 0.
368 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren6e06acb2016-08-05 09:47:51 -0600369 * @param translate Indicates whether to translate the returned value
370 * using the parent node's ranges property.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100371 * Return: address, if found, or FDT_ADDR_T_NONE if not
Stephen Warren02464e32015-08-06 15:31:02 -0600372 */
373fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
Stephen Warren6e06acb2016-08-05 09:47:51 -0600374 int node, const char *prop_name, int index, fdt_size_t *sizep,
375 bool translate);
Stephen Warren02464e32015-08-06 15:31:02 -0600376
377/*
378 * Look up an address property in a node and return the parsed address, and
379 * optionally the parsed size.
380 *
381 * This variant automatically determines the number of cells used to represent
382 * the address and size by parsing the parent node's #address-cells
383 * and #size-cells properties. The parent node is automatically found.
384 *
385 * The automatic parent lookup implemented by this function is slow.
386 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
387 * possible.
388 *
389 * @param blob FDT blob
390 * @param parent parent node of @node
391 * @param node node to examine
392 * @param prop_name name of property to find
393 * @param index which address to retrieve from a list of addresses. Often 0.
394 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren6e06acb2016-08-05 09:47:51 -0600395 * @param translate Indicates whether to translate the returned value
396 * using the parent node's ranges property.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100397 * Return: address, if found, or FDT_ADDR_T_NONE if not
Stephen Warren02464e32015-08-06 15:31:02 -0600398 */
399fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
Stephen Warren6e06acb2016-08-05 09:47:51 -0600400 const char *prop_name, int index, fdt_size_t *sizep,
401 bool translate);
Stephen Warren02464e32015-08-06 15:31:02 -0600402
403/*
404 * Look up an address property in a node and return the parsed address.
405 *
406 * This variant hard-codes the number of cells used to represent the address
407 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
408 * always returns the first address value in the property (index 0).
409 *
410 * Use of this function is not recommended due to the hard-coding of cell
411 * counts. There is no programmatic validation that these hard-coded values
412 * actually match the device tree content in any way at all. This assumption
413 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
414 * set in the U-Boot build and exercising strict control over DT content to
415 * ensure use of matching #address-cells/#size-cells properties. However, this
416 * approach is error-prone; those familiar with DT will not expect the
417 * assumption to exist, and could easily invalidate it. If the assumption is
418 * invalidated, this function will not report the issue, and debugging will
419 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
Simon Glassb5220bc2011-10-24 19:15:32 +0000420 *
421 * @param blob FDT blob
422 * @param node node to examine
423 * @param prop_name name of property to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100424 * Return: address, if found, or FDT_ADDR_T_NONE if not
Simon Glassb5220bc2011-10-24 19:15:32 +0000425 */
426fdt_addr_t fdtdec_get_addr(const void *blob, int node,
427 const char *prop_name);
428
Stephen Warren02464e32015-08-06 15:31:02 -0600429/*
430 * Look up an address property in a node and return the parsed address, and
431 * optionally the parsed size.
432 *
433 * This variant hard-codes the number of cells used to represent the address
434 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
435 * always returns the first address value in the property (index 0).
436 *
437 * Use of this function is not recommended due to the hard-coding of cell
438 * counts. There is no programmatic validation that these hard-coded values
439 * actually match the device tree content in any way at all. This assumption
440 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
441 * set in the U-Boot build and exercising strict control over DT content to
442 * ensure use of matching #address-cells/#size-cells properties. However, this
443 * approach is error-prone; those familiar with DT will not expect the
444 * assumption to exist, and could easily invalidate it. If the assumption is
445 * invalidated, this function will not report the issue, and debugging will
446 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
Simon Glass4397a2a2013-03-19 04:58:51 +0000447 *
448 * @param blob FDT blob
449 * @param node node to examine
450 * @param prop_name name of property to find
Stephen Warren02464e32015-08-06 15:31:02 -0600451 * @param sizep a pointer to store the size into. Use NULL if not required
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100452 * Return: address, if found, or FDT_ADDR_T_NONE if not
Simon Glass4397a2a2013-03-19 04:58:51 +0000453 */
454fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
455 const char *prop_name, fdt_size_t *sizep);
456
457/**
Bin Menga62e84d2014-12-31 16:05:11 +0800458 * Look at the compatible property of a device node that represents a PCI
459 * device and extract pci vendor id and device id from it.
460 *
461 * @param blob FDT blob
462 * @param node node to examine
463 * @param vendor vendor id of the pci device
464 * @param device device id of the pci device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100465 * Return: 0 if ok, negative on error
Bin Menga62e84d2014-12-31 16:05:11 +0800466 */
467int fdtdec_get_pci_vendev(const void *blob, int node,
468 u16 *vendor, u16 *device);
469
470/**
471 * Look at the pci address of a device node that represents a PCI device
Bin Menga62e84d2014-12-31 16:05:11 +0800472 * and return base address of the pci device's registers.
473 *
Simon Glassfcc0a872015-11-29 13:17:54 -0700474 * @param dev device to examine
Bin Menga62e84d2014-12-31 16:05:11 +0800475 * @param addr pci address in the form of fdt_pci_addr
476 * @param bar returns base address of the pci device's registers
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100477 * Return: 0 if ok, negative on error
Bin Menga62e84d2014-12-31 16:05:11 +0800478 */
Simon Glass194fca92020-01-27 08:49:38 -0700479int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr,
Simon Glassfcc0a872015-11-29 13:17:54 -0700480 u32 *bar);
Bin Menga62e84d2014-12-31 16:05:11 +0800481
482/**
Suneel Garapati1db7ee42019-10-19 15:19:35 -0700483 * Look at the bus range property of a device node and return the pci bus
484 * range for this node.
485 * The property must hold one fdt_pci_addr with a length.
486 * @param blob FDT blob
487 * @param node node to examine
488 * @param res the resource structure to return the bus range
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100489 * Return: 0 if ok, negative on error
Suneel Garapati1db7ee42019-10-19 15:19:35 -0700490 */
491
492int fdtdec_get_pci_bus_range(const void *blob, int node,
493 struct fdt_resource *res);
494
495/**
Simon Glassb5220bc2011-10-24 19:15:32 +0000496 * Look up a 32-bit integer property in a node and return it. The property
497 * must have at least 4 bytes of data. The value of the first cell is
498 * returned.
499 *
500 * @param blob FDT blob
501 * @param node node to examine
502 * @param prop_name name of property to find
503 * @param default_val default value to return if the property is not found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100504 * Return: integer value, if found, or default_val if not
Simon Glassb5220bc2011-10-24 19:15:32 +0000505 */
506s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
507 s32 default_val);
508
509/**
Chin Liang Seebfa3e552015-10-17 08:30:32 -0500510 * Unsigned version of fdtdec_get_int. The property must have at least
511 * 4 bytes of data. The value of the first cell is returned.
512 *
513 * @param blob FDT blob
514 * @param node node to examine
515 * @param prop_name name of property to find
516 * @param default_val default value to return if the property is not found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100517 * Return: unsigned integer value, if found, or default_val if not
Chin Liang Seebfa3e552015-10-17 08:30:32 -0500518 */
519unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
520 unsigned int default_val);
521
522/**
Simon Glass5f7bfdd2015-03-05 12:25:14 -0700523 * Get a variable-sized number from a property
524 *
525 * This reads a number from one or more cells.
526 *
527 * @param ptr Pointer to property
528 * @param cells Number of cells containing the number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100529 * Return: the value in the cells
Simon Glass5f7bfdd2015-03-05 12:25:14 -0700530 */
531u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
532
533/**
Che-Liang Chiouaadef0a2012-10-25 16:31:05 +0000534 * Look up a 64-bit integer property in a node and return it. The property
535 * must have at least 8 bytes of data (2 cells). The first two cells are
536 * concatenated to form a 8 bytes value, where the first cell is top half and
537 * the second cell is bottom half.
538 *
539 * @param blob FDT blob
540 * @param node node to examine
541 * @param prop_name name of property to find
542 * @param default_val default value to return if the property is not found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100543 * Return: integer value, if found, or default_val if not
Che-Liang Chiouaadef0a2012-10-25 16:31:05 +0000544 */
545uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
546 uint64_t default_val);
547
548/**
Simon Glassb5220bc2011-10-24 19:15:32 +0000549 * Checks whether a node is enabled.
550 * This looks for a 'status' property. If this exists, then returns 1 if
551 * the status is 'ok' and 0 otherwise. If there is no status property,
Simon Glassf88fe2d2012-02-27 10:52:34 +0000552 * it returns 1 on the assumption that anything mentioned should be enabled
553 * by default.
Simon Glassb5220bc2011-10-24 19:15:32 +0000554 *
555 * @param blob FDT blob
556 * @param node node to examine
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100557 * Return: integer value 0 (not enabled) or 1 (enabled)
Simon Glassb5220bc2011-10-24 19:15:32 +0000558 */
Simon Glassf88fe2d2012-02-27 10:52:34 +0000559int fdtdec_get_is_enabled(const void *blob, int node);
Simon Glassb5220bc2011-10-24 19:15:32 +0000560
561/**
Simon Glass9a263e52012-03-28 10:08:24 +0000562 * Checks that we have a valid fdt available to control U-Boot.
563
564 * However, if not then for the moment nothing is done, since this function
565 * is called too early to panic().
566 *
567 * @returns 0
Simon Glassb5220bc2011-10-24 19:15:32 +0000568 */
569int fdtdec_check_fdt(void);
Simon Glassa53f4a22012-01-17 08:20:50 +0000570
571/**
572 * Find the nodes for a peripheral and return a list of them in the correct
573 * order. This is used to enumerate all the peripherals of a certain type.
574 *
575 * To use this, optionally set up a /aliases node with alias properties for
576 * a peripheral. For example, for usb you could have:
577 *
578 * aliases {
579 * usb0 = "/ehci@c5008000";
580 * usb1 = "/ehci@c5000000";
581 * };
582 *
583 * Pass "usb" as the name to this function and will return a list of two
584 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
585 *
586 * All nodes returned will match the compatible ID, as it is assumed that
587 * all peripherals use the same driver.
588 *
589 * If no alias node is found, then the node list will be returned in the
590 * order found in the fdt. If the aliases mention a node which doesn't
591 * exist, then this will be ignored. If nodes are found with no aliases,
592 * they will be added in any order.
593 *
594 * If there is a gap in the aliases, then this function return a 0 node at
595 * that position. The return value will also count these gaps.
596 *
597 * This function checks node properties and will not return nodes which are
598 * marked disabled (status = "disabled").
599 *
600 * @param blob FDT blob to use
601 * @param name Root name of alias to search for
602 * @param id Compatible ID to look for
603 * @param node_list Place to put list of found nodes
604 * @param maxcount Maximum number of nodes to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100605 * Return: number of nodes found on success, FDT_ERR_... on error
Simon Glassa53f4a22012-01-17 08:20:50 +0000606 */
607int fdtdec_find_aliases_for_id(const void *blob, const char *name,
608 enum fdt_compat_id id, int *node_list, int maxcount);
609
610/*
Simon Glassc6782272012-02-03 15:13:53 +0000611 * This function is similar to fdtdec_find_aliases_for_id() except that it
612 * adds to the node_list that is passed in. Any 0 elements are considered
613 * available for allocation - others are considered already used and are
614 * skipped.
615 *
616 * You can use this by calling fdtdec_find_aliases_for_id() with an
617 * uninitialised array, then setting the elements that are returned to -1,
618 * say, then calling this function, perhaps with a different compat id.
619 * Any elements you get back that are >0 are new nodes added by the call
620 * to this function.
621 *
622 * Note that if you have some nodes with aliases and some without, you are
623 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
624 * one compat_id may fill in positions for which you have aliases defined
625 * for another compat_id. When you later call *this* function with the second
626 * compat_id, the alias positions may already be used. A debug warning may
627 * be generated in this case, but it is safest to define aliases for all
628 * nodes when you care about the ordering.
629 */
630int fdtdec_add_aliases_for_id(const void *blob, const char *name,
631 enum fdt_compat_id id, int *node_list, int maxcount);
632
Simon Glass5c33c9f2014-07-23 06:55:09 -0600633/**
634 * Get the alias sequence number of a node
635 *
636 * This works out whether a node is pointed to by an alias, and if so, the
637 * sequence number of that alias. Aliases are of the form <base><num> where
638 * <num> is the sequence number. For example spi2 would be sequence number
639 * 2.
640 *
641 * @param blob Device tree blob (if NULL, then error is returned)
642 * @param base Base name for alias (before the underscore)
643 * @param node Node to look up
644 * @param seqp This is set to the sequence number if one is found,
645 * but otherwise the value is left alone
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100646 * Return: 0 if a sequence was found, -ve if not
Simon Glass5c33c9f2014-07-23 06:55:09 -0600647 */
648int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
649 int *seqp);
650
Simon Glass3234aa42014-07-23 06:55:16 -0600651/**
Michal Simek003c9dc2019-01-31 16:30:58 +0100652 * Get the highest alias number for susbystem.
653 *
654 * It parses all aliases and find out highest recorded alias for subsystem.
655 * Aliases are of the form <base><num> where <num> is the sequence number.
656 *
657 * @param blob Device tree blob (if NULL, then error is returned)
658 * @param base Base name for alias susbystem (before the number)
659 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100660 * Return: 0 highest alias ID, -1 if not found
Michal Simek003c9dc2019-01-31 16:30:58 +0100661 */
662int fdtdec_get_alias_highest_id(const void *blob, const char *base);
663
664/**
Simon Glass3bc37a52015-10-17 19:41:14 -0600665 * Get a property from the /chosen node
666 *
667 * @param blob Device tree blob (if NULL, then NULL is returned)
668 * @param name Property name to look up
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100669 * Return: Value of property, or NULL if it does not exist
Simon Glass3bc37a52015-10-17 19:41:14 -0600670 */
671const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
672
673/**
674 * Get the offset of the given /chosen node
Simon Glassaac07d42014-09-04 16:27:24 -0600675 *
676 * This looks up a property in /chosen containing the path to another node,
677 * then finds the offset of that node.
678 *
679 * @param blob Device tree blob (if NULL, then error is returned)
680 * @param name Property name, e.g. "stdout-path"
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100681 * Return: Node offset referred to by that chosen node, or -ve FDT_ERR_...
Simon Glassaac07d42014-09-04 16:27:24 -0600682 */
683int fdtdec_get_chosen_node(const void *blob, const char *name);
684
Simon Glassc6782272012-02-03 15:13:53 +0000685/*
Simon Glassa53f4a22012-01-17 08:20:50 +0000686 * Get the name for a compatible ID
687 *
688 * @param id Compatible ID to look for
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100689 * Return: compatible string for that id
Simon Glassa53f4a22012-01-17 08:20:50 +0000690 */
691const char *fdtdec_get_compatible(enum fdt_compat_id id);
Simon Glassd17da652012-02-27 10:52:35 +0000692
693/* Look up a phandle and follow it to its node. Then return the offset
694 * of that node.
695 *
696 * @param blob FDT blob
697 * @param node node to examine
698 * @param prop_name name of property to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100699 * Return: node offset if found, -ve error code on error
Simon Glassd17da652012-02-27 10:52:35 +0000700 */
701int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
702
703/**
704 * Look up a property in a node and return its contents in an integer
705 * array of given length. The property must have at least enough data for
706 * the array (4*count bytes). It may have more, but this will be ignored.
707 *
708 * @param blob FDT blob
709 * @param node node to examine
710 * @param prop_name name of property to find
711 * @param array array to fill with data
712 * @param count number of array elements
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100713 * Return: 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
Simon Glassd17da652012-02-27 10:52:35 +0000714 * or -FDT_ERR_BADLAYOUT if not enough data
715 */
716int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
717 u32 *array, int count);
718
719/**
Simon Glassa9f04d42014-11-10 18:00:19 -0700720 * Look up a property in a node and return its contents in an integer
721 * array of given length. The property must exist but may have less data that
722 * expected (4*count bytes). It may have more, but this will be ignored.
723 *
724 * @param blob FDT blob
725 * @param node node to examine
726 * @param prop_name name of property to find
727 * @param array array to fill with data
728 * @param count number of array elements
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100729 * Return: number of array elements if ok, or -FDT_ERR_NOTFOUND if the
Simon Glassa9f04d42014-11-10 18:00:19 -0700730 * property is not found
731 */
732int fdtdec_get_int_array_count(const void *blob, int node,
733 const char *prop_name, u32 *array, int count);
734
735/**
Simon Glass96875e72012-04-02 13:18:41 +0000736 * Look up a property in a node and return a pointer to its contents as a
737 * unsigned int array of given length. The property must have at least enough
738 * data for the array ('count' cells). It may have more, but this will be
739 * ignored. The data is not copied.
740 *
741 * Note that you must access elements of the array with fdt32_to_cpu(),
742 * since the elements will be big endian even on a little endian machine.
743 *
744 * @param blob FDT blob
745 * @param node node to examine
746 * @param prop_name name of property to find
747 * @param count number of array elements
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100748 * Return: pointer to array if found, or NULL if the property is not
Simon Glass96875e72012-04-02 13:18:41 +0000749 * found or there is not enough data
750 */
751const u32 *fdtdec_locate_array(const void *blob, int node,
752 const char *prop_name, int count);
753
754/**
Simon Glassd17da652012-02-27 10:52:35 +0000755 * Look up a boolean property in a node and return it.
756 *
757 * A boolean properly is true if present in the device tree and false if not
758 * present, regardless of its value.
759 *
760 * @param blob FDT blob
761 * @param node node to examine
762 * @param prop_name name of property to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100763 * Return: 1 if the properly is present; 0 if it isn't present
Simon Glassd17da652012-02-27 10:52:35 +0000764 */
765int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
Simon Glassed3ee5c2012-02-27 10:52:36 +0000766
Peng Fan1889a7e2016-02-01 13:31:15 +0800767/*
768 * Count child nodes of one parent node.
769 *
770 * @param blob FDT blob
771 * @param node parent node
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100772 * Return: number of child node; 0 if there is not child node
Peng Fan1889a7e2016-02-01 13:31:15 +0800773 */
774int fdtdec_get_child_count(const void *blob, int node);
775
Anton Staffbed4d892012-04-17 09:01:28 +0000776/*
777 * Look up a property in a node and return its contents in a byte
778 * array of given length. The property must have at least enough data for
779 * the array (count bytes). It may have more, but this will be ignored.
780 *
781 * @param blob FDT blob
782 * @param node node to examine
783 * @param prop_name name of property to find
784 * @param array array to fill with data
785 * @param count number of array elements
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100786 * Return: 0 if ok, or -FDT_ERR_MISSING if the property is not found,
Anton Staffbed4d892012-04-17 09:01:28 +0000787 * or -FDT_ERR_BADLAYOUT if not enough data
788 */
789int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
790 u8 *array, int count);
791
792/**
793 * Look up a property in a node and return a pointer to its contents as a
794 * byte array of given length. The property must have at least enough data
795 * for the array (count bytes). It may have more, but this will be ignored.
796 * The data is not copied.
797 *
798 * @param blob FDT blob
799 * @param node node to examine
800 * @param prop_name name of property to find
801 * @param count number of array elements
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100802 * Return: pointer to byte array if found, or NULL if the property is not
Anton Staffbed4d892012-04-17 09:01:28 +0000803 * found or there is not enough data
804 */
805const u8 *fdtdec_locate_byte_array(const void *blob, int node,
806 const char *prop_name, int count);
Simon Glassf20c4612012-10-25 16:31:00 +0000807
808/**
Thierry Reding56f42242014-08-26 17:33:53 +0200809 * Obtain an indexed resource from a device property.
810 *
811 * @param fdt FDT blob
812 * @param node node to examine
813 * @param property name of the property to parse
814 * @param index index of the resource to retrieve
815 * @param res returns the resource
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100816 * Return: 0 if ok, negative on error
Thierry Reding56f42242014-08-26 17:33:53 +0200817 */
818int fdt_get_resource(const void *fdt, int node, const char *property,
819 unsigned int index, struct fdt_resource *res);
820
821/**
822 * Obtain a named resource from a device property.
823 *
824 * Look up the index of the name in a list of strings and return the resource
825 * at that index.
826 *
827 * @param fdt FDT blob
828 * @param node node to examine
829 * @param property name of the property to parse
830 * @param prop_names name of the property containing the list of names
831 * @param name the name of the entry to look up
832 * @param res returns the resource
833 */
834int fdt_get_named_resource(const void *fdt, int node, const char *property,
835 const char *prop_names, const char *name,
836 struct fdt_resource *res);
837
Simon Glass12e67112015-04-14 21:03:21 -0600838/* Display timings from linux include/video/display_timing.h */
839enum display_flags {
840 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
841 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
842 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
843 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
844
845 /* data enable flag */
846 DISPLAY_FLAGS_DE_LOW = 1 << 4,
847 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
848 /* drive data on pos. edge */
849 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
850 /* drive data on neg. edge */
851 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
852 DISPLAY_FLAGS_INTERLACED = 1 << 8,
853 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
854 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
855};
856
857/*
858 * A single signal can be specified via a range of minimal and maximal values
859 * with a typical value, that lies somewhere inbetween.
860 */
861struct timing_entry {
862 u32 min;
863 u32 typ;
864 u32 max;
865};
866
867/*
868 * Single "mode" entry. This describes one set of signal timings a display can
869 * have in one setting. This struct can later be converted to struct videomode
870 * (see include/video/videomode.h). As each timing_entry can be defined as a
871 * range, one struct display_timing may become multiple struct videomodes.
872 *
873 * Example: hsync active high, vsync active low
874 *
875 * Active Video
876 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
877 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
878 * | | porch | | porch |
879 *
880 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
881 *
882 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
883 */
884struct display_timing {
885 struct timing_entry pixelclock;
886
887 struct timing_entry hactive; /* hor. active video */
888 struct timing_entry hfront_porch; /* hor. front porch */
889 struct timing_entry hback_porch; /* hor. back porch */
890 struct timing_entry hsync_len; /* hor. sync len */
891
892 struct timing_entry vactive; /* ver. active video */
893 struct timing_entry vfront_porch; /* ver. front porch */
894 struct timing_entry vback_porch; /* ver. back porch */
895 struct timing_entry vsync_len; /* ver. sync len */
896
897 enum display_flags flags; /* display flags */
Jernej Skrabec43c6bdd2017-04-29 14:43:36 +0200898 bool hdmi_monitor; /* is hdmi monitor? */
Simon Glass12e67112015-04-14 21:03:21 -0600899};
900
901/**
902 * fdtdec_decode_display_timing() - decode display timings
903 *
904 * Decode display timings from the supplied 'display-timings' node.
905 * See doc/device-tree-bindings/video/display-timing.txt for binding
906 * information.
907 *
908 * @param blob FDT blob
909 * @param node 'display-timing' node containing the timing subnodes
910 * @param index Index number to read (0=first timing subnode)
911 * @param config Place to put timings
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100912 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
Simon Glass12e67112015-04-14 21:03:21 -0600913 */
914int fdtdec_decode_display_timing(const void *blob, int node, int index,
915 struct display_timing *config);
Nathan Rossi623f6012016-12-19 00:03:34 +1000916
917/**
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530918 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
919 * gd->ram_start
Nathan Rossi623f6012016-12-19 00:03:34 +1000920 *
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530921 * Decode the /memory 'reg' property to determine the size and start of the
922 * first memory bank, populate the global data with the size and start of the
923 * first bank of memory.
Nathan Rossi623f6012016-12-19 00:03:34 +1000924 *
925 * This function should be called from a boards dram_init(). This helper
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530926 * function allows for boards to query the device tree for DRAM size and start
927 * address instead of hard coding the value in the case where the memory size
928 * and start address cannot be detected automatically.
Nathan Rossi623f6012016-12-19 00:03:34 +1000929 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100930 * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
Nathan Rossi623f6012016-12-19 00:03:34 +1000931 * invalid
932 */
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530933int fdtdec_setup_mem_size_base(void);
Nathan Rossi623f6012016-12-19 00:03:34 +1000934
935/**
Michal Simek7fce7392020-07-09 14:09:52 +0200936 * fdtdec_setup_mem_size_base_lowest() - decode and setup gd->ram_size and
937 * gd->ram_start by lowest available memory base
938 *
939 * Decode the /memory 'reg' property to determine the lowest start of the memory
940 * bank bank and populate the global data with it.
941 *
942 * This function should be called from a boards dram_init(). This helper
943 * function allows for boards to query the device tree for DRAM size and start
944 * address instead of hard coding the value in the case where the memory size
945 * and start address cannot be detected automatically.
946 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100947 * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
Michal Simek7fce7392020-07-09 14:09:52 +0200948 * invalid
949 */
950int fdtdec_setup_mem_size_base_lowest(void);
951
952/**
Nathan Rossi623f6012016-12-19 00:03:34 +1000953 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
954 *
955 * Decode the /memory 'reg' property to determine the address and size of the
956 * memory banks. Use this data to populate the global data board info with the
957 * phys address and size of memory banks.
958 *
959 * This function should be called from a boards dram_init_banksize(). This
960 * helper function allows for boards to query the device tree for memory bank
961 * information instead of hard coding the information in cases where it cannot
962 * be detected automatically.
963 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100964 * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or
Nathan Rossi623f6012016-12-19 00:03:34 +1000965 * invalid
966 */
967int fdtdec_setup_memory_banksize(void);
968
Simon Glassb45122f2015-02-27 22:06:34 -0700969/**
Thierry Redingebf30e82019-04-15 11:32:13 +0200970 * fdtdec_set_ethernet_mac_address() - set MAC address for default interface
971 *
972 * Looks up the default interface via the "ethernet" alias (in the /aliases
973 * node) and stores the given MAC in its "local-mac-address" property. This
974 * is useful on platforms that store the MAC address in a custom location.
975 * Board code can call this in the late init stage to make sure that the
976 * interface device tree node has the right MAC address configured for the
977 * Ethernet uclass to pick it up.
978 *
979 * Typically the FDT passed into this function will be U-Boot's control DTB.
980 * Given that a lot of code may be holding offsets to various nodes in that
981 * tree, this code will only set the "local-mac-address" property in-place,
982 * which means that it needs to exist and have space for the 6-byte address.
983 * This ensures that the operation is non-destructive and does not invalidate
984 * offsets that other drivers may be using.
985 *
986 * @param fdt FDT blob
987 * @param mac buffer containing the MAC address to set
988 * @param size size of MAC address
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100989 * Return: 0 on success or a negative error code on failure
Thierry Redingebf30e82019-04-15 11:32:13 +0200990 */
991int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size);
992
993/**
Thierry Reding8153d532019-03-21 19:10:01 +0100994 * fdtdec_set_phandle() - sets the phandle of a given node
995 *
996 * @param blob FDT blob
997 * @param node offset in the FDT blob of the node whose phandle is to
998 * be set
999 * @param phandle phandle to set for the given node
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001000 * Return: 0 on success or a negative error code on failure
Thierry Reding8153d532019-03-21 19:10:01 +01001001 */
Thierry Redingd81d9692019-04-15 10:08:20 +02001002static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
1003{
1004 return fdt_setprop_u32(blob, node, "phandle", phandle);
1005}
Thierry Reding8153d532019-03-21 19:10:01 +01001006
Thierry Redingb9aad372021-09-03 15:16:21 +02001007/* add "no-map" property */
1008#define FDTDEC_RESERVED_MEMORY_NO_MAP (1 << 0)
1009
Thierry Reding8153d532019-03-21 19:10:01 +01001010/**
Thierry Redingc9222a02019-03-21 19:10:02 +01001011 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
1012 *
1013 * If a reserved-memory node already exists for the given carveout, a phandle
1014 * for that node will be returned. Otherwise a new node will be created and a
1015 * phandle corresponding to it will be returned.
1016 *
1017 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1018 * for details on how to use reserved memory regions.
1019 *
1020 * As an example, consider the following code snippet:
1021 *
1022 * struct fdt_memory fb = {
1023 * .start = 0x92cb3000,
1024 * .end = 0x934b2fff,
1025 * };
1026 * uint32_t phandle;
1027 *
Thierry Reding46cb0672021-09-03 15:16:19 +02001028 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, NULL, 0, &phandle,
Thierry Redingb9aad372021-09-03 15:16:21 +02001029 * 0);
Thierry Redingc9222a02019-03-21 19:10:02 +01001030 *
1031 * This results in the following subnode being added to the top-level
1032 * /reserved-memory node:
1033 *
1034 * reserved-memory {
1035 * #address-cells = <0x00000002>;
1036 * #size-cells = <0x00000002>;
1037 * ranges;
1038 *
1039 * framebuffer@92cb3000 {
1040 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1041 * phandle = <0x0000004d>;
1042 * };
1043 * };
1044 *
1045 * If the top-level /reserved-memory node does not exist, it will be created.
1046 * The phandle returned from the function call can be used to reference this
1047 * reserved memory region from other nodes.
1048 *
Thierry Reding16523ac2019-03-21 19:10:03 +01001049 * See fdtdec_set_carveout() for a more elaborate example.
1050 *
Thierry Redingc9222a02019-03-21 19:10:02 +01001051 * @param blob FDT blob
1052 * @param basename base name of the node to create
1053 * @param carveout information about the carveout region
Thierry Reding46cb0672021-09-03 15:16:19 +02001054 * @param compatibles list of compatible strings for the carveout region
1055 * @param count number of compatible strings for the carveout region
Thierry Redingc9222a02019-03-21 19:10:02 +01001056 * @param phandlep return location for the phandle of the carveout region
Heiko Stuebner357d2ce2019-10-23 16:46:39 +02001057 * can be NULL if no phandle should be added
Thierry Redingb9aad372021-09-03 15:16:21 +02001058 * @param flags bitmask of flags to set for the carveout region
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001059 * Return: 0 on success or a negative error code on failure
Thierry Redingc9222a02019-03-21 19:10:02 +01001060 */
1061int fdtdec_add_reserved_memory(void *blob, const char *basename,
1062 const struct fdt_memory *carveout,
Thierry Reding46cb0672021-09-03 15:16:19 +02001063 const char **compatibles, unsigned int count,
Thierry Redingb9aad372021-09-03 15:16:21 +02001064 uint32_t *phandlep, unsigned long flags);
Thierry Redingc9222a02019-03-21 19:10:02 +01001065
1066/**
Thierry Reding16523ac2019-03-21 19:10:03 +01001067 * fdtdec_get_carveout() - reads a carveout from an FDT
1068 *
1069 * Reads information about a carveout region from an FDT. The carveout is a
1070 * referenced by its phandle that is read from a given property in a given
1071 * node.
1072 *
1073 * @param blob FDT blob
1074 * @param node name of a node
Thierry Reding4bf88ba2021-09-03 15:16:18 +02001075 * @param prop_name name of the property in the given node that contains
Thierry Reding16523ac2019-03-21 19:10:03 +01001076 * the phandle for the carveout
1077 * @param index index of the phandle for which to read the carveout
1078 * @param carveout return location for the carveout information
Thierry Reding4bf88ba2021-09-03 15:16:18 +02001079 * @param name return location for the carveout name
Thierry Reding46cb0672021-09-03 15:16:19 +02001080 * @param compatiblesp return location for compatible strings
Thierry Redingb9aad372021-09-03 15:16:21 +02001081 * @param countp return location for the number of compatible strings
1082 * @param flags return location for the flags of the carveout
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001083 * Return: 0 on success or a negative error code on failure
Thierry Reding16523ac2019-03-21 19:10:03 +01001084 */
Thierry Reding4bf88ba2021-09-03 15:16:18 +02001085int fdtdec_get_carveout(const void *blob, const char *node,
1086 const char *prop_name, unsigned int index,
Thierry Reding46cb0672021-09-03 15:16:19 +02001087 struct fdt_memory *carveout, const char **name,
Thierry Redingb9aad372021-09-03 15:16:21 +02001088 const char ***compatiblesp, unsigned int *countp,
1089 unsigned long *flags);
Thierry Reding16523ac2019-03-21 19:10:03 +01001090
1091/**
1092 * fdtdec_set_carveout() - sets a carveout region for a given node
1093 *
1094 * Sets a carveout region for a given node. If a reserved-memory node already
1095 * exists for the carveout, the phandle for that node will be reused. If no
1096 * such node exists, a new one will be created and a phandle to it stored in
1097 * a specified property of the given node.
1098 *
1099 * As an example, consider the following code snippet:
1100 *
1101 * const char *node = "/host1x@50000000/dc@54240000";
1102 * struct fdt_memory fb = {
1103 * .start = 0x92cb3000,
1104 * .end = 0x934b2fff,
1105 * };
1106 *
Thierry Reding46cb0672021-09-03 15:16:19 +02001107 * fdtdec_set_carveout(fdt, node, "memory-region", 0, "framebuffer", NULL,
Thierry Redingb9aad372021-09-03 15:16:21 +02001108 * 0, &fb, 0);
Thierry Reding16523ac2019-03-21 19:10:03 +01001109 *
1110 * dc@54200000 is a display controller and was set up by the bootloader to
1111 * scan out the framebuffer specified by "fb". This would cause the following
1112 * reserved memory region to be added:
1113 *
1114 * reserved-memory {
1115 * #address-cells = <0x00000002>;
1116 * #size-cells = <0x00000002>;
1117 * ranges;
1118 *
1119 * framebuffer@92cb3000 {
1120 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1121 * phandle = <0x0000004d>;
1122 * };
1123 * };
1124 *
1125 * A "memory-region" property will also be added to the node referenced by the
1126 * offset parameter.
1127 *
1128 * host1x@50000000 {
1129 * ...
1130 *
1131 * dc@54240000 {
1132 * ...
1133 * memory-region = <0x0000004d>;
1134 * ...
1135 * };
1136 *
1137 * ...
1138 * };
1139 *
1140 * @param blob FDT blob
1141 * @param node name of the node to add the carveout to
1142 * @param prop_name name of the property in which to store the phandle of
1143 * the carveout
1144 * @param index index of the phandle to store
Thierry Reding16523ac2019-03-21 19:10:03 +01001145 * @param carveout information about the carveout to add
Thierry Reding90194872021-09-03 15:16:20 +02001146 * @param name base name of the reserved-memory node to create
Thierry Reding46cb0672021-09-03 15:16:19 +02001147 * @param compatibles compatible strings to set for the carveout
1148 * @param count number of compatible strings
Thierry Redingb9aad372021-09-03 15:16:21 +02001149 * @param flags bitmask of flags to set for the carveout
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001150 * Return: 0 on success or a negative error code on failure
Thierry Reding16523ac2019-03-21 19:10:03 +01001151 */
1152int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
Thierry Reding90194872021-09-03 15:16:20 +02001153 unsigned int index, const struct fdt_memory *carveout,
1154 const char *name, const char **compatibles,
Thierry Redingb9aad372021-09-03 15:16:21 +02001155 unsigned int count, unsigned long flags);
Thierry Reding16523ac2019-03-21 19:10:03 +01001156
1157/**
Simon Glassb45122f2015-02-27 22:06:34 -07001158 * Set up the device tree ready for use
1159 */
Simon Glass08793612015-02-27 22:06:35 -07001160int fdtdec_setup(void);
Simon Glassb45122f2015-02-27 22:06:34 -07001161
Marek Vasut0e2afc82020-04-11 21:18:59 +02001162/**
1163 * Perform board-specific early DT adjustments
1164 */
1165int fdtdec_board_setup(const void *fdt_blob);
1166
Jean-Jacques Hiblotf1d2bc92018-12-07 14:50:52 +01001167/**
1168 * fdtdec_resetup() - Set up the device tree again
1169 *
1170 * The main difference with fdtdec_setup() is that it returns if the fdt has
1171 * changed because a better match has been found.
1172 * This is typically used for boards that rely on a DM driver to detect the
1173 * board type. This function sould be called by the board code after the stuff
1174 * needed by board_fit_config_name_match() to operate porperly is available.
1175 * If this functions signals that a rescan is necessary, the board code must
1176 * unbind all the drivers using dm_uninit() and then rescan the DT with
1177 * dm_init_and_scan().
1178 *
1179 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1180 * the fdt is required
1181 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001182 * Return: 0 if OK, -ve on error
Jean-Jacques Hiblotf1d2bc92018-12-07 14:50:52 +01001183 */
1184int fdtdec_resetup(int *rescan);
Jean-Jacques Hiblotf1d2bc92018-12-07 14:50:52 +01001185
Alex Deymo82f766d2017-04-02 01:25:20 -07001186/**
1187 * Board-specific FDT initialization. Returns the address to a device tree blob.
Simon Glass98550342021-12-16 20:59:31 -07001188 *
1189 * Called when CONFIG_OF_BOARD is defined.
1190 *
1191 * The existing devicetree is available at gd->fdt_blob
Ilias Apalodimase7fb7892021-10-26 09:12:33 +03001192 *
1193 * @err internal error code if we fail to setup a DTB
Simon Glass98550342021-12-16 20:59:31 -07001194 * @returns new devicetree blob pointer
Alex Deymo82f766d2017-04-02 01:25:20 -07001195 */
Ilias Apalodimase7fb7892021-10-26 09:12:33 +03001196void *board_fdt_blob_setup(int *err);
Michael Pratt90c08fa2018-06-11 13:07:09 -06001197
1198/*
1199 * Decode the size of memory
1200 *
1201 * RAM size is normally set in a /memory node and consists of a list of
1202 * (base, size) cells in the 'reg' property. This information is used to
1203 * determine the total available memory as well as the address and size
1204 * of each bank.
1205 *
1206 * Optionally the memory configuration can vary depending on a board id,
1207 * typically read from strapping resistors or an EEPROM on the board.
1208 *
1209 * Finally, memory size can be detected (within certain limits) by probing
1210 * the available memory. It is safe to do so within the limits provides by
1211 * the board's device tree information. This makes it possible to produce
1212 * boards with different memory sizes, where the device tree specifies the
1213 * maximum memory configuration, and the smaller memory configuration is
1214 * probed.
1215 *
1216 * This function decodes that information, returning the memory base address,
1217 * size and bank information. See the memory.txt binding for full
1218 * documentation.
1219 *
1220 * @param blob Device tree blob
1221 * @param area Name of node to check (NULL means "/memory")
1222 * @param board_id Board ID to look up
1223 * @param basep Returns base address of first memory bank (NULL to
1224 * ignore)
1225 * @param sizep Returns total memory size (NULL to ignore)
1226 * @param bd Updated with the memory bank information (NULL to skip)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001227 * Return: 0 if OK, -ve on error
Michael Pratt90c08fa2018-06-11 13:07:09 -06001228 */
1229int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1230 phys_addr_t *basep, phys_size_t *sizep,
1231 struct bd_info *bd);
Alex Deymo82f766d2017-04-02 01:25:20 -07001232
Simon Glass39605c62021-12-16 20:59:33 -07001233/**
1234 * fdtdec_get_srcname() - Get the name of where the devicetree comes from
1235 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01001236 * Return: source name
Simon Glass39605c62021-12-16 20:59:33 -07001237 */
1238const char *fdtdec_get_srcname(void);
1239
Simon Glass5bfa78d2012-07-12 05:25:02 +00001240#endif