Software APIs
ast_program_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 "sw/device/lib/base/crc32.h"
6 #include "sw/device/lib/base/status.h"
9 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
11 #include "sw/device/silicon_creator/manuf/lib/flash_info_fields.h"
12 
13 OTTF_DEFINE_TEST_CONFIG(.console.test_may_clobber = true);
14 
15 static uint32_t ast_cfg_data[kFlashInfoAstCalibrationDataSizeIn32BitWords] = {
16  0};
17 
18 // These symbols come from the `ast_program` module.
19 extern status_t ast_program_config(bool verbose);
20 extern status_t ast_program_init(bool verbose);
21 extern dif_flash_ctrl_state_t flash_state;
22 
23 // Light-weight mocking: we export `ast_write` to the `ast_program` module so we
24 // can get a call for every word that would be written to AST.
25 uint32_t ast_crc;
26 uint32_t ast_nr_writes;
27 void ast_write(uint32_t addr, uint32_t data) {
28  crc32_add32(&ast_crc, data);
29  ast_nr_writes += 1;
30 }
31 
32 /**
33  * Reset the CRC and count between tests.
34  */
35 static void test_state_reset(void) {
36  ast_nr_writes = 0;
37  crc32_init(&ast_crc);
38 }
39 
40 /**
41  * Erase the INFO page containing the AST calibration data.
42  */
43 static status_t erase_page(void) {
45  uint32_t byte_address =
46  (kFlashInfoFieldAstCalibrationData.page * device_info.bytes_per_page);
47 
48  return flash_ctrl_testutils_erase_page(
49  &flash_state, byte_address, kFlashInfoFieldAstCalibrationData.partition,
50  kDifFlashCtrlPartitionTypeInfo);
51 }
52 
53 /**
54  * Program a blob into the AST calibration info page.
55  */
56 static status_t program_page(void) {
58  uint32_t byte_address =
59  (kFlashInfoFieldAstCalibrationData.page * device_info.bytes_per_page) +
60  kFlashInfoFieldAstCalibrationData.byte_offset;
61 
62  // Set dummy AST values for testing.
63  for (size_t i = 0; i < ARRAYSIZE(ast_cfg_data); ++i) {
64  ast_cfg_data[i] = i;
65  }
66  return flash_ctrl_testutils_write(
67  &flash_state, byte_address, kFlashInfoFieldAstCalibrationData.partition,
68  ast_cfg_data, kDifFlashCtrlPartitionTypeInfo,
69  kFlashInfoAstCalibrationDataSizeIn32BitWords);
70 }
71 
72 static status_t execute_test(void) {
73  LOG_INFO("Initialize");
74  TRY(ast_program_init(true));
75  LOG_INFO(
76  "Erase and program a sample blob into the INFO page, and verify AST "
77  "programming.");
78  TRY(erase_page());
79  test_state_reset();
80  TRY(program_page());
81  TRY(ast_program_config(true));
82  uint32_t crc =
83  crc32(ast_cfg_data,
84  kFlashInfoAstCalibrationDataSizeIn32BitWords * sizeof(uint32_t));
85  TRY_CHECK(ast_nr_writes == 39);
86  TRY_CHECK(crc32_finish(&ast_crc) == crc);
87  return OK_STATUS();
88 }
89 
90 bool test_main(void) {
91  // Run once just to get the flash controller initialzed.
92  status_t sts = execute_test();
93  LOG_INFO("result = %r", sts);
94  return status_ok(sts);
95 }