xref: /rk3399_rockchip-uboot/lib/fdtdec.c (revision 66312374dca86e77fc9b08f774546e62b6cd1aa7)
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>
125c33c9fdSSimon Glass #include <linux/ctype.h>
13b5220bc6SSimon Glass 
14b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
15b5220bc6SSimon Glass 
16b5220bc6SSimon Glass /*
17b5220bc6SSimon Glass  * Here are the type we know about. One day we might allow drivers to
18b5220bc6SSimon Glass  * register. For now we just put them here. The COMPAT macro allows us to
19b5220bc6SSimon Glass  * turn this into a sparse list later, and keeps the ID with the name.
20b5220bc6SSimon Glass  */
21b5220bc6SSimon Glass #define COMPAT(id, name) name
22b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = {
23f88fe2deSSimon Glass 	COMPAT(UNKNOWN, "<none>"),
2487f938c9SSimon Glass 	COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
257e44d932SJim Lin 	COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
267e44d932SJim Lin 	COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
270e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
280e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
296642a681SRakesh Iyer 	COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
30312693c3SJim Lin 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
31e1ae0d1fSSimon Glass 	COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
3287540de3SWei Ni 	COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
33a73ca478SStephen Warren 	COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
34f4e4e0b0STom Warren 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
35c9aa831eSTom Warren 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
36f315828bSThierry Reding 	COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
37f315828bSThierry Reding 	COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
38f315828bSThierry Reding 	COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
3979c7a90fSThierry Reding 	COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
40cc9fe33aSHatim RV 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
41cc9fe33aSHatim RV 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
42c34253d1SRajeshwari Shinde 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
4372dbff12SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
4472dbff12SRajeshwari Shinde 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
45713cb680SHung-ying Tyan 	COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
466abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
47108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
486abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
49108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
50618766c0SAkshay Saraswat 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
51d7377b51SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
52de461c52SPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
531e4706a7SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
547d3ca0f8SJaehoon Chung 	COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
553577fe8bSPiotr Wilczek 	COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
56ee1e3c2fSRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
57cd577e2bSRajeshwari Shinde 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
58bb8215f4SSimon Glass 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
597772bb78SRajeshwari Shinde 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
60f6267998SRong Chang 	COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
61ec34fa5eSVincent Palatin 	COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
62ecbd7e1eSnaveen krishna chatradhi 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
637d95f2a3SSimon Glass 	COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
64ac1058fdSTom Wai-Hong Tam 	COMPAT(TI_TPS65090, "ti,tps65090"),
65a9cf6da9SSimon Glass 	COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
6645c480c9SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
679e8f664eSVadim Bendebury 	COMPAT(PARADE_PS8625, "parade,ps8625"),
6877f9b1fbSSimon Glass 	COMPAT(INTEL_MICROCODE, "intel,microcode"),
6965dd74a6SSimon Glass 	COMPAT(MEMORY_SPD, "memory-spd"),
703ac83935SSimon Glass 	COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
71bb80be39SSimon Glass 	COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
72effcf067SSimon Glass 	COMPAT(INTEL_GMA, "intel,gma"),
736173c45bSThierry Reding 	COMPAT(AMS_AS3722, "ams,as3722"),
745da38086SSimon Glass 	COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
75c89ada01SBin Meng 	COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
766462cdedSMasahiro Yamada 	COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
7790b16d14SSimon Glass 	COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
78b5220bc6SSimon Glass };
79b5220bc6SSimon Glass 
80a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id)
81a53f4a29SSimon Glass {
82a53f4a29SSimon Glass 	/* We allow reading of the 'unknown' ID for testing purposes */
83a53f4a29SSimon Glass 	assert(id >= 0 && id < COMPAT_COUNT);
84a53f4a29SSimon Glass 	return compat_names[id];
85a53f4a29SSimon Glass }
86a53f4a29SSimon Glass 
874397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
884397a2a8SSimon Glass 		const char *prop_name, fdt_size_t *sizep)
89b5220bc6SSimon Glass {
90b5220bc6SSimon Glass 	const fdt_addr_t *cell;
91b5220bc6SSimon Glass 	int len;
92b5220bc6SSimon Glass 
931cb2323bSSimon Glass 	debug("%s: %s: ", __func__, prop_name);
94b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
954397a2a8SSimon Glass 	if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
961cb2323bSSimon Glass 		     len == sizeof(fdt_addr_t) * 2)) {
971cb2323bSSimon Glass 		fdt_addr_t addr = fdt_addr_to_cpu(*cell);
984397a2a8SSimon Glass 		if (sizep) {
994397a2a8SSimon Glass 			const fdt_size_t *size;
1001cb2323bSSimon Glass 
1014397a2a8SSimon Glass 			size = (fdt_size_t *)((char *)cell +
1024397a2a8SSimon Glass 					sizeof(fdt_addr_t));
1034397a2a8SSimon Glass 			*sizep = fdt_size_to_cpu(*size);
104370b6c5cSSimon Glass 			debug("addr=%08lx, size=%08x\n",
105370b6c5cSSimon Glass 			      (ulong)addr, *sizep);
1064397a2a8SSimon Glass 		} else {
107370b6c5cSSimon Glass 			debug("%08lx\n", (ulong)addr);
1084397a2a8SSimon Glass 		}
1091cb2323bSSimon Glass 		return addr;
1101cb2323bSSimon Glass 	}
1111cb2323bSSimon Glass 	debug("(not found)\n");
112b5220bc6SSimon Glass 	return FDT_ADDR_T_NONE;
113b5220bc6SSimon Glass }
114b5220bc6SSimon Glass 
1154397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
1164397a2a8SSimon Glass 		const char *prop_name)
1174397a2a8SSimon Glass {
1184397a2a8SSimon Glass 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
1194397a2a8SSimon Glass }
1204397a2a8SSimon Glass 
121a62e84d7SBin Meng #ifdef CONFIG_PCI
122a62e84d7SBin Meng int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
123a62e84d7SBin Meng 		const char *prop_name, struct fdt_pci_addr *addr)
124a62e84d7SBin Meng {
125a62e84d7SBin Meng 	const u32 *cell;
126a62e84d7SBin Meng 	int len;
127a62e84d7SBin Meng 	int ret = -ENOENT;
128a62e84d7SBin Meng 
129a62e84d7SBin Meng 	debug("%s: %s: ", __func__, prop_name);
130a62e84d7SBin Meng 
131a62e84d7SBin Meng 	/*
132a62e84d7SBin Meng 	 * If we follow the pci bus bindings strictly, we should check
133a62e84d7SBin Meng 	 * the value of the node's parent node's #address-cells and
134a62e84d7SBin Meng 	 * #size-cells. They need to be 3 and 2 accordingly. However,
135a62e84d7SBin Meng 	 * for simplicity we skip the check here.
136a62e84d7SBin Meng 	 */
137a62e84d7SBin Meng 	cell = fdt_getprop(blob, node, prop_name, &len);
138a62e84d7SBin Meng 	if (!cell)
139a62e84d7SBin Meng 		goto fail;
140a62e84d7SBin Meng 
141a62e84d7SBin Meng 	if ((len % FDT_PCI_REG_SIZE) == 0) {
142a62e84d7SBin Meng 		int num = len / FDT_PCI_REG_SIZE;
143a62e84d7SBin Meng 		int i;
144a62e84d7SBin Meng 
145a62e84d7SBin Meng 		for (i = 0; i < num; i++) {
146a62e84d7SBin Meng 			debug("pci address #%d: %08lx %08lx %08lx\n", i,
147a62e84d7SBin Meng 			      (ulong)fdt_addr_to_cpu(cell[0]),
148a62e84d7SBin Meng 			      (ulong)fdt_addr_to_cpu(cell[1]),
149a62e84d7SBin Meng 			      (ulong)fdt_addr_to_cpu(cell[2]));
150a62e84d7SBin Meng 			if ((fdt_addr_to_cpu(*cell) & type) == type) {
151a62e84d7SBin Meng 				addr->phys_hi = fdt_addr_to_cpu(cell[0]);
152a62e84d7SBin Meng 				addr->phys_mid = fdt_addr_to_cpu(cell[1]);
153a62e84d7SBin Meng 				addr->phys_lo = fdt_addr_to_cpu(cell[2]);
154a62e84d7SBin Meng 				break;
155a62e84d7SBin Meng 			} else {
156a62e84d7SBin Meng 				cell += (FDT_PCI_ADDR_CELLS +
157a62e84d7SBin Meng 					 FDT_PCI_SIZE_CELLS);
158a62e84d7SBin Meng 			}
159a62e84d7SBin Meng 		}
160a62e84d7SBin Meng 
161106cce96SSimon Glass 		if (i == num) {
162106cce96SSimon Glass 			ret = -ENXIO;
163a62e84d7SBin Meng 			goto fail;
164106cce96SSimon Glass 		}
165a62e84d7SBin Meng 
166a62e84d7SBin Meng 		return 0;
167a62e84d7SBin Meng 	} else {
168a62e84d7SBin Meng 		ret = -EINVAL;
169a62e84d7SBin Meng 	}
170a62e84d7SBin Meng 
171a62e84d7SBin Meng fail:
172a62e84d7SBin Meng 	debug("(not found)\n");
173a62e84d7SBin Meng 	return ret;
174a62e84d7SBin Meng }
175a62e84d7SBin Meng 
176a62e84d7SBin Meng int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
177a62e84d7SBin Meng {
178a62e84d7SBin Meng 	const char *list, *end;
179a62e84d7SBin Meng 	int len;
180a62e84d7SBin Meng 
181a62e84d7SBin Meng 	list = fdt_getprop(blob, node, "compatible", &len);
182a62e84d7SBin Meng 	if (!list)
183a62e84d7SBin Meng 		return -ENOENT;
184a62e84d7SBin Meng 
185a62e84d7SBin Meng 	end = list + len;
186a62e84d7SBin Meng 	while (list < end) {
187a62e84d7SBin Meng 		char *s;
188a62e84d7SBin Meng 
189a62e84d7SBin Meng 		len = strlen(list);
190a62e84d7SBin Meng 		if (len >= strlen("pciVVVV,DDDD")) {
191a62e84d7SBin Meng 			s = strstr(list, "pci");
192a62e84d7SBin Meng 
193a62e84d7SBin Meng 			/*
194a62e84d7SBin Meng 			 * check if the string is something like pciVVVV,DDDD.RR
195a62e84d7SBin Meng 			 * or just pciVVVV,DDDD
196a62e84d7SBin Meng 			 */
197a62e84d7SBin Meng 			if (s && s[7] == ',' &&
198a62e84d7SBin Meng 			    (s[12] == '.' || s[12] == 0)) {
199a62e84d7SBin Meng 				s += 3;
200a62e84d7SBin Meng 				*vendor = simple_strtol(s, NULL, 16);
201a62e84d7SBin Meng 
202a62e84d7SBin Meng 				s += 5;
203a62e84d7SBin Meng 				*device = simple_strtol(s, NULL, 16);
204a62e84d7SBin Meng 
205a62e84d7SBin Meng 				return 0;
206a62e84d7SBin Meng 			}
207a62e84d7SBin Meng 		} else {
208a62e84d7SBin Meng 			list += (len + 1);
209a62e84d7SBin Meng 		}
210a62e84d7SBin Meng 	}
211a62e84d7SBin Meng 
212a62e84d7SBin Meng 	return -ENOENT;
213a62e84d7SBin Meng }
214a62e84d7SBin Meng 
215a62e84d7SBin Meng int fdtdec_get_pci_bdf(const void *blob, int node,
216a62e84d7SBin Meng 		struct fdt_pci_addr *addr, pci_dev_t *bdf)
217a62e84d7SBin Meng {
218a62e84d7SBin Meng 	u16 dt_vendor, dt_device, vendor, device;
219a62e84d7SBin Meng 	int ret;
220a62e84d7SBin Meng 
221a62e84d7SBin Meng 	/* get vendor id & device id from the compatible string */
222a62e84d7SBin Meng 	ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
223a62e84d7SBin Meng 	if (ret)
224a62e84d7SBin Meng 		return ret;
225a62e84d7SBin Meng 
226a62e84d7SBin Meng 	/* extract the bdf from fdt_pci_addr */
227a62e84d7SBin Meng 	*bdf = addr->phys_hi & 0xffff00;
228a62e84d7SBin Meng 
229a62e84d7SBin Meng 	/* read vendor id & device id based on bdf */
230a62e84d7SBin Meng 	pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
231a62e84d7SBin Meng 	pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
232a62e84d7SBin Meng 
233a62e84d7SBin Meng 	/*
234a62e84d7SBin Meng 	 * Note there are two places in the device tree to fully describe
235a62e84d7SBin Meng 	 * a pci device: one is via compatible string with a format of
236a62e84d7SBin Meng 	 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
237a62e84d7SBin Meng 	 * the device node's reg address property. We read the vendor id
238a62e84d7SBin Meng 	 * and device id based on bdf and compare the values with the
239a62e84d7SBin Meng 	 * "VVVV,DDDD". If they are the same, then we are good to use bdf
240a62e84d7SBin Meng 	 * to read device's bar. But if they are different, we have to rely
241a62e84d7SBin Meng 	 * on the vendor id and device id extracted from the compatible
242a62e84d7SBin Meng 	 * string and locate the real bdf by pci_find_device(). This is
243a62e84d7SBin Meng 	 * because normally we may only know device's device number and
244a62e84d7SBin Meng 	 * function number when writing device tree. The bus number is
245a62e84d7SBin Meng 	 * dynamically assigned during the pci enumeration process.
246a62e84d7SBin Meng 	 */
247a62e84d7SBin Meng 	if ((dt_vendor != vendor) || (dt_device != device)) {
248a62e84d7SBin Meng 		*bdf = pci_find_device(dt_vendor, dt_device, 0);
249a62e84d7SBin Meng 		if (*bdf == -1)
250a62e84d7SBin Meng 			return -ENODEV;
251a62e84d7SBin Meng 	}
252a62e84d7SBin Meng 
253a62e84d7SBin Meng 	return 0;
254a62e84d7SBin Meng }
255a62e84d7SBin Meng 
256a62e84d7SBin Meng int fdtdec_get_pci_bar32(const void *blob, int node,
257a62e84d7SBin Meng 		struct fdt_pci_addr *addr, u32 *bar)
258a62e84d7SBin Meng {
259a62e84d7SBin Meng 	pci_dev_t bdf;
260a62e84d7SBin Meng 	int barnum;
261a62e84d7SBin Meng 	int ret;
262a62e84d7SBin Meng 
263a62e84d7SBin Meng 	/* get pci devices's bdf */
264a62e84d7SBin Meng 	ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
265a62e84d7SBin Meng 	if (ret)
266a62e84d7SBin Meng 		return ret;
267a62e84d7SBin Meng 
268a62e84d7SBin Meng 	/* extract the bar number from fdt_pci_addr */
269a62e84d7SBin Meng 	barnum = addr->phys_hi & 0xff;
270a62e84d7SBin Meng 	if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
271a62e84d7SBin Meng 		return -EINVAL;
272a62e84d7SBin Meng 
273a62e84d7SBin Meng 	barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
274a62e84d7SBin Meng 	*bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
275a62e84d7SBin Meng 
276a62e84d7SBin Meng 	return 0;
277a62e84d7SBin Meng }
278a62e84d7SBin Meng #endif
279a62e84d7SBin Meng 
280aadef0a1SChe-Liang Chiou uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
281aadef0a1SChe-Liang Chiou 		uint64_t default_val)
282aadef0a1SChe-Liang Chiou {
283aadef0a1SChe-Liang Chiou 	const uint64_t *cell64;
284aadef0a1SChe-Liang Chiou 	int length;
285aadef0a1SChe-Liang Chiou 
286aadef0a1SChe-Liang Chiou 	cell64 = fdt_getprop(blob, node, prop_name, &length);
287aadef0a1SChe-Liang Chiou 	if (!cell64 || length < sizeof(*cell64))
288aadef0a1SChe-Liang Chiou 		return default_val;
289aadef0a1SChe-Liang Chiou 
290aadef0a1SChe-Liang Chiou 	return fdt64_to_cpu(*cell64);
291aadef0a1SChe-Liang Chiou }
292aadef0a1SChe-Liang Chiou 
293f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node)
294b5220bc6SSimon Glass {
295b5220bc6SSimon Glass 	const char *cell;
296b5220bc6SSimon Glass 
297f88fe2deSSimon Glass 	/*
298f88fe2deSSimon Glass 	 * It should say "okay", so only allow that. Some fdts use "ok" but
299f88fe2deSSimon Glass 	 * this is a bug. Please fix your device tree source file. See here
300f88fe2deSSimon Glass 	 * for discussion:
301f88fe2deSSimon Glass 	 *
302f88fe2deSSimon Glass 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
303f88fe2deSSimon Glass 	 */
304b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, "status", NULL);
305b5220bc6SSimon Glass 	if (cell)
306f88fe2deSSimon Glass 		return 0 == strcmp(cell, "okay");
307f88fe2deSSimon Glass 	return 1;
308b5220bc6SSimon Glass }
309b5220bc6SSimon Glass 
3107cde397bSGerald Van Baren enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
311b5220bc6SSimon Glass {
312b5220bc6SSimon Glass 	enum fdt_compat_id id;
313b5220bc6SSimon Glass 
314b5220bc6SSimon Glass 	/* Search our drivers */
315b5220bc6SSimon Glass 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
316b5220bc6SSimon Glass 		if (0 == fdt_node_check_compatible(blob, node,
317b5220bc6SSimon Glass 				compat_names[id]))
318b5220bc6SSimon Glass 			return id;
319b5220bc6SSimon Glass 	return COMPAT_UNKNOWN;
320b5220bc6SSimon Glass }
321b5220bc6SSimon Glass 
322b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node,
323b5220bc6SSimon Glass 		enum fdt_compat_id id)
324b5220bc6SSimon Glass {
325b5220bc6SSimon Glass 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
326b5220bc6SSimon Glass }
327b5220bc6SSimon Glass 
3283ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
3293ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp)
3303ddecfc7SSimon Glass {
3313ddecfc7SSimon Glass 	do {
3323ddecfc7SSimon Glass 		node = fdt_next_node(blob, node, depthp);
3333ddecfc7SSimon Glass 	} while (*depthp > 1);
3343ddecfc7SSimon Glass 
3353ddecfc7SSimon Glass 	/* If this is a direct subnode, and compatible, return it */
3363ddecfc7SSimon Glass 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
3373ddecfc7SSimon Glass 						blob, node, compat_names[id]))
3383ddecfc7SSimon Glass 		return node;
3393ddecfc7SSimon Glass 
3403ddecfc7SSimon Glass 	return -FDT_ERR_NOTFOUND;
3413ddecfc7SSimon Glass }
3423ddecfc7SSimon Glass 
343b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
344b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto)
345b5220bc6SSimon Glass {
346b5220bc6SSimon Glass #define MAX_STR_LEN 20
347b5220bc6SSimon Glass 	char str[MAX_STR_LEN + 20];
348b5220bc6SSimon Glass 	int node, err;
349b5220bc6SSimon Glass 
350b5220bc6SSimon Glass 	/* snprintf() is not available */
351b5220bc6SSimon Glass 	assert(strlen(name) < MAX_STR_LEN);
352b5220bc6SSimon Glass 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
35300878476SSimon Glass 	node = fdt_path_offset(blob, str);
354b5220bc6SSimon Glass 	if (node < 0)
355b5220bc6SSimon Glass 		return node;
356b5220bc6SSimon Glass 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
357b5220bc6SSimon Glass 	if (err < 0)
358b5220bc6SSimon Glass 		return err;
359f88fe2deSSimon Glass 	if (err)
360f88fe2deSSimon Glass 		return -FDT_ERR_NOTFOUND;
361f88fe2deSSimon Glass 	(*upto)++;
362f88fe2deSSimon Glass 	return node;
363b5220bc6SSimon Glass }
364b5220bc6SSimon Glass 
365a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
366a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
367a53f4a29SSimon Glass {
368c6782270SSimon Glass 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
369c6782270SSimon Glass 
370c6782270SSimon Glass 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
371c6782270SSimon Glass }
372c6782270SSimon Glass 
373c6782270SSimon Glass /* TODO: Can we tighten this code up a little? */
374c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
375c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
376c6782270SSimon Glass {
377a53f4a29SSimon Glass 	int name_len = strlen(name);
378a53f4a29SSimon Glass 	int nodes[maxcount];
379a53f4a29SSimon Glass 	int num_found = 0;
380a53f4a29SSimon Glass 	int offset, node;
381a53f4a29SSimon Glass 	int alias_node;
382a53f4a29SSimon Glass 	int count;
383a53f4a29SSimon Glass 	int i, j;
384a53f4a29SSimon Glass 
385a53f4a29SSimon Glass 	/* find the alias node if present */
386a53f4a29SSimon Glass 	alias_node = fdt_path_offset(blob, "/aliases");
387a53f4a29SSimon Glass 
388a53f4a29SSimon Glass 	/*
389a53f4a29SSimon Glass 	 * start with nothing, and we can assume that the root node can't
390a53f4a29SSimon Glass 	 * match
391a53f4a29SSimon Glass 	 */
392a53f4a29SSimon Glass 	memset(nodes, '\0', sizeof(nodes));
393a53f4a29SSimon Glass 
394a53f4a29SSimon Glass 	/* First find all the compatible nodes */
395a53f4a29SSimon Glass 	for (node = count = 0; node >= 0 && count < maxcount;) {
396a53f4a29SSimon Glass 		node = fdtdec_next_compatible(blob, node, id);
397a53f4a29SSimon Glass 		if (node >= 0)
398a53f4a29SSimon Glass 			nodes[count++] = node;
399a53f4a29SSimon Glass 	}
400a53f4a29SSimon Glass 	if (node >= 0)
401a53f4a29SSimon Glass 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
402a53f4a29SSimon Glass 		       __func__, name);
403a53f4a29SSimon Glass 
404a53f4a29SSimon Glass 	/* Now find all the aliases */
405a53f4a29SSimon Glass 	for (offset = fdt_first_property_offset(blob, alias_node);
406a53f4a29SSimon Glass 			offset > 0;
407a53f4a29SSimon Glass 			offset = fdt_next_property_offset(blob, offset)) {
408a53f4a29SSimon Glass 		const struct fdt_property *prop;
409a53f4a29SSimon Glass 		const char *path;
410a53f4a29SSimon Glass 		int number;
411a53f4a29SSimon Glass 		int found;
412a53f4a29SSimon Glass 
413a53f4a29SSimon Glass 		node = 0;
414a53f4a29SSimon Glass 		prop = fdt_get_property_by_offset(blob, offset, NULL);
415a53f4a29SSimon Glass 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
416a53f4a29SSimon Glass 		if (prop->len && 0 == strncmp(path, name, name_len))
417a53f4a29SSimon Glass 			node = fdt_path_offset(blob, prop->data);
418a53f4a29SSimon Glass 		if (node <= 0)
419a53f4a29SSimon Glass 			continue;
420a53f4a29SSimon Glass 
421a53f4a29SSimon Glass 		/* Get the alias number */
422a53f4a29SSimon Glass 		number = simple_strtoul(path + name_len, NULL, 10);
423a53f4a29SSimon Glass 		if (number < 0 || number >= maxcount) {
424a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' is out of range\n",
425a53f4a29SSimon Glass 			       __func__, path);
426a53f4a29SSimon Glass 			continue;
427a53f4a29SSimon Glass 		}
428a53f4a29SSimon Glass 
429a53f4a29SSimon Glass 		/* Make sure the node we found is actually in our list! */
430a53f4a29SSimon Glass 		found = -1;
431a53f4a29SSimon Glass 		for (j = 0; j < count; j++)
432a53f4a29SSimon Glass 			if (nodes[j] == node) {
433a53f4a29SSimon Glass 				found = j;
434a53f4a29SSimon Glass 				break;
435a53f4a29SSimon Glass 			}
436a53f4a29SSimon Glass 
437a53f4a29SSimon Glass 		if (found == -1) {
438a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' points to a node "
439a53f4a29SSimon Glass 				"'%s' that is missing or is not compatible "
440a53f4a29SSimon Glass 				" with '%s'\n", __func__, path,
441a53f4a29SSimon Glass 				fdt_get_name(blob, node, NULL),
442a53f4a29SSimon Glass 			       compat_names[id]);
443a53f4a29SSimon Glass 			continue;
444a53f4a29SSimon Glass 		}
445a53f4a29SSimon Glass 
446a53f4a29SSimon Glass 		/*
447a53f4a29SSimon Glass 		 * Add this node to our list in the right place, and mark
448a53f4a29SSimon Glass 		 * it as done.
449a53f4a29SSimon Glass 		 */
450a53f4a29SSimon Glass 		if (fdtdec_get_is_enabled(blob, node)) {
451c6782270SSimon Glass 			if (node_list[number]) {
452c6782270SSimon Glass 				debug("%s: warning: alias '%s' requires that "
453c6782270SSimon Glass 				      "a node be placed in the list in a "
454c6782270SSimon Glass 				      "position which is already filled by "
455c6782270SSimon Glass 				      "node '%s'\n", __func__, path,
456c6782270SSimon Glass 				      fdt_get_name(blob, node, NULL));
457c6782270SSimon Glass 				continue;
458c6782270SSimon Glass 			}
459a53f4a29SSimon Glass 			node_list[number] = node;
460a53f4a29SSimon Glass 			if (number >= num_found)
461a53f4a29SSimon Glass 				num_found = number + 1;
462a53f4a29SSimon Glass 		}
463c6782270SSimon Glass 		nodes[found] = 0;
464a53f4a29SSimon Glass 	}
465a53f4a29SSimon Glass 
466a53f4a29SSimon Glass 	/* Add any nodes not mentioned by an alias */
467a53f4a29SSimon Glass 	for (i = j = 0; i < maxcount; i++) {
468a53f4a29SSimon Glass 		if (!node_list[i]) {
469a53f4a29SSimon Glass 			for (; j < maxcount; j++)
470a53f4a29SSimon Glass 				if (nodes[j] &&
471a53f4a29SSimon Glass 					fdtdec_get_is_enabled(blob, nodes[j]))
472a53f4a29SSimon Glass 					break;
473a53f4a29SSimon Glass 
474a53f4a29SSimon Glass 			/* Have we run out of nodes to add? */
475a53f4a29SSimon Glass 			if (j == maxcount)
476a53f4a29SSimon Glass 				break;
477a53f4a29SSimon Glass 
478a53f4a29SSimon Glass 			assert(!node_list[i]);
479a53f4a29SSimon Glass 			node_list[i] = nodes[j++];
480a53f4a29SSimon Glass 			if (i >= num_found)
481a53f4a29SSimon Glass 				num_found = i + 1;
482a53f4a29SSimon Glass 		}
483a53f4a29SSimon Glass 	}
484a53f4a29SSimon Glass 
485a53f4a29SSimon Glass 	return num_found;
486a53f4a29SSimon Glass }
487a53f4a29SSimon Glass 
4885c33c9fdSSimon Glass int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
4895c33c9fdSSimon Glass 			 int *seqp)
4905c33c9fdSSimon Glass {
4915c33c9fdSSimon Glass 	int base_len = strlen(base);
4925c33c9fdSSimon Glass 	const char *find_name;
4935c33c9fdSSimon Glass 	int find_namelen;
4945c33c9fdSSimon Glass 	int prop_offset;
4955c33c9fdSSimon Glass 	int aliases;
4965c33c9fdSSimon Glass 
4975c33c9fdSSimon Glass 	find_name = fdt_get_name(blob, offset, &find_namelen);
4985c33c9fdSSimon Glass 	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
4995c33c9fdSSimon Glass 
5005c33c9fdSSimon Glass 	aliases = fdt_path_offset(blob, "/aliases");
5015c33c9fdSSimon Glass 	for (prop_offset = fdt_first_property_offset(blob, aliases);
5025c33c9fdSSimon Glass 	     prop_offset > 0;
5035c33c9fdSSimon Glass 	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
5045c33c9fdSSimon Glass 		const char *prop;
5055c33c9fdSSimon Glass 		const char *name;
5065c33c9fdSSimon Glass 		const char *slash;
5075c33c9fdSSimon Glass 		const char *p;
5085c33c9fdSSimon Glass 		int len;
5095c33c9fdSSimon Glass 
5105c33c9fdSSimon Glass 		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
5115c33c9fdSSimon Glass 		debug("   - %s, %s\n", name, prop);
5125c33c9fdSSimon Glass 		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
5135c33c9fdSSimon Glass 		    strncmp(name, base, base_len))
5145c33c9fdSSimon Glass 			continue;
5155c33c9fdSSimon Glass 
5165c33c9fdSSimon Glass 		slash = strrchr(prop, '/');
5175c33c9fdSSimon Glass 		if (strcmp(slash + 1, find_name))
5185c33c9fdSSimon Glass 			continue;
519a88340dfSSimon Glass 		for (p = name + strlen(name) - 1; p > name; p--) {
520a88340dfSSimon Glass 			if (!isdigit(*p)) {
521a88340dfSSimon Glass 				*seqp = simple_strtoul(p + 1, NULL, 10);
5225c33c9fdSSimon Glass 				debug("Found seq %d\n", *seqp);
5235c33c9fdSSimon Glass 				return 0;
5245c33c9fdSSimon Glass 			}
5255c33c9fdSSimon Glass 		}
5265c33c9fdSSimon Glass 	}
5275c33c9fdSSimon Glass 
5285c33c9fdSSimon Glass 	debug("Not found\n");
5295c33c9fdSSimon Glass 	return -ENOENT;
5305c33c9fdSSimon Glass }
5315c33c9fdSSimon Glass 
532aac07d49SSimon Glass int fdtdec_get_chosen_node(const void *blob, const char *name)
533aac07d49SSimon Glass {
534aac07d49SSimon Glass 	const char *prop;
535aac07d49SSimon Glass 	int chosen_node;
536aac07d49SSimon Glass 	int len;
537aac07d49SSimon Glass 
538aac07d49SSimon Glass 	if (!blob)
539aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
540aac07d49SSimon Glass 	chosen_node = fdt_path_offset(blob, "/chosen");
541aac07d49SSimon Glass 	prop = fdt_getprop(blob, chosen_node, name, &len);
542aac07d49SSimon Glass 	if (!prop)
543aac07d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
544aac07d49SSimon Glass 	return fdt_path_offset(blob, prop);
545aac07d49SSimon Glass }
546aac07d49SSimon Glass 
5479a263e55SSimon Glass int fdtdec_check_fdt(void)
5489a263e55SSimon Glass {
5499a263e55SSimon Glass 	/*
5509a263e55SSimon Glass 	 * We must have an FDT, but we cannot panic() yet since the console
5519a263e55SSimon Glass 	 * is not ready. So for now, just assert(). Boards which need an early
5529a263e55SSimon Glass 	 * FDT (prior to console ready) will need to make their own
5539a263e55SSimon Glass 	 * arrangements and do their own checks.
5549a263e55SSimon Glass 	 */
5559a263e55SSimon Glass 	assert(!fdtdec_prepare_fdt());
5569a263e55SSimon Glass 	return 0;
5579a263e55SSimon Glass }
5589a263e55SSimon Glass 
559b5220bc6SSimon Glass /*
560b5220bc6SSimon Glass  * This function is a little odd in that it accesses global data. At some
561b5220bc6SSimon Glass  * point if the architecture board.c files merge this will make more sense.
562b5220bc6SSimon Glass  * Even now, it is common code.
563b5220bc6SSimon Glass  */
5649a263e55SSimon Glass int fdtdec_prepare_fdt(void)
565b5220bc6SSimon Glass {
566c309c2daSSimon Glass 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
567c309c2daSSimon Glass 	    fdt_check_header(gd->fdt_blob)) {
568*66312374SSimon Glass #ifdef CONFIG_SPL_BUILD
569*66312374SSimon Glass 		puts("Missing DTB\n");
570*66312374SSimon Glass #else
571*66312374SSimon 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");
572*66312374SSimon Glass #endif
5739a263e55SSimon Glass 		return -1;
5749a263e55SSimon Glass 	}
575b5220bc6SSimon Glass 	return 0;
576b5220bc6SSimon Glass }
577d17da655SSimon Glass 
578d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
579d17da655SSimon Glass {
580d17da655SSimon Glass 	const u32 *phandle;
581d17da655SSimon Glass 	int lookup;
582d17da655SSimon Glass 
5831cb2323bSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
584d17da655SSimon Glass 	phandle = fdt_getprop(blob, node, prop_name, NULL);
585d17da655SSimon Glass 	if (!phandle)
586d17da655SSimon Glass 		return -FDT_ERR_NOTFOUND;
587d17da655SSimon Glass 
588d17da655SSimon Glass 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
589d17da655SSimon Glass 	return lookup;
590d17da655SSimon Glass }
591d17da655SSimon Glass 
592d17da655SSimon Glass /**
593d17da655SSimon Glass  * Look up a property in a node and check that it has a minimum length.
594d17da655SSimon Glass  *
595d17da655SSimon Glass  * @param blob		FDT blob
596d17da655SSimon Glass  * @param node		node to examine
597d17da655SSimon Glass  * @param prop_name	name of property to find
598d17da655SSimon Glass  * @param min_len	minimum property length in bytes
599d17da655SSimon Glass  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
600d17da655SSimon Glass 			found, or -FDT_ERR_BADLAYOUT if not enough data
601d17da655SSimon Glass  * @return pointer to cell, which is only valid if err == 0
602d17da655SSimon Glass  */
603d17da655SSimon Glass static const void *get_prop_check_min_len(const void *blob, int node,
604d17da655SSimon Glass 		const char *prop_name, int min_len, int *err)
605d17da655SSimon Glass {
606d17da655SSimon Glass 	const void *cell;
607d17da655SSimon Glass 	int len;
608d17da655SSimon Glass 
609d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
610d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
611d17da655SSimon Glass 	if (!cell)
612d17da655SSimon Glass 		*err = -FDT_ERR_NOTFOUND;
613d17da655SSimon Glass 	else if (len < min_len)
614d17da655SSimon Glass 		*err = -FDT_ERR_BADLAYOUT;
615d17da655SSimon Glass 	else
616d17da655SSimon Glass 		*err = 0;
617d17da655SSimon Glass 	return cell;
618d17da655SSimon Glass }
619d17da655SSimon Glass 
620d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
621d17da655SSimon Glass 		u32 *array, int count)
622d17da655SSimon Glass {
623d17da655SSimon Glass 	const u32 *cell;
624d17da655SSimon Glass 	int i, err = 0;
625d17da655SSimon Glass 
626d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
627d17da655SSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
628d17da655SSimon Glass 				      sizeof(u32) * count, &err);
629d17da655SSimon Glass 	if (!err) {
630d17da655SSimon Glass 		for (i = 0; i < count; i++)
631d17da655SSimon Glass 			array[i] = fdt32_to_cpu(cell[i]);
632d17da655SSimon Glass 	}
633d17da655SSimon Glass 	return err;
634d17da655SSimon Glass }
635d17da655SSimon Glass 
636a9f04d49SSimon Glass int fdtdec_get_int_array_count(const void *blob, int node,
637a9f04d49SSimon Glass 			       const char *prop_name, u32 *array, int count)
638a9f04d49SSimon Glass {
639a9f04d49SSimon Glass 	const u32 *cell;
640a9f04d49SSimon Glass 	int len, elems;
641a9f04d49SSimon Glass 	int i;
642a9f04d49SSimon Glass 
643a9f04d49SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
644a9f04d49SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
645a9f04d49SSimon Glass 	if (!cell)
646a9f04d49SSimon Glass 		return -FDT_ERR_NOTFOUND;
647a9f04d49SSimon Glass 	elems = len / sizeof(u32);
648a9f04d49SSimon Glass 	if (count > elems)
649a9f04d49SSimon Glass 		count = elems;
650a9f04d49SSimon Glass 	for (i = 0; i < count; i++)
651a9f04d49SSimon Glass 		array[i] = fdt32_to_cpu(cell[i]);
652a9f04d49SSimon Glass 
653a9f04d49SSimon Glass 	return count;
654a9f04d49SSimon Glass }
655a9f04d49SSimon Glass 
65696875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
65796875e7dSSimon Glass 			       const char *prop_name, int count)
65896875e7dSSimon Glass {
65996875e7dSSimon Glass 	const u32 *cell;
66096875e7dSSimon Glass 	int err;
66196875e7dSSimon Glass 
66296875e7dSSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
66396875e7dSSimon Glass 				      sizeof(u32) * count, &err);
66496875e7dSSimon Glass 	return err ? NULL : cell;
66596875e7dSSimon Glass }
66696875e7dSSimon Glass 
667d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
668d17da655SSimon Glass {
669d17da655SSimon Glass 	const s32 *cell;
670d17da655SSimon Glass 	int len;
671d17da655SSimon Glass 
672d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
673d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
674d17da655SSimon Glass 	return cell != NULL;
675d17da655SSimon Glass }
676ed3ee5cdSSimon Glass 
67757068a7aSSimon Glass int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
67857068a7aSSimon Glass 				   const char *list_name,
67957068a7aSSimon Glass 				   const char *cells_name,
68057068a7aSSimon Glass 				   int cell_count, int index,
68157068a7aSSimon Glass 				   struct fdtdec_phandle_args *out_args)
68257068a7aSSimon Glass {
68357068a7aSSimon Glass 	const __be32 *list, *list_end;
68457068a7aSSimon Glass 	int rc = 0, size, cur_index = 0;
68557068a7aSSimon Glass 	uint32_t count = 0;
68657068a7aSSimon Glass 	int node = -1;
68757068a7aSSimon Glass 	int phandle;
68857068a7aSSimon Glass 
68957068a7aSSimon Glass 	/* Retrieve the phandle list property */
69057068a7aSSimon Glass 	list = fdt_getprop(blob, src_node, list_name, &size);
69157068a7aSSimon Glass 	if (!list)
69257068a7aSSimon Glass 		return -ENOENT;
69357068a7aSSimon Glass 	list_end = list + size / sizeof(*list);
69457068a7aSSimon Glass 
69557068a7aSSimon Glass 	/* Loop over the phandles until all the requested entry is found */
69657068a7aSSimon Glass 	while (list < list_end) {
69757068a7aSSimon Glass 		rc = -EINVAL;
69857068a7aSSimon Glass 		count = 0;
69957068a7aSSimon Glass 
70057068a7aSSimon Glass 		/*
70157068a7aSSimon Glass 		 * If phandle is 0, then it is an empty entry with no
70257068a7aSSimon Glass 		 * arguments.  Skip forward to the next entry.
70357068a7aSSimon Glass 		 */
70457068a7aSSimon Glass 		phandle = be32_to_cpup(list++);
70557068a7aSSimon Glass 		if (phandle) {
70657068a7aSSimon Glass 			/*
70757068a7aSSimon Glass 			 * Find the provider node and parse the #*-cells
70857068a7aSSimon Glass 			 * property to determine the argument length.
70957068a7aSSimon Glass 			 *
71057068a7aSSimon Glass 			 * This is not needed if the cell count is hard-coded
71157068a7aSSimon Glass 			 * (i.e. cells_name not set, but cell_count is set),
71257068a7aSSimon Glass 			 * except when we're going to return the found node
71357068a7aSSimon Glass 			 * below.
71457068a7aSSimon Glass 			 */
71557068a7aSSimon Glass 			if (cells_name || cur_index == index) {
71657068a7aSSimon Glass 				node = fdt_node_offset_by_phandle(blob,
71757068a7aSSimon Glass 								  phandle);
71857068a7aSSimon Glass 				if (!node) {
71957068a7aSSimon Glass 					debug("%s: could not find phandle\n",
72057068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
72157068a7aSSimon Glass 							   NULL));
72257068a7aSSimon Glass 					goto err;
72357068a7aSSimon Glass 				}
72457068a7aSSimon Glass 			}
72557068a7aSSimon Glass 
72657068a7aSSimon Glass 			if (cells_name) {
72757068a7aSSimon Glass 				count = fdtdec_get_int(blob, node, cells_name,
72857068a7aSSimon Glass 						       -1);
72957068a7aSSimon Glass 				if (count == -1) {
73057068a7aSSimon Glass 					debug("%s: could not get %s for %s\n",
73157068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
73257068a7aSSimon Glass 							   NULL),
73357068a7aSSimon Glass 					      cells_name,
73457068a7aSSimon Glass 					      fdt_get_name(blob, node,
73557068a7aSSimon Glass 							   NULL));
73657068a7aSSimon Glass 					goto err;
73757068a7aSSimon Glass 				}
73857068a7aSSimon Glass 			} else {
73957068a7aSSimon Glass 				count = cell_count;
74057068a7aSSimon Glass 			}
74157068a7aSSimon Glass 
74257068a7aSSimon Glass 			/*
74357068a7aSSimon Glass 			 * Make sure that the arguments actually fit in the
74457068a7aSSimon Glass 			 * remaining property data length
74557068a7aSSimon Glass 			 */
74657068a7aSSimon Glass 			if (list + count > list_end) {
74757068a7aSSimon Glass 				debug("%s: arguments longer than property\n",
74857068a7aSSimon Glass 				      fdt_get_name(blob, src_node, NULL));
74957068a7aSSimon Glass 				goto err;
75057068a7aSSimon Glass 			}
75157068a7aSSimon Glass 		}
75257068a7aSSimon Glass 
75357068a7aSSimon Glass 		/*
75457068a7aSSimon Glass 		 * All of the error cases above bail out of the loop, so at
75557068a7aSSimon Glass 		 * this point, the parsing is successful. If the requested
75657068a7aSSimon Glass 		 * index matches, then fill the out_args structure and return,
75757068a7aSSimon Glass 		 * or return -ENOENT for an empty entry.
75857068a7aSSimon Glass 		 */
75957068a7aSSimon Glass 		rc = -ENOENT;
76057068a7aSSimon Glass 		if (cur_index == index) {
76157068a7aSSimon Glass 			if (!phandle)
76257068a7aSSimon Glass 				goto err;
76357068a7aSSimon Glass 
76457068a7aSSimon Glass 			if (out_args) {
76557068a7aSSimon Glass 				int i;
76657068a7aSSimon Glass 
76757068a7aSSimon Glass 				if (count > MAX_PHANDLE_ARGS) {
76857068a7aSSimon Glass 					debug("%s: too many arguments %d\n",
76957068a7aSSimon Glass 					      fdt_get_name(blob, src_node,
77057068a7aSSimon Glass 							   NULL), count);
77157068a7aSSimon Glass 					count = MAX_PHANDLE_ARGS;
77257068a7aSSimon Glass 				}
77357068a7aSSimon Glass 				out_args->node = node;
77457068a7aSSimon Glass 				out_args->args_count = count;
77557068a7aSSimon Glass 				for (i = 0; i < count; i++) {
77657068a7aSSimon Glass 					out_args->args[i] =
77757068a7aSSimon Glass 							be32_to_cpup(list++);
77857068a7aSSimon Glass 				}
77957068a7aSSimon Glass 			}
78057068a7aSSimon Glass 
78157068a7aSSimon Glass 			/* Found it! return success */
78257068a7aSSimon Glass 			return 0;
78357068a7aSSimon Glass 		}
78457068a7aSSimon Glass 
78557068a7aSSimon Glass 		node = -1;
78657068a7aSSimon Glass 		list += count;
78757068a7aSSimon Glass 		cur_index++;
78857068a7aSSimon Glass 	}
78957068a7aSSimon Glass 
79057068a7aSSimon Glass 	/*
79157068a7aSSimon Glass 	 * Result will be one of:
79257068a7aSSimon Glass 	 * -ENOENT : index is for empty phandle
79357068a7aSSimon Glass 	 * -EINVAL : parsing error on data
79457068a7aSSimon Glass 	 * [1..n]  : Number of phandle (count mode; when index = -1)
79557068a7aSSimon Glass 	 */
79657068a7aSSimon Glass 	rc = index < 0 ? cur_index : -ENOENT;
79757068a7aSSimon Glass  err:
79857068a7aSSimon Glass 	return rc;
79957068a7aSSimon Glass }
80057068a7aSSimon Glass 
801bed4d892SAnton Staff int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
802bed4d892SAnton Staff 		u8 *array, int count)
803bed4d892SAnton Staff {
804bed4d892SAnton Staff 	const u8 *cell;
805bed4d892SAnton Staff 	int err;
806bed4d892SAnton Staff 
807bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
808bed4d892SAnton Staff 	if (!err)
809bed4d892SAnton Staff 		memcpy(array, cell, count);
810bed4d892SAnton Staff 	return err;
811bed4d892SAnton Staff }
812bed4d892SAnton Staff 
813bed4d892SAnton Staff const u8 *fdtdec_locate_byte_array(const void *blob, int node,
814bed4d892SAnton Staff 			     const char *prop_name, int count)
815bed4d892SAnton Staff {
816bed4d892SAnton Staff 	const u8 *cell;
817bed4d892SAnton Staff 	int err;
818bed4d892SAnton Staff 
819bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
820bed4d892SAnton Staff 	if (err)
821bed4d892SAnton Staff 		return NULL;
822bed4d892SAnton Staff 	return cell;
823bed4d892SAnton Staff }
82409258f1eSAbhilash Kesavan 
82509258f1eSAbhilash Kesavan int fdtdec_get_config_int(const void *blob, const char *prop_name,
82609258f1eSAbhilash Kesavan 		int default_val)
82709258f1eSAbhilash Kesavan {
82809258f1eSAbhilash Kesavan 	int config_node;
82909258f1eSAbhilash Kesavan 
83009258f1eSAbhilash Kesavan 	debug("%s: %s\n", __func__, prop_name);
83109258f1eSAbhilash Kesavan 	config_node = fdt_path_offset(blob, "/config");
83209258f1eSAbhilash Kesavan 	if (config_node < 0)
83309258f1eSAbhilash Kesavan 		return default_val;
83409258f1eSAbhilash Kesavan 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
83509258f1eSAbhilash Kesavan }
836332ab0d5SSimon Glass 
83779289c0bSGabe Black int fdtdec_get_config_bool(const void *blob, const char *prop_name)
83879289c0bSGabe Black {
83979289c0bSGabe Black 	int config_node;
84079289c0bSGabe Black 	const void *prop;
84179289c0bSGabe Black 
84279289c0bSGabe Black 	debug("%s: %s\n", __func__, prop_name);
84379289c0bSGabe Black 	config_node = fdt_path_offset(blob, "/config");
84479289c0bSGabe Black 	if (config_node < 0)
84579289c0bSGabe Black 		return 0;
84679289c0bSGabe Black 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
84779289c0bSGabe Black 
84879289c0bSGabe Black 	return prop != NULL;
84979289c0bSGabe Black }
85079289c0bSGabe Black 
851332ab0d5SSimon Glass char *fdtdec_get_config_string(const void *blob, const char *prop_name)
852332ab0d5SSimon Glass {
853332ab0d5SSimon Glass 	const char *nodep;
854332ab0d5SSimon Glass 	int nodeoffset;
855332ab0d5SSimon Glass 	int len;
856332ab0d5SSimon Glass 
857332ab0d5SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
858332ab0d5SSimon Glass 	nodeoffset = fdt_path_offset(blob, "/config");
859332ab0d5SSimon Glass 	if (nodeoffset < 0)
860332ab0d5SSimon Glass 		return NULL;
861332ab0d5SSimon Glass 
862332ab0d5SSimon Glass 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
863332ab0d5SSimon Glass 	if (!nodep)
864332ab0d5SSimon Glass 		return NULL;
865332ab0d5SSimon Glass 
866332ab0d5SSimon Glass 	return (char *)nodep;
867332ab0d5SSimon Glass }
868f20c4619SSimon Glass 
86976489832SSimon Glass int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
87076489832SSimon Glass 			 fdt_addr_t *basep, fdt_size_t *sizep)
871f20c4619SSimon Glass {
872f20c4619SSimon Glass 	const fdt_addr_t *cell;
873f20c4619SSimon Glass 	int len;
874f20c4619SSimon Glass 
87576489832SSimon Glass 	debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
87676489832SSimon Glass 	      prop_name);
877f20c4619SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
87876489832SSimon Glass 	if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
87976489832SSimon Glass 		debug("cell=%p, len=%d\n", cell, len);
880f20c4619SSimon Glass 		return -1;
88176489832SSimon Glass 	}
882f20c4619SSimon Glass 
88376489832SSimon Glass 	*basep = fdt_addr_to_cpu(*cell);
88476489832SSimon Glass 	*sizep = fdt_size_to_cpu(cell[1]);
88576489832SSimon Glass 	debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
88676489832SSimon Glass 	      (ulong)*sizep);
88776489832SSimon Glass 
888f20c4619SSimon Glass 	return 0;
889f20c4619SSimon Glass }
890006e73b9SSimon Glass 
891006e73b9SSimon Glass /**
892006e73b9SSimon Glass  * Read a flash entry from the fdt
893006e73b9SSimon Glass  *
894006e73b9SSimon Glass  * @param blob		FDT blob
895006e73b9SSimon Glass  * @param node		Offset of node to read
896006e73b9SSimon Glass  * @param name		Name of node being read
897006e73b9SSimon Glass  * @param entry		Place to put offset and size of this node
898006e73b9SSimon Glass  * @return 0 if ok, -ve on error
899006e73b9SSimon Glass  */
900006e73b9SSimon Glass int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
901006e73b9SSimon Glass 			   struct fmap_entry *entry)
902006e73b9SSimon Glass {
903f3cc44f9SSimon Glass 	const char *prop;
904006e73b9SSimon Glass 	u32 reg[2];
905006e73b9SSimon Glass 
906006e73b9SSimon Glass 	if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
907006e73b9SSimon Glass 		debug("Node '%s' has bad/missing 'reg' property\n", name);
908006e73b9SSimon Glass 		return -FDT_ERR_NOTFOUND;
909006e73b9SSimon Glass 	}
910006e73b9SSimon Glass 	entry->offset = reg[0];
911006e73b9SSimon Glass 	entry->length = reg[1];
912f3cc44f9SSimon Glass 	entry->used = fdtdec_get_int(blob, node, "used", entry->length);
913f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "compress", NULL);
914f3cc44f9SSimon Glass 	entry->compress_algo = prop && !strcmp(prop, "lzo") ?
915f3cc44f9SSimon Glass 		FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
916f3cc44f9SSimon Glass 	prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
917f3cc44f9SSimon Glass 	entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
918f3cc44f9SSimon Glass 	entry->hash = (uint8_t *)prop;
919006e73b9SSimon Glass 
920006e73b9SSimon Glass 	return 0;
921006e73b9SSimon Glass }
92256f42242SThierry Reding 
9235f7bfdd6SSimon Glass u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
92456f42242SThierry Reding {
92556f42242SThierry Reding 	u64 number = 0;
92656f42242SThierry Reding 
92756f42242SThierry Reding 	while (cells--)
92856f42242SThierry Reding 		number = (number << 32) | fdt32_to_cpu(*ptr++);
92956f42242SThierry Reding 
93056f42242SThierry Reding 	return number;
93156f42242SThierry Reding }
93256f42242SThierry Reding 
93356f42242SThierry Reding int fdt_get_resource(const void *fdt, int node, const char *property,
93456f42242SThierry Reding 		     unsigned int index, struct fdt_resource *res)
93556f42242SThierry Reding {
93656f42242SThierry Reding 	const fdt32_t *ptr, *end;
93756f42242SThierry Reding 	int na, ns, len, parent;
93856f42242SThierry Reding 	unsigned int i = 0;
93956f42242SThierry Reding 
94056f42242SThierry Reding 	parent = fdt_parent_offset(fdt, node);
94156f42242SThierry Reding 	if (parent < 0)
94256f42242SThierry Reding 		return parent;
94356f42242SThierry Reding 
94456f42242SThierry Reding 	na = fdt_address_cells(fdt, parent);
94556f42242SThierry Reding 	ns = fdt_size_cells(fdt, parent);
94656f42242SThierry Reding 
94756f42242SThierry Reding 	ptr = fdt_getprop(fdt, node, property, &len);
94856f42242SThierry Reding 	if (!ptr)
94956f42242SThierry Reding 		return len;
95056f42242SThierry Reding 
95156f42242SThierry Reding 	end = ptr + len / sizeof(*ptr);
95256f42242SThierry Reding 
95356f42242SThierry Reding 	while (ptr + na + ns <= end) {
95456f42242SThierry Reding 		if (i == index) {
95556f42242SThierry Reding 			res->start = res->end = fdtdec_get_number(ptr, na);
95656f42242SThierry Reding 			res->end += fdtdec_get_number(&ptr[na], ns) - 1;
95756f42242SThierry Reding 			return 0;
95856f42242SThierry Reding 		}
95956f42242SThierry Reding 
96056f42242SThierry Reding 		ptr += na + ns;
96156f42242SThierry Reding 		i++;
96256f42242SThierry Reding 	}
96356f42242SThierry Reding 
96456f42242SThierry Reding 	return -FDT_ERR_NOTFOUND;
96556f42242SThierry Reding }
96656f42242SThierry Reding 
96756f42242SThierry Reding int fdt_get_named_resource(const void *fdt, int node, const char *property,
96856f42242SThierry Reding 			   const char *prop_names, const char *name,
96956f42242SThierry Reding 			   struct fdt_resource *res)
97056f42242SThierry Reding {
97156f42242SThierry Reding 	int index;
97256f42242SThierry Reding 
97356f42242SThierry Reding 	index = fdt_find_string(fdt, node, prop_names, name);
97456f42242SThierry Reding 	if (index < 0)
97556f42242SThierry Reding 		return index;
97656f42242SThierry Reding 
97756f42242SThierry Reding 	return fdt_get_resource(fdt, node, property, index, res);
97856f42242SThierry Reding }
9799f85eee7SThierry Reding 
98026403871SSimon Glass int fdtdec_decode_memory_region(const void *blob, int config_node,
98126403871SSimon Glass 				const char *mem_type, const char *suffix,
98226403871SSimon Glass 				fdt_addr_t *basep, fdt_size_t *sizep)
98326403871SSimon Glass {
98426403871SSimon Glass 	char prop_name[50];
98526403871SSimon Glass 	const char *mem;
98626403871SSimon Glass 	fdt_size_t size, offset_size;
98726403871SSimon Glass 	fdt_addr_t base, offset;
98826403871SSimon Glass 	int node;
98926403871SSimon Glass 
99026403871SSimon Glass 	if (config_node == -1) {
99126403871SSimon Glass 		config_node = fdt_path_offset(blob, "/config");
99226403871SSimon Glass 		if (config_node < 0) {
99326403871SSimon Glass 			debug("%s: Cannot find /config node\n", __func__);
99426403871SSimon Glass 			return -ENOENT;
99526403871SSimon Glass 		}
99626403871SSimon Glass 	}
99726403871SSimon Glass 	if (!suffix)
99826403871SSimon Glass 		suffix = "";
99926403871SSimon Glass 
100026403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
100126403871SSimon Glass 		 suffix);
100226403871SSimon Glass 	mem = fdt_getprop(blob, config_node, prop_name, NULL);
100326403871SSimon Glass 	if (!mem) {
100426403871SSimon Glass 		debug("%s: No memory type for '%s', using /memory\n", __func__,
100526403871SSimon Glass 		      prop_name);
100626403871SSimon Glass 		mem = "/memory";
100726403871SSimon Glass 	}
100826403871SSimon Glass 
100926403871SSimon Glass 	node = fdt_path_offset(blob, mem);
101026403871SSimon Glass 	if (node < 0) {
101126403871SSimon Glass 		debug("%s: Failed to find node '%s': %s\n", __func__, mem,
101226403871SSimon Glass 		      fdt_strerror(node));
101326403871SSimon Glass 		return -ENOENT;
101426403871SSimon Glass 	}
101526403871SSimon Glass 
101626403871SSimon Glass 	/*
101726403871SSimon Glass 	 * Not strictly correct - the memory may have multiple banks. We just
101826403871SSimon Glass 	 * use the first
101926403871SSimon Glass 	 */
102026403871SSimon Glass 	if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
102126403871SSimon Glass 		debug("%s: Failed to decode memory region %s\n", __func__,
102226403871SSimon Glass 		      mem);
102326403871SSimon Glass 		return -EINVAL;
102426403871SSimon Glass 	}
102526403871SSimon Glass 
102626403871SSimon Glass 	snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
102726403871SSimon Glass 		 suffix);
102826403871SSimon Glass 	if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
102926403871SSimon Glass 				 &offset_size)) {
103026403871SSimon Glass 		debug("%s: Failed to decode memory region '%s'\n", __func__,
103126403871SSimon Glass 		      prop_name);
103226403871SSimon Glass 		return -EINVAL;
103326403871SSimon Glass 	}
103426403871SSimon Glass 
103526403871SSimon Glass 	*basep = base + offset;
103626403871SSimon Glass 	*sizep = offset_size;
103726403871SSimon Glass 
103826403871SSimon Glass 	return 0;
103926403871SSimon Glass }
104029a23f9dSHeiko Schocher #endif
1041