Software APIs
dif_aon_timer_autogen.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 // THIS FILE HAS BEEN GENERATED, DO NOT EDIT MANUALLY. COMMAND:
6 // util/make_new_dif.py --mode=regen --only=autogen
7 
9 
10 #include <assert.h>
11 #include <stdint.h>
12 
14 
15 #include "aon_timer_regs.h" // Generated.
16 
17 static_assert(AON_TIMER_INTR_STATE_WKUP_TIMER_EXPIRED_BIT ==
18  AON_TIMER_INTR_TEST_WKUP_TIMER_EXPIRED_BIT,
19  "Expected IRQ bit offsets to match across STATE/TEST regs.");
20 static_assert(AON_TIMER_INTR_STATE_WDOG_TIMER_BARK_BIT ==
21  AON_TIMER_INTR_TEST_WDOG_TIMER_BARK_BIT,
22  "Expected IRQ bit offsets to match across STATE/TEST regs.");
23 
26  dif_aon_timer_t *aon_timer) {
27  if (aon_timer == NULL) {
28  return kDifBadArg;
29  }
30 
31  aon_timer->base_addr = base_addr;
32 
33  return kDifOk;
34 }
35 
37  dif_aon_timer_alert_t alert) {
38  if (aon_timer == NULL) {
39  return kDifBadArg;
40  }
41 
42  bitfield_bit32_index_t alert_idx;
43  switch (alert) {
45  alert_idx = AON_TIMER_ALERT_TEST_FATAL_FAULT_BIT;
46  break;
47  default:
48  return kDifBadArg;
49  }
50 
51  uint32_t alert_test_reg = bitfield_bit32_write(0, alert_idx, true);
52  mmio_region_write32(aon_timer->base_addr,
53  (ptrdiff_t)AON_TIMER_ALERT_TEST_REG_OFFSET,
54  alert_test_reg);
55 
56  return kDifOk;
57 }
58 
59 /**
60  * Get the corresponding interrupt register bit offset of the IRQ.
61  */
62 static bool aon_timer_get_irq_bit_index(dif_aon_timer_irq_t irq,
63  bitfield_bit32_index_t *index_out) {
64  switch (irq) {
66  *index_out = AON_TIMER_INTR_STATE_WKUP_TIMER_EXPIRED_BIT;
67  break;
69  *index_out = AON_TIMER_INTR_STATE_WDOG_TIMER_BARK_BIT;
70  break;
71  default:
72  return false;
73  }
74 
75  return true;
76 }
77 
78 static dif_irq_type_t irq_types[] = {
81 };
82 
86  dif_irq_type_t *type) {
87  if (aon_timer == NULL || type == NULL ||
88  irq == kDifAonTimerIrqWdogTimerBark + 1) {
89  return kDifBadArg;
90  }
91 
92  *type = irq_types[irq];
93 
94  return kDifOk;
95 }
96 
99  const dif_aon_timer_t *aon_timer,
101  if (aon_timer == NULL || snapshot == NULL) {
102  return kDifBadArg;
103  }
104 
105  *snapshot = mmio_region_read32(aon_timer->base_addr,
106  (ptrdiff_t)AON_TIMER_INTR_STATE_REG_OFFSET);
107 
108  return kDifOk;
109 }
110 
113  const dif_aon_timer_t *aon_timer,
115  if (aon_timer == NULL) {
116  return kDifBadArg;
117  }
118 
119  mmio_region_write32(aon_timer->base_addr,
120  (ptrdiff_t)AON_TIMER_INTR_STATE_REG_OFFSET, snapshot);
121 
122  return kDifOk;
123 }
124 
128  bool *is_pending) {
129  if (aon_timer == NULL || is_pending == NULL) {
130  return kDifBadArg;
131  }
132 
134  if (!aon_timer_get_irq_bit_index(irq, &index)) {
135  return kDifBadArg;
136  }
137 
138  uint32_t intr_state_reg = mmio_region_read32(
139  aon_timer->base_addr, (ptrdiff_t)AON_TIMER_INTR_STATE_REG_OFFSET);
140 
141  *is_pending = bitfield_bit32_read(intr_state_reg, index);
142 
143  return kDifOk;
144 }
145 
148  const dif_aon_timer_t *aon_timer) {
149  if (aon_timer == NULL) {
150  return kDifBadArg;
151  }
152 
153  // Writing to the register clears the corresponding bits (Write-one clear).
154  mmio_region_write32(aon_timer->base_addr,
155  (ptrdiff_t)AON_TIMER_INTR_STATE_REG_OFFSET, UINT32_MAX);
156 
157  return kDifOk;
158 }
159 
162  dif_aon_timer_irq_t irq) {
163  if (aon_timer == NULL) {
164  return kDifBadArg;
165  }
166 
168  if (!aon_timer_get_irq_bit_index(irq, &index)) {
169  return kDifBadArg;
170  }
171 
172  // Writing to the register clears the corresponding bits (Write-one clear).
173  uint32_t intr_state_reg = bitfield_bit32_write(0, index, true);
174  mmio_region_write32(aon_timer->base_addr,
175  (ptrdiff_t)AON_TIMER_INTR_STATE_REG_OFFSET,
176  intr_state_reg);
177 
178  return kDifOk;
179 }
180 
183  dif_aon_timer_irq_t irq, const bool val) {
184  if (aon_timer == NULL) {
185  return kDifBadArg;
186  }
187 
189  if (!aon_timer_get_irq_bit_index(irq, &index)) {
190  return kDifBadArg;
191  }
192 
193  uint32_t intr_test_reg = bitfield_bit32_write(0, index, val);
194  mmio_region_write32(aon_timer->base_addr,
195  (ptrdiff_t)AON_TIMER_INTR_TEST_REG_OFFSET, intr_test_reg);
196 
197  return kDifOk;
198 }