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