|  | // Copyright lowRISC contributors. | 
|  | // Licensed under the Apache License, Version 2.0, see LICENSE for details. | 
|  | // SPDX-License-Identifier: Apache-2.0 | 
|  |  | 
|  | #include <stdint.h> | 
|  |  | 
|  | #include "sw/device/lib/base/mmio.h" | 
|  | #include "sw/device/lib/dif/dif_aon_timer.h" | 
|  | #include "sw/device/lib/runtime/hart.h" | 
|  | #include "sw/device/lib/runtime/log.h" | 
|  | #include "sw/device/lib/testing/aon_timer_testutils.h" | 
|  | #include "sw/device/lib/testing/test_framework/check.h" | 
|  | #include "sw/device/lib/testing/test_framework/ottf_main.h" | 
|  |  | 
|  | #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h" | 
|  |  | 
|  | OTTF_DEFINE_TEST_CONFIG(); | 
|  |  | 
|  | static void aon_timer_test_wakeup_timer(dif_aon_timer_t *aon) { | 
|  | // Test the wake-up timer functionality by setting a single cycle counter. | 
|  | // Delay to compensate for AON Timer 200kHz clock (less should suffice, but | 
|  | // to be on a cautious side - lets keep it at 100 for now). | 
|  | aon_timer_testutils_wakeup_config(aon, 1); | 
|  |  | 
|  | busy_spin_micros(100); | 
|  |  | 
|  | // Make sure that the timer has expired. | 
|  | bool is_pending; | 
|  | CHECK_DIF_OK(dif_aon_timer_irq_is_pending( | 
|  | aon, kDifAonTimerIrqWkupTimerExpired, &is_pending)); | 
|  | CHECK(is_pending); | 
|  |  | 
|  | CHECK_DIF_OK(dif_aon_timer_wakeup_stop(aon)); | 
|  |  | 
|  | CHECK_DIF_OK( | 
|  | dif_aon_timer_irq_acknowledge(aon, kDifAonTimerIrqWkupTimerExpired)); | 
|  | } | 
|  |  | 
|  | static void aon_timer_test_watchdog_timer(dif_aon_timer_t *aon) { | 
|  | // Test the watchdog timer functionality by setting a single cycle "bark" | 
|  | // counter. Delay to compensate for AON Timer 200kHz clock (less should | 
|  | // suffice, but to be on a cautious side - lets keep it at 100 for now). | 
|  | aon_timer_testutils_watchdog_config(aon, 1, UINT32_MAX, false); | 
|  | busy_spin_micros(100); | 
|  |  | 
|  | // Make sure that the timer has expired. | 
|  | bool is_pending; | 
|  | CHECK_DIF_OK(dif_aon_timer_irq_is_pending(aon, kDifAonTimerIrqWdogTimerBark, | 
|  | &is_pending)); | 
|  | CHECK(is_pending); | 
|  |  | 
|  | CHECK_DIF_OK(dif_aon_timer_watchdog_stop(aon)); | 
|  |  | 
|  | CHECK_DIF_OK( | 
|  | dif_aon_timer_irq_acknowledge(aon, kDifAonTimerIrqWdogTimerBark)); | 
|  | } | 
|  |  | 
|  | bool test_main(void) { | 
|  | dif_aon_timer_t aon; | 
|  |  | 
|  | LOG_INFO("Running AON timer test"); | 
|  |  | 
|  | // Initialise AON Timer. | 
|  | CHECK_DIF_OK(dif_aon_timer_init( | 
|  | mmio_region_from_addr(TOP_EARLGREY_AON_TIMER_AON_BASE_ADDR), &aon)); | 
|  |  | 
|  | aon_timer_test_wakeup_timer(&aon); | 
|  | aon_timer_test_watchdog_timer(&aon); | 
|  |  | 
|  | return true; | 
|  | } |