[sw/dif] Add a dif function to grab current state

- add a testutil function to wait until a certain entropy state

Signed-off-by: Timothy Chen <timothytim@google.com>

fix

Signed-off-by: Timothy Chen <timothytim@google.com>
diff --git a/sw/device/lib/dif/dif_entropy_src.c b/sw/device/lib/dif/dif_entropy_src.c
index 4eb2ddd..8e49d26 100644
--- a/sw/device/lib/dif/dif_entropy_src.c
+++ b/sw/device/lib/dif/dif_entropy_src.c
@@ -537,3 +537,15 @@
 
   return kDifOk;
 }
+
+dif_result_t dif_entropy_src_get_main_fsm_state(
+    const dif_entropy_src_t *entropy_src, dif_entropy_main_fsm_t *state) {
+  if (entropy_src == NULL || state == NULL) {
+    return kDifBadArg;
+  }
+
+  *state = mmio_region_read32(entropy_src->base_addr,
+                              ENTROPY_SRC_MAIN_SM_STATE_REG_OFFSET);
+
+  return kDifOk;
+}
diff --git a/sw/device/lib/dif/dif_entropy_src.h b/sw/device/lib/dif/dif_entropy_src.h
index 959ab2e..43713c9 100644
--- a/sw/device/lib/dif/dif_entropy_src.h
+++ b/sw/device/lib/dif/dif_entropy_src.h
@@ -54,6 +54,33 @@
 } dif_entropy_src_single_bit_mode_t;
 
 /**
+ * Main FSM state
+ */
+typedef enum dif_entropy_main_fsm {
+  kDifEntropySrcMainFsmStateIdle = 0x0f5,
+  kDifEntropySrcMainFsmStateBootHTRunning = 0x1d2,
+  kDifEntropySrcMainFsmStateBootPostHTChk = 0x16e,
+  kDifEntropySrcMainFsmStateBootPhaseDone = 0x08e,
+  kDifEntropySrcMainFsmStateStartupHTStart = 0x02c,
+  kDifEntropySrcMainFsmStateStartupPhase1 = 0x101,
+  kDifEntropySrcMainFsmStateStartupPass1 = 0x1a5,
+  kDifEntropySrcMainFsmStateStartupFail1 = 0x017,
+  kDifEntropySrcMainFsmStateContHTStart = 0x040,
+  kDifEntropySrcMainFsmStateContHTRunning = 0x1a2,
+  kDifEntropySrcMainFsmStateFWInsertStart = 0x0c3,
+  kDifEntropySrcMainFsmStateFWInsertMsg = 0x059,
+  kDifEntropySrcMainFsmStateSha3MsgDone = 0x10f,
+  kDifEntropySrcMainFsmStateSha3Prep = 0x0f8,
+  kDifEntropySrcMainFsmStateSha3Process = 0x0bf,
+  kDifEntropySrcMainFsmStateSha3Valid = 0x171,
+  kDifEntropySrcMainFsmStateSha3Done = 0x198,
+  kDifEntropySrcMainFsmStateSha3Quiesce = 0x1cd,
+  kDifEntropySrcMainFsmStateAlertState = 0x1fb,
+  kDifEntropySrcMainFsmStateAlertHang = 0x15c,
+  kDifEntropySrcMainFsmStateError = 0x13d
+} dif_entropy_main_fsm_t;
+
+/**
  * Firmware override parameters for an entropy source.
  */
 typedef struct dif_entropy_src_fw_override_config {
@@ -493,6 +520,17 @@
 dif_result_t dif_entropy_src_get_fifo_depth(
     const dif_entropy_src_t *entropy_src, uint32_t *fifo_depth);
 
+/**
+ * Read the current main fsm state.
+ *
+ * @param entropy_src An entropy source handle.
+ * @param[out] state The current state.
+ * @return The result of the operation.
+ */
+OT_WARN_UNUSED_RESULT
+dif_result_t dif_entropy_src_get_main_fsm_state(
+    const dif_entropy_src_t *entropy_src, dif_entropy_main_fsm_t *state);
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif  // __cplusplus
diff --git a/sw/device/lib/testing/entropy_testutils.c b/sw/device/lib/testing/entropy_testutils.c
index 49e17fc..64f3dd4 100644
--- a/sw/device/lib/testing/entropy_testutils.c
+++ b/sw/device/lib/testing/entropy_testutils.c
@@ -57,3 +57,12 @@
   setup_csrng();
   setup_edn();
 }
+
+void entropy_testutils_wait_for_state(const dif_entropy_src_t *entropy_src,
+                                      dif_entropy_main_fsm_t state) {
+  dif_entropy_main_fsm_t cur_state;
+
+  do {
+    CHECK_DIF_OK(dif_entropy_src_get_main_fsm_state(entropy_src, &cur_state));
+  } while (cur_state != state);
+}
diff --git a/sw/device/lib/testing/entropy_testutils.h b/sw/device/lib/testing/entropy_testutils.h
index 6466039..4bd5c30 100644
--- a/sw/device/lib/testing/entropy_testutils.h
+++ b/sw/device/lib/testing/entropy_testutils.h
@@ -5,6 +5,8 @@
 #ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_ENTROPY_TESTUTILS_H_
 #define OPENTITAN_SW_DEVICE_LIB_TESTING_ENTROPY_TESTUTILS_H_
 
+#include "sw/device/lib/dif/dif_entropy_src.h"
+
 /**
  * Initializes the entropy complex to serve random bits to EDN0 and EDN1.
  *
@@ -13,4 +15,10 @@
  */
 void entropy_testutils_boot_mode_init(void);
 
+/**
+ * Wait for the entropy_src to reach a certain state.
+ */
+void wait_entropy_src_state(const dif_entropy_src_t *entropy_src,
+                            dif_entropy_main_fsm_t state);
+
 #endif  // OPENTITAN_SW_DEVICE_LIB_TESTING_ENTROPY_TESTUTILS_H_