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