xref: /rk3399_rockchip-uboot/lib/fdtdec.c (revision 5b34436035fc862b5e8d0d2c3eab74ba36f1a7f4)
1b5220bc6SSimon Glass /*
2b5220bc6SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
31a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
4b5220bc6SSimon Glass  */
5b5220bc6SSimon Glass 
629a23f9dSHeiko Schocher #ifndef USE_HOSTCC
7b5220bc6SSimon Glass #include <common.h>
85c33c9fdSSimon Glass #include <errno.h>
9b5220bc6SSimon Glass #include <serial.h>
10b5220bc6SSimon Glass #include <libfdt.h>
11b5220bc6SSimon Glass #include <fdtdec.h>
12b45122fdSSimon Glass #include <asm/sections.h>
135c33c9fdSSimon Glass #include <linux/ctype.h>
14b5220bc6SSimon Glass 
15b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
16b5220bc6SSimon Glass 
17b5220bc6SSimon Glass /*
18b5220bc6SSimon Glass  * Here are the type we know about. One day we might allow drivers to
19b5220bc6SSimon Glass  * register. For now we just put them here. The COMPAT macro allows us to
20b5220bc6SSimon Glass  * turn this into a sparse list later, and keeps the ID with the name.
21b5220bc6SSimon Glass  */
22b5220bc6SSimon Glass #define COMPAT(id, name) name
23b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = {
24f88fe2deSSimon Glass 	COMPAT(UNKNOWN, "<none>"),
250e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
260e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
276642a681SRakesh Iyer 	COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
28312693c3SJim Lin 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
29e1ae0d1fSSimon Glass 	COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
3000f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_DC, "nvidia,tegra124-dc"),
3100f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_SOR, "nvidia,tegra124-sor"),
3200f37327SSimon Glass 	COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
3387540de3SWei Ni 	COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
34a73ca478SStephen Warren 	COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
35f4e4e0b0STom Warren 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
36c9aa831eSTom Warren 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
37f315828bSThierry Reding 	COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
38f315828bSThierry Reding 	COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
39f315828bSThierry Reding 	COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
4079c7a90fSThierry Reding 	COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
41cc9fe33aSHatim RV 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
42cc9fe33aSHatim RV 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
43c34253d1SRajeshwari Shinde 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
4472dbff12SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
4572dbff12SRajeshwari Shinde 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
46713cb680SHung-ying Tyan 	COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
476abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
48108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
49618766c0SAkshay Saraswat 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
50d7377b51SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
51de461c52SPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
521e4706a7SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
537d3ca0f8SJaehoon Chung 	COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
543577fe8bSPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
55ee1e3c2fSRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
56f37df0f8SPrzemyslaw Marczak 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
57bb8215f4SSimon Glass 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
587772bb78SRajeshwari Shinde 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
59f6267998SRong Chang 	COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
60f0e57b1bSSimon Glass 	COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645tt"),
61ecbd7e1eSnaveen krishna chatradhi 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
627d95f2a3SSimon Glass 	COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
63ac1058fdSTom Wai-Hong Tam 	COMPAT(TI_TPS65090, "ti,tps65090"),
64a9cf6da9SSimon Glass 	COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
6545c480c9SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
669e8f664eSVadim Bendebury 	COMPAT(PARADE_PS8625, "parade,ps8625"),
6777f9b1fbSSimon Glass 	COMPAT(INTEL_MICROCODE, "intel,microcode"),
6865dd74a6SSimon Glass 	COMPAT(MEMORY_SPD, "memory-spd"),
693ac83935SSimon Glass 	COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
70bb80be39SSimon Glass 	COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
71effcf067SSimon Glass 	COMPAT(INTEL_GMA, "intel,gma"),
726173c45bSThierry Reding 	COMPAT(AMS_AS3722, "ams,as3722"),
735da38086SSimon Glass 	COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
74c89ada01SBin Meng 	COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
755318f18dSGabriel Huau 	COMPAT(INTEL_X86_PINCTRL, "intel,x86-pinctrl"),
766462cdedSMasahiro Yamada 	COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
7790b16d14SSimon Glass 	COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
789c7dea60SBin Meng 	COMPAT(COMPAT_INTEL_IRQ_ROUTER, "intel,irq-router"),
79b5220bc6SSimon Glass };
80b5220bc6SSimon Glass 
81a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id)
82a53f4a29SSimon Glass {
83a53f4a29SSimon Glass 	/* We allow reading of the 'unknown' ID for testing purposes */
84a53f4a29SSimon Glass 	assert(id >= 0 && id < COMPAT_COUNT);
85a53f4a29SSimon Glass 	return compat_names[id];
86a53f4a29SSimon Glass }
87a53f4a29SSimon Glass 
884397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
894397a2a8SSimon Glass 		const char *prop_name, fdt_size_t *sizep)
90b5220bc6SSimon Glass {
91*5b344360SThierry Reding 	const fdt32_t *ptr, *end;
92*5b344360SThierry Reding 	int parent, na, ns, len;
93*5b344360SThierry Reding 	fdt_addr_t addr;
94b5220bc6SSimon Glass 
951cb2323bSSimon Glass 	debug("%s: %s: ", __func__, prop_name);
961cb2323bSSimon Glass 
97*5b344360SThierry Reding 	parent = fdt_parent_offset(blob, node);
98*5b344360SThierry Reding 	if (parent < 0) {
99*5b344360SThierry Reding 		debug("(no parent found)\n");
100*5b344360SThierry Reding 		return FDT_ADDR_T_NONE;
1014397a2a8SSimon Glass 	}
102*5b344360SThierry Reding 
103*5b344360SThierry Reding 	na = fdt_address_cells(blob, parent);
104*5b344360SThierry Reding 	ns = fdt_size_cells(blob, parent);
105*5b344360SThierry Reding 
106*5b344360SThierry Reding 	ptr = fdt_getprop(blob, node, prop_name, &len);
107*5b344360SThierry Reding 	if (!ptr) {
1081cb2323bSSimon Glass 		debug("(not found)\n");
109b5220bc6SSimon Glass 		return FDT_ADDR_T_NONE;
110b5220bc6SSimon Glass 	}
111b5220bc6SSimon Glass 
112*5b344360SThierry Reding 	end = ptr + len / sizeof(*ptr);
113*5b344360SThierry Reding 
114*5b344360SThierry Reding 	if (ptr + na + ns > end) {
115*5b344360SThierry Reding 		debug("(not enough data: expected %d bytes, got %d bytes)\n",
116*5b344360SThierry Reding 		      (na + ns) * 4, len);
117*5b344360SThierry Reding 		return FDT_ADDR_T_NONE;
118*5b344360SThierry Reding 	}
119*5b344360SThierry Reding 
120*5b344360SThierry Reding 	addr = fdtdec_get_number(ptr, na);
121*5b344360SThierry Reding 
122*5b344360SThierry Reding 	if (sizep) {
123*5b344360SThierry Reding 		*sizep = fdtdec_get_number(ptr + na, ns);
124*5b344360SThierry Reding 		debug("addr=%pa, size=%pa\n", &addr, sizep);
125*5b344360SThierry Reding 	} else {
126*5b344360SThierry Reding 		debug("%pa\n", &addr);
127*5b344360SThierry Reding 	}
128*5b344360SThierry Reding 
129*5b344360SThierry Reding 	return addr;
130*5b344360SThierry Reding }
131*5b344360SThierry Reding 
1324397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
1334397a2a8SSimon Glass 		const char *prop_name)
1344397a2a8SSimon Glass {
1354397a2a8SSimon Glass 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
1364397a2a8SSimon Glass }
1374397a2a8SSimon Glass 
138a62e84d7SBin Meng #ifdef CONFIG_PCI
139a62e84d7SBin Meng int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
140a62e84d7SBin Meng 		const char *prop_name, struct fdt_pci_addr *addr)
141a62e84d7SBin Meng {
142a62e84d7SBin Meng 	const u32 *cell;
143a62e84d7SBin Meng 	int len;
144a62e84d7SBin Meng 	int ret = -ENOENT;
145a62e84d7SBin Meng 
146a62e84d7SBin Meng 	debug("%s: %s: ", __func__, prop_name);
147a62e84d7SBin Meng 
148a62e84d7SBin Meng 	/*
149a62e84d7SBin Meng 	 * If we follow the pci bus bindings strictly, we should check
150a62e84d7SBin Meng 	 * the value of the node's parent node's #address-cells and
151a62e84d7SBin Meng 	 * #size-cells. They need to be 3 and 2 accordingly. However,
152a62e84d7SBin Meng 	 * for simplicity we skip the check here.
153a62e84d7SBin Meng 	 */
154a62e84d7SBin Meng 	cell = fdt_getprop(blob, node, prop_name, &len);
155a62e84d7SBin Meng 	if (!cell)
156a62e84d7SBin Meng 		goto fail;
157a62e84d7SBin Meng 
158a62e84d7SBin Meng 	if ((len % FDT_PCI_REG_SIZE) == 0) {
159a62e84d7SBin Meng 		int num = len / FDT_PCI_REG_SIZE;
160a62e84d7SBin Meng 		int i;
161a62e84d7SBin Meng 
162a62e84d7SBin Meng 		for (i = 0; i < num; i++) {
163a62e84d7SBin Meng 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
164a62e84d7SBin Meng 			      (ulong)fdt_addr_to_cpu(cell[0]),
165a62e84d7SBin Meng 			      (ulong)fdt_addr_to_cpu(cell[1]),
166a62e84d7SBin Meng 			      (ulong)fdt_addr_to_cpu(cell[2]));
167a62e84d7SBin Meng 			if ((fdt_addr_to_cpu(*cell) & type) == type) {
168a62e84d7SBin Meng 				addr->phys_hi = fdt_addr_to_cpu(cell[0]);
169a62e84d7SBin Meng 				addr->phys_mid = fdt_addr_to_cpu(cell[1]);
170a62e84d7SBin Meng 				addr->phys_lo = fdt_addr_to_cpu(cell[2]);
171a62e84d7SBin Meng 				break;
172a62e84d7SBin Meng 			} else {
173a62e84d7SBin Meng 				cell += (FDT_PCI_ADDR_CELLS +
174a62e84d7SBin Meng 					 FDT_PCI_SIZE_CELLS);
175a62e84d7SBin Meng 			}
176a62e84d7SBin Meng 		}
177a62e84d7SBin Meng 
178106cce96SSimon Glass 		if (i == num) {
179106cce96SSimon Glass 			ret = -ENXIO;
180a62e84d7SBin Meng 			goto fail;
181106cce96SSimon Glass 		}
182a62e84d7SBin Meng 
183a62e84d7SBin Meng 		return 0;
184a62e84d7SBin Meng 	} else {
185a62e84d7SBin Meng 		ret = -EINVAL;
186a62e84d7SBin Meng 	}
187a62e84d7SBin Meng 
188a62e84d7SBin Meng fail:
189a62e84d7SBin Meng 	debug("(not found)\n");
190a62e84d7SBin Meng 	return ret;
191a62e84d7SBin Meng }
192a62e84d7SBin Meng 
193a62e84d7SBin Meng int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
194a62e84d7SBin Meng {
195a62e84d7SBin Meng 	const char *list, *end;
196a62e84d7SBin Meng 	int len;
197a62e84d7SBin Meng 
198a62e84d7SBin Meng 	list = fdt_getprop(blob, node, "compatible", &len);
199a62e84d7SBin Meng 	if (!list)
200a62e84d7SBin Meng 		return -ENOENT;
201a62e84d7SBin Meng 
202a62e84d7SBin Meng 	end = list + len;
203a62e84d7SBin Meng 	while (list < end) {
204a62e84d7SBin Meng 		char *s;
205a62e84d7SBin Meng 
206a62e84d7SBin Meng 		len = strlen(list);
207a62e84d7SBin Meng 		if (len >= strlen("pciVVVV,DDDD")) {
208a62e84d7SBin Meng 			s = strstr(list, "pci");
209a62e84d7SBin Meng 
210a62e84d7SBin Meng 			/*
211a62e84d7SBin Meng 			 * check if the string is something like pciVVVV,DDDD.RR
212a62e84d7SBin Meng 			 * or just pciVVVV,DDDD
213a62e84d7SBin Meng 			 */
214a62e84d7SBin Meng 			if (s && s[7] == ',' &&
215a62e84d7SBin Meng 			    (s[12] == '.' || s[12] == 0)) {
216a62e84d7SBin Meng 				s += 3;
217a62e84d7SBin Meng 				*vendor = simple_strtol(s, NULL, 16);
218a62e84d7SBin Meng 
219a62e84d7SBin Meng 				s += 5;
220a62e84d7SBin Meng 				*device = simple_strtol(s, NULL, 16);
221a62e84d7SBin Meng 
222a62e84d7SBin Meng 				return 0;
223a62e84d7SBin Meng 			}
224a62e84d7SBin Meng 		} else {
225a62e84d7SBin Meng 			list += (len + 1);
226a62e84d7SBin Meng 		}
227a62e84d7SBin Meng 	}
228a62e84d7SBin Meng 
229a62e84d7SBin Meng 	return -ENOENT;
230a62e84d7SBin Meng }
231a62e84d7SBin Meng 
232a62e84d7SBin Meng int fdtdec_get_pci_bdf(const void *blob, int node,
233a62e84d7SBin Meng 		struct fdt_pci_addr *addr, pci_dev_t *bdf)
234a62e84d7SBin Meng {
235a62e84d7SBin Meng 	u16 dt_vendor, dt_device, vendor, device;
236a62e84d7SBin Meng 	int ret;
237a62e84d7SBin Meng 
238a62e84d7SBin Meng 	/* get vendor id & device id from the compatible string */
239a62e84d7SBin Meng 	ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
240a62e84d7SBin Meng 	if (ret)
241a62e84d7SBin Meng 		return ret;
242a62e84d7SBin Meng 
243a62e84d7SBin Meng 	/* extract the bdf from fdt_pci_addr */
244a62e84d7SBin Meng 	*bdf = addr->phys_hi & 0xffff00;
245a62e84d7SBin Meng 
246a62e84d7SBin Meng 	/* read vendor id & device id based on bdf */
247a62e84d7SBin Meng 	pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
248a62e84d7SBin Meng 	pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
249a62e84d7SBin Meng 
250a62e84d7SBin Meng 	/*
251a62e84d7SBin Meng 	 * Note there are two places in the device tree to fully describe
252a62e84d7SBin Meng 	 * a pci device: one is via compatible string with a format of
253a62e84d7SBin Meng 	 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
254a62e84d7SBin Meng 	 * the device node's reg address property. We read the vendor id
255a62e84d7SBin Meng 	 * and device id based on bdf and compare the values with the
256a62e84d7SBin Meng 	 * "VVVV,DDDD". If they are the same, then we are good to use bdf
257a62e84d7SBin Meng 	 * to read device's bar. But if they are different, we have to rely
258a62e84d7SBin Meng 	 * on the vendor id and device id extracted from the compatible
259a62e84d7SBin Meng 	 * string and locate the real bdf by pci_find_device(). This is
260a62e84d7SBin Meng 	 * because normally we may only know device's device number and
261a62e84d7SBin Meng 	 * function number when writing device tree. The bus number is
262a62e84d7SBin Meng 	 * dynamically assigned during the pci enumeration process.
263a62e84d7SBin Meng 	 */
264a62e84d7SBin Meng 	if ((dt_vendor != vendor) || (dt_device != device)) {
265a62e84d7SBin Meng 		*bdf = pci_find_device(dt_vendor, dt_device, 0);
266a62e84d7SBin Meng 		if (*bdf == -1)
267a62e84d7SBin Meng 			return -ENODEV;
268a62e84d7SBin Meng 	}
269a62e84d7SBin Meng 
270a62e84d7SBin Meng 	return 0;
271a62e84d7SBin Meng }
272a62e84d7SBin Meng 
273a62e84d7SBin Meng int fdtdec_get_pci_bar32(const void *blob, int node,
274a62e84d7SBin Meng 		struct fdt_pci_addr *addr, u32 *bar)
275a62e84d7SBin Meng {
276a62e84d7SBin Meng 	pci_dev_t bdf;
277a62e84d7SBin Meng 	int barnum;
278a62e84d7SBin Meng 	int ret;
279a62e84d7SBin Meng 
280a62e84d7SBin Meng 	/* get pci devices's bdf */
281a62e84d7SBin Meng 	ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
282a62e84d7SBin Meng 	if (ret)
283a62e84d7SBin Meng 		return ret;
284a62e84d7SBin Meng 
285a62e84d7SBin Meng 	/* extract the bar number from fdt_pci_addr */
286a62e84d7SBin Meng 	barnum = addr->phys_hi & 0xff;
287a62e84d7SBin Meng 	if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
288a62e84d7SBin Meng 		return -EINVAL;
289a62e84d7SBin Meng 
290a62e84d7SBin Meng 	barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
291a62e84d7SBin Meng 	*bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
292a62e84d7SBin Meng 
293a62e84d7SBin Meng 	return 0;
294a62e84d7SBin Meng }
295a62e84d7SBin Meng #endif
296a62e84d7SBin Meng 
297aadef0a1SChe-Liang Chiou uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
298aadef0a1SChe-Liang Chiou 		uint64_t default_val)
299aadef0a1SChe-Liang Chiou {
300aadef0a1SChe-Liang Chiou 	const uint64_t *cell64;
301aadef0a1SChe-Liang Chiou 	int length;
302aadef0a1SChe-Liang Chiou 
303aadef0a1SChe-Liang Chiou 	cell64 = fdt_getprop(blob, node, prop_name, &length);
304aadef0a1SChe-Liang Chiou 	if (!cell64 || length < sizeof(*cell64))
305aadef0a1SChe-Liang Chiou 		return default_val;
306aadef0a1SChe-Liang Chiou 
307aadef0a1SChe-Liang Chiou 	return fdt64_to_cpu(*cell64);
308aadef0a1SChe-Liang Chiou }
309aadef0a1SChe-Liang Chiou 
310f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node)
311b5220bc6SSimon Glass {
312b5220bc6SSimon Glass 	const char *cell;
313b5220bc6SSimon Glass 
314f88fe2deSSimon Glass 	/*
315f88fe2deSSimon Glass 	 * It should say "okay", so only allow that. Some fdts use "ok" but
316f88fe2deSSimon Glass 	 * this is a bug. Please fix your device tree source file. See here
317f88fe2deSSimon Glass 	 * for discussion:
318f88fe2deSSimon Glass 	 *
319f88fe2deSSimon Glass 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
320f88fe2deSSimon Glass 	 */
321b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, "status", NULL);
322b5220bc6SSimon Glass 	if (cell)
323f88fe2deSSimon Glass 		return 0 == strcmp(cell, "okay");
324f88fe2deSSimon Glass 	return 1;
325b5220bc6SSimon Glass }
326b5220bc6SSimon Glass 
3277cde397bSGerald Van Baren enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
328b5220bc6SSimon Glass {
329b5220bc6SSimon Glass 	enum fdt_compat_id id;
330b5220bc6SSimon Glass 
331b5220bc6SSimon Glass 	/* Search our drivers */
332b5220bc6SSimon Glass 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
333b5220bc6SSimon Glass 		if (0 == fdt_node_check_compatible(blob, node,
334b5220bc6SSimon Glass 				compat_names[id]))
335b5220bc6SSimon Glass 			return id;
336b5220bc6SSimon Glass 	return COMPAT_UNKNOWN;
337b5220bc6SSimon Glass }
338b5220bc6SSimon Glass 
339b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node,
340b5220bc6SSimon Glass 		enum fdt_compat_id id)
341b5220bc6SSimon Glass {
342b5220bc6SSimon Glass 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
343b5220bc6SSimon Glass }
344b5220bc6SSimon Glass 
3453ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
3463ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp)
3473ddecfc7SSimon Glass {
3483ddecfc7SSimon Glass 	do {
3493ddecfc7SSimon Glass 		node = fdt_next_node(blob, node, depthp);
3503ddecfc7SSimon Glass 	} while (*depthp > 1);
3513ddecfc7SSimon Glass 
3523ddecfc7SSimon Glass 	/* If this is a direct subnode, and compatible, return it */
3533ddecfc7SSimon Glass 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
3543ddecfc7SSimon Glass 						blob, node, compat_names[id]))
3553ddecfc7SSimon Glass 		return node;
3563ddecfc7SSimon Glass 
3573ddecfc7SSimon Glass 	return -FDT_ERR_NOTFOUND;
3583ddecfc7SSimon Glass }
3593ddecfc7SSimon Glass 
360b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
361b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto)
362b5220bc6SSimon Glass {
363b5220bc6SSimon Glass #define MAX_STR_LEN 20
364b5220bc6SSimon Glass 	char str[MAX_STR_LEN + 20];
365b5220bc6SSimon Glass 	int node, err;
366b5220bc6SSimon Glass 
367b5220bc6SSimon Glass 	/* snprintf() is not available */
368b5220bc6SSimon Glass 	assert(strlen(name) < MAX_STR_LEN);
369b5220bc6SSimon Glass 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
37000878476SSimon Glass 	node = fdt_path_offset(blob, str);
371b5220bc6SSimon Glass 	if (node < 0)
372b5220bc6SSimon Glass 		return node;
373b5220bc6SSimon Glass 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
374b5220bc6SSimon Glass 	if (err < 0)
375b5220bc6SSimon Glass 		return err;
376f88fe2deSSimon Glass 	if (err)
377f88fe2deSSimon Glass 		return -FDT_ERR_NOTFOUND;
378f88fe2deSSimon Glass 	(*upto)++;
379f88fe2deSSimon Glass 	return node;
380b5220bc6SSimon Glass }
381b5220bc6SSimon Glass 
382a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
383a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
384a53f4a29SSimon Glass {
385c6782270SSimon Glass 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
386c6782270SSimon Glass 
387c6782270SSimon Glass 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
388c6782270SSimon Glass }
389c6782270SSimon Glass 
390c6782270SSimon Glass /* TODO: Can we tighten this code up a little? */
391c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
392c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
393c6782270SSimon Glass {
394a53f4a29SSimon Glass 	int name_len = strlen(name);
395a53f4a29SSimon Glass 	int nodes[maxcount];
396a53f4a29SSimon Glass 	int num_found = 0;
397a53f4a29SSimon Glass 	int offset, node;
398a53f4a29SSimon Glass 	int alias_node;
399a53f4a29SSimon Glass 	int count;
400a53f4a29SSimon Glass 	int i, j;
401a53f4a29SSimon Glass 
402a53f4a29SSimon Glass 	/* find the alias node if present */
403a53f4a29SSimon Glass 	alias_node = fdt_path_offset(blob, "/aliases");
404a53f4a29SSimon Glass 
405a53f4a29SSimon Glass 	/*
406a53f4a29SSimon Glass 	 * start with nothing, and we can assume that the root node can't
407a53f4a29SSimon Glass 	 * match
408a53f4a29SSimon Glass 	 */
409a53f4a29SSimon Glass 	memset(nodes, '\0', sizeof(nodes));
410a53f4a29SSimon Glass 
411a53f4a29SSimon Glass 	/* First find all the compatible nodes */
412a53f4a29SSimon Glass 	for (node = count = 0; node >= 0 && count < maxcount;) {
413a53f4a29SSimon Glass 		node = fdtdec_next_compatible(blob, node, id);
414a53f4a29SSimon Glass 		if (node >= 0)
415a53f4a29SSimon Glass 			nodes[count++] = node;
416a53f4a29SSimon Glass 	}
417a53f4a29SSimon Glass 	if (node >= 0)
418a53f4a29SSimon Glass 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
419a53f4a29SSimon Glass 		       __func__, name);
420a53f4a29SSimon Glass 
421a53f4a29SSimon Glass 	/* Now find all the aliases */
422a53f4a29SSimon Glass 	for (offset = fdt_first_property_offset(blob, alias_node);
423a53f4a29SSimon Glass 			offset > 0;
424a53f4a29SSimon Glass 			offset = fdt_next_property_offset(blob, offset)) {
425a53f4a29SSimon Glass 		const struct fdt_property *prop;
426a53f4a29SSimon Glass 		const char *path;
427a53f4a29SSimon Glass 		int number;
428a53f4a29SSimon Glass 		int found;
429a53f4a29SSimon Glass 
430a53f4a29SSimon Glass 		node = 0;
431a53f4a29SSimon Glass 		prop = fdt_get_property_by_offset(blob, offset, NULL);
432a53f4a29SSimon Glass 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
433a53f4a29SSimon Glass 		if (prop->len && 0 == strncmp(path, name, name_len))
434a53f4a29SSimon Glass 			node = fdt_path_offset(blob, prop->data);
435a53f4a29SSimon Glass 		if (node <= 0)
436a53f4a29SSimon Glass 			continue;
437a53f4a29SSimon Glass 
438a53f4a29SSimon Glass 		/* Get the alias number */
439a53f4a29SSimon Glass 		number = simple_strtoul(path + name_len, NULL, 10);
440a53f4a29SSimon Glass 		if (number < 0 || number >= maxcount) {
441a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' is out of range\n",
442a53f4a29SSimon Glass 			       __func__, path);
443a53f4a29SSimon Glass 			continue;
444a53f4a29SSimon Glass 		}
445a53f4a29SSimon Glass 
446a53f4a29SSimon Glass 		/* Make sure the node we found is actually in our list! */
447a53f4a29SSimon Glass 		found = -1;
448a53f4a29SSimon Glass 		for (j = 0; j < count; j++)
449a53f4a29SSimon Glass 			if (nodes[j] == node) {
450a53f4a29SSimon Glass 				found = j;
451a53f4a29SSimon Glass 				break;
452a53f4a29SSimon Glass 			}
453a53f4a29SSimon Glass 
454a53f4a29SSimon Glass 		if (found == -1) {
455a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' points to a node "
456a53f4a29SSimon Glass 				"'%s' that is missing or is not compatible "
457a53f4a29SSimon Glass 				" with '%s'\n", __func__, path,
458a53f4a29SSimon Glass 				fdt_get_name(blob, node, NULL),
459a53f4a29SSimon Glass 			       compat_names[id]);
460a53f4a29SSimon Glass 			continue;
461a53f4a29SSimon Glass 		}
462a53f4a29SSimon Glass 
463a53f4a29SSimon Glass 		/*
464a53f4a29SSimon Glass 		 * Add this node to our list in the right place, and mark
465a53f4a29SSimon Glass 		 * it as done.
466a53f4a29SSimon Glass 		 */
467a53f4a29SSimon Glass 		if (fdtdec_get_is_enabled(blob, node)) {
468c6782270SSimon Glass 			if (node_list[number]) {
469c6782270SSimon Glass 				debug("%s: warning: alias '%s' requires that "
470c6782270SSimon Glass 				      "a node be placed in the list in a "
471c6782270SSimon Glass 				      "position which is already filled by "
472c6782270SSimon Glass 				      "node '%s'\n", __func__, path,
473c6782270SSimon Glass 				      fdt_get_name(blob, node, NULL));
474c6782270SSimon Glass 				continue;
475c6782270SSimon Glass 			}
476a53f4a29SSimon Glass 			node_list[number] = node;
477a53f4a29SSimon Glass 			if (number >= num_found)
478a53f4a29SSimon Glass 				num_found = number + 1;
479a53f4a29SSimon Glass 		}
480c6782270SSimon Glass 		nodes[found] = 0;
481a53f4a29SSimon Glass 	}
482a53f4a29SSimon Glass 
483a53f4a29SSimon Glass 	/* Add any nodes not mentioned by an alias */
484a53f4a29SSimon Glass 	for (i = j = 0; i < maxcount; i++) {
485a53f4a29SSimon Glass 		if (!node_list[i]) {
486a53f4a29SSimon Glass 			for (; j < maxcount; j++)
487a53f4a29SSimon Glass 				if (nodes[j] &&
488a53f4a29SSimon Glass 					fdtdec_get_is_enabled(blob, nodes[j]))
489a53f4a29SSimon Glass 					break;
490a53f4a29SSimon Glass 
491a53f4a29SSimon Glass 			/* Have we run out of nodes to add? */
492a53f4a29SSimon Glass 			if (j == maxcount)
493a53f4a29SSimon Glass 				break;
494a53f4a29SSimon Glass 
495a53f4a29SSimon Glass 			assert(!node_list[i]);
496a53f4a29SSimon Glass 			node_list[i] = nodes[j++];
497a53f4a29SSimon Glass 			if (i >= num_found)
498a53f4a29SSimon Glass 				num_found = i + 1;
499a53f4a29SSimon Glass 		}
500a53f4a29SSimon Glass 	}
501a53f4a29SSimon Glass 
502a53f4a29SSimon Glass 	return num_found;
503a53f4a29SSimon Glass }
504a53f4a29SSimon Glass 
5055c33c9fdSSimon Glass int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
5065c33c9fdSSimon Glass 			 int *seqp)
5075c33c9fdSSimon Glass {
5085c33c9fdSSimon Glass 	int base_len = strlen(base);
5095c33c9fdSSimon Glass 	const char *find_name;
5105c33c9fdSSimon Glass 	int find_namelen;
5115c33c9fdSSimon Glass 	int prop_offset;
5125c33c9fdSSimon Glass 	int aliases;
5135c33c9fdSSimon Glass 
5145c33c9fdSSimon Glass 	find_name = fdt_get_name(blob, offset, &find_namelen);
5155c33c9fdSSimon Glass 	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
5165c33c9fdSSimon Glass 
5175c33c9fdSSimon Glass 	aliases = fdt_path_offset(blob, "/aliases");
5185c33c9fdSSimon Glass 	for (prop_offset = fdt_first_property_offset(blob, aliases);
5195c33c9fdSSimon Glass 	     prop_offset > 0;
5205c33c9fdSSimon Glass 	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
5215c33c9fdSSimon Glass 		const char *prop;
5225c33c9fdSSimon Glass 		const char *name;
5235c33c9fdSSimon Glass 		const char *slash;
524c4af6732SSimon Glass 		int len, val;
5255c33c9fdSSimon Glass 
5265c33c9fdSSimon Glass 		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
5275c33c9fdSSimon Glass 		debug("   - %s, %s\n", name, prop);
5285c33c9fdSSimon Glass 		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
5295c33c9fdSSimon Glass 		    strncmp(name, base, base_len))
5305c33c9fdSSimon Glass 			continue;
5315c33c9fdSSimon Glass 
5325c33c9fdSSimon Glass 		slash = strrchr(prop, '/');
5335c33c9fdSSimon Glass 		if (strcmp(slash + 1, find_name))
5345c33c9fdSSimon Glass 			continue;
535c4af6732SSimon Glass 		val = trailing_strtol(name);
536c4af6732SSimon Glass 		if (val != -1) {
537c4af6732SSimon Glass 			*seqp = val;
5385c33c9fdSSimon Glass 			debug("Found seq %d\n", *seqp);
5395c33c9fdSSimon Glass 			return 0;
5405c33c9fdSSimon Glass 		}
5415c33c9fdSSimon Glass 	}
5425c33c9fdSSimon Glass 
5435c33c9fdSSimon Glass 	debug("Not found\n");
5445c33c9fdSSimon Glass 	return -ENOENT;
5455c33c9fdSSimon Glass }
5465c33c9fdSSimon Glass 
547aac07d49SSimon Glass int fdtdec_get_chosen_node(const void *blob, const char *name)
548aac07d49SSimon Glass {
549aac07d49SSimon Glass 	const char *prop;
550aac07d49SSimon Glass 	int chosen_node;
551aac07d49SSimon Glass 	int len;
552aac07d49SSimon Glass 
553aac07d49SSimon Glass 	if (!blob)
554aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
555aac07d49SSimon Glass 	chosen_node = fdt_path_offset(blob, "/chosen");
556aac07d49SSimon Glass 	prop = fdt_getprop(blob, chosen_node, name, &len);
557aac07d49SSimon Glass 	if (!prop)
558aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
559aac07d49SSimon Glass 	return fdt_path_offset(blob, prop);
560aac07d49SSimon Glass }
561aac07d49SSimon Glass 
5629a263e55SSimon Glass int fdtdec_check_fdt(void)
5639a263e55SSimon Glass {
5649a263e55SSimon Glass 	/*
5659a263e55SSimon Glass 	 * We must have an FDT, but we cannot panic() yet since the console
5669a263e55SSimon Glass 	 * is not ready. So for now, just assert(). Boards which need an early
5679a263e55SSimon Glass 	 * FDT (prior to console ready) will need to make their own
5689a263e55SSimon Glass 	 * arrangements and do their own checks.
5699a263e55SSimon Glass 	 */
5709a263e55SSimon Glass 	assert(!fdtdec_prepare_fdt());
5719a263e55SSimon Glass 	return 0;
5729a263e55SSimon Glass }
5739a263e55SSimon Glass 
574b5220bc6SSimon Glass /*
575b5220bc6SSimon Glass  * This function is a little odd in that it accesses global data. At some
576b5220bc6SSimon Glass  * point if the architecture board.c files merge this will make more sense.
577b5220bc6SSimon Glass  * Even now, it is common code.
578b5220bc6SSimon Glass  */
5799a263e55SSimon Glass int fdtdec_prepare_fdt(void)
580b5220bc6SSimon Glass {
581c309c2daSSimon Glass 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
582c309c2daSSimon Glass 	    fdt_check_header(gd->fdt_blob)) {
58366312374SSimon Glass #ifdef CONFIG_SPL_BUILD
58466312374SSimon Glass 		puts("Missing DTB\n");
58566312374SSimon Glass #else
58666312374SSimon 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");
587cb5f97f7SSimon Glass # ifdef DEBUG
588cb5f97f7SSimon Glass 		if (gd->fdt_blob) {
589cb5f97f7SSimon Glass 			printf("fdt_blob=%p\n", gd->fdt_blob);
590cb5f97f7SSimon Glass 			print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
591cb5f97f7SSimon Glass 				     32, 0);
592cb5f97f7SSimon Glass 		}
593cb5f97f7SSimon Glass # endif
59466312374SSimon Glass #endif
5959a263e55SSimon Glass 		return -1;
5969a263e55SSimon Glass 	}
597b5220bc6SSimon Glass 	return 0;
598b5220bc6SSimon Glass }
599d17da655SSimon Glass 
600d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
601d17da655SSimon Glass {
602d17da655SSimon Glass 	const u32 *phandle;
603d17da655SSimon Glass 	int lookup;
604d17da655SSimon Glass 
6051cb2323bSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
606d17da655SSimon Glass 	phandle = fdt_getprop(blob, node, prop_name, NULL);
607d17da655SSimon Glass 	if (!phandle)
608d17da655SSimon Glass 		return -FDT_ERR_NOTFOUND;
609d17da655SSimon Glass 
610d17da655SSimon Glass 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
611d17da655SSimon Glass 	return lookup;
612d17da655SSimon Glass }
613d17da655SSimon Glass 
614d17da655SSimon Glass /**
615d17da655SSimon Glass  * Look up a property in a node and check that it has a minimum length.
616d17da655SSimon Glass  *
617d17da655SSimon Glass  * @param blob		FDT blob
618d17da655SSimon Glass  * @param node		node to examine
619d17da655SSimon Glass  * @param prop_name	name of property to find
620d17da655SSimon Glass  * @param min_len	minimum property length in bytes
621d17da655SSimon Glass  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
622d17da655SSimon Glass 			found, or -FDT_ERR_BADLAYOUT if not enough data
623d17da655SSimon Glass  * @return pointer to cell, which is only valid if err == 0
624d17da655SSimon Glass  */
625d17da655SSimon Glass static const void *get_prop_check_min_len(const void *blob, int node,
626d17da655SSimon Glass 		const char *prop_name, int min_len, int *err)
627d17da655SSimon Glass {
628d17da655SSimon Glass 	const void *cell;
629d17da655SSimon Glass 	int len;
630d17da655SSimon Glass 
631d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
632d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
633d17da655SSimon Glass 	if (!cell)
634d17da655SSimon Glass 		*err = -FDT_ERR_NOTFOUND;
635d17da655SSimon Glass 	else if (len < min_len)
636d17da655SSimon Glass 		*err = -FDT_ERR_BADLAYOUT;
637d17da655SSimon Glass 	else
638d17da655SSimon Glass 		*err = 0;
639d17da655SSimon Glass 	return cell;
640d17da655SSimon Glass }
641d17da655SSimon Glass 
642d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
643d17da655SSimon Glass 		u32 *array, int count)
644d17da655SSimon Glass {
645d17da655SSimon Glass 	const u32 *cell;
646d17da655SSimon Glass 	int i, err = 0;
647d17da655SSimon Glass 
648d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
649d17da655SSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
650d17da655SSimon Glass 				      sizeof(u32) * count, &err);
651d17da655SSimon Glass 	if (!err) {
652d17da655SSimon Glass 		for (i = 0; i < count; i++)
653d17da655SSimon Glass 			array[i] = fdt32_to_cpu(cell[i]);
654d17da655SSimon Glass 	}
655d17da655SSimon Glass 	return err;
656d17da655SSimon Glass }
657d17da655SSimon Glass 
658a9f04d49SSimon Glass int fdtdec_get_int_array_count(const void *blob, int node,
659a9f04d49SSimon Glass 			       const char *prop_name, u32 *array, int count)
660a9f04d49SSimon Glass {
661a9f04d49SSimon Glass 	const u32 *cell;
662a9f04d49SSimon Glass 	int len, elems;
663a9f04d49SSimon Glass 	int i;
664a9f04d49SSimon Glass 
665a9f04d49SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
666a9f04d49SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
667a9f04d49SSimon Glass 	if (!cell)
668a9f04d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
669a9f04d49SSimon Glass 	elems = len / sizeof(u32);
670a9f04d49SSimon Glass 	if (count > elems)
671a9f04d49SSimon Glass 		count = elems;
672a9f04d49SSimon Glass 	for (i = 0; i < count; i++)
673a9f04d49SSimon Glass 		array[i] = fdt32_to_cpu(cell[i]);
674a9f04d49SSimon Glass 
675a9f04d49SSimon Glass 	return count;
676a9f04d49SSimon Glass }
677a9f04d49SSimon Glass 
67896875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
67996875e7dSSimon Glass 			       const char *prop_name, int count)
68096875e7dSSimon Glass {
68196875e7dSSimon Glass 	const u32 *cell;
68296875e7dSSimon Glass 	int err;
68396875e7dSSimon Glass 
68496875e7dSSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
68596875e7dSSimon Glass 				      sizeof(u32) * count, &err);
68696875e7dSSimon Glass 	return err ? NULL : cell;
68796875e7dSSimon Glass }
68896875e7dSSimon Glass 
689d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
690d17da655SSimon Glass {
691d17da655SSimon Glass 	const s32 *cell;
692d17da655SSimon Glass 	int len;
693d17da655SSimon Glass 
694d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
695d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
696d17da655SSimon Glass 	return cell != NULL;
697d17da655SSimon Glass }
698ed3ee5cdSSimon Glass 
69957068a7aSSimon Glass int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
70057068a7aSSimon Glass 				   const char *list_name,
70157068a7aSSimon Glass 				   const char *cells_name,
70257068a7aSSimon Glass 				   int cell_count, int index,
70357068a7aSSimon Glass 				   struct fdtdec_phandle_args *out_args)
70457068a7aSSimon Glass {
70557068a7aSSimon Glass 	const __be32 *list, *list_end;
70657068a7aSSimon Glass 	int rc = 0, size, cur_index = 0;
70757068a7aSSimon Glass 	uint32_t count = 0;
70857068a7aSSimon Glass 	int node = -1;
70957068a7aSSimon Glass 	int phandle;
71057068a7aSSimon Glass 
71157068a7aSSimon Glass 	/* Retrieve the phandle list property */
71257068a7aSSimon Glass 	list = fdt_getprop(blob, src_node, list_name, &size);
71357068a7aSSimon Glass 	if (!list)
71457068a7aSSimon Glass 		return -ENOENT;
71557068a7aSSimon Glass 	list_end = list + size / sizeof(*list);
71657068a7aSSimon Glass 
71757068a7aSSimon Glass 	/* Loop over the phandles until all the requested entry is found */
71857068a7aSSimon Glass 	while (list < list_end) {
71957068a7aSSimon Glass 		rc = -EINVAL;
72057068a7aSSimon Glass 		count = 0;
72157068a7aSSimon Glass 
72257068a7aSSimon Glass 		/*
72357068a7aSSimon Glass 		 * If phandle is 0, then it is an empty entry with no
72457068a7aSSimon Glass 		 * arguments.  Skip forward to the next entry.
72557068a7aSSimon Glass 		 */
72657068a7aSSimon Glass 		phandle = be32_to_cpup(list++);
72757068a7aSSimon Glass 		if (phandle) {
72857068a7aSSimon Glass 			/*
72957068a7aSSimon Glass 			 * Find the provider node and parse the #*-cells
73057068a7aSSimon Glass 			 * property to determine the argument length.
73157068a7aSSimon Glass 			 *
73257068a7aSSimon Glass 			 * This is not needed if the cell count is hard-coded
73357068a7aSSimon Glass 			 * (i.e. cells_name not set, but cell_count is set),
73457068a7aSSimon Glass 			 * except when we're going to return the found node
73557068a7aSSimon Glass 			 * below.
73657068a7aSSimon Glass 			 */
73757068a7aSSimon Glass 			if (cells_name || cur_index == index) {
73857068a7aSSimon Glass 				node = fdt_node_offset_by_phandle(blob,
73957068a7aSSimon Glass 								  phandle);
74057068a7aSSimon Glass 				if (!node) {
74157068a7aSSimon Glass 					debug("%s: could not find phandle\n",
74257068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
74357068a7aSSimon Glass 							   NULL));
74457068a7aSSimon Glass 					goto err;
74557068a7aSSimon Glass 				}
74657068a7aSSimon Glass 			}
74757068a7aSSimon Glass 
74857068a7aSSimon Glass 			if (cells_name) {
74957068a7aSSimon Glass 				count = fdtdec_get_int(blob, node, cells_name,
75057068a7aSSimon Glass 						       -1);
75157068a7aSSimon Glass 				if (count == -1) {
75257068a7aSSimon Glass 					debug("%s: could not get %s for %s\n",
75357068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
75457068a7aSSimon Glass 							   NULL),
75557068a7aSSimon Glass 					      cells_name,
75657068a7aSSimon Glass 					      fdt_get_name(blob, node,
75757068a7aSSimon Glass 							   NULL));
75857068a7aSSimon Glass 					goto err;
75957068a7aSSimon Glass 				}
76057068a7aSSimon Glass 			} else {
76157068a7aSSimon Glass 				count = cell_count;
76257068a7aSSimon Glass 			}
76357068a7aSSimon Glass 
76457068a7aSSimon Glass 			/*
76557068a7aSSimon Glass 			 * Make sure that the arguments actually fit in the
76657068a7aSSimon Glass 			 * remaining property data length
76757068a7aSSimon Glass 			 */
76857068a7aSSimon Glass 			if (list + count > list_end) {
76957068a7aSSimon Glass 				debug("%s: arguments longer than property\n",
77057068a7aSSimon Glass 				      fdt_get_name(blob, src_node, NULL));
77157068a7aSSimon Glass 				goto err;
77257068a7aSSimon Glass 			}
77357068a7aSSimon Glass 		}
77457068a7aSSimon Glass 
77557068a7aSSimon Glass 		/*
77657068a7aSSimon Glass 		 * All of the error cases above bail out of the loop, so at
77757068a7aSSimon Glass 		 * this point, the parsing is successful. If the requested
77857068a7aSSimon Glass 		 * index matches, then fill the out_args structure and return,
77957068a7aSSimon Glass 		 * or return -ENOENT for an empty entry.
78057068a7aSSimon Glass 		 */
78157068a7aSSimon Glass 		rc = -ENOENT;
78257068a7aSSimon Glass 		if (cur_index == index) {
78357068a7aSSimon Glass 			if (!phandle)
78457068a7aSSimon Glass 				goto err;
78557068a7aSSimon Glass 
78657068a7aSSimon Glass 			if (out_args) {
78757068a7aSSimon Glass 				int i;
78857068a7aSSimon Glass 
78957068a7aSSimon Glass 				if (count > MAX_PHANDLE_ARGS) {
79057068a7aSSimon Glass 					debug("%s: too many arguments %d\n",
79157068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
79257068a7aSSimon Glass 							   NULL), count);
79357068a7aSSimon Glass 					count = MAX_PHANDLE_ARGS;
79457068a7aSSimon Glass 				}
79557068a7aSSimon Glass 				out_args->node = node;
79657068a7aSSimon Glass 				out_args->args_count = count;
79757068a7aSSimon Glass 				for (i = 0; i < count; i++) {
79857068a7aSSimon Glass 					out_args->args[i] =
79957068a7aSSimon Glass 							be32_to_cpup(list++);
80057068a7aSSimon Glass 				}
80157068a7aSSimon Glass 			}
80257068a7aSSimon Glass 
80357068a7aSSimon Glass 			/* Found it! return success */
80457068a7aSSimon Glass 			return 0;
80557068a7aSSimon Glass 		}
80657068a7aSSimon Glass 
80757068a7aSSimon Glass 		node = -1;
80857068a7aSSimon Glass 		list += count;
80957068a7aSSimon Glass 		cur_index++;
81057068a7aSSimon Glass 	}
81157068a7aSSimon Glass 
81257068a7aSSimon Glass 	/*
81357068a7aSSimon Glass 	 * Result will be one of:
81457068a7aSSimon Glass 	 * -ENOENT : index is for empty phandle
81557068a7aSSimon Glass 	 * -EINVAL : parsing error on data
81657068a7aSSimon Glass 	 * [1..n]  : Number of phandle (count mode; when index = -1)
81757068a7aSSimon Glass 	 */
81857068a7aSSimon Glass 	rc = index < 0 ? cur_index : -ENOENT;
81957068a7aSSimon Glass  err:
82057068a7aSSimon Glass 	return rc;
82157068a7aSSimon Glass }
82257068a7aSSimon Glass 
823bed4d892SAnton Staff int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
824bed4d892SAnton Staff 		u8 *array, int count)
825bed4d892SAnton Staff {
826bed4d892SAnton Staff 	const u8 *cell;
827bed4d892SAnton Staff 	int err;
828bed4d892SAnton Staff 
829bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
830bed4d892SAnton Staff 	if (!err)
831bed4d892SAnton Staff 		memcpy(array, cell, count);
832bed4d892SAnton Staff 	return err;
833bed4d892SAnton Staff }
834bed4d892SAnton Staff 
835bed4d892SAnton Staff const u8 *fdtdec_locate_byte_array(const void *blob, int node,
836bed4d892SAnton Staff 			     const char *prop_name, int count)
837bed4d892SAnton Staff {
838bed4d892SAnton Staff 	const u8 *cell;
839bed4d892SAnton Staff 	int err;
840bed4d892SAnton Staff 
841bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
842bed4d892SAnton Staff 	if (err)
843bed4d892SAnton Staff 		return NULL;
844bed4d892SAnton Staff 	return cell;
845bed4d892SAnton Staff }
84609258f1eSAbhilash Kesavan 
84709258f1eSAbhilash Kesavan int fdtdec_get_config_int(const void *blob, const char *prop_name,
84809258f1eSAbhilash Kesavan 		int default_val)
84909258f1eSAbhilash Kesavan {
85009258f1eSAbhilash Kesavan 	int config_node;
85109258f1eSAbhilash Kesavan 
85209258f1eSAbhilash Kesavan 	debug("%s: %s\n", __func__, prop_name);
85309258f1eSAbhilash Kesavan 	config_node = fdt_path_offset(blob, "/config");
85409258f1eSAbhilash Kesavan 	if (config_node < 0)
85509258f1eSAbhilash Kesavan 		return default_val;
85609258f1eSAbhilash Kesavan 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
85709258f1eSAbhilash Kesavan }
858332ab0d5SSimon Glass 
85979289c0bSGabe Black int fdtdec_get_config_bool(const void *blob, const char *prop_name)
86079289c0bSGabe Black {
86179289c0bSGabe Black 	int config_node;
86279289c0bSGabe Black 	const void *prop;
86379289c0bSGabe Black 
86479289c0bSGabe Black 	debug("%s: %s\n", __func__, prop_name);
86579289c0bSGabe Black 	config_node = fdt_path_offset(blob, "/config");
86679289c0bSGabe Black 	if (config_node < 0)
86779289c0bSGabe Black 		return 0;
86879289c0bSGabe Black 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
86979289c0bSGabe Black 
87079289c0bSGabe Black 	return prop != NULL;
87179289c0bSGabe Black }
87279289c0bSGabe Black 
873332ab0d5SSimon Glass char *fdtdec_get_config_string(const void *blob, const char *prop_name)
874332ab0d5SSimon Glass {
875332ab0d5SSimon Glass 	const char *nodep;
876332ab0d5SSimon Glass 	int nodeoffset;
877332ab0d5SSimon Glass 	int len;
878332ab0d5SSimon Glass 
879332ab0d5SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
880332ab0d5SSimon Glass 	nodeoffset = fdt_path_offset(blob, "/config");
881332ab0d5SSimon Glass 	if (nodeoffset < 0)
882332ab0d5SSimon Glass 		return NULL;
883332ab0d5SSimon Glass 
884332ab0d5SSimon Glass 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
885332ab0d5SSimon Glass 	if (!nodep)
886332ab0d5SSimon Glass 		return NULL;
887332ab0d5SSimon Glass 
888332ab0d5SSimon Glass 	return (char *)nodep;
889332ab0d5SSimon Glass }
890f20c4619SSimon Glass 
89176489832SSimon Glass int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
89276489832SSimon Glass 			 fdt_addr_t *basep, fdt_size_t *sizep)
893f20c4619SSimon Glass {
894f20c4619SSimon Glass 	const fdt_addr_t *cell;
895f20c4619SSimon Glass 	int len;
896f20c4619SSimon Glass 
89776489832SSimon Glass 	debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
89876489832SSimon Glass 	      prop_name);
899f20c4619SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
90076489832SSimon Glass 	if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
90176489832SSimon Glass 		debug("cell=%p, len=%d\n", cell, len);
902f20c4619SSimon Glass 		return -1;
90376489832SSimon Glass 	}
904f20c4619SSimon Glass 
90576489832SSimon Glass 	*basep = fdt_addr_to_cpu(*cell);
90676489832SSimon Glass 	*sizep = fdt_size_to_cpu(cell[1]);
90776489832SSimon Glass 	debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
90876489832SSimon Glass 	      (ulong)*sizep);
90976489832SSimon Glass 
910f20c4619SSimon Glass 	return 0;
911f20c4619SSimon Glass }
912006e73b9SSimon Glass 
913006e73b9SSimon Glass /**
914006e73b9SSimon Glass  * Read a flash entry from the fdt
915006e73b9SSimon Glass  *
916006e73b9SSimon Glass  * @param blob		FDT blob
917006e73b9SSimon Glass  * @param node		Offset of node to read
918006e73b9SSimon Glass  * @param name		Name of node being read
919006e73b9SSimon Glass  * @param entry		Place to put offset and size of this node
920006e73b9SSimon Glass  * @return 0 if ok, -ve on error
921006e73b9SSimon Glass  */
922006e73b9SSimon Glass int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
923006e73b9SSimon Glass 			   struct fmap_entry *entry)
924006e73b9SSimon Glass {
925f3cc44f9SSimon Glass 	const char *prop;
926006e73b9SSimon Glass 	u32 reg[2];
927006e73b9SSimon Glass 
928006e73b9SSimon Glass 	if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
929006e73b9SSimon Glass 		debug("Node '%s' has bad/missing 'reg' property\n", name);
930006e73b9SSimon Glass 		return -FDT_ERR_NOTFOUND;
931006e73b9SSimon Glass 	}
932006e73b9SSimon Glass 	entry->offset = reg[0];
933006e73b9SSimon Glass 	entry->length = reg[1];
934f3cc44f9SSimon Glass 	entry->used = fdtdec_get_int(blob, node, "used", entry->length);
935f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "compress", NULL);
936f3cc44f9SSimon Glass 	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
937f3cc44f9SSimon Glass 		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
938f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
939f3cc44f9SSimon Glass 	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
940f3cc44f9SSimon Glass 	entry->hash = (uint8_t *)prop;
941006e73b9SSimon Glass 
942006e73b9SSimon Glass 	return 0;
943006e73b9SSimon Glass }
94456f42242SThierry Reding 
9455f7bfdd6SSimon Glass u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
94656f42242SThierry Reding {
94756f42242SThierry Reding 	u64 number = 0;
94856f42242SThierry Reding 
94956f42242SThierry Reding 	while (cells--)
95056f42242SThierry Reding 		number = (number << 32) | fdt32_to_cpu(*ptr++);
95156f42242SThierry Reding 
95256f42242SThierry Reding 	return number;
95356f42242SThierry Reding }
95456f42242SThierry Reding 
95556f42242SThierry Reding int fdt_get_resource(const void *fdt, int node, const char *property,
95656f42242SThierry Reding 		     unsigned int index, struct fdt_resource *res)
95756f42242SThierry Reding {
95856f42242SThierry Reding 	const fdt32_t *ptr, *end;
95956f42242SThierry Reding 	int na, ns, len, parent;
96056f42242SThierry Reding 	unsigned int i = 0;
96156f42242SThierry Reding 
96256f42242SThierry Reding 	parent = fdt_parent_offset(fdt, node);
96356f42242SThierry Reding 	if (parent < 0)
96456f42242SThierry Reding 		return parent;
96556f42242SThierry Reding 
96656f42242SThierry Reding 	na = fdt_address_cells(fdt, parent);
96756f42242SThierry Reding 	ns = fdt_size_cells(fdt, parent);
96856f42242SThierry Reding 
96956f42242SThierry Reding 	ptr = fdt_getprop(fdt, node, property, &len);
97056f42242SThierry Reding 	if (!ptr)
97156f42242SThierry Reding 		return len;
97256f42242SThierry Reding 
97356f42242SThierry Reding 	end = ptr + len / sizeof(*ptr);
97456f42242SThierry Reding 
97556f42242SThierry Reding 	while (ptr + na + ns <= end) {
97656f42242SThierry Reding 		if (i == index) {
97756f42242SThierry Reding 			res->start = res->end = fdtdec_get_number(ptr, na);
97856f42242SThierry Reding 			res->end += fdtdec_get_number(&ptr[na], ns) - 1;
97956f42242SThierry Reding 			return 0;
98056f42242SThierry Reding 		}
98156f42242SThierry Reding 
98256f42242SThierry Reding 		ptr += na + ns;
98356f42242SThierry Reding 		i++;
98456f42242SThierry Reding 	}
98556f42242SThierry Reding 
98656f42242SThierry Reding 	return -FDT_ERR_NOTFOUND;
98756f42242SThierry Reding }
98856f42242SThierry Reding 
98956f42242SThierry Reding int fdt_get_named_resource(const void *fdt, int node, const char *property,
99056f42242SThierry Reding 			   const char *prop_names, const char *name,
99156f42242SThierry Reding 			   struct fdt_resource *res)
99256f42242SThierry Reding {
99356f42242SThierry Reding 	int index;
99456f42242SThierry Reding 
99556f42242SThierry Reding 	index = fdt_find_string(fdt, node, prop_names, name);
99656f42242SThierry Reding 	if (index < 0)
99756f42242SThierry Reding 		return index;
99856f42242SThierry Reding 
99956f42242SThierry Reding 	return fdt_get_resource(fdt, node, property, index, res);
100056f42242SThierry Reding }
10019f85eee7SThierry Reding 
100226403871SSimon Glass int fdtdec_decode_memory_region(const void *blob, int config_node,
100326403871SSimon Glass 				const char *mem_type, const char *suffix,
100426403871SSimon Glass 				fdt_addr_t *basep, fdt_size_t *sizep)
100526403871SSimon Glass {
100626403871SSimon Glass 	char prop_name[50];
100726403871SSimon Glass 	const char *mem;
100826403871SSimon Glass 	fdt_size_t size, offset_size;
100926403871SSimon Glass 	fdt_addr_t base, offset;
101026403871SSimon Glass 	int node;
101126403871SSimon Glass 
101226403871SSimon Glass 	if (config_node == -1) {
101326403871SSimon Glass 		config_node = fdt_path_offset(blob, "/config");
101426403871SSimon Glass 		if (config_node < 0) {
101526403871SSimon Glass 			debug("%s: Cannot find /config node\n", __func__);
101626403871SSimon Glass 			return -ENOENT;
101726403871SSimon Glass 		}
101826403871SSimon Glass 	}
101926403871SSimon Glass 	if (!suffix)
102026403871SSimon Glass 		suffix = "";
102126403871SSimon Glass 
102226403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
102326403871SSimon Glass 		 suffix);
102426403871SSimon Glass 	mem = fdt_getprop(blob, config_node, prop_name, NULL);
102526403871SSimon Glass 	if (!mem) {
102626403871SSimon Glass 		debug("%s: No memory type for '%s', using /memory\n", __func__,
102726403871SSimon Glass 		      prop_name);
102826403871SSimon Glass 		mem = "/memory";
102926403871SSimon Glass 	}
103026403871SSimon Glass 
103126403871SSimon Glass 	node = fdt_path_offset(blob, mem);
103226403871SSimon Glass 	if (node < 0) {
103326403871SSimon Glass 		debug("%s: Failed to find node '%s': %s\n", __func__, mem,
103426403871SSimon Glass 		      fdt_strerror(node));
103526403871SSimon Glass 		return -ENOENT;
103626403871SSimon Glass 	}
103726403871SSimon Glass 
103826403871SSimon Glass 	/*
103926403871SSimon Glass 	 * Not strictly correct - the memory may have multiple banks. We just
104026403871SSimon Glass 	 * use the first
104126403871SSimon Glass 	 */
104226403871SSimon Glass 	if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
104326403871SSimon Glass 		debug("%s: Failed to decode memory region %s\n", __func__,
104426403871SSimon Glass 		      mem);
104526403871SSimon Glass 		return -EINVAL;
104626403871SSimon Glass 	}
104726403871SSimon Glass 
104826403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
104926403871SSimon Glass 		 suffix);
105026403871SSimon Glass 	if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
105126403871SSimon Glass 				 &offset_size)) {
105226403871SSimon Glass 		debug("%s: Failed to decode memory region '%s'\n", __func__,
105326403871SSimon Glass 		      prop_name);
105426403871SSimon Glass 		return -EINVAL;
105526403871SSimon Glass 	}
105626403871SSimon Glass 
105726403871SSimon Glass 	*basep = base + offset;
105826403871SSimon Glass 	*sizep = offset_size;
105926403871SSimon Glass 
106026403871SSimon Glass 	return 0;
106126403871SSimon Glass }
1062b45122fdSSimon Glass 
106312e67114SSimon Glass static int decode_timing_property(const void *blob, int node, const char *name,
106412e67114SSimon Glass 				  struct timing_entry *result)
106512e67114SSimon Glass {
106612e67114SSimon Glass 	int length, ret = 0;
106712e67114SSimon Glass 	const u32 *prop;
106812e67114SSimon Glass 
106912e67114SSimon Glass 	prop = fdt_getprop(blob, node, name, &length);
107012e67114SSimon Glass 	if (!prop) {
107112e67114SSimon Glass 		debug("%s: could not find property %s\n",
107212e67114SSimon Glass 		      fdt_get_name(blob, node, NULL), name);
107312e67114SSimon Glass 		return length;
107412e67114SSimon Glass 	}
107512e67114SSimon Glass 
107612e67114SSimon Glass 	if (length == sizeof(u32)) {
107712e67114SSimon Glass 		result->typ = fdtdec_get_int(blob, node, name, 0);
107812e67114SSimon Glass 		result->min = result->typ;
107912e67114SSimon Glass 		result->max = result->typ;
108012e67114SSimon Glass 	} else {
108112e67114SSimon Glass 		ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
108212e67114SSimon Glass 	}
108312e67114SSimon Glass 
108412e67114SSimon Glass 	return ret;
108512e67114SSimon Glass }
108612e67114SSimon Glass 
108712e67114SSimon Glass int fdtdec_decode_display_timing(const void *blob, int parent, int index,
108812e67114SSimon Glass 				 struct display_timing *dt)
108912e67114SSimon Glass {
109012e67114SSimon Glass 	int i, node, timings_node;
109112e67114SSimon Glass 	u32 val = 0;
109212e67114SSimon Glass 	int ret = 0;
109312e67114SSimon Glass 
109412e67114SSimon Glass 	timings_node = fdt_subnode_offset(blob, parent, "display-timings");
109512e67114SSimon Glass 	if (timings_node < 0)
109612e67114SSimon Glass 		return timings_node;
109712e67114SSimon Glass 
109812e67114SSimon Glass 	for (i = 0, node = fdt_first_subnode(blob, timings_node);
109912e67114SSimon Glass 	     node > 0 && i != index;
110012e67114SSimon Glass 	     node = fdt_next_subnode(blob, node))
110112e67114SSimon Glass 		i++;
110212e67114SSimon Glass 
110312e67114SSimon Glass 	if (node < 0)
110412e67114SSimon Glass 		return node;
110512e67114SSimon Glass 
110612e67114SSimon Glass 	memset(dt, 0, sizeof(*dt));
110712e67114SSimon Glass 
110812e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hback-porch",
110912e67114SSimon Glass 				      &dt->hback_porch);
111012e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hfront-porch",
111112e67114SSimon Glass 				      &dt->hfront_porch);
111212e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
111312e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
111412e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vback-porch",
111512e67114SSimon Glass 				      &dt->vback_porch);
111612e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vfront-porch",
111712e67114SSimon Glass 				      &dt->vfront_porch);
111812e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
111912e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
112012e67114SSimon Glass 	ret |= decode_timing_property(blob, node, "clock-frequency",
112112e67114SSimon Glass 				      &dt->pixelclock);
112212e67114SSimon Glass 
112312e67114SSimon Glass 	dt->flags = 0;
112412e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "vsync-active", -1);
112512e67114SSimon Glass 	if (val != -1) {
112612e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
112712e67114SSimon Glass 				DISPLAY_FLAGS_VSYNC_LOW;
112812e67114SSimon Glass 	}
112912e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "hsync-active", -1);
113012e67114SSimon Glass 	if (val != -1) {
113112e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
113212e67114SSimon Glass 				DISPLAY_FLAGS_HSYNC_LOW;
113312e67114SSimon Glass 	}
113412e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "de-active", -1);
113512e67114SSimon Glass 	if (val != -1) {
113612e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
113712e67114SSimon Glass 				DISPLAY_FLAGS_DE_LOW;
113812e67114SSimon Glass 	}
113912e67114SSimon Glass 	val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
114012e67114SSimon Glass 	if (val != -1) {
114112e67114SSimon Glass 		dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
114212e67114SSimon Glass 				DISPLAY_FLAGS_PIXDATA_NEGEDGE;
114312e67114SSimon Glass 	}
114412e67114SSimon Glass 
114512e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "interlaced"))
114612e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_INTERLACED;
114712e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doublescan"))
114812e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
114912e67114SSimon Glass 	if (fdtdec_get_bool(blob, node, "doubleclk"))
115012e67114SSimon Glass 		dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
115112e67114SSimon Glass 
115212e67114SSimon Glass 	return 0;
115312e67114SSimon Glass }
115412e67114SSimon Glass 
11550879361fSSimon Glass int fdtdec_setup(void)
1156b45122fdSSimon Glass {
1157b45122fdSSimon Glass #ifdef CONFIG_OF_CONTROL
1158b45122fdSSimon Glass # ifdef CONFIG_OF_EMBED
1159b45122fdSSimon Glass 	/* Get a pointer to the FDT */
1160b45122fdSSimon Glass 	gd->fdt_blob = __dtb_dt_begin;
1161b45122fdSSimon Glass # elif defined CONFIG_OF_SEPARATE
1162b45122fdSSimon Glass #  ifdef CONFIG_SPL_BUILD
1163b45122fdSSimon Glass 	/* FDT is at end of BSS */
1164b45122fdSSimon Glass 	gd->fdt_blob = (ulong *)&__bss_end;
1165b45122fdSSimon Glass #  else
1166b45122fdSSimon Glass 	/* FDT is at end of image */
1167b45122fdSSimon Glass 	gd->fdt_blob = (ulong *)&_end;
116829a23f9dSHeiko Schocher #endif
1169b45122fdSSimon Glass # elif defined(CONFIG_OF_HOSTFILE)
1170b45122fdSSimon Glass 	if (sandbox_read_fdt_from_file()) {
1171b45122fdSSimon Glass 		puts("Failed to read control FDT\n");
1172b45122fdSSimon Glass 		return -1;
1173b45122fdSSimon Glass 	}
1174b45122fdSSimon Glass # endif
1175b45122fdSSimon Glass # ifndef CONFIG_SPL_BUILD
1176b45122fdSSimon Glass 	/* Allow the early environment to override the fdt address */
1177b45122fdSSimon Glass 	gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
1178b45122fdSSimon Glass 						(uintptr_t)gd->fdt_blob);
1179b45122fdSSimon Glass # endif
1180b45122fdSSimon Glass #endif
11810879361fSSimon Glass 	return fdtdec_prepare_fdt();
1182b45122fdSSimon Glass }
1183b45122fdSSimon Glass 
1184b45122fdSSimon Glass #endif /* !USE_HOSTCC */
1185