blob: 09525ce510aa698641dc5fc0723be81e7412509c [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/*
21 * A typedef for a physical address. Note that fdt data is always big
22 * endian even on a litle endian machine.
23 */
York Sun28445aa2015-08-03 12:02:04 -070024typedef phys_addr_t fdt_addr_t;
25typedef phys_size_t fdt_size_t;
Thierry Reding4f253ad2019-03-21 19:10:00 +010026
Mario Sixc6b89f32018-02-12 08:05:57 +010027#define FDT_ADDR_T_NONE (-1U)
Chen Guanqiaoaa351a12021-04-12 14:51:11 +080028#define FDT_SIZE_T_NONE (-1U)
29
30#ifdef CONFIG_PHYS_64BIT
Simon Glassb5220bc2011-10-24 19:15:32 +000031#define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
Simon Glassf20c4612012-10-25 16:31:00 +000032#define fdt_size_to_cpu(reg) be64_to_cpu(reg)
Thierry Reding155d0a02019-03-21 19:09:59 +010033#define cpu_to_fdt_addr(reg) cpu_to_be64(reg)
34#define cpu_to_fdt_size(reg) cpu_to_be64(reg)
Simon Glassc20ee0e2017-08-29 14:15:50 -060035typedef fdt64_t fdt_val_t;
Simon Glassb5220bc2011-10-24 19:15:32 +000036#else
Simon Glassb5220bc2011-10-24 19:15:32 +000037#define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
Simon Glassf20c4612012-10-25 16:31:00 +000038#define fdt_size_to_cpu(reg) be32_to_cpu(reg)
Thierry Reding155d0a02019-03-21 19:09:59 +010039#define cpu_to_fdt_addr(reg) cpu_to_be32(reg)
40#define cpu_to_fdt_size(reg) cpu_to_be32(reg)
Simon Glassc20ee0e2017-08-29 14:15:50 -060041typedef fdt32_t fdt_val_t;
Simon Glassb5220bc2011-10-24 19:15:32 +000042#endif
43
44/* Information obtained about memory from the FDT */
45struct fdt_memory {
46 fdt_addr_t start;
47 fdt_addr_t end;
48};
49
Michael Pratt90c08fa2018-06-11 13:07:09 -060050struct bd_info;
51
Simon Glassaf282242015-03-06 13:19:03 -070052#ifdef CONFIG_SPL_BUILD
53#define SPL_BUILD 1
54#else
55#define SPL_BUILD 0
56#endif
57
Simon Glass39605c62021-12-16 20:59:33 -070058/**
59 * enum fdt_source_t - indicates where the devicetree came from
60 *
61 * These are listed in approximate order of desirability after FDTSRC_NONE
62 *
63 * @FDTSRC_SEPARATE: Appended to U-Boot. This is the normal approach if U-Boot
64 * is the only firmware being booted
65 * @FDTSRC_FIT: Found in a multi-dtb FIT. This should be used when U-Boot must
66 * select a devicetree from many options
67 * @FDTSRC_BOARD: Located by custom board code. This should only be used when
68 * the prior stage does not support FDTSRC_PASSAGE
69 * @FDTSRC_EMBED: Embedded into U-Boot executable. This should onyl be used when
70 * U-Boot is packaged as an ELF file, e.g. for debugging purposes
71 * @FDTSRC_ENV: Provided by the fdtcontroladdr environment variable. This should
72 * be used for debugging/development only
73 * @FDTSRC_NONE: No devicetree at all
74 */
75enum fdt_source_t {
76 FDTSRC_SEPARATE,
77 FDTSRC_FIT,
78 FDTSRC_BOARD,
79 FDTSRC_EMBED,
80 FDTSRC_ENV,
81};
82
Thierry Reding56f42242014-08-26 17:33:53 +020083/*
84 * Information about a resource. start is the first address of the resource
85 * and end is the last address (inclusive). The length of the resource will
86 * be equal to: end - start + 1.
87 */
88struct fdt_resource {
89 fdt_addr_t start;
90 fdt_addr_t end;
91};
92
Bin Menga62e84d2014-12-31 16:05:11 +080093enum fdt_pci_space {
94 FDT_PCI_SPACE_CONFIG = 0,
95 FDT_PCI_SPACE_IO = 0x01000000,
96 FDT_PCI_SPACE_MEM32 = 0x02000000,
97 FDT_PCI_SPACE_MEM64 = 0x03000000,
98 FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
99 FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
100};
101
102#define FDT_PCI_ADDR_CELLS 3
103#define FDT_PCI_SIZE_CELLS 2
104#define FDT_PCI_REG_SIZE \
105 ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
106
107/*
108 * The Open Firmware spec defines PCI physical address as follows:
109 *
110 * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
111 *
112 * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
113 * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
114 * phys.lo cell: llllllll llllllll llllllll llllllll
115 *
116 * where:
117 *
118 * n: is 0 if the address is relocatable, 1 otherwise
119 * p: is 1 if addressable region is prefetchable, 0 otherwise
120 * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
121 * (for Memory), or below 64KB (for relocatable I/O)
122 * ss: is the space code, denoting the address space
123 * bbbbbbbb: is the 8-bit Bus Number
124 * ddddd: is the 5-bit Device Number
125 * fff: is the 3-bit Function Number
126 * rrrrrrrr: is the 8-bit Register Number
127 * hhhhhhhh: is a 32-bit unsigned number
128 * llllllll: is a 32-bit unsigned number
129 */
130struct fdt_pci_addr {
131 u32 phys_hi;
132 u32 phys_mid;
133 u32 phys_lo;
134};
135
Simon Glass2ebebb92019-12-28 10:44:42 -0700136extern u8 __dtb_dt_begin[]; /* embedded device tree blob */
137extern u8 __dtb_dt_spl_begin[]; /* embedded device tree blob for SPL/TPL */
138
Simon Glassd893b8a2021-12-16 20:59:26 -0700139/* Get a pointer to the embedded devicetree, if there is one, else NULL */
140static inline u8 *dtb_dt_embedded(void)
141{
142#ifdef CONFIG_OF_EMBED
143# ifdef CONFIG_SPL_BUILD
144 return __dtb_dt_spl_begin;
145# else
146 return __dtb_dt_begin;
147# endif
148#else
149 return NULL;
150#endif
151}
152
Thierry Reding56f42242014-08-26 17:33:53 +0200153/**
154 * Compute the size of a resource.
155 *
156 * @param res the resource to operate on
157 * @return the size of the resource
158 */
159static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
160{
161 return res->end - res->start + 1;
162}
163
Simon Glassb5220bc2011-10-24 19:15:32 +0000164/**
165 * Compat types that we know about and for which we might have drivers.
166 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
167 * within drivers.
168 */
169enum fdt_compat_id {
170 COMPAT_UNKNOWN,
Allen Martin00a27492012-08-31 08:30:00 +0000171 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
172 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
Jim Lin312693c2012-07-29 20:53:29 +0000173 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
Thierry Reding79c7a902014-12-09 22:25:09 -0700174 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
175 /* Tegra124 XUSB pad controller */
Tom Warren7aaa5a62015-03-04 16:36:00 -0700176 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
177 /* Tegra210 XUSB pad controller */
Rajeshwari Shinde6abd1622013-01-07 23:35:05 +0000178 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
Vivek Gautam108b85b2013-09-14 14:02:48 +0530179 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
Akshay Saraswat618766c2013-02-25 01:13:01 +0000180 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
Piotr Wilczekde461c52014-03-07 14:59:39 +0100181 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
Jaehoon Chung7d3ca0f2014-05-16 13:59:51 +0900182 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
Simon Glassbb8215f2013-03-11 06:08:08 +0000183 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
Ajay Kumar45c480c2014-09-05 16:53:33 +0530184 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
Simon Glass77f9b1f2014-11-12 22:42:21 -0700185 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
Bin Mengc89ada02015-02-05 23:42:26 +0800186 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
Marek Vasut6ab00db2015-07-25 19:33:56 +0200187 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
Marek Vasut129adf52015-07-25 10:48:14 +0200188 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
Marek Vasutef4b01b2015-12-05 19:28:44 +0100189 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
Andrew Bradfordf3b84a32015-08-07 08:36:35 -0400190 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
191 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
Bin Meng394e0b62015-12-11 02:55:44 -0800192 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
Boris Brezillon4ccae812016-06-15 21:09:23 +0200193 COMPAT_SUNXI_NAND, /* SUNXI NAND controller */
Ley Foon Tane11b5e82017-04-05 17:32:47 +0800194 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
195 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
196 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
197 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
198 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
199 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
200 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
201 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
Tien Fong Cheeeb57c0b2017-09-25 16:40:03 +0800202 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
203 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
Marek Vasut19c8fc72018-05-12 11:56:10 +0200204 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
Simon Glassb5220bc2011-10-24 19:15:32 +0000205
206 COMPAT_COUNT,
207};
208
Simon Glass57068a72015-01-05 20:05:26 -0700209#define MAX_PHANDLE_ARGS 16
210struct fdtdec_phandle_args {
211 int node;
212 int args_count;
213 uint32_t args[MAX_PHANDLE_ARGS];
214};
215
216/**
217 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
218 *
219 * This function is useful to parse lists of phandles and their arguments.
220 *
221 * Example:
222 *
223 * phandle1: node1 {
224 * #list-cells = <2>;
225 * }
226 *
227 * phandle2: node2 {
228 * #list-cells = <1>;
229 * }
230 *
231 * node3 {
232 * list = <&phandle1 1 2 &phandle2 3>;
233 * }
234 *
235 * To get a device_node of the `node2' node you may call this:
236 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
237 * &args);
238 *
239 * (This function is a modified version of __of_parse_phandle_with_args() from
240 * Linux 3.18)
241 *
242 * @blob: Pointer to device tree
243 * @src_node: Offset of device tree node containing a list
244 * @list_name: property name that contains a list
245 * @cells_name: property name that specifies the phandles' arguments count,
246 * or NULL to use @cells_count
247 * @cells_count: Cell count to use if @cells_name is NULL
248 * @index: index of a phandle to parse out
249 * @out_args: optional pointer to output arguments structure (will be filled)
250 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
251 * @list_name does not exist, a phandle was not found, @cells_name
252 * could not be found, the arguments were truncated or there were too
253 * many arguments.
254 *
255 */
256int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
257 const char *list_name,
258 const char *cells_name,
259 int cell_count, int index,
260 struct fdtdec_phandle_args *out_args);
261
Sean Paul202ff752012-10-25 16:31:06 +0000262/**
Simon Glassb5220bc2011-10-24 19:15:32 +0000263 * Find the next numbered alias for a peripheral. This is used to enumerate
264 * all the peripherals of a certain type.
265 *
266 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
267 * this function will return a pointer to the node the alias points to, and
268 * then update *upto to 1. Next time you call this function, the next node
269 * will be returned.
270 *
271 * All nodes returned will match the compatible ID, as it is assumed that
272 * all peripherals use the same driver.
273 *
274 * @param blob FDT blob to use
275 * @param name Root name of alias to search for
276 * @param id Compatible ID to look for
277 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
278 */
279int fdtdec_next_alias(const void *blob, const char *name,
280 enum fdt_compat_id id, int *upto);
281
282/**
Gerald Van Baren7cde3972012-11-12 23:13:54 -0500283 * Find the compatible ID for a given node.
284 *
285 * Generally each node has at least one compatible string attached to it.
286 * This function looks through our list of known compatible strings and
287 * returns the corresponding ID which matches the compatible string.
288 *
289 * @param blob FDT blob to use
290 * @param node Node containing compatible string to find
291 * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
292 */
293enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
294
295/**
Simon Glassf88fe2d2012-02-27 10:52:34 +0000296 * Find the next compatible node for a peripheral.
297 *
298 * Do the first call with node = 0. This function will return a pointer to
299 * the next compatible node. Next time you call this function, pass the
300 * value returned, and the next node will be provided.
301 *
302 * @param blob FDT blob to use
303 * @param node Start node for search
304 * @param id Compatible ID to look for (enum fdt_compat_id)
305 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
306 */
307int fdtdec_next_compatible(const void *blob, int node,
308 enum fdt_compat_id id);
309
310/**
Simon Glass3ddecfc2012-04-02 13:18:42 +0000311 * Find the next compatible subnode for a peripheral.
312 *
313 * Do the first call with node set to the parent and depth = 0. This
314 * function will return the offset of the next compatible node. Next time
315 * you call this function, pass the node value returned last time, with
316 * depth unchanged, and the next node will be provided.
317 *
318 * @param blob FDT blob to use
319 * @param node Start node for search
320 * @param id Compatible ID to look for (enum fdt_compat_id)
321 * @param depthp Current depth (set to 0 before first call)
322 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
323 */
324int fdtdec_next_compatible_subnode(const void *blob, int node,
325 enum fdt_compat_id id, int *depthp);
326
Stephen Warren02464e32015-08-06 15:31:02 -0600327/*
328 * Look up an address property in a node and return the parsed address, and
329 * optionally the parsed size.
330 *
331 * This variant assumes a known and fixed number of cells are used to
332 * represent the address and size.
333 *
334 * You probably don't want to use this function directly except to parse
335 * non-standard properties, and never to parse the "reg" property. Instead,
336 * use one of the "auto" variants below, which automatically honor the
337 * #address-cells and #size-cells properties in the parent node.
338 *
339 * @param blob FDT blob
340 * @param node node to examine
341 * @param prop_name name of property to find
342 * @param index which address to retrieve from a list of addresses. Often 0.
343 * @param na the number of cells used to represent an address
344 * @param ns the number of cells used to represent a size
345 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren6e06acb2016-08-05 09:47:51 -0600346 * @param translate Indicates whether to translate the returned value
347 * using the parent node's ranges property.
Stephen Warren02464e32015-08-06 15:31:02 -0600348 * @return address, if found, or FDT_ADDR_T_NONE if not
349 */
350fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
351 const char *prop_name, int index, int na, int ns,
Stephen Warren6e06acb2016-08-05 09:47:51 -0600352 fdt_size_t *sizep, bool translate);
Stephen Warren02464e32015-08-06 15:31:02 -0600353
354/*
355 * Look up an address property in a node and return the parsed address, and
356 * optionally the parsed size.
357 *
358 * This variant automatically determines the number of cells used to represent
359 * the address and size by parsing the provided parent node's #address-cells
360 * and #size-cells properties.
361 *
362 * @param blob FDT blob
363 * @param parent parent node of @node
364 * @param node node to examine
365 * @param prop_name name of property to find
366 * @param index which address to retrieve from a list of addresses. Often 0.
367 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren6e06acb2016-08-05 09:47:51 -0600368 * @param translate Indicates whether to translate the returned value
369 * using the parent node's ranges property.
Stephen Warren02464e32015-08-06 15:31:02 -0600370 * @return address, if found, or FDT_ADDR_T_NONE if not
371 */
372fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
Stephen Warren6e06acb2016-08-05 09:47:51 -0600373 int node, const char *prop_name, int index, fdt_size_t *sizep,
374 bool translate);
Stephen Warren02464e32015-08-06 15:31:02 -0600375
376/*
377 * Look up an address property in a node and return the parsed address, and
378 * optionally the parsed size.
379 *
380 * This variant automatically determines the number of cells used to represent
381 * the address and size by parsing the parent node's #address-cells
382 * and #size-cells properties. The parent node is automatically found.
383 *
384 * The automatic parent lookup implemented by this function is slow.
385 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
386 * possible.
387 *
388 * @param blob FDT blob
389 * @param parent parent node of @node
390 * @param node node to examine
391 * @param prop_name name of property to find
392 * @param index which address to retrieve from a list of addresses. Often 0.
393 * @param sizep a pointer to store the size into. Use NULL if not required
Stephen Warren6e06acb2016-08-05 09:47:51 -0600394 * @param translate Indicates whether to translate the returned value
395 * using the parent node's ranges property.
Stephen Warren02464e32015-08-06 15:31:02 -0600396 * @return address, if found, or FDT_ADDR_T_NONE if not
397 */
398fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
Stephen Warren6e06acb2016-08-05 09:47:51 -0600399 const char *prop_name, int index, fdt_size_t *sizep,
400 bool translate);
Stephen Warren02464e32015-08-06 15:31:02 -0600401
402/*
403 * Look up an address property in a node and return the parsed address.
404 *
405 * This variant hard-codes the number of cells used to represent the address
406 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
407 * always returns the first address value in the property (index 0).
408 *
409 * Use of this function is not recommended due to the hard-coding of cell
410 * counts. There is no programmatic validation that these hard-coded values
411 * actually match the device tree content in any way at all. This assumption
412 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
413 * set in the U-Boot build and exercising strict control over DT content to
414 * ensure use of matching #address-cells/#size-cells properties. However, this
415 * approach is error-prone; those familiar with DT will not expect the
416 * assumption to exist, and could easily invalidate it. If the assumption is
417 * invalidated, this function will not report the issue, and debugging will
418 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
Simon Glassb5220bc2011-10-24 19:15:32 +0000419 *
420 * @param blob FDT blob
421 * @param node node to examine
422 * @param prop_name name of property to find
423 * @return address, if found, or FDT_ADDR_T_NONE if not
424 */
425fdt_addr_t fdtdec_get_addr(const void *blob, int node,
426 const char *prop_name);
427
Stephen Warren02464e32015-08-06 15:31:02 -0600428/*
429 * Look up an address property in a node and return the parsed address, and
430 * optionally the parsed size.
431 *
432 * This variant hard-codes the number of cells used to represent the address
433 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
434 * always returns the first address value in the property (index 0).
435 *
436 * Use of this function is not recommended due to the hard-coding of cell
437 * counts. There is no programmatic validation that these hard-coded values
438 * actually match the device tree content in any way at all. This assumption
439 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
440 * set in the U-Boot build and exercising strict control over DT content to
441 * ensure use of matching #address-cells/#size-cells properties. However, this
442 * approach is error-prone; those familiar with DT will not expect the
443 * assumption to exist, and could easily invalidate it. If the assumption is
444 * invalidated, this function will not report the issue, and debugging will
445 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
Simon Glass4397a2a2013-03-19 04:58:51 +0000446 *
447 * @param blob FDT blob
448 * @param node node to examine
449 * @param prop_name name of property to find
Stephen Warren02464e32015-08-06 15:31:02 -0600450 * @param sizep a pointer to store the size into. Use NULL if not required
Simon Glass4397a2a2013-03-19 04:58:51 +0000451 * @return address, if found, or FDT_ADDR_T_NONE if not
452 */
453fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
454 const char *prop_name, fdt_size_t *sizep);
455
456/**
Bin Menga62e84d2014-12-31 16:05:11 +0800457 * Look at the compatible property of a device node that represents a PCI
458 * device and extract pci vendor id and device id from it.
459 *
460 * @param blob FDT blob
461 * @param node node to examine
462 * @param vendor vendor id of the pci device
463 * @param device device id of the pci device
464 * @return 0 if ok, negative on error
465 */
466int fdtdec_get_pci_vendev(const void *blob, int node,
467 u16 *vendor, u16 *device);
468
469/**
470 * Look at the pci address of a device node that represents a PCI device
Bin Menga62e84d2014-12-31 16:05:11 +0800471 * and return base address of the pci device's registers.
472 *
Simon Glassfcc0a872015-11-29 13:17:54 -0700473 * @param dev device to examine
Bin Menga62e84d2014-12-31 16:05:11 +0800474 * @param addr pci address in the form of fdt_pci_addr
475 * @param bar returns base address of the pci device's registers
476 * @return 0 if ok, negative on error
477 */
Simon Glass194fca92020-01-27 08:49:38 -0700478int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr,
Simon Glassfcc0a872015-11-29 13:17:54 -0700479 u32 *bar);
Bin Menga62e84d2014-12-31 16:05:11 +0800480
481/**
Suneel Garapati1db7ee42019-10-19 15:19:35 -0700482 * Look at the bus range property of a device node and return the pci bus
483 * range for this node.
484 * The property must hold one fdt_pci_addr with a length.
485 * @param blob FDT blob
486 * @param node node to examine
487 * @param res the resource structure to return the bus range
488 * @return 0 if ok, negative on error
489 */
490
491int fdtdec_get_pci_bus_range(const void *blob, int node,
492 struct fdt_resource *res);
493
494/**
Simon Glassb5220bc2011-10-24 19:15:32 +0000495 * Look up a 32-bit integer property in a node and return it. The property
496 * must have at least 4 bytes of data. The value of the first cell is
497 * returned.
498 *
499 * @param blob FDT blob
500 * @param node node to examine
501 * @param prop_name name of property to find
502 * @param default_val default value to return if the property is not found
503 * @return integer value, if found, or default_val if not
504 */
505s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
506 s32 default_val);
507
508/**
Chin Liang Seebfa3e552015-10-17 08:30:32 -0500509 * Unsigned version of fdtdec_get_int. The property must have at least
510 * 4 bytes of data. The value of the first cell is returned.
511 *
512 * @param blob FDT blob
513 * @param node node to examine
514 * @param prop_name name of property to find
515 * @param default_val default value to return if the property is not found
516 * @return unsigned integer value, if found, or default_val if not
517 */
518unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
519 unsigned int default_val);
520
521/**
Simon Glass5f7bfdd2015-03-05 12:25:14 -0700522 * Get a variable-sized number from a property
523 *
524 * This reads a number from one or more cells.
525 *
526 * @param ptr Pointer to property
527 * @param cells Number of cells containing the number
528 * @return the value in the cells
529 */
530u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
531
532/**
Che-Liang Chiouaadef0a2012-10-25 16:31:05 +0000533 * Look up a 64-bit integer property in a node and return it. The property
534 * must have at least 8 bytes of data (2 cells). The first two cells are
535 * concatenated to form a 8 bytes value, where the first cell is top half and
536 * the second cell is bottom half.
537 *
538 * @param blob FDT blob
539 * @param node node to examine
540 * @param prop_name name of property to find
541 * @param default_val default value to return if the property is not found
542 * @return integer value, if found, or default_val if not
543 */
544uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
545 uint64_t default_val);
546
547/**
Simon Glassb5220bc2011-10-24 19:15:32 +0000548 * Checks whether a node is enabled.
549 * This looks for a 'status' property. If this exists, then returns 1 if
550 * the status is 'ok' and 0 otherwise. If there is no status property,
Simon Glassf88fe2d2012-02-27 10:52:34 +0000551 * it returns 1 on the assumption that anything mentioned should be enabled
552 * by default.
Simon Glassb5220bc2011-10-24 19:15:32 +0000553 *
554 * @param blob FDT blob
555 * @param node node to examine
Simon Glassf88fe2d2012-02-27 10:52:34 +0000556 * @return integer value 0 (not enabled) or 1 (enabled)
Simon Glassb5220bc2011-10-24 19:15:32 +0000557 */
Simon Glassf88fe2d2012-02-27 10:52:34 +0000558int fdtdec_get_is_enabled(const void *blob, int node);
Simon Glassb5220bc2011-10-24 19:15:32 +0000559
560/**
Simon Glass9a263e52012-03-28 10:08:24 +0000561 * Make sure we have a valid fdt available to control U-Boot.
562 *
563 * If not, a message is printed to the console if the console is ready.
564 *
565 * @return 0 if all ok, -1 if not
566 */
567int fdtdec_prepare_fdt(void);
568
569/**
570 * Checks that we have a valid fdt available to control U-Boot.
571
572 * However, if not then for the moment nothing is done, since this function
573 * is called too early to panic().
574 *
575 * @returns 0
Simon Glassb5220bc2011-10-24 19:15:32 +0000576 */
577int fdtdec_check_fdt(void);
Simon Glassa53f4a22012-01-17 08:20:50 +0000578
579/**
580 * Find the nodes for a peripheral and return a list of them in the correct
581 * order. This is used to enumerate all the peripherals of a certain type.
582 *
583 * To use this, optionally set up a /aliases node with alias properties for
584 * a peripheral. For example, for usb you could have:
585 *
586 * aliases {
587 * usb0 = "/ehci@c5008000";
588 * usb1 = "/ehci@c5000000";
589 * };
590 *
591 * Pass "usb" as the name to this function and will return a list of two
592 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
593 *
594 * All nodes returned will match the compatible ID, as it is assumed that
595 * all peripherals use the same driver.
596 *
597 * If no alias node is found, then the node list will be returned in the
598 * order found in the fdt. If the aliases mention a node which doesn't
599 * exist, then this will be ignored. If nodes are found with no aliases,
600 * they will be added in any order.
601 *
602 * If there is a gap in the aliases, then this function return a 0 node at
603 * that position. The return value will also count these gaps.
604 *
605 * This function checks node properties and will not return nodes which are
606 * marked disabled (status = "disabled").
607 *
608 * @param blob FDT blob to use
609 * @param name Root name of alias to search for
610 * @param id Compatible ID to look for
611 * @param node_list Place to put list of found nodes
612 * @param maxcount Maximum number of nodes to find
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400613 * @return number of nodes found on success, FDT_ERR_... on error
Simon Glassa53f4a22012-01-17 08:20:50 +0000614 */
615int fdtdec_find_aliases_for_id(const void *blob, const char *name,
616 enum fdt_compat_id id, int *node_list, int maxcount);
617
618/*
Simon Glassc6782272012-02-03 15:13:53 +0000619 * This function is similar to fdtdec_find_aliases_for_id() except that it
620 * adds to the node_list that is passed in. Any 0 elements are considered
621 * available for allocation - others are considered already used and are
622 * skipped.
623 *
624 * You can use this by calling fdtdec_find_aliases_for_id() with an
625 * uninitialised array, then setting the elements that are returned to -1,
626 * say, then calling this function, perhaps with a different compat id.
627 * Any elements you get back that are >0 are new nodes added by the call
628 * to this function.
629 *
630 * Note that if you have some nodes with aliases and some without, you are
631 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
632 * one compat_id may fill in positions for which you have aliases defined
633 * for another compat_id. When you later call *this* function with the second
634 * compat_id, the alias positions may already be used. A debug warning may
635 * be generated in this case, but it is safest to define aliases for all
636 * nodes when you care about the ordering.
637 */
638int fdtdec_add_aliases_for_id(const void *blob, const char *name,
639 enum fdt_compat_id id, int *node_list, int maxcount);
640
Simon Glass5c33c9f2014-07-23 06:55:09 -0600641/**
642 * Get the alias sequence number of a node
643 *
644 * This works out whether a node is pointed to by an alias, and if so, the
645 * sequence number of that alias. Aliases are of the form <base><num> where
646 * <num> is the sequence number. For example spi2 would be sequence number
647 * 2.
648 *
649 * @param blob Device tree blob (if NULL, then error is returned)
650 * @param base Base name for alias (before the underscore)
651 * @param node Node to look up
652 * @param seqp This is set to the sequence number if one is found,
653 * but otherwise the value is left alone
654 * @return 0 if a sequence was found, -ve if not
655 */
656int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
657 int *seqp);
658
Simon Glass3234aa42014-07-23 06:55:16 -0600659/**
Michal Simek003c9dc2019-01-31 16:30:58 +0100660 * Get the highest alias number for susbystem.
661 *
662 * It parses all aliases and find out highest recorded alias for subsystem.
663 * Aliases are of the form <base><num> where <num> is the sequence number.
664 *
665 * @param blob Device tree blob (if NULL, then error is returned)
666 * @param base Base name for alias susbystem (before the number)
667 *
668 * @return 0 highest alias ID, -1 if not found
669 */
670int fdtdec_get_alias_highest_id(const void *blob, const char *base);
671
672/**
Simon Glass3bc37a52015-10-17 19:41:14 -0600673 * Get a property from the /chosen node
674 *
675 * @param blob Device tree blob (if NULL, then NULL is returned)
676 * @param name Property name to look up
677 * @return Value of property, or NULL if it does not exist
678 */
679const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
680
681/**
682 * Get the offset of the given /chosen node
Simon Glassaac07d42014-09-04 16:27:24 -0600683 *
684 * This looks up a property in /chosen containing the path to another node,
685 * then finds the offset of that node.
686 *
687 * @param blob Device tree blob (if NULL, then error is returned)
688 * @param name Property name, e.g. "stdout-path"
689 * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
690 */
691int fdtdec_get_chosen_node(const void *blob, const char *name);
692
Simon Glassc6782272012-02-03 15:13:53 +0000693/*
Simon Glassa53f4a22012-01-17 08:20:50 +0000694 * Get the name for a compatible ID
695 *
696 * @param id Compatible ID to look for
697 * @return compatible string for that id
698 */
699const char *fdtdec_get_compatible(enum fdt_compat_id id);
Simon Glassd17da652012-02-27 10:52:35 +0000700
701/* Look up a phandle and follow it to its node. Then return the offset
702 * of that node.
703 *
704 * @param blob FDT blob
705 * @param node node to examine
706 * @param prop_name name of property to find
707 * @return node offset if found, -ve error code on error
708 */
709int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
710
711/**
712 * Look up a property in a node and return its contents in an integer
713 * array of given length. The property must have at least enough data for
714 * the array (4*count bytes). It may have more, but this will be ignored.
715 *
716 * @param blob FDT blob
717 * @param node node to examine
718 * @param prop_name name of property to find
719 * @param array array to fill with data
720 * @param count number of array elements
721 * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
722 * or -FDT_ERR_BADLAYOUT if not enough data
723 */
724int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
725 u32 *array, int count);
726
727/**
Simon Glassa9f04d42014-11-10 18:00:19 -0700728 * Look up a property in a node and return its contents in an integer
729 * array of given length. The property must exist but may have less data that
730 * expected (4*count bytes). It may have more, but this will be ignored.
731 *
732 * @param blob FDT blob
733 * @param node node to examine
734 * @param prop_name name of property to find
735 * @param array array to fill with data
736 * @param count number of array elements
737 * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
738 * property is not found
739 */
740int fdtdec_get_int_array_count(const void *blob, int node,
741 const char *prop_name, u32 *array, int count);
742
743/**
Simon Glass96875e72012-04-02 13:18:41 +0000744 * Look up a property in a node and return a pointer to its contents as a
745 * unsigned int array of given length. The property must have at least enough
746 * data for the array ('count' cells). It may have more, but this will be
747 * ignored. The data is not copied.
748 *
749 * Note that you must access elements of the array with fdt32_to_cpu(),
750 * since the elements will be big endian even on a little endian machine.
751 *
752 * @param blob FDT blob
753 * @param node node to examine
754 * @param prop_name name of property to find
755 * @param count number of array elements
756 * @return pointer to array if found, or NULL if the property is not
757 * found or there is not enough data
758 */
759const u32 *fdtdec_locate_array(const void *blob, int node,
760 const char *prop_name, int count);
761
762/**
Simon Glassd17da652012-02-27 10:52:35 +0000763 * Look up a boolean property in a node and return it.
764 *
765 * A boolean properly is true if present in the device tree and false if not
766 * present, regardless of its value.
767 *
768 * @param blob FDT blob
769 * @param node node to examine
770 * @param prop_name name of property to find
771 * @return 1 if the properly is present; 0 if it isn't present
772 */
773int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
Simon Glassed3ee5c2012-02-27 10:52:36 +0000774
Peng Fan1889a7e2016-02-01 13:31:15 +0800775/*
776 * Count child nodes of one parent node.
777 *
778 * @param blob FDT blob
779 * @param node parent node
780 * @return number of child node; 0 if there is not child node
781 */
782int fdtdec_get_child_count(const void *blob, int node);
783
Anton Staffbed4d892012-04-17 09:01:28 +0000784/*
785 * Look up a property in a node and return its contents in a byte
786 * array of given length. The property must have at least enough data for
787 * the array (count bytes). It may have more, but this will be ignored.
788 *
789 * @param blob FDT blob
790 * @param node node to examine
791 * @param prop_name name of property to find
792 * @param array array to fill with data
793 * @param count number of array elements
794 * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
795 * or -FDT_ERR_BADLAYOUT if not enough data
796 */
797int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
798 u8 *array, int count);
799
800/**
801 * Look up a property in a node and return a pointer to its contents as a
802 * byte array of given length. The property must have at least enough data
803 * for the array (count bytes). It may have more, but this will be ignored.
804 * The data is not copied.
805 *
806 * @param blob FDT blob
807 * @param node node to examine
808 * @param prop_name name of property to find
809 * @param count number of array elements
810 * @return pointer to byte array if found, or NULL if the property is not
811 * found or there is not enough data
812 */
813const u8 *fdtdec_locate_byte_array(const void *blob, int node,
814 const char *prop_name, int count);
Simon Glassf20c4612012-10-25 16:31:00 +0000815
816/**
Thierry Reding56f42242014-08-26 17:33:53 +0200817 * Obtain an indexed resource from a device property.
818 *
819 * @param fdt FDT blob
820 * @param node node to examine
821 * @param property name of the property to parse
822 * @param index index of the resource to retrieve
823 * @param res returns the resource
824 * @return 0 if ok, negative on error
825 */
826int fdt_get_resource(const void *fdt, int node, const char *property,
827 unsigned int index, struct fdt_resource *res);
828
829/**
830 * Obtain a named resource from a device property.
831 *
832 * Look up the index of the name in a list of strings and return the resource
833 * at that index.
834 *
835 * @param fdt FDT blob
836 * @param node node to examine
837 * @param property name of the property to parse
838 * @param prop_names name of the property containing the list of names
839 * @param name the name of the entry to look up
840 * @param res returns the resource
841 */
842int fdt_get_named_resource(const void *fdt, int node, const char *property,
843 const char *prop_names, const char *name,
844 struct fdt_resource *res);
845
Simon Glass12e67112015-04-14 21:03:21 -0600846/* Display timings from linux include/video/display_timing.h */
847enum display_flags {
848 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
849 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
850 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
851 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
852
853 /* data enable flag */
854 DISPLAY_FLAGS_DE_LOW = 1 << 4,
855 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
856 /* drive data on pos. edge */
857 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
858 /* drive data on neg. edge */
859 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
860 DISPLAY_FLAGS_INTERLACED = 1 << 8,
861 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
862 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
863};
864
865/*
866 * A single signal can be specified via a range of minimal and maximal values
867 * with a typical value, that lies somewhere inbetween.
868 */
869struct timing_entry {
870 u32 min;
871 u32 typ;
872 u32 max;
873};
874
875/*
876 * Single "mode" entry. This describes one set of signal timings a display can
877 * have in one setting. This struct can later be converted to struct videomode
878 * (see include/video/videomode.h). As each timing_entry can be defined as a
879 * range, one struct display_timing may become multiple struct videomodes.
880 *
881 * Example: hsync active high, vsync active low
882 *
883 * Active Video
884 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
885 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
886 * | | porch | | porch |
887 *
888 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
889 *
890 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
891 */
892struct display_timing {
893 struct timing_entry pixelclock;
894
895 struct timing_entry hactive; /* hor. active video */
896 struct timing_entry hfront_porch; /* hor. front porch */
897 struct timing_entry hback_porch; /* hor. back porch */
898 struct timing_entry hsync_len; /* hor. sync len */
899
900 struct timing_entry vactive; /* ver. active video */
901 struct timing_entry vfront_porch; /* ver. front porch */
902 struct timing_entry vback_porch; /* ver. back porch */
903 struct timing_entry vsync_len; /* ver. sync len */
904
905 enum display_flags flags; /* display flags */
Jernej Skrabec43c6bdd2017-04-29 14:43:36 +0200906 bool hdmi_monitor; /* is hdmi monitor? */
Simon Glass12e67112015-04-14 21:03:21 -0600907};
908
909/**
910 * fdtdec_decode_display_timing() - decode display timings
911 *
912 * Decode display timings from the supplied 'display-timings' node.
913 * See doc/device-tree-bindings/video/display-timing.txt for binding
914 * information.
915 *
916 * @param blob FDT blob
917 * @param node 'display-timing' node containing the timing subnodes
918 * @param index Index number to read (0=first timing subnode)
919 * @param config Place to put timings
920 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
921 */
922int fdtdec_decode_display_timing(const void *blob, int node, int index,
923 struct display_timing *config);
Nathan Rossi623f6012016-12-19 00:03:34 +1000924
925/**
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530926 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
927 * gd->ram_start
Nathan Rossi623f6012016-12-19 00:03:34 +1000928 *
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530929 * Decode the /memory 'reg' property to determine the size and start of the
930 * first memory bank, populate the global data with the size and start of the
931 * first bank of memory.
Nathan Rossi623f6012016-12-19 00:03:34 +1000932 *
933 * This function should be called from a boards dram_init(). This helper
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530934 * function allows for boards to query the device tree for DRAM size and start
935 * address instead of hard coding the value in the case where the memory size
936 * and start address cannot be detected automatically.
Nathan Rossi623f6012016-12-19 00:03:34 +1000937 *
938 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
939 * invalid
940 */
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530941int fdtdec_setup_mem_size_base(void);
Nathan Rossi623f6012016-12-19 00:03:34 +1000942
943/**
Michal Simek7fce7392020-07-09 14:09:52 +0200944 * fdtdec_setup_mem_size_base_lowest() - decode and setup gd->ram_size and
945 * gd->ram_start by lowest available memory base
946 *
947 * Decode the /memory 'reg' property to determine the lowest start of the memory
948 * bank bank and populate the global data with it.
949 *
950 * This function should be called from a boards dram_init(). This helper
951 * function allows for boards to query the device tree for DRAM size and start
952 * address instead of hard coding the value in the case where the memory size
953 * and start address cannot be detected automatically.
954 *
955 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
956 * invalid
957 */
958int fdtdec_setup_mem_size_base_lowest(void);
959
960/**
Nathan Rossi623f6012016-12-19 00:03:34 +1000961 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
962 *
963 * Decode the /memory 'reg' property to determine the address and size of the
964 * memory banks. Use this data to populate the global data board info with the
965 * phys address and size of memory banks.
966 *
967 * This function should be called from a boards dram_init_banksize(). This
968 * helper function allows for boards to query the device tree for memory bank
969 * information instead of hard coding the information in cases where it cannot
970 * be detected automatically.
971 *
972 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
973 * invalid
974 */
975int fdtdec_setup_memory_banksize(void);
976
Simon Glassb45122f2015-02-27 22:06:34 -0700977/**
Thierry Redingebf30e82019-04-15 11:32:13 +0200978 * fdtdec_set_ethernet_mac_address() - set MAC address for default interface
979 *
980 * Looks up the default interface via the "ethernet" alias (in the /aliases
981 * node) and stores the given MAC in its "local-mac-address" property. This
982 * is useful on platforms that store the MAC address in a custom location.
983 * Board code can call this in the late init stage to make sure that the
984 * interface device tree node has the right MAC address configured for the
985 * Ethernet uclass to pick it up.
986 *
987 * Typically the FDT passed into this function will be U-Boot's control DTB.
988 * Given that a lot of code may be holding offsets to various nodes in that
989 * tree, this code will only set the "local-mac-address" property in-place,
990 * which means that it needs to exist and have space for the 6-byte address.
991 * This ensures that the operation is non-destructive and does not invalidate
992 * offsets that other drivers may be using.
993 *
994 * @param fdt FDT blob
995 * @param mac buffer containing the MAC address to set
996 * @param size size of MAC address
997 * @return 0 on success or a negative error code on failure
998 */
999int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size);
1000
1001/**
Thierry Reding8153d532019-03-21 19:10:01 +01001002 * fdtdec_set_phandle() - sets the phandle of a given node
1003 *
1004 * @param blob FDT blob
1005 * @param node offset in the FDT blob of the node whose phandle is to
1006 * be set
1007 * @param phandle phandle to set for the given node
1008 * @return 0 on success or a negative error code on failure
1009 */
Thierry Redingd81d9692019-04-15 10:08:20 +02001010static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
1011{
1012 return fdt_setprop_u32(blob, node, "phandle", phandle);
1013}
Thierry Reding8153d532019-03-21 19:10:01 +01001014
Thierry Redingb9aad372021-09-03 15:16:21 +02001015/* add "no-map" property */
1016#define FDTDEC_RESERVED_MEMORY_NO_MAP (1 << 0)
1017
Thierry Reding8153d532019-03-21 19:10:01 +01001018/**
Thierry Redingc9222a02019-03-21 19:10:02 +01001019 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
1020 *
1021 * If a reserved-memory node already exists for the given carveout, a phandle
1022 * for that node will be returned. Otherwise a new node will be created and a
1023 * phandle corresponding to it will be returned.
1024 *
1025 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1026 * for details on how to use reserved memory regions.
1027 *
1028 * As an example, consider the following code snippet:
1029 *
1030 * struct fdt_memory fb = {
1031 * .start = 0x92cb3000,
1032 * .end = 0x934b2fff,
1033 * };
1034 * uint32_t phandle;
1035 *
Thierry Reding46cb0672021-09-03 15:16:19 +02001036 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, NULL, 0, &phandle,
Thierry Redingb9aad372021-09-03 15:16:21 +02001037 * 0);
Thierry Redingc9222a02019-03-21 19:10:02 +01001038 *
1039 * This results in the following subnode being added to the top-level
1040 * /reserved-memory node:
1041 *
1042 * reserved-memory {
1043 * #address-cells = <0x00000002>;
1044 * #size-cells = <0x00000002>;
1045 * ranges;
1046 *
1047 * framebuffer@92cb3000 {
1048 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1049 * phandle = <0x0000004d>;
1050 * };
1051 * };
1052 *
1053 * If the top-level /reserved-memory node does not exist, it will be created.
1054 * The phandle returned from the function call can be used to reference this
1055 * reserved memory region from other nodes.
1056 *
Thierry Reding16523ac2019-03-21 19:10:03 +01001057 * See fdtdec_set_carveout() for a more elaborate example.
1058 *
Thierry Redingc9222a02019-03-21 19:10:02 +01001059 * @param blob FDT blob
1060 * @param basename base name of the node to create
1061 * @param carveout information about the carveout region
Thierry Reding46cb0672021-09-03 15:16:19 +02001062 * @param compatibles list of compatible strings for the carveout region
1063 * @param count number of compatible strings for the carveout region
Thierry Redingc9222a02019-03-21 19:10:02 +01001064 * @param phandlep return location for the phandle of the carveout region
Heiko Stuebner357d2ce2019-10-23 16:46:39 +02001065 * can be NULL if no phandle should be added
Thierry Redingb9aad372021-09-03 15:16:21 +02001066 * @param flags bitmask of flags to set for the carveout region
Thierry Redingc9222a02019-03-21 19:10:02 +01001067 * @return 0 on success or a negative error code on failure
1068 */
1069int fdtdec_add_reserved_memory(void *blob, const char *basename,
1070 const struct fdt_memory *carveout,
Thierry Reding46cb0672021-09-03 15:16:19 +02001071 const char **compatibles, unsigned int count,
Thierry Redingb9aad372021-09-03 15:16:21 +02001072 uint32_t *phandlep, unsigned long flags);
Thierry Redingc9222a02019-03-21 19:10:02 +01001073
1074/**
Thierry Reding16523ac2019-03-21 19:10:03 +01001075 * fdtdec_get_carveout() - reads a carveout from an FDT
1076 *
1077 * Reads information about a carveout region from an FDT. The carveout is a
1078 * referenced by its phandle that is read from a given property in a given
1079 * node.
1080 *
1081 * @param blob FDT blob
1082 * @param node name of a node
Thierry Reding4bf88ba2021-09-03 15:16:18 +02001083 * @param prop_name name of the property in the given node that contains
Thierry Reding16523ac2019-03-21 19:10:03 +01001084 * the phandle for the carveout
1085 * @param index index of the phandle for which to read the carveout
1086 * @param carveout return location for the carveout information
Thierry Reding4bf88ba2021-09-03 15:16:18 +02001087 * @param name return location for the carveout name
Thierry Reding46cb0672021-09-03 15:16:19 +02001088 * @param compatiblesp return location for compatible strings
Thierry Redingb9aad372021-09-03 15:16:21 +02001089 * @param countp return location for the number of compatible strings
1090 * @param flags return location for the flags of the carveout
Thierry Reding16523ac2019-03-21 19:10:03 +01001091 * @return 0 on success or a negative error code on failure
1092 */
Thierry Reding4bf88ba2021-09-03 15:16:18 +02001093int fdtdec_get_carveout(const void *blob, const char *node,
1094 const char *prop_name, unsigned int index,
Thierry Reding46cb0672021-09-03 15:16:19 +02001095 struct fdt_memory *carveout, const char **name,
Thierry Redingb9aad372021-09-03 15:16:21 +02001096 const char ***compatiblesp, unsigned int *countp,
1097 unsigned long *flags);
Thierry Reding16523ac2019-03-21 19:10:03 +01001098
1099/**
1100 * fdtdec_set_carveout() - sets a carveout region for a given node
1101 *
1102 * Sets a carveout region for a given node. If a reserved-memory node already
1103 * exists for the carveout, the phandle for that node will be reused. If no
1104 * such node exists, a new one will be created and a phandle to it stored in
1105 * a specified property of the given node.
1106 *
1107 * As an example, consider the following code snippet:
1108 *
1109 * const char *node = "/host1x@50000000/dc@54240000";
1110 * struct fdt_memory fb = {
1111 * .start = 0x92cb3000,
1112 * .end = 0x934b2fff,
1113 * };
1114 *
Thierry Reding46cb0672021-09-03 15:16:19 +02001115 * fdtdec_set_carveout(fdt, node, "memory-region", 0, "framebuffer", NULL,
Thierry Redingb9aad372021-09-03 15:16:21 +02001116 * 0, &fb, 0);
Thierry Reding16523ac2019-03-21 19:10:03 +01001117 *
1118 * dc@54200000 is a display controller and was set up by the bootloader to
1119 * scan out the framebuffer specified by "fb". This would cause the following
1120 * reserved memory region to be added:
1121 *
1122 * reserved-memory {
1123 * #address-cells = <0x00000002>;
1124 * #size-cells = <0x00000002>;
1125 * ranges;
1126 *
1127 * framebuffer@92cb3000 {
1128 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1129 * phandle = <0x0000004d>;
1130 * };
1131 * };
1132 *
1133 * A "memory-region" property will also be added to the node referenced by the
1134 * offset parameter.
1135 *
1136 * host1x@50000000 {
1137 * ...
1138 *
1139 * dc@54240000 {
1140 * ...
1141 * memory-region = <0x0000004d>;
1142 * ...
1143 * };
1144 *
1145 * ...
1146 * };
1147 *
1148 * @param blob FDT blob
1149 * @param node name of the node to add the carveout to
1150 * @param prop_name name of the property in which to store the phandle of
1151 * the carveout
1152 * @param index index of the phandle to store
Thierry Reding16523ac2019-03-21 19:10:03 +01001153 * @param carveout information about the carveout to add
Thierry Reding90194872021-09-03 15:16:20 +02001154 * @param name base name of the reserved-memory node to create
Thierry Reding46cb0672021-09-03 15:16:19 +02001155 * @param compatibles compatible strings to set for the carveout
1156 * @param count number of compatible strings
Thierry Redingb9aad372021-09-03 15:16:21 +02001157 * @param flags bitmask of flags to set for the carveout
Thierry Reding16523ac2019-03-21 19:10:03 +01001158 * @return 0 on success or a negative error code on failure
1159 */
1160int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
Thierry Reding90194872021-09-03 15:16:20 +02001161 unsigned int index, const struct fdt_memory *carveout,
1162 const char *name, const char **compatibles,
Thierry Redingb9aad372021-09-03 15:16:21 +02001163 unsigned int count, unsigned long flags);
Thierry Reding16523ac2019-03-21 19:10:03 +01001164
1165/**
Simon Glassb45122f2015-02-27 22:06:34 -07001166 * Set up the device tree ready for use
1167 */
Simon Glass08793612015-02-27 22:06:35 -07001168int fdtdec_setup(void);
Simon Glassb45122f2015-02-27 22:06:34 -07001169
Marek Vasut0e2afc82020-04-11 21:18:59 +02001170/**
1171 * Perform board-specific early DT adjustments
1172 */
1173int fdtdec_board_setup(const void *fdt_blob);
1174
Jean-Jacques Hiblotf1d2bc92018-12-07 14:50:52 +01001175#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1176/**
1177 * fdtdec_resetup() - Set up the device tree again
1178 *
1179 * The main difference with fdtdec_setup() is that it returns if the fdt has
1180 * changed because a better match has been found.
1181 * This is typically used for boards that rely on a DM driver to detect the
1182 * board type. This function sould be called by the board code after the stuff
1183 * needed by board_fit_config_name_match() to operate porperly is available.
1184 * If this functions signals that a rescan is necessary, the board code must
1185 * unbind all the drivers using dm_uninit() and then rescan the DT with
1186 * dm_init_and_scan().
1187 *
1188 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1189 * the fdt is required
1190 *
1191 * @return 0 if OK, -ve on error
1192 */
1193int fdtdec_resetup(int *rescan);
1194#endif
1195
Alex Deymo82f766d2017-04-02 01:25:20 -07001196/**
1197 * Board-specific FDT initialization. Returns the address to a device tree blob.
Simon Glass98550342021-12-16 20:59:31 -07001198 *
1199 * Called when CONFIG_OF_BOARD is defined.
1200 *
1201 * The existing devicetree is available at gd->fdt_blob
Ilias Apalodimase7fb7892021-10-26 09:12:33 +03001202 *
1203 * @err internal error code if we fail to setup a DTB
Simon Glass98550342021-12-16 20:59:31 -07001204 * @returns new devicetree blob pointer
Alex Deymo82f766d2017-04-02 01:25:20 -07001205 */
Ilias Apalodimase7fb7892021-10-26 09:12:33 +03001206void *board_fdt_blob_setup(int *err);
Michael Pratt90c08fa2018-06-11 13:07:09 -06001207
1208/*
1209 * Decode the size of memory
1210 *
1211 * RAM size is normally set in a /memory node and consists of a list of
1212 * (base, size) cells in the 'reg' property. This information is used to
1213 * determine the total available memory as well as the address and size
1214 * of each bank.
1215 *
1216 * Optionally the memory configuration can vary depending on a board id,
1217 * typically read from strapping resistors or an EEPROM on the board.
1218 *
1219 * Finally, memory size can be detected (within certain limits) by probing
1220 * the available memory. It is safe to do so within the limits provides by
1221 * the board's device tree information. This makes it possible to produce
1222 * boards with different memory sizes, where the device tree specifies the
1223 * maximum memory configuration, and the smaller memory configuration is
1224 * probed.
1225 *
1226 * This function decodes that information, returning the memory base address,
1227 * size and bank information. See the memory.txt binding for full
1228 * documentation.
1229 *
1230 * @param blob Device tree blob
1231 * @param area Name of node to check (NULL means "/memory")
1232 * @param board_id Board ID to look up
1233 * @param basep Returns base address of first memory bank (NULL to
1234 * ignore)
1235 * @param sizep Returns total memory size (NULL to ignore)
1236 * @param bd Updated with the memory bank information (NULL to skip)
1237 * @return 0 if OK, -ve on error
1238 */
1239int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1240 phys_addr_t *basep, phys_size_t *sizep,
1241 struct bd_info *bd);
Alex Deymo82f766d2017-04-02 01:25:20 -07001242
Simon Glass39605c62021-12-16 20:59:33 -07001243/**
1244 * fdtdec_get_srcname() - Get the name of where the devicetree comes from
1245 *
1246 * @return source name
1247 */
1248const char *fdtdec_get_srcname(void);
1249
Simon Glass5bfa78d2012-07-12 05:25:02 +00001250#endif