xref: /openbmc/u-boot/lib/fdtdec.c (revision 3765b3e7)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * SPDX-License-Identifier:	GPL-2.0+
4  */
5 
6 #include <common.h>
7 #include <serial.h>
8 #include <libfdt.h>
9 #include <fdtdec.h>
10 
11 #include <asm/gpio.h>
12 
13 DECLARE_GLOBAL_DATA_PTR;
14 
15 /*
16  * Here are the type we know about. One day we might allow drivers to
17  * register. For now we just put them here. The COMPAT macro allows us to
18  * turn this into a sparse list later, and keeps the ID with the name.
19  */
20 #define COMPAT(id, name) name
21 static const char * const compat_names[COMPAT_COUNT] = {
22 	COMPAT(UNKNOWN, "<none>"),
23 	COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
24 	COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
25 	COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
26 	COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
27 	COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
28 	COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
29 	COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
30 	COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
31 	COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
32 	COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
33 	COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
34 	COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
35 	COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
36 	COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
37 	COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
38 	COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
39 	COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
40 	COMPAT(SMSC_LAN9215, "smsc,lan9215"),
41 	COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
42 	COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
43 	COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
44 	COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
45 	COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
46 	COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
47 	COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
48 	COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
49 	COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
50 	COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
51 	COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
52 	COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
53 	COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"),
54 	COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
55 	COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
56 	COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
57 	COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
58 	COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
59 	COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
60 	COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
61 };
62 
63 const char *fdtdec_get_compatible(enum fdt_compat_id id)
64 {
65 	/* We allow reading of the 'unknown' ID for testing purposes */
66 	assert(id >= 0 && id < COMPAT_COUNT);
67 	return compat_names[id];
68 }
69 
70 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
71 		const char *prop_name, fdt_size_t *sizep)
72 {
73 	const fdt_addr_t *cell;
74 	int len;
75 
76 	debug("%s: %s: ", __func__, prop_name);
77 	cell = fdt_getprop(blob, node, prop_name, &len);
78 	if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
79 		     len == sizeof(fdt_addr_t) * 2)) {
80 		fdt_addr_t addr = fdt_addr_to_cpu(*cell);
81 		if (sizep) {
82 			const fdt_size_t *size;
83 
84 			size = (fdt_size_t *)((char *)cell +
85 					sizeof(fdt_addr_t));
86 			*sizep = fdt_size_to_cpu(*size);
87 			debug("addr=%p, size=%p\n", (void *)addr,
88 			      (void *)*sizep);
89 		} else {
90 			debug("%p\n", (void *)addr);
91 		}
92 		return addr;
93 	}
94 	debug("(not found)\n");
95 	return FDT_ADDR_T_NONE;
96 }
97 
98 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
99 		const char *prop_name)
100 {
101 	return fdtdec_get_addr_size(blob, node, prop_name, NULL);
102 }
103 
104 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
105 		s32 default_val)
106 {
107 	const s32 *cell;
108 	int len;
109 
110 	debug("%s: %s: ", __func__, prop_name);
111 	cell = fdt_getprop(blob, node, prop_name, &len);
112 	if (cell && len >= sizeof(s32)) {
113 		s32 val = fdt32_to_cpu(cell[0]);
114 
115 		debug("%#x (%d)\n", val, val);
116 		return val;
117 	}
118 	debug("(not found)\n");
119 	return default_val;
120 }
121 
122 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
123 		uint64_t default_val)
124 {
125 	const uint64_t *cell64;
126 	int length;
127 
128 	cell64 = fdt_getprop(blob, node, prop_name, &length);
129 	if (!cell64 || length < sizeof(*cell64))
130 		return default_val;
131 
132 	return fdt64_to_cpu(*cell64);
133 }
134 
135 int fdtdec_get_is_enabled(const void *blob, int node)
136 {
137 	const char *cell;
138 
139 	/*
140 	 * It should say "okay", so only allow that. Some fdts use "ok" but
141 	 * this is a bug. Please fix your device tree source file. See here
142 	 * for discussion:
143 	 *
144 	 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
145 	 */
146 	cell = fdt_getprop(blob, node, "status", NULL);
147 	if (cell)
148 		return 0 == strcmp(cell, "okay");
149 	return 1;
150 }
151 
152 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
153 {
154 	enum fdt_compat_id id;
155 
156 	/* Search our drivers */
157 	for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
158 		if (0 == fdt_node_check_compatible(blob, node,
159 				compat_names[id]))
160 			return id;
161 	return COMPAT_UNKNOWN;
162 }
163 
164 int fdtdec_next_compatible(const void *blob, int node,
165 		enum fdt_compat_id id)
166 {
167 	return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
168 }
169 
170 int fdtdec_next_compatible_subnode(const void *blob, int node,
171 		enum fdt_compat_id id, int *depthp)
172 {
173 	do {
174 		node = fdt_next_node(blob, node, depthp);
175 	} while (*depthp > 1);
176 
177 	/* If this is a direct subnode, and compatible, return it */
178 	if (*depthp == 1 && 0 == fdt_node_check_compatible(
179 						blob, node, compat_names[id]))
180 		return node;
181 
182 	return -FDT_ERR_NOTFOUND;
183 }
184 
185 int fdtdec_next_alias(const void *blob, const char *name,
186 		enum fdt_compat_id id, int *upto)
187 {
188 #define MAX_STR_LEN 20
189 	char str[MAX_STR_LEN + 20];
190 	int node, err;
191 
192 	/* snprintf() is not available */
193 	assert(strlen(name) < MAX_STR_LEN);
194 	sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
195 	node = fdt_path_offset(blob, str);
196 	if (node < 0)
197 		return node;
198 	err = fdt_node_check_compatible(blob, node, compat_names[id]);
199 	if (err < 0)
200 		return err;
201 	if (err)
202 		return -FDT_ERR_NOTFOUND;
203 	(*upto)++;
204 	return node;
205 }
206 
207 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
208 			enum fdt_compat_id id, int *node_list, int maxcount)
209 {
210 	memset(node_list, '\0', sizeof(*node_list) * maxcount);
211 
212 	return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
213 }
214 
215 /* TODO: Can we tighten this code up a little? */
216 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
217 			enum fdt_compat_id id, int *node_list, int maxcount)
218 {
219 	int name_len = strlen(name);
220 	int nodes[maxcount];
221 	int num_found = 0;
222 	int offset, node;
223 	int alias_node;
224 	int count;
225 	int i, j;
226 
227 	/* find the alias node if present */
228 	alias_node = fdt_path_offset(blob, "/aliases");
229 
230 	/*
231 	 * start with nothing, and we can assume that the root node can't
232 	 * match
233 	 */
234 	memset(nodes, '\0', sizeof(nodes));
235 
236 	/* First find all the compatible nodes */
237 	for (node = count = 0; node >= 0 && count < maxcount;) {
238 		node = fdtdec_next_compatible(blob, node, id);
239 		if (node >= 0)
240 			nodes[count++] = node;
241 	}
242 	if (node >= 0)
243 		debug("%s: warning: maxcount exceeded with alias '%s'\n",
244 		       __func__, name);
245 
246 	/* Now find all the aliases */
247 	for (offset = fdt_first_property_offset(blob, alias_node);
248 			offset > 0;
249 			offset = fdt_next_property_offset(blob, offset)) {
250 		const struct fdt_property *prop;
251 		const char *path;
252 		int number;
253 		int found;
254 
255 		node = 0;
256 		prop = fdt_get_property_by_offset(blob, offset, NULL);
257 		path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
258 		if (prop->len && 0 == strncmp(path, name, name_len))
259 			node = fdt_path_offset(blob, prop->data);
260 		if (node <= 0)
261 			continue;
262 
263 		/* Get the alias number */
264 		number = simple_strtoul(path + name_len, NULL, 10);
265 		if (number < 0 || number >= maxcount) {
266 			debug("%s: warning: alias '%s' is out of range\n",
267 			       __func__, path);
268 			continue;
269 		}
270 
271 		/* Make sure the node we found is actually in our list! */
272 		found = -1;
273 		for (j = 0; j < count; j++)
274 			if (nodes[j] == node) {
275 				found = j;
276 				break;
277 			}
278 
279 		if (found == -1) {
280 			debug("%s: warning: alias '%s' points to a node "
281 				"'%s' that is missing or is not compatible "
282 				" with '%s'\n", __func__, path,
283 				fdt_get_name(blob, node, NULL),
284 			       compat_names[id]);
285 			continue;
286 		}
287 
288 		/*
289 		 * Add this node to our list in the right place, and mark
290 		 * it as done.
291 		 */
292 		if (fdtdec_get_is_enabled(blob, node)) {
293 			if (node_list[number]) {
294 				debug("%s: warning: alias '%s' requires that "
295 				      "a node be placed in the list in a "
296 				      "position which is already filled by "
297 				      "node '%s'\n", __func__, path,
298 				      fdt_get_name(blob, node, NULL));
299 				continue;
300 			}
301 			node_list[number] = node;
302 			if (number >= num_found)
303 				num_found = number + 1;
304 		}
305 		nodes[found] = 0;
306 	}
307 
308 	/* Add any nodes not mentioned by an alias */
309 	for (i = j = 0; i < maxcount; i++) {
310 		if (!node_list[i]) {
311 			for (; j < maxcount; j++)
312 				if (nodes[j] &&
313 					fdtdec_get_is_enabled(blob, nodes[j]))
314 					break;
315 
316 			/* Have we run out of nodes to add? */
317 			if (j == maxcount)
318 				break;
319 
320 			assert(!node_list[i]);
321 			node_list[i] = nodes[j++];
322 			if (i >= num_found)
323 				num_found = i + 1;
324 		}
325 	}
326 
327 	return num_found;
328 }
329 
330 int fdtdec_check_fdt(void)
331 {
332 	/*
333 	 * We must have an FDT, but we cannot panic() yet since the console
334 	 * is not ready. So for now, just assert(). Boards which need an early
335 	 * FDT (prior to console ready) will need to make their own
336 	 * arrangements and do their own checks.
337 	 */
338 	assert(!fdtdec_prepare_fdt());
339 	return 0;
340 }
341 
342 /*
343  * This function is a little odd in that it accesses global data. At some
344  * point if the architecture board.c files merge this will make more sense.
345  * Even now, it is common code.
346  */
347 int fdtdec_prepare_fdt(void)
348 {
349 	if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
350 	    fdt_check_header(gd->fdt_blob)) {
351 		printf("No valid FDT found - please append one to U-Boot "
352 			"binary, use u-boot-dtb.bin or define "
353 			"CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
354 		return -1;
355 	}
356 	return 0;
357 }
358 
359 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
360 {
361 	const u32 *phandle;
362 	int lookup;
363 
364 	debug("%s: %s\n", __func__, prop_name);
365 	phandle = fdt_getprop(blob, node, prop_name, NULL);
366 	if (!phandle)
367 		return -FDT_ERR_NOTFOUND;
368 
369 	lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
370 	return lookup;
371 }
372 
373 /**
374  * Look up a property in a node and check that it has a minimum length.
375  *
376  * @param blob		FDT blob
377  * @param node		node to examine
378  * @param prop_name	name of property to find
379  * @param min_len	minimum property length in bytes
380  * @param err		0 if ok, or -FDT_ERR_NOTFOUND if the property is not
381 			found, or -FDT_ERR_BADLAYOUT if not enough data
382  * @return pointer to cell, which is only valid if err == 0
383  */
384 static const void *get_prop_check_min_len(const void *blob, int node,
385 		const char *prop_name, int min_len, int *err)
386 {
387 	const void *cell;
388 	int len;
389 
390 	debug("%s: %s\n", __func__, prop_name);
391 	cell = fdt_getprop(blob, node, prop_name, &len);
392 	if (!cell)
393 		*err = -FDT_ERR_NOTFOUND;
394 	else if (len < min_len)
395 		*err = -FDT_ERR_BADLAYOUT;
396 	else
397 		*err = 0;
398 	return cell;
399 }
400 
401 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
402 		u32 *array, int count)
403 {
404 	const u32 *cell;
405 	int i, err = 0;
406 
407 	debug("%s: %s\n", __func__, prop_name);
408 	cell = get_prop_check_min_len(blob, node, prop_name,
409 				      sizeof(u32) * count, &err);
410 	if (!err) {
411 		for (i = 0; i < count; i++)
412 			array[i] = fdt32_to_cpu(cell[i]);
413 	}
414 	return err;
415 }
416 
417 const u32 *fdtdec_locate_array(const void *blob, int node,
418 			       const char *prop_name, int count)
419 {
420 	const u32 *cell;
421 	int err;
422 
423 	cell = get_prop_check_min_len(blob, node, prop_name,
424 				      sizeof(u32) * count, &err);
425 	return err ? NULL : cell;
426 }
427 
428 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
429 {
430 	const s32 *cell;
431 	int len;
432 
433 	debug("%s: %s\n", __func__, prop_name);
434 	cell = fdt_getprop(blob, node, prop_name, &len);
435 	return cell != NULL;
436 }
437 
438 /**
439  * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
440  * terminating item.
441  *
442  * @param blob		FDT blob to use
443  * @param node		Node to look at
444  * @param prop_name	Node property name
445  * @param gpio		Array of gpio elements to fill from FDT. This will be
446  *			untouched if either 0 or an error is returned
447  * @param max_count	Maximum number of elements allowed
448  * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
449  * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
450  */
451 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
452 		struct fdt_gpio_state *gpio, int max_count)
453 {
454 	const struct fdt_property *prop;
455 	const u32 *cell;
456 	const char *name;
457 	int len, i;
458 
459 	debug("%s: %s\n", __func__, prop_name);
460 	assert(max_count > 0);
461 	prop = fdt_get_property(blob, node, prop_name, &len);
462 	if (!prop) {
463 		debug("%s: property '%s' missing\n", __func__, prop_name);
464 		return -FDT_ERR_NOTFOUND;
465 	}
466 
467 	/* We will use the name to tag the GPIO */
468 	name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
469 	cell = (u32 *)prop->data;
470 	len /= sizeof(u32) * 3;		/* 3 cells per GPIO record */
471 	if (len > max_count) {
472 		debug(" %s: too many GPIOs / cells for "
473 			"property '%s'\n", __func__, prop_name);
474 		return -FDT_ERR_BADLAYOUT;
475 	}
476 
477 	/* Read out the GPIO data from the cells */
478 	for (i = 0; i < len; i++, cell += 3) {
479 		gpio[i].gpio = fdt32_to_cpu(cell[1]);
480 		gpio[i].flags = fdt32_to_cpu(cell[2]);
481 		gpio[i].name = name;
482 	}
483 
484 	return len;
485 }
486 
487 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
488 		struct fdt_gpio_state *gpio)
489 {
490 	int err;
491 
492 	debug("%s: %s\n", __func__, prop_name);
493 	gpio->gpio = FDT_GPIO_NONE;
494 	gpio->name = NULL;
495 	err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
496 	return err == 1 ? 0 : err;
497 }
498 
499 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
500 {
501 	int val;
502 
503 	if (!fdt_gpio_isvalid(gpio))
504 		return -1;
505 
506 	val = gpio_get_value(gpio->gpio);
507 	return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
508 }
509 
510 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
511 {
512 	if (!fdt_gpio_isvalid(gpio))
513 		return -1;
514 
515 	val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
516 	return gpio_set_value(gpio->gpio, val);
517 }
518 
519 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
520 {
521 	/*
522 	 * Return success if there is no GPIO defined. This is used for
523 	 * optional GPIOs)
524 	 */
525 	if (!fdt_gpio_isvalid(gpio))
526 		return 0;
527 
528 	if (gpio_request(gpio->gpio, gpio->name))
529 		return -1;
530 	return 0;
531 }
532 
533 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
534 		u8 *array, int count)
535 {
536 	const u8 *cell;
537 	int err;
538 
539 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
540 	if (!err)
541 		memcpy(array, cell, count);
542 	return err;
543 }
544 
545 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
546 			     const char *prop_name, int count)
547 {
548 	const u8 *cell;
549 	int err;
550 
551 	cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
552 	if (err)
553 		return NULL;
554 	return cell;
555 }
556 
557 int fdtdec_get_config_int(const void *blob, const char *prop_name,
558 		int default_val)
559 {
560 	int config_node;
561 
562 	debug("%s: %s\n", __func__, prop_name);
563 	config_node = fdt_path_offset(blob, "/config");
564 	if (config_node < 0)
565 		return default_val;
566 	return fdtdec_get_int(blob, config_node, prop_name, default_val);
567 }
568 
569 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
570 {
571 	int config_node;
572 	const void *prop;
573 
574 	debug("%s: %s\n", __func__, prop_name);
575 	config_node = fdt_path_offset(blob, "/config");
576 	if (config_node < 0)
577 		return 0;
578 	prop = fdt_get_property(blob, config_node, prop_name, NULL);
579 
580 	return prop != NULL;
581 }
582 
583 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
584 {
585 	const char *nodep;
586 	int nodeoffset;
587 	int len;
588 
589 	debug("%s: %s\n", __func__, prop_name);
590 	nodeoffset = fdt_path_offset(blob, "/config");
591 	if (nodeoffset < 0)
592 		return NULL;
593 
594 	nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
595 	if (!nodep)
596 		return NULL;
597 
598 	return (char *)nodep;
599 }
600 
601 int fdtdec_decode_region(const void *blob, int node,
602 		const char *prop_name, void **ptrp, size_t *size)
603 {
604 	const fdt_addr_t *cell;
605 	int len;
606 
607 	debug("%s: %s\n", __func__, prop_name);
608 	cell = fdt_getprop(blob, node, prop_name, &len);
609 	if (!cell || (len != sizeof(fdt_addr_t) * 2))
610 		return -1;
611 
612 	*ptrp = (void *)fdt_addr_to_cpu(*cell);
613 	*size = fdt_size_to_cpu(cell[1]);
614 	debug("%s: size=%zx\n", __func__, *size);
615 	return 0;
616 }
617