blob: b1b2679dea0c75604785144d562fb811681697ab [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
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200348 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
349 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
350 abuf_data(&buf), abuf_size(&buf));
351 abuf_uninit(&buf);
352 if (err < 0) {
353 printf("WARNING: could not set rng-seed %s.\n",
354 fdt_strerror(err));
355 return err;
356 }
357 }
358
Niko Mauno673db432021-02-22 19:18:51 +0000359 str = board_fdt_chosen_bootargs();
360
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900361 if (str) {
362 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
363 strlen(str) + 1);
364 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900365 printf("WARNING: could not set bootargs %s.\n",
366 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900367 return err;
368 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400369 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600370
Francesco Dolcini05cc8272022-05-19 16:22:26 +0200371 /* add u-boot version */
372 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
373 strlen(PLAIN_VERSION) + 1);
374 if (err < 0) {
375 printf("WARNING: could not set u-boot,version %s.\n",
376 fdt_strerror(err));
377 return err;
378 }
379
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900380 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400381}
382
Kumar Gala7e64cf82007-11-03 19:46:28 -0500383void do_fixup_by_path(void *fdt, const char *path, const char *prop,
384 const void *val, int len, int create)
385{
386#if defined(DEBUG)
387 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600388 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500389 for (i = 0; i < len; i++)
390 debug(" %.2x", *(u8*)(val+i));
391 debug("\n");
392#endif
393 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
394 if (rc)
395 printf("Unable to update property %s:%s, err=%s\n",
396 path, prop, fdt_strerror(rc));
397}
398
399void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
400 u32 val, int create)
401{
Kim Phillips6542c072013-01-16 14:00:11 +0000402 fdt32_t tmp = cpu_to_fdt32(val);
403 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500404}
405
Kumar Gala7590a192007-11-21 13:30:15 -0600406void do_fixup_by_prop(void *fdt,
407 const char *pname, const void *pval, int plen,
408 const char *prop, const void *val, int len,
409 int create)
410{
411 int off;
412#if defined(DEBUG)
413 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600414 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600415 for (i = 0; i < len; i++)
416 debug(" %.2x", *(u8*)(val+i));
417 debug("\n");
418#endif
419 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
420 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000421 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600422 fdt_setprop(fdt, off, prop, val, len);
423 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
424 }
425}
426
427void do_fixup_by_prop_u32(void *fdt,
428 const char *pname, const void *pval, int plen,
429 const char *prop, u32 val, int create)
430{
Kim Phillips6542c072013-01-16 14:00:11 +0000431 fdt32_t tmp = cpu_to_fdt32(val);
432 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600433}
434
435void do_fixup_by_compat(void *fdt, const char *compat,
436 const char *prop, const void *val, int len, int create)
437{
438 int off = -1;
439#if defined(DEBUG)
440 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600441 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600442 for (i = 0; i < len; i++)
443 debug(" %.2x", *(u8*)(val+i));
444 debug("\n");
445#endif
Marek Behún5d6b4482022-01-20 01:04:42 +0100446 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips6542c072013-01-16 14:00:11 +0000447 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600448 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala7590a192007-11-21 13:30:15 -0600449}
450
451void do_fixup_by_compat_u32(void *fdt, const char *compat,
452 const char *prop, u32 val, int create)
453{
Kim Phillips6542c072013-01-16 14:00:11 +0000454 fdt32_t tmp = cpu_to_fdt32(val);
455 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600456}
457
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900458#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900459/*
460 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
461 */
Simon Glass0058e112014-10-23 18:58:55 -0600462static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
463 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900464{
465 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100466 int address_cells = fdt_address_cells(fdt, 0);
467 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900468 char *p = buf;
469
470 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100471 if (address_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500472 put_unaligned_be64(address[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900473 else
474 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100475 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900476
Hans de Goede4c80fb02014-11-28 14:23:51 +0100477 if (size_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500478 put_unaligned_be64(size[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900479 else
480 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100481 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900482 }
483
484 return p - (char *)buf;
485}
486
Ramon Fried9cab1832018-08-14 00:35:42 +0300487#if CONFIG_NR_DRAM_BANKS > 4
488#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
489#else
Kim Phillips6542c072013-01-16 14:00:11 +0000490#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300491#endif
Michal Simek96283732021-08-10 09:21:54 +0200492
493/**
494 * fdt_fixup_memory_banks - Update DT memory node
495 * @blob: Pointer to DT blob
496 * @start: Pointer to memory start addresses array
497 * @size: Pointer to memory sizes array
498 * @banks: Number of memory banks
499 *
500 * Return: 0 on success, negative value on failure
501 *
502 * Based on the passed number of banks and arrays, the function is able to
503 * update existing DT memory nodes to match run time detected/changed memory
504 * configuration. Implementation is handling one specific case with only one
505 * memory node where multiple tuples could be added/updated.
506 * The case where multiple memory nodes with a single tuple (base, size) are
507 * used, this function is only updating the first memory node without removing
508 * others.
509 */
John Rigbyb4ae9422010-10-13 13:57:33 -0600510int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
511{
512 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100513 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000514 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600515
Kim Phillips6542c072013-01-16 14:00:11 +0000516 if (banks > MEMORY_BANKS_MAX) {
517 printf("%s: num banks %d exceeds hardcoded limit %d."
518 " Recompile with higher MEMORY_BANKS_MAX?\n",
519 __FUNCTION__, banks, MEMORY_BANKS_MAX);
520 return -1;
521 }
522
Kumar Gala0db72ed2007-11-26 14:57:45 -0600523 err = fdt_check_header(blob);
524 if (err < 0) {
525 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
526 return err;
527 }
528
Masahiro Yamadac0207282014-04-18 17:40:58 +0900529 /* find or create "/memory" node. */
530 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
531 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800532 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900533
Kumar Gala0db72ed2007-11-26 14:57:45 -0600534 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
535 sizeof("memory"));
536 if (err < 0) {
537 printf("WARNING: could not set %s %s.\n", "device_type",
538 fdt_strerror(err));
539 return err;
540 }
541
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100542 for (i = 0; i < banks; i++) {
543 if (start[i] == 0 && size[i] == 0)
544 break;
545 }
546
547 banks = i;
548
Andre Przywara3d2d4412015-06-21 00:29:54 +0100549 if (!banks)
550 return 0;
551
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900552 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600553
554 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
555 if (err < 0) {
556 printf("WARNING: could not set %s %s.\n",
557 "reg", fdt_strerror(err));
558 return err;
559 }
560 return 0;
561}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200562
563int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
564{
565 int err, nodeoffset;
566 int len;
567 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
568
569 if (areas > 8) {
570 printf("%s: num areas %d exceeds hardcoded limit %d\n",
571 __func__, areas, 8);
572 return -1;
573 }
574
575 err = fdt_check_header(blob);
576 if (err < 0) {
577 printf("%s: %s\n", __func__, fdt_strerror(err));
578 return err;
579 }
580
581 /* find or create "/memory" node. */
582 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
583 if (nodeoffset < 0)
584 return nodeoffset;
585
586 len = fdt_pack_reg(blob, tmp, start, size, areas);
587
588 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
589 if (err < 0) {
590 printf("WARNING: could not set %s %s.\n",
591 "reg", fdt_strerror(err));
592 return err;
593 }
594
595 return 0;
596}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900597#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600598
John Rigbyb4ae9422010-10-13 13:57:33 -0600599int fdt_fixup_memory(void *blob, u64 start, u64 size)
600{
601 return fdt_fixup_memory_banks(blob, &start, &size, 1);
602}
603
Kumar Galafabda922008-08-19 15:41:18 -0500604void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600605{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530606 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800607 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000608 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600609 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100610 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800611 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530612#ifdef FDT_SEQ_MACADDR_FROM_ENV
613 int nodeoff;
614 const struct fdt_property *fdt_prop;
615#endif
Kumar Gala22699102007-11-21 11:11:03 -0600616
Lev Iserovichcde77612016-01-07 18:04:16 -0500617 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500618 return;
619
Lev Iserovichcde77612016-01-07 18:04:16 -0500620 /* Cycle through all aliases */
621 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800622 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500623
Lev Iserovichcde77612016-01-07 18:04:16 -0500624 /* FDT might have been edited, recompute the offset */
625 offset = fdt_first_property_offset(fdt,
626 fdt_path_offset(fdt, "/aliases"));
627 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530628 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500629 offset = fdt_next_property_offset(fdt, offset);
630
631 if (offset < 0)
632 break;
633
Bin Mengbfb93cc2015-11-03 04:24:41 -0800634 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200635 if (!strncmp(name, "ethernet", 8)) {
636 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530637 if (!strcmp(name, "ethernet")
638#ifdef FDT_SEQ_MACADDR_FROM_ENV
639 || !strcmp(name, "ethernet0")
640#endif
641 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200642 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530643#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200644 else
645 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530646#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800647 if (i != -1) {
648 if (i == 0)
649 strcpy(mac, "ethaddr");
650 else
651 sprintf(mac, "eth%daddr", i);
652 } else {
653 continue;
654 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530655#ifdef FDT_SEQ_MACADDR_FROM_ENV
656 nodeoff = fdt_path_offset(fdt, path);
657 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
658 NULL);
659 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
660 continue;
661 i++;
662#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600663 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800664 if (!tmp)
665 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500666
Bin Mengbfb93cc2015-11-03 04:24:41 -0800667 for (j = 0; j < 6; j++) {
668 mac_addr[j] = tmp ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600669 hextoul(tmp, &end) : 0;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800670 if (tmp)
671 tmp = (*end) ? end + 1 : end;
672 }
Kumar Galafabda922008-08-19 15:41:18 -0500673
Bin Mengbfb93cc2015-11-03 04:24:41 -0800674 do_fixup_by_path(fdt, path, "mac-address",
675 &mac_addr, 6, 0);
676 do_fixup_by_path(fdt, path, "local-mac-address",
677 &mac_addr, 6, 1);
678 }
Kumar Gala22699102007-11-21 11:11:03 -0600679 }
680}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300681
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100682int fdt_record_loadable(void *blob, u32 index, const char *name,
683 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simek0e4f43d2021-05-27 11:40:09 +0200684 const char *type, const char *os, const char *arch)
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100685{
686 int err, node;
687
688 err = fdt_check_header(blob);
689 if (err < 0) {
690 printf("%s: %s\n", __func__, fdt_strerror(err));
691 return err;
692 }
693
694 /* find or create "/fit-images" node */
695 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
696 if (node < 0)
697 return node;
698
699 /* find or create "/fit-images/<name>" node */
700 node = fdt_find_or_add_subnode(blob, node, name);
701 if (node < 0)
702 return node;
703
Michal Simekd31b40f2020-09-03 12:44:51 +0200704 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100705 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200706 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100707 fdt_setprop_u32(blob, node, "size", size);
708 if (type)
709 fdt_setprop_string(blob, node, "type", type);
710 if (os)
711 fdt_setprop_string(blob, node, "os", os);
Michal Simek0e4f43d2021-05-27 11:40:09 +0200712 if (arch)
713 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100714
715 return node;
716}
717
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200718int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500719{
720 int i;
721 uint64_t addr, size;
722 int total, ret;
723 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200724 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500725
726 if (!blob)
727 return 0;
728
729 total = fdt_num_mem_rsv(blob);
730 for (i = 0; i < total; i++) {
731 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000732 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500733 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200734 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500735 break;
736 }
737 }
738
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100739 /*
740 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200741 * plus the size needed for 5 fdt_add_mem_rsv, one
742 * for the fdt itself and 4 for a possible initrd
743 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100744 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500745 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200746 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500747
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200748 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500749 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000750 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
751 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500752
753 /* Change the fdt header to reflect the correct size */
754 fdt_set_totalsize(blob, actualsize);
755
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200756 if (fdt_memrsv) {
757 /* Add the new reservation */
758 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
759 if (ret < 0)
760 return ret;
761 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500762
763 return actualsize;
764}
Kumar Galae0475cd2008-10-22 23:33:56 -0500765
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100766/**
767 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
768 *
769 * @blob: ptr to device tree
770 */
771int fdt_delete_disabled_nodes(void *blob)
772{
773 while (1) {
774 int ret, offset;
775
776 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
777 "disabled", 9);
778 if (offset < 0)
779 break;
780
781 ret = fdt_del_node(blob, offset);
782 if (ret < 0)
783 return ret;
784 }
785
786 return 0;
787}
788
Kumar Galae0475cd2008-10-22 23:33:56 -0500789#ifdef CONFIG_PCI
Tom Rini56af6592022-11-16 13:10:33 -0500790#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500791
792#define FDT_PCI_PREFETCH (0x40000000)
793#define FDT_PCI_MEM32 (0x02000000)
794#define FDT_PCI_IO (0x01000000)
795#define FDT_PCI_MEM64 (0x03000000)
796
797int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
798
799 int addrcell, sizecell, len, r;
800 u32 *dma_range;
801 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Rini56af6592022-11-16 13:10:33 -0500802 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Galae0475cd2008-10-22 23:33:56 -0500803
804 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
805 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
806
807 dma_range = &dma_ranges[0];
808 for (r = 0; r < hose->region_count; r++) {
809 u64 bus_start, phys_start, size;
810
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600811 /* skip if !PCI_REGION_SYS_MEMORY */
812 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500813 continue;
814
815 bus_start = (u64)hose->regions[r].bus_start;
816 phys_start = (u64)hose->regions[r].phys_start;
817 size = (u64)hose->regions[r].size;
818
819 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500820 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200821 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500822 else
Marek Vasut22203f52019-07-09 02:49:29 +0200823 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500824 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200825 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500826#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200827 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500828#else
829 dma_range[1] = 0;
830#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200831 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500832
833 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200834 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
835 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500836 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200837 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500838 }
839
840 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200841 dma_range[3 + addrcell + 0] =
842 cpu_to_fdt32(size >> 32);
843 dma_range[3 + addrcell + 1] =
844 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500845 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200846 dma_range[3 + addrcell + 0] =
847 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500848 }
849
850 dma_range += (3 + addrcell + sizecell);
851 }
852
853 len = dma_range - &dma_ranges[0];
854 if (len)
855 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
856
857 return 0;
858}
859#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200860
Matthew McClintockacda3a42010-10-13 13:39:26 +0200861int fdt_increase_size(void *fdt, int add_len)
862{
863 int newlen;
864
865 newlen = fdt_totalsize(fdt) + add_len;
866
867 /* Open in place with a new len */
868 return fdt_open_into(fdt, fdt, newlen);
869}
870
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100871#ifdef CONFIG_FDT_FIXUP_PARTITIONS
872#include <jffs2/load_kernel.h>
873#include <mtd_node.h>
874
Michal Simekc2ed0072018-07-31 14:31:04 +0200875static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100876{
877 int off, ndepth;
878 int ret;
879
880 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
881 (off >= 0) && (ndepth > 0);
882 off = fdt_next_node(blob, off, &ndepth)) {
883 if (ndepth == 1) {
884 debug("delete %s: offset: %x\n",
885 fdt_get_name(blob, off, 0), off);
886 ret = fdt_del_node((void *)blob, off);
887 if (ret < 0) {
888 printf("Can't delete node: %s\n",
889 fdt_strerror(ret));
890 return ret;
891 } else {
892 ndepth = 0;
893 off = parent_offset;
894 }
895 }
896 }
897 return 0;
898}
899
Michal Simekc2ed0072018-07-31 14:31:04 +0200900static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100901{
902 const void *prop;
903 int ndepth = 0;
904 int off;
905 int ret;
906
907 off = fdt_next_node(blob, parent_offset, &ndepth);
908 if (off > 0 && ndepth == 1) {
909 prop = fdt_getprop(blob, off, "label", NULL);
910 if (prop == NULL) {
911 /*
912 * Could not find label property, nand {}; node?
913 * Check subnode, delete partitions there if any.
914 */
915 return fdt_del_partitions(blob, off);
916 } else {
917 ret = fdt_del_subnodes(blob, parent_offset);
918 if (ret < 0) {
919 printf("Can't remove subnodes: %s\n",
920 fdt_strerror(ret));
921 return ret;
922 }
923 }
924 }
925 return 0;
926}
927
Masahiro Yamada65669602020-07-15 19:35:47 +0900928static int fdt_node_set_part_info(void *blob, int parent_offset,
929 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100930{
931 struct list_head *pentry;
932 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100933 int off, ndepth = 0;
934 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300935 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100936 char buf[64];
937
938 ret = fdt_del_partitions(blob, parent_offset);
939 if (ret < 0)
940 return ret;
941
942 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300943 * Check if size/address is 1 or 2 cells.
944 * We assume #address-cells and #size-cells have same value.
945 */
946 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
947 0, "#size-cells", 1);
948
949 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100950 * Check if it is nand {}; subnode, adjust
951 * the offset in this case
952 */
953 off = fdt_next_node(blob, parent_offset, &ndepth);
954 if (off > 0 && ndepth == 1)
955 parent_offset = off;
956
957 part_num = 0;
958 list_for_each_prev(pentry, &dev->parts) {
959 int newoff;
960
961 part = list_entry(pentry, struct part_info, link);
962
Scott Woodefe7f302013-10-15 17:41:27 -0500963 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100964 part_num, part->name, part->size,
965 part->offset, part->mask_flags);
966
Scott Woodefe7f302013-10-15 17:41:27 -0500967 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100968add_sub:
969 ret = fdt_add_subnode(blob, parent_offset, buf);
970 if (ret == -FDT_ERR_NOSPACE) {
971 ret = fdt_increase_size(blob, 512);
972 if (!ret)
973 goto add_sub;
974 else
975 goto err_size;
976 } else if (ret < 0) {
977 printf("Can't add partition node: %s\n",
978 fdt_strerror(ret));
979 return ret;
980 }
981 newoff = ret;
982
983 /* Check MTD_WRITEABLE_CMD flag */
984 if (part->mask_flags & 1) {
985add_ro:
986 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
987 if (ret == -FDT_ERR_NOSPACE) {
988 ret = fdt_increase_size(blob, 512);
989 if (!ret)
990 goto add_ro;
991 else
992 goto err_size;
993 } else if (ret < 0)
994 goto err_prop;
995 }
996
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100997add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300998 if (sizecell == 2) {
999 ret = fdt_setprop_u64(blob, newoff,
1000 "reg", part->offset);
1001 if (!ret)
1002 ret = fdt_appendprop_u64(blob, newoff,
1003 "reg", part->size);
1004 } else {
1005 ret = fdt_setprop_u32(blob, newoff,
1006 "reg", part->offset);
1007 if (!ret)
1008 ret = fdt_appendprop_u32(blob, newoff,
1009 "reg", part->size);
1010 }
1011
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001012 if (ret == -FDT_ERR_NOSPACE) {
1013 ret = fdt_increase_size(blob, 512);
1014 if (!ret)
1015 goto add_reg;
1016 else
1017 goto err_size;
1018 } else if (ret < 0)
1019 goto err_prop;
1020
1021add_label:
1022 ret = fdt_setprop_string(blob, newoff, "label", part->name);
1023 if (ret == -FDT_ERR_NOSPACE) {
1024 ret = fdt_increase_size(blob, 512);
1025 if (!ret)
1026 goto add_label;
1027 else
1028 goto err_size;
1029 } else if (ret < 0)
1030 goto err_prop;
1031
1032 part_num++;
1033 }
1034 return 0;
1035err_size:
1036 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1037 return ret;
1038err_prop:
1039 printf("Can't add property: %s\n", fdt_strerror(ret));
1040 return ret;
1041}
1042
1043/*
1044 * Update partitions in nor/nand nodes using info from
1045 * mtdparts environment variable. The nodes to update are
1046 * specified by node_info structure which contains mtd device
1047 * type and compatible string: E. g. the board code in
1048 * ft_board_setup() could use:
1049 *
1050 * struct node_info nodes[] = {
1051 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1052 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1053 * };
1054 *
1055 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1056 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001057void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1058 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001059{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001060 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001061 int i, idx;
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001062 int noff, parts;
Masahiro Yamada030348f2020-07-17 10:46:18 +09001063 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001064
1065 for (i = 0; i < node_info_size; i++) {
1066 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +09001067
Marek Behún5d6b4482022-01-20 01:04:42 +01001068 fdt_for_each_node_by_compatible(noff, blob, -1,
1069 node_info[i].compat) {
Masahiro Yamada21644292020-07-17 10:46:19 +09001070 const char *prop;
1071
1072 prop = fdt_getprop(blob, noff, "status", NULL);
1073 if (prop && !strcmp(prop, "disabled"))
1074 continue;
1075
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001076 debug("%s: %s, mtd dev type %d\n",
1077 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001078 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +09001079
1080 if (!inited) {
1081 if (mtdparts_init() != 0)
1082 return;
1083 inited = true;
1084 }
1085
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001086 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001087 if (dev) {
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001088 parts = fdt_subnode_offset(blob, noff,
1089 "partitions");
1090 if (parts < 0)
1091 parts = noff;
1092
1093 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001094 return; /* return on error */
1095 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001096 }
1097 }
1098}
1099#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001100
Patrick Delaunay019127b2023-06-08 17:16:38 +02001101int fdt_copy_fixed_partitions(void *blob)
1102{
1103 ofnode node, subnode;
1104 int off, suboff, res;
1105 char path[256];
1106 int address_cells, size_cells;
1107 u8 i, j, child_count;
1108
1109 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1110 while (ofnode_valid(node)) {
1111 /* copy the U-Boot fixed partition */
1112 address_cells = ofnode_read_simple_addr_cells(node);
1113 size_cells = ofnode_read_simple_size_cells(node);
1114
1115 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1116 if (res)
1117 return res;
1118
1119 off = fdt_path_offset(blob, path);
1120 if (off < 0)
1121 return -ENODEV;
1122
1123 off = fdt_find_or_add_subnode(blob, off, "partitions");
1124 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1125 if (res)
1126 return res;
1127
1128 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1129 if (res)
1130 return res;
1131
1132 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1133 if (res)
1134 return res;
1135
1136 /*
1137 * parse partition in reverse order as fdt_find_or_add_subnode() only
1138 * insert the new node after the parent's properties
1139 */
1140 child_count = ofnode_get_child_count(node);
1141 for (i = child_count; i > 0 ; i--) {
1142 subnode = ofnode_first_subnode(node);
1143 if (!ofnode_valid(subnode))
1144 break;
1145
1146 for (j = 0; (j < i - 1); j++)
1147 subnode = ofnode_next_subnode(subnode);
1148
1149 if (!ofnode_valid(subnode))
1150 break;
1151
1152 const u32 *reg;
1153 int len;
1154
1155 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1156 res = fdt_setprop_string(blob, suboff, "label",
1157 ofnode_read_string(subnode, "label"));
1158 if (res)
1159 return res;
1160
1161 reg = ofnode_get_property(subnode, "reg", &len);
1162 res = fdt_setprop(blob, suboff, "reg", reg, len);
1163 if (res)
1164 return res;
1165 }
1166
1167 /* go to next fixed-partitions node */
1168 node = ofnode_by_compatible(node, "fixed-partitions");
1169 }
1170
1171 return 0;
1172}
1173
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001174void fdt_del_node_and_alias(void *blob, const char *alias)
1175{
1176 int off = fdt_path_offset(blob, alias);
1177
1178 if (off < 0)
1179 return;
1180
1181 fdt_del_node(blob, off);
1182
1183 off = fdt_path_offset(blob, "/aliases");
1184 fdt_delprop(blob, off, alias);
1185}
Kumar Galab87e7932010-06-16 14:27:38 -05001186
Kumar Galab87e7932010-06-16 14:27:38 -05001187/* Max address size we deal with */
1188#define OF_MAX_ADDR_CELLS 4
Dario Binacchif8fc7032021-05-01 17:05:26 +02001189#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1190 (ns) > 0)
Kumar Galab87e7932010-06-16 14:27:38 -05001191
1192/* Debug utility */
1193#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +00001194static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001195{
1196 printf("%s", s);
1197 while(na--)
1198 printf(" %08x", *(addr++));
1199 printf("\n");
1200}
1201#else
Kim Phillips6542c072013-01-16 14:00:11 +00001202static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001203#endif
1204
Paul Burtondbef2f02016-05-17 07:43:24 +01001205/**
1206 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001207 * @name: A string used to identify this bus in debug output.
1208 * @addresses: The name of the DT property from which addresses are
1209 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001210 * @match: Return non-zero if the node whose parent is at
1211 * parentoffset in the FDT blob corresponds to a bus
1212 * of this type, otherwise return zero. If NULL a match
1213 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001214 * @count_cells:Count how many cells (be32 values) a node whose parent
1215 * is at parentoffset in the FDT blob will require to
1216 * represent its address (written to *addrc) & size
1217 * (written to *sizec).
1218 * @map: Map the address addr from the address space of this
1219 * bus to that of its parent, making use of the ranges
1220 * read from DT to an array at range. na and ns are the
1221 * number of cells (be32 values) used to hold and address
1222 * or size, respectively, for this bus. pna is the number
1223 * of cells used to hold an address for the parent bus.
1224 * Returns the address in the address space of the parent
1225 * bus.
1226 * @translate: Update the value of the address cells at addr within an
1227 * FDT by adding offset to it. na specifies the number of
1228 * cells used to hold the address being translated. Returns
1229 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001230 *
1231 * Each bus type will include a struct of_bus in the of_busses array,
1232 * providing implementations of some or all of the functions used to
1233 * match the bus & handle address translation for its children.
1234 */
Kumar Galab87e7932010-06-16 14:27:38 -05001235struct of_bus {
1236 const char *name;
1237 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001238 int (*match)(const void *blob, int parentoffset);
1239 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001240 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001241 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001242 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001243 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001244};
1245
1246/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001247void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001248 int *addrc, int *sizec)
1249{
Kim Phillips6542c072013-01-16 14:00:11 +00001250 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001251
Simon Glass9fbc6322014-10-23 18:58:57 -06001252 if (addrc)
1253 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001254
1255 if (sizec) {
1256 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1257 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001258 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001259 else
1260 *sizec = 1;
1261 }
Kumar Galab87e7932010-06-16 14:27:38 -05001262}
1263
Kim Phillips6542c072013-01-16 14:00:11 +00001264static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001265 int na, int ns, int pna)
1266{
1267 u64 cp, s, da;
1268
Simon Glassbb7c01e2017-05-18 20:09:26 -06001269 cp = fdt_read_number(range, na);
1270 s = fdt_read_number(range + na + pna, ns);
1271 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001272
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301273 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001274
1275 if (da < cp || da >= (cp + s))
1276 return OF_BAD_ADDR;
1277 return da - cp;
1278}
1279
Kim Phillips6542c072013-01-16 14:00:11 +00001280static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001281{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001282 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001283 memset(addr, 0, na * 4);
1284 a += offset;
1285 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001286 addr[na - 2] = cpu_to_fdt32(a >> 32);
1287 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001288
1289 return 0;
1290}
1291
Paul Burtondbef2f02016-05-17 07:43:24 +01001292#ifdef CONFIG_OF_ISA_BUS
1293
1294/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001295static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001296{
1297 const char *name;
1298
1299 name = fdt_get_name(blob, parentoffset, NULL);
1300 if (!name)
1301 return 0;
1302
1303 return !strcmp(name, "isa");
1304}
1305
Stephen Warrenc99581b2016-08-05 09:47:50 -06001306static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001307 int *addrc, int *sizec)
1308{
1309 if (addrc)
1310 *addrc = 2;
1311 if (sizec)
1312 *sizec = 1;
1313}
1314
1315static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1316 int na, int ns, int pna)
1317{
1318 u64 cp, s, da;
1319
1320 /* Check address type match */
1321 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1322 return OF_BAD_ADDR;
1323
Simon Glassbb7c01e2017-05-18 20:09:26 -06001324 cp = fdt_read_number(range + 1, na - 1);
1325 s = fdt_read_number(range + na + pna, ns);
1326 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001327
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301328 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001329
1330 if (da < cp || da >= (cp + s))
1331 return OF_BAD_ADDR;
1332 return da - cp;
1333}
1334
1335static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1336{
1337 return of_bus_default_translate(addr + 1, offset, na - 1);
1338}
1339
1340#endif /* CONFIG_OF_ISA_BUS */
1341
Kumar Galab87e7932010-06-16 14:27:38 -05001342/* Array of bus specific translators */
1343static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001344#ifdef CONFIG_OF_ISA_BUS
1345 /* ISA */
1346 {
1347 .name = "isa",
1348 .addresses = "reg",
1349 .match = of_bus_isa_match,
1350 .count_cells = of_bus_isa_count_cells,
1351 .map = of_bus_isa_map,
1352 .translate = of_bus_isa_translate,
1353 },
1354#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001355 /* Default */
1356 {
1357 .name = "default",
1358 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001359 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001360 .map = of_bus_default_map,
1361 .translate = of_bus_default_translate,
1362 },
1363};
1364
Stephen Warrenc99581b2016-08-05 09:47:50 -06001365static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001366{
1367 struct of_bus *bus;
1368
1369 if (ARRAY_SIZE(of_busses) == 1)
1370 return of_busses;
1371
1372 for (bus = of_busses; bus; bus++) {
1373 if (!bus->match || bus->match(blob, parentoffset))
1374 return bus;
1375 }
1376
1377 /*
1378 * We should always have matched the default bus at least, since
1379 * it has a NULL match field. If we didn't then it somehow isn't
1380 * in the of_busses array or something equally catastrophic has
1381 * gone wrong.
1382 */
1383 assert(0);
1384 return NULL;
1385}
1386
Stephen Warrenc99581b2016-08-05 09:47:50 -06001387static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001388 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001389 int na, int ns, int pna, const char *rprop)
1390{
Kim Phillips6542c072013-01-16 14:00:11 +00001391 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001392 int rlen;
1393 int rone;
1394 u64 offset = OF_BAD_ADDR;
1395
1396 /* Normally, an absence of a "ranges" property means we are
1397 * crossing a non-translatable boundary, and thus the addresses
1398 * below the current not cannot be converted to CPU physical ones.
1399 * Unfortunately, while this is very clear in the spec, it's not
1400 * what Apple understood, and they do have things like /uni-n or
1401 * /ht nodes with no "ranges" property and a lot of perfectly
1402 * useable mapped devices below them. Thus we treat the absence of
1403 * "ranges" as equivalent to an empty "ranges" property which means
1404 * a 1:1 translation at that level. It's up to the caller not to try
1405 * to translate addresses that aren't supposed to be translated in
1406 * the first place. --BenH.
1407 */
Kim Phillips6542c072013-01-16 14:00:11 +00001408 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001409 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001410 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001411 memset(addr, 0, pna * 4);
1412 debug("OF: no ranges, 1:1 translation\n");
1413 goto finish;
1414 }
1415
1416 debug("OF: walking ranges...\n");
1417
1418 /* Now walk through the ranges */
1419 rlen /= 4;
1420 rone = na + pna + ns;
1421 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1422 offset = bus->map(addr, ranges, na, ns, pna);
1423 if (offset != OF_BAD_ADDR)
1424 break;
1425 }
1426 if (offset == OF_BAD_ADDR) {
1427 debug("OF: not found !\n");
1428 return 1;
1429 }
1430 memcpy(addr, ranges + na, 4 * pna);
1431
1432 finish:
1433 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001434 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001435
1436 /* Translate it into parent bus space */
1437 return pbus->translate(addr, offset, pna);
1438}
1439
1440/*
1441 * Translate an address from the device-tree into a CPU physical address,
1442 * this walks up the tree and applies the various bus mappings on the
1443 * way.
1444 *
1445 * Note: We consider that crossing any level with #size-cells == 0 to mean
1446 * that translation is impossible (that is we are not dealing with a value
1447 * that can be mapped to a cpu physical address). This is not really specified
1448 * that way, but this is traditionally the way IBM at least do things
1449 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001450static u64 __of_translate_address(const void *blob, int node_offset,
1451 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001452{
1453 int parent;
1454 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001455 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001456 int na, ns, pna, pns;
1457 u64 result = OF_BAD_ADDR;
1458
1459 debug("OF: ** translation for device %s **\n",
1460 fdt_get_name(blob, node_offset, NULL));
1461
1462 /* Get parent & match bus type */
1463 parent = fdt_parent_offset(blob, node_offset);
1464 if (parent < 0)
1465 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001466 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001467
1468 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001469 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001470 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001471 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1472 fdt_get_name(blob, node_offset, NULL));
1473 goto bail;
1474 }
1475 memcpy(addr, in_addr, na * 4);
1476
1477 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1478 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1479 of_dump_addr("OF: translating address:", addr, na);
1480
1481 /* Translate */
1482 for (;;) {
1483 /* Switch to parent bus */
1484 node_offset = parent;
1485 parent = fdt_parent_offset(blob, node_offset);
1486
1487 /* If root, we have finished */
1488 if (parent < 0) {
1489 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001490 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001491 break;
1492 }
1493
1494 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001495 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001496 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001497 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001498 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1499 fdt_get_name(blob, node_offset, NULL));
1500 break;
1501 }
1502
1503 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1504 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1505
1506 /* Apply bus translation */
1507 if (of_translate_one(blob, node_offset, bus, pbus,
1508 addr, na, ns, pna, rprop))
1509 break;
1510
1511 /* Complete the move up one level */
1512 na = pna;
1513 ns = pns;
1514 bus = pbus;
1515
1516 of_dump_addr("OF: one level translation:", addr, na);
1517 }
1518 bail:
1519
1520 return result;
1521}
1522
Stephen Warrenc99581b2016-08-05 09:47:50 -06001523u64 fdt_translate_address(const void *blob, int node_offset,
1524 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001525{
1526 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1527}
Kumar Gala800d1d12010-07-04 12:48:21 -05001528
Fabien Dessenne22236e02019-05-31 15:11:30 +02001529u64 fdt_translate_dma_address(const void *blob, int node_offset,
1530 const fdt32_t *in_addr)
1531{
1532 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1533}
1534
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001535int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1536 dma_addr_t *bus, u64 *size)
1537{
1538 bool found_dma_ranges = false;
1539 struct of_bus *bus_node;
1540 const fdt32_t *ranges;
1541 int na, ns, pna, pns;
1542 int parent = node;
1543 int ret = 0;
1544 int len;
1545
1546 /* Find the closest dma-ranges property */
1547 while (parent >= 0) {
1548 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1549
1550 /* Ignore empty ranges, they imply no translation required */
1551 if (ranges && len > 0)
1552 break;
1553
1554 /* Once we find 'dma-ranges', then a missing one is an error */
1555 if (found_dma_ranges && !ranges) {
1556 ret = -EINVAL;
1557 goto out;
1558 }
1559
1560 if (ranges)
1561 found_dma_ranges = true;
1562
1563 parent = fdt_parent_offset(blob, parent);
1564 }
1565
1566 if (!ranges || parent < 0) {
1567 debug("no dma-ranges found for node %s\n",
1568 fdt_get_name(blob, node, NULL));
1569 ret = -ENOENT;
1570 goto out;
1571 }
1572
1573 /* switch to that node */
1574 node = parent;
1575 parent = fdt_parent_offset(blob, node);
1576 if (parent < 0) {
Vagrant Cascadiand00c5c72021-12-21 13:06:58 -08001577 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001578 ret = -EINVAL;
1579 goto out;
1580 }
1581
1582 /* Get the address sizes both for the bus and its parent */
1583 bus_node = of_match_bus(blob, node);
1584 bus_node->count_cells(blob, node, &na, &ns);
1585 if (!OF_CHECK_COUNTS(na, ns)) {
1586 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1587 fdt_get_name(blob, node, NULL));
1588 return -EINVAL;
1589 goto out;
1590 }
1591
1592 bus_node = of_match_bus(blob, parent);
1593 bus_node->count_cells(blob, parent, &pna, &pns);
1594 if (!OF_CHECK_COUNTS(pna, pns)) {
1595 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1596 fdt_get_name(blob, parent, NULL));
1597 return -EINVAL;
1598 goto out;
1599 }
1600
1601 *bus = fdt_read_number(ranges, na);
1602 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1603 *size = fdt_read_number(ranges + na + pna, ns);
1604out:
1605 return ret;
1606}
1607
Kumar Gala800d1d12010-07-04 12:48:21 -05001608/**
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001609 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala800d1d12010-07-04 12:48:21 -05001610 * who's reg property matches a physical cpu address
1611 *
1612 * @blob: ptr to device tree
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001613 * @compat: compatible string to match
Kumar Gala800d1d12010-07-04 12:48:21 -05001614 * @compat_off: property name
1615 *
1616 */
1617int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1618 phys_addr_t compat_off)
1619{
Marek Behún5d6b4482022-01-20 01:04:42 +01001620 int len, off;
1621
1622 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips6542c072013-01-16 14:00:11 +00001623 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún5d6b4482022-01-20 01:04:42 +01001624 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1625 return off;
Kumar Gala800d1d12010-07-04 12:48:21 -05001626 }
1627
1628 return -FDT_ERR_NOTFOUND;
1629}
1630
Marek Behún4ef5ca62021-11-26 14:57:10 +01001631static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1632{
1633 char path[512];
1634 int len;
1635
1636 len = vsnprintf(path, sizeof(path), fmt, ap);
1637 if (len < 0 || len + 1 > sizeof(path))
1638 return -FDT_ERR_NOSPACE;
1639
1640 return fdt_path_offset(blob, path);
1641}
1642
1643/**
1644 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1645 *
1646 * @blob: ptr to device tree
1647 * @fmt: path format
1648 * @ap: vsnprintf arguments
1649 */
1650int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1651{
1652 va_list ap;
1653 int res;
1654
1655 va_start(ap, fmt);
1656 res = vnode_offset_by_pathf(blob, fmt, ap);
1657 va_end(ap);
1658
1659 return res;
1660}
1661
Gerald Van Barenaf737882011-07-14 21:40:10 -04001662/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001663 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001664 *
1665 * @fdt: ptr to device tree
1666 * @nodeoffset: node to update
1667 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001668 */
1669int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001670{
1671 int ret;
1672
1673#ifdef DEBUG
1674 int off = fdt_node_offset_by_phandle(fdt, phandle);
1675
1676 if ((off >= 0) && (off != nodeoffset)) {
1677 char buf[64];
1678
1679 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1680 printf("Trying to update node %s with phandle %u ",
1681 buf, phandle);
1682
1683 fdt_get_path(fdt, off, buf, sizeof(buf));
1684 printf("that already exists in node %s.\n", buf);
1685 return -FDT_ERR_BADPHANDLE;
1686 }
1687#endif
1688
1689 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barenaf737882011-07-14 21:40:10 -04001690
1691 return ret;
1692}
1693
Kumar Galad5fc2582011-08-01 00:25:20 -05001694/*
Marek Behúnae0ef952021-11-26 14:57:09 +01001695 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Galad5fc2582011-08-01 00:25:20 -05001696 *
1697 * @fdt: ptr to device tree
1698 * @nodeoffset: node to update
1699 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001700unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001701{
1702 /* see if there is a phandle already */
Marek Behún3577eba32021-11-26 14:57:07 +01001703 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Galad5fc2582011-08-01 00:25:20 -05001704
1705 /* if we got 0, means no phandle so create one */
1706 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001707 int ret;
1708
Marek Behún3577eba32021-11-26 14:57:07 +01001709 ret = fdt_generate_phandle(fdt, &phandle);
1710 if (ret < 0) {
1711 printf("Can't generate phandle: %s\n",
1712 fdt_strerror(ret));
1713 return 0;
1714 }
1715
Timur Tabi0f46c802011-09-20 18:24:34 -05001716 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1717 if (ret < 0) {
1718 printf("Can't set phandle %u: %s\n", phandle,
1719 fdt_strerror(ret));
1720 return 0;
1721 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001722 }
1723
1724 return phandle;
1725}
1726
Marek Behún4ef5ca62021-11-26 14:57:10 +01001727/**
1728 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1729 * given compatible
1730 *
1731 * @fdt: ptr to device tree
1732 * @compat: node's compatible string
1733 */
1734unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1735{
1736 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1737
1738 if (offset < 0) {
1739 printf("Can't find node with compatible \"%s\": %s\n", compat,
1740 fdt_strerror(offset));
1741 return 0;
1742 }
1743
1744 return fdt_create_phandle(fdt, offset);
1745}
1746
1747/**
1748 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1749 * sprintf-formatted path
1750 *
1751 * @fdt: ptr to device tree
1752 * @fmt, ...: path format string and arguments to pass to sprintf
1753 */
1754unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1755{
1756 va_list ap;
1757 int offset;
1758
1759 va_start(ap, fmt);
1760 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1761 va_end(ap);
1762
1763 if (offset < 0) {
1764 printf("Can't find node by given path: %s\n",
1765 fdt_strerror(offset));
1766 return 0;
1767 }
1768
1769 return fdt_create_phandle(fdt, offset);
1770}
1771
Shengzhou Liua7be8022011-10-14 16:26:05 +08001772/*
1773 * fdt_set_node_status: Set status for the given node
1774 *
1775 * @fdt: ptr to device tree
1776 * @nodeoffset: node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001777 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001778 */
Marek Behúnf872e832021-11-26 14:57:08 +01001779int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001780{
Shengzhou Liua7be8022011-10-14 16:26:05 +08001781 int ret = 0;
1782
1783 if (nodeoffset < 0)
1784 return nodeoffset;
1785
1786 switch (status) {
1787 case FDT_STATUS_OKAY:
1788 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1789 break;
1790 case FDT_STATUS_DISABLED:
1791 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1792 break;
1793 case FDT_STATUS_FAIL:
1794 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1795 break;
Shengzhou Liua7be8022011-10-14 16:26:05 +08001796 default:
1797 printf("Invalid fdt status: %x\n", status);
1798 ret = -1;
1799 break;
1800 }
1801
1802 return ret;
1803}
1804
1805/*
1806 * fdt_set_status_by_alias: Set status for the given node given an alias
1807 *
1808 * @fdt: ptr to device tree
1809 * @alias: alias of node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001810 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001811 */
1812int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behúnf872e832021-11-26 14:57:08 +01001813 enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001814{
1815 int offset = fdt_path_offset(fdt, alias);
1816
Marek Behúnf872e832021-11-26 14:57:08 +01001817 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liua7be8022011-10-14 16:26:05 +08001818}
1819
Marek Behún4ef5ca62021-11-26 14:57:10 +01001820/**
1821 * fdt_set_status_by_compatible: Set node status for first node with given
1822 * compatible
1823 *
1824 * @fdt: ptr to device tree
1825 * @compat: node's compatible string
1826 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1827 */
1828int fdt_set_status_by_compatible(void *fdt, const char *compat,
1829 enum fdt_status status)
1830{
1831 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1832
1833 if (offset < 0)
1834 return offset;
1835
1836 return fdt_set_node_status(fdt, offset, status);
1837}
1838
1839/**
1840 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1841 * path
1842 *
1843 * @fdt: ptr to device tree
1844 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1845 * @fmt, ...: path format string and arguments to pass to sprintf
1846 */
1847int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1848 ...)
1849{
1850 va_list ap;
1851 int offset;
1852
1853 va_start(ap, fmt);
1854 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1855 va_end(ap);
1856
1857 if (offset < 0)
1858 return offset;
1859
1860 return fdt_set_node_status(fdt, offset, status);
1861}
1862
Timur Tabi8ebaf202011-05-03 13:24:07 -05001863/*
1864 * Verify the physical address of device tree node for a given alias
1865 *
1866 * This function locates the device tree node of a given alias, and then
1867 * verifies that the physical address of that device matches the given
1868 * parameter. It displays a message if there is a mismatch.
1869 *
1870 * Returns 1 on success, 0 on failure
1871 */
1872int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1873{
1874 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001875 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001876 int node, len;
1877 u64 dt_addr;
1878
1879 path = fdt_getprop(fdt, anode, alias, NULL);
1880 if (!path) {
1881 /* If there's no such alias, then it's not a failure */
1882 return 1;
1883 }
1884
1885 node = fdt_path_offset(fdt, path);
1886 if (node < 0) {
1887 printf("Warning: device tree alias '%s' points to invalid "
1888 "node %s.\n", alias, path);
1889 return 0;
1890 }
1891
1892 reg = fdt_getprop(fdt, node, "reg", &len);
1893 if (!reg) {
1894 printf("Warning: device tree node '%s' has no address.\n",
1895 path);
1896 return 0;
1897 }
1898
1899 dt_addr = fdt_translate_address(fdt, node, reg);
1900 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001901 printf("Warning: U-Boot configured device %s at address %llu,\n"
1902 "but the device tree has it address %llx.\n",
1903 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001904 return 0;
1905 }
1906
1907 return 1;
1908}
1909
1910/*
1911 * Returns the base address of an SOC or PCI node
1912 */
Simon Glass293340a2017-05-18 20:09:00 -06001913u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001914{
1915 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001916 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001917
Simon Glass29f304b2017-07-04 13:31:20 -06001918 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001919
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001920 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001921}
Alexander Graf19555be2014-04-11 17:09:41 +02001922
1923/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001924 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1925 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001926 */
1927static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1928 uint64_t *val, int cells)
1929{
Bin Mengd67d7d02021-02-25 17:22:24 +08001930 const fdt32_t *prop32;
1931 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001932
1933 if ((cell_off + cells) > prop_len)
1934 return -FDT_ERR_NOSPACE;
1935
Bin Mengd67d7d02021-02-25 17:22:24 +08001936 prop32 = &prop[cell_off];
1937
1938 /*
1939 * Special handling for PCI address in PCI bus <ranges>
1940 *
1941 * PCI child address is made up of 3 cells. Advance the cell offset
1942 * by 1 so that the PCI child address can be correctly read.
1943 */
1944 if (cells == 3)
1945 cell_off += 1;
1946 prop64 = (const fdt64_t *)&prop[cell_off];
1947
Alexander Graf19555be2014-04-11 17:09:41 +02001948 switch (cells) {
1949 case 1:
1950 *val = fdt32_to_cpu(*prop32);
1951 break;
1952 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001953 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001954 *val = fdt64_to_cpu(*prop64);
1955 break;
1956 default:
1957 return -FDT_ERR_NOSPACE;
1958 }
1959
1960 return 0;
1961}
1962
1963/**
1964 * fdt_read_range - Read a node's n'th range property
1965 *
1966 * @fdt: ptr to device tree
1967 * @node: offset of node
1968 * @n: range index
1969 * @child_addr: pointer to storage for the "child address" field
1970 * @addr: pointer to storage for the CPU view translated physical start
1971 * @len: pointer to storage for the range length
1972 *
1973 * Convenience function that reads and interprets a specific range out of
1974 * a number of the "ranges" property array.
1975 */
1976int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1977 uint64_t *addr, uint64_t *len)
1978{
1979 int pnode = fdt_parent_offset(fdt, node);
1980 const fdt32_t *ranges;
1981 int pacells;
1982 int acells;
1983 int scells;
1984 int ranges_len;
1985 int cell = 0;
1986 int r = 0;
1987
1988 /*
1989 * The "ranges" property is an array of
1990 * { <child address> <parent address> <size in child address space> }
1991 *
1992 * All 3 elements can span a diffent number of cells. Fetch their size.
1993 */
1994 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1995 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1996 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1997
1998 /* Now try to get the ranges property */
1999 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
2000 if (!ranges)
2001 return -FDT_ERR_NOTFOUND;
2002 ranges_len /= sizeof(uint32_t);
2003
2004 /* Jump to the n'th entry */
2005 cell = n * (pacells + acells + scells);
2006
2007 /* Read <child address> */
2008 if (child_addr) {
2009 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
2010 acells);
2011 if (r)
2012 return r;
2013 }
2014 cell += acells;
2015
2016 /* Read <parent address> */
2017 if (addr)
2018 *addr = fdt_translate_address(fdt, node, ranges + cell);
2019 cell += pacells;
2020
2021 /* Read <size in child address space> */
2022 if (len) {
2023 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
2024 if (r)
2025 return r;
2026 }
2027
2028 return 0;
2029}
Hans de Goedeea876372014-11-17 15:29:11 +01002030
2031/**
2032 * fdt_setup_simplefb_node - Fill and enable a simplefb node
2033 *
2034 * @fdt: ptr to device tree
2035 * @node: offset of the simplefb node
2036 * @base_address: framebuffer base address
2037 * @width: width in pixels
2038 * @height: height in pixels
2039 * @stride: bytes per line
2040 * @format: pixel format string
2041 *
2042 * Convenience function to fill and enable a simplefb node.
2043 */
2044int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2045 u32 height, u32 stride, const char *format)
2046{
2047 char name[32];
2048 fdt32_t cells[4];
2049 int i, addrc, sizec, ret;
2050
Simon Glassbb7c01e2017-05-18 20:09:26 -06002051 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2052 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01002053 i = 0;
2054 if (addrc == 2)
2055 cells[i++] = cpu_to_fdt32(base_address >> 32);
2056 cells[i++] = cpu_to_fdt32(base_address);
2057 if (sizec == 2)
2058 cells[i++] = 0;
2059 cells[i++] = cpu_to_fdt32(height * stride);
2060
2061 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2062 if (ret < 0)
2063 return ret;
2064
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002065 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01002066 ret = fdt_set_name(fdt, node, name);
2067 if (ret < 0)
2068 return ret;
2069
2070 ret = fdt_setprop_u32(fdt, node, "width", width);
2071 if (ret < 0)
2072 return ret;
2073
2074 ret = fdt_setprop_u32(fdt, node, "height", height);
2075 if (ret < 0)
2076 return ret;
2077
2078 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2079 if (ret < 0)
2080 return ret;
2081
2082 ret = fdt_setprop_string(fdt, node, "format", format);
2083 if (ret < 0)
2084 return ret;
2085
2086 ret = fdt_setprop_string(fdt, node, "status", "okay");
2087 if (ret < 0)
2088 return ret;
2089
2090 return 0;
2091}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002092
Devarsh Thakkar31199892024-02-22 18:38:10 +05302093#if CONFIG_IS_ENABLED(VIDEO)
2094int fdt_add_fb_mem_rsv(void *blob)
2095{
2096 struct fdt_memory mem;
2097
2098 /* nothing to do when the frame buffer is not defined */
2099 if (gd->video_bottom == gd->video_top)
2100 return 0;
2101
2102 /* reserved with no-map tag the video buffer */
2103 mem.start = gd->video_bottom;
2104 mem.end = gd->video_top - 1;
2105
2106 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2107 FDTDEC_RESERVED_MEMORY_NO_MAP);
2108}
2109#endif
2110
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002111/*
2112 * Update native-mode in display-timings from display environment variable.
2113 * The node to update are specified by path.
2114 */
2115int fdt_fixup_display(void *blob, const char *path, const char *display)
2116{
2117 int off, toff;
2118
2119 if (!display || !path)
2120 return -FDT_ERR_NOTFOUND;
2121
2122 toff = fdt_path_offset(blob, path);
2123 if (toff >= 0)
2124 toff = fdt_subnode_offset(blob, toff, "display-timings");
2125 if (toff < 0)
2126 return toff;
2127
2128 for (off = fdt_first_subnode(blob, toff);
2129 off >= 0;
2130 off = fdt_next_subnode(blob, off)) {
2131 uint32_t h = fdt_get_phandle(blob, off);
2132 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2133 fdt32_to_cpu(h));
2134 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2135 return fdt_setprop_u32(blob, toff, "native-mode", h);
2136 }
2137 return toff;
2138}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002139
2140#ifdef CONFIG_OF_LIBFDT_OVERLAY
2141/**
2142 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2143 *
2144 * @fdt: ptr to device tree
2145 * @fdto: ptr to device tree overlay
2146 *
2147 * Convenience function to apply an overlay and display helpful messages
2148 * in the case of an error
2149 */
2150int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2151{
2152 int err;
2153 bool has_symbols;
2154
2155 err = fdt_path_offset(fdt, "/__symbols__");
2156 has_symbols = err >= 0;
2157
2158 err = fdt_overlay_apply(fdt, fdto);
2159 if (err < 0) {
2160 printf("failed on fdt_overlay_apply(): %s\n",
2161 fdt_strerror(err));
2162 if (!has_symbols) {
Hugo Villeneuve90730f12023-10-26 15:54:49 -04002163 printf("base fdt does not have a /__symbols__ node\n");
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002164 printf("make sure you've compiled with -@\n");
2165 }
2166 }
2167 return err;
2168}
2169#endif
Kory Maincent623e1ac2021-05-04 19:31:21 +02002170
2171/**
2172 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2173 *
2174 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002175 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincent623e1ac2021-05-04 19:31:21 +02002176 */
2177int fdt_valid(struct fdt_header **blobp)
2178{
2179 const void *blob = *blobp;
2180 int err;
2181
2182 if (!blob) {
2183 printf("The address of the fdt is invalid (NULL).\n");
2184 return 0;
2185 }
2186
2187 err = fdt_check_header(blob);
2188 if (err == 0)
2189 return 1; /* valid */
2190
2191 if (err < 0) {
2192 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2193 /*
2194 * Be more informative on bad version.
2195 */
2196 if (err == -FDT_ERR_BADVERSION) {
2197 if (fdt_version(blob) <
2198 FDT_FIRST_SUPPORTED_VERSION) {
2199 printf(" - too old, fdt %d < %d",
2200 fdt_version(blob),
2201 FDT_FIRST_SUPPORTED_VERSION);
2202 }
2203 if (fdt_last_comp_version(blob) >
2204 FDT_LAST_SUPPORTED_VERSION) {
2205 printf(" - too new, fdt %d > %d",
2206 fdt_version(blob),
2207 FDT_LAST_SUPPORTED_VERSION);
2208 }
2209 }
2210 printf("\n");
2211 *blobp = NULL;
2212 return 0;
2213 }
2214 return 1;
2215}