blob: 4559adcd5e2e341873c7f664b815ef83e7c138a0 [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
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Tim Harvey00ae1222024-06-18 14:06:06 -070010#include <dm.h>
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +020011#include <abuf.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Heinrich Schuchardt668cabc2018-11-18 17:58:51 +010014#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Tim Harvey00ae1222024-06-18 14:06:06 -070016#include <rng.h>
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040017#include <stdio_dev.h>
Tim Harvey00ae1222024-06-18 14:06:06 -070018#include <dm/device_compat.h>
Patrick Delaunaye0682262023-06-08 17:16:37 +020019#include <dm/ofnode.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040020#include <linux/ctype.h>
21#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040022#include <asm/global_data.h>
Sam Protsenko94483d22024-03-29 19:55:53 -050023#include <asm/unaligned.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090024#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040025#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060026#include <exports.h>
Bin Mengd9a00932015-12-07 01:39:47 -080027#include <fdtdec.h>
Francesco Dolcini05cc8272022-05-19 16:22:26 +020028#include <version.h>
Devarsh Thakkar31199892024-02-22 18:38:10 +053029#include <video.h>
30
31DECLARE_GLOBAL_DATA_PTR;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040032
Kumar Gala1ba00402008-10-23 00:05:47 -050033/**
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020034 * fdt_getprop_u32_default_node - Return a node's property or a default
35 *
36 * @fdt: ptr to device tree
37 * @off: offset of node
38 * @cell: cell offset in property
39 * @prop: property name
40 * @dflt: default value if the property isn't found
41 *
42 * Convenience function to return a node's property or a default value if
43 * the property doesn't exist.
44 */
45u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
46 const char *prop, const u32 dflt)
47{
48 const fdt32_t *val;
49 int len;
50
51 val = fdt_getprop(fdt, off, prop, &len);
52
53 /* Check if property exists */
54 if (!val)
55 return dflt;
56
57 /* Check if property is long enough */
58 if (len < ((cell + 1) * sizeof(uint32_t)))
59 return dflt;
60
61 return fdt32_to_cpu(*val);
62}
63
64/**
Kumar Gala1ba00402008-10-23 00:05:47 -050065 * fdt_getprop_u32_default - Find a node and return it's property or a default
66 *
67 * @fdt: ptr to device tree
68 * @path: path of node
69 * @prop: property name
70 * @dflt: default value if the property isn't found
71 *
72 * Convenience function to find a node and return it's property or a
73 * default value if it doesn't exist.
74 */
Gabe Blackcd37de72011-11-08 01:09:44 -080075u32 fdt_getprop_u32_default(const void *fdt, const char *path,
76 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050077{
Kumar Gala1ba00402008-10-23 00:05:47 -050078 int off;
79
80 off = fdt_path_offset(fdt, path);
81 if (off < 0)
82 return dflt;
83
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020084 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala1ba00402008-10-23 00:05:47 -050085}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040086
Kumar Galabf771dc2007-10-24 10:21:57 -050087/**
88 * fdt_find_and_setprop: Find a node and set it's property
89 *
90 * @fdt: ptr to device tree
91 * @node: path of node
92 * @prop: property name
93 * @val: ptr to new value
94 * @len: length of new property value
95 * @create: flag to create the property if it doesn't exist
96 *
97 * Convenience function to directly set a property given the path to the node.
98 */
99int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
100 const void *val, int len, int create)
101{
Kumar Galac8ab7052007-10-24 11:04:22 -0500102 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -0500103
104 if (nodeoff < 0)
105 return nodeoff;
106
Kim Phillips6542c072013-01-16 14:00:11 +0000107 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500108 return 0; /* create flag not set; so exit quietly */
109
110 return fdt_setprop(fdt, nodeoff, prop, val, len);
111}
112
Masahiro Yamadac0207282014-04-18 17:40:58 +0900113/**
Simon Glass014aa052014-10-23 18:58:49 -0600114 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
115 *
Masahiro Yamadac0207282014-04-18 17:40:58 +0900116 * @fdt: pointer to the device tree blob
117 * @parentoffset: structure block offset of a node
118 * @name: name of the subnode to locate
119 *
120 * fdt_subnode_offset() finds a subnode of the node with a given name.
121 * If the subnode does not exist, it will be created.
122 */
Simon Glass014aa052014-10-23 18:58:49 -0600123int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamadac0207282014-04-18 17:40:58 +0900124{
125 int offset;
126
127 offset = fdt_subnode_offset(fdt, parentoffset, name);
128
129 if (offset == -FDT_ERR_NOTFOUND)
130 offset = fdt_add_subnode(fdt, parentoffset, name);
131
132 if (offset < 0)
133 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
134
135 return offset;
136}
137
Andre Przywara0d1fcf82021-02-14 10:35:18 +0000138#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel46e192a2008-06-20 22:24:05 +0200139static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600140{
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900141 int err;
142 int aliasoff;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600143 char sername[9] = { 0 };
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900144 const void *path;
145 int len;
146 char tmp[256]; /* long enough */
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600147
Bin Mengb8199112016-01-13 19:38:58 -0800148 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600149
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900150 aliasoff = fdt_path_offset(fdt, "/aliases");
151 if (aliasoff < 0) {
152 err = aliasoff;
Scott Woodedd551f2015-09-01 22:48:08 -0500153 goto noalias;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600154 }
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900155
156 path = fdt_getprop(fdt, aliasoff, sername, &len);
157 if (!path) {
158 err = len;
Scott Woodedd551f2015-09-01 22:48:08 -0500159 goto noalias;
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900160 }
161
162 /* fdt_setprop may break "path" so we copy it to tmp buffer */
163 memcpy(tmp, path, len);
164
165 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600166 if (err < 0)
167 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900168 fdt_strerror(err));
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600169
170 return err;
Scott Woodedd551f2015-09-01 22:48:08 -0500171
172noalias:
173 printf("WARNING: %s: could not read %s alias: %s\n",
174 __func__, sername, fdt_strerror(err));
175
176 return 0;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600177}
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900178#else
179static int fdt_fixup_stdout(void *fdt, int chosenoff)
180{
181 return 0;
182}
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600183#endif
184
Masahiro Yamadab891a342014-04-18 17:41:04 +0900185static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
186 uint64_t val, int is_u64)
187{
188 if (is_u64)
189 return fdt_setprop_u64(fdt, nodeoffset, name, val);
190 else
191 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
192}
193
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200194int fdt_root(void *fdt)
195{
196 char *serial;
197 int err;
198
199 err = fdt_check_header(fdt);
200 if (err < 0) {
201 printf("fdt_root: %s\n", fdt_strerror(err));
202 return err;
203 }
204
Simon Glass64b723f2017-08-03 12:22:12 -0600205 serial = env_get("serial#");
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200206 if (serial) {
207 err = fdt_setprop(fdt, 0, "serial-number", serial,
208 strlen(serial) + 1);
209
210 if (err < 0) {
211 printf("WARNING: could not set serial-number %s.\n",
212 fdt_strerror(err));
213 return err;
214 }
215 }
216
217 return 0;
218}
Masahiro Yamadab891a342014-04-18 17:41:04 +0900219
Masahiro Yamada5b114892014-04-18 17:40:59 +0900220int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400221{
Masahiro Yamadab891a342014-04-18 17:41:04 +0900222 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500223 int err, j, total;
Masahiro Yamadab891a342014-04-18 17:41:04 +0900224 int is_u64;
Kumar Gala99ebc052008-08-15 08:24:43 -0500225 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400226
Masahiro Yamada9a3b4de2014-04-18 17:41:05 +0900227 /* just return if the size of initrd is zero */
228 if (initrd_start == initrd_end)
229 return 0;
230
Masahiro Yamadac0207282014-04-18 17:40:58 +0900231 /* find or create "/chosen" node. */
232 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
233 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500234 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400235
Kumar Gala99ebc052008-08-15 08:24:43 -0500236 total = fdt_num_mem_rsv(fdt);
237
238 /*
239 * Look for an existing entry and update it. If we don't find
240 * the entry, we will j be the next available slot.
241 */
242 for (j = 0; j < total; j++) {
243 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
244 if (addr == initrd_start) {
245 fdt_del_mem_rsv(fdt, j);
246 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400247 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500248 }
249
Grant Likelyc379a562011-03-28 09:58:55 +0000250 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500251 if (err < 0) {
252 printf("fdt_initrd: %s\n", fdt_strerror(err));
253 return err;
254 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500255
Simon Glass9fbc6322014-10-23 18:58:57 -0600256 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengd9850132013-12-14 11:47:29 +0800257
Masahiro Yamadab891a342014-04-18 17:41:04 +0900258 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
259 (uint64_t)initrd_start, is_u64);
260
Masahiro Yamada5b114892014-04-18 17:40:59 +0900261 if (err < 0) {
262 printf("WARNING: could not set linux,initrd-start %s.\n",
263 fdt_strerror(err));
264 return err;
265 }
Masahiro Yamadab891a342014-04-18 17:41:04 +0900266
267 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
268 (uint64_t)initrd_end, is_u64);
269
Masahiro Yamada5b114892014-04-18 17:40:59 +0900270 if (err < 0) {
271 printf("WARNING: could not set linux,initrd-end %s.\n",
272 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500273
Masahiro Yamada5b114892014-04-18 17:40:59 +0900274 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400275 }
276
Kumar Gala99ebc052008-08-15 08:24:43 -0500277 return 0;
278}
279
Tim Harvey00ae1222024-06-18 14:06:06 -0700280int fdt_kaslrseed(void *fdt, bool overwrite)
281{
282 int len, err, nodeoffset;
283 struct udevice *dev;
284 const u64 *orig;
285 u64 data = 0;
286
287 err = fdt_check_header(fdt);
288 if (err < 0)
289 return err;
290
291 /* find or create "/chosen" node. */
292 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
293 if (nodeoffset < 0)
294 return nodeoffset;
295
296 /* return without error if we are not overwriting and existing non-zero node */
297 orig = fdt_getprop(fdt, nodeoffset, "kaslr-seed", &len);
298 if (orig && len == sizeof(*orig))
299 data = fdt64_to_cpu(*orig);
300 if (data && !overwrite) {
301 debug("not overwriting existing kaslr-seed\n");
302 return 0;
303 }
304 err = uclass_get_device(UCLASS_RNG, 0, &dev);
305 if (err) {
306 printf("No RNG device\n");
307 return err;
308 }
309 err = dm_rng_read(dev, &data, sizeof(data));
310 if (err) {
311 dev_err(dev, "dm_rng_read failed: %d\n", err);
312 return err;
313 }
314 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", &data, sizeof(data));
315 if (err < 0)
316 printf("WARNING: could not set kaslr-seed %s.\n", fdt_strerror(err));
317
318 return err;
319}
320
Niko Mauno673db432021-02-22 19:18:51 +0000321/**
322 * board_fdt_chosen_bootargs - boards may override this function to use
323 * alternative kernel command line arguments
324 */
325__weak char *board_fdt_chosen_bootargs(void)
326{
327 return env_get("bootargs");
328}
329
Masahiro Yamada451c2042014-04-18 17:41:00 +0900330int fdt_chosen(void *fdt)
Kumar Gala99ebc052008-08-15 08:24:43 -0500331{
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200332 struct abuf buf = {};
Kumar Gala99ebc052008-08-15 08:24:43 -0500333 int nodeoffset;
334 int err;
335 char *str; /* used to set string properties */
Kumar Gala99ebc052008-08-15 08:24:43 -0500336
337 err = fdt_check_header(fdt);
338 if (err < 0) {
339 printf("fdt_chosen: %s\n", fdt_strerror(err));
340 return err;
341 }
342
Masahiro Yamadac0207282014-04-18 17:40:58 +0900343 /* find or create "/chosen" node. */
344 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
345 if (nodeoffset < 0)
346 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400347
Tim Harveyf8f480b2024-06-18 14:06:07 -0700348 /* if DM_RNG enabled automatically inject kaslr-seed node unless:
349 * CONFIG_MEASURED_BOOT enabled: as dt modifications break measured boot
350 * CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT enabled: as that implementation does not use dm yet
351 */
352 if (IS_ENABLED(CONFIG_DM_RNG) &&
353 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
354 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
355 fdt_kaslrseed(fdt, false);
356
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200357 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
358 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
359 abuf_data(&buf), abuf_size(&buf));
360 abuf_uninit(&buf);
361 if (err < 0) {
362 printf("WARNING: could not set rng-seed %s.\n",
363 fdt_strerror(err));
364 return err;
365 }
366 }
367
Niko Mauno673db432021-02-22 19:18:51 +0000368 str = board_fdt_chosen_bootargs();
369
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900370 if (str) {
371 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
372 strlen(str) + 1);
373 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900374 printf("WARNING: could not set bootargs %s.\n",
375 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900376 return err;
377 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400378 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600379
Francesco Dolcini05cc8272022-05-19 16:22:26 +0200380 /* add u-boot version */
381 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
382 strlen(PLAIN_VERSION) + 1);
383 if (err < 0) {
384 printf("WARNING: could not set u-boot,version %s.\n",
385 fdt_strerror(err));
386 return err;
387 }
388
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900389 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400390}
391
Kumar Gala7e64cf82007-11-03 19:46:28 -0500392void do_fixup_by_path(void *fdt, const char *path, const char *prop,
393 const void *val, int len, int create)
394{
395#if defined(DEBUG)
396 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600397 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500398 for (i = 0; i < len; i++)
399 debug(" %.2x", *(u8*)(val+i));
400 debug("\n");
401#endif
402 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
403 if (rc)
404 printf("Unable to update property %s:%s, err=%s\n",
405 path, prop, fdt_strerror(rc));
406}
407
408void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
409 u32 val, int create)
410{
Kim Phillips6542c072013-01-16 14:00:11 +0000411 fdt32_t tmp = cpu_to_fdt32(val);
412 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500413}
414
Kumar Gala7590a192007-11-21 13:30:15 -0600415void do_fixup_by_prop(void *fdt,
416 const char *pname, const void *pval, int plen,
417 const char *prop, const void *val, int len,
418 int create)
419{
420 int off;
421#if defined(DEBUG)
422 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600423 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600424 for (i = 0; i < len; i++)
425 debug(" %.2x", *(u8*)(val+i));
426 debug("\n");
427#endif
428 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
429 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000430 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600431 fdt_setprop(fdt, off, prop, val, len);
432 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
433 }
434}
435
436void do_fixup_by_prop_u32(void *fdt,
437 const char *pname, const void *pval, int plen,
438 const char *prop, u32 val, int create)
439{
Kim Phillips6542c072013-01-16 14:00:11 +0000440 fdt32_t tmp = cpu_to_fdt32(val);
441 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600442}
443
444void do_fixup_by_compat(void *fdt, const char *compat,
445 const char *prop, const void *val, int len, int create)
446{
447 int off = -1;
448#if defined(DEBUG)
449 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600450 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600451 for (i = 0; i < len; i++)
452 debug(" %.2x", *(u8*)(val+i));
453 debug("\n");
454#endif
Marek Behún5d6b4482022-01-20 01:04:42 +0100455 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips6542c072013-01-16 14:00:11 +0000456 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600457 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala7590a192007-11-21 13:30:15 -0600458}
459
460void do_fixup_by_compat_u32(void *fdt, const char *compat,
461 const char *prop, u32 val, int create)
462{
Kim Phillips6542c072013-01-16 14:00:11 +0000463 fdt32_t tmp = cpu_to_fdt32(val);
464 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600465}
466
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900467#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900468/*
469 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
470 */
Simon Glass0058e112014-10-23 18:58:55 -0600471static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
472 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900473{
474 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100475 int address_cells = fdt_address_cells(fdt, 0);
476 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900477 char *p = buf;
478
479 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100480 if (address_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500481 put_unaligned_be64(address[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900482 else
483 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100484 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900485
Hans de Goede4c80fb02014-11-28 14:23:51 +0100486 if (size_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500487 put_unaligned_be64(size[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900488 else
489 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100490 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900491 }
492
493 return p - (char *)buf;
494}
495
Ramon Fried9cab1832018-08-14 00:35:42 +0300496#if CONFIG_NR_DRAM_BANKS > 4
497#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
498#else
Kim Phillips6542c072013-01-16 14:00:11 +0000499#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300500#endif
Michal Simek96283732021-08-10 09:21:54 +0200501
502/**
503 * fdt_fixup_memory_banks - Update DT memory node
504 * @blob: Pointer to DT blob
505 * @start: Pointer to memory start addresses array
506 * @size: Pointer to memory sizes array
507 * @banks: Number of memory banks
508 *
509 * Return: 0 on success, negative value on failure
510 *
511 * Based on the passed number of banks and arrays, the function is able to
512 * update existing DT memory nodes to match run time detected/changed memory
513 * configuration. Implementation is handling one specific case with only one
514 * memory node where multiple tuples could be added/updated.
515 * The case where multiple memory nodes with a single tuple (base, size) are
516 * used, this function is only updating the first memory node without removing
517 * others.
518 */
John Rigbyb4ae9422010-10-13 13:57:33 -0600519int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
520{
521 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100522 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000523 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600524
Kim Phillips6542c072013-01-16 14:00:11 +0000525 if (banks > MEMORY_BANKS_MAX) {
526 printf("%s: num banks %d exceeds hardcoded limit %d."
527 " Recompile with higher MEMORY_BANKS_MAX?\n",
528 __FUNCTION__, banks, MEMORY_BANKS_MAX);
529 return -1;
530 }
531
Kumar Gala0db72ed2007-11-26 14:57:45 -0600532 err = fdt_check_header(blob);
533 if (err < 0) {
534 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
535 return err;
536 }
537
Masahiro Yamadac0207282014-04-18 17:40:58 +0900538 /* find or create "/memory" node. */
539 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
540 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800541 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900542
Kumar Gala0db72ed2007-11-26 14:57:45 -0600543 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
544 sizeof("memory"));
545 if (err < 0) {
546 printf("WARNING: could not set %s %s.\n", "device_type",
547 fdt_strerror(err));
548 return err;
549 }
550
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100551 for (i = 0; i < banks; i++) {
552 if (start[i] == 0 && size[i] == 0)
553 break;
554 }
555
556 banks = i;
557
Andre Przywara3d2d4412015-06-21 00:29:54 +0100558 if (!banks)
559 return 0;
560
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900561 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600562
563 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
564 if (err < 0) {
565 printf("WARNING: could not set %s %s.\n",
566 "reg", fdt_strerror(err));
567 return err;
568 }
569 return 0;
570}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200571
572int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
573{
574 int err, nodeoffset;
575 int len;
576 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
577
578 if (areas > 8) {
579 printf("%s: num areas %d exceeds hardcoded limit %d\n",
580 __func__, areas, 8);
581 return -1;
582 }
583
584 err = fdt_check_header(blob);
585 if (err < 0) {
586 printf("%s: %s\n", __func__, fdt_strerror(err));
587 return err;
588 }
589
590 /* find or create "/memory" node. */
591 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
592 if (nodeoffset < 0)
593 return nodeoffset;
594
595 len = fdt_pack_reg(blob, tmp, start, size, areas);
596
597 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
598 if (err < 0) {
599 printf("WARNING: could not set %s %s.\n",
600 "reg", fdt_strerror(err));
601 return err;
602 }
603
604 return 0;
605}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900606#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600607
John Rigbyb4ae9422010-10-13 13:57:33 -0600608int fdt_fixup_memory(void *blob, u64 start, u64 size)
609{
610 return fdt_fixup_memory_banks(blob, &start, &size, 1);
611}
612
Kumar Galafabda922008-08-19 15:41:18 -0500613void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600614{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530615 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800616 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000617 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600618 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100619 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800620 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530621#ifdef FDT_SEQ_MACADDR_FROM_ENV
622 int nodeoff;
623 const struct fdt_property *fdt_prop;
624#endif
Kumar Gala22699102007-11-21 11:11:03 -0600625
Lev Iserovichcde77612016-01-07 18:04:16 -0500626 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500627 return;
628
Lev Iserovichcde77612016-01-07 18:04:16 -0500629 /* Cycle through all aliases */
630 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800631 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500632
Lev Iserovichcde77612016-01-07 18:04:16 -0500633 /* FDT might have been edited, recompute the offset */
634 offset = fdt_first_property_offset(fdt,
635 fdt_path_offset(fdt, "/aliases"));
636 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530637 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500638 offset = fdt_next_property_offset(fdt, offset);
639
640 if (offset < 0)
641 break;
642
Bin Mengbfb93cc2015-11-03 04:24:41 -0800643 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200644 if (!strncmp(name, "ethernet", 8)) {
645 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530646 if (!strcmp(name, "ethernet")
647#ifdef FDT_SEQ_MACADDR_FROM_ENV
648 || !strcmp(name, "ethernet0")
649#endif
650 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200651 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530652#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200653 else
654 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530655#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800656 if (i != -1) {
657 if (i == 0)
658 strcpy(mac, "ethaddr");
659 else
660 sprintf(mac, "eth%daddr", i);
661 } else {
662 continue;
663 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530664#ifdef FDT_SEQ_MACADDR_FROM_ENV
665 nodeoff = fdt_path_offset(fdt, path);
666 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
667 NULL);
668 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
669 continue;
670 i++;
671#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600672 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800673 if (!tmp)
674 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500675
Bin Mengbfb93cc2015-11-03 04:24:41 -0800676 for (j = 0; j < 6; j++) {
677 mac_addr[j] = tmp ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600678 hextoul(tmp, &end) : 0;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800679 if (tmp)
680 tmp = (*end) ? end + 1 : end;
681 }
Kumar Galafabda922008-08-19 15:41:18 -0500682
Bin Mengbfb93cc2015-11-03 04:24:41 -0800683 do_fixup_by_path(fdt, path, "mac-address",
684 &mac_addr, 6, 0);
685 do_fixup_by_path(fdt, path, "local-mac-address",
686 &mac_addr, 6, 1);
687 }
Kumar Gala22699102007-11-21 11:11:03 -0600688 }
689}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300690
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100691int fdt_record_loadable(void *blob, u32 index, const char *name,
692 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simek0e4f43d2021-05-27 11:40:09 +0200693 const char *type, const char *os, const char *arch)
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100694{
695 int err, node;
696
697 err = fdt_check_header(blob);
698 if (err < 0) {
699 printf("%s: %s\n", __func__, fdt_strerror(err));
700 return err;
701 }
702
703 /* find or create "/fit-images" node */
704 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
705 if (node < 0)
706 return node;
707
708 /* find or create "/fit-images/<name>" node */
709 node = fdt_find_or_add_subnode(blob, node, name);
710 if (node < 0)
711 return node;
712
Michal Simekd31b40f2020-09-03 12:44:51 +0200713 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100714 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200715 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100716 fdt_setprop_u32(blob, node, "size", size);
717 if (type)
718 fdt_setprop_string(blob, node, "type", type);
719 if (os)
720 fdt_setprop_string(blob, node, "os", os);
Michal Simek0e4f43d2021-05-27 11:40:09 +0200721 if (arch)
722 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100723
724 return node;
725}
726
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200727int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500728{
729 int i;
730 uint64_t addr, size;
731 int total, ret;
732 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200733 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500734
735 if (!blob)
736 return 0;
737
738 total = fdt_num_mem_rsv(blob);
739 for (i = 0; i < total; i++) {
740 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000741 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500742 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200743 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500744 break;
745 }
746 }
747
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100748 /*
749 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200750 * plus the size needed for 5 fdt_add_mem_rsv, one
751 * for the fdt itself and 4 for a possible initrd
752 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100753 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500754 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200755 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500756
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200757 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500758 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000759 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
760 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500761
762 /* Change the fdt header to reflect the correct size */
763 fdt_set_totalsize(blob, actualsize);
764
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200765 if (fdt_memrsv) {
766 /* Add the new reservation */
767 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
768 if (ret < 0)
769 return ret;
770 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500771
772 return actualsize;
773}
Kumar Galae0475cd2008-10-22 23:33:56 -0500774
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100775/**
776 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
777 *
778 * @blob: ptr to device tree
779 */
780int fdt_delete_disabled_nodes(void *blob)
781{
782 while (1) {
783 int ret, offset;
784
785 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
786 "disabled", 9);
787 if (offset < 0)
788 break;
789
790 ret = fdt_del_node(blob, offset);
791 if (ret < 0)
792 return ret;
793 }
794
795 return 0;
796}
797
Kumar Galae0475cd2008-10-22 23:33:56 -0500798#ifdef CONFIG_PCI
Tom Rini56af6592022-11-16 13:10:33 -0500799#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500800
801#define FDT_PCI_PREFETCH (0x40000000)
802#define FDT_PCI_MEM32 (0x02000000)
803#define FDT_PCI_IO (0x01000000)
804#define FDT_PCI_MEM64 (0x03000000)
805
806int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
807
808 int addrcell, sizecell, len, r;
809 u32 *dma_range;
810 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Rini56af6592022-11-16 13:10:33 -0500811 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Galae0475cd2008-10-22 23:33:56 -0500812
813 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
814 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
815
816 dma_range = &dma_ranges[0];
817 for (r = 0; r < hose->region_count; r++) {
818 u64 bus_start, phys_start, size;
819
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600820 /* skip if !PCI_REGION_SYS_MEMORY */
821 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500822 continue;
823
824 bus_start = (u64)hose->regions[r].bus_start;
825 phys_start = (u64)hose->regions[r].phys_start;
826 size = (u64)hose->regions[r].size;
827
828 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500829 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200830 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500831 else
Marek Vasut22203f52019-07-09 02:49:29 +0200832 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500833 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200834 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500835#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200836 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500837#else
838 dma_range[1] = 0;
839#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200840 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500841
842 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200843 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
844 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500845 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200846 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500847 }
848
849 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200850 dma_range[3 + addrcell + 0] =
851 cpu_to_fdt32(size >> 32);
852 dma_range[3 + addrcell + 1] =
853 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500854 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200855 dma_range[3 + addrcell + 0] =
856 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500857 }
858
859 dma_range += (3 + addrcell + sizecell);
860 }
861
862 len = dma_range - &dma_ranges[0];
863 if (len)
864 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
865
866 return 0;
867}
868#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200869
Matthew McClintockacda3a42010-10-13 13:39:26 +0200870int fdt_increase_size(void *fdt, int add_len)
871{
872 int newlen;
873
874 newlen = fdt_totalsize(fdt) + add_len;
875
876 /* Open in place with a new len */
877 return fdt_open_into(fdt, fdt, newlen);
878}
879
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100880#ifdef CONFIG_FDT_FIXUP_PARTITIONS
881#include <jffs2/load_kernel.h>
882#include <mtd_node.h>
883
Michal Simekc2ed0072018-07-31 14:31:04 +0200884static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100885{
886 int off, ndepth;
887 int ret;
888
889 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
890 (off >= 0) && (ndepth > 0);
891 off = fdt_next_node(blob, off, &ndepth)) {
892 if (ndepth == 1) {
893 debug("delete %s: offset: %x\n",
894 fdt_get_name(blob, off, 0), off);
895 ret = fdt_del_node((void *)blob, off);
896 if (ret < 0) {
897 printf("Can't delete node: %s\n",
898 fdt_strerror(ret));
899 return ret;
900 } else {
901 ndepth = 0;
902 off = parent_offset;
903 }
904 }
905 }
906 return 0;
907}
908
Michal Simekc2ed0072018-07-31 14:31:04 +0200909static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100910{
911 const void *prop;
912 int ndepth = 0;
913 int off;
914 int ret;
915
916 off = fdt_next_node(blob, parent_offset, &ndepth);
917 if (off > 0 && ndepth == 1) {
918 prop = fdt_getprop(blob, off, "label", NULL);
919 if (prop == NULL) {
920 /*
921 * Could not find label property, nand {}; node?
922 * Check subnode, delete partitions there if any.
923 */
924 return fdt_del_partitions(blob, off);
925 } else {
926 ret = fdt_del_subnodes(blob, parent_offset);
927 if (ret < 0) {
928 printf("Can't remove subnodes: %s\n",
929 fdt_strerror(ret));
930 return ret;
931 }
932 }
933 }
934 return 0;
935}
936
Masahiro Yamada65669602020-07-15 19:35:47 +0900937static int fdt_node_set_part_info(void *blob, int parent_offset,
938 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100939{
940 struct list_head *pentry;
941 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100942 int off, ndepth = 0;
943 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300944 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100945 char buf[64];
946
947 ret = fdt_del_partitions(blob, parent_offset);
948 if (ret < 0)
949 return ret;
950
951 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300952 * Check if size/address is 1 or 2 cells.
953 * We assume #address-cells and #size-cells have same value.
954 */
955 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
956 0, "#size-cells", 1);
957
958 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100959 * Check if it is nand {}; subnode, adjust
960 * the offset in this case
961 */
962 off = fdt_next_node(blob, parent_offset, &ndepth);
963 if (off > 0 && ndepth == 1)
964 parent_offset = off;
965
966 part_num = 0;
967 list_for_each_prev(pentry, &dev->parts) {
968 int newoff;
969
970 part = list_entry(pentry, struct part_info, link);
971
Scott Woodefe7f302013-10-15 17:41:27 -0500972 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100973 part_num, part->name, part->size,
974 part->offset, part->mask_flags);
975
Scott Woodefe7f302013-10-15 17:41:27 -0500976 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100977add_sub:
978 ret = fdt_add_subnode(blob, parent_offset, buf);
979 if (ret == -FDT_ERR_NOSPACE) {
980 ret = fdt_increase_size(blob, 512);
981 if (!ret)
982 goto add_sub;
983 else
984 goto err_size;
985 } else if (ret < 0) {
986 printf("Can't add partition node: %s\n",
987 fdt_strerror(ret));
988 return ret;
989 }
990 newoff = ret;
991
992 /* Check MTD_WRITEABLE_CMD flag */
993 if (part->mask_flags & 1) {
994add_ro:
995 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
996 if (ret == -FDT_ERR_NOSPACE) {
997 ret = fdt_increase_size(blob, 512);
998 if (!ret)
999 goto add_ro;
1000 else
1001 goto err_size;
1002 } else if (ret < 0)
1003 goto err_prop;
1004 }
1005
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001006add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +03001007 if (sizecell == 2) {
1008 ret = fdt_setprop_u64(blob, newoff,
1009 "reg", part->offset);
1010 if (!ret)
1011 ret = fdt_appendprop_u64(blob, newoff,
1012 "reg", part->size);
1013 } else {
1014 ret = fdt_setprop_u32(blob, newoff,
1015 "reg", part->offset);
1016 if (!ret)
1017 ret = fdt_appendprop_u32(blob, newoff,
1018 "reg", part->size);
1019 }
1020
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001021 if (ret == -FDT_ERR_NOSPACE) {
1022 ret = fdt_increase_size(blob, 512);
1023 if (!ret)
1024 goto add_reg;
1025 else
1026 goto err_size;
1027 } else if (ret < 0)
1028 goto err_prop;
1029
1030add_label:
1031 ret = fdt_setprop_string(blob, newoff, "label", part->name);
1032 if (ret == -FDT_ERR_NOSPACE) {
1033 ret = fdt_increase_size(blob, 512);
1034 if (!ret)
1035 goto add_label;
1036 else
1037 goto err_size;
1038 } else if (ret < 0)
1039 goto err_prop;
1040
1041 part_num++;
1042 }
1043 return 0;
1044err_size:
1045 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1046 return ret;
1047err_prop:
1048 printf("Can't add property: %s\n", fdt_strerror(ret));
1049 return ret;
1050}
1051
1052/*
1053 * Update partitions in nor/nand nodes using info from
1054 * mtdparts environment variable. The nodes to update are
1055 * specified by node_info structure which contains mtd device
1056 * type and compatible string: E. g. the board code in
1057 * ft_board_setup() could use:
1058 *
1059 * struct node_info nodes[] = {
1060 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1061 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1062 * };
1063 *
1064 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1065 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001066void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1067 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001068{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001069 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001070 int i, idx;
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001071 int noff, parts;
Masahiro Yamada030348f2020-07-17 10:46:18 +09001072 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001073
1074 for (i = 0; i < node_info_size; i++) {
1075 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +09001076
Marek Behún5d6b4482022-01-20 01:04:42 +01001077 fdt_for_each_node_by_compatible(noff, blob, -1,
1078 node_info[i].compat) {
Masahiro Yamada21644292020-07-17 10:46:19 +09001079 const char *prop;
1080
1081 prop = fdt_getprop(blob, noff, "status", NULL);
1082 if (prop && !strcmp(prop, "disabled"))
1083 continue;
1084
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001085 debug("%s: %s, mtd dev type %d\n",
1086 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001087 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +09001088
1089 if (!inited) {
1090 if (mtdparts_init() != 0)
1091 return;
1092 inited = true;
1093 }
1094
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001095 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001096 if (dev) {
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001097 parts = fdt_subnode_offset(blob, noff,
1098 "partitions");
1099 if (parts < 0)
1100 parts = noff;
1101
1102 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001103 return; /* return on error */
1104 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001105 }
1106 }
1107}
1108#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001109
Patrick Delaunay019127b2023-06-08 17:16:38 +02001110int fdt_copy_fixed_partitions(void *blob)
1111{
1112 ofnode node, subnode;
1113 int off, suboff, res;
1114 char path[256];
1115 int address_cells, size_cells;
1116 u8 i, j, child_count;
1117
1118 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1119 while (ofnode_valid(node)) {
1120 /* copy the U-Boot fixed partition */
1121 address_cells = ofnode_read_simple_addr_cells(node);
1122 size_cells = ofnode_read_simple_size_cells(node);
1123
1124 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1125 if (res)
1126 return res;
1127
1128 off = fdt_path_offset(blob, path);
1129 if (off < 0)
1130 return -ENODEV;
1131
1132 off = fdt_find_or_add_subnode(blob, off, "partitions");
1133 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1134 if (res)
1135 return res;
1136
1137 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1138 if (res)
1139 return res;
1140
1141 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1142 if (res)
1143 return res;
1144
1145 /*
1146 * parse partition in reverse order as fdt_find_or_add_subnode() only
1147 * insert the new node after the parent's properties
1148 */
1149 child_count = ofnode_get_child_count(node);
1150 for (i = child_count; i > 0 ; i--) {
1151 subnode = ofnode_first_subnode(node);
1152 if (!ofnode_valid(subnode))
1153 break;
1154
1155 for (j = 0; (j < i - 1); j++)
1156 subnode = ofnode_next_subnode(subnode);
1157
1158 if (!ofnode_valid(subnode))
1159 break;
1160
1161 const u32 *reg;
1162 int len;
1163
1164 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1165 res = fdt_setprop_string(blob, suboff, "label",
1166 ofnode_read_string(subnode, "label"));
1167 if (res)
1168 return res;
1169
1170 reg = ofnode_get_property(subnode, "reg", &len);
1171 res = fdt_setprop(blob, suboff, "reg", reg, len);
1172 if (res)
1173 return res;
1174 }
1175
1176 /* go to next fixed-partitions node */
1177 node = ofnode_by_compatible(node, "fixed-partitions");
1178 }
1179
1180 return 0;
1181}
1182
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001183void fdt_del_node_and_alias(void *blob, const char *alias)
1184{
1185 int off = fdt_path_offset(blob, alias);
1186
1187 if (off < 0)
1188 return;
1189
1190 fdt_del_node(blob, off);
1191
1192 off = fdt_path_offset(blob, "/aliases");
1193 fdt_delprop(blob, off, alias);
1194}
Kumar Galab87e7932010-06-16 14:27:38 -05001195
Kumar Galab87e7932010-06-16 14:27:38 -05001196/* Max address size we deal with */
1197#define OF_MAX_ADDR_CELLS 4
Dario Binacchif8fc7032021-05-01 17:05:26 +02001198#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1199 (ns) > 0)
Kumar Galab87e7932010-06-16 14:27:38 -05001200
1201/* Debug utility */
1202#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +00001203static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001204{
1205 printf("%s", s);
1206 while(na--)
1207 printf(" %08x", *(addr++));
1208 printf("\n");
1209}
1210#else
Kim Phillips6542c072013-01-16 14:00:11 +00001211static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001212#endif
1213
Paul Burtondbef2f02016-05-17 07:43:24 +01001214/**
1215 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001216 * @name: A string used to identify this bus in debug output.
1217 * @addresses: The name of the DT property from which addresses are
1218 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001219 * @match: Return non-zero if the node whose parent is at
1220 * parentoffset in the FDT blob corresponds to a bus
1221 * of this type, otherwise return zero. If NULL a match
1222 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001223 * @count_cells:Count how many cells (be32 values) a node whose parent
1224 * is at parentoffset in the FDT blob will require to
1225 * represent its address (written to *addrc) & size
1226 * (written to *sizec).
1227 * @map: Map the address addr from the address space of this
1228 * bus to that of its parent, making use of the ranges
1229 * read from DT to an array at range. na and ns are the
1230 * number of cells (be32 values) used to hold and address
1231 * or size, respectively, for this bus. pna is the number
1232 * of cells used to hold an address for the parent bus.
1233 * Returns the address in the address space of the parent
1234 * bus.
1235 * @translate: Update the value of the address cells at addr within an
1236 * FDT by adding offset to it. na specifies the number of
1237 * cells used to hold the address being translated. Returns
1238 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001239 *
1240 * Each bus type will include a struct of_bus in the of_busses array,
1241 * providing implementations of some or all of the functions used to
1242 * match the bus & handle address translation for its children.
1243 */
Kumar Galab87e7932010-06-16 14:27:38 -05001244struct of_bus {
1245 const char *name;
1246 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001247 int (*match)(const void *blob, int parentoffset);
1248 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001249 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001250 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001251 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001252 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001253};
1254
1255/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001256void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001257 int *addrc, int *sizec)
1258{
Kim Phillips6542c072013-01-16 14:00:11 +00001259 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001260
Simon Glass9fbc6322014-10-23 18:58:57 -06001261 if (addrc)
1262 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001263
1264 if (sizec) {
1265 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1266 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001267 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001268 else
1269 *sizec = 1;
1270 }
Kumar Galab87e7932010-06-16 14:27:38 -05001271}
1272
Kim Phillips6542c072013-01-16 14:00:11 +00001273static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001274 int na, int ns, int pna)
1275{
1276 u64 cp, s, da;
1277
Simon Glassbb7c01e2017-05-18 20:09:26 -06001278 cp = fdt_read_number(range, na);
1279 s = fdt_read_number(range + na + pna, ns);
1280 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001281
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301282 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001283
1284 if (da < cp || da >= (cp + s))
1285 return OF_BAD_ADDR;
1286 return da - cp;
1287}
1288
Kim Phillips6542c072013-01-16 14:00:11 +00001289static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001290{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001291 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001292 memset(addr, 0, na * 4);
1293 a += offset;
1294 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001295 addr[na - 2] = cpu_to_fdt32(a >> 32);
1296 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001297
1298 return 0;
1299}
1300
Paul Burtondbef2f02016-05-17 07:43:24 +01001301#ifdef CONFIG_OF_ISA_BUS
1302
1303/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001304static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001305{
1306 const char *name;
1307
1308 name = fdt_get_name(blob, parentoffset, NULL);
1309 if (!name)
1310 return 0;
1311
1312 return !strcmp(name, "isa");
1313}
1314
Stephen Warrenc99581b2016-08-05 09:47:50 -06001315static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001316 int *addrc, int *sizec)
1317{
1318 if (addrc)
1319 *addrc = 2;
1320 if (sizec)
1321 *sizec = 1;
1322}
1323
1324static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1325 int na, int ns, int pna)
1326{
1327 u64 cp, s, da;
1328
1329 /* Check address type match */
1330 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1331 return OF_BAD_ADDR;
1332
Simon Glassbb7c01e2017-05-18 20:09:26 -06001333 cp = fdt_read_number(range + 1, na - 1);
1334 s = fdt_read_number(range + na + pna, ns);
1335 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001336
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301337 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001338
1339 if (da < cp || da >= (cp + s))
1340 return OF_BAD_ADDR;
1341 return da - cp;
1342}
1343
1344static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1345{
1346 return of_bus_default_translate(addr + 1, offset, na - 1);
1347}
1348
1349#endif /* CONFIG_OF_ISA_BUS */
1350
Kumar Galab87e7932010-06-16 14:27:38 -05001351/* Array of bus specific translators */
1352static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001353#ifdef CONFIG_OF_ISA_BUS
1354 /* ISA */
1355 {
1356 .name = "isa",
1357 .addresses = "reg",
1358 .match = of_bus_isa_match,
1359 .count_cells = of_bus_isa_count_cells,
1360 .map = of_bus_isa_map,
1361 .translate = of_bus_isa_translate,
1362 },
1363#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001364 /* Default */
1365 {
1366 .name = "default",
1367 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001368 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001369 .map = of_bus_default_map,
1370 .translate = of_bus_default_translate,
1371 },
1372};
1373
Stephen Warrenc99581b2016-08-05 09:47:50 -06001374static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001375{
1376 struct of_bus *bus;
1377
1378 if (ARRAY_SIZE(of_busses) == 1)
1379 return of_busses;
1380
1381 for (bus = of_busses; bus; bus++) {
1382 if (!bus->match || bus->match(blob, parentoffset))
1383 return bus;
1384 }
1385
1386 /*
1387 * We should always have matched the default bus at least, since
1388 * it has a NULL match field. If we didn't then it somehow isn't
1389 * in the of_busses array or something equally catastrophic has
1390 * gone wrong.
1391 */
1392 assert(0);
1393 return NULL;
1394}
1395
Stephen Warrenc99581b2016-08-05 09:47:50 -06001396static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001397 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001398 int na, int ns, int pna, const char *rprop)
1399{
Kim Phillips6542c072013-01-16 14:00:11 +00001400 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001401 int rlen;
1402 int rone;
1403 u64 offset = OF_BAD_ADDR;
1404
1405 /* Normally, an absence of a "ranges" property means we are
1406 * crossing a non-translatable boundary, and thus the addresses
1407 * below the current not cannot be converted to CPU physical ones.
1408 * Unfortunately, while this is very clear in the spec, it's not
1409 * what Apple understood, and they do have things like /uni-n or
1410 * /ht nodes with no "ranges" property and a lot of perfectly
1411 * useable mapped devices below them. Thus we treat the absence of
1412 * "ranges" as equivalent to an empty "ranges" property which means
1413 * a 1:1 translation at that level. It's up to the caller not to try
1414 * to translate addresses that aren't supposed to be translated in
1415 * the first place. --BenH.
1416 */
Kim Phillips6542c072013-01-16 14:00:11 +00001417 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001418 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001419 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001420 memset(addr, 0, pna * 4);
1421 debug("OF: no ranges, 1:1 translation\n");
1422 goto finish;
1423 }
1424
1425 debug("OF: walking ranges...\n");
1426
1427 /* Now walk through the ranges */
1428 rlen /= 4;
1429 rone = na + pna + ns;
1430 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1431 offset = bus->map(addr, ranges, na, ns, pna);
1432 if (offset != OF_BAD_ADDR)
1433 break;
1434 }
1435 if (offset == OF_BAD_ADDR) {
1436 debug("OF: not found !\n");
1437 return 1;
1438 }
1439 memcpy(addr, ranges + na, 4 * pna);
1440
1441 finish:
1442 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001443 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001444
1445 /* Translate it into parent bus space */
1446 return pbus->translate(addr, offset, pna);
1447}
1448
1449/*
1450 * Translate an address from the device-tree into a CPU physical address,
1451 * this walks up the tree and applies the various bus mappings on the
1452 * way.
1453 *
1454 * Note: We consider that crossing any level with #size-cells == 0 to mean
1455 * that translation is impossible (that is we are not dealing with a value
1456 * that can be mapped to a cpu physical address). This is not really specified
1457 * that way, but this is traditionally the way IBM at least do things
1458 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001459static u64 __of_translate_address(const void *blob, int node_offset,
1460 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001461{
1462 int parent;
1463 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001464 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001465 int na, ns, pna, pns;
1466 u64 result = OF_BAD_ADDR;
1467
1468 debug("OF: ** translation for device %s **\n",
1469 fdt_get_name(blob, node_offset, NULL));
1470
1471 /* Get parent & match bus type */
1472 parent = fdt_parent_offset(blob, node_offset);
1473 if (parent < 0)
1474 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001475 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001476
1477 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001478 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001479 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001480 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1481 fdt_get_name(blob, node_offset, NULL));
1482 goto bail;
1483 }
1484 memcpy(addr, in_addr, na * 4);
1485
1486 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1487 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1488 of_dump_addr("OF: translating address:", addr, na);
1489
1490 /* Translate */
1491 for (;;) {
1492 /* Switch to parent bus */
1493 node_offset = parent;
1494 parent = fdt_parent_offset(blob, node_offset);
1495
1496 /* If root, we have finished */
1497 if (parent < 0) {
1498 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001499 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001500 break;
1501 }
1502
1503 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001504 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001505 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001506 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001507 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1508 fdt_get_name(blob, node_offset, NULL));
1509 break;
1510 }
1511
1512 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1513 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1514
1515 /* Apply bus translation */
1516 if (of_translate_one(blob, node_offset, bus, pbus,
1517 addr, na, ns, pna, rprop))
1518 break;
1519
1520 /* Complete the move up one level */
1521 na = pna;
1522 ns = pns;
1523 bus = pbus;
1524
1525 of_dump_addr("OF: one level translation:", addr, na);
1526 }
1527 bail:
1528
1529 return result;
1530}
1531
Stephen Warrenc99581b2016-08-05 09:47:50 -06001532u64 fdt_translate_address(const void *blob, int node_offset,
1533 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001534{
1535 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1536}
Kumar Gala800d1d12010-07-04 12:48:21 -05001537
Fabien Dessenne22236e02019-05-31 15:11:30 +02001538u64 fdt_translate_dma_address(const void *blob, int node_offset,
1539 const fdt32_t *in_addr)
1540{
1541 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1542}
1543
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001544int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1545 dma_addr_t *bus, u64 *size)
1546{
1547 bool found_dma_ranges = false;
1548 struct of_bus *bus_node;
1549 const fdt32_t *ranges;
1550 int na, ns, pna, pns;
1551 int parent = node;
1552 int ret = 0;
1553 int len;
1554
1555 /* Find the closest dma-ranges property */
1556 while (parent >= 0) {
1557 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1558
1559 /* Ignore empty ranges, they imply no translation required */
1560 if (ranges && len > 0)
1561 break;
1562
1563 /* Once we find 'dma-ranges', then a missing one is an error */
1564 if (found_dma_ranges && !ranges) {
1565 ret = -EINVAL;
1566 goto out;
1567 }
1568
1569 if (ranges)
1570 found_dma_ranges = true;
1571
1572 parent = fdt_parent_offset(blob, parent);
1573 }
1574
1575 if (!ranges || parent < 0) {
1576 debug("no dma-ranges found for node %s\n",
1577 fdt_get_name(blob, node, NULL));
1578 ret = -ENOENT;
1579 goto out;
1580 }
1581
1582 /* switch to that node */
1583 node = parent;
1584 parent = fdt_parent_offset(blob, node);
1585 if (parent < 0) {
Vagrant Cascadiand00c5c72021-12-21 13:06:58 -08001586 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001587 ret = -EINVAL;
1588 goto out;
1589 }
1590
1591 /* Get the address sizes both for the bus and its parent */
1592 bus_node = of_match_bus(blob, node);
1593 bus_node->count_cells(blob, node, &na, &ns);
1594 if (!OF_CHECK_COUNTS(na, ns)) {
1595 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1596 fdt_get_name(blob, node, NULL));
1597 return -EINVAL;
1598 goto out;
1599 }
1600
1601 bus_node = of_match_bus(blob, parent);
1602 bus_node->count_cells(blob, parent, &pna, &pns);
1603 if (!OF_CHECK_COUNTS(pna, pns)) {
1604 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1605 fdt_get_name(blob, parent, NULL));
1606 return -EINVAL;
1607 goto out;
1608 }
1609
1610 *bus = fdt_read_number(ranges, na);
1611 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1612 *size = fdt_read_number(ranges + na + pna, ns);
1613out:
1614 return ret;
1615}
1616
Kumar Gala800d1d12010-07-04 12:48:21 -05001617/**
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001618 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala800d1d12010-07-04 12:48:21 -05001619 * who's reg property matches a physical cpu address
1620 *
1621 * @blob: ptr to device tree
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001622 * @compat: compatible string to match
Kumar Gala800d1d12010-07-04 12:48:21 -05001623 * @compat_off: property name
1624 *
1625 */
1626int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1627 phys_addr_t compat_off)
1628{
Marek Behún5d6b4482022-01-20 01:04:42 +01001629 int len, off;
1630
1631 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips6542c072013-01-16 14:00:11 +00001632 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún5d6b4482022-01-20 01:04:42 +01001633 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1634 return off;
Kumar Gala800d1d12010-07-04 12:48:21 -05001635 }
1636
1637 return -FDT_ERR_NOTFOUND;
1638}
1639
Marek Behún4ef5ca62021-11-26 14:57:10 +01001640static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1641{
1642 char path[512];
1643 int len;
1644
1645 len = vsnprintf(path, sizeof(path), fmt, ap);
1646 if (len < 0 || len + 1 > sizeof(path))
1647 return -FDT_ERR_NOSPACE;
1648
1649 return fdt_path_offset(blob, path);
1650}
1651
1652/**
1653 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1654 *
1655 * @blob: ptr to device tree
1656 * @fmt: path format
1657 * @ap: vsnprintf arguments
1658 */
1659int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1660{
1661 va_list ap;
1662 int res;
1663
1664 va_start(ap, fmt);
1665 res = vnode_offset_by_pathf(blob, fmt, ap);
1666 va_end(ap);
1667
1668 return res;
1669}
1670
Gerald Van Barenaf737882011-07-14 21:40:10 -04001671/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001672 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001673 *
1674 * @fdt: ptr to device tree
1675 * @nodeoffset: node to update
1676 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001677 */
1678int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001679{
1680 int ret;
1681
1682#ifdef DEBUG
1683 int off = fdt_node_offset_by_phandle(fdt, phandle);
1684
1685 if ((off >= 0) && (off != nodeoffset)) {
1686 char buf[64];
1687
1688 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1689 printf("Trying to update node %s with phandle %u ",
1690 buf, phandle);
1691
1692 fdt_get_path(fdt, off, buf, sizeof(buf));
1693 printf("that already exists in node %s.\n", buf);
1694 return -FDT_ERR_BADPHANDLE;
1695 }
1696#endif
1697
1698 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barenaf737882011-07-14 21:40:10 -04001699
1700 return ret;
1701}
1702
Kumar Galad5fc2582011-08-01 00:25:20 -05001703/*
Marek Behúnae0ef952021-11-26 14:57:09 +01001704 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Galad5fc2582011-08-01 00:25:20 -05001705 *
1706 * @fdt: ptr to device tree
1707 * @nodeoffset: node to update
1708 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001709unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001710{
1711 /* see if there is a phandle already */
Marek Behún3577eba32021-11-26 14:57:07 +01001712 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Galad5fc2582011-08-01 00:25:20 -05001713
1714 /* if we got 0, means no phandle so create one */
1715 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001716 int ret;
1717
Marek Behún3577eba32021-11-26 14:57:07 +01001718 ret = fdt_generate_phandle(fdt, &phandle);
1719 if (ret < 0) {
1720 printf("Can't generate phandle: %s\n",
1721 fdt_strerror(ret));
1722 return 0;
1723 }
1724
Timur Tabi0f46c802011-09-20 18:24:34 -05001725 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1726 if (ret < 0) {
1727 printf("Can't set phandle %u: %s\n", phandle,
1728 fdt_strerror(ret));
1729 return 0;
1730 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001731 }
1732
1733 return phandle;
1734}
1735
Marek Behún4ef5ca62021-11-26 14:57:10 +01001736/**
1737 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1738 * given compatible
1739 *
1740 * @fdt: ptr to device tree
1741 * @compat: node's compatible string
1742 */
1743unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1744{
1745 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1746
1747 if (offset < 0) {
1748 printf("Can't find node with compatible \"%s\": %s\n", compat,
1749 fdt_strerror(offset));
1750 return 0;
1751 }
1752
1753 return fdt_create_phandle(fdt, offset);
1754}
1755
1756/**
1757 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1758 * sprintf-formatted path
1759 *
1760 * @fdt: ptr to device tree
1761 * @fmt, ...: path format string and arguments to pass to sprintf
1762 */
1763unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1764{
1765 va_list ap;
1766 int offset;
1767
1768 va_start(ap, fmt);
1769 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1770 va_end(ap);
1771
1772 if (offset < 0) {
1773 printf("Can't find node by given path: %s\n",
1774 fdt_strerror(offset));
1775 return 0;
1776 }
1777
1778 return fdt_create_phandle(fdt, offset);
1779}
1780
Shengzhou Liua7be8022011-10-14 16:26:05 +08001781/*
1782 * fdt_set_node_status: Set status for the given node
1783 *
1784 * @fdt: ptr to device tree
1785 * @nodeoffset: node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001786 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001787 */
Marek Behúnf872e832021-11-26 14:57:08 +01001788int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001789{
Shengzhou Liua7be8022011-10-14 16:26:05 +08001790 int ret = 0;
1791
1792 if (nodeoffset < 0)
1793 return nodeoffset;
1794
1795 switch (status) {
1796 case FDT_STATUS_OKAY:
1797 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1798 break;
1799 case FDT_STATUS_DISABLED:
1800 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1801 break;
1802 case FDT_STATUS_FAIL:
1803 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1804 break;
Shengzhou Liua7be8022011-10-14 16:26:05 +08001805 default:
1806 printf("Invalid fdt status: %x\n", status);
1807 ret = -1;
1808 break;
1809 }
1810
1811 return ret;
1812}
1813
1814/*
1815 * fdt_set_status_by_alias: Set status for the given node given an alias
1816 *
1817 * @fdt: ptr to device tree
1818 * @alias: alias of node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001819 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001820 */
1821int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behúnf872e832021-11-26 14:57:08 +01001822 enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001823{
1824 int offset = fdt_path_offset(fdt, alias);
1825
Marek Behúnf872e832021-11-26 14:57:08 +01001826 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liua7be8022011-10-14 16:26:05 +08001827}
1828
Marek Behún4ef5ca62021-11-26 14:57:10 +01001829/**
1830 * fdt_set_status_by_compatible: Set node status for first node with given
1831 * compatible
1832 *
1833 * @fdt: ptr to device tree
1834 * @compat: node's compatible string
1835 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1836 */
1837int fdt_set_status_by_compatible(void *fdt, const char *compat,
1838 enum fdt_status status)
1839{
1840 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1841
1842 if (offset < 0)
1843 return offset;
1844
1845 return fdt_set_node_status(fdt, offset, status);
1846}
1847
1848/**
1849 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1850 * path
1851 *
1852 * @fdt: ptr to device tree
1853 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1854 * @fmt, ...: path format string and arguments to pass to sprintf
1855 */
1856int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1857 ...)
1858{
1859 va_list ap;
1860 int offset;
1861
1862 va_start(ap, fmt);
1863 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1864 va_end(ap);
1865
1866 if (offset < 0)
1867 return offset;
1868
1869 return fdt_set_node_status(fdt, offset, status);
1870}
1871
Timur Tabi8ebaf202011-05-03 13:24:07 -05001872/*
1873 * Verify the physical address of device tree node for a given alias
1874 *
1875 * This function locates the device tree node of a given alias, and then
1876 * verifies that the physical address of that device matches the given
1877 * parameter. It displays a message if there is a mismatch.
1878 *
1879 * Returns 1 on success, 0 on failure
1880 */
1881int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1882{
1883 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001884 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001885 int node, len;
1886 u64 dt_addr;
1887
1888 path = fdt_getprop(fdt, anode, alias, NULL);
1889 if (!path) {
1890 /* If there's no such alias, then it's not a failure */
1891 return 1;
1892 }
1893
1894 node = fdt_path_offset(fdt, path);
1895 if (node < 0) {
1896 printf("Warning: device tree alias '%s' points to invalid "
1897 "node %s.\n", alias, path);
1898 return 0;
1899 }
1900
1901 reg = fdt_getprop(fdt, node, "reg", &len);
1902 if (!reg) {
1903 printf("Warning: device tree node '%s' has no address.\n",
1904 path);
1905 return 0;
1906 }
1907
1908 dt_addr = fdt_translate_address(fdt, node, reg);
1909 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001910 printf("Warning: U-Boot configured device %s at address %llu,\n"
1911 "but the device tree has it address %llx.\n",
1912 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001913 return 0;
1914 }
1915
1916 return 1;
1917}
1918
1919/*
1920 * Returns the base address of an SOC or PCI node
1921 */
Simon Glass293340a2017-05-18 20:09:00 -06001922u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001923{
1924 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001925 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001926
Simon Glass29f304b2017-07-04 13:31:20 -06001927 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001928
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001929 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001930}
Alexander Graf19555be2014-04-11 17:09:41 +02001931
1932/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001933 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1934 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001935 */
1936static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1937 uint64_t *val, int cells)
1938{
Bin Mengd67d7d02021-02-25 17:22:24 +08001939 const fdt32_t *prop32;
1940 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001941
1942 if ((cell_off + cells) > prop_len)
1943 return -FDT_ERR_NOSPACE;
1944
Bin Mengd67d7d02021-02-25 17:22:24 +08001945 prop32 = &prop[cell_off];
1946
1947 /*
1948 * Special handling for PCI address in PCI bus <ranges>
1949 *
1950 * PCI child address is made up of 3 cells. Advance the cell offset
1951 * by 1 so that the PCI child address can be correctly read.
1952 */
1953 if (cells == 3)
1954 cell_off += 1;
1955 prop64 = (const fdt64_t *)&prop[cell_off];
1956
Alexander Graf19555be2014-04-11 17:09:41 +02001957 switch (cells) {
1958 case 1:
1959 *val = fdt32_to_cpu(*prop32);
1960 break;
1961 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001962 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001963 *val = fdt64_to_cpu(*prop64);
1964 break;
1965 default:
1966 return -FDT_ERR_NOSPACE;
1967 }
1968
1969 return 0;
1970}
1971
1972/**
1973 * fdt_read_range - Read a node's n'th range property
1974 *
1975 * @fdt: ptr to device tree
1976 * @node: offset of node
1977 * @n: range index
1978 * @child_addr: pointer to storage for the "child address" field
1979 * @addr: pointer to storage for the CPU view translated physical start
1980 * @len: pointer to storage for the range length
1981 *
1982 * Convenience function that reads and interprets a specific range out of
1983 * a number of the "ranges" property array.
1984 */
1985int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1986 uint64_t *addr, uint64_t *len)
1987{
1988 int pnode = fdt_parent_offset(fdt, node);
1989 const fdt32_t *ranges;
1990 int pacells;
1991 int acells;
1992 int scells;
1993 int ranges_len;
1994 int cell = 0;
1995 int r = 0;
1996
1997 /*
1998 * The "ranges" property is an array of
1999 * { <child address> <parent address> <size in child address space> }
2000 *
2001 * All 3 elements can span a diffent number of cells. Fetch their size.
2002 */
2003 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
2004 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
2005 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
2006
2007 /* Now try to get the ranges property */
2008 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
2009 if (!ranges)
2010 return -FDT_ERR_NOTFOUND;
2011 ranges_len /= sizeof(uint32_t);
2012
2013 /* Jump to the n'th entry */
2014 cell = n * (pacells + acells + scells);
2015
2016 /* Read <child address> */
2017 if (child_addr) {
2018 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
2019 acells);
2020 if (r)
2021 return r;
2022 }
2023 cell += acells;
2024
2025 /* Read <parent address> */
2026 if (addr)
2027 *addr = fdt_translate_address(fdt, node, ranges + cell);
2028 cell += pacells;
2029
2030 /* Read <size in child address space> */
2031 if (len) {
2032 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
2033 if (r)
2034 return r;
2035 }
2036
2037 return 0;
2038}
Hans de Goedeea876372014-11-17 15:29:11 +01002039
2040/**
2041 * fdt_setup_simplefb_node - Fill and enable a simplefb node
2042 *
2043 * @fdt: ptr to device tree
2044 * @node: offset of the simplefb node
2045 * @base_address: framebuffer base address
2046 * @width: width in pixels
2047 * @height: height in pixels
2048 * @stride: bytes per line
2049 * @format: pixel format string
2050 *
2051 * Convenience function to fill and enable a simplefb node.
2052 */
2053int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2054 u32 height, u32 stride, const char *format)
2055{
2056 char name[32];
2057 fdt32_t cells[4];
2058 int i, addrc, sizec, ret;
2059
Simon Glassbb7c01e2017-05-18 20:09:26 -06002060 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2061 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01002062 i = 0;
2063 if (addrc == 2)
2064 cells[i++] = cpu_to_fdt32(base_address >> 32);
2065 cells[i++] = cpu_to_fdt32(base_address);
2066 if (sizec == 2)
2067 cells[i++] = 0;
2068 cells[i++] = cpu_to_fdt32(height * stride);
2069
2070 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2071 if (ret < 0)
2072 return ret;
2073
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002074 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01002075 ret = fdt_set_name(fdt, node, name);
2076 if (ret < 0)
2077 return ret;
2078
2079 ret = fdt_setprop_u32(fdt, node, "width", width);
2080 if (ret < 0)
2081 return ret;
2082
2083 ret = fdt_setprop_u32(fdt, node, "height", height);
2084 if (ret < 0)
2085 return ret;
2086
2087 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2088 if (ret < 0)
2089 return ret;
2090
2091 ret = fdt_setprop_string(fdt, node, "format", format);
2092 if (ret < 0)
2093 return ret;
2094
2095 ret = fdt_setprop_string(fdt, node, "status", "okay");
2096 if (ret < 0)
2097 return ret;
2098
2099 return 0;
2100}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002101
Devarsh Thakkar31199892024-02-22 18:38:10 +05302102#if CONFIG_IS_ENABLED(VIDEO)
2103int fdt_add_fb_mem_rsv(void *blob)
2104{
2105 struct fdt_memory mem;
2106
2107 /* nothing to do when the frame buffer is not defined */
2108 if (gd->video_bottom == gd->video_top)
2109 return 0;
2110
2111 /* reserved with no-map tag the video buffer */
2112 mem.start = gd->video_bottom;
2113 mem.end = gd->video_top - 1;
2114
2115 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2116 FDTDEC_RESERVED_MEMORY_NO_MAP);
2117}
2118#endif
2119
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002120/*
2121 * Update native-mode in display-timings from display environment variable.
2122 * The node to update are specified by path.
2123 */
2124int fdt_fixup_display(void *blob, const char *path, const char *display)
2125{
2126 int off, toff;
2127
2128 if (!display || !path)
2129 return -FDT_ERR_NOTFOUND;
2130
2131 toff = fdt_path_offset(blob, path);
2132 if (toff >= 0)
2133 toff = fdt_subnode_offset(blob, toff, "display-timings");
2134 if (toff < 0)
2135 return toff;
2136
2137 for (off = fdt_first_subnode(blob, toff);
2138 off >= 0;
2139 off = fdt_next_subnode(blob, off)) {
2140 uint32_t h = fdt_get_phandle(blob, off);
2141 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2142 fdt32_to_cpu(h));
2143 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2144 return fdt_setprop_u32(blob, toff, "native-mode", h);
2145 }
2146 return toff;
2147}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002148
2149#ifdef CONFIG_OF_LIBFDT_OVERLAY
2150/**
2151 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2152 *
2153 * @fdt: ptr to device tree
2154 * @fdto: ptr to device tree overlay
2155 *
2156 * Convenience function to apply an overlay and display helpful messages
2157 * in the case of an error
2158 */
2159int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2160{
2161 int err;
2162 bool has_symbols;
2163
2164 err = fdt_path_offset(fdt, "/__symbols__");
2165 has_symbols = err >= 0;
2166
2167 err = fdt_overlay_apply(fdt, fdto);
2168 if (err < 0) {
2169 printf("failed on fdt_overlay_apply(): %s\n",
2170 fdt_strerror(err));
2171 if (!has_symbols) {
Hugo Villeneuve90730f12023-10-26 15:54:49 -04002172 printf("base fdt does not have a /__symbols__ node\n");
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002173 printf("make sure you've compiled with -@\n");
2174 }
2175 }
2176 return err;
2177}
2178#endif
Kory Maincent623e1ac2021-05-04 19:31:21 +02002179
2180/**
2181 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2182 *
2183 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002184 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincent623e1ac2021-05-04 19:31:21 +02002185 */
2186int fdt_valid(struct fdt_header **blobp)
2187{
2188 const void *blob = *blobp;
2189 int err;
2190
2191 if (!blob) {
2192 printf("The address of the fdt is invalid (NULL).\n");
2193 return 0;
2194 }
2195
2196 err = fdt_check_header(blob);
2197 if (err == 0)
2198 return 1; /* valid */
2199
2200 if (err < 0) {
2201 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2202 /*
2203 * Be more informative on bad version.
2204 */
2205 if (err == -FDT_ERR_BADVERSION) {
2206 if (fdt_version(blob) <
2207 FDT_FIRST_SUPPORTED_VERSION) {
2208 printf(" - too old, fdt %d < %d",
2209 fdt_version(blob),
2210 FDT_FIRST_SUPPORTED_VERSION);
2211 }
2212 if (fdt_last_comp_version(blob) >
2213 FDT_LAST_SUPPORTED_VERSION) {
2214 printf(" - too new, fdt %d > %d",
2215 fdt_version(blob),
2216 FDT_LAST_SUPPORTED_VERSION);
2217 }
2218 }
2219 printf("\n");
2220 *blobp = NULL;
2221 return 0;
2222 }
2223 return 1;
2224}