Software APIs
dif_hmac_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/hmac/data/hmac.hjson -o
13// bazel-out/k8-fastbuild/bin/sw/device/lib/dif/autogen
14
15
16#include <stdint.h>
17
19#include "sw/device/lib/dif/autogen/dif_hmac_autogen.h"
20
21#include "hw/top/hmac_regs.h" // Generated.
22
23
25dif_result_t dif_hmac_init(
26 mmio_region_t base_addr,
27 dif_hmac_t *hmac) {
28 if (hmac == NULL) {
29 return kDifBadArg;
30 }
31
32 hmac->dt = kDtHmacCount;
33 hmac->base_addr = base_addr;
34
35 return kDifOk;
36}
37
39dif_result_t dif_hmac_init_from_dt(
40 dt_hmac_t dt,
41 dif_hmac_t *hmac) {
42 if (hmac == NULL) {
43 return kDifBadArg;
44 }
45
46 hmac->dt = dt;
47 hmac->base_addr = mmio_region_from_addr(dt_hmac_primary_reg_block(dt));
48
49 return kDifOk;
50}
51
52dif_result_t dif_hmac_get_dt(
53 const dif_hmac_t *hmac,
54 dt_hmac_t *dt) {
55 if ((int)hmac->dt == kDtHmacCount || dt == NULL) {
56 return kDifBadArg;
57 }
58 *dt = hmac->dt;
59 return kDifOk;
60}
61
62 dif_result_t dif_hmac_alert_force(
63 const dif_hmac_t *hmac,
64 dif_hmac_alert_t alert) {
65 if (hmac == NULL) {
66 return kDifBadArg;
67 }
68
69 bitfield_bit32_index_t alert_idx;
70 switch (alert) {
72 alert_idx = HMAC_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 hmac->base_addr,
81 (ptrdiff_t)HMAC_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 hmac_get_irq_bit_index(
93 dif_hmac_irq_t irq,
94 bitfield_bit32_index_t *index_out) {
95
96 switch (irq) {
98 *index_out = HMAC_INTR_COMMON_HMAC_DONE_BIT;
99 break;
101 *index_out = HMAC_INTR_COMMON_FIFO_EMPTY_BIT;
102 break;
104 *index_out = HMAC_INTR_COMMON_HMAC_ERR_BIT;
105 break;
106 default:
107 return false;
108 }
109
110 return true;
111 }
112
113 static dif_irq_type_t irq_types[] = {
117 };
118
120 dif_result_t dif_hmac_irq_get_type(
121 const dif_hmac_t *hmac,
122 dif_hmac_irq_t irq,
123 dif_irq_type_t *type) {
124
125
126 if (hmac == NULL ||
127 type == NULL ||
128 irq < kDifHmacIrqHmacDone ||
129 irq > kDifHmacIrqHmacErr) {
130 return kDifBadArg;
131 }
132
133 *type = irq_types[irq];
134
135 return kDifOk;
136 }
137
139 dif_result_t dif_hmac_irq_get_state(
140 const dif_hmac_t *hmac,
142
143 if (hmac == NULL || snapshot == NULL) {
144 return kDifBadArg;
145 }
146
147 *snapshot = mmio_region_read32(
148 hmac->base_addr,
149 (ptrdiff_t)HMAC_INTR_STATE_REG_OFFSET);
150
151
152 return kDifOk;
153 }
154
156 dif_result_t dif_hmac_irq_acknowledge_state(
157 const dif_hmac_t *hmac,
159 if (hmac == NULL) {
160 return kDifBadArg;
161 }
162
163 mmio_region_write32(
164 hmac->base_addr,
165 (ptrdiff_t)HMAC_INTR_STATE_REG_OFFSET,
166 snapshot);
167
168
169 return kDifOk;
170 }
171
173 dif_result_t dif_hmac_irq_is_pending(
174 const dif_hmac_t *hmac,
175 dif_hmac_irq_t irq,
176 bool *is_pending) {
177
178 if (hmac == NULL || is_pending == NULL) {
179 return kDifBadArg;
180 }
181
183 if (!hmac_get_irq_bit_index(irq, &index)) {
184 return kDifBadArg;
185 }
186
187 uint32_t intr_state_reg = mmio_region_read32(
188 hmac->base_addr,
189 (ptrdiff_t)HMAC_INTR_STATE_REG_OFFSET);
190
191
192 *is_pending = bitfield_bit32_read(intr_state_reg, index);
193
194 return kDifOk;
195 }
196
198 dif_result_t dif_hmac_irq_acknowledge_all(
199 const dif_hmac_t *hmac
200 ) {
201
202 if (hmac == NULL) {
203 return kDifBadArg;
204 }
205
206 // Writing to the register clears the corresponding bits (Write-one clear).
207 mmio_region_write32(
208 hmac->base_addr,
209 (ptrdiff_t)HMAC_INTR_STATE_REG_OFFSET,
210 UINT32_MAX);
211
212
213 return kDifOk;
214 }
215
217 dif_result_t dif_hmac_irq_acknowledge(
218 const dif_hmac_t *hmac,
219 dif_hmac_irq_t irq) {
220
221 if (hmac == NULL) {
222 return kDifBadArg;
223 }
224
226 if (!hmac_get_irq_bit_index(irq, &index)) {
227 return kDifBadArg;
228 }
229
230 // Writing to the register clears the corresponding bits (Write-one clear).
231 uint32_t intr_state_reg = bitfield_bit32_write(0, index, true);
232 mmio_region_write32(
233 hmac->base_addr,
234 (ptrdiff_t)HMAC_INTR_STATE_REG_OFFSET,
235 intr_state_reg);
236
237
238 return kDifOk;
239 }
240
242 dif_result_t dif_hmac_irq_force(
243 const dif_hmac_t *hmac,
244 dif_hmac_irq_t irq,
245 const bool val) {
246
247 if (hmac == NULL) {
248 return kDifBadArg;
249 }
250
252 if (!hmac_get_irq_bit_index(irq, &index)) {
253 return kDifBadArg;
254 }
255
256 uint32_t intr_test_reg = bitfield_bit32_write(0, index, val);
257 mmio_region_write32(
258 hmac->base_addr,
259 (ptrdiff_t)HMAC_INTR_TEST_REG_OFFSET,
260 intr_test_reg);
261
262
263 return kDifOk;
264 }
265
267 dif_result_t dif_hmac_irq_get_enabled(
268 const dif_hmac_t *hmac,
269 dif_hmac_irq_t irq,
270 dif_toggle_t *state) {
271
272 if (hmac == NULL || state == NULL) {
273 return kDifBadArg;
274 }
275
277 if (!hmac_get_irq_bit_index(irq, &index)) {
278 return kDifBadArg;
279 }
280
281 uint32_t intr_enable_reg = mmio_region_read32(
282 hmac->base_addr,
283 (ptrdiff_t)HMAC_INTR_ENABLE_REG_OFFSET);
284
285
286 bool is_enabled = bitfield_bit32_read(intr_enable_reg, index);
287 *state = is_enabled ?
289
290 return kDifOk;
291 }
292
294 dif_result_t dif_hmac_irq_set_enabled(
295 const dif_hmac_t *hmac,
296 dif_hmac_irq_t irq,
297 dif_toggle_t state) {
298
299 if (hmac == NULL) {
300 return kDifBadArg;
301 }
302
304 if (!hmac_get_irq_bit_index(irq, &index)) {
305 return kDifBadArg;
306 }
307
308 uint32_t intr_enable_reg = mmio_region_read32(
309 hmac->base_addr,
310 (ptrdiff_t)HMAC_INTR_ENABLE_REG_OFFSET);
311
312
313 bool enable_bit = (state == kDifToggleEnabled) ? true : false;
314 intr_enable_reg = bitfield_bit32_write(intr_enable_reg, index, enable_bit);
315 mmio_region_write32(
316 hmac->base_addr,
317 (ptrdiff_t)HMAC_INTR_ENABLE_REG_OFFSET,
318 intr_enable_reg);
319
320
321 return kDifOk;
322 }
323
325 dif_result_t dif_hmac_irq_disable_all(
326 const dif_hmac_t *hmac,
328
329 if (hmac == NULL) {
330 return kDifBadArg;
331 }
332
333 // Pass the current interrupt state to the caller, if requested.
334 if (snapshot != NULL) {
335 *snapshot = mmio_region_read32(
336 hmac->base_addr,
337 (ptrdiff_t)HMAC_INTR_ENABLE_REG_OFFSET);
338
339 }
340
341 // Disable all interrupts.
342 mmio_region_write32(
343 hmac->base_addr,
344 (ptrdiff_t)HMAC_INTR_ENABLE_REG_OFFSET,
345 0u);
346
347
348 return kDifOk;
349 }
350
352 dif_result_t dif_hmac_irq_restore_all(
353 const dif_hmac_t *hmac,
354 const dif_hmac_irq_enable_snapshot_t *snapshot) {
355
356 if (hmac == NULL || snapshot == NULL) {
357 return kDifBadArg;
358 }
359
360 mmio_region_write32(
361 hmac->base_addr,
362 (ptrdiff_t)HMAC_INTR_ENABLE_REG_OFFSET,
363 *snapshot);
364
365
366 return kDifOk;
367 }
368