blob: b7331bb76b3ef5fcf6c0a7d9c77db4befa8c2f0e [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
Tim Harvey00ae1222024-06-18 14:06:06 -07009#include <dm.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>
Tim Harvey00ae1222024-06-18 14:06:06 -070015#include <rng.h>
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040016#include <stdio_dev.h>
Tim Harvey00ae1222024-06-18 14:06:06 -070017#include <dm/device_compat.h>
Patrick Delaunaye0682262023-06-08 17:16:37 +020018#include <dm/ofnode.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040019#include <linux/ctype.h>
20#include <linux/types.h>
Masahisa Kojima4bd62062025-03-17 14:03:57 +053021#include <linux/sizes.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 Yamadac0207282014-04-18 17:40:58 +0900227 /* find or create "/chosen" node. */
228 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
229 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500230 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400231
Richard Weinberger47010172025-05-29 17:02:13 +0200232 /*
233 * Although we didn't setup an initrd, there could be a stale
234 * initrd setting from the previous boot firmware in the live
235 * device tree. So, make sure there is no setting left if we
236 * don't want an initrd.
237 */
238 if (initrd_start == initrd_end) {
239 fdt_delprop(fdt, nodeoffset, "linux,initrd-start");
240 fdt_delprop(fdt, nodeoffset, "linux,initrd-end");
241
242 return 0;
243 }
244
Kumar Gala99ebc052008-08-15 08:24:43 -0500245 total = fdt_num_mem_rsv(fdt);
246
247 /*
248 * Look for an existing entry and update it. If we don't find
249 * the entry, we will j be the next available slot.
250 */
251 for (j = 0; j < total; j++) {
252 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
253 if (addr == initrd_start) {
254 fdt_del_mem_rsv(fdt, j);
255 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400256 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500257 }
258
Grant Likelyc379a562011-03-28 09:58:55 +0000259 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500260 if (err < 0) {
261 printf("fdt_initrd: %s\n", fdt_strerror(err));
262 return err;
263 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500264
Simon Glass9fbc6322014-10-23 18:58:57 -0600265 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengd9850132013-12-14 11:47:29 +0800266
Masahiro Yamadab891a342014-04-18 17:41:04 +0900267 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
268 (uint64_t)initrd_start, is_u64);
269
Masahiro Yamada5b114892014-04-18 17:40:59 +0900270 if (err < 0) {
271 printf("WARNING: could not set linux,initrd-start %s.\n",
272 fdt_strerror(err));
273 return err;
274 }
Masahiro Yamadab891a342014-04-18 17:41:04 +0900275
276 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
277 (uint64_t)initrd_end, is_u64);
278
Masahiro Yamada5b114892014-04-18 17:40:59 +0900279 if (err < 0) {
280 printf("WARNING: could not set linux,initrd-end %s.\n",
281 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500282
Masahiro Yamada5b114892014-04-18 17:40:59 +0900283 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400284 }
285
Kumar Gala99ebc052008-08-15 08:24:43 -0500286 return 0;
287}
288
Tim Harvey00ae1222024-06-18 14:06:06 -0700289int fdt_kaslrseed(void *fdt, bool overwrite)
290{
291 int len, err, nodeoffset;
292 struct udevice *dev;
293 const u64 *orig;
294 u64 data = 0;
295
296 err = fdt_check_header(fdt);
297 if (err < 0)
298 return err;
299
300 /* find or create "/chosen" node. */
301 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
302 if (nodeoffset < 0)
303 return nodeoffset;
304
305 /* return without error if we are not overwriting and existing non-zero node */
306 orig = fdt_getprop(fdt, nodeoffset, "kaslr-seed", &len);
307 if (orig && len == sizeof(*orig))
308 data = fdt64_to_cpu(*orig);
309 if (data && !overwrite) {
310 debug("not overwriting existing kaslr-seed\n");
311 return 0;
312 }
313 err = uclass_get_device(UCLASS_RNG, 0, &dev);
314 if (err) {
315 printf("No RNG device\n");
316 return err;
317 }
318 err = dm_rng_read(dev, &data, sizeof(data));
319 if (err) {
320 dev_err(dev, "dm_rng_read failed: %d\n", err);
321 return err;
322 }
323 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", &data, sizeof(data));
324 if (err < 0)
325 printf("WARNING: could not set kaslr-seed %s.\n", fdt_strerror(err));
326
327 return err;
328}
329
Niko Mauno673db432021-02-22 19:18:51 +0000330/**
331 * board_fdt_chosen_bootargs - boards may override this function to use
332 * alternative kernel command line arguments
333 */
Dmitry Rokosovb1f13d72024-12-20 00:42:10 +0300334__weak const char *board_fdt_chosen_bootargs(const struct fdt_property *fdt_ba)
Niko Mauno673db432021-02-22 19:18:51 +0000335{
336 return env_get("bootargs");
337}
338
Masahiro Yamada451c2042014-04-18 17:41:00 +0900339int fdt_chosen(void *fdt)
Kumar Gala99ebc052008-08-15 08:24:43 -0500340{
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200341 struct abuf buf = {};
Kumar Gala99ebc052008-08-15 08:24:43 -0500342 int nodeoffset;
343 int err;
Dmitry Rokosovc652d512024-12-20 00:42:09 +0300344 const char *str; /* used to set string properties */
Kumar Gala99ebc052008-08-15 08:24:43 -0500345
346 err = fdt_check_header(fdt);
347 if (err < 0) {
348 printf("fdt_chosen: %s\n", fdt_strerror(err));
349 return err;
350 }
351
Masahiro Yamadac0207282014-04-18 17:40:58 +0900352 /* find or create "/chosen" node. */
353 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
354 if (nodeoffset < 0)
355 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400356
Tim Harveyf8f480b2024-06-18 14:06:07 -0700357 /* if DM_RNG enabled automatically inject kaslr-seed node unless:
358 * CONFIG_MEASURED_BOOT enabled: as dt modifications break measured boot
359 * CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT enabled: as that implementation does not use dm yet
360 */
361 if (IS_ENABLED(CONFIG_DM_RNG) &&
362 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
363 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
364 fdt_kaslrseed(fdt, false);
365
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200366 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
367 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
368 abuf_data(&buf), abuf_size(&buf));
369 abuf_uninit(&buf);
370 if (err < 0) {
371 printf("WARNING: could not set rng-seed %s.\n",
372 fdt_strerror(err));
373 return err;
374 }
375 }
376
Dmitry Rokosovb1f13d72024-12-20 00:42:10 +0300377 str = board_fdt_chosen_bootargs(fdt_get_property(fdt, nodeoffset,
378 "bootargs", NULL));
Niko Mauno673db432021-02-22 19:18:51 +0000379
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900380 if (str) {
381 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
382 strlen(str) + 1);
383 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900384 printf("WARNING: could not set bootargs %s.\n",
385 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900386 return err;
387 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400388 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600389
Francesco Dolcini05cc8272022-05-19 16:22:26 +0200390 /* add u-boot version */
391 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
392 strlen(PLAIN_VERSION) + 1);
393 if (err < 0) {
394 printf("WARNING: could not set u-boot,version %s.\n",
395 fdt_strerror(err));
396 return err;
397 }
398
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900399 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400400}
401
Kumar Gala7e64cf82007-11-03 19:46:28 -0500402void do_fixup_by_path(void *fdt, const char *path, const char *prop,
403 const void *val, int len, int create)
404{
405#if defined(DEBUG)
406 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600407 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500408 for (i = 0; i < len; i++)
409 debug(" %.2x", *(u8*)(val+i));
410 debug("\n");
411#endif
412 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
413 if (rc)
414 printf("Unable to update property %s:%s, err=%s\n",
415 path, prop, fdt_strerror(rc));
416}
417
418void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
419 u32 val, int create)
420{
Kim Phillips6542c072013-01-16 14:00:11 +0000421 fdt32_t tmp = cpu_to_fdt32(val);
422 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500423}
424
Kumar Gala7590a192007-11-21 13:30:15 -0600425void do_fixup_by_prop(void *fdt,
426 const char *pname, const void *pval, int plen,
427 const char *prop, const void *val, int len,
428 int create)
429{
430 int off;
431#if defined(DEBUG)
432 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600433 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600434 for (i = 0; i < len; i++)
435 debug(" %.2x", *(u8*)(val+i));
436 debug("\n");
437#endif
438 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
439 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000440 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600441 fdt_setprop(fdt, off, prop, val, len);
442 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
443 }
444}
445
446void do_fixup_by_prop_u32(void *fdt,
447 const char *pname, const void *pval, int plen,
448 const char *prop, u32 val, int create)
449{
Kim Phillips6542c072013-01-16 14:00:11 +0000450 fdt32_t tmp = cpu_to_fdt32(val);
451 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600452}
453
454void do_fixup_by_compat(void *fdt, const char *compat,
455 const char *prop, const void *val, int len, int create)
456{
457 int off = -1;
458#if defined(DEBUG)
459 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600460 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600461 for (i = 0; i < len; i++)
462 debug(" %.2x", *(u8*)(val+i));
463 debug("\n");
464#endif
Marek Behún5d6b4482022-01-20 01:04:42 +0100465 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips6542c072013-01-16 14:00:11 +0000466 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600467 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala7590a192007-11-21 13:30:15 -0600468}
469
470void do_fixup_by_compat_u32(void *fdt, const char *compat,
471 const char *prop, u32 val, int create)
472{
Kim Phillips6542c072013-01-16 14:00:11 +0000473 fdt32_t tmp = cpu_to_fdt32(val);
474 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600475}
476
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900477/*
478 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
479 */
Simon Glass0058e112014-10-23 18:58:55 -0600480static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
481 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900482{
483 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100484 int address_cells = fdt_address_cells(fdt, 0);
485 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900486 char *p = buf;
487
488 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100489 if (address_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500490 put_unaligned_be64(address[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900491 else
492 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100493 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900494
Hans de Goede4c80fb02014-11-28 14:23:51 +0100495 if (size_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500496 put_unaligned_be64(size[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900497 else
498 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100499 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900500 }
501
502 return p - (char *)buf;
503}
504
Masahisa Kojima4bd62062025-03-17 14:03:57 +0530505#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Ramon Fried9cab1832018-08-14 00:35:42 +0300506#if CONFIG_NR_DRAM_BANKS > 4
507#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
508#else
Kim Phillips6542c072013-01-16 14:00:11 +0000509#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300510#endif
Michal Simek96283732021-08-10 09:21:54 +0200511
512/**
513 * fdt_fixup_memory_banks - Update DT memory node
514 * @blob: Pointer to DT blob
515 * @start: Pointer to memory start addresses array
516 * @size: Pointer to memory sizes array
517 * @banks: Number of memory banks
518 *
519 * Return: 0 on success, negative value on failure
520 *
521 * Based on the passed number of banks and arrays, the function is able to
522 * update existing DT memory nodes to match run time detected/changed memory
523 * configuration. Implementation is handling one specific case with only one
524 * memory node where multiple tuples could be added/updated.
525 * The case where multiple memory nodes with a single tuple (base, size) are
526 * used, this function is only updating the first memory node without removing
527 * others.
528 */
John Rigbyb4ae9422010-10-13 13:57:33 -0600529int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
530{
531 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100532 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000533 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600534
Kim Phillips6542c072013-01-16 14:00:11 +0000535 if (banks > MEMORY_BANKS_MAX) {
536 printf("%s: num banks %d exceeds hardcoded limit %d."
537 " Recompile with higher MEMORY_BANKS_MAX?\n",
538 __FUNCTION__, banks, MEMORY_BANKS_MAX);
539 return -1;
540 }
541
Kumar Gala0db72ed2007-11-26 14:57:45 -0600542 err = fdt_check_header(blob);
543 if (err < 0) {
544 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
545 return err;
546 }
547
Masahiro Yamadac0207282014-04-18 17:40:58 +0900548 /* find or create "/memory" node. */
549 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
550 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800551 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900552
Kumar Gala0db72ed2007-11-26 14:57:45 -0600553 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
554 sizeof("memory"));
555 if (err < 0) {
556 printf("WARNING: could not set %s %s.\n", "device_type",
557 fdt_strerror(err));
558 return err;
559 }
560
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100561 for (i = 0; i < banks; i++) {
562 if (start[i] == 0 && size[i] == 0)
563 break;
564 }
565
566 banks = i;
567
Andre Przywara3d2d4412015-06-21 00:29:54 +0100568 if (!banks)
569 return 0;
570
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900571 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600572
573 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
574 if (err < 0) {
575 printf("WARNING: could not set %s %s.\n",
576 "reg", fdt_strerror(err));
577 return err;
578 }
579 return 0;
580}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200581
582int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
583{
584 int err, nodeoffset;
585 int len;
586 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
587
588 if (areas > 8) {
589 printf("%s: num areas %d exceeds hardcoded limit %d\n",
590 __func__, areas, 8);
591 return -1;
592 }
593
594 err = fdt_check_header(blob);
595 if (err < 0) {
596 printf("%s: %s\n", __func__, fdt_strerror(err));
597 return err;
598 }
599
600 /* find or create "/memory" node. */
601 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
602 if (nodeoffset < 0)
603 return nodeoffset;
604
605 len = fdt_pack_reg(blob, tmp, start, size, areas);
606
607 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
608 if (err < 0) {
609 printf("WARNING: could not set %s %s.\n",
610 "reg", fdt_strerror(err));
611 return err;
612 }
613
614 return 0;
615}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900616#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600617
John Rigbyb4ae9422010-10-13 13:57:33 -0600618int fdt_fixup_memory(void *blob, u64 start, u64 size)
619{
620 return fdt_fixup_memory_banks(blob, &start, &size, 1);
621}
622
Kumar Galafabda922008-08-19 15:41:18 -0500623void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600624{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530625 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800626 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000627 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600628 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100629 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800630 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530631#ifdef FDT_SEQ_MACADDR_FROM_ENV
632 int nodeoff;
633 const struct fdt_property *fdt_prop;
634#endif
Kumar Gala22699102007-11-21 11:11:03 -0600635
Lev Iserovichcde77612016-01-07 18:04:16 -0500636 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500637 return;
638
Lev Iserovichcde77612016-01-07 18:04:16 -0500639 /* Cycle through all aliases */
640 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800641 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500642
Lev Iserovichcde77612016-01-07 18:04:16 -0500643 /* FDT might have been edited, recompute the offset */
644 offset = fdt_first_property_offset(fdt,
645 fdt_path_offset(fdt, "/aliases"));
646 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530647 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500648 offset = fdt_next_property_offset(fdt, offset);
649
650 if (offset < 0)
651 break;
652
Bin Mengbfb93cc2015-11-03 04:24:41 -0800653 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200654 if (!strncmp(name, "ethernet", 8)) {
655 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530656 if (!strcmp(name, "ethernet")
657#ifdef FDT_SEQ_MACADDR_FROM_ENV
658 || !strcmp(name, "ethernet0")
659#endif
660 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200661 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530662#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200663 else
664 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530665#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800666 if (i != -1) {
667 if (i == 0)
668 strcpy(mac, "ethaddr");
669 else
670 sprintf(mac, "eth%daddr", i);
671 } else {
672 continue;
673 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530674#ifdef FDT_SEQ_MACADDR_FROM_ENV
675 nodeoff = fdt_path_offset(fdt, path);
676 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
677 NULL);
678 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
679 continue;
680 i++;
681#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600682 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800683 if (!tmp)
684 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500685
Bin Mengbfb93cc2015-11-03 04:24:41 -0800686 for (j = 0; j < 6; j++) {
687 mac_addr[j] = tmp ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600688 hextoul(tmp, &end) : 0;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800689 if (tmp)
690 tmp = (*end) ? end + 1 : end;
691 }
Kumar Galafabda922008-08-19 15:41:18 -0500692
Bin Mengbfb93cc2015-11-03 04:24:41 -0800693 do_fixup_by_path(fdt, path, "mac-address",
694 &mac_addr, 6, 0);
695 do_fixup_by_path(fdt, path, "local-mac-address",
696 &mac_addr, 6, 1);
697 }
Kumar Gala22699102007-11-21 11:11:03 -0600698 }
699}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300700
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100701int fdt_record_loadable(void *blob, u32 index, const char *name,
702 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simek0e4f43d2021-05-27 11:40:09 +0200703 const char *type, const char *os, const char *arch)
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100704{
705 int err, node;
706
707 err = fdt_check_header(blob);
708 if (err < 0) {
709 printf("%s: %s\n", __func__, fdt_strerror(err));
710 return err;
711 }
712
713 /* find or create "/fit-images" node */
714 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
715 if (node < 0)
716 return node;
717
718 /* find or create "/fit-images/<name>" node */
719 node = fdt_find_or_add_subnode(blob, node, name);
720 if (node < 0)
721 return node;
722
Michal Simekd31b40f2020-09-03 12:44:51 +0200723 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100724 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200725 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100726 fdt_setprop_u32(blob, node, "size", size);
727 if (type)
728 fdt_setprop_string(blob, node, "type", type);
729 if (os)
730 fdt_setprop_string(blob, node, "os", os);
Michal Simek0e4f43d2021-05-27 11:40:09 +0200731 if (arch)
732 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100733
734 return node;
735}
736
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200737int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500738{
739 int i;
740 uint64_t addr, size;
741 int total, ret;
742 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200743 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500744
745 if (!blob)
746 return 0;
747
748 total = fdt_num_mem_rsv(blob);
749 for (i = 0; i < total; i++) {
750 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000751 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500752 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200753 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500754 break;
755 }
756 }
757
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100758 /*
759 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200760 * plus the size needed for 5 fdt_add_mem_rsv, one
761 * for the fdt itself and 4 for a possible initrd
762 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100763 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500764 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200765 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500766
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200767 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500768 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000769 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
770 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500771
772 /* Change the fdt header to reflect the correct size */
773 fdt_set_totalsize(blob, actualsize);
774
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200775 if (fdt_memrsv) {
776 /* Add the new reservation */
777 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
778 if (ret < 0)
779 return ret;
780 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500781
782 return actualsize;
783}
Kumar Galae0475cd2008-10-22 23:33:56 -0500784
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100785/**
786 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
787 *
788 * @blob: ptr to device tree
789 */
790int fdt_delete_disabled_nodes(void *blob)
791{
792 while (1) {
793 int ret, offset;
794
795 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
796 "disabled", 9);
797 if (offset < 0)
798 break;
799
800 ret = fdt_del_node(blob, offset);
801 if (ret < 0)
802 return ret;
803 }
804
805 return 0;
806}
807
Kumar Galae0475cd2008-10-22 23:33:56 -0500808#ifdef CONFIG_PCI
Tom Rini56af6592022-11-16 13:10:33 -0500809#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500810
811#define FDT_PCI_PREFETCH (0x40000000)
812#define FDT_PCI_MEM32 (0x02000000)
813#define FDT_PCI_IO (0x01000000)
814#define FDT_PCI_MEM64 (0x03000000)
815
816int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
817
818 int addrcell, sizecell, len, r;
819 u32 *dma_range;
820 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Rini56af6592022-11-16 13:10:33 -0500821 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Galae0475cd2008-10-22 23:33:56 -0500822
823 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
824 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
825
826 dma_range = &dma_ranges[0];
827 for (r = 0; r < hose->region_count; r++) {
828 u64 bus_start, phys_start, size;
829
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600830 /* skip if !PCI_REGION_SYS_MEMORY */
831 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500832 continue;
833
834 bus_start = (u64)hose->regions[r].bus_start;
835 phys_start = (u64)hose->regions[r].phys_start;
836 size = (u64)hose->regions[r].size;
837
838 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500839 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200840 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500841 else
Marek Vasut22203f52019-07-09 02:49:29 +0200842 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500843 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200844 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500845#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200846 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500847#else
848 dma_range[1] = 0;
849#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200850 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500851
852 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200853 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
854 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500855 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200856 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500857 }
858
859 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200860 dma_range[3 + addrcell + 0] =
861 cpu_to_fdt32(size >> 32);
862 dma_range[3 + addrcell + 1] =
863 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500864 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200865 dma_range[3 + addrcell + 0] =
866 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500867 }
868
869 dma_range += (3 + addrcell + sizecell);
870 }
871
872 len = dma_range - &dma_ranges[0];
873 if (len)
874 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
875
876 return 0;
877}
878#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200879
Matthew McClintockacda3a42010-10-13 13:39:26 +0200880int fdt_increase_size(void *fdt, int add_len)
881{
882 int newlen;
883
884 newlen = fdt_totalsize(fdt) + add_len;
885
886 /* Open in place with a new len */
887 return fdt_open_into(fdt, fdt, newlen);
888}
889
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100890#ifdef CONFIG_FDT_FIXUP_PARTITIONS
891#include <jffs2/load_kernel.h>
892#include <mtd_node.h>
893
Michal Simekc2ed0072018-07-31 14:31:04 +0200894static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100895{
896 int off, ndepth;
897 int ret;
898
899 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
900 (off >= 0) && (ndepth > 0);
901 off = fdt_next_node(blob, off, &ndepth)) {
902 if (ndepth == 1) {
903 debug("delete %s: offset: %x\n",
904 fdt_get_name(blob, off, 0), off);
905 ret = fdt_del_node((void *)blob, off);
906 if (ret < 0) {
907 printf("Can't delete node: %s\n",
908 fdt_strerror(ret));
909 return ret;
910 } else {
911 ndepth = 0;
912 off = parent_offset;
913 }
914 }
915 }
916 return 0;
917}
918
Michal Simekc2ed0072018-07-31 14:31:04 +0200919static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100920{
921 const void *prop;
922 int ndepth = 0;
923 int off;
924 int ret;
925
926 off = fdt_next_node(blob, parent_offset, &ndepth);
927 if (off > 0 && ndepth == 1) {
928 prop = fdt_getprop(blob, off, "label", NULL);
929 if (prop == NULL) {
930 /*
931 * Could not find label property, nand {}; node?
932 * Check subnode, delete partitions there if any.
933 */
934 return fdt_del_partitions(blob, off);
935 } else {
936 ret = fdt_del_subnodes(blob, parent_offset);
937 if (ret < 0) {
938 printf("Can't remove subnodes: %s\n",
939 fdt_strerror(ret));
940 return ret;
941 }
942 }
943 }
944 return 0;
945}
946
Masahiro Yamada65669602020-07-15 19:35:47 +0900947static int fdt_node_set_part_info(void *blob, int parent_offset,
948 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100949{
950 struct list_head *pentry;
951 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100952 int off, ndepth = 0;
953 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300954 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100955 char buf[64];
956
957 ret = fdt_del_partitions(blob, parent_offset);
958 if (ret < 0)
959 return ret;
960
961 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300962 * Check if size/address is 1 or 2 cells.
963 * We assume #address-cells and #size-cells have same value.
964 */
965 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
966 0, "#size-cells", 1);
967
968 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100969 * Check if it is nand {}; subnode, adjust
970 * the offset in this case
971 */
972 off = fdt_next_node(blob, parent_offset, &ndepth);
973 if (off > 0 && ndepth == 1)
974 parent_offset = off;
975
976 part_num = 0;
977 list_for_each_prev(pentry, &dev->parts) {
978 int newoff;
979
980 part = list_entry(pentry, struct part_info, link);
981
Scott Woodefe7f302013-10-15 17:41:27 -0500982 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100983 part_num, part->name, part->size,
984 part->offset, part->mask_flags);
985
Scott Woodefe7f302013-10-15 17:41:27 -0500986 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100987add_sub:
988 ret = fdt_add_subnode(blob, parent_offset, buf);
989 if (ret == -FDT_ERR_NOSPACE) {
990 ret = fdt_increase_size(blob, 512);
991 if (!ret)
992 goto add_sub;
993 else
994 goto err_size;
995 } else if (ret < 0) {
996 printf("Can't add partition node: %s\n",
997 fdt_strerror(ret));
998 return ret;
999 }
1000 newoff = ret;
1001
1002 /* Check MTD_WRITEABLE_CMD flag */
1003 if (part->mask_flags & 1) {
1004add_ro:
1005 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
1006 if (ret == -FDT_ERR_NOSPACE) {
1007 ret = fdt_increase_size(blob, 512);
1008 if (!ret)
1009 goto add_ro;
1010 else
1011 goto err_size;
1012 } else if (ret < 0)
1013 goto err_prop;
1014 }
1015
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001016add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +03001017 if (sizecell == 2) {
1018 ret = fdt_setprop_u64(blob, newoff,
1019 "reg", part->offset);
1020 if (!ret)
1021 ret = fdt_appendprop_u64(blob, newoff,
1022 "reg", part->size);
1023 } else {
1024 ret = fdt_setprop_u32(blob, newoff,
1025 "reg", part->offset);
1026 if (!ret)
1027 ret = fdt_appendprop_u32(blob, newoff,
1028 "reg", part->size);
1029 }
1030
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001031 if (ret == -FDT_ERR_NOSPACE) {
1032 ret = fdt_increase_size(blob, 512);
1033 if (!ret)
1034 goto add_reg;
1035 else
1036 goto err_size;
1037 } else if (ret < 0)
1038 goto err_prop;
1039
1040add_label:
1041 ret = fdt_setprop_string(blob, newoff, "label", part->name);
1042 if (ret == -FDT_ERR_NOSPACE) {
1043 ret = fdt_increase_size(blob, 512);
1044 if (!ret)
1045 goto add_label;
1046 else
1047 goto err_size;
1048 } else if (ret < 0)
1049 goto err_prop;
1050
1051 part_num++;
1052 }
1053 return 0;
1054err_size:
1055 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1056 return ret;
1057err_prop:
1058 printf("Can't add property: %s\n", fdt_strerror(ret));
1059 return ret;
1060}
1061
1062/*
1063 * Update partitions in nor/nand nodes using info from
1064 * mtdparts environment variable. The nodes to update are
1065 * specified by node_info structure which contains mtd device
1066 * type and compatible string: E. g. the board code in
1067 * ft_board_setup() could use:
1068 *
1069 * struct node_info nodes[] = {
1070 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1071 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1072 * };
1073 *
1074 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1075 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001076void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1077 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001078{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001079 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001080 int i, idx;
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001081 int noff, parts;
Masahiro Yamada030348f2020-07-17 10:46:18 +09001082 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001083
1084 for (i = 0; i < node_info_size; i++) {
1085 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +09001086
Marek Behún5d6b4482022-01-20 01:04:42 +01001087 fdt_for_each_node_by_compatible(noff, blob, -1,
1088 node_info[i].compat) {
Masahiro Yamada21644292020-07-17 10:46:19 +09001089 const char *prop;
1090
1091 prop = fdt_getprop(blob, noff, "status", NULL);
1092 if (prop && !strcmp(prop, "disabled"))
1093 continue;
1094
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001095 debug("%s: %s, mtd dev type %d\n",
1096 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001097 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +09001098
1099 if (!inited) {
1100 if (mtdparts_init() != 0)
1101 return;
1102 inited = true;
1103 }
1104
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001105 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001106 if (dev) {
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001107 parts = fdt_subnode_offset(blob, noff,
1108 "partitions");
1109 if (parts < 0)
1110 parts = noff;
1111
1112 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001113 return; /* return on error */
1114 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001115 }
1116 }
1117}
1118#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001119
Patrick Delaunay019127b2023-06-08 17:16:38 +02001120int fdt_copy_fixed_partitions(void *blob)
1121{
1122 ofnode node, subnode;
1123 int off, suboff, res;
1124 char path[256];
1125 int address_cells, size_cells;
1126 u8 i, j, child_count;
1127
1128 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1129 while (ofnode_valid(node)) {
1130 /* copy the U-Boot fixed partition */
1131 address_cells = ofnode_read_simple_addr_cells(node);
1132 size_cells = ofnode_read_simple_size_cells(node);
1133
1134 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1135 if (res)
1136 return res;
1137
1138 off = fdt_path_offset(blob, path);
1139 if (off < 0)
1140 return -ENODEV;
1141
1142 off = fdt_find_or_add_subnode(blob, off, "partitions");
1143 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1144 if (res)
1145 return res;
1146
1147 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1148 if (res)
1149 return res;
1150
1151 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1152 if (res)
1153 return res;
1154
1155 /*
1156 * parse partition in reverse order as fdt_find_or_add_subnode() only
1157 * insert the new node after the parent's properties
1158 */
1159 child_count = ofnode_get_child_count(node);
1160 for (i = child_count; i > 0 ; i--) {
1161 subnode = ofnode_first_subnode(node);
1162 if (!ofnode_valid(subnode))
1163 break;
1164
1165 for (j = 0; (j < i - 1); j++)
1166 subnode = ofnode_next_subnode(subnode);
1167
1168 if (!ofnode_valid(subnode))
1169 break;
1170
1171 const u32 *reg;
1172 int len;
1173
1174 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1175 res = fdt_setprop_string(blob, suboff, "label",
1176 ofnode_read_string(subnode, "label"));
1177 if (res)
1178 return res;
1179
1180 reg = ofnode_get_property(subnode, "reg", &len);
1181 res = fdt_setprop(blob, suboff, "reg", reg, len);
1182 if (res)
1183 return res;
1184 }
1185
1186 /* go to next fixed-partitions node */
1187 node = ofnode_by_compatible(node, "fixed-partitions");
1188 }
1189
1190 return 0;
1191}
1192
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001193void fdt_del_node_and_alias(void *blob, const char *alias)
1194{
1195 int off = fdt_path_offset(blob, alias);
1196
1197 if (off < 0)
1198 return;
1199
1200 fdt_del_node(blob, off);
1201
1202 off = fdt_path_offset(blob, "/aliases");
1203 fdt_delprop(blob, off, alias);
1204}
Kumar Galab87e7932010-06-16 14:27:38 -05001205
Kumar Galab87e7932010-06-16 14:27:38 -05001206/* Max address size we deal with */
1207#define OF_MAX_ADDR_CELLS 4
Dario Binacchif8fc7032021-05-01 17:05:26 +02001208#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1209 (ns) > 0)
Kumar Galab87e7932010-06-16 14:27:38 -05001210
1211/* Debug utility */
1212#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +00001213static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001214{
1215 printf("%s", s);
1216 while(na--)
1217 printf(" %08x", *(addr++));
1218 printf("\n");
1219}
1220#else
Kim Phillips6542c072013-01-16 14:00:11 +00001221static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001222#endif
1223
Paul Burtondbef2f02016-05-17 07:43:24 +01001224/**
1225 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001226 * @name: A string used to identify this bus in debug output.
1227 * @addresses: The name of the DT property from which addresses are
1228 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001229 * @match: Return non-zero if the node whose parent is at
1230 * parentoffset in the FDT blob corresponds to a bus
1231 * of this type, otherwise return zero. If NULL a match
1232 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001233 * @count_cells:Count how many cells (be32 values) a node whose parent
1234 * is at parentoffset in the FDT blob will require to
1235 * represent its address (written to *addrc) & size
1236 * (written to *sizec).
1237 * @map: Map the address addr from the address space of this
1238 * bus to that of its parent, making use of the ranges
1239 * read from DT to an array at range. na and ns are the
1240 * number of cells (be32 values) used to hold and address
1241 * or size, respectively, for this bus. pna is the number
1242 * of cells used to hold an address for the parent bus.
1243 * Returns the address in the address space of the parent
1244 * bus.
1245 * @translate: Update the value of the address cells at addr within an
1246 * FDT by adding offset to it. na specifies the number of
1247 * cells used to hold the address being translated. Returns
1248 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001249 *
1250 * Each bus type will include a struct of_bus in the of_busses array,
1251 * providing implementations of some or all of the functions used to
1252 * match the bus & handle address translation for its children.
1253 */
Kumar Galab87e7932010-06-16 14:27:38 -05001254struct of_bus {
1255 const char *name;
1256 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001257 int (*match)(const void *blob, int parentoffset);
1258 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001259 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001260 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001261 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001262 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001263};
1264
1265/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001266void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001267 int *addrc, int *sizec)
1268{
Kim Phillips6542c072013-01-16 14:00:11 +00001269 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001270
Simon Glass9fbc6322014-10-23 18:58:57 -06001271 if (addrc)
1272 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001273
1274 if (sizec) {
1275 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1276 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001277 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001278 else
1279 *sizec = 1;
1280 }
Kumar Galab87e7932010-06-16 14:27:38 -05001281}
1282
Kim Phillips6542c072013-01-16 14:00:11 +00001283static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001284 int na, int ns, int pna)
1285{
1286 u64 cp, s, da;
1287
Simon Glassbb7c01e2017-05-18 20:09:26 -06001288 cp = fdt_read_number(range, na);
1289 s = fdt_read_number(range + na + pna, ns);
1290 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001291
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301292 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001293
1294 if (da < cp || da >= (cp + s))
1295 return OF_BAD_ADDR;
1296 return da - cp;
1297}
1298
Kim Phillips6542c072013-01-16 14:00:11 +00001299static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001300{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001301 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001302 memset(addr, 0, na * 4);
1303 a += offset;
1304 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001305 addr[na - 2] = cpu_to_fdt32(a >> 32);
1306 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001307
1308 return 0;
1309}
1310
Paul Burtondbef2f02016-05-17 07:43:24 +01001311#ifdef CONFIG_OF_ISA_BUS
1312
1313/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001314static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001315{
1316 const char *name;
1317
1318 name = fdt_get_name(blob, parentoffset, NULL);
1319 if (!name)
1320 return 0;
1321
1322 return !strcmp(name, "isa");
1323}
1324
Stephen Warrenc99581b2016-08-05 09:47:50 -06001325static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001326 int *addrc, int *sizec)
1327{
1328 if (addrc)
1329 *addrc = 2;
1330 if (sizec)
1331 *sizec = 1;
1332}
1333
1334static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1335 int na, int ns, int pna)
1336{
1337 u64 cp, s, da;
1338
1339 /* Check address type match */
1340 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1341 return OF_BAD_ADDR;
1342
Simon Glassbb7c01e2017-05-18 20:09:26 -06001343 cp = fdt_read_number(range + 1, na - 1);
1344 s = fdt_read_number(range + na + pna, ns);
1345 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001346
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301347 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001348
1349 if (da < cp || da >= (cp + s))
1350 return OF_BAD_ADDR;
1351 return da - cp;
1352}
1353
1354static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1355{
1356 return of_bus_default_translate(addr + 1, offset, na - 1);
1357}
1358
1359#endif /* CONFIG_OF_ISA_BUS */
1360
Kumar Galab87e7932010-06-16 14:27:38 -05001361/* Array of bus specific translators */
1362static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001363#ifdef CONFIG_OF_ISA_BUS
1364 /* ISA */
1365 {
1366 .name = "isa",
1367 .addresses = "reg",
1368 .match = of_bus_isa_match,
1369 .count_cells = of_bus_isa_count_cells,
1370 .map = of_bus_isa_map,
1371 .translate = of_bus_isa_translate,
1372 },
1373#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001374 /* Default */
1375 {
1376 .name = "default",
1377 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001378 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001379 .map = of_bus_default_map,
1380 .translate = of_bus_default_translate,
1381 },
1382};
1383
Stephen Warrenc99581b2016-08-05 09:47:50 -06001384static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001385{
1386 struct of_bus *bus;
1387
1388 if (ARRAY_SIZE(of_busses) == 1)
1389 return of_busses;
1390
1391 for (bus = of_busses; bus; bus++) {
1392 if (!bus->match || bus->match(blob, parentoffset))
1393 return bus;
1394 }
1395
1396 /*
1397 * We should always have matched the default bus at least, since
1398 * it has a NULL match field. If we didn't then it somehow isn't
1399 * in the of_busses array or something equally catastrophic has
1400 * gone wrong.
1401 */
1402 assert(0);
1403 return NULL;
1404}
1405
Stephen Warrenc99581b2016-08-05 09:47:50 -06001406static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001407 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001408 int na, int ns, int pna, const char *rprop)
1409{
Kim Phillips6542c072013-01-16 14:00:11 +00001410 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001411 int rlen;
1412 int rone;
1413 u64 offset = OF_BAD_ADDR;
1414
1415 /* Normally, an absence of a "ranges" property means we are
1416 * crossing a non-translatable boundary, and thus the addresses
1417 * below the current not cannot be converted to CPU physical ones.
1418 * Unfortunately, while this is very clear in the spec, it's not
1419 * what Apple understood, and they do have things like /uni-n or
1420 * /ht nodes with no "ranges" property and a lot of perfectly
1421 * useable mapped devices below them. Thus we treat the absence of
1422 * "ranges" as equivalent to an empty "ranges" property which means
1423 * a 1:1 translation at that level. It's up to the caller not to try
1424 * to translate addresses that aren't supposed to be translated in
1425 * the first place. --BenH.
1426 */
Kim Phillips6542c072013-01-16 14:00:11 +00001427 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001428 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001429 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001430 memset(addr, 0, pna * 4);
1431 debug("OF: no ranges, 1:1 translation\n");
1432 goto finish;
1433 }
1434
1435 debug("OF: walking ranges...\n");
1436
1437 /* Now walk through the ranges */
1438 rlen /= 4;
1439 rone = na + pna + ns;
1440 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1441 offset = bus->map(addr, ranges, na, ns, pna);
1442 if (offset != OF_BAD_ADDR)
1443 break;
1444 }
1445 if (offset == OF_BAD_ADDR) {
1446 debug("OF: not found !\n");
1447 return 1;
1448 }
1449 memcpy(addr, ranges + na, 4 * pna);
1450
1451 finish:
1452 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001453 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001454
1455 /* Translate it into parent bus space */
1456 return pbus->translate(addr, offset, pna);
1457}
1458
1459/*
1460 * Translate an address from the device-tree into a CPU physical address,
1461 * this walks up the tree and applies the various bus mappings on the
1462 * way.
1463 *
1464 * Note: We consider that crossing any level with #size-cells == 0 to mean
1465 * that translation is impossible (that is we are not dealing with a value
1466 * that can be mapped to a cpu physical address). This is not really specified
1467 * that way, but this is traditionally the way IBM at least do things
1468 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001469static u64 __of_translate_address(const void *blob, int node_offset,
1470 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001471{
1472 int parent;
1473 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001474 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001475 int na, ns, pna, pns;
1476 u64 result = OF_BAD_ADDR;
1477
1478 debug("OF: ** translation for device %s **\n",
1479 fdt_get_name(blob, node_offset, NULL));
1480
1481 /* Get parent & match bus type */
1482 parent = fdt_parent_offset(blob, node_offset);
1483 if (parent < 0)
1484 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001485 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001486
1487 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001488 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001489 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001490 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1491 fdt_get_name(blob, node_offset, NULL));
1492 goto bail;
1493 }
1494 memcpy(addr, in_addr, na * 4);
1495
1496 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1497 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1498 of_dump_addr("OF: translating address:", addr, na);
1499
1500 /* Translate */
1501 for (;;) {
1502 /* Switch to parent bus */
1503 node_offset = parent;
1504 parent = fdt_parent_offset(blob, node_offset);
1505
1506 /* If root, we have finished */
1507 if (parent < 0) {
1508 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001509 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001510 break;
1511 }
1512
1513 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001514 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001515 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001516 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001517 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1518 fdt_get_name(blob, node_offset, NULL));
1519 break;
1520 }
1521
1522 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1523 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1524
1525 /* Apply bus translation */
1526 if (of_translate_one(blob, node_offset, bus, pbus,
1527 addr, na, ns, pna, rprop))
1528 break;
1529
1530 /* Complete the move up one level */
1531 na = pna;
1532 ns = pns;
1533 bus = pbus;
1534
1535 of_dump_addr("OF: one level translation:", addr, na);
1536 }
1537 bail:
1538
1539 return result;
1540}
1541
Stephen Warrenc99581b2016-08-05 09:47:50 -06001542u64 fdt_translate_address(const void *blob, int node_offset,
1543 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001544{
1545 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1546}
Kumar Gala800d1d12010-07-04 12:48:21 -05001547
Fabien Dessenne22236e02019-05-31 15:11:30 +02001548u64 fdt_translate_dma_address(const void *blob, int node_offset,
1549 const fdt32_t *in_addr)
1550{
1551 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1552}
1553
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001554int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1555 dma_addr_t *bus, u64 *size)
1556{
1557 bool found_dma_ranges = false;
1558 struct of_bus *bus_node;
1559 const fdt32_t *ranges;
1560 int na, ns, pna, pns;
1561 int parent = node;
1562 int ret = 0;
1563 int len;
1564
1565 /* Find the closest dma-ranges property */
1566 while (parent >= 0) {
1567 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1568
1569 /* Ignore empty ranges, they imply no translation required */
1570 if (ranges && len > 0)
1571 break;
1572
1573 /* Once we find 'dma-ranges', then a missing one is an error */
1574 if (found_dma_ranges && !ranges) {
1575 ret = -EINVAL;
1576 goto out;
1577 }
1578
1579 if (ranges)
1580 found_dma_ranges = true;
1581
1582 parent = fdt_parent_offset(blob, parent);
1583 }
1584
1585 if (!ranges || parent < 0) {
1586 debug("no dma-ranges found for node %s\n",
1587 fdt_get_name(blob, node, NULL));
1588 ret = -ENOENT;
1589 goto out;
1590 }
1591
1592 /* switch to that node */
1593 node = parent;
1594 parent = fdt_parent_offset(blob, node);
1595 if (parent < 0) {
Vagrant Cascadiand00c5c72021-12-21 13:06:58 -08001596 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001597 ret = -EINVAL;
1598 goto out;
1599 }
1600
1601 /* Get the address sizes both for the bus and its parent */
1602 bus_node = of_match_bus(blob, node);
1603 bus_node->count_cells(blob, node, &na, &ns);
1604 if (!OF_CHECK_COUNTS(na, ns)) {
1605 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1606 fdt_get_name(blob, node, NULL));
1607 return -EINVAL;
1608 goto out;
1609 }
1610
1611 bus_node = of_match_bus(blob, parent);
1612 bus_node->count_cells(blob, parent, &pna, &pns);
1613 if (!OF_CHECK_COUNTS(pna, pns)) {
1614 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1615 fdt_get_name(blob, parent, NULL));
1616 return -EINVAL;
1617 goto out;
1618 }
1619
1620 *bus = fdt_read_number(ranges, na);
1621 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1622 *size = fdt_read_number(ranges + na + pna, ns);
1623out:
1624 return ret;
1625}
1626
Kumar Gala800d1d12010-07-04 12:48:21 -05001627/**
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001628 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala800d1d12010-07-04 12:48:21 -05001629 * who's reg property matches a physical cpu address
1630 *
1631 * @blob: ptr to device tree
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001632 * @compat: compatible string to match
Kumar Gala800d1d12010-07-04 12:48:21 -05001633 * @compat_off: property name
1634 *
1635 */
1636int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1637 phys_addr_t compat_off)
1638{
Marek Behún5d6b4482022-01-20 01:04:42 +01001639 int len, off;
1640
1641 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips6542c072013-01-16 14:00:11 +00001642 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún5d6b4482022-01-20 01:04:42 +01001643 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1644 return off;
Kumar Gala800d1d12010-07-04 12:48:21 -05001645 }
1646
1647 return -FDT_ERR_NOTFOUND;
1648}
1649
Marek Behún4ef5ca62021-11-26 14:57:10 +01001650static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1651{
1652 char path[512];
1653 int len;
1654
1655 len = vsnprintf(path, sizeof(path), fmt, ap);
1656 if (len < 0 || len + 1 > sizeof(path))
1657 return -FDT_ERR_NOSPACE;
1658
1659 return fdt_path_offset(blob, path);
1660}
1661
1662/**
1663 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1664 *
1665 * @blob: ptr to device tree
1666 * @fmt: path format
1667 * @ap: vsnprintf arguments
1668 */
1669int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1670{
1671 va_list ap;
1672 int res;
1673
1674 va_start(ap, fmt);
1675 res = vnode_offset_by_pathf(blob, fmt, ap);
1676 va_end(ap);
1677
1678 return res;
1679}
1680
Gerald Van Barenaf737882011-07-14 21:40:10 -04001681/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001682 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001683 *
1684 * @fdt: ptr to device tree
1685 * @nodeoffset: node to update
1686 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001687 */
1688int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001689{
1690 int ret;
1691
1692#ifdef DEBUG
1693 int off = fdt_node_offset_by_phandle(fdt, phandle);
1694
1695 if ((off >= 0) && (off != nodeoffset)) {
1696 char buf[64];
1697
1698 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1699 printf("Trying to update node %s with phandle %u ",
1700 buf, phandle);
1701
1702 fdt_get_path(fdt, off, buf, sizeof(buf));
1703 printf("that already exists in node %s.\n", buf);
1704 return -FDT_ERR_BADPHANDLE;
1705 }
1706#endif
1707
1708 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barenaf737882011-07-14 21:40:10 -04001709
1710 return ret;
1711}
1712
Kumar Galad5fc2582011-08-01 00:25:20 -05001713/*
Marek Behúnae0ef952021-11-26 14:57:09 +01001714 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Galad5fc2582011-08-01 00:25:20 -05001715 *
1716 * @fdt: ptr to device tree
1717 * @nodeoffset: node to update
1718 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001719unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001720{
1721 /* see if there is a phandle already */
Marek Behún3577eba32021-11-26 14:57:07 +01001722 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Galad5fc2582011-08-01 00:25:20 -05001723
1724 /* if we got 0, means no phandle so create one */
1725 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001726 int ret;
1727
Marek Behún3577eba32021-11-26 14:57:07 +01001728 ret = fdt_generate_phandle(fdt, &phandle);
1729 if (ret < 0) {
1730 printf("Can't generate phandle: %s\n",
1731 fdt_strerror(ret));
1732 return 0;
1733 }
1734
Timur Tabi0f46c802011-09-20 18:24:34 -05001735 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1736 if (ret < 0) {
1737 printf("Can't set phandle %u: %s\n", phandle,
1738 fdt_strerror(ret));
1739 return 0;
1740 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001741 }
1742
1743 return phandle;
1744}
1745
Marek Behún4ef5ca62021-11-26 14:57:10 +01001746/**
1747 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1748 * given compatible
1749 *
1750 * @fdt: ptr to device tree
1751 * @compat: node's compatible string
1752 */
1753unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1754{
1755 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1756
1757 if (offset < 0) {
1758 printf("Can't find node with compatible \"%s\": %s\n", compat,
1759 fdt_strerror(offset));
1760 return 0;
1761 }
1762
1763 return fdt_create_phandle(fdt, offset);
1764}
1765
1766/**
1767 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1768 * sprintf-formatted path
1769 *
1770 * @fdt: ptr to device tree
1771 * @fmt, ...: path format string and arguments to pass to sprintf
1772 */
1773unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1774{
1775 va_list ap;
1776 int offset;
1777
1778 va_start(ap, fmt);
1779 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1780 va_end(ap);
1781
1782 if (offset < 0) {
1783 printf("Can't find node by given path: %s\n",
1784 fdt_strerror(offset));
1785 return 0;
1786 }
1787
1788 return fdt_create_phandle(fdt, offset);
1789}
1790
Shengzhou Liua7be8022011-10-14 16:26:05 +08001791/*
1792 * fdt_set_node_status: Set status for the given node
1793 *
1794 * @fdt: ptr to device tree
1795 * @nodeoffset: node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001796 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001797 */
Marek Behúnf872e832021-11-26 14:57:08 +01001798int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001799{
Shengzhou Liua7be8022011-10-14 16:26:05 +08001800 int ret = 0;
1801
1802 if (nodeoffset < 0)
1803 return nodeoffset;
1804
1805 switch (status) {
1806 case FDT_STATUS_OKAY:
1807 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1808 break;
1809 case FDT_STATUS_DISABLED:
1810 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1811 break;
1812 case FDT_STATUS_FAIL:
1813 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1814 break;
Shengzhou Liua7be8022011-10-14 16:26:05 +08001815 default:
1816 printf("Invalid fdt status: %x\n", status);
1817 ret = -1;
1818 break;
1819 }
1820
1821 return ret;
1822}
1823
1824/*
1825 * fdt_set_status_by_alias: Set status for the given node given an alias
1826 *
1827 * @fdt: ptr to device tree
1828 * @alias: alias of node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001829 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001830 */
1831int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behúnf872e832021-11-26 14:57:08 +01001832 enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001833{
1834 int offset = fdt_path_offset(fdt, alias);
1835
Marek Behúnf872e832021-11-26 14:57:08 +01001836 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liua7be8022011-10-14 16:26:05 +08001837}
1838
Marek Behún4ef5ca62021-11-26 14:57:10 +01001839/**
1840 * fdt_set_status_by_compatible: Set node status for first node with given
1841 * compatible
1842 *
1843 * @fdt: ptr to device tree
1844 * @compat: node's compatible string
1845 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1846 */
1847int fdt_set_status_by_compatible(void *fdt, const char *compat,
1848 enum fdt_status status)
1849{
1850 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1851
1852 if (offset < 0)
1853 return offset;
1854
1855 return fdt_set_node_status(fdt, offset, status);
1856}
1857
1858/**
1859 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1860 * path
1861 *
1862 * @fdt: ptr to device tree
1863 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1864 * @fmt, ...: path format string and arguments to pass to sprintf
1865 */
1866int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1867 ...)
1868{
1869 va_list ap;
1870 int offset;
1871
1872 va_start(ap, fmt);
1873 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1874 va_end(ap);
1875
1876 if (offset < 0)
1877 return offset;
1878
1879 return fdt_set_node_status(fdt, offset, status);
1880}
1881
Timur Tabi8ebaf202011-05-03 13:24:07 -05001882/*
1883 * Verify the physical address of device tree node for a given alias
1884 *
1885 * This function locates the device tree node of a given alias, and then
1886 * verifies that the physical address of that device matches the given
1887 * parameter. It displays a message if there is a mismatch.
1888 *
1889 * Returns 1 on success, 0 on failure
1890 */
1891int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1892{
1893 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001894 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001895 int node, len;
1896 u64 dt_addr;
1897
1898 path = fdt_getprop(fdt, anode, alias, NULL);
1899 if (!path) {
1900 /* If there's no such alias, then it's not a failure */
1901 return 1;
1902 }
1903
1904 node = fdt_path_offset(fdt, path);
1905 if (node < 0) {
1906 printf("Warning: device tree alias '%s' points to invalid "
1907 "node %s.\n", alias, path);
1908 return 0;
1909 }
1910
1911 reg = fdt_getprop(fdt, node, "reg", &len);
1912 if (!reg) {
1913 printf("Warning: device tree node '%s' has no address.\n",
1914 path);
1915 return 0;
1916 }
1917
1918 dt_addr = fdt_translate_address(fdt, node, reg);
1919 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001920 printf("Warning: U-Boot configured device %s at address %llu,\n"
1921 "but the device tree has it address %llx.\n",
1922 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001923 return 0;
1924 }
1925
1926 return 1;
1927}
1928
1929/*
1930 * Returns the base address of an SOC or PCI node
1931 */
Simon Glass293340a2017-05-18 20:09:00 -06001932u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001933{
1934 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001935 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001936
Simon Glass29f304b2017-07-04 13:31:20 -06001937 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001938
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001939 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001940}
Alexander Graf19555be2014-04-11 17:09:41 +02001941
1942/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001943 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1944 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001945 */
1946static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1947 uint64_t *val, int cells)
1948{
Bin Mengd67d7d02021-02-25 17:22:24 +08001949 const fdt32_t *prop32;
1950 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001951
1952 if ((cell_off + cells) > prop_len)
1953 return -FDT_ERR_NOSPACE;
1954
Bin Mengd67d7d02021-02-25 17:22:24 +08001955 prop32 = &prop[cell_off];
1956
1957 /*
1958 * Special handling for PCI address in PCI bus <ranges>
1959 *
1960 * PCI child address is made up of 3 cells. Advance the cell offset
1961 * by 1 so that the PCI child address can be correctly read.
1962 */
1963 if (cells == 3)
1964 cell_off += 1;
1965 prop64 = (const fdt64_t *)&prop[cell_off];
1966
Alexander Graf19555be2014-04-11 17:09:41 +02001967 switch (cells) {
1968 case 1:
1969 *val = fdt32_to_cpu(*prop32);
1970 break;
1971 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001972 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001973 *val = fdt64_to_cpu(*prop64);
1974 break;
1975 default:
1976 return -FDT_ERR_NOSPACE;
1977 }
1978
1979 return 0;
1980}
1981
1982/**
1983 * fdt_read_range - Read a node's n'th range property
1984 *
1985 * @fdt: ptr to device tree
1986 * @node: offset of node
1987 * @n: range index
1988 * @child_addr: pointer to storage for the "child address" field
1989 * @addr: pointer to storage for the CPU view translated physical start
1990 * @len: pointer to storage for the range length
1991 *
1992 * Convenience function that reads and interprets a specific range out of
1993 * a number of the "ranges" property array.
1994 */
1995int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1996 uint64_t *addr, uint64_t *len)
1997{
1998 int pnode = fdt_parent_offset(fdt, node);
1999 const fdt32_t *ranges;
2000 int pacells;
2001 int acells;
2002 int scells;
2003 int ranges_len;
2004 int cell = 0;
2005 int r = 0;
2006
2007 /*
2008 * The "ranges" property is an array of
2009 * { <child address> <parent address> <size in child address space> }
2010 *
2011 * All 3 elements can span a diffent number of cells. Fetch their size.
2012 */
2013 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
2014 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
2015 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
2016
2017 /* Now try to get the ranges property */
2018 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
2019 if (!ranges)
2020 return -FDT_ERR_NOTFOUND;
2021 ranges_len /= sizeof(uint32_t);
2022
2023 /* Jump to the n'th entry */
2024 cell = n * (pacells + acells + scells);
2025
2026 /* Read <child address> */
2027 if (child_addr) {
2028 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
2029 acells);
2030 if (r)
2031 return r;
2032 }
2033 cell += acells;
2034
2035 /* Read <parent address> */
2036 if (addr)
2037 *addr = fdt_translate_address(fdt, node, ranges + cell);
2038 cell += pacells;
2039
2040 /* Read <size in child address space> */
2041 if (len) {
2042 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
2043 if (r)
2044 return r;
2045 }
2046
2047 return 0;
2048}
Hans de Goedeea876372014-11-17 15:29:11 +01002049
2050/**
2051 * fdt_setup_simplefb_node - Fill and enable a simplefb node
2052 *
2053 * @fdt: ptr to device tree
2054 * @node: offset of the simplefb node
2055 * @base_address: framebuffer base address
2056 * @width: width in pixels
2057 * @height: height in pixels
2058 * @stride: bytes per line
2059 * @format: pixel format string
2060 *
2061 * Convenience function to fill and enable a simplefb node.
2062 */
2063int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2064 u32 height, u32 stride, const char *format)
2065{
2066 char name[32];
2067 fdt32_t cells[4];
2068 int i, addrc, sizec, ret;
2069
Simon Glassbb7c01e2017-05-18 20:09:26 -06002070 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2071 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01002072 i = 0;
2073 if (addrc == 2)
2074 cells[i++] = cpu_to_fdt32(base_address >> 32);
2075 cells[i++] = cpu_to_fdt32(base_address);
2076 if (sizec == 2)
2077 cells[i++] = 0;
2078 cells[i++] = cpu_to_fdt32(height * stride);
2079
2080 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2081 if (ret < 0)
2082 return ret;
2083
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002084 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01002085 ret = fdt_set_name(fdt, node, name);
2086 if (ret < 0)
2087 return ret;
2088
2089 ret = fdt_setprop_u32(fdt, node, "width", width);
2090 if (ret < 0)
2091 return ret;
2092
2093 ret = fdt_setprop_u32(fdt, node, "height", height);
2094 if (ret < 0)
2095 return ret;
2096
2097 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2098 if (ret < 0)
2099 return ret;
2100
2101 ret = fdt_setprop_string(fdt, node, "format", format);
2102 if (ret < 0)
2103 return ret;
2104
2105 ret = fdt_setprop_string(fdt, node, "status", "okay");
2106 if (ret < 0)
2107 return ret;
2108
2109 return 0;
2110}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002111
Devarsh Thakkar31199892024-02-22 18:38:10 +05302112#if CONFIG_IS_ENABLED(VIDEO)
2113int fdt_add_fb_mem_rsv(void *blob)
2114{
2115 struct fdt_memory mem;
2116
2117 /* nothing to do when the frame buffer is not defined */
2118 if (gd->video_bottom == gd->video_top)
2119 return 0;
2120
2121 /* reserved with no-map tag the video buffer */
2122 mem.start = gd->video_bottom;
2123 mem.end = gd->video_top - 1;
2124
2125 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2126 FDTDEC_RESERVED_MEMORY_NO_MAP);
2127}
2128#endif
2129
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002130/*
2131 * Update native-mode in display-timings from display environment variable.
2132 * The node to update are specified by path.
2133 */
2134int fdt_fixup_display(void *blob, const char *path, const char *display)
2135{
2136 int off, toff;
2137
2138 if (!display || !path)
2139 return -FDT_ERR_NOTFOUND;
2140
2141 toff = fdt_path_offset(blob, path);
2142 if (toff >= 0)
2143 toff = fdt_subnode_offset(blob, toff, "display-timings");
2144 if (toff < 0)
2145 return toff;
2146
2147 for (off = fdt_first_subnode(blob, toff);
2148 off >= 0;
2149 off = fdt_next_subnode(blob, off)) {
2150 uint32_t h = fdt_get_phandle(blob, off);
2151 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2152 fdt32_to_cpu(h));
2153 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2154 return fdt_setprop_u32(blob, toff, "native-mode", h);
2155 }
2156 return toff;
2157}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002158
2159#ifdef CONFIG_OF_LIBFDT_OVERLAY
2160/**
2161 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2162 *
2163 * @fdt: ptr to device tree
2164 * @fdto: ptr to device tree overlay
2165 *
2166 * Convenience function to apply an overlay and display helpful messages
2167 * in the case of an error
2168 */
2169int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2170{
2171 int err;
2172 bool has_symbols;
2173
2174 err = fdt_path_offset(fdt, "/__symbols__");
2175 has_symbols = err >= 0;
2176
2177 err = fdt_overlay_apply(fdt, fdto);
2178 if (err < 0) {
2179 printf("failed on fdt_overlay_apply(): %s\n",
2180 fdt_strerror(err));
2181 if (!has_symbols) {
Hugo Villeneuve90730f12023-10-26 15:54:49 -04002182 printf("base fdt does not have a /__symbols__ node\n");
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002183 printf("make sure you've compiled with -@\n");
2184 }
2185 }
2186 return err;
2187}
2188#endif
Kory Maincent623e1ac2021-05-04 19:31:21 +02002189
2190/**
2191 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2192 *
2193 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002194 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincent623e1ac2021-05-04 19:31:21 +02002195 */
2196int fdt_valid(struct fdt_header **blobp)
2197{
2198 const void *blob = *blobp;
2199 int err;
2200
2201 if (!blob) {
2202 printf("The address of the fdt is invalid (NULL).\n");
2203 return 0;
2204 }
2205
2206 err = fdt_check_header(blob);
2207 if (err == 0)
2208 return 1; /* valid */
2209
2210 if (err < 0) {
2211 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2212 /*
2213 * Be more informative on bad version.
2214 */
2215 if (err == -FDT_ERR_BADVERSION) {
2216 if (fdt_version(blob) <
2217 FDT_FIRST_SUPPORTED_VERSION) {
2218 printf(" - too old, fdt %d < %d",
2219 fdt_version(blob),
2220 FDT_FIRST_SUPPORTED_VERSION);
2221 }
2222 if (fdt_last_comp_version(blob) >
2223 FDT_LAST_SUPPORTED_VERSION) {
2224 printf(" - too new, fdt %d > %d",
2225 fdt_version(blob),
2226 FDT_LAST_SUPPORTED_VERSION);
2227 }
2228 }
2229 printf("\n");
2230 *blobp = NULL;
2231 return 0;
2232 }
2233 return 1;
2234}
Masahisa Kojima4bd62062025-03-17 14:03:57 +05302235
2236int fdt_fixup_pmem_region(void *fdt, u64 pmem_start, u64 pmem_size)
2237{
2238 char node_name[32];
2239 int nodeoffset, len;
2240 int err;
2241 u8 tmp[4 * 16]; /* Up to 64-bit address + 64-bit size */
2242
2243 if (!IS_ALIGNED(pmem_start, SZ_2M) ||
2244 !IS_ALIGNED(pmem_start + pmem_size, SZ_2M)) {
2245 printf("Start and end address must be 2MiB aligned\n");
2246 return -1;
2247 }
2248
2249 snprintf(node_name, sizeof(node_name), "pmem@%llx", pmem_start);
2250 nodeoffset = fdt_find_or_add_subnode(fdt, 0, node_name);
2251 if (nodeoffset < 0)
2252 return nodeoffset;
2253
2254 err = fdt_setprop_string(fdt, nodeoffset, "compatible", "pmem-region");
2255 if (err)
2256 return err;
2257 err = fdt_setprop_empty(fdt, nodeoffset, "volatile");
2258 if (err)
2259 return err;
2260
2261 len = fdt_pack_reg(fdt, tmp, &pmem_start, &pmem_size, 1);
2262 err = fdt_setprop(fdt, nodeoffset, "reg", tmp, len);
2263 if (err < 0) {
2264 printf("WARNING: could not set pmem %s %s.\n", "reg",
2265 fdt_strerror(err));
2266 return err;
2267 }
2268
2269 return 0;
2270}