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_USB
, "nvidia,tegra20-ehci"),
26 COMPAT(NVIDIA_TEGRA30_USB
, "nvidia,tegra30-ehci"),
27 COMPAT(NVIDIA_TEGRA114_USB
, "nvidia,tegra114-ehci"),
28 COMPAT(NVIDIA_TEGRA20_EMC
, "nvidia,tegra20-emc"),
29 COMPAT(NVIDIA_TEGRA20_EMC_TABLE
, "nvidia,tegra20-emc-table"),
30 COMPAT(NVIDIA_TEGRA20_KBC
, "nvidia,tegra20-kbc"),
31 COMPAT(NVIDIA_TEGRA20_NAND
, "nvidia,tegra20-nand"),
32 COMPAT(NVIDIA_TEGRA20_PWM
, "nvidia,tegra20-pwm"),
33 COMPAT(NVIDIA_TEGRA124_DC
, "nvidia,tegra124-dc"),
34 COMPAT(NVIDIA_TEGRA124_SOR
, "nvidia,tegra124-sor"),
35 COMPAT(NVIDIA_TEGRA124_PMC
, "nvidia,tegra124-pmc"),
36 COMPAT(NVIDIA_TEGRA20_DC
, "nvidia,tegra20-dc"),
37 COMPAT(NVIDIA_TEGRA124_SDMMC
, "nvidia,tegra124-sdhci"),
38 COMPAT(NVIDIA_TEGRA30_SDMMC
, "nvidia,tegra30-sdhci"),
39 COMPAT(NVIDIA_TEGRA20_SDMMC
, "nvidia,tegra20-sdhci"),
40 COMPAT(NVIDIA_TEGRA124_PCIE
, "nvidia,tegra124-pcie"),
41 COMPAT(NVIDIA_TEGRA30_PCIE
, "nvidia,tegra30-pcie"),
42 COMPAT(NVIDIA_TEGRA20_PCIE
, "nvidia,tegra20-pcie"),
43 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL
, "nvidia,tegra124-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(INFINEON_SLB9635_TPM
, "infineon,slb9635-tpm"),
63 COMPAT(INFINEON_SLB9645_TPM
, "infineon,slb9645tt"),
64 COMPAT(SAMSUNG_EXYNOS5_I2C
, "samsung,exynos5-hsi2c"),
65 COMPAT(SANDBOX_LCD_SDL
, "sandbox,lcd-sdl"),
66 COMPAT(TI_TPS65090
, "ti,tps65090"),
67 COMPAT(COMPAT_NXP_PTN3460
, "nxp,ptn3460"),
68 COMPAT(SAMSUNG_EXYNOS_SYSMMU
, "samsung,sysmmu-v3.3"),
69 COMPAT(PARADE_PS8625
, "parade,ps8625"),
70 COMPAT(INTEL_MICROCODE
, "intel,microcode"),
71 COMPAT(MEMORY_SPD
, "memory-spd"),
72 COMPAT(INTEL_PANTHERPOINT_AHCI
, "intel,pantherpoint-ahci"),
73 COMPAT(INTEL_MODEL_206AX
, "intel,model-206ax"),
74 COMPAT(INTEL_GMA
, "intel,gma"),
75 COMPAT(AMS_AS3722
, "ams,as3722"),
76 COMPAT(INTEL_ICH_SPI
, "intel,ich-spi"),
77 COMPAT(INTEL_QRK_MRC
, "intel,quark-mrc"),
78 COMPAT(INTEL_X86_PINCTRL
, "intel,x86-pinctrl"),
79 COMPAT(SOCIONEXT_XHCI
, "socionext,uniphier-xhci"),
80 COMPAT(COMPAT_INTEL_PCH
, "intel,bd82x6x"),
81 COMPAT(COMPAT_INTEL_IRQ_ROUTER
, "intel,irq-router"),
84 const char *fdtdec_get_compatible(enum fdt_compat_id id
)
86 /* We allow reading of the 'unknown' ID for testing purposes */
87 assert(id
>= 0 && id
< COMPAT_COUNT
);
88 return compat_names
[id
];
91 fdt_addr_t
fdtdec_get_addr_size(const void *blob
, int node
,
92 const char *prop_name
, fdt_size_t
*sizep
)
94 const fdt_addr_t
*cell
;
97 debug("%s: %s: ", __func__
, prop_name
);
98 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
99 if (cell
&& ((!sizep
&& len
== sizeof(fdt_addr_t
)) ||
100 len
== sizeof(fdt_addr_t
) * 2)) {
101 fdt_addr_t addr
= fdt_addr_to_cpu(*cell
);
103 const fdt_size_t
*size
;
105 size
= (fdt_size_t
*)((char *)cell
+
107 *sizep
= fdt_size_to_cpu(*size
);
108 debug("addr=%08lx, size=%08x\n",
109 (ulong
)addr
, *sizep
);
111 debug("%08lx\n", (ulong
)addr
);
115 debug("(not found)\n");
116 return FDT_ADDR_T_NONE
;
119 fdt_addr_t
fdtdec_get_addr(const void *blob
, int node
,
120 const char *prop_name
)
122 return fdtdec_get_addr_size(blob
, node
, prop_name
, NULL
);
126 int fdtdec_get_pci_addr(const void *blob
, int node
, enum fdt_pci_space type
,
127 const char *prop_name
, struct fdt_pci_addr
*addr
)
133 debug("%s: %s: ", __func__
, prop_name
);
136 * If we follow the pci bus bindings strictly, we should check
137 * the value of the node's parent node's #address-cells and
138 * #size-cells. They need to be 3 and 2 accordingly. However,
139 * for simplicity we skip the check here.
141 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
145 if ((len
% FDT_PCI_REG_SIZE
) == 0) {
146 int num
= len
/ FDT_PCI_REG_SIZE
;
149 for (i
= 0; i
< num
; i
++) {
150 debug("pci address #%d: %08lx %08lx %08lx\n", i
,
151 (ulong
)fdt_addr_to_cpu(cell
[0]),
152 (ulong
)fdt_addr_to_cpu(cell
[1]),
153 (ulong
)fdt_addr_to_cpu(cell
[2]));
154 if ((fdt_addr_to_cpu(*cell
) & type
) == type
) {
155 addr
->phys_hi
= fdt_addr_to_cpu(cell
[0]);
156 addr
->phys_mid
= fdt_addr_to_cpu(cell
[1]);
157 addr
->phys_lo
= fdt_addr_to_cpu(cell
[2]);
160 cell
+= (FDT_PCI_ADDR_CELLS
+
176 debug("(not found)\n");
180 int fdtdec_get_pci_vendev(const void *blob
, int node
, u16
*vendor
, u16
*device
)
182 const char *list
, *end
;
185 list
= fdt_getprop(blob
, node
, "compatible", &len
);
194 if (len
>= strlen("pciVVVV,DDDD")) {
195 s
= strstr(list
, "pci");
198 * check if the string is something like pciVVVV,DDDD.RR
199 * or just pciVVVV,DDDD
201 if (s
&& s
[7] == ',' &&
202 (s
[12] == '.' || s
[12] == 0)) {
204 *vendor
= simple_strtol(s
, NULL
, 16);
207 *device
= simple_strtol(s
, NULL
, 16);
219 int fdtdec_get_pci_bdf(const void *blob
, int node
,
220 struct fdt_pci_addr
*addr
, pci_dev_t
*bdf
)
222 u16 dt_vendor
, dt_device
, vendor
, device
;
225 /* get vendor id & device id from the compatible string */
226 ret
= fdtdec_get_pci_vendev(blob
, node
, &dt_vendor
, &dt_device
);
230 /* extract the bdf from fdt_pci_addr */
231 *bdf
= addr
->phys_hi
& 0xffff00;
233 /* read vendor id & device id based on bdf */
234 pci_read_config_word(*bdf
, PCI_VENDOR_ID
, &vendor
);
235 pci_read_config_word(*bdf
, PCI_DEVICE_ID
, &device
);
238 * Note there are two places in the device tree to fully describe
239 * a pci device: one is via compatible string with a format of
240 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
241 * the device node's reg address property. We read the vendor id
242 * and device id based on bdf and compare the values with the
243 * "VVVV,DDDD". If they are the same, then we are good to use bdf
244 * to read device's bar. But if they are different, we have to rely
245 * on the vendor id and device id extracted from the compatible
246 * string and locate the real bdf by pci_find_device(). This is
247 * because normally we may only know device's device number and
248 * function number when writing device tree. The bus number is
249 * dynamically assigned during the pci enumeration process.
251 if ((dt_vendor
!= vendor
) || (dt_device
!= device
)) {
252 *bdf
= pci_find_device(dt_vendor
, dt_device
, 0);
260 int fdtdec_get_pci_bar32(const void *blob
, int node
,
261 struct fdt_pci_addr
*addr
, u32
*bar
)
267 /* get pci devices's bdf */
268 ret
= fdtdec_get_pci_bdf(blob
, node
, addr
, &bdf
);
272 /* extract the bar number from fdt_pci_addr */
273 barnum
= addr
->phys_hi
& 0xff;
274 if ((barnum
< PCI_BASE_ADDRESS_0
) || (barnum
> PCI_CARDBUS_CIS
))
277 barnum
= (barnum
- PCI_BASE_ADDRESS_0
) / 4;
278 *bar
= pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf
)), bdf
, barnum
);
284 uint64_t fdtdec_get_uint64(const void *blob
, int node
, const char *prop_name
,
285 uint64_t default_val
)
287 const uint64_t *cell64
;
290 cell64
= fdt_getprop(blob
, node
, prop_name
, &length
);
291 if (!cell64
|| length
< sizeof(*cell64
))
294 return fdt64_to_cpu(*cell64
);
297 int fdtdec_get_is_enabled(const void *blob
, int node
)
302 * It should say "okay", so only allow that. Some fdts use "ok" but
303 * this is a bug. Please fix your device tree source file. See here
306 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
308 cell
= fdt_getprop(blob
, node
, "status", NULL
);
310 return 0 == strcmp(cell
, "okay");
314 enum fdt_compat_id
fdtdec_lookup(const void *blob
, int node
)
316 enum fdt_compat_id id
;
318 /* Search our drivers */
319 for (id
= COMPAT_UNKNOWN
; id
< COMPAT_COUNT
; id
++)
320 if (0 == fdt_node_check_compatible(blob
, node
,
323 return COMPAT_UNKNOWN
;
326 int fdtdec_next_compatible(const void *blob
, int node
,
327 enum fdt_compat_id id
)
329 return fdt_node_offset_by_compatible(blob
, node
, compat_names
[id
]);
332 int fdtdec_next_compatible_subnode(const void *blob
, int node
,
333 enum fdt_compat_id id
, int *depthp
)
336 node
= fdt_next_node(blob
, node
, depthp
);
337 } while (*depthp
> 1);
339 /* If this is a direct subnode, and compatible, return it */
340 if (*depthp
== 1 && 0 == fdt_node_check_compatible(
341 blob
, node
, compat_names
[id
]))
344 return -FDT_ERR_NOTFOUND
;
347 int fdtdec_next_alias(const void *blob
, const char *name
,
348 enum fdt_compat_id id
, int *upto
)
350 #define MAX_STR_LEN 20
351 char str
[MAX_STR_LEN
+ 20];
354 /* snprintf() is not available */
355 assert(strlen(name
) < MAX_STR_LEN
);
356 sprintf(str
, "%.*s%d", MAX_STR_LEN
, name
, *upto
);
357 node
= fdt_path_offset(blob
, str
);
360 err
= fdt_node_check_compatible(blob
, node
, compat_names
[id
]);
364 return -FDT_ERR_NOTFOUND
;
369 int fdtdec_find_aliases_for_id(const void *blob
, const char *name
,
370 enum fdt_compat_id id
, int *node_list
, int maxcount
)
372 memset(node_list
, '\0', sizeof(*node_list
) * maxcount
);
374 return fdtdec_add_aliases_for_id(blob
, name
, id
, node_list
, maxcount
);
377 /* TODO: Can we tighten this code up a little? */
378 int fdtdec_add_aliases_for_id(const void *blob
, const char *name
,
379 enum fdt_compat_id id
, int *node_list
, int maxcount
)
381 int name_len
= strlen(name
);
389 /* find the alias node if present */
390 alias_node
= fdt_path_offset(blob
, "/aliases");
393 * start with nothing, and we can assume that the root node can't
396 memset(nodes
, '\0', sizeof(nodes
));
398 /* First find all the compatible nodes */
399 for (node
= count
= 0; node
>= 0 && count
< maxcount
;) {
400 node
= fdtdec_next_compatible(blob
, node
, id
);
402 nodes
[count
++] = node
;
405 debug("%s: warning: maxcount exceeded with alias '%s'\n",
408 /* Now find all the aliases */
409 for (offset
= fdt_first_property_offset(blob
, alias_node
);
411 offset
= fdt_next_property_offset(blob
, offset
)) {
412 const struct fdt_property
*prop
;
418 prop
= fdt_get_property_by_offset(blob
, offset
, NULL
);
419 path
= fdt_string(blob
, fdt32_to_cpu(prop
->nameoff
));
420 if (prop
->len
&& 0 == strncmp(path
, name
, name_len
))
421 node
= fdt_path_offset(blob
, prop
->data
);
425 /* Get the alias number */
426 number
= simple_strtoul(path
+ name_len
, NULL
, 10);
427 if (number
< 0 || number
>= maxcount
) {
428 debug("%s: warning: alias '%s' is out of range\n",
433 /* Make sure the node we found is actually in our list! */
435 for (j
= 0; j
< count
; j
++)
436 if (nodes
[j
] == node
) {
442 debug("%s: warning: alias '%s' points to a node "
443 "'%s' that is missing or is not compatible "
444 " with '%s'\n", __func__
, path
,
445 fdt_get_name(blob
, node
, NULL
),
451 * Add this node to our list in the right place, and mark
454 if (fdtdec_get_is_enabled(blob
, node
)) {
455 if (node_list
[number
]) {
456 debug("%s: warning: alias '%s' requires that "
457 "a node be placed in the list in a "
458 "position which is already filled by "
459 "node '%s'\n", __func__
, path
,
460 fdt_get_name(blob
, node
, NULL
));
463 node_list
[number
] = node
;
464 if (number
>= num_found
)
465 num_found
= number
+ 1;
470 /* Add any nodes not mentioned by an alias */
471 for (i
= j
= 0; i
< maxcount
; i
++) {
473 for (; j
< maxcount
; j
++)
475 fdtdec_get_is_enabled(blob
, nodes
[j
]))
478 /* Have we run out of nodes to add? */
482 assert(!node_list
[i
]);
483 node_list
[i
] = nodes
[j
++];
492 int fdtdec_get_alias_seq(const void *blob
, const char *base
, int offset
,
495 int base_len
= strlen(base
);
496 const char *find_name
;
501 find_name
= fdt_get_name(blob
, offset
, &find_namelen
);
502 debug("Looking for '%s' at %d, name %s\n", base
, offset
, find_name
);
504 aliases
= fdt_path_offset(blob
, "/aliases");
505 for (prop_offset
= fdt_first_property_offset(blob
, aliases
);
507 prop_offset
= fdt_next_property_offset(blob
, prop_offset
)) {
514 prop
= fdt_getprop_by_offset(blob
, prop_offset
, &name
, &len
);
515 debug(" - %s, %s\n", name
, prop
);
516 if (len
< find_namelen
|| *prop
!= '/' || prop
[len
- 1] ||
517 strncmp(name
, base
, base_len
))
520 slash
= strrchr(prop
, '/');
521 if (strcmp(slash
+ 1, find_name
))
523 for (p
= name
+ strlen(name
) - 1; p
> name
; p
--) {
525 *seqp
= simple_strtoul(p
+ 1, NULL
, 10);
526 debug("Found seq %d\n", *seqp
);
532 debug("Not found\n");
536 int fdtdec_get_chosen_node(const void *blob
, const char *name
)
543 return -FDT_ERR_NOTFOUND
;
544 chosen_node
= fdt_path_offset(blob
, "/chosen");
545 prop
= fdt_getprop(blob
, chosen_node
, name
, &len
);
547 return -FDT_ERR_NOTFOUND
;
548 return fdt_path_offset(blob
, prop
);
551 int fdtdec_check_fdt(void)
554 * We must have an FDT, but we cannot panic() yet since the console
555 * is not ready. So for now, just assert(). Boards which need an early
556 * FDT (prior to console ready) will need to make their own
557 * arrangements and do their own checks.
559 assert(!fdtdec_prepare_fdt());
564 * This function is a little odd in that it accesses global data. At some
565 * point if the architecture board.c files merge this will make more sense.
566 * Even now, it is common code.
568 int fdtdec_prepare_fdt(void)
570 if (!gd
->fdt_blob
|| ((uintptr_t)gd
->fdt_blob
& 3) ||
571 fdt_check_header(gd
->fdt_blob
)) {
572 #ifdef CONFIG_SPL_BUILD
573 puts("Missing DTB\n");
575 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");
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
);
681 int fdtdec_parse_phandle_with_args(const void *blob
, int src_node
,
682 const char *list_name
,
683 const char *cells_name
,
684 int cell_count
, int index
,
685 struct fdtdec_phandle_args
*out_args
)
687 const __be32
*list
, *list_end
;
688 int rc
= 0, size
, cur_index
= 0;
693 /* Retrieve the phandle list property */
694 list
= fdt_getprop(blob
, src_node
, list_name
, &size
);
697 list_end
= list
+ size
/ sizeof(*list
);
699 /* Loop over the phandles until all the requested entry is found */
700 while (list
< list_end
) {
705 * If phandle is 0, then it is an empty entry with no
706 * arguments. Skip forward to the next entry.
708 phandle
= be32_to_cpup(list
++);
711 * Find the provider node and parse the #*-cells
712 * property to determine the argument length.
714 * This is not needed if the cell count is hard-coded
715 * (i.e. cells_name not set, but cell_count is set),
716 * except when we're going to return the found node
719 if (cells_name
|| cur_index
== index
) {
720 node
= fdt_node_offset_by_phandle(blob
,
723 debug("%s: could not find phandle\n",
724 fdt_get_name(blob
, src_node
,
731 count
= fdtdec_get_int(blob
, node
, cells_name
,
734 debug("%s: could not get %s for %s\n",
735 fdt_get_name(blob
, src_node
,
738 fdt_get_name(blob
, node
,
747 * Make sure that the arguments actually fit in the
748 * remaining property data length
750 if (list
+ count
> list_end
) {
751 debug("%s: arguments longer than property\n",
752 fdt_get_name(blob
, src_node
, NULL
));
758 * All of the error cases above bail out of the loop, so at
759 * this point, the parsing is successful. If the requested
760 * index matches, then fill the out_args structure and return,
761 * or return -ENOENT for an empty entry.
764 if (cur_index
== index
) {
771 if (count
> MAX_PHANDLE_ARGS
) {
772 debug("%s: too many arguments %d\n",
773 fdt_get_name(blob
, src_node
,
775 count
= MAX_PHANDLE_ARGS
;
777 out_args
->node
= node
;
778 out_args
->args_count
= count
;
779 for (i
= 0; i
< count
; i
++) {
781 be32_to_cpup(list
++);
785 /* Found it! return success */
795 * Result will be one of:
796 * -ENOENT : index is for empty phandle
797 * -EINVAL : parsing error on data
798 * [1..n] : Number of phandle (count mode; when index = -1)
800 rc
= index
< 0 ? cur_index
: -ENOENT
;
805 int fdtdec_get_byte_array(const void *blob
, int node
, const char *prop_name
,
806 u8
*array
, int count
)
811 cell
= get_prop_check_min_len(blob
, node
, prop_name
, count
, &err
);
813 memcpy(array
, cell
, count
);
817 const u8
*fdtdec_locate_byte_array(const void *blob
, int node
,
818 const char *prop_name
, int count
)
823 cell
= get_prop_check_min_len(blob
, node
, prop_name
, count
, &err
);
829 int fdtdec_get_config_int(const void *blob
, const char *prop_name
,
834 debug("%s: %s\n", __func__
, prop_name
);
835 config_node
= fdt_path_offset(blob
, "/config");
838 return fdtdec_get_int(blob
, config_node
, prop_name
, default_val
);
841 int fdtdec_get_config_bool(const void *blob
, const char *prop_name
)
846 debug("%s: %s\n", __func__
, prop_name
);
847 config_node
= fdt_path_offset(blob
, "/config");
850 prop
= fdt_get_property(blob
, config_node
, prop_name
, NULL
);
855 char *fdtdec_get_config_string(const void *blob
, const char *prop_name
)
861 debug("%s: %s\n", __func__
, prop_name
);
862 nodeoffset
= fdt_path_offset(blob
, "/config");
866 nodep
= fdt_getprop(blob
, nodeoffset
, prop_name
, &len
);
870 return (char *)nodep
;
873 int fdtdec_decode_region(const void *blob
, int node
, const char *prop_name
,
874 fdt_addr_t
*basep
, fdt_size_t
*sizep
)
876 const fdt_addr_t
*cell
;
879 debug("%s: %s: %s\n", __func__
, fdt_get_name(blob
, node
, NULL
),
881 cell
= fdt_getprop(blob
, node
, prop_name
, &len
);
882 if (!cell
|| (len
< sizeof(fdt_addr_t
) * 2)) {
883 debug("cell=%p, len=%d\n", cell
, len
);
887 *basep
= fdt_addr_to_cpu(*cell
);
888 *sizep
= fdt_size_to_cpu(cell
[1]);
889 debug("%s: base=%08lx, size=%lx\n", __func__
, (ulong
)*basep
,
896 * Read a flash entry from the fdt
898 * @param blob FDT blob
899 * @param node Offset of node to read
900 * @param name Name of node being read
901 * @param entry Place to put offset and size of this node
902 * @return 0 if ok, -ve on error
904 int fdtdec_read_fmap_entry(const void *blob
, int node
, const char *name
,
905 struct fmap_entry
*entry
)
910 if (fdtdec_get_int_array(blob
, node
, "reg", reg
, 2)) {
911 debug("Node '%s' has bad/missing 'reg' property\n", name
);
912 return -FDT_ERR_NOTFOUND
;
914 entry
->offset
= reg
[0];
915 entry
->length
= reg
[1];
916 entry
->used
= fdtdec_get_int(blob
, node
, "used", entry
->length
);
917 prop
= fdt_getprop(blob
, node
, "compress", NULL
);
918 entry
->compress_algo
= prop
&& !strcmp(prop
, "lzo") ?
919 FMAP_COMPRESS_LZO
: FMAP_COMPRESS_NONE
;
920 prop
= fdt_getprop(blob
, node
, "hash", &entry
->hash_size
);
921 entry
->hash_algo
= prop
? FMAP_HASH_SHA256
: FMAP_HASH_NONE
;
922 entry
->hash
= (uint8_t *)prop
;
927 u64
fdtdec_get_number(const fdt32_t
*ptr
, unsigned int cells
)
932 number
= (number
<< 32) | fdt32_to_cpu(*ptr
++);
937 int fdt_get_resource(const void *fdt
, int node
, const char *property
,
938 unsigned int index
, struct fdt_resource
*res
)
940 const fdt32_t
*ptr
, *end
;
941 int na
, ns
, len
, parent
;
944 parent
= fdt_parent_offset(fdt
, node
);
948 na
= fdt_address_cells(fdt
, parent
);
949 ns
= fdt_size_cells(fdt
, parent
);
951 ptr
= fdt_getprop(fdt
, node
, property
, &len
);
955 end
= ptr
+ len
/ sizeof(*ptr
);
957 while (ptr
+ na
+ ns
<= end
) {
959 res
->start
= res
->end
= fdtdec_get_number(ptr
, na
);
960 res
->end
+= fdtdec_get_number(&ptr
[na
], ns
) - 1;
968 return -FDT_ERR_NOTFOUND
;
971 int fdt_get_named_resource(const void *fdt
, int node
, const char *property
,
972 const char *prop_names
, const char *name
,
973 struct fdt_resource
*res
)
977 index
= fdt_find_string(fdt
, node
, prop_names
, name
);
981 return fdt_get_resource(fdt
, node
, property
, index
, res
);
984 int fdtdec_decode_memory_region(const void *blob
, int config_node
,
985 const char *mem_type
, const char *suffix
,
986 fdt_addr_t
*basep
, fdt_size_t
*sizep
)
990 fdt_size_t size
, offset_size
;
991 fdt_addr_t base
, offset
;
994 if (config_node
== -1) {
995 config_node
= fdt_path_offset(blob
, "/config");
996 if (config_node
< 0) {
997 debug("%s: Cannot find /config node\n", __func__
);
1004 snprintf(prop_name
, sizeof(prop_name
), "%s-memory%s", mem_type
,
1006 mem
= fdt_getprop(blob
, config_node
, prop_name
, NULL
);
1008 debug("%s: No memory type for '%s', using /memory\n", __func__
,
1013 node
= fdt_path_offset(blob
, mem
);
1015 debug("%s: Failed to find node '%s': %s\n", __func__
, mem
,
1016 fdt_strerror(node
));
1021 * Not strictly correct - the memory may have multiple banks. We just
1024 if (fdtdec_decode_region(blob
, node
, "reg", &base
, &size
)) {
1025 debug("%s: Failed to decode memory region %s\n", __func__
,
1030 snprintf(prop_name
, sizeof(prop_name
), "%s-offset%s", mem_type
,
1032 if (fdtdec_decode_region(blob
, config_node
, prop_name
, &offset
,
1034 debug("%s: Failed to decode memory region '%s'\n", __func__
,
1039 *basep
= base
+ offset
;
1040 *sizep
= offset_size
;
1045 static int decode_timing_property(const void *blob
, int node
, const char *name
,
1046 struct timing_entry
*result
)
1048 int length
, ret
= 0;
1051 prop
= fdt_getprop(blob
, node
, name
, &length
);
1053 debug("%s: could not find property %s\n",
1054 fdt_get_name(blob
, node
, NULL
), name
);
1058 if (length
== sizeof(u32
)) {
1059 result
->typ
= fdtdec_get_int(blob
, node
, name
, 0);
1060 result
->min
= result
->typ
;
1061 result
->max
= result
->typ
;
1063 ret
= fdtdec_get_int_array(blob
, node
, name
, &result
->min
, 3);
1069 int fdtdec_decode_display_timing(const void *blob
, int parent
, int index
,
1070 struct display_timing
*dt
)
1072 int i
, node
, timings_node
;
1076 timings_node
= fdt_subnode_offset(blob
, parent
, "display-timings");
1077 if (timings_node
< 0)
1078 return timings_node
;
1080 for (i
= 0, node
= fdt_first_subnode(blob
, timings_node
);
1081 node
> 0 && i
!= index
;
1082 node
= fdt_next_subnode(blob
, node
))
1088 memset(dt
, 0, sizeof(*dt
));
1090 ret
|= decode_timing_property(blob
, node
, "hback-porch",
1092 ret
|= decode_timing_property(blob
, node
, "hfront-porch",
1094 ret
|= decode_timing_property(blob
, node
, "hactive", &dt
->hactive
);
1095 ret
|= decode_timing_property(blob
, node
, "hsync-len", &dt
->hsync_len
);
1096 ret
|= decode_timing_property(blob
, node
, "vback-porch",
1098 ret
|= decode_timing_property(blob
, node
, "vfront-porch",
1100 ret
|= decode_timing_property(blob
, node
, "vactive", &dt
->vactive
);
1101 ret
|= decode_timing_property(blob
, node
, "vsync-len", &dt
->vsync_len
);
1102 ret
|= decode_timing_property(blob
, node
, "clock-frequency",
1106 val
= fdtdec_get_int(blob
, node
, "vsync-active", -1);
1108 dt
->flags
|= val
? DISPLAY_FLAGS_VSYNC_HIGH
:
1109 DISPLAY_FLAGS_VSYNC_LOW
;
1111 val
= fdtdec_get_int(blob
, node
, "hsync-active", -1);
1113 dt
->flags
|= val
? DISPLAY_FLAGS_HSYNC_HIGH
:
1114 DISPLAY_FLAGS_HSYNC_LOW
;
1116 val
= fdtdec_get_int(blob
, node
, "de-active", -1);
1118 dt
->flags
|= val
? DISPLAY_FLAGS_DE_HIGH
:
1119 DISPLAY_FLAGS_DE_LOW
;
1121 val
= fdtdec_get_int(blob
, node
, "pixelclk-active", -1);
1123 dt
->flags
|= val
? DISPLAY_FLAGS_PIXDATA_POSEDGE
:
1124 DISPLAY_FLAGS_PIXDATA_NEGEDGE
;
1127 if (fdtdec_get_bool(blob
, node
, "interlaced"))
1128 dt
->flags
|= DISPLAY_FLAGS_INTERLACED
;
1129 if (fdtdec_get_bool(blob
, node
, "doublescan"))
1130 dt
->flags
|= DISPLAY_FLAGS_DOUBLESCAN
;
1131 if (fdtdec_get_bool(blob
, node
, "doubleclk"))
1132 dt
->flags
|= DISPLAY_FLAGS_DOUBLECLK
;
1137 int fdtdec_setup(void)
1139 #ifdef CONFIG_OF_CONTROL
1140 # ifdef CONFIG_OF_EMBED
1141 /* Get a pointer to the FDT */
1142 gd
->fdt_blob
= __dtb_dt_begin
;
1143 # elif defined CONFIG_OF_SEPARATE
1144 # ifdef CONFIG_SPL_BUILD
1145 /* FDT is at end of BSS */
1146 gd
->fdt_blob
= (ulong
*)&__bss_end
;
1148 /* FDT is at end of image */
1149 gd
->fdt_blob
= (ulong
*)&_end
;
1151 # elif defined(CONFIG_OF_HOSTFILE)
1152 if (sandbox_read_fdt_from_file()) {
1153 puts("Failed to read control FDT\n");
1157 # ifndef CONFIG_SPL_BUILD
1158 /* Allow the early environment to override the fdt address */
1159 gd
->fdt_blob
= (void *)getenv_ulong("fdtcontroladdr", 16,
1160 (uintptr_t)gd
->fdt_blob
);
1163 return fdtdec_prepare_fdt();
1166 #endif /* !USE_HOSTCC */