Software APIs
pwrmgr_sleep_power_glitch_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
9 #include "sw/device/lib/testing/pwrmgr_testutils.h"
10 #include "sw/device/lib/testing/rstmgr_testutils.h"
11 #include "sw/device/lib/testing/test_framework/check.h"
13 
15 
16 OTTF_DEFINE_TEST_CONFIG();
17 
18 // When the test first runs the rstmgr's `reset_info` CSR should have the POR
19 // bit set, the code clears reset_info and puts the chip in shallow sleep. WFI
20 // causes core_sleeping to rise, and that causes the SV side to glitch the main
21 // power rail, causing a pwrmgr internally generated reset. The next time the
22 // test runs is after the power glitch reset, which is confirmed reading the
23 // `reset_info` CSR.
24 bool test_main(void) {
25  dif_pwrmgr_t pwrmgr;
26  dif_rstmgr_t rstmgr;
27 
28  // Initialize pwrmgr since this will put the chip in shallow sleep.
29  CHECK_DIF_OK(dif_pwrmgr_init(
31 
32  // Initialize rstmgr since this will check some registers.
33  CHECK_DIF_OK(dif_rstmgr_init(
35 
36  // Notice we are clearing rstmgr's RESET_INFO, so after the power glitch there
37  // is only one bit set.
38 
39  if (UNWRAP(rstmgr_testutils_is_reset_info(&rstmgr, kDifRstmgrResetInfoPor))) {
40  LOG_INFO("Powered up for the first time, begin test");
41 
42  CHECK(UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) == true);
43 
44  CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
45 
46  // Configure shallow sleep.
47  CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
48  &pwrmgr, kDifPwrmgrWakeupRequestSourceFive,
49  kDifPwrmgrDomainOptionMainPowerInLowPower));
50 
51  // This causes core_sleeping to rise and triggers the injection of the
52  // power glitch. Enter shallow sleep mode.
53  LOG_INFO("Issue WFI to enter sleep");
55 
56  } else {
57  LOG_INFO("Checking reset status.");
59  CHECK_STATUS_OK(rstmgr_testutils_post_reset(
60  &rstmgr, kDifRstmgrResetInfoPowerUnstable, 0, 0, 0, 0));
61  LOG_INFO(
62  "Reset status indicates a main power glitch and low power exit reset");
63  }
64  return true;
65 }