From 39db721f9ace13cc210278c88c3f364b5d67b5b8 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Wed, 31 May 2017 12:12:12 +0800 Subject: hammer: Add board_get_entropy function (and test) This function will be used to generate some entropy using the Clock Recovery System. BRANCH=none BUG=b:38486828 TEST=make BOARD=hammer -j tests ./util/flash_ec --board=hammer --image=build/hammer/test-entropy.bin EC console: runtest TEST=Test fails when no USB connection is active TEST=Test passes when USB connection is active TEST=Pasting the values into: tr ';' '\n' | awk 'BEGIN { e = 0; tot=16384.0 } { p = $1/tot; if (p > 0) { e -= p*log(p)/log(2) } } END { print e }' shows an entropy > 4 bits per sample. Change-Id: I2363c7bce42c72c33ef0bf3f099d709ee9c13d13 Reviewed-on: https://chromium-review.googlesource.com/518608 Commit-Ready: Nicolas Boichat Tested-by: Nicolas Boichat Reviewed-by: Vincent Palatin --- board/hammer/board.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'board/hammer') diff --git a/board/hammer/board.c b/board/hammer/board.c index d50634aab8..b9f21acf1d 100644 --- a/board/hammer/board.c +++ b/board/hammer/board.c @@ -9,12 +9,14 @@ #include "touchpad_elan.h" #include "gpio.h" #include "hooks.h" +#include "hwtimer.h" #include "i2c.h" #include "keyboard_raw.h" #include "keyboard_scan.h" #include "pwm.h" #include "pwm_chip.h" #include "registers.h" +#include "rollback.h" #include "task.h" #include "timer.h" #include "update_fw.h" @@ -135,3 +137,30 @@ void board_usb_wake(void) gpio_set_flags(GPIO_BASE_DET, GPIO_INPUT); interrupt_enable(); } + +/* + * Get entropy based on Clock Recovery System, which is enabled on hammer to + * synchronize USB SOF with internal oscillator. + */ +int board_get_entropy(void *buffer, int len) +{ + int i = 0; + uint8_t *data = buffer; + uint32_t start; + /* We expect one SOF per ms, so wait at most 2ms. */ + const uint32_t timeout = 2*MSEC; + + for (i = 0; i < len; i++) { + STM32_CRS_ICR |= STM32_CRS_ICR_SYNCOKC; + start = __hw_clock_source_read(); + while (!(STM32_CRS_ISR & STM32_CRS_ISR_SYNCOKF)) { + if ((__hw_clock_source_read() - start) > timeout) + return 0; + usleep(500); + } + /* Pick 8 bits, including FEDIR and 7 LSB of FECAP. */ + data[i] = STM32_CRS_ISR >> 15; + } + + return 1; +} -- cgit v1.2.1