blob: e624bbdf404233b95e2ef0373f80dbbf45f5f0de [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
9#include <common.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>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040015#include <linux/ctype.h>
16#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040017#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040019#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060020#include <exports.h>
Bin Mengd9a00932015-12-07 01:39:47 -080021#include <fdtdec.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040022
Dario Binacchib574d682020-12-30 00:16:21 +010023DECLARE_GLOBAL_DATA_PTR;
24
Kumar Gala1ba00402008-10-23 00:05:47 -050025/**
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020026 * fdt_getprop_u32_default_node - Return a node's property or a default
27 *
28 * @fdt: ptr to device tree
29 * @off: offset of node
30 * @cell: cell offset in property
31 * @prop: property name
32 * @dflt: default value if the property isn't found
33 *
34 * Convenience function to return a node's property or a default value if
35 * the property doesn't exist.
36 */
37u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
38 const char *prop, const u32 dflt)
39{
40 const fdt32_t *val;
41 int len;
42
43 val = fdt_getprop(fdt, off, prop, &len);
44
45 /* Check if property exists */
46 if (!val)
47 return dflt;
48
49 /* Check if property is long enough */
50 if (len < ((cell + 1) * sizeof(uint32_t)))
51 return dflt;
52
53 return fdt32_to_cpu(*val);
54}
55
56/**
Kumar Gala1ba00402008-10-23 00:05:47 -050057 * fdt_getprop_u32_default - Find a node and return it's property or a default
58 *
59 * @fdt: ptr to device tree
60 * @path: path of node
61 * @prop: property name
62 * @dflt: default value if the property isn't found
63 *
64 * Convenience function to find a node and return it's property or a
65 * default value if it doesn't exist.
66 */
Gabe Blackcd37de72011-11-08 01:09:44 -080067u32 fdt_getprop_u32_default(const void *fdt, const char *path,
68 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050069{
Kumar Gala1ba00402008-10-23 00:05:47 -050070 int off;
71
72 off = fdt_path_offset(fdt, path);
73 if (off < 0)
74 return dflt;
75
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020076 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala1ba00402008-10-23 00:05:47 -050077}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040078
Kumar Galabf771dc2007-10-24 10:21:57 -050079/**
80 * fdt_find_and_setprop: Find a node and set it's property
81 *
82 * @fdt: ptr to device tree
83 * @node: path of node
84 * @prop: property name
85 * @val: ptr to new value
86 * @len: length of new property value
87 * @create: flag to create the property if it doesn't exist
88 *
89 * Convenience function to directly set a property given the path to the node.
90 */
91int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
92 const void *val, int len, int create)
93{
Kumar Galac8ab7052007-10-24 11:04:22 -050094 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050095
96 if (nodeoff < 0)
97 return nodeoff;
98
Kim Phillips6542c072013-01-16 14:00:11 +000099 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500100 return 0; /* create flag not set; so exit quietly */
101
102 return fdt_setprop(fdt, nodeoff, prop, val, len);
103}
104
Masahiro Yamadac0207282014-04-18 17:40:58 +0900105/**
Simon Glass014aa052014-10-23 18:58:49 -0600106 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
107 *
Masahiro Yamadac0207282014-04-18 17:40:58 +0900108 * @fdt: pointer to the device tree blob
109 * @parentoffset: structure block offset of a node
110 * @name: name of the subnode to locate
111 *
112 * fdt_subnode_offset() finds a subnode of the node with a given name.
113 * If the subnode does not exist, it will be created.
114 */
Simon Glass014aa052014-10-23 18:58:49 -0600115int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamadac0207282014-04-18 17:40:58 +0900116{
117 int offset;
118
119 offset = fdt_subnode_offset(fdt, parentoffset, name);
120
121 if (offset == -FDT_ERR_NOTFOUND)
122 offset = fdt_add_subnode(fdt, parentoffset, name);
123
124 if (offset < 0)
125 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
126
127 return offset;
128}
129
Andre Przywara0d1fcf82021-02-14 10:35:18 +0000130#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel46e192a2008-06-20 22:24:05 +0200131static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600132{
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900133 int err;
134 int aliasoff;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600135 char sername[9] = { 0 };
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900136 const void *path;
137 int len;
138 char tmp[256]; /* long enough */
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600139
Bin Mengb8199112016-01-13 19:38:58 -0800140 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600141
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900142 aliasoff = fdt_path_offset(fdt, "/aliases");
143 if (aliasoff < 0) {
144 err = aliasoff;
Scott Woodedd551f2015-09-01 22:48:08 -0500145 goto noalias;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600146 }
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900147
148 path = fdt_getprop(fdt, aliasoff, sername, &len);
149 if (!path) {
150 err = len;
Scott Woodedd551f2015-09-01 22:48:08 -0500151 goto noalias;
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900152 }
153
154 /* fdt_setprop may break "path" so we copy it to tmp buffer */
155 memcpy(tmp, path, len);
156
157 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600158 if (err < 0)
159 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900160 fdt_strerror(err));
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600161
162 return err;
Scott Woodedd551f2015-09-01 22:48:08 -0500163
164noalias:
165 printf("WARNING: %s: could not read %s alias: %s\n",
166 __func__, sername, fdt_strerror(err));
167
168 return 0;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600169}
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900170#else
171static int fdt_fixup_stdout(void *fdt, int chosenoff)
172{
173 return 0;
174}
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600175#endif
176
Masahiro Yamadab891a342014-04-18 17:41:04 +0900177static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
178 uint64_t val, int is_u64)
179{
180 if (is_u64)
181 return fdt_setprop_u64(fdt, nodeoffset, name, val);
182 else
183 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
184}
185
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200186int fdt_root(void *fdt)
187{
188 char *serial;
189 int err;
190
191 err = fdt_check_header(fdt);
192 if (err < 0) {
193 printf("fdt_root: %s\n", fdt_strerror(err));
194 return err;
195 }
196
Simon Glass64b723f2017-08-03 12:22:12 -0600197 serial = env_get("serial#");
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200198 if (serial) {
199 err = fdt_setprop(fdt, 0, "serial-number", serial,
200 strlen(serial) + 1);
201
202 if (err < 0) {
203 printf("WARNING: could not set serial-number %s.\n",
204 fdt_strerror(err));
205 return err;
206 }
207 }
208
209 return 0;
210}
Masahiro Yamadab891a342014-04-18 17:41:04 +0900211
Masahiro Yamada5b114892014-04-18 17:40:59 +0900212int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400213{
Masahiro Yamadab891a342014-04-18 17:41:04 +0900214 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500215 int err, j, total;
Masahiro Yamadab891a342014-04-18 17:41:04 +0900216 int is_u64;
Kumar Gala99ebc052008-08-15 08:24:43 -0500217 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400218
Masahiro Yamada9a3b4de2014-04-18 17:41:05 +0900219 /* just return if the size of initrd is zero */
220 if (initrd_start == initrd_end)
221 return 0;
222
Masahiro Yamadac0207282014-04-18 17:40:58 +0900223 /* find or create "/chosen" node. */
224 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
225 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500226 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400227
Kumar Gala99ebc052008-08-15 08:24:43 -0500228 total = fdt_num_mem_rsv(fdt);
229
230 /*
231 * Look for an existing entry and update it. If we don't find
232 * the entry, we will j be the next available slot.
233 */
234 for (j = 0; j < total; j++) {
235 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
236 if (addr == initrd_start) {
237 fdt_del_mem_rsv(fdt, j);
238 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400239 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500240 }
241
Grant Likelyc379a562011-03-28 09:58:55 +0000242 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500243 if (err < 0) {
244 printf("fdt_initrd: %s\n", fdt_strerror(err));
245 return err;
246 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500247
Simon Glass9fbc6322014-10-23 18:58:57 -0600248 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengd9850132013-12-14 11:47:29 +0800249
Masahiro Yamadab891a342014-04-18 17:41:04 +0900250 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
251 (uint64_t)initrd_start, is_u64);
252
Masahiro Yamada5b114892014-04-18 17:40:59 +0900253 if (err < 0) {
254 printf("WARNING: could not set linux,initrd-start %s.\n",
255 fdt_strerror(err));
256 return err;
257 }
Masahiro Yamadab891a342014-04-18 17:41:04 +0900258
259 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
260 (uint64_t)initrd_end, is_u64);
261
Masahiro Yamada5b114892014-04-18 17:40:59 +0900262 if (err < 0) {
263 printf("WARNING: could not set linux,initrd-end %s.\n",
264 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500265
Masahiro Yamada5b114892014-04-18 17:40:59 +0900266 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400267 }
268
Kumar Gala99ebc052008-08-15 08:24:43 -0500269 return 0;
270}
271
Masahiro Yamada451c2042014-04-18 17:41:00 +0900272int fdt_chosen(void *fdt)
Kumar Gala99ebc052008-08-15 08:24:43 -0500273{
274 int nodeoffset;
275 int err;
276 char *str; /* used to set string properties */
Kumar Gala99ebc052008-08-15 08:24:43 -0500277
278 err = fdt_check_header(fdt);
279 if (err < 0) {
280 printf("fdt_chosen: %s\n", fdt_strerror(err));
281 return err;
282 }
283
Masahiro Yamadac0207282014-04-18 17:40:58 +0900284 /* find or create "/chosen" node. */
285 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
286 if (nodeoffset < 0)
287 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400288
Simon Glass64b723f2017-08-03 12:22:12 -0600289 str = env_get("bootargs");
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900290 if (str) {
291 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
292 strlen(str) + 1);
293 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900294 printf("WARNING: could not set bootargs %s.\n",
295 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900296 return err;
297 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400298 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600299
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900300 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400301}
302
Kumar Gala7e64cf82007-11-03 19:46:28 -0500303void do_fixup_by_path(void *fdt, const char *path, const char *prop,
304 const void *val, int len, int create)
305{
306#if defined(DEBUG)
307 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600308 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500309 for (i = 0; i < len; i++)
310 debug(" %.2x", *(u8*)(val+i));
311 debug("\n");
312#endif
313 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
314 if (rc)
315 printf("Unable to update property %s:%s, err=%s\n",
316 path, prop, fdt_strerror(rc));
317}
318
319void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
320 u32 val, int create)
321{
Kim Phillips6542c072013-01-16 14:00:11 +0000322 fdt32_t tmp = cpu_to_fdt32(val);
323 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500324}
325
Kumar Gala7590a192007-11-21 13:30:15 -0600326void do_fixup_by_prop(void *fdt,
327 const char *pname, const void *pval, int plen,
328 const char *prop, const void *val, int len,
329 int create)
330{
331 int off;
332#if defined(DEBUG)
333 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600334 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600335 for (i = 0; i < len; i++)
336 debug(" %.2x", *(u8*)(val+i));
337 debug("\n");
338#endif
339 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
340 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000341 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600342 fdt_setprop(fdt, off, prop, val, len);
343 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
344 }
345}
346
347void do_fixup_by_prop_u32(void *fdt,
348 const char *pname, const void *pval, int plen,
349 const char *prop, u32 val, int create)
350{
Kim Phillips6542c072013-01-16 14:00:11 +0000351 fdt32_t tmp = cpu_to_fdt32(val);
352 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600353}
354
355void do_fixup_by_compat(void *fdt, const char *compat,
356 const char *prop, const void *val, int len, int create)
357{
358 int off = -1;
359#if defined(DEBUG)
360 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600361 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600362 for (i = 0; i < len; i++)
363 debug(" %.2x", *(u8*)(val+i));
364 debug("\n");
365#endif
366 off = fdt_node_offset_by_compatible(fdt, -1, compat);
367 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000368 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600369 fdt_setprop(fdt, off, prop, val, len);
370 off = fdt_node_offset_by_compatible(fdt, off, compat);
371 }
372}
373
374void do_fixup_by_compat_u32(void *fdt, const char *compat,
375 const char *prop, u32 val, int create)
376{
Kim Phillips6542c072013-01-16 14:00:11 +0000377 fdt32_t tmp = cpu_to_fdt32(val);
378 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600379}
380
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900381#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900382/*
383 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
384 */
Simon Glass0058e112014-10-23 18:58:55 -0600385static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
386 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900387{
388 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100389 int address_cells = fdt_address_cells(fdt, 0);
390 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900391 char *p = buf;
392
393 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100394 if (address_cells == 2)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900395 *(fdt64_t *)p = cpu_to_fdt64(address[i]);
396 else
397 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100398 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900399
Hans de Goede4c80fb02014-11-28 14:23:51 +0100400 if (size_cells == 2)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900401 *(fdt64_t *)p = cpu_to_fdt64(size[i]);
402 else
403 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100404 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900405 }
406
407 return p - (char *)buf;
408}
409
Ramon Fried9cab1832018-08-14 00:35:42 +0300410#if CONFIG_NR_DRAM_BANKS > 4
411#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
412#else
Kim Phillips6542c072013-01-16 14:00:11 +0000413#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300414#endif
John Rigbyb4ae9422010-10-13 13:57:33 -0600415int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
416{
417 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100418 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000419 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600420
Kim Phillips6542c072013-01-16 14:00:11 +0000421 if (banks > MEMORY_BANKS_MAX) {
422 printf("%s: num banks %d exceeds hardcoded limit %d."
423 " Recompile with higher MEMORY_BANKS_MAX?\n",
424 __FUNCTION__, banks, MEMORY_BANKS_MAX);
425 return -1;
426 }
427
Kumar Gala0db72ed2007-11-26 14:57:45 -0600428 err = fdt_check_header(blob);
429 if (err < 0) {
430 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
431 return err;
432 }
433
Masahiro Yamadac0207282014-04-18 17:40:58 +0900434 /* find or create "/memory" node. */
435 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
436 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800437 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900438
Kumar Gala0db72ed2007-11-26 14:57:45 -0600439 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
440 sizeof("memory"));
441 if (err < 0) {
442 printf("WARNING: could not set %s %s.\n", "device_type",
443 fdt_strerror(err));
444 return err;
445 }
446
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100447 for (i = 0; i < banks; i++) {
448 if (start[i] == 0 && size[i] == 0)
449 break;
450 }
451
452 banks = i;
453
Andre Przywara3d2d4412015-06-21 00:29:54 +0100454 if (!banks)
455 return 0;
456
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900457 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600458
459 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
460 if (err < 0) {
461 printf("WARNING: could not set %s %s.\n",
462 "reg", fdt_strerror(err));
463 return err;
464 }
465 return 0;
466}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200467
468int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
469{
470 int err, nodeoffset;
471 int len;
472 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
473
474 if (areas > 8) {
475 printf("%s: num areas %d exceeds hardcoded limit %d\n",
476 __func__, areas, 8);
477 return -1;
478 }
479
480 err = fdt_check_header(blob);
481 if (err < 0) {
482 printf("%s: %s\n", __func__, fdt_strerror(err));
483 return err;
484 }
485
486 /* find or create "/memory" node. */
487 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
488 if (nodeoffset < 0)
489 return nodeoffset;
490
491 len = fdt_pack_reg(blob, tmp, start, size, areas);
492
493 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
494 if (err < 0) {
495 printf("WARNING: could not set %s %s.\n",
496 "reg", fdt_strerror(err));
497 return err;
498 }
499
500 return 0;
501}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900502#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600503
John Rigbyb4ae9422010-10-13 13:57:33 -0600504int fdt_fixup_memory(void *blob, u64 start, u64 size)
505{
506 return fdt_fixup_memory_banks(blob, &start, &size, 1);
507}
508
Kumar Galafabda922008-08-19 15:41:18 -0500509void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600510{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530511 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800512 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000513 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600514 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100515 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800516 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530517#ifdef FDT_SEQ_MACADDR_FROM_ENV
518 int nodeoff;
519 const struct fdt_property *fdt_prop;
520#endif
Kumar Gala22699102007-11-21 11:11:03 -0600521
Lev Iserovichcde77612016-01-07 18:04:16 -0500522 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500523 return;
524
Lev Iserovichcde77612016-01-07 18:04:16 -0500525 /* Cycle through all aliases */
526 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800527 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500528
Lev Iserovichcde77612016-01-07 18:04:16 -0500529 /* FDT might have been edited, recompute the offset */
530 offset = fdt_first_property_offset(fdt,
531 fdt_path_offset(fdt, "/aliases"));
532 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530533 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500534 offset = fdt_next_property_offset(fdt, offset);
535
536 if (offset < 0)
537 break;
538
Bin Mengbfb93cc2015-11-03 04:24:41 -0800539 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200540 if (!strncmp(name, "ethernet", 8)) {
541 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530542 if (!strcmp(name, "ethernet")
543#ifdef FDT_SEQ_MACADDR_FROM_ENV
544 || !strcmp(name, "ethernet0")
545#endif
546 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200547 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530548#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200549 else
550 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530551#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800552 if (i != -1) {
553 if (i == 0)
554 strcpy(mac, "ethaddr");
555 else
556 sprintf(mac, "eth%daddr", i);
557 } else {
558 continue;
559 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530560#ifdef FDT_SEQ_MACADDR_FROM_ENV
561 nodeoff = fdt_path_offset(fdt, path);
562 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
563 NULL);
564 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
565 continue;
566 i++;
567#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600568 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800569 if (!tmp)
570 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500571
Bin Mengbfb93cc2015-11-03 04:24:41 -0800572 for (j = 0; j < 6; j++) {
573 mac_addr[j] = tmp ?
574 simple_strtoul(tmp, &end, 16) : 0;
575 if (tmp)
576 tmp = (*end) ? end + 1 : end;
577 }
Kumar Galafabda922008-08-19 15:41:18 -0500578
Bin Mengbfb93cc2015-11-03 04:24:41 -0800579 do_fixup_by_path(fdt, path, "mac-address",
580 &mac_addr, 6, 0);
581 do_fixup_by_path(fdt, path, "local-mac-address",
582 &mac_addr, 6, 1);
583 }
Kumar Gala22699102007-11-21 11:11:03 -0600584 }
585}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300586
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100587int fdt_record_loadable(void *blob, u32 index, const char *name,
588 uintptr_t load_addr, u32 size, uintptr_t entry_point,
589 const char *type, const char *os)
590{
591 int err, node;
592
593 err = fdt_check_header(blob);
594 if (err < 0) {
595 printf("%s: %s\n", __func__, fdt_strerror(err));
596 return err;
597 }
598
599 /* find or create "/fit-images" node */
600 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
601 if (node < 0)
602 return node;
603
604 /* find or create "/fit-images/<name>" node */
605 node = fdt_find_or_add_subnode(blob, node, name);
606 if (node < 0)
607 return node;
608
Michal Simekd31b40f2020-09-03 12:44:51 +0200609 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100610 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200611 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100612 fdt_setprop_u32(blob, node, "size", size);
613 if (type)
614 fdt_setprop_string(blob, node, "type", type);
615 if (os)
616 fdt_setprop_string(blob, node, "os", os);
617
618 return node;
619}
620
Kumar Galaf2f58c52008-08-15 08:24:42 -0500621/* Resize the fdt to its actual size + a bit of padding */
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200622int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500623{
624 int i;
625 uint64_t addr, size;
626 int total, ret;
627 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200628 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500629
630 if (!blob)
631 return 0;
632
633 total = fdt_num_mem_rsv(blob);
634 for (i = 0; i < total; i++) {
635 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000636 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500637 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200638 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500639 break;
640 }
641 }
642
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100643 /*
644 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200645 * plus the size needed for 5 fdt_add_mem_rsv, one
646 * for the fdt itself and 4 for a possible initrd
647 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100648 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500649 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200650 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500651
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200652 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500653 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000654 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
655 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500656
657 /* Change the fdt header to reflect the correct size */
658 fdt_set_totalsize(blob, actualsize);
659
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200660 if (fdt_memrsv) {
661 /* Add the new reservation */
662 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
663 if (ret < 0)
664 return ret;
665 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500666
667 return actualsize;
668}
Kumar Galae0475cd2008-10-22 23:33:56 -0500669
670#ifdef CONFIG_PCI
Kumar Galabaf41892009-08-05 09:03:54 -0500671#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500672
673#define FDT_PCI_PREFETCH (0x40000000)
674#define FDT_PCI_MEM32 (0x02000000)
675#define FDT_PCI_IO (0x01000000)
676#define FDT_PCI_MEM64 (0x03000000)
677
678int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
679
680 int addrcell, sizecell, len, r;
681 u32 *dma_range;
682 /* sized based on pci addr cells, size-cells, & address-cells */
683 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
684
685 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
686 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
687
688 dma_range = &dma_ranges[0];
689 for (r = 0; r < hose->region_count; r++) {
690 u64 bus_start, phys_start, size;
691
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600692 /* skip if !PCI_REGION_SYS_MEMORY */
693 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500694 continue;
695
696 bus_start = (u64)hose->regions[r].bus_start;
697 phys_start = (u64)hose->regions[r].phys_start;
698 size = (u64)hose->regions[r].size;
699
700 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500701 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200702 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500703 else
Marek Vasut22203f52019-07-09 02:49:29 +0200704 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500705 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200706 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500707#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200708 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500709#else
710 dma_range[1] = 0;
711#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200712 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500713
714 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200715 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
716 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500717 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200718 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500719 }
720
721 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200722 dma_range[3 + addrcell + 0] =
723 cpu_to_fdt32(size >> 32);
724 dma_range[3 + addrcell + 1] =
725 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500726 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200727 dma_range[3 + addrcell + 0] =
728 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500729 }
730
731 dma_range += (3 + addrcell + sizecell);
732 }
733
734 len = dma_range - &dma_ranges[0];
735 if (len)
736 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
737
738 return 0;
739}
740#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200741
Matthew McClintockacda3a42010-10-13 13:39:26 +0200742int fdt_increase_size(void *fdt, int add_len)
743{
744 int newlen;
745
746 newlen = fdt_totalsize(fdt) + add_len;
747
748 /* Open in place with a new len */
749 return fdt_open_into(fdt, fdt, newlen);
750}
751
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100752#ifdef CONFIG_FDT_FIXUP_PARTITIONS
753#include <jffs2/load_kernel.h>
754#include <mtd_node.h>
755
Michal Simekc2ed0072018-07-31 14:31:04 +0200756static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100757{
758 int off, ndepth;
759 int ret;
760
761 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
762 (off >= 0) && (ndepth > 0);
763 off = fdt_next_node(blob, off, &ndepth)) {
764 if (ndepth == 1) {
765 debug("delete %s: offset: %x\n",
766 fdt_get_name(blob, off, 0), off);
767 ret = fdt_del_node((void *)blob, off);
768 if (ret < 0) {
769 printf("Can't delete node: %s\n",
770 fdt_strerror(ret));
771 return ret;
772 } else {
773 ndepth = 0;
774 off = parent_offset;
775 }
776 }
777 }
778 return 0;
779}
780
Michal Simekc2ed0072018-07-31 14:31:04 +0200781static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100782{
783 const void *prop;
784 int ndepth = 0;
785 int off;
786 int ret;
787
788 off = fdt_next_node(blob, parent_offset, &ndepth);
789 if (off > 0 && ndepth == 1) {
790 prop = fdt_getprop(blob, off, "label", NULL);
791 if (prop == NULL) {
792 /*
793 * Could not find label property, nand {}; node?
794 * Check subnode, delete partitions there if any.
795 */
796 return fdt_del_partitions(blob, off);
797 } else {
798 ret = fdt_del_subnodes(blob, parent_offset);
799 if (ret < 0) {
800 printf("Can't remove subnodes: %s\n",
801 fdt_strerror(ret));
802 return ret;
803 }
804 }
805 }
806 return 0;
807}
808
Masahiro Yamada65669602020-07-15 19:35:47 +0900809static int fdt_node_set_part_info(void *blob, int parent_offset,
810 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100811{
812 struct list_head *pentry;
813 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100814 int off, ndepth = 0;
815 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300816 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100817 char buf[64];
818
819 ret = fdt_del_partitions(blob, parent_offset);
820 if (ret < 0)
821 return ret;
822
823 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300824 * Check if size/address is 1 or 2 cells.
825 * We assume #address-cells and #size-cells have same value.
826 */
827 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
828 0, "#size-cells", 1);
829
830 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100831 * Check if it is nand {}; subnode, adjust
832 * the offset in this case
833 */
834 off = fdt_next_node(blob, parent_offset, &ndepth);
835 if (off > 0 && ndepth == 1)
836 parent_offset = off;
837
838 part_num = 0;
839 list_for_each_prev(pentry, &dev->parts) {
840 int newoff;
841
842 part = list_entry(pentry, struct part_info, link);
843
Scott Woodefe7f302013-10-15 17:41:27 -0500844 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100845 part_num, part->name, part->size,
846 part->offset, part->mask_flags);
847
Scott Woodefe7f302013-10-15 17:41:27 -0500848 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100849add_sub:
850 ret = fdt_add_subnode(blob, parent_offset, buf);
851 if (ret == -FDT_ERR_NOSPACE) {
852 ret = fdt_increase_size(blob, 512);
853 if (!ret)
854 goto add_sub;
855 else
856 goto err_size;
857 } else if (ret < 0) {
858 printf("Can't add partition node: %s\n",
859 fdt_strerror(ret));
860 return ret;
861 }
862 newoff = ret;
863
864 /* Check MTD_WRITEABLE_CMD flag */
865 if (part->mask_flags & 1) {
866add_ro:
867 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
868 if (ret == -FDT_ERR_NOSPACE) {
869 ret = fdt_increase_size(blob, 512);
870 if (!ret)
871 goto add_ro;
872 else
873 goto err_size;
874 } else if (ret < 0)
875 goto err_prop;
876 }
877
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100878add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300879 if (sizecell == 2) {
880 ret = fdt_setprop_u64(blob, newoff,
881 "reg", part->offset);
882 if (!ret)
883 ret = fdt_appendprop_u64(blob, newoff,
884 "reg", part->size);
885 } else {
886 ret = fdt_setprop_u32(blob, newoff,
887 "reg", part->offset);
888 if (!ret)
889 ret = fdt_appendprop_u32(blob, newoff,
890 "reg", part->size);
891 }
892
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100893 if (ret == -FDT_ERR_NOSPACE) {
894 ret = fdt_increase_size(blob, 512);
895 if (!ret)
896 goto add_reg;
897 else
898 goto err_size;
899 } else if (ret < 0)
900 goto err_prop;
901
902add_label:
903 ret = fdt_setprop_string(blob, newoff, "label", part->name);
904 if (ret == -FDT_ERR_NOSPACE) {
905 ret = fdt_increase_size(blob, 512);
906 if (!ret)
907 goto add_label;
908 else
909 goto err_size;
910 } else if (ret < 0)
911 goto err_prop;
912
913 part_num++;
914 }
915 return 0;
916err_size:
917 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
918 return ret;
919err_prop:
920 printf("Can't add property: %s\n", fdt_strerror(ret));
921 return ret;
922}
923
924/*
925 * Update partitions in nor/nand nodes using info from
926 * mtdparts environment variable. The nodes to update are
927 * specified by node_info structure which contains mtd device
928 * type and compatible string: E. g. the board code in
929 * ft_board_setup() could use:
930 *
931 * struct node_info nodes[] = {
932 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
933 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
934 * };
935 *
936 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
937 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +0900938void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
939 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100940{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100941 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100942 int i, idx;
943 int noff;
Masahiro Yamada030348f2020-07-17 10:46:18 +0900944 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100945
946 for (i = 0; i < node_info_size; i++) {
947 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +0900948 noff = -1;
949
950 while ((noff = fdt_node_offset_by_compatible(blob, noff,
951 node_info[i].compat)) >= 0) {
952 const char *prop;
953
954 prop = fdt_getprop(blob, noff, "status", NULL);
955 if (prop && !strcmp(prop, "disabled"))
956 continue;
957
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100958 debug("%s: %s, mtd dev type %d\n",
959 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +0900960 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +0900961
962 if (!inited) {
963 if (mtdparts_init() != 0)
964 return;
965 inited = true;
966 }
967
Masahiro Yamadaf3d40142018-07-19 16:28:22 +0900968 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100969 if (dev) {
970 if (fdt_node_set_part_info(blob, noff, dev))
971 return; /* return on error */
972 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100973 }
974 }
975}
976#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -0500977
978void fdt_del_node_and_alias(void *blob, const char *alias)
979{
980 int off = fdt_path_offset(blob, alias);
981
982 if (off < 0)
983 return;
984
985 fdt_del_node(blob, off);
986
987 off = fdt_path_offset(blob, "/aliases");
988 fdt_delprop(blob, off, alias);
989}
Kumar Galab87e7932010-06-16 14:27:38 -0500990
Kumar Galab87e7932010-06-16 14:27:38 -0500991/* Max address size we deal with */
992#define OF_MAX_ADDR_CELLS 4
Bin Mengd9a00932015-12-07 01:39:47 -0800993#define OF_BAD_ADDR FDT_ADDR_T_NONE
Dario Binacchib574d682020-12-30 00:16:21 +0100994#define OF_CHECK_COUNTS(na, ns) (((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) && \
995 ((ns) > 0 || gd_size_cells_0()))
Kumar Galab87e7932010-06-16 14:27:38 -0500996
997/* Debug utility */
998#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +0000999static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001000{
1001 printf("%s", s);
1002 while(na--)
1003 printf(" %08x", *(addr++));
1004 printf("\n");
1005}
1006#else
Kim Phillips6542c072013-01-16 14:00:11 +00001007static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001008#endif
1009
Paul Burtondbef2f02016-05-17 07:43:24 +01001010/**
1011 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001012 * @name: A string used to identify this bus in debug output.
1013 * @addresses: The name of the DT property from which addresses are
1014 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001015 * @match: Return non-zero if the node whose parent is at
1016 * parentoffset in the FDT blob corresponds to a bus
1017 * of this type, otherwise return zero. If NULL a match
1018 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001019 * @count_cells:Count how many cells (be32 values) a node whose parent
1020 * is at parentoffset in the FDT blob will require to
1021 * represent its address (written to *addrc) & size
1022 * (written to *sizec).
1023 * @map: Map the address addr from the address space of this
1024 * bus to that of its parent, making use of the ranges
1025 * read from DT to an array at range. na and ns are the
1026 * number of cells (be32 values) used to hold and address
1027 * or size, respectively, for this bus. pna is the number
1028 * of cells used to hold an address for the parent bus.
1029 * Returns the address in the address space of the parent
1030 * bus.
1031 * @translate: Update the value of the address cells at addr within an
1032 * FDT by adding offset to it. na specifies the number of
1033 * cells used to hold the address being translated. Returns
1034 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001035 *
1036 * Each bus type will include a struct of_bus in the of_busses array,
1037 * providing implementations of some or all of the functions used to
1038 * match the bus & handle address translation for its children.
1039 */
Kumar Galab87e7932010-06-16 14:27:38 -05001040struct of_bus {
1041 const char *name;
1042 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001043 int (*match)(const void *blob, int parentoffset);
1044 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001045 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001046 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001047 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001048 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001049};
1050
1051/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001052void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001053 int *addrc, int *sizec)
1054{
Kim Phillips6542c072013-01-16 14:00:11 +00001055 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001056
Simon Glass9fbc6322014-10-23 18:58:57 -06001057 if (addrc)
1058 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001059
1060 if (sizec) {
1061 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1062 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001063 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001064 else
1065 *sizec = 1;
1066 }
Kumar Galab87e7932010-06-16 14:27:38 -05001067}
1068
Kim Phillips6542c072013-01-16 14:00:11 +00001069static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001070 int na, int ns, int pna)
1071{
1072 u64 cp, s, da;
1073
Simon Glassbb7c01e2017-05-18 20:09:26 -06001074 cp = fdt_read_number(range, na);
1075 s = fdt_read_number(range + na + pna, ns);
1076 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001077
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301078 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001079
1080 if (da < cp || da >= (cp + s))
1081 return OF_BAD_ADDR;
1082 return da - cp;
1083}
1084
Kim Phillips6542c072013-01-16 14:00:11 +00001085static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001086{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001087 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001088 memset(addr, 0, na * 4);
1089 a += offset;
1090 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001091 addr[na - 2] = cpu_to_fdt32(a >> 32);
1092 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001093
1094 return 0;
1095}
1096
Paul Burtondbef2f02016-05-17 07:43:24 +01001097#ifdef CONFIG_OF_ISA_BUS
1098
1099/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001100static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001101{
1102 const char *name;
1103
1104 name = fdt_get_name(blob, parentoffset, NULL);
1105 if (!name)
1106 return 0;
1107
1108 return !strcmp(name, "isa");
1109}
1110
Stephen Warrenc99581b2016-08-05 09:47:50 -06001111static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001112 int *addrc, int *sizec)
1113{
1114 if (addrc)
1115 *addrc = 2;
1116 if (sizec)
1117 *sizec = 1;
1118}
1119
1120static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1121 int na, int ns, int pna)
1122{
1123 u64 cp, s, da;
1124
1125 /* Check address type match */
1126 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1127 return OF_BAD_ADDR;
1128
Simon Glassbb7c01e2017-05-18 20:09:26 -06001129 cp = fdt_read_number(range + 1, na - 1);
1130 s = fdt_read_number(range + na + pna, ns);
1131 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001132
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301133 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001134
1135 if (da < cp || da >= (cp + s))
1136 return OF_BAD_ADDR;
1137 return da - cp;
1138}
1139
1140static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1141{
1142 return of_bus_default_translate(addr + 1, offset, na - 1);
1143}
1144
1145#endif /* CONFIG_OF_ISA_BUS */
1146
Kumar Galab87e7932010-06-16 14:27:38 -05001147/* Array of bus specific translators */
1148static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001149#ifdef CONFIG_OF_ISA_BUS
1150 /* ISA */
1151 {
1152 .name = "isa",
1153 .addresses = "reg",
1154 .match = of_bus_isa_match,
1155 .count_cells = of_bus_isa_count_cells,
1156 .map = of_bus_isa_map,
1157 .translate = of_bus_isa_translate,
1158 },
1159#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001160 /* Default */
1161 {
1162 .name = "default",
1163 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001164 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001165 .map = of_bus_default_map,
1166 .translate = of_bus_default_translate,
1167 },
1168};
1169
Stephen Warrenc99581b2016-08-05 09:47:50 -06001170static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001171{
1172 struct of_bus *bus;
1173
1174 if (ARRAY_SIZE(of_busses) == 1)
1175 return of_busses;
1176
1177 for (bus = of_busses; bus; bus++) {
1178 if (!bus->match || bus->match(blob, parentoffset))
1179 return bus;
1180 }
1181
1182 /*
1183 * We should always have matched the default bus at least, since
1184 * it has a NULL match field. If we didn't then it somehow isn't
1185 * in the of_busses array or something equally catastrophic has
1186 * gone wrong.
1187 */
1188 assert(0);
1189 return NULL;
1190}
1191
Stephen Warrenc99581b2016-08-05 09:47:50 -06001192static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001193 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001194 int na, int ns, int pna, const char *rprop)
1195{
Kim Phillips6542c072013-01-16 14:00:11 +00001196 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001197 int rlen;
1198 int rone;
1199 u64 offset = OF_BAD_ADDR;
1200
1201 /* Normally, an absence of a "ranges" property means we are
1202 * crossing a non-translatable boundary, and thus the addresses
1203 * below the current not cannot be converted to CPU physical ones.
1204 * Unfortunately, while this is very clear in the spec, it's not
1205 * what Apple understood, and they do have things like /uni-n or
1206 * /ht nodes with no "ranges" property and a lot of perfectly
1207 * useable mapped devices below them. Thus we treat the absence of
1208 * "ranges" as equivalent to an empty "ranges" property which means
1209 * a 1:1 translation at that level. It's up to the caller not to try
1210 * to translate addresses that aren't supposed to be translated in
1211 * the first place. --BenH.
1212 */
Kim Phillips6542c072013-01-16 14:00:11 +00001213 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001214 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001215 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001216 memset(addr, 0, pna * 4);
1217 debug("OF: no ranges, 1:1 translation\n");
1218 goto finish;
1219 }
1220
1221 debug("OF: walking ranges...\n");
1222
1223 /* Now walk through the ranges */
1224 rlen /= 4;
1225 rone = na + pna + ns;
1226 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1227 offset = bus->map(addr, ranges, na, ns, pna);
1228 if (offset != OF_BAD_ADDR)
1229 break;
1230 }
1231 if (offset == OF_BAD_ADDR) {
1232 debug("OF: not found !\n");
1233 return 1;
1234 }
1235 memcpy(addr, ranges + na, 4 * pna);
1236
1237 finish:
1238 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001239 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001240
1241 /* Translate it into parent bus space */
1242 return pbus->translate(addr, offset, pna);
1243}
1244
1245/*
1246 * Translate an address from the device-tree into a CPU physical address,
1247 * this walks up the tree and applies the various bus mappings on the
1248 * way.
1249 *
1250 * Note: We consider that crossing any level with #size-cells == 0 to mean
1251 * that translation is impossible (that is we are not dealing with a value
1252 * that can be mapped to a cpu physical address). This is not really specified
1253 * that way, but this is traditionally the way IBM at least do things
1254 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001255static u64 __of_translate_address(const void *blob, int node_offset,
1256 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001257{
1258 int parent;
1259 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001260 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001261 int na, ns, pna, pns;
1262 u64 result = OF_BAD_ADDR;
1263
1264 debug("OF: ** translation for device %s **\n",
1265 fdt_get_name(blob, node_offset, NULL));
1266
1267 /* Get parent & match bus type */
1268 parent = fdt_parent_offset(blob, node_offset);
1269 if (parent < 0)
1270 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001271 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001272
1273 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001274 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001275 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001276 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1277 fdt_get_name(blob, node_offset, NULL));
1278 goto bail;
1279 }
1280 memcpy(addr, in_addr, na * 4);
1281
1282 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1283 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1284 of_dump_addr("OF: translating address:", addr, na);
1285
1286 /* Translate */
1287 for (;;) {
1288 /* Switch to parent bus */
1289 node_offset = parent;
1290 parent = fdt_parent_offset(blob, node_offset);
1291
1292 /* If root, we have finished */
1293 if (parent < 0) {
1294 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001295 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001296 break;
1297 }
1298
1299 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001300 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001301 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001302 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001303 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1304 fdt_get_name(blob, node_offset, NULL));
1305 break;
1306 }
1307
1308 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1309 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1310
1311 /* Apply bus translation */
1312 if (of_translate_one(blob, node_offset, bus, pbus,
1313 addr, na, ns, pna, rprop))
1314 break;
1315
1316 /* Complete the move up one level */
1317 na = pna;
1318 ns = pns;
1319 bus = pbus;
1320
1321 of_dump_addr("OF: one level translation:", addr, na);
1322 }
1323 bail:
1324
1325 return result;
1326}
1327
Stephen Warrenc99581b2016-08-05 09:47:50 -06001328u64 fdt_translate_address(const void *blob, int node_offset,
1329 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001330{
1331 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1332}
Kumar Gala800d1d12010-07-04 12:48:21 -05001333
Fabien Dessenne22236e02019-05-31 15:11:30 +02001334u64 fdt_translate_dma_address(const void *blob, int node_offset,
1335 const fdt32_t *in_addr)
1336{
1337 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1338}
1339
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001340int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1341 dma_addr_t *bus, u64 *size)
1342{
1343 bool found_dma_ranges = false;
1344 struct of_bus *bus_node;
1345 const fdt32_t *ranges;
1346 int na, ns, pna, pns;
1347 int parent = node;
1348 int ret = 0;
1349 int len;
1350
1351 /* Find the closest dma-ranges property */
1352 while (parent >= 0) {
1353 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1354
1355 /* Ignore empty ranges, they imply no translation required */
1356 if (ranges && len > 0)
1357 break;
1358
1359 /* Once we find 'dma-ranges', then a missing one is an error */
1360 if (found_dma_ranges && !ranges) {
1361 ret = -EINVAL;
1362 goto out;
1363 }
1364
1365 if (ranges)
1366 found_dma_ranges = true;
1367
1368 parent = fdt_parent_offset(blob, parent);
1369 }
1370
1371 if (!ranges || parent < 0) {
1372 debug("no dma-ranges found for node %s\n",
1373 fdt_get_name(blob, node, NULL));
1374 ret = -ENOENT;
1375 goto out;
1376 }
1377
1378 /* switch to that node */
1379 node = parent;
1380 parent = fdt_parent_offset(blob, node);
1381 if (parent < 0) {
1382 printf("Found dma-ranges in root node, shoudln't happen\n");
1383 ret = -EINVAL;
1384 goto out;
1385 }
1386
1387 /* Get the address sizes both for the bus and its parent */
1388 bus_node = of_match_bus(blob, node);
1389 bus_node->count_cells(blob, node, &na, &ns);
1390 if (!OF_CHECK_COUNTS(na, ns)) {
1391 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1392 fdt_get_name(blob, node, NULL));
1393 return -EINVAL;
1394 goto out;
1395 }
1396
1397 bus_node = of_match_bus(blob, parent);
1398 bus_node->count_cells(blob, parent, &pna, &pns);
1399 if (!OF_CHECK_COUNTS(pna, pns)) {
1400 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1401 fdt_get_name(blob, parent, NULL));
1402 return -EINVAL;
1403 goto out;
1404 }
1405
1406 *bus = fdt_read_number(ranges, na);
1407 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1408 *size = fdt_read_number(ranges + na + pna, ns);
1409out:
1410 return ret;
1411}
1412
Kumar Gala800d1d12010-07-04 12:48:21 -05001413/**
1414 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1415 * who's reg property matches a physical cpu address
1416 *
1417 * @blob: ptr to device tree
1418 * @compat: compatiable string to match
1419 * @compat_off: property name
1420 *
1421 */
1422int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1423 phys_addr_t compat_off)
1424{
1425 int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1426 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +00001427 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Kumar Gala800d1d12010-07-04 12:48:21 -05001428 if (reg) {
1429 if (compat_off == fdt_translate_address(blob, off, reg))
1430 return off;
1431 }
1432 off = fdt_node_offset_by_compatible(blob, off, compat);
1433 }
1434
1435 return -FDT_ERR_NOTFOUND;
1436}
1437
Kumar Galab7afb112010-07-09 16:18:58 -05001438/**
1439 * fdt_alloc_phandle: Return next free phandle value
1440 *
1441 * @blob: ptr to device tree
1442 */
1443int fdt_alloc_phandle(void *blob)
1444{
Masahiro Yamadadb204642014-11-07 03:03:31 +09001445 int offset;
1446 uint32_t phandle = 0;
Kumar Galab7afb112010-07-09 16:18:58 -05001447
1448 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1449 offset = fdt_next_node(blob, offset, NULL)) {
Timur Tabic6d83032011-09-20 18:24:35 -05001450 phandle = max(phandle, fdt_get_phandle(blob, offset));
Kumar Galab7afb112010-07-09 16:18:58 -05001451 }
Kumar Gala800d1d12010-07-04 12:48:21 -05001452
Kumar Galab7afb112010-07-09 16:18:58 -05001453 return phandle + 1;
1454}
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001455
Gerald Van Barenaf737882011-07-14 21:40:10 -04001456/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001457 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001458 *
1459 * @fdt: ptr to device tree
1460 * @nodeoffset: node to update
1461 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001462 */
1463int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001464{
1465 int ret;
1466
1467#ifdef DEBUG
1468 int off = fdt_node_offset_by_phandle(fdt, phandle);
1469
1470 if ((off >= 0) && (off != nodeoffset)) {
1471 char buf[64];
1472
1473 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1474 printf("Trying to update node %s with phandle %u ",
1475 buf, phandle);
1476
1477 fdt_get_path(fdt, off, buf, sizeof(buf));
1478 printf("that already exists in node %s.\n", buf);
1479 return -FDT_ERR_BADPHANDLE;
1480 }
1481#endif
1482
1483 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1484 if (ret < 0)
1485 return ret;
1486
1487 /*
1488 * For now, also set the deprecated "linux,phandle" property, so that we
1489 * don't break older kernels.
1490 */
1491 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1492
1493 return ret;
1494}
1495
Kumar Galad5fc2582011-08-01 00:25:20 -05001496/*
1497 * fdt_create_phandle: Create a phandle property for the given node
1498 *
1499 * @fdt: ptr to device tree
1500 * @nodeoffset: node to update
1501 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001502unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001503{
1504 /* see if there is a phandle already */
1505 int phandle = fdt_get_phandle(fdt, nodeoffset);
1506
1507 /* if we got 0, means no phandle so create one */
1508 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001509 int ret;
1510
Kumar Galad5fc2582011-08-01 00:25:20 -05001511 phandle = fdt_alloc_phandle(fdt);
Timur Tabi0f46c802011-09-20 18:24:34 -05001512 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1513 if (ret < 0) {
1514 printf("Can't set phandle %u: %s\n", phandle,
1515 fdt_strerror(ret));
1516 return 0;
1517 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001518 }
1519
1520 return phandle;
1521}
1522
Shengzhou Liua7be8022011-10-14 16:26:05 +08001523/*
1524 * fdt_set_node_status: Set status for the given node
1525 *
1526 * @fdt: ptr to device tree
1527 * @nodeoffset: node to update
1528 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1529 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1530 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1531 */
1532int fdt_set_node_status(void *fdt, int nodeoffset,
1533 enum fdt_status status, unsigned int error_code)
1534{
1535 char buf[16];
1536 int ret = 0;
1537
1538 if (nodeoffset < 0)
1539 return nodeoffset;
1540
1541 switch (status) {
1542 case FDT_STATUS_OKAY:
1543 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1544 break;
1545 case FDT_STATUS_DISABLED:
1546 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1547 break;
1548 case FDT_STATUS_FAIL:
1549 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1550 break;
1551 case FDT_STATUS_FAIL_ERROR_CODE:
1552 sprintf(buf, "fail-%d", error_code);
1553 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1554 break;
1555 default:
1556 printf("Invalid fdt status: %x\n", status);
1557 ret = -1;
1558 break;
1559 }
1560
1561 return ret;
1562}
1563
1564/*
1565 * fdt_set_status_by_alias: Set status for the given node given an alias
1566 *
1567 * @fdt: ptr to device tree
1568 * @alias: alias of node to update
1569 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1570 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1571 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1572 */
1573int fdt_set_status_by_alias(void *fdt, const char* alias,
1574 enum fdt_status status, unsigned int error_code)
1575{
1576 int offset = fdt_path_offset(fdt, alias);
1577
1578 return fdt_set_node_status(fdt, offset, status, error_code);
1579}
1580
Tom Wai-Hong Tamdb4a88d2012-12-05 14:46:41 +00001581#if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
Anatolij Gustschin3b857f12010-08-18 11:25:20 +02001582int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1583{
1584 int noff;
1585 int ret;
1586
1587 noff = fdt_node_offset_by_compatible(blob, -1, compat);
1588 if (noff != -FDT_ERR_NOTFOUND) {
1589 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1590add_edid:
1591 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1592 if (ret == -FDT_ERR_NOSPACE) {
1593 ret = fdt_increase_size(blob, 512);
1594 if (!ret)
1595 goto add_edid;
1596 else
1597 goto err_size;
1598 } else if (ret < 0) {
1599 printf("Can't add property: %s\n", fdt_strerror(ret));
1600 return ret;
1601 }
1602 }
1603 return 0;
1604err_size:
1605 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1606 return ret;
1607}
1608#endif
Timur Tabi8ebaf202011-05-03 13:24:07 -05001609
1610/*
1611 * Verify the physical address of device tree node for a given alias
1612 *
1613 * This function locates the device tree node of a given alias, and then
1614 * verifies that the physical address of that device matches the given
1615 * parameter. It displays a message if there is a mismatch.
1616 *
1617 * Returns 1 on success, 0 on failure
1618 */
1619int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1620{
1621 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001622 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001623 int node, len;
1624 u64 dt_addr;
1625
1626 path = fdt_getprop(fdt, anode, alias, NULL);
1627 if (!path) {
1628 /* If there's no such alias, then it's not a failure */
1629 return 1;
1630 }
1631
1632 node = fdt_path_offset(fdt, path);
1633 if (node < 0) {
1634 printf("Warning: device tree alias '%s' points to invalid "
1635 "node %s.\n", alias, path);
1636 return 0;
1637 }
1638
1639 reg = fdt_getprop(fdt, node, "reg", &len);
1640 if (!reg) {
1641 printf("Warning: device tree node '%s' has no address.\n",
1642 path);
1643 return 0;
1644 }
1645
1646 dt_addr = fdt_translate_address(fdt, node, reg);
1647 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001648 printf("Warning: U-Boot configured device %s at address %llu,\n"
1649 "but the device tree has it address %llx.\n",
1650 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
1657/*
1658 * Returns the base address of an SOC or PCI node
1659 */
Simon Glass293340a2017-05-18 20:09:00 -06001660u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001661{
1662 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001663 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001664
Simon Glass29f304b2017-07-04 13:31:20 -06001665 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001666
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001667 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001668}
Alexander Graf19555be2014-04-11 17:09:41 +02001669
1670/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001671 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1672 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001673 */
1674static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1675 uint64_t *val, int cells)
1676{
Bin Mengd67d7d02021-02-25 17:22:24 +08001677 const fdt32_t *prop32;
1678 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001679
1680 if ((cell_off + cells) > prop_len)
1681 return -FDT_ERR_NOSPACE;
1682
Bin Mengd67d7d02021-02-25 17:22:24 +08001683 prop32 = &prop[cell_off];
1684
1685 /*
1686 * Special handling for PCI address in PCI bus <ranges>
1687 *
1688 * PCI child address is made up of 3 cells. Advance the cell offset
1689 * by 1 so that the PCI child address can be correctly read.
1690 */
1691 if (cells == 3)
1692 cell_off += 1;
1693 prop64 = (const fdt64_t *)&prop[cell_off];
1694
Alexander Graf19555be2014-04-11 17:09:41 +02001695 switch (cells) {
1696 case 1:
1697 *val = fdt32_to_cpu(*prop32);
1698 break;
1699 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001700 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001701 *val = fdt64_to_cpu(*prop64);
1702 break;
1703 default:
1704 return -FDT_ERR_NOSPACE;
1705 }
1706
1707 return 0;
1708}
1709
1710/**
1711 * fdt_read_range - Read a node's n'th range property
1712 *
1713 * @fdt: ptr to device tree
1714 * @node: offset of node
1715 * @n: range index
1716 * @child_addr: pointer to storage for the "child address" field
1717 * @addr: pointer to storage for the CPU view translated physical start
1718 * @len: pointer to storage for the range length
1719 *
1720 * Convenience function that reads and interprets a specific range out of
1721 * a number of the "ranges" property array.
1722 */
1723int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1724 uint64_t *addr, uint64_t *len)
1725{
1726 int pnode = fdt_parent_offset(fdt, node);
1727 const fdt32_t *ranges;
1728 int pacells;
1729 int acells;
1730 int scells;
1731 int ranges_len;
1732 int cell = 0;
1733 int r = 0;
1734
1735 /*
1736 * The "ranges" property is an array of
1737 * { <child address> <parent address> <size in child address space> }
1738 *
1739 * All 3 elements can span a diffent number of cells. Fetch their size.
1740 */
1741 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1742 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1743 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1744
1745 /* Now try to get the ranges property */
1746 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1747 if (!ranges)
1748 return -FDT_ERR_NOTFOUND;
1749 ranges_len /= sizeof(uint32_t);
1750
1751 /* Jump to the n'th entry */
1752 cell = n * (pacells + acells + scells);
1753
1754 /* Read <child address> */
1755 if (child_addr) {
1756 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1757 acells);
1758 if (r)
1759 return r;
1760 }
1761 cell += acells;
1762
1763 /* Read <parent address> */
1764 if (addr)
1765 *addr = fdt_translate_address(fdt, node, ranges + cell);
1766 cell += pacells;
1767
1768 /* Read <size in child address space> */
1769 if (len) {
1770 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1771 if (r)
1772 return r;
1773 }
1774
1775 return 0;
1776}
Hans de Goedeea876372014-11-17 15:29:11 +01001777
1778/**
1779 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1780 *
1781 * @fdt: ptr to device tree
1782 * @node: offset of the simplefb node
1783 * @base_address: framebuffer base address
1784 * @width: width in pixels
1785 * @height: height in pixels
1786 * @stride: bytes per line
1787 * @format: pixel format string
1788 *
1789 * Convenience function to fill and enable a simplefb node.
1790 */
1791int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
1792 u32 height, u32 stride, const char *format)
1793{
1794 char name[32];
1795 fdt32_t cells[4];
1796 int i, addrc, sizec, ret;
1797
Simon Glassbb7c01e2017-05-18 20:09:26 -06001798 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
1799 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01001800 i = 0;
1801 if (addrc == 2)
1802 cells[i++] = cpu_to_fdt32(base_address >> 32);
1803 cells[i++] = cpu_to_fdt32(base_address);
1804 if (sizec == 2)
1805 cells[i++] = 0;
1806 cells[i++] = cpu_to_fdt32(height * stride);
1807
1808 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
1809 if (ret < 0)
1810 return ret;
1811
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001812 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01001813 ret = fdt_set_name(fdt, node, name);
1814 if (ret < 0)
1815 return ret;
1816
1817 ret = fdt_setprop_u32(fdt, node, "width", width);
1818 if (ret < 0)
1819 return ret;
1820
1821 ret = fdt_setprop_u32(fdt, node, "height", height);
1822 if (ret < 0)
1823 return ret;
1824
1825 ret = fdt_setprop_u32(fdt, node, "stride", stride);
1826 if (ret < 0)
1827 return ret;
1828
1829 ret = fdt_setprop_string(fdt, node, "format", format);
1830 if (ret < 0)
1831 return ret;
1832
1833 ret = fdt_setprop_string(fdt, node, "status", "okay");
1834 if (ret < 0)
1835 return ret;
1836
1837 return 0;
1838}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07001839
1840/*
1841 * Update native-mode in display-timings from display environment variable.
1842 * The node to update are specified by path.
1843 */
1844int fdt_fixup_display(void *blob, const char *path, const char *display)
1845{
1846 int off, toff;
1847
1848 if (!display || !path)
1849 return -FDT_ERR_NOTFOUND;
1850
1851 toff = fdt_path_offset(blob, path);
1852 if (toff >= 0)
1853 toff = fdt_subnode_offset(blob, toff, "display-timings");
1854 if (toff < 0)
1855 return toff;
1856
1857 for (off = fdt_first_subnode(blob, toff);
1858 off >= 0;
1859 off = fdt_next_subnode(blob, off)) {
1860 uint32_t h = fdt_get_phandle(blob, off);
1861 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
1862 fdt32_to_cpu(h));
1863 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
1864 return fdt_setprop_u32(blob, toff, "native-mode", h);
1865 }
1866 return toff;
1867}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03001868
1869#ifdef CONFIG_OF_LIBFDT_OVERLAY
1870/**
1871 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
1872 *
1873 * @fdt: ptr to device tree
1874 * @fdto: ptr to device tree overlay
1875 *
1876 * Convenience function to apply an overlay and display helpful messages
1877 * in the case of an error
1878 */
1879int fdt_overlay_apply_verbose(void *fdt, void *fdto)
1880{
1881 int err;
1882 bool has_symbols;
1883
1884 err = fdt_path_offset(fdt, "/__symbols__");
1885 has_symbols = err >= 0;
1886
1887 err = fdt_overlay_apply(fdt, fdto);
1888 if (err < 0) {
1889 printf("failed on fdt_overlay_apply(): %s\n",
1890 fdt_strerror(err));
1891 if (!has_symbols) {
1892 printf("base fdt does did not have a /__symbols__ node\n");
1893 printf("make sure you've compiled with -@\n");
1894 }
1895 }
1896 return err;
1897}
1898#endif