2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 DECLARE_GLOBAL_DATA_PTR;
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.
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"),
50 const char *fdtdec_get_compatible(enum fdt_compat_id id)
52 /* We allow reading of the 'unknown' ID for testing purposes */
53 assert(id >= 0 && id < COMPAT_COUNT);
54 return compat_names[id];
57 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
58 const char *prop_name)
60 const fdt_addr_t *cell;
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);
69 debug("%p\n", (void *)addr);
72 debug("(not found)\n");
73 return FDT_ADDR_T_NONE;
76 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
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]);
87 debug("%#x (%d)\n", val, val);
90 debug("(not found)\n");
94 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
97 const uint64_t *cell64;
100 cell64 = fdt_getprop(blob, node, prop_name, &length);
101 if (!cell64 || length < sizeof(*cell64))
104 return fdt64_to_cpu(*cell64);
107 int fdtdec_get_is_enabled(const void *blob, int node)
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
118 cell = fdt_getprop(blob, node, "status", NULL);
120 return 0 == strcmp(cell, "okay");
124 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
126 enum fdt_compat_id id;
128 /* Search our drivers */
129 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
130 if (0 == fdt_node_check_compatible(blob, node,
133 return COMPAT_UNKNOWN;
136 int fdtdec_next_compatible(const void *blob, int node,
137 enum fdt_compat_id id)
139 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
142 int fdtdec_next_compatible_subnode(const void *blob, int node,
143 enum fdt_compat_id id, int *depthp)
146 node = fdt_next_node(blob, node, depthp);
147 } while (*depthp > 1);
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]))
154 return -FDT_ERR_NOTFOUND;
157 int fdtdec_next_alias(const void *blob, const char *name,
158 enum fdt_compat_id id, int *upto)
160 #define MAX_STR_LEN 20
161 char str[MAX_STR_LEN + 20];
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);
170 err = fdt_node_check_compatible(blob, node, compat_names[id]);
174 return -FDT_ERR_NOTFOUND;
179 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
180 enum fdt_compat_id id, int *node_list, int maxcount)
182 memset(node_list, '\0', sizeof(*node_list) * maxcount);
184 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
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)
191 int name_len = strlen(name);
199 /* find the alias node if present */
200 alias_node = fdt_path_offset(blob, "/aliases");
203 * start with nothing, and we can assume that the root node can't
206 memset(nodes, '\0', sizeof(nodes));
208 /* First find all the compatible nodes */
209 for (node = count = 0; node >= 0 && count < maxcount;) {
210 node = fdtdec_next_compatible(blob, node, id);
212 nodes[count++] = node;
215 debug("%s: warning: maxcount exceeded with alias '%s'\n",
218 /* Now find all the aliases */
219 for (offset = fdt_first_property_offset(blob, alias_node);
221 offset = fdt_next_property_offset(blob, offset)) {
222 const struct fdt_property *prop;
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);
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",
243 /* Make sure the node we found is actually in our list! */
245 for (j = 0; j < count; j++)
246 if (nodes[j] == node) {
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),
261 * Add this node to our list in the right place, and mark
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));
273 node_list[number] = node;
274 if (number >= num_found)
275 num_found = number + 1;
280 /* Add any nodes not mentioned by an alias */
281 for (i = j = 0; i < maxcount; i++) {
283 for (; j < maxcount; j++)
285 fdtdec_get_is_enabled(blob, nodes[j]))
288 /* Have we run out of nodes to add? */
292 assert(!node_list[i]);
293 node_list[i] = nodes[j++];
302 int fdtdec_check_fdt(void)
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.
310 assert(!fdtdec_prepare_fdt());
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.
319 int fdtdec_prepare_fdt(void)
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");
330 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
335 debug("%s: %s\n", __func__, prop_name);
336 phandle = fdt_getprop(blob, node, prop_name, NULL);
338 return -FDT_ERR_NOTFOUND;
340 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
345 * Look up a property in a node and check that it has a minimum length.
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
355 static const void *get_prop_check_min_len(const void *blob, int node,
356 const char *prop_name, int min_len, int *err)
361 debug("%s: %s\n", __func__, prop_name);
362 cell = fdt_getprop(blob, node, prop_name, &len);
364 *err = -FDT_ERR_NOTFOUND;
365 else if (len < min_len)
366 *err = -FDT_ERR_BADLAYOUT;
372 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
373 u32 *array, int count)
378 debug("%s: %s\n", __func__, prop_name);
379 cell = get_prop_check_min_len(blob, node, prop_name,
380 sizeof(u32) * count, &err);
382 for (i = 0; i < count; i++)
383 array[i] = fdt32_to_cpu(cell[i]);
388 const u32 *fdtdec_locate_array(const void *blob, int node,
389 const char *prop_name, int count)
394 cell = get_prop_check_min_len(blob, node, prop_name,
395 sizeof(u32) * count, &err);
396 return err ? NULL : cell;
399 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
404 debug("%s: %s\n", __func__, prop_name);
405 cell = fdt_getprop(blob, node, prop_name, &len);
410 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
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.
422 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
423 struct fdt_gpio_state *gpio, int max_count)
425 const struct fdt_property *prop;
430 debug("%s: %s\n", __func__, prop_name);
431 assert(max_count > 0);
432 prop = fdt_get_property(blob, node, prop_name, &len);
434 debug("%s: property '%s' missing\n", __func__, prop_name);
435 return -FDT_ERR_NOTFOUND;
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;
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]);
458 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
459 struct fdt_gpio_state *gpio)
463 debug("%s: %s\n", __func__, prop_name);
464 gpio->gpio = FDT_GPIO_NONE;
466 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
467 return err == 1 ? 0 : err;
470 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
474 if (!fdt_gpio_isvalid(gpio))
477 val = gpio_get_value(gpio->gpio);
478 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
481 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
483 if (!fdt_gpio_isvalid(gpio))
486 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
487 return gpio_set_value(gpio->gpio, val);
490 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
493 * Return success if there is no GPIO defined. This is used for
496 if (!fdt_gpio_isvalid(gpio))
499 if (gpio_request(gpio->gpio, gpio->name))
504 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
505 u8 *array, int count)
510 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
512 memcpy(array, cell, count);
516 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
517 const char *prop_name, int count)
522 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
528 int fdtdec_get_config_int(const void *blob, const char *prop_name,
533 debug("%s: %s\n", __func__, prop_name);
534 config_node = fdt_path_offset(blob, "/config");
537 return fdtdec_get_int(blob, config_node, prop_name, default_val);
540 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
545 debug("%s: %s\n", __func__, prop_name);
546 config_node = fdt_path_offset(blob, "/config");
549 prop = fdt_get_property(blob, config_node, prop_name, NULL);
554 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
560 debug("%s: %s\n", __func__, prop_name);
561 nodeoffset = fdt_path_offset(blob, "/config");
565 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
569 return (char *)nodep;
572 int fdtdec_decode_region(const void *blob, int node,
573 const char *prop_name, void **ptrp, size_t *size)
575 const fdt_addr_t *cell;
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))
583 *ptrp = (void *)fdt_addr_to_cpu(*cell);
584 *size = fdt_size_to_cpu(cell[1]);
585 debug("%s: size=%zx\n", __func__, *size);