xref: /rk3399_rockchip-uboot/common/fdt_support.c (revision ec4fa56743a3534c4bace2c9c72ea36b9d41000c)
1 /*
2  * (C) Copyright 2007
3  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4  *
5  * Copyright 2010-2011 Freescale Semiconductor, Inc.
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <stdio_dev.h>
12 #include <linux/ctype.h>
13 #include <linux/types.h>
14 #include <asm/global_data.h>
15 #include <libfdt.h>
16 #include <fdt_support.h>
17 #include <exports.h>
18 
19 /*
20  * Global data (for the gd->bd)
21  */
22 DECLARE_GLOBAL_DATA_PTR;
23 
24 /*
25  * Get cells len in bytes
26  *     if #NNNN-cells property is 2 then len is 8
27  *     otherwise len is 4
28  */
29 static int get_cells_len(void *blob, char *nr_cells_name)
30 {
31 	const fdt32_t *cell;
32 
33 	cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
34 	if (cell && fdt32_to_cpu(*cell) == 2)
35 		return 8;
36 
37 	return 4;
38 }
39 
40 /*
41  * Write a 4 or 8 byte big endian cell
42  */
43 static void write_cell(u8 *addr, u64 val, int size)
44 {
45 	int shift = (size - 1) * 8;
46 	while (size-- > 0) {
47 		*addr++ = (val >> shift) & 0xff;
48 		shift -= 8;
49 	}
50 }
51 
52 /**
53  * fdt_getprop_u32_default - Find a node and return it's property or a default
54  *
55  * @fdt: ptr to device tree
56  * @path: path of node
57  * @prop: property name
58  * @dflt: default value if the property isn't found
59  *
60  * Convenience function to find a node and return it's property or a
61  * default value if it doesn't exist.
62  */
63 u32 fdt_getprop_u32_default(const void *fdt, const char *path,
64 				const char *prop, const u32 dflt)
65 {
66 	const fdt32_t *val;
67 	int off;
68 
69 	off = fdt_path_offset(fdt, path);
70 	if (off < 0)
71 		return dflt;
72 
73 	val = fdt_getprop(fdt, off, prop, NULL);
74 	if (val)
75 		return fdt32_to_cpu(*val);
76 	else
77 		return dflt;
78 }
79 
80 /**
81  * fdt_find_and_setprop: Find a node and set it's property
82  *
83  * @fdt: ptr to device tree
84  * @node: path of node
85  * @prop: property name
86  * @val: ptr to new value
87  * @len: length of new property value
88  * @create: flag to create the property if it doesn't exist
89  *
90  * Convenience function to directly set a property given the path to the node.
91  */
92 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
93 			 const void *val, int len, int create)
94 {
95 	int nodeoff = fdt_path_offset(fdt, node);
96 
97 	if (nodeoff < 0)
98 		return nodeoff;
99 
100 	if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
101 		return 0; /* create flag not set; so exit quietly */
102 
103 	return fdt_setprop(fdt, nodeoff, prop, val, len);
104 }
105 
106 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
107 
108 #ifdef CONFIG_CONS_INDEX
109 static void fdt_fill_multisername(char *sername, size_t maxlen)
110 {
111 	const char *outname = stdio_devices[stdout]->name;
112 
113 	if (strcmp(outname, "serial") > 0)
114 		strncpy(sername, outname, maxlen);
115 
116 	/* eserial? */
117 	if (strcmp(outname + 1, "serial") > 0)
118 		strncpy(sername, outname + 1, maxlen);
119 }
120 #endif
121 
122 static int fdt_fixup_stdout(void *fdt, int chosenoff)
123 {
124 	int err = 0;
125 #ifdef CONFIG_CONS_INDEX
126 	int node;
127 	char sername[9] = { 0 };
128 	const char *path;
129 
130 	fdt_fill_multisername(sername, sizeof(sername) - 1);
131 	if (!sername[0])
132 		sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
133 
134 	err = node = fdt_path_offset(fdt, "/aliases");
135 	if (node >= 0) {
136 		int len;
137 		path = fdt_getprop(fdt, node, sername, &len);
138 		if (path) {
139 			char *p = malloc(len);
140 			err = -FDT_ERR_NOSPACE;
141 			if (p) {
142 				memcpy(p, path, len);
143 				err = fdt_setprop(fdt, chosenoff,
144 					"linux,stdout-path", p, len);
145 				free(p);
146 			}
147 		} else {
148 			err = len;
149 		}
150 	}
151 #endif
152 	if (err < 0)
153 		printf("WARNING: could not set linux,stdout-path %s.\n",
154 				fdt_strerror(err));
155 
156 	return err;
157 }
158 #endif
159 
160 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
161 {
162 	int   nodeoffset, addr_cell_len;
163 	int   err, j, total;
164 	fdt64_t  tmp;
165 	const char *path;
166 	uint64_t addr, size;
167 
168 	/* Find the "chosen" node.  */
169 	nodeoffset = fdt_path_offset (fdt, "/chosen");
170 
171 	/* If there is no "chosen" node in the blob return */
172 	if (nodeoffset < 0) {
173 		printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
174 		return nodeoffset;
175 	}
176 
177 	/* just return if initrd_start/end aren't valid */
178 	if ((initrd_start == 0) || (initrd_end == 0))
179 		return 0;
180 
181 	total = fdt_num_mem_rsv(fdt);
182 
183 	/*
184 	 * Look for an existing entry and update it.  If we don't find
185 	 * the entry, we will j be the next available slot.
186 	 */
187 	for (j = 0; j < total; j++) {
188 		err = fdt_get_mem_rsv(fdt, j, &addr, &size);
189 		if (addr == initrd_start) {
190 			fdt_del_mem_rsv(fdt, j);
191 			break;
192 		}
193 	}
194 
195 	err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
196 	if (err < 0) {
197 		printf("fdt_initrd: %s\n", fdt_strerror(err));
198 		return err;
199 	}
200 
201 	addr_cell_len = get_cells_len(fdt, "#address-cells");
202 
203 	path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
204 	if ((path == NULL) || force) {
205 		write_cell((u8 *)&tmp, initrd_start, addr_cell_len);
206 		err = fdt_setprop(fdt, nodeoffset,
207 			"linux,initrd-start", &tmp, sizeof(tmp));
208 		if (err < 0) {
209 			printf("WARNING: "
210 				"could not set linux,initrd-start %s.\n",
211 				fdt_strerror(err));
212 			return err;
213 		}
214 		write_cell((u8 *)&tmp, initrd_end, addr_cell_len);
215 		err = fdt_setprop(fdt, nodeoffset,
216 			"linux,initrd-end", &tmp, sizeof(tmp));
217 		if (err < 0) {
218 			printf("WARNING: could not set linux,initrd-end %s.\n",
219 				fdt_strerror(err));
220 
221 			return err;
222 		}
223 	}
224 
225 	return 0;
226 }
227 
228 int fdt_chosen(void *fdt, int force)
229 {
230 	int   nodeoffset;
231 	int   err;
232 	char  *str;		/* used to set string properties */
233 	const char *path;
234 
235 	err = fdt_check_header(fdt);
236 	if (err < 0) {
237 		printf("fdt_chosen: %s\n", fdt_strerror(err));
238 		return err;
239 	}
240 
241 	/*
242 	 * Find the "chosen" node.
243 	 */
244 	nodeoffset = fdt_path_offset (fdt, "/chosen");
245 
246 	/*
247 	 * If there is no "chosen" node in the blob, create it.
248 	 */
249 	if (nodeoffset < 0) {
250 		/*
251 		 * Create a new node "/chosen" (offset 0 is root level)
252 		 */
253 		nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
254 		if (nodeoffset < 0) {
255 			printf("WARNING: could not create /chosen %s.\n",
256 				fdt_strerror(nodeoffset));
257 			return nodeoffset;
258 		}
259 	}
260 
261 	/*
262 	 * Create /chosen properites that don't exist in the fdt.
263 	 * If the property exists, update it only if the "force" parameter
264 	 * is true.
265 	 */
266 	str = getenv("bootargs");
267 	if (str != NULL) {
268 		path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
269 		if ((path == NULL) || force) {
270 			err = fdt_setprop(fdt, nodeoffset,
271 				"bootargs", str, strlen(str)+1);
272 			if (err < 0)
273 				printf("WARNING: could not set bootargs %s.\n",
274 					fdt_strerror(err));
275 		}
276 	}
277 
278 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
279 	path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
280 	if ((path == NULL) || force)
281 		err = fdt_fixup_stdout(fdt, nodeoffset);
282 #endif
283 
284 #ifdef OF_STDOUT_PATH
285 	path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
286 	if ((path == NULL) || force) {
287 		err = fdt_setprop(fdt, nodeoffset,
288 			"linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
289 		if (err < 0)
290 			printf("WARNING: could not set linux,stdout-path %s.\n",
291 				fdt_strerror(err));
292 	}
293 #endif
294 
295 	return err;
296 }
297 
298 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
299 		      const void *val, int len, int create)
300 {
301 #if defined(DEBUG)
302 	int i;
303 	debug("Updating property '%s/%s' = ", path, prop);
304 	for (i = 0; i < len; i++)
305 		debug(" %.2x", *(u8*)(val+i));
306 	debug("\n");
307 #endif
308 	int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
309 	if (rc)
310 		printf("Unable to update property %s:%s, err=%s\n",
311 			path, prop, fdt_strerror(rc));
312 }
313 
314 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
315 			  u32 val, int create)
316 {
317 	fdt32_t tmp = cpu_to_fdt32(val);
318 	do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
319 }
320 
321 void do_fixup_by_prop(void *fdt,
322 		      const char *pname, const void *pval, int plen,
323 		      const char *prop, const void *val, int len,
324 		      int create)
325 {
326 	int off;
327 #if defined(DEBUG)
328 	int i;
329 	debug("Updating property '%s' = ", prop);
330 	for (i = 0; i < len; i++)
331 		debug(" %.2x", *(u8*)(val+i));
332 	debug("\n");
333 #endif
334 	off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
335 	while (off != -FDT_ERR_NOTFOUND) {
336 		if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
337 			fdt_setprop(fdt, off, prop, val, len);
338 		off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
339 	}
340 }
341 
342 void do_fixup_by_prop_u32(void *fdt,
343 			  const char *pname, const void *pval, int plen,
344 			  const char *prop, u32 val, int create)
345 {
346 	fdt32_t tmp = cpu_to_fdt32(val);
347 	do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
348 }
349 
350 void do_fixup_by_compat(void *fdt, const char *compat,
351 			const char *prop, const void *val, int len, int create)
352 {
353 	int off = -1;
354 #if defined(DEBUG)
355 	int i;
356 	debug("Updating property '%s' = ", prop);
357 	for (i = 0; i < len; i++)
358 		debug(" %.2x", *(u8*)(val+i));
359 	debug("\n");
360 #endif
361 	off = fdt_node_offset_by_compatible(fdt, -1, compat);
362 	while (off != -FDT_ERR_NOTFOUND) {
363 		if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
364 			fdt_setprop(fdt, off, prop, val, len);
365 		off = fdt_node_offset_by_compatible(fdt, off, compat);
366 	}
367 }
368 
369 void do_fixup_by_compat_u32(void *fdt, const char *compat,
370 			    const char *prop, u32 val, int create)
371 {
372 	fdt32_t tmp = cpu_to_fdt32(val);
373 	do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
374 }
375 
376 #ifdef CONFIG_NR_DRAM_BANKS
377 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
378 #else
379 #define MEMORY_BANKS_MAX 4
380 #endif
381 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
382 {
383 	int err, nodeoffset;
384 	int addr_cell_len, size_cell_len, len;
385 	u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
386 	int bank;
387 
388 	if (banks > MEMORY_BANKS_MAX) {
389 		printf("%s: num banks %d exceeds hardcoded limit %d."
390 		       " Recompile with higher MEMORY_BANKS_MAX?\n",
391 		       __FUNCTION__, banks, MEMORY_BANKS_MAX);
392 		return -1;
393 	}
394 
395 	err = fdt_check_header(blob);
396 	if (err < 0) {
397 		printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
398 		return err;
399 	}
400 
401 	/* update, or add and update /memory node */
402 	nodeoffset = fdt_path_offset(blob, "/memory");
403 	if (nodeoffset < 0) {
404 		nodeoffset = fdt_add_subnode(blob, 0, "memory");
405 		if (nodeoffset < 0)
406 			printf("WARNING: could not create /memory: %s.\n",
407 					fdt_strerror(nodeoffset));
408 		return nodeoffset;
409 	}
410 	err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
411 			sizeof("memory"));
412 	if (err < 0) {
413 		printf("WARNING: could not set %s %s.\n", "device_type",
414 				fdt_strerror(err));
415 		return err;
416 	}
417 
418 	addr_cell_len = get_cells_len(blob, "#address-cells");
419 	size_cell_len = get_cells_len(blob, "#size-cells");
420 
421 	for (bank = 0, len = 0; bank < banks; bank++) {
422 		write_cell(tmp + len, start[bank], addr_cell_len);
423 		len += addr_cell_len;
424 
425 		write_cell(tmp + len, size[bank], size_cell_len);
426 		len += size_cell_len;
427 	}
428 
429 	err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
430 	if (err < 0) {
431 		printf("WARNING: could not set %s %s.\n",
432 				"reg", fdt_strerror(err));
433 		return err;
434 	}
435 	return 0;
436 }
437 
438 int fdt_fixup_memory(void *blob, u64 start, u64 size)
439 {
440 	return fdt_fixup_memory_banks(blob, &start, &size, 1);
441 }
442 
443 void fdt_fixup_ethernet(void *fdt)
444 {
445 	int node, i, j;
446 	char enet[16], *tmp, *end;
447 	char mac[16];
448 	const char *path;
449 	unsigned char mac_addr[6];
450 
451 	node = fdt_path_offset(fdt, "/aliases");
452 	if (node < 0)
453 		return;
454 
455 	i = 0;
456 	strcpy(mac, "ethaddr");
457 	while ((tmp = getenv(mac)) != NULL) {
458 		sprintf(enet, "ethernet%d", i);
459 		path = fdt_getprop(fdt, node, enet, NULL);
460 		if (!path) {
461 			debug("No alias for %s\n", enet);
462 			sprintf(mac, "eth%daddr", ++i);
463 			continue;
464 		}
465 
466 		for (j = 0; j < 6; j++) {
467 			mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
468 			if (tmp)
469 				tmp = (*end) ? end+1 : end;
470 		}
471 
472 		do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
473 		do_fixup_by_path(fdt, path, "local-mac-address",
474 				&mac_addr, 6, 1);
475 
476 		sprintf(mac, "eth%daddr", ++i);
477 	}
478 }
479 
480 /* Resize the fdt to its actual size + a bit of padding */
481 int fdt_resize(void *blob)
482 {
483 	int i;
484 	uint64_t addr, size;
485 	int total, ret;
486 	uint actualsize;
487 
488 	if (!blob)
489 		return 0;
490 
491 	total = fdt_num_mem_rsv(blob);
492 	for (i = 0; i < total; i++) {
493 		fdt_get_mem_rsv(blob, i, &addr, &size);
494 		if (addr == (uintptr_t)blob) {
495 			fdt_del_mem_rsv(blob, i);
496 			break;
497 		}
498 	}
499 
500 	/*
501 	 * Calculate the actual size of the fdt
502 	 * plus the size needed for 5 fdt_add_mem_rsv, one
503 	 * for the fdt itself and 4 for a possible initrd
504 	 * ((initrd-start + initrd-end) * 2 (name & value))
505 	 */
506 	actualsize = fdt_off_dt_strings(blob) +
507 		fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
508 
509 	/* Make it so the fdt ends on a page boundary */
510 	actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
511 	actualsize = actualsize - ((uintptr_t)blob & 0xfff);
512 
513 	/* Change the fdt header to reflect the correct size */
514 	fdt_set_totalsize(blob, actualsize);
515 
516 	/* Add the new reservation */
517 	ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
518 	if (ret < 0)
519 		return ret;
520 
521 	return actualsize;
522 }
523 
524 #ifdef CONFIG_PCI
525 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
526 
527 #define FDT_PCI_PREFETCH	(0x40000000)
528 #define FDT_PCI_MEM32		(0x02000000)
529 #define FDT_PCI_IO		(0x01000000)
530 #define FDT_PCI_MEM64		(0x03000000)
531 
532 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
533 
534 	int addrcell, sizecell, len, r;
535 	u32 *dma_range;
536 	/* sized based on pci addr cells, size-cells, & address-cells */
537 	u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
538 
539 	addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
540 	sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
541 
542 	dma_range = &dma_ranges[0];
543 	for (r = 0; r < hose->region_count; r++) {
544 		u64 bus_start, phys_start, size;
545 
546 		/* skip if !PCI_REGION_SYS_MEMORY */
547 		if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
548 			continue;
549 
550 		bus_start = (u64)hose->regions[r].bus_start;
551 		phys_start = (u64)hose->regions[r].phys_start;
552 		size = (u64)hose->regions[r].size;
553 
554 		dma_range[0] = 0;
555 		if (size >= 0x100000000ull)
556 			dma_range[0] |= FDT_PCI_MEM64;
557 		else
558 			dma_range[0] |= FDT_PCI_MEM32;
559 		if (hose->regions[r].flags & PCI_REGION_PREFETCH)
560 			dma_range[0] |= FDT_PCI_PREFETCH;
561 #ifdef CONFIG_SYS_PCI_64BIT
562 		dma_range[1] = bus_start >> 32;
563 #else
564 		dma_range[1] = 0;
565 #endif
566 		dma_range[2] = bus_start & 0xffffffff;
567 
568 		if (addrcell == 2) {
569 			dma_range[3] = phys_start >> 32;
570 			dma_range[4] = phys_start & 0xffffffff;
571 		} else {
572 			dma_range[3] = phys_start & 0xffffffff;
573 		}
574 
575 		if (sizecell == 2) {
576 			dma_range[3 + addrcell + 0] = size >> 32;
577 			dma_range[3 + addrcell + 1] = size & 0xffffffff;
578 		} else {
579 			dma_range[3 + addrcell + 0] = size & 0xffffffff;
580 		}
581 
582 		dma_range += (3 + addrcell + sizecell);
583 	}
584 
585 	len = dma_range - &dma_ranges[0];
586 	if (len)
587 		fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
588 
589 	return 0;
590 }
591 #endif
592 
593 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
594 /*
595  * Provide a weak default function to return the flash bank size.
596  * There might be multiple non-identical flash chips connected to one
597  * chip-select, so we need to pass an index as well.
598  */
599 u32 __flash_get_bank_size(int cs, int idx)
600 {
601 	extern flash_info_t flash_info[];
602 
603 	/*
604 	 * As default, a simple 1:1 mapping is provided. Boards with
605 	 * a different mapping need to supply a board specific mapping
606 	 * routine.
607 	 */
608 	return flash_info[cs].size;
609 }
610 u32 flash_get_bank_size(int cs, int idx)
611 	__attribute__((weak, alias("__flash_get_bank_size")));
612 
613 /*
614  * This function can be used to update the size in the "reg" property
615  * of all NOR FLASH device nodes. This is necessary for boards with
616  * non-fixed NOR FLASH sizes.
617  */
618 int fdt_fixup_nor_flash_size(void *blob)
619 {
620 	char compat[][16] = { "cfi-flash", "jedec-flash" };
621 	int off;
622 	int len;
623 	struct fdt_property *prop;
624 	u32 *reg, *reg2;
625 	int i;
626 
627 	for (i = 0; i < 2; i++) {
628 		off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
629 		while (off != -FDT_ERR_NOTFOUND) {
630 			int idx;
631 
632 			/*
633 			 * Found one compatible node, so fixup the size
634 			 * int its reg properties
635 			 */
636 			prop = fdt_get_property_w(blob, off, "reg", &len);
637 			if (prop) {
638 				int tuple_size = 3 * sizeof(reg);
639 
640 				/*
641 				 * There might be multiple reg-tuples,
642 				 * so loop through them all
643 				 */
644 				reg = reg2 = (u32 *)&prop->data[0];
645 				for (idx = 0; idx < (len / tuple_size); idx++) {
646 					/*
647 					 * Update size in reg property
648 					 */
649 					reg[2] = flash_get_bank_size(reg[0],
650 								     idx);
651 
652 					/*
653 					 * Point to next reg tuple
654 					 */
655 					reg += 3;
656 				}
657 
658 				fdt_setprop(blob, off, "reg", reg2, len);
659 			}
660 
661 			/* Move to next compatible node */
662 			off = fdt_node_offset_by_compatible(blob, off,
663 							    compat[i]);
664 		}
665 	}
666 
667 	return 0;
668 }
669 #endif
670 
671 int fdt_increase_size(void *fdt, int add_len)
672 {
673 	int newlen;
674 
675 	newlen = fdt_totalsize(fdt) + add_len;
676 
677 	/* Open in place with a new len */
678 	return fdt_open_into(fdt, fdt, newlen);
679 }
680 
681 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
682 #include <jffs2/load_kernel.h>
683 #include <mtd_node.h>
684 
685 struct reg_cell {
686 	unsigned int r0;
687 	unsigned int r1;
688 };
689 
690 int fdt_del_subnodes(const void *blob, int parent_offset)
691 {
692 	int off, ndepth;
693 	int ret;
694 
695 	for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
696 	     (off >= 0) && (ndepth > 0);
697 	     off = fdt_next_node(blob, off, &ndepth)) {
698 		if (ndepth == 1) {
699 			debug("delete %s: offset: %x\n",
700 				fdt_get_name(blob, off, 0), off);
701 			ret = fdt_del_node((void *)blob, off);
702 			if (ret < 0) {
703 				printf("Can't delete node: %s\n",
704 					fdt_strerror(ret));
705 				return ret;
706 			} else {
707 				ndepth = 0;
708 				off = parent_offset;
709 			}
710 		}
711 	}
712 	return 0;
713 }
714 
715 int fdt_del_partitions(void *blob, int parent_offset)
716 {
717 	const void *prop;
718 	int ndepth = 0;
719 	int off;
720 	int ret;
721 
722 	off = fdt_next_node(blob, parent_offset, &ndepth);
723 	if (off > 0 && ndepth == 1) {
724 		prop = fdt_getprop(blob, off, "label", NULL);
725 		if (prop == NULL) {
726 			/*
727 			 * Could not find label property, nand {}; node?
728 			 * Check subnode, delete partitions there if any.
729 			 */
730 			return fdt_del_partitions(blob, off);
731 		} else {
732 			ret = fdt_del_subnodes(blob, parent_offset);
733 			if (ret < 0) {
734 				printf("Can't remove subnodes: %s\n",
735 					fdt_strerror(ret));
736 				return ret;
737 			}
738 		}
739 	}
740 	return 0;
741 }
742 
743 int fdt_node_set_part_info(void *blob, int parent_offset,
744 			   struct mtd_device *dev)
745 {
746 	struct list_head *pentry;
747 	struct part_info *part;
748 	struct reg_cell cell;
749 	int off, ndepth = 0;
750 	int part_num, ret;
751 	char buf[64];
752 
753 	ret = fdt_del_partitions(blob, parent_offset);
754 	if (ret < 0)
755 		return ret;
756 
757 	/*
758 	 * Check if it is nand {}; subnode, adjust
759 	 * the offset in this case
760 	 */
761 	off = fdt_next_node(blob, parent_offset, &ndepth);
762 	if (off > 0 && ndepth == 1)
763 		parent_offset = off;
764 
765 	part_num = 0;
766 	list_for_each_prev(pentry, &dev->parts) {
767 		int newoff;
768 
769 		part = list_entry(pentry, struct part_info, link);
770 
771 		debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
772 			part_num, part->name, part->size,
773 			part->offset, part->mask_flags);
774 
775 		sprintf(buf, "partition@%llx", part->offset);
776 add_sub:
777 		ret = fdt_add_subnode(blob, parent_offset, buf);
778 		if (ret == -FDT_ERR_NOSPACE) {
779 			ret = fdt_increase_size(blob, 512);
780 			if (!ret)
781 				goto add_sub;
782 			else
783 				goto err_size;
784 		} else if (ret < 0) {
785 			printf("Can't add partition node: %s\n",
786 				fdt_strerror(ret));
787 			return ret;
788 		}
789 		newoff = ret;
790 
791 		/* Check MTD_WRITEABLE_CMD flag */
792 		if (part->mask_flags & 1) {
793 add_ro:
794 			ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
795 			if (ret == -FDT_ERR_NOSPACE) {
796 				ret = fdt_increase_size(blob, 512);
797 				if (!ret)
798 					goto add_ro;
799 				else
800 					goto err_size;
801 			} else if (ret < 0)
802 				goto err_prop;
803 		}
804 
805 		cell.r0 = cpu_to_fdt32(part->offset);
806 		cell.r1 = cpu_to_fdt32(part->size);
807 add_reg:
808 		ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
809 		if (ret == -FDT_ERR_NOSPACE) {
810 			ret = fdt_increase_size(blob, 512);
811 			if (!ret)
812 				goto add_reg;
813 			else
814 				goto err_size;
815 		} else if (ret < 0)
816 			goto err_prop;
817 
818 add_label:
819 		ret = fdt_setprop_string(blob, newoff, "label", part->name);
820 		if (ret == -FDT_ERR_NOSPACE) {
821 			ret = fdt_increase_size(blob, 512);
822 			if (!ret)
823 				goto add_label;
824 			else
825 				goto err_size;
826 		} else if (ret < 0)
827 			goto err_prop;
828 
829 		part_num++;
830 	}
831 	return 0;
832 err_size:
833 	printf("Can't increase blob size: %s\n", fdt_strerror(ret));
834 	return ret;
835 err_prop:
836 	printf("Can't add property: %s\n", fdt_strerror(ret));
837 	return ret;
838 }
839 
840 /*
841  * Update partitions in nor/nand nodes using info from
842  * mtdparts environment variable. The nodes to update are
843  * specified by node_info structure which contains mtd device
844  * type and compatible string: E. g. the board code in
845  * ft_board_setup() could use:
846  *
847  *	struct node_info nodes[] = {
848  *		{ "fsl,mpc5121-nfc",    MTD_DEV_TYPE_NAND, },
849  *		{ "cfi-flash",          MTD_DEV_TYPE_NOR,  },
850  *	};
851  *
852  *	fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
853  */
854 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
855 {
856 	struct node_info *ni = node_info;
857 	struct mtd_device *dev;
858 	char *parts;
859 	int i, idx;
860 	int noff;
861 
862 	parts = getenv("mtdparts");
863 	if (!parts)
864 		return;
865 
866 	if (mtdparts_init() != 0)
867 		return;
868 
869 	for (i = 0; i < node_info_size; i++) {
870 		idx = 0;
871 		noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
872 		while (noff != -FDT_ERR_NOTFOUND) {
873 			debug("%s: %s, mtd dev type %d\n",
874 				fdt_get_name(blob, noff, 0),
875 				ni[i].compat, ni[i].type);
876 			dev = device_find(ni[i].type, idx++);
877 			if (dev) {
878 				if (fdt_node_set_part_info(blob, noff, dev))
879 					return; /* return on error */
880 			}
881 
882 			/* Jump to next flash node */
883 			noff = fdt_node_offset_by_compatible(blob, noff,
884 							     ni[i].compat);
885 		}
886 	}
887 }
888 #endif
889 
890 void fdt_del_node_and_alias(void *blob, const char *alias)
891 {
892 	int off = fdt_path_offset(blob, alias);
893 
894 	if (off < 0)
895 		return;
896 
897 	fdt_del_node(blob, off);
898 
899 	off = fdt_path_offset(blob, "/aliases");
900 	fdt_delprop(blob, off, alias);
901 }
902 
903 /* Helper to read a big number; size is in cells (not bytes) */
904 static inline u64 of_read_number(const fdt32_t *cell, int size)
905 {
906 	u64 r = 0;
907 	while (size--)
908 		r = (r << 32) | fdt32_to_cpu(*(cell++));
909 	return r;
910 }
911 
912 #define PRu64	"%llx"
913 
914 /* Max address size we deal with */
915 #define OF_MAX_ADDR_CELLS	4
916 #define OF_BAD_ADDR	((u64)-1)
917 #define OF_CHECK_COUNTS(na, ns)	((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
918 			(ns) > 0)
919 
920 /* Debug utility */
921 #ifdef DEBUG
922 static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
923 {
924 	printf("%s", s);
925 	while(na--)
926 		printf(" %08x", *(addr++));
927 	printf("\n");
928 }
929 #else
930 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
931 #endif
932 
933 /* Callbacks for bus specific translators */
934 struct of_bus {
935 	const char	*name;
936 	const char	*addresses;
937 	void		(*count_cells)(void *blob, int parentoffset,
938 				int *addrc, int *sizec);
939 	u64		(*map)(fdt32_t *addr, const fdt32_t *range,
940 				int na, int ns, int pna);
941 	int		(*translate)(fdt32_t *addr, u64 offset, int na);
942 };
943 
944 /* Default translator (generic bus) */
945 static void of_bus_default_count_cells(void *blob, int parentoffset,
946 					int *addrc, int *sizec)
947 {
948 	const fdt32_t *prop;
949 
950 	if (addrc) {
951 		prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
952 		if (prop)
953 			*addrc = be32_to_cpup(prop);
954 		else
955 			*addrc = 2;
956 	}
957 
958 	if (sizec) {
959 		prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
960 		if (prop)
961 			*sizec = be32_to_cpup(prop);
962 		else
963 			*sizec = 1;
964 	}
965 }
966 
967 static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
968 		int na, int ns, int pna)
969 {
970 	u64 cp, s, da;
971 
972 	cp = of_read_number(range, na);
973 	s  = of_read_number(range + na + pna, ns);
974 	da = of_read_number(addr, na);
975 
976 	debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
977 	    cp, s, da);
978 
979 	if (da < cp || da >= (cp + s))
980 		return OF_BAD_ADDR;
981 	return da - cp;
982 }
983 
984 static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
985 {
986 	u64 a = of_read_number(addr, na);
987 	memset(addr, 0, na * 4);
988 	a += offset;
989 	if (na > 1)
990 		addr[na - 2] = cpu_to_fdt32(a >> 32);
991 	addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
992 
993 	return 0;
994 }
995 
996 /* Array of bus specific translators */
997 static struct of_bus of_busses[] = {
998 	/* Default */
999 	{
1000 		.name = "default",
1001 		.addresses = "reg",
1002 		.count_cells = of_bus_default_count_cells,
1003 		.map = of_bus_default_map,
1004 		.translate = of_bus_default_translate,
1005 	},
1006 };
1007 
1008 static int of_translate_one(void * blob, int parent, struct of_bus *bus,
1009 			    struct of_bus *pbus, fdt32_t *addr,
1010 			    int na, int ns, int pna, const char *rprop)
1011 {
1012 	const fdt32_t *ranges;
1013 	int rlen;
1014 	int rone;
1015 	u64 offset = OF_BAD_ADDR;
1016 
1017 	/* Normally, an absence of a "ranges" property means we are
1018 	 * crossing a non-translatable boundary, and thus the addresses
1019 	 * below the current not cannot be converted to CPU physical ones.
1020 	 * Unfortunately, while this is very clear in the spec, it's not
1021 	 * what Apple understood, and they do have things like /uni-n or
1022 	 * /ht nodes with no "ranges" property and a lot of perfectly
1023 	 * useable mapped devices below them. Thus we treat the absence of
1024 	 * "ranges" as equivalent to an empty "ranges" property which means
1025 	 * a 1:1 translation at that level. It's up to the caller not to try
1026 	 * to translate addresses that aren't supposed to be translated in
1027 	 * the first place. --BenH.
1028 	 */
1029 	ranges = fdt_getprop(blob, parent, rprop, &rlen);
1030 	if (ranges == NULL || rlen == 0) {
1031 		offset = of_read_number(addr, na);
1032 		memset(addr, 0, pna * 4);
1033 		debug("OF: no ranges, 1:1 translation\n");
1034 		goto finish;
1035 	}
1036 
1037 	debug("OF: walking ranges...\n");
1038 
1039 	/* Now walk through the ranges */
1040 	rlen /= 4;
1041 	rone = na + pna + ns;
1042 	for (; rlen >= rone; rlen -= rone, ranges += rone) {
1043 		offset = bus->map(addr, ranges, na, ns, pna);
1044 		if (offset != OF_BAD_ADDR)
1045 			break;
1046 	}
1047 	if (offset == OF_BAD_ADDR) {
1048 		debug("OF: not found !\n");
1049 		return 1;
1050 	}
1051 	memcpy(addr, ranges + na, 4 * pna);
1052 
1053  finish:
1054 	of_dump_addr("OF: parent translation for:", addr, pna);
1055 	debug("OF: with offset: "PRu64"\n", offset);
1056 
1057 	/* Translate it into parent bus space */
1058 	return pbus->translate(addr, offset, pna);
1059 }
1060 
1061 /*
1062  * Translate an address from the device-tree into a CPU physical address,
1063  * this walks up the tree and applies the various bus mappings on the
1064  * way.
1065  *
1066  * Note: We consider that crossing any level with #size-cells == 0 to mean
1067  * that translation is impossible (that is we are not dealing with a value
1068  * that can be mapped to a cpu physical address). This is not really specified
1069  * that way, but this is traditionally the way IBM at least do things
1070  */
1071 static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
1072 				  const char *rprop)
1073 {
1074 	int parent;
1075 	struct of_bus *bus, *pbus;
1076 	fdt32_t addr[OF_MAX_ADDR_CELLS];
1077 	int na, ns, pna, pns;
1078 	u64 result = OF_BAD_ADDR;
1079 
1080 	debug("OF: ** translation for device %s **\n",
1081 		fdt_get_name(blob, node_offset, NULL));
1082 
1083 	/* Get parent & match bus type */
1084 	parent = fdt_parent_offset(blob, node_offset);
1085 	if (parent < 0)
1086 		goto bail;
1087 	bus = &of_busses[0];
1088 
1089 	/* Cound address cells & copy address locally */
1090 	bus->count_cells(blob, parent, &na, &ns);
1091 	if (!OF_CHECK_COUNTS(na, ns)) {
1092 		printf("%s: Bad cell count for %s\n", __FUNCTION__,
1093 		       fdt_get_name(blob, node_offset, NULL));
1094 		goto bail;
1095 	}
1096 	memcpy(addr, in_addr, na * 4);
1097 
1098 	debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1099 	    bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1100 	of_dump_addr("OF: translating address:", addr, na);
1101 
1102 	/* Translate */
1103 	for (;;) {
1104 		/* Switch to parent bus */
1105 		node_offset = parent;
1106 		parent = fdt_parent_offset(blob, node_offset);
1107 
1108 		/* If root, we have finished */
1109 		if (parent < 0) {
1110 			debug("OF: reached root node\n");
1111 			result = of_read_number(addr, na);
1112 			break;
1113 		}
1114 
1115 		/* Get new parent bus and counts */
1116 		pbus = &of_busses[0];
1117 		pbus->count_cells(blob, parent, &pna, &pns);
1118 		if (!OF_CHECK_COUNTS(pna, pns)) {
1119 			printf("%s: Bad cell count for %s\n", __FUNCTION__,
1120 				fdt_get_name(blob, node_offset, NULL));
1121 			break;
1122 		}
1123 
1124 		debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1125 		    pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1126 
1127 		/* Apply bus translation */
1128 		if (of_translate_one(blob, node_offset, bus, pbus,
1129 					addr, na, ns, pna, rprop))
1130 			break;
1131 
1132 		/* Complete the move up one level */
1133 		na = pna;
1134 		ns = pns;
1135 		bus = pbus;
1136 
1137 		of_dump_addr("OF: one level translation:", addr, na);
1138 	}
1139  bail:
1140 
1141 	return result;
1142 }
1143 
1144 u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
1145 {
1146 	return __of_translate_address(blob, node_offset, in_addr, "ranges");
1147 }
1148 
1149 /**
1150  * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1151  * who's reg property matches a physical cpu address
1152  *
1153  * @blob: ptr to device tree
1154  * @compat: compatiable string to match
1155  * @compat_off: property name
1156  *
1157  */
1158 int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1159 					phys_addr_t compat_off)
1160 {
1161 	int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
1162 	while (off != -FDT_ERR_NOTFOUND) {
1163 		const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
1164 		if (reg) {
1165 			if (compat_off == fdt_translate_address(blob, off, reg))
1166 				return off;
1167 		}
1168 		off = fdt_node_offset_by_compatible(blob, off, compat);
1169 	}
1170 
1171 	return -FDT_ERR_NOTFOUND;
1172 }
1173 
1174 /**
1175  * fdt_alloc_phandle: Return next free phandle value
1176  *
1177  * @blob: ptr to device tree
1178  */
1179 int fdt_alloc_phandle(void *blob)
1180 {
1181 	int offset, phandle = 0;
1182 
1183 	for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1184 	     offset = fdt_next_node(blob, offset, NULL)) {
1185 		phandle = max(phandle, fdt_get_phandle(blob, offset));
1186 	}
1187 
1188 	return phandle + 1;
1189 }
1190 
1191 /*
1192  * fdt_set_phandle: Create a phandle property for the given node
1193  *
1194  * @fdt: ptr to device tree
1195  * @nodeoffset: node to update
1196  * @phandle: phandle value to set (must be unique)
1197  */
1198 int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
1199 {
1200 	int ret;
1201 
1202 #ifdef DEBUG
1203 	int off = fdt_node_offset_by_phandle(fdt, phandle);
1204 
1205 	if ((off >= 0) && (off != nodeoffset)) {
1206 		char buf[64];
1207 
1208 		fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1209 		printf("Trying to update node %s with phandle %u ",
1210 		       buf, phandle);
1211 
1212 		fdt_get_path(fdt, off, buf, sizeof(buf));
1213 		printf("that already exists in node %s.\n", buf);
1214 		return -FDT_ERR_BADPHANDLE;
1215 	}
1216 #endif
1217 
1218 	ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1219 	if (ret < 0)
1220 		return ret;
1221 
1222 	/*
1223 	 * For now, also set the deprecated "linux,phandle" property, so that we
1224 	 * don't break older kernels.
1225 	 */
1226 	ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1227 
1228 	return ret;
1229 }
1230 
1231 /*
1232  * fdt_create_phandle: Create a phandle property for the given node
1233  *
1234  * @fdt: ptr to device tree
1235  * @nodeoffset: node to update
1236  */
1237 unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
1238 {
1239 	/* see if there is a phandle already */
1240 	int phandle = fdt_get_phandle(fdt, nodeoffset);
1241 
1242 	/* if we got 0, means no phandle so create one */
1243 	if (phandle == 0) {
1244 		int ret;
1245 
1246 		phandle = fdt_alloc_phandle(fdt);
1247 		ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1248 		if (ret < 0) {
1249 			printf("Can't set phandle %u: %s\n", phandle,
1250 			       fdt_strerror(ret));
1251 			return 0;
1252 		}
1253 	}
1254 
1255 	return phandle;
1256 }
1257 
1258 /*
1259  * fdt_set_node_status: Set status for the given node
1260  *
1261  * @fdt: ptr to device tree
1262  * @nodeoffset: node to update
1263  * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1264  *	    FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1265  * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1266  */
1267 int fdt_set_node_status(void *fdt, int nodeoffset,
1268 			enum fdt_status status, unsigned int error_code)
1269 {
1270 	char buf[16];
1271 	int ret = 0;
1272 
1273 	if (nodeoffset < 0)
1274 		return nodeoffset;
1275 
1276 	switch (status) {
1277 	case FDT_STATUS_OKAY:
1278 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1279 		break;
1280 	case FDT_STATUS_DISABLED:
1281 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1282 		break;
1283 	case FDT_STATUS_FAIL:
1284 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1285 		break;
1286 	case FDT_STATUS_FAIL_ERROR_CODE:
1287 		sprintf(buf, "fail-%d", error_code);
1288 		ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1289 		break;
1290 	default:
1291 		printf("Invalid fdt status: %x\n", status);
1292 		ret = -1;
1293 		break;
1294 	}
1295 
1296 	return ret;
1297 }
1298 
1299 /*
1300  * fdt_set_status_by_alias: Set status for the given node given an alias
1301  *
1302  * @fdt: ptr to device tree
1303  * @alias: alias of node to update
1304  * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1305  *	    FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1306  * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1307  */
1308 int fdt_set_status_by_alias(void *fdt, const char* alias,
1309 			    enum fdt_status status, unsigned int error_code)
1310 {
1311 	int offset = fdt_path_offset(fdt, alias);
1312 
1313 	return fdt_set_node_status(fdt, offset, status, error_code);
1314 }
1315 
1316 #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
1317 int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1318 {
1319 	int noff;
1320 	int ret;
1321 
1322 	noff = fdt_node_offset_by_compatible(blob, -1, compat);
1323 	if (noff != -FDT_ERR_NOTFOUND) {
1324 		debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1325 add_edid:
1326 		ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1327 		if (ret == -FDT_ERR_NOSPACE) {
1328 			ret = fdt_increase_size(blob, 512);
1329 			if (!ret)
1330 				goto add_edid;
1331 			else
1332 				goto err_size;
1333 		} else if (ret < 0) {
1334 			printf("Can't add property: %s\n", fdt_strerror(ret));
1335 			return ret;
1336 		}
1337 	}
1338 	return 0;
1339 err_size:
1340 	printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1341 	return ret;
1342 }
1343 #endif
1344 
1345 /*
1346  * Verify the physical address of device tree node for a given alias
1347  *
1348  * This function locates the device tree node of a given alias, and then
1349  * verifies that the physical address of that device matches the given
1350  * parameter.  It displays a message if there is a mismatch.
1351  *
1352  * Returns 1 on success, 0 on failure
1353  */
1354 int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1355 {
1356 	const char *path;
1357 	const fdt32_t *reg;
1358 	int node, len;
1359 	u64 dt_addr;
1360 
1361 	path = fdt_getprop(fdt, anode, alias, NULL);
1362 	if (!path) {
1363 		/* If there's no such alias, then it's not a failure */
1364 		return 1;
1365 	}
1366 
1367 	node = fdt_path_offset(fdt, path);
1368 	if (node < 0) {
1369 		printf("Warning: device tree alias '%s' points to invalid "
1370 		       "node %s.\n", alias, path);
1371 		return 0;
1372 	}
1373 
1374 	reg = fdt_getprop(fdt, node, "reg", &len);
1375 	if (!reg) {
1376 		printf("Warning: device tree node '%s' has no address.\n",
1377 		       path);
1378 		return 0;
1379 	}
1380 
1381 	dt_addr = fdt_translate_address(fdt, node, reg);
1382 	if (addr != dt_addr) {
1383 		printf("Warning: U-Boot configured device %s at address %llx,\n"
1384 		       " but the device tree has it address %llx.\n",
1385 		       alias, addr, dt_addr);
1386 		return 0;
1387 	}
1388 
1389 	return 1;
1390 }
1391 
1392 /*
1393  * Returns the base address of an SOC or PCI node
1394  */
1395 u64 fdt_get_base_address(void *fdt, int node)
1396 {
1397 	int size;
1398 	u32 naddr;
1399 	const fdt32_t *prop;
1400 
1401 	prop = fdt_getprop(fdt, node, "#address-cells", &size);
1402 	if (prop && size == 4)
1403 		naddr = be32_to_cpup(prop);
1404 	else
1405 		naddr = 2;
1406 
1407 	prop = fdt_getprop(fdt, node, "ranges", &size);
1408 
1409 	return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1410 }
1411