Software APIs
lc_ctrl_otp_hw_cfg0_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 
14 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
15 #include "sw/device/lib/testing/test_framework/check.h"
17 
19 
20 static dif_otp_ctrl_t otp;
21 static dif_lc_ctrl_t lc;
22 
23 OTTF_DEFINE_TEST_CONFIG();
24 static const uint8_t kNumDeviceId = 8;
25 
26 /**
27  * Read and return 32-bit OTP data via the DAI interface.
28  */
29 static void otp_ctrl_dai_read_32(const dif_otp_ctrl_t *otp,
30  dif_otp_ctrl_partition_t partition,
31  uint32_t address, uint32_t *buf) {
32  CHECK_DIF_OK(dif_otp_ctrl_dai_read_start(otp, partition, address));
33  CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(otp));
34  CHECK_DIF_OK(dif_otp_ctrl_dai_read32_end(otp, buf));
35 }
36 
37 /**
38  * Tests that the OTP sends correct HW_CFG0 partition data to the receiving IPs.
39  */
40 // TODO: needs to support other recipients besides LC_CTRL.
41 bool test_main(void) {
42  CHECK_DIF_OK(dif_otp_ctrl_init(
44 
45  mmio_region_t lc_reg =
47  CHECK_DIF_OK(dif_lc_ctrl_init(lc_reg, &lc));
48 
49  dif_otp_ctrl_config_t config = {
50  .check_timeout = 100000,
51  .integrity_period_mask = 0x3ffff,
52  .consistency_period_mask = 0x3ffffff,
53  };
54  CHECK_DIF_OK(dif_otp_ctrl_configure(&otp, config));
55 
56  // Read out Device ID from LC_CTRL's `device_id` registers.
57  dif_lc_ctrl_device_id_t lc_device_id;
58  CHECK_DIF_OK(dif_lc_ctrl_get_device_id(&lc, &lc_device_id));
59 
60  // Read out Device ID from OTP DAI read, and compare the value with LC_CTRL's
61  // `device_id` registers.
62  // TODO: current HW CFG value is randomly genenrated from the HJSON file,
63  // plan to backdoor inject.
64  uint32_t otp_device_id;
65  for (uint32_t i = 0; i < kNumDeviceId; i++) {
66  otp_ctrl_dai_read_32(&otp, kDifOtpCtrlPartitionHwCfg0, i * 4,
67  &otp_device_id);
68 
69  CHECK(otp_device_id == lc_device_id.data[i],
70  "Device_ID_%d mismatch bewtween otp_ctrl: %08x and lc_ctrl: %08x", i,
71  otp_device_id, lc_device_id.data[i]);
72  }
73  return true;
74 }