5 #include "sw/device/tests/pwrmgr_sleep_resets_lib.h"
24 #include "sw/device/lib/runtime/irq.h"
26 #include "sw/device/lib/testing/alert_handler_testutils.h"
27 #include "sw/device/lib/testing/aon_timer_testutils.h"
28 #include "sw/device/lib/testing/pwrmgr_testutils.h"
29 #include "sw/device/lib/testing/rstmgr_testutils.h"
30 #include "sw/device/lib/testing/rv_plic_testutils.h"
31 #include "sw/device/lib/testing/test_framework/check.h"
38 kWdogBarkMicros < kWdogBiteMicros &&
39 kWdogBarkMicros > kEscalationPhase0Micros &&
40 kWdogBarkMicros < (kEscalationPhase0Micros + kEscalationPhase1Micros) &&
41 kWdogBiteMicros < (kEscalationPhase0Micros + kEscalationPhase1Micros),
42 "The wdog bark and bite should happen during the escalation phase 1");
46 dif_alert_handler_t *alert_handler;
47 dif_aon_timer_t *aon_timer;
49 dif_sysrst_ctrl_t *sysrst_ctrl_aon;
53 dif_rv_plic_t plic_actual;
54 dif_alert_handler_t alert_handler_actual;
55 dif_aon_timer_t aon_timer_actual;
56 dif_pwrmgr_t pwrmgr_actual;
57 dif_sysrst_ctrl_t sysrst_ctrl_aon_actual;
58 dif_rstmgr_t rstmgr_actual;
63 static const dt_rstmgr_t kRstmgrDt = 0;
64 static_assert(kDtRstmgrCount == 1,
"this test expects a rstmgr");
65 static const dt_pwrmgr_t kPwrmgrDt = 0;
66 static_assert(kDtPwrmgrCount == 1,
"this library expects exactly one pwrmgr");
67 static const dt_alert_handler_t kAlertHandlerDt = 0;
68 static_assert(kDtAlertHandlerCount == 1,
69 "this library expects exactly one alert_handler");
70 static const dt_aon_timer_t kAonTimerDt = 0;
71 static_assert(kDtAonTimerCount == 1,
72 "this library expects exactly one aon_timer");
73 static const dt_flash_ctrl_t kFlashCtrlDt = 0;
74 static_assert(kDtFlashCtrlCount >= 1,
75 "this test expects at least one flash_ctrl");
76 static_assert(kDtSysrstCtrlCount >= 1,
77 "this test expects at least one sysrst_ctrl");
78 static const dt_sysrst_ctrl_t kSysrstCtrlDt = 0;
79 static const dt_rv_plic_t kRvPlicDt = 0;
80 static_assert(kDtRvPlicCount >= 1,
"this test expects at least one rv_plic");
82 void init_peripherals(
void) {
84 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr_actual));
85 pwrmgr = &pwrmgr_actual;
89 dif_sysrst_ctrl_init_from_dt(kSysrstCtrlDt, &sysrst_ctrl_aon_actual));
90 sysrst_ctrl_aon = &sysrst_ctrl_aon_actual;
93 CHECK_DIF_OK(dif_rstmgr_init_from_dt(kRstmgrDt, &rstmgr_actual));
94 rstmgr = &rstmgr_actual;
97 CHECK_DIF_OK(dif_aon_timer_init_from_dt(kAonTimerDt, &aon_timer_actual));
98 aon_timer = &aon_timer_actual;
103 flash_ctrl = &flash_ctrl_actual;
106 CHECK_DIF_OK(dif_rv_plic_init_from_dt(kRvPlicDt, &plic_actual));
109 rv_plic_testutils_irq_range_enable(
115 dif_alert_handler_init_from_dt(kAlertHandlerDt, &alert_handler_actual));
116 alert_handler = &alert_handler_actual;
121 kDtAonTimerWakeupWkupReq, &aon_timer_wakeup_sources));
124 dt_sysrst_ctrl_instance_id(kSysrstCtrlDt), kDtSysrstCtrlWakeupWkupReq,
125 &sysrst_ctrl_wakeup_sources));
128 void config_alert_handler(
void) {
132 uint32_t cycles[4] = {0};
133 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
134 kEscalationPhase0Micros, &cycles[0]));
135 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
136 kEscalationPhase1Micros, &cycles[1]));
137 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
138 kEscalationPhase2Micros, &cycles[2]));
139 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(10, &cycles[3]));
145 cycles[0] * alert_handler_testutils_cycle_rescaling_factor()},
149 cycles[1] * alert_handler_testutils_cycle_rescaling_factor()},
153 cycles[2] * alert_handler_testutils_cycle_rescaling_factor()}};
157 .accumulator_threshold = 0,
158 .irq_deadline_cycles =
159 cycles[3] * alert_handler_testutils_cycle_rescaling_factor(),
160 .escalation_phases = esc_phases,
161 .escalation_phases_len =
ARRAYSIZE(esc_phases),
168 .alert_classes = alert_classes,
171 .class_configs = class_config,
173 .ping_timeout = kAlertHandlerTestutilsDefaultPingTimeout,
176 CHECK_STATUS_OK(alert_handler_testutils_configure_all(alert_handler, config,
179 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
190 kDtSysrstCtrlResetReqRstReq, &reset_sources));
193 LOG_INFO(
"Reset Request SourceOne is set");
201 .detection_time_threshold = 10,
203 .embedded_controller_reset_duration = 10};
214 CHECK_DIF_OK(dif_pinmux_init(
218 sysrst_ctrl_aon, sysrst_ctrl_input_change_config));
224 void config_wdog(uint64_t bark_micros, uint64_t bite_micros) {
225 uint32_t bark_cycles = 0;
227 aon_timer_testutils_get_aon_cycles_32_from_us(bark_micros, &bark_cycles));
228 uint32_t bite_cycles = 0;
230 aon_timer_testutils_get_aon_cycles_32_from_us(bite_micros, &bite_cycles));
232 LOG_INFO(
"Wdog will bark after %u microseconds (%u aon cycles)",
233 (uint32_t)bark_micros, bark_cycles);
234 LOG_INFO(
"Wdog will bite after %u microseconds (%u aon cycles)",
235 (uint32_t)bite_micros, bite_cycles);
238 CHECK_STATUS_OK(aon_timer_testutils_watchdog_config(aon_timer, bark_cycles,
239 bite_cycles,
false));
244 kDtAonTimerResetReqAonTimer, &reset_sources));
249 void trigger_escalation(
void) {
253 kWdogBarkMicros * alert_handler_testutils_cycle_rescaling_factor(),
254 kWdogBiteMicros * alert_handler_testutils_cycle_rescaling_factor());
256 dif_pwrmgr_alert_t alert = kDifPwrmgrAlertFatalFault;
257 CHECK_DIF_OK(dif_pwrmgr_alert_force(pwrmgr, alert));
261 CHECK(
false,
"Timeout waiting for escalation to occur.");
264 void prepare_for_wdog(pwrmgr_sleep_resets_lib_modes_t mode) {
265 if (mode == kPwrmgrSleepResetsLibModesActive) {
269 bool deep_sleep = mode == kPwrmgrSleepResetsLibModesDeepSleep;
273 : kDifPwrmgrDomainOptionUsbClockInLowPower |
276 kDifPwrmgrDomainOptionMainPowerInLowPower;
281 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
282 pwrmgr, sysrst_ctrl_wakeup_sources, config));
286 CHECK(
false,
"Failed to reset!");
289 void prepare_for_sysrst(pwrmgr_sleep_resets_lib_modes_t mode) {
290 if (mode == kPwrmgrSleepResetsLibModesActive) {
291 LOG_INFO(
"Sysrst reset in active mode");
295 bool deep_sleep = mode == kPwrmgrSleepResetsLibModesDeepSleep;
299 : kDifPwrmgrDomainOptionUsbClockInLowPower |
302 kDifPwrmgrDomainOptionMainPowerInLowPower;
303 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
304 pwrmgr, sysrst_ctrl_wakeup_sources, config));
308 LOG_INFO(
"Sysrst reset in %s sleep mode", deep_sleep ?
"deep" :
"normal");
313 CHECK(
false,
"Failed to reset!");
316 void ottf_external_isr(uint32_t *exc_info) {
329 (dif_aon_timer_irq_t)(irq_id -
335 kDifAlertHandlerClassA));
336 CHECK_DIF_OK(dif_aon_timer_irq_acknowledge(aon_timer, irq));
339 "AON Timer Wdog should not bark");
350 alert_handler, kDifAlertHandlerClassA, &state));
354 CHECK_DIF_OK(dif_alert_handler_irq_acknowledge(alert_handler, irq));