2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/ctype.h>
16 DECLARE_GLOBAL_DATA_PTR;
19 * Here are the type we know about. One day we might allow drivers to
20 * register. For now we just put them here. The COMPAT macro allows us to
21 * turn this into a sparse list later, and keeps the ID with the name.
23 #define COMPAT(id, name) name
24 static const char * const compat_names[COMPAT_COUNT] = {
25 COMPAT(UNKNOWN, "<none>"),
26 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
27 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
28 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
29 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
30 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
31 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
32 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
33 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
34 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
35 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
36 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
37 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
38 COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
39 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
40 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
41 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
42 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
43 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
44 COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
45 COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
46 COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
47 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
48 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
49 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
50 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
51 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
52 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
53 COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
54 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
55 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
56 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
57 COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
58 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
59 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
60 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
61 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
62 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
63 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
64 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
65 COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
66 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
67 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
68 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
69 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
70 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
71 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
72 COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
73 COMPAT(SANDBOX_HOST_EMULATION, "sandbox,host-emulation"),
74 COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
75 COMPAT(TI_TPS65090, "ti,tps65090"),
76 COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
77 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
78 COMPAT(PARADE_PS8625, "parade,ps8625"),
79 COMPAT(COMPAT_INTEL_LPC, "intel,lpc"),
80 COMPAT(INTEL_MICROCODE, "intel,microcode"),
81 COMPAT(MEMORY_SPD, "memory-spd"),
82 COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
83 COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
84 COMPAT(INTEL_GMA, "intel,gma"),
85 COMPAT(AMS_AS3722, "ams,as3722"),
88 const char *fdtdec_get_compatible(enum fdt_compat_id id)
90 /* We allow reading of the 'unknown' ID for testing purposes */
91 assert(id >= 0 && id < COMPAT_COUNT);
92 return compat_names[id];
95 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
96 const char *prop_name, fdt_size_t *sizep)
98 const fdt_addr_t *cell;
101 debug("%s: %s: ", __func__, prop_name);
102 cell = fdt_getprop(blob, node, prop_name, &len);
103 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
104 len == sizeof(fdt_addr_t) * 2)) {
105 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
107 const fdt_size_t *size;
109 size = (fdt_size_t *)((char *)cell +
111 *sizep = fdt_size_to_cpu(*size);
112 debug("addr=%08lx, size=%08x\n",
113 (ulong)addr, *sizep);
115 debug("%08lx\n", (ulong)addr);
119 debug("(not found)\n");
120 return FDT_ADDR_T_NONE;
123 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
124 const char *prop_name)
126 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
130 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
131 const char *prop_name, struct fdt_pci_addr *addr)
137 debug("%s: %s: ", __func__, prop_name);
140 * If we follow the pci bus bindings strictly, we should check
141 * the value of the node's parent node's #address-cells and
142 * #size-cells. They need to be 3 and 2 accordingly. However,
143 * for simplicity we skip the check here.
145 cell = fdt_getprop(blob, node, prop_name, &len);
149 if ((len % FDT_PCI_REG_SIZE) == 0) {
150 int num = len / FDT_PCI_REG_SIZE;
153 for (i = 0; i < num; i++) {
154 debug("pci address #%d: %08lx %08lx %08lx\n", i,
155 (ulong)fdt_addr_to_cpu(cell[0]),
156 (ulong)fdt_addr_to_cpu(cell[1]),
157 (ulong)fdt_addr_to_cpu(cell[2]));
158 if ((fdt_addr_to_cpu(*cell) & type) == type) {
159 addr->phys_hi = fdt_addr_to_cpu(cell[0]);
160 addr->phys_mid = fdt_addr_to_cpu(cell[1]);
161 addr->phys_lo = fdt_addr_to_cpu(cell[2]);
164 cell += (FDT_PCI_ADDR_CELLS +
178 debug("(not found)\n");
182 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
184 const char *list, *end;
187 list = fdt_getprop(blob, node, "compatible", &len);
196 if (len >= strlen("pciVVVV,DDDD")) {
197 s = strstr(list, "pci");
200 * check if the string is something like pciVVVV,DDDD.RR
201 * or just pciVVVV,DDDD
203 if (s && s[7] == ',' &&
204 (s[12] == '.' || s[12] == 0)) {
206 *vendor = simple_strtol(s, NULL, 16);
209 *device = simple_strtol(s, NULL, 16);
221 int fdtdec_get_pci_bdf(const void *blob, int node,
222 struct fdt_pci_addr *addr, pci_dev_t *bdf)
224 u16 dt_vendor, dt_device, vendor, device;
227 /* get vendor id & device id from the compatible string */
228 ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
232 /* extract the bdf from fdt_pci_addr */
233 *bdf = addr->phys_hi & 0xffff00;
235 /* read vendor id & device id based on bdf */
236 pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
237 pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
240 * Note there are two places in the device tree to fully describe
241 * a pci device: one is via compatible string with a format of
242 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
243 * the device node's reg address property. We read the vendor id
244 * and device id based on bdf and compare the values with the
245 * "VVVV,DDDD". If they are the same, then we are good to use bdf
246 * to read device's bar. But if they are different, we have to rely
247 * on the vendor id and device id extracted from the compatible
248 * string and locate the real bdf by pci_find_device(). This is
249 * because normally we may only know device's device number and
250 * function number when writing device tree. The bus number is
251 * dynamically assigned during the pci enumeration process.
253 if ((dt_vendor != vendor) || (dt_device != device)) {
254 *bdf = pci_find_device(dt_vendor, dt_device, 0);
262 int fdtdec_get_pci_bar32(const void *blob, int node,
263 struct fdt_pci_addr *addr, u32 *bar)
269 /* get pci devices's bdf */
270 ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
274 /* extract the bar number from fdt_pci_addr */
275 barnum = addr->phys_hi & 0xff;
276 if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
279 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
280 *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
286 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
287 uint64_t default_val)
289 const uint64_t *cell64;
292 cell64 = fdt_getprop(blob, node, prop_name, &length);
293 if (!cell64 || length < sizeof(*cell64))
296 return fdt64_to_cpu(*cell64);
299 int fdtdec_get_is_enabled(const void *blob, int node)
304 * It should say "okay", so only allow that. Some fdts use "ok" but
305 * this is a bug. Please fix your device tree source file. See here
308 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
310 cell = fdt_getprop(blob, node, "status", NULL);
312 return 0 == strcmp(cell, "okay");
316 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
318 enum fdt_compat_id id;
320 /* Search our drivers */
321 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
322 if (0 == fdt_node_check_compatible(blob, node,
325 return COMPAT_UNKNOWN;
328 int fdtdec_next_compatible(const void *blob, int node,
329 enum fdt_compat_id id)
331 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
334 int fdtdec_next_compatible_subnode(const void *blob, int node,
335 enum fdt_compat_id id, int *depthp)
338 node = fdt_next_node(blob, node, depthp);
339 } while (*depthp > 1);
341 /* If this is a direct subnode, and compatible, return it */
342 if (*depthp == 1 && 0 == fdt_node_check_compatible(
343 blob, node, compat_names[id]))
346 return -FDT_ERR_NOTFOUND;
349 int fdtdec_next_alias(const void *blob, const char *name,
350 enum fdt_compat_id id, int *upto)
352 #define MAX_STR_LEN 20
353 char str[MAX_STR_LEN + 20];
356 /* snprintf() is not available */
357 assert(strlen(name) < MAX_STR_LEN);
358 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
359 node = fdt_path_offset(blob, str);
362 err = fdt_node_check_compatible(blob, node, compat_names[id]);
366 return -FDT_ERR_NOTFOUND;
371 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
372 enum fdt_compat_id id, int *node_list, int maxcount)
374 memset(node_list, '\0', sizeof(*node_list) * maxcount);
376 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
379 /* TODO: Can we tighten this code up a little? */
380 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
381 enum fdt_compat_id id, int *node_list, int maxcount)
383 int name_len = strlen(name);
391 /* find the alias node if present */
392 alias_node = fdt_path_offset(blob, "/aliases");
395 * start with nothing, and we can assume that the root node can't
398 memset(nodes, '\0', sizeof(nodes));
400 /* First find all the compatible nodes */
401 for (node = count = 0; node >= 0 && count < maxcount;) {
402 node = fdtdec_next_compatible(blob, node, id);
404 nodes[count++] = node;
407 debug("%s: warning: maxcount exceeded with alias '%s'\n",
410 /* Now find all the aliases */
411 for (offset = fdt_first_property_offset(blob, alias_node);
413 offset = fdt_next_property_offset(blob, offset)) {
414 const struct fdt_property *prop;
420 prop = fdt_get_property_by_offset(blob, offset, NULL);
421 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
422 if (prop->len && 0 == strncmp(path, name, name_len))
423 node = fdt_path_offset(blob, prop->data);
427 /* Get the alias number */
428 number = simple_strtoul(path + name_len, NULL, 10);
429 if (number < 0 || number >= maxcount) {
430 debug("%s: warning: alias '%s' is out of range\n",
435 /* Make sure the node we found is actually in our list! */
437 for (j = 0; j < count; j++)
438 if (nodes[j] == node) {
444 debug("%s: warning: alias '%s' points to a node "
445 "'%s' that is missing or is not compatible "
446 " with '%s'\n", __func__, path,
447 fdt_get_name(blob, node, NULL),
453 * Add this node to our list in the right place, and mark
456 if (fdtdec_get_is_enabled(blob, node)) {
457 if (node_list[number]) {
458 debug("%s: warning: alias '%s' requires that "
459 "a node be placed in the list in a "
460 "position which is already filled by "
461 "node '%s'\n", __func__, path,
462 fdt_get_name(blob, node, NULL));
465 node_list[number] = node;
466 if (number >= num_found)
467 num_found = number + 1;
472 /* Add any nodes not mentioned by an alias */
473 for (i = j = 0; i < maxcount; i++) {
475 for (; j < maxcount; j++)
477 fdtdec_get_is_enabled(blob, nodes[j]))
480 /* Have we run out of nodes to add? */
484 assert(!node_list[i]);
485 node_list[i] = nodes[j++];
494 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
497 int base_len = strlen(base);
498 const char *find_name;
503 find_name = fdt_get_name(blob, offset, &find_namelen);
504 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
506 aliases = fdt_path_offset(blob, "/aliases");
507 for (prop_offset = fdt_first_property_offset(blob, aliases);
509 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
516 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
517 debug(" - %s, %s\n", name, prop);
518 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
519 strncmp(name, base, base_len))
522 slash = strrchr(prop, '/');
523 if (strcmp(slash + 1, find_name))
525 for (p = name + strlen(name) - 1; p > name; p--) {
527 *seqp = simple_strtoul(p + 1, NULL, 10);
528 debug("Found seq %d\n", *seqp);
534 debug("Not found\n");
538 int fdtdec_get_chosen_node(const void *blob, const char *name)
545 return -FDT_ERR_NOTFOUND;
546 chosen_node = fdt_path_offset(blob, "/chosen");
547 prop = fdt_getprop(blob, chosen_node, name, &len);
549 return -FDT_ERR_NOTFOUND;
550 return fdt_path_offset(blob, prop);
553 int fdtdec_check_fdt(void)
556 * We must have an FDT, but we cannot panic() yet since the console
557 * is not ready. So for now, just assert(). Boards which need an early
558 * FDT (prior to console ready) will need to make their own
559 * arrangements and do their own checks.
561 assert(!fdtdec_prepare_fdt());
566 * This function is a little odd in that it accesses global data. At some
567 * point if the architecture board.c files merge this will make more sense.
568 * Even now, it is common code.
570 int fdtdec_prepare_fdt(void)
572 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
573 fdt_check_header(gd->fdt_blob)) {
574 printf("No valid FDT found - please append one to U-Boot "
575 "binary, use u-boot-dtb.bin or define "
576 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
582 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
587 debug("%s: %s\n", __func__, prop_name);
588 phandle = fdt_getprop(blob, node, prop_name, NULL);
590 return -FDT_ERR_NOTFOUND;
592 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
597 * Look up a property in a node and check that it has a minimum length.
599 * @param blob FDT blob
600 * @param node node to examine
601 * @param prop_name name of property to find
602 * @param min_len minimum property length in bytes
603 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
604 found, or -FDT_ERR_BADLAYOUT if not enough data
605 * @return pointer to cell, which is only valid if err == 0
607 static const void *get_prop_check_min_len(const void *blob, int node,
608 const char *prop_name, int min_len, int *err)
613 debug("%s: %s\n", __func__, prop_name);
614 cell = fdt_getprop(blob, node, prop_name, &len);
616 *err = -FDT_ERR_NOTFOUND;
617 else if (len < min_len)
618 *err = -FDT_ERR_BADLAYOUT;
624 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
625 u32 *array, int count)
630 debug("%s: %s\n", __func__, prop_name);
631 cell = get_prop_check_min_len(blob, node, prop_name,
632 sizeof(u32) * count, &err);
634 for (i = 0; i < count; i++)
635 array[i] = fdt32_to_cpu(cell[i]);
640 int fdtdec_get_int_array_count(const void *blob, int node,
641 const char *prop_name, u32 *array, int count)
647 debug("%s: %s\n", __func__, prop_name);
648 cell = fdt_getprop(blob, node, prop_name, &len);
650 return -FDT_ERR_NOTFOUND;
651 elems = len / sizeof(u32);
654 for (i = 0; i < count; i++)
655 array[i] = fdt32_to_cpu(cell[i]);
660 const u32 *fdtdec_locate_array(const void *blob, int node,
661 const char *prop_name, int count)
666 cell = get_prop_check_min_len(blob, node, prop_name,
667 sizeof(u32) * count, &err);
668 return err ? NULL : cell;
671 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
676 debug("%s: %s\n", __func__, prop_name);
677 cell = fdt_getprop(blob, node, prop_name, &len);
682 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
685 * @param blob FDT blob to use
686 * @param node Node to look at
687 * @param prop_name Node property name
688 * @param gpio Array of gpio elements to fill from FDT. This will be
689 * untouched if either 0 or an error is returned
690 * @param max_count Maximum number of elements allowed
691 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
692 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
694 int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
695 struct fdt_gpio_state *gpio, int max_count)
697 const struct fdt_property *prop;
702 debug("%s: %s\n", __func__, prop_name);
703 assert(max_count > 0);
704 prop = fdt_get_property(blob, node, prop_name, &len);
706 debug("%s: property '%s' missing\n", __func__, prop_name);
707 return -FDT_ERR_NOTFOUND;
710 /* We will use the name to tag the GPIO */
711 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
712 cell = (u32 *)prop->data;
713 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
714 if (len > max_count) {
715 debug(" %s: too many GPIOs / cells for "
716 "property '%s'\n", __func__, prop_name);
717 return -FDT_ERR_BADLAYOUT;
720 /* Read out the GPIO data from the cells */
721 for (i = 0; i < len; i++, cell += 3) {
722 gpio[i].gpio = fdt32_to_cpu(cell[1]);
723 gpio[i].flags = fdt32_to_cpu(cell[2]);
730 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
731 struct fdt_gpio_state *gpio)
735 debug("%s: %s\n", __func__, prop_name);
736 gpio->gpio = FDT_GPIO_NONE;
738 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
739 return err == 1 ? 0 : err;
742 int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
746 if (!fdt_gpio_isvalid(gpio))
749 val = gpio_get_value(gpio->gpio);
750 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
753 int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
755 if (!fdt_gpio_isvalid(gpio))
758 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
759 return gpio_set_value(gpio->gpio, val);
762 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
765 * Return success if there is no GPIO defined. This is used for
768 if (!fdt_gpio_isvalid(gpio))
771 if (gpio_request(gpio->gpio, gpio->name))
776 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
777 u8 *array, int count)
782 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
784 memcpy(array, cell, count);
788 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
789 const char *prop_name, int count)
794 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
800 int fdtdec_get_config_int(const void *blob, const char *prop_name,
805 debug("%s: %s\n", __func__, prop_name);
806 config_node = fdt_path_offset(blob, "/config");
809 return fdtdec_get_int(blob, config_node, prop_name, default_val);
812 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
817 debug("%s: %s\n", __func__, prop_name);
818 config_node = fdt_path_offset(blob, "/config");
821 prop = fdt_get_property(blob, config_node, prop_name, NULL);
826 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
832 debug("%s: %s\n", __func__, prop_name);
833 nodeoffset = fdt_path_offset(blob, "/config");
837 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
841 return (char *)nodep;
844 int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
845 fdt_addr_t *basep, fdt_size_t *sizep)
847 const fdt_addr_t *cell;
850 debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
852 cell = fdt_getprop(blob, node, prop_name, &len);
853 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
854 debug("cell=%p, len=%d\n", cell, len);
858 *basep = fdt_addr_to_cpu(*cell);
859 *sizep = fdt_size_to_cpu(cell[1]);
860 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
867 * Read a flash entry from the fdt
869 * @param blob FDT blob
870 * @param node Offset of node to read
871 * @param name Name of node being read
872 * @param entry Place to put offset and size of this node
873 * @return 0 if ok, -ve on error
875 int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
876 struct fmap_entry *entry)
881 if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
882 debug("Node '%s' has bad/missing 'reg' property\n", name);
883 return -FDT_ERR_NOTFOUND;
885 entry->offset = reg[0];
886 entry->length = reg[1];
887 entry->used = fdtdec_get_int(blob, node, "used", entry->length);
888 prop = fdt_getprop(blob, node, "compress", NULL);
889 entry->compress_algo = prop && !strcmp(prop, "lzo") ?
890 FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
891 prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
892 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
893 entry->hash = (uint8_t *)prop;
898 static u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
903 number = (number << 32) | fdt32_to_cpu(*ptr++);
908 int fdt_get_resource(const void *fdt, int node, const char *property,
909 unsigned int index, struct fdt_resource *res)
911 const fdt32_t *ptr, *end;
912 int na, ns, len, parent;
915 parent = fdt_parent_offset(fdt, node);
919 na = fdt_address_cells(fdt, parent);
920 ns = fdt_size_cells(fdt, parent);
922 ptr = fdt_getprop(fdt, node, property, &len);
926 end = ptr + len / sizeof(*ptr);
928 while (ptr + na + ns <= end) {
930 res->start = res->end = fdtdec_get_number(ptr, na);
931 res->end += fdtdec_get_number(&ptr[na], ns) - 1;
939 return -FDT_ERR_NOTFOUND;
942 int fdt_get_named_resource(const void *fdt, int node, const char *property,
943 const char *prop_names, const char *name,
944 struct fdt_resource *res)
948 index = fdt_find_string(fdt, node, prop_names, name);
952 return fdt_get_resource(fdt, node, property, index, res);
955 int fdtdec_decode_memory_region(const void *blob, int config_node,
956 const char *mem_type, const char *suffix,
957 fdt_addr_t *basep, fdt_size_t *sizep)
961 fdt_size_t size, offset_size;
962 fdt_addr_t base, offset;
965 if (config_node == -1) {
966 config_node = fdt_path_offset(blob, "/config");
967 if (config_node < 0) {
968 debug("%s: Cannot find /config node\n", __func__);
975 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
977 mem = fdt_getprop(blob, config_node, prop_name, NULL);
979 debug("%s: No memory type for '%s', using /memory\n", __func__,
984 node = fdt_path_offset(blob, mem);
986 debug("%s: Failed to find node '%s': %s\n", __func__, mem,
992 * Not strictly correct - the memory may have multiple banks. We just
995 if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
996 debug("%s: Failed to decode memory region %s\n", __func__,
1001 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1003 if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1005 debug("%s: Failed to decode memory region '%s'\n", __func__,
1010 *basep = base + offset;
1011 *sizep = offset_size;