xref: /rk3399_rockchip-uboot/lib/fdtdec.c (revision b7e0d73bad051b666c6cbf9dff381f4c48dcb8a2)
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>
8fcc0a877SSimon Glass #include <dm.h>
95c33c9fdSSimon Glass #include <errno.h>
10b5220bc6SSimon Glass #include <serial.h>
11b5220bc6SSimon Glass #include <libfdt.h>
126e06acb7SStephen Warren #include <fdt_support.h>
13b5220bc6SSimon Glass #include <fdtdec.h>
14b45122fdSSimon Glass #include <asm/sections.h>
15*b7e0d73bSSimon Glass #include <dm/of_extra.h>
165c33c9fdSSimon Glass #include <linux/ctype.h>
17b5220bc6SSimon Glass 
18b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
19b5220bc6SSimon Glass 
20b5220bc6SSimon Glass /*
21b5220bc6SSimon Glass  * Here are the type we know about. One day we might allow drivers to
22b5220bc6SSimon Glass  * register. For now we just put them here. The COMPAT macro allows us to
23b5220bc6SSimon Glass  * turn this into a sparse list later, and keeps the ID with the name.
2401a227dfSSimon Glass  *
2501a227dfSSimon Glass  * NOTE: This list is basically a TODO list for things that need to be
2601a227dfSSimon Glass  * converted to driver model. So don't add new things here unless there is a
2701a227dfSSimon Glass  * good reason why driver-model conversion is infeasible. Examples include
2801a227dfSSimon Glass  * things which are used before driver model is available.
29b5220bc6SSimon Glass  */
30b5220bc6SSimon Glass #define COMPAT(id, name) name
31b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = {
32f88fe2deSSimon Glass 	COMPAT(UNKNOWN, "<none>"),
330e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
340e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
35312693c3SJim Lin 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
3600f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
3739f63332SStephen Warren 	COMPAT(NVIDIA_TEGRA186_SDMMC, "nvidia,tegra186-sdhci"),
387aaa5a60STom Warren 	COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
39a73ca478SStephen Warren 	COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
40f4e4e0b0STom Warren 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
41c9aa831eSTom Warren 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
4279c7a90fSThierry Reding 	COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
437aaa5a60STom Warren 	COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
44cc9fe33aSHatim RV 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
45cc9fe33aSHatim RV 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
46c34253d1SRajeshwari Shinde 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
4772dbff12SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
4872dbff12SRajeshwari Shinde 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
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"),
52de461c52SPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
537d3ca0f8SJaehoon Chung 	COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
543577fe8bSPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
55f37df0f8SPrzemyslaw Marczak 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
56bb8215f4SSimon Glass 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
577772bb78SRajeshwari Shinde 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
58ecbd7e1eSnaveen krishna chatradhi 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
5945c480c9SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
6077f9b1fbSSimon Glass 	COMPAT(INTEL_MICROCODE, "intel,microcode"),
616173c45bSThierry Reding 	COMPAT(AMS_AS3722, "ams,as3722"),
62c89ada01SBin Meng 	COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
636ab00db2SMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
64129adf5bSMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
65ef4b01b2SMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
6639ea0ee9SSimon Glass 	COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
6739ea0ee9SSimon Glass 	COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
6839ea0ee9SSimon Glass 	COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
694ccae81cSBoris Brezillon 	COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
70e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
71e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
72e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
73e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
74e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
75e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
76e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
77e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
78b5220bc6SSimon Glass };
79b5220bc6SSimon Glass 
80a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id)
81a53f4a29SSimon Glass {
82a53f4a29SSimon Glass 	/* We allow reading of the 'unknown' ID for testing purposes */
83a53f4a29SSimon Glass 	assert(id >= 0 && id < COMPAT_COUNT);
84a53f4a29SSimon Glass 	return compat_names[id];
85a53f4a29SSimon Glass }
86a53f4a29SSimon Glass 
8702464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
8802464e38SStephen Warren 		const char *prop_name, int index, int na, int ns,
896e06acb7SStephen Warren 		fdt_size_t *sizep, bool translate)
9002464e38SStephen Warren {
9102464e38SStephen Warren 	const fdt32_t *prop, *prop_end;
9202464e38SStephen Warren 	const fdt32_t *prop_addr, *prop_size, *prop_after_size;
9302464e38SStephen Warren 	int len;
9402464e38SStephen Warren 	fdt_addr_t addr;
9502464e38SStephen Warren 
9602464e38SStephen Warren 	debug("%s: %s: ", __func__, prop_name);
9702464e38SStephen Warren 
9802464e38SStephen Warren 	if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
9902464e38SStephen Warren 		debug("(na too large for fdt_addr_t type)\n");
10002464e38SStephen Warren 		return FDT_ADDR_T_NONE;
10102464e38SStephen Warren 	}
10202464e38SStephen Warren 
10302464e38SStephen Warren 	if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
10402464e38SStephen Warren 		debug("(ns too large for fdt_size_t type)\n");
10502464e38SStephen Warren 		return FDT_ADDR_T_NONE;
10602464e38SStephen Warren 	}
10702464e38SStephen Warren 
10802464e38SStephen Warren 	prop = fdt_getprop(blob, node, prop_name, &len);
10902464e38SStephen Warren 	if (!prop) {
11002464e38SStephen Warren 		debug("(not found)\n");
11102464e38SStephen Warren 		return FDT_ADDR_T_NONE;
11202464e38SStephen Warren 	}
11302464e38SStephen Warren 	prop_end = prop + (len / sizeof(*prop));
11402464e38SStephen Warren 
11502464e38SStephen Warren 	prop_addr = prop + (index * (na + ns));
11602464e38SStephen Warren 	prop_size = prop_addr + na;
11702464e38SStephen Warren 	prop_after_size = prop_size + ns;
11802464e38SStephen Warren 	if (prop_after_size > prop_end) {
11902464e38SStephen Warren 		debug("(not enough data: expected >= %d cells, got %d cells)\n",
12002464e38SStephen Warren 		      (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
12102464e38SStephen Warren 		return FDT_ADDR_T_NONE;
12202464e38SStephen Warren 	}
12302464e38SStephen Warren 
1245efa1bfbSVignesh R #if CONFIG_IS_ENABLED(OF_TRANSLATE)
1256e06acb7SStephen Warren 	if (translate)
1266e06acb7SStephen Warren 		addr = fdt_translate_address(blob, node, prop_addr);
1276e06acb7SStephen Warren 	else
1286e06acb7SStephen Warren #endif
12902464e38SStephen Warren 		addr = fdtdec_get_number(prop_addr, na);
13002464e38SStephen Warren 
13102464e38SStephen Warren 	if (sizep) {
13202464e38SStephen Warren 		*sizep = fdtdec_get_number(prop_size, ns);
133fd30d2c6SSimon Glass 		debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
134fd30d2c6SSimon Glass 		      (unsigned long long)*sizep);
13502464e38SStephen Warren 	} else {
136fd30d2c6SSimon Glass 		debug("addr=%08llx\n", (unsigned long long)addr);
13702464e38SStephen Warren 	}
13802464e38SStephen Warren 
13902464e38SStephen Warren 	return addr;
14002464e38SStephen Warren }
14102464e38SStephen Warren 
14202464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
1436e06acb7SStephen Warren 		int node, const char *prop_name, int index, fdt_size_t *sizep,
1446e06acb7SStephen Warren 		bool translate)
14502464e38SStephen Warren {
14602464e38SStephen Warren 	int na, ns;
14702464e38SStephen Warren 
14802464e38SStephen Warren 	debug("%s: ", __func__);
14902464e38SStephen Warren 
15002464e38SStephen Warren 	na = fdt_address_cells(blob, parent);
15102464e38SStephen Warren 	if (na < 1) {
15202464e38SStephen Warren 		debug("(bad #address-cells)\n");
15302464e38SStephen Warren 		return FDT_ADDR_T_NONE;
15402464e38SStephen Warren 	}
15502464e38SStephen Warren 
15602464e38SStephen Warren 	ns = fdt_size_cells(blob, parent);
157ff0a6358SPrzemyslaw Marczak 	if (ns < 0) {
15802464e38SStephen Warren 		debug("(bad #size-cells)\n");
15902464e38SStephen Warren 		return FDT_ADDR_T_NONE;
16002464e38SStephen Warren 	}
16102464e38SStephen Warren 
16202464e38SStephen Warren 	debug("na=%d, ns=%d, ", na, ns);
16302464e38SStephen Warren 
16402464e38SStephen Warren 	return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
1656e06acb7SStephen Warren 					  ns, sizep, translate);
16602464e38SStephen Warren }
16702464e38SStephen Warren 
16802464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
1696e06acb7SStephen Warren 		const char *prop_name, int index, fdt_size_t *sizep,
1706e06acb7SStephen Warren 		bool translate)
17102464e38SStephen Warren {
17202464e38SStephen Warren 	int parent;
17302464e38SStephen Warren 
17402464e38SStephen Warren 	debug("%s: ", __func__);
17502464e38SStephen Warren 
17602464e38SStephen Warren 	parent = fdt_parent_offset(blob, node);
17702464e38SStephen Warren 	if (parent < 0) {
17802464e38SStephen Warren 		debug("(no parent found)\n");
17902464e38SStephen Warren 		return FDT_ADDR_T_NONE;
18002464e38SStephen Warren 	}
18102464e38SStephen Warren 
18202464e38SStephen Warren 	return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
1836e06acb7SStephen Warren 						index, sizep, translate);
18402464e38SStephen Warren }
18502464e38SStephen Warren 
1864397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
1874397a2a8SSimon Glass 		const char *prop_name, fdt_size_t *sizep)
188b5220bc6SSimon Glass {
189d93b9a07SStephen Warren 	int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
190d93b9a07SStephen Warren 
19102464e38SStephen Warren 	return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
19202464e38SStephen Warren 					  sizeof(fdt_addr_t) / sizeof(fdt32_t),
1936e06acb7SStephen Warren 					  ns, sizep, false);
194b5220bc6SSimon Glass }
195b5220bc6SSimon Glass 
1964397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
1974397a2a8SSimon Glass 		const char *prop_name)
1984397a2a8SSimon Glass {
1994397a2a8SSimon Glass 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
2004397a2a8SSimon Glass }
2014397a2a8SSimon Glass 
202fcc0a877SSimon Glass #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
203a62e84d7SBin Meng int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
204a62e84d7SBin Meng 		const char *prop_name, struct fdt_pci_addr *addr)
205a62e84d7SBin Meng {
206a62e84d7SBin Meng 	const u32 *cell;
207a62e84d7SBin Meng 	int len;
208a62e84d7SBin Meng 	int ret = -ENOENT;
209a62e84d7SBin Meng 
210a62e84d7SBin Meng 	debug("%s: %s: ", __func__, prop_name);
211a62e84d7SBin Meng 
212a62e84d7SBin Meng 	/*
213a62e84d7SBin Meng 	 * If we follow the pci bus bindings strictly, we should check
214a62e84d7SBin Meng 	 * the value of the node's parent node's #address-cells and
215a62e84d7SBin Meng 	 * #size-cells. They need to be 3 and 2 accordingly. However,
216a62e84d7SBin Meng 	 * for simplicity we skip the check here.
217a62e84d7SBin Meng 	 */
218a62e84d7SBin Meng 	cell = fdt_getprop(blob, node, prop_name, &len);
219a62e84d7SBin Meng 	if (!cell)
220a62e84d7SBin Meng 		goto fail;
221a62e84d7SBin Meng 
222a62e84d7SBin Meng 	if ((len % FDT_PCI_REG_SIZE) == 0) {
223a62e84d7SBin Meng 		int num = len / FDT_PCI_REG_SIZE;
224a62e84d7SBin Meng 		int i;
225a62e84d7SBin Meng 
226a62e84d7SBin Meng 		for (i = 0; i < num; i++) {
227a62e84d7SBin Meng 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
2284ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[0]),
2294ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[1]),
2304ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[2]));
2314ea5243aSStephen Warren 			if ((fdt32_to_cpu(*cell) & type) == type) {
2324ea5243aSStephen Warren 				addr->phys_hi = fdt32_to_cpu(cell[0]);
2334ea5243aSStephen Warren 				addr->phys_mid = fdt32_to_cpu(cell[1]);
2344ea5243aSStephen Warren 				addr->phys_lo = fdt32_to_cpu(cell[1]);
235a62e84d7SBin Meng 				break;
236a62e84d7SBin Meng 			} else {
237a62e84d7SBin Meng 				cell += (FDT_PCI_ADDR_CELLS +
238a62e84d7SBin Meng 					 FDT_PCI_SIZE_CELLS);
239a62e84d7SBin Meng 			}
240a62e84d7SBin Meng 		}
241a62e84d7SBin Meng 
242106cce96SSimon Glass 		if (i == num) {
243106cce96SSimon Glass 			ret = -ENXIO;
244a62e84d7SBin Meng 			goto fail;
245106cce96SSimon Glass 		}
246a62e84d7SBin Meng 
247a62e84d7SBin Meng 		return 0;
248a62e84d7SBin Meng 	} else {
249a62e84d7SBin Meng 		ret = -EINVAL;
250a62e84d7SBin Meng 	}
251a62e84d7SBin Meng 
252a62e84d7SBin Meng fail:
253a62e84d7SBin Meng 	debug("(not found)\n");
254a62e84d7SBin Meng 	return ret;
255a62e84d7SBin Meng }
256a62e84d7SBin Meng 
257a62e84d7SBin Meng int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
258a62e84d7SBin Meng {
259a62e84d7SBin Meng 	const char *list, *end;
260a62e84d7SBin Meng 	int len;
261a62e84d7SBin Meng 
262a62e84d7SBin Meng 	list = fdt_getprop(blob, node, "compatible", &len);
263a62e84d7SBin Meng 	if (!list)
264a62e84d7SBin Meng 		return -ENOENT;
265a62e84d7SBin Meng 
266a62e84d7SBin Meng 	end = list + len;
267a62e84d7SBin Meng 	while (list < end) {
268a62e84d7SBin Meng 		char *s;
269a62e84d7SBin Meng 
270a62e84d7SBin Meng 		len = strlen(list);
271a62e84d7SBin Meng 		if (len >= strlen("pciVVVV,DDDD")) {
272a62e84d7SBin Meng 			s = strstr(list, "pci");
273a62e84d7SBin Meng 
274a62e84d7SBin Meng 			/*
275a62e84d7SBin Meng 			 * check if the string is something like pciVVVV,DDDD.RR
276a62e84d7SBin Meng 			 * or just pciVVVV,DDDD
277a62e84d7SBin Meng 			 */
278a62e84d7SBin Meng 			if (s && s[7] == ',' &&
279a62e84d7SBin Meng 			    (s[12] == '.' || s[12] == 0)) {
280a62e84d7SBin Meng 				s += 3;
281a62e84d7SBin Meng 				*vendor = simple_strtol(s, NULL, 16);
282a62e84d7SBin Meng 
283a62e84d7SBin Meng 				s += 5;
284a62e84d7SBin Meng 				*device = simple_strtol(s, NULL, 16);
285a62e84d7SBin Meng 
286a62e84d7SBin Meng 				return 0;
287a62e84d7SBin Meng 			}
288a62e84d7SBin Meng 		}
289bc6351ebSBin Meng 		list += (len + 1);
290a62e84d7SBin Meng 	}
291a62e84d7SBin Meng 
292a62e84d7SBin Meng 	return -ENOENT;
293a62e84d7SBin Meng }
294a62e84d7SBin Meng 
295fcc0a877SSimon Glass int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
296fcc0a877SSimon Glass 			 u32 *bar)
297a62e84d7SBin Meng {
298a62e84d7SBin Meng 	int barnum;
299a62e84d7SBin Meng 
300a62e84d7SBin Meng 	/* extract the bar number from fdt_pci_addr */
301a62e84d7SBin Meng 	barnum = addr->phys_hi & 0xff;
302a62e84d7SBin Meng 	if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
303a62e84d7SBin Meng 		return -EINVAL;
304a62e84d7SBin Meng 
305a62e84d7SBin Meng 	barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
306fcc0a877SSimon Glass 	*bar = dm_pci_read_bar32(dev, barnum);
307a62e84d7SBin Meng 
308a62e84d7SBin Meng 	return 0;
309a62e84d7SBin Meng }
310a62e84d7SBin Meng #endif
311a62e84d7SBin Meng 
312aadef0a1SChe-Liang Chiou uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
313aadef0a1SChe-Liang Chiou 		uint64_t default_val)
314aadef0a1SChe-Liang Chiou {
315aadef0a1SChe-Liang Chiou 	const uint64_t *cell64;
316aadef0a1SChe-Liang Chiou 	int length;
317aadef0a1SChe-Liang Chiou 
318aadef0a1SChe-Liang Chiou 	cell64 = fdt_getprop(blob, node, prop_name, &length);
319aadef0a1SChe-Liang Chiou 	if (!cell64 || length < sizeof(*cell64))
320aadef0a1SChe-Liang Chiou 		return default_val;
321aadef0a1SChe-Liang Chiou 
322aadef0a1SChe-Liang Chiou 	return fdt64_to_cpu(*cell64);
323aadef0a1SChe-Liang Chiou }
324aadef0a1SChe-Liang Chiou 
325f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node)
326b5220bc6SSimon Glass {
327b5220bc6SSimon Glass 	const char *cell;
328b5220bc6SSimon Glass 
329f88fe2deSSimon Glass 	/*
330f88fe2deSSimon Glass 	 * It should say "okay", so only allow that. Some fdts use "ok" but
331f88fe2deSSimon Glass 	 * this is a bug. Please fix your device tree source file. See here
332f88fe2deSSimon Glass 	 * for discussion:
333f88fe2deSSimon Glass 	 *
334f88fe2deSSimon Glass 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
335f88fe2deSSimon Glass 	 */
336b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, "status", NULL);
337b5220bc6SSimon Glass 	if (cell)
338f88fe2deSSimon Glass 		return 0 == strcmp(cell, "okay");
339f88fe2deSSimon Glass 	return 1;
340b5220bc6SSimon Glass }
341b5220bc6SSimon Glass 
3427cde397bSGerald Van Baren enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
343b5220bc6SSimon Glass {
344b5220bc6SSimon Glass 	enum fdt_compat_id id;
345b5220bc6SSimon Glass 
346b5220bc6SSimon Glass 	/* Search our drivers */
347b5220bc6SSimon Glass 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
348b5220bc6SSimon Glass 		if (0 == fdt_node_check_compatible(blob, node,
349b5220bc6SSimon Glass 				compat_names[id]))
350b5220bc6SSimon Glass 			return id;
351b5220bc6SSimon Glass 	return COMPAT_UNKNOWN;
352b5220bc6SSimon Glass }
353b5220bc6SSimon Glass 
354b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node,
355b5220bc6SSimon Glass 		enum fdt_compat_id id)
356b5220bc6SSimon Glass {
357b5220bc6SSimon Glass 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
358b5220bc6SSimon Glass }
359b5220bc6SSimon Glass 
3603ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
3613ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp)
3623ddecfc7SSimon Glass {
3633ddecfc7SSimon Glass 	do {
3643ddecfc7SSimon Glass 		node = fdt_next_node(blob, node, depthp);
3653ddecfc7SSimon Glass 	} while (*depthp > 1);
3663ddecfc7SSimon Glass 
3673ddecfc7SSimon Glass 	/* If this is a direct subnode, and compatible, return it */
3683ddecfc7SSimon Glass 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
3693ddecfc7SSimon Glass 						blob, node, compat_names[id]))
3703ddecfc7SSimon Glass 		return node;
3713ddecfc7SSimon Glass 
3723ddecfc7SSimon Glass 	return -FDT_ERR_NOTFOUND;
3733ddecfc7SSimon Glass }
3743ddecfc7SSimon Glass 
375b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
376b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto)
377b5220bc6SSimon Glass {
378b5220bc6SSimon Glass #define MAX_STR_LEN 20
379b5220bc6SSimon Glass 	char str[MAX_STR_LEN + 20];
380b5220bc6SSimon Glass 	int node, err;
381b5220bc6SSimon Glass 
382b5220bc6SSimon Glass 	/* snprintf() is not available */
383b5220bc6SSimon Glass 	assert(strlen(name) < MAX_STR_LEN);
384b5220bc6SSimon Glass 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
38500878476SSimon Glass 	node = fdt_path_offset(blob, str);
386b5220bc6SSimon Glass 	if (node < 0)
387b5220bc6SSimon Glass 		return node;
388b5220bc6SSimon Glass 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
389b5220bc6SSimon Glass 	if (err < 0)
390b5220bc6SSimon Glass 		return err;
391f88fe2deSSimon Glass 	if (err)
392f88fe2deSSimon Glass 		return -FDT_ERR_NOTFOUND;
393f88fe2deSSimon Glass 	(*upto)++;
394f88fe2deSSimon Glass 	return node;
395b5220bc6SSimon Glass }
396b5220bc6SSimon Glass 
397a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
398a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
399a53f4a29SSimon Glass {
400c6782270SSimon Glass 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
401c6782270SSimon Glass 
402c6782270SSimon Glass 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
403c6782270SSimon Glass }
404c6782270SSimon Glass 
405c6782270SSimon Glass /* TODO: Can we tighten this code up a little? */
406c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
407c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
408c6782270SSimon Glass {
409a53f4a29SSimon Glass 	int name_len = strlen(name);
410a53f4a29SSimon Glass 	int nodes[maxcount];
411a53f4a29SSimon Glass 	int num_found = 0;
412a53f4a29SSimon Glass 	int offset, node;
413a53f4a29SSimon Glass 	int alias_node;
414a53f4a29SSimon Glass 	int count;
415a53f4a29SSimon Glass 	int i, j;
416a53f4a29SSimon Glass 
417a53f4a29SSimon Glass 	/* find the alias node if present */
418a53f4a29SSimon Glass 	alias_node = fdt_path_offset(blob, "/aliases");
419a53f4a29SSimon Glass 
420a53f4a29SSimon Glass 	/*
421a53f4a29SSimon Glass 	 * start with nothing, and we can assume that the root node can't
422a53f4a29SSimon Glass 	 * match
423a53f4a29SSimon Glass 	 */
424a53f4a29SSimon Glass 	memset(nodes, '\0', sizeof(nodes));
425a53f4a29SSimon Glass 
426a53f4a29SSimon Glass 	/* First find all the compatible nodes */
427a53f4a29SSimon Glass 	for (node = count = 0; node >= 0 && count < maxcount;) {
428a53f4a29SSimon Glass 		node = fdtdec_next_compatible(blob, node, id);
429a53f4a29SSimon Glass 		if (node >= 0)
430a53f4a29SSimon Glass 			nodes[count++] = node;
431a53f4a29SSimon Glass 	}
432a53f4a29SSimon Glass 	if (node >= 0)
433a53f4a29SSimon Glass 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
434a53f4a29SSimon Glass 		       __func__, name);
435a53f4a29SSimon Glass 
436a53f4a29SSimon Glass 	/* Now find all the aliases */
437a53f4a29SSimon Glass 	for (offset = fdt_first_property_offset(blob, alias_node);
438a53f4a29SSimon Glass 			offset > 0;
439a53f4a29SSimon Glass 			offset = fdt_next_property_offset(blob, offset)) {
440a53f4a29SSimon Glass 		const struct fdt_property *prop;
441a53f4a29SSimon Glass 		const char *path;
442a53f4a29SSimon Glass 		int number;
443a53f4a29SSimon Glass 		int found;
444a53f4a29SSimon Glass 
445a53f4a29SSimon Glass 		node = 0;
446a53f4a29SSimon Glass 		prop = fdt_get_property_by_offset(blob, offset, NULL);
447a53f4a29SSimon Glass 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
448a53f4a29SSimon Glass 		if (prop->len && 0 == strncmp(path, name, name_len))
449a53f4a29SSimon Glass 			node = fdt_path_offset(blob, prop->data);
450a53f4a29SSimon Glass 		if (node <= 0)
451a53f4a29SSimon Glass 			continue;
452a53f4a29SSimon Glass 
453a53f4a29SSimon Glass 		/* Get the alias number */
454a53f4a29SSimon Glass 		number = simple_strtoul(path + name_len, NULL, 10);
455a53f4a29SSimon Glass 		if (number < 0 || number >= maxcount) {
456a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' is out of range\n",
457a53f4a29SSimon Glass 			       __func__, path);
458a53f4a29SSimon Glass 			continue;
459a53f4a29SSimon Glass 		}
460a53f4a29SSimon Glass 
461a53f4a29SSimon Glass 		/* Make sure the node we found is actually in our list! */
462a53f4a29SSimon Glass 		found = -1;
463a53f4a29SSimon Glass 		for (j = 0; j < count; j++)
464a53f4a29SSimon Glass 			if (nodes[j] == node) {
465a53f4a29SSimon Glass 				found = j;
466a53f4a29SSimon Glass 				break;
467a53f4a29SSimon Glass 			}
468a53f4a29SSimon Glass 
469a53f4a29SSimon Glass 		if (found == -1) {
470a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' points to a node "
471a53f4a29SSimon Glass 				"'%s' that is missing or is not compatible "
472a53f4a29SSimon Glass 				" with '%s'\n", __func__, path,
473a53f4a29SSimon Glass 				fdt_get_name(blob, node, NULL),
474a53f4a29SSimon Glass 			       compat_names[id]);
475a53f4a29SSimon Glass 			continue;
476a53f4a29SSimon Glass 		}
477a53f4a29SSimon Glass 
478a53f4a29SSimon Glass 		/*
479a53f4a29SSimon Glass 		 * Add this node to our list in the right place, and mark
480a53f4a29SSimon Glass 		 * it as done.
481a53f4a29SSimon Glass 		 */
482a53f4a29SSimon Glass 		if (fdtdec_get_is_enabled(blob, node)) {
483c6782270SSimon Glass 			if (node_list[number]) {
484c6782270SSimon Glass 				debug("%s: warning: alias '%s' requires that "
485c6782270SSimon Glass 				      "a node be placed in the list in a "
486c6782270SSimon Glass 				      "position which is already filled by "
487c6782270SSimon Glass 				      "node '%s'\n", __func__, path,
488c6782270SSimon Glass 				      fdt_get_name(blob, node, NULL));
489c6782270SSimon Glass 				continue;
490c6782270SSimon Glass 			}
491a53f4a29SSimon Glass 			node_list[number] = node;
492a53f4a29SSimon Glass 			if (number >= num_found)
493a53f4a29SSimon Glass 				num_found = number + 1;
494a53f4a29SSimon Glass 		}
495c6782270SSimon Glass 		nodes[found] = 0;
496a53f4a29SSimon Glass 	}
497a53f4a29SSimon Glass 
498a53f4a29SSimon Glass 	/* Add any nodes not mentioned by an alias */
499a53f4a29SSimon Glass 	for (i = j = 0; i < maxcount; i++) {
500a53f4a29SSimon Glass 		if (!node_list[i]) {
501a53f4a29SSimon Glass 			for (; j < maxcount; j++)
502a53f4a29SSimon Glass 				if (nodes[j] &&
503a53f4a29SSimon Glass 					fdtdec_get_is_enabled(blob, nodes[j]))
504a53f4a29SSimon Glass 					break;
505a53f4a29SSimon Glass 
506a53f4a29SSimon Glass 			/* Have we run out of nodes to add? */
507a53f4a29SSimon Glass 			if (j == maxcount)
508a53f4a29SSimon Glass 				break;
509a53f4a29SSimon Glass 
510a53f4a29SSimon Glass 			assert(!node_list[i]);
511a53f4a29SSimon Glass 			node_list[i] = nodes[j++];
512a53f4a29SSimon Glass 			if (i >= num_found)
513a53f4a29SSimon Glass 				num_found = i + 1;
514a53f4a29SSimon Glass 		}
515a53f4a29SSimon Glass 	}
516a53f4a29SSimon Glass 
517a53f4a29SSimon Glass 	return num_found;
518a53f4a29SSimon Glass }
519a53f4a29SSimon Glass 
5205c33c9fdSSimon Glass int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
5215c33c9fdSSimon Glass 			 int *seqp)
5225c33c9fdSSimon Glass {
5235c33c9fdSSimon Glass 	int base_len = strlen(base);
5245c33c9fdSSimon Glass 	const char *find_name;
5255c33c9fdSSimon Glass 	int find_namelen;
5265c33c9fdSSimon Glass 	int prop_offset;
5275c33c9fdSSimon Glass 	int aliases;
5285c33c9fdSSimon Glass 
5295c33c9fdSSimon Glass 	find_name = fdt_get_name(blob, offset, &find_namelen);
5305c33c9fdSSimon Glass 	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
5315c33c9fdSSimon Glass 
5325c33c9fdSSimon Glass 	aliases = fdt_path_offset(blob, "/aliases");
5335c33c9fdSSimon Glass 	for (prop_offset = fdt_first_property_offset(blob, aliases);
5345c33c9fdSSimon Glass 	     prop_offset > 0;
5355c33c9fdSSimon Glass 	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
5365c33c9fdSSimon Glass 		const char *prop;
5375c33c9fdSSimon Glass 		const char *name;
5385c33c9fdSSimon Glass 		const char *slash;
539c4af6732SSimon Glass 		int len, val;
5405c33c9fdSSimon Glass 
5415c33c9fdSSimon Glass 		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
5425c33c9fdSSimon Glass 		debug("   - %s, %s\n", name, prop);
5435c33c9fdSSimon Glass 		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
5445c33c9fdSSimon Glass 		    strncmp(name, base, base_len))
5455c33c9fdSSimon Glass 			continue;
5465c33c9fdSSimon Glass 
5475c33c9fdSSimon Glass 		slash = strrchr(prop, '/');
5485c33c9fdSSimon Glass 		if (strcmp(slash + 1, find_name))
5495c33c9fdSSimon Glass 			continue;
550c4af6732SSimon Glass 		val = trailing_strtol(name);
551c4af6732SSimon Glass 		if (val != -1) {
552c4af6732SSimon Glass 			*seqp = val;
5535c33c9fdSSimon Glass 			debug("Found seq %d\n", *seqp);
5545c33c9fdSSimon Glass 			return 0;
5555c33c9fdSSimon Glass 		}
5565c33c9fdSSimon Glass 	}
5575c33c9fdSSimon Glass 
5585c33c9fdSSimon Glass 	debug("Not found\n");
5595c33c9fdSSimon Glass 	return -ENOENT;
5605c33c9fdSSimon Glass }
5615c33c9fdSSimon Glass 
5623bc37a50SSimon Glass const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
5633bc37a50SSimon Glass {
5643bc37a50SSimon Glass 	int chosen_node;
5653bc37a50SSimon Glass 
5663bc37a50SSimon Glass 	if (!blob)
5673bc37a50SSimon Glass 		return NULL;
5683bc37a50SSimon Glass 	chosen_node = fdt_path_offset(blob, "/chosen");
5693bc37a50SSimon Glass 	return fdt_getprop(blob, chosen_node, name, NULL);
5703bc37a50SSimon Glass }
5713bc37a50SSimon Glass 
572aac07d49SSimon Glass int fdtdec_get_chosen_node(const void *blob, const char *name)
573aac07d49SSimon Glass {
574aac07d49SSimon Glass 	const char *prop;
575aac07d49SSimon Glass 
5763bc37a50SSimon Glass 	prop = fdtdec_get_chosen_prop(blob, name);
577aac07d49SSimon Glass 	if (!prop)
578aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
579aac07d49SSimon Glass 	return fdt_path_offset(blob, prop);
580aac07d49SSimon Glass }
581aac07d49SSimon Glass 
5829a263e55SSimon Glass int fdtdec_check_fdt(void)
5839a263e55SSimon Glass {
5849a263e55SSimon Glass 	/*
5859a263e55SSimon Glass 	 * We must have an FDT, but we cannot panic() yet since the console
5869a263e55SSimon Glass 	 * is not ready. So for now, just assert(). Boards which need an early
5879a263e55SSimon Glass 	 * FDT (prior to console ready) will need to make their own
5889a263e55SSimon Glass 	 * arrangements and do their own checks.
5899a263e55SSimon Glass 	 */
5909a263e55SSimon Glass 	assert(!fdtdec_prepare_fdt());
5919a263e55SSimon Glass 	return 0;
5929a263e55SSimon Glass }
5939a263e55SSimon Glass 
594b5220bc6SSimon Glass /*
595b5220bc6SSimon Glass  * This function is a little odd in that it accesses global data. At some
596b5220bc6SSimon Glass  * point if the architecture board.c files merge this will make more sense.
597b5220bc6SSimon Glass  * Even now, it is common code.
598b5220bc6SSimon Glass  */
5999a263e55SSimon Glass int fdtdec_prepare_fdt(void)
600b5220bc6SSimon Glass {
601c309c2daSSimon Glass 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
602c309c2daSSimon Glass 	    fdt_check_header(gd->fdt_blob)) {
60366312374SSimon Glass #ifdef CONFIG_SPL_BUILD
60466312374SSimon Glass 		puts("Missing DTB\n");
60566312374SSimon Glass #else
60666312374SSimon 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");
607cb5f97f7SSimon Glass # ifdef DEBUG
608cb5f97f7SSimon Glass 		if (gd->fdt_blob) {
609cb5f97f7SSimon Glass 			printf("fdt_blob=%p\n", gd->fdt_blob);
610cb5f97f7SSimon Glass 			print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
611cb5f97f7SSimon Glass 				     32, 0);
612cb5f97f7SSimon Glass 		}
613cb5f97f7SSimon Glass # endif
61466312374SSimon Glass #endif
6159a263e55SSimon Glass 		return -1;
6169a263e55SSimon Glass 	}
617b5220bc6SSimon Glass 	return 0;
618b5220bc6SSimon Glass }
619d17da655SSimon Glass 
620d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
621d17da655SSimon Glass {
622d17da655SSimon Glass 	const u32 *phandle;
623d17da655SSimon Glass 	int lookup;
624d17da655SSimon Glass 
6251cb2323bSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
626d17da655SSimon Glass 	phandle = fdt_getprop(blob, node, prop_name, NULL);
627d17da655SSimon Glass 	if (!phandle)
628d17da655SSimon Glass 		return -FDT_ERR_NOTFOUND;
629d17da655SSimon Glass 
630d17da655SSimon Glass 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
631d17da655SSimon Glass 	return lookup;
632d17da655SSimon Glass }
633d17da655SSimon Glass 
634d17da655SSimon Glass /**
635d17da655SSimon Glass  * Look up a property in a node and check that it has a minimum length.
636d17da655SSimon Glass  *
637d17da655SSimon Glass  * @param blob		FDT blob
638d17da655SSimon Glass  * @param node		node to examine
639d17da655SSimon Glass  * @param prop_name	name of property to find
640d17da655SSimon Glass  * @param min_len	minimum property length in bytes
641d17da655SSimon Glass  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
642d17da655SSimon Glass 			found, or -FDT_ERR_BADLAYOUT if not enough data
643d17da655SSimon Glass  * @return pointer to cell, which is only valid if err == 0
644d17da655SSimon Glass  */
645d17da655SSimon Glass static const void *get_prop_check_min_len(const void *blob, int node,
646d17da655SSimon Glass 		const char *prop_name, int min_len, int *err)
647d17da655SSimon Glass {
648d17da655SSimon Glass 	const void *cell;
649d17da655SSimon Glass 	int len;
650d17da655SSimon Glass 
651d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
652d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
653d17da655SSimon Glass 	if (!cell)
654d17da655SSimon Glass 		*err = -FDT_ERR_NOTFOUND;
655d17da655SSimon Glass 	else if (len < min_len)
656d17da655SSimon Glass 		*err = -FDT_ERR_BADLAYOUT;
657d17da655SSimon Glass 	else
658d17da655SSimon Glass 		*err = 0;
659d17da655SSimon Glass 	return cell;
660d17da655SSimon Glass }
661d17da655SSimon Glass 
662d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
663d17da655SSimon Glass 		u32 *array, int count)
664d17da655SSimon Glass {
665d17da655SSimon Glass 	const u32 *cell;
666d17da655SSimon Glass 	int i, err = 0;
667d17da655SSimon Glass 
668d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
669d17da655SSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
670d17da655SSimon Glass 				      sizeof(u32) * count, &err);
671d17da655SSimon Glass 	if (!err) {
672d17da655SSimon Glass 		for (i = 0; i < count; i++)
673d17da655SSimon Glass 			array[i] = fdt32_to_cpu(cell[i]);
674d17da655SSimon Glass 	}
675d17da655SSimon Glass 	return err;
676d17da655SSimon Glass }
677d17da655SSimon Glass 
678a9f04d49SSimon Glass int fdtdec_get_int_array_count(const void *blob, int node,
679a9f04d49SSimon Glass 			       const char *prop_name, u32 *array, int count)
680a9f04d49SSimon Glass {
681a9f04d49SSimon Glass 	const u32 *cell;
682a9f04d49SSimon Glass 	int len, elems;
683a9f04d49SSimon Glass 	int i;
684a9f04d49SSimon Glass 
685a9f04d49SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
686a9f04d49SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
687a9f04d49SSimon Glass 	if (!cell)
688a9f04d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
689a9f04d49SSimon Glass 	elems = len / sizeof(u32);
690a9f04d49SSimon Glass 	if (count > elems)
691a9f04d49SSimon Glass 		count = elems;
692a9f04d49SSimon Glass 	for (i = 0; i < count; i++)
693a9f04d49SSimon Glass 		array[i] = fdt32_to_cpu(cell[i]);
694a9f04d49SSimon Glass 
695a9f04d49SSimon Glass 	return count;
696a9f04d49SSimon Glass }
697a9f04d49SSimon Glass 
69896875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
69996875e7dSSimon Glass 			       const char *prop_name, int count)
70096875e7dSSimon Glass {
70196875e7dSSimon Glass 	const u32 *cell;
70296875e7dSSimon Glass 	int err;
70396875e7dSSimon Glass 
70496875e7dSSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
70596875e7dSSimon Glass 				      sizeof(u32) * count, &err);
70696875e7dSSimon Glass 	return err ? NULL : cell;
70796875e7dSSimon Glass }
70896875e7dSSimon Glass 
709d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
710d17da655SSimon Glass {
711d17da655SSimon Glass 	const s32 *cell;
712d17da655SSimon Glass 	int len;
713d17da655SSimon Glass 
714d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
715d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
716d17da655SSimon Glass 	return cell != NULL;
717d17da655SSimon Glass }
718ed3ee5cdSSimon Glass 
71957068a7aSSimon Glass int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
72057068a7aSSimon Glass 				   const char *list_name,
72157068a7aSSimon Glass 				   const char *cells_name,
72257068a7aSSimon Glass 				   int cell_count, int index,
72357068a7aSSimon Glass 				   struct fdtdec_phandle_args *out_args)
72457068a7aSSimon Glass {
72557068a7aSSimon Glass 	const __be32 *list, *list_end;
72657068a7aSSimon Glass 	int rc = 0, size, cur_index = 0;
72757068a7aSSimon Glass 	uint32_t count = 0;
72857068a7aSSimon Glass 	int node = -1;
72957068a7aSSimon Glass 	int phandle;
73057068a7aSSimon Glass 
73157068a7aSSimon Glass 	/* Retrieve the phandle list property */
73257068a7aSSimon Glass 	list = fdt_getprop(blob, src_node, list_name, &size);
73357068a7aSSimon Glass 	if (!list)
73457068a7aSSimon Glass 		return -ENOENT;
73557068a7aSSimon Glass 	list_end = list + size / sizeof(*list);
73657068a7aSSimon Glass 
73757068a7aSSimon Glass 	/* Loop over the phandles until all the requested entry is found */
73857068a7aSSimon Glass 	while (list < list_end) {
73957068a7aSSimon Glass 		rc = -EINVAL;
74057068a7aSSimon Glass 		count = 0;
74157068a7aSSimon Glass 
74257068a7aSSimon Glass 		/*
74357068a7aSSimon Glass 		 * If phandle is 0, then it is an empty entry with no
74457068a7aSSimon Glass 		 * arguments.  Skip forward to the next entry.
74557068a7aSSimon Glass 		 */
74657068a7aSSimon Glass 		phandle = be32_to_cpup(list++);
74757068a7aSSimon Glass 		if (phandle) {
74857068a7aSSimon Glass 			/*
74957068a7aSSimon Glass 			 * Find the provider node and parse the #*-cells
75057068a7aSSimon Glass 			 * property to determine the argument length.
75157068a7aSSimon Glass 			 *
75257068a7aSSimon Glass 			 * This is not needed if the cell count is hard-coded
75357068a7aSSimon Glass 			 * (i.e. cells_name not set, but cell_count is set),
75457068a7aSSimon Glass 			 * except when we're going to return the found node
75557068a7aSSimon Glass 			 * below.
75657068a7aSSimon Glass 			 */
75757068a7aSSimon Glass 			if (cells_name || cur_index == index) {
75857068a7aSSimon Glass 				node = fdt_node_offset_by_phandle(blob,
75957068a7aSSimon Glass 								  phandle);
76057068a7aSSimon Glass 				if (!node) {
76157068a7aSSimon Glass 					debug("%s: could not find phandle\n",
76257068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
76357068a7aSSimon Glass 							   NULL));
76457068a7aSSimon Glass 					goto err;
76557068a7aSSimon Glass 				}
76657068a7aSSimon Glass 			}
76757068a7aSSimon Glass 
76857068a7aSSimon Glass 			if (cells_name) {
76957068a7aSSimon Glass 				count = fdtdec_get_int(blob, node, cells_name,
77057068a7aSSimon Glass 						       -1);
77157068a7aSSimon Glass 				if (count == -1) {
77257068a7aSSimon Glass 					debug("%s: could not get %s for %s\n",
77357068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
77457068a7aSSimon Glass 							   NULL),
77557068a7aSSimon Glass 					      cells_name,
77657068a7aSSimon Glass 					      fdt_get_name(blob, node,
77757068a7aSSimon Glass 							   NULL));
77857068a7aSSimon Glass 					goto err;
77957068a7aSSimon Glass 				}
78057068a7aSSimon Glass 			} else {
78157068a7aSSimon Glass 				count = cell_count;
78257068a7aSSimon Glass 			}
78357068a7aSSimon Glass 
78457068a7aSSimon Glass 			/*
78557068a7aSSimon Glass 			 * Make sure that the arguments actually fit in the
78657068a7aSSimon Glass 			 * remaining property data length
78757068a7aSSimon Glass 			 */
78857068a7aSSimon Glass 			if (list + count > list_end) {
78957068a7aSSimon Glass 				debug("%s: arguments longer than property\n",
79057068a7aSSimon Glass 				      fdt_get_name(blob, src_node, NULL));
79157068a7aSSimon Glass 				goto err;
79257068a7aSSimon Glass 			}
79357068a7aSSimon Glass 		}
79457068a7aSSimon Glass 
79557068a7aSSimon Glass 		/*
79657068a7aSSimon Glass 		 * All of the error cases above bail out of the loop, so at
79757068a7aSSimon Glass 		 * this point, the parsing is successful. If the requested
79857068a7aSSimon Glass 		 * index matches, then fill the out_args structure and return,
79957068a7aSSimon Glass 		 * or return -ENOENT for an empty entry.
80057068a7aSSimon Glass 		 */
80157068a7aSSimon Glass 		rc = -ENOENT;
80257068a7aSSimon Glass 		if (cur_index == index) {
80357068a7aSSimon Glass 			if (!phandle)
80457068a7aSSimon Glass 				goto err;
80557068a7aSSimon Glass 
80657068a7aSSimon Glass 			if (out_args) {
80757068a7aSSimon Glass 				int i;
80857068a7aSSimon Glass 
80957068a7aSSimon Glass 				if (count > MAX_PHANDLE_ARGS) {
81057068a7aSSimon Glass 					debug("%s: too many arguments %d\n",
81157068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
81257068a7aSSimon Glass 							   NULL), count);
81357068a7aSSimon Glass 					count = MAX_PHANDLE_ARGS;
81457068a7aSSimon Glass 				}
81557068a7aSSimon Glass 				out_args->node = node;
81657068a7aSSimon Glass 				out_args->args_count = count;
81757068a7aSSimon Glass 				for (i = 0; i < count; i++) {
81857068a7aSSimon Glass 					out_args->args[i] =
81957068a7aSSimon Glass 							be32_to_cpup(list++);
82057068a7aSSimon Glass 				}
82157068a7aSSimon Glass 			}
82257068a7aSSimon Glass 
82357068a7aSSimon Glass 			/* Found it! return success */
82457068a7aSSimon Glass 			return 0;
82557068a7aSSimon Glass 		}
82657068a7aSSimon Glass 
82757068a7aSSimon Glass 		node = -1;
82857068a7aSSimon Glass 		list += count;
82957068a7aSSimon Glass 		cur_index++;
83057068a7aSSimon Glass 	}
83157068a7aSSimon Glass 
83257068a7aSSimon Glass 	/*
83357068a7aSSimon Glass 	 * Result will be one of:
83457068a7aSSimon Glass 	 * -ENOENT : index is for empty phandle
83557068a7aSSimon Glass 	 * -EINVAL : parsing error on data
83657068a7aSSimon Glass 	 * [1..n]  : Number of phandle (count mode; when index = -1)
83757068a7aSSimon Glass 	 */
83857068a7aSSimon Glass 	rc = index < 0 ? cur_index : -ENOENT;
83957068a7aSSimon Glass  err:
84057068a7aSSimon Glass 	return rc;
84157068a7aSSimon Glass }
84257068a7aSSimon Glass 
8431889a7e2SPeng Fan int fdtdec_get_child_count(const void *blob, int node)
8441889a7e2SPeng Fan {
8451889a7e2SPeng Fan 	int subnode;
8461889a7e2SPeng Fan 	int num = 0;
8471889a7e2SPeng Fan 
848df87e6b1SSimon Glass 	fdt_for_each_subnode(subnode, blob, node)
8491889a7e2SPeng Fan 		num++;
8501889a7e2SPeng Fan 
8511889a7e2SPeng Fan 	return num;
8521889a7e2SPeng Fan }
8531889a7e2SPeng Fan 
854bed4d892SAnton Staff int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
855bed4d892SAnton Staff 		u8 *array, int count)
856bed4d892SAnton Staff {
857bed4d892SAnton Staff 	const u8 *cell;
858bed4d892SAnton Staff 	int err;
859bed4d892SAnton Staff 
860bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
861bed4d892SAnton Staff 	if (!err)
862bed4d892SAnton Staff 		memcpy(array, cell, count);
863bed4d892SAnton Staff 	return err;
864bed4d892SAnton Staff }
865bed4d892SAnton Staff 
866bed4d892SAnton Staff const u8 *fdtdec_locate_byte_array(const void *blob, int node,
867bed4d892SAnton Staff 			     const char *prop_name, int count)
868bed4d892SAnton Staff {
869bed4d892SAnton Staff 	const u8 *cell;
870bed4d892SAnton Staff 	int err;
871bed4d892SAnton Staff 
872bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
873bed4d892SAnton Staff 	if (err)
874bed4d892SAnton Staff 		return NULL;
875bed4d892SAnton Staff 	return cell;
876bed4d892SAnton Staff }
87709258f1eSAbhilash Kesavan 
87809258f1eSAbhilash Kesavan int fdtdec_get_config_int(const void *blob, const char *prop_name,
87909258f1eSAbhilash Kesavan 		int default_val)
88009258f1eSAbhilash Kesavan {
88109258f1eSAbhilash Kesavan 	int config_node;
88209258f1eSAbhilash Kesavan 
88309258f1eSAbhilash Kesavan 	debug("%s: %s\n", __func__, prop_name);
88409258f1eSAbhilash Kesavan 	config_node = fdt_path_offset(blob, "/config");
88509258f1eSAbhilash Kesavan 	if (config_node < 0)
88609258f1eSAbhilash Kesavan 		return default_val;
88709258f1eSAbhilash Kesavan 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
88809258f1eSAbhilash Kesavan }
889332ab0d5SSimon Glass 
89079289c0bSGabe Black int fdtdec_get_config_bool(const void *blob, const char *prop_name)
89179289c0bSGabe Black {
89279289c0bSGabe Black 	int config_node;
89379289c0bSGabe Black 	const void *prop;
89479289c0bSGabe Black 
89579289c0bSGabe Black 	debug("%s: %s\n", __func__, prop_name);
89679289c0bSGabe Black 	config_node = fdt_path_offset(blob, "/config");
89779289c0bSGabe Black 	if (config_node < 0)
89879289c0bSGabe Black 		return 0;
89979289c0bSGabe Black 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
90079289c0bSGabe Black 
90179289c0bSGabe Black 	return prop != NULL;
90279289c0bSGabe Black }
90379289c0bSGabe Black 
904332ab0d5SSimon Glass char *fdtdec_get_config_string(const void *blob, const char *prop_name)
905332ab0d5SSimon Glass {
906332ab0d5SSimon Glass 	const char *nodep;
907332ab0d5SSimon Glass 	int nodeoffset;
908332ab0d5SSimon Glass 	int len;
909332ab0d5SSimon Glass 
910332ab0d5SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
911332ab0d5SSimon Glass 	nodeoffset = fdt_path_offset(blob, "/config");
912332ab0d5SSimon Glass 	if (nodeoffset < 0)
913332ab0d5SSimon Glass 		return NULL;
914332ab0d5SSimon Glass 
915332ab0d5SSimon Glass 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
916332ab0d5SSimon Glass 	if (!nodep)
917332ab0d5SSimon Glass 		return NULL;
918332ab0d5SSimon Glass 
919332ab0d5SSimon Glass 	return (char *)nodep;
920332ab0d5SSimon Glass }
921f20c4619SSimon Glass 
92276489832SSimon Glass int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
92376489832SSimon Glass 			 fdt_addr_t *basep, fdt_size_t *sizep)
924f20c4619SSimon Glass {
925f20c4619SSimon Glass 	const fdt_addr_t *cell;
926f20c4619SSimon Glass 	int len;
927f20c4619SSimon Glass 
92876489832SSimon Glass 	debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
92976489832SSimon Glass 	      prop_name);
930f20c4619SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
93176489832SSimon Glass 	if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
93276489832SSimon Glass 		debug("cell=%p, len=%d\n", cell, len);
933f20c4619SSimon Glass 		return -1;
93476489832SSimon Glass 	}
935f20c4619SSimon Glass 
93676489832SSimon Glass 	*basep = fdt_addr_to_cpu(*cell);
93776489832SSimon Glass 	*sizep = fdt_size_to_cpu(cell[1]);
93876489832SSimon Glass 	debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
93976489832SSimon Glass 	      (ulong)*sizep);
94076489832SSimon Glass 
941f20c4619SSimon Glass 	return 0;
942f20c4619SSimon Glass }
943006e73b9SSimon Glass 
944006e73b9SSimon Glass /**
945006e73b9SSimon Glass  * Read a flash entry from the fdt
946006e73b9SSimon Glass  *
947006e73b9SSimon Glass  * @param blob		FDT blob
948006e73b9SSimon Glass  * @param node		Offset of node to read
949006e73b9SSimon Glass  * @param name		Name of node being read
950006e73b9SSimon Glass  * @param entry		Place to put offset and size of this node
951006e73b9SSimon Glass  * @return 0 if ok, -ve on error
952006e73b9SSimon Glass  */
953006e73b9SSimon Glass int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
954006e73b9SSimon Glass 			   struct fmap_entry *entry)
955006e73b9SSimon Glass {
956f3cc44f9SSimon Glass 	const char *prop;
957006e73b9SSimon Glass 	u32 reg[2];
958006e73b9SSimon Glass 
959006e73b9SSimon Glass 	if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
960006e73b9SSimon Glass 		debug("Node '%s' has bad/missing 'reg' property\n", name);
961006e73b9SSimon Glass 		return -FDT_ERR_NOTFOUND;
962006e73b9SSimon Glass 	}
963006e73b9SSimon Glass 	entry->offset = reg[0];
964006e73b9SSimon Glass 	entry->length = reg[1];
965f3cc44f9SSimon Glass 	entry->used = fdtdec_get_int(blob, node, "used", entry->length);
966f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "compress", NULL);
967f3cc44f9SSimon Glass 	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
968f3cc44f9SSimon Glass 		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
969f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
970f3cc44f9SSimon Glass 	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
971f3cc44f9SSimon Glass 	entry->hash = (uint8_t *)prop;
972006e73b9SSimon Glass 
973006e73b9SSimon Glass 	return 0;
974006e73b9SSimon Glass }
97556f42242SThierry Reding 
9765f7bfdd6SSimon Glass u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
97756f42242SThierry Reding {
97856f42242SThierry Reding 	u64 number = 0;
97956f42242SThierry Reding 
98056f42242SThierry Reding 	while (cells--)
98156f42242SThierry Reding 		number = (number << 32) | fdt32_to_cpu(*ptr++);
98256f42242SThierry Reding 
98356f42242SThierry Reding 	return number;
98456f42242SThierry Reding }
98556f42242SThierry Reding 
98656f42242SThierry Reding int fdt_get_resource(const void *fdt, int node, const char *property,
98756f42242SThierry Reding 		     unsigned int index, struct fdt_resource *res)
98856f42242SThierry Reding {
98956f42242SThierry Reding 	const fdt32_t *ptr, *end;
99056f42242SThierry Reding 	int na, ns, len, parent;
99156f42242SThierry Reding 	unsigned int i = 0;
99256f42242SThierry Reding 
99356f42242SThierry Reding 	parent = fdt_parent_offset(fdt, node);
99456f42242SThierry Reding 	if (parent < 0)
99556f42242SThierry Reding 		return parent;
99656f42242SThierry Reding 
99756f42242SThierry Reding 	na = fdt_address_cells(fdt, parent);
99856f42242SThierry Reding 	ns = fdt_size_cells(fdt, parent);
99956f42242SThierry Reding 
100056f42242SThierry Reding 	ptr = fdt_getprop(fdt, node, property, &len);
100156f42242SThierry Reding 	if (!ptr)
100256f42242SThierry Reding 		return len;
100356f42242SThierry Reding 
100456f42242SThierry Reding 	end = ptr + len / sizeof(*ptr);
100556f42242SThierry Reding 
100656f42242SThierry Reding 	while (ptr + na + ns <= end) {
100756f42242SThierry Reding 		if (i == index) {
100856f42242SThierry Reding 			res->start = res->end = fdtdec_get_number(ptr, na);
100956f42242SThierry Reding 			res->end += fdtdec_get_number(&ptr[na], ns) - 1;
101056f42242SThierry Reding 			return 0;
101156f42242SThierry Reding 		}
101256f42242SThierry Reding 
101356f42242SThierry Reding 		ptr += na + ns;
101456f42242SThierry Reding 		i++;
101556f42242SThierry Reding 	}
101656f42242SThierry Reding 
101756f42242SThierry Reding 	return -FDT_ERR_NOTFOUND;
101856f42242SThierry Reding }
101956f42242SThierry Reding 
102056f42242SThierry Reding int fdt_get_named_resource(const void *fdt, int node, const char *property,
102156f42242SThierry Reding 			   const char *prop_names, const char *name,
102256f42242SThierry Reding 			   struct fdt_resource *res)
102356f42242SThierry Reding {
102456f42242SThierry Reding 	int index;
102556f42242SThierry Reding 
1026b02e4044SSimon Glass 	index = fdt_stringlist_search(fdt, node, prop_names, name);
102756f42242SThierry Reding 	if (index < 0)
102856f42242SThierry Reding 		return index;
102956f42242SThierry Reding 
103056f42242SThierry Reding 	return fdt_get_resource(fdt, node, property, index, res);
103156f42242SThierry Reding }
10329f85eee7SThierry Reding 
103326403871SSimon Glass int fdtdec_decode_memory_region(const void *blob, int config_node,
103426403871SSimon Glass 				const char *mem_type, const char *suffix,
103526403871SSimon Glass 				fdt_addr_t *basep, fdt_size_t *sizep)
103626403871SSimon Glass {
103726403871SSimon Glass 	char prop_name[50];
103826403871SSimon Glass 	const char *mem;
103926403871SSimon Glass 	fdt_size_t size, offset_size;
104026403871SSimon Glass 	fdt_addr_t base, offset;
104126403871SSimon Glass 	int node;
104226403871SSimon Glass 
104326403871SSimon Glass 	if (config_node == -1) {
104426403871SSimon Glass 		config_node = fdt_path_offset(blob, "/config");
104526403871SSimon Glass 		if (config_node < 0) {
104626403871SSimon Glass 			debug("%s: Cannot find /config node\n", __func__);
104726403871SSimon Glass 			return -ENOENT;
104826403871SSimon Glass 		}
104926403871SSimon Glass 	}
105026403871SSimon Glass 	if (!suffix)
105126403871SSimon Glass 		suffix = "";
105226403871SSimon Glass 
105326403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
105426403871SSimon Glass 		 suffix);
105526403871SSimon Glass 	mem = fdt_getprop(blob, config_node, prop_name, NULL);
105626403871SSimon Glass 	if (!mem) {
105726403871SSimon Glass 		debug("%s: No memory type for '%s', using /memory\n", __func__,
105826403871SSimon Glass 		      prop_name);
105926403871SSimon Glass 		mem = "/memory";
106026403871SSimon Glass 	}
106126403871SSimon Glass 
106226403871SSimon Glass 	node = fdt_path_offset(blob, mem);
106326403871SSimon Glass 	if (node < 0) {
106426403871SSimon Glass 		debug("%s: Failed to find node '%s': %s\n", __func__, mem,
106526403871SSimon Glass 		      fdt_strerror(node));
106626403871SSimon Glass 		return -ENOENT;
106726403871SSimon Glass 	}
106826403871SSimon Glass 
106926403871SSimon Glass 	/*
107026403871SSimon Glass 	 * Not strictly correct - the memory may have multiple banks. We just
107126403871SSimon Glass 	 * use the first
107226403871SSimon Glass 	 */
107326403871SSimon Glass 	if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
107426403871SSimon Glass 		debug("%s: Failed to decode memory region %s\n", __func__,
107526403871SSimon Glass 		      mem);
107626403871SSimon Glass 		return -EINVAL;
107726403871SSimon Glass 	}
107826403871SSimon Glass 
107926403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
108026403871SSimon Glass 		 suffix);
108126403871SSimon Glass 	if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
108226403871SSimon Glass 				 &offset_size)) {
108326403871SSimon Glass 		debug("%s: Failed to decode memory region '%s'\n", __func__,
108426403871SSimon Glass 		      prop_name);
108526403871SSimon Glass 		return -EINVAL;
108626403871SSimon Glass 	}
108726403871SSimon Glass 
108826403871SSimon Glass 	*basep = base + offset;
108926403871SSimon Glass 	*sizep = offset_size;
109026403871SSimon Glass 
109126403871SSimon Glass 	return 0;
109226403871SSimon Glass }
1093b45122fdSSimon Glass 
109412e67114SSimon Glass static int decode_timing_property(const void *blob, int node, const char *name,
109512e67114SSimon Glass 				  struct timing_entry *result)
109612e67114SSimon Glass {
109712e67114SSimon Glass 	int length, ret = 0;
109812e67114SSimon Glass 	const u32 *prop;
109912e67114SSimon Glass 
110012e67114SSimon Glass 	prop = fdt_getprop(blob, node, name, &length);
110112e67114SSimon Glass 	if (!prop) {
110212e67114SSimon Glass 		debug("%s: could not find property %s\n",
110312e67114SSimon Glass 		      fdt_get_name(blob, node, NULL), name);
110412e67114SSimon Glass 		return length;
110512e67114SSimon Glass 	}
110612e67114SSimon Glass 
110712e67114SSimon Glass 	if (length == sizeof(u32)) {
110812e67114SSimon Glass 		result->typ = fdtdec_get_int(blob, node, name, 0);
110912e67114SSimon Glass 		result->min = result->typ;
111012e67114SSimon Glass 		result->max = result->typ;
111112e67114SSimon Glass 	} else {
111212e67114SSimon Glass 		ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
111312e67114SSimon Glass 	}
111412e67114SSimon Glass 
111512e67114SSimon Glass 	return ret;
111612e67114SSimon Glass }
111712e67114SSimon Glass 
111812e67114SSimon Glass int fdtdec_decode_display_timing(const void *blob, int parent, int index,
111912e67114SSimon Glass 				 struct display_timing *dt)
112012e67114SSimon Glass {
112112e67114SSimon Glass 	int i, node, timings_node;
112212e67114SSimon Glass 	u32 val = 0;
112312e67114SSimon Glass 	int ret = 0;
112412e67114SSimon Glass 
112512e67114SSimon Glass 	timings_node = fdt_subnode_offset(blob, parent, "display-timings");
112612e67114SSimon Glass 	if (timings_node < 0)
112712e67114SSimon Glass 		return timings_node;
112812e67114SSimon Glass 
112912e67114SSimon Glass 	for (i = 0, node = fdt_first_subnode(blob, timings_node);
113012e67114SSimon Glass 	     node > 0 && i != index;
113112e67114SSimon Glass 	     node = fdt_next_subnode(blob, node))
113212e67114SSimon Glass 		i++;
113312e67114SSimon Glass 
113412e67114SSimon Glass 	if (node < 0)
113512e67114SSimon Glass 		return node;
113612e67114SSimon Glass 
113712e67114SSimon Glass 	memset(dt, 0, sizeof(*dt));
113812e67114SSimon Glass 
113912e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hback-porch",
114012e67114SSimon Glass 				      &dt->hback_porch);
114112e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hfront-porch",
114212e67114SSimon Glass 				      &dt->hfront_porch);
114312e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
114412e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
114512e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vback-porch",
114612e67114SSimon Glass 				      &dt->vback_porch);
114712e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vfront-porch",
114812e67114SSimon Glass 				      &dt->vfront_porch);
114912e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
115012e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
115112e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "clock-frequency",
115212e67114SSimon Glass 				      &dt->pixelclock);
115312e67114SSimon Glass 
115412e67114SSimon Glass 	dt->flags = 0;
115512e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "vsync-active", -1);
115612e67114SSimon Glass 	if (val != -1) {
115712e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
115812e67114SSimon Glass 				DISPLAY_FLAGS_VSYNC_LOW;
115912e67114SSimon Glass 	}
116012e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "hsync-active", -1);
116112e67114SSimon Glass 	if (val != -1) {
116212e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
116312e67114SSimon Glass 				DISPLAY_FLAGS_HSYNC_LOW;
116412e67114SSimon Glass 	}
116512e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "de-active", -1);
116612e67114SSimon Glass 	if (val != -1) {
116712e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
116812e67114SSimon Glass 				DISPLAY_FLAGS_DE_LOW;
116912e67114SSimon Glass 	}
117012e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
117112e67114SSimon Glass 	if (val != -1) {
117212e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
117312e67114SSimon Glass 				DISPLAY_FLAGS_PIXDATA_NEGEDGE;
117412e67114SSimon Glass 	}
117512e67114SSimon Glass 
117612e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "interlaced"))
117712e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_INTERLACED;
117812e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doublescan"))
117912e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
118012e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doubleclk"))
118112e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
118212e67114SSimon Glass 
118304b9dd10SSimon Glass 	return ret;
118412e67114SSimon Glass }
118512e67114SSimon Glass 
1186623f6019SNathan Rossi int fdtdec_setup_memory_size(void)
1187623f6019SNathan Rossi {
1188623f6019SNathan Rossi 	int ret, mem;
1189623f6019SNathan Rossi 	struct fdt_resource res;
1190623f6019SNathan Rossi 
1191623f6019SNathan Rossi 	mem = fdt_path_offset(gd->fdt_blob, "/memory");
1192623f6019SNathan Rossi 	if (mem < 0) {
1193623f6019SNathan Rossi 		debug("%s: Missing /memory node\n", __func__);
1194623f6019SNathan Rossi 		return -EINVAL;
1195623f6019SNathan Rossi 	}
1196623f6019SNathan Rossi 
1197623f6019SNathan Rossi 	ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
1198623f6019SNathan Rossi 	if (ret != 0) {
1199623f6019SNathan Rossi 		debug("%s: Unable to decode first memory bank\n", __func__);
1200623f6019SNathan Rossi 		return -EINVAL;
1201623f6019SNathan Rossi 	}
1202623f6019SNathan Rossi 
1203623f6019SNathan Rossi 	gd->ram_size = (phys_size_t)(res.end - res.start + 1);
1204623f6019SNathan Rossi 	debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
1205623f6019SNathan Rossi 
1206623f6019SNathan Rossi 	return 0;
1207623f6019SNathan Rossi }
1208623f6019SNathan Rossi 
1209623f6019SNathan Rossi #if defined(CONFIG_NR_DRAM_BANKS)
1210623f6019SNathan Rossi int fdtdec_setup_memory_banksize(void)
1211623f6019SNathan Rossi {
1212623f6019SNathan Rossi 	int bank, ret, mem;
1213623f6019SNathan Rossi 	struct fdt_resource res;
1214623f6019SNathan Rossi 
1215623f6019SNathan Rossi 	mem = fdt_path_offset(gd->fdt_blob, "/memory");
1216623f6019SNathan Rossi 	if (mem < 0) {
1217623f6019SNathan Rossi 		debug("%s: Missing /memory node\n", __func__);
1218623f6019SNathan Rossi 		return -EINVAL;
1219623f6019SNathan Rossi 	}
1220623f6019SNathan Rossi 
1221623f6019SNathan Rossi 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1222623f6019SNathan Rossi 		ret = fdt_get_resource(gd->fdt_blob, mem, "reg", bank, &res);
1223623f6019SNathan Rossi 		if (ret == -FDT_ERR_NOTFOUND)
1224623f6019SNathan Rossi 			break;
1225623f6019SNathan Rossi 		if (ret != 0)
1226623f6019SNathan Rossi 			return -EINVAL;
1227623f6019SNathan Rossi 
1228623f6019SNathan Rossi 		gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
1229623f6019SNathan Rossi 		gd->bd->bi_dram[bank].size =
1230623f6019SNathan Rossi 			(phys_size_t)(res.end - res.start + 1);
1231623f6019SNathan Rossi 
1232623f6019SNathan Rossi 		debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
1233623f6019SNathan Rossi 		      __func__, bank,
1234623f6019SNathan Rossi 		      (unsigned long long)gd->bd->bi_dram[bank].start,
1235623f6019SNathan Rossi 		      (unsigned long long)gd->bd->bi_dram[bank].size);
1236623f6019SNathan Rossi 	}
1237623f6019SNathan Rossi 
1238623f6019SNathan Rossi 	return 0;
1239623f6019SNathan Rossi }
1240623f6019SNathan Rossi #endif
1241623f6019SNathan Rossi 
12420879361fSSimon Glass int fdtdec_setup(void)
1243b45122fdSSimon Glass {
12440f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
1245b45122fdSSimon Glass # ifdef CONFIG_OF_EMBED
1246b45122fdSSimon Glass 	/* Get a pointer to the FDT */
1247b45122fdSSimon Glass 	gd->fdt_blob = __dtb_dt_begin;
1248b45122fdSSimon Glass # elif defined CONFIG_OF_SEPARATE
1249b45122fdSSimon Glass #  ifdef CONFIG_SPL_BUILD
125010172962SSimon Glass 	/* FDT is at end of BSS unless it is in a different memory region */
125110172962SSimon Glass 	if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
125210172962SSimon Glass 		gd->fdt_blob = (ulong *)&_image_binary_end;
125310172962SSimon Glass 	else
1254b45122fdSSimon Glass 		gd->fdt_blob = (ulong *)&__bss_end;
1255b45122fdSSimon Glass #  else
1256b45122fdSSimon Glass 	/* FDT is at end of image */
1257b45122fdSSimon Glass 	gd->fdt_blob = (ulong *)&_end;
125829a23f9dSHeiko Schocher #  endif
125982f766d1SAlex Deymo # elif defined(CONFIG_OF_BOARD)
126082f766d1SAlex Deymo 	/* Allow the board to override the fdt address. */
126182f766d1SAlex Deymo 	gd->fdt_blob = board_fdt_blob_setup();
1262b45122fdSSimon Glass # elif defined(CONFIG_OF_HOSTFILE)
1263b45122fdSSimon Glass 	if (sandbox_read_fdt_from_file()) {
1264b45122fdSSimon Glass 		puts("Failed to read control FDT\n");
1265b45122fdSSimon Glass 		return -1;
1266b45122fdSSimon Glass 	}
1267b45122fdSSimon Glass # endif
1268b45122fdSSimon Glass # ifndef CONFIG_SPL_BUILD
1269b45122fdSSimon Glass 	/* Allow the early environment to override the fdt address */
1270b45122fdSSimon Glass 	gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
1271b45122fdSSimon Glass 						(uintptr_t)gd->fdt_blob);
1272b45122fdSSimon Glass # endif
1273b45122fdSSimon Glass #endif
12740879361fSSimon Glass 	return fdtdec_prepare_fdt();
1275b45122fdSSimon Glass }
1276b45122fdSSimon Glass 
1277b45122fdSSimon Glass #endif /* !USE_HOSTCC */
1278