blob: 2392027d40ba292a4cd714c7f23497e9879ac454 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04002/*
3 * (C) Copyright 2007
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 *
Shengzhou Liu2a523f52011-10-14 16:26:05 +08006 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04007 */
8
Tim Harvey00afd1e2024-06-18 14:06:06 -07009#include <dm.h>
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +020010#include <abuf.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Heinrich Schuchardt9ad0a792018-11-18 17:58:51 +010013#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <net.h>
Tim Harvey00afd1e2024-06-18 14:06:06 -070015#include <rng.h>
Anton Vorontsov3e303f72009-10-15 17:47:04 +040016#include <stdio_dev.h>
Tim Harvey00afd1e2024-06-18 14:06:06 -070017#include <dm/device_compat.h>
Patrick Delaunay0e7cc082023-06-08 17:16:37 +020018#include <dm/ofnode.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040019#include <linux/ctype.h>
20#include <linux/types.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040021#include <asm/global_data.h>
Sam Protsenko16b80c92024-03-29 19:55:53 -050022#include <asm/unaligned.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090023#include <linux/libfdt.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040024#include <fdt_support.h>
Kumar Gala151c8b02007-11-26 17:06:15 -060025#include <exports.h>
Bin Menga0ae3802015-12-07 01:39:47 -080026#include <fdtdec.h>
Francesco Dolcini622ecee2022-05-19 16:22:26 +020027#include <version.h>
Devarsh Thakkarefe1cee2024-02-22 18:38:10 +053028#include <video.h>
29
30DECLARE_GLOBAL_DATA_PTR;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040031
Kumar Gala3bed2aa2008-10-23 00:05:47 -050032/**
Alexander Graf94fb1822014-04-11 17:09:40 +020033 * fdt_getprop_u32_default_node - Return a node's property or a default
34 *
35 * @fdt: ptr to device tree
36 * @off: offset of node
37 * @cell: cell offset in property
38 * @prop: property name
39 * @dflt: default value if the property isn't found
40 *
41 * Convenience function to return a node's property or a default value if
42 * the property doesn't exist.
43 */
44u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
45 const char *prop, const u32 dflt)
46{
47 const fdt32_t *val;
48 int len;
49
50 val = fdt_getprop(fdt, off, prop, &len);
51
52 /* Check if property exists */
53 if (!val)
54 return dflt;
55
56 /* Check if property is long enough */
57 if (len < ((cell + 1) * sizeof(uint32_t)))
58 return dflt;
59
60 return fdt32_to_cpu(*val);
61}
62
63/**
Kumar Gala3bed2aa2008-10-23 00:05:47 -050064 * fdt_getprop_u32_default - Find a node and return it's property or a default
65 *
66 * @fdt: ptr to device tree
67 * @path: path of node
68 * @prop: property name
69 * @dflt: default value if the property isn't found
70 *
71 * Convenience function to find a node and return it's property or a
72 * default value if it doesn't exist.
73 */
Gabe Black07e12782011-11-08 01:09:44 -080074u32 fdt_getprop_u32_default(const void *fdt, const char *path,
75 const char *prop, const u32 dflt)
Kumar Gala3bed2aa2008-10-23 00:05:47 -050076{
Kumar Gala3bed2aa2008-10-23 00:05:47 -050077 int off;
78
79 off = fdt_path_offset(fdt, path);
80 if (off < 0)
81 return dflt;
82
Alexander Graf94fb1822014-04-11 17:09:40 +020083 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala3bed2aa2008-10-23 00:05:47 -050084}
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040085
Kumar Galaa3c29332007-10-24 10:21:57 -050086/**
87 * fdt_find_and_setprop: Find a node and set it's property
88 *
89 * @fdt: ptr to device tree
90 * @node: path of node
91 * @prop: property name
92 * @val: ptr to new value
93 * @len: length of new property value
94 * @create: flag to create the property if it doesn't exist
95 *
96 * Convenience function to directly set a property given the path to the node.
97 */
98int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
99 const void *val, int len, int create)
100{
Kumar Gala8d04f022007-10-24 11:04:22 -0500101 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galaa3c29332007-10-24 10:21:57 -0500102
103 if (nodeoff < 0)
104 return nodeoff;
105
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000106 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galaa3c29332007-10-24 10:21:57 -0500107 return 0; /* create flag not set; so exit quietly */
108
109 return fdt_setprop(fdt, nodeoff, prop, val, len);
110}
111
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900112/**
Simon Glassa9e8e292014-10-23 18:58:49 -0600113 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
114 *
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900115 * @fdt: pointer to the device tree blob
116 * @parentoffset: structure block offset of a node
117 * @name: name of the subnode to locate
118 *
119 * fdt_subnode_offset() finds a subnode of the node with a given name.
120 * If the subnode does not exist, it will be created.
121 */
Simon Glassa9e8e292014-10-23 18:58:49 -0600122int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900123{
124 int offset;
125
126 offset = fdt_subnode_offset(fdt, parentoffset, name);
127
128 if (offset == -FDT_ERR_NOTFOUND)
129 offset = fdt_add_subnode(fdt, parentoffset, name);
130
131 if (offset < 0)
132 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
133
134 return offset;
135}
136
Andre Przywarae036a1d2021-02-14 10:35:18 +0000137#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel40777812008-06-20 22:24:05 +0200138static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala151c8b02007-11-26 17:06:15 -0600139{
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900140 int err;
141 int aliasoff;
Kumar Gala151c8b02007-11-26 17:06:15 -0600142 char sername[9] = { 0 };
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900143 const void *path;
144 int len;
145 char tmp[256]; /* long enough */
Kumar Gala151c8b02007-11-26 17:06:15 -0600146
Bin Meng9f29aeb2016-01-13 19:38:58 -0800147 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala151c8b02007-11-26 17:06:15 -0600148
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900149 aliasoff = fdt_path_offset(fdt, "/aliases");
150 if (aliasoff < 0) {
151 err = aliasoff;
Scott Woodda77c812015-09-01 22:48:08 -0500152 goto noalias;
Kumar Gala151c8b02007-11-26 17:06:15 -0600153 }
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900154
155 path = fdt_getprop(fdt, aliasoff, sername, &len);
156 if (!path) {
157 err = len;
Scott Woodda77c812015-09-01 22:48:08 -0500158 goto noalias;
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900159 }
160
161 /* fdt_setprop may break "path" so we copy it to tmp buffer */
162 memcpy(tmp, path, len);
163
164 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala151c8b02007-11-26 17:06:15 -0600165 if (err < 0)
166 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900167 fdt_strerror(err));
Kumar Gala151c8b02007-11-26 17:06:15 -0600168
169 return err;
Scott Woodda77c812015-09-01 22:48:08 -0500170
171noalias:
172 printf("WARNING: %s: could not read %s alias: %s\n",
173 __func__, sername, fdt_strerror(err));
174
175 return 0;
Kumar Gala151c8b02007-11-26 17:06:15 -0600176}
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900177#else
178static int fdt_fixup_stdout(void *fdt, int chosenoff)
179{
180 return 0;
181}
Kumar Gala151c8b02007-11-26 17:06:15 -0600182#endif
183
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900184static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
185 uint64_t val, int is_u64)
186{
187 if (is_u64)
188 return fdt_setprop_u64(fdt, nodeoffset, name, val);
189 else
190 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
191}
192
Paul Kocialkowski10be5b52015-05-21 11:27:03 +0200193int fdt_root(void *fdt)
194{
195 char *serial;
196 int err;
197
198 err = fdt_check_header(fdt);
199 if (err < 0) {
200 printf("fdt_root: %s\n", fdt_strerror(err));
201 return err;
202 }
203
Simon Glass00caae62017-08-03 12:22:12 -0600204 serial = env_get("serial#");
Paul Kocialkowski10be5b52015-05-21 11:27:03 +0200205 if (serial) {
206 err = fdt_setprop(fdt, 0, "serial-number", serial,
207 strlen(serial) + 1);
208
209 if (err < 0) {
210 printf("WARNING: could not set serial-number %s.\n",
211 fdt_strerror(err));
212 return err;
213 }
214 }
215
216 return 0;
217}
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900218
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900219int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500220{
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900221 int nodeoffset;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500222 int err, j, total;
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900223 int is_u64;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500224 uint64_t addr, size;
225
Masahiro Yamada50babaf2014-04-18 17:41:05 +0900226 /* just return if the size of initrd is zero */
227 if (initrd_start == initrd_end)
228 return 0;
229
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900230 /* find or create "/chosen" node. */
231 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
232 if (nodeoffset < 0)
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500233 return nodeoffset;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500234
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500235 total = fdt_num_mem_rsv(fdt);
236
237 /*
238 * Look for an existing entry and update it. If we don't find
239 * the entry, we will j be the next available slot.
240 */
241 for (j = 0; j < total; j++) {
242 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
243 if (addr == initrd_start) {
244 fdt_del_mem_rsv(fdt, j);
245 break;
246 }
247 }
248
Grant Likelyce6b27a2011-03-28 09:58:55 +0000249 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500250 if (err < 0) {
251 printf("fdt_initrd: %s\n", fdt_strerror(err));
252 return err;
253 }
254
Simon Glass933cdbb2014-10-23 18:58:57 -0600255 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengf77a6062013-12-14 11:47:29 +0800256
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900257 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
258 (uint64_t)initrd_start, is_u64);
259
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900260 if (err < 0) {
261 printf("WARNING: could not set linux,initrd-start %s.\n",
262 fdt_strerror(err));
263 return err;
264 }
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900265
266 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
267 (uint64_t)initrd_end, is_u64);
268
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900269 if (err < 0) {
270 printf("WARNING: could not set linux,initrd-end %s.\n",
271 fdt_strerror(err));
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500272
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900273 return err;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500274 }
275
276 return 0;
277}
278
Tim Harvey00afd1e2024-06-18 14:06:06 -0700279int fdt_kaslrseed(void *fdt, bool overwrite)
280{
281 int len, err, nodeoffset;
282 struct udevice *dev;
283 const u64 *orig;
284 u64 data = 0;
285
286 err = fdt_check_header(fdt);
287 if (err < 0)
288 return err;
289
290 /* find or create "/chosen" node. */
291 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
292 if (nodeoffset < 0)
293 return nodeoffset;
294
295 /* return without error if we are not overwriting and existing non-zero node */
296 orig = fdt_getprop(fdt, nodeoffset, "kaslr-seed", &len);
297 if (orig && len == sizeof(*orig))
298 data = fdt64_to_cpu(*orig);
299 if (data && !overwrite) {
300 debug("not overwriting existing kaslr-seed\n");
301 return 0;
302 }
303 err = uclass_get_device(UCLASS_RNG, 0, &dev);
304 if (err) {
305 printf("No RNG device\n");
306 return err;
307 }
308 err = dm_rng_read(dev, &data, sizeof(data));
309 if (err) {
310 dev_err(dev, "dm_rng_read failed: %d\n", err);
311 return err;
312 }
313 err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", &data, sizeof(data));
314 if (err < 0)
315 printf("WARNING: could not set kaslr-seed %s.\n", fdt_strerror(err));
316
317 return err;
318}
319
Niko Maunof0b21eb2021-02-22 19:18:51 +0000320/**
321 * board_fdt_chosen_bootargs - boards may override this function to use
322 * alternative kernel command line arguments
323 */
324__weak char *board_fdt_chosen_bootargs(void)
325{
326 return env_get("bootargs");
327}
328
Masahiro Yamadabc6ed0f2014-04-18 17:41:00 +0900329int fdt_chosen(void *fdt)
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400330{
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +0200331 struct abuf buf = {};
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400332 int nodeoffset;
333 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400334 char *str; /* used to set string properties */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400335
336 err = fdt_check_header(fdt);
337 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400338 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400339 return err;
340 }
341
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900342 /* find or create "/chosen" node. */
343 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
344 if (nodeoffset < 0)
345 return nodeoffset;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400346
Tim Harveyea955ee2024-06-18 14:06:07 -0700347 /* if DM_RNG enabled automatically inject kaslr-seed node unless:
348 * CONFIG_MEASURED_BOOT enabled: as dt modifications break measured boot
349 * CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT enabled: as that implementation does not use dm yet
350 */
351 if (IS_ENABLED(CONFIG_DM_RNG) &&
352 !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
353 !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
354 fdt_kaslrseed(fdt, false);
355
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +0200356 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
357 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
358 abuf_data(&buf), abuf_size(&buf));
359 abuf_uninit(&buf);
360 if (err < 0) {
361 printf("WARNING: could not set rng-seed %s.\n",
362 fdt_strerror(err));
363 return err;
364 }
365 }
366
Niko Maunof0b21eb2021-02-22 19:18:51 +0000367 str = board_fdt_chosen_bootargs();
368
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900369 if (str) {
370 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
371 strlen(str) + 1);
372 if (err < 0) {
Masahiro Yamadabc6ed0f2014-04-18 17:41:00 +0900373 printf("WARNING: could not set bootargs %s.\n",
374 fdt_strerror(err));
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900375 return err;
376 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400377 }
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500378
Francesco Dolcini622ecee2022-05-19 16:22:26 +0200379 /* add u-boot version */
380 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
381 strlen(PLAIN_VERSION) + 1);
382 if (err < 0) {
383 printf("WARNING: could not set u-boot,version %s.\n",
384 fdt_strerror(err));
385 return err;
386 }
387
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900388 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400389}
390
Kumar Galae93becf2007-11-03 19:46:28 -0500391void do_fixup_by_path(void *fdt, const char *path, const char *prop,
392 const void *val, int len, int create)
393{
394#if defined(DEBUG)
395 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600396 debug("Updating property '%s/%s' = ", path, prop);
Kumar Galae93becf2007-11-03 19:46:28 -0500397 for (i = 0; i < len; i++)
398 debug(" %.2x", *(u8*)(val+i));
399 debug("\n");
400#endif
401 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
402 if (rc)
403 printf("Unable to update property %s:%s, err=%s\n",
404 path, prop, fdt_strerror(rc));
405}
406
407void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
408 u32 val, int create)
409{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000410 fdt32_t tmp = cpu_to_fdt32(val);
411 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Galae93becf2007-11-03 19:46:28 -0500412}
413
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600414void do_fixup_by_prop(void *fdt,
415 const char *pname, const void *pval, int plen,
416 const char *prop, const void *val, int len,
417 int create)
418{
419 int off;
420#if defined(DEBUG)
421 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600422 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600423 for (i = 0; i < len; i++)
424 debug(" %.2x", *(u8*)(val+i));
425 debug("\n");
426#endif
427 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
428 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000429 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600430 fdt_setprop(fdt, off, prop, val, len);
431 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
432 }
433}
434
435void do_fixup_by_prop_u32(void *fdt,
436 const char *pname, const void *pval, int plen,
437 const char *prop, u32 val, int create)
438{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000439 fdt32_t tmp = cpu_to_fdt32(val);
440 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600441}
442
443void do_fixup_by_compat(void *fdt, const char *compat,
444 const char *prop, const void *val, int len, int create)
445{
446 int off = -1;
447#if defined(DEBUG)
448 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600449 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600450 for (i = 0; i < len; i++)
451 debug(" %.2x", *(u8*)(val+i));
452 debug("\n");
453#endif
Marek Behún3058e282022-01-20 01:04:42 +0100454 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000455 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600456 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600457}
458
459void do_fixup_by_compat_u32(void *fdt, const char *compat,
460 const char *prop, u32 val, int create)
461{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000462 fdt32_t tmp = cpu_to_fdt32(val);
463 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600464}
465
Masahiro Yamada63c09412016-11-26 11:02:10 +0900466#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900467/*
468 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
469 */
Simon Glass41f09bb2014-10-23 18:58:55 -0600470static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
471 int n)
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900472{
473 int i;
Hans de Goedeffccb842014-11-28 14:23:51 +0100474 int address_cells = fdt_address_cells(fdt, 0);
475 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900476 char *p = buf;
477
478 for (i = 0; i < n; i++) {
Hans de Goedeffccb842014-11-28 14:23:51 +0100479 if (address_cells == 2)
Sam Protsenko16b80c92024-03-29 19:55:53 -0500480 put_unaligned_be64(address[i], p);
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900481 else
482 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goedeffccb842014-11-28 14:23:51 +0100483 p += 4 * address_cells;
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900484
Hans de Goedeffccb842014-11-28 14:23:51 +0100485 if (size_cells == 2)
Sam Protsenko16b80c92024-03-29 19:55:53 -0500486 put_unaligned_be64(size[i], p);
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900487 else
488 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goedeffccb842014-11-28 14:23:51 +0100489 p += 4 * size_cells;
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900490 }
491
492 return p - (char *)buf;
493}
494
Ramon Fried6b6f2162018-08-14 00:35:42 +0300495#if CONFIG_NR_DRAM_BANKS > 4
496#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
497#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000498#define MEMORY_BANKS_MAX 4
Ramon Fried6b6f2162018-08-14 00:35:42 +0300499#endif
Michal Simek5e1a3be2021-08-10 09:21:54 +0200500
501/**
502 * fdt_fixup_memory_banks - Update DT memory node
503 * @blob: Pointer to DT blob
504 * @start: Pointer to memory start addresses array
505 * @size: Pointer to memory sizes array
506 * @banks: Number of memory banks
507 *
508 * Return: 0 on success, negative value on failure
509 *
510 * Based on the passed number of banks and arrays, the function is able to
511 * update existing DT memory nodes to match run time detected/changed memory
512 * configuration. Implementation is handling one specific case with only one
513 * memory node where multiple tuples could be added/updated.
514 * The case where multiple memory nodes with a single tuple (base, size) are
515 * used, this function is only updating the first memory node without removing
516 * others.
517 */
John Rigbya6bd9e82010-10-13 13:57:33 -0600518int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
519{
520 int err, nodeoffset;
Thierry Reding6d29cc72018-01-30 11:34:17 +0100521 int len, i;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000522 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala3c927282007-11-26 14:57:45 -0600523
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000524 if (banks > MEMORY_BANKS_MAX) {
525 printf("%s: num banks %d exceeds hardcoded limit %d."
526 " Recompile with higher MEMORY_BANKS_MAX?\n",
527 __FUNCTION__, banks, MEMORY_BANKS_MAX);
528 return -1;
529 }
530
Kumar Gala3c927282007-11-26 14:57:45 -0600531 err = fdt_check_header(blob);
532 if (err < 0) {
533 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
534 return err;
535 }
536
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900537 /* find or create "/memory" node. */
538 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
539 if (nodeoffset < 0)
Miao Yan35940de2013-11-28 17:51:39 +0800540 return nodeoffset;
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900541
Kumar Gala3c927282007-11-26 14:57:45 -0600542 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
543 sizeof("memory"));
544 if (err < 0) {
545 printf("WARNING: could not set %s %s.\n", "device_type",
546 fdt_strerror(err));
547 return err;
548 }
549
Thierry Redinged5af032018-02-15 19:05:59 +0100550 for (i = 0; i < banks; i++) {
551 if (start[i] == 0 && size[i] == 0)
552 break;
553 }
554
555 banks = i;
556
Andre Przywara5c1cf892015-06-21 00:29:54 +0100557 if (!banks)
558 return 0;
559
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900560 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala3c927282007-11-26 14:57:45 -0600561
562 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
563 if (err < 0) {
564 printf("WARNING: could not set %s %s.\n",
565 "reg", fdt_strerror(err));
566 return err;
567 }
568 return 0;
569}
Igor Opaniuk949b5a92019-12-03 14:04:46 +0200570
571int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
572{
573 int err, nodeoffset;
574 int len;
575 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
576
577 if (areas > 8) {
578 printf("%s: num areas %d exceeds hardcoded limit %d\n",
579 __func__, areas, 8);
580 return -1;
581 }
582
583 err = fdt_check_header(blob);
584 if (err < 0) {
585 printf("%s: %s\n", __func__, fdt_strerror(err));
586 return err;
587 }
588
589 /* find or create "/memory" node. */
590 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
591 if (nodeoffset < 0)
592 return nodeoffset;
593
594 len = fdt_pack_reg(blob, tmp, start, size, areas);
595
596 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
597 if (err < 0) {
598 printf("WARNING: could not set %s %s.\n",
599 "reg", fdt_strerror(err));
600 return err;
601 }
602
603 return 0;
604}
Masahiro Yamada63c09412016-11-26 11:02:10 +0900605#endif
Kumar Gala3c927282007-11-26 14:57:45 -0600606
John Rigbya6bd9e82010-10-13 13:57:33 -0600607int fdt_fixup_memory(void *blob, u64 start, u64 size)
608{
609 return fdt_fixup_memory_banks(blob, &start, &size, 1);
610}
611
Kumar Galaba37aa02008-08-19 15:41:18 -0500612void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600613{
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530614 int i = 0, j, prop;
Bin Mengbc393a72015-11-03 04:24:41 -0800615 char *tmp, *end;
Stephen Warren064d55f2013-05-27 18:01:19 +0000616 char mac[16];
Kumar Galaab544632007-11-21 11:11:03 -0600617 const char *path;
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100618 unsigned char mac_addr[ARP_HLEN];
Bin Mengbc393a72015-11-03 04:24:41 -0800619 int offset;
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530620#ifdef FDT_SEQ_MACADDR_FROM_ENV
621 int nodeoff;
622 const struct fdt_property *fdt_prop;
623#endif
Kumar Galaab544632007-11-21 11:11:03 -0600624
Lev Iserovicha434fd12016-01-07 18:04:16 -0500625 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galaba37aa02008-08-19 15:41:18 -0500626 return;
627
Lev Iserovicha434fd12016-01-07 18:04:16 -0500628 /* Cycle through all aliases */
629 for (prop = 0; ; prop++) {
Bin Mengbc393a72015-11-03 04:24:41 -0800630 const char *name;
Bin Mengbc393a72015-11-03 04:24:41 -0800631
Lev Iserovicha434fd12016-01-07 18:04:16 -0500632 /* FDT might have been edited, recompute the offset */
633 offset = fdt_first_property_offset(fdt,
634 fdt_path_offset(fdt, "/aliases"));
635 /* Select property number 'prop' */
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530636 for (j = 0; j < prop; j++)
Lev Iserovicha434fd12016-01-07 18:04:16 -0500637 offset = fdt_next_property_offset(fdt, offset);
638
639 if (offset < 0)
640 break;
641
Bin Mengbc393a72015-11-03 04:24:41 -0800642 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynenf8e57c62017-03-20 10:04:55 +0200643 if (!strncmp(name, "ethernet", 8)) {
644 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530645 if (!strcmp(name, "ethernet")
646#ifdef FDT_SEQ_MACADDR_FROM_ENV
647 || !strcmp(name, "ethernet0")
648#endif
649 )
Tuomas Tynkkynenf8e57c62017-03-20 10:04:55 +0200650 i = 0;
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530651#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynenf8e57c62017-03-20 10:04:55 +0200652 else
653 i = trailing_strtol(name);
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530654#endif
Bin Mengbc393a72015-11-03 04:24:41 -0800655 if (i != -1) {
656 if (i == 0)
657 strcpy(mac, "ethaddr");
658 else
659 sprintf(mac, "eth%daddr", i);
660 } else {
661 continue;
662 }
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530663#ifdef FDT_SEQ_MACADDR_FROM_ENV
664 nodeoff = fdt_path_offset(fdt, path);
665 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
666 NULL);
667 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
668 continue;
669 i++;
670#endif
Simon Glass00caae62017-08-03 12:22:12 -0600671 tmp = env_get(mac);
Bin Mengbc393a72015-11-03 04:24:41 -0800672 if (!tmp)
673 continue;
674
675 for (j = 0; j < 6; j++) {
676 mac_addr[j] = tmp ?
Simon Glass7e5f4602021-07-24 09:03:29 -0600677 hextoul(tmp, &end) : 0;
Bin Mengbc393a72015-11-03 04:24:41 -0800678 if (tmp)
679 tmp = (*end) ? end + 1 : end;
680 }
681
682 do_fixup_by_path(fdt, path, "mac-address",
683 &mac_addr, 6, 0);
684 do_fixup_by_path(fdt, path, "local-mac-address",
685 &mac_addr, 6, 1);
Dan Murphyb1f49ab2013-10-02 14:00:15 -0500686 }
Kumar Galaab544632007-11-21 11:11:03 -0600687 }
688}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300689
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100690int fdt_record_loadable(void *blob, u32 index, const char *name,
691 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simekbe2d1a82021-05-27 11:40:09 +0200692 const char *type, const char *os, const char *arch)
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100693{
694 int err, node;
695
696 err = fdt_check_header(blob);
697 if (err < 0) {
698 printf("%s: %s\n", __func__, fdt_strerror(err));
699 return err;
700 }
701
702 /* find or create "/fit-images" node */
703 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
704 if (node < 0)
705 return node;
706
707 /* find or create "/fit-images/<name>" node */
708 node = fdt_find_or_add_subnode(blob, node, name);
709 if (node < 0)
710 return node;
711
Michal Simek13d1ca82020-09-03 12:44:51 +0200712 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100713 if (entry_point != -1)
Michal Simek13d1ca82020-09-03 12:44:51 +0200714 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100715 fdt_setprop_u32(blob, node, "size", size);
716 if (type)
717 fdt_setprop_string(blob, node, "type", type);
718 if (os)
719 fdt_setprop_string(blob, node, "os", os);
Michal Simekbe2d1a82021-05-27 11:40:09 +0200720 if (arch)
721 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100722
723 return node;
724}
725
Hannes Schmelzeref476832016-09-20 18:10:43 +0200726int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Gala3082d232008-08-15 08:24:42 -0500727{
728 int i;
729 uint64_t addr, size;
730 int total, ret;
731 uint actualsize;
Simon Goldschmidt59bd7962019-05-03 21:19:03 +0200732 int fdt_memrsv = 0;
Kumar Gala3082d232008-08-15 08:24:42 -0500733
734 if (!blob)
735 return 0;
736
737 total = fdt_num_mem_rsv(blob);
738 for (i = 0; i < total; i++) {
739 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glass92549352011-09-17 06:48:58 +0000740 if (addr == (uintptr_t)blob) {
Kumar Gala3082d232008-08-15 08:24:42 -0500741 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt59bd7962019-05-03 21:19:03 +0200742 fdt_memrsv = 1;
Kumar Gala3082d232008-08-15 08:24:42 -0500743 break;
744 }
745 }
746
Peter Korsgaardf242a082008-10-28 08:26:52 +0100747 /*
748 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200749 * plus the size needed for 5 fdt_add_mem_rsv, one
750 * for the fdt itself and 4 for a possible initrd
751 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100752 */
Kumar Gala3082d232008-08-15 08:24:42 -0500753 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200754 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500755
Hannes Schmelzeref476832016-09-20 18:10:43 +0200756 actualsize += extrasize;
Kumar Gala3082d232008-08-15 08:24:42 -0500757 /* Make it so the fdt ends on a page boundary */
Simon Glass92549352011-09-17 06:48:58 +0000758 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
759 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Gala3082d232008-08-15 08:24:42 -0500760
761 /* Change the fdt header to reflect the correct size */
762 fdt_set_totalsize(blob, actualsize);
763
Simon Goldschmidt59bd7962019-05-03 21:19:03 +0200764 if (fdt_memrsv) {
765 /* Add the new reservation */
766 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
767 if (ret < 0)
768 return ret;
769 }
Kumar Gala3082d232008-08-15 08:24:42 -0500770
771 return actualsize;
772}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500773
Marek Behún574506c2021-11-26 14:57:15 +0100774/**
775 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
776 *
777 * @blob: ptr to device tree
778 */
779int fdt_delete_disabled_nodes(void *blob)
780{
781 while (1) {
782 int ret, offset;
783
784 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
785 "disabled", 9);
786 if (offset < 0)
787 break;
788
789 ret = fdt_del_node(blob, offset);
790 if (ret < 0)
791 return ret;
792 }
793
794 return 0;
795}
796
Kumar Gala8ab451c2008-10-22 23:33:56 -0500797#ifdef CONFIG_PCI
Tom Riniecc8d422022-11-16 13:10:33 -0500798#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500799
800#define FDT_PCI_PREFETCH (0x40000000)
801#define FDT_PCI_MEM32 (0x02000000)
802#define FDT_PCI_IO (0x01000000)
803#define FDT_PCI_MEM64 (0x03000000)
804
805int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
806
807 int addrcell, sizecell, len, r;
808 u32 *dma_range;
809 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Riniecc8d422022-11-16 13:10:33 -0500810 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Gala8ab451c2008-10-22 23:33:56 -0500811
812 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
813 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
814
815 dma_range = &dma_ranges[0];
816 for (r = 0; r < hose->region_count; r++) {
817 u64 bus_start, phys_start, size;
818
Kumar Galaff4e66e2009-02-06 09:49:31 -0600819 /* skip if !PCI_REGION_SYS_MEMORY */
820 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500821 continue;
822
823 bus_start = (u64)hose->regions[r].bus_start;
824 phys_start = (u64)hose->regions[r].phys_start;
825 size = (u64)hose->regions[r].size;
826
827 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500828 if (size >= 0x100000000ull)
Marek Vasut867aaf62019-07-09 02:49:29 +0200829 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500830 else
Marek Vasut867aaf62019-07-09 02:49:29 +0200831 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500832 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut867aaf62019-07-09 02:49:29 +0200833 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500834#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut867aaf62019-07-09 02:49:29 +0200835 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500836#else
837 dma_range[1] = 0;
838#endif
Marek Vasut867aaf62019-07-09 02:49:29 +0200839 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500840
841 if (addrcell == 2) {
Marek Vasut867aaf62019-07-09 02:49:29 +0200842 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
843 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500844 } else {
Marek Vasut867aaf62019-07-09 02:49:29 +0200845 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500846 }
847
848 if (sizecell == 2) {
Marek Vasut867aaf62019-07-09 02:49:29 +0200849 dma_range[3 + addrcell + 0] =
850 cpu_to_fdt32(size >> 32);
851 dma_range[3 + addrcell + 1] =
852 cpu_to_fdt32(size & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500853 } else {
Marek Vasut867aaf62019-07-09 02:49:29 +0200854 dma_range[3 + addrcell + 0] =
855 cpu_to_fdt32(size & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500856 }
857
858 dma_range += (3 + addrcell + sizecell);
859 }
860
861 len = dma_range - &dma_ranges[0];
862 if (len)
863 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
864
865 return 0;
866}
867#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200868
Matthew McClintockcb2707a2010-10-13 13:39:26 +0200869int fdt_increase_size(void *fdt, int add_len)
870{
871 int newlen;
872
873 newlen = fdt_totalsize(fdt) + add_len;
874
875 /* Open in place with a new len */
876 return fdt_open_into(fdt, fdt, newlen);
877}
878
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100879#ifdef CONFIG_FDT_FIXUP_PARTITIONS
880#include <jffs2/load_kernel.h>
881#include <mtd_node.h>
882
Michal Simekcd1457d2018-07-31 14:31:04 +0200883static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100884{
885 int off, ndepth;
886 int ret;
887
888 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
889 (off >= 0) && (ndepth > 0);
890 off = fdt_next_node(blob, off, &ndepth)) {
891 if (ndepth == 1) {
892 debug("delete %s: offset: %x\n",
893 fdt_get_name(blob, off, 0), off);
894 ret = fdt_del_node((void *)blob, off);
895 if (ret < 0) {
896 printf("Can't delete node: %s\n",
897 fdt_strerror(ret));
898 return ret;
899 } else {
900 ndepth = 0;
901 off = parent_offset;
902 }
903 }
904 }
905 return 0;
906}
907
Michal Simekcd1457d2018-07-31 14:31:04 +0200908static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100909{
910 const void *prop;
911 int ndepth = 0;
912 int off;
913 int ret;
914
915 off = fdt_next_node(blob, parent_offset, &ndepth);
916 if (off > 0 && ndepth == 1) {
917 prop = fdt_getprop(blob, off, "label", NULL);
918 if (prop == NULL) {
919 /*
920 * Could not find label property, nand {}; node?
921 * Check subnode, delete partitions there if any.
922 */
923 return fdt_del_partitions(blob, off);
924 } else {
925 ret = fdt_del_subnodes(blob, parent_offset);
926 if (ret < 0) {
927 printf("Can't remove subnodes: %s\n",
928 fdt_strerror(ret));
929 return ret;
930 }
931 }
932 }
933 return 0;
934}
935
Masahiro Yamada8ce8e422020-07-15 19:35:47 +0900936static int fdt_node_set_part_info(void *blob, int parent_offset,
937 struct mtd_device *dev)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100938{
939 struct list_head *pentry;
940 struct part_info *part;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100941 int off, ndepth = 0;
942 int part_num, ret;
Stefan Mavrodiev3a9a62a2019-04-24 08:31:54 +0300943 int sizecell;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100944 char buf[64];
945
946 ret = fdt_del_partitions(blob, parent_offset);
947 if (ret < 0)
948 return ret;
949
950 /*
Stefan Mavrodiev3a9a62a2019-04-24 08:31:54 +0300951 * Check if size/address is 1 or 2 cells.
952 * We assume #address-cells and #size-cells have same value.
953 */
954 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
955 0, "#size-cells", 1);
956
957 /*
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100958 * Check if it is nand {}; subnode, adjust
959 * the offset in this case
960 */
961 off = fdt_next_node(blob, parent_offset, &ndepth);
962 if (off > 0 && ndepth == 1)
963 parent_offset = off;
964
965 part_num = 0;
966 list_for_each_prev(pentry, &dev->parts) {
967 int newoff;
968
969 part = list_entry(pentry, struct part_info, link);
970
Scott Wood06503f12013-10-15 17:41:27 -0500971 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100972 part_num, part->name, part->size,
973 part->offset, part->mask_flags);
974
Scott Wood06503f12013-10-15 17:41:27 -0500975 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100976add_sub:
977 ret = fdt_add_subnode(blob, parent_offset, buf);
978 if (ret == -FDT_ERR_NOSPACE) {
979 ret = fdt_increase_size(blob, 512);
980 if (!ret)
981 goto add_sub;
982 else
983 goto err_size;
984 } else if (ret < 0) {
985 printf("Can't add partition node: %s\n",
986 fdt_strerror(ret));
987 return ret;
988 }
989 newoff = ret;
990
991 /* Check MTD_WRITEABLE_CMD flag */
992 if (part->mask_flags & 1) {
993add_ro:
994 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
995 if (ret == -FDT_ERR_NOSPACE) {
996 ret = fdt_increase_size(blob, 512);
997 if (!ret)
998 goto add_ro;
999 else
1000 goto err_size;
1001 } else if (ret < 0)
1002 goto err_prop;
1003 }
1004
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001005add_reg:
Stefan Mavrodiev3a9a62a2019-04-24 08:31:54 +03001006 if (sizecell == 2) {
1007 ret = fdt_setprop_u64(blob, newoff,
1008 "reg", part->offset);
1009 if (!ret)
1010 ret = fdt_appendprop_u64(blob, newoff,
1011 "reg", part->size);
1012 } else {
1013 ret = fdt_setprop_u32(blob, newoff,
1014 "reg", part->offset);
1015 if (!ret)
1016 ret = fdt_appendprop_u32(blob, newoff,
1017 "reg", part->size);
1018 }
1019
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001020 if (ret == -FDT_ERR_NOSPACE) {
1021 ret = fdt_increase_size(blob, 512);
1022 if (!ret)
1023 goto add_reg;
1024 else
1025 goto err_size;
1026 } else if (ret < 0)
1027 goto err_prop;
1028
1029add_label:
1030 ret = fdt_setprop_string(blob, newoff, "label", part->name);
1031 if (ret == -FDT_ERR_NOSPACE) {
1032 ret = fdt_increase_size(blob, 512);
1033 if (!ret)
1034 goto add_label;
1035 else
1036 goto err_size;
1037 } else if (ret < 0)
1038 goto err_prop;
1039
1040 part_num++;
1041 }
1042 return 0;
1043err_size:
1044 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1045 return ret;
1046err_prop:
1047 printf("Can't add property: %s\n", fdt_strerror(ret));
1048 return ret;
1049}
1050
1051/*
1052 * Update partitions in nor/nand nodes using info from
1053 * mtdparts environment variable. The nodes to update are
1054 * specified by node_info structure which contains mtd device
1055 * type and compatible string: E. g. the board code in
1056 * ft_board_setup() could use:
1057 *
1058 * struct node_info nodes[] = {
1059 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1060 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1061 * };
1062 *
1063 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1064 */
Masahiro Yamada5f4e32d2018-07-19 16:28:22 +09001065void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1066 int node_info_size)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001067{
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001068 struct mtd_device *dev;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001069 int i, idx;
Matthias Schiffer36fee2f2022-02-03 15:14:47 +01001070 int noff, parts;
Masahiro Yamada53a89662020-07-17 10:46:18 +09001071 bool inited = false;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001072
1073 for (i = 0; i < node_info_size; i++) {
1074 idx = 0;
Masahiro Yamada7d8073e2020-07-17 10:46:19 +09001075
Marek Behún3058e282022-01-20 01:04:42 +01001076 fdt_for_each_node_by_compatible(noff, blob, -1,
1077 node_info[i].compat) {
Masahiro Yamada7d8073e2020-07-17 10:46:19 +09001078 const char *prop;
1079
1080 prop = fdt_getprop(blob, noff, "status", NULL);
1081 if (prop && !strcmp(prop, "disabled"))
1082 continue;
1083
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001084 debug("%s: %s, mtd dev type %d\n",
1085 fdt_get_name(blob, noff, 0),
Masahiro Yamada5f4e32d2018-07-19 16:28:22 +09001086 node_info[i].compat, node_info[i].type);
Masahiro Yamada53a89662020-07-17 10:46:18 +09001087
1088 if (!inited) {
1089 if (mtdparts_init() != 0)
1090 return;
1091 inited = true;
1092 }
1093
Masahiro Yamada5f4e32d2018-07-19 16:28:22 +09001094 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001095 if (dev) {
Matthias Schiffer36fee2f2022-02-03 15:14:47 +01001096 parts = fdt_subnode_offset(blob, noff,
1097 "partitions");
1098 if (parts < 0)
1099 parts = noff;
1100
1101 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001102 return; /* return on error */
1103 }
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001104 }
1105 }
1106}
1107#endif
Kumar Gala49b97d92010-03-30 10:19:26 -05001108
Patrick Delaunay163c5f62023-06-08 17:16:38 +02001109int fdt_copy_fixed_partitions(void *blob)
1110{
1111 ofnode node, subnode;
1112 int off, suboff, res;
1113 char path[256];
1114 int address_cells, size_cells;
1115 u8 i, j, child_count;
1116
1117 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1118 while (ofnode_valid(node)) {
1119 /* copy the U-Boot fixed partition */
1120 address_cells = ofnode_read_simple_addr_cells(node);
1121 size_cells = ofnode_read_simple_size_cells(node);
1122
1123 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1124 if (res)
1125 return res;
1126
1127 off = fdt_path_offset(blob, path);
1128 if (off < 0)
1129 return -ENODEV;
1130
1131 off = fdt_find_or_add_subnode(blob, off, "partitions");
1132 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1133 if (res)
1134 return res;
1135
1136 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1137 if (res)
1138 return res;
1139
1140 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1141 if (res)
1142 return res;
1143
1144 /*
1145 * parse partition in reverse order as fdt_find_or_add_subnode() only
1146 * insert the new node after the parent's properties
1147 */
1148 child_count = ofnode_get_child_count(node);
1149 for (i = child_count; i > 0 ; i--) {
1150 subnode = ofnode_first_subnode(node);
1151 if (!ofnode_valid(subnode))
1152 break;
1153
1154 for (j = 0; (j < i - 1); j++)
1155 subnode = ofnode_next_subnode(subnode);
1156
1157 if (!ofnode_valid(subnode))
1158 break;
1159
1160 const u32 *reg;
1161 int len;
1162
1163 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1164 res = fdt_setprop_string(blob, suboff, "label",
1165 ofnode_read_string(subnode, "label"));
1166 if (res)
1167 return res;
1168
1169 reg = ofnode_get_property(subnode, "reg", &len);
1170 res = fdt_setprop(blob, suboff, "reg", reg, len);
1171 if (res)
1172 return res;
1173 }
1174
1175 /* go to next fixed-partitions node */
1176 node = ofnode_by_compatible(node, "fixed-partitions");
1177 }
1178
1179 return 0;
1180}
1181
Kumar Gala49b97d92010-03-30 10:19:26 -05001182void fdt_del_node_and_alias(void *blob, const char *alias)
1183{
1184 int off = fdt_path_offset(blob, alias);
1185
1186 if (off < 0)
1187 return;
1188
1189 fdt_del_node(blob, off);
1190
1191 off = fdt_path_offset(blob, "/aliases");
1192 fdt_delprop(blob, off, alias);
1193}
Kumar Galaa0342c02010-06-16 14:27:38 -05001194
Kumar Galaa0342c02010-06-16 14:27:38 -05001195/* Max address size we deal with */
1196#define OF_MAX_ADDR_CELLS 4
Dario Binacchia47abd72021-05-01 17:05:26 +02001197#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1198 (ns) > 0)
Kumar Galaa0342c02010-06-16 14:27:38 -05001199
1200/* Debug utility */
1201#ifdef DEBUG
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001202static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -05001203{
1204 printf("%s", s);
1205 while(na--)
1206 printf(" %08x", *(addr++));
1207 printf("\n");
1208}
1209#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001210static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galaa0342c02010-06-16 14:27:38 -05001211#endif
1212
Paul Burton0a222d52016-05-17 07:43:24 +01001213/**
1214 * struct of_bus - Callbacks for bus specific translators
Paul Burton49717b12016-05-17 07:43:25 +01001215 * @name: A string used to identify this bus in debug output.
1216 * @addresses: The name of the DT property from which addresses are
1217 * to be read, typically "reg".
Paul Burton0a222d52016-05-17 07:43:24 +01001218 * @match: Return non-zero if the node whose parent is at
1219 * parentoffset in the FDT blob corresponds to a bus
1220 * of this type, otherwise return zero. If NULL a match
1221 * is assumed.
Paul Burton49717b12016-05-17 07:43:25 +01001222 * @count_cells:Count how many cells (be32 values) a node whose parent
1223 * is at parentoffset in the FDT blob will require to
1224 * represent its address (written to *addrc) & size
1225 * (written to *sizec).
1226 * @map: Map the address addr from the address space of this
1227 * bus to that of its parent, making use of the ranges
1228 * read from DT to an array at range. na and ns are the
1229 * number of cells (be32 values) used to hold and address
1230 * or size, respectively, for this bus. pna is the number
1231 * of cells used to hold an address for the parent bus.
1232 * Returns the address in the address space of the parent
1233 * bus.
1234 * @translate: Update the value of the address cells at addr within an
1235 * FDT by adding offset to it. na specifies the number of
1236 * cells used to hold the address being translated. Returns
1237 * zero on success, non-zero on error.
Paul Burton0a222d52016-05-17 07:43:24 +01001238 *
1239 * Each bus type will include a struct of_bus in the of_busses array,
1240 * providing implementations of some or all of the functions used to
1241 * match the bus & handle address translation for its children.
1242 */
Kumar Galaa0342c02010-06-16 14:27:38 -05001243struct of_bus {
1244 const char *name;
1245 const char *addresses;
Stephen Warren11e44fc2016-08-05 09:47:50 -06001246 int (*match)(const void *blob, int parentoffset);
1247 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -05001248 int *addrc, int *sizec);
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001249 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -05001250 int na, int ns, int pna);
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001251 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001252};
1253
1254/* Default translator (generic bus) */
Simon Glasseed36602017-05-18 20:09:26 -06001255void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -05001256 int *addrc, int *sizec)
1257{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001258 const fdt32_t *prop;
Scott Wood6395f312010-08-12 18:37:39 -05001259
Simon Glass933cdbb2014-10-23 18:58:57 -06001260 if (addrc)
1261 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood6395f312010-08-12 18:37:39 -05001262
1263 if (sizec) {
1264 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1265 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001266 *sizec = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -05001267 else
1268 *sizec = 1;
1269 }
Kumar Galaa0342c02010-06-16 14:27:38 -05001270}
1271
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001272static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -05001273 int na, int ns, int pna)
1274{
1275 u64 cp, s, da;
1276
Simon Glasseed36602017-05-18 20:09:26 -06001277 cp = fdt_read_number(range, na);
1278 s = fdt_read_number(range + na + pna, ns);
1279 da = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001280
Sekhar Norid8e9cf42018-12-06 15:20:47 +05301281 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galaa0342c02010-06-16 14:27:38 -05001282
1283 if (da < cp || da >= (cp + s))
1284 return OF_BAD_ADDR;
1285 return da - cp;
1286}
1287
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001288static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -05001289{
Simon Glasseed36602017-05-18 20:09:26 -06001290 u64 a = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001291 memset(addr, 0, na * 4);
1292 a += offset;
1293 if (na > 1)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001294 addr[na - 2] = cpu_to_fdt32(a >> 32);
1295 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galaa0342c02010-06-16 14:27:38 -05001296
1297 return 0;
1298}
1299
Paul Burton0a222d52016-05-17 07:43:24 +01001300#ifdef CONFIG_OF_ISA_BUS
1301
1302/* ISA bus translator */
Stephen Warren11e44fc2016-08-05 09:47:50 -06001303static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burton0a222d52016-05-17 07:43:24 +01001304{
1305 const char *name;
1306
1307 name = fdt_get_name(blob, parentoffset, NULL);
1308 if (!name)
1309 return 0;
1310
1311 return !strcmp(name, "isa");
1312}
1313
Stephen Warren11e44fc2016-08-05 09:47:50 -06001314static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burton0a222d52016-05-17 07:43:24 +01001315 int *addrc, int *sizec)
1316{
1317 if (addrc)
1318 *addrc = 2;
1319 if (sizec)
1320 *sizec = 1;
1321}
1322
1323static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1324 int na, int ns, int pna)
1325{
1326 u64 cp, s, da;
1327
1328 /* Check address type match */
1329 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1330 return OF_BAD_ADDR;
1331
Simon Glasseed36602017-05-18 20:09:26 -06001332 cp = fdt_read_number(range + 1, na - 1);
1333 s = fdt_read_number(range + na + pna, ns);
1334 da = fdt_read_number(addr + 1, na - 1);
Paul Burton0a222d52016-05-17 07:43:24 +01001335
Sekhar Norid8e9cf42018-12-06 15:20:47 +05301336 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burton0a222d52016-05-17 07:43:24 +01001337
1338 if (da < cp || da >= (cp + s))
1339 return OF_BAD_ADDR;
1340 return da - cp;
1341}
1342
1343static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1344{
1345 return of_bus_default_translate(addr + 1, offset, na - 1);
1346}
1347
1348#endif /* CONFIG_OF_ISA_BUS */
1349
Kumar Galaa0342c02010-06-16 14:27:38 -05001350/* Array of bus specific translators */
1351static struct of_bus of_busses[] = {
Paul Burton0a222d52016-05-17 07:43:24 +01001352#ifdef CONFIG_OF_ISA_BUS
1353 /* ISA */
1354 {
1355 .name = "isa",
1356 .addresses = "reg",
1357 .match = of_bus_isa_match,
1358 .count_cells = of_bus_isa_count_cells,
1359 .map = of_bus_isa_map,
1360 .translate = of_bus_isa_translate,
1361 },
1362#endif /* CONFIG_OF_ISA_BUS */
Kumar Galaa0342c02010-06-16 14:27:38 -05001363 /* Default */
1364 {
1365 .name = "default",
1366 .addresses = "reg",
Simon Glasseed36602017-05-18 20:09:26 -06001367 .count_cells = fdt_support_default_count_cells,
Kumar Galaa0342c02010-06-16 14:27:38 -05001368 .map = of_bus_default_map,
1369 .translate = of_bus_default_translate,
1370 },
1371};
1372
Stephen Warren11e44fc2016-08-05 09:47:50 -06001373static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burton0a222d52016-05-17 07:43:24 +01001374{
1375 struct of_bus *bus;
1376
1377 if (ARRAY_SIZE(of_busses) == 1)
1378 return of_busses;
1379
1380 for (bus = of_busses; bus; bus++) {
1381 if (!bus->match || bus->match(blob, parentoffset))
1382 return bus;
1383 }
1384
1385 /*
1386 * We should always have matched the default bus at least, since
1387 * it has a NULL match field. If we didn't then it somehow isn't
1388 * in the of_busses array or something equally catastrophic has
1389 * gone wrong.
1390 */
1391 assert(0);
1392 return NULL;
1393}
1394
Stephen Warren11e44fc2016-08-05 09:47:50 -06001395static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001396 struct of_bus *pbus, fdt32_t *addr,
Kumar Galaa0342c02010-06-16 14:27:38 -05001397 int na, int ns, int pna, const char *rprop)
1398{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001399 const fdt32_t *ranges;
Kumar Galaa0342c02010-06-16 14:27:38 -05001400 int rlen;
1401 int rone;
1402 u64 offset = OF_BAD_ADDR;
1403
1404 /* Normally, an absence of a "ranges" property means we are
1405 * crossing a non-translatable boundary, and thus the addresses
1406 * below the current not cannot be converted to CPU physical ones.
1407 * Unfortunately, while this is very clear in the spec, it's not
1408 * what Apple understood, and they do have things like /uni-n or
1409 * /ht nodes with no "ranges" property and a lot of perfectly
1410 * useable mapped devices below them. Thus we treat the absence of
1411 * "ranges" as equivalent to an empty "ranges" property which means
1412 * a 1:1 translation at that level. It's up to the caller not to try
1413 * to translate addresses that aren't supposed to be translated in
1414 * the first place. --BenH.
1415 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001416 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galaa0342c02010-06-16 14:27:38 -05001417 if (ranges == NULL || rlen == 0) {
Simon Glasseed36602017-05-18 20:09:26 -06001418 offset = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001419 memset(addr, 0, pna * 4);
1420 debug("OF: no ranges, 1:1 translation\n");
1421 goto finish;
1422 }
1423
1424 debug("OF: walking ranges...\n");
1425
1426 /* Now walk through the ranges */
1427 rlen /= 4;
1428 rone = na + pna + ns;
1429 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1430 offset = bus->map(addr, ranges, na, ns, pna);
1431 if (offset != OF_BAD_ADDR)
1432 break;
1433 }
1434 if (offset == OF_BAD_ADDR) {
1435 debug("OF: not found !\n");
1436 return 1;
1437 }
1438 memcpy(addr, ranges + na, 4 * pna);
1439
1440 finish:
1441 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001442 debug("OF: with offset: %llu\n", offset);
Kumar Galaa0342c02010-06-16 14:27:38 -05001443
1444 /* Translate it into parent bus space */
1445 return pbus->translate(addr, offset, pna);
1446}
1447
1448/*
1449 * Translate an address from the device-tree into a CPU physical address,
1450 * this walks up the tree and applies the various bus mappings on the
1451 * way.
1452 *
1453 * Note: We consider that crossing any level with #size-cells == 0 to mean
1454 * that translation is impossible (that is we are not dealing with a value
1455 * that can be mapped to a cpu physical address). This is not really specified
1456 * that way, but this is traditionally the way IBM at least do things
1457 */
Stephen Warren11e44fc2016-08-05 09:47:50 -06001458static u64 __of_translate_address(const void *blob, int node_offset,
1459 const fdt32_t *in_addr, const char *rprop)
Kumar Galaa0342c02010-06-16 14:27:38 -05001460{
1461 int parent;
1462 struct of_bus *bus, *pbus;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001463 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galaa0342c02010-06-16 14:27:38 -05001464 int na, ns, pna, pns;
1465 u64 result = OF_BAD_ADDR;
1466
1467 debug("OF: ** translation for device %s **\n",
1468 fdt_get_name(blob, node_offset, NULL));
1469
1470 /* Get parent & match bus type */
1471 parent = fdt_parent_offset(blob, node_offset);
1472 if (parent < 0)
1473 goto bail;
Paul Burton0a222d52016-05-17 07:43:24 +01001474 bus = of_match_bus(blob, parent);
Kumar Galaa0342c02010-06-16 14:27:38 -05001475
1476 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001477 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczak4428f3c2016-01-12 15:40:44 +01001478 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galaa0342c02010-06-16 14:27:38 -05001479 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1480 fdt_get_name(blob, node_offset, NULL));
1481 goto bail;
1482 }
1483 memcpy(addr, in_addr, na * 4);
1484
1485 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1486 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1487 of_dump_addr("OF: translating address:", addr, na);
1488
1489 /* Translate */
1490 for (;;) {
1491 /* Switch to parent bus */
1492 node_offset = parent;
1493 parent = fdt_parent_offset(blob, node_offset);
1494
1495 /* If root, we have finished */
1496 if (parent < 0) {
1497 debug("OF: reached root node\n");
Simon Glasseed36602017-05-18 20:09:26 -06001498 result = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001499 break;
1500 }
1501
1502 /* Get new parent bus and counts */
Paul Burton0a222d52016-05-17 07:43:24 +01001503 pbus = of_match_bus(blob, parent);
Scott Wood6395f312010-08-12 18:37:39 -05001504 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczak4428f3c2016-01-12 15:40:44 +01001505 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galaa0342c02010-06-16 14:27:38 -05001506 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1507 fdt_get_name(blob, node_offset, NULL));
1508 break;
1509 }
1510
1511 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1512 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1513
1514 /* Apply bus translation */
1515 if (of_translate_one(blob, node_offset, bus, pbus,
1516 addr, na, ns, pna, rprop))
1517 break;
1518
1519 /* Complete the move up one level */
1520 na = pna;
1521 ns = pns;
1522 bus = pbus;
1523
1524 of_dump_addr("OF: one level translation:", addr, na);
1525 }
1526 bail:
1527
1528 return result;
1529}
1530
Stephen Warren11e44fc2016-08-05 09:47:50 -06001531u64 fdt_translate_address(const void *blob, int node_offset,
1532 const fdt32_t *in_addr)
Kumar Galaa0342c02010-06-16 14:27:38 -05001533{
1534 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1535}
Kumar Gala75e73af2010-07-04 12:48:21 -05001536
Fabien Dessenne641067f2019-05-31 15:11:30 +02001537u64 fdt_translate_dma_address(const void *blob, int node_offset,
1538 const fdt32_t *in_addr)
1539{
1540 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1541}
1542
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001543int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1544 dma_addr_t *bus, u64 *size)
1545{
1546 bool found_dma_ranges = false;
1547 struct of_bus *bus_node;
1548 const fdt32_t *ranges;
1549 int na, ns, pna, pns;
1550 int parent = node;
1551 int ret = 0;
1552 int len;
1553
1554 /* Find the closest dma-ranges property */
1555 while (parent >= 0) {
1556 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1557
1558 /* Ignore empty ranges, they imply no translation required */
1559 if (ranges && len > 0)
1560 break;
1561
1562 /* Once we find 'dma-ranges', then a missing one is an error */
1563 if (found_dma_ranges && !ranges) {
1564 ret = -EINVAL;
1565 goto out;
1566 }
1567
1568 if (ranges)
1569 found_dma_ranges = true;
1570
1571 parent = fdt_parent_offset(blob, parent);
1572 }
1573
1574 if (!ranges || parent < 0) {
1575 debug("no dma-ranges found for node %s\n",
1576 fdt_get_name(blob, node, NULL));
1577 ret = -ENOENT;
1578 goto out;
1579 }
1580
1581 /* switch to that node */
1582 node = parent;
1583 parent = fdt_parent_offset(blob, node);
1584 if (parent < 0) {
Vagrant Cascadian8c8bf4f2021-12-21 13:06:58 -08001585 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001586 ret = -EINVAL;
1587 goto out;
1588 }
1589
1590 /* Get the address sizes both for the bus and its parent */
1591 bus_node = of_match_bus(blob, node);
1592 bus_node->count_cells(blob, node, &na, &ns);
1593 if (!OF_CHECK_COUNTS(na, ns)) {
1594 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1595 fdt_get_name(blob, node, NULL));
1596 return -EINVAL;
1597 goto out;
1598 }
1599
1600 bus_node = of_match_bus(blob, parent);
1601 bus_node->count_cells(blob, parent, &pna, &pns);
1602 if (!OF_CHECK_COUNTS(pna, pns)) {
1603 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1604 fdt_get_name(blob, parent, NULL));
1605 return -EINVAL;
1606 goto out;
1607 }
1608
1609 *bus = fdt_read_number(ranges, na);
1610 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1611 *size = fdt_read_number(ranges + na + pna, ns);
1612out:
1613 return ret;
1614}
1615
Kumar Gala75e73af2010-07-04 12:48:21 -05001616/**
Hugo Villeneuveded112f2023-04-24 16:51:50 -04001617 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala75e73af2010-07-04 12:48:21 -05001618 * who's reg property matches a physical cpu address
1619 *
1620 * @blob: ptr to device tree
Hugo Villeneuveded112f2023-04-24 16:51:50 -04001621 * @compat: compatible string to match
Kumar Gala75e73af2010-07-04 12:48:21 -05001622 * @compat_off: property name
1623 *
1624 */
1625int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1626 phys_addr_t compat_off)
1627{
Marek Behún3058e282022-01-20 01:04:42 +01001628 int len, off;
1629
1630 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001631 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún3058e282022-01-20 01:04:42 +01001632 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1633 return off;
Kumar Gala75e73af2010-07-04 12:48:21 -05001634 }
1635
1636 return -FDT_ERR_NOTFOUND;
1637}
1638
Marek Behún9ab0c2f2021-11-26 14:57:10 +01001639static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1640{
1641 char path[512];
1642 int len;
1643
1644 len = vsnprintf(path, sizeof(path), fmt, ap);
1645 if (len < 0 || len + 1 > sizeof(path))
1646 return -FDT_ERR_NOSPACE;
1647
1648 return fdt_path_offset(blob, path);
1649}
1650
1651/**
1652 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1653 *
1654 * @blob: ptr to device tree
1655 * @fmt: path format
1656 * @ap: vsnprintf arguments
1657 */
1658int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1659{
1660 va_list ap;
1661 int res;
1662
1663 va_start(ap, fmt);
1664 res = vnode_offset_by_pathf(blob, fmt, ap);
1665 va_end(ap);
1666
1667 return res;
1668}
1669
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001670/*
Kumar Galaf117c0f2011-08-01 00:23:23 -05001671 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001672 *
1673 * @fdt: ptr to device tree
1674 * @nodeoffset: node to update
1675 * @phandle: phandle value to set (must be unique)
Kumar Galaf117c0f2011-08-01 00:23:23 -05001676 */
1677int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001678{
1679 int ret;
1680
1681#ifdef DEBUG
1682 int off = fdt_node_offset_by_phandle(fdt, phandle);
1683
1684 if ((off >= 0) && (off != nodeoffset)) {
1685 char buf[64];
1686
1687 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1688 printf("Trying to update node %s with phandle %u ",
1689 buf, phandle);
1690
1691 fdt_get_path(fdt, off, buf, sizeof(buf));
1692 printf("that already exists in node %s.\n", buf);
1693 return -FDT_ERR_BADPHANDLE;
1694 }
1695#endif
1696
1697 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001698
1699 return ret;
1700}
1701
Kumar Gala10aeabd2011-08-01 00:25:20 -05001702/*
Marek Behúnc92ccba2021-11-26 14:57:09 +01001703 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Gala10aeabd2011-08-01 00:25:20 -05001704 *
1705 * @fdt: ptr to device tree
1706 * @nodeoffset: node to update
1707 */
Timur Tabi3c927cc2011-09-20 18:24:34 -05001708unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Gala10aeabd2011-08-01 00:25:20 -05001709{
1710 /* see if there is a phandle already */
Marek Behún76f5a722021-11-26 14:57:07 +01001711 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Gala10aeabd2011-08-01 00:25:20 -05001712
1713 /* if we got 0, means no phandle so create one */
1714 if (phandle == 0) {
Timur Tabi3c927cc2011-09-20 18:24:34 -05001715 int ret;
1716
Marek Behún76f5a722021-11-26 14:57:07 +01001717 ret = fdt_generate_phandle(fdt, &phandle);
1718 if (ret < 0) {
1719 printf("Can't generate phandle: %s\n",
1720 fdt_strerror(ret));
1721 return 0;
1722 }
1723
Timur Tabi3c927cc2011-09-20 18:24:34 -05001724 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1725 if (ret < 0) {
1726 printf("Can't set phandle %u: %s\n", phandle,
1727 fdt_strerror(ret));
1728 return 0;
1729 }
Kumar Gala10aeabd2011-08-01 00:25:20 -05001730 }
1731
1732 return phandle;
1733}
1734
Marek Behún9ab0c2f2021-11-26 14:57:10 +01001735/**
1736 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1737 * given compatible
1738 *
1739 * @fdt: ptr to device tree
1740 * @compat: node's compatible string
1741 */
1742unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1743{
1744 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1745
1746 if (offset < 0) {
1747 printf("Can't find node with compatible \"%s\": %s\n", compat,
1748 fdt_strerror(offset));
1749 return 0;
1750 }
1751
1752 return fdt_create_phandle(fdt, offset);
1753}
1754
1755/**
1756 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1757 * sprintf-formatted path
1758 *
1759 * @fdt: ptr to device tree
1760 * @fmt, ...: path format string and arguments to pass to sprintf
1761 */
1762unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1763{
1764 va_list ap;
1765 int offset;
1766
1767 va_start(ap, fmt);
1768 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1769 va_end(ap);
1770
1771 if (offset < 0) {
1772 printf("Can't find node by given path: %s\n",
1773 fdt_strerror(offset));
1774 return 0;
1775 }
1776
1777 return fdt_create_phandle(fdt, offset);
1778}
1779
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001780/*
1781 * fdt_set_node_status: Set status for the given node
1782 *
1783 * @fdt: ptr to device tree
1784 * @nodeoffset: node to update
Marek Behún2105cd02021-11-26 14:57:08 +01001785 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001786 */
Marek Behún2105cd02021-11-26 14:57:08 +01001787int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001788{
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001789 int ret = 0;
1790
1791 if (nodeoffset < 0)
1792 return nodeoffset;
1793
1794 switch (status) {
1795 case FDT_STATUS_OKAY:
1796 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1797 break;
1798 case FDT_STATUS_DISABLED:
1799 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1800 break;
1801 case FDT_STATUS_FAIL:
1802 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1803 break;
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001804 default:
1805 printf("Invalid fdt status: %x\n", status);
1806 ret = -1;
1807 break;
1808 }
1809
1810 return ret;
1811}
1812
1813/*
1814 * fdt_set_status_by_alias: Set status for the given node given an alias
1815 *
1816 * @fdt: ptr to device tree
1817 * @alias: alias of node to update
Marek Behún2105cd02021-11-26 14:57:08 +01001818 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001819 */
1820int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behún2105cd02021-11-26 14:57:08 +01001821 enum fdt_status status)
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001822{
1823 int offset = fdt_path_offset(fdt, alias);
1824
Marek Behún2105cd02021-11-26 14:57:08 +01001825 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001826}
1827
Marek Behún9ab0c2f2021-11-26 14:57:10 +01001828/**
1829 * fdt_set_status_by_compatible: Set node status for first node with given
1830 * compatible
1831 *
1832 * @fdt: ptr to device tree
1833 * @compat: node's compatible string
1834 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1835 */
1836int fdt_set_status_by_compatible(void *fdt, const char *compat,
1837 enum fdt_status status)
1838{
1839 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1840
1841 if (offset < 0)
1842 return offset;
1843
1844 return fdt_set_node_status(fdt, offset, status);
1845}
1846
1847/**
1848 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1849 * path
1850 *
1851 * @fdt: ptr to device tree
1852 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1853 * @fmt, ...: path format string and arguments to pass to sprintf
1854 */
1855int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1856 ...)
1857{
1858 va_list ap;
1859 int offset;
1860
1861 va_start(ap, fmt);
1862 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1863 va_end(ap);
1864
1865 if (offset < 0)
1866 return offset;
1867
1868 return fdt_set_node_status(fdt, offset, status);
1869}
1870
Timur Tabibb682002011-05-03 13:24:07 -05001871/*
1872 * Verify the physical address of device tree node for a given alias
1873 *
1874 * This function locates the device tree node of a given alias, and then
1875 * verifies that the physical address of that device matches the given
1876 * parameter. It displays a message if there is a mismatch.
1877 *
1878 * Returns 1 on success, 0 on failure
1879 */
1880int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1881{
1882 const char *path;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001883 const fdt32_t *reg;
Timur Tabibb682002011-05-03 13:24:07 -05001884 int node, len;
1885 u64 dt_addr;
1886
1887 path = fdt_getprop(fdt, anode, alias, NULL);
1888 if (!path) {
1889 /* If there's no such alias, then it's not a failure */
1890 return 1;
1891 }
1892
1893 node = fdt_path_offset(fdt, path);
1894 if (node < 0) {
1895 printf("Warning: device tree alias '%s' points to invalid "
1896 "node %s.\n", alias, path);
1897 return 0;
1898 }
1899
1900 reg = fdt_getprop(fdt, node, "reg", &len);
1901 if (!reg) {
1902 printf("Warning: device tree node '%s' has no address.\n",
1903 path);
1904 return 0;
1905 }
1906
1907 dt_addr = fdt_translate_address(fdt, node, reg);
1908 if (addr != dt_addr) {
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001909 printf("Warning: U-Boot configured device %s at address %llu,\n"
1910 "but the device tree has it address %llx.\n",
1911 alias, addr, dt_addr);
Timur Tabibb682002011-05-03 13:24:07 -05001912 return 0;
1913 }
1914
1915 return 1;
1916}
1917
1918/*
1919 * Returns the base address of an SOC or PCI node
1920 */
Simon Glassec002112017-05-18 20:09:00 -06001921u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabibb682002011-05-03 13:24:07 -05001922{
1923 int size;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001924 const fdt32_t *prop;
Timur Tabibb682002011-05-03 13:24:07 -05001925
Simon Glass336a4482017-07-04 13:31:20 -06001926 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabibb682002011-05-03 13:24:07 -05001927
Masahiro Yamadae3665ba2019-06-27 16:39:57 +09001928 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabibb682002011-05-03 13:24:07 -05001929}
Alexander Grafc48e6862014-04-11 17:09:41 +02001930
1931/*
Bin Menga932aa32021-02-25 17:22:24 +08001932 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1933 * or 3 cells specially for a PCI address.
Alexander Grafc48e6862014-04-11 17:09:41 +02001934 */
1935static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1936 uint64_t *val, int cells)
1937{
Bin Menga932aa32021-02-25 17:22:24 +08001938 const fdt32_t *prop32;
1939 const unaligned_fdt64_t *prop64;
Alexander Grafc48e6862014-04-11 17:09:41 +02001940
1941 if ((cell_off + cells) > prop_len)
1942 return -FDT_ERR_NOSPACE;
1943
Bin Menga932aa32021-02-25 17:22:24 +08001944 prop32 = &prop[cell_off];
1945
1946 /*
1947 * Special handling for PCI address in PCI bus <ranges>
1948 *
1949 * PCI child address is made up of 3 cells. Advance the cell offset
1950 * by 1 so that the PCI child address can be correctly read.
1951 */
1952 if (cells == 3)
1953 cell_off += 1;
1954 prop64 = (const fdt64_t *)&prop[cell_off];
1955
Alexander Grafc48e6862014-04-11 17:09:41 +02001956 switch (cells) {
1957 case 1:
1958 *val = fdt32_to_cpu(*prop32);
1959 break;
1960 case 2:
Bin Menga932aa32021-02-25 17:22:24 +08001961 case 3:
Alexander Grafc48e6862014-04-11 17:09:41 +02001962 *val = fdt64_to_cpu(*prop64);
1963 break;
1964 default:
1965 return -FDT_ERR_NOSPACE;
1966 }
1967
1968 return 0;
1969}
1970
1971/**
1972 * fdt_read_range - Read a node's n'th range property
1973 *
1974 * @fdt: ptr to device tree
1975 * @node: offset of node
1976 * @n: range index
1977 * @child_addr: pointer to storage for the "child address" field
1978 * @addr: pointer to storage for the CPU view translated physical start
1979 * @len: pointer to storage for the range length
1980 *
1981 * Convenience function that reads and interprets a specific range out of
1982 * a number of the "ranges" property array.
1983 */
1984int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1985 uint64_t *addr, uint64_t *len)
1986{
1987 int pnode = fdt_parent_offset(fdt, node);
1988 const fdt32_t *ranges;
1989 int pacells;
1990 int acells;
1991 int scells;
1992 int ranges_len;
1993 int cell = 0;
1994 int r = 0;
1995
1996 /*
1997 * The "ranges" property is an array of
1998 * { <child address> <parent address> <size in child address space> }
1999 *
2000 * All 3 elements can span a diffent number of cells. Fetch their size.
2001 */
2002 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
2003 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
2004 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
2005
2006 /* Now try to get the ranges property */
2007 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
2008 if (!ranges)
2009 return -FDT_ERR_NOTFOUND;
2010 ranges_len /= sizeof(uint32_t);
2011
2012 /* Jump to the n'th entry */
2013 cell = n * (pacells + acells + scells);
2014
2015 /* Read <child address> */
2016 if (child_addr) {
2017 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
2018 acells);
2019 if (r)
2020 return r;
2021 }
2022 cell += acells;
2023
2024 /* Read <parent address> */
2025 if (addr)
2026 *addr = fdt_translate_address(fdt, node, ranges + cell);
2027 cell += pacells;
2028
2029 /* Read <size in child address space> */
2030 if (len) {
2031 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
2032 if (r)
2033 return r;
2034 }
2035
2036 return 0;
2037}
Hans de Goeded4f495a2014-11-17 15:29:11 +01002038
2039/**
2040 * fdt_setup_simplefb_node - Fill and enable a simplefb node
2041 *
2042 * @fdt: ptr to device tree
2043 * @node: offset of the simplefb node
2044 * @base_address: framebuffer base address
2045 * @width: width in pixels
2046 * @height: height in pixels
2047 * @stride: bytes per line
2048 * @format: pixel format string
2049 *
2050 * Convenience function to fill and enable a simplefb node.
2051 */
2052int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2053 u32 height, u32 stride, const char *format)
2054{
2055 char name[32];
2056 fdt32_t cells[4];
2057 int i, addrc, sizec, ret;
2058
Simon Glasseed36602017-05-18 20:09:26 -06002059 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2060 &addrc, &sizec);
Hans de Goeded4f495a2014-11-17 15:29:11 +01002061 i = 0;
2062 if (addrc == 2)
2063 cells[i++] = cpu_to_fdt32(base_address >> 32);
2064 cells[i++] = cpu_to_fdt32(base_address);
2065 if (sizec == 2)
2066 cells[i++] = 0;
2067 cells[i++] = cpu_to_fdt32(height * stride);
2068
2069 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2070 if (ret < 0)
2071 return ret;
2072
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002073 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goeded4f495a2014-11-17 15:29:11 +01002074 ret = fdt_set_name(fdt, node, name);
2075 if (ret < 0)
2076 return ret;
2077
2078 ret = fdt_setprop_u32(fdt, node, "width", width);
2079 if (ret < 0)
2080 return ret;
2081
2082 ret = fdt_setprop_u32(fdt, node, "height", height);
2083 if (ret < 0)
2084 return ret;
2085
2086 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2087 if (ret < 0)
2088 return ret;
2089
2090 ret = fdt_setprop_string(fdt, node, "format", format);
2091 if (ret < 0)
2092 return ret;
2093
2094 ret = fdt_setprop_string(fdt, node, "status", "okay");
2095 if (ret < 0)
2096 return ret;
2097
2098 return 0;
2099}
Tim Harvey08daa252015-04-08 11:45:39 -07002100
Devarsh Thakkarefe1cee2024-02-22 18:38:10 +05302101#if CONFIG_IS_ENABLED(VIDEO)
2102int fdt_add_fb_mem_rsv(void *blob)
2103{
2104 struct fdt_memory mem;
2105
2106 /* nothing to do when the frame buffer is not defined */
2107 if (gd->video_bottom == gd->video_top)
2108 return 0;
2109
2110 /* reserved with no-map tag the video buffer */
2111 mem.start = gd->video_bottom;
2112 mem.end = gd->video_top - 1;
2113
2114 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2115 FDTDEC_RESERVED_MEMORY_NO_MAP);
2116}
2117#endif
2118
Tim Harvey08daa252015-04-08 11:45:39 -07002119/*
2120 * Update native-mode in display-timings from display environment variable.
2121 * The node to update are specified by path.
2122 */
2123int fdt_fixup_display(void *blob, const char *path, const char *display)
2124{
2125 int off, toff;
2126
2127 if (!display || !path)
2128 return -FDT_ERR_NOTFOUND;
2129
2130 toff = fdt_path_offset(blob, path);
2131 if (toff >= 0)
2132 toff = fdt_subnode_offset(blob, toff, "display-timings");
2133 if (toff < 0)
2134 return toff;
2135
2136 for (off = fdt_first_subnode(blob, toff);
2137 off >= 0;
2138 off = fdt_next_subnode(blob, off)) {
2139 uint32_t h = fdt_get_phandle(blob, off);
2140 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2141 fdt32_to_cpu(h));
2142 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2143 return fdt_setprop_u32(blob, toff, "native-mode", h);
2144 }
2145 return toff;
2146}
Pantelis Antonioufc7c3182017-09-04 23:12:11 +03002147
2148#ifdef CONFIG_OF_LIBFDT_OVERLAY
2149/**
2150 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2151 *
2152 * @fdt: ptr to device tree
2153 * @fdto: ptr to device tree overlay
2154 *
2155 * Convenience function to apply an overlay and display helpful messages
2156 * in the case of an error
2157 */
2158int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2159{
2160 int err;
2161 bool has_symbols;
2162
2163 err = fdt_path_offset(fdt, "/__symbols__");
2164 has_symbols = err >= 0;
2165
2166 err = fdt_overlay_apply(fdt, fdto);
2167 if (err < 0) {
2168 printf("failed on fdt_overlay_apply(): %s\n",
2169 fdt_strerror(err));
2170 if (!has_symbols) {
Hugo Villeneuvea3a884c2023-10-26 15:54:49 -04002171 printf("base fdt does not have a /__symbols__ node\n");
Pantelis Antonioufc7c3182017-09-04 23:12:11 +03002172 printf("make sure you've compiled with -@\n");
2173 }
2174 }
2175 return err;
2176}
2177#endif
Kory Maincentbbdbcaf2021-05-04 19:31:21 +02002178
2179/**
2180 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2181 *
2182 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01002183 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincentbbdbcaf2021-05-04 19:31:21 +02002184 */
2185int fdt_valid(struct fdt_header **blobp)
2186{
2187 const void *blob = *blobp;
2188 int err;
2189
2190 if (!blob) {
2191 printf("The address of the fdt is invalid (NULL).\n");
2192 return 0;
2193 }
2194
2195 err = fdt_check_header(blob);
2196 if (err == 0)
2197 return 1; /* valid */
2198
2199 if (err < 0) {
2200 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2201 /*
2202 * Be more informative on bad version.
2203 */
2204 if (err == -FDT_ERR_BADVERSION) {
2205 if (fdt_version(blob) <
2206 FDT_FIRST_SUPPORTED_VERSION) {
2207 printf(" - too old, fdt %d < %d",
2208 fdt_version(blob),
2209 FDT_FIRST_SUPPORTED_VERSION);
2210 }
2211 if (fdt_last_comp_version(blob) >
2212 FDT_LAST_SUPPORTED_VERSION) {
2213 printf(" - too new, fdt %d > %d",
2214 fdt_version(blob),
2215 FDT_LAST_SUPPORTED_VERSION);
2216 }
2217 }
2218 printf("\n");
2219 *blobp = NULL;
2220 return 0;
2221 }
2222 return 1;
2223}