Merge tag 'u-boot-imx-20191009' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / common / spl / spl.c
index 88d4b8a9bf15b70e990758cd33ea5b984ef93f11..5fdd6d0d032181cfd6e841ede1c2dd47fd6b9701 100644 (file)
@@ -22,6 +22,7 @@
 #include <linux/compiler.h>
 #include <fdt_support.h>
 #include <bootcount.h>
+#include <wdt.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -194,10 +195,12 @@ static int spl_load_fit_image(struct spl_image_info *spl_image,
 #ifdef CONFIG_SPL_FIT_SIGNATURE
        images.verify = 1;
 #endif
-       fit_image_load(&images, (ulong)header,
+       ret = fit_image_load(&images, (ulong)header,
                       &fit_uname_fdt, &fit_uname_config,
                       IH_ARCH_DEFAULT, IH_TYPE_FLATDT, -1,
                       FIT_LOAD_OPTIONAL, &dt_data, &dt_len);
+       if (ret >= 0)
+               spl_image->fdt_addr = (void *)dt_data;
 
        conf_noffset = fit_conf_get_node((const void *)header,
                                         fit_uname_config);
@@ -353,17 +356,23 @@ static int setup_spl_handoff(void)
        return 0;
 }
 
+__weak int handoff_arch_save(struct spl_handoff *ho)
+{
+       return 0;
+}
+
 static int write_spl_handoff(void)
 {
        struct spl_handoff *ho;
+       int ret;
 
        ho = bloblist_find(BLOBLISTT_SPL_HANDOFF, sizeof(struct spl_handoff));
        if (!ho)
                return -ENOENT;
        handoff_save_dram(ho);
-#ifdef CONFIG_SANDBOX
-       ho->arch.magic = TEST_HANDOFF_MAGIC;
-#endif
+       ret = handoff_arch_save(ho);
+       if (ret)
+               return ret;
        debug(SPL_TPL_PROMPT "Wrote SPL handoff\n");
 
        return 0;
@@ -401,23 +410,6 @@ static int spl_common_init(bool setup_malloc)
                return ret;
        }
 #endif
-       if (CONFIG_IS_ENABLED(BLOBLIST)) {
-               ret = bloblist_init();
-               if (ret) {
-                       debug("%s: Failed to set up bloblist: ret=%d\n",
-                             __func__, ret);
-                       return ret;
-               }
-       }
-       if (CONFIG_IS_ENABLED(HANDOFF)) {
-               int ret;
-
-               ret = setup_spl_handoff();
-               if (ret) {
-                       puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
-                       hang();
-               }
-       }
        if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
                ret = fdtdec_setup();
                if (ret) {
@@ -532,7 +524,7 @@ static int spl_load_image(struct spl_image_info *spl_image,
 }
 
 /**
- * boot_from_devices() - Try loading an booting U-Boot from a list of devices
+ * boot_from_devices() - Try loading a booting U-Boot from a list of devices
  *
  * @spl_image: Place to put the image details if successful
  * @spl_boot_list: List of boot devices to try
@@ -595,11 +587,33 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
         */
        timer_init();
 #endif
+       if (CONFIG_IS_ENABLED(BLOBLIST)) {
+               ret = bloblist_init();
+               if (ret) {
+                       debug("%s: Failed to set up bloblist: ret=%d\n",
+                             __func__, ret);
+                       puts(SPL_TPL_PROMPT "Cannot set up bloblist\n");
+                       hang();
+               }
+       }
+       if (CONFIG_IS_ENABLED(HANDOFF)) {
+               int ret;
+
+               ret = setup_spl_handoff();
+               if (ret) {
+                       puts(SPL_TPL_PROMPT "Cannot set up SPL handoff\n");
+                       hang();
+               }
+       }
 
 #if CONFIG_IS_ENABLED(BOARD_INIT)
        spl_board_init();
 #endif
 
+#if defined(CONFIG_SPL_WATCHDOG_SUPPORT) && CONFIG_IS_ENABLED(WDT)
+       initr_watchdog();
+#endif
+
        if (IS_ENABLED(CONFIG_SPL_OS_BOOT) || CONFIG_IS_ENABLED(HANDOFF))
                dram_init_banksize();
 
@@ -652,6 +666,12 @@ void board_init_r(gd_t *dummy1, ulong dummy2)
                                (void *)spl_image.entry_point);
                break;
 #endif
+#if CONFIG_IS_ENABLED(OPENSBI)
+       case IH_OS_OPENSBI:
+               debug("Jumping to U-Boot via RISC-V OpenSBI\n");
+               spl_invoke_opensbi(&spl_image);
+               break;
+#endif
 #ifdef CONFIG_SPL_OS_BOOT
        case IH_OS_LINUX:
                debug("Jumping to Linux\n");
@@ -702,6 +722,28 @@ void preloader_console_init(void)
 }
 #endif
 
+/**
+ * This function is called before the stack is changed from initial stack to
+ * relocated stack. It tries to dump the stack size used
+ */
+__weak void spl_relocate_stack_check(void)
+{
+#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
+       ulong init_sp = gd->start_addr_sp;
+       ulong stack_bottom = init_sp - CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
+       u8 *ptr = (u8 *)stack_bottom;
+       ulong i;
+
+       for (i = 0; i < CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK); i++) {
+               if (*ptr != CONFIG_VAL(SYS_STACK_F_CHECK_BYTE))
+                       break;
+               ptr++;
+       }
+       printf("SPL initial stack usage: %lu bytes\n",
+              CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - i);
+#endif
+}
+
 /**
  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
  *
@@ -726,6 +768,9 @@ ulong spl_relocate_stack_gd(void)
        gd_t *new_gd;
        ulong ptr = CONFIG_SPL_STACK_R_ADDR;
 
+       if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
+               spl_relocate_stack_check();
+
 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
        if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
                debug("SPL malloc() before relocation used 0x%lx bytes (%ld KB)\n",
@@ -743,7 +788,7 @@ ulong spl_relocate_stack_gd(void)
 #if CONFIG_IS_ENABLED(DM)
        dm_fixup_for_gd_move(new_gd);
 #endif
-#if !defined(CONFIG_ARM)
+#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
        gd = new_gd;
 #endif
        return ptr;