Software APIs
dif_sysrst_ctrl_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
6
7
8
9
10
11// THIS FILE HAS BEEN GENERATED, DO NOT EDIT MANUALLY. COMMAND:
12// util/autogen_dif.py -i hw/ip/sysrst_ctrl/data/sysrst_ctrl.hjson -o
13// bazel-out/k8-fastbuild/bin/sw/device/lib/dif/autogen
14
15
16#include <stdint.h>
17
20
21#include "sysrst_ctrl_regs.h" // Generated.
22
23
25dif_result_t dif_sysrst_ctrl_init(
26 mmio_region_t base_addr,
27 dif_sysrst_ctrl_t *sysrst_ctrl) {
28 if (sysrst_ctrl == NULL) {
29 return kDifBadArg;
30 }
31
32 sysrst_ctrl->dt = kDtSysrstCtrlCount;
33 sysrst_ctrl->base_addr = base_addr;
34
35 return kDifOk;
36}
37
39dif_result_t dif_sysrst_ctrl_init_from_dt(
41 dif_sysrst_ctrl_t *sysrst_ctrl) {
42 if (sysrst_ctrl == NULL) {
43 return kDifBadArg;
44 }
45
46 sysrst_ctrl->dt = dt;
47 sysrst_ctrl->base_addr = mmio_region_from_addr(dt_sysrst_ctrl_primary_reg_block(dt));
48
49 return kDifOk;
50}
51
52dif_result_t dif_sysrst_ctrl_get_dt(
53 const dif_sysrst_ctrl_t *sysrst_ctrl,
54 dt_sysrst_ctrl_t *dt) {
55 if (sysrst_ctrl->dt == kDtSysrstCtrlCount || dt == NULL) {
56 return kDifBadArg;
57 }
58 *dt = sysrst_ctrl->dt;
59 return kDifOk;
60}
61
62 dif_result_t dif_sysrst_ctrl_alert_force(
63 const dif_sysrst_ctrl_t *sysrst_ctrl,
65 if (sysrst_ctrl == NULL) {
66 return kDifBadArg;
67 }
68
69 bitfield_bit32_index_t alert_idx;
70 switch (alert) {
72 alert_idx = SYSRST_CTRL_ALERT_TEST_FATAL_FAULT_BIT;
73 break;
74 default:
75 return kDifBadArg;
76 }
77
78 uint32_t alert_test_reg = bitfield_bit32_write(0, alert_idx, true);
79 mmio_region_write32(
80 sysrst_ctrl->base_addr,
81 (ptrdiff_t)SYSRST_CTRL_ALERT_TEST_REG_OFFSET,
82 alert_test_reg);
83
84
85 return kDifOk;
86}
87
88
89 /**
90 * Get the corresponding interrupt register bit offset of the IRQ.
91 */
92 static bool sysrst_ctrl_get_irq_bit_index(
93 dif_sysrst_ctrl_irq_t irq,
94 bitfield_bit32_index_t *index_out) {
95
96 switch (irq) {
98 *index_out = SYSRST_CTRL_INTR_COMMON_EVENT_DETECTED_BIT;
99 break;
100 default:
101 return false;
102 }
103
104 return true;
105 }
106
107 static dif_irq_type_t irq_types[] = {
109 };
110
112 dif_result_t dif_sysrst_ctrl_irq_get_type(
113 const dif_sysrst_ctrl_t *sysrst_ctrl,
114 dif_sysrst_ctrl_irq_t irq,
115 dif_irq_type_t *type) {
116
117
118 if (sysrst_ctrl == NULL ||
119 type == NULL ||
122 return kDifBadArg;
123 }
124
125 *type = irq_types[irq];
126
127 return kDifOk;
128 }
129
131 dif_result_t dif_sysrst_ctrl_irq_get_state(
132 const dif_sysrst_ctrl_t *sysrst_ctrl,
134
135 if (sysrst_ctrl == NULL || snapshot == NULL) {
136 return kDifBadArg;
137 }
138
139 *snapshot = mmio_region_read32(
140 sysrst_ctrl->base_addr,
141 (ptrdiff_t)SYSRST_CTRL_INTR_STATE_REG_OFFSET);
142
143
144 return kDifOk;
145 }
146
148 dif_result_t dif_sysrst_ctrl_irq_acknowledge_state(
149 const dif_sysrst_ctrl_t *sysrst_ctrl,
151 if (sysrst_ctrl == NULL) {
152 return kDifBadArg;
153 }
154
155 mmio_region_write32(
156 sysrst_ctrl->base_addr,
157 (ptrdiff_t)SYSRST_CTRL_INTR_STATE_REG_OFFSET,
158 snapshot);
159
160
161 return kDifOk;
162 }
163
165 dif_result_t dif_sysrst_ctrl_irq_is_pending(
166 const dif_sysrst_ctrl_t *sysrst_ctrl,
167 dif_sysrst_ctrl_irq_t irq,
168 bool *is_pending) {
169
170 if (sysrst_ctrl == NULL || is_pending == NULL) {
171 return kDifBadArg;
172 }
173
175 if (!sysrst_ctrl_get_irq_bit_index(irq, &index)) {
176 return kDifBadArg;
177 }
178
179 uint32_t intr_state_reg = mmio_region_read32(
180 sysrst_ctrl->base_addr,
181 (ptrdiff_t)SYSRST_CTRL_INTR_STATE_REG_OFFSET);
182
183
184 *is_pending = bitfield_bit32_read(intr_state_reg, index);
185
186 return kDifOk;
187 }
188
190 dif_result_t dif_sysrst_ctrl_irq_acknowledge_all(
191 const dif_sysrst_ctrl_t *sysrst_ctrl
192 ) {
193
194 if (sysrst_ctrl == NULL) {
195 return kDifBadArg;
196 }
197
198 // Writing to the register clears the corresponding bits (Write-one clear).
199 mmio_region_write32(
200 sysrst_ctrl->base_addr,
201 (ptrdiff_t)SYSRST_CTRL_INTR_STATE_REG_OFFSET,
202 UINT32_MAX);
203
204
205 return kDifOk;
206 }
207
209 dif_result_t dif_sysrst_ctrl_irq_acknowledge(
210 const dif_sysrst_ctrl_t *sysrst_ctrl,
211 dif_sysrst_ctrl_irq_t irq) {
212
213 if (sysrst_ctrl == NULL) {
214 return kDifBadArg;
215 }
216
218 if (!sysrst_ctrl_get_irq_bit_index(irq, &index)) {
219 return kDifBadArg;
220 }
221
222 // Writing to the register clears the corresponding bits (Write-one clear).
223 uint32_t intr_state_reg = bitfield_bit32_write(0, index, true);
224 mmio_region_write32(
225 sysrst_ctrl->base_addr,
226 (ptrdiff_t)SYSRST_CTRL_INTR_STATE_REG_OFFSET,
227 intr_state_reg);
228
229
230 return kDifOk;
231 }
232
234 dif_result_t dif_sysrst_ctrl_irq_force(
235 const dif_sysrst_ctrl_t *sysrst_ctrl,
236 dif_sysrst_ctrl_irq_t irq,
237 const bool val) {
238
239 if (sysrst_ctrl == NULL) {
240 return kDifBadArg;
241 }
242
244 if (!sysrst_ctrl_get_irq_bit_index(irq, &index)) {
245 return kDifBadArg;
246 }
247
248 uint32_t intr_test_reg = bitfield_bit32_write(0, index, val);
249 mmio_region_write32(
250 sysrst_ctrl->base_addr,
251 (ptrdiff_t)SYSRST_CTRL_INTR_TEST_REG_OFFSET,
252 intr_test_reg);
253
254
255 return kDifOk;
256 }
257
259 dif_result_t dif_sysrst_ctrl_irq_get_enabled(
260 const dif_sysrst_ctrl_t *sysrst_ctrl,
261 dif_sysrst_ctrl_irq_t irq,
262 dif_toggle_t *state) {
263
264 if (sysrst_ctrl == NULL || state == NULL) {
265 return kDifBadArg;
266 }
267
269 if (!sysrst_ctrl_get_irq_bit_index(irq, &index)) {
270 return kDifBadArg;
271 }
272
273 uint32_t intr_enable_reg = mmio_region_read32(
274 sysrst_ctrl->base_addr,
275 (ptrdiff_t)SYSRST_CTRL_INTR_ENABLE_REG_OFFSET);
276
277
278 bool is_enabled = bitfield_bit32_read(intr_enable_reg, index);
279 *state = is_enabled ?
281
282 return kDifOk;
283 }
284
286 dif_result_t dif_sysrst_ctrl_irq_set_enabled(
287 const dif_sysrst_ctrl_t *sysrst_ctrl,
288 dif_sysrst_ctrl_irq_t irq,
289 dif_toggle_t state) {
290
291 if (sysrst_ctrl == NULL) {
292 return kDifBadArg;
293 }
294
296 if (!sysrst_ctrl_get_irq_bit_index(irq, &index)) {
297 return kDifBadArg;
298 }
299
300 uint32_t intr_enable_reg = mmio_region_read32(
301 sysrst_ctrl->base_addr,
302 (ptrdiff_t)SYSRST_CTRL_INTR_ENABLE_REG_OFFSET);
303
304
305 bool enable_bit = (state == kDifToggleEnabled) ? true : false;
306 intr_enable_reg = bitfield_bit32_write(intr_enable_reg, index, enable_bit);
307 mmio_region_write32(
308 sysrst_ctrl->base_addr,
309 (ptrdiff_t)SYSRST_CTRL_INTR_ENABLE_REG_OFFSET,
310 intr_enable_reg);
311
312
313 return kDifOk;
314 }
315
317 dif_result_t dif_sysrst_ctrl_irq_disable_all(
318 const dif_sysrst_ctrl_t *sysrst_ctrl,
320
321 if (sysrst_ctrl == NULL) {
322 return kDifBadArg;
323 }
324
325 // Pass the current interrupt state to the caller, if requested.
326 if (snapshot != NULL) {
327 *snapshot = mmio_region_read32(
328 sysrst_ctrl->base_addr,
329 (ptrdiff_t)SYSRST_CTRL_INTR_ENABLE_REG_OFFSET);
330
331 }
332
333 // Disable all interrupts.
334 mmio_region_write32(
335 sysrst_ctrl->base_addr,
336 (ptrdiff_t)SYSRST_CTRL_INTR_ENABLE_REG_OFFSET,
337 0u);
338
339
340 return kDifOk;
341 }
342
344 dif_result_t dif_sysrst_ctrl_irq_restore_all(
345 const dif_sysrst_ctrl_t *sysrst_ctrl,
347
348 if (sysrst_ctrl == NULL || snapshot == NULL) {
349 return kDifBadArg;
350 }
351
352 mmio_region_write32(
353 sysrst_ctrl->base_addr,
354 (ptrdiff_t)SYSRST_CTRL_INTR_ENABLE_REG_OFFSET,
355 *snapshot);
356
357
358 return kDifOk;
359 }
360