15 #include "sw/device/lib/dif/dif_rv_core_ibex.h"
20 #include "sw/device/lib/runtime/irq.h"
22 #include "sw/device/lib/testing/alert_handler_testutils.h"
23 #include "sw/device/lib/testing/ret_sram_testutils.h"
24 #include "sw/device/lib/testing/rstmgr_testutils.h"
25 #include "sw/device/lib/testing/rv_plic_testutils.h"
26 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h"
27 #include "sw/device/lib/testing/test_framework/check.h"
30 #include "alert_handler_regs.h"
33 #include "spi_device_regs.h"
34 #include "spi_host_regs.h"
35 #include "sw/device/lib/testing/autogen/isr_testutils.h"
36 #include "usbdev_regs.h"
38 OTTF_DEFINE_TEST_CONFIG();
40 static dif_rv_plic_t plic;
41 static dif_alert_handler_t alert_handler;
42 static dif_rstmgr_t rstmgr;
44 static dif_spi_host_t spi_host0;
45 static dif_spi_host_t spi_host1;
46 static dif_usbdev_t usbdev;
47 static dif_i2c_t i2c0;
48 static dif_i2c_t i2c1;
49 static dif_i2c_t i2c2;
50 static dif_rv_core_ibex_t ibex;
56 static void init_peripherals(
void) {
59 CHECK_DIF_OK(dif_rv_plic_init(base_addr, &plic));
62 CHECK_DIF_OK(dif_alert_handler_init(base_addr, &alert_handler));
64 CHECK_DIF_OK(dif_rstmgr_init(
67 CHECK_DIF_OK(dif_usbdev_init(
70 CHECK_DIF_OK(dif_spi_device_init(
73 CHECK_DIF_OK(dif_spi_host_init(
76 CHECK_DIF_OK(dif_spi_host_init(
90 CHECK_DIF_OK(dif_rv_core_ibex_init(ibex_addr, &ibex));
93 rv_plic_testutils_irq_range_enable(&plic, kPlicTarget,
99 static const uint32_t spidev_alerts[] = {
101 static const uint32_t spihost0_alerts[] = {
103 static const uint32_t spihost1_alerts[] = {
110 static const uint32_t num_spihost0_alerts =
ARRAYSIZE(spihost0_alerts);
111 static const uint32_t num_spihost1_alerts =
ARRAYSIZE(spihost1_alerts);
112 static const uint32_t num_usbdev_alerts =
ARRAYSIZE(usbdev_alerts);
113 static const uint32_t num_spidev_alerts =
ARRAYSIZE(spidev_alerts);
114 static const uint32_t num_i2c0_alerts =
ARRAYSIZE(i2c0_alerts);
115 static const uint32_t num_i2c1_alerts =
ARRAYSIZE(i2c1_alerts);
116 static const uint32_t num_i2c2_alerts =
ARRAYSIZE(i2c2_alerts);
118 static const size_t num_alerts =
119 num_spihost0_alerts + num_spihost1_alerts + num_usbdev_alerts +
120 num_i2c0_alerts + num_i2c1_alerts + num_i2c2_alerts + num_spidev_alerts;
125 typedef struct test {
153 static const test_t kPeripherals[] = {
158 .alert_ids = spihost0_alerts,
159 .num_alert_peri = num_spihost0_alerts,
160 .reset_index = kTopEarlgreyResetManagerSwResetsSpiHost0,
166 .alert_ids = spihost1_alerts,
167 .num_alert_peri = num_spihost1_alerts,
168 .reset_index = kTopEarlgreyResetManagerSwResetsSpiHost1,
174 .alert_ids = usbdev_alerts,
175 .num_alert_peri = num_usbdev_alerts,
176 .reset_index = kTopEarlgreyResetManagerSwResetsUsb,
179 .name =
"SPI_DEVICE",
182 .alert_ids = spidev_alerts,
183 .num_alert_peri = num_spidev_alerts,
184 .reset_index = kTopEarlgreyResetManagerSwResetsSpiDevice,
190 .alert_ids = i2c0_alerts,
191 .num_alert_peri = num_i2c0_alerts,
192 .reset_index = kTopEarlgreyResetManagerSwResetsI2c0,
198 .alert_ids = i2c1_alerts,
199 .num_alert_peri = num_i2c1_alerts,
200 .reset_index = kTopEarlgreyResetManagerSwResetsI2c1,
206 .alert_ids = i2c2_alerts,
207 .num_alert_peri = num_i2c2_alerts,
208 .reset_index = kTopEarlgreyResetManagerSwResetsI2c2,
220 static void alert_handler_config_peripherals(uint32_t num_peripherals,
221 const test_t *first_peripheral,
222 uint32_t ping_timeout) {
228 size_t array_idx = 0;
230 for (
int ii = 0; ii < num_peripherals; ii++) {
231 peri_ptr = (
test_t *)first_peripheral + ii;
233 alerts[array_idx] = peri_ptr->
alert_ids[jj];
234 alert_classes[array_idx] = kDifAlertHandlerClassA;
241 kDifAlertHandlerLocalAlertAlertPingFail};
247 .duration_cycles = 2000}};
251 .accumulator_threshold = 0,
252 .irq_deadline_cycles = 10000,
253 .escalation_phases = esc_phases,
254 .escalation_phases_len =
ARRAYSIZE(esc_phases),
262 kDifAlertHandlerClassB};
265 .alert_classes = alert_classes,
266 .alerts_len = array_idx,
267 .local_alerts = loc_alerts,
268 .local_alert_classes = loc_alert_classes,
269 .local_alerts_len =
ARRAYSIZE(loc_alerts),
271 .class_configs = class_configs,
273 .ping_timeout = ping_timeout,
276 CHECK_STATUS_OK(alert_handler_testutils_configure_all(&alert_handler, config,
279 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
282 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
290 void wait_enough_for_alert_ping(
void) {
306 LOG_FATAL(
"SUPPORTED PLATFORMS: DV and FPGA");
307 LOG_FATAL(
"TO SUPPORT THE PLATFORM %d, COMPUTE THE RIGHT WAIT-TIME",
321 static void get_rand_words(dif_rv_core_ibex_t *ibex,
int num, uint32_t *rnd_buf,
324 for (
int i = 0; i < num; ++i) {
326 while (found ==
false) {
328 CHECK_DIF_OK(dif_rv_core_ibex_read_rnd_data(ibex, &rnd_word));
329 rnd_word = rnd_word % max;
332 for (
int j = 0; j < i; ++j) {
333 if (rnd_buf[j] == rnd_word) {
341 rnd_buf[i] = rnd_word;
347 kCounterTestSteps = 0,
357 void ottf_external_isr(uint32_t *exc_info) {
360 CHECK(
false,
"Unexpected external interrupt triggered.");
372 ret_sram_testutils_init();
377 uint32_t rnd_wait_time;
383 size_t test_step_cnt;
389 rst_info = rstmgr_testutils_reason_get();
390 rstmgr_testutils_reason_clear();
394 CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterTestSteps));
399 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
417 while (test_step_cnt < 1 *
ARRAYSIZE(kPeripherals)) {
424 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
425 test_phase = test_step_cnt /
ARRAYSIZE(kPeripherals);
426 peri_idx = test_step_cnt - (test_phase)*
ARRAYSIZE(kPeripherals);
429 get_rand_words(&ibex, 1, &rnd_wait_time,
435 kPeripherals[peri_idx].reset_index,
438 alert_handler_config_peripherals(
439 1, &kPeripherals[peri_idx],
443 wait_enough_for_alert_ping();
447 &alert_handler, kDifAlertHandlerLocalAlertAlertPingFail, &is_cause));
449 "Expected response is ping_timeout_fail= 0! But we got "
450 "ping_timrout_fail = 1 for peripheral[%d]",
455 kPeripherals[peri_idx].reset_index,
459 CHECK_STATUS_OK(ret_sram_testutils_counter_increment(kCounterTestSteps));
486 if (test_step_cnt == 1 *
ARRAYSIZE(kPeripherals)) {
487 alert_handler_config_peripherals(
489 &kPeripherals[0], 256);
492 while (test_step_cnt < 2 *
ARRAYSIZE(kPeripherals)) {
495 test_phase = test_step_cnt /
ARRAYSIZE(kPeripherals);
496 peri_idx = test_step_cnt - (test_phase)*
ARRAYSIZE(kPeripherals);
499 get_rand_words(&ibex, 1, &rnd_wait_time,
505 kPeripherals[peri_idx].reset_index,
509 wait_enough_for_alert_ping();
513 &alert_handler, kDifAlertHandlerLocalAlertAlertPingFail, &is_cause));
514 CHECK(!is_cause,
"Expected response: No alert_ping_fail! but we got %d",
519 kPeripherals[peri_idx].reset_index,