Software APIs
rstmgr_functest.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 <stdbool.h>
6 #include <stdint.h>
7 
9 #include "sw/device/lib/testing/rstmgr_testutils.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
12 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
13 #include "sw/device/silicon_creator/lib/drivers/rstmgr.h"
14 #include "sw/device/silicon_creator/lib/error.h"
15 
16 OTTF_DEFINE_TEST_CONFIG();
17 
18 /**
19  * Test phases tracked in retention RAM.
20  */
21 enum {
22  kTestPhaseInit = 0,
23  kTestPhaseReset = 1,
24  kTestPhaseDone = 2,
25 };
26 
27 bool test_main(void) {
28  // Read and clear reset reason.
29  uint32_t reason = rstmgr_testutils_reason_get();
30  rstmgr_testutils_reason_clear();
31  LOG_INFO("Reset reason: 0x%08x", reason);
32  // This test assumes that reset reason is unique.
33  CHECK(bitfield_popcount32(reason) == 1, "Expected exactly 1 reset reason.");
34 
35  // Use the part of the retention RAM reserved for the silicon owner to store
36  // the test phase.
37  uint32_t *phase = &retention_sram_get()->owner.reserved[0];
38 
39  if (bitfield_bit32_read(reason, kRstmgrReasonPowerOn)) {
40  // Clear retention RAM on power-on reset.
41  retention_sram_clear();
42  // Request a system reset.
43  *phase = kTestPhaseReset;
44  rstmgr_reset();
45  CHECK(false, "Should have reset before this line."); // Unreachable
46  } else if (bitfield_bit32_read(reason, kRstmgrReasonSoftwareRequest)) {
47  LOG_INFO("Detected software reset.");
48  CHECK(*phase == kTestPhaseReset, "Unexpected test phase: 0x%08x", *phase);
49  *phase = kTestPhaseDone;
50  return true;
51  }
52  LOG_ERROR("Unknown reset reason: 0x%08x", reason);
53  return false;
54 }