Software APIs
dif_otp_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
13// hw/top_earlgrey/ip_autogen/otp_ctrl/data/otp_ctrl.hjson -o
14// bazel-out/k8-fastbuild/bin/sw/device/lib/dif/autogen
15
16
17#include <stdint.h>
18
20#include "sw/device/lib/dif/autogen/dif_otp_ctrl_autogen.h"
21
22#include "hw/top/otp_ctrl_regs.h" // Generated.
23
24
26dif_result_t dif_otp_ctrl_init(
27 mmio_region_t base_addr,
28 dif_otp_ctrl_t *otp_ctrl) {
29 if (otp_ctrl == NULL) {
30 return kDifBadArg;
31 }
32
33 otp_ctrl->dt = kDtOtpCtrlCount;
34 otp_ctrl->base_addr = base_addr;
35
36 return kDifOk;
37}
38
40dif_result_t dif_otp_ctrl_init_from_dt(
42 dif_otp_ctrl_t *otp_ctrl) {
43 if (otp_ctrl == NULL) {
44 return kDifBadArg;
45 }
46
47 otp_ctrl->dt = dt;
48 otp_ctrl->base_addr = mmio_region_from_addr(dt_otp_ctrl_primary_reg_block(dt));
49
50 return kDifOk;
51}
52
53dif_result_t dif_otp_ctrl_get_dt(
54 const dif_otp_ctrl_t *otp_ctrl,
55 dt_otp_ctrl_t *dt) {
56 if ((int)otp_ctrl->dt == kDtOtpCtrlCount || dt == NULL) {
57 return kDifBadArg;
58 }
59 *dt = otp_ctrl->dt;
60 return kDifOk;
61}
62
63 dif_result_t dif_otp_ctrl_alert_force(
64 const dif_otp_ctrl_t *otp_ctrl,
66 if (otp_ctrl == NULL) {
67 return kDifBadArg;
68 }
69
70 bitfield_bit32_index_t alert_idx;
71 switch (alert) {
73 alert_idx = OTP_CTRL_ALERT_TEST_FATAL_MACRO_ERROR_BIT;
74 break;
76 alert_idx = OTP_CTRL_ALERT_TEST_FATAL_CHECK_ERROR_BIT;
77 break;
79 alert_idx = OTP_CTRL_ALERT_TEST_FATAL_BUS_INTEG_ERROR_BIT;
80 break;
82 alert_idx = OTP_CTRL_ALERT_TEST_FATAL_PRIM_OTP_ALERT_BIT;
83 break;
85 alert_idx = OTP_CTRL_ALERT_TEST_RECOV_PRIM_OTP_ALERT_BIT;
86 break;
87 default:
88 return kDifBadArg;
89 }
90
91 uint32_t alert_test_reg = bitfield_bit32_write(0, alert_idx, true);
92 mmio_region_write32(
93 otp_ctrl->base_addr,
94 (ptrdiff_t)OTP_CTRL_ALERT_TEST_REG_OFFSET,
95 alert_test_reg);
96
97
98 return kDifOk;
99}
100
101
102 /**
103 * Get the corresponding interrupt register bit offset of the IRQ.
104 */
105 static bool otp_ctrl_get_irq_bit_index(
106 dif_otp_ctrl_irq_t irq,
107 bitfield_bit32_index_t *index_out) {
108
109 switch (irq) {
111 *index_out = OTP_CTRL_INTR_COMMON_OTP_OPERATION_DONE_BIT;
112 break;
114 *index_out = OTP_CTRL_INTR_COMMON_OTP_ERROR_BIT;
115 break;
116 default:
117 return false;
118 }
119
120 return true;
121 }
122
123 static dif_irq_type_t irq_types[] = {
126 };
127
129 dif_result_t dif_otp_ctrl_irq_get_type(
130 const dif_otp_ctrl_t *otp_ctrl,
131 dif_otp_ctrl_irq_t irq,
132 dif_irq_type_t *type) {
133
134
135 if (otp_ctrl == NULL ||
136 type == NULL ||
139 return kDifBadArg;
140 }
141
142 *type = irq_types[irq];
143
144 return kDifOk;
145 }
146
148 dif_result_t dif_otp_ctrl_irq_get_state(
149 const dif_otp_ctrl_t *otp_ctrl,
151
152 if (otp_ctrl == NULL || snapshot == NULL) {
153 return kDifBadArg;
154 }
155
156 *snapshot = mmio_region_read32(
157 otp_ctrl->base_addr,
158 (ptrdiff_t)OTP_CTRL_INTR_STATE_REG_OFFSET);
159
160
161 return kDifOk;
162 }
163
165 dif_result_t dif_otp_ctrl_irq_acknowledge_state(
166 const dif_otp_ctrl_t *otp_ctrl,
168 if (otp_ctrl == NULL) {
169 return kDifBadArg;
170 }
171
172 mmio_region_write32(
173 otp_ctrl->base_addr,
174 (ptrdiff_t)OTP_CTRL_INTR_STATE_REG_OFFSET,
175 snapshot);
176
177
178 return kDifOk;
179 }
180
182 dif_result_t dif_otp_ctrl_irq_is_pending(
183 const dif_otp_ctrl_t *otp_ctrl,
184 dif_otp_ctrl_irq_t irq,
185 bool *is_pending) {
186
187 if (otp_ctrl == NULL || is_pending == NULL) {
188 return kDifBadArg;
189 }
190
192 if (!otp_ctrl_get_irq_bit_index(irq, &index)) {
193 return kDifBadArg;
194 }
195
196 uint32_t intr_state_reg = mmio_region_read32(
197 otp_ctrl->base_addr,
198 (ptrdiff_t)OTP_CTRL_INTR_STATE_REG_OFFSET);
199
200
201 *is_pending = bitfield_bit32_read(intr_state_reg, index);
202
203 return kDifOk;
204 }
205
207 dif_result_t dif_otp_ctrl_irq_acknowledge_all(
208 const dif_otp_ctrl_t *otp_ctrl
209 ) {
210
211 if (otp_ctrl == NULL) {
212 return kDifBadArg;
213 }
214
215 // Writing to the register clears the corresponding bits (Write-one clear).
216 mmio_region_write32(
217 otp_ctrl->base_addr,
218 (ptrdiff_t)OTP_CTRL_INTR_STATE_REG_OFFSET,
219 UINT32_MAX);
220
221
222 return kDifOk;
223 }
224
226 dif_result_t dif_otp_ctrl_irq_acknowledge(
227 const dif_otp_ctrl_t *otp_ctrl,
228 dif_otp_ctrl_irq_t irq) {
229
230 if (otp_ctrl == NULL) {
231 return kDifBadArg;
232 }
233
235 if (!otp_ctrl_get_irq_bit_index(irq, &index)) {
236 return kDifBadArg;
237 }
238
239 // Writing to the register clears the corresponding bits (Write-one clear).
240 uint32_t intr_state_reg = bitfield_bit32_write(0, index, true);
241 mmio_region_write32(
242 otp_ctrl->base_addr,
243 (ptrdiff_t)OTP_CTRL_INTR_STATE_REG_OFFSET,
244 intr_state_reg);
245
246
247 return kDifOk;
248 }
249
251 dif_result_t dif_otp_ctrl_irq_force(
252 const dif_otp_ctrl_t *otp_ctrl,
253 dif_otp_ctrl_irq_t irq,
254 const bool val) {
255
256 if (otp_ctrl == NULL) {
257 return kDifBadArg;
258 }
259
261 if (!otp_ctrl_get_irq_bit_index(irq, &index)) {
262 return kDifBadArg;
263 }
264
265 uint32_t intr_test_reg = bitfield_bit32_write(0, index, val);
266 mmio_region_write32(
267 otp_ctrl->base_addr,
268 (ptrdiff_t)OTP_CTRL_INTR_TEST_REG_OFFSET,
269 intr_test_reg);
270
271
272 return kDifOk;
273 }
274
276 dif_result_t dif_otp_ctrl_irq_get_enabled(
277 const dif_otp_ctrl_t *otp_ctrl,
278 dif_otp_ctrl_irq_t irq,
279 dif_toggle_t *state) {
280
281 if (otp_ctrl == NULL || state == NULL) {
282 return kDifBadArg;
283 }
284
286 if (!otp_ctrl_get_irq_bit_index(irq, &index)) {
287 return kDifBadArg;
288 }
289
290 uint32_t intr_enable_reg = mmio_region_read32(
291 otp_ctrl->base_addr,
292 (ptrdiff_t)OTP_CTRL_INTR_ENABLE_REG_OFFSET);
293
294
295 bool is_enabled = bitfield_bit32_read(intr_enable_reg, index);
296 *state = is_enabled ?
298
299 return kDifOk;
300 }
301
303 dif_result_t dif_otp_ctrl_irq_set_enabled(
304 const dif_otp_ctrl_t *otp_ctrl,
305 dif_otp_ctrl_irq_t irq,
306 dif_toggle_t state) {
307
308 if (otp_ctrl == NULL) {
309 return kDifBadArg;
310 }
311
313 if (!otp_ctrl_get_irq_bit_index(irq, &index)) {
314 return kDifBadArg;
315 }
316
317 uint32_t intr_enable_reg = mmio_region_read32(
318 otp_ctrl->base_addr,
319 (ptrdiff_t)OTP_CTRL_INTR_ENABLE_REG_OFFSET);
320
321
322 bool enable_bit = (state == kDifToggleEnabled) ? true : false;
323 intr_enable_reg = bitfield_bit32_write(intr_enable_reg, index, enable_bit);
324 mmio_region_write32(
325 otp_ctrl->base_addr,
326 (ptrdiff_t)OTP_CTRL_INTR_ENABLE_REG_OFFSET,
327 intr_enable_reg);
328
329
330 return kDifOk;
331 }
332
334 dif_result_t dif_otp_ctrl_irq_disable_all(
335 const dif_otp_ctrl_t *otp_ctrl,
337
338 if (otp_ctrl == NULL) {
339 return kDifBadArg;
340 }
341
342 // Pass the current interrupt state to the caller, if requested.
343 if (snapshot != NULL) {
344 *snapshot = mmio_region_read32(
345 otp_ctrl->base_addr,
346 (ptrdiff_t)OTP_CTRL_INTR_ENABLE_REG_OFFSET);
347
348 }
349
350 // Disable all interrupts.
351 mmio_region_write32(
352 otp_ctrl->base_addr,
353 (ptrdiff_t)OTP_CTRL_INTR_ENABLE_REG_OFFSET,
354 0u);
355
356
357 return kDifOk;
358 }
359
361 dif_result_t dif_otp_ctrl_irq_restore_all(
362 const dif_otp_ctrl_t *otp_ctrl,
363 const dif_otp_ctrl_irq_enable_snapshot_t *snapshot) {
364
365 if (otp_ctrl == NULL || snapshot == NULL) {
366 return kDifBadArg;
367 }
368
369 mmio_region_write32(
370 otp_ctrl->base_addr,
371 (ptrdiff_t)OTP_CTRL_INTR_ENABLE_REG_OFFSET,
372 *snapshot);
373
374
375 return kDifOk;
376 }
377