10 #include "sw/device/lib/runtime/irq.h"
12 #include "sw/device/lib/testing/aon_timer_testutils.h"
13 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
14 #include "sw/device/lib/testing/pwrmgr_testutils.h"
15 #include "sw/device/lib/testing/rstmgr_testutils.h"
16 #include "sw/device/lib/testing/rv_plic_testutils.h"
17 #include "sw/device/lib/testing/sram_ctrl_testutils.h"
18 #include "sw/device/lib/testing/test_framework/check.h"
19 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
22 #include "sw/device/lib/testing/autogen/isr_testutils.h"
34 kTestBufferSizeWords = 16,
37 static dif_rv_plic_t rv_plic;
38 static dif_aon_timer_t aon_timer;
39 static dif_pwrmgr_t pwrmgr;
40 static dif_rstmgr_t rstmgr;
41 static plic_isr_ctx_t plic_ctx = {.rv_plic = &rv_plic,
43 static pwrmgr_isr_ctx_t pwrmgr_isr_ctx = {
46 .expected_irq = kDifPwrmgrIrqWakeup,
50 const uint32_t kTestData[kTestBufferSizeWords] = {
51 0xe647e5d5, 0x4b5fe6f6, 0x1608a98a, 0x5e347116, 0xb2dc5e92, 0x899e3c0f,
52 0xc98295c2, 0x0fa84434, 0x15747561, 0xfecb5aa1, 0x7a78bb8c, 0x8f9c5d0f,
53 0x49338fbd, 0x093e82cb, 0xaaa58121, 0x5b806f96,
62 if (config.do_write) {
63 sram_ctrl_testutils_write(
66 .len = kTestBufferSizeWords});
69 uint32_t tmp_buffer[kTestBufferSizeWords];
70 memcpy(tmp_buffer, (uint8_t *)kRetSramOwnerAddr,
sizeof(tmp_buffer));
72 if (config.is_equal) {
73 CHECK_ARRAYS_EQ(tmp_buffer, kTestData, kTestBufferSizeWords);
75 CHECK_ARRAYS_NE(tmp_buffer, kTestData, kTestBufferSizeWords);
77 LOG_INFO(
"retention ram check with write=%d and is_equal=%d succeeded",
78 config.do_write, config.is_equal);
85 void ottf_internal_isr(uint32_t *exc_info) {}
90 void ottf_external_isr(uint32_t *exc_info) {
91 dif_pwrmgr_irq_t irq_id;
94 isr_testutils_pwrmgr_isr(plic_ctx, pwrmgr_isr_ctx, &peripheral, &irq_id);
96 LOG_INFO(
"Receive irq in normal sleep");
99 "IRQ peripheral: %d is incorrect", peripheral);
100 CHECK(irq_id == kDifPwrmgrIrqWakeup,
"IRQ ID: %d is incorrect", irq_id);
106 void test_ret_sram_in_normal_sleep(
void) {
108 retention_sram_check((
check_config_t){.do_write =
true, .is_equal =
true});
111 CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(&aon_timer, 20));
119 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
120 &pwrmgr, kDifPwrmgrWakeupRequestSourceFive,
122 kDifPwrmgrDomainOptionUsbClockInActivePower |
123 kDifPwrmgrDomainOptionMainPowerInLowPower));
125 LOG_INFO(
"Issue WFI to enter normal sleep");
128 retention_sram_check((
check_config_t){.do_write =
false, .is_equal =
true});
134 void enter_deep_sleep(
void) {
136 CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
138 CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(&aon_timer, 20));
140 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
141 &pwrmgr, kDifPwrmgrWakeupRequestSourceFive, 0));
144 LOG_INFO(
"Issue WFI to enter deep sleep");
146 CHECK(
false,
"Should have a reset to CPU before this line");
149 void set_up_reset_request(
void) {
151 CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
153 kDifPwrmgrResetRequestSourceTwo,
160 CHECK_STATUS_OK(aon_timer_testutils_watchdog_config(
161 &aon_timer, (uint64_t)UINT32_MAX, 20,
false));
164 CHECK(
false,
"Should have a reset to CPU and ret_sram before this line");
167 bool execute_sram_ctrl_sleep_ret_sram_contents_test(
bool scramble) {
169 irq_global_ctrl(
true);
170 irq_external_ctrl(
true);
173 CHECK_DIF_OK(dif_pwrmgr_init(addr, &pwrmgr));
175 CHECK_DIF_OK(dif_rstmgr_init(addr, &rstmgr));
177 CHECK_DIF_OK(dif_aon_timer_init(addr, &aon_timer));
179 CHECK_DIF_OK(dif_rv_plic_init(addr, &rv_plic));
182 rstmgr_reset_info = rstmgr_testutils_reason_get();
184 LOG_INFO(
"Reset info = %08x", rstmgr_reset_info);
188 LOG_INFO(
"Start to test retention sram %sscrambled",
189 scramble ?
"" :
"not ");
192 dif_sram_ctrl_t ret_sram;
195 CHECK_DIF_OK(dif_sram_ctrl_init(addr, &ret_sram));
197 CHECK_STATUS_OK(sram_ctrl_testutils_wipe(&ret_sram));
199 CHECK_STATUS_OK(sram_ctrl_testutils_scramble(&ret_sram));
201 test_ret_sram_in_normal_sleep();
205 LOG_INFO(
"Wake up from deep sleep");
207 CHECK(UNWRAP(pwrmgr_testutils_is_wakeup_reason(
208 &pwrmgr, kDifPwrmgrWakeupRequestSourceFive)) ==
true);
210 retention_sram_check((
check_config_t){.do_write =
false, .is_equal =
true});
212 set_up_reset_request();
213 }
else if (rstmgr_reset_info & kDifRstmgrResetInfoWatchdog) {
216 retention_sram_check(
219 LOG_FATAL(
"Unexepected reset type detected.");