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