Software APIs
lc_ctrl_scrap_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 <stdbool.h>
7 
12 #include "sw/device/lib/testing/lc_ctrl_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
15 
16 static dif_lc_ctrl_t lc;
17 
18 // Symbol that tells if the transition should be done via SW (overriden by
19 // Host).
20 static volatile const uint8_t kPerformTransitionBySW = 0;
21 
22 /**
23  * Tests the state transition request handshake between LC_CTRL and OTP_CTRL for
24  * transition into `Scrap` state.
25  *
26  * 1). OTP pre-load image with lc_count = `8`.
27  * 2). Program LC state transition request to advance to `Scrap` state,
28  * if doing the transition through SW.
29  * 3). Return control to Host side.
30  * 4). Host will wait for LC_CTRL to enter PostTransition state, then issue a
31  * hard reset 5). Host will check the LC state is `Scrap`
32  *
33  * In summary, this sequence walks through the following LC states:
34  * "Any Source State (!Scrap) -> Scrap".
35  */
36 
37 bool execute_lc_ctrl_scrap_test(bool use_ext_clk) {
38  if (kPerformTransitionBySW) {
39  LOG_INFO("Start LC_CTRL scrap test");
40 
41  dt_lc_ctrl_t kLcCtrlDt = (dt_lc_ctrl_t)0;
42  static_assert(kDtLcCtrlCount == 1,
43  "This test expects exactly one LC controller");
44  CHECK_DIF_OK(dif_lc_ctrl_init_from_dt(kLcCtrlDt, &lc));
45 
46  // Acquire the mutex and perform a transition to SCRAP
47  CHECK_DIF_OK(dif_lc_ctrl_mutex_try_acquire(&lc));
48  CHECK_DIF_OK(
49  dif_lc_ctrl_configure(&lc, kDifLcCtrlStateScrap, use_ext_clk, NULL),
50  "LC transition configuration failed!");
51  CHECK_DIF_OK(dif_lc_ctrl_transition(&lc), "LC transition failed!");
52 
53  LOG_INFO("Waiting for LC transtition done and reboot.");
54 
55  // Release the access mutex to LC conroller's registers before
56  // finishing the SW.
57  CHECK_DIF_OK(dif_lc_ctrl_mutex_release(&lc));
58 
59  // At this point a reset should be issued for the new LC state to take
60  // effect. However, as we are transitioning into SCRAP, the core won't be
61  // able to execute any SW code, so this sequence will never return true.
62  // Instead, the reset task will be handled by Host code.
64  CHECK(false, "Should have reset before this line.");
65  } else {
66  LOG_INFO("LC transition is performed by JTAG. Skipping SW...");
67  }
68 
69  return true;
70 }
71 
72 OTTF_DEFINE_TEST_CONFIG();
73 
74 bool test_main(void) {
75  return execute_lc_ctrl_scrap_test(/*use_ext_clk=*/false);
76 }