2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
13 DECLARE_GLOBAL_DATA_PTR;
16 * Here are the type we know about. One day we might allow drivers to
17 * register. For now we just put them here. The COMPAT macro allows us to
18 * turn this into a sparse list later, and keeps the ID with the name.
20 #define COMPAT(id, name) name
21 static const char * const compat_names[COMPAT_COUNT] = {
22 COMPAT(UNKNOWN, "<none>"),
23 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
24 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
25 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
26 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
27 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
28 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
29 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
30 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
31 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
32 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
33 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
34 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
35 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
36 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
37 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
38 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
39 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
40 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
41 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
42 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
43 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
44 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
45 COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
46 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
47 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
48 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
49 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
50 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
51 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
52 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
53 COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"),
54 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
55 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
56 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
57 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
58 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
59 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
62 const char *fdtdec_get_compatible(enum fdt_compat_id id)
64 /* We allow reading of the 'unknown' ID for testing purposes */
65 assert(id >= 0 && id < COMPAT_COUNT);
66 return compat_names[id];
69 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
70 const char *prop_name, fdt_size_t *sizep)
72 const fdt_addr_t *cell;
75 debug("%s: %s: ", __func__, prop_name);
76 cell = fdt_getprop(blob, node, prop_name, &len);
77 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
78 len == sizeof(fdt_addr_t) * 2)) {
79 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
81 const fdt_size_t *size;
83 size = (fdt_size_t *)((char *)cell +
85 *sizep = fdt_size_to_cpu(*size);
86 debug("addr=%p, size=%p\n", (void *)addr,
89 debug("%p\n", (void *)addr);
93 debug("(not found)\n");
94 return FDT_ADDR_T_NONE;
97 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
98 const char *prop_name)
100 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
103 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
109 debug("%s: %s: ", __func__, prop_name);
110 cell = fdt_getprop(blob, node, prop_name, &len);
111 if (cell && len >= sizeof(s32)) {
112 s32 val = fdt32_to_cpu(cell[0]);
114 debug("%#x (%d)\n", val, val);
117 debug("(not found)\n");
121 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
122 uint64_t default_val)
124 const uint64_t *cell64;
127 cell64 = fdt_getprop(blob, node, prop_name, &length);
128 if (!cell64 || length < sizeof(*cell64))
131 return fdt64_to_cpu(*cell64);
134 int fdtdec_get_is_enabled(const void *blob, int node)
139 * It should say "okay", so only allow that. Some fdts use "ok" but
140 * this is a bug. Please fix your device tree source file. See here
143 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
145 cell = fdt_getprop(blob, node, "status", NULL);
147 return 0 == strcmp(cell, "okay");
151 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
153 enum fdt_compat_id id;
155 /* Search our drivers */
156 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
157 if (0 == fdt_node_check_compatible(blob, node,
160 return COMPAT_UNKNOWN;
163 int fdtdec_next_compatible(const void *blob, int node,
164 enum fdt_compat_id id)
166 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
169 int fdtdec_next_compatible_subnode(const void *blob, int node,
170 enum fdt_compat_id id, int *depthp)
173 node = fdt_next_node(blob, node, depthp);
174 } while (*depthp > 1);
176 /* If this is a direct subnode, and compatible, return it */
177 if (*depthp == 1 && 0 == fdt_node_check_compatible(
178 blob, node, compat_names[id]))
181 return -FDT_ERR_NOTFOUND;
184 int fdtdec_next_alias(const void *blob, const char *name,
185 enum fdt_compat_id id, int *upto)
187 #define MAX_STR_LEN 20
188 char str[MAX_STR_LEN + 20];
191 /* snprintf() is not available */
192 assert(strlen(name) < MAX_STR_LEN);
193 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
194 node = fdt_path_offset(blob, str);
197 err = fdt_node_check_compatible(blob, node, compat_names[id]);
201 return -FDT_ERR_NOTFOUND;
206 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
207 enum fdt_compat_id id, int *node_list, int maxcount)
209 memset(node_list, '\0', sizeof(*node_list) * maxcount);
211 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
214 /* TODO: Can we tighten this code up a little? */
215 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
216 enum fdt_compat_id id, int *node_list, int maxcount)
218 int name_len = strlen(name);
226 /* find the alias node if present */
227 alias_node = fdt_path_offset(blob, "/aliases");
230 * start with nothing, and we can assume that the root node can't
233 memset(nodes, '\0', sizeof(nodes));
235 /* First find all the compatible nodes */
236 for (node = count = 0; node >= 0 && count < maxcount;) {
237 node = fdtdec_next_compatible(blob, node, id);
239 nodes[count++] = node;
242 debug("%s: warning: maxcount exceeded with alias '%s'\n",
245 /* Now find all the aliases */
246 for (offset = fdt_first_property_offset(blob, alias_node);
248 offset = fdt_next_property_offset(blob, offset)) {
249 const struct fdt_property *prop;
255 prop = fdt_get_property_by_offset(blob, offset, NULL);
256 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
257 if (prop->len && 0 == strncmp(path, name, name_len))
258 node = fdt_path_offset(blob, prop->data);
262 /* Get the alias number */
263 number = simple_strtoul(path + name_len, NULL, 10);
264 if (number < 0 || number >= maxcount) {
265 debug("%s: warning: alias '%s' is out of range\n",
270 /* Make sure the node we found is actually in our list! */
272 for (j = 0; j < count; j++)
273 if (nodes[j] == node) {
279 debug("%s: warning: alias '%s' points to a node "
280 "'%s' that is missing or is not compatible "
281 " with '%s'\n", __func__, path,
282 fdt_get_name(blob, node, NULL),
288 * Add this node to our list in the right place, and mark
291 if (fdtdec_get_is_enabled(blob, node)) {
292 if (node_list[number]) {
293 debug("%s: warning: alias '%s' requires that "
294 "a node be placed in the list in a "
295 "position which is already filled by "
296 "node '%s'\n", __func__, path,
297 fdt_get_name(blob, node, NULL));
300 node_list[number] = node;
301 if (number >= num_found)
302 num_found = number + 1;
307 /* Add any nodes not mentioned by an alias */
308 for (i = j = 0; i < maxcount; i++) {
310 for (; j < maxcount; j++)
312 fdtdec_get_is_enabled(blob, nodes[j]))
315 /* Have we run out of nodes to add? */
319 assert(!node_list[i]);
320 node_list[i] = nodes[j++];
329 int fdtdec_check_fdt(void)
332 * We must have an FDT, but we cannot panic() yet since the console
333 * is not ready. So for now, just assert(). Boards which need an early
334 * FDT (prior to console ready) will need to make their own
335 * arrangements and do their own checks.
337 assert(!fdtdec_prepare_fdt());
342 * This function is a little odd in that it accesses global data. At some
343 * point if the architecture board.c files merge this will make more sense.
344 * Even now, it is common code.
346 int fdtdec_prepare_fdt(void)
348 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
349 fdt_check_header(gd->fdt_blob)) {
350 printf("No valid FDT found - please append one to U-Boot "
351 "binary, use u-boot-dtb.bin or define "
352 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
358 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
363 debug("%s: %s\n", __func__, prop_name);
364 phandle = fdt_getprop(blob, node, prop_name, NULL);
366 return -FDT_ERR_NOTFOUND;
368 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
373 * Look up a property in a node and check that it has a minimum length.
375 * @param blob FDT blob
376 * @param node node to examine
377 * @param prop_name name of property to find
378 * @param min_len minimum property length in bytes
379 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
380 found, or -FDT_ERR_BADLAYOUT if not enough data
381 * @return pointer to cell, which is only valid if err == 0
383 static const void *get_prop_check_min_len(const void *blob, int node,
384 const char *prop_name, int min_len, int *err)
389 debug("%s: %s\n", __func__, prop_name);
390 cell = fdt_getprop(blob, node, prop_name, &len);
392 *err = -FDT_ERR_NOTFOUND;
393 else if (len < min_len)
394 *err = -FDT_ERR_BADLAYOUT;
400 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
401 u32 *array, int count)
406 debug("%s: %s\n", __func__, prop_name);
407 cell = get_prop_check_min_len(blob, node, prop_name,
408 sizeof(u32) * count, &err);
410 for (i = 0; i < count; i++)
411 array[i] = fdt32_to_cpu(cell[i]);
416 const u32 *fdtdec_locate_array(const void *blob, int node,
417 const char *prop_name, int count)
422 cell = get_prop_check_min_len(blob, node, prop_name,
423 sizeof(u32) * count, &err);
424 return err ? NULL : cell;
427 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
432 debug("%s: %s\n", __func__, prop_name);
433 cell = fdt_getprop(blob, node, prop_name, &len);
438 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
441 * @param blob FDT blob to use
442 * @param node Node to look at
443 * @param prop_name Node property name
444 * @param gpio Array of gpio elements to fill from FDT. This will be
445 * untouched if either 0 or an error is returned
446 * @param max_count Maximum number of elements allowed
447 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
448 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
450 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
451 struct fdt_gpio_state *gpio, int max_count)
453 const struct fdt_property *prop;
458 debug("%s: %s\n", __func__, prop_name);
459 assert(max_count > 0);
460 prop = fdt_get_property(blob, node, prop_name, &len);
462 debug("%s: property '%s' missing\n", __func__, prop_name);
463 return -FDT_ERR_NOTFOUND;
466 /* We will use the name to tag the GPIO */
467 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
468 cell = (u32 *)prop->data;
469 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
470 if (len > max_count) {
471 debug(" %s: too many GPIOs / cells for "
472 "property '%s'\n", __func__, prop_name);
473 return -FDT_ERR_BADLAYOUT;
476 /* Read out the GPIO data from the cells */
477 for (i = 0; i < len; i++, cell += 3) {
478 gpio[i].gpio = fdt32_to_cpu(cell[1]);
479 gpio[i].flags = fdt32_to_cpu(cell[2]);
486 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
487 struct fdt_gpio_state *gpio)
491 debug("%s: %s\n", __func__, prop_name);
492 gpio->gpio = FDT_GPIO_NONE;
494 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
495 return err == 1 ? 0 : err;
498 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
502 if (!fdt_gpio_isvalid(gpio))
505 val = gpio_get_value(gpio->gpio);
506 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
509 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
511 if (!fdt_gpio_isvalid(gpio))
514 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
515 return gpio_set_value(gpio->gpio, val);
518 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
521 * Return success if there is no GPIO defined. This is used for
524 if (!fdt_gpio_isvalid(gpio))
527 if (gpio_request(gpio->gpio, gpio->name))
532 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
533 u8 *array, int count)
538 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
540 memcpy(array, cell, count);
544 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
545 const char *prop_name, int count)
550 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
556 int fdtdec_get_config_int(const void *blob, const char *prop_name,
561 debug("%s: %s\n", __func__, prop_name);
562 config_node = fdt_path_offset(blob, "/config");
565 return fdtdec_get_int(blob, config_node, prop_name, default_val);
568 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
573 debug("%s: %s\n", __func__, prop_name);
574 config_node = fdt_path_offset(blob, "/config");
577 prop = fdt_get_property(blob, config_node, prop_name, NULL);
582 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
588 debug("%s: %s\n", __func__, prop_name);
589 nodeoffset = fdt_path_offset(blob, "/config");
593 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
597 return (char *)nodep;
600 int fdtdec_decode_region(const void *blob, int node,
601 const char *prop_name, void **ptrp, size_t *size)
603 const fdt_addr_t *cell;
606 debug("%s: %s\n", __func__, prop_name);
607 cell = fdt_getprop(blob, node, prop_name, &len);
608 if (!cell || (len != sizeof(fdt_addr_t) * 2))
611 *ptrp = (void *)fdt_addr_to_cpu(*cell);
612 *size = fdt_size_to_cpu(cell[1]);
613 debug("%s: size=%zx\n", __func__, *size);