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