Merge tag 'u-boot-imx-20191009' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[oweals/u-boot.git] / include / spl.h
index f9c674d33bfd77240ed8005317cc6818b6188af1..4359636d87bc219465208a33b4ac2062f4be92ff 100644 (file)
@@ -49,6 +49,72 @@ static inline bool u_boot_first_phase(void)
        return false;
 }
 
+enum u_boot_phase {
+       PHASE_TPL,      /* Running in TPL */
+       PHASE_SPL,      /* Running in SPL */
+       PHASE_BOARD_F,  /* Running in U-Boot before relocation */
+       PHASE_BOARD_R,  /* Running in U-Boot after relocation */
+};
+
+/**
+ * spl_phase() - Find out the phase of U-Boot
+ *
+ * This can be used to avoid #ifdef logic and use if() instead.
+ *
+ * For example, to include code only in TPL, you might do:
+ *
+ *    #ifdef CONFIG_TPL_BUILD
+ *    ...
+ *    #endif
+ *
+ * but with this you can use:
+ *
+ *    if (spl_phase() == PHASE_TPL) {
+ *       ...
+ *    }
+ *
+ * To include code only in SPL, you might do:
+ *
+ *    #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
+ *    ...
+ *    #endif
+ *
+ * but with this you can use:
+ *
+ *    if (spl_phase() == PHASE_SPL) {
+ *       ...
+ *    }
+ *
+ * To include code only in U-Boot proper, you might do:
+ *
+ *    #ifndef CONFIG_SPL_BUILD
+ *    ...
+ *    #endif
+ *
+ * but with this you can use:
+ *
+ *    if (spl_phase() == PHASE_BOARD_F) {
+ *       ...
+ *    }
+ *
+ * @return U-Boot phase
+ */
+static inline enum u_boot_phase spl_phase(void)
+{
+#ifdef CONFIG_TPL_BUILD
+       return PHASE_TPL;
+#elif CONFIG_SPL_BUILD
+       return PHASE_SPL;
+#else
+       DECLARE_GLOBAL_DATA_PTR;
+
+       if (!(gd->flags & GD_FLG_RELOC))
+               return PHASE_BOARD_F;
+       else
+               return PHASE_BOARD_R;
+#endif
+}
+
 /* A string name for SPL or TPL */
 #ifdef CONFIG_SPL_BUILD
 # ifdef CONFIG_TPL_BUILD