11 #include "sw/device/lib/testing/lc_ctrl_testutils.h"
12 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
14 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
18 #define LC_TOKEN_SIZE 16
20 OTTF_DEFINE_TEST_CONFIG();
22 static dif_lc_ctrl_t lc;
23 static dif_otp_ctrl_t otp;
24 static dif_rstmgr_t rstmgr;
32 static volatile const uint8_t kDestState;
35 static volatile const uint8_t kLcExitToken[LC_TOKEN_SIZE] = {
36 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
37 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
41 static volatile const uint8_t kOtpExitToken[LC_TOKEN_SIZE] = {
42 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
43 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
47 static volatile const uint8_t kOtpUnlockToken[LC_TOKEN_SIZE] = {
48 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
49 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
53 static volatile const uint8_t kLcRmaToken[LC_TOKEN_SIZE] = {
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
59 static volatile const uint8_t kOtpRmaToken[LC_TOKEN_SIZE] = {
60 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
61 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
64 static void lock_otp_secret0_partition(
void) {
65 uint64_t otp_unlock_token_0 = 0;
66 uint64_t otp_unlock_token_1 = 0;
67 for (
int i = 0; i < LC_TOKEN_SIZE; i++) {
68 if (i < LC_TOKEN_SIZE / 2) {
69 otp_unlock_token_0 |= (uint64_t)kOtpUnlockToken[i] << (i * 8);
71 otp_unlock_token_1 |= (uint64_t)kOtpUnlockToken[i]
72 << ((i - LC_TOKEN_SIZE / 2) * 8);
76 uint64_t otp_exit_token_0 = 0;
77 uint64_t otp_exit_token_1 = 0;
78 for (
int i = 0; i < LC_TOKEN_SIZE; i++) {
79 if (i < LC_TOKEN_SIZE / 2) {
80 otp_exit_token_0 |= (uint64_t)kOtpExitToken[i] << (i * 8);
82 otp_exit_token_1 |= (uint64_t)kOtpExitToken[i]
83 << ((i - LC_TOKEN_SIZE / 2) * 8);
90 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
94 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
98 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
102 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
106 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
109 static void lock_otp_secret2_partition(
void) {
111 uint64_t otp_rma_token_0 = 0;
112 uint64_t otp_rma_token_1 = 0;
113 for (
int i = 0; i < LC_TOKEN_SIZE; i++) {
114 if (i < LC_TOKEN_SIZE / 2) {
115 otp_rma_token_0 |= (uint64_t)kOtpRmaToken[i] << (i * 8);
117 otp_rma_token_1 |= (uint64_t)kOtpRmaToken[i]
118 << ((i - LC_TOKEN_SIZE / 2) * 8);
124 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
128 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
132 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
157 LOG_INFO(
"Start LC walkthrough %d test.", kDestState);
161 CHECK_DIF_OK(dif_lc_ctrl_init(lc_reg, &lc));
165 CHECK_DIF_OK(dif_otp_ctrl_init(otp_reg, &otp));
167 CHECK_DIF_OK(dif_rstmgr_init(
170 LOG_INFO(
"Read and check LC state and count.");
175 CHECK_STATUS_OK(lc_ctrl_testutils_check_transition_count(&lc, 1));
180 if (!secret0_locked) {
181 LOG_INFO(
"In TestUnlocked0 state. Write and lock OTP secret0 partition.");
182 lock_otp_secret0_partition();
183 LOG_INFO(
"Written and locked OTP secret0 partition!");
188 LOG_INFO(
"LC RMA token start with %08x", kLcRmaToken[0]);
193 "In TestUnlocked0 state. Issue LC state transfer to destination "
196 for (
int i = 0; i < LC_TOKEN_SIZE; i++) {
198 token.data[i] = kLcExitToken[i];
210 bool use_ext_clock =
false;
213 "LC transition configuration failed!");
216 LOG_INFO(
"Waiting for LC transition done and reboot.");
222 }
else if (curr_state == kDestState) {
227 if (!secret2_locked) {
229 "In destination state. Check LC count and lock secret2 partition.");
230 CHECK(curr_state == kDestState);
231 CHECK_STATUS_OK(lc_ctrl_testutils_check_transition_count(&lc, 2));
233 lock_otp_secret2_partition();
234 LOG_INFO(
"Written and locked OTP secret2 partition in next power cycle!");
242 CHECK_STATUS_OK(lc_ctrl_testutils_check_transition_count(&lc, 2));
248 "In destination state. Check LC count and transfer to RMA "
250 CHECK_STATUS_OK(lc_ctrl_testutils_check_transition_count(&lc, 2));
254 for (
int i = 0; i < LC_TOKEN_SIZE; i++) {
255 token.data[i] = kLcRmaToken[i];
261 bool use_ext_clock =
true;
263 use_ext_clock, &token),
264 "LC transition configuration failed!");
267 LOG_INFO(
"Waiting for LC RMA transition done and reboot.");
275 LOG_INFO(
"In RMA state. Check LC count and exit.");
277 CHECK_STATUS_OK(lc_ctrl_testutils_check_transition_count(&lc, 3));