mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2026-01-06 23:51:28 +00:00
It turns out TRNG could turn idle under certain circumstances, and needs to be restarted in that case. This code adds a check for the idle state and necessary recovery. BRANCH=none BUG=b:27646393 TEST=none yet Change-Id: Ibd0a13f40f5ce081d4211b2c0f1026468967f826 Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/332573 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
/* 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)) {
|
|
if (!GREAD_FIELD(TRNG, FSM_STATE, FSM_IDLE))
|
|
continue;
|
|
|
|
/* TRNG must have stopped, needs to be restarted. */
|
|
GWRITE(TRNG, STOP_WORK, 1);
|
|
init_trng();
|
|
}
|
|
return GREAD(TRNG, READ_DATA);
|
|
}
|
|
|
|
void rand_bytes(void *buffer, size_t len)
|
|
{
|
|
int random_togo = 0;
|
|
int buffer_index = 0;
|
|
uint32_t random_value;
|
|
uint8_t *buf = (uint8_t *) buffer;
|
|
|
|
/*
|
|
* Retrieve random numbers in 4 byte quantities and pack as many bytes
|
|
* as needed into 'buffer'. If len is not divisible by 4, the
|
|
* remaining random bytes get dropped.
|
|
*/
|
|
while (buffer_index < len) {
|
|
if (!random_togo) {
|
|
random_value = rand();
|
|
random_togo = sizeof(random_value);
|
|
}
|
|
buf[buffer_index++] = random_value >>
|
|
((random_togo-- - 1) * 8);
|
|
}
|
|
}
|