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_TEGRA30_USB, "nvidia,tegra30-ehci"),
41 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
42 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
43 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
44 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
45 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
46 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
47 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
48 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
49 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
50 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
51 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
52 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
53 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
54 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
55 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
56 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
57 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
58 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
59 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
60 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
61 COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
62 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
63 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
64 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
65 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
66 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
67 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
68 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
69 COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"),
70 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
71 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
72 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
73 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
74 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
75 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
78 const char *fdtdec_get_compatible(enum fdt_compat_id id)
80 /* We allow reading of the 'unknown' ID for testing purposes */
81 assert(id >= 0 && id < COMPAT_COUNT);
82 return compat_names[id];
85 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
86 const char *prop_name, fdt_size_t *sizep)
88 const fdt_addr_t *cell;
91 debug("%s: %s: ", __func__, prop_name);
92 cell = fdt_getprop(blob, node, prop_name, &len);
93 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
94 len == sizeof(fdt_addr_t) * 2)) {
95 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
97 const fdt_size_t *size;
99 size = (fdt_size_t *)((char *)cell +
101 *sizep = fdt_size_to_cpu(*size);
102 debug("addr=%p, size=%p\n", (void *)addr,
105 debug("%p\n", (void *)addr);
109 debug("(not found)\n");
110 return FDT_ADDR_T_NONE;
113 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
114 const char *prop_name)
116 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
119 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
125 debug("%s: %s: ", __func__, prop_name);
126 cell = fdt_getprop(blob, node, prop_name, &len);
127 if (cell && len >= sizeof(s32)) {
128 s32 val = fdt32_to_cpu(cell[0]);
130 debug("%#x (%d)\n", val, val);
133 debug("(not found)\n");
137 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
138 uint64_t default_val)
140 const uint64_t *cell64;
143 cell64 = fdt_getprop(blob, node, prop_name, &length);
144 if (!cell64 || length < sizeof(*cell64))
147 return fdt64_to_cpu(*cell64);
150 int fdtdec_get_is_enabled(const void *blob, int node)
155 * It should say "okay", so only allow that. Some fdts use "ok" but
156 * this is a bug. Please fix your device tree source file. See here
159 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
161 cell = fdt_getprop(blob, node, "status", NULL);
163 return 0 == strcmp(cell, "okay");
167 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
169 enum fdt_compat_id id;
171 /* Search our drivers */
172 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
173 if (0 == fdt_node_check_compatible(blob, node,
176 return COMPAT_UNKNOWN;
179 int fdtdec_next_compatible(const void *blob, int node,
180 enum fdt_compat_id id)
182 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
185 int fdtdec_next_compatible_subnode(const void *blob, int node,
186 enum fdt_compat_id id, int *depthp)
189 node = fdt_next_node(blob, node, depthp);
190 } while (*depthp > 1);
192 /* If this is a direct subnode, and compatible, return it */
193 if (*depthp == 1 && 0 == fdt_node_check_compatible(
194 blob, node, compat_names[id]))
197 return -FDT_ERR_NOTFOUND;
200 int fdtdec_next_alias(const void *blob, const char *name,
201 enum fdt_compat_id id, int *upto)
203 #define MAX_STR_LEN 20
204 char str[MAX_STR_LEN + 20];
207 /* snprintf() is not available */
208 assert(strlen(name) < MAX_STR_LEN);
209 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
210 node = fdt_path_offset(blob, str);
213 err = fdt_node_check_compatible(blob, node, compat_names[id]);
217 return -FDT_ERR_NOTFOUND;
222 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
223 enum fdt_compat_id id, int *node_list, int maxcount)
225 memset(node_list, '\0', sizeof(*node_list) * maxcount);
227 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
230 /* TODO: Can we tighten this code up a little? */
231 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
232 enum fdt_compat_id id, int *node_list, int maxcount)
234 int name_len = strlen(name);
242 /* find the alias node if present */
243 alias_node = fdt_path_offset(blob, "/aliases");
246 * start with nothing, and we can assume that the root node can't
249 memset(nodes, '\0', sizeof(nodes));
251 /* First find all the compatible nodes */
252 for (node = count = 0; node >= 0 && count < maxcount;) {
253 node = fdtdec_next_compatible(blob, node, id);
255 nodes[count++] = node;
258 debug("%s: warning: maxcount exceeded with alias '%s'\n",
261 /* Now find all the aliases */
262 for (offset = fdt_first_property_offset(blob, alias_node);
264 offset = fdt_next_property_offset(blob, offset)) {
265 const struct fdt_property *prop;
271 prop = fdt_get_property_by_offset(blob, offset, NULL);
272 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
273 if (prop->len && 0 == strncmp(path, name, name_len))
274 node = fdt_path_offset(blob, prop->data);
278 /* Get the alias number */
279 number = simple_strtoul(path + name_len, NULL, 10);
280 if (number < 0 || number >= maxcount) {
281 debug("%s: warning: alias '%s' is out of range\n",
286 /* Make sure the node we found is actually in our list! */
288 for (j = 0; j < count; j++)
289 if (nodes[j] == node) {
295 debug("%s: warning: alias '%s' points to a node "
296 "'%s' that is missing or is not compatible "
297 " with '%s'\n", __func__, path,
298 fdt_get_name(blob, node, NULL),
304 * Add this node to our list in the right place, and mark
307 if (fdtdec_get_is_enabled(blob, node)) {
308 if (node_list[number]) {
309 debug("%s: warning: alias '%s' requires that "
310 "a node be placed in the list in a "
311 "position which is already filled by "
312 "node '%s'\n", __func__, path,
313 fdt_get_name(blob, node, NULL));
316 node_list[number] = node;
317 if (number >= num_found)
318 num_found = number + 1;
323 /* Add any nodes not mentioned by an alias */
324 for (i = j = 0; i < maxcount; i++) {
326 for (; j < maxcount; j++)
328 fdtdec_get_is_enabled(blob, nodes[j]))
331 /* Have we run out of nodes to add? */
335 assert(!node_list[i]);
336 node_list[i] = nodes[j++];
345 int fdtdec_check_fdt(void)
348 * We must have an FDT, but we cannot panic() yet since the console
349 * is not ready. So for now, just assert(). Boards which need an early
350 * FDT (prior to console ready) will need to make their own
351 * arrangements and do their own checks.
353 assert(!fdtdec_prepare_fdt());
358 * This function is a little odd in that it accesses global data. At some
359 * point if the architecture board.c files merge this will make more sense.
360 * Even now, it is common code.
362 int fdtdec_prepare_fdt(void)
364 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
365 fdt_check_header(gd->fdt_blob)) {
366 printf("No valid FDT found - please append one to U-Boot "
367 "binary, use u-boot-dtb.bin or define "
368 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
374 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
379 debug("%s: %s\n", __func__, prop_name);
380 phandle = fdt_getprop(blob, node, prop_name, NULL);
382 return -FDT_ERR_NOTFOUND;
384 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
389 * Look up a property in a node and check that it has a minimum length.
391 * @param blob FDT blob
392 * @param node node to examine
393 * @param prop_name name of property to find
394 * @param min_len minimum property length in bytes
395 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
396 found, or -FDT_ERR_BADLAYOUT if not enough data
397 * @return pointer to cell, which is only valid if err == 0
399 static const void *get_prop_check_min_len(const void *blob, int node,
400 const char *prop_name, int min_len, int *err)
405 debug("%s: %s\n", __func__, prop_name);
406 cell = fdt_getprop(blob, node, prop_name, &len);
408 *err = -FDT_ERR_NOTFOUND;
409 else if (len < min_len)
410 *err = -FDT_ERR_BADLAYOUT;
416 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
417 u32 *array, int count)
422 debug("%s: %s\n", __func__, prop_name);
423 cell = get_prop_check_min_len(blob, node, prop_name,
424 sizeof(u32) * count, &err);
426 for (i = 0; i < count; i++)
427 array[i] = fdt32_to_cpu(cell[i]);
432 const u32 *fdtdec_locate_array(const void *blob, int node,
433 const char *prop_name, int count)
438 cell = get_prop_check_min_len(blob, node, prop_name,
439 sizeof(u32) * count, &err);
440 return err ? NULL : cell;
443 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
448 debug("%s: %s\n", __func__, prop_name);
449 cell = fdt_getprop(blob, node, prop_name, &len);
454 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
457 * @param blob FDT blob to use
458 * @param node Node to look at
459 * @param prop_name Node property name
460 * @param gpio Array of gpio elements to fill from FDT. This will be
461 * untouched if either 0 or an error is returned
462 * @param max_count Maximum number of elements allowed
463 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
464 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
466 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
467 struct fdt_gpio_state *gpio, int max_count)
469 const struct fdt_property *prop;
474 debug("%s: %s\n", __func__, prop_name);
475 assert(max_count > 0);
476 prop = fdt_get_property(blob, node, prop_name, &len);
478 debug("%s: property '%s' missing\n", __func__, prop_name);
479 return -FDT_ERR_NOTFOUND;
482 /* We will use the name to tag the GPIO */
483 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
484 cell = (u32 *)prop->data;
485 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
486 if (len > max_count) {
487 debug(" %s: too many GPIOs / cells for "
488 "property '%s'\n", __func__, prop_name);
489 return -FDT_ERR_BADLAYOUT;
492 /* Read out the GPIO data from the cells */
493 for (i = 0; i < len; i++, cell += 3) {
494 gpio[i].gpio = fdt32_to_cpu(cell[1]);
495 gpio[i].flags = fdt32_to_cpu(cell[2]);
502 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
503 struct fdt_gpio_state *gpio)
507 debug("%s: %s\n", __func__, prop_name);
508 gpio->gpio = FDT_GPIO_NONE;
510 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
511 return err == 1 ? 0 : err;
514 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
518 if (!fdt_gpio_isvalid(gpio))
521 val = gpio_get_value(gpio->gpio);
522 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
525 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
527 if (!fdt_gpio_isvalid(gpio))
530 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
531 return gpio_set_value(gpio->gpio, val);
534 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
537 * Return success if there is no GPIO defined. This is used for
540 if (!fdt_gpio_isvalid(gpio))
543 if (gpio_request(gpio->gpio, gpio->name))
548 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
549 u8 *array, int count)
554 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
556 memcpy(array, cell, count);
560 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
561 const char *prop_name, int count)
566 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
572 int fdtdec_get_config_int(const void *blob, const char *prop_name,
577 debug("%s: %s\n", __func__, prop_name);
578 config_node = fdt_path_offset(blob, "/config");
581 return fdtdec_get_int(blob, config_node, prop_name, default_val);
584 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
589 debug("%s: %s\n", __func__, prop_name);
590 config_node = fdt_path_offset(blob, "/config");
593 prop = fdt_get_property(blob, config_node, prop_name, NULL);
598 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
604 debug("%s: %s\n", __func__, prop_name);
605 nodeoffset = fdt_path_offset(blob, "/config");
609 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
613 return (char *)nodep;
616 int fdtdec_decode_region(const void *blob, int node,
617 const char *prop_name, void **ptrp, size_t *size)
619 const fdt_addr_t *cell;
622 debug("%s: %s\n", __func__, prop_name);
623 cell = fdt_getprop(blob, node, prop_name, &len);
624 if (!cell || (len != sizeof(fdt_addr_t) * 2))
627 *ptrp = (void *)fdt_addr_to_cpu(*cell);
628 *size = fdt_size_to_cpu(cell[1]);
629 debug("%s: size=%zx\n", __func__, *size);