2017-07-01 05:48:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Erwin Rol <erwin@erwinrol.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-03 17:55:50 +08:00
|
|
|
#define DT_DRV_COMPAT st_stm32_rng
|
|
|
|
|
2017-07-01 05:48:22 +08:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <device.h>
|
2019-06-26 03:53:49 +08:00
|
|
|
#include <drivers/entropy.h>
|
2017-10-14 06:45:02 +08:00
|
|
|
#include <random/rand32.h>
|
2017-07-01 05:48:22 +08:00
|
|
|
#include <init.h>
|
2019-06-26 22:33:39 +08:00
|
|
|
#include <sys/__assert.h>
|
2019-06-26 22:33:55 +08:00
|
|
|
#include <sys/util.h>
|
2017-07-01 05:48:22 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <soc.h>
|
2019-06-26 22:33:49 +08:00
|
|
|
#include <sys/printk.h>
|
2019-06-26 03:53:47 +08:00
|
|
|
#include <drivers/clock_control.h>
|
2020-01-25 19:34:53 +08:00
|
|
|
#include <drivers/clock_control/stm32_clock_control.h>
|
2017-07-01 05:48:22 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
struct entropy_stm32_rng_dev_cfg {
|
2017-07-01 05:48:22 +08:00
|
|
|
struct stm32_pclken pclken;
|
|
|
|
};
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
struct entropy_stm32_rng_dev_data {
|
2017-07-01 05:48:22 +08:00
|
|
|
RNG_TypeDef *rng;
|
|
|
|
struct device *clock;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEV_DATA(dev) \
|
2017-10-14 07:30:55 +08:00
|
|
|
((struct entropy_stm32_rng_dev_data *)(dev)->driver_data)
|
2017-07-01 05:48:22 +08:00
|
|
|
|
|
|
|
#define DEV_CFG(dev) \
|
2017-10-14 07:30:55 +08:00
|
|
|
((struct entropy_stm32_rng_dev_cfg *)(dev)->config->config_info)
|
2017-07-01 05:48:22 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static void entropy_stm32_rng_reset(RNG_TypeDef *rng)
|
2017-07-01 05:48:22 +08:00
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(rng != NULL);
|
|
|
|
|
|
|
|
/* Reset RNG as described in RM0090 Reference manual
|
|
|
|
* section 24.3.2 Error management.
|
|
|
|
*/
|
|
|
|
LL_RNG_ClearFlag_CEIS(rng);
|
|
|
|
LL_RNG_ClearFlag_SEIS(rng);
|
|
|
|
|
|
|
|
LL_RNG_Disable(rng);
|
|
|
|
LL_RNG_Enable(rng);
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_stm32_got_error(RNG_TypeDef *rng)
|
2017-07-01 05:48:22 +08:00
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(rng != NULL);
|
|
|
|
|
|
|
|
if (LL_RNG_IsActiveFlag_CECS(rng)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LL_RNG_IsActiveFlag_SECS(rng)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_stm32_wait_ready(RNG_TypeDef *rng)
|
2017-07-01 05:48:22 +08:00
|
|
|
{
|
|
|
|
/* Agording to the reference manual it takes 40 periods
|
|
|
|
* of the RNG_CLK clock signal between two consecutive
|
|
|
|
* random numbers. Also RNG_CLK may not be smaller than
|
|
|
|
* HCLK/16. So it should not take more than 640 HCLK
|
|
|
|
* ticks. Assuming the CPU can do 1 instruction per HCLK
|
|
|
|
* the number of times to loop before the RNG is ready
|
|
|
|
* is less than 1000. And that is when assumming the loop
|
|
|
|
* only takes 1 instruction. So looping a million times
|
|
|
|
* should be more than enough.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int timeout = 1000000;
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(rng != NULL);
|
|
|
|
|
|
|
|
while (!LL_RNG_IsActiveFlag_DRDY(rng)) {
|
2017-10-14 07:30:55 +08:00
|
|
|
if (entropy_stm32_got_error(rng)) {
|
2017-07-01 05:48:22 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout-- == 0) {
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_yield();
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
if (entropy_stm32_got_error(rng)) {
|
2017-07-01 05:48:22 +08:00
|
|
|
return -EIO;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_stm32_rng_get_entropy(struct device *dev, u8_t *buffer,
|
|
|
|
u16_t length)
|
2017-07-01 05:48:22 +08:00
|
|
|
{
|
2017-10-14 07:30:55 +08:00
|
|
|
struct entropy_stm32_rng_dev_data *dev_data;
|
2017-07-01 05:48:22 +08:00
|
|
|
int n = sizeof(u32_t);
|
|
|
|
int res;
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(dev != NULL);
|
|
|
|
__ASSERT_NO_MSG(buffer != NULL);
|
|
|
|
|
|
|
|
dev_data = DEV_DATA(dev);
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(dev_data != NULL);
|
|
|
|
|
|
|
|
/* if the RNG has errors reset it before use */
|
2017-10-14 07:30:55 +08:00
|
|
|
if (entropy_stm32_got_error(dev_data->rng)) {
|
|
|
|
entropy_stm32_rng_reset(dev_data->rng);
|
2017-07-01 05:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
u32_t rndbits;
|
|
|
|
u8_t *p_rndbits = (u8_t *)&rndbits;
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
res = entropy_stm32_wait_ready(dev_data->rng);
|
2017-07-01 05:48:22 +08:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
rndbits = LL_RNG_ReadRandData32(dev_data->rng);
|
|
|
|
|
|
|
|
if (length < sizeof(u32_t))
|
|
|
|
n = length;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
*buffer++ = *p_rndbits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_stm32_rng_init(struct device *dev)
|
2017-07-01 05:48:22 +08:00
|
|
|
{
|
2017-10-14 07:30:55 +08:00
|
|
|
struct entropy_stm32_rng_dev_data *dev_data;
|
|
|
|
struct entropy_stm32_rng_dev_cfg *dev_cfg;
|
2018-12-07 18:09:28 +08:00
|
|
|
int res;
|
2017-07-01 05:48:22 +08:00
|
|
|
|
|
|
|
__ASSERT_NO_MSG(dev != NULL);
|
|
|
|
|
|
|
|
dev_data = DEV_DATA(dev);
|
|
|
|
dev_cfg = DEV_CFG(dev);
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(dev_data != NULL);
|
|
|
|
__ASSERT_NO_MSG(dev_cfg != NULL);
|
|
|
|
|
2017-12-21 17:47:08 +08:00
|
|
|
#if CONFIG_SOC_SERIES_STM32L4X
|
|
|
|
/* Configure PLLSA11 to enable 48M domain */
|
|
|
|
LL_RCC_PLLSAI1_ConfigDomain_48M(LL_RCC_PLLSOURCE_MSI,
|
|
|
|
LL_RCC_PLLM_DIV_1,
|
|
|
|
24, LL_RCC_PLLSAI1Q_DIV_2);
|
|
|
|
|
|
|
|
/* Enable PLLSA1 */
|
|
|
|
LL_RCC_PLLSAI1_Enable();
|
|
|
|
|
|
|
|
/* Enable PLLSAI1 output mapped on 48MHz domain clock */
|
|
|
|
LL_RCC_PLLSAI1_EnableDomain_48M();
|
|
|
|
|
|
|
|
/* Wait for PLLSA1 ready flag */
|
|
|
|
while (LL_RCC_PLLSAI1_IsReady() != 1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the peripherals independent clock configuration register :
|
|
|
|
* choose PLLSAI1 source as the 48 MHz clock is needed for the RNG
|
|
|
|
* Linear Feedback Shift Register
|
|
|
|
*/
|
|
|
|
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_PLLSAI1);
|
2019-09-04 16:47:46 +08:00
|
|
|
#elif CONFIG_SOC_SERIES_STM32G4X
|
|
|
|
/* Use the HSI48 for the RNG */
|
|
|
|
LL_RCC_HSI48_Enable();
|
|
|
|
while (!LL_RCC_HSI48_IsReady()) {
|
|
|
|
/* Wait for HSI48 to become ready */
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_HSI48);
|
2017-12-21 17:47:08 +08:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32L4X */
|
|
|
|
|
2017-07-01 05:48:22 +08:00
|
|
|
dev_data->clock = device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
|
|
|
__ASSERT_NO_MSG(dev_data->clock != NULL);
|
|
|
|
|
2018-12-07 18:09:28 +08:00
|
|
|
res = clock_control_on(dev_data->clock,
|
2017-07-01 05:48:22 +08:00
|
|
|
(clock_control_subsys_t *)&dev_cfg->pclken);
|
2019-02-20 19:28:53 +08:00
|
|
|
__ASSERT_NO_MSG(res == 0);
|
2017-07-01 05:48:22 +08:00
|
|
|
|
|
|
|
LL_RNG_Enable(dev_data->rng);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static const struct entropy_driver_api entropy_stm32_rng_api = {
|
|
|
|
.get_entropy = entropy_stm32_rng_get_entropy
|
2017-07-01 05:48:22 +08:00
|
|
|
};
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static const struct entropy_stm32_rng_dev_cfg entropy_stm32_rng_config = {
|
2020-04-03 17:55:50 +08:00
|
|
|
.pclken = { .bus = DT_INST_CLOCKS_CELL(0, bus),
|
|
|
|
.enr = DT_INST_CLOCKS_CELL(0, bits) },
|
2017-07-01 05:48:22 +08:00
|
|
|
};
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static struct entropy_stm32_rng_dev_data entropy_stm32_rng_data = {
|
2020-04-03 17:55:50 +08:00
|
|
|
.rng = (RNG_TypeDef *)DT_INST_REG_ADDR(0),
|
2017-07-01 05:48:22 +08:00
|
|
|
};
|
|
|
|
|
2020-04-03 17:55:50 +08:00
|
|
|
DEVICE_AND_API_INIT(entropy_stm32_rng, DT_INST_LABEL(0),
|
2017-10-14 07:30:55 +08:00
|
|
|
entropy_stm32_rng_init,
|
|
|
|
&entropy_stm32_rng_data, &entropy_stm32_rng_config,
|
2018-05-23 06:08:38 +08:00
|
|
|
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
2017-10-14 07:30:55 +08:00
|
|
|
&entropy_stm32_rng_api);
|