blob: 2bd80a9dfb18e5d0fcb5605d9e1bce0ff53fdbb6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -04002/*
3 * (C) Copyright 2007
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 *
Shengzhou Liua7be8022011-10-14 16:26:05 +08006 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -04007 */
8
9#include <common.h>
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +020010#include <abuf.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Heinrich Schuchardt668cabc2018-11-18 17:58:51 +010013#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <net.h>
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040015#include <stdio_dev.h>
Patrick Delaunaye0682262023-06-08 17:16:37 +020016#include <dm/ofnode.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040017#include <linux/ctype.h>
18#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040019#include <asm/global_data.h>
Sam Protsenko94483d22024-03-29 19:55:53 -050020#include <asm/unaligned.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090021#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040022#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060023#include <exports.h>
Bin Mengd9a00932015-12-07 01:39:47 -080024#include <fdtdec.h>
Francesco Dolcini05cc8272022-05-19 16:22:26 +020025#include <version.h>
Devarsh Thakkar31199892024-02-22 18:38:10 +053026#include <video.h>
27
28DECLARE_GLOBAL_DATA_PTR;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040029
Kumar Gala1ba00402008-10-23 00:05:47 -050030/**
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020031 * fdt_getprop_u32_default_node - Return a node's property or a default
32 *
33 * @fdt: ptr to device tree
34 * @off: offset of node
35 * @cell: cell offset in property
36 * @prop: property name
37 * @dflt: default value if the property isn't found
38 *
39 * Convenience function to return a node's property or a default value if
40 * the property doesn't exist.
41 */
42u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
43 const char *prop, const u32 dflt)
44{
45 const fdt32_t *val;
46 int len;
47
48 val = fdt_getprop(fdt, off, prop, &len);
49
50 /* Check if property exists */
51 if (!val)
52 return dflt;
53
54 /* Check if property is long enough */
55 if (len < ((cell + 1) * sizeof(uint32_t)))
56 return dflt;
57
58 return fdt32_to_cpu(*val);
59}
60
61/**
Kumar Gala1ba00402008-10-23 00:05:47 -050062 * fdt_getprop_u32_default - Find a node and return it's property or a default
63 *
64 * @fdt: ptr to device tree
65 * @path: path of node
66 * @prop: property name
67 * @dflt: default value if the property isn't found
68 *
69 * Convenience function to find a node and return it's property or a
70 * default value if it doesn't exist.
71 */
Gabe Blackcd37de72011-11-08 01:09:44 -080072u32 fdt_getprop_u32_default(const void *fdt, const char *path,
73 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050074{
Kumar Gala1ba00402008-10-23 00:05:47 -050075 int off;
76
77 off = fdt_path_offset(fdt, path);
78 if (off < 0)
79 return dflt;
80
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020081 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala1ba00402008-10-23 00:05:47 -050082}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040083
Kumar Galabf771dc2007-10-24 10:21:57 -050084/**
85 * fdt_find_and_setprop: Find a node and set it's property
86 *
87 * @fdt: ptr to device tree
88 * @node: path of node
89 * @prop: property name
90 * @val: ptr to new value
91 * @len: length of new property value
92 * @create: flag to create the property if it doesn't exist
93 *
94 * Convenience function to directly set a property given the path to the node.
95 */
96int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
97 const void *val, int len, int create)
98{
Kumar Galac8ab7052007-10-24 11:04:22 -050099 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -0500100
101 if (nodeoff < 0)
102 return nodeoff;
103
Kim Phillips6542c072013-01-16 14:00:11 +0000104 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500105 return 0; /* create flag not set; so exit quietly */
106
107 return fdt_setprop(fdt, nodeoff, prop, val, len);
108}
109
Masahiro Yamadac0207282014-04-18 17:40:58 +0900110/**
Simon Glass014aa052014-10-23 18:58:49 -0600111 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
112 *
Masahiro Yamadac0207282014-04-18 17:40:58 +0900113 * @fdt: pointer to the device tree blob
114 * @parentoffset: structure block offset of a node
115 * @name: name of the subnode to locate
116 *
117 * fdt_subnode_offset() finds a subnode of the node with a given name.
118 * If the subnode does not exist, it will be created.
119 */
Simon Glass014aa052014-10-23 18:58:49 -0600120int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamadac0207282014-04-18 17:40:58 +0900121{
122 int offset;
123
124 offset = fdt_subnode_offset(fdt, parentoffset, name);
125
126 if (offset == -FDT_ERR_NOTFOUND)
127 offset = fdt_add_subnode(fdt, parentoffset, name);
128
129 if (offset < 0)
130 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
131
132 return offset;
133}
134
Andre Przywara0d1fcf82021-02-14 10:35:18 +0000135#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel46e192a2008-06-20 22:24:05 +0200136static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600137{
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900138 int err;
139 int aliasoff;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600140 char sername[9] = { 0 };
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900141 const void *path;
142 int len;
143 char tmp[256]; /* long enough */
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600144
Bin Mengb8199112016-01-13 19:38:58 -0800145 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600146
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900147 aliasoff = fdt_path_offset(fdt, "/aliases");
148 if (aliasoff < 0) {
149 err = aliasoff;
Scott Woodedd551f2015-09-01 22:48:08 -0500150 goto noalias;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600151 }
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900152
153 path = fdt_getprop(fdt, aliasoff, sername, &len);
154 if (!path) {
155 err = len;
Scott Woodedd551f2015-09-01 22:48:08 -0500156 goto noalias;
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900157 }
158
159 /* fdt_setprop may break "path" so we copy it to tmp buffer */
160 memcpy(tmp, path, len);
161
162 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600163 if (err < 0)
164 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900165 fdt_strerror(err));
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600166
167 return err;
Scott Woodedd551f2015-09-01 22:48:08 -0500168
169noalias:
170 printf("WARNING: %s: could not read %s alias: %s\n",
171 __func__, sername, fdt_strerror(err));
172
173 return 0;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600174}
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900175#else
176static int fdt_fixup_stdout(void *fdt, int chosenoff)
177{
178 return 0;
179}
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600180#endif
181
Masahiro Yamadab891a342014-04-18 17:41:04 +0900182static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
183 uint64_t val, int is_u64)
184{
185 if (is_u64)
186 return fdt_setprop_u64(fdt, nodeoffset, name, val);
187 else
188 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
189}
190
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200191int fdt_root(void *fdt)
192{
193 char *serial;
194 int err;
195
196 err = fdt_check_header(fdt);
197 if (err < 0) {
198 printf("fdt_root: %s\n", fdt_strerror(err));
199 return err;
200 }
201
Simon Glass64b723f2017-08-03 12:22:12 -0600202 serial = env_get("serial#");
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200203 if (serial) {
204 err = fdt_setprop(fdt, 0, "serial-number", serial,
205 strlen(serial) + 1);
206
207 if (err < 0) {
208 printf("WARNING: could not set serial-number %s.\n",
209 fdt_strerror(err));
210 return err;
211 }
212 }
213
214 return 0;
215}
Masahiro Yamadab891a342014-04-18 17:41:04 +0900216
Masahiro Yamada5b114892014-04-18 17:40:59 +0900217int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400218{
Masahiro Yamadab891a342014-04-18 17:41:04 +0900219 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500220 int err, j, total;
Masahiro Yamadab891a342014-04-18 17:41:04 +0900221 int is_u64;
Kumar Gala99ebc052008-08-15 08:24:43 -0500222 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400223
Masahiro Yamada9a3b4de2014-04-18 17:41:05 +0900224 /* just return if the size of initrd is zero */
225 if (initrd_start == initrd_end)
226 return 0;
227
Masahiro Yamadac0207282014-04-18 17:40:58 +0900228 /* find or create "/chosen" node. */
229 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
230 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500231 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400232
Kumar Gala99ebc052008-08-15 08:24:43 -0500233 total = fdt_num_mem_rsv(fdt);
234
235 /*
236 * Look for an existing entry and update it. If we don't find
237 * the entry, we will j be the next available slot.
238 */
239 for (j = 0; j < total; j++) {
240 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
241 if (addr == initrd_start) {
242 fdt_del_mem_rsv(fdt, j);
243 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400244 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500245 }
246
Grant Likelyc379a562011-03-28 09:58:55 +0000247 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500248 if (err < 0) {
249 printf("fdt_initrd: %s\n", fdt_strerror(err));
250 return err;
251 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500252
Simon Glass9fbc6322014-10-23 18:58:57 -0600253 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengd9850132013-12-14 11:47:29 +0800254
Masahiro Yamadab891a342014-04-18 17:41:04 +0900255 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
256 (uint64_t)initrd_start, is_u64);
257
Masahiro Yamada5b114892014-04-18 17:40:59 +0900258 if (err < 0) {
259 printf("WARNING: could not set linux,initrd-start %s.\n",
260 fdt_strerror(err));
261 return err;
262 }
Masahiro Yamadab891a342014-04-18 17:41:04 +0900263
264 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
265 (uint64_t)initrd_end, is_u64);
266
Masahiro Yamada5b114892014-04-18 17:40:59 +0900267 if (err < 0) {
268 printf("WARNING: could not set linux,initrd-end %s.\n",
269 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500270
Masahiro Yamada5b114892014-04-18 17:40:59 +0900271 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400272 }
273
Kumar Gala99ebc052008-08-15 08:24:43 -0500274 return 0;
275}
276
Niko Mauno673db432021-02-22 19:18:51 +0000277/**
278 * board_fdt_chosen_bootargs - boards may override this function to use
279 * alternative kernel command line arguments
280 */
281__weak char *board_fdt_chosen_bootargs(void)
282{
283 return env_get("bootargs");
284}
285
Masahiro Yamada451c2042014-04-18 17:41:00 +0900286int fdt_chosen(void *fdt)
Kumar Gala99ebc052008-08-15 08:24:43 -0500287{
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200288 struct abuf buf = {};
Kumar Gala99ebc052008-08-15 08:24:43 -0500289 int nodeoffset;
290 int err;
291 char *str; /* used to set string properties */
Kumar Gala99ebc052008-08-15 08:24:43 -0500292
293 err = fdt_check_header(fdt);
294 if (err < 0) {
295 printf("fdt_chosen: %s\n", fdt_strerror(err));
296 return err;
297 }
298
Masahiro Yamadac0207282014-04-18 17:40:58 +0900299 /* find or create "/chosen" node. */
300 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
301 if (nodeoffset < 0)
302 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400303
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200304 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
305 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
306 abuf_data(&buf), abuf_size(&buf));
307 abuf_uninit(&buf);
308 if (err < 0) {
309 printf("WARNING: could not set rng-seed %s.\n",
310 fdt_strerror(err));
311 return err;
312 }
313 }
314
Niko Mauno673db432021-02-22 19:18:51 +0000315 str = board_fdt_chosen_bootargs();
316
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900317 if (str) {
318 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
319 strlen(str) + 1);
320 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900321 printf("WARNING: could not set bootargs %s.\n",
322 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900323 return err;
324 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400325 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600326
Francesco Dolcini05cc8272022-05-19 16:22:26 +0200327 /* add u-boot version */
328 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
329 strlen(PLAIN_VERSION) + 1);
330 if (err < 0) {
331 printf("WARNING: could not set u-boot,version %s.\n",
332 fdt_strerror(err));
333 return err;
334 }
335
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900336 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400337}
338
Kumar Gala7e64cf82007-11-03 19:46:28 -0500339void do_fixup_by_path(void *fdt, const char *path, const char *prop,
340 const void *val, int len, int create)
341{
342#if defined(DEBUG)
343 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600344 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500345 for (i = 0; i < len; i++)
346 debug(" %.2x", *(u8*)(val+i));
347 debug("\n");
348#endif
349 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
350 if (rc)
351 printf("Unable to update property %s:%s, err=%s\n",
352 path, prop, fdt_strerror(rc));
353}
354
355void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
356 u32 val, int create)
357{
Kim Phillips6542c072013-01-16 14:00:11 +0000358 fdt32_t tmp = cpu_to_fdt32(val);
359 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500360}
361
Kumar Gala7590a192007-11-21 13:30:15 -0600362void do_fixup_by_prop(void *fdt,
363 const char *pname, const void *pval, int plen,
364 const char *prop, const void *val, int len,
365 int create)
366{
367 int off;
368#if defined(DEBUG)
369 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600370 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600371 for (i = 0; i < len; i++)
372 debug(" %.2x", *(u8*)(val+i));
373 debug("\n");
374#endif
375 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
376 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000377 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600378 fdt_setprop(fdt, off, prop, val, len);
379 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
380 }
381}
382
383void do_fixup_by_prop_u32(void *fdt,
384 const char *pname, const void *pval, int plen,
385 const char *prop, u32 val, int create)
386{
Kim Phillips6542c072013-01-16 14:00:11 +0000387 fdt32_t tmp = cpu_to_fdt32(val);
388 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600389}
390
391void do_fixup_by_compat(void *fdt, const char *compat,
392 const char *prop, const void *val, int len, int create)
393{
394 int off = -1;
395#if defined(DEBUG)
396 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600397 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600398 for (i = 0; i < len; i++)
399 debug(" %.2x", *(u8*)(val+i));
400 debug("\n");
401#endif
Marek Behún5d6b4482022-01-20 01:04:42 +0100402 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips6542c072013-01-16 14:00:11 +0000403 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600404 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala7590a192007-11-21 13:30:15 -0600405}
406
407void do_fixup_by_compat_u32(void *fdt, const char *compat,
408 const char *prop, u32 val, int create)
409{
Kim Phillips6542c072013-01-16 14:00:11 +0000410 fdt32_t tmp = cpu_to_fdt32(val);
411 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600412}
413
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900414#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900415/*
416 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
417 */
Simon Glass0058e112014-10-23 18:58:55 -0600418static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
419 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900420{
421 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100422 int address_cells = fdt_address_cells(fdt, 0);
423 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900424 char *p = buf;
425
426 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100427 if (address_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500428 put_unaligned_be64(address[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900429 else
430 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100431 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900432
Hans de Goede4c80fb02014-11-28 14:23:51 +0100433 if (size_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500434 put_unaligned_be64(size[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900435 else
436 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100437 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900438 }
439
440 return p - (char *)buf;
441}
442
Ramon Fried9cab1832018-08-14 00:35:42 +0300443#if CONFIG_NR_DRAM_BANKS > 4
444#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
445#else
Kim Phillips6542c072013-01-16 14:00:11 +0000446#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300447#endif
Michal Simek96283732021-08-10 09:21:54 +0200448
449/**
450 * fdt_fixup_memory_banks - Update DT memory node
451 * @blob: Pointer to DT blob
452 * @start: Pointer to memory start addresses array
453 * @size: Pointer to memory sizes array
454 * @banks: Number of memory banks
455 *
456 * Return: 0 on success, negative value on failure
457 *
458 * Based on the passed number of banks and arrays, the function is able to
459 * update existing DT memory nodes to match run time detected/changed memory
460 * configuration. Implementation is handling one specific case with only one
461 * memory node where multiple tuples could be added/updated.
462 * The case where multiple memory nodes with a single tuple (base, size) are
463 * used, this function is only updating the first memory node without removing
464 * others.
465 */
John Rigbyb4ae9422010-10-13 13:57:33 -0600466int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
467{
468 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100469 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000470 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600471
Kim Phillips6542c072013-01-16 14:00:11 +0000472 if (banks > MEMORY_BANKS_MAX) {
473 printf("%s: num banks %d exceeds hardcoded limit %d."
474 " Recompile with higher MEMORY_BANKS_MAX?\n",
475 __FUNCTION__, banks, MEMORY_BANKS_MAX);
476 return -1;
477 }
478
Kumar Gala0db72ed2007-11-26 14:57:45 -0600479 err = fdt_check_header(blob);
480 if (err < 0) {
481 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
482 return err;
483 }
484
Masahiro Yamadac0207282014-04-18 17:40:58 +0900485 /* find or create "/memory" node. */
486 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
487 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800488 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900489
Kumar Gala0db72ed2007-11-26 14:57:45 -0600490 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
491 sizeof("memory"));
492 if (err < 0) {
493 printf("WARNING: could not set %s %s.\n", "device_type",
494 fdt_strerror(err));
495 return err;
496 }
497
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100498 for (i = 0; i < banks; i++) {
499 if (start[i] == 0 && size[i] == 0)
500 break;
501 }
502
503 banks = i;
504
Andre Przywara3d2d4412015-06-21 00:29:54 +0100505 if (!banks)
506 return 0;
507
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900508 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600509
510 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
511 if (err < 0) {
512 printf("WARNING: could not set %s %s.\n",
513 "reg", fdt_strerror(err));
514 return err;
515 }
516 return 0;
517}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200518
519int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
520{
521 int err, nodeoffset;
522 int len;
523 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
524
525 if (areas > 8) {
526 printf("%s: num areas %d exceeds hardcoded limit %d\n",
527 __func__, areas, 8);
528 return -1;
529 }
530
531 err = fdt_check_header(blob);
532 if (err < 0) {
533 printf("%s: %s\n", __func__, fdt_strerror(err));
534 return err;
535 }
536
537 /* find or create "/memory" node. */
538 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
539 if (nodeoffset < 0)
540 return nodeoffset;
541
542 len = fdt_pack_reg(blob, tmp, start, size, areas);
543
544 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
545 if (err < 0) {
546 printf("WARNING: could not set %s %s.\n",
547 "reg", fdt_strerror(err));
548 return err;
549 }
550
551 return 0;
552}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900553#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600554
John Rigbyb4ae9422010-10-13 13:57:33 -0600555int fdt_fixup_memory(void *blob, u64 start, u64 size)
556{
557 return fdt_fixup_memory_banks(blob, &start, &size, 1);
558}
559
Kumar Galafabda922008-08-19 15:41:18 -0500560void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600561{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530562 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800563 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000564 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600565 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100566 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800567 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530568#ifdef FDT_SEQ_MACADDR_FROM_ENV
569 int nodeoff;
570 const struct fdt_property *fdt_prop;
571#endif
Kumar Gala22699102007-11-21 11:11:03 -0600572
Lev Iserovichcde77612016-01-07 18:04:16 -0500573 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500574 return;
575
Lev Iserovichcde77612016-01-07 18:04:16 -0500576 /* Cycle through all aliases */
577 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800578 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500579
Lev Iserovichcde77612016-01-07 18:04:16 -0500580 /* FDT might have been edited, recompute the offset */
581 offset = fdt_first_property_offset(fdt,
582 fdt_path_offset(fdt, "/aliases"));
583 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530584 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500585 offset = fdt_next_property_offset(fdt, offset);
586
587 if (offset < 0)
588 break;
589
Bin Mengbfb93cc2015-11-03 04:24:41 -0800590 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200591 if (!strncmp(name, "ethernet", 8)) {
592 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530593 if (!strcmp(name, "ethernet")
594#ifdef FDT_SEQ_MACADDR_FROM_ENV
595 || !strcmp(name, "ethernet0")
596#endif
597 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200598 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530599#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200600 else
601 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530602#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800603 if (i != -1) {
604 if (i == 0)
605 strcpy(mac, "ethaddr");
606 else
607 sprintf(mac, "eth%daddr", i);
608 } else {
609 continue;
610 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530611#ifdef FDT_SEQ_MACADDR_FROM_ENV
612 nodeoff = fdt_path_offset(fdt, path);
613 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
614 NULL);
615 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
616 continue;
617 i++;
618#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600619 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800620 if (!tmp)
621 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500622
Bin Mengbfb93cc2015-11-03 04:24:41 -0800623 for (j = 0; j < 6; j++) {
624 mac_addr[j] = tmp ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600625 hextoul(tmp, &end) : 0;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800626 if (tmp)
627 tmp = (*end) ? end + 1 : end;
628 }
Kumar Galafabda922008-08-19 15:41:18 -0500629
Bin Mengbfb93cc2015-11-03 04:24:41 -0800630 do_fixup_by_path(fdt, path, "mac-address",
631 &mac_addr, 6, 0);
632 do_fixup_by_path(fdt, path, "local-mac-address",
633 &mac_addr, 6, 1);
634 }
Kumar Gala22699102007-11-21 11:11:03 -0600635 }
636}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300637
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100638int fdt_record_loadable(void *blob, u32 index, const char *name,
639 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simek0e4f43d2021-05-27 11:40:09 +0200640 const char *type, const char *os, const char *arch)
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100641{
642 int err, node;
643
644 err = fdt_check_header(blob);
645 if (err < 0) {
646 printf("%s: %s\n", __func__, fdt_strerror(err));
647 return err;
648 }
649
650 /* find or create "/fit-images" node */
651 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
652 if (node < 0)
653 return node;
654
655 /* find or create "/fit-images/<name>" node */
656 node = fdt_find_or_add_subnode(blob, node, name);
657 if (node < 0)
658 return node;
659
Michal Simekd31b40f2020-09-03 12:44:51 +0200660 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100661 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200662 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100663 fdt_setprop_u32(blob, node, "size", size);
664 if (type)
665 fdt_setprop_string(blob, node, "type", type);
666 if (os)
667 fdt_setprop_string(blob, node, "os", os);
Michal Simek0e4f43d2021-05-27 11:40:09 +0200668 if (arch)
669 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100670
671 return node;
672}
673
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200674int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500675{
676 int i;
677 uint64_t addr, size;
678 int total, ret;
679 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200680 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500681
682 if (!blob)
683 return 0;
684
685 total = fdt_num_mem_rsv(blob);
686 for (i = 0; i < total; i++) {
687 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000688 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500689 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200690 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500691 break;
692 }
693 }
694
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100695 /*
696 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200697 * plus the size needed for 5 fdt_add_mem_rsv, one
698 * for the fdt itself and 4 for a possible initrd
699 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100700 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500701 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200702 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500703
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200704 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500705 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000706 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
707 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500708
709 /* Change the fdt header to reflect the correct size */
710 fdt_set_totalsize(blob, actualsize);
711
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200712 if (fdt_memrsv) {
713 /* Add the new reservation */
714 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
715 if (ret < 0)
716 return ret;
717 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500718
719 return actualsize;
720}
Kumar Galae0475cd2008-10-22 23:33:56 -0500721
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100722/**
723 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
724 *
725 * @blob: ptr to device tree
726 */
727int fdt_delete_disabled_nodes(void *blob)
728{
729 while (1) {
730 int ret, offset;
731
732 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
733 "disabled", 9);
734 if (offset < 0)
735 break;
736
737 ret = fdt_del_node(blob, offset);
738 if (ret < 0)
739 return ret;
740 }
741
742 return 0;
743}
744
Kumar Galae0475cd2008-10-22 23:33:56 -0500745#ifdef CONFIG_PCI
Tom Rini56af6592022-11-16 13:10:33 -0500746#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500747
748#define FDT_PCI_PREFETCH (0x40000000)
749#define FDT_PCI_MEM32 (0x02000000)
750#define FDT_PCI_IO (0x01000000)
751#define FDT_PCI_MEM64 (0x03000000)
752
753int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
754
755 int addrcell, sizecell, len, r;
756 u32 *dma_range;
757 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Rini56af6592022-11-16 13:10:33 -0500758 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Galae0475cd2008-10-22 23:33:56 -0500759
760 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
761 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
762
763 dma_range = &dma_ranges[0];
764 for (r = 0; r < hose->region_count; r++) {
765 u64 bus_start, phys_start, size;
766
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600767 /* skip if !PCI_REGION_SYS_MEMORY */
768 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500769 continue;
770
771 bus_start = (u64)hose->regions[r].bus_start;
772 phys_start = (u64)hose->regions[r].phys_start;
773 size = (u64)hose->regions[r].size;
774
775 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500776 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200777 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500778 else
Marek Vasut22203f52019-07-09 02:49:29 +0200779 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500780 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200781 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500782#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200783 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500784#else
785 dma_range[1] = 0;
786#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200787 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500788
789 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200790 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
791 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500792 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200793 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500794 }
795
796 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200797 dma_range[3 + addrcell + 0] =
798 cpu_to_fdt32(size >> 32);
799 dma_range[3 + addrcell + 1] =
800 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500801 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200802 dma_range[3 + addrcell + 0] =
803 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500804 }
805
806 dma_range += (3 + addrcell + sizecell);
807 }
808
809 len = dma_range - &dma_ranges[0];
810 if (len)
811 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
812
813 return 0;
814}
815#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200816
Matthew McClintockacda3a42010-10-13 13:39:26 +0200817int fdt_increase_size(void *fdt, int add_len)
818{
819 int newlen;
820
821 newlen = fdt_totalsize(fdt) + add_len;
822
823 /* Open in place with a new len */
824 return fdt_open_into(fdt, fdt, newlen);
825}
826
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100827#ifdef CONFIG_FDT_FIXUP_PARTITIONS
828#include <jffs2/load_kernel.h>
829#include <mtd_node.h>
830
Michal Simekc2ed0072018-07-31 14:31:04 +0200831static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100832{
833 int off, ndepth;
834 int ret;
835
836 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
837 (off >= 0) && (ndepth > 0);
838 off = fdt_next_node(blob, off, &ndepth)) {
839 if (ndepth == 1) {
840 debug("delete %s: offset: %x\n",
841 fdt_get_name(blob, off, 0), off);
842 ret = fdt_del_node((void *)blob, off);
843 if (ret < 0) {
844 printf("Can't delete node: %s\n",
845 fdt_strerror(ret));
846 return ret;
847 } else {
848 ndepth = 0;
849 off = parent_offset;
850 }
851 }
852 }
853 return 0;
854}
855
Michal Simekc2ed0072018-07-31 14:31:04 +0200856static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100857{
858 const void *prop;
859 int ndepth = 0;
860 int off;
861 int ret;
862
863 off = fdt_next_node(blob, parent_offset, &ndepth);
864 if (off > 0 && ndepth == 1) {
865 prop = fdt_getprop(blob, off, "label", NULL);
866 if (prop == NULL) {
867 /*
868 * Could not find label property, nand {}; node?
869 * Check subnode, delete partitions there if any.
870 */
871 return fdt_del_partitions(blob, off);
872 } else {
873 ret = fdt_del_subnodes(blob, parent_offset);
874 if (ret < 0) {
875 printf("Can't remove subnodes: %s\n",
876 fdt_strerror(ret));
877 return ret;
878 }
879 }
880 }
881 return 0;
882}
883
Masahiro Yamada65669602020-07-15 19:35:47 +0900884static int fdt_node_set_part_info(void *blob, int parent_offset,
885 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100886{
887 struct list_head *pentry;
888 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100889 int off, ndepth = 0;
890 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300891 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100892 char buf[64];
893
894 ret = fdt_del_partitions(blob, parent_offset);
895 if (ret < 0)
896 return ret;
897
898 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300899 * Check if size/address is 1 or 2 cells.
900 * We assume #address-cells and #size-cells have same value.
901 */
902 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
903 0, "#size-cells", 1);
904
905 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100906 * Check if it is nand {}; subnode, adjust
907 * the offset in this case
908 */
909 off = fdt_next_node(blob, parent_offset, &ndepth);
910 if (off > 0 && ndepth == 1)
911 parent_offset = off;
912
913 part_num = 0;
914 list_for_each_prev(pentry, &dev->parts) {
915 int newoff;
916
917 part = list_entry(pentry, struct part_info, link);
918
Scott Woodefe7f302013-10-15 17:41:27 -0500919 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100920 part_num, part->name, part->size,
921 part->offset, part->mask_flags);
922
Scott Woodefe7f302013-10-15 17:41:27 -0500923 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100924add_sub:
925 ret = fdt_add_subnode(blob, parent_offset, buf);
926 if (ret == -FDT_ERR_NOSPACE) {
927 ret = fdt_increase_size(blob, 512);
928 if (!ret)
929 goto add_sub;
930 else
931 goto err_size;
932 } else if (ret < 0) {
933 printf("Can't add partition node: %s\n",
934 fdt_strerror(ret));
935 return ret;
936 }
937 newoff = ret;
938
939 /* Check MTD_WRITEABLE_CMD flag */
940 if (part->mask_flags & 1) {
941add_ro:
942 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
943 if (ret == -FDT_ERR_NOSPACE) {
944 ret = fdt_increase_size(blob, 512);
945 if (!ret)
946 goto add_ro;
947 else
948 goto err_size;
949 } else if (ret < 0)
950 goto err_prop;
951 }
952
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100953add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300954 if (sizecell == 2) {
955 ret = fdt_setprop_u64(blob, newoff,
956 "reg", part->offset);
957 if (!ret)
958 ret = fdt_appendprop_u64(blob, newoff,
959 "reg", part->size);
960 } else {
961 ret = fdt_setprop_u32(blob, newoff,
962 "reg", part->offset);
963 if (!ret)
964 ret = fdt_appendprop_u32(blob, newoff,
965 "reg", part->size);
966 }
967
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100968 if (ret == -FDT_ERR_NOSPACE) {
969 ret = fdt_increase_size(blob, 512);
970 if (!ret)
971 goto add_reg;
972 else
973 goto err_size;
974 } else if (ret < 0)
975 goto err_prop;
976
977add_label:
978 ret = fdt_setprop_string(blob, newoff, "label", part->name);
979 if (ret == -FDT_ERR_NOSPACE) {
980 ret = fdt_increase_size(blob, 512);
981 if (!ret)
982 goto add_label;
983 else
984 goto err_size;
985 } else if (ret < 0)
986 goto err_prop;
987
988 part_num++;
989 }
990 return 0;
991err_size:
992 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
993 return ret;
994err_prop:
995 printf("Can't add property: %s\n", fdt_strerror(ret));
996 return ret;
997}
998
999/*
1000 * Update partitions in nor/nand nodes using info from
1001 * mtdparts environment variable. The nodes to update are
1002 * specified by node_info structure which contains mtd device
1003 * type and compatible string: E. g. the board code in
1004 * ft_board_setup() could use:
1005 *
1006 * struct node_info nodes[] = {
1007 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1008 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1009 * };
1010 *
1011 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1012 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001013void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1014 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001015{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001016 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001017 int i, idx;
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001018 int noff, parts;
Masahiro Yamada030348f2020-07-17 10:46:18 +09001019 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001020
1021 for (i = 0; i < node_info_size; i++) {
1022 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +09001023
Marek Behún5d6b4482022-01-20 01:04:42 +01001024 fdt_for_each_node_by_compatible(noff, blob, -1,
1025 node_info[i].compat) {
Masahiro Yamada21644292020-07-17 10:46:19 +09001026 const char *prop;
1027
1028 prop = fdt_getprop(blob, noff, "status", NULL);
1029 if (prop && !strcmp(prop, "disabled"))
1030 continue;
1031
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001032 debug("%s: %s, mtd dev type %d\n",
1033 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001034 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +09001035
1036 if (!inited) {
1037 if (mtdparts_init() != 0)
1038 return;
1039 inited = true;
1040 }
1041
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001042 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001043 if (dev) {
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001044 parts = fdt_subnode_offset(blob, noff,
1045 "partitions");
1046 if (parts < 0)
1047 parts = noff;
1048
1049 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001050 return; /* return on error */
1051 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001052 }
1053 }
1054}
1055#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001056
Patrick Delaunay019127b2023-06-08 17:16:38 +02001057int fdt_copy_fixed_partitions(void *blob)
1058{
1059 ofnode node, subnode;
1060 int off, suboff, res;
1061 char path[256];
1062 int address_cells, size_cells;
1063 u8 i, j, child_count;
1064
1065 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1066 while (ofnode_valid(node)) {
1067 /* copy the U-Boot fixed partition */
1068 address_cells = ofnode_read_simple_addr_cells(node);
1069 size_cells = ofnode_read_simple_size_cells(node);
1070
1071 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1072 if (res)
1073 return res;
1074
1075 off = fdt_path_offset(blob, path);
1076 if (off < 0)
1077 return -ENODEV;
1078
1079 off = fdt_find_or_add_subnode(blob, off, "partitions");
1080 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1081 if (res)
1082 return res;
1083
1084 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1085 if (res)
1086 return res;
1087
1088 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1089 if (res)
1090 return res;
1091
1092 /*
1093 * parse partition in reverse order as fdt_find_or_add_subnode() only
1094 * insert the new node after the parent's properties
1095 */
1096 child_count = ofnode_get_child_count(node);
1097 for (i = child_count; i > 0 ; i--) {
1098 subnode = ofnode_first_subnode(node);
1099 if (!ofnode_valid(subnode))
1100 break;
1101
1102 for (j = 0; (j < i - 1); j++)
1103 subnode = ofnode_next_subnode(subnode);
1104
1105 if (!ofnode_valid(subnode))
1106 break;
1107
1108 const u32 *reg;
1109 int len;
1110
1111 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1112 res = fdt_setprop_string(blob, suboff, "label",
1113 ofnode_read_string(subnode, "label"));
1114 if (res)
1115 return res;
1116
1117 reg = ofnode_get_property(subnode, "reg", &len);
1118 res = fdt_setprop(blob, suboff, "reg", reg, len);
1119 if (res)
1120 return res;
1121 }
1122
1123 /* go to next fixed-partitions node */
1124 node = ofnode_by_compatible(node, "fixed-partitions");
1125 }
1126
1127 return 0;
1128}
1129
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001130void fdt_del_node_and_alias(void *blob, const char *alias)
1131{
1132 int off = fdt_path_offset(blob, alias);
1133
1134 if (off < 0)
1135 return;
1136
1137 fdt_del_node(blob, off);
1138
1139 off = fdt_path_offset(blob, "/aliases");
1140 fdt_delprop(blob, off, alias);
1141}
Kumar Galab87e7932010-06-16 14:27:38 -05001142
Kumar Galab87e7932010-06-16 14:27:38 -05001143/* Max address size we deal with */
1144#define OF_MAX_ADDR_CELLS 4
Dario Binacchif8fc7032021-05-01 17:05:26 +02001145#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1146 (ns) > 0)
Kumar Galab87e7932010-06-16 14:27:38 -05001147
1148/* Debug utility */
1149#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +00001150static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001151{
1152 printf("%s", s);
1153 while(na--)
1154 printf(" %08x", *(addr++));
1155 printf("\n");
1156}
1157#else
Kim Phillips6542c072013-01-16 14:00:11 +00001158static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001159#endif
1160
Paul Burtondbef2f02016-05-17 07:43:24 +01001161/**
1162 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001163 * @name: A string used to identify this bus in debug output.
1164 * @addresses: The name of the DT property from which addresses are
1165 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001166 * @match: Return non-zero if the node whose parent is at
1167 * parentoffset in the FDT blob corresponds to a bus
1168 * of this type, otherwise return zero. If NULL a match
1169 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001170 * @count_cells:Count how many cells (be32 values) a node whose parent
1171 * is at parentoffset in the FDT blob will require to
1172 * represent its address (written to *addrc) & size
1173 * (written to *sizec).
1174 * @map: Map the address addr from the address space of this
1175 * bus to that of its parent, making use of the ranges
1176 * read from DT to an array at range. na and ns are the
1177 * number of cells (be32 values) used to hold and address
1178 * or size, respectively, for this bus. pna is the number
1179 * of cells used to hold an address for the parent bus.
1180 * Returns the address in the address space of the parent
1181 * bus.
1182 * @translate: Update the value of the address cells at addr within an
1183 * FDT by adding offset to it. na specifies the number of
1184 * cells used to hold the address being translated. Returns
1185 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001186 *
1187 * Each bus type will include a struct of_bus in the of_busses array,
1188 * providing implementations of some or all of the functions used to
1189 * match the bus & handle address translation for its children.
1190 */
Kumar Galab87e7932010-06-16 14:27:38 -05001191struct of_bus {
1192 const char *name;
1193 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001194 int (*match)(const void *blob, int parentoffset);
1195 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001196 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001197 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001198 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001199 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001200};
1201
1202/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001203void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001204 int *addrc, int *sizec)
1205{
Kim Phillips6542c072013-01-16 14:00:11 +00001206 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001207
Simon Glass9fbc6322014-10-23 18:58:57 -06001208 if (addrc)
1209 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001210
1211 if (sizec) {
1212 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1213 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001214 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001215 else
1216 *sizec = 1;
1217 }
Kumar Galab87e7932010-06-16 14:27:38 -05001218}
1219
Kim Phillips6542c072013-01-16 14:00:11 +00001220static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001221 int na, int ns, int pna)
1222{
1223 u64 cp, s, da;
1224
Simon Glassbb7c01e2017-05-18 20:09:26 -06001225 cp = fdt_read_number(range, na);
1226 s = fdt_read_number(range + na + pna, ns);
1227 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001228
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301229 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001230
1231 if (da < cp || da >= (cp + s))
1232 return OF_BAD_ADDR;
1233 return da - cp;
1234}
1235
Kim Phillips6542c072013-01-16 14:00:11 +00001236static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001237{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001238 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001239 memset(addr, 0, na * 4);
1240 a += offset;
1241 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001242 addr[na - 2] = cpu_to_fdt32(a >> 32);
1243 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001244
1245 return 0;
1246}
1247
Paul Burtondbef2f02016-05-17 07:43:24 +01001248#ifdef CONFIG_OF_ISA_BUS
1249
1250/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001251static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001252{
1253 const char *name;
1254
1255 name = fdt_get_name(blob, parentoffset, NULL);
1256 if (!name)
1257 return 0;
1258
1259 return !strcmp(name, "isa");
1260}
1261
Stephen Warrenc99581b2016-08-05 09:47:50 -06001262static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001263 int *addrc, int *sizec)
1264{
1265 if (addrc)
1266 *addrc = 2;
1267 if (sizec)
1268 *sizec = 1;
1269}
1270
1271static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1272 int na, int ns, int pna)
1273{
1274 u64 cp, s, da;
1275
1276 /* Check address type match */
1277 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1278 return OF_BAD_ADDR;
1279
Simon Glassbb7c01e2017-05-18 20:09:26 -06001280 cp = fdt_read_number(range + 1, na - 1);
1281 s = fdt_read_number(range + na + pna, ns);
1282 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001283
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301284 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001285
1286 if (da < cp || da >= (cp + s))
1287 return OF_BAD_ADDR;
1288 return da - cp;
1289}
1290
1291static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1292{
1293 return of_bus_default_translate(addr + 1, offset, na - 1);
1294}
1295
1296#endif /* CONFIG_OF_ISA_BUS */
1297
Kumar Galab87e7932010-06-16 14:27:38 -05001298/* Array of bus specific translators */
1299static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001300#ifdef CONFIG_OF_ISA_BUS
1301 /* ISA */
1302 {
1303 .name = "isa",
1304 .addresses = "reg",
1305 .match = of_bus_isa_match,
1306 .count_cells = of_bus_isa_count_cells,
1307 .map = of_bus_isa_map,
1308 .translate = of_bus_isa_translate,
1309 },
1310#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001311 /* Default */
1312 {
1313 .name = "default",
1314 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001315 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001316 .map = of_bus_default_map,
1317 .translate = of_bus_default_translate,
1318 },
1319};
1320
Stephen Warrenc99581b2016-08-05 09:47:50 -06001321static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001322{
1323 struct of_bus *bus;
1324
1325 if (ARRAY_SIZE(of_busses) == 1)
1326 return of_busses;
1327
1328 for (bus = of_busses; bus; bus++) {
1329 if (!bus->match || bus->match(blob, parentoffset))
1330 return bus;
1331 }
1332
1333 /*
1334 * We should always have matched the default bus at least, since
1335 * it has a NULL match field. If we didn't then it somehow isn't
1336 * in the of_busses array or something equally catastrophic has
1337 * gone wrong.
1338 */
1339 assert(0);
1340 return NULL;
1341}
1342
Stephen Warrenc99581b2016-08-05 09:47:50 -06001343static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001344 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001345 int na, int ns, int pna, const char *rprop)
1346{
Kim Phillips6542c072013-01-16 14:00:11 +00001347 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001348 int rlen;
1349 int rone;
1350 u64 offset = OF_BAD_ADDR;
1351
1352 /* Normally, an absence of a "ranges" property means we are
1353 * crossing a non-translatable boundary, and thus the addresses
1354 * below the current not cannot be converted to CPU physical ones.
1355 * Unfortunately, while this is very clear in the spec, it's not
1356 * what Apple understood, and they do have things like /uni-n or
1357 * /ht nodes with no "ranges" property and a lot of perfectly
1358 * useable mapped devices below them. Thus we treat the absence of
1359 * "ranges" as equivalent to an empty "ranges" property which means
1360 * a 1:1 translation at that level. It's up to the caller not to try
1361 * to translate addresses that aren't supposed to be translated in
1362 * the first place. --BenH.
1363 */
Kim Phillips6542c072013-01-16 14:00:11 +00001364 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001365 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001366 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001367 memset(addr, 0, pna * 4);
1368 debug("OF: no ranges, 1:1 translation\n");
1369 goto finish;
1370 }
1371
1372 debug("OF: walking ranges...\n");
1373
1374 /* Now walk through the ranges */
1375 rlen /= 4;
1376 rone = na + pna + ns;
1377 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1378 offset = bus->map(addr, ranges, na, ns, pna);
1379 if (offset != OF_BAD_ADDR)
1380 break;
1381 }
1382 if (offset == OF_BAD_ADDR) {
1383 debug("OF: not found !\n");
1384 return 1;
1385 }
1386 memcpy(addr, ranges + na, 4 * pna);
1387
1388 finish:
1389 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001390 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001391
1392 /* Translate it into parent bus space */
1393 return pbus->translate(addr, offset, pna);
1394}
1395
1396/*
1397 * Translate an address from the device-tree into a CPU physical address,
1398 * this walks up the tree and applies the various bus mappings on the
1399 * way.
1400 *
1401 * Note: We consider that crossing any level with #size-cells == 0 to mean
1402 * that translation is impossible (that is we are not dealing with a value
1403 * that can be mapped to a cpu physical address). This is not really specified
1404 * that way, but this is traditionally the way IBM at least do things
1405 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001406static u64 __of_translate_address(const void *blob, int node_offset,
1407 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001408{
1409 int parent;
1410 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001411 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001412 int na, ns, pna, pns;
1413 u64 result = OF_BAD_ADDR;
1414
1415 debug("OF: ** translation for device %s **\n",
1416 fdt_get_name(blob, node_offset, NULL));
1417
1418 /* Get parent & match bus type */
1419 parent = fdt_parent_offset(blob, node_offset);
1420 if (parent < 0)
1421 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001422 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001423
1424 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001425 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001426 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001427 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1428 fdt_get_name(blob, node_offset, NULL));
1429 goto bail;
1430 }
1431 memcpy(addr, in_addr, na * 4);
1432
1433 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1434 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1435 of_dump_addr("OF: translating address:", addr, na);
1436
1437 /* Translate */
1438 for (;;) {
1439 /* Switch to parent bus */
1440 node_offset = parent;
1441 parent = fdt_parent_offset(blob, node_offset);
1442
1443 /* If root, we have finished */
1444 if (parent < 0) {
1445 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001446 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001447 break;
1448 }
1449
1450 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001451 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001452 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001453 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001454 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1455 fdt_get_name(blob, node_offset, NULL));
1456 break;
1457 }
1458
1459 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1460 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1461
1462 /* Apply bus translation */
1463 if (of_translate_one(blob, node_offset, bus, pbus,
1464 addr, na, ns, pna, rprop))
1465 break;
1466
1467 /* Complete the move up one level */
1468 na = pna;
1469 ns = pns;
1470 bus = pbus;
1471
1472 of_dump_addr("OF: one level translation:", addr, na);
1473 }
1474 bail:
1475
1476 return result;
1477}
1478
Stephen Warrenc99581b2016-08-05 09:47:50 -06001479u64 fdt_translate_address(const void *blob, int node_offset,
1480 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001481{
1482 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1483}
Kumar Gala800d1d12010-07-04 12:48:21 -05001484
Fabien Dessenne22236e02019-05-31 15:11:30 +02001485u64 fdt_translate_dma_address(const void *blob, int node_offset,
1486 const fdt32_t *in_addr)
1487{
1488 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1489}
1490
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001491int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1492 dma_addr_t *bus, u64 *size)
1493{
1494 bool found_dma_ranges = false;
1495 struct of_bus *bus_node;
1496 const fdt32_t *ranges;
1497 int na, ns, pna, pns;
1498 int parent = node;
1499 int ret = 0;
1500 int len;
1501
1502 /* Find the closest dma-ranges property */
1503 while (parent >= 0) {
1504 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1505
1506 /* Ignore empty ranges, they imply no translation required */
1507 if (ranges && len > 0)
1508 break;
1509
1510 /* Once we find 'dma-ranges', then a missing one is an error */
1511 if (found_dma_ranges && !ranges) {
1512 ret = -EINVAL;
1513 goto out;
1514 }
1515
1516 if (ranges)
1517 found_dma_ranges = true;
1518
1519 parent = fdt_parent_offset(blob, parent);
1520 }
1521
1522 if (!ranges || parent < 0) {
1523 debug("no dma-ranges found for node %s\n",
1524 fdt_get_name(blob, node, NULL));
1525 ret = -ENOENT;
1526 goto out;
1527 }
1528
1529 /* switch to that node */
1530 node = parent;
1531 parent = fdt_parent_offset(blob, node);
1532 if (parent < 0) {
Vagrant Cascadiand00c5c72021-12-21 13:06:58 -08001533 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001534 ret = -EINVAL;
1535 goto out;
1536 }
1537
1538 /* Get the address sizes both for the bus and its parent */
1539 bus_node = of_match_bus(blob, node);
1540 bus_node->count_cells(blob, node, &na, &ns);
1541 if (!OF_CHECK_COUNTS(na, ns)) {
1542 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1543 fdt_get_name(blob, node, NULL));
1544 return -EINVAL;
1545 goto out;
1546 }
1547
1548 bus_node = of_match_bus(blob, parent);
1549 bus_node->count_cells(blob, parent, &pna, &pns);
1550 if (!OF_CHECK_COUNTS(pna, pns)) {
1551 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1552 fdt_get_name(blob, parent, NULL));
1553 return -EINVAL;
1554 goto out;
1555 }
1556
1557 *bus = fdt_read_number(ranges, na);
1558 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1559 *size = fdt_read_number(ranges + na + pna, ns);
1560out:
1561 return ret;
1562}
1563
Kumar Gala800d1d12010-07-04 12:48:21 -05001564/**
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001565 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala800d1d12010-07-04 12:48:21 -05001566 * who's reg property matches a physical cpu address
1567 *
1568 * @blob: ptr to device tree
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001569 * @compat: compatible string to match
Kumar Gala800d1d12010-07-04 12:48:21 -05001570 * @compat_off: property name
1571 *
1572 */
1573int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1574 phys_addr_t compat_off)
1575{
Marek Behún5d6b4482022-01-20 01:04:42 +01001576 int len, off;
1577
1578 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips6542c072013-01-16 14:00:11 +00001579 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún5d6b4482022-01-20 01:04:42 +01001580 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1581 return off;
Kumar Gala800d1d12010-07-04 12:48:21 -05001582 }
1583
1584 return -FDT_ERR_NOTFOUND;
1585}
1586
Marek Behún4ef5ca62021-11-26 14:57:10 +01001587static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1588{
1589 char path[512];
1590 int len;
1591
1592 len = vsnprintf(path, sizeof(path), fmt, ap);
1593 if (len < 0 || len + 1 > sizeof(path))
1594 return -FDT_ERR_NOSPACE;
1595
1596 return fdt_path_offset(blob, path);
1597}
1598
1599/**
1600 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1601 *
1602 * @blob: ptr to device tree
1603 * @fmt: path format
1604 * @ap: vsnprintf arguments
1605 */
1606int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1607{
1608 va_list ap;
1609 int res;
1610
1611 va_start(ap, fmt);
1612 res = vnode_offset_by_pathf(blob, fmt, ap);
1613 va_end(ap);
1614
1615 return res;
1616}
1617
Gerald Van Barenaf737882011-07-14 21:40:10 -04001618/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001619 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001620 *
1621 * @fdt: ptr to device tree
1622 * @nodeoffset: node to update
1623 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001624 */
1625int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001626{
1627 int ret;
1628
1629#ifdef DEBUG
1630 int off = fdt_node_offset_by_phandle(fdt, phandle);
1631
1632 if ((off >= 0) && (off != nodeoffset)) {
1633 char buf[64];
1634
1635 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1636 printf("Trying to update node %s with phandle %u ",
1637 buf, phandle);
1638
1639 fdt_get_path(fdt, off, buf, sizeof(buf));
1640 printf("that already exists in node %s.\n", buf);
1641 return -FDT_ERR_BADPHANDLE;
1642 }
1643#endif
1644
1645 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barenaf737882011-07-14 21:40:10 -04001646
1647 return ret;
1648}
1649
Kumar Galad5fc2582011-08-01 00:25:20 -05001650/*
Marek Behúnae0ef952021-11-26 14:57:09 +01001651 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Galad5fc2582011-08-01 00:25:20 -05001652 *
1653 * @fdt: ptr to device tree
1654 * @nodeoffset: node to update
1655 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001656unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001657{
1658 /* see if there is a phandle already */
Marek Behún3577eba32021-11-26 14:57:07 +01001659 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Galad5fc2582011-08-01 00:25:20 -05001660
1661 /* if we got 0, means no phandle so create one */
1662 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001663 int ret;
1664
Marek Behún3577eba32021-11-26 14:57:07 +01001665 ret = fdt_generate_phandle(fdt, &phandle);
1666 if (ret < 0) {
1667 printf("Can't generate phandle: %s\n",
1668 fdt_strerror(ret));
1669 return 0;
1670 }
1671
Timur Tabi0f46c802011-09-20 18:24:34 -05001672 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1673 if (ret < 0) {
1674 printf("Can't set phandle %u: %s\n", phandle,
1675 fdt_strerror(ret));
1676 return 0;
1677 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001678 }
1679
1680 return phandle;
1681}
1682
Marek Behún4ef5ca62021-11-26 14:57:10 +01001683/**
1684 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1685 * given compatible
1686 *
1687 * @fdt: ptr to device tree
1688 * @compat: node's compatible string
1689 */
1690unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1691{
1692 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1693
1694 if (offset < 0) {
1695 printf("Can't find node with compatible \"%s\": %s\n", compat,
1696 fdt_strerror(offset));
1697 return 0;
1698 }
1699
1700 return fdt_create_phandle(fdt, offset);
1701}
1702
1703/**
1704 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1705 * sprintf-formatted path
1706 *
1707 * @fdt: ptr to device tree
1708 * @fmt, ...: path format string and arguments to pass to sprintf
1709 */
1710unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1711{
1712 va_list ap;
1713 int offset;
1714
1715 va_start(ap, fmt);
1716 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1717 va_end(ap);
1718
1719 if (offset < 0) {
1720 printf("Can't find node by given path: %s\n",
1721 fdt_strerror(offset));
1722 return 0;
1723 }
1724
1725 return fdt_create_phandle(fdt, offset);
1726}
1727
Shengzhou Liua7be8022011-10-14 16:26:05 +08001728/*
1729 * fdt_set_node_status: Set status for the given node
1730 *
1731 * @fdt: ptr to device tree
1732 * @nodeoffset: node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001733 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001734 */
Marek Behúnf872e832021-11-26 14:57:08 +01001735int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001736{
Shengzhou Liua7be8022011-10-14 16:26:05 +08001737 int ret = 0;
1738
1739 if (nodeoffset < 0)
1740 return nodeoffset;
1741
1742 switch (status) {
1743 case FDT_STATUS_OKAY:
1744 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1745 break;
1746 case FDT_STATUS_DISABLED:
1747 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1748 break;
1749 case FDT_STATUS_FAIL:
1750 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1751 break;
Shengzhou Liua7be8022011-10-14 16:26:05 +08001752 default:
1753 printf("Invalid fdt status: %x\n", status);
1754 ret = -1;
1755 break;
1756 }
1757
1758 return ret;
1759}
1760
1761/*
1762 * fdt_set_status_by_alias: Set status for the given node given an alias
1763 *
1764 * @fdt: ptr to device tree
1765 * @alias: alias of node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001766 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001767 */
1768int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behúnf872e832021-11-26 14:57:08 +01001769 enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001770{
1771 int offset = fdt_path_offset(fdt, alias);
1772
Marek Behúnf872e832021-11-26 14:57:08 +01001773 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liua7be8022011-10-14 16:26:05 +08001774}
1775
Marek Behún4ef5ca62021-11-26 14:57:10 +01001776/**
1777 * fdt_set_status_by_compatible: Set node status for first node with given
1778 * compatible
1779 *
1780 * @fdt: ptr to device tree
1781 * @compat: node's compatible string
1782 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1783 */
1784int fdt_set_status_by_compatible(void *fdt, const char *compat,
1785 enum fdt_status status)
1786{
1787 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1788
1789 if (offset < 0)
1790 return offset;
1791
1792 return fdt_set_node_status(fdt, offset, status);
1793}
1794
1795/**
1796 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1797 * path
1798 *
1799 * @fdt: ptr to device tree
1800 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1801 * @fmt, ...: path format string and arguments to pass to sprintf
1802 */
1803int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1804 ...)
1805{
1806 va_list ap;
1807 int offset;
1808
1809 va_start(ap, fmt);
1810 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1811 va_end(ap);
1812
1813 if (offset < 0)
1814 return offset;
1815
1816 return fdt_set_node_status(fdt, offset, status);
1817}
1818
Timur Tabi8ebaf202011-05-03 13:24:07 -05001819/*
1820 * Verify the physical address of device tree node for a given alias
1821 *
1822 * This function locates the device tree node of a given alias, and then
1823 * verifies that the physical address of that device matches the given
1824 * parameter. It displays a message if there is a mismatch.
1825 *
1826 * Returns 1 on success, 0 on failure
1827 */
1828int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1829{
1830 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001831 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001832 int node, len;
1833 u64 dt_addr;
1834
1835 path = fdt_getprop(fdt, anode, alias, NULL);
1836 if (!path) {
1837 /* If there's no such alias, then it's not a failure */
1838 return 1;
1839 }
1840
1841 node = fdt_path_offset(fdt, path);
1842 if (node < 0) {
1843 printf("Warning: device tree alias '%s' points to invalid "
1844 "node %s.\n", alias, path);
1845 return 0;
1846 }
1847
1848 reg = fdt_getprop(fdt, node, "reg", &len);
1849 if (!reg) {
1850 printf("Warning: device tree node '%s' has no address.\n",
1851 path);
1852 return 0;
1853 }
1854
1855 dt_addr = fdt_translate_address(fdt, node, reg);
1856 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001857 printf("Warning: U-Boot configured device %s at address %llu,\n"
1858 "but the device tree has it address %llx.\n",
1859 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001860 return 0;
1861 }
1862
1863 return 1;
1864}
1865
1866/*
1867 * Returns the base address of an SOC or PCI node
1868 */
Simon Glass293340a2017-05-18 20:09:00 -06001869u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001870{
1871 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001872 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001873
Simon Glass29f304b2017-07-04 13:31:20 -06001874 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001875
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001876 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001877}
Alexander Graf19555be2014-04-11 17:09:41 +02001878
1879/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001880 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1881 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001882 */
1883static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1884 uint64_t *val, int cells)
1885{
Bin Mengd67d7d02021-02-25 17:22:24 +08001886 const fdt32_t *prop32;
1887 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001888
1889 if ((cell_off + cells) > prop_len)
1890 return -FDT_ERR_NOSPACE;
1891
Bin Mengd67d7d02021-02-25 17:22:24 +08001892 prop32 = &prop[cell_off];
1893
1894 /*
1895 * Special handling for PCI address in PCI bus <ranges>
1896 *
1897 * PCI child address is made up of 3 cells. Advance the cell offset
1898 * by 1 so that the PCI child address can be correctly read.
1899 */
1900 if (cells == 3)
1901 cell_off += 1;
1902 prop64 = (const fdt64_t *)&prop[cell_off];
1903
Alexander Graf19555be2014-04-11 17:09:41 +02001904 switch (cells) {
1905 case 1:
1906 *val = fdt32_to_cpu(*prop32);
1907 break;
1908 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001909 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001910 *val = fdt64_to_cpu(*prop64);
1911 break;
1912 default:
1913 return -FDT_ERR_NOSPACE;
1914 }
1915
1916 return 0;
1917}
1918
1919/**
1920 * fdt_read_range - Read a node's n'th range property
1921 *
1922 * @fdt: ptr to device tree
1923 * @node: offset of node
1924 * @n: range index
1925 * @child_addr: pointer to storage for the "child address" field
1926 * @addr: pointer to storage for the CPU view translated physical start
1927 * @len: pointer to storage for the range length
1928 *
1929 * Convenience function that reads and interprets a specific range out of
1930 * a number of the "ranges" property array.
1931 */
1932int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1933 uint64_t *addr, uint64_t *len)
1934{
1935 int pnode = fdt_parent_offset(fdt, node);
1936 const fdt32_t *ranges;
1937 int pacells;
1938 int acells;
1939 int scells;
1940 int ranges_len;
1941 int cell = 0;
1942 int r = 0;
1943
1944 /*
1945 * The "ranges" property is an array of
1946 * { <child address> <parent address> <size in child address space> }
1947 *
1948 * All 3 elements can span a diffent number of cells. Fetch their size.
1949 */
1950 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1951 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1952 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1953
1954 /* Now try to get the ranges property */
1955 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1956 if (!ranges)
1957 return -FDT_ERR_NOTFOUND;
1958 ranges_len /= sizeof(uint32_t);
1959
1960 /* Jump to the n'th entry */
1961 cell = n * (pacells + acells + scells);
1962
1963 /* Read <child address> */
1964 if (child_addr) {
1965 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1966 acells);
1967 if (r)
1968 return r;
1969 }
1970 cell += acells;
1971
1972 /* Read <parent address> */
1973 if (addr)
1974 *addr = fdt_translate_address(fdt, node, ranges + cell);
1975 cell += pacells;
1976
1977 /* Read <size in child address space> */
1978 if (len) {
1979 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1980 if (r)
1981 return r;
1982 }
1983
1984 return 0;
1985}
Hans de Goedeea876372014-11-17 15:29:11 +01001986
1987/**
1988 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1989 *
1990 * @fdt: ptr to device tree
1991 * @node: offset of the simplefb node
1992 * @base_address: framebuffer base address
1993 * @width: width in pixels
1994 * @height: height in pixels
1995 * @stride: bytes per line
1996 * @format: pixel format string
1997 *
1998 * Convenience function to fill and enable a simplefb node.
1999 */
2000int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2001 u32 height, u32 stride, const char *format)
2002{
2003 char name[32];
2004 fdt32_t cells[4];
2005 int i, addrc, sizec, ret;
2006
Simon Glassbb7c01e2017-05-18 20:09:26 -06002007 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2008 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01002009 i = 0;
2010 if (addrc == 2)
2011 cells[i++] = cpu_to_fdt32(base_address >> 32);
2012 cells[i++] = cpu_to_fdt32(base_address);
2013 if (sizec == 2)
2014 cells[i++] = 0;
2015 cells[i++] = cpu_to_fdt32(height * stride);
2016
2017 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2018 if (ret < 0)
2019 return ret;
2020
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002021 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01002022 ret = fdt_set_name(fdt, node, name);
2023 if (ret < 0)
2024 return ret;
2025
2026 ret = fdt_setprop_u32(fdt, node, "width", width);
2027 if (ret < 0)
2028 return ret;
2029
2030 ret = fdt_setprop_u32(fdt, node, "height", height);
2031 if (ret < 0)
2032 return ret;
2033
2034 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2035 if (ret < 0)
2036 return ret;
2037
2038 ret = fdt_setprop_string(fdt, node, "format", format);
2039 if (ret < 0)
2040 return ret;
2041
2042 ret = fdt_setprop_string(fdt, node, "status", "okay");
2043 if (ret < 0)
2044 return ret;
2045
2046 return 0;
2047}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002048
Devarsh Thakkar31199892024-02-22 18:38:10 +05302049#if CONFIG_IS_ENABLED(VIDEO)
2050int fdt_add_fb_mem_rsv(void *blob)
2051{
2052 struct fdt_memory mem;
2053
2054 /* nothing to do when the frame buffer is not defined */
2055 if (gd->video_bottom == gd->video_top)
2056 return 0;
2057
2058 /* reserved with no-map tag the video buffer */
2059 mem.start = gd->video_bottom;
2060 mem.end = gd->video_top - 1;
2061
2062 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2063 FDTDEC_RESERVED_MEMORY_NO_MAP);
2064}
2065#endif
2066
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002067/*
2068 * Update native-mode in display-timings from display environment variable.
2069 * The node to update are specified by path.
2070 */
2071int fdt_fixup_display(void *blob, const char *path, const char *display)
2072{
2073 int off, toff;
2074
2075 if (!display || !path)
2076 return -FDT_ERR_NOTFOUND;
2077
2078 toff = fdt_path_offset(blob, path);
2079 if (toff >= 0)
2080 toff = fdt_subnode_offset(blob, toff, "display-timings");
2081 if (toff < 0)
2082 return toff;
2083
2084 for (off = fdt_first_subnode(blob, toff);
2085 off >= 0;
2086 off = fdt_next_subnode(blob, off)) {
2087 uint32_t h = fdt_get_phandle(blob, off);
2088 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2089 fdt32_to_cpu(h));
2090 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2091 return fdt_setprop_u32(blob, toff, "native-mode", h);
2092 }
2093 return toff;
2094}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002095
2096#ifdef CONFIG_OF_LIBFDT_OVERLAY
2097/**
2098 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2099 *
2100 * @fdt: ptr to device tree
2101 * @fdto: ptr to device tree overlay
2102 *
2103 * Convenience function to apply an overlay and display helpful messages
2104 * in the case of an error
2105 */
2106int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2107{
2108 int err;
2109 bool has_symbols;
2110
2111 err = fdt_path_offset(fdt, "/__symbols__");
2112 has_symbols = err >= 0;
2113
2114 err = fdt_overlay_apply(fdt, fdto);
2115 if (err < 0) {
2116 printf("failed on fdt_overlay_apply(): %s\n",
2117 fdt_strerror(err));
2118 if (!has_symbols) {
Hugo Villeneuve90730f12023-10-26 15:54:49 -04002119 printf("base fdt does not have a /__symbols__ node\n");
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002120 printf("make sure you've compiled with -@\n");
2121 }
2122 }
2123 return err;
2124}
2125#endif
Kory Maincent623e1ac2021-05-04 19:31:21 +02002126
2127/**
2128 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2129 *
2130 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002131 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincent623e1ac2021-05-04 19:31:21 +02002132 */
2133int fdt_valid(struct fdt_header **blobp)
2134{
2135 const void *blob = *blobp;
2136 int err;
2137
2138 if (!blob) {
2139 printf("The address of the fdt is invalid (NULL).\n");
2140 return 0;
2141 }
2142
2143 err = fdt_check_header(blob);
2144 if (err == 0)
2145 return 1; /* valid */
2146
2147 if (err < 0) {
2148 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2149 /*
2150 * Be more informative on bad version.
2151 */
2152 if (err == -FDT_ERR_BADVERSION) {
2153 if (fdt_version(blob) <
2154 FDT_FIRST_SUPPORTED_VERSION) {
2155 printf(" - too old, fdt %d < %d",
2156 fdt_version(blob),
2157 FDT_FIRST_SUPPORTED_VERSION);
2158 }
2159 if (fdt_last_comp_version(blob) >
2160 FDT_LAST_SUPPORTED_VERSION) {
2161 printf(" - too new, fdt %d > %d",
2162 fdt_version(blob),
2163 FDT_LAST_SUPPORTED_VERSION);
2164 }
2165 }
2166 printf("\n");
2167 *blobp = NULL;
2168 return 0;
2169 }
2170 return 1;
2171}