From 34a745efe89c735162207531b3b9211fa88789bc Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Fri, 16 Oct 2015 14:35:32 -0700 Subject: [PATCH] g: implement support for hardware based TRNG The TRNG operation is simple: once started it begins to fill up an internal FIFO with random values. The consumer of these values might have to wait if the next number is not ready yet. BRANCH=none BUG=chrome-os-partner:43025 TEST=with the rest of the patches in place TPM2 gets a stream of random numbers when required Change-Id: I877452733377ec5b179fb6df8581af570b4f3668 Signed-off-by: Vadim Bendebury Reviewed-on: https://chromium-review.googlesource.com/306689 Reviewed-by: Bill Richardson --- chip/g/build.mk | 1 + chip/g/trng.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 chip/g/trng.c diff --git a/chip/g/build.mk b/chip/g/build.mk index 6b235ced4b..4450faf43a 100644 --- a/chip/g/build.mk +++ b/chip/g/build.mk @@ -25,6 +25,7 @@ else chip-y += uart.o endif chip-y+= pmu.o +chip-y+= trng.o chip-$(CONFIG_SPS)+= sps.o chip-$(CONFIG_HOSTCMD_SPS)+=sps_hc.o chip-$(CONFIG_TPM_SPS)+=sps_tpm.o diff --git a/chip/g/trng.c b/chip/g/trng.c new file mode 100644 index 0000000000..e462127da8 --- /dev/null +++ b/chip/g/trng.c @@ -0,0 +1,22 @@ +/* Copyright 2015 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "registers.h" + +void init_trng(void) +{ + GWRITE(TRNG, POWER_DOWN_B, 1); + GWRITE(TRNG, GO_EVENT, 1); + while (GREAD(TRNG, EMPTY)) + ; + GREAD(TRNG, READ_DATA); +} + +uint32_t rand(void) +{ + while (GREAD(TRNG, EMPTY)) + ; + return GREAD(TRNG, READ_DATA); +}