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