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 kTestBufferSizeWords = 16,
26 static dif_rv_plic_t rv_plic;
27 static dif_aon_timer_t aon_timer;
28 static dif_pwrmgr_t pwrmgr;
29 static dif_rstmgr_t rstmgr;
31 static const dt_rstmgr_t kRstmgrDt = 0;
32 static_assert(kDtRstmgrCount == 1,
"this test expects a rstmgr");
33 static const dt_pwrmgr_t kPwrmgrDt = 0;
34 static_assert(kDtPwrmgrCount == 1,
"this library expects exactly one pwrmgr");
35 static const dt_aon_timer_t kAonTimerDt = 0;
36 static_assert(kDtAonTimerCount == 1,
37 "this library expects exactly one aon_timer");
38 static const dt_rv_plic_t kRvPlicDt = 0;
39 static_assert(kDtRvPlicCount >= 1,
"this test expects at least one rv_plic");
41 static const dt_sram_ctrl_t kRetSramCtrlDt = kDtSramCtrlRetAon;
46 const uint32_t kTestData[kTestBufferSizeWords] = {
47 0xe647e5d5, 0x4b5fe6f6, 0x1608a98a, 0x5e347116, 0xb2dc5e92, 0x899e3c0f,
48 0xc98295c2, 0x0fa84434, 0x15747561, 0xfecb5aa1, 0x7a78bb8c, 0x8f9c5d0f,
49 0x49338fbd, 0x093e82cb, 0xaaa58121, 0x5b806f96,
58 uintptr_t ret_sram_owner_addr =
59 dt_sram_ctrl_reg_block(kRetSramCtrlDt, kDtSramCtrlRegBlockRam) +
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 *)ret_sram_owner_addr,
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 bool ottf_handle_irq(uint32_t *exc_info, dt_instance_id_t devid,
92 if (devid == dt_pwrmgr_instance_id(kPwrmgrDt) &&
93 irq_id == dt_pwrmgr_irq_to_plic_id(kPwrmgrDt, kDtPwrmgrIrqWakeup)) {
94 LOG_INFO(
"Receive irq in normal sleep");
95 CHECK_DIF_OK(dif_pwrmgr_irq_acknowledge(&pwrmgr, kDtPwrmgrIrqWakeup));
105 void test_ret_sram_in_normal_sleep(
void) {
107 retention_sram_check((
check_config_t){.do_write =
true, .is_equal =
true});
110 CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(&aon_timer, 20));
113 dt_pwrmgr_irq_to_plic_id(kPwrmgrDt, kDtPwrmgrIrqWakeup);
114 rv_plic_testutils_irq_range_enable(&rv_plic, kPlicTarget, irq_id, irq_id);
118 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
119 &pwrmgr, aon_timer_wakeup_sources,
121 kDifPwrmgrDomainOptionUsbClockInActivePower |
122 kDifPwrmgrDomainOptionMainPowerInLowPower));
124 LOG_INFO(
"Issue WFI to enter normal sleep");
127 retention_sram_check((
check_config_t){.do_write =
false, .is_equal =
true});
133 void enter_deep_sleep(
void) {
135 CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
137 CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(&aon_timer, 20));
140 pwrmgr_testutils_enable_low_power(&pwrmgr, aon_timer_wakeup_sources, 0));
143 LOG_INFO(
"Issue WFI to enter deep sleep");
145 CHECK(
false,
"Should have a reset to CPU before this line");
148 void set_up_reset_request(
void) {
150 CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
154 kDtAonTimerResetReqAonTimer, &reset_sources));
162 CHECK_STATUS_OK(aon_timer_testutils_watchdog_config(
163 &aon_timer, (uint64_t)UINT32_MAX, 20,
false));
166 CHECK(
false,
"Should have a reset to CPU and ret_sram before this line");
169 bool execute_sram_ctrl_sleep_ret_sram_contents_test(
bool scramble) {
171 irq_global_ctrl(
true);
172 irq_external_ctrl(
true);
174 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
175 CHECK_DIF_OK(dif_rstmgr_init_from_dt(kRstmgrDt, &rstmgr));
176 CHECK_DIF_OK(dif_aon_timer_init_from_dt(kAonTimerDt, &aon_timer));
177 CHECK_DIF_OK(dif_rv_plic_init_from_dt(kRvPlicDt, &rv_plic));
181 kDtAonTimerWakeupWkupReq, &aon_timer_wakeup_sources));
184 rstmgr_reset_info = rstmgr_testutils_reason_get();
186 LOG_INFO(
"Reset info = %08x", rstmgr_reset_info);
190 LOG_INFO(
"Start to test retention sram %sscrambled",
191 scramble ?
"" :
"not ");
194 dif_sram_ctrl_t ret_sram;
195 CHECK_DIF_OK(dif_sram_ctrl_init_from_dt(kRetSramCtrlDt, &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, aon_timer_wakeup_sources)) ==
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.");