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_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"),
66 const char *fdtdec_get_compatible(enum fdt_compat_id id)
68 /* We allow reading of the 'unknown' ID for testing purposes */
69 assert(id >= 0 && id < COMPAT_COUNT);
70 return compat_names[id];
73 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
74 const char *prop_name)
76 const fdt_addr_t *cell;
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);
85 debug("%p\n", (void *)addr);
88 debug("(not found)\n");
89 return FDT_ADDR_T_NONE;
92 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
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]);
103 debug("%#x (%d)\n", val, val);
106 debug("(not found)\n");
110 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
111 uint64_t default_val)
113 const uint64_t *cell64;
116 cell64 = fdt_getprop(blob, node, prop_name, &length);
117 if (!cell64 || length < sizeof(*cell64))
120 return fdt64_to_cpu(*cell64);
123 int fdtdec_get_is_enabled(const void *blob, int node)
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
132 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
134 cell = fdt_getprop(blob, node, "status", NULL);
136 return 0 == strcmp(cell, "okay");
140 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
142 enum fdt_compat_id id;
144 /* Search our drivers */
145 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
146 if (0 == fdt_node_check_compatible(blob, node,
149 return COMPAT_UNKNOWN;
152 int fdtdec_next_compatible(const void *blob, int node,
153 enum fdt_compat_id id)
155 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
158 int fdtdec_next_compatible_subnode(const void *blob, int node,
159 enum fdt_compat_id id, int *depthp)
162 node = fdt_next_node(blob, node, depthp);
163 } while (*depthp > 1);
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]))
170 return -FDT_ERR_NOTFOUND;
173 int fdtdec_next_alias(const void *blob, const char *name,
174 enum fdt_compat_id id, int *upto)
176 #define MAX_STR_LEN 20
177 char str[MAX_STR_LEN + 20];
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);
186 err = fdt_node_check_compatible(blob, node, compat_names[id]);
190 return -FDT_ERR_NOTFOUND;
195 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
196 enum fdt_compat_id id, int *node_list, int maxcount)
198 memset(node_list, '\0', sizeof(*node_list) * maxcount);
200 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
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)
207 int name_len = strlen(name);
215 /* find the alias node if present */
216 alias_node = fdt_path_offset(blob, "/aliases");
219 * start with nothing, and we can assume that the root node can't
222 memset(nodes, '\0', sizeof(nodes));
224 /* First find all the compatible nodes */
225 for (node = count = 0; node >= 0 && count < maxcount;) {
226 node = fdtdec_next_compatible(blob, node, id);
228 nodes[count++] = node;
231 debug("%s: warning: maxcount exceeded with alias '%s'\n",
234 /* Now find all the aliases */
235 for (offset = fdt_first_property_offset(blob, alias_node);
237 offset = fdt_next_property_offset(blob, offset)) {
238 const struct fdt_property *prop;
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);
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",
259 /* Make sure the node we found is actually in our list! */
261 for (j = 0; j < count; j++)
262 if (nodes[j] == node) {
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),
277 * Add this node to our list in the right place, and mark
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));
289 node_list[number] = node;
290 if (number >= num_found)
291 num_found = number + 1;
296 /* Add any nodes not mentioned by an alias */
297 for (i = j = 0; i < maxcount; i++) {
299 for (; j < maxcount; j++)
301 fdtdec_get_is_enabled(blob, nodes[j]))
304 /* Have we run out of nodes to add? */
308 assert(!node_list[i]);
309 node_list[i] = nodes[j++];
318 int fdtdec_check_fdt(void)
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.
326 assert(!fdtdec_prepare_fdt());
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.
335 int fdtdec_prepare_fdt(void)
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");
346 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
351 debug("%s: %s\n", __func__, prop_name);
352 phandle = fdt_getprop(blob, node, prop_name, NULL);
354 return -FDT_ERR_NOTFOUND;
356 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
361 * Look up a property in a node and check that it has a minimum length.
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
371 static const void *get_prop_check_min_len(const void *blob, int node,
372 const char *prop_name, int min_len, int *err)
377 debug("%s: %s\n", __func__, prop_name);
378 cell = fdt_getprop(blob, node, prop_name, &len);
380 *err = -FDT_ERR_NOTFOUND;
381 else if (len < min_len)
382 *err = -FDT_ERR_BADLAYOUT;
388 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
389 u32 *array, int count)
394 debug("%s: %s\n", __func__, prop_name);
395 cell = get_prop_check_min_len(blob, node, prop_name,
396 sizeof(u32) * count, &err);
398 for (i = 0; i < count; i++)
399 array[i] = fdt32_to_cpu(cell[i]);
404 const u32 *fdtdec_locate_array(const void *blob, int node,
405 const char *prop_name, int count)
410 cell = get_prop_check_min_len(blob, node, prop_name,
411 sizeof(u32) * count, &err);
412 return err ? NULL : cell;
415 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
420 debug("%s: %s\n", __func__, prop_name);
421 cell = fdt_getprop(blob, node, prop_name, &len);
426 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
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.
438 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
439 struct fdt_gpio_state *gpio, int max_count)
441 const struct fdt_property *prop;
446 debug("%s: %s\n", __func__, prop_name);
447 assert(max_count > 0);
448 prop = fdt_get_property(blob, node, prop_name, &len);
450 debug("%s: property '%s' missing\n", __func__, prop_name);
451 return -FDT_ERR_NOTFOUND;
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;
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]);
474 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
475 struct fdt_gpio_state *gpio)
479 debug("%s: %s\n", __func__, prop_name);
480 gpio->gpio = FDT_GPIO_NONE;
482 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
483 return err == 1 ? 0 : err;
486 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
490 if (!fdt_gpio_isvalid(gpio))
493 val = gpio_get_value(gpio->gpio);
494 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
497 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
499 if (!fdt_gpio_isvalid(gpio))
502 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
503 return gpio_set_value(gpio->gpio, val);
506 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
509 * Return success if there is no GPIO defined. This is used for
512 if (!fdt_gpio_isvalid(gpio))
515 if (gpio_request(gpio->gpio, gpio->name))
520 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
521 u8 *array, int count)
526 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
528 memcpy(array, cell, count);
532 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
533 const char *prop_name, int count)
538 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
544 int fdtdec_get_config_int(const void *blob, const char *prop_name,
549 debug("%s: %s\n", __func__, prop_name);
550 config_node = fdt_path_offset(blob, "/config");
553 return fdtdec_get_int(blob, config_node, prop_name, default_val);
556 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
561 debug("%s: %s\n", __func__, prop_name);
562 config_node = fdt_path_offset(blob, "/config");
565 prop = fdt_get_property(blob, config_node, prop_name, NULL);
570 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
576 debug("%s: %s\n", __func__, prop_name);
577 nodeoffset = fdt_path_offset(blob, "/config");
581 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
585 return (char *)nodep;
588 int fdtdec_decode_region(const void *blob, int node,
589 const char *prop_name, void **ptrp, size_t *size)
591 const fdt_addr_t *cell;
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))
599 *ptrp = (void *)fdt_addr_to_cpu(*cell);
600 *size = fdt_size_to_cpu(cell[1]);
601 debug("%s: size=%zx\n", __func__, *size);