xref: /rk3399_rockchip-uboot/lib/fdtdec.c (revision bfebc8c965e41d62dc6355d09bdd63ca57011b99)
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
72059ecf3SCooper Jr., Franklin #include <boot_fit.h>
8b5220bc6SSimon Glass #include <common.h>
9fcc0a877SSimon Glass #include <dm.h>
105c33c9fdSSimon Glass #include <errno.h>
11b5220bc6SSimon Glass #include <serial.h>
12b5220bc6SSimon Glass #include <libfdt.h>
136e06acb7SStephen Warren #include <fdt_support.h>
14b5220bc6SSimon Glass #include <fdtdec.h>
15b45122fdSSimon Glass #include <asm/sections.h>
16b7e0d73bSSimon Glass #include <dm/of_extra.h>
175c33c9fdSSimon Glass #include <linux/ctype.h>
18b5220bc6SSimon Glass 
19b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
20b5220bc6SSimon Glass 
21b5220bc6SSimon Glass /*
22b5220bc6SSimon Glass  * Here are the type we know about. One day we might allow drivers to
23b5220bc6SSimon Glass  * register. For now we just put them here. The COMPAT macro allows us to
24b5220bc6SSimon Glass  * turn this into a sparse list later, and keeps the ID with the name.
2501a227dfSSimon Glass  *
2601a227dfSSimon Glass  * NOTE: This list is basically a TODO list for things that need to be
2701a227dfSSimon Glass  * converted to driver model. So don't add new things here unless there is a
2801a227dfSSimon Glass  * good reason why driver-model conversion is infeasible. Examples include
2901a227dfSSimon Glass  * things which are used before driver model is available.
30b5220bc6SSimon Glass  */
31b5220bc6SSimon Glass #define COMPAT(id, name) name
32b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = {
33f88fe2deSSimon Glass 	COMPAT(UNKNOWN, "<none>"),
340e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
350e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
36312693c3SJim Lin 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
3779c7a90fSThierry Reding 	COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
387aaa5a60STom Warren 	COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
39cc9fe33aSHatim RV 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
40cc9fe33aSHatim RV 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
41c34253d1SRajeshwari Shinde 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
4272dbff12SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
4372dbff12SRajeshwari Shinde 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
446abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
45108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
46618766c0SAkshay Saraswat 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
47de461c52SPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
487d3ca0f8SJaehoon Chung 	COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
493577fe8bSPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
50f37df0f8SPrzemyslaw Marczak 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
51bb8215f4SSimon Glass 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
527772bb78SRajeshwari Shinde 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
53ecbd7e1eSnaveen krishna chatradhi 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
5445c480c9SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
5577f9b1fbSSimon Glass 	COMPAT(INTEL_MICROCODE, "intel,microcode"),
566173c45bSThierry Reding 	COMPAT(AMS_AS3722, "ams,as3722"),
57c89ada01SBin Meng 	COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
586ab00db2SMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
59129adf5bSMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
60ef4b01b2SMarek Vasut 	COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
6139ea0ee9SSimon Glass 	COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
6239ea0ee9SSimon Glass 	COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
6339ea0ee9SSimon Glass 	COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
644ccae81cSBoris Brezillon 	COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
65e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
66e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
67e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
68e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
69e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
70e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
71e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
72e11b5e8dSLey Foon Tan 	COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
73b5220bc6SSimon Glass };
74b5220bc6SSimon Glass 
75a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id)
76a53f4a29SSimon Glass {
77a53f4a29SSimon Glass 	/* We allow reading of the 'unknown' ID for testing purposes */
78a53f4a29SSimon Glass 	assert(id >= 0 && id < COMPAT_COUNT);
79a53f4a29SSimon Glass 	return compat_names[id];
80a53f4a29SSimon Glass }
81a53f4a29SSimon Glass 
8202464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
8302464e38SStephen Warren 		const char *prop_name, int index, int na, int ns,
846e06acb7SStephen Warren 		fdt_size_t *sizep, bool translate)
8502464e38SStephen Warren {
8602464e38SStephen Warren 	const fdt32_t *prop, *prop_end;
8702464e38SStephen Warren 	const fdt32_t *prop_addr, *prop_size, *prop_after_size;
8802464e38SStephen Warren 	int len;
8902464e38SStephen Warren 	fdt_addr_t addr;
9002464e38SStephen Warren 
9102464e38SStephen Warren 	debug("%s: %s: ", __func__, prop_name);
9202464e38SStephen Warren 
9302464e38SStephen Warren 	if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
9402464e38SStephen Warren 		debug("(na too large for fdt_addr_t type)\n");
9502464e38SStephen Warren 		return FDT_ADDR_T_NONE;
9602464e38SStephen Warren 	}
9702464e38SStephen Warren 
9802464e38SStephen Warren 	if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
9902464e38SStephen Warren 		debug("(ns too large for fdt_size_t type)\n");
10002464e38SStephen Warren 		return FDT_ADDR_T_NONE;
10102464e38SStephen Warren 	}
10202464e38SStephen Warren 
10302464e38SStephen Warren 	prop = fdt_getprop(blob, node, prop_name, &len);
10402464e38SStephen Warren 	if (!prop) {
10502464e38SStephen Warren 		debug("(not found)\n");
10602464e38SStephen Warren 		return FDT_ADDR_T_NONE;
10702464e38SStephen Warren 	}
10802464e38SStephen Warren 	prop_end = prop + (len / sizeof(*prop));
10902464e38SStephen Warren 
11002464e38SStephen Warren 	prop_addr = prop + (index * (na + ns));
11102464e38SStephen Warren 	prop_size = prop_addr + na;
11202464e38SStephen Warren 	prop_after_size = prop_size + ns;
11302464e38SStephen Warren 	if (prop_after_size > prop_end) {
11402464e38SStephen Warren 		debug("(not enough data: expected >= %d cells, got %d cells)\n",
11502464e38SStephen Warren 		      (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
11602464e38SStephen Warren 		return FDT_ADDR_T_NONE;
11702464e38SStephen Warren 	}
11802464e38SStephen Warren 
1195efa1bfbSVignesh R #if CONFIG_IS_ENABLED(OF_TRANSLATE)
1206e06acb7SStephen Warren 	if (translate)
1216e06acb7SStephen Warren 		addr = fdt_translate_address(blob, node, prop_addr);
1226e06acb7SStephen Warren 	else
1236e06acb7SStephen Warren #endif
12402464e38SStephen Warren 		addr = fdtdec_get_number(prop_addr, na);
12502464e38SStephen Warren 
12602464e38SStephen Warren 	if (sizep) {
12702464e38SStephen Warren 		*sizep = fdtdec_get_number(prop_size, ns);
128fd30d2c6SSimon Glass 		debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
129fd30d2c6SSimon Glass 		      (unsigned long long)*sizep);
13002464e38SStephen Warren 	} else {
131fd30d2c6SSimon Glass 		debug("addr=%08llx\n", (unsigned long long)addr);
13202464e38SStephen Warren 	}
13302464e38SStephen Warren 
13402464e38SStephen Warren 	return addr;
13502464e38SStephen Warren }
13602464e38SStephen Warren 
13702464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
1386e06acb7SStephen Warren 		int node, const char *prop_name, int index, fdt_size_t *sizep,
1396e06acb7SStephen Warren 		bool translate)
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,
1606e06acb7SStephen Warren 					  ns, sizep, translate);
16102464e38SStephen Warren }
16202464e38SStephen Warren 
16302464e38SStephen Warren fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
1646e06acb7SStephen Warren 		const char *prop_name, int index, fdt_size_t *sizep,
1656e06acb7SStephen Warren 		bool translate)
16602464e38SStephen Warren {
16702464e38SStephen Warren 	int parent;
16802464e38SStephen Warren 
16902464e38SStephen Warren 	debug("%s: ", __func__);
17002464e38SStephen Warren 
17102464e38SStephen Warren 	parent = fdt_parent_offset(blob, node);
17202464e38SStephen Warren 	if (parent < 0) {
17302464e38SStephen Warren 		debug("(no parent found)\n");
17402464e38SStephen Warren 		return FDT_ADDR_T_NONE;
17502464e38SStephen Warren 	}
17602464e38SStephen Warren 
17702464e38SStephen Warren 	return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
1786e06acb7SStephen Warren 						index, sizep, translate);
17902464e38SStephen Warren }
18002464e38SStephen Warren 
1814397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
1824397a2a8SSimon Glass 		const char *prop_name, fdt_size_t *sizep)
183b5220bc6SSimon Glass {
184d93b9a07SStephen Warren 	int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
185d93b9a07SStephen Warren 
18602464e38SStephen Warren 	return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
18702464e38SStephen Warren 					  sizeof(fdt_addr_t) / sizeof(fdt32_t),
1886e06acb7SStephen Warren 					  ns, sizep, false);
189b5220bc6SSimon Glass }
190b5220bc6SSimon Glass 
1914397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
1924397a2a8SSimon Glass 		const char *prop_name)
1934397a2a8SSimon Glass {
1944397a2a8SSimon Glass 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
1954397a2a8SSimon Glass }
1964397a2a8SSimon Glass 
197fcc0a877SSimon Glass #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
198a62e84d7SBin Meng int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
199a62e84d7SBin Meng 		const char *prop_name, struct fdt_pci_addr *addr)
200a62e84d7SBin Meng {
201a62e84d7SBin Meng 	const u32 *cell;
202a62e84d7SBin Meng 	int len;
203a62e84d7SBin Meng 	int ret = -ENOENT;
204a62e84d7SBin Meng 
205a62e84d7SBin Meng 	debug("%s: %s: ", __func__, prop_name);
206a62e84d7SBin Meng 
207a62e84d7SBin Meng 	/*
208a62e84d7SBin Meng 	 * If we follow the pci bus bindings strictly, we should check
209a62e84d7SBin Meng 	 * the value of the node's parent node's #address-cells and
210a62e84d7SBin Meng 	 * #size-cells. They need to be 3 and 2 accordingly. However,
211a62e84d7SBin Meng 	 * for simplicity we skip the check here.
212a62e84d7SBin Meng 	 */
213a62e84d7SBin Meng 	cell = fdt_getprop(blob, node, prop_name, &len);
214a62e84d7SBin Meng 	if (!cell)
215a62e84d7SBin Meng 		goto fail;
216a62e84d7SBin Meng 
217a62e84d7SBin Meng 	if ((len % FDT_PCI_REG_SIZE) == 0) {
218a62e84d7SBin Meng 		int num = len / FDT_PCI_REG_SIZE;
219a62e84d7SBin Meng 		int i;
220a62e84d7SBin Meng 
221a62e84d7SBin Meng 		for (i = 0; i < num; i++) {
222a62e84d7SBin Meng 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
2234ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[0]),
2244ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[1]),
2254ea5243aSStephen Warren 			      (ulong)fdt32_to_cpu(cell[2]));
2264ea5243aSStephen Warren 			if ((fdt32_to_cpu(*cell) & type) == type) {
2274ea5243aSStephen Warren 				addr->phys_hi = fdt32_to_cpu(cell[0]);
2284ea5243aSStephen Warren 				addr->phys_mid = fdt32_to_cpu(cell[1]);
2294ea5243aSStephen Warren 				addr->phys_lo = fdt32_to_cpu(cell[1]);
230a62e84d7SBin Meng 				break;
231a62e84d7SBin Meng 			} else {
232a62e84d7SBin Meng 				cell += (FDT_PCI_ADDR_CELLS +
233a62e84d7SBin Meng 					 FDT_PCI_SIZE_CELLS);
234a62e84d7SBin Meng 			}
235a62e84d7SBin Meng 		}
236a62e84d7SBin Meng 
237106cce96SSimon Glass 		if (i == num) {
238106cce96SSimon Glass 			ret = -ENXIO;
239a62e84d7SBin Meng 			goto fail;
240106cce96SSimon Glass 		}
241a62e84d7SBin Meng 
242a62e84d7SBin Meng 		return 0;
243a62e84d7SBin Meng 	} else {
244a62e84d7SBin Meng 		ret = -EINVAL;
245a62e84d7SBin Meng 	}
246a62e84d7SBin Meng 
247a62e84d7SBin Meng fail:
248a62e84d7SBin Meng 	debug("(not found)\n");
249a62e84d7SBin Meng 	return ret;
250a62e84d7SBin Meng }
251a62e84d7SBin Meng 
252a62e84d7SBin Meng int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
253a62e84d7SBin Meng {
254a62e84d7SBin Meng 	const char *list, *end;
255a62e84d7SBin Meng 	int len;
256a62e84d7SBin Meng 
257a62e84d7SBin Meng 	list = fdt_getprop(blob, node, "compatible", &len);
258a62e84d7SBin Meng 	if (!list)
259a62e84d7SBin Meng 		return -ENOENT;
260a62e84d7SBin Meng 
261a62e84d7SBin Meng 	end = list + len;
262a62e84d7SBin Meng 	while (list < end) {
263a62e84d7SBin Meng 		char *s;
264a62e84d7SBin Meng 
265a62e84d7SBin Meng 		len = strlen(list);
266a62e84d7SBin Meng 		if (len >= strlen("pciVVVV,DDDD")) {
267a62e84d7SBin Meng 			s = strstr(list, "pci");
268a62e84d7SBin Meng 
269a62e84d7SBin Meng 			/*
270a62e84d7SBin Meng 			 * check if the string is something like pciVVVV,DDDD.RR
271a62e84d7SBin Meng 			 * or just pciVVVV,DDDD
272a62e84d7SBin Meng 			 */
273a62e84d7SBin Meng 			if (s && s[7] == ',' &&
274a62e84d7SBin Meng 			    (s[12] == '.' || s[12] == 0)) {
275a62e84d7SBin Meng 				s += 3;
276a62e84d7SBin Meng 				*vendor = simple_strtol(s, NULL, 16);
277a62e84d7SBin Meng 
278a62e84d7SBin Meng 				s += 5;
279a62e84d7SBin Meng 				*device = simple_strtol(s, NULL, 16);
280a62e84d7SBin Meng 
281a62e84d7SBin Meng 				return 0;
282a62e84d7SBin Meng 			}
283a62e84d7SBin Meng 		}
284bc6351ebSBin Meng 		list += (len + 1);
285a62e84d7SBin Meng 	}
286a62e84d7SBin Meng 
287a62e84d7SBin Meng 	return -ENOENT;
288a62e84d7SBin Meng }
289a62e84d7SBin Meng 
290fcc0a877SSimon Glass int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
291fcc0a877SSimon Glass 			 u32 *bar)
292a62e84d7SBin Meng {
293a62e84d7SBin Meng 	int barnum;
294a62e84d7SBin Meng 
295a62e84d7SBin Meng 	/* extract the bar number from fdt_pci_addr */
296a62e84d7SBin Meng 	barnum = addr->phys_hi & 0xff;
297a62e84d7SBin Meng 	if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
298a62e84d7SBin Meng 		return -EINVAL;
299a62e84d7SBin Meng 
300a62e84d7SBin Meng 	barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
301fcc0a877SSimon Glass 	*bar = dm_pci_read_bar32(dev, barnum);
302a62e84d7SBin Meng 
303a62e84d7SBin Meng 	return 0;
304a62e84d7SBin Meng }
305a62e84d7SBin Meng #endif
306a62e84d7SBin Meng 
307aadef0a1SChe-Liang Chiou uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
308aadef0a1SChe-Liang Chiou 		uint64_t default_val)
309aadef0a1SChe-Liang Chiou {
310aadef0a1SChe-Liang Chiou 	const uint64_t *cell64;
311aadef0a1SChe-Liang Chiou 	int length;
312aadef0a1SChe-Liang Chiou 
313aadef0a1SChe-Liang Chiou 	cell64 = fdt_getprop(blob, node, prop_name, &length);
314aadef0a1SChe-Liang Chiou 	if (!cell64 || length < sizeof(*cell64))
315aadef0a1SChe-Liang Chiou 		return default_val;
316aadef0a1SChe-Liang Chiou 
317aadef0a1SChe-Liang Chiou 	return fdt64_to_cpu(*cell64);
318aadef0a1SChe-Liang Chiou }
319aadef0a1SChe-Liang Chiou 
320f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node)
321b5220bc6SSimon Glass {
322b5220bc6SSimon Glass 	const char *cell;
323b5220bc6SSimon Glass 
324f88fe2deSSimon Glass 	/*
325f88fe2deSSimon Glass 	 * It should say "okay", so only allow that. Some fdts use "ok" but
326f88fe2deSSimon Glass 	 * this is a bug. Please fix your device tree source file. See here
327f88fe2deSSimon Glass 	 * for discussion:
328f88fe2deSSimon Glass 	 *
329f88fe2deSSimon Glass 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
330f88fe2deSSimon Glass 	 */
331b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, "status", NULL);
332b5220bc6SSimon Glass 	if (cell)
333f88fe2deSSimon Glass 		return 0 == strcmp(cell, "okay");
334f88fe2deSSimon Glass 	return 1;
335b5220bc6SSimon Glass }
336b5220bc6SSimon Glass 
3377cde397bSGerald Van Baren enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
338b5220bc6SSimon Glass {
339b5220bc6SSimon Glass 	enum fdt_compat_id id;
340b5220bc6SSimon Glass 
341b5220bc6SSimon Glass 	/* Search our drivers */
342b5220bc6SSimon Glass 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
343b5220bc6SSimon Glass 		if (0 == fdt_node_check_compatible(blob, node,
344b5220bc6SSimon Glass 				compat_names[id]))
345b5220bc6SSimon Glass 			return id;
346b5220bc6SSimon Glass 	return COMPAT_UNKNOWN;
347b5220bc6SSimon Glass }
348b5220bc6SSimon Glass 
349b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node,
350b5220bc6SSimon Glass 		enum fdt_compat_id id)
351b5220bc6SSimon Glass {
352b5220bc6SSimon Glass 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
353b5220bc6SSimon Glass }
354b5220bc6SSimon Glass 
3553ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
3563ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp)
3573ddecfc7SSimon Glass {
3583ddecfc7SSimon Glass 	do {
3593ddecfc7SSimon Glass 		node = fdt_next_node(blob, node, depthp);
3603ddecfc7SSimon Glass 	} while (*depthp > 1);
3613ddecfc7SSimon Glass 
3623ddecfc7SSimon Glass 	/* If this is a direct subnode, and compatible, return it */
3633ddecfc7SSimon Glass 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
3643ddecfc7SSimon Glass 						blob, node, compat_names[id]))
3653ddecfc7SSimon Glass 		return node;
3663ddecfc7SSimon Glass 
3673ddecfc7SSimon Glass 	return -FDT_ERR_NOTFOUND;
3683ddecfc7SSimon Glass }
3693ddecfc7SSimon Glass 
370b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
371b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto)
372b5220bc6SSimon Glass {
373b5220bc6SSimon Glass #define MAX_STR_LEN 20
374b5220bc6SSimon Glass 	char str[MAX_STR_LEN + 20];
375b5220bc6SSimon Glass 	int node, err;
376b5220bc6SSimon Glass 
377b5220bc6SSimon Glass 	/* snprintf() is not available */
378b5220bc6SSimon Glass 	assert(strlen(name) < MAX_STR_LEN);
379b5220bc6SSimon Glass 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
38000878476SSimon Glass 	node = fdt_path_offset(blob, str);
381b5220bc6SSimon Glass 	if (node < 0)
382b5220bc6SSimon Glass 		return node;
383b5220bc6SSimon Glass 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
384b5220bc6SSimon Glass 	if (err < 0)
385b5220bc6SSimon Glass 		return err;
386f88fe2deSSimon Glass 	if (err)
387f88fe2deSSimon Glass 		return -FDT_ERR_NOTFOUND;
388f88fe2deSSimon Glass 	(*upto)++;
389f88fe2deSSimon Glass 	return node;
390b5220bc6SSimon Glass }
391b5220bc6SSimon Glass 
392a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
393a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
394a53f4a29SSimon Glass {
395c6782270SSimon Glass 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
396c6782270SSimon Glass 
397c6782270SSimon Glass 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
398c6782270SSimon Glass }
399c6782270SSimon Glass 
400c6782270SSimon Glass /* TODO: Can we tighten this code up a little? */
401c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
402c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
403c6782270SSimon Glass {
404a53f4a29SSimon Glass 	int name_len = strlen(name);
405a53f4a29SSimon Glass 	int nodes[maxcount];
406a53f4a29SSimon Glass 	int num_found = 0;
407a53f4a29SSimon Glass 	int offset, node;
408a53f4a29SSimon Glass 	int alias_node;
409a53f4a29SSimon Glass 	int count;
410a53f4a29SSimon Glass 	int i, j;
411a53f4a29SSimon Glass 
412a53f4a29SSimon Glass 	/* find the alias node if present */
413a53f4a29SSimon Glass 	alias_node = fdt_path_offset(blob, "/aliases");
414a53f4a29SSimon Glass 
415a53f4a29SSimon Glass 	/*
416a53f4a29SSimon Glass 	 * start with nothing, and we can assume that the root node can't
417a53f4a29SSimon Glass 	 * match
418a53f4a29SSimon Glass 	 */
419a53f4a29SSimon Glass 	memset(nodes, '\0', sizeof(nodes));
420a53f4a29SSimon Glass 
421a53f4a29SSimon Glass 	/* First find all the compatible nodes */
422a53f4a29SSimon Glass 	for (node = count = 0; node >= 0 && count < maxcount;) {
423a53f4a29SSimon Glass 		node = fdtdec_next_compatible(blob, node, id);
424a53f4a29SSimon Glass 		if (node >= 0)
425a53f4a29SSimon Glass 			nodes[count++] = node;
426a53f4a29SSimon Glass 	}
427a53f4a29SSimon Glass 	if (node >= 0)
428a53f4a29SSimon Glass 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
429a53f4a29SSimon Glass 		       __func__, name);
430a53f4a29SSimon Glass 
431a53f4a29SSimon Glass 	/* Now find all the aliases */
432a53f4a29SSimon Glass 	for (offset = fdt_first_property_offset(blob, alias_node);
433a53f4a29SSimon Glass 			offset > 0;
434a53f4a29SSimon Glass 			offset = fdt_next_property_offset(blob, offset)) {
435a53f4a29SSimon Glass 		const struct fdt_property *prop;
436a53f4a29SSimon Glass 		const char *path;
437a53f4a29SSimon Glass 		int number;
438a53f4a29SSimon Glass 		int found;
439a53f4a29SSimon Glass 
440a53f4a29SSimon Glass 		node = 0;
441a53f4a29SSimon Glass 		prop = fdt_get_property_by_offset(blob, offset, NULL);
442a53f4a29SSimon Glass 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
443a53f4a29SSimon Glass 		if (prop->len && 0 == strncmp(path, name, name_len))
444a53f4a29SSimon Glass 			node = fdt_path_offset(blob, prop->data);
445a53f4a29SSimon Glass 		if (node <= 0)
446a53f4a29SSimon Glass 			continue;
447a53f4a29SSimon Glass 
448a53f4a29SSimon Glass 		/* Get the alias number */
449a53f4a29SSimon Glass 		number = simple_strtoul(path + name_len, NULL, 10);
450a53f4a29SSimon Glass 		if (number < 0 || number >= maxcount) {
451a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' is out of range\n",
452a53f4a29SSimon Glass 			       __func__, path);
453a53f4a29SSimon Glass 			continue;
454a53f4a29SSimon Glass 		}
455a53f4a29SSimon Glass 
456a53f4a29SSimon Glass 		/* Make sure the node we found is actually in our list! */
457a53f4a29SSimon Glass 		found = -1;
458a53f4a29SSimon Glass 		for (j = 0; j < count; j++)
459a53f4a29SSimon Glass 			if (nodes[j] == node) {
460a53f4a29SSimon Glass 				found = j;
461a53f4a29SSimon Glass 				break;
462a53f4a29SSimon Glass 			}
463a53f4a29SSimon Glass 
464a53f4a29SSimon Glass 		if (found == -1) {
465a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' points to a node "
466a53f4a29SSimon Glass 				"'%s' that is missing or is not compatible "
467a53f4a29SSimon Glass 				" with '%s'\n", __func__, path,
468a53f4a29SSimon Glass 				fdt_get_name(blob, node, NULL),
469a53f4a29SSimon Glass 			       compat_names[id]);
470a53f4a29SSimon Glass 			continue;
471a53f4a29SSimon Glass 		}
472a53f4a29SSimon Glass 
473a53f4a29SSimon Glass 		/*
474a53f4a29SSimon Glass 		 * Add this node to our list in the right place, and mark
475a53f4a29SSimon Glass 		 * it as done.
476a53f4a29SSimon Glass 		 */
477a53f4a29SSimon Glass 		if (fdtdec_get_is_enabled(blob, node)) {
478c6782270SSimon Glass 			if (node_list[number]) {
479c6782270SSimon Glass 				debug("%s: warning: alias '%s' requires that "
480c6782270SSimon Glass 				      "a node be placed in the list in a "
481c6782270SSimon Glass 				      "position which is already filled by "
482c6782270SSimon Glass 				      "node '%s'\n", __func__, path,
483c6782270SSimon Glass 				      fdt_get_name(blob, node, NULL));
484c6782270SSimon Glass 				continue;
485c6782270SSimon Glass 			}
486a53f4a29SSimon Glass 			node_list[number] = node;
487a53f4a29SSimon Glass 			if (number >= num_found)
488a53f4a29SSimon Glass 				num_found = number + 1;
489a53f4a29SSimon Glass 		}
490c6782270SSimon Glass 		nodes[found] = 0;
491a53f4a29SSimon Glass 	}
492a53f4a29SSimon Glass 
493a53f4a29SSimon Glass 	/* Add any nodes not mentioned by an alias */
494a53f4a29SSimon Glass 	for (i = j = 0; i < maxcount; i++) {
495a53f4a29SSimon Glass 		if (!node_list[i]) {
496a53f4a29SSimon Glass 			for (; j < maxcount; j++)
497a53f4a29SSimon Glass 				if (nodes[j] &&
498a53f4a29SSimon Glass 					fdtdec_get_is_enabled(blob, nodes[j]))
499a53f4a29SSimon Glass 					break;
500a53f4a29SSimon Glass 
501a53f4a29SSimon Glass 			/* Have we run out of nodes to add? */
502a53f4a29SSimon Glass 			if (j == maxcount)
503a53f4a29SSimon Glass 				break;
504a53f4a29SSimon Glass 
505a53f4a29SSimon Glass 			assert(!node_list[i]);
506a53f4a29SSimon Glass 			node_list[i] = nodes[j++];
507a53f4a29SSimon Glass 			if (i >= num_found)
508a53f4a29SSimon Glass 				num_found = i + 1;
509a53f4a29SSimon Glass 		}
510a53f4a29SSimon Glass 	}
511a53f4a29SSimon Glass 
512a53f4a29SSimon Glass 	return num_found;
513a53f4a29SSimon Glass }
514a53f4a29SSimon Glass 
5155c33c9fdSSimon Glass int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
5165c33c9fdSSimon Glass 			 int *seqp)
5175c33c9fdSSimon Glass {
5185c33c9fdSSimon Glass 	int base_len = strlen(base);
5195c33c9fdSSimon Glass 	const char *find_name;
5205c33c9fdSSimon Glass 	int find_namelen;
5215c33c9fdSSimon Glass 	int prop_offset;
5225c33c9fdSSimon Glass 	int aliases;
5235c33c9fdSSimon Glass 
5245c33c9fdSSimon Glass 	find_name = fdt_get_name(blob, offset, &find_namelen);
5255c33c9fdSSimon Glass 	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
5265c33c9fdSSimon Glass 
5275c33c9fdSSimon Glass 	aliases = fdt_path_offset(blob, "/aliases");
5285c33c9fdSSimon Glass 	for (prop_offset = fdt_first_property_offset(blob, aliases);
5295c33c9fdSSimon Glass 	     prop_offset > 0;
5305c33c9fdSSimon Glass 	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
5315c33c9fdSSimon Glass 		const char *prop;
5325c33c9fdSSimon Glass 		const char *name;
5335c33c9fdSSimon Glass 		const char *slash;
534c4af6732SSimon Glass 		int len, val;
5355c33c9fdSSimon Glass 
5365c33c9fdSSimon Glass 		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
5375c33c9fdSSimon Glass 		debug("   - %s, %s\n", name, prop);
5385c33c9fdSSimon Glass 		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
5395c33c9fdSSimon Glass 		    strncmp(name, base, base_len))
5405c33c9fdSSimon Glass 			continue;
5415c33c9fdSSimon Glass 
5425c33c9fdSSimon Glass 		slash = strrchr(prop, '/');
5435c33c9fdSSimon Glass 		if (strcmp(slash + 1, find_name))
5445c33c9fdSSimon Glass 			continue;
545c4af6732SSimon Glass 		val = trailing_strtol(name);
546c4af6732SSimon Glass 		if (val != -1) {
547c4af6732SSimon Glass 			*seqp = val;
5485c33c9fdSSimon Glass 			debug("Found seq %d\n", *seqp);
5495c33c9fdSSimon Glass 			return 0;
5505c33c9fdSSimon Glass 		}
5515c33c9fdSSimon Glass 	}
5525c33c9fdSSimon Glass 
5535c33c9fdSSimon Glass 	debug("Not found\n");
5545c33c9fdSSimon Glass 	return -ENOENT;
5555c33c9fdSSimon Glass }
5565c33c9fdSSimon Glass 
5573bc37a50SSimon Glass const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
5583bc37a50SSimon Glass {
5593bc37a50SSimon Glass 	int chosen_node;
5603bc37a50SSimon Glass 
5613bc37a50SSimon Glass 	if (!blob)
5623bc37a50SSimon Glass 		return NULL;
5633bc37a50SSimon Glass 	chosen_node = fdt_path_offset(blob, "/chosen");
5643bc37a50SSimon Glass 	return fdt_getprop(blob, chosen_node, name, NULL);
5653bc37a50SSimon Glass }
5663bc37a50SSimon Glass 
567aac07d49SSimon Glass int fdtdec_get_chosen_node(const void *blob, const char *name)
568aac07d49SSimon Glass {
569aac07d49SSimon Glass 	const char *prop;
570aac07d49SSimon Glass 
5713bc37a50SSimon Glass 	prop = fdtdec_get_chosen_prop(blob, name);
572aac07d49SSimon Glass 	if (!prop)
573aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
574aac07d49SSimon Glass 	return fdt_path_offset(blob, prop);
575aac07d49SSimon Glass }
576aac07d49SSimon Glass 
5779a263e55SSimon Glass int fdtdec_check_fdt(void)
5789a263e55SSimon Glass {
5799a263e55SSimon Glass 	/*
5809a263e55SSimon Glass 	 * We must have an FDT, but we cannot panic() yet since the console
5819a263e55SSimon Glass 	 * is not ready. So for now, just assert(). Boards which need an early
5829a263e55SSimon Glass 	 * FDT (prior to console ready) will need to make their own
5839a263e55SSimon Glass 	 * arrangements and do their own checks.
5849a263e55SSimon Glass 	 */
5859a263e55SSimon Glass 	assert(!fdtdec_prepare_fdt());
5869a263e55SSimon Glass 	return 0;
5879a263e55SSimon Glass }
5889a263e55SSimon Glass 
589b5220bc6SSimon Glass /*
590b5220bc6SSimon Glass  * This function is a little odd in that it accesses global data. At some
591b5220bc6SSimon Glass  * point if the architecture board.c files merge this will make more sense.
592b5220bc6SSimon Glass  * Even now, it is common code.
593b5220bc6SSimon Glass  */
5949a263e55SSimon Glass int fdtdec_prepare_fdt(void)
595b5220bc6SSimon Glass {
596c309c2daSSimon Glass 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
597c309c2daSSimon Glass 	    fdt_check_header(gd->fdt_blob)) {
59866312374SSimon Glass #ifdef CONFIG_SPL_BUILD
59966312374SSimon Glass 		puts("Missing DTB\n");
60066312374SSimon Glass #else
60166312374SSimon 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");
602cb5f97f7SSimon Glass # ifdef DEBUG
603cb5f97f7SSimon Glass 		if (gd->fdt_blob) {
604cb5f97f7SSimon Glass 			printf("fdt_blob=%p\n", gd->fdt_blob);
605cb5f97f7SSimon Glass 			print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
606cb5f97f7SSimon Glass 				     32, 0);
607cb5f97f7SSimon Glass 		}
608cb5f97f7SSimon Glass # endif
60966312374SSimon Glass #endif
6109a263e55SSimon Glass 		return -1;
6119a263e55SSimon Glass 	}
612b5220bc6SSimon Glass 	return 0;
613b5220bc6SSimon Glass }
614d17da655SSimon Glass 
615d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
616d17da655SSimon Glass {
617d17da655SSimon Glass 	const u32 *phandle;
618d17da655SSimon Glass 	int lookup;
619d17da655SSimon Glass 
6201cb2323bSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
621d17da655SSimon Glass 	phandle = fdt_getprop(blob, node, prop_name, NULL);
622d17da655SSimon Glass 	if (!phandle)
623d17da655SSimon Glass 		return -FDT_ERR_NOTFOUND;
624d17da655SSimon Glass 
625d17da655SSimon Glass 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
626d17da655SSimon Glass 	return lookup;
627d17da655SSimon Glass }
628d17da655SSimon Glass 
629d17da655SSimon Glass /**
630d17da655SSimon Glass  * Look up a property in a node and check that it has a minimum length.
631d17da655SSimon Glass  *
632d17da655SSimon Glass  * @param blob		FDT blob
633d17da655SSimon Glass  * @param node		node to examine
634d17da655SSimon Glass  * @param prop_name	name of property to find
635d17da655SSimon Glass  * @param min_len	minimum property length in bytes
636d17da655SSimon Glass  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
637d17da655SSimon Glass 			found, or -FDT_ERR_BADLAYOUT if not enough data
638d17da655SSimon Glass  * @return pointer to cell, which is only valid if err == 0
639d17da655SSimon Glass  */
640d17da655SSimon Glass static const void *get_prop_check_min_len(const void *blob, int node,
641d17da655SSimon Glass 		const char *prop_name, int min_len, int *err)
642d17da655SSimon Glass {
643d17da655SSimon Glass 	const void *cell;
644d17da655SSimon Glass 	int len;
645d17da655SSimon Glass 
646d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
647d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
648d17da655SSimon Glass 	if (!cell)
649d17da655SSimon Glass 		*err = -FDT_ERR_NOTFOUND;
650d17da655SSimon Glass 	else if (len < min_len)
651d17da655SSimon Glass 		*err = -FDT_ERR_BADLAYOUT;
652d17da655SSimon Glass 	else
653d17da655SSimon Glass 		*err = 0;
654d17da655SSimon Glass 	return cell;
655d17da655SSimon Glass }
656d17da655SSimon Glass 
657d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
658d17da655SSimon Glass 		u32 *array, int count)
659d17da655SSimon Glass {
660d17da655SSimon Glass 	const u32 *cell;
661d17da655SSimon Glass 	int i, err = 0;
662d17da655SSimon Glass 
663d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
664d17da655SSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
665d17da655SSimon Glass 				      sizeof(u32) * count, &err);
666d17da655SSimon Glass 	if (!err) {
667d17da655SSimon Glass 		for (i = 0; i < count; i++)
668d17da655SSimon Glass 			array[i] = fdt32_to_cpu(cell[i]);
669d17da655SSimon Glass 	}
670d17da655SSimon Glass 	return err;
671d17da655SSimon Glass }
672d17da655SSimon Glass 
673a9f04d49SSimon Glass int fdtdec_get_int_array_count(const void *blob, int node,
674a9f04d49SSimon Glass 			       const char *prop_name, u32 *array, int count)
675a9f04d49SSimon Glass {
676a9f04d49SSimon Glass 	const u32 *cell;
677a9f04d49SSimon Glass 	int len, elems;
678a9f04d49SSimon Glass 	int i;
679a9f04d49SSimon Glass 
680a9f04d49SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
681a9f04d49SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
682a9f04d49SSimon Glass 	if (!cell)
683a9f04d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
684a9f04d49SSimon Glass 	elems = len / sizeof(u32);
685a9f04d49SSimon Glass 	if (count > elems)
686a9f04d49SSimon Glass 		count = elems;
687a9f04d49SSimon Glass 	for (i = 0; i < count; i++)
688a9f04d49SSimon Glass 		array[i] = fdt32_to_cpu(cell[i]);
689a9f04d49SSimon Glass 
690a9f04d49SSimon Glass 	return count;
691a9f04d49SSimon Glass }
692a9f04d49SSimon Glass 
69396875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
69496875e7dSSimon Glass 			       const char *prop_name, int count)
69596875e7dSSimon Glass {
69696875e7dSSimon Glass 	const u32 *cell;
69796875e7dSSimon Glass 	int err;
69896875e7dSSimon Glass 
69996875e7dSSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
70096875e7dSSimon Glass 				      sizeof(u32) * count, &err);
70196875e7dSSimon Glass 	return err ? NULL : cell;
70296875e7dSSimon Glass }
70396875e7dSSimon Glass 
704d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
705d17da655SSimon Glass {
706d17da655SSimon Glass 	const s32 *cell;
707d17da655SSimon Glass 	int len;
708d17da655SSimon Glass 
709d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
710d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
711d17da655SSimon Glass 	return cell != NULL;
712d17da655SSimon Glass }
713ed3ee5cdSSimon Glass 
71457068a7aSSimon Glass int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
71557068a7aSSimon Glass 				   const char *list_name,
71657068a7aSSimon Glass 				   const char *cells_name,
71757068a7aSSimon Glass 				   int cell_count, int index,
71857068a7aSSimon Glass 				   struct fdtdec_phandle_args *out_args)
71957068a7aSSimon Glass {
72057068a7aSSimon Glass 	const __be32 *list, *list_end;
72157068a7aSSimon Glass 	int rc = 0, size, cur_index = 0;
72257068a7aSSimon Glass 	uint32_t count = 0;
72357068a7aSSimon Glass 	int node = -1;
72457068a7aSSimon Glass 	int phandle;
72557068a7aSSimon Glass 
72657068a7aSSimon Glass 	/* Retrieve the phandle list property */
72757068a7aSSimon Glass 	list = fdt_getprop(blob, src_node, list_name, &size);
72857068a7aSSimon Glass 	if (!list)
72957068a7aSSimon Glass 		return -ENOENT;
73057068a7aSSimon Glass 	list_end = list + size / sizeof(*list);
73157068a7aSSimon Glass 
73257068a7aSSimon Glass 	/* Loop over the phandles until all the requested entry is found */
73357068a7aSSimon Glass 	while (list < list_end) {
73457068a7aSSimon Glass 		rc = -EINVAL;
73557068a7aSSimon Glass 		count = 0;
73657068a7aSSimon Glass 
73757068a7aSSimon Glass 		/*
73857068a7aSSimon Glass 		 * If phandle is 0, then it is an empty entry with no
73957068a7aSSimon Glass 		 * arguments.  Skip forward to the next entry.
74057068a7aSSimon Glass 		 */
74157068a7aSSimon Glass 		phandle = be32_to_cpup(list++);
74257068a7aSSimon Glass 		if (phandle) {
74357068a7aSSimon Glass 			/*
74457068a7aSSimon Glass 			 * Find the provider node and parse the #*-cells
74557068a7aSSimon Glass 			 * property to determine the argument length.
74657068a7aSSimon Glass 			 *
74757068a7aSSimon Glass 			 * This is not needed if the cell count is hard-coded
74857068a7aSSimon Glass 			 * (i.e. cells_name not set, but cell_count is set),
74957068a7aSSimon Glass 			 * except when we're going to return the found node
75057068a7aSSimon Glass 			 * below.
75157068a7aSSimon Glass 			 */
75257068a7aSSimon Glass 			if (cells_name || cur_index == index) {
75357068a7aSSimon Glass 				node = fdt_node_offset_by_phandle(blob,
75457068a7aSSimon Glass 								  phandle);
75557068a7aSSimon Glass 				if (!node) {
75657068a7aSSimon Glass 					debug("%s: could not find phandle\n",
75757068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
75857068a7aSSimon Glass 							   NULL));
75957068a7aSSimon Glass 					goto err;
76057068a7aSSimon Glass 				}
76157068a7aSSimon Glass 			}
76257068a7aSSimon Glass 
76357068a7aSSimon Glass 			if (cells_name) {
76457068a7aSSimon Glass 				count = fdtdec_get_int(blob, node, cells_name,
76557068a7aSSimon Glass 						       -1);
76657068a7aSSimon Glass 				if (count == -1) {
76757068a7aSSimon Glass 					debug("%s: could not get %s for %s\n",
76857068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
76957068a7aSSimon Glass 							   NULL),
77057068a7aSSimon Glass 					      cells_name,
77157068a7aSSimon Glass 					      fdt_get_name(blob, node,
77257068a7aSSimon Glass 							   NULL));
77357068a7aSSimon Glass 					goto err;
77457068a7aSSimon Glass 				}
77557068a7aSSimon Glass 			} else {
77657068a7aSSimon Glass 				count = cell_count;
77757068a7aSSimon Glass 			}
77857068a7aSSimon Glass 
77957068a7aSSimon Glass 			/*
78057068a7aSSimon Glass 			 * Make sure that the arguments actually fit in the
78157068a7aSSimon Glass 			 * remaining property data length
78257068a7aSSimon Glass 			 */
78357068a7aSSimon Glass 			if (list + count > list_end) {
78457068a7aSSimon Glass 				debug("%s: arguments longer than property\n",
78557068a7aSSimon Glass 				      fdt_get_name(blob, src_node, NULL));
78657068a7aSSimon Glass 				goto err;
78757068a7aSSimon Glass 			}
78857068a7aSSimon Glass 		}
78957068a7aSSimon Glass 
79057068a7aSSimon Glass 		/*
79157068a7aSSimon Glass 		 * All of the error cases above bail out of the loop, so at
79257068a7aSSimon Glass 		 * this point, the parsing is successful. If the requested
79357068a7aSSimon Glass 		 * index matches, then fill the out_args structure and return,
79457068a7aSSimon Glass 		 * or return -ENOENT for an empty entry.
79557068a7aSSimon Glass 		 */
79657068a7aSSimon Glass 		rc = -ENOENT;
79757068a7aSSimon Glass 		if (cur_index == index) {
79857068a7aSSimon Glass 			if (!phandle)
79957068a7aSSimon Glass 				goto err;
80057068a7aSSimon Glass 
80157068a7aSSimon Glass 			if (out_args) {
80257068a7aSSimon Glass 				int i;
80357068a7aSSimon Glass 
80457068a7aSSimon Glass 				if (count > MAX_PHANDLE_ARGS) {
80557068a7aSSimon Glass 					debug("%s: too many arguments %d\n",
80657068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
80757068a7aSSimon Glass 							   NULL), count);
80857068a7aSSimon Glass 					count = MAX_PHANDLE_ARGS;
80957068a7aSSimon Glass 				}
81057068a7aSSimon Glass 				out_args->node = node;
81157068a7aSSimon Glass 				out_args->args_count = count;
81257068a7aSSimon Glass 				for (i = 0; i < count; i++) {
81357068a7aSSimon Glass 					out_args->args[i] =
81457068a7aSSimon Glass 							be32_to_cpup(list++);
81557068a7aSSimon Glass 				}
81657068a7aSSimon Glass 			}
81757068a7aSSimon Glass 
81857068a7aSSimon Glass 			/* Found it! return success */
81957068a7aSSimon Glass 			return 0;
82057068a7aSSimon Glass 		}
82157068a7aSSimon Glass 
82257068a7aSSimon Glass 		node = -1;
82357068a7aSSimon Glass 		list += count;
82457068a7aSSimon Glass 		cur_index++;
82557068a7aSSimon Glass 	}
82657068a7aSSimon Glass 
82757068a7aSSimon Glass 	/*
82857068a7aSSimon Glass 	 * Result will be one of:
82957068a7aSSimon Glass 	 * -ENOENT : index is for empty phandle
83057068a7aSSimon Glass 	 * -EINVAL : parsing error on data
83157068a7aSSimon Glass 	 * [1..n]  : Number of phandle (count mode; when index = -1)
83257068a7aSSimon Glass 	 */
83357068a7aSSimon Glass 	rc = index < 0 ? cur_index : -ENOENT;
83457068a7aSSimon Glass  err:
83557068a7aSSimon Glass 	return rc;
83657068a7aSSimon Glass }
83757068a7aSSimon Glass 
8381889a7e2SPeng Fan int fdtdec_get_child_count(const void *blob, int node)
8391889a7e2SPeng Fan {
8401889a7e2SPeng Fan 	int subnode;
8411889a7e2SPeng Fan 	int num = 0;
8421889a7e2SPeng Fan 
843df87e6b1SSimon Glass 	fdt_for_each_subnode(subnode, blob, node)
8441889a7e2SPeng Fan 		num++;
8451889a7e2SPeng Fan 
8461889a7e2SPeng Fan 	return num;
8471889a7e2SPeng Fan }
8481889a7e2SPeng Fan 
849bed4d892SAnton Staff int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
850bed4d892SAnton Staff 		u8 *array, int count)
851bed4d892SAnton Staff {
852bed4d892SAnton Staff 	const u8 *cell;
853bed4d892SAnton Staff 	int err;
854bed4d892SAnton Staff 
855bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
856bed4d892SAnton Staff 	if (!err)
857bed4d892SAnton Staff 		memcpy(array, cell, count);
858bed4d892SAnton Staff 	return err;
859bed4d892SAnton Staff }
860bed4d892SAnton Staff 
861bed4d892SAnton Staff const u8 *fdtdec_locate_byte_array(const void *blob, int node,
862bed4d892SAnton Staff 			     const char *prop_name, int count)
863bed4d892SAnton Staff {
864bed4d892SAnton Staff 	const u8 *cell;
865bed4d892SAnton Staff 	int err;
866bed4d892SAnton Staff 
867bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
868bed4d892SAnton Staff 	if (err)
869bed4d892SAnton Staff 		return NULL;
870bed4d892SAnton Staff 	return cell;
871bed4d892SAnton Staff }
87209258f1eSAbhilash Kesavan 
87309258f1eSAbhilash Kesavan int fdtdec_get_config_int(const void *blob, const char *prop_name,
87409258f1eSAbhilash Kesavan 		int default_val)
87509258f1eSAbhilash Kesavan {
87609258f1eSAbhilash Kesavan 	int config_node;
87709258f1eSAbhilash Kesavan 
87809258f1eSAbhilash Kesavan 	debug("%s: %s\n", __func__, prop_name);
87909258f1eSAbhilash Kesavan 	config_node = fdt_path_offset(blob, "/config");
88009258f1eSAbhilash Kesavan 	if (config_node < 0)
88109258f1eSAbhilash Kesavan 		return default_val;
88209258f1eSAbhilash Kesavan 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
88309258f1eSAbhilash Kesavan }
884332ab0d5SSimon Glass 
88579289c0bSGabe Black int fdtdec_get_config_bool(const void *blob, const char *prop_name)
88679289c0bSGabe Black {
88779289c0bSGabe Black 	int config_node;
88879289c0bSGabe Black 	const void *prop;
88979289c0bSGabe Black 
89079289c0bSGabe Black 	debug("%s: %s\n", __func__, prop_name);
89179289c0bSGabe Black 	config_node = fdt_path_offset(blob, "/config");
89279289c0bSGabe Black 	if (config_node < 0)
89379289c0bSGabe Black 		return 0;
89479289c0bSGabe Black 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
89579289c0bSGabe Black 
89679289c0bSGabe Black 	return prop != NULL;
89779289c0bSGabe Black }
89879289c0bSGabe Black 
899332ab0d5SSimon Glass char *fdtdec_get_config_string(const void *blob, const char *prop_name)
900332ab0d5SSimon Glass {
901332ab0d5SSimon Glass 	const char *nodep;
902332ab0d5SSimon Glass 	int nodeoffset;
903332ab0d5SSimon Glass 	int len;
904332ab0d5SSimon Glass 
905332ab0d5SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
906332ab0d5SSimon Glass 	nodeoffset = fdt_path_offset(blob, "/config");
907332ab0d5SSimon Glass 	if (nodeoffset < 0)
908332ab0d5SSimon Glass 		return NULL;
909332ab0d5SSimon Glass 
910332ab0d5SSimon Glass 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
911332ab0d5SSimon Glass 	if (!nodep)
912332ab0d5SSimon Glass 		return NULL;
913332ab0d5SSimon Glass 
914332ab0d5SSimon Glass 	return (char *)nodep;
915332ab0d5SSimon Glass }
916f20c4619SSimon Glass 
91776489832SSimon Glass int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
91876489832SSimon Glass 			 fdt_addr_t *basep, fdt_size_t *sizep)
919f20c4619SSimon Glass {
920f20c4619SSimon Glass 	const fdt_addr_t *cell;
921f20c4619SSimon Glass 	int len;
922f20c4619SSimon Glass 
92376489832SSimon Glass 	debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
92476489832SSimon Glass 	      prop_name);
925f20c4619SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
92676489832SSimon Glass 	if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
92776489832SSimon Glass 		debug("cell=%p, len=%d\n", cell, len);
928f20c4619SSimon Glass 		return -1;
92976489832SSimon Glass 	}
930f20c4619SSimon Glass 
93176489832SSimon Glass 	*basep = fdt_addr_to_cpu(*cell);
93276489832SSimon Glass 	*sizep = fdt_size_to_cpu(cell[1]);
93376489832SSimon Glass 	debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
93476489832SSimon Glass 	      (ulong)*sizep);
93576489832SSimon Glass 
936f20c4619SSimon Glass 	return 0;
937f20c4619SSimon Glass }
938006e73b9SSimon Glass 
9395f7bfdd6SSimon Glass u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
94056f42242SThierry Reding {
94156f42242SThierry Reding 	u64 number = 0;
94256f42242SThierry Reding 
94356f42242SThierry Reding 	while (cells--)
94456f42242SThierry Reding 		number = (number << 32) | fdt32_to_cpu(*ptr++);
94556f42242SThierry Reding 
94656f42242SThierry Reding 	return number;
94756f42242SThierry Reding }
94856f42242SThierry Reding 
94956f42242SThierry Reding int fdt_get_resource(const void *fdt, int node, const char *property,
95056f42242SThierry Reding 		     unsigned int index, struct fdt_resource *res)
95156f42242SThierry Reding {
95256f42242SThierry Reding 	const fdt32_t *ptr, *end;
95356f42242SThierry Reding 	int na, ns, len, parent;
95456f42242SThierry Reding 	unsigned int i = 0;
95556f42242SThierry Reding 
95656f42242SThierry Reding 	parent = fdt_parent_offset(fdt, node);
95756f42242SThierry Reding 	if (parent < 0)
95856f42242SThierry Reding 		return parent;
95956f42242SThierry Reding 
96056f42242SThierry Reding 	na = fdt_address_cells(fdt, parent);
96156f42242SThierry Reding 	ns = fdt_size_cells(fdt, parent);
96256f42242SThierry Reding 
96356f42242SThierry Reding 	ptr = fdt_getprop(fdt, node, property, &len);
96456f42242SThierry Reding 	if (!ptr)
96556f42242SThierry Reding 		return len;
96656f42242SThierry Reding 
96756f42242SThierry Reding 	end = ptr + len / sizeof(*ptr);
96856f42242SThierry Reding 
96956f42242SThierry Reding 	while (ptr + na + ns <= end) {
97056f42242SThierry Reding 		if (i == index) {
97156f42242SThierry Reding 			res->start = res->end = fdtdec_get_number(ptr, na);
97256f42242SThierry Reding 			res->end += fdtdec_get_number(&ptr[na], ns) - 1;
97356f42242SThierry Reding 			return 0;
97456f42242SThierry Reding 		}
97556f42242SThierry Reding 
97656f42242SThierry Reding 		ptr += na + ns;
97756f42242SThierry Reding 		i++;
97856f42242SThierry Reding 	}
97956f42242SThierry Reding 
98056f42242SThierry Reding 	return -FDT_ERR_NOTFOUND;
98156f42242SThierry Reding }
98256f42242SThierry Reding 
98356f42242SThierry Reding int fdt_get_named_resource(const void *fdt, int node, const char *property,
98456f42242SThierry Reding 			   const char *prop_names, const char *name,
98556f42242SThierry Reding 			   struct fdt_resource *res)
98656f42242SThierry Reding {
98756f42242SThierry Reding 	int index;
98856f42242SThierry Reding 
989b02e4044SSimon Glass 	index = fdt_stringlist_search(fdt, node, prop_names, name);
99056f42242SThierry Reding 	if (index < 0)
99156f42242SThierry Reding 		return index;
99256f42242SThierry Reding 
99356f42242SThierry Reding 	return fdt_get_resource(fdt, node, property, index, res);
99456f42242SThierry Reding }
9959f85eee7SThierry Reding 
99626403871SSimon Glass int fdtdec_decode_memory_region(const void *blob, int config_node,
99726403871SSimon Glass 				const char *mem_type, const char *suffix,
99826403871SSimon Glass 				fdt_addr_t *basep, fdt_size_t *sizep)
99926403871SSimon Glass {
100026403871SSimon Glass 	char prop_name[50];
100126403871SSimon Glass 	const char *mem;
100226403871SSimon Glass 	fdt_size_t size, offset_size;
100326403871SSimon Glass 	fdt_addr_t base, offset;
100426403871SSimon Glass 	int node;
100526403871SSimon Glass 
100626403871SSimon Glass 	if (config_node == -1) {
100726403871SSimon Glass 		config_node = fdt_path_offset(blob, "/config");
100826403871SSimon Glass 		if (config_node < 0) {
100926403871SSimon Glass 			debug("%s: Cannot find /config node\n", __func__);
101026403871SSimon Glass 			return -ENOENT;
101126403871SSimon Glass 		}
101226403871SSimon Glass 	}
101326403871SSimon Glass 	if (!suffix)
101426403871SSimon Glass 		suffix = "";
101526403871SSimon Glass 
101626403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
101726403871SSimon Glass 		 suffix);
101826403871SSimon Glass 	mem = fdt_getprop(blob, config_node, prop_name, NULL);
101926403871SSimon Glass 	if (!mem) {
102026403871SSimon Glass 		debug("%s: No memory type for '%s', using /memory\n", __func__,
102126403871SSimon Glass 		      prop_name);
102226403871SSimon Glass 		mem = "/memory";
102326403871SSimon Glass 	}
102426403871SSimon Glass 
102526403871SSimon Glass 	node = fdt_path_offset(blob, mem);
102626403871SSimon Glass 	if (node < 0) {
102726403871SSimon Glass 		debug("%s: Failed to find node '%s': %s\n", __func__, mem,
102826403871SSimon Glass 		      fdt_strerror(node));
102926403871SSimon Glass 		return -ENOENT;
103026403871SSimon Glass 	}
103126403871SSimon Glass 
103226403871SSimon Glass 	/*
103326403871SSimon Glass 	 * Not strictly correct - the memory may have multiple banks. We just
103426403871SSimon Glass 	 * use the first
103526403871SSimon Glass 	 */
103626403871SSimon Glass 	if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
103726403871SSimon Glass 		debug("%s: Failed to decode memory region %s\n", __func__,
103826403871SSimon Glass 		      mem);
103926403871SSimon Glass 		return -EINVAL;
104026403871SSimon Glass 	}
104126403871SSimon Glass 
104226403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
104326403871SSimon Glass 		 suffix);
104426403871SSimon Glass 	if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
104526403871SSimon Glass 				 &offset_size)) {
104626403871SSimon Glass 		debug("%s: Failed to decode memory region '%s'\n", __func__,
104726403871SSimon Glass 		      prop_name);
104826403871SSimon Glass 		return -EINVAL;
104926403871SSimon Glass 	}
105026403871SSimon Glass 
105126403871SSimon Glass 	*basep = base + offset;
105226403871SSimon Glass 	*sizep = offset_size;
105326403871SSimon Glass 
105426403871SSimon Glass 	return 0;
105526403871SSimon Glass }
1056b45122fdSSimon Glass 
105712e67114SSimon Glass static int decode_timing_property(const void *blob, int node, const char *name,
105812e67114SSimon Glass 				  struct timing_entry *result)
105912e67114SSimon Glass {
106012e67114SSimon Glass 	int length, ret = 0;
106112e67114SSimon Glass 	const u32 *prop;
106212e67114SSimon Glass 
106312e67114SSimon Glass 	prop = fdt_getprop(blob, node, name, &length);
106412e67114SSimon Glass 	if (!prop) {
106512e67114SSimon Glass 		debug("%s: could not find property %s\n",
106612e67114SSimon Glass 		      fdt_get_name(blob, node, NULL), name);
106712e67114SSimon Glass 		return length;
106812e67114SSimon Glass 	}
106912e67114SSimon Glass 
107012e67114SSimon Glass 	if (length == sizeof(u32)) {
107112e67114SSimon Glass 		result->typ = fdtdec_get_int(blob, node, name, 0);
107212e67114SSimon Glass 		result->min = result->typ;
107312e67114SSimon Glass 		result->max = result->typ;
107412e67114SSimon Glass 	} else {
107512e67114SSimon Glass 		ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
107612e67114SSimon Glass 	}
107712e67114SSimon Glass 
107812e67114SSimon Glass 	return ret;
107912e67114SSimon Glass }
108012e67114SSimon Glass 
108112e67114SSimon Glass int fdtdec_decode_display_timing(const void *blob, int parent, int index,
108212e67114SSimon Glass 				 struct display_timing *dt)
108312e67114SSimon Glass {
108412e67114SSimon Glass 	int i, node, timings_node;
108512e67114SSimon Glass 	u32 val = 0;
108612e67114SSimon Glass 	int ret = 0;
108712e67114SSimon Glass 
108812e67114SSimon Glass 	timings_node = fdt_subnode_offset(blob, parent, "display-timings");
108912e67114SSimon Glass 	if (timings_node < 0)
109012e67114SSimon Glass 		return timings_node;
109112e67114SSimon Glass 
109212e67114SSimon Glass 	for (i = 0, node = fdt_first_subnode(blob, timings_node);
109312e67114SSimon Glass 	     node > 0 && i != index;
109412e67114SSimon Glass 	     node = fdt_next_subnode(blob, node))
109512e67114SSimon Glass 		i++;
109612e67114SSimon Glass 
109712e67114SSimon Glass 	if (node < 0)
109812e67114SSimon Glass 		return node;
109912e67114SSimon Glass 
110012e67114SSimon Glass 	memset(dt, 0, sizeof(*dt));
110112e67114SSimon Glass 
110212e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hback-porch",
110312e67114SSimon Glass 				      &dt->hback_porch);
110412e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hfront-porch",
110512e67114SSimon Glass 				      &dt->hfront_porch);
110612e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
110712e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
110812e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vback-porch",
110912e67114SSimon Glass 				      &dt->vback_porch);
111012e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vfront-porch",
111112e67114SSimon Glass 				      &dt->vfront_porch);
111212e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
111312e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
111412e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "clock-frequency",
111512e67114SSimon Glass 				      &dt->pixelclock);
111612e67114SSimon Glass 
111712e67114SSimon Glass 	dt->flags = 0;
111812e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "vsync-active", -1);
111912e67114SSimon Glass 	if (val != -1) {
112012e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
112112e67114SSimon Glass 				DISPLAY_FLAGS_VSYNC_LOW;
112212e67114SSimon Glass 	}
112312e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "hsync-active", -1);
112412e67114SSimon Glass 	if (val != -1) {
112512e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
112612e67114SSimon Glass 				DISPLAY_FLAGS_HSYNC_LOW;
112712e67114SSimon Glass 	}
112812e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "de-active", -1);
112912e67114SSimon Glass 	if (val != -1) {
113012e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
113112e67114SSimon Glass 				DISPLAY_FLAGS_DE_LOW;
113212e67114SSimon Glass 	}
113312e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
113412e67114SSimon Glass 	if (val != -1) {
113512e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
113612e67114SSimon Glass 				DISPLAY_FLAGS_PIXDATA_NEGEDGE;
113712e67114SSimon Glass 	}
113812e67114SSimon Glass 
113912e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "interlaced"))
114012e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_INTERLACED;
114112e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doublescan"))
114212e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
114312e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doubleclk"))
114412e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
114512e67114SSimon Glass 
114604b9dd10SSimon Glass 	return ret;
114712e67114SSimon Glass }
114812e67114SSimon Glass 
1149623f6019SNathan Rossi int fdtdec_setup_memory_size(void)
1150623f6019SNathan Rossi {
1151623f6019SNathan Rossi 	int ret, mem;
1152623f6019SNathan Rossi 	struct fdt_resource res;
1153623f6019SNathan Rossi 
1154623f6019SNathan Rossi 	mem = fdt_path_offset(gd->fdt_blob, "/memory");
1155623f6019SNathan Rossi 	if (mem < 0) {
1156623f6019SNathan Rossi 		debug("%s: Missing /memory node\n", __func__);
1157623f6019SNathan Rossi 		return -EINVAL;
1158623f6019SNathan Rossi 	}
1159623f6019SNathan Rossi 
1160623f6019SNathan Rossi 	ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
1161623f6019SNathan Rossi 	if (ret != 0) {
1162623f6019SNathan Rossi 		debug("%s: Unable to decode first memory bank\n", __func__);
1163623f6019SNathan Rossi 		return -EINVAL;
1164623f6019SNathan Rossi 	}
1165623f6019SNathan Rossi 
1166623f6019SNathan Rossi 	gd->ram_size = (phys_size_t)(res.end - res.start + 1);
1167c69380f8SSimon Glass 	debug("%s: Initial DRAM size %llx\n", __func__,
1168c69380f8SSimon Glass 	      (unsigned long long)gd->ram_size);
1169623f6019SNathan Rossi 
1170623f6019SNathan Rossi 	return 0;
1171623f6019SNathan Rossi }
1172623f6019SNathan Rossi 
1173623f6019SNathan Rossi #if defined(CONFIG_NR_DRAM_BANKS)
1174623f6019SNathan Rossi int fdtdec_setup_memory_banksize(void)
1175623f6019SNathan Rossi {
1176623f6019SNathan Rossi 	int bank, ret, mem;
1177623f6019SNathan Rossi 	struct fdt_resource res;
1178623f6019SNathan Rossi 
1179623f6019SNathan Rossi 	mem = fdt_path_offset(gd->fdt_blob, "/memory");
1180623f6019SNathan Rossi 	if (mem < 0) {
1181623f6019SNathan Rossi 		debug("%s: Missing /memory node\n", __func__);
1182623f6019SNathan Rossi 		return -EINVAL;
1183623f6019SNathan Rossi 	}
1184623f6019SNathan Rossi 
1185623f6019SNathan Rossi 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1186623f6019SNathan Rossi 		ret = fdt_get_resource(gd->fdt_blob, mem, "reg", bank, &res);
1187623f6019SNathan Rossi 		if (ret == -FDT_ERR_NOTFOUND)
1188623f6019SNathan Rossi 			break;
1189623f6019SNathan Rossi 		if (ret != 0)
1190623f6019SNathan Rossi 			return -EINVAL;
1191623f6019SNathan Rossi 
1192623f6019SNathan Rossi 		gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
1193623f6019SNathan Rossi 		gd->bd->bi_dram[bank].size =
1194623f6019SNathan Rossi 			(phys_size_t)(res.end - res.start + 1);
1195623f6019SNathan Rossi 
1196623f6019SNathan Rossi 		debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
1197623f6019SNathan Rossi 		      __func__, bank,
1198623f6019SNathan Rossi 		      (unsigned long long)gd->bd->bi_dram[bank].start,
1199623f6019SNathan Rossi 		      (unsigned long long)gd->bd->bi_dram[bank].size);
1200623f6019SNathan Rossi 	}
1201623f6019SNathan Rossi 
1202623f6019SNathan Rossi 	return 0;
1203623f6019SNathan Rossi }
1204623f6019SNathan Rossi #endif
1205623f6019SNathan Rossi 
12060879361fSSimon Glass int fdtdec_setup(void)
1207b45122fdSSimon Glass {
12080f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
1209b45122fdSSimon Glass # ifdef CONFIG_OF_EMBED
1210b45122fdSSimon Glass 	/* Get a pointer to the FDT */
1211b45122fdSSimon Glass 	gd->fdt_blob = __dtb_dt_begin;
1212b45122fdSSimon Glass # elif defined CONFIG_OF_SEPARATE
1213b45122fdSSimon Glass #  ifdef CONFIG_SPL_BUILD
121410172962SSimon Glass 	/* FDT is at end of BSS unless it is in a different memory region */
121510172962SSimon Glass 	if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
121610172962SSimon Glass 		gd->fdt_blob = (ulong *)&_image_binary_end;
121710172962SSimon Glass 	else
1218b45122fdSSimon Glass 		gd->fdt_blob = (ulong *)&__bss_end;
12192059ecf3SCooper Jr., Franklin 
12202059ecf3SCooper Jr., Franklin #  elif defined CONFIG_FIT_EMBED
12212059ecf3SCooper Jr., Franklin 	gd->fdt_blob = locate_dtb_in_fit(&_end);
12222059ecf3SCooper Jr., Franklin 
12232059ecf3SCooper Jr., Franklin 	if (gd->fdt_blob == NULL || gd->fdt_blob <= ((void *)&_end)) {
12242059ecf3SCooper Jr., Franklin 		puts("Failed to find proper dtb in embedded FIT Image\n");
12252059ecf3SCooper Jr., Franklin 		return -1;
12262059ecf3SCooper Jr., Franklin 	}
12272059ecf3SCooper Jr., Franklin 
1228b45122fdSSimon Glass #  else
1229b45122fdSSimon Glass 	/* FDT is at end of image */
1230b45122fdSSimon Glass 	gd->fdt_blob = (ulong *)&_end;
123129a23f9dSHeiko Schocher #  endif
123282f766d1SAlex Deymo # elif defined(CONFIG_OF_BOARD)
123382f766d1SAlex Deymo 	/* Allow the board to override the fdt address. */
123482f766d1SAlex Deymo 	gd->fdt_blob = board_fdt_blob_setup();
1235b45122fdSSimon Glass # elif defined(CONFIG_OF_HOSTFILE)
1236b45122fdSSimon Glass 	if (sandbox_read_fdt_from_file()) {
1237b45122fdSSimon Glass 		puts("Failed to read control FDT\n");
1238b45122fdSSimon Glass 		return -1;
1239b45122fdSSimon Glass 	}
1240b45122fdSSimon Glass # endif
1241b45122fdSSimon Glass # ifndef CONFIG_SPL_BUILD
1242b45122fdSSimon Glass 	/* Allow the early environment to override the fdt address */
1243*bfebc8c9SSimon Glass 	gd->fdt_blob = (void *)env_get_ulong("fdtcontroladdr", 16,
1244b45122fdSSimon Glass 						(uintptr_t)gd->fdt_blob);
1245b45122fdSSimon Glass # endif
1246b45122fdSSimon Glass #endif
12470879361fSSimon Glass 	return fdtdec_prepare_fdt();
1248b45122fdSSimon Glass }
1249b45122fdSSimon Glass 
1250b45122fdSSimon Glass #endif /* !USE_HOSTCC */
1251