blob: 874ca4d6f5afe7fb2bc0da6102d7ac0f535eb763 [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
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +02009#include <abuf.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Heinrich Schuchardt668cabc2018-11-18 17:58:51 +010012#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <net.h>
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040014#include <stdio_dev.h>
Patrick Delaunaye0682262023-06-08 17:16:37 +020015#include <dm/ofnode.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040016#include <linux/ctype.h>
17#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040018#include <asm/global_data.h>
Sam Protsenko94483d22024-03-29 19:55:53 -050019#include <asm/unaligned.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090020#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040021#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060022#include <exports.h>
Bin Mengd9a00932015-12-07 01:39:47 -080023#include <fdtdec.h>
Francesco Dolcini05cc8272022-05-19 16:22:26 +020024#include <version.h>
Devarsh Thakkar31199892024-02-22 18:38:10 +053025#include <video.h>
26
27DECLARE_GLOBAL_DATA_PTR;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040028
Kumar Gala1ba00402008-10-23 00:05:47 -050029/**
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020030 * fdt_getprop_u32_default_node - Return a node's property or a default
31 *
32 * @fdt: ptr to device tree
33 * @off: offset of node
34 * @cell: cell offset in property
35 * @prop: property name
36 * @dflt: default value if the property isn't found
37 *
38 * Convenience function to return a node's property or a default value if
39 * the property doesn't exist.
40 */
41u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
42 const char *prop, const u32 dflt)
43{
44 const fdt32_t *val;
45 int len;
46
47 val = fdt_getprop(fdt, off, prop, &len);
48
49 /* Check if property exists */
50 if (!val)
51 return dflt;
52
53 /* Check if property is long enough */
54 if (len < ((cell + 1) * sizeof(uint32_t)))
55 return dflt;
56
57 return fdt32_to_cpu(*val);
58}
59
60/**
Kumar Gala1ba00402008-10-23 00:05:47 -050061 * fdt_getprop_u32_default - Find a node and return it's property or a default
62 *
63 * @fdt: ptr to device tree
64 * @path: path of node
65 * @prop: property name
66 * @dflt: default value if the property isn't found
67 *
68 * Convenience function to find a node and return it's property or a
69 * default value if it doesn't exist.
70 */
Gabe Blackcd37de72011-11-08 01:09:44 -080071u32 fdt_getprop_u32_default(const void *fdt, const char *path,
72 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050073{
Kumar Gala1ba00402008-10-23 00:05:47 -050074 int off;
75
76 off = fdt_path_offset(fdt, path);
77 if (off < 0)
78 return dflt;
79
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020080 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala1ba00402008-10-23 00:05:47 -050081}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040082
Kumar Galabf771dc2007-10-24 10:21:57 -050083/**
84 * fdt_find_and_setprop: Find a node and set it's property
85 *
86 * @fdt: ptr to device tree
87 * @node: path of node
88 * @prop: property name
89 * @val: ptr to new value
90 * @len: length of new property value
91 * @create: flag to create the property if it doesn't exist
92 *
93 * Convenience function to directly set a property given the path to the node.
94 */
95int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
96 const void *val, int len, int create)
97{
Kumar Galac8ab7052007-10-24 11:04:22 -050098 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050099
100 if (nodeoff < 0)
101 return nodeoff;
102
Kim Phillips6542c072013-01-16 14:00:11 +0000103 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500104 return 0; /* create flag not set; so exit quietly */
105
106 return fdt_setprop(fdt, nodeoff, prop, val, len);
107}
108
Masahiro Yamadac0207282014-04-18 17:40:58 +0900109/**
Simon Glass014aa052014-10-23 18:58:49 -0600110 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
111 *
Masahiro Yamadac0207282014-04-18 17:40:58 +0900112 * @fdt: pointer to the device tree blob
113 * @parentoffset: structure block offset of a node
114 * @name: name of the subnode to locate
115 *
116 * fdt_subnode_offset() finds a subnode of the node with a given name.
117 * If the subnode does not exist, it will be created.
118 */
Simon Glass014aa052014-10-23 18:58:49 -0600119int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamadac0207282014-04-18 17:40:58 +0900120{
121 int offset;
122
123 offset = fdt_subnode_offset(fdt, parentoffset, name);
124
125 if (offset == -FDT_ERR_NOTFOUND)
126 offset = fdt_add_subnode(fdt, parentoffset, name);
127
128 if (offset < 0)
129 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
130
131 return offset;
132}
133
Andre Przywara0d1fcf82021-02-14 10:35:18 +0000134#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel46e192a2008-06-20 22:24:05 +0200135static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600136{
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900137 int err;
138 int aliasoff;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600139 char sername[9] = { 0 };
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900140 const void *path;
141 int len;
142 char tmp[256]; /* long enough */
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600143
Bin Mengb8199112016-01-13 19:38:58 -0800144 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600145
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900146 aliasoff = fdt_path_offset(fdt, "/aliases");
147 if (aliasoff < 0) {
148 err = aliasoff;
Scott Woodedd551f2015-09-01 22:48:08 -0500149 goto noalias;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600150 }
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900151
152 path = fdt_getprop(fdt, aliasoff, sername, &len);
153 if (!path) {
154 err = len;
Scott Woodedd551f2015-09-01 22:48:08 -0500155 goto noalias;
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900156 }
157
158 /* fdt_setprop may break "path" so we copy it to tmp buffer */
159 memcpy(tmp, path, len);
160
161 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600162 if (err < 0)
163 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900164 fdt_strerror(err));
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600165
166 return err;
Scott Woodedd551f2015-09-01 22:48:08 -0500167
168noalias:
169 printf("WARNING: %s: could not read %s alias: %s\n",
170 __func__, sername, fdt_strerror(err));
171
172 return 0;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600173}
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900174#else
175static int fdt_fixup_stdout(void *fdt, int chosenoff)
176{
177 return 0;
178}
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600179#endif
180
Masahiro Yamadab891a342014-04-18 17:41:04 +0900181static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
182 uint64_t val, int is_u64)
183{
184 if (is_u64)
185 return fdt_setprop_u64(fdt, nodeoffset, name, val);
186 else
187 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
188}
189
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200190int fdt_root(void *fdt)
191{
192 char *serial;
193 int err;
194
195 err = fdt_check_header(fdt);
196 if (err < 0) {
197 printf("fdt_root: %s\n", fdt_strerror(err));
198 return err;
199 }
200
Simon Glass64b723f2017-08-03 12:22:12 -0600201 serial = env_get("serial#");
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200202 if (serial) {
203 err = fdt_setprop(fdt, 0, "serial-number", serial,
204 strlen(serial) + 1);
205
206 if (err < 0) {
207 printf("WARNING: could not set serial-number %s.\n",
208 fdt_strerror(err));
209 return err;
210 }
211 }
212
213 return 0;
214}
Masahiro Yamadab891a342014-04-18 17:41:04 +0900215
Masahiro Yamada5b114892014-04-18 17:40:59 +0900216int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400217{
Masahiro Yamadab891a342014-04-18 17:41:04 +0900218 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500219 int err, j, total;
Masahiro Yamadab891a342014-04-18 17:41:04 +0900220 int is_u64;
Kumar Gala99ebc052008-08-15 08:24:43 -0500221 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400222
Masahiro Yamada9a3b4de2014-04-18 17:41:05 +0900223 /* just return if the size of initrd is zero */
224 if (initrd_start == initrd_end)
225 return 0;
226
Masahiro Yamadac0207282014-04-18 17:40:58 +0900227 /* find or create "/chosen" node. */
228 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
229 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500230 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400231
Kumar Gala99ebc052008-08-15 08:24:43 -0500232 total = fdt_num_mem_rsv(fdt);
233
234 /*
235 * Look for an existing entry and update it. If we don't find
236 * the entry, we will j be the next available slot.
237 */
238 for (j = 0; j < total; j++) {
239 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
240 if (addr == initrd_start) {
241 fdt_del_mem_rsv(fdt, j);
242 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400243 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500244 }
245
Grant Likelyc379a562011-03-28 09:58:55 +0000246 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500247 if (err < 0) {
248 printf("fdt_initrd: %s\n", fdt_strerror(err));
249 return err;
250 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500251
Simon Glass9fbc6322014-10-23 18:58:57 -0600252 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengd9850132013-12-14 11:47:29 +0800253
Masahiro Yamadab891a342014-04-18 17:41:04 +0900254 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
255 (uint64_t)initrd_start, is_u64);
256
Masahiro Yamada5b114892014-04-18 17:40:59 +0900257 if (err < 0) {
258 printf("WARNING: could not set linux,initrd-start %s.\n",
259 fdt_strerror(err));
260 return err;
261 }
Masahiro Yamadab891a342014-04-18 17:41:04 +0900262
263 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
264 (uint64_t)initrd_end, is_u64);
265
Masahiro Yamada5b114892014-04-18 17:40:59 +0900266 if (err < 0) {
267 printf("WARNING: could not set linux,initrd-end %s.\n",
268 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500269
Masahiro Yamada5b114892014-04-18 17:40:59 +0900270 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400271 }
272
Kumar Gala99ebc052008-08-15 08:24:43 -0500273 return 0;
274}
275
Niko Mauno673db432021-02-22 19:18:51 +0000276/**
277 * board_fdt_chosen_bootargs - boards may override this function to use
278 * alternative kernel command line arguments
279 */
280__weak char *board_fdt_chosen_bootargs(void)
281{
282 return env_get("bootargs");
283}
284
Masahiro Yamada451c2042014-04-18 17:41:00 +0900285int fdt_chosen(void *fdt)
Kumar Gala99ebc052008-08-15 08:24:43 -0500286{
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200287 struct abuf buf = {};
Kumar Gala99ebc052008-08-15 08:24:43 -0500288 int nodeoffset;
289 int err;
290 char *str; /* used to set string properties */
Kumar Gala99ebc052008-08-15 08:24:43 -0500291
292 err = fdt_check_header(fdt);
293 if (err < 0) {
294 printf("fdt_chosen: %s\n", fdt_strerror(err));
295 return err;
296 }
297
Masahiro Yamadac0207282014-04-18 17:40:58 +0900298 /* find or create "/chosen" node. */
299 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
300 if (nodeoffset < 0)
301 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400302
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200303 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
304 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
305 abuf_data(&buf), abuf_size(&buf));
306 abuf_uninit(&buf);
307 if (err < 0) {
308 printf("WARNING: could not set rng-seed %s.\n",
309 fdt_strerror(err));
310 return err;
311 }
312 }
313
Niko Mauno673db432021-02-22 19:18:51 +0000314 str = board_fdt_chosen_bootargs();
315
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900316 if (str) {
317 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
318 strlen(str) + 1);
319 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900320 printf("WARNING: could not set bootargs %s.\n",
321 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900322 return err;
323 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400324 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600325
Francesco Dolcini05cc8272022-05-19 16:22:26 +0200326 /* add u-boot version */
327 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
328 strlen(PLAIN_VERSION) + 1);
329 if (err < 0) {
330 printf("WARNING: could not set u-boot,version %s.\n",
331 fdt_strerror(err));
332 return err;
333 }
334
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900335 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400336}
337
Kumar Gala7e64cf82007-11-03 19:46:28 -0500338void do_fixup_by_path(void *fdt, const char *path, const char *prop,
339 const void *val, int len, int create)
340{
341#if defined(DEBUG)
342 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600343 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500344 for (i = 0; i < len; i++)
345 debug(" %.2x", *(u8*)(val+i));
346 debug("\n");
347#endif
348 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
349 if (rc)
350 printf("Unable to update property %s:%s, err=%s\n",
351 path, prop, fdt_strerror(rc));
352}
353
354void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
355 u32 val, int create)
356{
Kim Phillips6542c072013-01-16 14:00:11 +0000357 fdt32_t tmp = cpu_to_fdt32(val);
358 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500359}
360
Kumar Gala7590a192007-11-21 13:30:15 -0600361void do_fixup_by_prop(void *fdt,
362 const char *pname, const void *pval, int plen,
363 const char *prop, const void *val, int len,
364 int create)
365{
366 int off;
367#if defined(DEBUG)
368 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600369 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600370 for (i = 0; i < len; i++)
371 debug(" %.2x", *(u8*)(val+i));
372 debug("\n");
373#endif
374 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
375 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000376 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600377 fdt_setprop(fdt, off, prop, val, len);
378 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
379 }
380}
381
382void do_fixup_by_prop_u32(void *fdt,
383 const char *pname, const void *pval, int plen,
384 const char *prop, u32 val, int create)
385{
Kim Phillips6542c072013-01-16 14:00:11 +0000386 fdt32_t tmp = cpu_to_fdt32(val);
387 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600388}
389
390void do_fixup_by_compat(void *fdt, const char *compat,
391 const char *prop, const void *val, int len, int create)
392{
393 int off = -1;
394#if defined(DEBUG)
395 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600396 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600397 for (i = 0; i < len; i++)
398 debug(" %.2x", *(u8*)(val+i));
399 debug("\n");
400#endif
Marek Behún5d6b4482022-01-20 01:04:42 +0100401 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips6542c072013-01-16 14:00:11 +0000402 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600403 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala7590a192007-11-21 13:30:15 -0600404}
405
406void do_fixup_by_compat_u32(void *fdt, const char *compat,
407 const char *prop, u32 val, int create)
408{
Kim Phillips6542c072013-01-16 14:00:11 +0000409 fdt32_t tmp = cpu_to_fdt32(val);
410 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600411}
412
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900413#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900414/*
415 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
416 */
Simon Glass0058e112014-10-23 18:58:55 -0600417static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
418 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900419{
420 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100421 int address_cells = fdt_address_cells(fdt, 0);
422 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900423 char *p = buf;
424
425 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100426 if (address_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500427 put_unaligned_be64(address[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900428 else
429 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100430 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900431
Hans de Goede4c80fb02014-11-28 14:23:51 +0100432 if (size_cells == 2)
Sam Protsenko94483d22024-03-29 19:55:53 -0500433 put_unaligned_be64(size[i], p);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900434 else
435 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100436 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900437 }
438
439 return p - (char *)buf;
440}
441
Ramon Fried9cab1832018-08-14 00:35:42 +0300442#if CONFIG_NR_DRAM_BANKS > 4
443#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
444#else
Kim Phillips6542c072013-01-16 14:00:11 +0000445#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300446#endif
Michal Simek96283732021-08-10 09:21:54 +0200447
448/**
449 * fdt_fixup_memory_banks - Update DT memory node
450 * @blob: Pointer to DT blob
451 * @start: Pointer to memory start addresses array
452 * @size: Pointer to memory sizes array
453 * @banks: Number of memory banks
454 *
455 * Return: 0 on success, negative value on failure
456 *
457 * Based on the passed number of banks and arrays, the function is able to
458 * update existing DT memory nodes to match run time detected/changed memory
459 * configuration. Implementation is handling one specific case with only one
460 * memory node where multiple tuples could be added/updated.
461 * The case where multiple memory nodes with a single tuple (base, size) are
462 * used, this function is only updating the first memory node without removing
463 * others.
464 */
John Rigbyb4ae9422010-10-13 13:57:33 -0600465int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
466{
467 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100468 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000469 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600470
Kim Phillips6542c072013-01-16 14:00:11 +0000471 if (banks > MEMORY_BANKS_MAX) {
472 printf("%s: num banks %d exceeds hardcoded limit %d."
473 " Recompile with higher MEMORY_BANKS_MAX?\n",
474 __FUNCTION__, banks, MEMORY_BANKS_MAX);
475 return -1;
476 }
477
Kumar Gala0db72ed2007-11-26 14:57:45 -0600478 err = fdt_check_header(blob);
479 if (err < 0) {
480 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
481 return err;
482 }
483
Masahiro Yamadac0207282014-04-18 17:40:58 +0900484 /* find or create "/memory" node. */
485 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
486 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800487 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900488
Kumar Gala0db72ed2007-11-26 14:57:45 -0600489 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
490 sizeof("memory"));
491 if (err < 0) {
492 printf("WARNING: could not set %s %s.\n", "device_type",
493 fdt_strerror(err));
494 return err;
495 }
496
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100497 for (i = 0; i < banks; i++) {
498 if (start[i] == 0 && size[i] == 0)
499 break;
500 }
501
502 banks = i;
503
Andre Przywara3d2d4412015-06-21 00:29:54 +0100504 if (!banks)
505 return 0;
506
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900507 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600508
509 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
510 if (err < 0) {
511 printf("WARNING: could not set %s %s.\n",
512 "reg", fdt_strerror(err));
513 return err;
514 }
515 return 0;
516}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200517
518int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
519{
520 int err, nodeoffset;
521 int len;
522 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
523
524 if (areas > 8) {
525 printf("%s: num areas %d exceeds hardcoded limit %d\n",
526 __func__, areas, 8);
527 return -1;
528 }
529
530 err = fdt_check_header(blob);
531 if (err < 0) {
532 printf("%s: %s\n", __func__, fdt_strerror(err));
533 return err;
534 }
535
536 /* find or create "/memory" node. */
537 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
538 if (nodeoffset < 0)
539 return nodeoffset;
540
541 len = fdt_pack_reg(blob, tmp, start, size, areas);
542
543 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
544 if (err < 0) {
545 printf("WARNING: could not set %s %s.\n",
546 "reg", fdt_strerror(err));
547 return err;
548 }
549
550 return 0;
551}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900552#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600553
John Rigbyb4ae9422010-10-13 13:57:33 -0600554int fdt_fixup_memory(void *blob, u64 start, u64 size)
555{
556 return fdt_fixup_memory_banks(blob, &start, &size, 1);
557}
558
Kumar Galafabda922008-08-19 15:41:18 -0500559void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600560{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530561 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800562 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000563 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600564 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100565 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800566 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530567#ifdef FDT_SEQ_MACADDR_FROM_ENV
568 int nodeoff;
569 const struct fdt_property *fdt_prop;
570#endif
Kumar Gala22699102007-11-21 11:11:03 -0600571
Lev Iserovichcde77612016-01-07 18:04:16 -0500572 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500573 return;
574
Lev Iserovichcde77612016-01-07 18:04:16 -0500575 /* Cycle through all aliases */
576 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800577 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500578
Lev Iserovichcde77612016-01-07 18:04:16 -0500579 /* FDT might have been edited, recompute the offset */
580 offset = fdt_first_property_offset(fdt,
581 fdt_path_offset(fdt, "/aliases"));
582 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530583 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500584 offset = fdt_next_property_offset(fdt, offset);
585
586 if (offset < 0)
587 break;
588
Bin Mengbfb93cc2015-11-03 04:24:41 -0800589 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200590 if (!strncmp(name, "ethernet", 8)) {
591 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530592 if (!strcmp(name, "ethernet")
593#ifdef FDT_SEQ_MACADDR_FROM_ENV
594 || !strcmp(name, "ethernet0")
595#endif
596 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200597 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530598#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200599 else
600 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530601#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800602 if (i != -1) {
603 if (i == 0)
604 strcpy(mac, "ethaddr");
605 else
606 sprintf(mac, "eth%daddr", i);
607 } else {
608 continue;
609 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530610#ifdef FDT_SEQ_MACADDR_FROM_ENV
611 nodeoff = fdt_path_offset(fdt, path);
612 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
613 NULL);
614 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
615 continue;
616 i++;
617#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600618 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800619 if (!tmp)
620 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500621
Bin Mengbfb93cc2015-11-03 04:24:41 -0800622 for (j = 0; j < 6; j++) {
623 mac_addr[j] = tmp ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600624 hextoul(tmp, &end) : 0;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800625 if (tmp)
626 tmp = (*end) ? end + 1 : end;
627 }
Kumar Galafabda922008-08-19 15:41:18 -0500628
Bin Mengbfb93cc2015-11-03 04:24:41 -0800629 do_fixup_by_path(fdt, path, "mac-address",
630 &mac_addr, 6, 0);
631 do_fixup_by_path(fdt, path, "local-mac-address",
632 &mac_addr, 6, 1);
633 }
Kumar Gala22699102007-11-21 11:11:03 -0600634 }
635}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300636
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100637int fdt_record_loadable(void *blob, u32 index, const char *name,
638 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simek0e4f43d2021-05-27 11:40:09 +0200639 const char *type, const char *os, const char *arch)
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100640{
641 int err, node;
642
643 err = fdt_check_header(blob);
644 if (err < 0) {
645 printf("%s: %s\n", __func__, fdt_strerror(err));
646 return err;
647 }
648
649 /* find or create "/fit-images" node */
650 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
651 if (node < 0)
652 return node;
653
654 /* find or create "/fit-images/<name>" node */
655 node = fdt_find_or_add_subnode(blob, node, name);
656 if (node < 0)
657 return node;
658
Michal Simekd31b40f2020-09-03 12:44:51 +0200659 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100660 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200661 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100662 fdt_setprop_u32(blob, node, "size", size);
663 if (type)
664 fdt_setprop_string(blob, node, "type", type);
665 if (os)
666 fdt_setprop_string(blob, node, "os", os);
Michal Simek0e4f43d2021-05-27 11:40:09 +0200667 if (arch)
668 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100669
670 return node;
671}
672
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200673int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500674{
675 int i;
676 uint64_t addr, size;
677 int total, ret;
678 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200679 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500680
681 if (!blob)
682 return 0;
683
684 total = fdt_num_mem_rsv(blob);
685 for (i = 0; i < total; i++) {
686 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000687 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500688 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200689 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500690 break;
691 }
692 }
693
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100694 /*
695 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200696 * plus the size needed for 5 fdt_add_mem_rsv, one
697 * for the fdt itself and 4 for a possible initrd
698 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100699 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500700 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200701 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500702
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200703 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500704 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000705 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
706 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500707
708 /* Change the fdt header to reflect the correct size */
709 fdt_set_totalsize(blob, actualsize);
710
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200711 if (fdt_memrsv) {
712 /* Add the new reservation */
713 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
714 if (ret < 0)
715 return ret;
716 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500717
718 return actualsize;
719}
Kumar Galae0475cd2008-10-22 23:33:56 -0500720
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100721/**
722 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
723 *
724 * @blob: ptr to device tree
725 */
726int fdt_delete_disabled_nodes(void *blob)
727{
728 while (1) {
729 int ret, offset;
730
731 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
732 "disabled", 9);
733 if (offset < 0)
734 break;
735
736 ret = fdt_del_node(blob, offset);
737 if (ret < 0)
738 return ret;
739 }
740
741 return 0;
742}
743
Kumar Galae0475cd2008-10-22 23:33:56 -0500744#ifdef CONFIG_PCI
Tom Rini56af6592022-11-16 13:10:33 -0500745#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500746
747#define FDT_PCI_PREFETCH (0x40000000)
748#define FDT_PCI_MEM32 (0x02000000)
749#define FDT_PCI_IO (0x01000000)
750#define FDT_PCI_MEM64 (0x03000000)
751
752int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
753
754 int addrcell, sizecell, len, r;
755 u32 *dma_range;
756 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Rini56af6592022-11-16 13:10:33 -0500757 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Galae0475cd2008-10-22 23:33:56 -0500758
759 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
760 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
761
762 dma_range = &dma_ranges[0];
763 for (r = 0; r < hose->region_count; r++) {
764 u64 bus_start, phys_start, size;
765
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600766 /* skip if !PCI_REGION_SYS_MEMORY */
767 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500768 continue;
769
770 bus_start = (u64)hose->regions[r].bus_start;
771 phys_start = (u64)hose->regions[r].phys_start;
772 size = (u64)hose->regions[r].size;
773
774 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500775 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200776 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500777 else
Marek Vasut22203f52019-07-09 02:49:29 +0200778 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500779 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200780 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500781#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200782 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500783#else
784 dma_range[1] = 0;
785#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200786 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500787
788 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200789 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
790 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500791 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200792 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500793 }
794
795 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200796 dma_range[3 + addrcell + 0] =
797 cpu_to_fdt32(size >> 32);
798 dma_range[3 + addrcell + 1] =
799 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500800 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200801 dma_range[3 + addrcell + 0] =
802 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500803 }
804
805 dma_range += (3 + addrcell + sizecell);
806 }
807
808 len = dma_range - &dma_ranges[0];
809 if (len)
810 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
811
812 return 0;
813}
814#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200815
Matthew McClintockacda3a42010-10-13 13:39:26 +0200816int fdt_increase_size(void *fdt, int add_len)
817{
818 int newlen;
819
820 newlen = fdt_totalsize(fdt) + add_len;
821
822 /* Open in place with a new len */
823 return fdt_open_into(fdt, fdt, newlen);
824}
825
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100826#ifdef CONFIG_FDT_FIXUP_PARTITIONS
827#include <jffs2/load_kernel.h>
828#include <mtd_node.h>
829
Michal Simekc2ed0072018-07-31 14:31:04 +0200830static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100831{
832 int off, ndepth;
833 int ret;
834
835 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
836 (off >= 0) && (ndepth > 0);
837 off = fdt_next_node(blob, off, &ndepth)) {
838 if (ndepth == 1) {
839 debug("delete %s: offset: %x\n",
840 fdt_get_name(blob, off, 0), off);
841 ret = fdt_del_node((void *)blob, off);
842 if (ret < 0) {
843 printf("Can't delete node: %s\n",
844 fdt_strerror(ret));
845 return ret;
846 } else {
847 ndepth = 0;
848 off = parent_offset;
849 }
850 }
851 }
852 return 0;
853}
854
Michal Simekc2ed0072018-07-31 14:31:04 +0200855static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100856{
857 const void *prop;
858 int ndepth = 0;
859 int off;
860 int ret;
861
862 off = fdt_next_node(blob, parent_offset, &ndepth);
863 if (off > 0 && ndepth == 1) {
864 prop = fdt_getprop(blob, off, "label", NULL);
865 if (prop == NULL) {
866 /*
867 * Could not find label property, nand {}; node?
868 * Check subnode, delete partitions there if any.
869 */
870 return fdt_del_partitions(blob, off);
871 } else {
872 ret = fdt_del_subnodes(blob, parent_offset);
873 if (ret < 0) {
874 printf("Can't remove subnodes: %s\n",
875 fdt_strerror(ret));
876 return ret;
877 }
878 }
879 }
880 return 0;
881}
882
Masahiro Yamada65669602020-07-15 19:35:47 +0900883static int fdt_node_set_part_info(void *blob, int parent_offset,
884 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100885{
886 struct list_head *pentry;
887 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100888 int off, ndepth = 0;
889 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300890 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100891 char buf[64];
892
893 ret = fdt_del_partitions(blob, parent_offset);
894 if (ret < 0)
895 return ret;
896
897 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300898 * Check if size/address is 1 or 2 cells.
899 * We assume #address-cells and #size-cells have same value.
900 */
901 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
902 0, "#size-cells", 1);
903
904 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100905 * Check if it is nand {}; subnode, adjust
906 * the offset in this case
907 */
908 off = fdt_next_node(blob, parent_offset, &ndepth);
909 if (off > 0 && ndepth == 1)
910 parent_offset = off;
911
912 part_num = 0;
913 list_for_each_prev(pentry, &dev->parts) {
914 int newoff;
915
916 part = list_entry(pentry, struct part_info, link);
917
Scott Woodefe7f302013-10-15 17:41:27 -0500918 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100919 part_num, part->name, part->size,
920 part->offset, part->mask_flags);
921
Scott Woodefe7f302013-10-15 17:41:27 -0500922 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100923add_sub:
924 ret = fdt_add_subnode(blob, parent_offset, buf);
925 if (ret == -FDT_ERR_NOSPACE) {
926 ret = fdt_increase_size(blob, 512);
927 if (!ret)
928 goto add_sub;
929 else
930 goto err_size;
931 } else if (ret < 0) {
932 printf("Can't add partition node: %s\n",
933 fdt_strerror(ret));
934 return ret;
935 }
936 newoff = ret;
937
938 /* Check MTD_WRITEABLE_CMD flag */
939 if (part->mask_flags & 1) {
940add_ro:
941 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
942 if (ret == -FDT_ERR_NOSPACE) {
943 ret = fdt_increase_size(blob, 512);
944 if (!ret)
945 goto add_ro;
946 else
947 goto err_size;
948 } else if (ret < 0)
949 goto err_prop;
950 }
951
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100952add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300953 if (sizecell == 2) {
954 ret = fdt_setprop_u64(blob, newoff,
955 "reg", part->offset);
956 if (!ret)
957 ret = fdt_appendprop_u64(blob, newoff,
958 "reg", part->size);
959 } else {
960 ret = fdt_setprop_u32(blob, newoff,
961 "reg", part->offset);
962 if (!ret)
963 ret = fdt_appendprop_u32(blob, newoff,
964 "reg", part->size);
965 }
966
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100967 if (ret == -FDT_ERR_NOSPACE) {
968 ret = fdt_increase_size(blob, 512);
969 if (!ret)
970 goto add_reg;
971 else
972 goto err_size;
973 } else if (ret < 0)
974 goto err_prop;
975
976add_label:
977 ret = fdt_setprop_string(blob, newoff, "label", part->name);
978 if (ret == -FDT_ERR_NOSPACE) {
979 ret = fdt_increase_size(blob, 512);
980 if (!ret)
981 goto add_label;
982 else
983 goto err_size;
984 } else if (ret < 0)
985 goto err_prop;
986
987 part_num++;
988 }
989 return 0;
990err_size:
991 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
992 return ret;
993err_prop:
994 printf("Can't add property: %s\n", fdt_strerror(ret));
995 return ret;
996}
997
998/*
999 * Update partitions in nor/nand nodes using info from
1000 * mtdparts environment variable. The nodes to update are
1001 * specified by node_info structure which contains mtd device
1002 * type and compatible string: E. g. the board code in
1003 * ft_board_setup() could use:
1004 *
1005 * struct node_info nodes[] = {
1006 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1007 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1008 * };
1009 *
1010 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1011 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001012void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1013 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001014{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001015 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001016 int i, idx;
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001017 int noff, parts;
Masahiro Yamada030348f2020-07-17 10:46:18 +09001018 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001019
1020 for (i = 0; i < node_info_size; i++) {
1021 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +09001022
Marek Behún5d6b4482022-01-20 01:04:42 +01001023 fdt_for_each_node_by_compatible(noff, blob, -1,
1024 node_info[i].compat) {
Masahiro Yamada21644292020-07-17 10:46:19 +09001025 const char *prop;
1026
1027 prop = fdt_getprop(blob, noff, "status", NULL);
1028 if (prop && !strcmp(prop, "disabled"))
1029 continue;
1030
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001031 debug("%s: %s, mtd dev type %d\n",
1032 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001033 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +09001034
1035 if (!inited) {
1036 if (mtdparts_init() != 0)
1037 return;
1038 inited = true;
1039 }
1040
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001041 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001042 if (dev) {
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001043 parts = fdt_subnode_offset(blob, noff,
1044 "partitions");
1045 if (parts < 0)
1046 parts = noff;
1047
1048 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001049 return; /* return on error */
1050 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001051 }
1052 }
1053}
1054#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001055
Patrick Delaunay019127b2023-06-08 17:16:38 +02001056int fdt_copy_fixed_partitions(void *blob)
1057{
1058 ofnode node, subnode;
1059 int off, suboff, res;
1060 char path[256];
1061 int address_cells, size_cells;
1062 u8 i, j, child_count;
1063
1064 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1065 while (ofnode_valid(node)) {
1066 /* copy the U-Boot fixed partition */
1067 address_cells = ofnode_read_simple_addr_cells(node);
1068 size_cells = ofnode_read_simple_size_cells(node);
1069
1070 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1071 if (res)
1072 return res;
1073
1074 off = fdt_path_offset(blob, path);
1075 if (off < 0)
1076 return -ENODEV;
1077
1078 off = fdt_find_or_add_subnode(blob, off, "partitions");
1079 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1080 if (res)
1081 return res;
1082
1083 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1084 if (res)
1085 return res;
1086
1087 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1088 if (res)
1089 return res;
1090
1091 /*
1092 * parse partition in reverse order as fdt_find_or_add_subnode() only
1093 * insert the new node after the parent's properties
1094 */
1095 child_count = ofnode_get_child_count(node);
1096 for (i = child_count; i > 0 ; i--) {
1097 subnode = ofnode_first_subnode(node);
1098 if (!ofnode_valid(subnode))
1099 break;
1100
1101 for (j = 0; (j < i - 1); j++)
1102 subnode = ofnode_next_subnode(subnode);
1103
1104 if (!ofnode_valid(subnode))
1105 break;
1106
1107 const u32 *reg;
1108 int len;
1109
1110 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1111 res = fdt_setprop_string(blob, suboff, "label",
1112 ofnode_read_string(subnode, "label"));
1113 if (res)
1114 return res;
1115
1116 reg = ofnode_get_property(subnode, "reg", &len);
1117 res = fdt_setprop(blob, suboff, "reg", reg, len);
1118 if (res)
1119 return res;
1120 }
1121
1122 /* go to next fixed-partitions node */
1123 node = ofnode_by_compatible(node, "fixed-partitions");
1124 }
1125
1126 return 0;
1127}
1128
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001129void fdt_del_node_and_alias(void *blob, const char *alias)
1130{
1131 int off = fdt_path_offset(blob, alias);
1132
1133 if (off < 0)
1134 return;
1135
1136 fdt_del_node(blob, off);
1137
1138 off = fdt_path_offset(blob, "/aliases");
1139 fdt_delprop(blob, off, alias);
1140}
Kumar Galab87e7932010-06-16 14:27:38 -05001141
Kumar Galab87e7932010-06-16 14:27:38 -05001142/* Max address size we deal with */
1143#define OF_MAX_ADDR_CELLS 4
Dario Binacchif8fc7032021-05-01 17:05:26 +02001144#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1145 (ns) > 0)
Kumar Galab87e7932010-06-16 14:27:38 -05001146
1147/* Debug utility */
1148#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +00001149static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001150{
1151 printf("%s", s);
1152 while(na--)
1153 printf(" %08x", *(addr++));
1154 printf("\n");
1155}
1156#else
Kim Phillips6542c072013-01-16 14:00:11 +00001157static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001158#endif
1159
Paul Burtondbef2f02016-05-17 07:43:24 +01001160/**
1161 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001162 * @name: A string used to identify this bus in debug output.
1163 * @addresses: The name of the DT property from which addresses are
1164 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001165 * @match: Return non-zero if the node whose parent is at
1166 * parentoffset in the FDT blob corresponds to a bus
1167 * of this type, otherwise return zero. If NULL a match
1168 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001169 * @count_cells:Count how many cells (be32 values) a node whose parent
1170 * is at parentoffset in the FDT blob will require to
1171 * represent its address (written to *addrc) & size
1172 * (written to *sizec).
1173 * @map: Map the address addr from the address space of this
1174 * bus to that of its parent, making use of the ranges
1175 * read from DT to an array at range. na and ns are the
1176 * number of cells (be32 values) used to hold and address
1177 * or size, respectively, for this bus. pna is the number
1178 * of cells used to hold an address for the parent bus.
1179 * Returns the address in the address space of the parent
1180 * bus.
1181 * @translate: Update the value of the address cells at addr within an
1182 * FDT by adding offset to it. na specifies the number of
1183 * cells used to hold the address being translated. Returns
1184 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001185 *
1186 * Each bus type will include a struct of_bus in the of_busses array,
1187 * providing implementations of some or all of the functions used to
1188 * match the bus & handle address translation for its children.
1189 */
Kumar Galab87e7932010-06-16 14:27:38 -05001190struct of_bus {
1191 const char *name;
1192 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001193 int (*match)(const void *blob, int parentoffset);
1194 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001195 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001196 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001197 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001198 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001199};
1200
1201/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001202void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001203 int *addrc, int *sizec)
1204{
Kim Phillips6542c072013-01-16 14:00:11 +00001205 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001206
Simon Glass9fbc6322014-10-23 18:58:57 -06001207 if (addrc)
1208 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001209
1210 if (sizec) {
1211 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1212 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001213 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001214 else
1215 *sizec = 1;
1216 }
Kumar Galab87e7932010-06-16 14:27:38 -05001217}
1218
Kim Phillips6542c072013-01-16 14:00:11 +00001219static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001220 int na, int ns, int pna)
1221{
1222 u64 cp, s, da;
1223
Simon Glassbb7c01e2017-05-18 20:09:26 -06001224 cp = fdt_read_number(range, na);
1225 s = fdt_read_number(range + na + pna, ns);
1226 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001227
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301228 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001229
1230 if (da < cp || da >= (cp + s))
1231 return OF_BAD_ADDR;
1232 return da - cp;
1233}
1234
Kim Phillips6542c072013-01-16 14:00:11 +00001235static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001236{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001237 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001238 memset(addr, 0, na * 4);
1239 a += offset;
1240 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001241 addr[na - 2] = cpu_to_fdt32(a >> 32);
1242 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001243
1244 return 0;
1245}
1246
Paul Burtondbef2f02016-05-17 07:43:24 +01001247#ifdef CONFIG_OF_ISA_BUS
1248
1249/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001250static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001251{
1252 const char *name;
1253
1254 name = fdt_get_name(blob, parentoffset, NULL);
1255 if (!name)
1256 return 0;
1257
1258 return !strcmp(name, "isa");
1259}
1260
Stephen Warrenc99581b2016-08-05 09:47:50 -06001261static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001262 int *addrc, int *sizec)
1263{
1264 if (addrc)
1265 *addrc = 2;
1266 if (sizec)
1267 *sizec = 1;
1268}
1269
1270static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1271 int na, int ns, int pna)
1272{
1273 u64 cp, s, da;
1274
1275 /* Check address type match */
1276 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1277 return OF_BAD_ADDR;
1278
Simon Glassbb7c01e2017-05-18 20:09:26 -06001279 cp = fdt_read_number(range + 1, na - 1);
1280 s = fdt_read_number(range + na + pna, ns);
1281 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001282
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301283 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001284
1285 if (da < cp || da >= (cp + s))
1286 return OF_BAD_ADDR;
1287 return da - cp;
1288}
1289
1290static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1291{
1292 return of_bus_default_translate(addr + 1, offset, na - 1);
1293}
1294
1295#endif /* CONFIG_OF_ISA_BUS */
1296
Kumar Galab87e7932010-06-16 14:27:38 -05001297/* Array of bus specific translators */
1298static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001299#ifdef CONFIG_OF_ISA_BUS
1300 /* ISA */
1301 {
1302 .name = "isa",
1303 .addresses = "reg",
1304 .match = of_bus_isa_match,
1305 .count_cells = of_bus_isa_count_cells,
1306 .map = of_bus_isa_map,
1307 .translate = of_bus_isa_translate,
1308 },
1309#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001310 /* Default */
1311 {
1312 .name = "default",
1313 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001314 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001315 .map = of_bus_default_map,
1316 .translate = of_bus_default_translate,
1317 },
1318};
1319
Stephen Warrenc99581b2016-08-05 09:47:50 -06001320static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001321{
1322 struct of_bus *bus;
1323
1324 if (ARRAY_SIZE(of_busses) == 1)
1325 return of_busses;
1326
1327 for (bus = of_busses; bus; bus++) {
1328 if (!bus->match || bus->match(blob, parentoffset))
1329 return bus;
1330 }
1331
1332 /*
1333 * We should always have matched the default bus at least, since
1334 * it has a NULL match field. If we didn't then it somehow isn't
1335 * in the of_busses array or something equally catastrophic has
1336 * gone wrong.
1337 */
1338 assert(0);
1339 return NULL;
1340}
1341
Stephen Warrenc99581b2016-08-05 09:47:50 -06001342static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001343 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001344 int na, int ns, int pna, const char *rprop)
1345{
Kim Phillips6542c072013-01-16 14:00:11 +00001346 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001347 int rlen;
1348 int rone;
1349 u64 offset = OF_BAD_ADDR;
1350
1351 /* Normally, an absence of a "ranges" property means we are
1352 * crossing a non-translatable boundary, and thus the addresses
1353 * below the current not cannot be converted to CPU physical ones.
1354 * Unfortunately, while this is very clear in the spec, it's not
1355 * what Apple understood, and they do have things like /uni-n or
1356 * /ht nodes with no "ranges" property and a lot of perfectly
1357 * useable mapped devices below them. Thus we treat the absence of
1358 * "ranges" as equivalent to an empty "ranges" property which means
1359 * a 1:1 translation at that level. It's up to the caller not to try
1360 * to translate addresses that aren't supposed to be translated in
1361 * the first place. --BenH.
1362 */
Kim Phillips6542c072013-01-16 14:00:11 +00001363 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001364 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001365 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001366 memset(addr, 0, pna * 4);
1367 debug("OF: no ranges, 1:1 translation\n");
1368 goto finish;
1369 }
1370
1371 debug("OF: walking ranges...\n");
1372
1373 /* Now walk through the ranges */
1374 rlen /= 4;
1375 rone = na + pna + ns;
1376 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1377 offset = bus->map(addr, ranges, na, ns, pna);
1378 if (offset != OF_BAD_ADDR)
1379 break;
1380 }
1381 if (offset == OF_BAD_ADDR) {
1382 debug("OF: not found !\n");
1383 return 1;
1384 }
1385 memcpy(addr, ranges + na, 4 * pna);
1386
1387 finish:
1388 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001389 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001390
1391 /* Translate it into parent bus space */
1392 return pbus->translate(addr, offset, pna);
1393}
1394
1395/*
1396 * Translate an address from the device-tree into a CPU physical address,
1397 * this walks up the tree and applies the various bus mappings on the
1398 * way.
1399 *
1400 * Note: We consider that crossing any level with #size-cells == 0 to mean
1401 * that translation is impossible (that is we are not dealing with a value
1402 * that can be mapped to a cpu physical address). This is not really specified
1403 * that way, but this is traditionally the way IBM at least do things
1404 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001405static u64 __of_translate_address(const void *blob, int node_offset,
1406 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001407{
1408 int parent;
1409 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001410 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001411 int na, ns, pna, pns;
1412 u64 result = OF_BAD_ADDR;
1413
1414 debug("OF: ** translation for device %s **\n",
1415 fdt_get_name(blob, node_offset, NULL));
1416
1417 /* Get parent & match bus type */
1418 parent = fdt_parent_offset(blob, node_offset);
1419 if (parent < 0)
1420 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001421 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001422
1423 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001424 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001425 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001426 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1427 fdt_get_name(blob, node_offset, NULL));
1428 goto bail;
1429 }
1430 memcpy(addr, in_addr, na * 4);
1431
1432 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1433 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1434 of_dump_addr("OF: translating address:", addr, na);
1435
1436 /* Translate */
1437 for (;;) {
1438 /* Switch to parent bus */
1439 node_offset = parent;
1440 parent = fdt_parent_offset(blob, node_offset);
1441
1442 /* If root, we have finished */
1443 if (parent < 0) {
1444 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001445 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001446 break;
1447 }
1448
1449 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001450 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001451 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001452 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001453 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1454 fdt_get_name(blob, node_offset, NULL));
1455 break;
1456 }
1457
1458 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1459 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1460
1461 /* Apply bus translation */
1462 if (of_translate_one(blob, node_offset, bus, pbus,
1463 addr, na, ns, pna, rprop))
1464 break;
1465
1466 /* Complete the move up one level */
1467 na = pna;
1468 ns = pns;
1469 bus = pbus;
1470
1471 of_dump_addr("OF: one level translation:", addr, na);
1472 }
1473 bail:
1474
1475 return result;
1476}
1477
Stephen Warrenc99581b2016-08-05 09:47:50 -06001478u64 fdt_translate_address(const void *blob, int node_offset,
1479 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001480{
1481 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1482}
Kumar Gala800d1d12010-07-04 12:48:21 -05001483
Fabien Dessenne22236e02019-05-31 15:11:30 +02001484u64 fdt_translate_dma_address(const void *blob, int node_offset,
1485 const fdt32_t *in_addr)
1486{
1487 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1488}
1489
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001490int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1491 dma_addr_t *bus, u64 *size)
1492{
1493 bool found_dma_ranges = false;
1494 struct of_bus *bus_node;
1495 const fdt32_t *ranges;
1496 int na, ns, pna, pns;
1497 int parent = node;
1498 int ret = 0;
1499 int len;
1500
1501 /* Find the closest dma-ranges property */
1502 while (parent >= 0) {
1503 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1504
1505 /* Ignore empty ranges, they imply no translation required */
1506 if (ranges && len > 0)
1507 break;
1508
1509 /* Once we find 'dma-ranges', then a missing one is an error */
1510 if (found_dma_ranges && !ranges) {
1511 ret = -EINVAL;
1512 goto out;
1513 }
1514
1515 if (ranges)
1516 found_dma_ranges = true;
1517
1518 parent = fdt_parent_offset(blob, parent);
1519 }
1520
1521 if (!ranges || parent < 0) {
1522 debug("no dma-ranges found for node %s\n",
1523 fdt_get_name(blob, node, NULL));
1524 ret = -ENOENT;
1525 goto out;
1526 }
1527
1528 /* switch to that node */
1529 node = parent;
1530 parent = fdt_parent_offset(blob, node);
1531 if (parent < 0) {
Vagrant Cascadiand00c5c72021-12-21 13:06:58 -08001532 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001533 ret = -EINVAL;
1534 goto out;
1535 }
1536
1537 /* Get the address sizes both for the bus and its parent */
1538 bus_node = of_match_bus(blob, node);
1539 bus_node->count_cells(blob, node, &na, &ns);
1540 if (!OF_CHECK_COUNTS(na, ns)) {
1541 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1542 fdt_get_name(blob, node, NULL));
1543 return -EINVAL;
1544 goto out;
1545 }
1546
1547 bus_node = of_match_bus(blob, parent);
1548 bus_node->count_cells(blob, parent, &pna, &pns);
1549 if (!OF_CHECK_COUNTS(pna, pns)) {
1550 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1551 fdt_get_name(blob, parent, NULL));
1552 return -EINVAL;
1553 goto out;
1554 }
1555
1556 *bus = fdt_read_number(ranges, na);
1557 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1558 *size = fdt_read_number(ranges + na + pna, ns);
1559out:
1560 return ret;
1561}
1562
Kumar Gala800d1d12010-07-04 12:48:21 -05001563/**
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001564 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala800d1d12010-07-04 12:48:21 -05001565 * who's reg property matches a physical cpu address
1566 *
1567 * @blob: ptr to device tree
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001568 * @compat: compatible string to match
Kumar Gala800d1d12010-07-04 12:48:21 -05001569 * @compat_off: property name
1570 *
1571 */
1572int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1573 phys_addr_t compat_off)
1574{
Marek Behún5d6b4482022-01-20 01:04:42 +01001575 int len, off;
1576
1577 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips6542c072013-01-16 14:00:11 +00001578 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún5d6b4482022-01-20 01:04:42 +01001579 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1580 return off;
Kumar Gala800d1d12010-07-04 12:48:21 -05001581 }
1582
1583 return -FDT_ERR_NOTFOUND;
1584}
1585
Marek Behún4ef5ca62021-11-26 14:57:10 +01001586static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1587{
1588 char path[512];
1589 int len;
1590
1591 len = vsnprintf(path, sizeof(path), fmt, ap);
1592 if (len < 0 || len + 1 > sizeof(path))
1593 return -FDT_ERR_NOSPACE;
1594
1595 return fdt_path_offset(blob, path);
1596}
1597
1598/**
1599 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1600 *
1601 * @blob: ptr to device tree
1602 * @fmt: path format
1603 * @ap: vsnprintf arguments
1604 */
1605int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1606{
1607 va_list ap;
1608 int res;
1609
1610 va_start(ap, fmt);
1611 res = vnode_offset_by_pathf(blob, fmt, ap);
1612 va_end(ap);
1613
1614 return res;
1615}
1616
Gerald Van Barenaf737882011-07-14 21:40:10 -04001617/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001618 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001619 *
1620 * @fdt: ptr to device tree
1621 * @nodeoffset: node to update
1622 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001623 */
1624int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001625{
1626 int ret;
1627
1628#ifdef DEBUG
1629 int off = fdt_node_offset_by_phandle(fdt, phandle);
1630
1631 if ((off >= 0) && (off != nodeoffset)) {
1632 char buf[64];
1633
1634 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1635 printf("Trying to update node %s with phandle %u ",
1636 buf, phandle);
1637
1638 fdt_get_path(fdt, off, buf, sizeof(buf));
1639 printf("that already exists in node %s.\n", buf);
1640 return -FDT_ERR_BADPHANDLE;
1641 }
1642#endif
1643
1644 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barenaf737882011-07-14 21:40:10 -04001645
1646 return ret;
1647}
1648
Kumar Galad5fc2582011-08-01 00:25:20 -05001649/*
Marek Behúnae0ef952021-11-26 14:57:09 +01001650 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Galad5fc2582011-08-01 00:25:20 -05001651 *
1652 * @fdt: ptr to device tree
1653 * @nodeoffset: node to update
1654 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001655unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001656{
1657 /* see if there is a phandle already */
Marek Behún3577eba32021-11-26 14:57:07 +01001658 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Galad5fc2582011-08-01 00:25:20 -05001659
1660 /* if we got 0, means no phandle so create one */
1661 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001662 int ret;
1663
Marek Behún3577eba32021-11-26 14:57:07 +01001664 ret = fdt_generate_phandle(fdt, &phandle);
1665 if (ret < 0) {
1666 printf("Can't generate phandle: %s\n",
1667 fdt_strerror(ret));
1668 return 0;
1669 }
1670
Timur Tabi0f46c802011-09-20 18:24:34 -05001671 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1672 if (ret < 0) {
1673 printf("Can't set phandle %u: %s\n", phandle,
1674 fdt_strerror(ret));
1675 return 0;
1676 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001677 }
1678
1679 return phandle;
1680}
1681
Marek Behún4ef5ca62021-11-26 14:57:10 +01001682/**
1683 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1684 * given compatible
1685 *
1686 * @fdt: ptr to device tree
1687 * @compat: node's compatible string
1688 */
1689unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1690{
1691 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1692
1693 if (offset < 0) {
1694 printf("Can't find node with compatible \"%s\": %s\n", compat,
1695 fdt_strerror(offset));
1696 return 0;
1697 }
1698
1699 return fdt_create_phandle(fdt, offset);
1700}
1701
1702/**
1703 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1704 * sprintf-formatted path
1705 *
1706 * @fdt: ptr to device tree
1707 * @fmt, ...: path format string and arguments to pass to sprintf
1708 */
1709unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1710{
1711 va_list ap;
1712 int offset;
1713
1714 va_start(ap, fmt);
1715 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1716 va_end(ap);
1717
1718 if (offset < 0) {
1719 printf("Can't find node by given path: %s\n",
1720 fdt_strerror(offset));
1721 return 0;
1722 }
1723
1724 return fdt_create_phandle(fdt, offset);
1725}
1726
Shengzhou Liua7be8022011-10-14 16:26:05 +08001727/*
1728 * fdt_set_node_status: Set status for the given node
1729 *
1730 * @fdt: ptr to device tree
1731 * @nodeoffset: node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001732 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001733 */
Marek Behúnf872e832021-11-26 14:57:08 +01001734int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001735{
Shengzhou Liua7be8022011-10-14 16:26:05 +08001736 int ret = 0;
1737
1738 if (nodeoffset < 0)
1739 return nodeoffset;
1740
1741 switch (status) {
1742 case FDT_STATUS_OKAY:
1743 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1744 break;
1745 case FDT_STATUS_DISABLED:
1746 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1747 break;
1748 case FDT_STATUS_FAIL:
1749 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1750 break;
Shengzhou Liua7be8022011-10-14 16:26:05 +08001751 default:
1752 printf("Invalid fdt status: %x\n", status);
1753 ret = -1;
1754 break;
1755 }
1756
1757 return ret;
1758}
1759
1760/*
1761 * fdt_set_status_by_alias: Set status for the given node given an alias
1762 *
1763 * @fdt: ptr to device tree
1764 * @alias: alias of node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001765 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001766 */
1767int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behúnf872e832021-11-26 14:57:08 +01001768 enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001769{
1770 int offset = fdt_path_offset(fdt, alias);
1771
Marek Behúnf872e832021-11-26 14:57:08 +01001772 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liua7be8022011-10-14 16:26:05 +08001773}
1774
Marek Behún4ef5ca62021-11-26 14:57:10 +01001775/**
1776 * fdt_set_status_by_compatible: Set node status for first node with given
1777 * compatible
1778 *
1779 * @fdt: ptr to device tree
1780 * @compat: node's compatible string
1781 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1782 */
1783int fdt_set_status_by_compatible(void *fdt, const char *compat,
1784 enum fdt_status status)
1785{
1786 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1787
1788 if (offset < 0)
1789 return offset;
1790
1791 return fdt_set_node_status(fdt, offset, status);
1792}
1793
1794/**
1795 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1796 * path
1797 *
1798 * @fdt: ptr to device tree
1799 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1800 * @fmt, ...: path format string and arguments to pass to sprintf
1801 */
1802int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1803 ...)
1804{
1805 va_list ap;
1806 int offset;
1807
1808 va_start(ap, fmt);
1809 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1810 va_end(ap);
1811
1812 if (offset < 0)
1813 return offset;
1814
1815 return fdt_set_node_status(fdt, offset, status);
1816}
1817
Timur Tabi8ebaf202011-05-03 13:24:07 -05001818/*
1819 * Verify the physical address of device tree node for a given alias
1820 *
1821 * This function locates the device tree node of a given alias, and then
1822 * verifies that the physical address of that device matches the given
1823 * parameter. It displays a message if there is a mismatch.
1824 *
1825 * Returns 1 on success, 0 on failure
1826 */
1827int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1828{
1829 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001830 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001831 int node, len;
1832 u64 dt_addr;
1833
1834 path = fdt_getprop(fdt, anode, alias, NULL);
1835 if (!path) {
1836 /* If there's no such alias, then it's not a failure */
1837 return 1;
1838 }
1839
1840 node = fdt_path_offset(fdt, path);
1841 if (node < 0) {
1842 printf("Warning: device tree alias '%s' points to invalid "
1843 "node %s.\n", alias, path);
1844 return 0;
1845 }
1846
1847 reg = fdt_getprop(fdt, node, "reg", &len);
1848 if (!reg) {
1849 printf("Warning: device tree node '%s' has no address.\n",
1850 path);
1851 return 0;
1852 }
1853
1854 dt_addr = fdt_translate_address(fdt, node, reg);
1855 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001856 printf("Warning: U-Boot configured device %s at address %llu,\n"
1857 "but the device tree has it address %llx.\n",
1858 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001859 return 0;
1860 }
1861
1862 return 1;
1863}
1864
1865/*
1866 * Returns the base address of an SOC or PCI node
1867 */
Simon Glass293340a2017-05-18 20:09:00 -06001868u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001869{
1870 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001871 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001872
Simon Glass29f304b2017-07-04 13:31:20 -06001873 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001874
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001875 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001876}
Alexander Graf19555be2014-04-11 17:09:41 +02001877
1878/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001879 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1880 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001881 */
1882static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1883 uint64_t *val, int cells)
1884{
Bin Mengd67d7d02021-02-25 17:22:24 +08001885 const fdt32_t *prop32;
1886 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001887
1888 if ((cell_off + cells) > prop_len)
1889 return -FDT_ERR_NOSPACE;
1890
Bin Mengd67d7d02021-02-25 17:22:24 +08001891 prop32 = &prop[cell_off];
1892
1893 /*
1894 * Special handling for PCI address in PCI bus <ranges>
1895 *
1896 * PCI child address is made up of 3 cells. Advance the cell offset
1897 * by 1 so that the PCI child address can be correctly read.
1898 */
1899 if (cells == 3)
1900 cell_off += 1;
1901 prop64 = (const fdt64_t *)&prop[cell_off];
1902
Alexander Graf19555be2014-04-11 17:09:41 +02001903 switch (cells) {
1904 case 1:
1905 *val = fdt32_to_cpu(*prop32);
1906 break;
1907 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001908 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001909 *val = fdt64_to_cpu(*prop64);
1910 break;
1911 default:
1912 return -FDT_ERR_NOSPACE;
1913 }
1914
1915 return 0;
1916}
1917
1918/**
1919 * fdt_read_range - Read a node's n'th range property
1920 *
1921 * @fdt: ptr to device tree
1922 * @node: offset of node
1923 * @n: range index
1924 * @child_addr: pointer to storage for the "child address" field
1925 * @addr: pointer to storage for the CPU view translated physical start
1926 * @len: pointer to storage for the range length
1927 *
1928 * Convenience function that reads and interprets a specific range out of
1929 * a number of the "ranges" property array.
1930 */
1931int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1932 uint64_t *addr, uint64_t *len)
1933{
1934 int pnode = fdt_parent_offset(fdt, node);
1935 const fdt32_t *ranges;
1936 int pacells;
1937 int acells;
1938 int scells;
1939 int ranges_len;
1940 int cell = 0;
1941 int r = 0;
1942
1943 /*
1944 * The "ranges" property is an array of
1945 * { <child address> <parent address> <size in child address space> }
1946 *
1947 * All 3 elements can span a diffent number of cells. Fetch their size.
1948 */
1949 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1950 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1951 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1952
1953 /* Now try to get the ranges property */
1954 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1955 if (!ranges)
1956 return -FDT_ERR_NOTFOUND;
1957 ranges_len /= sizeof(uint32_t);
1958
1959 /* Jump to the n'th entry */
1960 cell = n * (pacells + acells + scells);
1961
1962 /* Read <child address> */
1963 if (child_addr) {
1964 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1965 acells);
1966 if (r)
1967 return r;
1968 }
1969 cell += acells;
1970
1971 /* Read <parent address> */
1972 if (addr)
1973 *addr = fdt_translate_address(fdt, node, ranges + cell);
1974 cell += pacells;
1975
1976 /* Read <size in child address space> */
1977 if (len) {
1978 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1979 if (r)
1980 return r;
1981 }
1982
1983 return 0;
1984}
Hans de Goedeea876372014-11-17 15:29:11 +01001985
1986/**
1987 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1988 *
1989 * @fdt: ptr to device tree
1990 * @node: offset of the simplefb node
1991 * @base_address: framebuffer base address
1992 * @width: width in pixels
1993 * @height: height in pixels
1994 * @stride: bytes per line
1995 * @format: pixel format string
1996 *
1997 * Convenience function to fill and enable a simplefb node.
1998 */
1999int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2000 u32 height, u32 stride, const char *format)
2001{
2002 char name[32];
2003 fdt32_t cells[4];
2004 int i, addrc, sizec, ret;
2005
Simon Glassbb7c01e2017-05-18 20:09:26 -06002006 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2007 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01002008 i = 0;
2009 if (addrc == 2)
2010 cells[i++] = cpu_to_fdt32(base_address >> 32);
2011 cells[i++] = cpu_to_fdt32(base_address);
2012 if (sizec == 2)
2013 cells[i++] = 0;
2014 cells[i++] = cpu_to_fdt32(height * stride);
2015
2016 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2017 if (ret < 0)
2018 return ret;
2019
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002020 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01002021 ret = fdt_set_name(fdt, node, name);
2022 if (ret < 0)
2023 return ret;
2024
2025 ret = fdt_setprop_u32(fdt, node, "width", width);
2026 if (ret < 0)
2027 return ret;
2028
2029 ret = fdt_setprop_u32(fdt, node, "height", height);
2030 if (ret < 0)
2031 return ret;
2032
2033 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2034 if (ret < 0)
2035 return ret;
2036
2037 ret = fdt_setprop_string(fdt, node, "format", format);
2038 if (ret < 0)
2039 return ret;
2040
2041 ret = fdt_setprop_string(fdt, node, "status", "okay");
2042 if (ret < 0)
2043 return ret;
2044
2045 return 0;
2046}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002047
Devarsh Thakkar31199892024-02-22 18:38:10 +05302048#if CONFIG_IS_ENABLED(VIDEO)
2049int fdt_add_fb_mem_rsv(void *blob)
2050{
2051 struct fdt_memory mem;
2052
2053 /* nothing to do when the frame buffer is not defined */
2054 if (gd->video_bottom == gd->video_top)
2055 return 0;
2056
2057 /* reserved with no-map tag the video buffer */
2058 mem.start = gd->video_bottom;
2059 mem.end = gd->video_top - 1;
2060
2061 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2062 FDTDEC_RESERVED_MEMORY_NO_MAP);
2063}
2064#endif
2065
Tim Harvey5d7c6b52015-04-08 11:45:39 -07002066/*
2067 * Update native-mode in display-timings from display environment variable.
2068 * The node to update are specified by path.
2069 */
2070int fdt_fixup_display(void *blob, const char *path, const char *display)
2071{
2072 int off, toff;
2073
2074 if (!display || !path)
2075 return -FDT_ERR_NOTFOUND;
2076
2077 toff = fdt_path_offset(blob, path);
2078 if (toff >= 0)
2079 toff = fdt_subnode_offset(blob, toff, "display-timings");
2080 if (toff < 0)
2081 return toff;
2082
2083 for (off = fdt_first_subnode(blob, toff);
2084 off >= 0;
2085 off = fdt_next_subnode(blob, off)) {
2086 uint32_t h = fdt_get_phandle(blob, off);
2087 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2088 fdt32_to_cpu(h));
2089 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2090 return fdt_setprop_u32(blob, toff, "native-mode", h);
2091 }
2092 return toff;
2093}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002094
2095#ifdef CONFIG_OF_LIBFDT_OVERLAY
2096/**
2097 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2098 *
2099 * @fdt: ptr to device tree
2100 * @fdto: ptr to device tree overlay
2101 *
2102 * Convenience function to apply an overlay and display helpful messages
2103 * in the case of an error
2104 */
2105int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2106{
2107 int err;
2108 bool has_symbols;
2109
2110 err = fdt_path_offset(fdt, "/__symbols__");
2111 has_symbols = err >= 0;
2112
2113 err = fdt_overlay_apply(fdt, fdto);
2114 if (err < 0) {
2115 printf("failed on fdt_overlay_apply(): %s\n",
2116 fdt_strerror(err));
2117 if (!has_symbols) {
Hugo Villeneuve90730f12023-10-26 15:54:49 -04002118 printf("base fdt does not have a /__symbols__ node\n");
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002119 printf("make sure you've compiled with -@\n");
2120 }
2121 }
2122 return err;
2123}
2124#endif
Kory Maincent623e1ac2021-05-04 19:31:21 +02002125
2126/**
2127 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2128 *
2129 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002130 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincent623e1ac2021-05-04 19:31:21 +02002131 */
2132int fdt_valid(struct fdt_header **blobp)
2133{
2134 const void *blob = *blobp;
2135 int err;
2136
2137 if (!blob) {
2138 printf("The address of the fdt is invalid (NULL).\n");
2139 return 0;
2140 }
2141
2142 err = fdt_check_header(blob);
2143 if (err == 0)
2144 return 1; /* valid */
2145
2146 if (err < 0) {
2147 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2148 /*
2149 * Be more informative on bad version.
2150 */
2151 if (err == -FDT_ERR_BADVERSION) {
2152 if (fdt_version(blob) <
2153 FDT_FIRST_SUPPORTED_VERSION) {
2154 printf(" - too old, fdt %d < %d",
2155 fdt_version(blob),
2156 FDT_FIRST_SUPPORTED_VERSION);
2157 }
2158 if (fdt_last_comp_version(blob) >
2159 FDT_LAST_SUPPORTED_VERSION) {
2160 printf(" - too new, fdt %d > %d",
2161 fdt_version(blob),
2162 FDT_LAST_SUPPORTED_VERSION);
2163 }
2164 }
2165 printf("\n");
2166 *blobp = NULL;
2167 return 0;
2168 }
2169 return 1;
2170}