11 #include "sw/device/lib/dif/dif_csrng_shared.h"
13 #include "csrng_regs.h"
18 static void get_output_status(
const dif_csrng_t *csrng,
21 mmio_region_read32(csrng->
base_addr, CSRNG_GENBITS_VLD_REG_OFFSET);
31 static void spin_until_ready(
const dif_csrng_t *csrng) {
34 get_output_status(csrng, &
status);
35 }
while (!
status.valid_data);
39 if (mmio_region_read32(csrng->
base_addr, CSRNG_REGWEN_REG_OFFSET) == 0) {
59 mmio_region_write32(csrng->
base_addr, CSRNG_CTRL_REG_OFFSET, reg);
66 if (csrng == NULL || seed_material == NULL) {
69 return csrng_send_app_cmd(csrng->
base_addr, kCsrngAppCmdTypeCsrng,
71 .id = kCsrngAppCmdInstantiate,
72 .entropy_src_enable = entropy_src_enable,
73 .seed_material = seed_material,
79 if (csrng == NULL || seed_material == NULL) {
82 return csrng_send_app_cmd(csrng->
base_addr, kCsrngAppCmdTypeCsrng,
84 .id = kCsrngAppCmdReseed,
85 .seed_material = seed_material,
91 if (csrng == NULL || seed_material == NULL) {
94 return csrng_send_app_cmd(csrng->
base_addr, kCsrngAppCmdTypeCsrng,
96 .id = kCsrngAppCmdUpdate,
97 .seed_material = seed_material,
102 if (csrng == NULL || len == 0) {
108 const uint32_t num_128bit_blocks = (len + 3) / 4;
109 return csrng_send_app_cmd(csrng->
base_addr, kCsrngAppCmdTypeCsrng,
111 .id = kCsrngAppCmdGenerate,
112 .generate_len = num_128bit_blocks,
118 if (csrng == NULL || buf == NULL) {
122 for (
size_t i = 0; i < len; ++i) {
124 if (i % kCsrngGenBitsBufferSize == 0) {
125 spin_until_ready(csrng);
127 buf[i] = mmio_region_read32(csrng->
base_addr, CSRNG_GENBITS_REG_OFFSET);
136 return csrng_send_app_cmd(csrng->
base_addr, kCsrngAppCmdTypeCsrng,
138 .id = kCsrngAppCmdUninstantiate,
144 if (csrng == NULL ||
status == NULL) {
150 mmio_region_read32(csrng->
base_addr, CSRNG_SW_CMD_STS_REG_OFFSET);
154 status->cmd_sts = cmd_sts;
178 case kDifCsrngFifoCmd:
179 fifo_bit = CSRNG_ERR_CODE_SFIFO_CMD_ERR_BIT;
181 case kDifCsrngFifoGenBits:
182 fifo_bit = CSRNG_ERR_CODE_SFIFO_GENBITS_ERR_BIT;
184 case kDifCsrngFifoCmdReq:
185 fifo_bit = CSRNG_ERR_CODE_SFIFO_CMDREQ_ERR_BIT;
187 case kDifCsrngFifoRcStage:
188 fifo_bit = CSRNG_ERR_CODE_SFIFO_RCSTAGE_ERR_BIT;
190 case kDifCsrngFifoKeyVrc:
191 fifo_bit = CSRNG_ERR_CODE_SFIFO_KEYVRC_ERR_BIT;
193 case kDifCsrngFifoUpdateReq:
194 fifo_bit = CSRNG_ERR_CODE_SFIFO_UPDREQ_ERR_BIT;
196 case kDifCsrngFifoBencRec:
197 fifo_bit = CSRNG_ERR_CODE_SFIFO_BENCREQ_ERR_BIT;
199 case kDifCsrngFifoBencAck:
200 fifo_bit = CSRNG_ERR_CODE_SFIFO_BENCACK_ERR_BIT;
202 case kDifCsrngFifoPData:
203 fifo_bit = CSRNG_ERR_CODE_SFIFO_PDATA_ERR_BIT;
205 case kDifCsrngFifoFinal:
206 fifo_bit = CSRNG_ERR_CODE_SFIFO_FINAL_ERR_BIT;
208 case kDifCsrngFifoGBencAck:
209 fifo_bit = CSRNG_ERR_CODE_SFIFO_GBENCACK_ERR_BIT;
211 case kDifCsrngFifoGrcStage:
212 fifo_bit = CSRNG_ERR_CODE_SFIFO_GRCSTAGE_ERR_BIT;
214 case kDifCsrngFifoGGenReq:
215 fifo_bit = CSRNG_ERR_CODE_SFIFO_GGENREQ_ERR_BIT;
217 case kDifCsrngFifoGadStage:
218 fifo_bit = CSRNG_ERR_CODE_SFIFO_GADSTAGE_ERR_BIT;
220 case kDifCsrngFifoBlockEnc:
221 fifo_bit = CSRNG_ERR_CODE_SFIFO_BLKENC_ERR_BIT;
228 mmio_region_write32(csrng->
base_addr, CSRNG_ERR_CODE_TEST_REG_OFFSET,
242 error_bit = CSRNG_ERR_CODE_CMD_STAGE_SM_ERR_BIT;
245 error_bit = CSRNG_ERR_CODE_MAIN_SM_ERR_BIT;
248 error_bit = CSRNG_ERR_CODE_DRBG_GEN_SM_ERR_BIT;
251 error_bit = CSRNG_ERR_CODE_DRBG_UPDBE_SM_ERR_BIT;
254 error_bit = CSRNG_ERR_CODE_DRBG_UPDOB_SM_ERR_BIT;
257 error_bit = CSRNG_ERR_CODE_AES_CIPHER_SM_ERR_BIT;
260 error_bit = CSRNG_ERR_CODE_CMD_GEN_CNT_ERR_BIT;
263 error_bit = CSRNG_ERR_CODE_FIFO_WRITE_ERR_BIT;
266 error_bit = CSRNG_ERR_CODE_FIFO_READ_ERR_BIT;
269 error_bit = CSRNG_ERR_CODE_FIFO_STATE_ERR_BIT;
276 mmio_region_write32(csrng->
base_addr, CSRNG_ERR_CODE_TEST_REG_OFFSET,
283 if (csrng == NULL || state == NULL) {
287 *state = mmio_region_read32(csrng->
base_addr, CSRNG_MAIN_SM_STATE_REG_OFFSET);
292 uint32_t *exceptions) {
293 if (csrng == NULL || exceptions == NULL) {
298 mmio_region_read32(csrng->
base_addr, CSRNG_HW_EXC_STS_REG_OFFSET);
307 mmio_region_write32(csrng->
base_addr, CSRNG_HW_EXC_STS_REG_OFFSET, 0);
313 if (csrng == NULL ||
status == NULL) {
316 get_output_status(csrng,
status);
323 if (csrng == NULL || state == NULL) {
330 0, CSRNG_INT_STATE_NUM_INT_STATE_NUM_FIELD, instance_id);
331 mmio_region_write32(csrng->
base_addr, CSRNG_INT_STATE_NUM_REG_OFFSET, reg);
332 uint32_t actual_reg =
333 mmio_region_read32(csrng->
base_addr, CSRNG_INT_STATE_NUM_REG_OFFSET);
334 if (reg != actual_reg) {
340 mmio_region_read32(csrng->
base_addr, CSRNG_INT_STATE_VAL_REG_OFFSET);
342 for (
size_t i = 0; i <
ARRAYSIZE(state->
v); ++i) {
344 mmio_region_read32(csrng->
base_addr, CSRNG_INT_STATE_VAL_REG_OFFSET);
349 mmio_region_read32(csrng->
base_addr, CSRNG_INT_STATE_VAL_REG_OFFSET);
353 mmio_region_read32(csrng->
base_addr, CSRNG_INT_STATE_VAL_REG_OFFSET);
365 uint32_t *reseed_counter) {
366 if (csrng == NULL || reseed_counter == NULL) {
371 switch (instance_id) {
373 reg_offset = CSRNG_RESEED_COUNTER_0_REG_OFFSET;
376 reg_offset = CSRNG_RESEED_COUNTER_1_REG_OFFSET;
379 reg_offset = CSRNG_RESEED_COUNTER_2_REG_OFFSET;
386 *reseed_counter = mmio_region_read32(csrng->
base_addr, (ptrdiff_t)reg_offset);
395 mmio_region_write32(csrng->
base_addr, CSRNG_REGWEN_REG_OFFSET, 0);
400 if (csrng == NULL || is_locked == NULL) {
403 *is_locked = check_locked(csrng) !=
kDifOk;
412 mmio_region_write32(csrng->
base_addr, CSRNG_CTRL_REG_OFFSET,
413 CSRNG_CTRL_REG_RESVAL);
419 if (csrng == NULL || alerts == NULL) {
424 mmio_region_read32(csrng->
base_addr, CSRNG_RECOV_ALERT_STS_REG_OFFSET);
433 mmio_region_write32(csrng->
base_addr, CSRNG_RECOV_ALERT_STS_REG_OFFSET, 0);