9 #include "sw/device/lib/runtime/irq.h" 
   11 #include "sw/device/lib/testing/flash_ctrl_testutils.h" 
   12 #include "sw/device/lib/testing/pwrmgr_testutils.h" 
   13 #include "sw/device/lib/testing/rand_testutils.h" 
   14 #include "sw/device/lib/testing/rv_plic_testutils.h" 
   15 #include "sw/device/lib/testing/test_framework/check.h" 
   17 #include "sw/device/lib/testing/test_framework/ottf_utils.h" 
   21 #include "flash_ctrl_regs.h" 
   22 #include "pinmux_regs.h" 
   26 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control = 
true);
 
   28 static const dt_pwrmgr_t kPwrmgrDt = 0;
 
   29 static_assert(kDtPwrmgrCount == 1, 
"this library expects exactly one pwrmgr");
 
   30 static const dt_pinmux_t kPinmuxDt = 0;
 
   31 static_assert(kDtPinmuxCount == 1, 
"this library expects exactly one pinmux");
 
   32 static const dt_rv_plic_t kRvPlicDt = 0;
 
   33 static_assert(kDtRvPlicCount == 1, 
"this library expects exactly one rv_plic");
 
   34 static const dt_flash_ctrl_t kFlashCtrlDt = 0;
 
   35 static_assert(kDtFlashCtrlCount >= 1,
 
   36               "this library expects at least one flash_ctrl");
 
   39 static dif_pwrmgr_t pwrmgr;
 
   40 static dif_pinmux_t pinmux;
 
   41 static dif_rv_plic_t plic;
 
   48 static const uint32_t kNumDio = 16;  
 
   52 #define NUM_DIRECT_DIO 5 
   53 static const uint32_t kDirectDio[NUM_DIRECT_DIO] = {6, 12, 13, 14, 15};
 
   59 int8_t sival_mio_pad = -1;  
 
   61 int8_t sival_wakeup_detector_idx = -1;  
 
   63 bool sival_ready_to_sleep = 
false;
 
   68 bool ottf_handle_irq(uint32_t *exc_info, dt_instance_id_t devid,
 
   70   if (devid == dt_pwrmgr_instance_id(kPwrmgrDt) &&
 
   71       irq_id == dt_pwrmgr_irq_to_plic_id(kPwrmgrDt, kDtPwrmgrIrqWakeup)) {
 
   72     CHECK_DIF_OK(dif_pwrmgr_irq_acknowledge(&pwrmgr, kDtPwrmgrIrqWakeup));
 
   86   irq_global_ctrl(
true);
 
   87   irq_external_ctrl(
true);
 
   90   CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
 
   91   CHECK_DIF_OK(dif_rv_plic_init_from_dt(kRvPlicDt, &plic));
 
   92   CHECK_DIF_OK(dif_pinmux_init_from_dt(kPinmuxDt, &pinmux));
 
  100       kDtPinmuxWakeupPinWkupReq, &wakeup_sources));
 
  105         flash_ctrl_testutils_default_region_access(&flash_ctrl_state,
 
  118   if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) == 
true) {
 
  123     wakeup_detector_selected =
 
  124         rand_testutils_gen32_range(0, PINMUX_PARAM_N_WKUP_DETECT - 1);
 
  126       CHECK_STATUS_OK(flash_ctrl_testutils_write(
 
  128           (uint32_t)(&wakeup_detector_idx) -
 
  130           0, &wakeup_detector_selected, kDifFlashCtrlPartitionTypeData, 1));
 
  132     LOG_INFO(
"detector %d is selected", wakeup_detector_selected);
 
  147     uint32_t deep_powerdown_en = rand_testutils_gen32_range(0, 1);
 
  153     uint32_t pad_sel = 0;
 
  156     if (deep_powerdown_en == 0) {
 
  159           dt_pwrmgr_irq_to_plic_id(kPwrmgrDt, kDtPwrmgrIrqWakeup);
 
  160       rv_plic_testutils_irq_range_enable(&plic, kPlicTarget, irq_id, irq_id);
 
  170       pad_sel = rand_testutils_gen32_range(0, kNumDio - 1 - NUM_DIRECT_DIO);
 
  172       for (
int i = 0; i < NUM_DIRECT_DIO; i++) {
 
  173         if (pad_sel >= kDirectDio[i]) {
 
  177       LOG_INFO(
"Pad Selection: %d / %d", mio0_dio1, pad_sel);
 
  181         pad_sel = rand_testutils_gen32_range(2, kTopEarlgreyPinmuxInselLast);
 
  183         OTTF_WAIT_FOR(sival_mio_pad != -1, 1000000);
 
  184         pad_sel = (uint32_t)sival_mio_pad + 2;  
 
  187       LOG_INFO(
"Pad Selection: %d / %d", mio0_dio1, pad_sel - 2);
 
  190     if (mio0_dio1 == 0) {
 
  207         &pinmux, wakeup_detector_selected, wakeup_cfg));
 
  209     if (deep_powerdown_en == 0) {
 
  211       pwrmgr_domain_cfg |= kDifPwrmgrDomainOptionMainPowerInLowPower;
 
  215       OTTF_WAIT_FOR(sival_ready_to_sleep != 0, 1000000);
 
  219     CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(&pwrmgr, wakeup_sources,
 
  222     LOG_INFO(
"Entering low power mode.");
 
  226   if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, wakeup_sources))) {
 
  227     LOG_INFO(
"Test in post-sleep pin wakeup phase");
 
  228     uint32_t wakeup_cause;
 
  232       wakeup_detector_selected = wakeup_detector_idx;
 
  234       OTTF_WAIT_FOR(sival_wakeup_detector_idx != -1, 1000000);
 
  235       wakeup_detector_selected = (uint32_t)sival_wakeup_detector_idx;
 
  238     LOG_INFO(
"wakeup_cause: %x %d %d", wakeup_cause,
 
  239              1 << wakeup_detector_selected, wakeup_detector_selected);
 
  240     CHECK(wakeup_cause == 1 << wakeup_detector_selected);
 
  249     LOG_ERROR(
"Unexpected wakeup detected: type = %d, request_source = %d",
 
  257 #undef NUM_DIRECT_DIO 
  258 #undef NUM_LOCKED_MIO