xref: /rk3399_rockchip-uboot/lib/fdtdec.c (revision ef4b01b2f7f176bb9107654257f7f3ed7fc2cf19)
1b5220bc6SSimon Glass /*
2b5220bc6SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
31a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
4b5220bc6SSimon Glass  */
5b5220bc6SSimon Glass 
629a23f9dSHeiko Schocher #ifndef USE_HOSTCC
7b5220bc6SSimon Glass #include <common.h>
85c33c9fdSSimon Glass #include <errno.h>
9b5220bc6SSimon Glass #include <serial.h>
10b5220bc6SSimon Glass #include <libfdt.h>
11b5220bc6SSimon Glass #include <fdtdec.h>
12b45122fdSSimon Glass #include <asm/sections.h>
135c33c9fdSSimon Glass #include <linux/ctype.h>
14b5220bc6SSimon Glass 
15b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
16b5220bc6SSimon Glass 
17b5220bc6SSimon Glass /*
18b5220bc6SSimon Glass  * Here are the type we know about. One day we might allow drivers to
19b5220bc6SSimon Glass  * register. For now we just put them here. The COMPAT macro allows us to
20b5220bc6SSimon Glass  * turn this into a sparse list later, and keeps the ID with the name.
21b5220bc6SSimon Glass  */
22b5220bc6SSimon Glass #define COMPAT(id, name) name
23b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = {
24f88fe2deSSimon Glass 	COMPAT(UNKNOWN, "<none>"),
250e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
260e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
27312693c3SJim Lin 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
28e1ae0d1fSSimon Glass 	COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
2900f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_DC, "nvidia,tegra124-dc"),
3000f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_SOR, "nvidia,tegra124-sor"),
3100f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
3287540de3SWei Ni 	COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
337aaa5a60STom Warren 	COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
34a73ca478SStephen Warren 	COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
35f4e4e0b0STom Warren 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
36c9aa831eSTom Warren 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
3779c7a90fSThierry Reding 	COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
387aaa5a60STom Warren 	COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
39cc9fe33aSHatim RV 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
40cc9fe33aSHatim RV 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
41c34253d1SRajeshwari Shinde 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
4272dbff12SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
4372dbff12SRajeshwari Shinde 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
44713cb680SHung-ying Tyan 	COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
456abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
46108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
47618766c0SAkshay Saraswat 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
48d7377b51SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
49de461c52SPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
501e4706a7SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
517d3ca0f8SJaehoon Chung 	COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
523577fe8bSPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
53ee1e3c2fSRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
54f37df0f8SPrzemyslaw Marczak 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
55bb8215f4SSimon Glass 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
567772bb78SRajeshwari Shinde 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
57ecbd7e1eSnaveen krishna chatradhi 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
587d95f2a3SSimon Glass 	COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
5945c480c9SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
6077f9b1fbSSimon Glass 	COMPAT(INTEL_MICROCODE, "intel,microcode"),
6165dd74a6SSimon Glass 	COMPAT(MEMORY_SPD, "memory-spd"),
623ac83935SSimon Glass 	COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
63bb80be39SSimon Glass 	COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
64effcf067SSimon Glass 	COMPAT(INTEL_GMA, "intel,gma"),
656173c45bSThierry Reding 	COMPAT(AMS_AS3722, "ams,as3722"),
665da38086SSimon Glass 	COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
67c89ada01SBin Meng 	COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
685318f18dSGabriel Huau 	COMPAT(INTEL_X86_PINCTRL, "intel,x86-pinctrl"),
696462cdedSMasahiro Yamada 	COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
7090b16d14SSimon Glass 	COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
719c7dea60SBin Meng 	COMPAT(COMPAT_INTEL_IRQ_ROUTER, "intel,irq-router"),
726ab00db2SMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
73129adf5bSMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
74*ef4b01b2SMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
75f3b84a30SAndrew Bradford 	COMPAT(COMPAT_INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
76f3b84a30SAndrew Bradford 	COMPAT(COMPAT_INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
77b5220bc6SSimon Glass };
78b5220bc6SSimon Glass 
79a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id)
80a53f4a29SSimon Glass {
81a53f4a29SSimon Glass 	/* We allow reading of the 'unknown' ID for testing purposes */
82a53f4a29SSimon Glass 	assert(id >= 0 && id < COMPAT_COUNT);
83a53f4a29SSimon Glass 	return compat_names[id];
84a53f4a29SSimon Glass }
85a53f4a29SSimon Glass 
8602464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
8702464e38SStephen Warren 		const char *prop_name, int index, int na, int ns,
8802464e38SStephen Warren 		fdt_size_t *sizep)
8902464e38SStephen Warren {
9002464e38SStephen Warren 	const fdt32_t *prop, *prop_end;
9102464e38SStephen Warren 	const fdt32_t *prop_addr, *prop_size, *prop_after_size;
9202464e38SStephen Warren 	int len;
9302464e38SStephen Warren 	fdt_addr_t addr;
9402464e38SStephen Warren 
9502464e38SStephen Warren 	debug("%s: %s: ", __func__, prop_name);
9602464e38SStephen Warren 
9702464e38SStephen Warren 	if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
9802464e38SStephen Warren 		debug("(na too large for fdt_addr_t type)\n");
9902464e38SStephen Warren 		return FDT_ADDR_T_NONE;
10002464e38SStephen Warren 	}
10102464e38SStephen Warren 
10202464e38SStephen Warren 	if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
10302464e38SStephen Warren 		debug("(ns too large for fdt_size_t type)\n");
10402464e38SStephen Warren 		return FDT_ADDR_T_NONE;
10502464e38SStephen Warren 	}
10602464e38SStephen Warren 
10702464e38SStephen Warren 	prop = fdt_getprop(blob, node, prop_name, &len);
10802464e38SStephen Warren 	if (!prop) {
10902464e38SStephen Warren 		debug("(not found)\n");
11002464e38SStephen Warren 		return FDT_ADDR_T_NONE;
11102464e38SStephen Warren 	}
11202464e38SStephen Warren 	prop_end = prop + (len / sizeof(*prop));
11302464e38SStephen Warren 
11402464e38SStephen Warren 	prop_addr = prop + (index * (na + ns));
11502464e38SStephen Warren 	prop_size = prop_addr + na;
11602464e38SStephen Warren 	prop_after_size = prop_size + ns;
11702464e38SStephen Warren 	if (prop_after_size > prop_end) {
11802464e38SStephen Warren 		debug("(not enough data: expected >= %d cells, got %d cells)\n",
11902464e38SStephen Warren 		      (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
12002464e38SStephen Warren 		return FDT_ADDR_T_NONE;
12102464e38SStephen Warren 	}
12202464e38SStephen Warren 
12302464e38SStephen Warren 	addr = fdtdec_get_number(prop_addr, na);
12402464e38SStephen Warren 
12502464e38SStephen Warren 	if (sizep) {
12602464e38SStephen Warren 		*sizep = fdtdec_get_number(prop_size, ns);
12702464e38SStephen Warren 		debug("addr=%08llx, size=%llx\n", (u64)addr, (u64)*sizep);
12802464e38SStephen Warren 	} else {
12902464e38SStephen Warren 		debug("addr=%08llx\n", (u64)addr);
13002464e38SStephen Warren 	}
13102464e38SStephen Warren 
13202464e38SStephen Warren 	return addr;
13302464e38SStephen Warren }
13402464e38SStephen Warren 
13502464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
13602464e38SStephen Warren 		int node, const char *prop_name, int index, fdt_size_t *sizep)
13702464e38SStephen Warren {
13802464e38SStephen Warren 	int na, ns;
13902464e38SStephen Warren 
14002464e38SStephen Warren 	debug("%s: ", __func__);
14102464e38SStephen Warren 
14202464e38SStephen Warren 	na = fdt_address_cells(blob, parent);
14302464e38SStephen Warren 	if (na < 1) {
14402464e38SStephen Warren 		debug("(bad #address-cells)\n");
14502464e38SStephen Warren 		return FDT_ADDR_T_NONE;
14602464e38SStephen Warren 	}
14702464e38SStephen Warren 
14802464e38SStephen Warren 	ns = fdt_size_cells(blob, parent);
149ff0a6358SPrzemyslaw Marczak 	if (ns < 0) {
15002464e38SStephen Warren 		debug("(bad #size-cells)\n");
15102464e38SStephen Warren 		return FDT_ADDR_T_NONE;
15202464e38SStephen Warren 	}
15302464e38SStephen Warren 
15402464e38SStephen Warren 	debug("na=%d, ns=%d, ", na, ns);
15502464e38SStephen Warren 
15602464e38SStephen Warren 	return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
15702464e38SStephen Warren 					  ns, sizep);
15802464e38SStephen Warren }
15902464e38SStephen Warren 
16002464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
16102464e38SStephen Warren 		const char *prop_name, int index, fdt_size_t *sizep)
16202464e38SStephen Warren {
16302464e38SStephen Warren 	int parent;
16402464e38SStephen Warren 
16502464e38SStephen Warren 	debug("%s: ", __func__);
16602464e38SStephen Warren 
16702464e38SStephen Warren 	parent = fdt_parent_offset(blob, node);
16802464e38SStephen Warren 	if (parent < 0) {
16902464e38SStephen Warren 		debug("(no parent found)\n");
17002464e38SStephen Warren 		return FDT_ADDR_T_NONE;
17102464e38SStephen Warren 	}
17202464e38SStephen Warren 
17302464e38SStephen Warren 	return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
17402464e38SStephen Warren 						index, sizep);
17502464e38SStephen Warren }
17602464e38SStephen Warren 
1774397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
1784397a2a8SSimon Glass 		const char *prop_name, fdt_size_t *sizep)
179b5220bc6SSimon Glass {
180d93b9a07SStephen Warren 	int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
181d93b9a07SStephen Warren 
18202464e38SStephen Warren 	return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
18302464e38SStephen Warren 					  sizeof(fdt_addr_t) / sizeof(fdt32_t),
184d93b9a07SStephen Warren 					  ns, sizep);
185b5220bc6SSimon Glass }
186b5220bc6SSimon Glass 
1874397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
1884397a2a8SSimon Glass 		const char *prop_name)
1894397a2a8SSimon Glass {
1904397a2a8SSimon Glass 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
1914397a2a8SSimon Glass }
1924397a2a8SSimon Glass 
193a62e84d7SBin Meng #ifdef CONFIG_PCI
194a62e84d7SBin Meng int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
195a62e84d7SBin Meng 		const char *prop_name, struct fdt_pci_addr *addr)
196a62e84d7SBin Meng {
197a62e84d7SBin Meng 	const u32 *cell;
198a62e84d7SBin Meng 	int len;
199a62e84d7SBin Meng 	int ret = -ENOENT;
200a62e84d7SBin Meng 
201a62e84d7SBin Meng 	debug("%s: %s: ", __func__, prop_name);
202a62e84d7SBin Meng 
203a62e84d7SBin Meng 	/*
204a62e84d7SBin Meng 	 * If we follow the pci bus bindings strictly, we should check
205a62e84d7SBin Meng 	 * the value of the node's parent node's #address-cells and
206a62e84d7SBin Meng 	 * #size-cells. They need to be 3 and 2 accordingly. However,
207a62e84d7SBin Meng 	 * for simplicity we skip the check here.
208a62e84d7SBin Meng 	 */
209a62e84d7SBin Meng 	cell = fdt_getprop(blob, node, prop_name, &len);
210a62e84d7SBin Meng 	if (!cell)
211a62e84d7SBin Meng 		goto fail;
212a62e84d7SBin Meng 
213a62e84d7SBin Meng 	if ((len % FDT_PCI_REG_SIZE) == 0) {
214a62e84d7SBin Meng 		int num = len / FDT_PCI_REG_SIZE;
215a62e84d7SBin Meng 		int i;
216a62e84d7SBin Meng 
217a62e84d7SBin Meng 		for (i = 0; i < num; i++) {
218a62e84d7SBin Meng 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
2194ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[0]),
2204ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[1]),
2214ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[2]));
2224ea5243aSStephen Warren 			if ((fdt32_to_cpu(*cell) & type) == type) {
2234ea5243aSStephen Warren 				addr->phys_hi = fdt32_to_cpu(cell[0]);
2244ea5243aSStephen Warren 				addr->phys_mid = fdt32_to_cpu(cell[1]);
2254ea5243aSStephen Warren 				addr->phys_lo = fdt32_to_cpu(cell[1]);
226a62e84d7SBin Meng 				break;
227a62e84d7SBin Meng 			} else {
228a62e84d7SBin Meng 				cell += (FDT_PCI_ADDR_CELLS +
229a62e84d7SBin Meng 					 FDT_PCI_SIZE_CELLS);
230a62e84d7SBin Meng 			}
231a62e84d7SBin Meng 		}
232a62e84d7SBin Meng 
233106cce96SSimon Glass 		if (i == num) {
234106cce96SSimon Glass 			ret = -ENXIO;
235a62e84d7SBin Meng 			goto fail;
236106cce96SSimon Glass 		}
237a62e84d7SBin Meng 
238a62e84d7SBin Meng 		return 0;
239a62e84d7SBin Meng 	} else {
240a62e84d7SBin Meng 		ret = -EINVAL;
241a62e84d7SBin Meng 	}
242a62e84d7SBin Meng 
243a62e84d7SBin Meng fail:
244a62e84d7SBin Meng 	debug("(not found)\n");
245a62e84d7SBin Meng 	return ret;
246a62e84d7SBin Meng }
247a62e84d7SBin Meng 
248a62e84d7SBin Meng int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
249a62e84d7SBin Meng {
250a62e84d7SBin Meng 	const char *list, *end;
251a62e84d7SBin Meng 	int len;
252a62e84d7SBin Meng 
253a62e84d7SBin Meng 	list = fdt_getprop(blob, node, "compatible", &len);
254a62e84d7SBin Meng 	if (!list)
255a62e84d7SBin Meng 		return -ENOENT;
256a62e84d7SBin Meng 
257a62e84d7SBin Meng 	end = list + len;
258a62e84d7SBin Meng 	while (list < end) {
259a62e84d7SBin Meng 		char *s;
260a62e84d7SBin Meng 
261a62e84d7SBin Meng 		len = strlen(list);
262a62e84d7SBin Meng 		if (len >= strlen("pciVVVV,DDDD")) {
263a62e84d7SBin Meng 			s = strstr(list, "pci");
264a62e84d7SBin Meng 
265a62e84d7SBin Meng 			/*
266a62e84d7SBin Meng 			 * check if the string is something like pciVVVV,DDDD.RR
267a62e84d7SBin Meng 			 * or just pciVVVV,DDDD
268a62e84d7SBin Meng 			 */
269a62e84d7SBin Meng 			if (s && s[7] == ',' &&
270a62e84d7SBin Meng 			    (s[12] == '.' || s[12] == 0)) {
271a62e84d7SBin Meng 				s += 3;
272a62e84d7SBin Meng 				*vendor = simple_strtol(s, NULL, 16);
273a62e84d7SBin Meng 
274a62e84d7SBin Meng 				s += 5;
275a62e84d7SBin Meng 				*device = simple_strtol(s, NULL, 16);
276a62e84d7SBin Meng 
277a62e84d7SBin Meng 				return 0;
278a62e84d7SBin Meng 			}
279a62e84d7SBin Meng 		}
280bc6351ebSBin Meng 		list += (len + 1);
281a62e84d7SBin Meng 	}
282a62e84d7SBin Meng 
283a62e84d7SBin Meng 	return -ENOENT;
284a62e84d7SBin Meng }
285a62e84d7SBin Meng 
286a62e84d7SBin Meng int fdtdec_get_pci_bdf(const void *blob, int node,
287a62e84d7SBin Meng 		struct fdt_pci_addr *addr, pci_dev_t *bdf)
288a62e84d7SBin Meng {
289a62e84d7SBin Meng 	u16 dt_vendor, dt_device, vendor, device;
290a62e84d7SBin Meng 	int ret;
291a62e84d7SBin Meng 
292a62e84d7SBin Meng 	/* get vendor id & device id from the compatible string */
293a62e84d7SBin Meng 	ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
294a62e84d7SBin Meng 	if (ret)
295a62e84d7SBin Meng 		return ret;
296a62e84d7SBin Meng 
297a62e84d7SBin Meng 	/* extract the bdf from fdt_pci_addr */
298a62e84d7SBin Meng 	*bdf = addr->phys_hi & 0xffff00;
299a62e84d7SBin Meng 
300a62e84d7SBin Meng 	/* read vendor id & device id based on bdf */
301a62e84d7SBin Meng 	pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
302a62e84d7SBin Meng 	pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
303a62e84d7SBin Meng 
304a62e84d7SBin Meng 	/*
305a62e84d7SBin Meng 	 * Note there are two places in the device tree to fully describe
306a62e84d7SBin Meng 	 * a pci device: one is via compatible string with a format of
307a62e84d7SBin Meng 	 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
308a62e84d7SBin Meng 	 * the device node's reg address property. We read the vendor id
309a62e84d7SBin Meng 	 * and device id based on bdf and compare the values with the
310a62e84d7SBin Meng 	 * "VVVV,DDDD". If they are the same, then we are good to use bdf
311a62e84d7SBin Meng 	 * to read device's bar. But if they are different, we have to rely
312a62e84d7SBin Meng 	 * on the vendor id and device id extracted from the compatible
313a62e84d7SBin Meng 	 * string and locate the real bdf by pci_find_device(). This is
314a62e84d7SBin Meng 	 * because normally we may only know device's device number and
315a62e84d7SBin Meng 	 * function number when writing device tree. The bus number is
316a62e84d7SBin Meng 	 * dynamically assigned during the pci enumeration process.
317a62e84d7SBin Meng 	 */
318a62e84d7SBin Meng 	if ((dt_vendor != vendor) || (dt_device != device)) {
319a62e84d7SBin Meng 		*bdf = pci_find_device(dt_vendor, dt_device, 0);
320a62e84d7SBin Meng 		if (*bdf == -1)
321a62e84d7SBin Meng 			return -ENODEV;
322a62e84d7SBin Meng 	}
323a62e84d7SBin Meng 
324a62e84d7SBin Meng 	return 0;
325a62e84d7SBin Meng }
326a62e84d7SBin Meng 
327a62e84d7SBin Meng int fdtdec_get_pci_bar32(const void *blob, int node,
328a62e84d7SBin Meng 		struct fdt_pci_addr *addr, u32 *bar)
329a62e84d7SBin Meng {
330a62e84d7SBin Meng 	pci_dev_t bdf;
331a62e84d7SBin Meng 	int barnum;
332a62e84d7SBin Meng 	int ret;
333a62e84d7SBin Meng 
334a62e84d7SBin Meng 	/* get pci devices's bdf */
335a62e84d7SBin Meng 	ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
336a62e84d7SBin Meng 	if (ret)
337a62e84d7SBin Meng 		return ret;
338a62e84d7SBin Meng 
339a62e84d7SBin Meng 	/* extract the bar number from fdt_pci_addr */
340a62e84d7SBin Meng 	barnum = addr->phys_hi & 0xff;
341a62e84d7SBin Meng 	if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
342a62e84d7SBin Meng 		return -EINVAL;
343a62e84d7SBin Meng 
344a62e84d7SBin Meng 	barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
345a62e84d7SBin Meng 	*bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
346a62e84d7SBin Meng 
347a62e84d7SBin Meng 	return 0;
348a62e84d7SBin Meng }
349a62e84d7SBin Meng #endif
350a62e84d7SBin Meng 
351aadef0a1SChe-Liang Chiou uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
352aadef0a1SChe-Liang Chiou 		uint64_t default_val)
353aadef0a1SChe-Liang Chiou {
354aadef0a1SChe-Liang Chiou 	const uint64_t *cell64;
355aadef0a1SChe-Liang Chiou 	int length;
356aadef0a1SChe-Liang Chiou 
357aadef0a1SChe-Liang Chiou 	cell64 = fdt_getprop(blob, node, prop_name, &length);
358aadef0a1SChe-Liang Chiou 	if (!cell64 || length < sizeof(*cell64))
359aadef0a1SChe-Liang Chiou 		return default_val;
360aadef0a1SChe-Liang Chiou 
361aadef0a1SChe-Liang Chiou 	return fdt64_to_cpu(*cell64);
362aadef0a1SChe-Liang Chiou }
363aadef0a1SChe-Liang Chiou 
364f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node)
365b5220bc6SSimon Glass {
366b5220bc6SSimon Glass 	const char *cell;
367b5220bc6SSimon Glass 
368f88fe2deSSimon Glass 	/*
369f88fe2deSSimon Glass 	 * It should say "okay", so only allow that. Some fdts use "ok" but
370f88fe2deSSimon Glass 	 * this is a bug. Please fix your device tree source file. See here
371f88fe2deSSimon Glass 	 * for discussion:
372f88fe2deSSimon Glass 	 *
373f88fe2deSSimon Glass 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
374f88fe2deSSimon Glass 	 */
375b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, "status", NULL);
376b5220bc6SSimon Glass 	if (cell)
377f88fe2deSSimon Glass 		return 0 == strcmp(cell, "okay");
378f88fe2deSSimon Glass 	return 1;
379b5220bc6SSimon Glass }
380b5220bc6SSimon Glass 
3817cde397bSGerald Van Baren enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
382b5220bc6SSimon Glass {
383b5220bc6SSimon Glass 	enum fdt_compat_id id;
384b5220bc6SSimon Glass 
385b5220bc6SSimon Glass 	/* Search our drivers */
386b5220bc6SSimon Glass 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
387b5220bc6SSimon Glass 		if (0 == fdt_node_check_compatible(blob, node,
388b5220bc6SSimon Glass 				compat_names[id]))
389b5220bc6SSimon Glass 			return id;
390b5220bc6SSimon Glass 	return COMPAT_UNKNOWN;
391b5220bc6SSimon Glass }
392b5220bc6SSimon Glass 
393b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node,
394b5220bc6SSimon Glass 		enum fdt_compat_id id)
395b5220bc6SSimon Glass {
396b5220bc6SSimon Glass 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
397b5220bc6SSimon Glass }
398b5220bc6SSimon Glass 
3993ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
4003ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp)
4013ddecfc7SSimon Glass {
4023ddecfc7SSimon Glass 	do {
4033ddecfc7SSimon Glass 		node = fdt_next_node(blob, node, depthp);
4043ddecfc7SSimon Glass 	} while (*depthp > 1);
4053ddecfc7SSimon Glass 
4063ddecfc7SSimon Glass 	/* If this is a direct subnode, and compatible, return it */
4073ddecfc7SSimon Glass 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
4083ddecfc7SSimon Glass 						blob, node, compat_names[id]))
4093ddecfc7SSimon Glass 		return node;
4103ddecfc7SSimon Glass 
4113ddecfc7SSimon Glass 	return -FDT_ERR_NOTFOUND;
4123ddecfc7SSimon Glass }
4133ddecfc7SSimon Glass 
414b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
415b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto)
416b5220bc6SSimon Glass {
417b5220bc6SSimon Glass #define MAX_STR_LEN 20
418b5220bc6SSimon Glass 	char str[MAX_STR_LEN + 20];
419b5220bc6SSimon Glass 	int node, err;
420b5220bc6SSimon Glass 
421b5220bc6SSimon Glass 	/* snprintf() is not available */
422b5220bc6SSimon Glass 	assert(strlen(name) < MAX_STR_LEN);
423b5220bc6SSimon Glass 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
42400878476SSimon Glass 	node = fdt_path_offset(blob, str);
425b5220bc6SSimon Glass 	if (node < 0)
426b5220bc6SSimon Glass 		return node;
427b5220bc6SSimon Glass 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
428b5220bc6SSimon Glass 	if (err < 0)
429b5220bc6SSimon Glass 		return err;
430f88fe2deSSimon Glass 	if (err)
431f88fe2deSSimon Glass 		return -FDT_ERR_NOTFOUND;
432f88fe2deSSimon Glass 	(*upto)++;
433f88fe2deSSimon Glass 	return node;
434b5220bc6SSimon Glass }
435b5220bc6SSimon Glass 
436a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
437a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
438a53f4a29SSimon Glass {
439c6782270SSimon Glass 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
440c6782270SSimon Glass 
441c6782270SSimon Glass 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
442c6782270SSimon Glass }
443c6782270SSimon Glass 
444c6782270SSimon Glass /* TODO: Can we tighten this code up a little? */
445c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
446c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
447c6782270SSimon Glass {
448a53f4a29SSimon Glass 	int name_len = strlen(name);
449a53f4a29SSimon Glass 	int nodes[maxcount];
450a53f4a29SSimon Glass 	int num_found = 0;
451a53f4a29SSimon Glass 	int offset, node;
452a53f4a29SSimon Glass 	int alias_node;
453a53f4a29SSimon Glass 	int count;
454a53f4a29SSimon Glass 	int i, j;
455a53f4a29SSimon Glass 
456a53f4a29SSimon Glass 	/* find the alias node if present */
457a53f4a29SSimon Glass 	alias_node = fdt_path_offset(blob, "/aliases");
458a53f4a29SSimon Glass 
459a53f4a29SSimon Glass 	/*
460a53f4a29SSimon Glass 	 * start with nothing, and we can assume that the root node can't
461a53f4a29SSimon Glass 	 * match
462a53f4a29SSimon Glass 	 */
463a53f4a29SSimon Glass 	memset(nodes, '\0', sizeof(nodes));
464a53f4a29SSimon Glass 
465a53f4a29SSimon Glass 	/* First find all the compatible nodes */
466a53f4a29SSimon Glass 	for (node = count = 0; node >= 0 && count < maxcount;) {
467a53f4a29SSimon Glass 		node = fdtdec_next_compatible(blob, node, id);
468a53f4a29SSimon Glass 		if (node >= 0)
469a53f4a29SSimon Glass 			nodes[count++] = node;
470a53f4a29SSimon Glass 	}
471a53f4a29SSimon Glass 	if (node >= 0)
472a53f4a29SSimon Glass 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
473a53f4a29SSimon Glass 		       __func__, name);
474a53f4a29SSimon Glass 
475a53f4a29SSimon Glass 	/* Now find all the aliases */
476a53f4a29SSimon Glass 	for (offset = fdt_first_property_offset(blob, alias_node);
477a53f4a29SSimon Glass 			offset > 0;
478a53f4a29SSimon Glass 			offset = fdt_next_property_offset(blob, offset)) {
479a53f4a29SSimon Glass 		const struct fdt_property *prop;
480a53f4a29SSimon Glass 		const char *path;
481a53f4a29SSimon Glass 		int number;
482a53f4a29SSimon Glass 		int found;
483a53f4a29SSimon Glass 
484a53f4a29SSimon Glass 		node = 0;
485a53f4a29SSimon Glass 		prop = fdt_get_property_by_offset(blob, offset, NULL);
486a53f4a29SSimon Glass 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
487a53f4a29SSimon Glass 		if (prop->len && 0 == strncmp(path, name, name_len))
488a53f4a29SSimon Glass 			node = fdt_path_offset(blob, prop->data);
489a53f4a29SSimon Glass 		if (node <= 0)
490a53f4a29SSimon Glass 			continue;
491a53f4a29SSimon Glass 
492a53f4a29SSimon Glass 		/* Get the alias number */
493a53f4a29SSimon Glass 		number = simple_strtoul(path + name_len, NULL, 10);
494a53f4a29SSimon Glass 		if (number < 0 || number >= maxcount) {
495a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' is out of range\n",
496a53f4a29SSimon Glass 			       __func__, path);
497a53f4a29SSimon Glass 			continue;
498a53f4a29SSimon Glass 		}
499a53f4a29SSimon Glass 
500a53f4a29SSimon Glass 		/* Make sure the node we found is actually in our list! */
501a53f4a29SSimon Glass 		found = -1;
502a53f4a29SSimon Glass 		for (j = 0; j < count; j++)
503a53f4a29SSimon Glass 			if (nodes[j] == node) {
504a53f4a29SSimon Glass 				found = j;
505a53f4a29SSimon Glass 				break;
506a53f4a29SSimon Glass 			}
507a53f4a29SSimon Glass 
508a53f4a29SSimon Glass 		if (found == -1) {
509a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' points to a node "
510a53f4a29SSimon Glass 				"'%s' that is missing or is not compatible "
511a53f4a29SSimon Glass 				" with '%s'\n", __func__, path,
512a53f4a29SSimon Glass 				fdt_get_name(blob, node, NULL),
513a53f4a29SSimon Glass 			       compat_names[id]);
514a53f4a29SSimon Glass 			continue;
515a53f4a29SSimon Glass 		}
516a53f4a29SSimon Glass 
517a53f4a29SSimon Glass 		/*
518a53f4a29SSimon Glass 		 * Add this node to our list in the right place, and mark
519a53f4a29SSimon Glass 		 * it as done.
520a53f4a29SSimon Glass 		 */
521a53f4a29SSimon Glass 		if (fdtdec_get_is_enabled(blob, node)) {
522c6782270SSimon Glass 			if (node_list[number]) {
523c6782270SSimon Glass 				debug("%s: warning: alias '%s' requires that "
524c6782270SSimon Glass 				      "a node be placed in the list in a "
525c6782270SSimon Glass 				      "position which is already filled by "
526c6782270SSimon Glass 				      "node '%s'\n", __func__, path,
527c6782270SSimon Glass 				      fdt_get_name(blob, node, NULL));
528c6782270SSimon Glass 				continue;
529c6782270SSimon Glass 			}
530a53f4a29SSimon Glass 			node_list[number] = node;
531a53f4a29SSimon Glass 			if (number >= num_found)
532a53f4a29SSimon Glass 				num_found = number + 1;
533a53f4a29SSimon Glass 		}
534c6782270SSimon Glass 		nodes[found] = 0;
535a53f4a29SSimon Glass 	}
536a53f4a29SSimon Glass 
537a53f4a29SSimon Glass 	/* Add any nodes not mentioned by an alias */
538a53f4a29SSimon Glass 	for (i = j = 0; i < maxcount; i++) {
539a53f4a29SSimon Glass 		if (!node_list[i]) {
540a53f4a29SSimon Glass 			for (; j < maxcount; j++)
541a53f4a29SSimon Glass 				if (nodes[j] &&
542a53f4a29SSimon Glass 					fdtdec_get_is_enabled(blob, nodes[j]))
543a53f4a29SSimon Glass 					break;
544a53f4a29SSimon Glass 
545a53f4a29SSimon Glass 			/* Have we run out of nodes to add? */
546a53f4a29SSimon Glass 			if (j == maxcount)
547a53f4a29SSimon Glass 				break;
548a53f4a29SSimon Glass 
549a53f4a29SSimon Glass 			assert(!node_list[i]);
550a53f4a29SSimon Glass 			node_list[i] = nodes[j++];
551a53f4a29SSimon Glass 			if (i >= num_found)
552a53f4a29SSimon Glass 				num_found = i + 1;
553a53f4a29SSimon Glass 		}
554a53f4a29SSimon Glass 	}
555a53f4a29SSimon Glass 
556a53f4a29SSimon Glass 	return num_found;
557a53f4a29SSimon Glass }
558a53f4a29SSimon Glass 
5595c33c9fdSSimon Glass int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
5605c33c9fdSSimon Glass 			 int *seqp)
5615c33c9fdSSimon Glass {
5625c33c9fdSSimon Glass 	int base_len = strlen(base);
5635c33c9fdSSimon Glass 	const char *find_name;
5645c33c9fdSSimon Glass 	int find_namelen;
5655c33c9fdSSimon Glass 	int prop_offset;
5665c33c9fdSSimon Glass 	int aliases;
5675c33c9fdSSimon Glass 
5685c33c9fdSSimon Glass 	find_name = fdt_get_name(blob, offset, &find_namelen);
5695c33c9fdSSimon Glass 	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
5705c33c9fdSSimon Glass 
5715c33c9fdSSimon Glass 	aliases = fdt_path_offset(blob, "/aliases");
5725c33c9fdSSimon Glass 	for (prop_offset = fdt_first_property_offset(blob, aliases);
5735c33c9fdSSimon Glass 	     prop_offset > 0;
5745c33c9fdSSimon Glass 	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
5755c33c9fdSSimon Glass 		const char *prop;
5765c33c9fdSSimon Glass 		const char *name;
5775c33c9fdSSimon Glass 		const char *slash;
578c4af6732SSimon Glass 		int len, val;
5795c33c9fdSSimon Glass 
5805c33c9fdSSimon Glass 		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
5815c33c9fdSSimon Glass 		debug("   - %s, %s\n", name, prop);
5825c33c9fdSSimon Glass 		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
5835c33c9fdSSimon Glass 		    strncmp(name, base, base_len))
5845c33c9fdSSimon Glass 			continue;
5855c33c9fdSSimon Glass 
5865c33c9fdSSimon Glass 		slash = strrchr(prop, '/');
5875c33c9fdSSimon Glass 		if (strcmp(slash + 1, find_name))
5885c33c9fdSSimon Glass 			continue;
589c4af6732SSimon Glass 		val = trailing_strtol(name);
590c4af6732SSimon Glass 		if (val != -1) {
591c4af6732SSimon Glass 			*seqp = val;
5925c33c9fdSSimon Glass 			debug("Found seq %d\n", *seqp);
5935c33c9fdSSimon Glass 			return 0;
5945c33c9fdSSimon Glass 		}
5955c33c9fdSSimon Glass 	}
5965c33c9fdSSimon Glass 
5975c33c9fdSSimon Glass 	debug("Not found\n");
5985c33c9fdSSimon Glass 	return -ENOENT;
5995c33c9fdSSimon Glass }
6005c33c9fdSSimon Glass 
6013bc37a50SSimon Glass const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
6023bc37a50SSimon Glass {
6033bc37a50SSimon Glass 	int chosen_node;
6043bc37a50SSimon Glass 
6053bc37a50SSimon Glass 	if (!blob)
6063bc37a50SSimon Glass 		return NULL;
6073bc37a50SSimon Glass 	chosen_node = fdt_path_offset(blob, "/chosen");
6083bc37a50SSimon Glass 	return fdt_getprop(blob, chosen_node, name, NULL);
6093bc37a50SSimon Glass }
6103bc37a50SSimon Glass 
611aac07d49SSimon Glass int fdtdec_get_chosen_node(const void *blob, const char *name)
612aac07d49SSimon Glass {
613aac07d49SSimon Glass 	const char *prop;
614aac07d49SSimon Glass 
6153bc37a50SSimon Glass 	prop = fdtdec_get_chosen_prop(blob, name);
616aac07d49SSimon Glass 	if (!prop)
617aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
618aac07d49SSimon Glass 	return fdt_path_offset(blob, prop);
619aac07d49SSimon Glass }
620aac07d49SSimon Glass 
6219a263e55SSimon Glass int fdtdec_check_fdt(void)
6229a263e55SSimon Glass {
6239a263e55SSimon Glass 	/*
6249a263e55SSimon Glass 	 * We must have an FDT, but we cannot panic() yet since the console
6259a263e55SSimon Glass 	 * is not ready. So for now, just assert(). Boards which need an early
6269a263e55SSimon Glass 	 * FDT (prior to console ready) will need to make their own
6279a263e55SSimon Glass 	 * arrangements and do their own checks.
6289a263e55SSimon Glass 	 */
6299a263e55SSimon Glass 	assert(!fdtdec_prepare_fdt());
6309a263e55SSimon Glass 	return 0;
6319a263e55SSimon Glass }
6329a263e55SSimon Glass 
633b5220bc6SSimon Glass /*
634b5220bc6SSimon Glass  * This function is a little odd in that it accesses global data. At some
635b5220bc6SSimon Glass  * point if the architecture board.c files merge this will make more sense.
636b5220bc6SSimon Glass  * Even now, it is common code.
637b5220bc6SSimon Glass  */
6389a263e55SSimon Glass int fdtdec_prepare_fdt(void)
639b5220bc6SSimon Glass {
640c309c2daSSimon Glass 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
641c309c2daSSimon Glass 	    fdt_check_header(gd->fdt_blob)) {
64266312374SSimon Glass #ifdef CONFIG_SPL_BUILD
64366312374SSimon Glass 		puts("Missing DTB\n");
64466312374SSimon Glass #else
64566312374SSimon Glass 		puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
646cb5f97f7SSimon Glass # ifdef DEBUG
647cb5f97f7SSimon Glass 		if (gd->fdt_blob) {
648cb5f97f7SSimon Glass 			printf("fdt_blob=%p\n", gd->fdt_blob);
649cb5f97f7SSimon Glass 			print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
650cb5f97f7SSimon Glass 				     32, 0);
651cb5f97f7SSimon Glass 		}
652cb5f97f7SSimon Glass # endif
65366312374SSimon Glass #endif
6549a263e55SSimon Glass 		return -1;
6559a263e55SSimon Glass 	}
656b5220bc6SSimon Glass 	return 0;
657b5220bc6SSimon Glass }
658d17da655SSimon Glass 
659d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
660d17da655SSimon Glass {
661d17da655SSimon Glass 	const u32 *phandle;
662d17da655SSimon Glass 	int lookup;
663d17da655SSimon Glass 
6641cb2323bSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
665d17da655SSimon Glass 	phandle = fdt_getprop(blob, node, prop_name, NULL);
666d17da655SSimon Glass 	if (!phandle)
667d17da655SSimon Glass 		return -FDT_ERR_NOTFOUND;
668d17da655SSimon Glass 
669d17da655SSimon Glass 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
670d17da655SSimon Glass 	return lookup;
671d17da655SSimon Glass }
672d17da655SSimon Glass 
673d17da655SSimon Glass /**
674d17da655SSimon Glass  * Look up a property in a node and check that it has a minimum length.
675d17da655SSimon Glass  *
676d17da655SSimon Glass  * @param blob		FDT blob
677d17da655SSimon Glass  * @param node		node to examine
678d17da655SSimon Glass  * @param prop_name	name of property to find
679d17da655SSimon Glass  * @param min_len	minimum property length in bytes
680d17da655SSimon Glass  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
681d17da655SSimon Glass 			found, or -FDT_ERR_BADLAYOUT if not enough data
682d17da655SSimon Glass  * @return pointer to cell, which is only valid if err == 0
683d17da655SSimon Glass  */
684d17da655SSimon Glass static const void *get_prop_check_min_len(const void *blob, int node,
685d17da655SSimon Glass 		const char *prop_name, int min_len, int *err)
686d17da655SSimon Glass {
687d17da655SSimon Glass 	const void *cell;
688d17da655SSimon Glass 	int len;
689d17da655SSimon Glass 
690d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
691d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
692d17da655SSimon Glass 	if (!cell)
693d17da655SSimon Glass 		*err = -FDT_ERR_NOTFOUND;
694d17da655SSimon Glass 	else if (len < min_len)
695d17da655SSimon Glass 		*err = -FDT_ERR_BADLAYOUT;
696d17da655SSimon Glass 	else
697d17da655SSimon Glass 		*err = 0;
698d17da655SSimon Glass 	return cell;
699d17da655SSimon Glass }
700d17da655SSimon Glass 
701d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
702d17da655SSimon Glass 		u32 *array, int count)
703d17da655SSimon Glass {
704d17da655SSimon Glass 	const u32 *cell;
705d17da655SSimon Glass 	int i, err = 0;
706d17da655SSimon Glass 
707d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
708d17da655SSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
709d17da655SSimon Glass 				      sizeof(u32) * count, &err);
710d17da655SSimon Glass 	if (!err) {
711d17da655SSimon Glass 		for (i = 0; i < count; i++)
712d17da655SSimon Glass 			array[i] = fdt32_to_cpu(cell[i]);
713d17da655SSimon Glass 	}
714d17da655SSimon Glass 	return err;
715d17da655SSimon Glass }
716d17da655SSimon Glass 
717a9f04d49SSimon Glass int fdtdec_get_int_array_count(const void *blob, int node,
718a9f04d49SSimon Glass 			       const char *prop_name, u32 *array, int count)
719a9f04d49SSimon Glass {
720a9f04d49SSimon Glass 	const u32 *cell;
721a9f04d49SSimon Glass 	int len, elems;
722a9f04d49SSimon Glass 	int i;
723a9f04d49SSimon Glass 
724a9f04d49SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
725a9f04d49SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
726a9f04d49SSimon Glass 	if (!cell)
727a9f04d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
728a9f04d49SSimon Glass 	elems = len / sizeof(u32);
729a9f04d49SSimon Glass 	if (count > elems)
730a9f04d49SSimon Glass 		count = elems;
731a9f04d49SSimon Glass 	for (i = 0; i < count; i++)
732a9f04d49SSimon Glass 		array[i] = fdt32_to_cpu(cell[i]);
733a9f04d49SSimon Glass 
734a9f04d49SSimon Glass 	return count;
735a9f04d49SSimon Glass }
736a9f04d49SSimon Glass 
73796875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
73896875e7dSSimon Glass 			       const char *prop_name, int count)
73996875e7dSSimon Glass {
74096875e7dSSimon Glass 	const u32 *cell;
74196875e7dSSimon Glass 	int err;
74296875e7dSSimon Glass 
74396875e7dSSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
74496875e7dSSimon Glass 				      sizeof(u32) * count, &err);
74596875e7dSSimon Glass 	return err ? NULL : cell;
74696875e7dSSimon Glass }
74796875e7dSSimon Glass 
748d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
749d17da655SSimon Glass {
750d17da655SSimon Glass 	const s32 *cell;
751d17da655SSimon Glass 	int len;
752d17da655SSimon Glass 
753d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
754d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
755d17da655SSimon Glass 	return cell != NULL;
756d17da655SSimon Glass }
757ed3ee5cdSSimon Glass 
75857068a7aSSimon Glass int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
75957068a7aSSimon Glass 				   const char *list_name,
76057068a7aSSimon Glass 				   const char *cells_name,
76157068a7aSSimon Glass 				   int cell_count, int index,
76257068a7aSSimon Glass 				   struct fdtdec_phandle_args *out_args)
76357068a7aSSimon Glass {
76457068a7aSSimon Glass 	const __be32 *list, *list_end;
76557068a7aSSimon Glass 	int rc = 0, size, cur_index = 0;
76657068a7aSSimon Glass 	uint32_t count = 0;
76757068a7aSSimon Glass 	int node = -1;
76857068a7aSSimon Glass 	int phandle;
76957068a7aSSimon Glass 
77057068a7aSSimon Glass 	/* Retrieve the phandle list property */
77157068a7aSSimon Glass 	list = fdt_getprop(blob, src_node, list_name, &size);
77257068a7aSSimon Glass 	if (!list)
77357068a7aSSimon Glass 		return -ENOENT;
77457068a7aSSimon Glass 	list_end = list + size / sizeof(*list);
77557068a7aSSimon Glass 
77657068a7aSSimon Glass 	/* Loop over the phandles until all the requested entry is found */
77757068a7aSSimon Glass 	while (list < list_end) {
77857068a7aSSimon Glass 		rc = -EINVAL;
77957068a7aSSimon Glass 		count = 0;
78057068a7aSSimon Glass 
78157068a7aSSimon Glass 		/*
78257068a7aSSimon Glass 		 * If phandle is 0, then it is an empty entry with no
78357068a7aSSimon Glass 		 * arguments.  Skip forward to the next entry.
78457068a7aSSimon Glass 		 */
78557068a7aSSimon Glass 		phandle = be32_to_cpup(list++);
78657068a7aSSimon Glass 		if (phandle) {
78757068a7aSSimon Glass 			/*
78857068a7aSSimon Glass 			 * Find the provider node and parse the #*-cells
78957068a7aSSimon Glass 			 * property to determine the argument length.
79057068a7aSSimon Glass 			 *
79157068a7aSSimon Glass 			 * This is not needed if the cell count is hard-coded
79257068a7aSSimon Glass 			 * (i.e. cells_name not set, but cell_count is set),
79357068a7aSSimon Glass 			 * except when we're going to return the found node
79457068a7aSSimon Glass 			 * below.
79557068a7aSSimon Glass 			 */
79657068a7aSSimon Glass 			if (cells_name || cur_index == index) {
79757068a7aSSimon Glass 				node = fdt_node_offset_by_phandle(blob,
79857068a7aSSimon Glass 								  phandle);
79957068a7aSSimon Glass 				if (!node) {
80057068a7aSSimon Glass 					debug("%s: could not find phandle\n",
80157068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
80257068a7aSSimon Glass 							   NULL));
80357068a7aSSimon Glass 					goto err;
80457068a7aSSimon Glass 				}
80557068a7aSSimon Glass 			}
80657068a7aSSimon Glass 
80757068a7aSSimon Glass 			if (cells_name) {
80857068a7aSSimon Glass 				count = fdtdec_get_int(blob, node, cells_name,
80957068a7aSSimon Glass 						       -1);
81057068a7aSSimon Glass 				if (count == -1) {
81157068a7aSSimon Glass 					debug("%s: could not get %s for %s\n",
81257068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
81357068a7aSSimon Glass 							   NULL),
81457068a7aSSimon Glass 					      cells_name,
81557068a7aSSimon Glass 					      fdt_get_name(blob, node,
81657068a7aSSimon Glass 							   NULL));
81757068a7aSSimon Glass 					goto err;
81857068a7aSSimon Glass 				}
81957068a7aSSimon Glass 			} else {
82057068a7aSSimon Glass 				count = cell_count;
82157068a7aSSimon Glass 			}
82257068a7aSSimon Glass 
82357068a7aSSimon Glass 			/*
82457068a7aSSimon Glass 			 * Make sure that the arguments actually fit in the
82557068a7aSSimon Glass 			 * remaining property data length
82657068a7aSSimon Glass 			 */
82757068a7aSSimon Glass 			if (list + count > list_end) {
82857068a7aSSimon Glass 				debug("%s: arguments longer than property\n",
82957068a7aSSimon Glass 				      fdt_get_name(blob, src_node, NULL));
83057068a7aSSimon Glass 				goto err;
83157068a7aSSimon Glass 			}
83257068a7aSSimon Glass 		}
83357068a7aSSimon Glass 
83457068a7aSSimon Glass 		/*
83557068a7aSSimon Glass 		 * All of the error cases above bail out of the loop, so at
83657068a7aSSimon Glass 		 * this point, the parsing is successful. If the requested
83757068a7aSSimon Glass 		 * index matches, then fill the out_args structure and return,
83857068a7aSSimon Glass 		 * or return -ENOENT for an empty entry.
83957068a7aSSimon Glass 		 */
84057068a7aSSimon Glass 		rc = -ENOENT;
84157068a7aSSimon Glass 		if (cur_index == index) {
84257068a7aSSimon Glass 			if (!phandle)
84357068a7aSSimon Glass 				goto err;
84457068a7aSSimon Glass 
84557068a7aSSimon Glass 			if (out_args) {
84657068a7aSSimon Glass 				int i;
84757068a7aSSimon Glass 
84857068a7aSSimon Glass 				if (count > MAX_PHANDLE_ARGS) {
84957068a7aSSimon Glass 					debug("%s: too many arguments %d\n",
85057068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
85157068a7aSSimon Glass 							   NULL), count);
85257068a7aSSimon Glass 					count = MAX_PHANDLE_ARGS;
85357068a7aSSimon Glass 				}
85457068a7aSSimon Glass 				out_args->node = node;
85557068a7aSSimon Glass 				out_args->args_count = count;
85657068a7aSSimon Glass 				for (i = 0; i < count; i++) {
85757068a7aSSimon Glass 					out_args->args[i] =
85857068a7aSSimon Glass 							be32_to_cpup(list++);
85957068a7aSSimon Glass 				}
86057068a7aSSimon Glass 			}
86157068a7aSSimon Glass 
86257068a7aSSimon Glass 			/* Found it! return success */
86357068a7aSSimon Glass 			return 0;
86457068a7aSSimon Glass 		}
86557068a7aSSimon Glass 
86657068a7aSSimon Glass 		node = -1;
86757068a7aSSimon Glass 		list += count;
86857068a7aSSimon Glass 		cur_index++;
86957068a7aSSimon Glass 	}
87057068a7aSSimon Glass 
87157068a7aSSimon Glass 	/*
87257068a7aSSimon Glass 	 * Result will be one of:
87357068a7aSSimon Glass 	 * -ENOENT : index is for empty phandle
87457068a7aSSimon Glass 	 * -EINVAL : parsing error on data
87557068a7aSSimon Glass 	 * [1..n]  : Number of phandle (count mode; when index = -1)
87657068a7aSSimon Glass 	 */
87757068a7aSSimon Glass 	rc = index < 0 ? cur_index : -ENOENT;
87857068a7aSSimon Glass  err:
87957068a7aSSimon Glass 	return rc;
88057068a7aSSimon Glass }
88157068a7aSSimon Glass 
882bed4d892SAnton Staff int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
883bed4d892SAnton Staff 		u8 *array, int count)
884bed4d892SAnton Staff {
885bed4d892SAnton Staff 	const u8 *cell;
886bed4d892SAnton Staff 	int err;
887bed4d892SAnton Staff 
888bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
889bed4d892SAnton Staff 	if (!err)
890bed4d892SAnton Staff 		memcpy(array, cell, count);
891bed4d892SAnton Staff 	return err;
892bed4d892SAnton Staff }
893bed4d892SAnton Staff 
894bed4d892SAnton Staff const u8 *fdtdec_locate_byte_array(const void *blob, int node,
895bed4d892SAnton Staff 			     const char *prop_name, int count)
896bed4d892SAnton Staff {
897bed4d892SAnton Staff 	const u8 *cell;
898bed4d892SAnton Staff 	int err;
899bed4d892SAnton Staff 
900bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
901bed4d892SAnton Staff 	if (err)
902bed4d892SAnton Staff 		return NULL;
903bed4d892SAnton Staff 	return cell;
904bed4d892SAnton Staff }
90509258f1eSAbhilash Kesavan 
90609258f1eSAbhilash Kesavan int fdtdec_get_config_int(const void *blob, const char *prop_name,
90709258f1eSAbhilash Kesavan 		int default_val)
90809258f1eSAbhilash Kesavan {
90909258f1eSAbhilash Kesavan 	int config_node;
91009258f1eSAbhilash Kesavan 
91109258f1eSAbhilash Kesavan 	debug("%s: %s\n", __func__, prop_name);
91209258f1eSAbhilash Kesavan 	config_node = fdt_path_offset(blob, "/config");
91309258f1eSAbhilash Kesavan 	if (config_node < 0)
91409258f1eSAbhilash Kesavan 		return default_val;
91509258f1eSAbhilash Kesavan 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
91609258f1eSAbhilash Kesavan }
917332ab0d5SSimon Glass 
91879289c0bSGabe Black int fdtdec_get_config_bool(const void *blob, const char *prop_name)
91979289c0bSGabe Black {
92079289c0bSGabe Black 	int config_node;
92179289c0bSGabe Black 	const void *prop;
92279289c0bSGabe Black 
92379289c0bSGabe Black 	debug("%s: %s\n", __func__, prop_name);
92479289c0bSGabe Black 	config_node = fdt_path_offset(blob, "/config");
92579289c0bSGabe Black 	if (config_node < 0)
92679289c0bSGabe Black 		return 0;
92779289c0bSGabe Black 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
92879289c0bSGabe Black 
92979289c0bSGabe Black 	return prop != NULL;
93079289c0bSGabe Black }
93179289c0bSGabe Black 
932332ab0d5SSimon Glass char *fdtdec_get_config_string(const void *blob, const char *prop_name)
933332ab0d5SSimon Glass {
934332ab0d5SSimon Glass 	const char *nodep;
935332ab0d5SSimon Glass 	int nodeoffset;
936332ab0d5SSimon Glass 	int len;
937332ab0d5SSimon Glass 
938332ab0d5SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
939332ab0d5SSimon Glass 	nodeoffset = fdt_path_offset(blob, "/config");
940332ab0d5SSimon Glass 	if (nodeoffset < 0)
941332ab0d5SSimon Glass 		return NULL;
942332ab0d5SSimon Glass 
943332ab0d5SSimon Glass 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
944332ab0d5SSimon Glass 	if (!nodep)
945332ab0d5SSimon Glass 		return NULL;
946332ab0d5SSimon Glass 
947332ab0d5SSimon Glass 	return (char *)nodep;
948332ab0d5SSimon Glass }
949f20c4619SSimon Glass 
95076489832SSimon Glass int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
95176489832SSimon Glass 			 fdt_addr_t *basep, fdt_size_t *sizep)
952f20c4619SSimon Glass {
953f20c4619SSimon Glass 	const fdt_addr_t *cell;
954f20c4619SSimon Glass 	int len;
955f20c4619SSimon Glass 
95676489832SSimon Glass 	debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
95776489832SSimon Glass 	      prop_name);
958f20c4619SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
95976489832SSimon Glass 	if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
96076489832SSimon Glass 		debug("cell=%p, len=%d\n", cell, len);
961f20c4619SSimon Glass 		return -1;
96276489832SSimon Glass 	}
963f20c4619SSimon Glass 
96476489832SSimon Glass 	*basep = fdt_addr_to_cpu(*cell);
96576489832SSimon Glass 	*sizep = fdt_size_to_cpu(cell[1]);
96676489832SSimon Glass 	debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
96776489832SSimon Glass 	      (ulong)*sizep);
96876489832SSimon Glass 
969f20c4619SSimon Glass 	return 0;
970f20c4619SSimon Glass }
971006e73b9SSimon Glass 
972006e73b9SSimon Glass /**
973006e73b9SSimon Glass  * Read a flash entry from the fdt
974006e73b9SSimon Glass  *
975006e73b9SSimon Glass  * @param blob		FDT blob
976006e73b9SSimon Glass  * @param node		Offset of node to read
977006e73b9SSimon Glass  * @param name		Name of node being read
978006e73b9SSimon Glass  * @param entry		Place to put offset and size of this node
979006e73b9SSimon Glass  * @return 0 if ok, -ve on error
980006e73b9SSimon Glass  */
981006e73b9SSimon Glass int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
982006e73b9SSimon Glass 			   struct fmap_entry *entry)
983006e73b9SSimon Glass {
984f3cc44f9SSimon Glass 	const char *prop;
985006e73b9SSimon Glass 	u32 reg[2];
986006e73b9SSimon Glass 
987006e73b9SSimon Glass 	if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
988006e73b9SSimon Glass 		debug("Node '%s' has bad/missing 'reg' property\n", name);
989006e73b9SSimon Glass 		return -FDT_ERR_NOTFOUND;
990006e73b9SSimon Glass 	}
991006e73b9SSimon Glass 	entry->offset = reg[0];
992006e73b9SSimon Glass 	entry->length = reg[1];
993f3cc44f9SSimon Glass 	entry->used = fdtdec_get_int(blob, node, "used", entry->length);
994f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "compress", NULL);
995f3cc44f9SSimon Glass 	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
996f3cc44f9SSimon Glass 		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
997f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
998f3cc44f9SSimon Glass 	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
999f3cc44f9SSimon Glass 	entry->hash = (uint8_t *)prop;
1000006e73b9SSimon Glass 
1001006e73b9SSimon Glass 	return 0;
1002006e73b9SSimon Glass }
100356f42242SThierry Reding 
10045f7bfdd6SSimon Glass u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
100556f42242SThierry Reding {
100656f42242SThierry Reding 	u64 number = 0;
100756f42242SThierry Reding 
100856f42242SThierry Reding 	while (cells--)
100956f42242SThierry Reding 		number = (number << 32) | fdt32_to_cpu(*ptr++);
101056f42242SThierry Reding 
101156f42242SThierry Reding 	return number;
101256f42242SThierry Reding }
101356f42242SThierry Reding 
101456f42242SThierry Reding int fdt_get_resource(const void *fdt, int node, const char *property,
101556f42242SThierry Reding 		     unsigned int index, struct fdt_resource *res)
101656f42242SThierry Reding {
101756f42242SThierry Reding 	const fdt32_t *ptr, *end;
101856f42242SThierry Reding 	int na, ns, len, parent;
101956f42242SThierry Reding 	unsigned int i = 0;
102056f42242SThierry Reding 
102156f42242SThierry Reding 	parent = fdt_parent_offset(fdt, node);
102256f42242SThierry Reding 	if (parent < 0)
102356f42242SThierry Reding 		return parent;
102456f42242SThierry Reding 
102556f42242SThierry Reding 	na = fdt_address_cells(fdt, parent);
102656f42242SThierry Reding 	ns = fdt_size_cells(fdt, parent);
102756f42242SThierry Reding 
102856f42242SThierry Reding 	ptr = fdt_getprop(fdt, node, property, &len);
102956f42242SThierry Reding 	if (!ptr)
103056f42242SThierry Reding 		return len;
103156f42242SThierry Reding 
103256f42242SThierry Reding 	end = ptr + len / sizeof(*ptr);
103356f42242SThierry Reding 
103456f42242SThierry Reding 	while (ptr + na + ns <= end) {
103556f42242SThierry Reding 		if (i == index) {
103656f42242SThierry Reding 			res->start = res->end = fdtdec_get_number(ptr, na);
103756f42242SThierry Reding 			res->end += fdtdec_get_number(&ptr[na], ns) - 1;
103856f42242SThierry Reding 			return 0;
103956f42242SThierry Reding 		}
104056f42242SThierry Reding 
104156f42242SThierry Reding 		ptr += na + ns;
104256f42242SThierry Reding 		i++;
104356f42242SThierry Reding 	}
104456f42242SThierry Reding 
104556f42242SThierry Reding 	return -FDT_ERR_NOTFOUND;
104656f42242SThierry Reding }
104756f42242SThierry Reding 
104856f42242SThierry Reding int fdt_get_named_resource(const void *fdt, int node, const char *property,
104956f42242SThierry Reding 			   const char *prop_names, const char *name,
105056f42242SThierry Reding 			   struct fdt_resource *res)
105156f42242SThierry Reding {
105256f42242SThierry Reding 	int index;
105356f42242SThierry Reding 
105456f42242SThierry Reding 	index = fdt_find_string(fdt, node, prop_names, name);
105556f42242SThierry Reding 	if (index < 0)
105656f42242SThierry Reding 		return index;
105756f42242SThierry Reding 
105856f42242SThierry Reding 	return fdt_get_resource(fdt, node, property, index, res);
105956f42242SThierry Reding }
10609f85eee7SThierry Reding 
106126403871SSimon Glass int fdtdec_decode_memory_region(const void *blob, int config_node,
106226403871SSimon Glass 				const char *mem_type, const char *suffix,
106326403871SSimon Glass 				fdt_addr_t *basep, fdt_size_t *sizep)
106426403871SSimon Glass {
106526403871SSimon Glass 	char prop_name[50];
106626403871SSimon Glass 	const char *mem;
106726403871SSimon Glass 	fdt_size_t size, offset_size;
106826403871SSimon Glass 	fdt_addr_t base, offset;
106926403871SSimon Glass 	int node;
107026403871SSimon Glass 
107126403871SSimon Glass 	if (config_node == -1) {
107226403871SSimon Glass 		config_node = fdt_path_offset(blob, "/config");
107326403871SSimon Glass 		if (config_node < 0) {
107426403871SSimon Glass 			debug("%s: Cannot find /config node\n", __func__);
107526403871SSimon Glass 			return -ENOENT;
107626403871SSimon Glass 		}
107726403871SSimon Glass 	}
107826403871SSimon Glass 	if (!suffix)
107926403871SSimon Glass 		suffix = "";
108026403871SSimon Glass 
108126403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
108226403871SSimon Glass 		 suffix);
108326403871SSimon Glass 	mem = fdt_getprop(blob, config_node, prop_name, NULL);
108426403871SSimon Glass 	if (!mem) {
108526403871SSimon Glass 		debug("%s: No memory type for '%s', using /memory\n", __func__,
108626403871SSimon Glass 		      prop_name);
108726403871SSimon Glass 		mem = "/memory";
108826403871SSimon Glass 	}
108926403871SSimon Glass 
109026403871SSimon Glass 	node = fdt_path_offset(blob, mem);
109126403871SSimon Glass 	if (node < 0) {
109226403871SSimon Glass 		debug("%s: Failed to find node '%s': %s\n", __func__, mem,
109326403871SSimon Glass 		      fdt_strerror(node));
109426403871SSimon Glass 		return -ENOENT;
109526403871SSimon Glass 	}
109626403871SSimon Glass 
109726403871SSimon Glass 	/*
109826403871SSimon Glass 	 * Not strictly correct - the memory may have multiple banks. We just
109926403871SSimon Glass 	 * use the first
110026403871SSimon Glass 	 */
110126403871SSimon Glass 	if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
110226403871SSimon Glass 		debug("%s: Failed to decode memory region %s\n", __func__,
110326403871SSimon Glass 		      mem);
110426403871SSimon Glass 		return -EINVAL;
110526403871SSimon Glass 	}
110626403871SSimon Glass 
110726403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
110826403871SSimon Glass 		 suffix);
110926403871SSimon Glass 	if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
111026403871SSimon Glass 				 &offset_size)) {
111126403871SSimon Glass 		debug("%s: Failed to decode memory region '%s'\n", __func__,
111226403871SSimon Glass 		      prop_name);
111326403871SSimon Glass 		return -EINVAL;
111426403871SSimon Glass 	}
111526403871SSimon Glass 
111626403871SSimon Glass 	*basep = base + offset;
111726403871SSimon Glass 	*sizep = offset_size;
111826403871SSimon Glass 
111926403871SSimon Glass 	return 0;
112026403871SSimon Glass }
1121b45122fdSSimon Glass 
112212e67114SSimon Glass static int decode_timing_property(const void *blob, int node, const char *name,
112312e67114SSimon Glass 				  struct timing_entry *result)
112412e67114SSimon Glass {
112512e67114SSimon Glass 	int length, ret = 0;
112612e67114SSimon Glass 	const u32 *prop;
112712e67114SSimon Glass 
112812e67114SSimon Glass 	prop = fdt_getprop(blob, node, name, &length);
112912e67114SSimon Glass 	if (!prop) {
113012e67114SSimon Glass 		debug("%s: could not find property %s\n",
113112e67114SSimon Glass 		      fdt_get_name(blob, node, NULL), name);
113212e67114SSimon Glass 		return length;
113312e67114SSimon Glass 	}
113412e67114SSimon Glass 
113512e67114SSimon Glass 	if (length == sizeof(u32)) {
113612e67114SSimon Glass 		result->typ = fdtdec_get_int(blob, node, name, 0);
113712e67114SSimon Glass 		result->min = result->typ;
113812e67114SSimon Glass 		result->max = result->typ;
113912e67114SSimon Glass 	} else {
114012e67114SSimon Glass 		ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
114112e67114SSimon Glass 	}
114212e67114SSimon Glass 
114312e67114SSimon Glass 	return ret;
114412e67114SSimon Glass }
114512e67114SSimon Glass 
114612e67114SSimon Glass int fdtdec_decode_display_timing(const void *blob, int parent, int index,
114712e67114SSimon Glass 				 struct display_timing *dt)
114812e67114SSimon Glass {
114912e67114SSimon Glass 	int i, node, timings_node;
115012e67114SSimon Glass 	u32 val = 0;
115112e67114SSimon Glass 	int ret = 0;
115212e67114SSimon Glass 
115312e67114SSimon Glass 	timings_node = fdt_subnode_offset(blob, parent, "display-timings");
115412e67114SSimon Glass 	if (timings_node < 0)
115512e67114SSimon Glass 		return timings_node;
115612e67114SSimon Glass 
115712e67114SSimon Glass 	for (i = 0, node = fdt_first_subnode(blob, timings_node);
115812e67114SSimon Glass 	     node > 0 && i != index;
115912e67114SSimon Glass 	     node = fdt_next_subnode(blob, node))
116012e67114SSimon Glass 		i++;
116112e67114SSimon Glass 
116212e67114SSimon Glass 	if (node < 0)
116312e67114SSimon Glass 		return node;
116412e67114SSimon Glass 
116512e67114SSimon Glass 	memset(dt, 0, sizeof(*dt));
116612e67114SSimon Glass 
116712e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hback-porch",
116812e67114SSimon Glass 				      &dt->hback_porch);
116912e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hfront-porch",
117012e67114SSimon Glass 				      &dt->hfront_porch);
117112e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
117212e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
117312e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vback-porch",
117412e67114SSimon Glass 				      &dt->vback_porch);
117512e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vfront-porch",
117612e67114SSimon Glass 				      &dt->vfront_porch);
117712e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
117812e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
117912e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "clock-frequency",
118012e67114SSimon Glass 				      &dt->pixelclock);
118112e67114SSimon Glass 
118212e67114SSimon Glass 	dt->flags = 0;
118312e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "vsync-active", -1);
118412e67114SSimon Glass 	if (val != -1) {
118512e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
118612e67114SSimon Glass 				DISPLAY_FLAGS_VSYNC_LOW;
118712e67114SSimon Glass 	}
118812e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "hsync-active", -1);
118912e67114SSimon Glass 	if (val != -1) {
119012e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
119112e67114SSimon Glass 				DISPLAY_FLAGS_HSYNC_LOW;
119212e67114SSimon Glass 	}
119312e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "de-active", -1);
119412e67114SSimon Glass 	if (val != -1) {
119512e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
119612e67114SSimon Glass 				DISPLAY_FLAGS_DE_LOW;
119712e67114SSimon Glass 	}
119812e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
119912e67114SSimon Glass 	if (val != -1) {
120012e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
120112e67114SSimon Glass 				DISPLAY_FLAGS_PIXDATA_NEGEDGE;
120212e67114SSimon Glass 	}
120312e67114SSimon Glass 
120412e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "interlaced"))
120512e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_INTERLACED;
120612e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doublescan"))
120712e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
120812e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doubleclk"))
120912e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
121012e67114SSimon Glass 
121112e67114SSimon Glass 	return 0;
121212e67114SSimon Glass }
121312e67114SSimon Glass 
12140879361fSSimon Glass int fdtdec_setup(void)
1215b45122fdSSimon Glass {
12160f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
1217b45122fdSSimon Glass # ifdef CONFIG_OF_EMBED
1218b45122fdSSimon Glass 	/* Get a pointer to the FDT */
1219b45122fdSSimon Glass 	gd->fdt_blob = __dtb_dt_begin;
1220b45122fdSSimon Glass # elif defined CONFIG_OF_SEPARATE
1221b45122fdSSimon Glass #  ifdef CONFIG_SPL_BUILD
122210172962SSimon Glass 	/* FDT is at end of BSS unless it is in a different memory region */
122310172962SSimon Glass 	if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
122410172962SSimon Glass 		gd->fdt_blob = (ulong *)&_image_binary_end;
122510172962SSimon Glass 	else
1226b45122fdSSimon Glass 		gd->fdt_blob = (ulong *)&__bss_end;
1227b45122fdSSimon Glass #  else
1228b45122fdSSimon Glass 	/* FDT is at end of image */
1229b45122fdSSimon Glass 	gd->fdt_blob = (ulong *)&_end;
123029a23f9dSHeiko Schocher #  endif
1231b45122fdSSimon Glass # elif defined(CONFIG_OF_HOSTFILE)
1232b45122fdSSimon Glass 	if (sandbox_read_fdt_from_file()) {
1233b45122fdSSimon Glass 		puts("Failed to read control FDT\n");
1234b45122fdSSimon Glass 		return -1;
1235b45122fdSSimon Glass 	}
1236b45122fdSSimon Glass # endif
1237b45122fdSSimon Glass # ifndef CONFIG_SPL_BUILD
1238b45122fdSSimon Glass 	/* Allow the early environment to override the fdt address */
1239b45122fdSSimon Glass 	gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
1240b45122fdSSimon Glass 						(uintptr_t)gd->fdt_blob);
1241b45122fdSSimon Glass # endif
1242b45122fdSSimon Glass #endif
12430879361fSSimon Glass 	return fdtdec_prepare_fdt();
1244b45122fdSSimon Glass }
1245b45122fdSSimon Glass 
1246b45122fdSSimon Glass #endif /* !USE_HOSTCC */
1247