Software APIs
pwrmgr_all_reset_reqs_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 
5 #include <assert.h>
6 #include <limits.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 
16 #include "sw/device/lib/runtime/irq.h"
18 #include "sw/device/lib/testing/ret_sram_testutils.h"
19 #include "sw/device/lib/testing/rstmgr_testutils.h"
20 #include "sw/device/lib/testing/rv_plic_testutils.h"
21 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h"
22 #include "sw/device/lib/testing/test_framework/check.h"
24 #include "sw/device/tests/pwrmgr_sleep_resets_lib.h"
25 
27 
28 // In dvsim, one run
29 // with --waves can take
30 // 1.874h | 38.068ms
31 // without --waves,
32 // 38.072m | 39.484ms
33 
34 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control = true);
35 
36 static const uint32_t kPlicTarget = kTopEarlgreyPlicTargetIbex0;
37 
38 bool test_main(void) {
39  // Enable global and external IRQ at Ibex.
40  irq_global_ctrl(true);
41  irq_external_ctrl(true);
42 
43  init_peripherals();
44 
45  ret_sram_testutils_init();
46 
47  // Enable all the Alert handler interrupts used in this test.
48  rv_plic_testutils_irq_range_enable(plic, kPlicTarget,
51 
52  // Check if there was a HW reset caused by expected cases.
54  rst_info = rstmgr_testutils_reason_get();
55  rstmgr_testutils_reason_clear();
56 
57  enum { kCounterResets = 0 };
58  if (rst_info == kDifRstmgrResetInfoPor) {
59  CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterResets));
60  }
61  CHECK_STATUS_OK(rstmgr_testutils_pre_reset(rstmgr));
62 
63  uint32_t reset_case = 0;
64  CHECK_STATUS_OK(ret_sram_testutils_counter_get(kCounterResets, &reset_case));
65  CHECK_STATUS_OK(ret_sram_testutils_counter_increment(kCounterResets));
66  LOG_INFO("New reset event");
67  LOG_INFO(" case %d, active mode. reason 0x%x", reset_case, rst_info);
68 
69  switch (reset_case) {
70  case 0:
71  CHECK(rst_info == kDifRstmgrResetInfoPor, "Wrong reset reason 0x%x",
72  rst_info);
75  prepare_for_sysrst(kPwrmgrSleepResetsLibModesActive);
76  break;
77  case 1:
78  CHECK(rst_info == kDifRstmgrResetInfoSysRstCtrl,
79  "Wrong reset reason 0x%x", rst_info);
80  LOG_INFO("Watchdog reset in deep sleep mode");
81  LOG_INFO("Let SV wait timer reset");
82  CHECK_STATUS_OK(rstmgr_testutils_pre_reset(rstmgr));
83  config_wdog(/*bark_micros=*/200, /*bite_micros=*/2 * 200);
84  prepare_for_wdog(kPwrmgrSleepResetsLibModesActive);
85  break;
86  case 2:
87  CHECK(rst_info == kDifRstmgrResetInfoWatchdog, "Wrong reset reason 0x%x",
88  rst_info);
89  LOG_INFO("Rstmgr software reset in deep sleep mode");
90  LOG_INFO("Let SV wait timer reset");
91  CHECK_STATUS_OK(rstmgr_testutils_pre_reset(rstmgr));
92  LOG_INFO("Device reset from sw");
93  // Triggering a sw reset will prevent the device from completing the
94  // setup required to enter sleep mode. This sets a watchdog, but it will
95  // most likely be wiped out by the software reset, unless they land in
96  // very close temporal proximity.
97  config_wdog(/*bark_micros=*/200, /*bite_micros=*/2 * 200);
98  // Assert rstmgr software reset request.
99  CHECK_DIF_OK(dif_rstmgr_software_device_reset(rstmgr));
100  prepare_for_wdog(kPwrmgrSleepResetsLibModesActive);
101  break;
102  case 3:
103  CHECK(rst_info == kDifRstmgrResetInfoSw, "Wrong reset reason 0x%x",
104  rst_info);
105  LOG_INFO("Booting and running normal sleep followed by escalation reset");
106  LOG_INFO("Let SV wait timer reset");
107  config_alert_handler();
108  trigger_escalation();
109  break;
110  case 4:
111  CHECK(rst_info == kDifRstmgrResetInfoEscalation,
112  "Wrong reset reason 0x%x", rst_info);
113  LOG_INFO("Last Booting");
114  return true;
115  default:
116  LOG_INFO("Booting for undefined case %d", reset_case);
117  }
118 
119  return false;
120 }