Merge branch 'master' of git://git.denx.de/u-boot-spi
[oweals/u-boot.git] / common / image-fdt.c
index da4d0070815db68782bdd0731b59bbf211067710..5988808f1872c6e0c84520f56e55e29857e0ea26 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (c) 2013, Google Inc.
  *
@@ -5,15 +6,14 @@
  *
  * (C) Copyright 2000-2006
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <fdt_support.h>
+#include <fdtdec.h>
 #include <errno.h>
 #include <image.h>
-#include <libfdt.h>
+#include <linux/libfdt.h>
 #include <mapmem.h>
 #include <asm/io.h>
 
@@ -21,6 +21,9 @@
 #define CONFIG_SYS_FDT_PAD 0x3000
 #endif
 
+/* adding a ramdisk needs 0x44 bytes in version 2008.10 */
+#define FDT_RAMDISK_OVERHEAD   0x80
+
 DECLARE_GLOBAL_DATA_PTR;
 
 static void fdt_error(const char *msg)
@@ -65,30 +68,66 @@ static const image_header_t *image_get_fdt(ulong fdt_addr)
 }
 #endif
 
+static void boot_fdt_reserve_region(struct lmb *lmb, uint64_t addr,
+                                   uint64_t size)
+{
+       int ret;
+
+       ret = lmb_reserve(lmb, addr, size);
+       if (!ret) {
+               debug("   reserving fdt memory region: addr=%llx size=%llx\n",
+                     (unsigned long long)addr, (unsigned long long)size);
+       } else {
+               puts("ERROR: reserving fdt memory region failed ");
+               printf("(addr=%llx size=%llx)\n",
+                      (unsigned long long)addr, (unsigned long long)size);
+       }
+}
+
 /**
- * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
+ * boot_fdt_add_mem_rsv_regions - Mark the memreserve and reserved-memory
+ * sections as unusable
  * @lmb: pointer to lmb handle, will be used for memory mgmt
  * @fdt_blob: pointer to fdt blob base address
  *
- * Adds the memreserve regions in the dtb to the lmb block.  Adding the
- * memreserve regions prevents u-boot from using them to store the initrd
- * or the fdt blob.
+ * Adds the and reserved-memorymemreserve regions in the dtb to the lmb block.
+ * Adding the memreserve regions prevents u-boot from using them to store the
+ * initrd or the fdt blob.
  */
 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
 {
        uint64_t addr, size;
-       int i, total;
+       int i, total, ret;
+       int nodeoffset, subnode;
+       struct fdt_resource res;
 
        if (fdt_check_header(fdt_blob) != 0)
                return;
 
+       /* process memreserve sections */
        total = fdt_num_mem_rsv(fdt_blob);
        for (i = 0; i < total; i++) {
                if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
                        continue;
-               printf("   reserving fdt memory region: addr=%llx size=%llx\n",
-                      (unsigned long long)addr, (unsigned long long)size);
-               lmb_reserve(lmb, addr, size);
+               boot_fdt_reserve_region(lmb, addr, size);
+       }
+
+       /* process reserved-memory */
+       nodeoffset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory");
+       if (nodeoffset >= 0) {
+               subnode = fdt_first_subnode(fdt_blob, nodeoffset);
+               while (subnode >= 0) {
+                       /* check if this subnode has a reg property */
+                       ret = fdt_get_resource(fdt_blob, subnode, "reg", 0,
+                                              &res);
+                       if (!ret) {
+                               addr = res.start;
+                               size = res.end - res.start + 1;
+                               boot_fdt_reserve_region(lmb, addr, size);
+                       }
+
+                       subnode = fdt_next_subnode(fdt_blob, subnode);
+               }
        }
 }
 
@@ -191,7 +230,8 @@ int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
        *of_flat_tree = of_start;
        *of_size = of_len;
 
-       set_working_fdt_addr((ulong)*of_flat_tree);
+       if (CONFIG_IS_ENABLED(CMD_FDT))
+               set_working_fdt_addr(map_to_sysmem(*of_flat_tree));
        return 0;
 
 error:
@@ -294,9 +334,6 @@ int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
                debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
                      fdt_addr);
 
-               /* copy from dataflash if needed */
-               fdt_addr = genimg_get_image(fdt_addr);
-
                /*
                 * Check if there is an FDT image at the
                 * address provided in the second bootm argument
@@ -356,17 +393,16 @@ int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
                        if (fit_check_format(buf)) {
                                ulong load, len;
 
-                               fdt_noffset = fit_image_load(images,
+                               fdt_noffset = boot_get_fdt_fit(images,
                                        fdt_addr, &fit_uname_fdt,
                                        &fit_uname_config,
-                                       arch, IH_TYPE_FLATDT,
-                                       BOOTSTAGE_ID_FIT_FDT_START,
-                                       FIT_LOAD_OPTIONAL, &load, &len);
+                                       arch, &load, &len);
 
                                images->fit_hdr_fdt = map_sysmem(fdt_addr, 0);
                                images->fit_uname_fdt = fit_uname_fdt;
                                images->fit_noffset_fdt = fdt_noffset;
                                fdt_addr = load;
+
                                break;
                        } else
 #endif
@@ -458,6 +494,11 @@ __weak int ft_verify_fdt(void *fdt)
        return 1;
 }
 
+__weak int arch_fixup_fdt(void *blob)
+{
+       return 0;
+}
+
 int image_setup_libfdt(bootm_headers_t *images, void *blob,
                       int of_size, struct lmb *lmb)
 {