xref: /rk3399_rockchip-uboot/lib/fdtdec.c (revision 108b85be25e4a87d59f762b5e6cca83313ecc9e8)
1b5220bc6SSimon Glass /*
2b5220bc6SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
31a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
4b5220bc6SSimon Glass  */
5b5220bc6SSimon Glass 
6b5220bc6SSimon Glass #include <common.h>
7b5220bc6SSimon Glass #include <serial.h>
8b5220bc6SSimon Glass #include <libfdt.h>
9b5220bc6SSimon Glass #include <fdtdec.h>
10b5220bc6SSimon Glass 
11e46431e1SMichal Simek #include <asm/gpio.h>
12ed3ee5cdSSimon Glass 
13b5220bc6SSimon Glass DECLARE_GLOBAL_DATA_PTR;
14b5220bc6SSimon Glass 
15b5220bc6SSimon Glass /*
16b5220bc6SSimon Glass  * Here are the type we know about. One day we might allow drivers to
17b5220bc6SSimon Glass  * register. For now we just put them here. The COMPAT macro allows us to
18b5220bc6SSimon Glass  * turn this into a sparse list later, and keeps the ID with the name.
19b5220bc6SSimon Glass  */
20b5220bc6SSimon Glass #define COMPAT(id, name) name
21b5220bc6SSimon Glass static const char * const compat_names[COMPAT_COUNT] = {
22f88fe2deSSimon Glass 	COMPAT(UNKNOWN, "<none>"),
2387f938c9SSimon Glass 	COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
247e44d932SJim Lin 	COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
257e44d932SJim Lin 	COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
26e32624efSTom Warren 	COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
2796a78ac0SYen Lin 	COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
2896a78ac0SYen Lin 	COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
290e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
300e35ad05SJimmy Zhang 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
316642a681SRakesh Iyer 	COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
32312693c3SJim Lin 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
33e1ae0d1fSSimon Glass 	COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
3487540de3SWei Ni 	COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
35f4e4e0b0STom Warren 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
36c9aa831eSTom Warren 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
378f1b46b1SAllen Martin 	COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
38b19f5749SAllen Martin 	COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
39c3bb3c8bSAllen Martin 	COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
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"),
455d50659dSRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
4688364387SHung-ying Tyan 	COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
47713cb680SHung-ying Tyan 	COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
486abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
49*108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
506abd1620SRajeshwari Shinde 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
51*108b85beSVivek Gautam 	COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
52618766c0SAkshay Saraswat 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
53d7377b51SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
541e4706a7SAjay Kumar 	COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
5545a4d4d3SAmar 	COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"),
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"),
63b5220bc6SSimon Glass };
64b5220bc6SSimon Glass 
65a53f4a29SSimon Glass const char *fdtdec_get_compatible(enum fdt_compat_id id)
66a53f4a29SSimon Glass {
67a53f4a29SSimon Glass 	/* We allow reading of the 'unknown' ID for testing purposes */
68a53f4a29SSimon Glass 	assert(id >= 0 && id < COMPAT_COUNT);
69a53f4a29SSimon Glass 	return compat_names[id];
70a53f4a29SSimon Glass }
71a53f4a29SSimon Glass 
724397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
734397a2a8SSimon Glass 		const char *prop_name, fdt_size_t *sizep)
74b5220bc6SSimon Glass {
75b5220bc6SSimon Glass 	const fdt_addr_t *cell;
76b5220bc6SSimon Glass 	int len;
77b5220bc6SSimon Glass 
781cb2323bSSimon Glass 	debug("%s: %s: ", __func__, prop_name);
79b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
804397a2a8SSimon Glass 	if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
811cb2323bSSimon Glass 		     len == sizeof(fdt_addr_t) * 2)) {
821cb2323bSSimon Glass 		fdt_addr_t addr = fdt_addr_to_cpu(*cell);
834397a2a8SSimon Glass 		if (sizep) {
844397a2a8SSimon Glass 			const fdt_size_t *size;
851cb2323bSSimon Glass 
864397a2a8SSimon Glass 			size = (fdt_size_t *)((char *)cell +
874397a2a8SSimon Glass 					sizeof(fdt_addr_t));
884397a2a8SSimon Glass 			*sizep = fdt_size_to_cpu(*size);
894397a2a8SSimon Glass 			debug("addr=%p, size=%p\n", (void *)addr,
904397a2a8SSimon Glass 			      (void *)*sizep);
914397a2a8SSimon Glass 		} else {
921cb2323bSSimon Glass 			debug("%p\n", (void *)addr);
934397a2a8SSimon Glass 		}
941cb2323bSSimon Glass 		return addr;
951cb2323bSSimon Glass 	}
961cb2323bSSimon Glass 	debug("(not found)\n");
97b5220bc6SSimon Glass 	return FDT_ADDR_T_NONE;
98b5220bc6SSimon Glass }
99b5220bc6SSimon Glass 
1004397a2a8SSimon Glass fdt_addr_t fdtdec_get_addr(const void *blob, int node,
1014397a2a8SSimon Glass 		const char *prop_name)
1024397a2a8SSimon Glass {
1034397a2a8SSimon Glass 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
1044397a2a8SSimon Glass }
1054397a2a8SSimon Glass 
106b5220bc6SSimon Glass s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
107b5220bc6SSimon Glass 		s32 default_val)
108b5220bc6SSimon Glass {
109b5220bc6SSimon Glass 	const s32 *cell;
110b5220bc6SSimon Glass 	int len;
111b5220bc6SSimon Glass 
1121cb2323bSSimon Glass 	debug("%s: %s: ", __func__, prop_name);
113b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
1141cb2323bSSimon Glass 	if (cell && len >= sizeof(s32)) {
1151cb2323bSSimon Glass 		s32 val = fdt32_to_cpu(cell[0]);
1161cb2323bSSimon Glass 
1171cb2323bSSimon Glass 		debug("%#x (%d)\n", val, val);
1181cb2323bSSimon Glass 		return val;
1191cb2323bSSimon Glass 	}
1201cb2323bSSimon Glass 	debug("(not found)\n");
121b5220bc6SSimon Glass 	return default_val;
122b5220bc6SSimon Glass }
123b5220bc6SSimon Glass 
124aadef0a1SChe-Liang Chiou uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
125aadef0a1SChe-Liang Chiou 		uint64_t default_val)
126aadef0a1SChe-Liang Chiou {
127aadef0a1SChe-Liang Chiou 	const uint64_t *cell64;
128aadef0a1SChe-Liang Chiou 	int length;
129aadef0a1SChe-Liang Chiou 
130aadef0a1SChe-Liang Chiou 	cell64 = fdt_getprop(blob, node, prop_name, &length);
131aadef0a1SChe-Liang Chiou 	if (!cell64 || length < sizeof(*cell64))
132aadef0a1SChe-Liang Chiou 		return default_val;
133aadef0a1SChe-Liang Chiou 
134aadef0a1SChe-Liang Chiou 	return fdt64_to_cpu(*cell64);
135aadef0a1SChe-Liang Chiou }
136aadef0a1SChe-Liang Chiou 
137f88fe2deSSimon Glass int fdtdec_get_is_enabled(const void *blob, int node)
138b5220bc6SSimon Glass {
139b5220bc6SSimon Glass 	const char *cell;
140b5220bc6SSimon Glass 
141f88fe2deSSimon Glass 	/*
142f88fe2deSSimon Glass 	 * It should say "okay", so only allow that. Some fdts use "ok" but
143f88fe2deSSimon Glass 	 * this is a bug. Please fix your device tree source file. See here
144f88fe2deSSimon Glass 	 * for discussion:
145f88fe2deSSimon Glass 	 *
146f88fe2deSSimon Glass 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
147f88fe2deSSimon Glass 	 */
148b5220bc6SSimon Glass 	cell = fdt_getprop(blob, node, "status", NULL);
149b5220bc6SSimon Glass 	if (cell)
150f88fe2deSSimon Glass 		return 0 == strcmp(cell, "okay");
151f88fe2deSSimon Glass 	return 1;
152b5220bc6SSimon Glass }
153b5220bc6SSimon Glass 
1547cde397bSGerald Van Baren enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
155b5220bc6SSimon Glass {
156b5220bc6SSimon Glass 	enum fdt_compat_id id;
157b5220bc6SSimon Glass 
158b5220bc6SSimon Glass 	/* Search our drivers */
159b5220bc6SSimon Glass 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
160b5220bc6SSimon Glass 		if (0 == fdt_node_check_compatible(blob, node,
161b5220bc6SSimon Glass 				compat_names[id]))
162b5220bc6SSimon Glass 			return id;
163b5220bc6SSimon Glass 	return COMPAT_UNKNOWN;
164b5220bc6SSimon Glass }
165b5220bc6SSimon Glass 
166b5220bc6SSimon Glass int fdtdec_next_compatible(const void *blob, int node,
167b5220bc6SSimon Glass 		enum fdt_compat_id id)
168b5220bc6SSimon Glass {
169b5220bc6SSimon Glass 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
170b5220bc6SSimon Glass }
171b5220bc6SSimon Glass 
1723ddecfc7SSimon Glass int fdtdec_next_compatible_subnode(const void *blob, int node,
1733ddecfc7SSimon Glass 		enum fdt_compat_id id, int *depthp)
1743ddecfc7SSimon Glass {
1753ddecfc7SSimon Glass 	do {
1763ddecfc7SSimon Glass 		node = fdt_next_node(blob, node, depthp);
1773ddecfc7SSimon Glass 	} while (*depthp > 1);
1783ddecfc7SSimon Glass 
1793ddecfc7SSimon Glass 	/* If this is a direct subnode, and compatible, return it */
1803ddecfc7SSimon Glass 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
1813ddecfc7SSimon Glass 						blob, node, compat_names[id]))
1823ddecfc7SSimon Glass 		return node;
1833ddecfc7SSimon Glass 
1843ddecfc7SSimon Glass 	return -FDT_ERR_NOTFOUND;
1853ddecfc7SSimon Glass }
1863ddecfc7SSimon Glass 
187b5220bc6SSimon Glass int fdtdec_next_alias(const void *blob, const char *name,
188b5220bc6SSimon Glass 		enum fdt_compat_id id, int *upto)
189b5220bc6SSimon Glass {
190b5220bc6SSimon Glass #define MAX_STR_LEN 20
191b5220bc6SSimon Glass 	char str[MAX_STR_LEN + 20];
192b5220bc6SSimon Glass 	int node, err;
193b5220bc6SSimon Glass 
194b5220bc6SSimon Glass 	/* snprintf() is not available */
195b5220bc6SSimon Glass 	assert(strlen(name) < MAX_STR_LEN);
196b5220bc6SSimon Glass 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
19700878476SSimon Glass 	node = fdt_path_offset(blob, str);
198b5220bc6SSimon Glass 	if (node < 0)
199b5220bc6SSimon Glass 		return node;
200b5220bc6SSimon Glass 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
201b5220bc6SSimon Glass 	if (err < 0)
202b5220bc6SSimon Glass 		return err;
203f88fe2deSSimon Glass 	if (err)
204f88fe2deSSimon Glass 		return -FDT_ERR_NOTFOUND;
205f88fe2deSSimon Glass 	(*upto)++;
206f88fe2deSSimon Glass 	return node;
207b5220bc6SSimon Glass }
208b5220bc6SSimon Glass 
209a53f4a29SSimon Glass int fdtdec_find_aliases_for_id(const void *blob, const char *name,
210a53f4a29SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
211a53f4a29SSimon Glass {
212c6782270SSimon Glass 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
213c6782270SSimon Glass 
214c6782270SSimon Glass 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
215c6782270SSimon Glass }
216c6782270SSimon Glass 
217c6782270SSimon Glass /* TODO: Can we tighten this code up a little? */
218c6782270SSimon Glass int fdtdec_add_aliases_for_id(const void *blob, const char *name,
219c6782270SSimon Glass 			enum fdt_compat_id id, int *node_list, int maxcount)
220c6782270SSimon Glass {
221a53f4a29SSimon Glass 	int name_len = strlen(name);
222a53f4a29SSimon Glass 	int nodes[maxcount];
223a53f4a29SSimon Glass 	int num_found = 0;
224a53f4a29SSimon Glass 	int offset, node;
225a53f4a29SSimon Glass 	int alias_node;
226a53f4a29SSimon Glass 	int count;
227a53f4a29SSimon Glass 	int i, j;
228a53f4a29SSimon Glass 
229a53f4a29SSimon Glass 	/* find the alias node if present */
230a53f4a29SSimon Glass 	alias_node = fdt_path_offset(blob, "/aliases");
231a53f4a29SSimon Glass 
232a53f4a29SSimon Glass 	/*
233a53f4a29SSimon Glass 	 * start with nothing, and we can assume that the root node can't
234a53f4a29SSimon Glass 	 * match
235a53f4a29SSimon Glass 	 */
236a53f4a29SSimon Glass 	memset(nodes, '\0', sizeof(nodes));
237a53f4a29SSimon Glass 
238a53f4a29SSimon Glass 	/* First find all the compatible nodes */
239a53f4a29SSimon Glass 	for (node = count = 0; node >= 0 && count < maxcount;) {
240a53f4a29SSimon Glass 		node = fdtdec_next_compatible(blob, node, id);
241a53f4a29SSimon Glass 		if (node >= 0)
242a53f4a29SSimon Glass 			nodes[count++] = node;
243a53f4a29SSimon Glass 	}
244a53f4a29SSimon Glass 	if (node >= 0)
245a53f4a29SSimon Glass 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
246a53f4a29SSimon Glass 		       __func__, name);
247a53f4a29SSimon Glass 
248a53f4a29SSimon Glass 	/* Now find all the aliases */
249a53f4a29SSimon Glass 	for (offset = fdt_first_property_offset(blob, alias_node);
250a53f4a29SSimon Glass 			offset > 0;
251a53f4a29SSimon Glass 			offset = fdt_next_property_offset(blob, offset)) {
252a53f4a29SSimon Glass 		const struct fdt_property *prop;
253a53f4a29SSimon Glass 		const char *path;
254a53f4a29SSimon Glass 		int number;
255a53f4a29SSimon Glass 		int found;
256a53f4a29SSimon Glass 
257a53f4a29SSimon Glass 		node = 0;
258a53f4a29SSimon Glass 		prop = fdt_get_property_by_offset(blob, offset, NULL);
259a53f4a29SSimon Glass 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
260a53f4a29SSimon Glass 		if (prop->len && 0 == strncmp(path, name, name_len))
261a53f4a29SSimon Glass 			node = fdt_path_offset(blob, prop->data);
262a53f4a29SSimon Glass 		if (node <= 0)
263a53f4a29SSimon Glass 			continue;
264a53f4a29SSimon Glass 
265a53f4a29SSimon Glass 		/* Get the alias number */
266a53f4a29SSimon Glass 		number = simple_strtoul(path + name_len, NULL, 10);
267a53f4a29SSimon Glass 		if (number < 0 || number >= maxcount) {
268a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' is out of range\n",
269a53f4a29SSimon Glass 			       __func__, path);
270a53f4a29SSimon Glass 			continue;
271a53f4a29SSimon Glass 		}
272a53f4a29SSimon Glass 
273a53f4a29SSimon Glass 		/* Make sure the node we found is actually in our list! */
274a53f4a29SSimon Glass 		found = -1;
275a53f4a29SSimon Glass 		for (j = 0; j < count; j++)
276a53f4a29SSimon Glass 			if (nodes[j] == node) {
277a53f4a29SSimon Glass 				found = j;
278a53f4a29SSimon Glass 				break;
279a53f4a29SSimon Glass 			}
280a53f4a29SSimon Glass 
281a53f4a29SSimon Glass 		if (found == -1) {
282a53f4a29SSimon Glass 			debug("%s: warning: alias '%s' points to a node "
283a53f4a29SSimon Glass 				"'%s' that is missing or is not compatible "
284a53f4a29SSimon Glass 				" with '%s'\n", __func__, path,
285a53f4a29SSimon Glass 				fdt_get_name(blob, node, NULL),
286a53f4a29SSimon Glass 			       compat_names[id]);
287a53f4a29SSimon Glass 			continue;
288a53f4a29SSimon Glass 		}
289a53f4a29SSimon Glass 
290a53f4a29SSimon Glass 		/*
291a53f4a29SSimon Glass 		 * Add this node to our list in the right place, and mark
292a53f4a29SSimon Glass 		 * it as done.
293a53f4a29SSimon Glass 		 */
294a53f4a29SSimon Glass 		if (fdtdec_get_is_enabled(blob, node)) {
295c6782270SSimon Glass 			if (node_list[number]) {
296c6782270SSimon Glass 				debug("%s: warning: alias '%s' requires that "
297c6782270SSimon Glass 				      "a node be placed in the list in a "
298c6782270SSimon Glass 				      "position which is already filled by "
299c6782270SSimon Glass 				      "node '%s'\n", __func__, path,
300c6782270SSimon Glass 				      fdt_get_name(blob, node, NULL));
301c6782270SSimon Glass 				continue;
302c6782270SSimon Glass 			}
303a53f4a29SSimon Glass 			node_list[number] = node;
304a53f4a29SSimon Glass 			if (number >= num_found)
305a53f4a29SSimon Glass 				num_found = number + 1;
306a53f4a29SSimon Glass 		}
307c6782270SSimon Glass 		nodes[found] = 0;
308a53f4a29SSimon Glass 	}
309a53f4a29SSimon Glass 
310a53f4a29SSimon Glass 	/* Add any nodes not mentioned by an alias */
311a53f4a29SSimon Glass 	for (i = j = 0; i < maxcount; i++) {
312a53f4a29SSimon Glass 		if (!node_list[i]) {
313a53f4a29SSimon Glass 			for (; j < maxcount; j++)
314a53f4a29SSimon Glass 				if (nodes[j] &&
315a53f4a29SSimon Glass 					fdtdec_get_is_enabled(blob, nodes[j]))
316a53f4a29SSimon Glass 					break;
317a53f4a29SSimon Glass 
318a53f4a29SSimon Glass 			/* Have we run out of nodes to add? */
319a53f4a29SSimon Glass 			if (j == maxcount)
320a53f4a29SSimon Glass 				break;
321a53f4a29SSimon Glass 
322a53f4a29SSimon Glass 			assert(!node_list[i]);
323a53f4a29SSimon Glass 			node_list[i] = nodes[j++];
324a53f4a29SSimon Glass 			if (i >= num_found)
325a53f4a29SSimon Glass 				num_found = i + 1;
326a53f4a29SSimon Glass 		}
327a53f4a29SSimon Glass 	}
328a53f4a29SSimon Glass 
329a53f4a29SSimon Glass 	return num_found;
330a53f4a29SSimon Glass }
331a53f4a29SSimon Glass 
3329a263e55SSimon Glass int fdtdec_check_fdt(void)
3339a263e55SSimon Glass {
3349a263e55SSimon Glass 	/*
3359a263e55SSimon Glass 	 * We must have an FDT, but we cannot panic() yet since the console
3369a263e55SSimon Glass 	 * is not ready. So for now, just assert(). Boards which need an early
3379a263e55SSimon Glass 	 * FDT (prior to console ready) will need to make their own
3389a263e55SSimon Glass 	 * arrangements and do their own checks.
3399a263e55SSimon Glass 	 */
3409a263e55SSimon Glass 	assert(!fdtdec_prepare_fdt());
3419a263e55SSimon Glass 	return 0;
3429a263e55SSimon Glass }
3439a263e55SSimon Glass 
344b5220bc6SSimon Glass /*
345b5220bc6SSimon Glass  * This function is a little odd in that it accesses global data. At some
346b5220bc6SSimon Glass  * point if the architecture board.c files merge this will make more sense.
347b5220bc6SSimon Glass  * Even now, it is common code.
348b5220bc6SSimon Glass  */
3499a263e55SSimon Glass int fdtdec_prepare_fdt(void)
350b5220bc6SSimon Glass {
351c309c2daSSimon Glass 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
352c309c2daSSimon Glass 	    fdt_check_header(gd->fdt_blob)) {
3539a263e55SSimon Glass 		printf("No valid FDT found - please append one to U-Boot "
3549a263e55SSimon Glass 			"binary, use u-boot-dtb.bin or define "
355a733b06bSSimon Glass 			"CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
3569a263e55SSimon Glass 		return -1;
3579a263e55SSimon Glass 	}
358b5220bc6SSimon Glass 	return 0;
359b5220bc6SSimon Glass }
360d17da655SSimon Glass 
361d17da655SSimon Glass int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
362d17da655SSimon Glass {
363d17da655SSimon Glass 	const u32 *phandle;
364d17da655SSimon Glass 	int lookup;
365d17da655SSimon Glass 
3661cb2323bSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
367d17da655SSimon Glass 	phandle = fdt_getprop(blob, node, prop_name, NULL);
368d17da655SSimon Glass 	if (!phandle)
369d17da655SSimon Glass 		return -FDT_ERR_NOTFOUND;
370d17da655SSimon Glass 
371d17da655SSimon Glass 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
372d17da655SSimon Glass 	return lookup;
373d17da655SSimon Glass }
374d17da655SSimon Glass 
375d17da655SSimon Glass /**
376d17da655SSimon Glass  * Look up a property in a node and check that it has a minimum length.
377d17da655SSimon Glass  *
378d17da655SSimon Glass  * @param blob		FDT blob
379d17da655SSimon Glass  * @param node		node to examine
380d17da655SSimon Glass  * @param prop_name	name of property to find
381d17da655SSimon Glass  * @param min_len	minimum property length in bytes
382d17da655SSimon Glass  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
383d17da655SSimon Glass 			found, or -FDT_ERR_BADLAYOUT if not enough data
384d17da655SSimon Glass  * @return pointer to cell, which is only valid if err == 0
385d17da655SSimon Glass  */
386d17da655SSimon Glass static const void *get_prop_check_min_len(const void *blob, int node,
387d17da655SSimon Glass 		const char *prop_name, int min_len, int *err)
388d17da655SSimon Glass {
389d17da655SSimon Glass 	const void *cell;
390d17da655SSimon Glass 	int len;
391d17da655SSimon Glass 
392d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
393d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
394d17da655SSimon Glass 	if (!cell)
395d17da655SSimon Glass 		*err = -FDT_ERR_NOTFOUND;
396d17da655SSimon Glass 	else if (len < min_len)
397d17da655SSimon Glass 		*err = -FDT_ERR_BADLAYOUT;
398d17da655SSimon Glass 	else
399d17da655SSimon Glass 		*err = 0;
400d17da655SSimon Glass 	return cell;
401d17da655SSimon Glass }
402d17da655SSimon Glass 
403d17da655SSimon Glass int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
404d17da655SSimon Glass 		u32 *array, int count)
405d17da655SSimon Glass {
406d17da655SSimon Glass 	const u32 *cell;
407d17da655SSimon Glass 	int i, err = 0;
408d17da655SSimon Glass 
409d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
410d17da655SSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
411d17da655SSimon Glass 				      sizeof(u32) * count, &err);
412d17da655SSimon Glass 	if (!err) {
413d17da655SSimon Glass 		for (i = 0; i < count; i++)
414d17da655SSimon Glass 			array[i] = fdt32_to_cpu(cell[i]);
415d17da655SSimon Glass 	}
416d17da655SSimon Glass 	return err;
417d17da655SSimon Glass }
418d17da655SSimon Glass 
41996875e7dSSimon Glass const u32 *fdtdec_locate_array(const void *blob, int node,
42096875e7dSSimon Glass 			       const char *prop_name, int count)
42196875e7dSSimon Glass {
42296875e7dSSimon Glass 	const u32 *cell;
42396875e7dSSimon Glass 	int err;
42496875e7dSSimon Glass 
42596875e7dSSimon Glass 	cell = get_prop_check_min_len(blob, node, prop_name,
42696875e7dSSimon Glass 				      sizeof(u32) * count, &err);
42796875e7dSSimon Glass 	return err ? NULL : cell;
42896875e7dSSimon Glass }
42996875e7dSSimon Glass 
430d17da655SSimon Glass int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
431d17da655SSimon Glass {
432d17da655SSimon Glass 	const s32 *cell;
433d17da655SSimon Glass 	int len;
434d17da655SSimon Glass 
435d17da655SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
436d17da655SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
437d17da655SSimon Glass 	return cell != NULL;
438d17da655SSimon Glass }
439ed3ee5cdSSimon Glass 
440ed3ee5cdSSimon Glass /**
441ed3ee5cdSSimon Glass  * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
442ed3ee5cdSSimon Glass  * terminating item.
443ed3ee5cdSSimon Glass  *
444ed3ee5cdSSimon Glass  * @param blob		FDT blob to use
445ed3ee5cdSSimon Glass  * @param node		Node to look at
446ed3ee5cdSSimon Glass  * @param prop_name	Node property name
447ed3ee5cdSSimon Glass  * @param gpio		Array of gpio elements to fill from FDT. This will be
448ed3ee5cdSSimon Glass  *			untouched if either 0 or an error is returned
449ed3ee5cdSSimon Glass  * @param max_count	Maximum number of elements allowed
450ed3ee5cdSSimon Glass  * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
451ed3ee5cdSSimon Glass  * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
452ed3ee5cdSSimon Glass  */
4535921f6a2SAbhilash Kesavan int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
4545921f6a2SAbhilash Kesavan 		struct fdt_gpio_state *gpio, int max_count)
455ed3ee5cdSSimon Glass {
456ed3ee5cdSSimon Glass 	const struct fdt_property *prop;
457ed3ee5cdSSimon Glass 	const u32 *cell;
458ed3ee5cdSSimon Glass 	const char *name;
459ed3ee5cdSSimon Glass 	int len, i;
460ed3ee5cdSSimon Glass 
461ed3ee5cdSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
462ed3ee5cdSSimon Glass 	assert(max_count > 0);
463ed3ee5cdSSimon Glass 	prop = fdt_get_property(blob, node, prop_name, &len);
464ed3ee5cdSSimon Glass 	if (!prop) {
4651cb2323bSSimon Glass 		debug("%s: property '%s' missing\n", __func__, prop_name);
466ed3ee5cdSSimon Glass 		return -FDT_ERR_NOTFOUND;
467ed3ee5cdSSimon Glass 	}
468ed3ee5cdSSimon Glass 
469ed3ee5cdSSimon Glass 	/* We will use the name to tag the GPIO */
470ed3ee5cdSSimon Glass 	name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
471ed3ee5cdSSimon Glass 	cell = (u32 *)prop->data;
472ed3ee5cdSSimon Glass 	len /= sizeof(u32) * 3;		/* 3 cells per GPIO record */
473ed3ee5cdSSimon Glass 	if (len > max_count) {
4741cb2323bSSimon Glass 		debug(" %s: too many GPIOs / cells for "
475ed3ee5cdSSimon Glass 			"property '%s'\n", __func__, prop_name);
476ed3ee5cdSSimon Glass 		return -FDT_ERR_BADLAYOUT;
477ed3ee5cdSSimon Glass 	}
478ed3ee5cdSSimon Glass 
479ed3ee5cdSSimon Glass 	/* Read out the GPIO data from the cells */
480ed3ee5cdSSimon Glass 	for (i = 0; i < len; i++, cell += 3) {
481ed3ee5cdSSimon Glass 		gpio[i].gpio = fdt32_to_cpu(cell[1]);
482ed3ee5cdSSimon Glass 		gpio[i].flags = fdt32_to_cpu(cell[2]);
483ed3ee5cdSSimon Glass 		gpio[i].name = name;
484ed3ee5cdSSimon Glass 	}
485ed3ee5cdSSimon Glass 
486ed3ee5cdSSimon Glass 	return len;
487ed3ee5cdSSimon Glass }
488ed3ee5cdSSimon Glass 
489ed3ee5cdSSimon Glass int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
490ed3ee5cdSSimon Glass 		struct fdt_gpio_state *gpio)
491ed3ee5cdSSimon Glass {
492ed3ee5cdSSimon Glass 	int err;
493ed3ee5cdSSimon Glass 
494ed3ee5cdSSimon Glass 	debug("%s: %s\n", __func__, prop_name);
495ed3ee5cdSSimon Glass 	gpio->gpio = FDT_GPIO_NONE;
496ed3ee5cdSSimon Glass 	gpio->name = NULL;
497ed3ee5cdSSimon Glass 	err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
498ed3ee5cdSSimon Glass 	return err == 1 ? 0 : err;
499ed3ee5cdSSimon Glass }
500ed3ee5cdSSimon Glass 
501202ff753SSean Paul int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
502202ff753SSean Paul {
503202ff753SSean Paul 	int val;
504202ff753SSean Paul 
505202ff753SSean Paul 	if (!fdt_gpio_isvalid(gpio))
506202ff753SSean Paul 		return -1;
507202ff753SSean Paul 
508202ff753SSean Paul 	val = gpio_get_value(gpio->gpio);
509202ff753SSean Paul 	return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
510202ff753SSean Paul }
511202ff753SSean Paul 
512202ff753SSean Paul int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
513202ff753SSean Paul {
514202ff753SSean Paul 	if (!fdt_gpio_isvalid(gpio))
515202ff753SSean Paul 		return -1;
516202ff753SSean Paul 
517202ff753SSean Paul 	val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
518202ff753SSean Paul 	return gpio_set_value(gpio->gpio, val);
519202ff753SSean Paul }
520202ff753SSean Paul 
521ed3ee5cdSSimon Glass int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
522ed3ee5cdSSimon Glass {
523ed3ee5cdSSimon Glass 	/*
524ed3ee5cdSSimon Glass 	 * Return success if there is no GPIO defined. This is used for
525ed3ee5cdSSimon Glass 	 * optional GPIOs)
526ed3ee5cdSSimon Glass 	 */
527ed3ee5cdSSimon Glass 	if (!fdt_gpio_isvalid(gpio))
528ed3ee5cdSSimon Glass 		return 0;
529ed3ee5cdSSimon Glass 
530ed3ee5cdSSimon Glass 	if (gpio_request(gpio->gpio, gpio->name))
531ed3ee5cdSSimon Glass 		return -1;
532ed3ee5cdSSimon Glass 	return 0;
533ed3ee5cdSSimon Glass }
534bed4d892SAnton Staff 
535bed4d892SAnton Staff int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
536bed4d892SAnton Staff 		u8 *array, int count)
537bed4d892SAnton Staff {
538bed4d892SAnton Staff 	const u8 *cell;
539bed4d892SAnton Staff 	int err;
540bed4d892SAnton Staff 
541bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
542bed4d892SAnton Staff 	if (!err)
543bed4d892SAnton Staff 		memcpy(array, cell, count);
544bed4d892SAnton Staff 	return err;
545bed4d892SAnton Staff }
546bed4d892SAnton Staff 
547bed4d892SAnton Staff const u8 *fdtdec_locate_byte_array(const void *blob, int node,
548bed4d892SAnton Staff 			     const char *prop_name, int count)
549bed4d892SAnton Staff {
550bed4d892SAnton Staff 	const u8 *cell;
551bed4d892SAnton Staff 	int err;
552bed4d892SAnton Staff 
553bed4d892SAnton Staff 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
554bed4d892SAnton Staff 	if (err)
555bed4d892SAnton Staff 		return NULL;
556bed4d892SAnton Staff 	return cell;
557bed4d892SAnton Staff }
55809258f1eSAbhilash Kesavan 
55909258f1eSAbhilash Kesavan int fdtdec_get_config_int(const void *blob, const char *prop_name,
56009258f1eSAbhilash Kesavan 		int default_val)
56109258f1eSAbhilash Kesavan {
56209258f1eSAbhilash Kesavan 	int config_node;
56309258f1eSAbhilash Kesavan 
56409258f1eSAbhilash Kesavan 	debug("%s: %s\n", __func__, prop_name);
56509258f1eSAbhilash Kesavan 	config_node = fdt_path_offset(blob, "/config");
56609258f1eSAbhilash Kesavan 	if (config_node < 0)
56709258f1eSAbhilash Kesavan 		return default_val;
56809258f1eSAbhilash Kesavan 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
56909258f1eSAbhilash Kesavan }
570332ab0d5SSimon Glass 
57179289c0bSGabe Black int fdtdec_get_config_bool(const void *blob, const char *prop_name)
57279289c0bSGabe Black {
57379289c0bSGabe Black 	int config_node;
57479289c0bSGabe Black 	const void *prop;
57579289c0bSGabe Black 
57679289c0bSGabe Black 	debug("%s: %s\n", __func__, prop_name);
57779289c0bSGabe Black 	config_node = fdt_path_offset(blob, "/config");
57879289c0bSGabe Black 	if (config_node < 0)
57979289c0bSGabe Black 		return 0;
58079289c0bSGabe Black 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
58179289c0bSGabe Black 
58279289c0bSGabe Black 	return prop != NULL;
58379289c0bSGabe Black }
58479289c0bSGabe Black 
585332ab0d5SSimon Glass char *fdtdec_get_config_string(const void *blob, const char *prop_name)
586332ab0d5SSimon Glass {
587332ab0d5SSimon Glass 	const char *nodep;
588332ab0d5SSimon Glass 	int nodeoffset;
589332ab0d5SSimon Glass 	int len;
590332ab0d5SSimon Glass 
591332ab0d5SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
592332ab0d5SSimon Glass 	nodeoffset = fdt_path_offset(blob, "/config");
593332ab0d5SSimon Glass 	if (nodeoffset < 0)
594332ab0d5SSimon Glass 		return NULL;
595332ab0d5SSimon Glass 
596332ab0d5SSimon Glass 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
597332ab0d5SSimon Glass 	if (!nodep)
598332ab0d5SSimon Glass 		return NULL;
599332ab0d5SSimon Glass 
600332ab0d5SSimon Glass 	return (char *)nodep;
601332ab0d5SSimon Glass }
602f20c4619SSimon Glass 
603f20c4619SSimon Glass int fdtdec_decode_region(const void *blob, int node,
604f20c4619SSimon Glass 		const char *prop_name, void **ptrp, size_t *size)
605f20c4619SSimon Glass {
606f20c4619SSimon Glass 	const fdt_addr_t *cell;
607f20c4619SSimon Glass 	int len;
608f20c4619SSimon Glass 
609f20c4619SSimon Glass 	debug("%s: %s\n", __func__, prop_name);
610f20c4619SSimon Glass 	cell = fdt_getprop(blob, node, prop_name, &len);
611f20c4619SSimon Glass 	if (!cell || (len != sizeof(fdt_addr_t) * 2))
612f20c4619SSimon Glass 		return -1;
613f20c4619SSimon Glass 
614f20c4619SSimon Glass 	*ptrp = (void *)fdt_addr_to_cpu(*cell);
615f20c4619SSimon Glass 	*size = fdt_size_to_cpu(cell[1]);
616f20c4619SSimon Glass 	debug("%s: size=%zx\n", __func__, *size);
617f20c4619SSimon Glass 	return 0;
618f20c4619SSimon Glass }
619