27 #include "sw/device/lib/runtime/pmp.h"
29 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
30 #include "sw/device/lib/testing/pinmux_testutils.h"
31 #include "sw/device/lib/testing/test_framework/check.h"
32 #include "sw/device/lib/testing/test_framework/ottf_test_config.h"
33 #include "sw/device/lib/testing/test_framework/status.h"
36 #include "flash_ctrl_regs.h"
39 #include "rv_timer_regs.h"
41 OTTF_DEFINE_TEST_CONFIG();
48 kRomTestLocContent = 0x8082,
51 kFlashBytesPerPage = FLASH_CTRL_PARAM_BYTES_PER_PAGE,
55 kRomExtPageCount = CHIP_ROM_EXT_SIZE_MAX / kFlashBytesPerPage,
59 kBank1StartPageNum = 256 + kRomExtPageCount,
62 kBank1StartPageNum * kFlashBytesPerPage,
67 const uint32_t kFlashTestLocContent = 0x00008067;
68 void (*flash_test_gadget)(void) = (
void (*)(void))kFlashTestLoc;
70 volatile uint32_t *kMMIOTestLoc1 =
72 RV_TIMER_COMPARE_LOWER0_0_REG_OFFSET);
73 const uint32_t kMMIOTestLoc1Content = 0x126d8c15;
75 volatile uint32_t *kMMIOTestLoc2 =
77 const uint32_t kMMIOTestLoc2Content = 0xe4210e64;
82 static void setup_uart(
void) {
84 static dif_uart_t uart0;
85 static dif_pinmux_t pinmux;
88 CHECK_DIF_OK(dif_pinmux_init(
90 CHECK_DIF_OK(dif_uart_init(
94 pinmux_testutils_init(&pinmux);
95 CHECK(
kUartBaudrate <= UINT32_MAX,
"kUartBaudrate must fit in uint32_t");
97 "kClockFreqPeripheralHz must fit in uint32_t");
115 static void use_icache(
bool enable) {
123 CHECK((csr & 1) == enable,
"Couldn't enable or disable icache.");
129 static void setup_flash(
void) {
132 .
lock = kPmpRegionLockLocked,
133 .permissions = kPmpRegionPermissionsReadWriteExecute,
135 pmp_region_configure_napot_result_t result = pmp_region_configure_napot(
137 CHECK(result == kPmpRegionConfigureNapotOk,
138 "Load configuration failed, error code = %d", result);
142 pmp_region_configure_result_t configure_result =
143 pmp_region_configure_off(5, 0);
144 CHECK(configure_result == kPmpRegionConfigureOk,
145 "Load configuration failed, error code = %d", configure_result);
153 CHECK_STATUS_OK(flash_ctrl_testutils_wait_for_init(&flash_ctrl));
156 .
rd_en = kMultiBitBool4True,
157 .prog_en = kMultiBitBool4True,
158 .erase_en = kMultiBitBool4True,
159 .scramble_en = kMultiBitBool4False,
160 .ecc_en = kMultiBitBool4False,
161 .high_endurance_en = kMultiBitBool4False};
163 .
base = kBank1StartPageNum, .size = 0x1, .properties = region_properties};
175 CHECK_STATUS_OK(flash_ctrl_testutils_erase_and_write_page(
179 &kFlashTestLocContent,
180 kDifFlashCtrlPartitionTypeData,
192 LOG_INFO(
"Testing Load from ROM Location.");
197 volatile uint16_t *test_loc;
198 for (test_loc = (uint16_t *)kRomTestLocStart;
199 test_loc < (uint16_t *)kRomTestLocEnd; test_loc++) {
200 if (*test_loc == kRomTestLocContent) {
204 CHECK(test_loc != (uint16_t *)kRomTestLocEnd,
205 "Couldn't find the expected content in ROM test location.");
206 LOG_INFO(
"Found the expected content at 0x%p", test_loc);
207 void (*rom_test_gadget)(void) = (
void (*)(void))test_loc;
210 LOG_INFO(
"Running an instruction from ROM with icache disabled.");
214 LOG_INFO(
"Running an instruction from ROM with icache enabled.");
218 LOG_INFO(
"Testing Store to and Load from MMIO Location 1");
219 *kMMIOTestLoc1 = kMMIOTestLoc1Content;
220 uint32_t load = *kMMIOTestLoc1;
222 load == kMMIOTestLoc1Content,
223 "The content of the MMIO address was 0x%08x and not the expected value.",
226 LOG_INFO(
"Testing Store to and Load from MMIO Location 2");
227 *kMMIOTestLoc2 = kMMIOTestLoc2Content;
228 load = *kMMIOTestLoc2;
230 load == kMMIOTestLoc2Content,
231 "The content of the MMIO address was 0x%08x and not the expected value.",
234 LOG_INFO(
"Setting up the flash test location.");
238 load = *(
volatile const uint32_t *)kFlashTestLoc;
240 load == kFlashTestLocContent,
241 "The content of the Flash address was 0x%08x and not the expected value.",
245 LOG_INFO(
"Running an instruction from Flash with icache disabled.");
248 LOG_INFO(
"Running an instruction from Flash with icache enabled.");
251 test_status_set(kTestStatusPassed);