blob: 1af6deb72c5e6cc54361ca3a78784cd23e1ac1e0 [file] [log] [blame]
dp-arm8f59e152017-02-27 12:21:43 +00001/*
Roberto Vargas2b36b152018-02-12 12:36:17 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
dp-arm8f59e152017-02-27 12:21:43 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
dp-arm8f59e152017-02-27 12:21:43 +00005 */
6
Andre Przywarac7d10e32020-10-16 12:06:57 +01007#include <arm_acle.h>
dp-arm8f59e152017-02-27 12:21:43 +00008#include <assert.h>
Andre Przywara927b3992020-10-08 00:43:50 +01009#include <stdbool.h>
10#include <stdint.h>
dp-arm8f59e152017-02-27 12:21:43 +000011#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <lib/mmio.h>
14#include <lib/utils_def.h>
Antonio Nino Diaza320ecd2019-01-15 14:19:50 +000015#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016
Roberto Vargas2b36b152018-02-12 12:36:17 +000017#include "juno_decl.h"
dp-arm8f59e152017-02-27 12:21:43 +000018
19#define NSAMPLE_CLOCKS 1 /* min 1 cycle, max 231 cycles */
20#define NRETRIES 5
21
Andre Przywara927b3992020-10-08 00:43:50 +010022/* initialised to false */
23static bool juno_trng_initialized;
24
25static bool output_valid(void)
dp-arm8f59e152017-02-27 12:21:43 +000026{
27 int i;
28
29 for (i = 0; i < NRETRIES; i++) {
30 uint32_t val;
31
32 val = mmio_read_32(TRNG_BASE + TRNG_STATUS);
33 if (val & 1U)
Andre Przywara927b3992020-10-08 00:43:50 +010034 return true;
dp-arm8f59e152017-02-27 12:21:43 +000035 }
Andre Przywara927b3992020-10-08 00:43:50 +010036 return false; /* No output data available. */
dp-arm8f59e152017-02-27 12:21:43 +000037}
38
Andre Przywarac7d10e32020-10-16 12:06:57 +010039static uint32_t crc_value = ~0U;
40
dp-arm8f59e152017-02-27 12:21:43 +000041/*
Andre Przywara927b3992020-10-08 00:43:50 +010042 * This function fills `buf` with 8 bytes of entropy.
dp-arm8f59e152017-02-27 12:21:43 +000043 * It uses the Trusted Entropy Source peripheral on Juno.
Andre Przywara927b3992020-10-08 00:43:50 +010044 * Returns 'true' when the buffer has been filled with entropy
45 * successfully, or 'false' otherwise.
dp-arm8f59e152017-02-27 12:21:43 +000046 */
Andre Przywara927b3992020-10-08 00:43:50 +010047bool juno_getentropy(uint64_t *buf)
dp-arm8f59e152017-02-27 12:21:43 +000048{
Andre Przywara927b3992020-10-08 00:43:50 +010049 uint64_t ret;
dp-arm8f59e152017-02-27 12:21:43 +000050
51 assert(buf);
Andre Przywara927b3992020-10-08 00:43:50 +010052 assert(!check_uptr_overflow((uintptr_t)buf, sizeof(*buf)));
dp-arm8f59e152017-02-27 12:21:43 +000053
Andre Przywara927b3992020-10-08 00:43:50 +010054 if (!juno_trng_initialized) {
55 /* Disable interrupt mode. */
56 mmio_write_32(TRNG_BASE + TRNG_INTMASK, 0);
57 /* Program TRNG to sample for `NSAMPLE_CLOCKS`. */
58 mmio_write_32(TRNG_BASE + TRNG_CONFIG, NSAMPLE_CLOCKS);
59 /* Abort any potentially pending sampling. */
60 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 2);
61 /* Reset TRNG outputs. */
62 mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
dp-arm8f59e152017-02-27 12:21:43 +000063
Andre Przywara927b3992020-10-08 00:43:50 +010064 juno_trng_initialized = true;
65 }
dp-arm8f59e152017-02-27 12:21:43 +000066
Andre Przywara927b3992020-10-08 00:43:50 +010067 if (!output_valid()) {
dp-arm8f59e152017-02-27 12:21:43 +000068 /* Start TRNG. */
69 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
70
dp-arm8f59e152017-02-27 12:21:43 +000071 if (!output_valid())
Andre Przywara927b3992020-10-08 00:43:50 +010072 return false;
73 }
dp-arm8f59e152017-02-27 12:21:43 +000074
Andre Przywarac7d10e32020-10-16 12:06:57 +010075 /* CRC each two 32-bit registers together, combine the pairs */
76 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 0));
77 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 4));
78 ret = (uint64_t)crc_value << 32;
dp-arm8f59e152017-02-27 12:21:43 +000079
Andre Przywarac7d10e32020-10-16 12:06:57 +010080 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 8));
81 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 12));
82 *buf = ret | crc_value;
dp-arm8f59e152017-02-27 12:21:43 +000083
Andre Przywara927b3992020-10-08 00:43:50 +010084 /* Acknowledge current cycle, clear output registers. */
85 mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
86 /* Trigger next TRNG cycle. */
87 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
dp-arm8f59e152017-02-27 12:21:43 +000088
Andre Przywara927b3992020-10-08 00:43:50 +010089 return true;
dp-arm8f59e152017-02-27 12:21:43 +000090}