blob: 891b274aa3c1c7eb14b51ba3c46008f1f424524a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2d2af852011-10-24 19:15:32 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass2d2af852011-10-24 19:15:32 +00004 */
5
Heiko Schocherd7b42322014-03-03 12:19:30 +01006#ifndef USE_HOSTCC
Simon Glass2d2af852011-10-24 19:15:32 +00007#include <common.h>
Jean-Jacques Hiblot5d96cab2017-09-15 12:57:31 +02008#include <boot_fit.h>
Simon Glass1ab16922022-07-31 12:28:48 -06009#include <display_options.h>
Simon Glassf1037ad2015-11-29 13:17:54 -070010#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -070011#include <hang.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -070012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Simon Glass84137372022-12-21 16:08:19 -070016#include <spl.h>
Simon Glass6eaea252019-08-01 09:46:48 -060017#include <env.h>
Simon Glass9908ad42014-07-23 06:55:09 -060018#include <errno.h>
Simon Glass2d2af852011-10-24 19:15:32 +000019#include <fdtdec.h>
Jean-Jacques Hiblot5d96cab2017-09-15 12:57:31 +020020#include <fdt_support.h>
Simon Glass1a974af2019-08-01 09:46:36 -060021#include <gzip.h>
Heinrich Schuchardt370a1762018-11-18 17:58:50 +010022#include <mapmem.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090023#include <linux/libfdt.h>
Jean-Jacques Hiblot5d96cab2017-09-15 12:57:31 +020024#include <serial.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060025#include <asm/global_data.h>
Simon Glass26b78b22015-02-27 22:06:34 -070026#include <asm/sections.h>
Simon Glassba8457b2022-09-06 20:27:19 -060027#include <dm/ofnode.h>
28#include <dm/of_extra.h>
Simon Glass9908ad42014-07-23 06:55:09 -060029#include <linux/ctype.h>
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +020030#include <linux/lzo.h>
Michal Simek6dd9cfd2020-07-10 13:34:44 +020031#include <linux/ioport.h>
Simon Glass2d2af852011-10-24 19:15:32 +000032
33DECLARE_GLOBAL_DATA_PTR;
34
35/*
36 * Here are the type we know about. One day we might allow drivers to
37 * register. For now we just put them here. The COMPAT macro allows us to
38 * turn this into a sparse list later, and keeps the ID with the name.
Simon Glass7c380302016-06-19 17:33:13 -060039 *
40 * NOTE: This list is basically a TODO list for things that need to be
41 * converted to driver model. So don't add new things here unless there is a
42 * good reason why driver-model conversion is infeasible. Examples include
43 * things which are used before driver model is available.
Simon Glass2d2af852011-10-24 19:15:32 +000044 */
45#define COMPAT(id, name) name
46static const char * const compat_names[COMPAT_COUNT] = {
Simon Glass65444982012-02-27 10:52:34 +000047 COMPAT(UNKNOWN, "<none>"),
Jimmy Zhang5983ef82012-04-02 13:18:52 +000048 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
49 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
Jim Lin5d309e62012-07-29 20:53:29 +000050 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
Thierry Redingf202e022014-12-09 22:25:09 -070051 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
Tom Warrenab0cc6b2015-03-04 16:36:00 -070052 COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
Rajeshwari Shinded6e9f442013-01-07 23:35:05 +000053 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
Vivek Gautamf8745232013-09-14 14:02:48 +053054 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
Akshay Saraswataa773182013-02-25 01:13:01 +000055 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
Piotr Wilczek24e368a2014-03-07 14:59:39 +010056 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
Jaehoon Chunge3fb6fa2014-05-16 13:59:51 +090057 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
Neil Armstrongf6625b42019-02-10 10:16:21 +000058 COMPAT(GENERIC_SPI_FLASH, "jedec,spi-nor"),
Ajay Kumare24c5022014-09-05 16:53:33 +053059 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
Simon Glassf79d5382014-11-12 22:42:21 -070060 COMPAT(INTEL_MICROCODE, "intel,microcode"),
Bin Meng2bfd2642015-02-05 23:42:26 +080061 COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
Marek Vasutf3f8fe22015-07-25 19:33:56 +020062 COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
Marek Vasut17497232015-07-25 10:48:14 +020063 COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
Marek Vasutf26c1ae2015-12-05 19:28:44 +010064 COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
Simon Glass6f70c112016-06-19 17:33:14 -060065 COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
66 COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
67 COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
Boris Brezillon57f20382016-06-15 21:09:23 +020068 COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
Ley Foon Tanb25ae722017-04-05 17:32:47 +080069 COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
70 COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
71 COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
72 COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
73 COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
74 COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
75 COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
76 COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
Tien Fong Cheebbfa5eb2017-09-25 16:40:03 +080077 COMPAT(ALTERA_SOCFPGA_FPGA0, "altr,socfpga-a10-fpga-mgr"),
78 COMPAT(ALTERA_SOCFPGA_NOC, "altr,socfpga-a10-noc"),
Marek Vasutee4e36b2018-05-12 11:56:10 +020079 COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init")
Simon Glass2d2af852011-10-24 19:15:32 +000080};
81
Simon Glass8f9ce7e2021-12-16 20:59:34 -070082static const char *const fdt_src_name[] = {
83 [FDTSRC_SEPARATE] = "separate",
84 [FDTSRC_FIT] = "fit",
85 [FDTSRC_BOARD] = "board",
86 [FDTSRC_EMBED] = "embed",
87 [FDTSRC_ENV] = "env",
88};
89
90const char *fdtdec_get_srcname(void)
91{
92 return fdt_src_name[gd->fdt_src];
93}
94
Simon Glassb022ead2012-01-17 08:20:50 +000095const char *fdtdec_get_compatible(enum fdt_compat_id id)
96{
97 /* We allow reading of the 'unknown' ID for testing purposes */
98 assert(id >= 0 && id < COMPAT_COUNT);
99 return compat_names[id];
100}
101
Stephen Warren6dfe6182015-08-06 15:31:02 -0600102fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100103 const char *prop_name, int index, int na,
104 int ns, fdt_size_t *sizep,
105 bool translate)
Simon Glass2d2af852011-10-24 19:15:32 +0000106{
Stephen Warren6dfe6182015-08-06 15:31:02 -0600107 const fdt32_t *prop, *prop_end;
108 const fdt32_t *prop_addr, *prop_size, *prop_after_size;
Simon Glass04c50d12015-08-02 18:13:50 -0600109 int len;
Stephen Warren6dfe6182015-08-06 15:31:02 -0600110 fdt_addr_t addr;
Simon Glass2d2af852011-10-24 19:15:32 +0000111
Simon Glassc8c86dd2012-07-12 05:25:01 +0000112 debug("%s: %s: ", __func__, prop_name);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600113
Stephen Warren6dfe6182015-08-06 15:31:02 -0600114 prop = fdt_getprop(blob, node, prop_name, &len);
115 if (!prop) {
116 debug("(not found)\n");
117 return FDT_ADDR_T_NONE;
118 }
119 prop_end = prop + (len / sizeof(*prop));
Simon Glassc8c86dd2012-07-12 05:25:01 +0000120
Stephen Warren6dfe6182015-08-06 15:31:02 -0600121 prop_addr = prop + (index * (na + ns));
122 prop_size = prop_addr + na;
123 prop_after_size = prop_size + ns;
124 if (prop_after_size > prop_end) {
125 debug("(not enough data: expected >= %d cells, got %d cells)\n",
126 (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
127 return FDT_ADDR_T_NONE;
Simon Glassc8c86dd2012-07-12 05:25:01 +0000128 }
Stephen Warren6dfe6182015-08-06 15:31:02 -0600129
Vignesh R621cc092017-03-20 10:04:38 +0530130#if CONFIG_IS_ENABLED(OF_TRANSLATE)
Stephen Warren7d30e102016-08-05 09:47:51 -0600131 if (translate)
132 addr = fdt_translate_address(blob, node, prop_addr);
133 else
134#endif
135 addr = fdtdec_get_number(prop_addr, na);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600136
137 if (sizep) {
138 *sizep = fdtdec_get_number(prop_size, ns);
Simon Glass0b14dc72016-02-29 15:25:37 -0700139 debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
140 (unsigned long long)*sizep);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600141 } else {
Simon Glass0b14dc72016-02-29 15:25:37 -0700142 debug("addr=%08llx\n", (unsigned long long)addr);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600143 }
144
145 return addr;
146}
147
148fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100149 int node, const char *prop_name,
150 int index, fdt_size_t *sizep,
151 bool translate)
Stephen Warren6dfe6182015-08-06 15:31:02 -0600152{
153 int na, ns;
154
155 debug("%s: ", __func__);
156
157 na = fdt_address_cells(blob, parent);
158 if (na < 1) {
159 debug("(bad #address-cells)\n");
160 return FDT_ADDR_T_NONE;
161 }
162
163 ns = fdt_size_cells(blob, parent);
Przemyslaw Marczak7b42f4d2015-09-30 13:14:50 +0200164 if (ns < 0) {
Stephen Warren6dfe6182015-08-06 15:31:02 -0600165 debug("(bad #size-cells)\n");
166 return FDT_ADDR_T_NONE;
167 }
168
169 debug("na=%d, ns=%d, ", na, ns);
170
171 return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
Stephen Warren7d30e102016-08-05 09:47:51 -0600172 ns, sizep, translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600173}
174
175fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100176 const char *prop_name, int index,
177 fdt_size_t *sizep,
178 bool translate)
Stephen Warren6dfe6182015-08-06 15:31:02 -0600179{
180 int parent;
181
182 debug("%s: ", __func__);
183
184 parent = fdt_parent_offset(blob, node);
185 if (parent < 0) {
186 debug("(no parent found)\n");
187 return FDT_ADDR_T_NONE;
188 }
189
190 return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
Stephen Warren7d30e102016-08-05 09:47:51 -0600191 index, sizep, translate);
Stephen Warren6dfe6182015-08-06 15:31:02 -0600192}
193
194fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100195 const char *prop_name, fdt_size_t *sizep)
Stephen Warren6dfe6182015-08-06 15:31:02 -0600196{
Stephen Warrenfda59162015-09-25 10:11:41 -0600197 int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
198
Stephen Warren6dfe6182015-08-06 15:31:02 -0600199 return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
200 sizeof(fdt_addr_t) / sizeof(fdt32_t),
Stephen Warren7d30e102016-08-05 09:47:51 -0600201 ns, sizep, false);
Simon Glass2d2af852011-10-24 19:15:32 +0000202}
203
Mario Sixbb3e06c2018-01-15 11:07:35 +0100204fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name)
Simon Glassf4299552013-03-19 04:58:51 +0000205{
206 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
207}
208
Bin Meng779847e2014-12-31 16:05:11 +0800209int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
210{
211 const char *list, *end;
212 int len;
213
214 list = fdt_getprop(blob, node, "compatible", &len);
215 if (!list)
216 return -ENOENT;
217
218 end = list + len;
219 while (list < end) {
Bin Meng779847e2014-12-31 16:05:11 +0800220 len = strlen(list);
221 if (len >= strlen("pciVVVV,DDDD")) {
Mario Six6abfbc82018-01-15 11:07:36 +0100222 char *s = strstr(list, "pci");
Bin Meng779847e2014-12-31 16:05:11 +0800223
224 /*
225 * check if the string is something like pciVVVV,DDDD.RR
226 * or just pciVVVV,DDDD
227 */
228 if (s && s[7] == ',' &&
229 (s[12] == '.' || s[12] == 0)) {
230 s += 3;
231 *vendor = simple_strtol(s, NULL, 16);
232
233 s += 5;
234 *device = simple_strtol(s, NULL, 16);
235
236 return 0;
237 }
Bin Meng779847e2014-12-31 16:05:11 +0800238 }
Bin Menga2822b82015-08-20 06:40:25 -0700239 list += (len + 1);
Bin Meng779847e2014-12-31 16:05:11 +0800240 }
241
242 return -ENOENT;
243}
244
Simon Glassc92aac12020-01-27 08:49:38 -0700245int fdtdec_get_pci_bar32(const struct udevice *dev, struct fdt_pci_addr *addr,
Simon Glassf1037ad2015-11-29 13:17:54 -0700246 u32 *bar)
Bin Meng779847e2014-12-31 16:05:11 +0800247{
Bin Meng779847e2014-12-31 16:05:11 +0800248 int barnum;
Bin Meng779847e2014-12-31 16:05:11 +0800249
250 /* extract the bar number from fdt_pci_addr */
251 barnum = addr->phys_hi & 0xff;
Mario Six6abfbc82018-01-15 11:07:36 +0100252 if (barnum < PCI_BASE_ADDRESS_0 || barnum > PCI_CARDBUS_CIS)
Bin Meng779847e2014-12-31 16:05:11 +0800253 return -EINVAL;
254
255 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
Simon Glassfde8b762021-08-01 18:54:18 -0600256
Simon Glassf1037ad2015-11-29 13:17:54 -0700257 *bar = dm_pci_read_bar32(dev, barnum);
Bin Meng779847e2014-12-31 16:05:11 +0800258
259 return 0;
260}
Suneel Garapati78d0bea2019-10-19 15:19:35 -0700261
262int fdtdec_get_pci_bus_range(const void *blob, int node,
263 struct fdt_resource *res)
264{
265 const u32 *values;
266 int len;
267
268 values = fdt_getprop(blob, node, "bus-range", &len);
269 if (!values || len < sizeof(*values) * 2)
270 return -EINVAL;
271
272 res->start = fdt32_to_cpu(*values++);
273 res->end = fdt32_to_cpu(*values);
274
275 return 0;
276}
Bin Meng779847e2014-12-31 16:05:11 +0800277
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000278uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100279 uint64_t default_val)
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000280{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +0200281 const unaligned_fdt64_t *cell64;
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000282 int length;
283
284 cell64 = fdt_getprop(blob, node, prop_name, &length);
285 if (!cell64 || length < sizeof(*cell64))
286 return default_val;
287
288 return fdt64_to_cpu(*cell64);
289}
290
Simon Glass65444982012-02-27 10:52:34 +0000291int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glass2d2af852011-10-24 19:15:32 +0000292{
293 const char *cell;
294
Simon Glass65444982012-02-27 10:52:34 +0000295 /*
296 * It should say "okay", so only allow that. Some fdts use "ok" but
297 * this is a bug. Please fix your device tree source file. See here
298 * for discussion:
299 *
300 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
301 */
Simon Glass2d2af852011-10-24 19:15:32 +0000302 cell = fdt_getprop(blob, node, "status", NULL);
303 if (cell)
Mario Six6abfbc82018-01-15 11:07:36 +0100304 return strcmp(cell, "okay") == 0;
Simon Glass65444982012-02-27 10:52:34 +0000305 return 1;
Simon Glass2d2af852011-10-24 19:15:32 +0000306}
307
Gerald Van Barencdac3392012-11-12 23:13:54 -0500308enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
Simon Glass2d2af852011-10-24 19:15:32 +0000309{
310 enum fdt_compat_id id;
311
312 /* Search our drivers */
313 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
Mario Six6abfbc82018-01-15 11:07:36 +0100314 if (fdt_node_check_compatible(blob, node,
315 compat_names[id]) == 0)
Simon Glass2d2af852011-10-24 19:15:32 +0000316 return id;
317 return COMPAT_UNKNOWN;
318}
319
Mario Sixbb3e06c2018-01-15 11:07:35 +0100320int fdtdec_next_compatible(const void *blob, int node, enum fdt_compat_id id)
Simon Glass2d2af852011-10-24 19:15:32 +0000321{
322 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
323}
324
Simon Glass2e979b12012-04-02 13:18:42 +0000325int fdtdec_next_compatible_subnode(const void *blob, int node,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100326 enum fdt_compat_id id, int *depthp)
Simon Glass2e979b12012-04-02 13:18:42 +0000327{
328 do {
329 node = fdt_next_node(blob, node, depthp);
330 } while (*depthp > 1);
331
332 /* If this is a direct subnode, and compatible, return it */
333 if (*depthp == 1 && 0 == fdt_node_check_compatible(
334 blob, node, compat_names[id]))
335 return node;
336
337 return -FDT_ERR_NOTFOUND;
338}
339
Mario Sixbb3e06c2018-01-15 11:07:35 +0100340int fdtdec_next_alias(const void *blob, const char *name, enum fdt_compat_id id,
341 int *upto)
Simon Glass2d2af852011-10-24 19:15:32 +0000342{
343#define MAX_STR_LEN 20
344 char str[MAX_STR_LEN + 20];
345 int node, err;
346
347 /* snprintf() is not available */
348 assert(strlen(name) < MAX_STR_LEN);
349 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glasse1d15ca2012-10-31 14:02:42 +0000350 node = fdt_path_offset(blob, str);
Simon Glass2d2af852011-10-24 19:15:32 +0000351 if (node < 0)
352 return node;
353 err = fdt_node_check_compatible(blob, node, compat_names[id]);
354 if (err < 0)
355 return err;
Simon Glass65444982012-02-27 10:52:34 +0000356 if (err)
357 return -FDT_ERR_NOTFOUND;
358 (*upto)++;
359 return node;
Simon Glass2d2af852011-10-24 19:15:32 +0000360}
361
Simon Glassb022ead2012-01-17 08:20:50 +0000362int fdtdec_find_aliases_for_id(const void *blob, const char *name,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100363 enum fdt_compat_id id, int *node_list,
364 int maxcount)
Simon Glassb022ead2012-01-17 08:20:50 +0000365{
Simon Glass1f772662012-02-03 15:13:53 +0000366 memset(node_list, '\0', sizeof(*node_list) * maxcount);
367
368 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
369}
370
371/* TODO: Can we tighten this code up a little? */
372int fdtdec_add_aliases_for_id(const void *blob, const char *name,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100373 enum fdt_compat_id id, int *node_list,
374 int maxcount)
Simon Glass1f772662012-02-03 15:13:53 +0000375{
Simon Glassb022ead2012-01-17 08:20:50 +0000376 int name_len = strlen(name);
377 int nodes[maxcount];
378 int num_found = 0;
379 int offset, node;
380 int alias_node;
381 int count;
382 int i, j;
383
384 /* find the alias node if present */
385 alias_node = fdt_path_offset(blob, "/aliases");
386
387 /*
388 * start with nothing, and we can assume that the root node can't
389 * match
390 */
391 memset(nodes, '\0', sizeof(nodes));
392
393 /* First find all the compatible nodes */
394 for (node = count = 0; node >= 0 && count < maxcount;) {
395 node = fdtdec_next_compatible(blob, node, id);
396 if (node >= 0)
397 nodes[count++] = node;
398 }
399 if (node >= 0)
400 debug("%s: warning: maxcount exceeded with alias '%s'\n",
Mario Sixbb3e06c2018-01-15 11:07:35 +0100401 __func__, name);
Simon Glassb022ead2012-01-17 08:20:50 +0000402
403 /* Now find all the aliases */
Simon Glassb022ead2012-01-17 08:20:50 +0000404 for (offset = fdt_first_property_offset(blob, alias_node);
405 offset > 0;
406 offset = fdt_next_property_offset(blob, offset)) {
407 const struct fdt_property *prop;
408 const char *path;
409 int number;
410 int found;
411
412 node = 0;
413 prop = fdt_get_property_by_offset(blob, offset, NULL);
414 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
415 if (prop->len && 0 == strncmp(path, name, name_len))
416 node = fdt_path_offset(blob, prop->data);
417 if (node <= 0)
418 continue;
419
420 /* Get the alias number */
Simon Glassff9b9032021-07-24 09:03:30 -0600421 number = dectoul(path + name_len, NULL);
Simon Glassb022ead2012-01-17 08:20:50 +0000422 if (number < 0 || number >= maxcount) {
423 debug("%s: warning: alias '%s' is out of range\n",
Mario Sixbb3e06c2018-01-15 11:07:35 +0100424 __func__, path);
Simon Glassb022ead2012-01-17 08:20:50 +0000425 continue;
426 }
427
428 /* Make sure the node we found is actually in our list! */
429 found = -1;
430 for (j = 0; j < count; j++)
431 if (nodes[j] == node) {
432 found = j;
433 break;
434 }
435
436 if (found == -1) {
437 debug("%s: warning: alias '%s' points to a node "
438 "'%s' that is missing or is not compatible "
439 " with '%s'\n", __func__, path,
440 fdt_get_name(blob, node, NULL),
441 compat_names[id]);
442 continue;
443 }
444
445 /*
446 * Add this node to our list in the right place, and mark
447 * it as done.
448 */
449 if (fdtdec_get_is_enabled(blob, node)) {
Simon Glass1f772662012-02-03 15:13:53 +0000450 if (node_list[number]) {
451 debug("%s: warning: alias '%s' requires that "
452 "a node be placed in the list in a "
453 "position which is already filled by "
454 "node '%s'\n", __func__, path,
455 fdt_get_name(blob, node, NULL));
456 continue;
457 }
Simon Glassb022ead2012-01-17 08:20:50 +0000458 node_list[number] = node;
459 if (number >= num_found)
460 num_found = number + 1;
461 }
Simon Glass1f772662012-02-03 15:13:53 +0000462 nodes[found] = 0;
Simon Glassb022ead2012-01-17 08:20:50 +0000463 }
464
465 /* Add any nodes not mentioned by an alias */
466 for (i = j = 0; i < maxcount; i++) {
467 if (!node_list[i]) {
468 for (; j < maxcount; j++)
469 if (nodes[j] &&
Mario Sixbb3e06c2018-01-15 11:07:35 +0100470 fdtdec_get_is_enabled(blob, nodes[j]))
Simon Glassb022ead2012-01-17 08:20:50 +0000471 break;
472
473 /* Have we run out of nodes to add? */
474 if (j == maxcount)
475 break;
476
477 assert(!node_list[i]);
478 node_list[i] = nodes[j++];
479 if (i >= num_found)
480 num_found = i + 1;
481 }
482 }
483
484 return num_found;
485}
486
Simon Glass9908ad42014-07-23 06:55:09 -0600487int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
488 int *seqp)
489{
490 int base_len = strlen(base);
491 const char *find_name;
492 int find_namelen;
493 int prop_offset;
494 int aliases;
495
496 find_name = fdt_get_name(blob, offset, &find_namelen);
497 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
498
499 aliases = fdt_path_offset(blob, "/aliases");
500 for (prop_offset = fdt_first_property_offset(blob, aliases);
501 prop_offset > 0;
502 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
503 const char *prop;
504 const char *name;
505 const char *slash;
Simon Glass4aaa4602015-06-23 15:39:08 -0600506 int len, val;
Simon Glass9908ad42014-07-23 06:55:09 -0600507
508 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
509 debug(" - %s, %s\n", name, prop);
510 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
511 strncmp(name, base, base_len))
512 continue;
513
514 slash = strrchr(prop, '/');
515 if (strcmp(slash + 1, find_name))
516 continue;
Aswath Govindraju3dd81252020-12-03 10:55:45 +0530517
518 /*
519 * Adding an extra check to distinguish DT nodes with
520 * same name
521 */
Rasmus Villemoesc55d2612022-05-19 11:10:43 +0200522 if (offset != fdt_path_offset(blob, prop))
523 continue;
Aswath Govindraju3dd81252020-12-03 10:55:45 +0530524
Simon Glass4aaa4602015-06-23 15:39:08 -0600525 val = trailing_strtol(name);
526 if (val != -1) {
527 *seqp = val;
528 debug("Found seq %d\n", *seqp);
529 return 0;
Simon Glass9908ad42014-07-23 06:55:09 -0600530 }
531 }
532
533 debug("Not found\n");
534 return -ENOENT;
535}
536
Michal Simek4b586672019-01-31 16:30:58 +0100537int fdtdec_get_alias_highest_id(const void *blob, const char *base)
538{
539 int base_len = strlen(base);
540 int prop_offset;
541 int aliases;
542 int max = -1;
543
544 debug("Looking for highest alias id for '%s'\n", base);
545
546 aliases = fdt_path_offset(blob, "/aliases");
547 for (prop_offset = fdt_first_property_offset(blob, aliases);
548 prop_offset > 0;
549 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
550 const char *prop;
551 const char *name;
552 int len, val;
553
554 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
555 debug(" - %s, %s\n", name, prop);
556 if (*prop != '/' || prop[len - 1] ||
557 strncmp(name, base, base_len))
558 continue;
559
560 val = trailing_strtol(name);
561 if (val > max) {
562 debug("Found seq %d\n", val);
563 max = val;
564 }
565 }
566
567 return max;
568}
569
Simon Glass088dccb2015-10-17 19:41:14 -0600570const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
Simon Glass1e374fb2014-09-04 16:27:24 -0600571{
Simon Glass1e374fb2014-09-04 16:27:24 -0600572 int chosen_node;
Simon Glass1e374fb2014-09-04 16:27:24 -0600573
574 if (!blob)
Simon Glass088dccb2015-10-17 19:41:14 -0600575 return NULL;
Simon Glass1e374fb2014-09-04 16:27:24 -0600576 chosen_node = fdt_path_offset(blob, "/chosen");
Simon Glass088dccb2015-10-17 19:41:14 -0600577 return fdt_getprop(blob, chosen_node, name, NULL);
578}
579
580int fdtdec_get_chosen_node(const void *blob, const char *name)
581{
582 const char *prop;
583
584 prop = fdtdec_get_chosen_prop(blob, name);
Simon Glass1e374fb2014-09-04 16:27:24 -0600585 if (!prop)
586 return -FDT_ERR_NOTFOUND;
587 return fdt_path_offset(blob, prop);
588}
589
Simon Glass84137372022-12-21 16:08:19 -0700590/**
591 * fdtdec_prepare_fdt() - Check we have a valid fdt available to control U-Boot
592 *
593 * If not, a message is printed to the console if the console is ready.
594 *
595 * Return: 0 if all ok, -ENOENT if not
Simon Glass2d2af852011-10-24 19:15:32 +0000596 */
Simon Glassfa3f52d2022-12-21 16:08:18 -0700597static int fdtdec_prepare_fdt(void)
Simon Glass2d2af852011-10-24 19:15:32 +0000598{
Simon Glass71136852013-04-20 08:42:46 +0000599 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
600 fdt_check_header(gd->fdt_blob)) {
Simon Glass84137372022-12-21 16:08:19 -0700601 if (spl_phase() <= PHASE_SPL) {
602 puts("Missing DTB\n");
603 } else {
604 printf("No valid device tree binary found at %p\n",
605 gd->fdt_blob);
606 if (_DEBUG && gd->fdt_blob) {
607 printf("fdt_blob=%p\n", gd->fdt_blob);
Simon Glass63884ec2015-06-23 15:39:09 -0600608 print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
609 32, 0);
Simon Glass84137372022-12-21 16:08:19 -0700610 }
Simon Glass63884ec2015-06-23 15:39:09 -0600611 }
Simon Glass84137372022-12-21 16:08:19 -0700612 return -ENOENT;
Simon Glass1691f692012-03-28 10:08:24 +0000613 }
Simon Glass84137372022-12-21 16:08:19 -0700614
Simon Glass2d2af852011-10-24 19:15:32 +0000615 return 0;
616}
Simon Glassc4697362012-02-27 10:52:35 +0000617
Simon Glassfa3f52d2022-12-21 16:08:18 -0700618int fdtdec_check_fdt(void)
619{
620 /*
621 * We must have an FDT, but we cannot panic() yet since the console
622 * is not ready. So for now, just assert(). Boards which need an early
623 * FDT (prior to console ready) will need to make their own
624 * arrangements and do their own checks.
625 */
626 assert(!fdtdec_prepare_fdt());
627 return 0;
628}
629
Simon Glassc4697362012-02-27 10:52:35 +0000630int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
631{
632 const u32 *phandle;
633 int lookup;
634
Simon Glassc8c86dd2012-07-12 05:25:01 +0000635 debug("%s: %s\n", __func__, prop_name);
Simon Glassc4697362012-02-27 10:52:35 +0000636 phandle = fdt_getprop(blob, node, prop_name, NULL);
637 if (!phandle)
638 return -FDT_ERR_NOTFOUND;
639
640 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
641 return lookup;
642}
643
644/**
645 * Look up a property in a node and check that it has a minimum length.
646 *
647 * @param blob FDT blob
648 * @param node node to examine
649 * @param prop_name name of property to find
650 * @param min_len minimum property length in bytes
651 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
652 found, or -FDT_ERR_BADLAYOUT if not enough data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100653 * Return: pointer to cell, which is only valid if err == 0
Simon Glassc4697362012-02-27 10:52:35 +0000654 */
655static const void *get_prop_check_min_len(const void *blob, int node,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100656 const char *prop_name, int min_len,
657 int *err)
Simon Glassc4697362012-02-27 10:52:35 +0000658{
659 const void *cell;
660 int len;
661
662 debug("%s: %s\n", __func__, prop_name);
663 cell = fdt_getprop(blob, node, prop_name, &len);
664 if (!cell)
665 *err = -FDT_ERR_NOTFOUND;
666 else if (len < min_len)
667 *err = -FDT_ERR_BADLAYOUT;
668 else
669 *err = 0;
670 return cell;
671}
672
673int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100674 u32 *array, int count)
Simon Glassc4697362012-02-27 10:52:35 +0000675{
676 const u32 *cell;
Mario Six6abfbc82018-01-15 11:07:36 +0100677 int err = 0;
Simon Glassc4697362012-02-27 10:52:35 +0000678
679 debug("%s: %s\n", __func__, prop_name);
680 cell = get_prop_check_min_len(blob, node, prop_name,
681 sizeof(u32) * count, &err);
682 if (!err) {
Mario Six6abfbc82018-01-15 11:07:36 +0100683 int i;
684
Simon Glassc4697362012-02-27 10:52:35 +0000685 for (i = 0; i < count; i++)
686 array[i] = fdt32_to_cpu(cell[i]);
687 }
688 return err;
689}
690
Simon Glasseda0c3f2014-11-10 18:00:19 -0700691int fdtdec_get_int_array_count(const void *blob, int node,
692 const char *prop_name, u32 *array, int count)
693{
694 const u32 *cell;
695 int len, elems;
696 int i;
697
698 debug("%s: %s\n", __func__, prop_name);
699 cell = fdt_getprop(blob, node, prop_name, &len);
700 if (!cell)
701 return -FDT_ERR_NOTFOUND;
702 elems = len / sizeof(u32);
703 if (count > elems)
704 count = elems;
705 for (i = 0; i < count; i++)
706 array[i] = fdt32_to_cpu(cell[i]);
707
708 return count;
709}
710
Simon Glass744ec232012-04-02 13:18:41 +0000711const u32 *fdtdec_locate_array(const void *blob, int node,
712 const char *prop_name, int count)
713{
714 const u32 *cell;
715 int err;
716
717 cell = get_prop_check_min_len(blob, node, prop_name,
718 sizeof(u32) * count, &err);
719 return err ? NULL : cell;
720}
721
Simon Glassc4697362012-02-27 10:52:35 +0000722int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
723{
724 const s32 *cell;
725 int len;
726
727 debug("%s: %s\n", __func__, prop_name);
728 cell = fdt_getprop(blob, node, prop_name, &len);
729 return cell != NULL;
730}
Simon Glass0d83c4d2012-02-27 10:52:36 +0000731
Simon Glassb491e9c2015-01-05 20:05:26 -0700732int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
733 const char *list_name,
734 const char *cells_name,
735 int cell_count, int index,
736 struct fdtdec_phandle_args *out_args)
737{
738 const __be32 *list, *list_end;
739 int rc = 0, size, cur_index = 0;
740 uint32_t count = 0;
741 int node = -1;
742 int phandle;
743
744 /* Retrieve the phandle list property */
745 list = fdt_getprop(blob, src_node, list_name, &size);
746 if (!list)
747 return -ENOENT;
748 list_end = list + size / sizeof(*list);
749
750 /* Loop over the phandles until all the requested entry is found */
751 while (list < list_end) {
752 rc = -EINVAL;
753 count = 0;
754
755 /*
756 * If phandle is 0, then it is an empty entry with no
757 * arguments. Skip forward to the next entry.
758 */
759 phandle = be32_to_cpup(list++);
760 if (phandle) {
761 /*
762 * Find the provider node and parse the #*-cells
763 * property to determine the argument length.
764 *
765 * This is not needed if the cell count is hard-coded
766 * (i.e. cells_name not set, but cell_count is set),
767 * except when we're going to return the found node
768 * below.
769 */
770 if (cells_name || cur_index == index) {
771 node = fdt_node_offset_by_phandle(blob,
772 phandle);
Patrick Delaunayf9cbd1a2020-09-25 09:41:15 +0200773 if (node < 0) {
Simon Glassb491e9c2015-01-05 20:05:26 -0700774 debug("%s: could not find phandle\n",
775 fdt_get_name(blob, src_node,
776 NULL));
777 goto err;
778 }
779 }
780
781 if (cells_name) {
782 count = fdtdec_get_int(blob, node, cells_name,
783 -1);
784 if (count == -1) {
785 debug("%s: could not get %s for %s\n",
786 fdt_get_name(blob, src_node,
787 NULL),
788 cells_name,
789 fdt_get_name(blob, node,
790 NULL));
791 goto err;
792 }
793 } else {
794 count = cell_count;
795 }
796
797 /*
798 * Make sure that the arguments actually fit in the
799 * remaining property data length
800 */
801 if (list + count > list_end) {
802 debug("%s: arguments longer than property\n",
803 fdt_get_name(blob, src_node, NULL));
804 goto err;
805 }
806 }
807
808 /*
809 * All of the error cases above bail out of the loop, so at
810 * this point, the parsing is successful. If the requested
811 * index matches, then fill the out_args structure and return,
812 * or return -ENOENT for an empty entry.
813 */
814 rc = -ENOENT;
815 if (cur_index == index) {
816 if (!phandle)
817 goto err;
818
819 if (out_args) {
820 int i;
821
822 if (count > MAX_PHANDLE_ARGS) {
823 debug("%s: too many arguments %d\n",
824 fdt_get_name(blob, src_node,
825 NULL), count);
826 count = MAX_PHANDLE_ARGS;
827 }
828 out_args->node = node;
829 out_args->args_count = count;
830 for (i = 0; i < count; i++) {
831 out_args->args[i] =
832 be32_to_cpup(list++);
833 }
834 }
835
836 /* Found it! return success */
837 return 0;
838 }
839
840 node = -1;
841 list += count;
842 cur_index++;
843 }
844
845 /*
846 * Result will be one of:
847 * -ENOENT : index is for empty phandle
848 * -EINVAL : parsing error on data
849 * [1..n] : Number of phandle (count mode; when index = -1)
850 */
851 rc = index < 0 ? cur_index : -ENOENT;
852 err:
853 return rc;
854}
855
Anton Stafff2305ec2012-04-17 09:01:28 +0000856int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100857 u8 *array, int count)
Anton Stafff2305ec2012-04-17 09:01:28 +0000858{
859 const u8 *cell;
860 int err;
861
862 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
863 if (!err)
864 memcpy(array, cell, count);
865 return err;
866}
867
868const u8 *fdtdec_locate_byte_array(const void *blob, int node,
Mario Sixbb3e06c2018-01-15 11:07:35 +0100869 const char *prop_name, int count)
Anton Stafff2305ec2012-04-17 09:01:28 +0000870{
871 const u8 *cell;
872 int err;
873
874 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
875 if (err)
876 return NULL;
877 return cell;
878}
Abhilash Kesavan67217af2012-10-25 16:30:58 +0000879
Simon Glass9ffbe332015-03-05 12:25:14 -0700880u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
Thierry Redinga937b802014-08-26 17:33:53 +0200881{
882 u64 number = 0;
883
884 while (cells--)
885 number = (number << 32) | fdt32_to_cpu(*ptr++);
886
887 return number;
888}
889
890int fdt_get_resource(const void *fdt, int node, const char *property,
891 unsigned int index, struct fdt_resource *res)
892{
893 const fdt32_t *ptr, *end;
894 int na, ns, len, parent;
895 unsigned int i = 0;
896
897 parent = fdt_parent_offset(fdt, node);
898 if (parent < 0)
899 return parent;
900
901 na = fdt_address_cells(fdt, parent);
902 ns = fdt_size_cells(fdt, parent);
903
904 ptr = fdt_getprop(fdt, node, property, &len);
905 if (!ptr)
906 return len;
907
908 end = ptr + len / sizeof(*ptr);
909
910 while (ptr + na + ns <= end) {
911 if (i == index) {
Patrick Delaunay2a28c6e2021-04-06 09:38:06 +0200912 if (CONFIG_IS_ENABLED(OF_TRANSLATE))
913 res->start = fdt_translate_address(fdt, node, ptr);
914 else
915 res->start = fdtdec_get_number(ptr, na);
916
Mario Six6abfbc82018-01-15 11:07:36 +0100917 res->end = res->start;
Thierry Redinga937b802014-08-26 17:33:53 +0200918 res->end += fdtdec_get_number(&ptr[na], ns) - 1;
919 return 0;
920 }
921
922 ptr += na + ns;
923 i++;
924 }
925
926 return -FDT_ERR_NOTFOUND;
927}
928
929int fdt_get_named_resource(const void *fdt, int node, const char *property,
930 const char *prop_names, const char *name,
931 struct fdt_resource *res)
932{
933 int index;
934
Simon Glassb0ea7402016-10-02 17:59:28 -0600935 index = fdt_stringlist_search(fdt, node, prop_names, name);
Thierry Redinga937b802014-08-26 17:33:53 +0200936 if (index < 0)
937 return index;
938
939 return fdt_get_resource(fdt, node, property, index, res);
940}
Thierry Reding24856312014-08-26 17:33:54 +0200941
Simon Glassb24e8642015-04-14 21:03:21 -0600942static int decode_timing_property(const void *blob, int node, const char *name,
943 struct timing_entry *result)
944{
945 int length, ret = 0;
946 const u32 *prop;
947
948 prop = fdt_getprop(blob, node, name, &length);
949 if (!prop) {
950 debug("%s: could not find property %s\n",
951 fdt_get_name(blob, node, NULL), name);
952 return length;
953 }
954
955 if (length == sizeof(u32)) {
956 result->typ = fdtdec_get_int(blob, node, name, 0);
957 result->min = result->typ;
958 result->max = result->typ;
959 } else {
960 ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
961 }
962
963 return ret;
964}
965
966int fdtdec_decode_display_timing(const void *blob, int parent, int index,
967 struct display_timing *dt)
968{
969 int i, node, timings_node;
970 u32 val = 0;
971 int ret = 0;
972
973 timings_node = fdt_subnode_offset(blob, parent, "display-timings");
974 if (timings_node < 0)
975 return timings_node;
976
977 for (i = 0, node = fdt_first_subnode(blob, timings_node);
978 node > 0 && i != index;
979 node = fdt_next_subnode(blob, node))
980 i++;
981
982 if (node < 0)
983 return node;
984
985 memset(dt, 0, sizeof(*dt));
986
987 ret |= decode_timing_property(blob, node, "hback-porch",
988 &dt->hback_porch);
989 ret |= decode_timing_property(blob, node, "hfront-porch",
990 &dt->hfront_porch);
991 ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
992 ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
993 ret |= decode_timing_property(blob, node, "vback-porch",
994 &dt->vback_porch);
995 ret |= decode_timing_property(blob, node, "vfront-porch",
996 &dt->vfront_porch);
997 ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
998 ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
999 ret |= decode_timing_property(blob, node, "clock-frequency",
1000 &dt->pixelclock);
1001
1002 dt->flags = 0;
1003 val = fdtdec_get_int(blob, node, "vsync-active", -1);
1004 if (val != -1) {
1005 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
1006 DISPLAY_FLAGS_VSYNC_LOW;
1007 }
1008 val = fdtdec_get_int(blob, node, "hsync-active", -1);
1009 if (val != -1) {
1010 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
1011 DISPLAY_FLAGS_HSYNC_LOW;
1012 }
1013 val = fdtdec_get_int(blob, node, "de-active", -1);
1014 if (val != -1) {
1015 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1016 DISPLAY_FLAGS_DE_LOW;
1017 }
1018 val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
1019 if (val != -1) {
1020 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1021 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1022 }
1023
1024 if (fdtdec_get_bool(blob, node, "interlaced"))
1025 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1026 if (fdtdec_get_bool(blob, node, "doublescan"))
1027 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1028 if (fdtdec_get_bool(blob, node, "doubleclk"))
1029 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1030
Simon Glass6eb90e12016-05-05 07:28:21 -06001031 return ret;
Simon Glassb24e8642015-04-14 21:03:21 -06001032}
1033
Michal Simek43ba85b2020-07-10 13:16:50 +02001034int fdtdec_setup_mem_size_base(void)
Nathan Rossiba97b722016-12-19 00:03:34 +10001035{
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001036 int ret;
1037 ofnode mem;
1038 struct resource res;
Nathan Rossiba97b722016-12-19 00:03:34 +10001039
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001040 mem = ofnode_path("/memory");
1041 if (!ofnode_valid(mem)) {
Nathan Rossiba97b722016-12-19 00:03:34 +10001042 debug("%s: Missing /memory node\n", __func__);
1043 return -EINVAL;
1044 }
1045
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001046 ret = ofnode_read_resource(mem, 0, &res);
Nathan Rossiba97b722016-12-19 00:03:34 +10001047 if (ret != 0) {
1048 debug("%s: Unable to decode first memory bank\n", __func__);
1049 return -EINVAL;
1050 }
1051
1052 gd->ram_size = (phys_size_t)(res.end - res.start + 1);
Siva Durga Prasad Paladugu94a1d522018-07-16 15:56:10 +05301053 gd->ram_base = (unsigned long)res.start;
Simon Glass640b78f2017-05-27 07:38:13 -06001054 debug("%s: Initial DRAM size %llx\n", __func__,
1055 (unsigned long long)gd->ram_size);
Nathan Rossiba97b722016-12-19 00:03:34 +10001056
1057 return 0;
1058}
1059
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001060ofnode get_next_memory_node(ofnode mem)
Jens Wiklandere2da4c92018-07-13 12:12:11 +02001061{
Jens Wiklandere2da4c92018-07-13 12:12:11 +02001062 do {
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001063 mem = ofnode_by_prop_value(mem, "device_type", "memory", 7);
Simon Glass2e4938b2022-09-06 20:27:17 -06001064 } while (!ofnode_is_enabled(mem));
Jens Wiklandere2da4c92018-07-13 12:12:11 +02001065
1066 return mem;
1067}
1068
Michal Simekf7833292020-07-10 13:16:49 +02001069int fdtdec_setup_memory_banksize(void)
Nathan Rossiba97b722016-12-19 00:03:34 +10001070{
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001071 int bank, ret, reg = 0;
1072 struct resource res;
1073 ofnode mem = ofnode_null();
Nathan Rossiba97b722016-12-19 00:03:34 +10001074
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001075 mem = get_next_memory_node(mem);
1076 if (!ofnode_valid(mem)) {
Michal Simek0bd065c2018-10-03 15:53:52 +02001077 debug("%s: Missing /memory node\n", __func__);
1078 return -EINVAL;
1079 }
Nathan Rossiba97b722016-12-19 00:03:34 +10001080
1081 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001082 ret = ofnode_read_resource(mem, reg++, &res);
1083 if (ret < 0) {
Marek Vasut7f0293a2017-11-27 05:32:42 +01001084 reg = 0;
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001085 mem = get_next_memory_node(mem);
Marek Vasut253c1a42020-09-12 12:42:52 +02001086 if (!ofnode_valid(mem))
Michal Simek0bd065c2018-10-03 15:53:52 +02001087 break;
1088
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001089 ret = ofnode_read_resource(mem, reg++, &res);
1090 if (ret < 0)
Michal Simek0bd065c2018-10-03 15:53:52 +02001091 break;
1092 }
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001093
1094 if (ret != 0)
Michal Simek0bd065c2018-10-03 15:53:52 +02001095 return -EINVAL;
Nathan Rossiba97b722016-12-19 00:03:34 +10001096
1097 gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
1098 gd->bd->bi_dram[bank].size =
1099 (phys_size_t)(res.end - res.start + 1);
1100
1101 debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
1102 __func__, bank,
1103 (unsigned long long)gd->bd->bi_dram[bank].start,
1104 (unsigned long long)gd->bd->bi_dram[bank].size);
1105 }
1106
1107 return 0;
1108}
Michal Simek9d2cf072020-07-09 14:09:52 +02001109
1110int fdtdec_setup_mem_size_base_lowest(void)
1111{
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001112 int bank, ret, reg = 0;
1113 struct resource res;
Michal Simek9d2cf072020-07-09 14:09:52 +02001114 unsigned long base;
1115 phys_size_t size;
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001116 ofnode mem = ofnode_null();
Michal Simek9d2cf072020-07-09 14:09:52 +02001117
1118 gd->ram_base = (unsigned long)~0;
1119
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001120 mem = get_next_memory_node(mem);
1121 if (!ofnode_valid(mem)) {
Michal Simek9d2cf072020-07-09 14:09:52 +02001122 debug("%s: Missing /memory node\n", __func__);
1123 return -EINVAL;
1124 }
1125
1126 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001127 ret = ofnode_read_resource(mem, reg++, &res);
1128 if (ret < 0) {
Michal Simek9d2cf072020-07-09 14:09:52 +02001129 reg = 0;
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001130 mem = get_next_memory_node(mem);
Marek Vasut253c1a42020-09-12 12:42:52 +02001131 if (!ofnode_valid(mem))
Michal Simek9d2cf072020-07-09 14:09:52 +02001132 break;
1133
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001134 ret = ofnode_read_resource(mem, reg++, &res);
1135 if (ret < 0)
Michal Simek9d2cf072020-07-09 14:09:52 +02001136 break;
1137 }
Michal Simek6dd9cfd2020-07-10 13:34:44 +02001138
Michal Simek9d2cf072020-07-09 14:09:52 +02001139 if (ret != 0)
1140 return -EINVAL;
1141
1142 base = (unsigned long)res.start;
1143 size = (phys_size_t)(res.end - res.start + 1);
1144
1145 if (gd->ram_base > base && size) {
1146 gd->ram_base = base;
1147 gd->ram_size = size;
1148 debug("%s: Initial DRAM base %lx size %lx\n",
1149 __func__, base, (unsigned long)size);
1150 }
1151 }
1152
1153 return 0;
1154}
Nathan Rossiba97b722016-12-19 00:03:34 +10001155
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001156static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
1157{
Simon Glass69daf5a2021-12-16 20:59:25 -07001158#if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
1159 CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO)
Marek Vasutaeaa29d2019-03-08 16:06:55 +01001160 size_t sz_out = CONFIG_VAL(MULTI_DTB_FIT_UNCOMPRESS_SZ);
Marek Vasut42e5c032019-03-13 21:11:22 +01001161 bool gzip = 0, lzo = 0;
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001162 ulong sz_in = sz_src;
1163 void *dst;
1164 int rc;
1165
1166 if (CONFIG_IS_ENABLED(GZIP))
Marek Vasut42e5c032019-03-13 21:11:22 +01001167 if (gzip_parse_header(src, sz_in) >= 0)
1168 gzip = 1;
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001169 if (CONFIG_IS_ENABLED(LZO))
Marek Vasut42e5c032019-03-13 21:11:22 +01001170 if (!gzip && lzop_is_valid_header(src))
1171 lzo = 1;
1172
1173 if (!gzip && !lzo)
1174 return -EBADMSG;
1175
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001176
1177 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) {
1178 dst = malloc(sz_out);
1179 if (!dst) {
1180 puts("uncompress_blob: Unable to allocate memory\n");
1181 return -ENOMEM;
1182 }
1183 } else {
Simon Glass69daf5a2021-12-16 20:59:25 -07001184# if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA)
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001185 dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR);
Simon Glass69daf5a2021-12-16 20:59:25 -07001186# else
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001187 return -ENOTSUPP;
Simon Glass69daf5a2021-12-16 20:59:25 -07001188# endif
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001189 }
1190
Marek Vasut42e5c032019-03-13 21:11:22 +01001191 if (CONFIG_IS_ENABLED(GZIP) && gzip)
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001192 rc = gunzip(dst, sz_out, (u8 *)src, &sz_in);
Marek Vasut42e5c032019-03-13 21:11:22 +01001193 else if (CONFIG_IS_ENABLED(LZO) && lzo)
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001194 rc = lzop_decompress(src, sz_in, dst, &sz_out);
Marek Vasut42e5c032019-03-13 21:11:22 +01001195 else
1196 hang();
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001197
1198 if (rc < 0) {
1199 /* not a valid compressed blob */
1200 puts("uncompress_blob: Unable to uncompress\n");
1201 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC))
1202 free(dst);
1203 return -EBADMSG;
1204 }
1205 *dstp = dst;
Simon Glass69daf5a2021-12-16 20:59:25 -07001206#else
1207 *dstp = (void *)src;
Marek Vasut5bd49fb2018-10-18 20:37:05 +02001208 *dstp = (void *)src;
Simon Glass69daf5a2021-12-16 20:59:25 -07001209#endif
Marek Vasut5bd49fb2018-10-18 20:37:05 +02001210 return 0;
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001211}
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001212
Simon Glass28b76512021-12-16 20:59:31 -07001213/**
1214 * fdt_find_separate() - Find a devicetree at the end of the image
1215 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001216 * Return: pointer to FDT blob
Rob Clark974dcac2018-01-10 11:34:37 +01001217 */
Simon Glass28b76512021-12-16 20:59:31 -07001218static void *fdt_find_separate(void)
Rob Clark974dcac2018-01-10 11:34:37 +01001219{
1220 void *fdt_blob = NULL;
Ilias Apalodimasab5348a2021-10-26 09:12:33 +03001221
Simon Glassdf5f4382022-02-28 12:08:24 -07001222 if (IS_ENABLED(CONFIG_SANDBOX))
1223 return NULL;
1224
Rob Clark974dcac2018-01-10 11:34:37 +01001225#ifdef CONFIG_SPL_BUILD
1226 /* FDT is at end of BSS unless it is in a different memory region */
Andrew Abbottb449fed2022-04-16 10:12:31 +10001227 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
Rob Clark974dcac2018-01-10 11:34:37 +01001228 fdt_blob = (ulong *)&_image_binary_end;
1229 else
1230 fdt_blob = (ulong *)&__bss_end;
1231#else
1232 /* FDT is at end of image */
1233 fdt_blob = (ulong *)&_end;
1234#endif
Ilias Apalodimasab5348a2021-10-26 09:12:33 +03001235
Rob Clark974dcac2018-01-10 11:34:37 +01001236 return fdt_blob;
1237}
Rob Clark974dcac2018-01-10 11:34:37 +01001238
Thierry Reding1bb59a92019-04-15 11:32:13 +02001239int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size)
1240{
1241 const char *path;
1242 int offset, err;
1243
1244 if (!is_valid_ethaddr(mac))
1245 return -EINVAL;
1246
1247 path = fdt_get_alias(fdt, "ethernet");
1248 if (!path)
1249 return 0;
1250
1251 debug("ethernet alias found: %s\n", path);
1252
1253 offset = fdt_path_offset(fdt, path);
1254 if (offset < 0) {
1255 debug("ethernet alias points to absent node %s\n", path);
1256 return -ENOENT;
1257 }
1258
1259 err = fdt_setprop_inplace(fdt, offset, "local-mac-address", mac, size);
1260 if (err < 0)
1261 return err;
1262
1263 debug("MAC address: %pM\n", mac);
1264
1265 return 0;
1266}
1267
Thierry Redinge8dfad42019-03-21 19:10:02 +01001268static int fdtdec_init_reserved_memory(void *blob)
1269{
1270 int na, ns, node, err;
1271 fdt32_t value;
1272
1273 /* inherit #address-cells and #size-cells from the root node */
1274 na = fdt_address_cells(blob, 0);
1275 ns = fdt_size_cells(blob, 0);
1276
1277 node = fdt_add_subnode(blob, 0, "reserved-memory");
1278 if (node < 0)
1279 return node;
1280
1281 err = fdt_setprop(blob, node, "ranges", NULL, 0);
1282 if (err < 0)
1283 return err;
1284
1285 value = cpu_to_fdt32(ns);
1286
1287 err = fdt_setprop(blob, node, "#size-cells", &value, sizeof(value));
1288 if (err < 0)
1289 return err;
1290
1291 value = cpu_to_fdt32(na);
1292
1293 err = fdt_setprop(blob, node, "#address-cells", &value, sizeof(value));
1294 if (err < 0)
1295 return err;
1296
1297 return node;
1298}
1299
1300int fdtdec_add_reserved_memory(void *blob, const char *basename,
1301 const struct fdt_memory *carveout,
Thierry Reding5e336912021-09-03 15:16:19 +02001302 const char **compatibles, unsigned int count,
Thierry Redingbedc4392021-09-03 15:16:21 +02001303 uint32_t *phandlep, unsigned long flags)
Thierry Redinge8dfad42019-03-21 19:10:02 +01001304{
1305 fdt32_t cells[4] = {}, *ptr = cells;
1306 uint32_t upper, lower, phandle;
1307 int parent, node, na, ns, err;
Thierry Reding26130012019-04-15 10:08:21 +02001308 fdt_size_t size;
Thierry Redinge8dfad42019-03-21 19:10:02 +01001309 char name[64];
1310
1311 /* create an empty /reserved-memory node if one doesn't exist */
1312 parent = fdt_path_offset(blob, "/reserved-memory");
1313 if (parent < 0) {
1314 parent = fdtdec_init_reserved_memory(blob);
1315 if (parent < 0)
1316 return parent;
1317 }
1318
1319 /* only 1 or 2 #address-cells and #size-cells are supported */
1320 na = fdt_address_cells(blob, parent);
1321 if (na < 1 || na > 2)
1322 return -FDT_ERR_BADNCELLS;
1323
1324 ns = fdt_size_cells(blob, parent);
1325 if (ns < 1 || ns > 2)
1326 return -FDT_ERR_BADNCELLS;
1327
1328 /* find a matching node and return the phandle to that */
1329 fdt_for_each_subnode(node, blob, parent) {
1330 const char *name = fdt_get_name(blob, node, NULL);
Bin Menga26ad7a2020-05-19 23:38:32 -07001331 fdt_addr_t addr;
1332 fdt_size_t size;
Thierry Redinge8dfad42019-03-21 19:10:02 +01001333
Bin Meng32d294b2020-05-19 23:38:33 -07001334 addr = fdtdec_get_addr_size_fixed(blob, node, "reg", 0, na, ns,
1335 &size, false);
Thierry Redinge8dfad42019-03-21 19:10:02 +01001336 if (addr == FDT_ADDR_T_NONE) {
1337 debug("failed to read address/size for %s\n", name);
1338 continue;
1339 }
1340
Atish Patrab57af062020-04-21 11:15:00 -07001341 if (addr == carveout->start && (addr + size - 1) ==
1342 carveout->end) {
Heiko Stuebner49723ac2019-10-23 16:46:38 +02001343 if (phandlep)
1344 *phandlep = fdt_get_phandle(blob, node);
Thierry Redinge8dfad42019-03-21 19:10:02 +01001345 return 0;
1346 }
1347 }
1348
1349 /*
1350 * Unpack the start address and generate the name of the new node
1351 * base on the basename and the unit-address.
1352 */
Thierry Reding26130012019-04-15 10:08:21 +02001353 upper = upper_32_bits(carveout->start);
1354 lower = lower_32_bits(carveout->start);
Thierry Redinge8dfad42019-03-21 19:10:02 +01001355
1356 if (na > 1 && upper > 0)
1357 snprintf(name, sizeof(name), "%s@%x,%x", basename, upper,
1358 lower);
1359 else {
1360 if (upper > 0) {
1361 debug("address %08x:%08x exceeds addressable space\n",
1362 upper, lower);
1363 return -FDT_ERR_BADVALUE;
1364 }
1365
1366 snprintf(name, sizeof(name), "%s@%x", basename, lower);
1367 }
1368
1369 node = fdt_add_subnode(blob, parent, name);
1370 if (node < 0)
1371 return node;
1372
Thierry Redingbedc4392021-09-03 15:16:21 +02001373 if (flags & FDTDEC_RESERVED_MEMORY_NO_MAP) {
1374 err = fdt_setprop(blob, node, "no-map", NULL, 0);
1375 if (err < 0)
1376 return err;
1377 }
1378
Heiko Stuebner754302f2019-10-23 16:46:39 +02001379 if (phandlep) {
1380 err = fdt_generate_phandle(blob, &phandle);
1381 if (err < 0)
1382 return err;
Thierry Redinge8dfad42019-03-21 19:10:02 +01001383
Heiko Stuebner754302f2019-10-23 16:46:39 +02001384 err = fdtdec_set_phandle(blob, node, phandle);
1385 if (err < 0)
1386 return err;
1387 }
Thierry Redinge8dfad42019-03-21 19:10:02 +01001388
1389 /* store one or two address cells */
1390 if (na > 1)
1391 *ptr++ = cpu_to_fdt32(upper);
1392
1393 *ptr++ = cpu_to_fdt32(lower);
1394
1395 /* store one or two size cells */
Thierry Reding26130012019-04-15 10:08:21 +02001396 size = carveout->end - carveout->start + 1;
1397 upper = upper_32_bits(size);
1398 lower = lower_32_bits(size);
Thierry Redinge8dfad42019-03-21 19:10:02 +01001399
1400 if (ns > 1)
1401 *ptr++ = cpu_to_fdt32(upper);
1402
1403 *ptr++ = cpu_to_fdt32(lower);
1404
1405 err = fdt_setprop(blob, node, "reg", cells, (na + ns) * sizeof(*cells));
1406 if (err < 0)
1407 return err;
1408
Thierry Reding5e336912021-09-03 15:16:19 +02001409 if (compatibles && count > 0) {
1410 size_t length = 0, len = 0;
1411 unsigned int i;
1412 char *buffer;
1413
1414 for (i = 0; i < count; i++)
1415 length += strlen(compatibles[i]) + 1;
1416
1417 buffer = malloc(length);
1418 if (!buffer)
1419 return -FDT_ERR_INTERNAL;
1420
1421 for (i = 0; i < count; i++)
1422 len += strlcpy(buffer + len, compatibles[i],
1423 length - len) + 1;
1424
1425 err = fdt_setprop(blob, node, "compatible", buffer, length);
1426 free(buffer);
1427 if (err < 0)
1428 return err;
1429 }
1430
Thierry Redinge8dfad42019-03-21 19:10:02 +01001431 /* return the phandle for the new node for the caller to use */
1432 if (phandlep)
1433 *phandlep = phandle;
1434
1435 return 0;
1436}
1437
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001438int fdtdec_get_carveout(const void *blob, const char *node,
1439 const char *prop_name, unsigned int index,
Thierry Reding5e336912021-09-03 15:16:19 +02001440 struct fdt_memory *carveout, const char **name,
Thierry Redingbedc4392021-09-03 15:16:21 +02001441 const char ***compatiblesp, unsigned int *countp,
1442 unsigned long *flags)
Thierry Redinge629b0d2019-03-21 19:10:03 +01001443{
1444 const fdt32_t *prop;
1445 uint32_t phandle;
1446 int offset, len;
1447 fdt_size_t size;
1448
1449 offset = fdt_path_offset(blob, node);
1450 if (offset < 0)
1451 return offset;
1452
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001453 prop = fdt_getprop(blob, offset, prop_name, &len);
Thierry Redinge629b0d2019-03-21 19:10:03 +01001454 if (!prop) {
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001455 debug("failed to get %s for %s\n", prop_name, node);
Thierry Redinge629b0d2019-03-21 19:10:03 +01001456 return -FDT_ERR_NOTFOUND;
1457 }
1458
1459 if ((len % sizeof(phandle)) != 0) {
1460 debug("invalid phandle property\n");
1461 return -FDT_ERR_BADPHANDLE;
1462 }
1463
1464 if (len < (sizeof(phandle) * (index + 1))) {
1465 debug("invalid phandle index\n");
Thierry Reding3dcc4cf2021-09-03 15:16:17 +02001466 return -FDT_ERR_NOTFOUND;
Thierry Redinge629b0d2019-03-21 19:10:03 +01001467 }
1468
1469 phandle = fdt32_to_cpu(prop[index]);
1470
1471 offset = fdt_node_offset_by_phandle(blob, phandle);
1472 if (offset < 0) {
1473 debug("failed to find node for phandle %u\n", phandle);
1474 return offset;
1475 }
1476
Thierry Reding8a3c9fc2021-09-03 15:16:18 +02001477 if (name)
1478 *name = fdt_get_name(blob, offset, NULL);
1479
Thierry Reding5e336912021-09-03 15:16:19 +02001480 if (compatiblesp) {
1481 const char **compatibles = NULL;
1482 const char *start, *end, *ptr;
1483 unsigned int count = 0;
1484
1485 prop = fdt_getprop(blob, offset, "compatible", &len);
1486 if (!prop)
1487 goto skip_compat;
1488
1489 start = ptr = (const char *)prop;
1490 end = start + len;
1491
1492 while (ptr < end) {
1493 ptr = strchrnul(ptr, '\0');
1494 count++;
1495 ptr++;
1496 }
1497
1498 compatibles = malloc(sizeof(ptr) * count);
1499 if (!compatibles)
1500 return -FDT_ERR_INTERNAL;
1501
1502 ptr = start;
1503 count = 0;
1504
1505 while (ptr < end) {
1506 compatibles[count] = ptr;
1507 ptr = strchrnul(ptr, '\0');
1508 count++;
1509 ptr++;
1510 }
1511
1512skip_compat:
1513 *compatiblesp = compatibles;
1514
1515 if (countp)
1516 *countp = count;
1517 }
1518
Thierry Redinge629b0d2019-03-21 19:10:03 +01001519 carveout->start = fdtdec_get_addr_size_auto_noparent(blob, offset,
1520 "reg", 0, &size,
1521 true);
1522 if (carveout->start == FDT_ADDR_T_NONE) {
1523 debug("failed to read address/size from \"reg\" property\n");
1524 return -FDT_ERR_NOTFOUND;
1525 }
1526
1527 carveout->end = carveout->start + size - 1;
1528
Thierry Redingbedc4392021-09-03 15:16:21 +02001529 if (flags) {
1530 *flags = 0;
1531
1532 if (fdtdec_get_bool(blob, offset, "no-map"))
1533 *flags |= FDTDEC_RESERVED_MEMORY_NO_MAP;
1534 }
1535
Thierry Redinge629b0d2019-03-21 19:10:03 +01001536 return 0;
1537}
1538
1539int fdtdec_set_carveout(void *blob, const char *node, const char *prop_name,
Thierry Redingd6ad8072021-09-03 15:16:20 +02001540 unsigned int index, const struct fdt_memory *carveout,
1541 const char *name, const char **compatibles,
Thierry Redingbedc4392021-09-03 15:16:21 +02001542 unsigned int count, unsigned long flags)
Thierry Redinge629b0d2019-03-21 19:10:03 +01001543{
1544 uint32_t phandle;
Laurentiu Tudoref135222020-04-03 13:43:03 +03001545 int err, offset, len;
Thierry Redinge629b0d2019-03-21 19:10:03 +01001546 fdt32_t value;
Laurentiu Tudoref135222020-04-03 13:43:03 +03001547 void *prop;
Thierry Redinge629b0d2019-03-21 19:10:03 +01001548
Thierry Reding5e336912021-09-03 15:16:19 +02001549 err = fdtdec_add_reserved_memory(blob, name, carveout, compatibles,
Thierry Redingbedc4392021-09-03 15:16:21 +02001550 count, &phandle, flags);
Thierry Redinge629b0d2019-03-21 19:10:03 +01001551 if (err < 0) {
1552 debug("failed to add reserved memory: %d\n", err);
1553 return err;
1554 }
1555
1556 offset = fdt_path_offset(blob, node);
1557 if (offset < 0) {
1558 debug("failed to find offset for node %s: %d\n", node, offset);
1559 return offset;
1560 }
1561
1562 value = cpu_to_fdt32(phandle);
1563
Laurentiu Tudoref135222020-04-03 13:43:03 +03001564 if (!fdt_getprop(blob, offset, prop_name, &len)) {
1565 if (len == -FDT_ERR_NOTFOUND)
1566 len = 0;
1567 else
1568 return len;
1569 }
1570
1571 if ((index + 1) * sizeof(value) > len) {
1572 err = fdt_setprop_placeholder(blob, offset, prop_name,
1573 (index + 1) * sizeof(value),
1574 &prop);
1575 if (err < 0) {
1576 debug("failed to resize reserved memory property: %s\n",
1577 fdt_strerror(err));
1578 return err;
1579 }
1580 }
1581
1582 err = fdt_setprop_inplace_namelen_partial(blob, offset, prop_name,
1583 strlen(prop_name),
1584 index * sizeof(value),
1585 &value, sizeof(value));
Thierry Redinge629b0d2019-03-21 19:10:03 +01001586 if (err < 0) {
Laurentiu Tudoref135222020-04-03 13:43:03 +03001587 debug("failed to update %s property for node %s: %s\n",
1588 prop_name, node, fdt_strerror(err));
Thierry Redinge629b0d2019-03-21 19:10:03 +01001589 return err;
1590 }
1591
1592 return 0;
1593}
1594
Simon Glass37604392021-12-16 20:59:35 -07001595/* TODO(sjg@chromium.org): This function should not be weak */
Marek Vasutedeb6542020-04-11 21:18:59 +02001596__weak int fdtdec_board_setup(const void *fdt_blob)
1597{
1598 return 0;
1599}
1600
Simon Glasse1d46fa2021-12-16 20:59:24 -07001601/**
1602 * setup_multi_dtb_fit() - locate the correct dtb from a FIT
1603 *
1604 * This supports the CONFIG_MULTI_DTB_FIT feature, looking for the dtb in a
1605 * supplied FIT
1606 *
1607 * It accepts the current value of gd->fdt_blob, which points to the FIT, then
1608 * updates that gd->fdt_blob, to point to the chosen dtb so that U-Boot uses the
1609 * correct one
1610 */
1611static void setup_multi_dtb_fit(void)
1612{
Simon Glasse1d46fa2021-12-16 20:59:24 -07001613 void *blob;
1614
1615 /*
1616 * Try and uncompress the blob.
1617 * Unfortunately there is no way to know how big the input blob really
1618 * is. So let us set the maximum input size arbitrarily high. 16MB
1619 * ought to be more than enough for packed DTBs.
1620 */
1621 if (uncompress_blob(gd->fdt_blob, 0x1000000, &blob) == 0)
1622 gd->fdt_blob = blob;
1623
1624 /*
1625 * Check if blob is a FIT images containings DTBs.
1626 * If so, pick the most relevant
1627 */
1628 blob = locate_dtb_in_fit(gd->fdt_blob);
1629 if (blob) {
Simon Glass69daf5a2021-12-16 20:59:25 -07001630 gd_set_multi_dtb_fit(gd->fdt_blob);
Simon Glasse1d46fa2021-12-16 20:59:24 -07001631 gd->fdt_blob = blob;
Simon Glassbed6bc72021-12-16 20:59:33 -07001632 gd->fdt_src = FDTSRC_FIT;
Simon Glasse1d46fa2021-12-16 20:59:24 -07001633 }
Simon Glasse1d46fa2021-12-16 20:59:24 -07001634}
1635
Simon Glassa0877672015-02-27 22:06:35 -07001636int fdtdec_setup(void)
Simon Glass26b78b22015-02-27 22:06:34 -07001637{
Marek Vasutedeb6542020-04-11 21:18:59 +02001638 int ret;
Simon Glass2a3789b2021-12-16 20:59:30 -07001639
1640 /* The devicetree is typically appended to U-Boot */
Simon Glassbed6bc72021-12-16 20:59:33 -07001641 if (IS_ENABLED(CONFIG_OF_SEPARATE)) {
Simon Glass28b76512021-12-16 20:59:31 -07001642 gd->fdt_blob = fdt_find_separate();
Simon Glassbed6bc72021-12-16 20:59:33 -07001643 gd->fdt_src = FDTSRC_SEPARATE;
1644 } else { /* embed dtb in ELF file for testing / development */
Simon Glass28b76512021-12-16 20:59:31 -07001645 gd->fdt_blob = dtb_dt_embedded();
Simon Glassbed6bc72021-12-16 20:59:33 -07001646 gd->fdt_src = FDTSRC_EMBED;
1647 }
Simon Glass28b76512021-12-16 20:59:31 -07001648
1649 /* Allow the board to override the fdt address. */
1650 if (IS_ENABLED(CONFIG_OF_BOARD)) {
Simon Glass2a3789b2021-12-16 20:59:30 -07001651 gd->fdt_blob = board_fdt_blob_setup(&ret);
1652 if (ret)
1653 return ret;
Simon Glassbed6bc72021-12-16 20:59:33 -07001654 gd->fdt_src = FDTSRC_BOARD;
Simon Glass2a3789b2021-12-16 20:59:30 -07001655 }
1656
Simon Glassbed6bc72021-12-16 20:59:33 -07001657 /* Allow the early environment to override the fdt address */
Simon Glassbcafa8c2021-12-16 20:59:28 -07001658 if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
Simon Glassbed6bc72021-12-16 20:59:33 -07001659 ulong addr;
1660
1661 addr = env_get_hex("fdtcontroladdr", 0);
1662 if (addr) {
1663 gd->fdt_blob = map_sysmem(addr, 0);
1664 gd->fdt_src = FDTSRC_ENV;
1665 }
Simon Glassbcafa8c2021-12-16 20:59:28 -07001666 }
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001667
Simon Glasse1d46fa2021-12-16 20:59:24 -07001668 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT))
1669 setup_multi_dtb_fit();
Jean-Jacques Hiblota2fee162017-09-15 12:57:32 +02001670
Marek Vasutedeb6542020-04-11 21:18:59 +02001671 ret = fdtdec_prepare_fdt();
1672 if (!ret)
1673 ret = fdtdec_board_setup(gd->fdt_blob);
Simon Glassba8457b2022-09-06 20:27:19 -06001674 oftree_reset();
1675
Marek Vasutedeb6542020-04-11 21:18:59 +02001676 return ret;
Simon Glass26b78b22015-02-27 22:06:34 -07001677}
1678
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001679int fdtdec_resetup(int *rescan)
1680{
1681 void *fdt_blob;
1682
1683 /*
1684 * If the current DTB is part of a compressed FIT image,
1685 * try to locate the best match from the uncompressed
1686 * FIT image stillpresent there. Save the time and space
1687 * required to uncompress it again.
1688 */
Simon Glass69daf5a2021-12-16 20:59:25 -07001689 if (gd_multi_dtb_fit()) {
1690 fdt_blob = locate_dtb_in_fit(gd_multi_dtb_fit());
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001691
1692 if (fdt_blob == gd->fdt_blob) {
1693 /*
1694 * The best match did not change. no need to tear down
1695 * the DM and rescan the fdt.
1696 */
1697 *rescan = 0;
1698 return 0;
1699 }
1700
1701 *rescan = 1;
1702 gd->fdt_blob = fdt_blob;
1703 return fdtdec_prepare_fdt();
1704 }
1705
1706 /*
1707 * If multi_dtb_fit is NULL, it means that blob appended to u-boot is
1708 * not a FIT image containings DTB, but a single DTB. There is no need
1709 * to teard down DM and rescan the DT in this case.
1710 */
1711 *rescan = 0;
1712 return 0;
1713}
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +01001714
Michael Prattfa28d142018-06-11 13:07:09 -06001715int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001716 phys_addr_t *basep, phys_size_t *sizep,
1717 struct bd_info *bd)
Michael Prattfa28d142018-06-11 13:07:09 -06001718{
1719 int addr_cells, size_cells;
1720 const u32 *cell, *end;
1721 u64 total_size, size, addr;
1722 int node, child;
1723 bool auto_size;
1724 int bank;
1725 int len;
1726
1727 debug("%s: board_id=%d\n", __func__, board_id);
1728 if (!area)
1729 area = "/memory";
1730 node = fdt_path_offset(blob, area);
1731 if (node < 0) {
1732 debug("No %s node found\n", area);
1733 return -ENOENT;
1734 }
1735
1736 cell = fdt_getprop(blob, node, "reg", &len);
1737 if (!cell) {
1738 debug("No reg property found\n");
1739 return -ENOENT;
1740 }
1741
1742 addr_cells = fdt_address_cells(blob, node);
1743 size_cells = fdt_size_cells(blob, node);
1744
1745 /* Check the board id and mask */
1746 for (child = fdt_first_subnode(blob, node);
1747 child >= 0;
1748 child = fdt_next_subnode(blob, child)) {
1749 int match_mask, match_value;
1750
1751 match_mask = fdtdec_get_int(blob, child, "match-mask", -1);
1752 match_value = fdtdec_get_int(blob, child, "match-value", -1);
1753
1754 if (match_value >= 0 &&
1755 ((board_id & match_mask) == match_value)) {
1756 /* Found matching mask */
1757 debug("Found matching mask %d\n", match_mask);
1758 node = child;
1759 cell = fdt_getprop(blob, node, "reg", &len);
1760 if (!cell) {
1761 debug("No memory-banks property found\n");
1762 return -EINVAL;
1763 }
1764 break;
1765 }
1766 }
1767 /* Note: if no matching subnode was found we use the parent node */
1768
1769 if (bd) {
1770 memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) *
1771 CONFIG_NR_DRAM_BANKS);
1772 }
1773
1774 auto_size = fdtdec_get_bool(blob, node, "auto-size");
1775
1776 total_size = 0;
1777 end = cell + len / 4 - addr_cells - size_cells;
1778 debug("cell at %p, end %p\n", cell, end);
1779 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1780 if (cell > end)
1781 break;
1782 addr = 0;
1783 if (addr_cells == 2)
1784 addr += (u64)fdt32_to_cpu(*cell++) << 32UL;
1785 addr += fdt32_to_cpu(*cell++);
1786 if (bd)
1787 bd->bi_dram[bank].start = addr;
1788 if (basep && !bank)
1789 *basep = (phys_addr_t)addr;
1790
1791 size = 0;
1792 if (size_cells == 2)
1793 size += (u64)fdt32_to_cpu(*cell++) << 32UL;
1794 size += fdt32_to_cpu(*cell++);
1795
1796 if (auto_size) {
1797 u64 new_size;
1798
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001799 debug("Auto-sizing %llx, size %llx: ", addr, size);
Michael Prattfa28d142018-06-11 13:07:09 -06001800 new_size = get_ram_size((long *)(uintptr_t)addr, size);
1801 if (new_size == size) {
1802 debug("OK\n");
1803 } else {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001804 debug("sized to %llx\n", new_size);
Michael Prattfa28d142018-06-11 13:07:09 -06001805 size = new_size;
1806 }
1807 }
1808
1809 if (bd)
1810 bd->bi_dram[bank].size = size;
1811 total_size += size;
1812 }
1813
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001814 debug("Memory size %llu\n", total_size);
Michael Prattfa28d142018-06-11 13:07:09 -06001815 if (sizep)
1816 *sizep = (phys_size_t)total_size;
1817
1818 return 0;
1819}
Michael Prattfa28d142018-06-11 13:07:09 -06001820
Simon Glass26b78b22015-02-27 22:06:34 -07001821#endif /* !USE_HOSTCC */