2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/ctype.h>
14 DECLARE_GLOBAL_DATA_PTR;
17 * Here are the type we know about. One day we might allow drivers to
18 * register. For now we just put them here. The COMPAT macro allows us to
19 * turn this into a sparse list later, and keeps the ID with the name.
21 #define COMPAT(id, name) name
22 static const char * const compat_names[COMPAT_COUNT] = {
23 COMPAT(UNKNOWN, "<none>"),
24 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
25 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
26 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
27 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
28 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
29 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
30 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
31 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
32 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
33 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
34 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
35 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
36 COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
37 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
38 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
39 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
40 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
41 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
42 COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
43 COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
44 COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
45 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
46 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
47 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
48 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
49 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
50 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
51 COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
52 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
53 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
54 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
55 COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
56 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
57 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
58 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
59 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
60 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
61 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
62 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
63 COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
64 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
65 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
66 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
67 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
68 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
69 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
70 COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
71 COMPAT(SANDBOX_HOST_EMULATION, "sandbox,host-emulation"),
72 COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
73 COMPAT(TI_TPS65090, "ti,tps65090"),
74 COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
75 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
76 COMPAT(PARADE_PS8625, "parade,ps8625"),
77 COMPAT(COMPAT_INTEL_LPC, "intel,lpc"),
78 COMPAT(INTEL_MICROCODE, "intel,microcode"),
79 COMPAT(MEMORY_SPD, "memory-spd"),
80 COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
81 COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
82 COMPAT(INTEL_GMA, "intel,gma"),
83 COMPAT(AMS_AS3722, "ams,as3722"),
84 COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
87 const char *fdtdec_get_compatible(enum fdt_compat_id id)
89 /* We allow reading of the 'unknown' ID for testing purposes */
90 assert(id >= 0 && id < COMPAT_COUNT);
91 return compat_names[id];
94 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
95 const char *prop_name, fdt_size_t *sizep)
97 const fdt_addr_t *cell;
100 debug("%s: %s: ", __func__, prop_name);
101 cell = fdt_getprop(blob, node, prop_name, &len);
102 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
103 len == sizeof(fdt_addr_t) * 2)) {
104 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
106 const fdt_size_t *size;
108 size = (fdt_size_t *)((char *)cell +
110 *sizep = fdt_size_to_cpu(*size);
111 debug("addr=%08lx, size=%08x\n",
112 (ulong)addr, *sizep);
114 debug("%08lx\n", (ulong)addr);
118 debug("(not found)\n");
119 return FDT_ADDR_T_NONE;
122 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
123 const char *prop_name)
125 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
129 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
130 const char *prop_name, struct fdt_pci_addr *addr)
136 debug("%s: %s: ", __func__, prop_name);
139 * If we follow the pci bus bindings strictly, we should check
140 * the value of the node's parent node's #address-cells and
141 * #size-cells. They need to be 3 and 2 accordingly. However,
142 * for simplicity we skip the check here.
144 cell = fdt_getprop(blob, node, prop_name, &len);
148 if ((len % FDT_PCI_REG_SIZE) == 0) {
149 int num = len / FDT_PCI_REG_SIZE;
152 for (i = 0; i < num; i++) {
153 debug("pci address #%d: %08lx %08lx %08lx\n", i,
154 (ulong)fdt_addr_to_cpu(cell[0]),
155 (ulong)fdt_addr_to_cpu(cell[1]),
156 (ulong)fdt_addr_to_cpu(cell[2]));
157 if ((fdt_addr_to_cpu(*cell) & type) == type) {
158 addr->phys_hi = fdt_addr_to_cpu(cell[0]);
159 addr->phys_mid = fdt_addr_to_cpu(cell[1]);
160 addr->phys_lo = fdt_addr_to_cpu(cell[2]);
163 cell += (FDT_PCI_ADDR_CELLS +
177 debug("(not found)\n");
181 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
183 const char *list, *end;
186 list = fdt_getprop(blob, node, "compatible", &len);
195 if (len >= strlen("pciVVVV,DDDD")) {
196 s = strstr(list, "pci");
199 * check if the string is something like pciVVVV,DDDD.RR
200 * or just pciVVVV,DDDD
202 if (s && s[7] == ',' &&
203 (s[12] == '.' || s[12] == 0)) {
205 *vendor = simple_strtol(s, NULL, 16);
208 *device = simple_strtol(s, NULL, 16);
220 int fdtdec_get_pci_bdf(const void *blob, int node,
221 struct fdt_pci_addr *addr, pci_dev_t *bdf)
223 u16 dt_vendor, dt_device, vendor, device;
226 /* get vendor id & device id from the compatible string */
227 ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
231 /* extract the bdf from fdt_pci_addr */
232 *bdf = addr->phys_hi & 0xffff00;
234 /* read vendor id & device id based on bdf */
235 pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
236 pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
239 * Note there are two places in the device tree to fully describe
240 * a pci device: one is via compatible string with a format of
241 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
242 * the device node's reg address property. We read the vendor id
243 * and device id based on bdf and compare the values with the
244 * "VVVV,DDDD". If they are the same, then we are good to use bdf
245 * to read device's bar. But if they are different, we have to rely
246 * on the vendor id and device id extracted from the compatible
247 * string and locate the real bdf by pci_find_device(). This is
248 * because normally we may only know device's device number and
249 * function number when writing device tree. The bus number is
250 * dynamically assigned during the pci enumeration process.
252 if ((dt_vendor != vendor) || (dt_device != device)) {
253 *bdf = pci_find_device(dt_vendor, dt_device, 0);
261 int fdtdec_get_pci_bar32(const void *blob, int node,
262 struct fdt_pci_addr *addr, u32 *bar)
268 /* get pci devices's bdf */
269 ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
273 /* extract the bar number from fdt_pci_addr */
274 barnum = addr->phys_hi & 0xff;
275 if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
278 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
279 *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
285 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
286 uint64_t default_val)
288 const uint64_t *cell64;
291 cell64 = fdt_getprop(blob, node, prop_name, &length);
292 if (!cell64 || length < sizeof(*cell64))
295 return fdt64_to_cpu(*cell64);
298 int fdtdec_get_is_enabled(const void *blob, int node)
303 * It should say "okay", so only allow that. Some fdts use "ok" but
304 * this is a bug. Please fix your device tree source file. See here
307 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
309 cell = fdt_getprop(blob, node, "status", NULL);
311 return 0 == strcmp(cell, "okay");
315 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
317 enum fdt_compat_id id;
319 /* Search our drivers */
320 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
321 if (0 == fdt_node_check_compatible(blob, node,
324 return COMPAT_UNKNOWN;
327 int fdtdec_next_compatible(const void *blob, int node,
328 enum fdt_compat_id id)
330 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
333 int fdtdec_next_compatible_subnode(const void *blob, int node,
334 enum fdt_compat_id id, int *depthp)
337 node = fdt_next_node(blob, node, depthp);
338 } while (*depthp > 1);
340 /* If this is a direct subnode, and compatible, return it */
341 if (*depthp == 1 && 0 == fdt_node_check_compatible(
342 blob, node, compat_names[id]))
345 return -FDT_ERR_NOTFOUND;
348 int fdtdec_next_alias(const void *blob, const char *name,
349 enum fdt_compat_id id, int *upto)
351 #define MAX_STR_LEN 20
352 char str[MAX_STR_LEN + 20];
355 /* snprintf() is not available */
356 assert(strlen(name) < MAX_STR_LEN);
357 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
358 node = fdt_path_offset(blob, str);
361 err = fdt_node_check_compatible(blob, node, compat_names[id]);
365 return -FDT_ERR_NOTFOUND;
370 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
371 enum fdt_compat_id id, int *node_list, int maxcount)
373 memset(node_list, '\0', sizeof(*node_list) * maxcount);
375 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
378 /* TODO: Can we tighten this code up a little? */
379 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
380 enum fdt_compat_id id, int *node_list, int maxcount)
382 int name_len = strlen(name);
390 /* find the alias node if present */
391 alias_node = fdt_path_offset(blob, "/aliases");
394 * start with nothing, and we can assume that the root node can't
397 memset(nodes, '\0', sizeof(nodes));
399 /* First find all the compatible nodes */
400 for (node = count = 0; node >= 0 && count < maxcount;) {
401 node = fdtdec_next_compatible(blob, node, id);
403 nodes[count++] = node;
406 debug("%s: warning: maxcount exceeded with alias '%s'\n",
409 /* Now find all the aliases */
410 for (offset = fdt_first_property_offset(blob, alias_node);
412 offset = fdt_next_property_offset(blob, offset)) {
413 const struct fdt_property *prop;
419 prop = fdt_get_property_by_offset(blob, offset, NULL);
420 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
421 if (prop->len && 0 == strncmp(path, name, name_len))
422 node = fdt_path_offset(blob, prop->data);
426 /* Get the alias number */
427 number = simple_strtoul(path + name_len, NULL, 10);
428 if (number < 0 || number >= maxcount) {
429 debug("%s: warning: alias '%s' is out of range\n",
434 /* Make sure the node we found is actually in our list! */
436 for (j = 0; j < count; j++)
437 if (nodes[j] == node) {
443 debug("%s: warning: alias '%s' points to a node "
444 "'%s' that is missing or is not compatible "
445 " with '%s'\n", __func__, path,
446 fdt_get_name(blob, node, NULL),
452 * Add this node to our list in the right place, and mark
455 if (fdtdec_get_is_enabled(blob, node)) {
456 if (node_list[number]) {
457 debug("%s: warning: alias '%s' requires that "
458 "a node be placed in the list in a "
459 "position which is already filled by "
460 "node '%s'\n", __func__, path,
461 fdt_get_name(blob, node, NULL));
464 node_list[number] = node;
465 if (number >= num_found)
466 num_found = number + 1;
471 /* Add any nodes not mentioned by an alias */
472 for (i = j = 0; i < maxcount; i++) {
474 for (; j < maxcount; j++)
476 fdtdec_get_is_enabled(blob, nodes[j]))
479 /* Have we run out of nodes to add? */
483 assert(!node_list[i]);
484 node_list[i] = nodes[j++];
493 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
496 int base_len = strlen(base);
497 const char *find_name;
502 find_name = fdt_get_name(blob, offset, &find_namelen);
503 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
505 aliases = fdt_path_offset(blob, "/aliases");
506 for (prop_offset = fdt_first_property_offset(blob, aliases);
508 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
515 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
516 debug(" - %s, %s\n", name, prop);
517 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
518 strncmp(name, base, base_len))
521 slash = strrchr(prop, '/');
522 if (strcmp(slash + 1, find_name))
524 for (p = name + strlen(name) - 1; p > name; p--) {
526 *seqp = simple_strtoul(p + 1, NULL, 10);
527 debug("Found seq %d\n", *seqp);
533 debug("Not found\n");
537 int fdtdec_get_chosen_node(const void *blob, const char *name)
544 return -FDT_ERR_NOTFOUND;
545 chosen_node = fdt_path_offset(blob, "/chosen");
546 prop = fdt_getprop(blob, chosen_node, name, &len);
548 return -FDT_ERR_NOTFOUND;
549 return fdt_path_offset(blob, prop);
552 int fdtdec_check_fdt(void)
555 * We must have an FDT, but we cannot panic() yet since the console
556 * is not ready. So for now, just assert(). Boards which need an early
557 * FDT (prior to console ready) will need to make their own
558 * arrangements and do their own checks.
560 assert(!fdtdec_prepare_fdt());
565 * This function is a little odd in that it accesses global data. At some
566 * point if the architecture board.c files merge this will make more sense.
567 * Even now, it is common code.
569 int fdtdec_prepare_fdt(void)
571 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
572 fdt_check_header(gd->fdt_blob)) {
573 printf("No valid FDT found - please append one to U-Boot "
574 "binary, use u-boot-dtb.bin or define "
575 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
581 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
586 debug("%s: %s\n", __func__, prop_name);
587 phandle = fdt_getprop(blob, node, prop_name, NULL);
589 return -FDT_ERR_NOTFOUND;
591 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
596 * Look up a property in a node and check that it has a minimum length.
598 * @param blob FDT blob
599 * @param node node to examine
600 * @param prop_name name of property to find
601 * @param min_len minimum property length in bytes
602 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
603 found, or -FDT_ERR_BADLAYOUT if not enough data
604 * @return pointer to cell, which is only valid if err == 0
606 static const void *get_prop_check_min_len(const void *blob, int node,
607 const char *prop_name, int min_len, int *err)
612 debug("%s: %s\n", __func__, prop_name);
613 cell = fdt_getprop(blob, node, prop_name, &len);
615 *err = -FDT_ERR_NOTFOUND;
616 else if (len < min_len)
617 *err = -FDT_ERR_BADLAYOUT;
623 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
624 u32 *array, int count)
629 debug("%s: %s\n", __func__, prop_name);
630 cell = get_prop_check_min_len(blob, node, prop_name,
631 sizeof(u32) * count, &err);
633 for (i = 0; i < count; i++)
634 array[i] = fdt32_to_cpu(cell[i]);
639 int fdtdec_get_int_array_count(const void *blob, int node,
640 const char *prop_name, u32 *array, int count)
646 debug("%s: %s\n", __func__, prop_name);
647 cell = fdt_getprop(blob, node, prop_name, &len);
649 return -FDT_ERR_NOTFOUND;
650 elems = len / sizeof(u32);
653 for (i = 0; i < count; i++)
654 array[i] = fdt32_to_cpu(cell[i]);
659 const u32 *fdtdec_locate_array(const void *blob, int node,
660 const char *prop_name, int count)
665 cell = get_prop_check_min_len(blob, node, prop_name,
666 sizeof(u32) * count, &err);
667 return err ? NULL : cell;
670 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
675 debug("%s: %s\n", __func__, prop_name);
676 cell = fdt_getprop(blob, node, prop_name, &len);
680 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
681 const char *list_name,
682 const char *cells_name,
683 int cell_count, int index,
684 struct fdtdec_phandle_args *out_args)
686 const __be32 *list, *list_end;
687 int rc = 0, size, cur_index = 0;
692 /* Retrieve the phandle list property */
693 list = fdt_getprop(blob, src_node, list_name, &size);
696 list_end = list + size / sizeof(*list);
698 /* Loop over the phandles until all the requested entry is found */
699 while (list < list_end) {
704 * If phandle is 0, then it is an empty entry with no
705 * arguments. Skip forward to the next entry.
707 phandle = be32_to_cpup(list++);
710 * Find the provider node and parse the #*-cells
711 * property to determine the argument length.
713 * This is not needed if the cell count is hard-coded
714 * (i.e. cells_name not set, but cell_count is set),
715 * except when we're going to return the found node
718 if (cells_name || cur_index == index) {
719 node = fdt_node_offset_by_phandle(blob,
722 debug("%s: could not find phandle\n",
723 fdt_get_name(blob, src_node,
730 count = fdtdec_get_int(blob, node, cells_name,
733 debug("%s: could not get %s for %s\n",
734 fdt_get_name(blob, src_node,
737 fdt_get_name(blob, node,
746 * Make sure that the arguments actually fit in the
747 * remaining property data length
749 if (list + count > list_end) {
750 debug("%s: arguments longer than property\n",
751 fdt_get_name(blob, src_node, NULL));
757 * All of the error cases above bail out of the loop, so at
758 * this point, the parsing is successful. If the requested
759 * index matches, then fill the out_args structure and return,
760 * or return -ENOENT for an empty entry.
763 if (cur_index == index) {
770 if (count > MAX_PHANDLE_ARGS) {
771 debug("%s: too many arguments %d\n",
772 fdt_get_name(blob, src_node,
774 count = MAX_PHANDLE_ARGS;
776 out_args->node = node;
777 out_args->args_count = count;
778 for (i = 0; i < count; i++) {
780 be32_to_cpup(list++);
784 /* Found it! return success */
794 * Result will be one of:
795 * -ENOENT : index is for empty phandle
796 * -EINVAL : parsing error on data
797 * [1..n] : Number of phandle (count mode; when index = -1)
799 rc = index < 0 ? cur_index : -ENOENT;
804 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
805 u8 *array, int count)
810 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
812 memcpy(array, cell, count);
816 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
817 const char *prop_name, int count)
822 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
828 int fdtdec_get_config_int(const void *blob, const char *prop_name,
833 debug("%s: %s\n", __func__, prop_name);
834 config_node = fdt_path_offset(blob, "/config");
837 return fdtdec_get_int(blob, config_node, prop_name, default_val);
840 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
845 debug("%s: %s\n", __func__, prop_name);
846 config_node = fdt_path_offset(blob, "/config");
849 prop = fdt_get_property(blob, config_node, prop_name, NULL);
854 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
860 debug("%s: %s\n", __func__, prop_name);
861 nodeoffset = fdt_path_offset(blob, "/config");
865 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
869 return (char *)nodep;
872 int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
873 fdt_addr_t *basep, fdt_size_t *sizep)
875 const fdt_addr_t *cell;
878 debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
880 cell = fdt_getprop(blob, node, prop_name, &len);
881 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
882 debug("cell=%p, len=%d\n", cell, len);
886 *basep = fdt_addr_to_cpu(*cell);
887 *sizep = fdt_size_to_cpu(cell[1]);
888 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
895 * Read a flash entry from the fdt
897 * @param blob FDT blob
898 * @param node Offset of node to read
899 * @param name Name of node being read
900 * @param entry Place to put offset and size of this node
901 * @return 0 if ok, -ve on error
903 int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
904 struct fmap_entry *entry)
909 if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
910 debug("Node '%s' has bad/missing 'reg' property\n", name);
911 return -FDT_ERR_NOTFOUND;
913 entry->offset = reg[0];
914 entry->length = reg[1];
915 entry->used = fdtdec_get_int(blob, node, "used", entry->length);
916 prop = fdt_getprop(blob, node, "compress", NULL);
917 entry->compress_algo = prop && !strcmp(prop, "lzo") ?
918 FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
919 prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
920 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
921 entry->hash = (uint8_t *)prop;
926 static u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
931 number = (number << 32) | fdt32_to_cpu(*ptr++);
936 int fdt_get_resource(const void *fdt, int node, const char *property,
937 unsigned int index, struct fdt_resource *res)
939 const fdt32_t *ptr, *end;
940 int na, ns, len, parent;
943 parent = fdt_parent_offset(fdt, node);
947 na = fdt_address_cells(fdt, parent);
948 ns = fdt_size_cells(fdt, parent);
950 ptr = fdt_getprop(fdt, node, property, &len);
954 end = ptr + len / sizeof(*ptr);
956 while (ptr + na + ns <= end) {
958 res->start = res->end = fdtdec_get_number(ptr, na);
959 res->end += fdtdec_get_number(&ptr[na], ns) - 1;
967 return -FDT_ERR_NOTFOUND;
970 int fdt_get_named_resource(const void *fdt, int node, const char *property,
971 const char *prop_names, const char *name,
972 struct fdt_resource *res)
976 index = fdt_find_string(fdt, node, prop_names, name);
980 return fdt_get_resource(fdt, node, property, index, res);
983 int fdtdec_decode_memory_region(const void *blob, int config_node,
984 const char *mem_type, const char *suffix,
985 fdt_addr_t *basep, fdt_size_t *sizep)
989 fdt_size_t size, offset_size;
990 fdt_addr_t base, offset;
993 if (config_node == -1) {
994 config_node = fdt_path_offset(blob, "/config");
995 if (config_node < 0) {
996 debug("%s: Cannot find /config node\n", __func__);
1003 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
1005 mem = fdt_getprop(blob, config_node, prop_name, NULL);
1007 debug("%s: No memory type for '%s', using /memory\n", __func__,
1012 node = fdt_path_offset(blob, mem);
1014 debug("%s: Failed to find node '%s': %s\n", __func__, mem,
1015 fdt_strerror(node));
1020 * Not strictly correct - the memory may have multiple banks. We just
1023 if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
1024 debug("%s: Failed to decode memory region %s\n", __func__,
1029 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1031 if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1033 debug("%s: Failed to decode memory region '%s'\n", __func__,
1038 *basep = base + offset;
1039 *sizep = offset_size;