Software APIs
sram_ft_individualize.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 <stdint.h>
6 
13 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
14 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
15 #include "sw/device/lib/testing/pinmux_testutils.h"
16 #include "sw/device/lib/testing/test_framework/check.h"
17 #include "sw/device/lib/testing/test_framework/ottf_console.h"
18 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
19 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
20 #include "sw/device/silicon_creator/manuf/base/flash_info_permissions.h"
21 #include "sw/device/silicon_creator/manuf/lib/flash_info_fields.h"
22 #include "sw/device/silicon_creator/manuf/lib/individualize.h"
23 #include "sw/device/silicon_creator/manuf/lib/individualize_sw_cfg.h"
24 #include "sw/device/silicon_creator/manuf/lib/otp_fields.h"
25 
27 
28 OTTF_DEFINE_TEST_CONFIG(.console.type = kOttfConsoleSpiDevice,
29  .console.base_addr = TOP_EARLGREY_SPI_DEVICE_BASE_ADDR,
30  .console.test_may_clobber = false, );
31 
32 static dif_flash_ctrl_state_t flash_ctrl_state;
33 static dif_otp_ctrl_t otp_ctrl;
34 static dif_pinmux_t pinmux;
35 
36 static manuf_ft_individualize_data_t in_data;
37 static uint32_t device_id[kHwCfgDeviceIdSizeIn32BitWords];
38 static uint32_t ast_cfg_data[kFlashInfoAstCalibrationDataSizeIn32BitWords];
39 
40 /**
41  * Initializes all DIF handles used in this SRAM program.
42  */
43 static status_t peripheral_handles_init(void) {
45  &flash_ctrl_state,
47  TRY(dif_otp_ctrl_init(
50  &pinmux));
51  return OK_STATUS();
52 }
53 
54 /**
55  * Print data stored in flash info page 0 to console for manual verification
56  * purposes during silicon bring-up.
57  */
58 static status_t print_flash_info_0_data_to_console(void) {
59  uint32_t byte_address = 0;
60  TRY(flash_ctrl_testutils_info_region_setup_properties(
61  &flash_ctrl_state, kFlashInfoFieldDeviceId.page,
62  kFlashInfoFieldDeviceId.bank, kFlashInfoFieldDeviceId.partition,
63  kFlashInfoPage0Permissions, &byte_address));
64 
65  LOG_INFO("Device ID:");
66  TRY(manuf_flash_info_field_read(&flash_ctrl_state, kFlashInfoFieldDeviceId,
67  device_id, kHwCfgDeviceIdSizeIn32BitWords));
68  for (size_t i = 0; i < kHwCfgDeviceIdSizeIn32BitWords; ++i) {
69  LOG_INFO("0x%08x", device_id[i]);
70  }
71 
72  LOG_INFO("AST Calibration Values:");
73  TRY(manuf_flash_info_field_read(
74  &flash_ctrl_state, kFlashInfoFieldAstCalibrationData, ast_cfg_data,
75  kFlashInfoAstCalibrationDataSizeIn32BitWords));
76  for (size_t i = 0; i < kFlashInfoAstCalibrationDataSizeIn32BitWords; ++i) {
77  LOG_INFO("0x%08x", ast_cfg_data[i]);
78  }
79 
80  return OK_STATUS();
81 }
82 
83 /**
84  * Provision OTP {CreatorSw,OwnerSw,Hw}Cfg and RotCreatorAuth{Codesign,State}
85  * partitions.
86  *
87  * Note: CreatorSwCfg and OwnerSwCfg partitions are not locked yet, as not
88  * all fields can be programmed until the personalization stage.
89  */
90 static status_t provision(ujson_t *uj) {
91  LOG_INFO("Waiting for FT SRAM provisioning data ...");
92  TRY(ujson_deserialize_manuf_ft_individualize_data_t(uj, &in_data));
93  TRY(manuf_individualize_device_hw_cfg(&flash_ctrl_state, &otp_ctrl,
94  kFlashInfoPage0Permissions,
95  in_data.device_id));
96  TRY(manuf_individualize_device_creator_sw_cfg(&otp_ctrl, &flash_ctrl_state));
97  TRY(manuf_individualize_device_owner_sw_cfg(&otp_ctrl));
98  TRY(manuf_individualize_device_rot_creator_auth_codesign(&otp_ctrl));
99  TRY(manuf_individualize_device_rot_creator_auth_state(&otp_ctrl));
100  LOG_INFO("FT SRAM provisioning done.");
101  return OK_STATUS();
102 }
103 
104 bool test_main(void) {
105  CHECK_STATUS_OK(peripheral_handles_init());
106  pinmux_testutils_init(&pinmux);
107  ottf_console_init();
108  ujson_t uj = ujson_ottf_console();
109 
110  // Print flash data to console (for manual verification purposes) and perform
111  // provisioning operations.
112  CHECK_STATUS_OK(print_flash_info_0_data_to_console());
113  CHECK_STATUS_OK(provision(&uj));
114 
115  // Halt the CPU here to enable JTAG to perform an LC transition to mission
116  // mode, as ROM execution should be active now.
117  abort();
118 
119  return true;
120 }