[sca] Add a parameter to sca_init() for selecting the trigger source

Signed-off-by: Alphan Ulusoy <alphan@google.com>
diff --git a/hw/top_englishbreakfast/util/sw_sources.patch b/hw/top_englishbreakfast/util/sw_sources.patch
index bd80b72..3168022 100644
--- a/hw/top_englishbreakfast/util/sw_sources.patch
+++ b/hw/top_englishbreakfast/util/sw_sources.patch
@@ -56,7 +56,7 @@
 --- a/sw/device/sca/aes_serial.c
 +++ b/sw/device/sca/aes_serial.c
 @@ -203,21 +203,13 @@ int main(void) {
-   sca_init();
+   sca_init(kScaTriggerSourceAes);
    sca_get_uart(&uart1);
  
 -  LOG_INFO("Running AES serial");
diff --git a/sw/device/sca/aes_serial.c b/sw/device/sca/aes_serial.c
index 50bee15..3fb4849 100644
--- a/sw/device/sca/aes_serial.c
+++ b/sw/device/sca/aes_serial.c
@@ -200,7 +200,7 @@
 int main(void) {
   const dif_uart_t *uart1;
 
-  sca_init();
+  sca_init(kScaTriggerSourceAes);
   sca_get_uart(&uart1);
 
   LOG_INFO("Running AES serial");
diff --git a/sw/device/sca/lib/sca.c b/sw/device/sca/lib/sca.c
index 1c74eb0..3f50ae5 100644
--- a/sw/device/sca/lib/sca.c
+++ b/sw/device/sca/lib/sca.c
@@ -5,6 +5,7 @@
 #include "sw/device/sca/lib/sca.h"
 
 #include "sw/device/lib/arch/device.h"
+#include "sw/device/lib/base/bitfield.h"
 #include "sw/device/lib/dif/dif_clkmgr.h"
 #include "sw/device/lib/dif/dif_entropy_src.h"
 #include "sw/device/lib/dif/dif_gpio.h"
@@ -30,19 +31,24 @@
   if (expr) {               \
   }
 
+/**
+ * Bitfield for the trigger source.
+ *
+ * Bits 9 to 11 are used to select the trigger source. See chiplevel.sv.tpl for
+ * details.
+ */
+static const bitfield_field32_t kTriggerSourceBitfield = {
+    .index = 9,
+    .mask = 0x7,
+};
+
 enum {
   /**
-   * GPIO capture trigger values.
+   * Bit index of the trigger gate signal for gating the trigger from software.
    *
-   * GPIO10[11:9]: Trigger select, 000 for AES, see chiplevel.sv.tpl for
-   *               details.
-   * GPIO8:        Trigger enable
+   * See chiplevel.sv.tpl for details.
    */
-  kGpioCaptureTriggerSelMask = 0x00E00,
-  kGpioCaptureTriggerEnMask = 0x00100,
-  kGpioCaptureTriggerSel = 0x00000,
-  kGpioCaptureTriggerHigh = 0x00100,
-  kGpioCaptureTriggerLow = 0x00000,
+  kTriggerGateBitIndex = 8,
   /**
    * RV timer settings.
    */
@@ -86,15 +92,23 @@
 
 /**
  * Initializes the GPIO peripheral.
+ *
+ * @param trigger Trigger source.
  */
-static void sca_init_gpio(void) {
+static void sca_init_gpio(sca_trigger_source_t trigger) {
   dif_gpio_params_t gpio_params = {
       .base_addr = mmio_region_from_addr(TOP_EARLGREY_GPIO_BASE_ADDR)};
   IGNORE_RESULT(dif_gpio_init(gpio_params, &gpio));
-  IGNORE_RESULT(dif_gpio_output_set_enabled_all(
-      &gpio, kGpioCaptureTriggerSelMask | kGpioCaptureTriggerEnMask));
-  IGNORE_RESULT(dif_gpio_write_masked(&gpio, kGpioCaptureTriggerSelMask,
-                                      kGpioCaptureTriggerSel));
+
+  uint32_t select_mask =
+      bitfield_field32_write(0, kTriggerSourceBitfield, UINT32_MAX);
+  uint32_t enable_mask = bitfield_bit32_write(0, kTriggerGateBitIndex, true);
+  IGNORE_RESULT(
+      dif_gpio_output_set_enabled_all(&gpio, select_mask | enable_mask));
+
+  IGNORE_RESULT(dif_gpio_write_masked(
+      &gpio, select_mask,
+      bitfield_field32_write(0, kTriggerSourceBitfield, trigger)));
 }
 
 /**
@@ -129,10 +143,10 @@
       dif_rv_timer_irq_clear(&timer, kRvTimerHart, kRvTimerComparator));
 }
 
-void sca_init(void) {
+void sca_init(sca_trigger_source_t trigger) {
   pinmux_init();
   sca_init_uart();
-  sca_init_gpio();
+  sca_init_gpio(trigger);
   sca_init_timer();
 }
 
@@ -181,13 +195,11 @@
 void sca_get_uart(const dif_uart_t **uart_out) { *uart_out = &uart1; }
 
 void sca_set_trigger_high() {
-  IGNORE_RESULT(dif_gpio_write_masked(&gpio, kGpioCaptureTriggerEnMask,
-                                      kGpioCaptureTriggerHigh));
+  IGNORE_RESULT(dif_gpio_write(&gpio, kTriggerGateBitIndex, true));
 }
 
 void sca_set_trigger_low() {
-  IGNORE_RESULT(dif_gpio_write_masked(&gpio, kGpioCaptureTriggerEnMask,
-                                      kGpioCaptureTriggerLow));
+  IGNORE_RESULT(dif_gpio_write(&gpio, kTriggerGateBitIndex, false));
 }
 
 void sca_call_and_sleep(sca_callee callee, uint32_t sleep_cycles) {
diff --git a/sw/device/sca/lib/sca.h b/sw/device/sca/lib/sca.h
index cd007d9..5414b21 100644
--- a/sw/device/sca/lib/sca.h
+++ b/sw/device/sca/lib/sca.h
@@ -15,9 +15,43 @@
  */
 
 /**
- * Initializes the peripherals (pinmux, uart, gpio, and timer) used by SCA code.
+ * Trigger sources.
+ *
+ * The trigger signal for a peripheral is based on its clkmgr_aon_idle signal.
+ * These values must match the values in chiplevel.sv.tpl.
  */
-void sca_init(void);
+typedef enum sca_trigger_source {
+  /**
+   * Use AES for capture trigger.
+   *
+   * The trigger signal will go high 40 cycles after `dif_aes_trigger()` is
+   * called and remain high until the operation is complete.
+   */
+  kScaTriggerSourceAes,
+  /**
+   * Use HMAC for capture trigger.
+   */
+  kScaTriggerSourceHmac,
+  /**
+   * Use KMAC for capture trigger.
+   */
+  kScaTriggerSourceKmac,
+  /**
+   * Use OTBN (IO_DIV4 clock) for capture trigger.
+   */
+  kScaTriggerSourceOtbnIoDiv4,
+  /**
+   * Use OTBN for capture trigger.
+   */
+  kScaTriggerSourceOtbn,
+} sca_trigger_source_t;
+
+/**
+ * Initializes the peripherals (pinmux, uart, gpio, and timer) used by SCA code.
+ *
+ * @param trigger Peripheral to use for the trigger signal.
+ */
+void sca_init(sca_trigger_source_t trigger);
 
 /**
  * Disables the entropy complex and clocks of IPs not needed for SCA to reduce
@@ -40,10 +74,8 @@
  * Sets capture trigger high.
  *
  * The actual trigger signal used for capture is a combination (logical AND) of:
- * - GPIO15 enabled here, and
- * - the busy signal of the AES unit. This signal will go high 40 cycles
- *   after aes_manual_trigger() is executed below and remain high until
- *   the operation has finished.
+ * - the trigger gate enabled here, and
+ * - the busy signal of the peripheral of interest.
  */
 void sca_set_trigger_high(void);