juno_trng.c 2.5 KB
Newer Older
1
/*
Roberto Vargas's avatar
Roberto Vargas committed
2
 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
#include <arm_acle.h>
8
#include <assert.h>
9
10
#include <stdbool.h>
#include <stdint.h>
11
#include <string.h>
12
13
14

#include <lib/mmio.h>
#include <lib/utils_def.h>
15
#include <platform_def.h>
16

17
18
19
20
21
#include <lib/smccc.h>
#include <services/trng_svc.h>
#include <smccc_helpers.h>

#include <plat/common/platform.h>
22
23
24
25

#define NSAMPLE_CLOCKS	1 /* min 1 cycle, max 231 cycles */
#define NRETRIES	5

26
27
28
29
/* initialised to false */
static bool juno_trng_initialized;

static bool output_valid(void)
30
31
32
33
34
35
36
37
{
	int i;

	for (i = 0; i < NRETRIES; i++) {
		uint32_t val;

		val = mmio_read_32(TRNG_BASE + TRNG_STATUS);
		if (val & 1U)
38
			return true;
39
	}
40
	return false; /* No output data available. */
41
42
}

43
44
45
46
47
48
DEFINE_SVC_UUID2(_plat_trng_uuid,
	0x23523c58, 0x7448, 0x4083, 0x9d, 0x16,
	0xe3, 0xfa, 0xb9, 0xf1, 0x73, 0xbc
);
uuid_t plat_trng_uuid;

49
50
static uint32_t crc_value = ~0U;

51
/*
52
53
 * Uses the Trusted Entropy Source peripheral on Juno to return 8 bytes of
 * entropy. Returns 'true' when done successfully, 'false' otherwise.
54
 */
55
bool plat_get_entropy(uint64_t *out)
56
{
57
	uint64_t ret;
58

59
60
	assert(out);
	assert(!check_uptr_overflow((uintptr_t)out, sizeof(*out)));
61
62
63
64
65
66
67
68
69
70

	if (!juno_trng_initialized) {
		/* Disable interrupt mode. */
		mmio_write_32(TRNG_BASE + TRNG_INTMASK, 0);
		/* Program TRNG to sample for `NSAMPLE_CLOCKS`. */
		mmio_write_32(TRNG_BASE + TRNG_CONFIG, NSAMPLE_CLOCKS);
		/* Abort any potentially pending sampling. */
		mmio_write_32(TRNG_BASE + TRNG_CONTROL, 2);
		/* Reset TRNG outputs. */
		mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
71

72
73
		juno_trng_initialized = true;
	}
74

75
	if (!output_valid()) {
76
77
78
79
		/* Start TRNG. */
		mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);

		if (!output_valid())
80
			return false;
81
82
	}

83
84
85
86
	/* CRC each two 32-bit registers together, combine the pairs */
	crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 0));
	crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 4));
	ret = (uint64_t)crc_value << 32;
87

88
89
	crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 8));
	crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 12));
90
	*out = ret | crc_value;
91
92
93
94
95
96
97

	/* Acknowledge current cycle, clear output registers. */
	mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
	/* Trigger next TRNG cycle. */
	mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);

	return true;
98
}
99
100
101
102
103
104
105
106
107
108

void plat_entropy_setup(void)
{
	uint64_t dummy;

	plat_trng_uuid = _plat_trng_uuid;

	/* Initialise the entropy source and trigger RNG generation */
	plat_get_entropy(&dummy);
}