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