]> git.ipfire.org Git - thirdparty/u-boot.git/blame - boot/fdt_support.c
Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[thirdparty/u-boot.git] / boot / fdt_support.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
64dbbd40
GVB
2/*
3 * (C) Copyright 2007
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 *
2a523f52 6 * Copyright 2010-2011 Freescale Semiconductor, Inc.
64dbbd40
GVB
7 */
8
d678a59d 9#include <common.h>
6dca1d9a 10#include <abuf.h>
7b51b576 11#include <env.h>
f7ae49fc 12#include <log.h>
9ad0a799 13#include <mapmem.h>
90526e9f 14#include <net.h>
3e303f74 15#include <stdio_dev.h>
0e7cc083 16#include <dm/ofnode.h>
64dbbd40
GVB
17#include <linux/ctype.h>
18#include <linux/types.h>
64dbbd40 19#include <asm/global_data.h>
16b80c93 20#include <asm/unaligned.h>
b08c8c48 21#include <linux/libfdt.h>
64dbbd40 22#include <fdt_support.h>
151c8b09 23#include <exports.h>
a0ae380b 24#include <fdtdec.h>
622ecee9 25#include <version.h>
efe1ceec
DT
26#include <video.h>
27
28DECLARE_GLOBAL_DATA_PTR;
64dbbd40 29
94fb182c
AG
30/**
31 * fdt_getprop_u32_default_node - Return a node's property or a default
32 *
33 * @fdt: ptr to device tree
34 * @off: offset of node
35 * @cell: cell offset in property
36 * @prop: property name
37 * @dflt: default value if the property isn't found
38 *
39 * Convenience function to return a node's property or a default value if
40 * the property doesn't exist.
41 */
42u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
43 const char *prop, const u32 dflt)
44{
45 const fdt32_t *val;
46 int len;
47
48 val = fdt_getprop(fdt, off, prop, &len);
49
50 /* Check if property exists */
51 if (!val)
52 return dflt;
53
54 /* Check if property is long enough */
55 if (len < ((cell + 1) * sizeof(uint32_t)))
56 return dflt;
57
58 return fdt32_to_cpu(*val);
59}
60
3bed2aaf
KG
61/**
62 * fdt_getprop_u32_default - Find a node and return it's property or a default
63 *
64 * @fdt: ptr to device tree
65 * @path: path of node
66 * @prop: property name
67 * @dflt: default value if the property isn't found
68 *
69 * Convenience function to find a node and return it's property or a
70 * default value if it doesn't exist.
71 */
07e12784
GB
72u32 fdt_getprop_u32_default(const void *fdt, const char *path,
73 const char *prop, const u32 dflt)
3bed2aaf 74{
3bed2aaf
KG
75 int off;
76
77 off = fdt_path_offset(fdt, path);
78 if (off < 0)
79 return dflt;
80
94fb182c 81 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
3bed2aaf 82}
64dbbd40 83
a3c2933e
KG
84/**
85 * fdt_find_and_setprop: Find a node and set it's property
86 *
87 * @fdt: ptr to device tree
88 * @node: path of node
89 * @prop: property name
90 * @val: ptr to new value
91 * @len: length of new property value
92 * @create: flag to create the property if it doesn't exist
93 *
94 * Convenience function to directly set a property given the path to the node.
95 */
96int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
97 const void *val, int len, int create)
98{
8d04f02f 99 int nodeoff = fdt_path_offset(fdt, node);
a3c2933e
KG
100
101 if (nodeoff < 0)
102 return nodeoff;
103
8aa5ec6e 104 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
a3c2933e
KG
105 return 0; /* create flag not set; so exit quietly */
106
107 return fdt_setprop(fdt, nodeoff, prop, val, len);
108}
109
8edb2192 110/**
a9e8e291
SG
111 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
112 *
8edb2192
MY
113 * @fdt: pointer to the device tree blob
114 * @parentoffset: structure block offset of a node
115 * @name: name of the subnode to locate
116 *
117 * fdt_subnode_offset() finds a subnode of the node with a given name.
118 * If the subnode does not exist, it will be created.
119 */
a9e8e291 120int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
8edb2192
MY
121{
122 int offset;
123
124 offset = fdt_subnode_offset(fdt, parentoffset, name);
125
126 if (offset == -FDT_ERR_NOTFOUND)
127 offset = fdt_add_subnode(fdt, parentoffset, name);
128
129 if (offset < 0)
130 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
131
132 return offset;
133}
134
e036a1d2 135#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
40777812 136static int fdt_fixup_stdout(void *fdt, int chosenoff)
151c8b09 137{
972f2a89
MY
138 int err;
139 int aliasoff;
151c8b09 140 char sername[9] = { 0 };
972f2a89
MY
141 const void *path;
142 int len;
143 char tmp[256]; /* long enough */
151c8b09 144
9f29aeb8 145 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
151c8b09 146
972f2a89
MY
147 aliasoff = fdt_path_offset(fdt, "/aliases");
148 if (aliasoff < 0) {
149 err = aliasoff;
da77c819 150 goto noalias;
151c8b09 151 }
972f2a89
MY
152
153 path = fdt_getprop(fdt, aliasoff, sername, &len);
154 if (!path) {
155 err = len;
da77c819 156 goto noalias;
972f2a89
MY
157 }
158
159 /* fdt_setprop may break "path" so we copy it to tmp buffer */
160 memcpy(tmp, path, len);
161
162 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
151c8b09
KG
163 if (err < 0)
164 printf("WARNING: could not set linux,stdout-path %s.\n",
972f2a89 165 fdt_strerror(err));
151c8b09
KG
166
167 return err;
da77c819
SW
168
169noalias:
170 printf("WARNING: %s: could not read %s alias: %s\n",
171 __func__, sername, fdt_strerror(err));
172
173 return 0;
151c8b09 174}
972f2a89
MY
175#else
176static int fdt_fixup_stdout(void *fdt, int chosenoff)
177{
178 return 0;
179}
151c8b09
KG
180#endif
181
f18295d3
MY
182static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
183 uint64_t val, int is_u64)
184{
185 if (is_u64)
186 return fdt_setprop_u64(fdt, nodeoffset, name, val);
187 else
188 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
189}
190
10be5b5d
PK
191int fdt_root(void *fdt)
192{
193 char *serial;
194 int err;
195
196 err = fdt_check_header(fdt);
197 if (err < 0) {
198 printf("fdt_root: %s\n", fdt_strerror(err));
199 return err;
200 }
201
00caae6d 202 serial = env_get("serial#");
10be5b5d
PK
203 if (serial) {
204 err = fdt_setprop(fdt, 0, "serial-number", serial,
205 strlen(serial) + 1);
206
207 if (err < 0) {
208 printf("WARNING: could not set serial-number %s.\n",
209 fdt_strerror(err));
210 return err;
211 }
212 }
213
214 return 0;
215}
f18295d3 216
dbe963ae 217int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
64dbbd40 218{
f18295d3 219 int nodeoffset;
2a1a2cb6 220 int err, j, total;
f18295d3 221 int is_u64;
2a1a2cb6 222 uint64_t addr, size;
64dbbd40 223
50babaf8
MY
224 /* just return if the size of initrd is zero */
225 if (initrd_start == initrd_end)
226 return 0;
227
8edb2192
MY
228 /* find or create "/chosen" node. */
229 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
230 if (nodeoffset < 0)
2a1a2cb6 231 return nodeoffset;
64dbbd40 232
2a1a2cb6
KG
233 total = fdt_num_mem_rsv(fdt);
234
235 /*
236 * Look for an existing entry and update it. If we don't find
237 * the entry, we will j be the next available slot.
238 */
239 for (j = 0; j < total; j++) {
240 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
241 if (addr == initrd_start) {
242 fdt_del_mem_rsv(fdt, j);
243 break;
c28abb9c 244 }
2a1a2cb6 245 }
8d04f02f 246
ce6b27a8 247 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
2a1a2cb6
KG
248 if (err < 0) {
249 printf("fdt_initrd: %s\n", fdt_strerror(err));
250 return err;
251 }
252
933cdbb4 253 is_u64 = (fdt_address_cells(fdt, 0) == 2);
f18295d3
MY
254
255 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
256 (uint64_t)initrd_start, is_u64);
f77a606a 257
dbe963ae
MY
258 if (err < 0) {
259 printf("WARNING: could not set linux,initrd-start %s.\n",
260 fdt_strerror(err));
261 return err;
262 }
f18295d3
MY
263
264 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
265 (uint64_t)initrd_end, is_u64);
266
dbe963ae
MY
267 if (err < 0) {
268 printf("WARNING: could not set linux,initrd-end %s.\n",
269 fdt_strerror(err));
2a1a2cb6 270
dbe963ae 271 return err;
64dbbd40
GVB
272 }
273
2a1a2cb6
KG
274 return 0;
275}
276
f0b21ebd
NM
277/**
278 * board_fdt_chosen_bootargs - boards may override this function to use
279 * alternative kernel command line arguments
280 */
281__weak char *board_fdt_chosen_bootargs(void)
282{
283 return env_get("bootargs");
284}
285
bc6ed0f9 286int fdt_chosen(void *fdt)
2a1a2cb6 287{
6dca1d9a 288 struct abuf buf = {};
2a1a2cb6
KG
289 int nodeoffset;
290 int err;
291 char *str; /* used to set string properties */
2a1a2cb6
KG
292
293 err = fdt_check_header(fdt);
294 if (err < 0) {
295 printf("fdt_chosen: %s\n", fdt_strerror(err));
296 return err;
297 }
298
8edb2192
MY
299 /* find or create "/chosen" node. */
300 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
301 if (nodeoffset < 0)
302 return nodeoffset;
64dbbd40 303
6dca1d9a
RV
304 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
305 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
306 abuf_data(&buf), abuf_size(&buf));
307 abuf_uninit(&buf);
308 if (err < 0) {
309 printf("WARNING: could not set rng-seed %s.\n",
310 fdt_strerror(err));
311 return err;
312 }
313 }
314
f0b21ebd
NM
315 str = board_fdt_chosen_bootargs();
316
972f2a89
MY
317 if (str) {
318 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
319 strlen(str) + 1);
320 if (err < 0) {
bc6ed0f9
MY
321 printf("WARNING: could not set bootargs %s.\n",
322 fdt_strerror(err));
972f2a89
MY
323 return err;
324 }
64dbbd40 325 }
2a1a2cb6 326
622ecee9
FD
327 /* add u-boot version */
328 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
329 strlen(PLAIN_VERSION) + 1);
330 if (err < 0) {
331 printf("WARNING: could not set u-boot,version %s.\n",
332 fdt_strerror(err));
333 return err;
334 }
335
972f2a89 336 return fdt_fixup_stdout(fdt, nodeoffset);
64dbbd40
GVB
337}
338
e93becf8
KG
339void do_fixup_by_path(void *fdt, const char *path, const char *prop,
340 const void *val, int len, int create)
341{
342#if defined(DEBUG)
343 int i;
d9ad115b 344 debug("Updating property '%s/%s' = ", path, prop);
e93becf8
KG
345 for (i = 0; i < len; i++)
346 debug(" %.2x", *(u8*)(val+i));
347 debug("\n");
348#endif
349 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
350 if (rc)
351 printf("Unable to update property %s:%s, err=%s\n",
352 path, prop, fdt_strerror(rc));
353}
354
355void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
356 u32 val, int create)
357{
8aa5ec6e
KP
358 fdt32_t tmp = cpu_to_fdt32(val);
359 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
e93becf8
KG
360}
361
9eb77cea
KG
362void do_fixup_by_prop(void *fdt,
363 const char *pname, const void *pval, int plen,
364 const char *prop, const void *val, int len,
365 int create)
366{
367 int off;
368#if defined(DEBUG)
369 int i;
d9ad115b 370 debug("Updating property '%s' = ", prop);
9eb77cea
KG
371 for (i = 0; i < len; i++)
372 debug(" %.2x", *(u8*)(val+i));
373 debug("\n");
374#endif
375 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
376 while (off != -FDT_ERR_NOTFOUND) {
8aa5ec6e 377 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
9eb77cea
KG
378 fdt_setprop(fdt, off, prop, val, len);
379 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
380 }
381}
382
383void do_fixup_by_prop_u32(void *fdt,
384 const char *pname, const void *pval, int plen,
385 const char *prop, u32 val, int create)
386{
8aa5ec6e
KP
387 fdt32_t tmp = cpu_to_fdt32(val);
388 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
9eb77cea
KG
389}
390
391void do_fixup_by_compat(void *fdt, const char *compat,
392 const char *prop, const void *val, int len, int create)
393{
394 int off = -1;
395#if defined(DEBUG)
396 int i;
d9ad115b 397 debug("Updating property '%s' = ", prop);
9eb77cea
KG
398 for (i = 0; i < len; i++)
399 debug(" %.2x", *(u8*)(val+i));
400 debug("\n");
401#endif
3058e283 402 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
8aa5ec6e 403 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
9eb77cea 404 fdt_setprop(fdt, off, prop, val, len);
9eb77cea
KG
405}
406
407void do_fixup_by_compat_u32(void *fdt, const char *compat,
408 const char *prop, u32 val, int create)
409{
8aa5ec6e
KP
410 fdt32_t tmp = cpu_to_fdt32(val);
411 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
9eb77cea
KG
412}
413
63c09417 414#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
739a01ed
MY
415/*
416 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
417 */
41f09bbe
SG
418static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
419 int n)
739a01ed
MY
420{
421 int i;
ffccb84c
HG
422 int address_cells = fdt_address_cells(fdt, 0);
423 int size_cells = fdt_size_cells(fdt, 0);
739a01ed
MY
424 char *p = buf;
425
426 for (i = 0; i < n; i++) {
ffccb84c 427 if (address_cells == 2)
16b80c93 428 put_unaligned_be64(address[i], p);
739a01ed
MY
429 else
430 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
ffccb84c 431 p += 4 * address_cells;
739a01ed 432
ffccb84c 433 if (size_cells == 2)
16b80c93 434 put_unaligned_be64(size[i], p);
739a01ed
MY
435 else
436 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
ffccb84c 437 p += 4 * size_cells;
739a01ed
MY
438 }
439
440 return p - (char *)buf;
441}
442
6b6f216f
RF
443#if CONFIG_NR_DRAM_BANKS > 4
444#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
445#else
8aa5ec6e 446#define MEMORY_BANKS_MAX 4
6b6f216f 447#endif
5e1a3be6
MS
448
449/**
450 * fdt_fixup_memory_banks - Update DT memory node
451 * @blob: Pointer to DT blob
452 * @start: Pointer to memory start addresses array
453 * @size: Pointer to memory sizes array
454 * @banks: Number of memory banks
455 *
456 * Return: 0 on success, negative value on failure
457 *
458 * Based on the passed number of banks and arrays, the function is able to
459 * update existing DT memory nodes to match run time detected/changed memory
460 * configuration. Implementation is handling one specific case with only one
461 * memory node where multiple tuples could be added/updated.
462 * The case where multiple memory nodes with a single tuple (base, size) are
463 * used, this function is only updating the first memory node without removing
464 * others.
465 */
a6bd9e83
JR
466int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
467{
468 int err, nodeoffset;
6d29cc7d 469 int len, i;
8aa5ec6e 470 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
3c927281 471
8aa5ec6e
KP
472 if (banks > MEMORY_BANKS_MAX) {
473 printf("%s: num banks %d exceeds hardcoded limit %d."
474 " Recompile with higher MEMORY_BANKS_MAX?\n",
475 __FUNCTION__, banks, MEMORY_BANKS_MAX);
476 return -1;
477 }
478
3c927281
KG
479 err = fdt_check_header(blob);
480 if (err < 0) {
481 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
482 return err;
483 }
484
8edb2192
MY
485 /* find or create "/memory" node. */
486 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
487 if (nodeoffset < 0)
35940de1 488 return nodeoffset;
8edb2192 489
3c927281
KG
490 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
491 sizeof("memory"));
492 if (err < 0) {
493 printf("WARNING: could not set %s %s.\n", "device_type",
494 fdt_strerror(err));
495 return err;
496 }
497
ed5af03f
TR
498 for (i = 0; i < banks; i++) {
499 if (start[i] == 0 && size[i] == 0)
500 break;
501 }
502
503 banks = i;
504
5c1cf89f
AP
505 if (!banks)
506 return 0;
507
739a01ed 508 len = fdt_pack_reg(blob, tmp, start, size, banks);
3c927281
KG
509
510 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
511 if (err < 0) {
512 printf("WARNING: could not set %s %s.\n",
513 "reg", fdt_strerror(err));
514 return err;
515 }
516 return 0;
517}
949b5a96
IO
518
519int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
520{
521 int err, nodeoffset;
522 int len;
523 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
524
525 if (areas > 8) {
526 printf("%s: num areas %d exceeds hardcoded limit %d\n",
527 __func__, areas, 8);
528 return -1;
529 }
530
531 err = fdt_check_header(blob);
532 if (err < 0) {
533 printf("%s: %s\n", __func__, fdt_strerror(err));
534 return err;
535 }
536
537 /* find or create "/memory" node. */
538 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
539 if (nodeoffset < 0)
540 return nodeoffset;
541
542 len = fdt_pack_reg(blob, tmp, start, size, areas);
543
544 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
545 if (err < 0) {
546 printf("WARNING: could not set %s %s.\n",
547 "reg", fdt_strerror(err));
548 return err;
549 }
550
551 return 0;
552}
63c09417 553#endif
3c927281 554
a6bd9e83
JR
555int fdt_fixup_memory(void *blob, u64 start, u64 size)
556{
557 return fdt_fixup_memory_banks(blob, &start, &size, 1);
558}
559
ba37aa03 560void fdt_fixup_ethernet(void *fdt)
ab544633 561{
24acb83d 562 int i = 0, j, prop;
bc393a79 563 char *tmp, *end;
064d55f8 564 char mac[16];
ab544633 565 const char *path;
a40db6d5 566 unsigned char mac_addr[ARP_HLEN];
bc393a79 567 int offset;
24acb83d
PK
568#ifdef FDT_SEQ_MACADDR_FROM_ENV
569 int nodeoff;
570 const struct fdt_property *fdt_prop;
571#endif
ab544633 572
a434fd1d 573 if (fdt_path_offset(fdt, "/aliases") < 0)
ba37aa03
KG
574 return;
575
a434fd1d
LI
576 /* Cycle through all aliases */
577 for (prop = 0; ; prop++) {
bc393a79 578 const char *name;
bc393a79 579
a434fd1d
LI
580 /* FDT might have been edited, recompute the offset */
581 offset = fdt_first_property_offset(fdt,
582 fdt_path_offset(fdt, "/aliases"));
583 /* Select property number 'prop' */
24acb83d 584 for (j = 0; j < prop; j++)
a434fd1d
LI
585 offset = fdt_next_property_offset(fdt, offset);
586
587 if (offset < 0)
588 break;
589
bc393a79 590 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
f8e57c65
TT
591 if (!strncmp(name, "ethernet", 8)) {
592 /* Treat plain "ethernet" same as "ethernet0". */
24acb83d
PK
593 if (!strcmp(name, "ethernet")
594#ifdef FDT_SEQ_MACADDR_FROM_ENV
595 || !strcmp(name, "ethernet0")
596#endif
597 )
f8e57c65 598 i = 0;
24acb83d 599#ifndef FDT_SEQ_MACADDR_FROM_ENV
f8e57c65
TT
600 else
601 i = trailing_strtol(name);
24acb83d 602#endif
bc393a79
BM
603 if (i != -1) {
604 if (i == 0)
605 strcpy(mac, "ethaddr");
606 else
607 sprintf(mac, "eth%daddr", i);
608 } else {
609 continue;
610 }
24acb83d
PK
611#ifdef FDT_SEQ_MACADDR_FROM_ENV
612 nodeoff = fdt_path_offset(fdt, path);
613 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
614 NULL);
615 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
616 continue;
617 i++;
618#endif
00caae6d 619 tmp = env_get(mac);
bc393a79
BM
620 if (!tmp)
621 continue;
622
623 for (j = 0; j < 6; j++) {
624 mac_addr[j] = tmp ?
7e5f460e 625 hextoul(tmp, &end) : 0;
bc393a79
BM
626 if (tmp)
627 tmp = (*end) ? end + 1 : end;
628 }
629
630 do_fixup_by_path(fdt, path, "mac-address",
631 &mac_addr, 6, 0);
632 do_fixup_by_path(fdt, path, "local-mac-address",
633 &mac_addr, 6, 1);
ab544633 634 }
ab544633
KG
635 }
636}
18e69a35 637
a5af51a7
PT
638int fdt_record_loadable(void *blob, u32 index, const char *name,
639 uintptr_t load_addr, u32 size, uintptr_t entry_point,
be2d1a87 640 const char *type, const char *os, const char *arch)
a5af51a7
PT
641{
642 int err, node;
643
644 err = fdt_check_header(blob);
645 if (err < 0) {
646 printf("%s: %s\n", __func__, fdt_strerror(err));
647 return err;
648 }
649
650 /* find or create "/fit-images" node */
651 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
652 if (node < 0)
653 return node;
654
655 /* find or create "/fit-images/<name>" node */
656 node = fdt_find_or_add_subnode(blob, node, name);
657 if (node < 0)
658 return node;
659
13d1ca87 660 fdt_setprop_u64(blob, node, "load", load_addr);
a5af51a7 661 if (entry_point != -1)
13d1ca87 662 fdt_setprop_u64(blob, node, "entry", entry_point);
a5af51a7
PT
663 fdt_setprop_u32(blob, node, "size", size);
664 if (type)
665 fdt_setprop_string(blob, node, "type", type);
666 if (os)
667 fdt_setprop_string(blob, node, "os", os);
be2d1a87
MS
668 if (arch)
669 fdt_setprop_string(blob, node, "arch", arch);
a5af51a7
PT
670
671 return node;
672}
673
ef476836 674int fdt_shrink_to_minimum(void *blob, uint extrasize)
3082d234
KG
675{
676 int i;
677 uint64_t addr, size;
678 int total, ret;
679 uint actualsize;
59bd7968 680 int fdt_memrsv = 0;
3082d234
KG
681
682 if (!blob)
683 return 0;
684
685 total = fdt_num_mem_rsv(blob);
686 for (i = 0; i < total; i++) {
687 fdt_get_mem_rsv(blob, i, &addr, &size);
92549358 688 if (addr == (uintptr_t)blob) {
3082d234 689 fdt_del_mem_rsv(blob, i);
59bd7968 690 fdt_memrsv = 1;
3082d234
KG
691 break;
692 }
693 }
694
f242a088
PK
695 /*
696 * Calculate the actual size of the fdt
3840ebfa
FW
697 * plus the size needed for 5 fdt_add_mem_rsv, one
698 * for the fdt itself and 4 for a possible initrd
699 * ((initrd-start + initrd-end) * 2 (name & value))
f242a088 700 */
3082d234 701 actualsize = fdt_off_dt_strings(blob) +
3840ebfa 702 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
3082d234 703
ef476836 704 actualsize += extrasize;
3082d234 705 /* Make it so the fdt ends on a page boundary */
92549358
SG
706 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
707 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
3082d234
KG
708
709 /* Change the fdt header to reflect the correct size */
710 fdt_set_totalsize(blob, actualsize);
711
59bd7968
SG
712 if (fdt_memrsv) {
713 /* Add the new reservation */
714 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
715 if (ret < 0)
716 return ret;
717 }
3082d234
KG
718
719 return actualsize;
720}
8ab451c4 721
574506c3
MB
722/**
723 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
724 *
725 * @blob: ptr to device tree
726 */
727int fdt_delete_disabled_nodes(void *blob)
728{
729 while (1) {
730 int ret, offset;
731
732 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
733 "disabled", 9);
734 if (offset < 0)
735 break;
736
737 ret = fdt_del_node(blob, offset);
738 if (ret < 0)
739 return ret;
740 }
741
742 return 0;
743}
744
8ab451c4 745#ifdef CONFIG_PCI
ecc8d425 746#define CFG_SYS_PCI_NR_INBOUND_WIN 4
8ab451c4
KG
747
748#define FDT_PCI_PREFETCH (0x40000000)
749#define FDT_PCI_MEM32 (0x02000000)
750#define FDT_PCI_IO (0x01000000)
751#define FDT_PCI_MEM64 (0x03000000)
752
753int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
754
755 int addrcell, sizecell, len, r;
756 u32 *dma_range;
757 /* sized based on pci addr cells, size-cells, & address-cells */
ecc8d425 758 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
8ab451c4
KG
759
760 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
761 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
762
763 dma_range = &dma_ranges[0];
764 for (r = 0; r < hose->region_count; r++) {
765 u64 bus_start, phys_start, size;
766
ff4e66e9
KG
767 /* skip if !PCI_REGION_SYS_MEMORY */
768 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
8ab451c4
KG
769 continue;
770
771 bus_start = (u64)hose->regions[r].bus_start;
772 phys_start = (u64)hose->regions[r].phys_start;
773 size = (u64)hose->regions[r].size;
774
775 dma_range[0] = 0;
cfd700be 776 if (size >= 0x100000000ull)
867aaf68 777 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
8ab451c4 778 else
867aaf68 779 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
8ab451c4 780 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
867aaf68 781 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
8ab451c4 782#ifdef CONFIG_SYS_PCI_64BIT
867aaf68 783 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
8ab451c4
KG
784#else
785 dma_range[1] = 0;
786#endif
867aaf68 787 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
8ab451c4
KG
788
789 if (addrcell == 2) {
867aaf68
MV
790 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
791 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
8ab451c4 792 } else {
867aaf68 793 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
8ab451c4
KG
794 }
795
796 if (sizecell == 2) {
867aaf68
MV
797 dma_range[3 + addrcell + 0] =
798 cpu_to_fdt32(size >> 32);
799 dma_range[3 + addrcell + 1] =
800 cpu_to_fdt32(size & 0xffffffff);
8ab451c4 801 } else {
867aaf68
MV
802 dma_range[3 + addrcell + 0] =
803 cpu_to_fdt32(size & 0xffffffff);
8ab451c4
KG
804 }
805
806 dma_range += (3 + addrcell + sizecell);
807 }
808
809 len = dma_range - &dma_ranges[0];
810 if (len)
811 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
812
813 return 0;
814}
815#endif
30d45c0d 816
cb2707af
MM
817int fdt_increase_size(void *fdt, int add_len)
818{
819 int newlen;
820
821 newlen = fdt_totalsize(fdt) + add_len;
822
823 /* Open in place with a new len */
824 return fdt_open_into(fdt, fdt, newlen);
825}
826
3c950e2e
AG
827#ifdef CONFIG_FDT_FIXUP_PARTITIONS
828#include <jffs2/load_kernel.h>
829#include <mtd_node.h>
830
cd1457d7 831static int fdt_del_subnodes(const void *blob, int parent_offset)
3c950e2e
AG
832{
833 int off, ndepth;
834 int ret;
835
836 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
837 (off >= 0) && (ndepth > 0);
838 off = fdt_next_node(blob, off, &ndepth)) {
839 if (ndepth == 1) {
840 debug("delete %s: offset: %x\n",
841 fdt_get_name(blob, off, 0), off);
842 ret = fdt_del_node((void *)blob, off);
843 if (ret < 0) {
844 printf("Can't delete node: %s\n",
845 fdt_strerror(ret));
846 return ret;
847 } else {
848 ndepth = 0;
849 off = parent_offset;
850 }
851 }
852 }
853 return 0;
854}
855
cd1457d7 856static int fdt_del_partitions(void *blob, int parent_offset)
3c950e2e
AG
857{
858 const void *prop;
859 int ndepth = 0;
860 int off;
861 int ret;
862
863 off = fdt_next_node(blob, parent_offset, &ndepth);
864 if (off > 0 && ndepth == 1) {
865 prop = fdt_getprop(blob, off, "label", NULL);
866 if (prop == NULL) {
867 /*
868 * Could not find label property, nand {}; node?
869 * Check subnode, delete partitions there if any.
870 */
871 return fdt_del_partitions(blob, off);
872 } else {
873 ret = fdt_del_subnodes(blob, parent_offset);
874 if (ret < 0) {
875 printf("Can't remove subnodes: %s\n",
876 fdt_strerror(ret));
877 return ret;
878 }
879 }
880 }
881 return 0;
882}
883
8ce8e42e
MY
884static int fdt_node_set_part_info(void *blob, int parent_offset,
885 struct mtd_device *dev)
3c950e2e
AG
886{
887 struct list_head *pentry;
888 struct part_info *part;
3c950e2e
AG
889 int off, ndepth = 0;
890 int part_num, ret;
3a9a62a1 891 int sizecell;
3c950e2e
AG
892 char buf[64];
893
894 ret = fdt_del_partitions(blob, parent_offset);
895 if (ret < 0)
896 return ret;
897
3a9a62a1
SM
898 /*
899 * Check if size/address is 1 or 2 cells.
900 * We assume #address-cells and #size-cells have same value.
901 */
902 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
903 0, "#size-cells", 1);
904
3c950e2e
AG
905 /*
906 * Check if it is nand {}; subnode, adjust
907 * the offset in this case
908 */
909 off = fdt_next_node(blob, parent_offset, &ndepth);
910 if (off > 0 && ndepth == 1)
911 parent_offset = off;
912
913 part_num = 0;
914 list_for_each_prev(pentry, &dev->parts) {
915 int newoff;
916
917 part = list_entry(pentry, struct part_info, link);
918
06503f16 919 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
3c950e2e
AG
920 part_num, part->name, part->size,
921 part->offset, part->mask_flags);
922
06503f16 923 sprintf(buf, "partition@%llx", part->offset);
3c950e2e
AG
924add_sub:
925 ret = fdt_add_subnode(blob, parent_offset, buf);
926 if (ret == -FDT_ERR_NOSPACE) {
927 ret = fdt_increase_size(blob, 512);
928 if (!ret)
929 goto add_sub;
930 else
931 goto err_size;
932 } else if (ret < 0) {
933 printf("Can't add partition node: %s\n",
934 fdt_strerror(ret));
935 return ret;
936 }
937 newoff = ret;
938
939 /* Check MTD_WRITEABLE_CMD flag */
940 if (part->mask_flags & 1) {
941add_ro:
942 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
943 if (ret == -FDT_ERR_NOSPACE) {
944 ret = fdt_increase_size(blob, 512);
945 if (!ret)
946 goto add_ro;
947 else
948 goto err_size;
949 } else if (ret < 0)
950 goto err_prop;
951 }
952
3c950e2e 953add_reg:
3a9a62a1
SM
954 if (sizecell == 2) {
955 ret = fdt_setprop_u64(blob, newoff,
956 "reg", part->offset);
957 if (!ret)
958 ret = fdt_appendprop_u64(blob, newoff,
959 "reg", part->size);
960 } else {
961 ret = fdt_setprop_u32(blob, newoff,
962 "reg", part->offset);
963 if (!ret)
964 ret = fdt_appendprop_u32(blob, newoff,
965 "reg", part->size);
966 }
967
3c950e2e
AG
968 if (ret == -FDT_ERR_NOSPACE) {
969 ret = fdt_increase_size(blob, 512);
970 if (!ret)
971 goto add_reg;
972 else
973 goto err_size;
974 } else if (ret < 0)
975 goto err_prop;
976
977add_label:
978 ret = fdt_setprop_string(blob, newoff, "label", part->name);
979 if (ret == -FDT_ERR_NOSPACE) {
980 ret = fdt_increase_size(blob, 512);
981 if (!ret)
982 goto add_label;
983 else
984 goto err_size;
985 } else if (ret < 0)
986 goto err_prop;
987
988 part_num++;
989 }
990 return 0;
991err_size:
992 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
993 return ret;
994err_prop:
995 printf("Can't add property: %s\n", fdt_strerror(ret));
996 return ret;
997}
998
999/*
1000 * Update partitions in nor/nand nodes using info from
1001 * mtdparts environment variable. The nodes to update are
1002 * specified by node_info structure which contains mtd device
1003 * type and compatible string: E. g. the board code in
1004 * ft_board_setup() could use:
1005 *
1006 * struct node_info nodes[] = {
1007 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1008 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1009 * };
1010 *
1011 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1012 */
5f4e32d0
MY
1013void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1014 int node_info_size)
3c950e2e 1015{
3c950e2e 1016 struct mtd_device *dev;
3c950e2e 1017 int i, idx;
36fee2f7 1018 int noff, parts;
53a89664 1019 bool inited = false;
3c950e2e
AG
1020
1021 for (i = 0; i < node_info_size; i++) {
1022 idx = 0;
7d8073e7 1023
3058e283
MB
1024 fdt_for_each_node_by_compatible(noff, blob, -1,
1025 node_info[i].compat) {
7d8073e7
MY
1026 const char *prop;
1027
1028 prop = fdt_getprop(blob, noff, "status", NULL);
1029 if (prop && !strcmp(prop, "disabled"))
1030 continue;
1031
3c950e2e
AG
1032 debug("%s: %s, mtd dev type %d\n",
1033 fdt_get_name(blob, noff, 0),
5f4e32d0 1034 node_info[i].compat, node_info[i].type);
53a89664
MY
1035
1036 if (!inited) {
1037 if (mtdparts_init() != 0)
1038 return;
1039 inited = true;
1040 }
1041
5f4e32d0 1042 dev = device_find(node_info[i].type, idx++);
3c950e2e 1043 if (dev) {
36fee2f7
MS
1044 parts = fdt_subnode_offset(blob, noff,
1045 "partitions");
1046 if (parts < 0)
1047 parts = noff;
1048
1049 if (fdt_node_set_part_info(blob, parts, dev))
3c950e2e
AG
1050 return; /* return on error */
1051 }
3c950e2e
AG
1052 }
1053 }
1054}
1055#endif
49b97d9c 1056
163c5f60
PD
1057int fdt_copy_fixed_partitions(void *blob)
1058{
1059 ofnode node, subnode;
1060 int off, suboff, res;
1061 char path[256];
1062 int address_cells, size_cells;
1063 u8 i, j, child_count;
1064
1065 node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
1066 while (ofnode_valid(node)) {
1067 /* copy the U-Boot fixed partition */
1068 address_cells = ofnode_read_simple_addr_cells(node);
1069 size_cells = ofnode_read_simple_size_cells(node);
1070
1071 res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
1072 if (res)
1073 return res;
1074
1075 off = fdt_path_offset(blob, path);
1076 if (off < 0)
1077 return -ENODEV;
1078
1079 off = fdt_find_or_add_subnode(blob, off, "partitions");
1080 res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
1081 if (res)
1082 return res;
1083
1084 res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
1085 if (res)
1086 return res;
1087
1088 res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
1089 if (res)
1090 return res;
1091
1092 /*
1093 * parse partition in reverse order as fdt_find_or_add_subnode() only
1094 * insert the new node after the parent's properties
1095 */
1096 child_count = ofnode_get_child_count(node);
1097 for (i = child_count; i > 0 ; i--) {
1098 subnode = ofnode_first_subnode(node);
1099 if (!ofnode_valid(subnode))
1100 break;
1101
1102 for (j = 0; (j < i - 1); j++)
1103 subnode = ofnode_next_subnode(subnode);
1104
1105 if (!ofnode_valid(subnode))
1106 break;
1107
1108 const u32 *reg;
1109 int len;
1110
1111 suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
1112 res = fdt_setprop_string(blob, suboff, "label",
1113 ofnode_read_string(subnode, "label"));
1114 if (res)
1115 return res;
1116
1117 reg = ofnode_get_property(subnode, "reg", &len);
1118 res = fdt_setprop(blob, suboff, "reg", reg, len);
1119 if (res)
1120 return res;
1121 }
1122
1123 /* go to next fixed-partitions node */
1124 node = ofnode_by_compatible(node, "fixed-partitions");
1125 }
1126
1127 return 0;
1128}
1129
49b97d9c
KG
1130void fdt_del_node_and_alias(void *blob, const char *alias)
1131{
1132 int off = fdt_path_offset(blob, alias);
1133
1134 if (off < 0)
1135 return;
1136
1137 fdt_del_node(blob, off);
1138
1139 off = fdt_path_offset(blob, "/aliases");
1140 fdt_delprop(blob, off, alias);
1141}
a0342c08 1142
a0342c08
KG
1143/* Max address size we deal with */
1144#define OF_MAX_ADDR_CELLS 4
a47abd7b
DB
1145#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1146 (ns) > 0)
a0342c08
KG
1147
1148/* Debug utility */
1149#ifdef DEBUG
8aa5ec6e 1150static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
a0342c08
KG
1151{
1152 printf("%s", s);
1153 while(na--)
1154 printf(" %08x", *(addr++));
1155 printf("\n");
1156}
1157#else
8aa5ec6e 1158static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
a0342c08
KG
1159#endif
1160
0a222d53
PB
1161/**
1162 * struct of_bus - Callbacks for bus specific translators
49717b18
PB
1163 * @name: A string used to identify this bus in debug output.
1164 * @addresses: The name of the DT property from which addresses are
1165 * to be read, typically "reg".
0a222d53
PB
1166 * @match: Return non-zero if the node whose parent is at
1167 * parentoffset in the FDT blob corresponds to a bus
1168 * of this type, otherwise return zero. If NULL a match
1169 * is assumed.
49717b18
PB
1170 * @count_cells:Count how many cells (be32 values) a node whose parent
1171 * is at parentoffset in the FDT blob will require to
1172 * represent its address (written to *addrc) & size
1173 * (written to *sizec).
1174 * @map: Map the address addr from the address space of this
1175 * bus to that of its parent, making use of the ranges
1176 * read from DT to an array at range. na and ns are the
1177 * number of cells (be32 values) used to hold and address
1178 * or size, respectively, for this bus. pna is the number
1179 * of cells used to hold an address for the parent bus.
1180 * Returns the address in the address space of the parent
1181 * bus.
1182 * @translate: Update the value of the address cells at addr within an
1183 * FDT by adding offset to it. na specifies the number of
1184 * cells used to hold the address being translated. Returns
1185 * zero on success, non-zero on error.
0a222d53
PB
1186 *
1187 * Each bus type will include a struct of_bus in the of_busses array,
1188 * providing implementations of some or all of the functions used to
1189 * match the bus & handle address translation for its children.
1190 */
a0342c08
KG
1191struct of_bus {
1192 const char *name;
1193 const char *addresses;
11e44fc6
SW
1194 int (*match)(const void *blob, int parentoffset);
1195 void (*count_cells)(const void *blob, int parentoffset,
a0342c08 1196 int *addrc, int *sizec);
8aa5ec6e 1197 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
a0342c08 1198 int na, int ns, int pna);
8aa5ec6e 1199 int (*translate)(fdt32_t *addr, u64 offset, int na);
a0342c08
KG
1200};
1201
1202/* Default translator (generic bus) */
eed36609 1203void fdt_support_default_count_cells(const void *blob, int parentoffset,
a0342c08
KG
1204 int *addrc, int *sizec)
1205{
8aa5ec6e 1206 const fdt32_t *prop;
6395f318 1207
933cdbb4
SG
1208 if (addrc)
1209 *addrc = fdt_address_cells(blob, parentoffset);
6395f318
SW
1210
1211 if (sizec) {
1212 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1213 if (prop)
8aa5ec6e 1214 *sizec = be32_to_cpup(prop);
6395f318
SW
1215 else
1216 *sizec = 1;
1217 }
a0342c08
KG
1218}
1219
8aa5ec6e 1220static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
a0342c08
KG
1221 int na, int ns, int pna)
1222{
1223 u64 cp, s, da;
1224
eed36609
SG
1225 cp = fdt_read_number(range, na);
1226 s = fdt_read_number(range + na + pna, ns);
1227 da = fdt_read_number(addr, na);
a0342c08 1228
d8e9cf4d 1229 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
a0342c08
KG
1230
1231 if (da < cp || da >= (cp + s))
1232 return OF_BAD_ADDR;
1233 return da - cp;
1234}
1235
8aa5ec6e 1236static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
a0342c08 1237{
eed36609 1238 u64 a = fdt_read_number(addr, na);
a0342c08
KG
1239 memset(addr, 0, na * 4);
1240 a += offset;
1241 if (na > 1)
8aa5ec6e
KP
1242 addr[na - 2] = cpu_to_fdt32(a >> 32);
1243 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
a0342c08
KG
1244
1245 return 0;
1246}
1247
0a222d53
PB
1248#ifdef CONFIG_OF_ISA_BUS
1249
1250/* ISA bus translator */
11e44fc6 1251static int of_bus_isa_match(const void *blob, int parentoffset)
0a222d53
PB
1252{
1253 const char *name;
1254
1255 name = fdt_get_name(blob, parentoffset, NULL);
1256 if (!name)
1257 return 0;
1258
1259 return !strcmp(name, "isa");
1260}
1261
11e44fc6 1262static void of_bus_isa_count_cells(const void *blob, int parentoffset,
0a222d53
PB
1263 int *addrc, int *sizec)
1264{
1265 if (addrc)
1266 *addrc = 2;
1267 if (sizec)
1268 *sizec = 1;
1269}
1270
1271static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1272 int na, int ns, int pna)
1273{
1274 u64 cp, s, da;
1275
1276 /* Check address type match */
1277 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1278 return OF_BAD_ADDR;
1279
eed36609
SG
1280 cp = fdt_read_number(range + 1, na - 1);
1281 s = fdt_read_number(range + na + pna, ns);
1282 da = fdt_read_number(addr + 1, na - 1);
0a222d53 1283
d8e9cf4d 1284 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
0a222d53
PB
1285
1286 if (da < cp || da >= (cp + s))
1287 return OF_BAD_ADDR;
1288 return da - cp;
1289}
1290
1291static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1292{
1293 return of_bus_default_translate(addr + 1, offset, na - 1);
1294}
1295
1296#endif /* CONFIG_OF_ISA_BUS */
1297
a0342c08
KG
1298/* Array of bus specific translators */
1299static struct of_bus of_busses[] = {
0a222d53
PB
1300#ifdef CONFIG_OF_ISA_BUS
1301 /* ISA */
1302 {
1303 .name = "isa",
1304 .addresses = "reg",
1305 .match = of_bus_isa_match,
1306 .count_cells = of_bus_isa_count_cells,
1307 .map = of_bus_isa_map,
1308 .translate = of_bus_isa_translate,
1309 },
1310#endif /* CONFIG_OF_ISA_BUS */
a0342c08
KG
1311 /* Default */
1312 {
1313 .name = "default",
1314 .addresses = "reg",
eed36609 1315 .count_cells = fdt_support_default_count_cells,
a0342c08
KG
1316 .map = of_bus_default_map,
1317 .translate = of_bus_default_translate,
1318 },
1319};
1320
11e44fc6 1321static struct of_bus *of_match_bus(const void *blob, int parentoffset)
0a222d53
PB
1322{
1323 struct of_bus *bus;
1324
1325 if (ARRAY_SIZE(of_busses) == 1)
1326 return of_busses;
1327
1328 for (bus = of_busses; bus; bus++) {
1329 if (!bus->match || bus->match(blob, parentoffset))
1330 return bus;
1331 }
1332
1333 /*
1334 * We should always have matched the default bus at least, since
1335 * it has a NULL match field. If we didn't then it somehow isn't
1336 * in the of_busses array or something equally catastrophic has
1337 * gone wrong.
1338 */
1339 assert(0);
1340 return NULL;
1341}
1342
11e44fc6 1343static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
8aa5ec6e 1344 struct of_bus *pbus, fdt32_t *addr,
a0342c08
KG
1345 int na, int ns, int pna, const char *rprop)
1346{
8aa5ec6e 1347 const fdt32_t *ranges;
a0342c08
KG
1348 int rlen;
1349 int rone;
1350 u64 offset = OF_BAD_ADDR;
1351
1352 /* Normally, an absence of a "ranges" property means we are
1353 * crossing a non-translatable boundary, and thus the addresses
1354 * below the current not cannot be converted to CPU physical ones.
1355 * Unfortunately, while this is very clear in the spec, it's not
1356 * what Apple understood, and they do have things like /uni-n or
1357 * /ht nodes with no "ranges" property and a lot of perfectly
1358 * useable mapped devices below them. Thus we treat the absence of
1359 * "ranges" as equivalent to an empty "ranges" property which means
1360 * a 1:1 translation at that level. It's up to the caller not to try
1361 * to translate addresses that aren't supposed to be translated in
1362 * the first place. --BenH.
1363 */
8aa5ec6e 1364 ranges = fdt_getprop(blob, parent, rprop, &rlen);
a0342c08 1365 if (ranges == NULL || rlen == 0) {
eed36609 1366 offset = fdt_read_number(addr, na);
a0342c08
KG
1367 memset(addr, 0, pna * 4);
1368 debug("OF: no ranges, 1:1 translation\n");
1369 goto finish;
1370 }
1371
1372 debug("OF: walking ranges...\n");
1373
1374 /* Now walk through the ranges */
1375 rlen /= 4;
1376 rone = na + pna + ns;
1377 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1378 offset = bus->map(addr, ranges, na, ns, pna);
1379 if (offset != OF_BAD_ADDR)
1380 break;
1381 }
1382 if (offset == OF_BAD_ADDR) {
1383 debug("OF: not found !\n");
1384 return 1;
1385 }
1386 memcpy(addr, ranges + na, 4 * pna);
1387
1388 finish:
1389 of_dump_addr("OF: parent translation for:", addr, pna);
dee37fc9 1390 debug("OF: with offset: %llu\n", offset);
a0342c08
KG
1391
1392 /* Translate it into parent bus space */
1393 return pbus->translate(addr, offset, pna);
1394}
1395
1396/*
1397 * Translate an address from the device-tree into a CPU physical address,
1398 * this walks up the tree and applies the various bus mappings on the
1399 * way.
1400 *
1401 * Note: We consider that crossing any level with #size-cells == 0 to mean
1402 * that translation is impossible (that is we are not dealing with a value
1403 * that can be mapped to a cpu physical address). This is not really specified
1404 * that way, but this is traditionally the way IBM at least do things
1405 */
11e44fc6
SW
1406static u64 __of_translate_address(const void *blob, int node_offset,
1407 const fdt32_t *in_addr, const char *rprop)
a0342c08
KG
1408{
1409 int parent;
1410 struct of_bus *bus, *pbus;
8aa5ec6e 1411 fdt32_t addr[OF_MAX_ADDR_CELLS];
a0342c08
KG
1412 int na, ns, pna, pns;
1413 u64 result = OF_BAD_ADDR;
1414
1415 debug("OF: ** translation for device %s **\n",
1416 fdt_get_name(blob, node_offset, NULL));
1417
1418 /* Get parent & match bus type */
1419 parent = fdt_parent_offset(blob, node_offset);
1420 if (parent < 0)
1421 goto bail;
0a222d53 1422 bus = of_match_bus(blob, parent);
a0342c08
KG
1423
1424 /* Cound address cells & copy address locally */
6395f318 1425 bus->count_cells(blob, parent, &na, &ns);
4428f3c8 1426 if (!OF_CHECK_COUNTS(na, ns)) {
a0342c08
KG
1427 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1428 fdt_get_name(blob, node_offset, NULL));
1429 goto bail;
1430 }
1431 memcpy(addr, in_addr, na * 4);
1432
1433 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1434 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1435 of_dump_addr("OF: translating address:", addr, na);
1436
1437 /* Translate */
1438 for (;;) {
1439 /* Switch to parent bus */
1440 node_offset = parent;
1441 parent = fdt_parent_offset(blob, node_offset);
1442
1443 /* If root, we have finished */
1444 if (parent < 0) {
1445 debug("OF: reached root node\n");
eed36609 1446 result = fdt_read_number(addr, na);
a0342c08
KG
1447 break;
1448 }
1449
1450 /* Get new parent bus and counts */
0a222d53 1451 pbus = of_match_bus(blob, parent);
6395f318 1452 pbus->count_cells(blob, parent, &pna, &pns);
4428f3c8 1453 if (!OF_CHECK_COUNTS(pna, pns)) {
a0342c08
KG
1454 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1455 fdt_get_name(blob, node_offset, NULL));
1456 break;
1457 }
1458
1459 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1460 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1461
1462 /* Apply bus translation */
1463 if (of_translate_one(blob, node_offset, bus, pbus,
1464 addr, na, ns, pna, rprop))
1465 break;
1466
1467 /* Complete the move up one level */
1468 na = pna;
1469 ns = pns;
1470 bus = pbus;
1471
1472 of_dump_addr("OF: one level translation:", addr, na);
1473 }
1474 bail:
1475
1476 return result;
1477}
1478
11e44fc6
SW
1479u64 fdt_translate_address(const void *blob, int node_offset,
1480 const fdt32_t *in_addr)
a0342c08
KG
1481{
1482 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1483}
75e73afd 1484
641067fb
FD
1485u64 fdt_translate_dma_address(const void *blob, int node_offset,
1486 const fdt32_t *in_addr)
1487{
1488 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1489}
1490
51bdb509
NSJ
1491int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1492 dma_addr_t *bus, u64 *size)
1493{
1494 bool found_dma_ranges = false;
1495 struct of_bus *bus_node;
1496 const fdt32_t *ranges;
1497 int na, ns, pna, pns;
1498 int parent = node;
1499 int ret = 0;
1500 int len;
1501
1502 /* Find the closest dma-ranges property */
1503 while (parent >= 0) {
1504 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1505
1506 /* Ignore empty ranges, they imply no translation required */
1507 if (ranges && len > 0)
1508 break;
1509
1510 /* Once we find 'dma-ranges', then a missing one is an error */
1511 if (found_dma_ranges && !ranges) {
1512 ret = -EINVAL;
1513 goto out;
1514 }
1515
1516 if (ranges)
1517 found_dma_ranges = true;
1518
1519 parent = fdt_parent_offset(blob, parent);
1520 }
1521
1522 if (!ranges || parent < 0) {
1523 debug("no dma-ranges found for node %s\n",
1524 fdt_get_name(blob, node, NULL));
1525 ret = -ENOENT;
1526 goto out;
1527 }
1528
1529 /* switch to that node */
1530 node = parent;
1531 parent = fdt_parent_offset(blob, node);
1532 if (parent < 0) {
8c8bf4f1 1533 printf("Found dma-ranges in root node, shouldn't happen\n");
51bdb509
NSJ
1534 ret = -EINVAL;
1535 goto out;
1536 }
1537
1538 /* Get the address sizes both for the bus and its parent */
1539 bus_node = of_match_bus(blob, node);
1540 bus_node->count_cells(blob, node, &na, &ns);
1541 if (!OF_CHECK_COUNTS(na, ns)) {
1542 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1543 fdt_get_name(blob, node, NULL));
1544 return -EINVAL;
1545 goto out;
1546 }
1547
1548 bus_node = of_match_bus(blob, parent);
1549 bus_node->count_cells(blob, parent, &pna, &pns);
1550 if (!OF_CHECK_COUNTS(pna, pns)) {
1551 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1552 fdt_get_name(blob, parent, NULL));
1553 return -EINVAL;
1554 goto out;
1555 }
1556
1557 *bus = fdt_read_number(ranges, na);
1558 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1559 *size = fdt_read_number(ranges + na + pna, ns);
1560out:
1561 return ret;
1562}
1563
75e73afd 1564/**
ded112f2 1565 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
75e73afd
KG
1566 * who's reg property matches a physical cpu address
1567 *
1568 * @blob: ptr to device tree
ded112f2 1569 * @compat: compatible string to match
75e73afd
KG
1570 * @compat_off: property name
1571 *
1572 */
1573int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1574 phys_addr_t compat_off)
1575{
3058e283
MB
1576 int len, off;
1577
1578 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
8aa5ec6e 1579 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
3058e283
MB
1580 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1581 return off;
75e73afd
KG
1582 }
1583
1584 return -FDT_ERR_NOTFOUND;
1585}
1586
9ab0c2f8
MB
1587static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1588{
1589 char path[512];
1590 int len;
1591
1592 len = vsnprintf(path, sizeof(path), fmt, ap);
1593 if (len < 0 || len + 1 > sizeof(path))
1594 return -FDT_ERR_NOSPACE;
1595
1596 return fdt_path_offset(blob, path);
1597}
1598
1599/**
1600 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1601 *
1602 * @blob: ptr to device tree
1603 * @fmt: path format
1604 * @ap: vsnprintf arguments
1605 */
1606int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1607{
1608 va_list ap;
1609 int res;
1610
1611 va_start(ap, fmt);
1612 res = vnode_offset_by_pathf(blob, fmt, ap);
1613 va_end(ap);
1614
1615 return res;
1616}
1617
a8d2a75d 1618/*
f117c0f0 1619 * fdt_set_phandle: Create a phandle property for the given node
a8d2a75d
GVB
1620 *
1621 * @fdt: ptr to device tree
1622 * @nodeoffset: node to update
1623 * @phandle: phandle value to set (must be unique)
f117c0f0
KG
1624 */
1625int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
a8d2a75d
GVB
1626{
1627 int ret;
1628
1629#ifdef DEBUG
1630 int off = fdt_node_offset_by_phandle(fdt, phandle);
1631
1632 if ((off >= 0) && (off != nodeoffset)) {
1633 char buf[64];
1634
1635 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1636 printf("Trying to update node %s with phandle %u ",
1637 buf, phandle);
1638
1639 fdt_get_path(fdt, off, buf, sizeof(buf));
1640 printf("that already exists in node %s.\n", buf);
1641 return -FDT_ERR_BADPHANDLE;
1642 }
1643#endif
1644
1645 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
a8d2a75d
GVB
1646
1647 return ret;
1648}
1649
10aeabd1 1650/*
c92ccba7 1651 * fdt_create_phandle: Get or create a phandle property for the given node
10aeabd1
KG
1652 *
1653 * @fdt: ptr to device tree
1654 * @nodeoffset: node to update
1655 */
3c927ccc 1656unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
10aeabd1
KG
1657{
1658 /* see if there is a phandle already */
76f5a728 1659 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
10aeabd1
KG
1660
1661 /* if we got 0, means no phandle so create one */
1662 if (phandle == 0) {
3c927ccc
TT
1663 int ret;
1664
76f5a728
MB
1665 ret = fdt_generate_phandle(fdt, &phandle);
1666 if (ret < 0) {
1667 printf("Can't generate phandle: %s\n",
1668 fdt_strerror(ret));
1669 return 0;
1670 }
1671
3c927ccc
TT
1672 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1673 if (ret < 0) {
1674 printf("Can't set phandle %u: %s\n", phandle,
1675 fdt_strerror(ret));
1676 return 0;
1677 }
10aeabd1
KG
1678 }
1679
1680 return phandle;
1681}
1682
9ab0c2f8
MB
1683/**
1684 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1685 * given compatible
1686 *
1687 * @fdt: ptr to device tree
1688 * @compat: node's compatible string
1689 */
1690unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1691{
1692 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1693
1694 if (offset < 0) {
1695 printf("Can't find node with compatible \"%s\": %s\n", compat,
1696 fdt_strerror(offset));
1697 return 0;
1698 }
1699
1700 return fdt_create_phandle(fdt, offset);
1701}
1702
1703/**
1704 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1705 * sprintf-formatted path
1706 *
1707 * @fdt: ptr to device tree
1708 * @fmt, ...: path format string and arguments to pass to sprintf
1709 */
1710unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1711{
1712 va_list ap;
1713 int offset;
1714
1715 va_start(ap, fmt);
1716 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1717 va_end(ap);
1718
1719 if (offset < 0) {
1720 printf("Can't find node by given path: %s\n",
1721 fdt_strerror(offset));
1722 return 0;
1723 }
1724
1725 return fdt_create_phandle(fdt, offset);
1726}
1727
2a523f52
SL
1728/*
1729 * fdt_set_node_status: Set status for the given node
1730 *
1731 * @fdt: ptr to device tree
1732 * @nodeoffset: node to update
2105cd04 1733 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
2a523f52 1734 */
2105cd04 1735int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
2a523f52 1736{
2a523f52
SL
1737 int ret = 0;
1738
1739 if (nodeoffset < 0)
1740 return nodeoffset;
1741
1742 switch (status) {
1743 case FDT_STATUS_OKAY:
1744 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1745 break;
1746 case FDT_STATUS_DISABLED:
1747 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1748 break;
1749 case FDT_STATUS_FAIL:
1750 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1751 break;
2a523f52
SL
1752 default:
1753 printf("Invalid fdt status: %x\n", status);
1754 ret = -1;
1755 break;
1756 }
1757
1758 return ret;
1759}
1760
1761/*
1762 * fdt_set_status_by_alias: Set status for the given node given an alias
1763 *
1764 * @fdt: ptr to device tree
1765 * @alias: alias of node to update
2105cd04 1766 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
2a523f52
SL
1767 */
1768int fdt_set_status_by_alias(void *fdt, const char* alias,
2105cd04 1769 enum fdt_status status)
2a523f52
SL
1770{
1771 int offset = fdt_path_offset(fdt, alias);
1772
2105cd04 1773 return fdt_set_node_status(fdt, offset, status);
2a523f52
SL
1774}
1775
9ab0c2f8
MB
1776/**
1777 * fdt_set_status_by_compatible: Set node status for first node with given
1778 * compatible
1779 *
1780 * @fdt: ptr to device tree
1781 * @compat: node's compatible string
1782 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1783 */
1784int fdt_set_status_by_compatible(void *fdt, const char *compat,
1785 enum fdt_status status)
1786{
1787 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1788
1789 if (offset < 0)
1790 return offset;
1791
1792 return fdt_set_node_status(fdt, offset, status);
1793}
1794
1795/**
1796 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1797 * path
1798 *
1799 * @fdt: ptr to device tree
1800 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1801 * @fmt, ...: path format string and arguments to pass to sprintf
1802 */
1803int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1804 ...)
1805{
1806 va_list ap;
1807 int offset;
1808
1809 va_start(ap, fmt);
1810 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1811 va_end(ap);
1812
1813 if (offset < 0)
1814 return offset;
1815
1816 return fdt_set_node_status(fdt, offset, status);
1817}
1818
bb682001
TT
1819/*
1820 * Verify the physical address of device tree node for a given alias
1821 *
1822 * This function locates the device tree node of a given alias, and then
1823 * verifies that the physical address of that device matches the given
1824 * parameter. It displays a message if there is a mismatch.
1825 *
1826 * Returns 1 on success, 0 on failure
1827 */
1828int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1829{
1830 const char *path;
8aa5ec6e 1831 const fdt32_t *reg;
bb682001
TT
1832 int node, len;
1833 u64 dt_addr;
1834
1835 path = fdt_getprop(fdt, anode, alias, NULL);
1836 if (!path) {
1837 /* If there's no such alias, then it's not a failure */
1838 return 1;
1839 }
1840
1841 node = fdt_path_offset(fdt, path);
1842 if (node < 0) {
1843 printf("Warning: device tree alias '%s' points to invalid "
1844 "node %s.\n", alias, path);
1845 return 0;
1846 }
1847
1848 reg = fdt_getprop(fdt, node, "reg", &len);
1849 if (!reg) {
1850 printf("Warning: device tree node '%s' has no address.\n",
1851 path);
1852 return 0;
1853 }
1854
1855 dt_addr = fdt_translate_address(fdt, node, reg);
1856 if (addr != dt_addr) {
dee37fc9
MY
1857 printf("Warning: U-Boot configured device %s at address %llu,\n"
1858 "but the device tree has it address %llx.\n",
1859 alias, addr, dt_addr);
bb682001
TT
1860 return 0;
1861 }
1862
1863 return 1;
1864}
1865
1866/*
1867 * Returns the base address of an SOC or PCI node
1868 */
ec002119 1869u64 fdt_get_base_address(const void *fdt, int node)
bb682001
TT
1870{
1871 int size;
8aa5ec6e 1872 const fdt32_t *prop;
bb682001 1873
336a4487 1874 prop = fdt_getprop(fdt, node, "reg", &size);
bb682001 1875
e3665ba9 1876 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
bb682001 1877}
c48e6868
AG
1878
1879/*
a932aa3c
BM
1880 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1881 * or 3 cells specially for a PCI address.
c48e6868
AG
1882 */
1883static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1884 uint64_t *val, int cells)
1885{
a932aa3c
BM
1886 const fdt32_t *prop32;
1887 const unaligned_fdt64_t *prop64;
c48e6868
AG
1888
1889 if ((cell_off + cells) > prop_len)
1890 return -FDT_ERR_NOSPACE;
1891
a932aa3c
BM
1892 prop32 = &prop[cell_off];
1893
1894 /*
1895 * Special handling for PCI address in PCI bus <ranges>
1896 *
1897 * PCI child address is made up of 3 cells. Advance the cell offset
1898 * by 1 so that the PCI child address can be correctly read.
1899 */
1900 if (cells == 3)
1901 cell_off += 1;
1902 prop64 = (const fdt64_t *)&prop[cell_off];
1903
c48e6868
AG
1904 switch (cells) {
1905 case 1:
1906 *val = fdt32_to_cpu(*prop32);
1907 break;
1908 case 2:
a932aa3c 1909 case 3:
c48e6868
AG
1910 *val = fdt64_to_cpu(*prop64);
1911 break;
1912 default:
1913 return -FDT_ERR_NOSPACE;
1914 }
1915
1916 return 0;
1917}
1918
1919/**
1920 * fdt_read_range - Read a node's n'th range property
1921 *
1922 * @fdt: ptr to device tree
1923 * @node: offset of node
1924 * @n: range index
1925 * @child_addr: pointer to storage for the "child address" field
1926 * @addr: pointer to storage for the CPU view translated physical start
1927 * @len: pointer to storage for the range length
1928 *
1929 * Convenience function that reads and interprets a specific range out of
1930 * a number of the "ranges" property array.
1931 */
1932int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1933 uint64_t *addr, uint64_t *len)
1934{
1935 int pnode = fdt_parent_offset(fdt, node);
1936 const fdt32_t *ranges;
1937 int pacells;
1938 int acells;
1939 int scells;
1940 int ranges_len;
1941 int cell = 0;
1942 int r = 0;
1943
1944 /*
1945 * The "ranges" property is an array of
1946 * { <child address> <parent address> <size in child address space> }
1947 *
1948 * All 3 elements can span a diffent number of cells. Fetch their size.
1949 */
1950 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1951 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1952 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1953
1954 /* Now try to get the ranges property */
1955 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1956 if (!ranges)
1957 return -FDT_ERR_NOTFOUND;
1958 ranges_len /= sizeof(uint32_t);
1959
1960 /* Jump to the n'th entry */
1961 cell = n * (pacells + acells + scells);
1962
1963 /* Read <child address> */
1964 if (child_addr) {
1965 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1966 acells);
1967 if (r)
1968 return r;
1969 }
1970 cell += acells;
1971
1972 /* Read <parent address> */
1973 if (addr)
1974 *addr = fdt_translate_address(fdt, node, ranges + cell);
1975 cell += pacells;
1976
1977 /* Read <size in child address space> */
1978 if (len) {
1979 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1980 if (r)
1981 return r;
1982 }
1983
1984 return 0;
1985}
d4f495a8
HG
1986
1987/**
1988 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1989 *
1990 * @fdt: ptr to device tree
1991 * @node: offset of the simplefb node
1992 * @base_address: framebuffer base address
1993 * @width: width in pixels
1994 * @height: height in pixels
1995 * @stride: bytes per line
1996 * @format: pixel format string
1997 *
1998 * Convenience function to fill and enable a simplefb node.
1999 */
2000int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
2001 u32 height, u32 stride, const char *format)
2002{
2003 char name[32];
2004 fdt32_t cells[4];
2005 int i, addrc, sizec, ret;
2006
eed36609
SG
2007 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
2008 &addrc, &sizec);
d4f495a8
HG
2009 i = 0;
2010 if (addrc == 2)
2011 cells[i++] = cpu_to_fdt32(base_address >> 32);
2012 cells[i++] = cpu_to_fdt32(base_address);
2013 if (sizec == 2)
2014 cells[i++] = 0;
2015 cells[i++] = cpu_to_fdt32(height * stride);
2016
2017 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
2018 if (ret < 0)
2019 return ret;
2020
dee37fc9 2021 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
d4f495a8
HG
2022 ret = fdt_set_name(fdt, node, name);
2023 if (ret < 0)
2024 return ret;
2025
2026 ret = fdt_setprop_u32(fdt, node, "width", width);
2027 if (ret < 0)
2028 return ret;
2029
2030 ret = fdt_setprop_u32(fdt, node, "height", height);
2031 if (ret < 0)
2032 return ret;
2033
2034 ret = fdt_setprop_u32(fdt, node, "stride", stride);
2035 if (ret < 0)
2036 return ret;
2037
2038 ret = fdt_setprop_string(fdt, node, "format", format);
2039 if (ret < 0)
2040 return ret;
2041
2042 ret = fdt_setprop_string(fdt, node, "status", "okay");
2043 if (ret < 0)
2044 return ret;
2045
2046 return 0;
2047}
08daa258 2048
efe1ceec
DT
2049#if CONFIG_IS_ENABLED(VIDEO)
2050int fdt_add_fb_mem_rsv(void *blob)
2051{
2052 struct fdt_memory mem;
2053
2054 /* nothing to do when the frame buffer is not defined */
2055 if (gd->video_bottom == gd->video_top)
2056 return 0;
2057
2058 /* reserved with no-map tag the video buffer */
2059 mem.start = gd->video_bottom;
2060 mem.end = gd->video_top - 1;
2061
2062 return fdtdec_add_reserved_memory(blob, "framebuffer", &mem, NULL, 0, NULL,
2063 FDTDEC_RESERVED_MEMORY_NO_MAP);
2064}
2065#endif
2066
08daa258
TH
2067/*
2068 * Update native-mode in display-timings from display environment variable.
2069 * The node to update are specified by path.
2070 */
2071int fdt_fixup_display(void *blob, const char *path, const char *display)
2072{
2073 int off, toff;
2074
2075 if (!display || !path)
2076 return -FDT_ERR_NOTFOUND;
2077
2078 toff = fdt_path_offset(blob, path);
2079 if (toff >= 0)
2080 toff = fdt_subnode_offset(blob, toff, "display-timings");
2081 if (toff < 0)
2082 return toff;
2083
2084 for (off = fdt_first_subnode(blob, toff);
2085 off >= 0;
2086 off = fdt_next_subnode(blob, off)) {
2087 uint32_t h = fdt_get_phandle(blob, off);
2088 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
2089 fdt32_to_cpu(h));
2090 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2091 return fdt_setprop_u32(blob, toff, "native-mode", h);
2092 }
2093 return toff;
2094}
fc7c3189
PA
2095
2096#ifdef CONFIG_OF_LIBFDT_OVERLAY
2097/**
2098 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2099 *
2100 * @fdt: ptr to device tree
2101 * @fdto: ptr to device tree overlay
2102 *
2103 * Convenience function to apply an overlay and display helpful messages
2104 * in the case of an error
2105 */
2106int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2107{
2108 int err;
2109 bool has_symbols;
2110
2111 err = fdt_path_offset(fdt, "/__symbols__");
2112 has_symbols = err >= 0;
2113
2114 err = fdt_overlay_apply(fdt, fdto);
2115 if (err < 0) {
2116 printf("failed on fdt_overlay_apply(): %s\n",
2117 fdt_strerror(err));
2118 if (!has_symbols) {
a3a884c6 2119 printf("base fdt does not have a /__symbols__ node\n");
fc7c3189
PA
2120 printf("make sure you've compiled with -@\n");
2121 }
2122 }
2123 return err;
2124}
2125#endif
bbdbcaf5
KM
2126
2127/**
2128 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2129 *
2130 * @blobp: Pointer to FDT pointer
185f812c 2131 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
bbdbcaf5
KM
2132 */
2133int fdt_valid(struct fdt_header **blobp)
2134{
2135 const void *blob = *blobp;
2136 int err;
2137
2138 if (!blob) {
2139 printf("The address of the fdt is invalid (NULL).\n");
2140 return 0;
2141 }
2142
2143 err = fdt_check_header(blob);
2144 if (err == 0)
2145 return 1; /* valid */
2146
2147 if (err < 0) {
2148 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2149 /*
2150 * Be more informative on bad version.
2151 */
2152 if (err == -FDT_ERR_BADVERSION) {
2153 if (fdt_version(blob) <
2154 FDT_FIRST_SUPPORTED_VERSION) {
2155 printf(" - too old, fdt %d < %d",
2156 fdt_version(blob),
2157 FDT_FIRST_SUPPORTED_VERSION);
2158 }
2159 if (fdt_last_comp_version(blob) >
2160 FDT_LAST_SUPPORTED_VERSION) {
2161 printf(" - too new, fdt %d > %d",
2162 fdt_version(blob),
2163 FDT_LAST_SUPPORTED_VERSION);
2164 }
2165 }
2166 printf("\n");
2167 *blobp = NULL;
2168 return 0;
2169 }
2170 return 1;
2171}