Просмотр исходного кода

feat(ds): use RCC atomic block to enable/reset the DS peripheral

harshal.patil 2 лет назад
Родитель
Сommit
43864f7fb4

+ 9 - 2
components/esp_hw_support/esp_ds.c

@@ -266,7 +266,11 @@ static void ds_acquire_enable(void)
     }
 
     periph_module_enable(PERIPH_SHA_MODULE);
-    periph_module_enable(PERIPH_DS_MODULE);
+
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(true);
+        ds_ll_reset_register();
+    }
 
     hmac_hal_start();
 }
@@ -275,7 +279,10 @@ static void ds_disable_release(void)
 {
     ds_hal_finish();
 
-    periph_module_disable(PERIPH_DS_MODULE);
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(false);
+    }
+
     periph_module_disable(PERIPH_SHA_MODULE);
 
     HMAC_RCC_ATOMIC() {

+ 10 - 2
components/esp_hw_support/esp_hmac.c

@@ -18,6 +18,7 @@
 #include "soc/system_reg.h"
 
 #if !CONFIG_IDF_TARGET_ESP32S2
+#include "hal/ds_ll.h"
 #include "hal/hmac_hal.h"
 #include "hal/hmac_ll.h"
 #include "esp_private/periph_ctrl.h"
@@ -75,7 +76,11 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
     }
 
     periph_module_enable(PERIPH_SHA_MODULE);
-    periph_module_enable(PERIPH_DS_MODULE);
+
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(true);
+        ds_ll_reset_register();
+    }
 
     hmac_hal_start();
 
@@ -137,7 +142,10 @@ esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
     // Read back result (bit swapped)
     hmac_hal_read_result_256(hmac);
 
