blob: b38e49f453036f67a4a648945513ea2b53e97ed0 [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
7#include <assert.h>
Andre Przywara927b3992020-10-08 00:43:50 +01008#include <stdbool.h>
9#include <stdint.h>
dp-arm8f59e152017-02-27 12:21:43 +000010#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <lib/mmio.h>
13#include <lib/utils_def.h>
Antonio Nino Diaza320ecd2019-01-15 14:19:50 +000014#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015
Roberto Vargas2b36b152018-02-12 12:36:17 +000016#include "juno_decl.h"
dp-arm8f59e152017-02-27 12:21:43 +000017
18#define NSAMPLE_CLOCKS 1 /* min 1 cycle, max 231 cycles */
19#define NRETRIES 5
20
Andre Przywara927b3992020-10-08 00:43:50 +010021/* initialised to false */
22static bool juno_trng_initialized;
23
24static bool output_valid(void)
dp-arm8f59e152017-02-27 12:21:43 +000025{
26 int i;
27
28 for (i = 0; i < NRETRIES; i++) {
29 uint32_t val;
30
31 val = mmio_read_32(TRNG_BASE + TRNG_STATUS);
32 if (val & 1U)
Andre Przywara927b3992020-10-08 00:43:50 +010033 return true;
dp-arm8f59e152017-02-27 12:21:43 +000034 }
Andre Przywara927b3992020-10-08 00:43:50 +010035 return false; /* No output data available. */
dp-arm8f59e152017-02-27 12:21:43 +000036}
37
38/*
Andre Przywara927b3992020-10-08 00:43:50 +010039 * This function fills `buf` with 8 bytes of entropy.
dp-arm8f59e152017-02-27 12:21:43 +000040 * It uses the Trusted Entropy Source peripheral on Juno.
Andre Przywara927b3992020-10-08 00:43:50 +010041 * Returns 'true' when the buffer has been filled with entropy
42 * successfully, or 'false' otherwise.
dp-arm8f59e152017-02-27 12:21:43 +000043 */
Andre Przywara927b3992020-10-08 00:43:50 +010044bool juno_getentropy(uint64_t *buf)
dp-arm8f59e152017-02-27 12:21:43 +000045{
Andre Przywara927b3992020-10-08 00:43:50 +010046 uint64_t ret;
dp-arm8f59e152017-02-27 12:21:43 +000047
48 assert(buf);
Andre Przywara927b3992020-10-08 00:43:50 +010049 assert(!check_uptr_overflow((uintptr_t)buf, sizeof(*buf)));
dp-arm8f59e152017-02-27 12:21:43 +000050
Andre Przywara927b3992020-10-08 00:43:50 +010051 if (!juno_trng_initialized) {
52 /* Disable interrupt mode. */
53 mmio_write_32(TRNG_BASE + TRNG_INTMASK, 0);
54 /* Program TRNG to sample for `NSAMPLE_CLOCKS`. */
55 mmio_write_32(TRNG_BASE + TRNG_CONFIG, NSAMPLE_CLOCKS);
56 /* Abort any potentially pending sampling. */
57 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 2);
58 /* Reset TRNG outputs. */
59 mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
dp-arm8f59e152017-02-27 12:21:43 +000060
Andre Przywara927b3992020-10-08 00:43:50 +010061 juno_trng_initialized = true;
62 }
dp-arm8f59e152017-02-27 12:21:43 +000063
Andre Przywara927b3992020-10-08 00:43:50 +010064 if (!output_valid()) {
dp-arm8f59e152017-02-27 12:21:43 +000065 /* Start TRNG. */
66 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
67
dp-arm8f59e152017-02-27 12:21:43 +000068 if (!output_valid())
Andre Przywara927b3992020-10-08 00:43:50 +010069 return false;
70 }
dp-arm8f59e152017-02-27 12:21:43 +000071
Andre Przywara927b3992020-10-08 00:43:50 +010072 /* XOR each two 32-bit registers together, combine the pairs */
73 ret = mmio_read_32(TRNG_BASE + 0);
74 ret ^= mmio_read_32(TRNG_BASE + 4);
75 ret <<= 32;
dp-arm8f59e152017-02-27 12:21:43 +000076
Andre Przywara927b3992020-10-08 00:43:50 +010077 ret |= mmio_read_32(TRNG_BASE + 8);
78 ret ^= mmio_read_32(TRNG_BASE + 12);
79 *buf = ret;
dp-arm8f59e152017-02-27 12:21:43 +000080
Andre Przywara927b3992020-10-08 00:43:50 +010081 /* Acknowledge current cycle, clear output registers. */
82 mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
83 /* Trigger next TRNG cycle. */
84 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
dp-arm8f59e152017-02-27 12:21:43 +000085
Andre Przywara927b3992020-10-08 00:43:50 +010086 return true;
dp-arm8f59e152017-02-27 12:21:43 +000087}