Joshua Park | f83b3f9 | 2022-05-11 14:20:27 -0700 | [diff] [blame] | 1 | // Copyright lowRISC contributors. |
| 2 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | #include <limits.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include "sw/device/lib/base/math.h" |
| 10 | #include "sw/device/lib/base/mmio.h" |
| 11 | #include "sw/device/lib/dif/dif_aon_timer.h" |
| 12 | #include "sw/device/lib/dif/dif_pwrmgr.h" |
| 13 | #include "sw/device/lib/dif/dif_rstmgr.h" |
| 14 | #include "sw/device/lib/runtime/log.h" |
| 15 | #include "sw/device/lib/testing/aon_timer_testutils.h" |
| 16 | #include "sw/device/lib/testing/pwrmgr_testutils.h" |
Drew Macrae | 556e325 | 2022-07-07 16:11:25 -0400 | [diff] [blame] | 17 | #include "sw/device/lib/testing/rstmgr_testutils.h" |
Joshua Park | f83b3f9 | 2022-05-11 14:20:27 -0700 | [diff] [blame] | 18 | #include "sw/device/lib/testing/test_framework/check.h" |
| 19 | #include "sw/device/lib/testing/test_framework/ottf_main.h" |
| 20 | |
| 21 | #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h" |
| 22 | |
Alphan Ulusoy | 9f4fc67 | 2022-06-17 21:39:16 -0400 | [diff] [blame] | 23 | OTTF_DEFINE_TEST_CONFIG(); |
Joshua Park | f83b3f9 | 2022-05-11 14:20:27 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Configure the wdog. |
| 27 | */ |
| 28 | static void config_wdog(const dif_aon_timer_t *aon_timer, |
| 29 | const dif_pwrmgr_t *pwrmgr, uint64_t bark_time_us, |
| 30 | uint64_t bite_time_us) { |
| 31 | uint32_t bark_cycles = |
| 32 | aon_timer_testutils_get_aon_cycles_from_us(bark_time_us); |
| 33 | uint32_t bite_cycles = |
| 34 | aon_timer_testutils_get_aon_cycles_from_us(bite_time_us); |
| 35 | |
| 36 | LOG_INFO("Wdog will bark after %u us and bite after %u us", |
| 37 | (uint32_t)bark_time_us, (uint32_t)bite_time_us); |
| 38 | |
| 39 | // Set wdog as a reset source. |
| 40 | CHECK_DIF_OK(dif_pwrmgr_set_request_sources(pwrmgr, kDifPwrmgrReqTypeReset, |
| 41 | kDifPwrmgrResetRequestSourceTwo, |
| 42 | kDifToggleEnabled)); |
| 43 | |
| 44 | // Setup the wdog bark and bite timeouts. |
| 45 | aon_timer_testutils_watchdog_config(aon_timer, bark_cycles, bite_cycles, |
| 46 | false); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Execute the aon timer wdog bite reset test. |
| 51 | */ |
| 52 | static void wdog_bite_test(const dif_aon_timer_t *aon_timer, |
| 53 | const dif_pwrmgr_t *pwrmgr, uint64_t bark_time_us) { |
| 54 | uint64_t bite_time_us = bark_time_us * 2; |
| 55 | config_wdog(aon_timer, pwrmgr, bark_time_us, bite_time_us); |
| 56 | |
| 57 | // The `intr_state` takes 3 aon clock cycles to rise plus 2 extra cycles as a |
| 58 | // precaution. |
| 59 | uint32_t wait_us = |
| 60 | bark_time_us + |
| 61 | udiv64_slow(5 * 1000000 + kClockFreqAonHz - 1, kClockFreqAonHz, NULL); |
| 62 | |
| 63 | // Wait bark time and check that the bark interrupt requested. |
| 64 | busy_spin_micros(wait_us); |
| 65 | bool is_pending = false; |
| 66 | CHECK_DIF_OK(dif_aon_timer_irq_is_pending( |
| 67 | aon_timer, kDifAonTimerIrqWdogTimerBark, &is_pending)); |
| 68 | CHECK(is_pending, "Wdog bark irq did not rise after %u microseconds", |
| 69 | wait_us); |
| 70 | |
| 71 | // Wait for the remaining time to the wdog bite. |
| 72 | busy_spin_micros(wait_us); |
| 73 | // If we arrive here the test must fail. |
| 74 | CHECK(false, "Timeout waiting for Wdog bite reset!"); |
| 75 | } |
| 76 | |
| 77 | bool test_main(void) { |
| 78 | // Initialize pwrmgr. |
| 79 | dif_pwrmgr_t pwrmgr; |
| 80 | CHECK_DIF_OK(dif_pwrmgr_init( |
| 81 | mmio_region_from_addr(TOP_EARLGREY_PWRMGR_AON_BASE_ADDR), &pwrmgr)); |
| 82 | |
| 83 | // Initialize rstmgr to check the reset reason. |
| 84 | dif_rstmgr_t rstmgr; |
| 85 | CHECK_DIF_OK(dif_rstmgr_init( |
| 86 | mmio_region_from_addr(TOP_EARLGREY_RSTMGR_AON_BASE_ADDR), &rstmgr)); |
| 87 | |
| 88 | // Initialize aon timer to use the wdog. |
| 89 | dif_aon_timer_t aon_timer; |
| 90 | CHECK_DIF_OK(dif_aon_timer_init( |
| 91 | mmio_region_from_addr(TOP_EARLGREY_AON_TIMER_AON_BASE_ADDR), &aon_timer)); |
| 92 | |
| 93 | // Check if there was a HW reset caused by the wdog bite. |
| 94 | dif_rstmgr_reset_info_bitfield_t rst_info; |
Drew Macrae | 8990396 | 2022-07-08 15:17:50 -0400 | [diff] [blame] | 95 | rst_info = rstmgr_testutils_reason_get(); |
| 96 | rstmgr_testutils_reason_clear(); |
Joshua Park | f83b3f9 | 2022-05-11 14:20:27 -0700 | [diff] [blame] | 97 | |
| 98 | CHECK(rst_info == kDifRstmgrResetInfoPor || |
| 99 | rst_info == kDifRstmgrResetInfoWatchdog, |
| 100 | "Wrong reset reason %02X", rst_info); |
| 101 | |
| 102 | if (rst_info == kDifRstmgrResetInfoPor) { |
| 103 | LOG_INFO("Booting for the first time, setting wdog"); |
| 104 | // Executing the wdog bite reset test. |
| 105 | wdog_bite_test(&aon_timer, &pwrmgr, /*bark_time_us=*/200); |
| 106 | } else if (rst_info == kDifRstmgrResetInfoWatchdog) { |
| 107 | LOG_INFO("Booting for the second time due to wdog bite reset"); |
Srikrishna Iyer | 89c37bd | 2022-09-02 11:26:55 -0700 | [diff] [blame] | 108 | |
Guillermo Maturana | 6f52bf2 | 2022-06-02 22:14:57 -0700 | [diff] [blame] | 109 | return true; |
Joshua Park | f83b3f9 | 2022-05-11 14:20:27 -0700 | [diff] [blame] | 110 | } |
Guillermo Maturana | 6f52bf2 | 2022-06-02 22:14:57 -0700 | [diff] [blame] | 111 | LOG_ERROR("Got unexpected reset info 0x%x", rst_info); |
| 112 | return false; |
Joshua Park | f83b3f9 | 2022-05-11 14:20:27 -0700 | [diff] [blame] | 113 | } |