1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
10 #include <dm/of_extra.h>
13 #include <fdt_support.h>
15 #include <linux/libfdt.h>
17 #include <asm/sections.h>
18 #include <linux/ctype.h>
19 #include <linux/lzo.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 * Here are the type we know about. One day we might allow drivers to
25 * register. For now we just put them here. The COMPAT macro allows us to
26 * turn this into a sparse list later, and keeps the ID with the name.
28 * NOTE: This list is basically a TODO list for things that need to be
29 * converted to driver model. So don't add new things here unless there is a
30 * good reason why driver-model conversion is infeasible. Examples include
31 * things which are used before driver model is available.
33 #define COMPAT(id, name) name
34 static const char * const compat_names[COMPAT_COUNT] = {
35 COMPAT(UNKNOWN, "<none>"),
36 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
37 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
38 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
39 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
40 COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
41 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
42 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
43 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
44 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
45 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
46 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
47 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
48 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
49 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
50 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
51 COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
52 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
53 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
54 COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
55 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
56 COMPAT(INTEL_MICROCODE, "intel,microcode"),
57 COMPAT(AMS_AS3722, "ams,as3722"),
58 COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
59 COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
60 COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
61 COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
62 COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
63 COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
64 COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
65 COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
66 COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
67 COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
68 COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
69 COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
70 COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
71 COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
72 COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
73 COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
74 COMPAT(ALTERA_SOCFPGA_FPGA0, "altr,socfpga-a10-fpga-mgr"),
75 COMPAT(ALTERA_SOCFPGA_NOC, "altr,socfpga-a10-noc"),
76 COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init")
79 const char *fdtdec_get_compatible(enum fdt_compat_id id)
81 /* We allow reading of the 'unknown' ID for testing purposes */
82 assert(id >= 0 && id < COMPAT_COUNT);
83 return compat_names[id];
86 fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
87 const char *prop_name, int index, int na,
88 int ns, fdt_size_t *sizep,
91 const fdt32_t *prop, *prop_end;
92 const fdt32_t *prop_addr, *prop_size, *prop_after_size;
96 debug("%s: %s: ", __func__, prop_name);
98 if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
99 debug("(na too large for fdt_addr_t type)\n");
100 return FDT_ADDR_T_NONE;
103 if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
104 debug("(ns too large for fdt_size_t type)\n");
105 return FDT_ADDR_T_NONE;
108 prop = fdt_getprop(blob, node, prop_name, &len);
110 debug("(not found)\n");
111 return FDT_ADDR_T_NONE;
113 prop_end = prop + (len / sizeof(*prop));
115 prop_addr = prop + (index * (na + ns));
116 prop_size = prop_addr + na;
117 prop_after_size = prop_size + ns;
118 if (prop_after_size > prop_end) {
119 debug("(not enough data: expected >= %d cells, got %d cells)\n",
120 (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
121 return FDT_ADDR_T_NONE;
124 #if CONFIG_IS_ENABLED(OF_TRANSLATE)
126 addr = fdt_translate_address(blob, node, prop_addr);
129 addr = fdtdec_get_number(prop_addr, na);
132 *sizep = fdtdec_get_number(prop_size, ns);
133 debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
134 (unsigned long long)*sizep);
136 debug("addr=%08llx\n", (unsigned long long)addr);
142 fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
143 int node, const char *prop_name,
144 int index, fdt_size_t *sizep,
149 debug("%s: ", __func__);
151 na = fdt_address_cells(blob, parent);
153 debug("(bad #address-cells)\n");
154 return FDT_ADDR_T_NONE;
157 ns = fdt_size_cells(blob, parent);
159 debug("(bad #size-cells)\n");
160 return FDT_ADDR_T_NONE;
163 debug("na=%d, ns=%d, ", na, ns);
165 return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
166 ns, sizep, translate);
169 fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
170 const char *prop_name, int index,
176 debug("%s: ", __func__);
178 parent = fdt_parent_offset(blob, node);
180 debug("(no parent found)\n");
181 return FDT_ADDR_T_NONE;
184 return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
185 index, sizep, translate);
188 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
189 const char *prop_name, fdt_size_t *sizep)
191 int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
193 return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
194 sizeof(fdt_addr_t) / sizeof(fdt32_t),
198 fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name)
200 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
203 #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
204 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
205 const char *prop_name, struct fdt_pci_addr *addr)
211 debug("%s: %s: ", __func__, prop_name);
214 * If we follow the pci bus bindings strictly, we should check
215 * the value of the node's parent node's #address-cells and
216 * #size-cells. They need to be 3 and 2 accordingly. However,
217 * for simplicity we skip the check here.
219 cell = fdt_getprop(blob, node, prop_name, &len);
223 if ((len % FDT_PCI_REG_SIZE) == 0) {
224 int num = len / FDT_PCI_REG_SIZE;
227 for (i = 0; i < num; i++) {
228 debug("pci address #%d: %08lx %08lx %08lx\n", i,
229 (ulong)fdt32_to_cpu(cell[0]),
230 (ulong)fdt32_to_cpu(cell[1]),
231 (ulong)fdt32_to_cpu(cell[2]));
232 if ((fdt32_to_cpu(*cell) & type) == type) {
233 addr->phys_hi = fdt32_to_cpu(cell[0]);
234 addr->phys_mid = fdt32_to_cpu(cell[1]);
235 addr->phys_lo = fdt32_to_cpu(cell[1]);
239 cell += (FDT_PCI_ADDR_CELLS +
254 debug("(not found)\n");
258 int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
260 const char *list, *end;
263 list = fdt_getprop(blob, node, "compatible", &len);
270 if (len >= strlen("pciVVVV,DDDD")) {
271 char *s = strstr(list, "pci");
274 * check if the string is something like pciVVVV,DDDD.RR
275 * or just pciVVVV,DDDD
277 if (s && s[7] == ',' &&
278 (s[12] == '.' || s[12] == 0)) {
280 *vendor = simple_strtol(s, NULL, 16);
283 *device = simple_strtol(s, NULL, 16);
294 int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
299 /* extract the bar number from fdt_pci_addr */
300 barnum = addr->phys_hi & 0xff;
301 if (barnum < PCI_BASE_ADDRESS_0 || barnum > PCI_CARDBUS_CIS)
304 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
305 *bar = dm_pci_read_bar32(dev, barnum);
311 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
312 uint64_t default_val)
314 const uint64_t *cell64;
317 cell64 = fdt_getprop(blob, node, prop_name, &length);
318 if (!cell64 || length < sizeof(*cell64))
321 return fdt64_to_cpu(*cell64);
324 int fdtdec_get_is_enabled(const void *blob, int node)
329 * It should say "okay", so only allow that. Some fdts use "ok" but
330 * this is a bug. Please fix your device tree source file. See here
333 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
335 cell = fdt_getprop(blob, node, "status", NULL);
337 return strcmp(cell, "okay") == 0;
341 enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
343 enum fdt_compat_id id;
345 /* Search our drivers */
346 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
347 if (fdt_node_check_compatible(blob, node,
348 compat_names[id]) == 0)
350 return COMPAT_UNKNOWN;
353 int fdtdec_next_compatible(const void *blob, int node, enum fdt_compat_id id)
355 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
358 int fdtdec_next_compatible_subnode(const void *blob, int node,
359 enum fdt_compat_id id, int *depthp)
362 node = fdt_next_node(blob, node, depthp);
363 } while (*depthp > 1);
365 /* If this is a direct subnode, and compatible, return it */
366 if (*depthp == 1 && 0 == fdt_node_check_compatible(
367 blob, node, compat_names[id]))
370 return -FDT_ERR_NOTFOUND;
373 int fdtdec_next_alias(const void *blob, const char *name, enum fdt_compat_id id,
376 #define MAX_STR_LEN 20
377 char str[MAX_STR_LEN + 20];
380 /* snprintf() is not available */
381 assert(strlen(name) < MAX_STR_LEN);
382 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
383 node = fdt_path_offset(blob, str);
386 err = fdt_node_check_compatible(blob, node, compat_names[id]);
390 return -FDT_ERR_NOTFOUND;
395 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
396 enum fdt_compat_id id, int *node_list,
399 memset(node_list, '\0', sizeof(*node_list) * maxcount);
401 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
404 /* TODO: Can we tighten this code up a little? */
405 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
406 enum fdt_compat_id id, int *node_list,
409 int name_len = strlen(name);
417 /* find the alias node if present */
418 alias_node = fdt_path_offset(blob, "/aliases");
421 * start with nothing, and we can assume that the root node can't
424 memset(nodes, '\0', sizeof(nodes));
426 /* First find all the compatible nodes */
427 for (node = count = 0; node >= 0 && count < maxcount;) {
428 node = fdtdec_next_compatible(blob, node, id);
430 nodes[count++] = node;
433 debug("%s: warning: maxcount exceeded with alias '%s'\n",
436 /* Now find all the aliases */
437 for (offset = fdt_first_property_offset(blob, alias_node);
439 offset = fdt_next_property_offset(blob, offset)) {
440 const struct fdt_property *prop;
446 prop = fdt_get_property_by_offset(blob, offset, NULL);
447 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
448 if (prop->len && 0 == strncmp(path, name, name_len))
449 node = fdt_path_offset(blob, prop->data);
453 /* Get the alias number */
454 number = simple_strtoul(path + name_len, NULL, 10);
455 if (number < 0 || number >= maxcount) {
456 debug("%s: warning: alias '%s' is out of range\n",
461 /* Make sure the node we found is actually in our list! */
463 for (j = 0; j < count; j++)
464 if (nodes[j] == node) {
470 debug("%s: warning: alias '%s' points to a node "
471 "'%s' that is missing or is not compatible "
472 " with '%s'\n", __func__, path,
473 fdt_get_name(blob, node, NULL),
479 * Add this node to our list in the right place, and mark
482 if (fdtdec_get_is_enabled(blob, node)) {
483 if (node_list[number]) {
484 debug("%s: warning: alias '%s' requires that "
485 "a node be placed in the list in a "
486 "position which is already filled by "
487 "node '%s'\n", __func__, path,
488 fdt_get_name(blob, node, NULL));
491 node_list[number] = node;
492 if (number >= num_found)
493 num_found = number + 1;
498 /* Add any nodes not mentioned by an alias */
499 for (i = j = 0; i < maxcount; i++) {
501 for (; j < maxcount; j++)
503 fdtdec_get_is_enabled(blob, nodes[j]))
506 /* Have we run out of nodes to add? */
510 assert(!node_list[i]);
511 node_list[i] = nodes[j++];
520 int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
523 int base_len = strlen(base);
524 const char *find_name;
529 find_name = fdt_get_name(blob, offset, &find_namelen);
530 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
532 aliases = fdt_path_offset(blob, "/aliases");
533 for (prop_offset = fdt_first_property_offset(blob, aliases);
535 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
541 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
542 debug(" - %s, %s\n", name, prop);
543 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
544 strncmp(name, base, base_len))
547 slash = strrchr(prop, '/');
548 if (strcmp(slash + 1, find_name))
550 val = trailing_strtol(name);
553 debug("Found seq %d\n", *seqp);
558 debug("Not found\n");
562 const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
568 chosen_node = fdt_path_offset(blob, "/chosen");
569 return fdt_getprop(blob, chosen_node, name, NULL);
572 int fdtdec_get_chosen_node(const void *blob, const char *name)
576 prop = fdtdec_get_chosen_prop(blob, name);
578 return -FDT_ERR_NOTFOUND;
579 return fdt_path_offset(blob, prop);
582 int fdtdec_check_fdt(void)
585 * We must have an FDT, but we cannot panic() yet since the console
586 * is not ready. So for now, just assert(). Boards which need an early
587 * FDT (prior to console ready) will need to make their own
588 * arrangements and do their own checks.
590 assert(!fdtdec_prepare_fdt());
595 * This function is a little odd in that it accesses global data. At some
596 * point if the architecture board.c files merge this will make more sense.
597 * Even now, it is common code.
599 int fdtdec_prepare_fdt(void)
601 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
602 fdt_check_header(gd->fdt_blob)) {
603 #ifdef CONFIG_SPL_BUILD
604 puts("Missing DTB\n");
606 puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
609 printf("fdt_blob=%p\n", gd->fdt_blob);
610 print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
620 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
625 debug("%s: %s\n", __func__, prop_name);
626 phandle = fdt_getprop(blob, node, prop_name, NULL);
628 return -FDT_ERR_NOTFOUND;
630 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
635 * Look up a property in a node and check that it has a minimum length.
637 * @param blob FDT blob
638 * @param node node to examine
639 * @param prop_name name of property to find
640 * @param min_len minimum property length in bytes
641 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
642 found, or -FDT_ERR_BADLAYOUT if not enough data
643 * @return pointer to cell, which is only valid if err == 0
645 static const void *get_prop_check_min_len(const void *blob, int node,
646 const char *prop_name, int min_len,
652 debug("%s: %s\n", __func__, prop_name);
653 cell = fdt_getprop(blob, node, prop_name, &len);
655 *err = -FDT_ERR_NOTFOUND;
656 else if (len < min_len)
657 *err = -FDT_ERR_BADLAYOUT;
663 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
664 u32 *array, int count)
669 debug("%s: %s\n", __func__, prop_name);
670 cell = get_prop_check_min_len(blob, node, prop_name,
671 sizeof(u32) * count, &err);
675 for (i = 0; i < count; i++)
676 array[i] = fdt32_to_cpu(cell[i]);
681 int fdtdec_get_int_array_count(const void *blob, int node,
682 const char *prop_name, u32 *array, int count)
688 debug("%s: %s\n", __func__, prop_name);
689 cell = fdt_getprop(blob, node, prop_name, &len);
691 return -FDT_ERR_NOTFOUND;
692 elems = len / sizeof(u32);
695 for (i = 0; i < count; i++)
696 array[i] = fdt32_to_cpu(cell[i]);
701 const u32 *fdtdec_locate_array(const void *blob, int node,
702 const char *prop_name, int count)
707 cell = get_prop_check_min_len(blob, node, prop_name,
708 sizeof(u32) * count, &err);
709 return err ? NULL : cell;
712 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
717 debug("%s: %s\n", __func__, prop_name);
718 cell = fdt_getprop(blob, node, prop_name, &len);
722 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
723 const char *list_name,
724 const char *cells_name,
725 int cell_count, int index,
726 struct fdtdec_phandle_args *out_args)
728 const __be32 *list, *list_end;
729 int rc = 0, size, cur_index = 0;
734 /* Retrieve the phandle list property */
735 list = fdt_getprop(blob, src_node, list_name, &size);
738 list_end = list + size / sizeof(*list);
740 /* Loop over the phandles until all the requested entry is found */
741 while (list < list_end) {
746 * If phandle is 0, then it is an empty entry with no
747 * arguments. Skip forward to the next entry.
749 phandle = be32_to_cpup(list++);
752 * Find the provider node and parse the #*-cells
753 * property to determine the argument length.
755 * This is not needed if the cell count is hard-coded
756 * (i.e. cells_name not set, but cell_count is set),
757 * except when we're going to return the found node
760 if (cells_name || cur_index == index) {
761 node = fdt_node_offset_by_phandle(blob,
764 debug("%s: could not find phandle\n",
765 fdt_get_name(blob, src_node,
772 count = fdtdec_get_int(blob, node, cells_name,
775 debug("%s: could not get %s for %s\n",
776 fdt_get_name(blob, src_node,
779 fdt_get_name(blob, node,
788 * Make sure that the arguments actually fit in the
789 * remaining property data length
791 if (list + count > list_end) {
792 debug("%s: arguments longer than property\n",
793 fdt_get_name(blob, src_node, NULL));
799 * All of the error cases above bail out of the loop, so at
800 * this point, the parsing is successful. If the requested
801 * index matches, then fill the out_args structure and return,
802 * or return -ENOENT for an empty entry.
805 if (cur_index == index) {
812 if (count > MAX_PHANDLE_ARGS) {
813 debug("%s: too many arguments %d\n",
814 fdt_get_name(blob, src_node,
816 count = MAX_PHANDLE_ARGS;
818 out_args->node = node;
819 out_args->args_count = count;
820 for (i = 0; i < count; i++) {
822 be32_to_cpup(list++);
826 /* Found it! return success */
836 * Result will be one of:
837 * -ENOENT : index is for empty phandle
838 * -EINVAL : parsing error on data
839 * [1..n] : Number of phandle (count mode; when index = -1)
841 rc = index < 0 ? cur_index : -ENOENT;
846 int fdtdec_get_child_count(const void *blob, int node)
851 fdt_for_each_subnode(subnode, blob, node)
857 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
858 u8 *array, int count)
863 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
865 memcpy(array, cell, count);
869 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
870 const char *prop_name, int count)
875 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
881 int fdtdec_get_config_int(const void *blob, const char *prop_name,
886 debug("%s: %s\n", __func__, prop_name);
887 config_node = fdt_path_offset(blob, "/config");
890 return fdtdec_get_int(blob, config_node, prop_name, default_val);
893 int fdtdec_get_config_bool(const void *blob, const char *prop_name)
898 debug("%s: %s\n", __func__, prop_name);
899 config_node = fdt_path_offset(blob, "/config");
902 prop = fdt_get_property(blob, config_node, prop_name, NULL);
907 char *fdtdec_get_config_string(const void *blob, const char *prop_name)
913 debug("%s: %s\n", __func__, prop_name);
914 nodeoffset = fdt_path_offset(blob, "/config");
918 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
922 return (char *)nodep;
925 int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
926 fdt_addr_t *basep, fdt_size_t *sizep)
928 const fdt_addr_t *cell;
931 debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
933 cell = fdt_getprop(blob, node, prop_name, &len);
934 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
935 debug("cell=%p, len=%d\n", cell, len);
939 *basep = fdt_addr_to_cpu(*cell);
940 *sizep = fdt_size_to_cpu(cell[1]);
941 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
947 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
952 number = (number << 32) | fdt32_to_cpu(*ptr++);
957 int fdt_get_resource(const void *fdt, int node, const char *property,
958 unsigned int index, struct fdt_resource *res)
960 const fdt32_t *ptr, *end;
961 int na, ns, len, parent;
964 parent = fdt_parent_offset(fdt, node);
968 na = fdt_address_cells(fdt, parent);
969 ns = fdt_size_cells(fdt, parent);
971 ptr = fdt_getprop(fdt, node, property, &len);
975 end = ptr + len / sizeof(*ptr);
977 while (ptr + na + ns <= end) {
979 res->start = fdtdec_get_number(ptr, na);
980 res->end = res->start;
981 res->end += fdtdec_get_number(&ptr[na], ns) - 1;
989 return -FDT_ERR_NOTFOUND;
992 int fdt_get_named_resource(const void *fdt, int node, const char *property,
993 const char *prop_names, const char *name,
994 struct fdt_resource *res)
998 index = fdt_stringlist_search(fdt, node, prop_names, name);
1002 return fdt_get_resource(fdt, node, property, index, res);
1005 int fdtdec_decode_memory_region(const void *blob, int config_node,
1006 const char *mem_type, const char *suffix,
1007 fdt_addr_t *basep, fdt_size_t *sizep)
1011 fdt_size_t size, offset_size;
1012 fdt_addr_t base, offset;
1015 if (config_node == -1) {
1016 config_node = fdt_path_offset(blob, "/config");
1017 if (config_node < 0) {
1018 debug("%s: Cannot find /config node\n", __func__);
1025 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
1027 mem = fdt_getprop(blob, config_node, prop_name, NULL);
1029 debug("%s: No memory type for '%s', using /memory\n", __func__,
1034 node = fdt_path_offset(blob, mem);
1036 debug("%s: Failed to find node '%s': %s\n", __func__, mem,
1037 fdt_strerror(node));
1042 * Not strictly correct - the memory may have multiple banks. We just
1045 if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
1046 debug("%s: Failed to decode memory region %s\n", __func__,
1051 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1053 if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1055 debug("%s: Failed to decode memory region '%s'\n", __func__,
1060 *basep = base + offset;
1061 *sizep = offset_size;
1066 static int decode_timing_property(const void *blob, int node, const char *name,
1067 struct timing_entry *result)
1069 int length, ret = 0;
1072 prop = fdt_getprop(blob, node, name, &length);
1074 debug("%s: could not find property %s\n",
1075 fdt_get_name(blob, node, NULL), name);
1079 if (length == sizeof(u32)) {
1080 result->typ = fdtdec_get_int(blob, node, name, 0);
1081 result->min = result->typ;
1082 result->max = result->typ;
1084 ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
1090 int fdtdec_decode_display_timing(const void *blob, int parent, int index,
1091 struct display_timing *dt)
1093 int i, node, timings_node;
1097 timings_node = fdt_subnode_offset(blob, parent, "display-timings");
1098 if (timings_node < 0)
1099 return timings_node;
1101 for (i = 0, node = fdt_first_subnode(blob, timings_node);
1102 node > 0 && i != index;
1103 node = fdt_next_subnode(blob, node))
1109 memset(dt, 0, sizeof(*dt));
1111 ret |= decode_timing_property(blob, node, "hback-porch",
1113 ret |= decode_timing_property(blob, node, "hfront-porch",
1115 ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
1116 ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
1117 ret |= decode_timing_property(blob, node, "vback-porch",
1119 ret |= decode_timing_property(blob, node, "vfront-porch",
1121 ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
1122 ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
1123 ret |= decode_timing_property(blob, node, "clock-frequency",
1127 val = fdtdec_get_int(blob, node, "vsync-active", -1);
1129 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
1130 DISPLAY_FLAGS_VSYNC_LOW;
1132 val = fdtdec_get_int(blob, node, "hsync-active", -1);
1134 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
1135 DISPLAY_FLAGS_HSYNC_LOW;
1137 val = fdtdec_get_int(blob, node, "de-active", -1);
1139 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
1140 DISPLAY_FLAGS_DE_LOW;
1142 val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
1144 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
1145 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
1148 if (fdtdec_get_bool(blob, node, "interlaced"))
1149 dt->flags |= DISPLAY_FLAGS_INTERLACED;
1150 if (fdtdec_get_bool(blob, node, "doublescan"))
1151 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
1152 if (fdtdec_get_bool(blob, node, "doubleclk"))
1153 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
1158 int fdtdec_setup_memory_size(void)
1161 struct fdt_resource res;
1163 mem = fdt_path_offset(gd->fdt_blob, "/memory");
1165 debug("%s: Missing /memory node\n", __func__);
1169 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
1171 debug("%s: Unable to decode first memory bank\n", __func__);
1175 gd->ram_size = (phys_size_t)(res.end - res.start + 1);
1176 debug("%s: Initial DRAM size %llx\n", __func__,
1177 (unsigned long long)gd->ram_size);
1182 #if defined(CONFIG_NR_DRAM_BANKS)
1183 int fdtdec_setup_memory_banksize(void)
1185 int bank, ret, mem, reg = 0;
1186 struct fdt_resource res;
1188 mem = fdt_node_offset_by_prop_value(gd->fdt_blob, -1, "device_type",
1191 debug("%s: Missing /memory node\n", __func__);
1195 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1196 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
1197 if (ret == -FDT_ERR_NOTFOUND) {
1199 mem = fdt_node_offset_by_prop_value(gd->fdt_blob, mem,
1202 if (mem == -FDT_ERR_NOTFOUND)
1205 ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
1206 if (ret == -FDT_ERR_NOTFOUND)
1213 gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
1214 gd->bd->bi_dram[bank].size =
1215 (phys_size_t)(res.end - res.start + 1);
1217 debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
1219 (unsigned long long)gd->bd->bi_dram[bank].start,
1220 (unsigned long long)gd->bd->bi_dram[bank].size);
1227 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1228 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\
1229 CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO)
1230 static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
1232 size_t sz_out = CONFIG_SPL_MULTI_DTB_FIT_UNCOMPRESS_SZ;
1233 ulong sz_in = sz_src;
1237 if (CONFIG_IS_ENABLED(GZIP))
1238 if (gzip_parse_header(src, sz_in) < 0)
1240 if (CONFIG_IS_ENABLED(LZO))
1241 if (!lzop_is_valid_header(src))
1244 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) {
1245 dst = malloc(sz_out);
1247 puts("uncompress_blob: Unable to allocate memory\n");
1251 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA)
1252 dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR);
1258 if (CONFIG_IS_ENABLED(GZIP))
1259 rc = gunzip(dst, sz_out, (u8 *)src, &sz_in);
1260 else if (CONFIG_IS_ENABLED(LZO))
1261 rc = lzop_decompress(src, sz_in, dst, &sz_out);
1264 /* not a valid compressed blob */
1265 puts("uncompress_blob: Unable to uncompress\n");
1266 if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC))
1274 static int uncompress_blob(const void *src, ulong sz_src, void **dstp)
1281 #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
1283 * For CONFIG_OF_SEPARATE, the board may optionally implement this to
1284 * provide and/or fixup the fdt.
1286 __weak void *board_fdt_blob_setup(void)
1288 void *fdt_blob = NULL;
1289 #ifdef CONFIG_SPL_BUILD
1290 /* FDT is at end of BSS unless it is in a different memory region */
1291 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
1292 fdt_blob = (ulong *)&_image_binary_end;
1294 fdt_blob = (ulong *)&__bss_end;
1296 /* FDT is at end of image */
1297 fdt_blob = (ulong *)&_end;
1303 int fdtdec_setup(void)
1305 #if CONFIG_IS_ENABLED(OF_CONTROL)
1306 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1309 # ifdef CONFIG_OF_EMBED
1310 /* Get a pointer to the FDT */
1311 # ifdef CONFIG_SPL_BUILD
1312 gd->fdt_blob = __dtb_dt_spl_begin;
1314 gd->fdt_blob = __dtb_dt_begin;
1316 # elif defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE)
1317 /* Allow the board to override the fdt address. */
1318 gd->fdt_blob = board_fdt_blob_setup();
1319 # elif defined(CONFIG_OF_HOSTFILE)
1320 if (sandbox_read_fdt_from_file()) {
1321 puts("Failed to read control FDT\n");
1325 # ifndef CONFIG_SPL_BUILD
1326 /* Allow the early environment to override the fdt address */
1327 # if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
1328 gd->fdt_blob = (void *)prior_stage_fdt_address;
1330 gd->fdt_blob = (void *)env_get_ulong("fdtcontroladdr", 16,
1331 (uintptr_t)gd->fdt_blob);
1335 # if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1337 * Try and uncompress the blob.
1338 * Unfortunately there is no way to know how big the input blob really
1339 * is. So let us set the maximum input size arbitrarily high. 16MB
1340 * ought to be more than enough for packed DTBs.
1342 if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0)
1343 gd->fdt_blob = fdt_blob;
1346 * Check if blob is a FIT images containings DTBs.
1347 * If so, pick the most relevant
1349 fdt_blob = locate_dtb_in_fit(gd->fdt_blob);
1351 gd->fdt_blob = fdt_blob;
1355 return fdtdec_prepare_fdt();
1358 #ifdef CONFIG_NR_DRAM_BANKS
1359 int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1360 phys_addr_t *basep, phys_size_t *sizep, bd_t *bd)
1362 int addr_cells, size_cells;
1363 const u32 *cell, *end;
1364 u64 total_size, size, addr;
1370 debug("%s: board_id=%d\n", __func__, board_id);
1373 node = fdt_path_offset(blob, area);
1375 debug("No %s node found\n", area);
1379 cell = fdt_getprop(blob, node, "reg", &len);
1381 debug("No reg property found\n");
1385 addr_cells = fdt_address_cells(blob, node);
1386 size_cells = fdt_size_cells(blob, node);
1388 /* Check the board id and mask */
1389 for (child = fdt_first_subnode(blob, node);
1391 child = fdt_next_subnode(blob, child)) {
1392 int match_mask, match_value;
1394 match_mask = fdtdec_get_int(blob, child, "match-mask", -1);
1395 match_value = fdtdec_get_int(blob, child, "match-value", -1);
1397 if (match_value >= 0 &&
1398 ((board_id & match_mask) == match_value)) {
1399 /* Found matching mask */
1400 debug("Found matching mask %d\n", match_mask);
1402 cell = fdt_getprop(blob, node, "reg", &len);
1404 debug("No memory-banks property found\n");
1410 /* Note: if no matching subnode was found we use the parent node */
1413 memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) *
1414 CONFIG_NR_DRAM_BANKS);
1417 auto_size = fdtdec_get_bool(blob, node, "auto-size");
1420 end = cell + len / 4 - addr_cells - size_cells;
1421 debug("cell at %p, end %p\n", cell, end);
1422 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1426 if (addr_cells == 2)
1427 addr += (u64)fdt32_to_cpu(*cell++) << 32UL;
1428 addr += fdt32_to_cpu(*cell++);
1430 bd->bi_dram[bank].start = addr;
1432 *basep = (phys_addr_t)addr;
1435 if (size_cells == 2)
1436 size += (u64)fdt32_to_cpu(*cell++) << 32UL;
1437 size += fdt32_to_cpu(*cell++);
1442 debug("Auto-sizing %" PRIx64 ", size %" PRIx64 ": ",
1444 new_size = get_ram_size((long *)(uintptr_t)addr, size);
1445 if (new_size == size) {
1448 debug("sized to %" PRIx64 "\n", new_size);
1454 bd->bi_dram[bank].size = size;
1458 debug("Memory size %" PRIu64 "\n", total_size);
1460 *sizep = (phys_size_t)total_size;
1464 #endif /* CONFIG_NR_DRAM_BANKS */
1466 #endif /* !USE_HOSTCC */