5 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
10 #include "sw/device/lib/testing/test_framework/check.h"
12 #define MODULE_ID MAKE_MODULE_ID('o', 'c', 't')
20 const uint16_t kOtpDaiTimeoutUs = 5000;
25 static bool dai_finished(
const dif_otp_ctrl_t *otp_ctrl) {
32 status_t otp_ctrl_testutils_dai_access_error_check(
33 const dif_otp_ctrl_t *otp_ctrl, exp_test_result_t exp_result,
37 if (exp_result == kExpectFailed) {
39 LOG_ERROR(
"Expected a DAI error for access to 0x%x", address);
43 LOG_ERROR(
"Expected access locked error for access to 0x%x", address);
47 LOG_ERROR(
"No DAI error expected for access to 0x%x", address);
50 LOG_ERROR(
"No DAI error code expected for access to 0x%x", address);
56 status_t otp_ctrl_testutils_wait_for_dai(
const dif_otp_ctrl_t *otp_ctrl) {
61 status_t otp_ctrl_testutils_lock_partition(
const dif_otp_ctrl_t *otp,
65 return otp_ctrl_testutils_wait_for_dai(otp);
68 status_t otp_ctrl_testutils_dai_read32(
const dif_otp_ctrl_t *otp,
70 uint32_t address, uint32_t *result) {
71 TRY(otp_ctrl_testutils_wait_for_dai(otp));
73 TRY(otp_ctrl_testutils_wait_for_dai(otp));
78 status_t otp_ctrl_testutils_dai_read32_array(
const dif_otp_ctrl_t *otp,
80 uint32_t start_address,
81 uint32_t *buffer,
size_t len) {
82 uint32_t stop_address = start_address + (len *
sizeof(uint32_t));
83 for (uint32_t addr = start_address, i = 0; addr < stop_address;
84 addr +=
sizeof(uint32_t), ++i) {
85 TRY(otp_ctrl_testutils_wait_for_dai(otp));
87 TRY(otp_ctrl_testutils_wait_for_dai(otp));
93 status_t otp_ctrl_testutils_dai_read64(
const dif_otp_ctrl_t *otp,
95 uint32_t address, uint64_t *result) {
96 TRY(otp_ctrl_testutils_wait_for_dai(otp));
98 TRY(otp_ctrl_testutils_wait_for_dai(otp));
103 status_t otp_ctrl_testutils_dai_read64_array(
const dif_otp_ctrl_t *otp,
105 uint32_t start_address,
106 uint64_t *buffer,
size_t len) {
107 uint32_t stop_address = start_address + (len *
sizeof(uint64_t));
108 for (uint32_t addr = start_address, i = 0; addr < stop_address;
109 addr +=
sizeof(uint64_t), ++i) {
110 TRY(otp_ctrl_testutils_wait_for_dai(otp));
112 TRY(otp_ctrl_testutils_wait_for_dai(otp));
126 static status_t otp_ctrl_dai_write_error_check(
const dif_otp_ctrl_t *otp) {
139 status_t otp_ctrl_testutils_dai_write32(
const dif_otp_ctrl_t *otp,
141 uint32_t start_address,
142 const uint32_t *buffer,
size_t len) {
146 bool check_before_write = (
147 #ifdef OPENTITAN_IS_EARLGREY
148 partition == kDifOtpCtrlPartitionRotCreatorAuthCodesign ||
149 partition == kDifOtpCtrlPartitionRotCreatorAuthState ||
153 uint32_t stop_address = start_address + (len *
sizeof(uint32_t));
154 for (uint32_t addr = start_address, i = 0; addr < stop_address;
155 addr +=
sizeof(uint32_t), ++i) {
157 if (check_before_write) {
158 TRY(otp_ctrl_testutils_dai_read32(otp, partition, addr, &read_data));
159 if (read_data == buffer[i]) {
162 if (read_data != 0) {
163 LOG_ERROR(
"OTP partition: %d addr[0x%x] got: 0x%08x, expected: 0x%08x",
164 partition, addr, read_data, buffer[i]);
169 TRY(otp_ctrl_testutils_wait_for_dai(otp));
171 TRY(otp_ctrl_testutils_wait_for_dai(otp));
172 TRY(otp_ctrl_dai_write_error_check(otp));
174 TRY(otp_ctrl_testutils_dai_read32(otp, partition, addr, &read_data));
175 if (read_data != buffer[i]) {
182 status_t otp_ctrl_testutils_dai_write64(
const dif_otp_ctrl_t *otp,
184 uint32_t start_address,
185 const uint64_t *buffer,
size_t len) {
186 uint32_t stop_address = start_address + (len *
sizeof(uint64_t));
187 for (uint32_t addr = start_address, i = 0; addr < stop_address;
188 addr +=
sizeof(uint64_t), ++i) {
189 TRY(otp_ctrl_testutils_wait_for_dai(otp));
191 TRY(otp_ctrl_testutils_wait_for_dai(otp));
192 TRY(otp_ctrl_dai_write_error_check(otp));
195 TRY(otp_ctrl_testutils_dai_read64(otp, partition, addr, &read_data));
196 if (read_data != buffer[i]) {