14 #include "aon_timer_regs.h"
16 static_assert(AON_TIMER_INTR_STATE_WKUP_TIMER_EXPIRED_BIT ==
17 AON_TIMER_INTR_TEST_WKUP_TIMER_EXPIRED_BIT,
18 "Wake-up IRQ have different indexes in different registers!");
19 static_assert(AON_TIMER_INTR_STATE_WDOG_TIMER_BARK_BIT ==
20 AON_TIMER_INTR_TEST_WDOG_TIMER_BARK_BIT,
21 "Watchdog IRQ have different indexes in different registers!");
25 static void aon_timer_wakeup_clear_counter(
const dif_aon_timer_t *aon) {
26 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_COUNT_LO_REG_OFFSET, 0);
27 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_COUNT_HI_REG_OFFSET, 0);
30 static void aon_timer_wakeup_toggle(
const dif_aon_timer_t *aon,
bool enable) {
32 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_CTRL_REG_OFFSET);
34 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_CTRL_REG_OFFSET, reg);
37 static void aon_timer_watchdog_clear_counter(
const dif_aon_timer_t *aon) {
38 mmio_region_write32(aon->
base_addr, AON_TIMER_WDOG_COUNT_REG_OFFSET, 0);
41 static void aon_timer_watchdog_toggle(
const dif_aon_timer_t *aon,
bool enable) {
43 mmio_region_read32(aon->
base_addr, AON_TIMER_WDOG_CTRL_REG_OFFSET);
45 mmio_region_write32(aon->
base_addr, AON_TIMER_WDOG_CTRL_REG_OFFSET, reg);
51 mmio_region_write32(aon->
base_addr, AON_TIMER_WDOG_REGWEN_REG_OFFSET,
57 mmio_region_read32(aon->
base_addr, AON_TIMER_WDOG_REGWEN_REG_OFFSET);
66 uint64_t threshold_dec;
68 if (aon == NULL || prescaler > AON_TIMER_WKUP_CTRL_PRESCALER_MASK) {
73 aon_timer_wakeup_toggle(aon,
false);
74 aon_timer_wakeup_clear_counter(aon);
76 threshold_dec = threshold - 1;
80 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_THOLD_LO_REG_OFFSET,
81 (uint32_t)(threshold_dec & 0xffffffff));
82 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_THOLD_HI_REG_OFFSET,
83 (uint32_t)(threshold_dec >> 32));
88 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_CTRL_REG_OFFSET, reg);
98 aon_timer_wakeup_toggle(aon,
false);
108 aon_timer_wakeup_toggle(aon,
false);
109 aon_timer_wakeup_clear_counter(aon);
110 aon_timer_wakeup_toggle(aon,
true);
117 if (aon == NULL || is_enabled == NULL) {
122 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_CTRL_REG_OFFSET);
131 if (aon == NULL || cause == NULL) {
135 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_CAUSE_REG_OFFSET);
144 mmio_region_write32(aon->
base_addr, AON_TIMER_WKUP_CAUSE_REG_OFFSET, 0);
154 if (aon == NULL || count == NULL) {
159 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_COUNT_HI_REG_OFFSET);
161 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_COUNT_LO_REG_OFFSET);
163 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_COUNT_HI_REG_OFFSET);
168 if (count_hi_2 != count_hi) {
170 mmio_region_read32(aon->
base_addr, AON_TIMER_WKUP_COUNT_LO_REG_OFFSET);
171 count_hi = count_hi_2;
174 *count = (uint64_t)count_lo | (((uint64_t)count_hi) << 32);
180 uint32_t bark_threshold,
181 uint32_t bite_threshold,
182 bool pause_in_sleep,
bool lock) {
187 if (aon_timer_watchdog_is_locked(aon)) {
192 aon_timer_watchdog_toggle(aon,
false);
193 aon_timer_watchdog_clear_counter(aon);
197 mmio_region_write32(aon->
base_addr, AON_TIMER_WDOG_BARK_THOLD_REG_OFFSET,
199 mmio_region_write32(aon->
base_addr, AON_TIMER_WDOG_BITE_THOLD_REG_OFFSET,
203 if (pause_in_sleep) {
207 mmio_region_write32(aon->
base_addr, AON_TIMER_WDOG_CTRL_REG_OFFSET, reg);
212 aon_timer_watchdog_lock(aon);
223 if (aon_timer_watchdog_is_locked(aon)) {
227 aon_timer_watchdog_toggle(aon,
false);
237 if (aon_timer_watchdog_is_locked(aon)) {
241 aon_timer_watchdog_clear_counter(aon);
242 aon_timer_watchdog_toggle(aon,
true);
249 if (aon == NULL || is_enabled == NULL) {
254 mmio_region_read32(aon->
base_addr, AON_TIMER_WDOG_CTRL_REG_OFFSET);
263 if (aon == NULL || count == NULL) {
267 *count = mmio_region_read32(aon->
base_addr, AON_TIMER_WDOG_COUNT_REG_OFFSET);
277 aon_timer_watchdog_clear_counter(aon);
287 aon_timer_watchdog_lock(aon);
294 if (aon == NULL || is_locked == NULL) {
298 *is_locked = aon_timer_watchdog_is_locked(aon);