Software APIs
sram_device_info_flash_wr_functest.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 
14 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
15 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
16 #include "sw/device/lib/testing/pinmux_testutils.h"
17 #include "sw/device/lib/testing/test_framework/check.h"
18 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
19 #include "sw/device/silicon_creator/manuf/lib/flash_info_fields.h"
20 #include "sw/device/silicon_creator/manuf/lib/individualize_sw_cfg.h"
21 #include "sw/device/silicon_creator/manuf/tests/test_wafer_auth_secret.h"
22 
24 #include "otp_ctrl_regs.h" // Generated.
25 
26 OTTF_DEFINE_TEST_CONFIG();
27 
28 static dif_uart_t uart;
29 static dif_otp_ctrl_t otp_ctrl;
30 static dif_pinmux_t pinmux;
31 static dif_flash_ctrl_state_t flash_ctrl_state;
32 static dif_lc_ctrl_t lc_ctrl;
33 
34 /**
35  * Initializes all DIF handles used in this SRAM program.
36  */
37 static status_t peripheral_handles_init(void) {
39  &flash_ctrl_state,
41  TRY(dif_lc_ctrl_init(
43  TRY(dif_otp_ctrl_init(
46  &pinmux));
48  &uart));
49  return OK_STATUS();
50 }
51 
52 bool test_main(void) {
53  CHECK_STATUS_OK(peripheral_handles_init());
54 
55  // Initialize UART (for console, since we do not have the OTTF).
56  pinmux_testutils_init(&pinmux);
57 
58  CHECK(kUartBaudrate <= UINT32_MAX, "kUartBaudrate must fit in uint32_t");
59  CHECK(kClockFreqPeripheralHz <= UINT32_MAX,
60  "kClockFreqPeripheralHz must fit in uint32_t");
61  CHECK_DIF_OK(dif_uart_configure(
62  &uart, (dif_uart_config_t){
63  .baudrate = (uint32_t)kUartBaudrate,
64  .clk_freq_hz = (uint32_t)kClockFreqPeripheralHz,
65  .parity_enable = kDifToggleDisabled,
66  .parity = kDifUartParityEven,
67  .tx_enable = kDifToggleEnabled,
68  .rx_enable = kDifToggleEnabled,
69  }));
70  base_uart_stdout(&uart);
71 
72  LOG_INFO("Executing from SRAM.");
73 
74  // Read LC state.
76  CHECK_DIF_OK(dif_lc_ctrl_get_state(&lc_ctrl, &lc_state));
77 
78  switch (lc_state) {
87  LOG_INFO("Writing to the isolated flash partition.");
88  uint32_t byte_address = 0;
89  CHECK_STATUS_OK(flash_ctrl_testutils_info_region_setup(
90  &flash_ctrl_state, kFlashInfoFieldWaferAuthSecret.page,
91  kFlashInfoFieldWaferAuthSecret.bank,
92  kFlashInfoFieldWaferAuthSecret.partition, &byte_address));
93  CHECK_STATUS_OK(flash_ctrl_testutils_erase_and_write_page(
94  &flash_ctrl_state, byte_address,
95  kFlashInfoFieldWaferAuthSecret.partition, kExpectedWaferAuthSecret,
96  kDifFlashCtrlPartitionTypeInfo,
97  kFlashInfoWaferAuthSecretSizeIn32BitWords));
98  LOG_INFO("Enabling ROM execution to enable bootstrap after reset.");
99  CHECK_STATUS_OK(manuf_individualize_device_creator_sw_cfg(
100  &otp_ctrl, &flash_ctrl_state));
101  CHECK_STATUS_OK(manuf_individualize_device_owner_sw_cfg(&otp_ctrl));
102  LOG_INFO("Done. Perform an LC transition and run flash stage.");
103  break;
104  default:
105  return false;
106  }
107 
108  return true;
109 }