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