xref: /openbmc/u-boot/common/fdt_support.c (revision 0c331ebc)
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  * fdt_getprop_u32_default_node - Return a node's property or a default
21  *
22  * @fdt: ptr to device tree
23  * @off: offset of node
24  * @cell: cell offset in property
25  * @prop: property name
26  * @dflt: default value if the property isn't found
27  *
28  * Convenience function to return a node's property or a default value if
29  * the property doesn't exist.
30  */
31 u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
32 				const char *prop, const u32 dflt)
33 {
34 	const fdt32_t *val;
35 	int len;
36 
37 	val = fdt_getprop(fdt, off, prop, &len);
38 
39 	/* Check if property exists */
40 	if (!val)
41 		return dflt;
42 
43 	/* Check if property is long enough */
44 	if (len < ((cell + 1) * sizeof(uint32_t)))
45 		return dflt;
46 
47 	return fdt32_to_cpu(*val);
48 }
49 
50 /**
51  * fdt_getprop_u32_default - Find a node and return it's property or a default
52  *
53  * @fdt: ptr to device tree
54  * @path: path of node
55  * @prop: property name
56  * @dflt: default value if the property isn't found
57  *
58  * Convenience function to find a node and return it's property or a
59  * default value if it doesn't exist.
60  */
61 u32 fdt_getprop_u32_default(const void *fdt, const char *path,
62 				const char *prop, const u32 dflt)
63 {
64 	int off;
65 
66 	off = fdt_path_offset(fdt, path);
67 	if (off < 0)
68 		return dflt;
69 
70 	return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
71 }
72 
73 /**
74  * fdt_find_and_setprop: Find a node and set it's property
75  *
76  * @fdt: ptr to device tree
77  * @node: path of node
78  * @prop: property name
79  * @val: ptr to new value
80  * @len: length of new property value
81  * @create: flag to create the property if it doesn't exist
82  *
83  * Convenience function to directly set a property given the path to the node.
84  */
85 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
86 			 const void *val, int len, int create)
87 {
88 	int nodeoff = fdt_path_offset(fdt, node);
89 
90 	if (nodeoff < 0)
91 		return nodeoff;
92 
93 	if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
94 		return 0; /* create flag not set; so exit quietly */
95 
96 	return fdt_setprop(fdt, nodeoff, prop, val, len);
97 }
98 
99 /**
100  * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
101  *
102  * @fdt: pointer to the device tree blob
103  * @parentoffset: structure block offset of a node
104  * @name: name of the subnode to locate
105  *
106  * fdt_subnode_offset() finds a subnode of the node with a given name.
107  * If the subnode does not exist, it will be created.
108  */
109 int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
110 {
111 	int offset;
112 
113 	offset = fdt_subnode_offset(fdt, parentoffset, name);
114 
115 	if (offset == -FDT_ERR_NOTFOUND)
116 		offset = fdt_add_subnode(fdt, parentoffset, name);
117 
118 	if (offset < 0)
119 		printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
120 
121 	return offset;
122 }
123 
124 /* rename to CONFIG_OF_STDOUT_PATH ? */
125 #if defined(OF_STDOUT_PATH)
126 static int fdt_fixup_stdout(void *fdt, int chosenoff)
127 {
128 	return fdt_setprop(fdt, chosenoff, "linux,stdout-path",
129 			      OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1);
130 }
131 #elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
132 static void fdt_fill_multisername(char *sername, size_t maxlen)
133 {
134 	const char *outname = stdio_devices[stdout]->name;
135 
136 	if (strcmp(outname, "serial") > 0)
137 		strncpy(sername, outname, maxlen);
138 
139 	/* eserial? */
140 	if (strcmp(outname + 1, "serial") > 0)
141 		strncpy(sername, outname + 1, maxlen);
142 }
143 
144 static int fdt_fixup_stdout(void *fdt, int chosenoff)
145 {
146 	int err;
147 	int aliasoff;
148 	char sername[9] = { 0 };
149 	const void *path;
150 	int len;
151 	char tmp[256]; /* long enough */
152 
153 	fdt_fill_multisername(sername, sizeof(sername) - 1);
154 	if (!sername[0])
155 		sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
156 
157 	aliasoff = fdt_path_offset(fdt, "/aliases");
158 	if (aliasoff < 0) {
159 		err = aliasoff;
160 		goto error;
161 	}
162 
163 	path = fdt_getprop(fdt, aliasoff, sername, &len);
164 	if (!path) {
165 		err = len;
166 		goto error;
167 	}
168 
169 	/* fdt_setprop may break "path" so we copy it to tmp buffer */
170 	memcpy(tmp, path, len);
171 
172 	err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
173 error:
174 	if (err < 0)
175 		printf("WARNING: could not set linux,stdout-path %s.\n",
176 		       fdt_strerror(err));
177 
178 	return err;
179 }
180 #else
181 static int fdt_fixup_stdout(void *fdt, int chosenoff)
182 {
183 	return 0;
184 }
185 #endif
186 
187 static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
188 				  uint64_t val, int is_u64)
189 {
190 	if (is_u64)
191 		return fdt_setprop_u64(fdt, nodeoffset, name, val);
192 	else
193 		return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
194 }
195 
196 
197 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
198 {
199 	int   nodeoffset;
200 	int   err, j, total;
201 	int is_u64;
202 	uint64_t addr, size;
203 
204 	/* just return if the size of initrd is zero */
205 	if (initrd_start == initrd_end)
206 		return 0;
207 
208 	/* find or create "/chosen" node. */
209 	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
210 	if (nodeoffset < 0)
211 		return nodeoffset;
212 
213 	total = fdt_num_mem_rsv(fdt);
214 
215 	/*
216 	 * Look for an existing entry and update it.  If we don't find
217 	 * the entry, we will j be the next available slot.
218 	 */
219 	for (j = 0; j < total; j++) {
220 		err = fdt_get_mem_rsv(fdt, j, &addr, &size);
221 		if (addr == initrd_start) {
222 			fdt_del_mem_rsv(fdt, j);
223 			break;
224 		}
225 	}
226 
227 	err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
228 	if (err < 0) {
229 		printf("fdt_initrd: %s\n", fdt_strerror(err));
230 		return err;
231 	}
232 
233 	is_u64 = (fdt_address_cells(fdt, 0) == 2);
234 
235 	err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
236 			      (uint64_t)initrd_start, is_u64);
237 
238 	if (err < 0) {
239 		printf("WARNING: could not set linux,initrd-start %s.\n",
240 		       fdt_strerror(err));
241 		return err;
242 	}
243 
244 	err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
245 			      (uint64_t)initrd_end, is_u64);
246 
247 	if (err < 0) {
248 		printf("WARNING: could not set linux,initrd-end %s.\n",
249 		       fdt_strerror(err));
250 
251 		return err;
252 	}
253 
254 	return 0;
255 }
256 
257 int fdt_chosen(void *fdt)
258 {
259 	int   nodeoffset;
260 	int   err;
261 	char  *str;		/* used to set string properties */
262 
263 	err = fdt_check_header(fdt);
264 	if (err < 0) {
265 		printf("fdt_chosen: %s\n", fdt_strerror(err));
266 		return err;
267 	}
268 
269 	/* find or create "/chosen" node. */
270 	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
271 	if (nodeoffset < 0)
272 		return nodeoffset;
273 
274 	str = getenv("bootargs");
275 	if (str) {
276 		err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
277 				  strlen(str) + 1);
278 		if (err < 0) {
279 			printf("WARNING: could not set bootargs %s.\n",
280 			       fdt_strerror(err));
281 			return err;
282 		}
283 	}
284 
285 	return fdt_fixup_stdout(fdt, nodeoffset);
286 }
287 
288 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
289 		      const void *val, int len, int create)
290 {
291 #if defined(DEBUG)
292 	int i;
293 	debug("Updating property '%s/%s' = ", path, prop);
294 	for (i = 0; i < len; i++)
295 		debug(" %.2x", *(u8*)(val+i));
296 	debug("\n");
297 #endif
298 	int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
299 	if (rc)
300 		printf("Unable to update property %s:%s, err=%s\n",
301 			path, prop, fdt_strerror(rc));
302 }
303 
304 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
305 			  u32 val, int create)
306 {
307 	fdt32_t tmp = cpu_to_fdt32(val);
308 	do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
309 }
310 
311 void do_fixup_by_prop(void *fdt,
312 		      const char *pname, const void *pval, int plen,
313 		      const char *prop, const void *val, int len,
314 		      int create)
315 {
316 	int off;
317 #if defined(DEBUG)
318 	int i;
319 	debug("Updating property '%s' = ", prop);
320 	for (i = 0; i < len; i++)
321 		debug(" %.2x", *(u8*)(val+i));
322 	debug("\n");
323 #endif
324 	off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
325 	while (off != -FDT_ERR_NOTFOUND) {
326 		if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
327 			fdt_setprop(fdt, off, prop, val, len);
328 		off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
329 	}
330 }
331 
332 void do_fixup_by_prop_u32(void *fdt,
333 			  const char *pname, const void *pval, int plen,
334 			  const char *prop, u32 val, int create)
335 {
336 	fdt32_t tmp = cpu_to_fdt32(val);
337 	do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
338 }
339 
340 void do_fixup_by_compat(void *fdt, const char *compat,
341 			const char *prop, const void *val, int len, int create)
342 {
343 	int off = -1;
344 #if defined(DEBUG)
345 	int i;
346 	debug("Updating property '%s' = ", prop);
347 	for (i = 0; i < len; i++)
348 		debug(" %.2x", *(u8*)(val+i));
349 	debug("\n");
350 #endif
351 	off = fdt_node_offset_by_compatible(fdt, -1, compat);
352 	while (off != -FDT_ERR_NOTFOUND) {
353 		if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
354 			fdt_setprop(fdt, off, prop, val, len);
355 		off = fdt_node_offset_by_compatible(fdt, off, compat);
356 	}
357 }
358 
359 void do_fixup_by_compat_u32(void *fdt, const char *compat,
360 			    const char *prop, u32 val, int create)
361 {
362 	fdt32_t tmp = cpu_to_fdt32(val);
363 	do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
364 }
365 
366 /*
367  * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
368  */
369 static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
370 			int n)
371 {
372 	int i;
373 	int address_cells = fdt_address_cells(fdt, 0);
374 	int size_cells = fdt_size_cells(fdt, 0);
375 	char *p = buf;
376 
377 	for (i = 0; i < n; i++) {
378 		if (address_cells == 2)
379 			*(fdt64_t *)p = cpu_to_fdt64(address[i]);
380 		else
381 			*(fdt32_t *)p = cpu_to_fdt32(address[i]);
382 		p += 4 * address_cells;
383 
384 		if (size_cells == 2)
385 			*(fdt64_t *)p = cpu_to_fdt64(size[i]);
386 		else
387 			*(fdt32_t *)p = cpu_to_fdt32(size[i]);
388 		p += 4 * size_cells;
389 	}
390 
391 	return p - (char *)buf;
392 }
393 
394 #ifdef CONFIG_NR_DRAM_BANKS
395 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
396 #else
397 #define MEMORY_BANKS_MAX 4
398 #endif
399 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
400 {
401 	int err, nodeoffset;
402 	int len;
403 	u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
404 
405 	if (banks > MEMORY_BANKS_MAX) {
406 		printf("%s: num banks %d exceeds hardcoded limit %d."
407 		       " Recompile with higher MEMORY_BANKS_MAX?\n",
408 		       __FUNCTION__, banks, MEMORY_BANKS_MAX);
409 		return -1;
410 	}
411 
412 	err = fdt_check_header(blob);
413 	if (err < 0) {
414 		printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
415 		return err;
416 	}
417 
418 	/* find or create "/memory" node. */
419 	nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
420 	if (nodeoffset < 0)
421 			return nodeoffset;
422 
423 	err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
424 			sizeof("memory"));
425 	if (err < 0) {
426 		printf("WARNING: could not set %s %s.\n", "device_type",
427 				fdt_strerror(err));
428 		return err;
429 	}
430 
431 	len = fdt_pack_reg(blob, tmp, start, size, banks);
432 
433 	err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
434 	if (err < 0) {
435 		printf("WARNING: could not set %s %s.\n",
436 				"reg", fdt_strerror(err));
437 		return err;
438 	}
439 	return 0;
440 }
441 
442 int fdt_fixup_memory(void *blob, u64 start, u64 size)
443 {
444 	return fdt_fixup_memory_banks(blob, &start, &size, 1);
445 }
446 
447 void fdt_fixup_ethernet(void *fdt)
448 {
449 	int node, i, j;
450 	char enet[16], *tmp, *end;
451 	char mac[16];
452 	const char *path;
453 	unsigned char mac_addr[6];
454 
455 	node = fdt_path_offset(fdt, "/aliases");
456 	if (node < 0)
457 		return;
458 
459 	if (!getenv("ethaddr")) {
460 		if (getenv("usbethaddr")) {
461 			strcpy(mac, "usbethaddr");
462 		} else {
463 			debug("No ethernet MAC Address defined\n");
464 			return;
465 		}
466 	} else {
467 		strcpy(mac, "ethaddr");
468 	}
469 
470 	i = 0;
471 	while ((tmp = getenv(mac)) != NULL) {
472 		sprintf(enet, "ethernet%d", i);
473 		path = fdt_getprop(fdt, node, enet, NULL);
474 		if (!path) {
475 			debug("No alias for %s\n", enet);
476 			sprintf(mac, "eth%daddr", ++i);
477 			continue;
478 		}
479 
480 		for (j = 0; j < 6; j++) {
481 			mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
482 			if (tmp)
483 				tmp = (*end) ? end+1 : end;
484 		}
485 
486 		do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
487 		do_fixup_by_path(fdt, path, "local-mac-address",
488 				&mac_addr, 6, 1);
489 
490 		sprintf(mac, "eth%daddr", ++i);
491 	}
492 }
493 
494 /* Resize the fdt to its actual size + a bit of padding */
495 int fdt_shrink_to_minimum(void *blob)
496 {
497 	int i;
498 	uint64_t addr, size;
499 	int total, ret;
500 	uint actualsize;
501 
502 	if (!blob)
503 		return 0;
504 
505 	total = fdt_num_mem_rsv(blob);
506 	for (i = 0; i < total; i++) {
507 		fdt_get_mem_rsv(blob, i, &addr, &size);
508 		if (addr == (uintptr_t)blob) {
509 			fdt_del_mem_rsv(blob, i);
510 			break;
511 		}
512 	}
513 
514 	/*
515 	 * Calculate the actual size of the fdt
516 	 * plus the size needed for 5 fdt_add_mem_rsv, one
517 	 * for the fdt itself and 4 for a possible initrd
518 	 * ((initrd-start + initrd-end) * 2 (name & value))
519 	 */
520 	actualsize = fdt_off_dt_strings(blob) +
521 		fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
522 
523 	/* Make it so the fdt ends on a page boundary */
524 	actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
525 	actualsize = actualsize - ((uintptr_t)blob & 0xfff);
526 
527 	/* Change the fdt header to reflect the correct size */
528 	fdt_set_totalsize(blob, actualsize);
529 
530 	/* Add the new reservation */
531 	ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
532 	if (ret < 0)
533 		return ret;
534 
535 	return actualsize;
536 }
537 
538 #ifdef CONFIG_PCI
539 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
540 
541 #define FDT_PCI_PREFETCH	(0x40000000)
542 #define FDT_PCI_MEM32		(0x02000000)
543 #define FDT_PCI_IO		(0x01000000)
544 #define FDT_PCI_MEM64		(0x03000000)
545 
546 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
547 
548 	int addrcell, sizecell, len, r;
549 	u32 *dma_range;
550 	/* sized based on pci addr cells, size-cells, & address-cells */
551 	u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
552 
553 	addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
554 	sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
555 
556 	dma_range = &dma_ranges[0];
557 	for (r = 0; r < hose->region_count; r++) {
558 		u64 bus_start, phys_start, size;
559 
560 		/* skip if !PCI_REGION_SYS_MEMORY */
561 		if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
562 			continue;
563 
564 		bus_start = (u64)hose->regions[r].bus_start;
565 		phys_start = (u64)hose->regions[r].phys_start;
566 		size = (u64)hose->regions[r].size;
567 
568 		dma_range[0] = 0;
569 		if (size >= 0x100000000ull)
570 			dma_range[0] |= FDT_PCI_MEM64;
571 		else
572 			dma_range[0] |= FDT_PCI_MEM32;
573 		if (hose->regions[r].flags & PCI_REGION_PREFETCH)
574 			dma_range[0] |= FDT_PCI_PREFETCH;
575 #ifdef CONFIG_SYS_PCI_64BIT
576 		dma_range[1] = bus_start >> 32;
577 #else
578 		dma_range[1] = 0;
579 #endif
580 		dma_range[2] = bus_start & 0xffffffff;
581 
582 		if (addrcell == 2) {
583 			dma_range[3] = phys_start >> 32;
584 			dma_range[4] = phys_start & 0xffffffff;
585 		} else {
586 			dma_range[3] = phys_start & 0xffffffff;
587 		}
588 
589 		if (sizecell == 2) {
590 			dma_range[3 + addrcell + 0] = size >> 32;
591 			dma_range[3 + addrcell + 1] = size & 0xffffffff;
592 		} else {
593 			dma_range[3 + addrcell + 0] = size & 0xffffffff;
594 		}
595 
596 		dma_range += (3 + addrcell + sizecell);
597 	}
598 
599 	len = dma_range - &dma_ranges[0];
600 	if (len)
601 		fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
602 
603 	return 0;
604 }
605 #endif
606 
607 #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
608 /*
609  * Provide a weak default function to return the flash bank size.
610  * There might be multiple non-identical flash chips connected to one
611  * chip-select, so we need to pass an index as well.
612  */
613 u32 __flash_get_bank_size(int cs, int idx)
614 {
615 	extern flash_info_t flash_info[];
616 
617 	/*
618 	 * As default, a simple 1:1 mapping is provided. Boards with
619 	 * a different mapping need to supply a board specific mapping
620 	 * routine.
621 	 */
622 	return flash_info[cs].size;
623 }
624 u32 flash_get_bank_size(int cs, int idx)
625 	__attribute__((weak, alias("__flash_get_bank_size")));
626 
627 /*
628  * This function can be used to update the size in the "reg" property
629  * of all NOR FLASH device nodes. This is necessary for boards with
630  * non-fixed NOR FLASH sizes.
631  */
632 int fdt_fixup_nor_flash_size(void *blob)
633 {
634 	char compat[][16] = { "cfi-flash", "jedec-flash" };
635 	int off;
636 	int len;
637 	struct fdt_property *prop;
638 	u32 *reg, *reg2;
639 	int i;
640 
641 	for (i = 0; i < 2; i++) {
642 		off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
643 		while (off != -FDT_ERR_NOTFOUND) {
644 			int idx;
645 
646 			/*
647 			 * Found one compatible node, so fixup the size
648 			 * int its reg properties
649 			 */
650 			prop = fdt_get_property_w(blob, off, "reg", &len);
651 			if (prop) {
652 				int tuple_size = 3 * sizeof(reg);
653 
654 				/*
655 				 * There might be multiple reg-tuples,
656 				 * so loop through them all
657 				 */
658 				reg = reg2 = (u32 *)&prop->data[0];
659 				for (idx = 0; idx < (len / tuple_size); idx++) {
660 					/*
661 					 * Update size in reg property
662 					 */
663 					reg[2] = flash_get_bank_size(reg[0],
664 								     idx);
665 
666 					/*
667 					 * Point to next reg tuple
668 					 */
669 					reg += 3;
670 				}
671 
672 				fdt_setprop(blob, off, "reg", reg2, len);
673 			}
674 
675 			/* Move to next compatible node */
676 			off = fdt_node_offset_by_compatible(blob, off,
677 							    compat[i]);
678 		}
679 	}
680 
681 	return 0;
682 }
683 #endif
684 
685 int fdt_increase_size(void *fdt, int add_len)
686 {
687 	int newlen;
688 
689 	newlen = fdt_totalsize(fdt) + add_len;
690 
691 	/* Open in place with a new len */
692 	return fdt_open_into(fdt, fdt, newlen);
693 }
694 
695 #ifdef CONFIG_FDT_FIXUP_PARTITIONS
696 #include <jffs2/load_kernel.h>
697 #include <mtd_node.h>
698 
699 struct reg_cell {
700 	unsigned int r0;
701 	unsigned int r1;
702 };
703 
704 int fdt_del_subnodes(const void *blob, int parent_offset)
705 {
706 	int off, ndepth;
707 	int ret;
708 
709 	for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
710 	     (off >= 0) && (ndepth > 0);
711 	     off = fdt_next_node(blob, off, &ndepth)) {
712 		if (ndepth == 1) {
713 			debug("delete %s: offset: %x\n",
714 				fdt_get_name(blob, off, 0), off);
715 			ret = fdt_del_node((void *)blob, off);
716 			if (ret < 0) {
717 				printf("Can't delete node: %s\n",
718 					fdt_strerror(ret));
719 				return ret;
720 			} else {
721 				ndepth = 0;
722 				off = parent_offset;
723 			}
724 		}
725 	}
726 	return 0;
727 }
728 
729 int fdt_del_partitions(void *blob, int parent_offset)
730 {
731 	const void *prop;
732 	int ndepth = 0;
733 	int off;
734 	int ret;
735 
736 	off = fdt_next_node(blob, parent_offset, &ndepth);
737 	if (off > 0 && ndepth == 1) {
738 		prop = fdt_getprop(blob, off, "label", NULL);
739 		if (prop == NULL) {
740 			/*
741 			 * Could not find label property, nand {}; node?
742 			 * Check subnode, delete partitions there if any.
743 			 */
744 			return fdt_del_partitions(blob, off);
745 		} else {
746 			ret = fdt_del_subnodes(blob, parent_offset);
747 			if (ret < 0) {
748 				printf("Can't remove subnodes: %s\n",
749 					fdt_strerror(ret));
750 				return ret;
751 			}
752 		}
753 	}
754 	return 0;
755 }
756 
757 int fdt_node_set_part_info(void *blob, int parent_offset,
758 			   struct mtd_device *dev)
759 {
760 	struct list_head *pentry;
761 	struct part_info *part;
762 	struct reg_cell cell;
763 	int off, ndepth = 0;
764 	int part_num, ret;
765 	char buf[64];
766 
767 	ret = fdt_del_partitions(blob, parent_offset);
768 	if (ret < 0)
769 		return ret;
770 
771 	/*
772 	 * Check if it is nand {}; subnode, adjust
773 	 * the offset in this case
774 	 */
775 	off = fdt_next_node(blob, parent_offset, &ndepth);
776 	if (off > 0 && ndepth == 1)
777 		parent_offset = off;
778 
779 	part_num = 0;
780 	list_for_each_prev(pentry, &dev->parts) {
781 		int newoff;
782 
783 		part = list_entry(pentry, struct part_info, link);
784 
785 		debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
786 			part_num, part->name, part->size,
787 			part->offset, part->mask_flags);
788 
789 		sprintf(buf, "partition@%llx", part->offset);
790 add_sub:
791 		ret = fdt_add_subnode(blob, parent_offset, buf);
792 		if (ret == -FDT_ERR_NOSPACE) {
793 			ret = fdt_increase_size(blob, 512);
794 			if (!ret)
795 				goto add_sub;
796 			else
797 				goto err_size;
798 		} else if (ret < 0) {
799 			printf("Can't add partition node: %s\n",
800 				fdt_strerror(ret));
801 			return ret;
802 		}
803 		newoff = ret;
804 
805 		/* Check MTD_WRITEABLE_CMD flag */
806 		if (part->mask_flags & 1) {
807 add_ro:
808 			ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
809 			if (ret == -FDT_ERR_NOSPACE) {
810 				ret = fdt_increase_size(blob, 512);
811 				if (!ret)
812 					goto add_ro;
813 				else
814 					goto err_size;
815 			} else if (ret < 0)
816 				goto err_prop;
817 		}
818 
819 		cell.r0 = cpu_to_fdt32(part->offset);
820 		cell.r1 = cpu_to_fdt32(part->size);
821 add_reg:
822 		ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
823 		if (ret == -FDT_ERR_NOSPACE) {
824 			ret = fdt_increase_size(blob, 512);
825 			if (!ret)
826 				goto add_reg;
827 			else
828 				goto err_size;
829 		} else if (ret < 0)
830 			goto err_prop;
831 
832 add_label:
833 		ret = fdt_setprop_string(blob, newoff, "label", part->name);
834 		if (ret == -FDT_ERR_NOSPACE) {
835 			ret = fdt_increase_size(blob, 512);
836 			if (!ret)
837 				goto add_label;
838 			else
839 				goto err_size;
840 		} else if (ret < 0)
841 			goto err_prop;
842 
843 		part_num++;
844 	}
845 	return 0;
846 err_size:
847 	printf("Can't increase blob size: %s\n", fdt_strerror(ret));
848 	return ret;
849 err_prop:
850 	printf("Can't add property: %s\n", fdt_strerror(ret));
851 	return ret;
852 }
853 
854 /*
855  * Update partitions in nor/nand nodes using info from
856  * mtdparts environment variable. The nodes to update are
857  * specified by node_info structure which contains mtd device
858  * type and compatible string: E. g. the board code in
859  * ft_board_setup() could use:
860  *
861  *	struct node_info nodes[] = {
862  *		{ "fsl,mpc5121-nfc",    MTD_DEV_TYPE_NAND, },
863  *		{ "cfi-flash",          MTD_DEV_TYPE_NOR,  },
864  *	};
865  *
866  *	fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
867  */
868 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
869 {
870 	struct node_info *ni = node_info;
871 	struct mtd_device *dev;
872 	char *parts;
873 	int i, idx;
874 	int noff;
875 
876 	parts = getenv("mtdparts");
877 	if (!parts)
878 		return;
879 
880 	if (mtdparts_init() != 0)
881 		return;
882 
883 	for (i = 0; i < node_info_size; i++) {
884 		idx = 0;
885 		noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
886 		while (noff != -FDT_ERR_NOTFOUND) {
887 			debug("%s: %s, mtd dev type %d\n",
888 				fdt_get_name(blob, noff, 0),
889 				ni[i].compat, ni[i].type);
890 			dev = device_find(ni[i].type, idx++);
891 			if (dev) {
892 				if (fdt_node_set_part_info(blob, noff, dev))
893 					return; /* return on error */
894 			}
895 
896 			/* Jump to next flash node */
897 			noff = fdt_node_offset_by_compatible(blob, noff,
898 							     ni[i].compat);
899 		}
900 	}
901 }
902 #endif
903 
904 void fdt_del_node_and_alias(void *blob, const char *alias)
905 {
906 	int off = fdt_path_offset(blob, alias);
907 
908 	if (off < 0)
909 		return;
910 
911 	fdt_del_node(blob, off);
912 
913 	off = fdt_path_offset(blob, "/aliases");
914 	fdt_delprop(blob, off, alias);
915 }
916 
917 #define PRu64	"%llx"
918 
919 /* Max address size we deal with */
920 #define OF_MAX_ADDR_CELLS	4
921 #define OF_BAD_ADDR	((u64)-1)
922 #define OF_CHECK_COUNTS(na, ns)	((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
923 			(ns) > 0)
924 
925 /* Debug utility */
926 #ifdef DEBUG
927 static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
928 {
929 	printf("%s", s);
930 	while(na--)
931 		printf(" %08x", *(addr++));
932 	printf("\n");
933 }
934 #else
935 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
936 #endif
937 
938 /* Callbacks for bus specific translators */
939 struct of_bus {
940 	const char	*name;
941 	const char	*addresses;
942 	void		(*count_cells)(void *blob, int parentoffset,
943 				int *addrc, int *sizec);
944 	u64		(*map)(fdt32_t *addr, const fdt32_t *range,
945 				int na, int ns, int pna);
946 	int		(*translate)(fdt32_t *addr, u64 offset, int na);
947 };
948 
949 /* Default translator (generic bus) */
950 void of_bus_default_count_cells(void *blob, int parentoffset,
951 					int *addrc, int *sizec)
952 {
953 	const fdt32_t *prop;
954 
955 	if (addrc)
956 		*addrc = fdt_address_cells(blob, parentoffset);
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;
1182 	uint32_t phandle = 0;
1183 
1184 	for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
1185 	     offset = fdt_next_node(blob, offset, NULL)) {
1186 		phandle = max(phandle, fdt_get_phandle(blob, offset));
1187 	}
1188 
1189 	return phandle + 1;
1190 }
1191 
1192 /*
1193  * fdt_set_phandle: Create a phandle property for the given node
1194  *
1195  * @fdt: ptr to device tree
1196  * @nodeoffset: node to update
1197  * @phandle: phandle value to set (must be unique)
1198  */
1199 int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
1200 {
1201 	int ret;
1202 
1203 #ifdef DEBUG
1204 	int off = fdt_node_offset_by_phandle(fdt, phandle);
1205 
1206 	if ((off >= 0) && (off != nodeoffset)) {
1207 		char buf[64];
1208 
1209 		fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1210 		printf("Trying to update node %s with phandle %u ",
1211 		       buf, phandle);
1212 
1213 		fdt_get_path(fdt, off, buf, sizeof(buf));
1214 		printf("that already exists in node %s.\n", buf);
1215 		return -FDT_ERR_BADPHANDLE;
1216 	}
1217 #endif
1218 
1219 	ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
1220 	if (ret < 0)
1221 		return ret;
1222 
1223 	/*
1224 	 * For now, also set the deprecated "linux,phandle" property, so that we
1225 	 * don't break older kernels.
1226 	 */
1227 	ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
1228 
1229 	return ret;
1230 }
1231 
1232 /*
1233  * fdt_create_phandle: Create a phandle property for the given node
1234  *
1235  * @fdt: ptr to device tree
1236  * @nodeoffset: node to update
1237  */
1238 unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
1239 {
1240 	/* see if there is a phandle already */
1241 	int phandle = fdt_get_phandle(fdt, nodeoffset);
1242 
1243 	/* if we got 0, means no phandle so create one */
1244 	if (phandle == 0) {
1245 		int ret;
1246 
1247 		phandle = fdt_alloc_phandle(fdt);
1248 		ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1249 		if (ret < 0) {
1250 			printf("Can't set phandle %u: %s\n", phandle,
1251 			       fdt_strerror(ret));
1252 			return 0;
1253 		}
1254 	}
1255 
1256 	return phandle;
1257 }
1258 
1259 /*
1260  * fdt_set_node_status: Set status for the given node
1261  *
1262  * @fdt: ptr to device tree
1263  * @nodeoffset: node to update
1264  * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1265  *	    FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1266  * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1267  */
1268 int fdt_set_node_status(void *fdt, int nodeoffset,
1269 			enum fdt_status status, unsigned int error_code)
1270 {
1271 	char buf[16];
1272 	int ret = 0;
1273 
1274 	if (nodeoffset < 0)
1275 		return nodeoffset;
1276 
1277 	switch (status) {
1278 	case FDT_STATUS_OKAY:
1279 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1280 		break;
1281 	case FDT_STATUS_DISABLED:
1282 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1283 		break;
1284 	case FDT_STATUS_FAIL:
1285 		ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1286 		break;
1287 	case FDT_STATUS_FAIL_ERROR_CODE:
1288 		sprintf(buf, "fail-%d", error_code);
1289 		ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
1290 		break;
1291 	default:
1292 		printf("Invalid fdt status: %x\n", status);
1293 		ret = -1;
1294 		break;
1295 	}
1296 
1297 	return ret;
1298 }
1299 
1300 /*
1301  * fdt_set_status_by_alias: Set status for the given node given an alias
1302  *
1303  * @fdt: ptr to device tree
1304  * @alias: alias of node to update
1305  * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
1306  *	    FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
1307  * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
1308  */
1309 int fdt_set_status_by_alias(void *fdt, const char* alias,
1310 			    enum fdt_status status, unsigned int error_code)
1311 {
1312 	int offset = fdt_path_offset(fdt, alias);
1313 
1314 	return fdt_set_node_status(fdt, offset, status, error_code);
1315 }
1316 
1317 #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
1318 int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
1319 {
1320 	int noff;
1321 	int ret;
1322 
1323 	noff = fdt_node_offset_by_compatible(blob, -1, compat);
1324 	if (noff != -FDT_ERR_NOTFOUND) {
1325 		debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
1326 add_edid:
1327 		ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
1328 		if (ret == -FDT_ERR_NOSPACE) {
1329 			ret = fdt_increase_size(blob, 512);
1330 			if (!ret)
1331 				goto add_edid;
1332 			else
1333 				goto err_size;
1334 		} else if (ret < 0) {
1335 			printf("Can't add property: %s\n", fdt_strerror(ret));
1336 			return ret;
1337 		}
1338 	}
1339 	return 0;
1340 err_size:
1341 	printf("Can't increase blob size: %s\n", fdt_strerror(ret));
1342 	return ret;
1343 }
1344 #endif
1345 
1346 /*
1347  * Verify the physical address of device tree node for a given alias
1348  *
1349  * This function locates the device tree node of a given alias, and then
1350  * verifies that the physical address of that device matches the given
1351  * parameter.  It displays a message if there is a mismatch.
1352  *
1353  * Returns 1 on success, 0 on failure
1354  */
1355 int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1356 {
1357 	const char *path;
1358 	const fdt32_t *reg;
1359 	int node, len;
1360 	u64 dt_addr;
1361 
1362 	path = fdt_getprop(fdt, anode, alias, NULL);
1363 	if (!path) {
1364 		/* If there's no such alias, then it's not a failure */
1365 		return 1;
1366 	}
1367 
1368 	node = fdt_path_offset(fdt, path);
1369 	if (node < 0) {
1370 		printf("Warning: device tree alias '%s' points to invalid "
1371 		       "node %s.\n", alias, path);
1372 		return 0;
1373 	}
1374 
1375 	reg = fdt_getprop(fdt, node, "reg", &len);
1376 	if (!reg) {
1377 		printf("Warning: device tree node '%s' has no address.\n",
1378 		       path);
1379 		return 0;
1380 	}
1381 
1382 	dt_addr = fdt_translate_address(fdt, node, reg);
1383 	if (addr != dt_addr) {
1384 		printf("Warning: U-Boot configured device %s at address %llx,\n"
1385 		       " but the device tree has it address %llx.\n",
1386 		       alias, addr, dt_addr);
1387 		return 0;
1388 	}
1389 
1390 	return 1;
1391 }
1392 
1393 /*
1394  * Returns the base address of an SOC or PCI node
1395  */
1396 u64 fdt_get_base_address(void *fdt, int node)
1397 {
1398 	int size;
1399 	u32 naddr;
1400 	const fdt32_t *prop;
1401 
1402 	naddr = fdt_address_cells(fdt, node);
1403 
1404 	prop = fdt_getprop(fdt, node, "ranges", &size);
1405 
1406 	return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
1407 }
1408 
1409 /*
1410  * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
1411  */
1412 static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1413 			 uint64_t *val, int cells)
1414 {
1415 	const fdt32_t *prop32 = &prop[cell_off];
1416 	const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
1417 
1418 	if ((cell_off + cells) > prop_len)
1419 		return -FDT_ERR_NOSPACE;
1420 
1421 	switch (cells) {
1422 	case 1:
1423 		*val = fdt32_to_cpu(*prop32);
1424 		break;
1425 	case 2:
1426 		*val = fdt64_to_cpu(*prop64);
1427 		break;
1428 	default:
1429 		return -FDT_ERR_NOSPACE;
1430 	}
1431 
1432 	return 0;
1433 }
1434 
1435 /**
1436  * fdt_read_range - Read a node's n'th range property
1437  *
1438  * @fdt: ptr to device tree
1439  * @node: offset of node
1440  * @n: range index
1441  * @child_addr: pointer to storage for the "child address" field
1442  * @addr: pointer to storage for the CPU view translated physical start
1443  * @len: pointer to storage for the range length
1444  *
1445  * Convenience function that reads and interprets a specific range out of
1446  * a number of the "ranges" property array.
1447  */
1448 int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1449 		   uint64_t *addr, uint64_t *len)
1450 {
1451 	int pnode = fdt_parent_offset(fdt, node);
1452 	const fdt32_t *ranges;
1453 	int pacells;
1454 	int acells;
1455 	int scells;
1456 	int ranges_len;
1457 	int cell = 0;
1458 	int r = 0;
1459 
1460 	/*
1461 	 * The "ranges" property is an array of
1462 	 * { <child address> <parent address> <size in child address space> }
1463 	 *
1464 	 * All 3 elements can span a diffent number of cells. Fetch their size.
1465 	 */
1466 	pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1467 	acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1468 	scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1469 
1470 	/* Now try to get the ranges property */
1471 	ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1472 	if (!ranges)
1473 		return -FDT_ERR_NOTFOUND;
1474 	ranges_len /= sizeof(uint32_t);
1475 
1476 	/* Jump to the n'th entry */
1477 	cell = n * (pacells + acells + scells);
1478 
1479 	/* Read <child address> */
1480 	if (child_addr) {
1481 		r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1482 				  acells);
1483 		if (r)
1484 			return r;
1485 	}
1486 	cell += acells;
1487 
1488 	/* Read <parent address> */
1489 	if (addr)
1490 		*addr = fdt_translate_address(fdt, node, ranges + cell);
1491 	cell += pacells;
1492 
1493 	/* Read <size in child address space> */
1494 	if (len) {
1495 		r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1496 		if (r)
1497 			return r;
1498 	}
1499 
1500 	return 0;
1501 }
1502 
1503 /**
1504  * fdt_setup_simplefb_node - Fill and enable a simplefb node
1505  *
1506  * @fdt: ptr to device tree
1507  * @node: offset of the simplefb node
1508  * @base_address: framebuffer base address
1509  * @width: width in pixels
1510  * @height: height in pixels
1511  * @stride: bytes per line
1512  * @format: pixel format string
1513  *
1514  * Convenience function to fill and enable a simplefb node.
1515  */
1516 int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
1517 			    u32 height, u32 stride, const char *format)
1518 {
1519 	char name[32];
1520 	fdt32_t cells[4];
1521 	int i, addrc, sizec, ret;
1522 
1523 	of_bus_default_count_cells(fdt, fdt_parent_offset(fdt, node),
1524 				   &addrc, &sizec);
1525 	i = 0;
1526 	if (addrc == 2)
1527 		cells[i++] = cpu_to_fdt32(base_address >> 32);
1528 	cells[i++] = cpu_to_fdt32(base_address);
1529 	if (sizec == 2)
1530 		cells[i++] = 0;
1531 	cells[i++] = cpu_to_fdt32(height * stride);
1532 
1533 	ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
1534 	if (ret < 0)
1535 		return ret;
1536 
1537 	snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
1538 	ret = fdt_set_name(fdt, node, name);
1539 	if (ret < 0)
1540 		return ret;
1541 
1542 	ret = fdt_setprop_u32(fdt, node, "width", width);
1543 	if (ret < 0)
1544 		return ret;
1545 
1546 	ret = fdt_setprop_u32(fdt, node, "height", height);
1547 	if (ret < 0)
1548 		return ret;
1549 
1550 	ret = fdt_setprop_u32(fdt, node, "stride", stride);
1551 	if (ret < 0)
1552 		return ret;
1553 
1554 	ret = fdt_setprop_string(fdt, node, "format", format);
1555 	if (ret < 0)
1556 		return ret;
1557 
1558 	ret = fdt_setprop_string(fdt, node, "status", "okay");
1559 	if (ret < 0)
1560 		return ret;
1561 
1562 	return 0;
1563 }
1564