-    periph_module_disable(PERIPH_DS_MODULE);
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(false);
+    }
+
     periph_module_disable(PERIPH_SHA_MODULE);
 
     HMAC_RCC_ATOMIC() {

+ 2 - 0
components/esp_hw_support/include/esp_private/esp_crypto_lock_internal.h

@@ -17,10 +17,12 @@ extern "C" {
 #define MPI_RCC_ATOMIC()
 #define ECC_RCC_ATOMIC()
 #define HMAC_RCC_ATOMIC()
+#define DS_RCC_ATOMIC()
 #else /* !SOC_RCC_IS_INDEPENDENT */
 #define MPI_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
 #define ECC_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
 #define HMAC_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
+#define DS_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
 #endif /* SOC_RCC_IS_INDEPENDENT */
 
 #ifdef __cplusplus

+ 28 - 0
components/hal/esp32c3/include/hal/ds_ll.h

@@ -17,12 +17,40 @@
 
 #include "soc/hwcrypto_reg.h"
 #include "soc/soc_caps.h"
+#include "soc/system_struct.h"
 #include "hal/ds_types.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/**
+ * @brief Enable the bus clock for Digital Signature peripheral module
+ *
+ * @param true to enable the module, false to disable the module
+ */
+static inline void ds_ll_enable_bus_clock(bool enable)
+{
+    SYSTEM.perip_clk_en1.reg_crypto_ds_clk_en = enable;
+}
+
+/// use a macro to wrap the function, force the caller to use it in a critical section
+/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
+#define ds_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_enable_bus_clock(__VA_ARGS__)
+
+/**
+ * @brief Reset the Digital Signature peripheral module
+ */
+static inline void ds_ll_reset_register(void)
+{
+    SYSTEM.perip_rst_en1.reg_crypto_ds_rst = 1;
+    SYSTEM.perip_rst_en1.reg_crypto_ds_rst = 0;
+}
+
+/// use a macro to wrap the function, force the caller to use it in a critical section
+/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
+#define ds_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_reset_register(__VA_ARGS__)
+
 static inline void ds_ll_start(void)
 {
     REG_WRITE(DS_SET_START_REG, 1);

+ 20 - 0
components/hal/esp32c6/include/hal/ds_ll.h

@@ -17,6 +17,7 @@
 
 #include "soc/hwcrypto_reg.h"
 #include "soc/soc_caps.h"
+#include "soc/pcr_struct.h"
 #include "hal/ds_types.h"
 
 
@@ -24,6 +25,25 @@
 extern "C" {
 #endif
 
+/**
+ * @brief Enable the bus clock for Digital Signature peripheral module
+ *
+ * @param true to enable the module, false to disable the module
+ */
+static inline void ds_ll_enable_bus_clock(bool enable)
+{
+    PCR.ds_conf.ds_clk_en = enable;
+}
+
+/**
+ * @brief Reset the Digital Signature peripheral module
+ */
+static inline void ds_ll_reset_register(void)
+{
+    PCR.ds_conf.ds_rst_en = 1;
+    PCR.ds_conf.ds_rst_en = 0;
+}
+
 static inline void ds_ll_start(void)
 {
     REG_WRITE(DS_SET_START_REG, 1);

+ 20 - 0
components/hal/esp32h2/include/hal/ds_ll.h

@@ -17,12 +17,32 @@
 
 #include "soc/hwcrypto_reg.h"
 #include "soc/soc_caps.h"
+#include "soc/pcr_struct.h"
 #include "hal/ds_types.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/**
+ * @brief Enable the bus clock for Digital Signature peripheral module
+ *
+ * @param true to enable the module, false to disable the module
+ */
+static inline void ds_ll_enable_bus_clock(bool enable)
+{
+    PCR.ds_conf.ds_clk_en = enable;
+}
+
+/**
+ * @brief Reset the Digital Signature peripheral module
+ */
+static inline void ds_ll_reset_register(void)
+{
+    PCR.ds_conf.ds_rst_en = 1;
+    PCR.ds_conf.ds_rst_en = 0;
+}
+
 static inline void ds_ll_start(void)
 {
     REG_WRITE(DS_SET_START_REG, 1);

+ 30 - 0
components/hal/esp32p4/include/hal/ds_ll.h

@@ -16,6 +16,7 @@
 #include <string.h>
 
 #include "soc/hwcrypto_reg.h"
+#include "soc/hp_sys_clkrst_struct.h"
 #include "soc/soc_caps.h"
 #include "hal/ds_types.h"
 
@@ -24,6 +25,35 @@
 extern "C" {
 #endif
 
+/**
+ * @brief Enable the bus clock for DS peripheral module
+ *
+ * @param true to enable the module, false to disable the module
+ */
+static inline void ds_ll_enable_bus_clock(bool enable)
+{
+    HP_SYS_CLKRST.peri_clk_ctrl25.reg_crypto_ds_clk_en = enable;
+}
+
+/// use a macro to wrap the function, force the caller to use it in a critical section
+/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
+#define ds_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_enable_bus_clock(__VA_ARGS__)
+
+/**
+ * @brief Reset the DS peripheral module
+ */
+static inline void ds_ll_reset_register(void)
+{
+    HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_ds = 1;
+    HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_ds = 0;
+    HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_crypto = 1;
+    HP_SYS_CLKRST.hp_rst_en2.reg_rst_en_crypto = 0;
+}
+
+/// use a macro to wrap the function, force the caller to use it in a critical section
+/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
+#define ds_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_reset_register(__VA_ARGS__)
+
 static inline void ds_ll_start(void)
 {
     REG_WRITE(DS_SET_START_REG, 1);

+ 29 - 1
components/hal/esp32s3/include/hal/ds_ll.h

@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
+ * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
  */
@@ -12,12 +12,40 @@
 
 #include "soc/hwcrypto_reg.h"
 #include "soc/soc_caps.h"
+#include "soc/system_struct.h"
 #include "hal/ds_types.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/**
+ * @brief Enable the bus clock for Digital Signature peripheral module
+ *
+ * @param true to enable the module, false to disable the module
+ */
+static inline void ds_ll_enable_bus_clock(bool enable)
+{
+    SYSTEM.perip_clk_en1.crypto_ds_clk_en = enable;
+}
+
+/// use a macro to wrap the function, force the caller to use it in a critical section
+/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
+#define ds_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_enable_bus_clock(__VA_ARGS__)
+
+/**
+ * @brief Reset the Digital Signature peripheral module
+ */
+static inline void ds_ll_reset_register(void)
+{
+    SYSTEM.perip_rst_en1.crypto_ds_rst = 1;
+    SYSTEM.perip_rst_en1.crypto_ds_rst = 0;
+}
+
+/// use a macro to wrap the function, force the caller to use it in a critical section
+/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
+#define ds_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; ds_ll_reset_register(__VA_ARGS__)
+
 static inline void ds_ll_start(void)
 {
     REG_WRITE(DS_SET_START_REG, 1);

+ 11 - 3
components/hal/test_apps/crypto/main/ds/test_ds.c

@@ -139,20 +139,28 @@ static void ds_acquire_enable(void)
     }
 
     periph_module_enable(PERIPH_SHA_MODULE);
-    periph_module_enable(PERIPH_DS_MODULE);
+
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(true);
+        ds_ll_reset_register();
+    }
+
     hmac_hal_start();
 }
 
 static void ds_disable_release(void)
 {
     ds_hal_finish();
-    periph_module_disable(PERIPH_DS_MODULE);
+
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(false);
+    }
+
     periph_module_disable(PERIPH_SHA_MODULE);
 
     HMAC_RCC_ATOMIC() {
         hmac_ll_enable_bus_clock(false);
     }
-
 }
 
 

+ 10 - 2
components/hal/test_apps/crypto/main/hmac/test_hmac.c

@@ -41,6 +41,7 @@ static esp_err_t hmac_jtag_disable(void)
 
 #include "hal/hmac_hal.h"
 #include "hal/hmac_ll.h"
+#include "hal/ds_ll.h"
 #include "esp_private/periph_ctrl.h"
 
 #define SHA256_BLOCK_SZ 64
@@ -78,7 +79,11 @@ static esp_err_t hmac_calculate(hmac_key_id_t key_id, const void *message, size_
     }
 
     periph_module_enable(PERIPH_SHA_MODULE);
-    periph_module_enable(PERIPH_DS_MODULE);
+
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(true);
+        ds_ll_reset_register();
+    }
 
     hmac_hal_start();
 
@@ -130,7 +135,10 @@ static esp_err_t hmac_calculate(hmac_key_id_t key_id, const void *message, size_
 
     hmac_hal_read_result_256(hmac);
 
-    periph_module_disable(PERIPH_DS_MODULE);
+    DS_RCC_ATOMIC() {
+        ds_ll_enable_bus_clock(false);
+    }
+
     periph_module_disable(PERIPH_SHA_MODULE);
 
     HMAC_RCC_ATOMIC() {