2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/sections.h>
13 #include <linux/ctype.h>
15 DECLARE_GLOBAL_DATA_PTR
;
18 * Here are the type we know about. One day we might allow drivers to
19 * register. For now we just put them here. The COMPAT macro allows us to
20 * turn this into a sparse list later, and keeps the ID with the name.
22 #define COMPAT(id, name) name
23 static const char * const compat_names
[COMPAT_COUNT
] = {
24 COMPAT(UNKNOWN
, "<none>"),
25 COMPAT(NVIDIA_TEGRA20_EMC
, "nvidia,tegra20-emc"),
26 COMPAT(NVIDIA_TEGRA20_EMC_TABLE
, "nvidia,tegra20-emc-table"),
27 COMPAT(NVIDIA_TEGRA20_KBC
, "nvidia,tegra20-kbc"),
28 COMPAT(NVIDIA_TEGRA20_NAND
, "nvidia,tegra20-nand"),
29 COMPAT(NVIDIA_TEGRA20_PWM
, "nvidia,tegra20-pwm"),
30 COMPAT(NVIDIA_TEGRA124_DC
, "nvidia,tegra124-dc"),
31 COMPAT(NVIDIA_TEGRA124_SOR
, "nvidia,tegra124-sor"),
32 COMPAT(NVIDIA_TEGRA124_PMC
, "nvidia,tegra124-pmc"),
33 COMPAT(NVIDIA_TEGRA20_DC
, "nvidia,tegra20-dc"),
34 COMPAT(NVIDIA_TEGRA210_SDMMC
, "nvidia,tegra210-sdhci"),
35 COMPAT(NVIDIA_TEGRA124_SDMMC
, "nvidia,tegra124-sdhci"),
36 COMPAT(NVIDIA_TEGRA30_SDMMC
, "nvidia,tegra30-sdhci"),
37 COMPAT(NVIDIA_TEGRA20_SDMMC
, "nvidia,tegra20-sdhci"),
38 COMPAT(NVIDIA_TEGRA124_PCIE
, "nvidia,tegra124-pcie"),
39 COMPAT(NVIDIA_TEGRA210_PCIE
, "nvidia,tegra210-pcie"),
40 COMPAT(NVIDIA_TEGRA30_PCIE
, "nvidia,tegra30-pcie"),
41 COMPAT(NVIDIA_TEGRA20_PCIE
, "nvidia,tegra20-pcie"),
42 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL
, "nvidia,tegra124-xusb-padctl"),
43 COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL
, "nvidia,tegra210-xusb-padctl"),
44 COMPAT(SMSC_LAN9215
, "smsc,lan9215"),
45 COMPAT(SAMSUNG_EXYNOS5_SROMC
, "samsung,exynos-sromc"),
46 COMPAT(SAMSUNG_S3C2440_I2C
, "samsung,s3c2440-i2c"),
47 COMPAT(SAMSUNG_EXYNOS5_SOUND
, "samsung,exynos-sound"),
48 COMPAT(WOLFSON_WM8994_CODEC
, "wolfson,wm8994-codec"),
49 COMPAT(GOOGLE_CROS_EC_KEYB
, "google,cros-ec-keyb"),
50 COMPAT(SAMSUNG_EXYNOS_USB_PHY
, "samsung,exynos-usb-phy"),
51 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY
, "samsung,exynos5250-usb3-phy"),
52 COMPAT(SAMSUNG_EXYNOS_TMU
, "samsung,exynos-tmu"),
53 COMPAT(SAMSUNG_EXYNOS_FIMD
, "samsung,exynos-fimd"),
54 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI
, "samsung,exynos-mipi-dsi"),
55 COMPAT(SAMSUNG_EXYNOS5_DP
, "samsung,exynos5-dp"),
56 COMPAT(SAMSUNG_EXYNOS_DWMMC
, "samsung,exynos-dwmmc"),
57 COMPAT(SAMSUNG_EXYNOS_MMC
, "samsung,exynos-mmc"),
58 COMPAT(SAMSUNG_EXYNOS_SERIAL
, "samsung,exynos4210-uart"),
59 COMPAT(MAXIM_MAX77686_PMIC
, "maxim,max77686"),
60 COMPAT(GENERIC_SPI_FLASH
, "spi-flash"),
61 COMPAT(MAXIM_98095_CODEC
, "maxim,max98095-codec"),
62 COMPAT(SAMSUNG_EXYNOS5_I2C
, "samsung,exynos5-hsi2c"),
63 COMPAT(SANDBOX_LCD_SDL
, "sandbox,lcd-sdl"),
64 COMPAT(SAMSUNG_EXYNOS_SYSMMU
, "samsung,sysmmu-v3.3"),
65 COMPAT(INTEL_MICROCODE
, "intel,microcode"),
66 COMPAT(MEMORY_SPD
, "memory-spd"),
67 COMPAT(INTEL_PANTHERPOINT_AHCI
, "intel,pantherpoint-ahci"),
68 COMPAT(INTEL_MODEL_206AX
, "intel,model-206ax"),
69 COMPAT(INTEL_GMA
, "intel,gma"),
70 COMPAT(AMS_AS3722
, "ams,as3722"),
71 COMPAT(INTEL_ICH_SPI
, "intel,ich-spi"),
72 COMPAT(INTEL_QRK_MRC
, "intel,quark-mrc"),
73 COMPAT(INTEL_X86_PINCTRL
, "intel,x86-pinctrl"),
74 COMPAT(SOCIONEXT_XHCI
, "socionext,uniphier-xhci"),
75 COMPAT(COMPAT_INTEL_PCH
, "intel,bd82x6x"),
76 COMPAT(COMPAT_INTEL_IRQ_ROUTER
, "intel,irq-router"),
77 COMPAT(ALTERA_SOCFPGA_DWMAC
, "altr,socfpga-stmmac"),
78 COMPAT(ALTERA_SOCFPGA_DWMMC
, "altr,socfpga-dw-mshc"),
79 COMPAT(COMPAT_INTEL_BAYTRAIL_FSP
, "intel,baytrail-fsp"),
80 COMPAT(COMPAT_INTEL_BAYTRAIL_FSP_MDP
, "intel,baytrail-fsp-mdp"),
83 const char *fdtdec_get_compatible(enum fdt_compat_id id
)
85 /* We allow reading of the 'unknown' ID for testing purposes */
86 assert(id
>= 0 && id
< COMPAT_COUNT
);
87 return compat_names
[id
];
90 fdt_addr_t
fdtdec_get_addr_size_fixed(const void *blob
, int node
,
91 const char *prop_name
, int index
, int na
, int ns
,
94 const fdt32_t
*prop
, *prop_end
;
95 const fdt32_t
*prop_addr
, *prop_size
, *prop_after_size
;
99 debug("%s: %s: ", __func__
, prop_name
);
101 if (na
> (sizeof(fdt_addr_t
) / sizeof(fdt32_t
))) {
102 debug("(na too large for fdt_addr_t type)\n");
103 return FDT_ADDR_T_NONE
;
106 if (ns
> (sizeof(fdt_size_t
) / sizeof(fdt32_t
))) {
107 debug("(ns too large for fdt_size_t type)\n");
108 return FDT_ADDR_T_NONE
;
111 prop
= fdt_getprop(blob
, node
, prop_name
, &len
);
113 debug("(not found)\n");
114 return FDT_ADDR_T_NONE
;
116 prop_end
= prop
+ (len
/ sizeof(*prop
));
118 prop_addr
= prop
+ (index
* (na
+ ns
));
119 prop_size
= prop_addr
+ na
;
120 prop_after_size
= prop_size
+ ns
;
121 if (prop_after_size
> prop_end
) {
122 debug("(not enough data: expected >= %d cells, got %d cells)\n",
123 (u32
)(prop_after_size
- prop
), ((u32
)(prop_end
- prop
)));
124 return FDT_ADDR_T_NONE
;
127 addr
= fdtdec_get_number(prop_addr
, na
);
130 *sizep
= fdtdec_get_number(prop_size
, ns
);
131 debug("addr=%08llx, size=%llx\n", (u64
)addr
, (u64
)*sizep
);
133 debug("addr=%08llx\n", (u64
)addr
);
139 fdt_addr_t
fdtdec_get_addr_size_auto_parent(const void *blob
, int parent
,
140 int node
, const char *prop_name
, int index
, fdt_size_t
*sizep
)
144 debug("%s: ", __func__
);
146 na
= fdt_address_cells(blob
, parent
);
148 debug("(bad #address-cells)\n");
149 return FDT_ADDR_T_NONE
;
152 ns
= fdt_size_cells(blob
, parent
);
154 debug("(bad #size-cells)\n");
155 return FDT_ADDR_T_NONE
;
158 debug("na=%d, ns=%d, ", na
, ns
);
160 return fdtdec_get_addr_size_fixed(blob
, node
, prop_name
, index
, na
,
164 fdt_addr_t
fdtdec_get_addr_size_auto_noparent(const void *blob
, int node
,
165 const char *prop_name
, int index
, fdt_size_t
*sizep
)
169 debug("%s: ", __func__
);
171 parent
= fdt_parent_offset(blob
, node
);
173 debug("(no parent found)\n");
174 return FDT_ADDR_T_NONE
;
177 return fdtdec_get_addr_size_auto_parent(blob
, parent
, node
, prop_name
,
181 fdt_addr_t
fdtdec_get_addr_size(const void *blob
, int node
,
182 const char *prop_name
, fdt_size_t
*sizep
)
184 int ns
= sizep
? (sizeof(fdt_size_t
) / sizeof(fdt32_t
)) : 0;
186 return fdtdec_get_addr_size_fixed(blob
, node
, prop_name
, 0,
187 sizeof(fdt_addr_t
) / sizeof(fdt32_t
),
191 fdt_addr_t
fdtdec_get_addr(const void *blob
, int node
,
192 const char *prop_name
)
194 return fdtdec_get_addr_size(blob
, node
, prop_name
, NULL
);
198 int fdtdec_get_pci_addr(const void *blob
, int node
, enum fdt_pci_space type
,
199 const char *prop_name
, struct fdt_pci_addr
*addr
)
205 debug("%s: %s: ", __func__
, prop_name
);
208 * If we follow the pci bus bindings strictly, we should check
209 * the value of the node's parent node's #address-cells and
210 * #size-cells. They need to be 3 and 2 accordingly. However,
211 * for simplicity we skip the check here.
213 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
217 if ((len
% FDT_PCI_REG_SIZE
) == 0) {
218 int num
= len
/ FDT_PCI_REG_SIZE
;
221 for (i
= 0; i
< num
; i
++) {
222 debug("pci address #%d: %08lx %08lx %08lx\n", i
,
223 (ulong
)fdt32_to_cpu(cell
[0]),
224 (ulong
)fdt32_to_cpu(cell
[1]),
225 (ulong
)fdt32_to_cpu(cell
[2]));
226 if ((fdt32_to_cpu(*cell
) & type
) == type
) {
227 addr
->phys_hi
= fdt32_to_cpu(cell
[0]);
228 addr
->phys_mid
= fdt32_to_cpu(cell
[1]);
229 addr
->phys_lo
= fdt32_to_cpu(cell
[1]);
232 cell
+= (FDT_PCI_ADDR_CELLS
+
248 debug("(not found)\n");
252 int fdtdec_get_pci_vendev(const void *blob
, int node
, u16
*vendor
, u16
*device
)
254 const char *list
, *end
;
257 list
= fdt_getprop(blob
, node
, "compatible", &len
);
266 if (len
>= strlen("pciVVVV,DDDD")) {
267 s
= strstr(list
, "pci");
270 * check if the string is something like pciVVVV,DDDD.RR
271 * or just pciVVVV,DDDD
273 if (s
&& s
[7] == ',' &&
274 (s
[12] == '.' || s
[12] == 0)) {
276 *vendor
= simple_strtol(s
, NULL
, 16);
279 *device
= simple_strtol(s
, NULL
, 16);
290 int fdtdec_get_pci_bdf(const void *blob
, int node
,
291 struct fdt_pci_addr
*addr
, pci_dev_t
*bdf
)
293 u16 dt_vendor
, dt_device
, vendor
, device
;
296 /* get vendor id & device id from the compatible string */
297 ret
= fdtdec_get_pci_vendev(blob
, node
, &dt_vendor
, &dt_device
);
301 /* extract the bdf from fdt_pci_addr */
302 *bdf
= addr
->phys_hi
& 0xffff00;
304 /* read vendor id & device id based on bdf */
305 pci_read_config_word(*bdf
, PCI_VENDOR_ID
, &vendor
);
306 pci_read_config_word(*bdf
, PCI_DEVICE_ID
, &device
);
309 * Note there are two places in the device tree to fully describe
310 * a pci device: one is via compatible string with a format of
311 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
312 * the device node's reg address property. We read the vendor id
313 * and device id based on bdf and compare the values with the
314 * "VVVV,DDDD". If they are the same, then we are good to use bdf
315 * to read device's bar. But if they are different, we have to rely
316 * on the vendor id and device id extracted from the compatible
317 * string and locate the real bdf by pci_find_device(). This is
318 * because normally we may only know device's device number and
319 * function number when writing device tree. The bus number is
320 * dynamically assigned during the pci enumeration process.
322 if ((dt_vendor
!= vendor
) || (dt_device
!= device
)) {
323 *bdf
= pci_find_device(dt_vendor
, dt_device
, 0);
331 int fdtdec_get_pci_bar32(const void *blob
, int node
,
332 struct fdt_pci_addr
*addr
, u32
*bar
)
338 /* get pci devices's bdf */
339 ret
= fdtdec_get_pci_bdf(blob
, node
, addr
, &bdf
);
343 /* extract the bar number from fdt_pci_addr */
344 barnum
= addr
->phys_hi
& 0xff;
345 if ((barnum
< PCI_BASE_ADDRESS_0
) || (barnum
> PCI_CARDBUS_CIS
))
348 barnum
= (barnum
- PCI_BASE_ADDRESS_0
) / 4;
349 *bar
= pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf
)), bdf
, barnum
);
355 uint64_t fdtdec_get_uint64(const void *blob
, int node
, const char *prop_name
,
356 uint64_t default_val
)
358 const uint64_t *cell64
;
361 cell64
= fdt_getprop(blob
, node
, prop_name
, &length
);
362 if (!cell64
|| length
< sizeof(*cell64
))
365 return fdt64_to_cpu(*cell64
);
368 int fdtdec_get_is_enabled(const void *blob
, int node
)
373 * It should say "okay", so only allow that. Some fdts use "ok" but
374 * this is a bug. Please fix your device tree source file. See here
377 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
379 cell
= fdt_getprop(blob
, node
, "status", NULL
);
381 return 0 == strcmp(cell
, "okay");
385 enum fdt_compat_id
fdtdec_lookup(const void *blob
, int node
)
387 enum fdt_compat_id id
;
389 /* Search our drivers */
390 for (id
= COMPAT_UNKNOWN
; id
< COMPAT_COUNT
; id
++)
391 if (0 == fdt_node_check_compatible(blob
, node
,
394 return COMPAT_UNKNOWN
;
397 int fdtdec_next_compatible(const void *blob
, int node
,
398 enum fdt_compat_id id
)
400 return fdt_node_offset_by_compatible(blob
, node
, compat_names
[id
]);
403 int fdtdec_next_compatible_subnode(const void *blob
, int node
,
404 enum fdt_compat_id id
, int *depthp
)
407 node
= fdt_next_node(blob
, node
, depthp
);
408 } while (*depthp
> 1);
410 /* If this is a direct subnode, and compatible, return it */
411 if (*depthp
== 1 && 0 == fdt_node_check_compatible(
412 blob
, node
, compat_names
[id
]))
415 return -FDT_ERR_NOTFOUND
;
418 int fdtdec_next_alias(const void *blob
, const char *name
,
419 enum fdt_compat_id id
, int *upto
)
421 #define MAX_STR_LEN 20
422 char str
[MAX_STR_LEN
+ 20];
425 /* snprintf() is not available */
426 assert(strlen(name
) < MAX_STR_LEN
);
427 sprintf(str
, "%.*s%d", MAX_STR_LEN
, name
, *upto
);
428 node
= fdt_path_offset(blob
, str
);
431 err
= fdt_node_check_compatible(blob
, node
, compat_names
[id
]);
435 return -FDT_ERR_NOTFOUND
;
440 int fdtdec_find_aliases_for_id(const void *blob
, const char *name
,
441 enum fdt_compat_id id
, int *node_list
, int maxcount
)
443 memset(node_list
, '\0', sizeof(*node_list
) * maxcount
);
445 return fdtdec_add_aliases_for_id(blob
, name
, id
, node_list
, maxcount
);
448 /* TODO: Can we tighten this code up a little? */
449 int fdtdec_add_aliases_for_id(const void *blob
, const char *name
,
450 enum fdt_compat_id id
, int *node_list
, int maxcount
)
452 int name_len
= strlen(name
);
460 /* find the alias node if present */
461 alias_node
= fdt_path_offset(blob
, "/aliases");
464 * start with nothing, and we can assume that the root node can't
467 memset(nodes
, '\0', sizeof(nodes
));
469 /* First find all the compatible nodes */
470 for (node
= count
= 0; node
>= 0 && count
< maxcount
;) {
471 node
= fdtdec_next_compatible(blob
, node
, id
);
473 nodes
[count
++] = node
;
476 debug("%s: warning: maxcount exceeded with alias '%s'\n",
479 /* Now find all the aliases */
480 for (offset
= fdt_first_property_offset(blob
, alias_node
);
482 offset
= fdt_next_property_offset(blob
, offset
)) {
483 const struct fdt_property
*prop
;
489 prop
= fdt_get_property_by_offset(blob
, offset
, NULL
);
490 path
= fdt_string(blob
, fdt32_to_cpu(prop
->nameoff
));
491 if (prop
->len
&& 0 == strncmp(path
, name
, name_len
))
492 node
= fdt_path_offset(blob
, prop
->data
);
496 /* Get the alias number */
497 number
= simple_strtoul(path
+ name_len
, NULL
, 10);
498 if (number
< 0 || number
>= maxcount
) {
499 debug("%s: warning: alias '%s' is out of range\n",
504 /* Make sure the node we found is actually in our list! */
506 for (j
= 0; j
< count
; j
++)
507 if (nodes
[j
] == node
) {
513 debug("%s: warning: alias '%s' points to a node "
514 "'%s' that is missing or is not compatible "
515 " with '%s'\n", __func__
, path
,
516 fdt_get_name(blob
, node
, NULL
),
522 * Add this node to our list in the right place, and mark
525 if (fdtdec_get_is_enabled(blob
, node
)) {
526 if (node_list
[number
]) {
527 debug("%s: warning: alias '%s' requires that "
528 "a node be placed in the list in a "
529 "position which is already filled by "
530 "node '%s'\n", __func__
, path
,
531 fdt_get_name(blob
, node
, NULL
));
534 node_list
[number
] = node
;
535 if (number
>= num_found
)
536 num_found
= number
+ 1;
541 /* Add any nodes not mentioned by an alias */
542 for (i
= j
= 0; i
< maxcount
; i
++) {
544 for (; j
< maxcount
; j
++)
546 fdtdec_get_is_enabled(blob
, nodes
[j
]))
549 /* Have we run out of nodes to add? */
553 assert(!node_list
[i
]);
554 node_list
[i
] = nodes
[j
++];
563 int fdtdec_get_alias_seq(const void *blob
, const char *base
, int offset
,
566 int base_len
= strlen(base
);
567 const char *find_name
;
572 find_name
= fdt_get_name(blob
, offset
, &find_namelen
);
573 debug("Looking for '%s' at %d, name %s\n", base
, offset
, find_name
);
575 aliases
= fdt_path_offset(blob
, "/aliases");
576 for (prop_offset
= fdt_first_property_offset(blob
, aliases
);
578 prop_offset
= fdt_next_property_offset(blob
, prop_offset
)) {
584 prop
= fdt_getprop_by_offset(blob
, prop_offset
, &name
, &len
);
585 debug(" - %s, %s\n", name
, prop
);
586 if (len
< find_namelen
|| *prop
!= '/' || prop
[len
- 1] ||
587 strncmp(name
, base
, base_len
))
590 slash
= strrchr(prop
, '/');
591 if (strcmp(slash
+ 1, find_name
))
593 val
= trailing_strtol(name
);
596 debug("Found seq %d\n", *seqp
);
601 debug("Not found\n");
605 const char *fdtdec_get_chosen_prop(const void *blob
, const char *name
)
611 chosen_node
= fdt_path_offset(blob
, "/chosen");
612 return fdt_getprop(blob
, chosen_node
, name
, NULL
);
615 int fdtdec_get_chosen_node(const void *blob
, const char *name
)
619 prop
= fdtdec_get_chosen_prop(blob
, name
);
621 return -FDT_ERR_NOTFOUND
;
622 return fdt_path_offset(blob
, prop
);
625 int fdtdec_check_fdt(void)
628 * We must have an FDT, but we cannot panic() yet since the console
629 * is not ready. So for now, just assert(). Boards which need an early
630 * FDT (prior to console ready) will need to make their own
631 * arrangements and do their own checks.
633 assert(!fdtdec_prepare_fdt());
638 * This function is a little odd in that it accesses global data. At some
639 * point if the architecture board.c files merge this will make more sense.
640 * Even now, it is common code.
642 int fdtdec_prepare_fdt(void)
644 if (!gd
->fdt_blob
|| ((uintptr_t)gd
->fdt_blob
& 3) ||
645 fdt_check_header(gd
->fdt_blob
)) {
646 #ifdef CONFIG_SPL_BUILD
647 puts("Missing DTB\n");
649 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");
652 printf("fdt_blob=%p\n", gd
->fdt_blob
);
653 print_buffer((ulong
)gd
->fdt_blob
, gd
->fdt_blob
, 4,
663 int fdtdec_lookup_phandle(const void *blob
, int node
, const char *prop_name
)
668 debug("%s: %s\n", __func__
, prop_name
);
669 phandle
= fdt_getprop(blob
, node
, prop_name
, NULL
);
671 return -FDT_ERR_NOTFOUND
;
673 lookup
= fdt_node_offset_by_phandle(blob
, fdt32_to_cpu(*phandle
));
678 * Look up a property in a node and check that it has a minimum length.
680 * @param blob FDT blob
681 * @param node node to examine
682 * @param prop_name name of property to find
683 * @param min_len minimum property length in bytes
684 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
685 found, or -FDT_ERR_BADLAYOUT if not enough data
686 * @return pointer to cell, which is only valid if err == 0
688 static const void *get_prop_check_min_len(const void *blob
, int node
,
689 const char *prop_name
, int min_len
, int *err
)
694 debug("%s: %s\n", __func__
, prop_name
);
695 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
697 *err
= -FDT_ERR_NOTFOUND
;
698 else if (len
< min_len
)
699 *err
= -FDT_ERR_BADLAYOUT
;
705 int fdtdec_get_int_array(const void *blob
, int node
, const char *prop_name
,
706 u32
*array
, int count
)
711 debug("%s: %s\n", __func__
, prop_name
);
712 cell
= get_prop_check_min_len(blob
, node
, prop_name
,
713 sizeof(u32
) * count
, &err
);
715 for (i
= 0; i
< count
; i
++)
716 array
[i
] = fdt32_to_cpu(cell
[i
]);
721 int fdtdec_get_int_array_count(const void *blob
, int node
,
722 const char *prop_name
, u32
*array
, int count
)
728 debug("%s: %s\n", __func__
, prop_name
);
729 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
731 return -FDT_ERR_NOTFOUND
;
732 elems
= len
/ sizeof(u32
);
735 for (i
= 0; i
< count
; i
++)
736 array
[i
] = fdt32_to_cpu(cell
[i
]);
741 const u32
*fdtdec_locate_array(const void *blob
, int node
,
742 const char *prop_name
, int count
)
747 cell
= get_prop_check_min_len(blob
, node
, prop_name
,
748 sizeof(u32
) * count
, &err
);
749 return err
? NULL
: cell
;
752 int fdtdec_get_bool(const void *blob
, int node
, const char *prop_name
)
757 debug("%s: %s\n", __func__
, prop_name
);
758 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
762 int fdtdec_parse_phandle_with_args(const void *blob
, int src_node
,
763 const char *list_name
,
764 const char *cells_name
,
765 int cell_count
, int index
,
766 struct fdtdec_phandle_args
*out_args
)
768 const __be32
*list
, *list_end
;
769 int rc
= 0, size
, cur_index
= 0;
774 /* Retrieve the phandle list property */
775 list
= fdt_getprop(blob
, src_node
, list_name
, &size
);
778 list_end
= list
+ size
/ sizeof(*list
);
780 /* Loop over the phandles until all the requested entry is found */
781 while (list
< list_end
) {
786 * If phandle is 0, then it is an empty entry with no
787 * arguments. Skip forward to the next entry.
789 phandle
= be32_to_cpup(list
++);
792 * Find the provider node and parse the #*-cells
793 * property to determine the argument length.
795 * This is not needed if the cell count is hard-coded
796 * (i.e. cells_name not set, but cell_count is set),
797 * except when we're going to return the found node
800 if (cells_name
|| cur_index
== index
) {
801 node
= fdt_node_offset_by_phandle(blob
,
804 debug("%s: could not find phandle\n",
805 fdt_get_name(blob
, src_node
,
812 count
= fdtdec_get_int(blob
, node
, cells_name
,
815 debug("%s: could not get %s for %s\n",
816 fdt_get_name(blob
, src_node
,
819 fdt_get_name(blob
, node
,
828 * Make sure that the arguments actually fit in the
829 * remaining property data length
831 if (list
+ count
> list_end
) {
832 debug("%s: arguments longer than property\n",
833 fdt_get_name(blob
, src_node
, NULL
));
839 * All of the error cases above bail out of the loop, so at
840 * this point, the parsing is successful. If the requested
841 * index matches, then fill the out_args structure and return,
842 * or return -ENOENT for an empty entry.
845 if (cur_index
== index
) {
852 if (count
> MAX_PHANDLE_ARGS
) {
853 debug("%s: too many arguments %d\n",
854 fdt_get_name(blob
, src_node
,
856 count
= MAX_PHANDLE_ARGS
;
858 out_args
->node
= node
;
859 out_args
->args_count
= count
;
860 for (i
= 0; i
< count
; i
++) {
862 be32_to_cpup(list
++);
866 /* Found it! return success */
876 * Result will be one of:
877 * -ENOENT : index is for empty phandle
878 * -EINVAL : parsing error on data
879 * [1..n] : Number of phandle (count mode; when index = -1)
881 rc
= index
< 0 ? cur_index
: -ENOENT
;
886 int fdtdec_get_byte_array(const void *blob
, int node
, const char *prop_name
,
887 u8
*array
, int count
)
892 cell
= get_prop_check_min_len(blob
, node
, prop_name
, count
, &err
);
894 memcpy(array
, cell
, count
);
898 const u8
*fdtdec_locate_byte_array(const void *blob
, int node
,
899 const char *prop_name
, int count
)
904 cell
= get_prop_check_min_len(blob
, node
, prop_name
, count
, &err
);
910 int fdtdec_get_config_int(const void *blob
, const char *prop_name
,
915 debug("%s: %s\n", __func__
, prop_name
);
916 config_node
= fdt_path_offset(blob
, "/config");
919 return fdtdec_get_int(blob
, config_node
, prop_name
, default_val
);
922 int fdtdec_get_config_bool(const void *blob
, const char *prop_name
)
927 debug("%s: %s\n", __func__
, prop_name
);
928 config_node
= fdt_path_offset(blob
, "/config");
931 prop
= fdt_get_property(blob
, config_node
, prop_name
, NULL
);
936 char *fdtdec_get_config_string(const void *blob
, const char *prop_name
)
942 debug("%s: %s\n", __func__
, prop_name
);
943 nodeoffset
= fdt_path_offset(blob
, "/config");
947 nodep
= fdt_getprop(blob
, nodeoffset
, prop_name
, &len
);
951 return (char *)nodep
;
954 int fdtdec_decode_region(const void *blob
, int node
, const char *prop_name
,
955 fdt_addr_t
*basep
, fdt_size_t
*sizep
)
957 const fdt_addr_t
*cell
;
960 debug("%s: %s: %s\n", __func__
, fdt_get_name(blob
, node
, NULL
),
962 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
963 if (!cell
|| (len
< sizeof(fdt_addr_t
) * 2)) {
964 debug("cell=%p, len=%d\n", cell
, len
);
968 *basep
= fdt_addr_to_cpu(*cell
);
969 *sizep
= fdt_size_to_cpu(cell
[1]);
970 debug("%s: base=%08lx, size=%lx\n", __func__
, (ulong
)*basep
,
977 * Read a flash entry from the fdt
979 * @param blob FDT blob
980 * @param node Offset of node to read
981 * @param name Name of node being read
982 * @param entry Place to put offset and size of this node
983 * @return 0 if ok, -ve on error
985 int fdtdec_read_fmap_entry(const void *blob
, int node
, const char *name
,
986 struct fmap_entry
*entry
)
991 if (fdtdec_get_int_array(blob
, node
, "reg", reg
, 2)) {
992 debug("Node '%s' has bad/missing 'reg' property\n", name
);
993 return -FDT_ERR_NOTFOUND
;
995 entry
->offset
= reg
[0];
996 entry
->length
= reg
[1];
997 entry
->used
= fdtdec_get_int(blob
, node
, "used", entry
->length
);
998 prop
= fdt_getprop(blob
, node
, "compress", NULL
);
999 entry
->compress_algo
= prop
&& !strcmp(prop
, "lzo") ?
1000 FMAP_COMPRESS_LZO
: FMAP_COMPRESS_NONE
;
1001 prop
= fdt_getprop(blob
, node
, "hash", &entry
->hash_size
);
1002 entry
->hash_algo
= prop
? FMAP_HASH_SHA256
: FMAP_HASH_NONE
;
1003 entry
->hash
= (uint8_t *)prop
;
1008 u64
fdtdec_get_number(const fdt32_t
*ptr
, unsigned int cells
)
1013 number
= (number
<< 32) | fdt32_to_cpu(*ptr
++);
1018 int fdt_get_resource(const void *fdt
, int node
, const char *property
,
1019 unsigned int index
, struct fdt_resource
*res
)
1021 const fdt32_t
*ptr
, *end
;
1022 int na
, ns
, len
, parent
;
1025 parent
= fdt_parent_offset(fdt
, node
);
1029 na
= fdt_address_cells(fdt
, parent
);
1030 ns
= fdt_size_cells(fdt
, parent
);
1032 ptr
= fdt_getprop(fdt
, node
, property
, &len
);
1036 end
= ptr
+ len
/ sizeof(*ptr
);
1038 while (ptr
+ na
+ ns
<= end
) {
1040 res
->start
= res
->end
= fdtdec_get_number(ptr
, na
);
1041 res
->end
+= fdtdec_get_number(&ptr
[na
], ns
) - 1;
1049 return -FDT_ERR_NOTFOUND
;
1052 int fdt_get_named_resource(const void *fdt
, int node
, const char *property
,
1053 const char *prop_names
, const char *name
,
1054 struct fdt_resource
*res
)
1058 index
= fdt_find_string(fdt
, node
, prop_names
, name
);
1062 return fdt_get_resource(fdt
, node
, property
, index
, res
);
1065 int fdtdec_decode_memory_region(const void *blob
, int config_node
,
1066 const char *mem_type
, const char *suffix
,
1067 fdt_addr_t
*basep
, fdt_size_t
*sizep
)
1071 fdt_size_t size
, offset_size
;
1072 fdt_addr_t base
, offset
;
1075 if (config_node
== -1) {
1076 config_node
= fdt_path_offset(blob
, "/config");
1077 if (config_node
< 0) {
1078 debug("%s: Cannot find /config node\n", __func__
);
1085 snprintf(prop_name
, sizeof(prop_name
), "%s-memory%s", mem_type
,
1087 mem
= fdt_getprop(blob
, config_node
, prop_name
, NULL
);
1089 debug("%s: No memory type for '%s', using /memory\n", __func__
,
1094 node
= fdt_path_offset(blob
, mem
);
1096 debug("%s: Failed to find node '%s': %s\n", __func__
, mem
,
1097 fdt_strerror(node
));
1102 * Not strictly correct - the memory may have multiple banks. We just
1105 if (fdtdec_decode_region(blob
, node
, "reg", &base
, &size
)) {
1106 debug("%s: Failed to decode memory region %s\n", __func__
,
1111 snprintf(prop_name
, sizeof(prop_name
), "%s-offset%s", mem_type
,
1113 if (fdtdec_decode_region(blob
, config_node
, prop_name
, &offset
,
1115 debug("%s: Failed to decode memory region '%s'\n", __func__
,
1120 *basep
= base
+ offset
;
1121 *sizep
= offset_size
;
1126 static int decode_timing_property(const void *blob
, int node
, const char *name
,
1127 struct timing_entry
*result
)
1129 int length
, ret
= 0;
1132 prop
= fdt_getprop(blob
, node
, name
, &length
);
1134 debug("%s: could not find property %s\n",
1135 fdt_get_name(blob
, node
, NULL
), name
);
1139 if (length
== sizeof(u32
)) {
1140 result
->typ
= fdtdec_get_int(blob
, node
, name
, 0);
1141 result
->min
= result
->typ
;
1142 result
->max
= result
->typ
;
1144 ret
= fdtdec_get_int_array(blob
, node
, name
, &result
->min
, 3);
1150 int fdtdec_decode_display_timing(const void *blob
, int parent
, int index
,
1151 struct display_timing
*dt
)
1153 int i
, node
, timings_node
;
1157 timings_node
= fdt_subnode_offset(blob
, parent
, "display-timings");
1158 if (timings_node
< 0)
1159 return timings_node
;
1161 for (i
= 0, node
= fdt_first_subnode(blob
, timings_node
);
1162 node
> 0 && i
!= index
;
1163 node
= fdt_next_subnode(blob
, node
))
1169 memset(dt
, 0, sizeof(*dt
));
1171 ret
|= decode_timing_property(blob
, node
, "hback-porch",
1173 ret
|= decode_timing_property(blob
, node
, "hfront-porch",
1175 ret
|= decode_timing_property(blob
, node
, "hactive", &dt
->hactive
);
1176 ret
|= decode_timing_property(blob
, node
, "hsync-len", &dt
->hsync_len
);
1177 ret
|= decode_timing_property(blob
, node
, "vback-porch",
1179 ret
|= decode_timing_property(blob
, node
, "vfront-porch",
1181 ret
|= decode_timing_property(blob
, node
, "vactive", &dt
->vactive
);
1182 ret
|= decode_timing_property(blob
, node
, "vsync-len", &dt
->vsync_len
);
1183 ret
|= decode_timing_property(blob
, node
, "clock-frequency",
1187 val
= fdtdec_get_int(blob
, node
, "vsync-active", -1);
1189 dt
->flags
|= val
? DISPLAY_FLAGS_VSYNC_HIGH
:
1190 DISPLAY_FLAGS_VSYNC_LOW
;
1192 val
= fdtdec_get_int(blob
, node
, "hsync-active", -1);
1194 dt
->flags
|= val
? DISPLAY_FLAGS_HSYNC_HIGH
:
1195 DISPLAY_FLAGS_HSYNC_LOW
;
1197 val
= fdtdec_get_int(blob
, node
, "de-active", -1);
1199 dt
->flags
|= val
? DISPLAY_FLAGS_DE_HIGH
:
1200 DISPLAY_FLAGS_DE_LOW
;
1202 val
= fdtdec_get_int(blob
, node
, "pixelclk-active", -1);
1204 dt
->flags
|= val
? DISPLAY_FLAGS_PIXDATA_POSEDGE
:
1205 DISPLAY_FLAGS_PIXDATA_NEGEDGE
;
1208 if (fdtdec_get_bool(blob
, node
, "interlaced"))
1209 dt
->flags
|= DISPLAY_FLAGS_INTERLACED
;
1210 if (fdtdec_get_bool(blob
, node
, "doublescan"))
1211 dt
->flags
|= DISPLAY_FLAGS_DOUBLESCAN
;
1212 if (fdtdec_get_bool(blob
, node
, "doubleclk"))
1213 dt
->flags
|= DISPLAY_FLAGS_DOUBLECLK
;
1218 int fdtdec_setup(void)
1220 #if CONFIG_IS_ENABLED(OF_CONTROL)
1221 # ifdef CONFIG_OF_EMBED
1222 /* Get a pointer to the FDT */
1223 gd
->fdt_blob
= __dtb_dt_begin
;
1224 # elif defined CONFIG_OF_SEPARATE
1225 # ifdef CONFIG_SPL_BUILD
1226 /* FDT is at end of BSS unless it is in a different memory region */
1227 if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS
))
1228 gd
->fdt_blob
= (ulong
*)&_image_binary_end
;
1230 gd
->fdt_blob
= (ulong
*)&__bss_end
;
1232 /* FDT is at end of image */
1233 gd
->fdt_blob
= (ulong
*)&_end
;
1235 # elif defined(CONFIG_OF_HOSTFILE)
1236 if (sandbox_read_fdt_from_file()) {
1237 puts("Failed to read control FDT\n");
1241 # ifndef CONFIG_SPL_BUILD
1242 /* Allow the early environment to override the fdt address */
1243 gd
->fdt_blob
= (void *)getenv_ulong("fdtcontroladdr", 16,
1244 (uintptr_t)gd
->fdt_blob
);
1247 return fdtdec_prepare_fdt();
1250 #endif /* !USE_HOSTCC */