xref: /openbmc/u-boot/lib/fdtdec.c (revision 1603bf3c)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * SPDX-License-Identifier:	GPL-2.0+
4  */
5 
6 #ifndef USE_HOSTCC
7 #include <common.h>
8 #include <errno.h>
9 #include <serial.h>
10 #include <libfdt.h>
11 #include <fdtdec.h>
12 #include <linux/ctype.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 /*
17  * Here are the type we know about. One day we might allow drivers to
18  * register. For now we just put them here. The COMPAT macro allows us to
19  * turn this into a sparse list later, and keeps the ID with the name.
20  */
21 #define COMPAT(id, name) name
22 static const char * const compat_names[COMPAT_COUNT] = {
23 	COMPAT(UNKNOWN, "<none>"),
24 	COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
25 	COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
26 	COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
27 	COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
28 	COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
29 	COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
30 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
31 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
32 	COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
33 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
34 	COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
35 	COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
36 	COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
37 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
38 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
39 	COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
40 	COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
41 	COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
42 	COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
43 	COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
44 	COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
45 	COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
46 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
47 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
48 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
49 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
50 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
51 	COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
52 	COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
53 	COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
54 	COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
55 	COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
56 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
57 	COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
58 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
59 	COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
60 	COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
61 	COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
62 	COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
63 	COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
64 	COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
65 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
66 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
67 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
68 	COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
69 	COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
70 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
71 	COMPAT(SANDBOX_HOST_EMULATION, "sandbox,host-emulation"),
72 	COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
73 	COMPAT(TI_TPS65090, "ti,tps65090"),
74 	COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
75 	COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
76 	COMPAT(PARADE_PS8625, "parade,ps8625"),
77 	COMPAT(COMPAT_INTEL_LPC, "intel,lpc"),
78 	COMPAT(INTEL_MICROCODE, "intel,microcode"),
79 	COMPAT(MEMORY_SPD, "memory-spd"),
80 	COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
81 	COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
82 	COMPAT(INTEL_GMA, "intel,gma"),
83 	COMPAT(AMS_AS3722, "ams,as3722"),
84 	COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
85 };
86 
87 const char *fdtdec_get_compatible(enum fdt_compat_id id)
88 {
89 	/* We allow reading of the 'unknown' ID for testing purposes */
90 	assert(id >= 0 && id < COMPAT_COUNT);
91 	return compat_names[id];
92 }
93 
94 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
95 		const char *prop_name, fdt_size_t *sizep)
96 {
97 	const fdt_addr_t *cell;
98 	int len;
99 
100 	debug("%s: %s: ", __func__, prop_name);
101 	cell = fdt_getprop(blob, node, prop_name, &len);
102 	if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
103 		     len == sizeof(fdt_addr_t) * 2)) {
104 		fdt_addr_t addr = fdt_addr_to_cpu(*cell);
105 		if (sizep) {
106 			const fdt_size_t *size;
107 
108 			size = (fdt_size_t *)((char *)cell +
109 					sizeof(fdt_addr_t));
110 			*sizep = fdt_size_to_cpu(*size);
111 			debug("addr=%08lx, size=%08x\n",
112 			      (ulong)addr, *sizep);
113 		} else {
114 			debug("%08lx\n", (ulong)addr);
115 		}
116 		return addr;
117 	}
118 	debug("(not found)\n");
119 	return FDT_ADDR_T_NONE;
120 }
121 
122 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
123 		const char *prop_name)
124 {
125 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
126 }
127 
128 #ifdef CONFIG_PCI
129 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
130 		const char *prop_name, struct fdt_pci_addr *addr)
131 {
132 	const u32 *cell;
133 	int len;
134 	int ret = -ENOENT;
135 
136 	debug("%s: %s: ", __func__, prop_name);
137 
138 	/*
139 	 * If we follow the pci bus bindings strictly, we should check
140 	 * the value of the node's parent node's #address-cells and
141 	 * #size-cells. They need to be 3 and 2 accordingly. However,
142 	 * for simplicity we skip the check here.
143 	 */
144 	cell = fdt_getprop(blob, node, prop_name, &len);
145 	if (!cell)
146 		goto fail;
147 
148 	if ((len % FDT_PCI_REG_SIZE) == 0) {
149 		int num = len / FDT_PCI_REG_SIZE;
150 		int i;
151 
152 		for (i = 0; i < num; i++) {
153 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
154 			      (ulong)fdt_addr_to_cpu(cell[0]),
155 			      (ulong)fdt_addr_to_cpu(cell[1]),
156 			      (ulong)fdt_addr_to_cpu(cell[2]));
157 			if ((fdt_addr_to_cpu(*cell) & type) == type) {
158 				addr->phys_hi = fdt_addr_to_cpu(cell[0]);
159 				addr->phys_mid = fdt_addr_to_cpu(cell[1]);
160 				addr->phys_lo = fdt_addr_to_cpu(cell[2]);
161 				break;
162 			} else {
163 				cell += (FDT_PCI_ADDR_CELLS +
164 					 FDT_PCI_SIZE_CELLS);
165 			}
166 		}
167 
168 		if (i == num)
169 			goto fail;
170 
171 		return 0;
172 	} else {
173 		ret = -EINVAL;
174 	}
175 
176 fail:
177 	debug("(not found)\n");
178 	return ret;
179 }
180 
181 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
182 {
183 	const char *list, *end;
184 	int len;
185 
186 	list = fdt_getprop(blob, node, "compatible", &len);
187 	if (!list)
188 		return -ENOENT;
189 
190 	end = list + len;
191 	while (list < end) {
192 		char *s;
193 
194 		len = strlen(list);
195 		if (len >= strlen("pciVVVV,DDDD")) {
196 			s = strstr(list, "pci");
197 
198 			/*
199 			 * check if the string is something like pciVVVV,DDDD.RR
200 			 * or just pciVVVV,DDDD
201 			 */
202 			if (s && s[7] == ',' &&
203 			    (s[12] == '.' || s[12] == 0)) {
204 				s += 3;
205 				*vendor = simple_strtol(s, NULL, 16);
206 
207 				s += 5;
208 				*device = simple_strtol(s, NULL, 16);
209 
210 				return 0;
211 			}
212 		} else {
213 			list += (len + 1);
214 		}
215 	}
216 
217 	return -ENOENT;
218 }
219 
220 int fdtdec_get_pci_bdf(const void *blob, int node,
221 		struct fdt_pci_addr *addr, pci_dev_t *bdf)
222 {
223 	u16 dt_vendor, dt_device, vendor, device;
224 	int ret;
225 
226 	/* get vendor id & device id from the compatible string */
227 	ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
228 	if (ret)
229 		return ret;
230 
231 	/* extract the bdf from fdt_pci_addr */
232 	*bdf = addr->phys_hi & 0xffff00;
233 
234 	/* read vendor id & device id based on bdf */
235 	pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
236 	pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
237 
238 	/*
239 	 * Note there are two places in the device tree to fully describe
240 	 * a pci device: one is via compatible string with a format of
241 	 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
242 	 * the device node's reg address property. We read the vendor id
243 	 * and device id based on bdf and compare the values with the
244 	 * "VVVV,DDDD". If they are the same, then we are good to use bdf
245 	 * to read device's bar. But if they are different, we have to rely
246 	 * on the vendor id and device id extracted from the compatible
247 	 * string and locate the real bdf by pci_find_device(). This is
248 	 * because normally we may only know device's device number and
249 	 * function number when writing device tree. The bus number is
250 	 * dynamically assigned during the pci enumeration process.
251 	 */
252 	if ((dt_vendor != vendor) || (dt_device != device)) {
253 		*bdf = pci_find_device(dt_vendor, dt_device, 0);
254 		if (*bdf == -1)
255 			return -ENODEV;
256 	}
257 
258 	return 0;
259 }
260 
261 int fdtdec_get_pci_bar32(const void *blob, int node,
262 		struct fdt_pci_addr *addr, u32 *bar)
263 {
264 	pci_dev_t bdf;
265 	int barnum;
266 	int ret;
267 
268 	/* get pci devices's bdf */
269 	ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
270 	if (ret)
271 		return ret;
272 
273 	/* extract the bar number from fdt_pci_addr */
274 	barnum = addr->phys_hi & 0xff;
275 	if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
276 		return -EINVAL;
277 
278 	barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
279 	*bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
280 
281 	return 0;
282 }
283 #endif
284 
285 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
286 		uint64_t default_val)
287 {
288 	const uint64_t *cell64;
289 	int length;
290 
291 	cell64 = fdt_getprop(blob, node, prop_name, &length);
292 	if (!cell64 || length < sizeof(*cell64))
293 		return default_val;
294 
295 	return fdt64_to_cpu(*cell64);
296 }
297 
298 int fdtdec_get_is_enabled(const void *blob, int node)
299 {
300 	const char *cell;
301 
302 	/*
303 	 * It should say "okay", so only allow that. Some fdts use "ok" but
304 	 * this is a bug. Please fix your device tree source file. See here
305 	 * for discussion:
306 	 *
307 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
308 	 */
309 	cell = fdt_getprop(blob, node, "status", NULL);
310 	if (cell)
311 		return 0 == strcmp(cell, "okay");
312 	return 1;
313 }
314 
315 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
316 {
317 	enum fdt_compat_id id;
318 
319 	/* Search our drivers */
320 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
321 		if (0 == fdt_node_check_compatible(blob, node,
322 				compat_names[id]))
323 			return id;
324 	return COMPAT_UNKNOWN;
325 }
326 
327 int fdtdec_next_compatible(const void *blob, int node,
328 		enum fdt_compat_id id)
329 {
330 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
331 }
332 
333 int fdtdec_next_compatible_subnode(const void *blob, int node,
334 		enum fdt_compat_id id, int *depthp)
335 {
336 	do {
337 		node = fdt_next_node(blob, node, depthp);
338 	} while (*depthp > 1);
339 
340 	/* If this is a direct subnode, and compatible, return it */
341 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
342 						blob, node, compat_names[id]))
343 		return node;
344 
345 	return -FDT_ERR_NOTFOUND;
346 }
347 
348 int fdtdec_next_alias(const void *blob, const char *name,
349 		enum fdt_compat_id id, int *upto)
350 {
351 #define MAX_STR_LEN 20
352 	char str[MAX_STR_LEN + 20];
353 	int node, err;
354 
355 	/* snprintf() is not available */
356 	assert(strlen(name) < MAX_STR_LEN);
357 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
358 	node = fdt_path_offset(blob, str);
359 	if (node < 0)
360 		return node;
361 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
362 	if (err < 0)
363 		return err;
364 	if (err)
365 		return -FDT_ERR_NOTFOUND;
366 	(*upto)++;
367 	return node;
368 }
369 
370 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
371 			enum fdt_compat_id id, int *node_list, int maxcount)
372 {
373 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
374 
375 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
376 }
377 
378 /* TODO: Can we tighten this code up a little? */
379 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
380 			enum fdt_compat_id id, int *node_list, int maxcount)
381 {
382 	int name_len = strlen(name);
383 	int nodes[maxcount];
384 	int num_found = 0;
385 	int offset, node;
386 	int alias_node;
387 	int count;
388 	int i, j;
389 
390 	/* find the alias node if present */
391 	alias_node = fdt_path_offset(blob, "/aliases");
392 
393 	/*
394 	 * start with nothing, and we can assume that the root node can't
395 	 * match
396 	 */
397 	memset(nodes, '\0', sizeof(nodes));
398 
399 	/* First find all the compatible nodes */
400 	for (node = count = 0; node >= 0 && count < maxcount;) {
401 		node = fdtdec_next_compatible(blob, node, id);
402 		if (node >= 0)
403 			nodes[count++] = node;
404 	}
405 	if (node >= 0)
406 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
407 		       __func__, name);
408 
409 	/* Now find all the aliases */
410 	for (offset = fdt_first_property_offset(blob, alias_node);
411 			offset > 0;
412 			offset = fdt_next_property_offset(blob, offset)) {
413 		const struct fdt_property *prop;
414 		const char *path;
415 		int number;
416 		int found;
417 
418 		node = 0;
419 		prop = fdt_get_property_by_offset(blob, offset, NULL);
420 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
421 		if (prop->len && 0 == strncmp(path, name, name_len))
422 			node = fdt_path_offset(blob, prop->data);
423 		if (node <= 0)
424 			continue;
425 
426 		/* Get the alias number */
427 		number = simple_strtoul(path + name_len, NULL, 10);
428 		if (number < 0 || number >= maxcount) {
429 			debug("%s: warning: alias '%s' is out of range\n",
430 			       __func__, path);
431 			continue;
432 		}
433 
434 		/* Make sure the node we found is actually in our list! */
435 		found = -1;
436 		for (j = 0; j < count; j++)
437 			if (nodes[j] == node) {
438 				found = j;
439 				break;
440 			}
441 
442 		if (found == -1) {
443 			debug("%s: warning: alias '%s' points to a node "
444 				"'%s' that is missing or is not compatible "
445 				" with '%s'\n", __func__, path,
446 				fdt_get_name(blob, node, NULL),
447 			       compat_names[id]);
448 			continue;
449 		}
450 
451 		/*
452 		 * Add this node to our list in the right place, and mark
453 		 * it as done.
454 		 */
455 		if (fdtdec_get_is_enabled(blob, node)) {
456 			if (node_list[number]) {
457 				debug("%s: warning: alias '%s' requires that "
458 				      "a node be placed in the list in a "
459 				      "position which is already filled by "
460 				      "node '%s'\n", __func__, path,
461 				      fdt_get_name(blob, node, NULL));
462 				continue;
463 			}
464 			node_list[number] = node;
465 			if (number >= num_found)
466 				num_found = number + 1;
467 		}
468 		nodes[found] = 0;
469 	}
470 
471 	/* Add any nodes not mentioned by an alias */
472 	for (i = j = 0; i < maxcount; i++) {
473 		if (!node_list[i]) {
474 			for (; j < maxcount; j++)
475 				if (nodes[j] &&
476 					fdtdec_get_is_enabled(blob, nodes[j]))
477 					break;
478 
479 			/* Have we run out of nodes to add? */
480 			if (j == maxcount)
481 				break;
482 
483 			assert(!node_list[i]);
484 			node_list[i] = nodes[j++];
485 			if (i >= num_found)
486 				num_found = i + 1;
487 		}
488 	}
489 
490 	return num_found;
491 }
492 
493 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
494 			 int *seqp)
495 {
496 	int base_len = strlen(base);
497 	const char *find_name;
498 	int find_namelen;
499 	int prop_offset;
500 	int aliases;
501 
502 	find_name = fdt_get_name(blob, offset, &find_namelen);
503 	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
504 
505 	aliases = fdt_path_offset(blob, "/aliases");
506 	for (prop_offset = fdt_first_property_offset(blob, aliases);
507 	     prop_offset > 0;
508 	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
509 		const char *prop;
510 		const char *name;
511 		const char *slash;
512 		const char *p;
513 		int len;
514 
515 		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
516 		debug("   - %s, %s\n", name, prop);
517 		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
518 		    strncmp(name, base, base_len))
519 			continue;
520 
521 		slash = strrchr(prop, '/');
522 		if (strcmp(slash + 1, find_name))
523 			continue;
524 		for (p = name + strlen(name) - 1; p > name; p--) {
525 			if (!isdigit(*p)) {
526 				*seqp = simple_strtoul(p + 1, NULL, 10);
527 				debug("Found seq %d\n", *seqp);
528 				return 0;
529 			}
530 		}
531 	}
532 
533 	debug("Not found\n");
534 	return -ENOENT;
535 }
536 
537 int fdtdec_get_chosen_node(const void *blob, const char *name)
538 {
539 	const char *prop;
540 	int chosen_node;
541 	int len;
542 
543 	if (!blob)
544 		return -FDT_ERR_NOTFOUND;
545 	chosen_node = fdt_path_offset(blob, "/chosen");
546 	prop = fdt_getprop(blob, chosen_node, name, &len);
547 	if (!prop)
548 		return -FDT_ERR_NOTFOUND;
549 	return fdt_path_offset(blob, prop);
550 }
551 
552 int fdtdec_check_fdt(void)
553 {
554 	/*
555 	 * We must have an FDT, but we cannot panic() yet since the console
556 	 * is not ready. So for now, just assert(). Boards which need an early
557 	 * FDT (prior to console ready) will need to make their own
558 	 * arrangements and do their own checks.
559 	 */
560 	assert(!fdtdec_prepare_fdt());
561 	return 0;
562 }
563 
564 /*
565  * This function is a little odd in that it accesses global data. At some
566  * point if the architecture board.c files merge this will make more sense.
567  * Even now, it is common code.
568  */
569 int fdtdec_prepare_fdt(void)
570 {
571 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
572 	    fdt_check_header(gd->fdt_blob)) {
573 		printf("No valid FDT found - please append one to U-Boot "
574 			"binary, use u-boot-dtb.bin or define "
575 			"CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
576 		return -1;
577 	}
578 	return 0;
579 }
580 
581 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
582 {
583 	const u32 *phandle;
584 	int lookup;
585 
586 	debug("%s: %s\n", __func__, prop_name);
587 	phandle = fdt_getprop(blob, node, prop_name, NULL);
588 	if (!phandle)
589 		return -FDT_ERR_NOTFOUND;
590 
591 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
592 	return lookup;
593 }
594 
595 /**
596  * Look up a property in a node and check that it has a minimum length.
597  *
598  * @param blob		FDT blob
599  * @param node		node to examine
600  * @param prop_name	name of property to find
601  * @param min_len	minimum property length in bytes
602  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
603 			found, or -FDT_ERR_BADLAYOUT if not enough data
604  * @return pointer to cell, which is only valid if err == 0
605  */
606 static const void *get_prop_check_min_len(const void *blob, int node,
607 		const char *prop_name, int min_len, int *err)
608 {
609 	const void *cell;
610 	int len;
611 
612 	debug("%s: %s\n", __func__, prop_name);
613 	cell = fdt_getprop(blob, node, prop_name, &len);
614 	if (!cell)
615 		*err = -FDT_ERR_NOTFOUND;
616 	else if (len < min_len)
617 		*err = -FDT_ERR_BADLAYOUT;
618 	else
619 		*err = 0;
620 	return cell;
621 }
622 
623 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
624 		u32 *array, int count)
625 {
626 	const u32 *cell;
627 	int i, err = 0;
628 
629 	debug("%s: %s\n", __func__, prop_name);
630 	cell = get_prop_check_min_len(blob, node, prop_name,
631 				      sizeof(u32) * count, &err);
632 	if (!err) {
633 		for (i = 0; i < count; i++)
634 			array[i] = fdt32_to_cpu(cell[i]);
635 	}
636 	return err;
637 }
638 
639 int fdtdec_get_int_array_count(const void *blob, int node,
640 			       const char *prop_name, u32 *array, int count)
641 {
642 	const u32 *cell;
643 	int len, elems;
644 	int i;
645 
646 	debug("%s: %s\n", __func__, prop_name);
647 	cell = fdt_getprop(blob, node, prop_name, &len);
648 	if (!cell)
649 		return -FDT_ERR_NOTFOUND;
650 	elems = len / sizeof(u32);
651 	if (count > elems)
652 		count = elems;
653 	for (i = 0; i < count; i++)
654 		array[i] = fdt32_to_cpu(cell[i]);
655 
656 	return count;
657 }
658 
659 const u32 *fdtdec_locate_array(const void *blob, int node,
660 			       const char *prop_name, int count)
661 {
662 	const u32 *cell;
663 	int err;
664 
665 	cell = get_prop_check_min_len(blob, node, prop_name,
666 				      sizeof(u32) * count, &err);
667 	return err ? NULL : cell;
668 }
669 
670 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
671 {
672 	const s32 *cell;
673 	int len;
674 
675 	debug("%s: %s\n", __func__, prop_name);
676 	cell = fdt_getprop(blob, node, prop_name, &len);
677 	return cell != NULL;
678 }
679 
680 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
681 				   const char *list_name,
682 				   const char *cells_name,
683 				   int cell_count, int index,
684 				   struct fdtdec_phandle_args *out_args)
685 {
686 	const __be32 *list, *list_end;
687 	int rc = 0, size, cur_index = 0;
688 	uint32_t count = 0;
689 	int node = -1;
690 	int phandle;
691 
692 	/* Retrieve the phandle list property */
693 	list = fdt_getprop(blob, src_node, list_name, &size);
694 	if (!list)
695 		return -ENOENT;
696 	list_end = list + size / sizeof(*list);
697 
698 	/* Loop over the phandles until all the requested entry is found */
699 	while (list < list_end) {
700 		rc = -EINVAL;
701 		count = 0;
702 
703 		/*
704 		 * If phandle is 0, then it is an empty entry with no
705 		 * arguments.  Skip forward to the next entry.
706 		 */
707 		phandle = be32_to_cpup(list++);
708 		if (phandle) {
709 			/*
710 			 * Find the provider node and parse the #*-cells
711 			 * property to determine the argument length.
712 			 *
713 			 * This is not needed if the cell count is hard-coded
714 			 * (i.e. cells_name not set, but cell_count is set),
715 			 * except when we're going to return the found node
716 			 * below.
717 			 */
718 			if (cells_name || cur_index == index) {
719 				node = fdt_node_offset_by_phandle(blob,
720 								  phandle);
721 				if (!node) {
722 					debug("%s: could not find phandle\n",
723 					      fdt_get_name(blob, src_node,
724 							   NULL));
725 					goto err;
726 				}
727 			}
728 
729 			if (cells_name) {
730 				count = fdtdec_get_int(blob, node, cells_name,
731 						       -1);
732 				if (count == -1) {
733 					debug("%s: could not get %s for %s\n",
734 					      fdt_get_name(blob, src_node,
735 							   NULL),
736 					      cells_name,
737 					      fdt_get_name(blob, node,
738 							   NULL));
739 					goto err;
740 				}
741 			} else {
742 				count = cell_count;
743 			}
744 
745 			/*
746 			 * Make sure that the arguments actually fit in the
747 			 * remaining property data length
748 			 */
749 			if (list + count > list_end) {
750 				debug("%s: arguments longer than property\n",
751 				      fdt_get_name(blob, src_node, NULL));
752 				goto err;
753 			}
754 		}
755 
756 		/*
757 		 * All of the error cases above bail out of the loop, so at
758 		 * this point, the parsing is successful. If the requested
759 		 * index matches, then fill the out_args structure and return,
760 		 * or return -ENOENT for an empty entry.
761 		 */
762 		rc = -ENOENT;
763 		if (cur_index == index) {
764 			if (!phandle)
765 				goto err;
766 
767 			if (out_args) {
768 				int i;
769 
770 				if (count > MAX_PHANDLE_ARGS) {
771 					debug("%s: too many arguments %d\n",
772 					      fdt_get_name(blob, src_node,
773 							   NULL), count);
774 					count = MAX_PHANDLE_ARGS;
775 				}
776 				out_args->node = node;
777 				out_args->args_count = count;
778 				for (i = 0; i < count; i++) {
779 					out_args->args[i] =
780 							be32_to_cpup(list++);
781 				}
782 			}
783 
784 			/* Found it! return success */
785 			return 0;
786 		}
787 
788 		node = -1;
789 		list += count;
790 		cur_index++;
791 	}
792 
793 	/*
794 	 * Result will be one of:
795 	 * -ENOENT : index is for empty phandle
796 	 * -EINVAL : parsing error on data
797 	 * [1..n]  : Number of phandle (count mode; when index = -1)
798 	 */
799 	rc = index < 0 ? cur_index : -ENOENT;
800  err:
801 	return rc;
802 }
803 
804 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
805 		u8 *array, int count)
806 {
807 	const u8 *cell;
808 	int err;
809 
810 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
811 	if (!err)
812 		memcpy(array, cell, count);
813 	return err;
814 }
815 
816 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
817 			     const char *prop_name, int count)
818 {
819 	const u8 *cell;
820 	int err;
821 
822 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
823 	if (err)
824 		return NULL;
825 	return cell;
826 }
827 
828 int fdtdec_get_config_int(const void *blob, const char *prop_name,
829 		int default_val)
830 {
831 	int config_node;
832 
833 	debug("%s: %s\n", __func__, prop_name);
834 	config_node = fdt_path_offset(blob, "/config");
835 	if (config_node < 0)
836 		return default_val;
837 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
838 }
839 
840 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
841 {
842 	int config_node;
843 	const void *prop;
844 
845 	debug("%s: %s\n", __func__, prop_name);
846 	config_node = fdt_path_offset(blob, "/config");
847 	if (config_node < 0)
848 		return 0;
849 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
850 
851 	return prop != NULL;
852 }
853 
854 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
855 {
856 	const char *nodep;
857 	int nodeoffset;
858 	int len;
859 
860 	debug("%s: %s\n", __func__, prop_name);
861 	nodeoffset = fdt_path_offset(blob, "/config");
862 	if (nodeoffset < 0)
863 		return NULL;
864 
865 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
866 	if (!nodep)
867 		return NULL;
868 
869 	return (char *)nodep;
870 }
871 
872 int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
873 			 fdt_addr_t *basep, fdt_size_t *sizep)
874 {
875 	const fdt_addr_t *cell;
876 	int len;
877 
878 	debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
879 	      prop_name);
880 	cell = fdt_getprop(blob, node, prop_name, &len);
881 	if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
882 		debug("cell=%p, len=%d\n", cell, len);
883 		return -1;
884 	}
885 
886 	*basep = fdt_addr_to_cpu(*cell);
887 	*sizep = fdt_size_to_cpu(cell[1]);
888 	debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
889 	      (ulong)*sizep);
890 
891 	return 0;
892 }
893 
894 /**
895  * Read a flash entry from the fdt
896  *
897  * @param blob		FDT blob
898  * @param node		Offset of node to read
899  * @param name		Name of node being read
900  * @param entry		Place to put offset and size of this node
901  * @return 0 if ok, -ve on error
902  */
903 int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
904 			   struct fmap_entry *entry)
905 {
906 	const char *prop;
907 	u32 reg[2];
908 
909 	if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
910 		debug("Node '%s' has bad/missing 'reg' property\n", name);
911 		return -FDT_ERR_NOTFOUND;
912 	}
913 	entry->offset = reg[0];
914 	entry->length = reg[1];
915 	entry->used = fdtdec_get_int(blob, node, "used", entry->length);
916 	prop = fdt_getprop(blob, node, "compress", NULL);
917 	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
918 		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
919 	prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
920 	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
921 	entry->hash = (uint8_t *)prop;
922 
923 	return 0;
924 }
925 
926 static u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
927 {
928 	u64 number = 0;
929 
930 	while (cells--)
931 		number = (number << 32) | fdt32_to_cpu(*ptr++);
932 
933 	return number;
934 }
935 
936 int fdt_get_resource(const void *fdt, int node, const char *property,
937 		     unsigned int index, struct fdt_resource *res)
938 {
939 	const fdt32_t *ptr, *end;
940 	int na, ns, len, parent;
941 	unsigned int i = 0;
942 
943 	parent = fdt_parent_offset(fdt, node);
944 	if (parent < 0)
945 		return parent;
946 
947 	na = fdt_address_cells(fdt, parent);
948 	ns = fdt_size_cells(fdt, parent);
949 
950 	ptr = fdt_getprop(fdt, node, property, &len);
951 	if (!ptr)
952 		return len;
953 
954 	end = ptr + len / sizeof(*ptr);
955 
956 	while (ptr + na + ns <= end) {
957 		if (i == index) {
958 			res->start = res->end = fdtdec_get_number(ptr, na);
959 			res->end += fdtdec_get_number(&ptr[na], ns) - 1;
960 			return 0;
961 		}
962 
963 		ptr += na + ns;
964 		i++;
965 	}
966 
967 	return -FDT_ERR_NOTFOUND;
968 }
969 
970 int fdt_get_named_resource(const void *fdt, int node, const char *property,
971 			   const char *prop_names, const char *name,
972 			   struct fdt_resource *res)
973 {
974 	int index;
975 
976 	index = fdt_find_string(fdt, node, prop_names, name);
977 	if (index < 0)
978 		return index;
979 
980 	return fdt_get_resource(fdt, node, property, index, res);
981 }
982 
983 int fdtdec_decode_memory_region(const void *blob, int config_node,
984 				const char *mem_type, const char *suffix,
985 				fdt_addr_t *basep, fdt_size_t *sizep)
986 {
987 	char prop_name[50];
988 	const char *mem;
989 	fdt_size_t size, offset_size;
990 	fdt_addr_t base, offset;
991 	int node;
992 
993 	if (config_node == -1) {
994 		config_node = fdt_path_offset(blob, "/config");
995 		if (config_node < 0) {
996 			debug("%s: Cannot find /config node\n", __func__);
997 			return -ENOENT;
998 		}
999 	}
1000 	if (!suffix)
1001 		suffix = "";
1002 
1003 	snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
1004 		 suffix);
1005 	mem = fdt_getprop(blob, config_node, prop_name, NULL);
1006 	if (!mem) {
1007 		debug("%s: No memory type for '%s', using /memory\n", __func__,
1008 		      prop_name);
1009 		mem = "/memory";
1010 	}
1011 
1012 	node = fdt_path_offset(blob, mem);
1013 	if (node < 0) {
1014 		debug("%s: Failed to find node '%s': %s\n", __func__, mem,
1015 		      fdt_strerror(node));
1016 		return -ENOENT;
1017 	}
1018 
1019 	/*
1020 	 * Not strictly correct - the memory may have multiple banks. We just
1021 	 * use the first
1022 	 */
1023 	if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
1024 		debug("%s: Failed to decode memory region %s\n", __func__,
1025 		      mem);
1026 		return -EINVAL;
1027 	}
1028 
1029 	snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1030 		 suffix);
1031 	if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1032 				 &offset_size)) {
1033 		debug("%s: Failed to decode memory region '%s'\n", __func__,
1034 		      prop_name);
1035 		return -EINVAL;
1036 	}
1037 
1038 	*basep = base + offset;
1039 	*sizep = offset_size;
1040 
1041 	return 0;
1042 }
1043 #endif
1044