2017-06-30 23:48:22 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Erwin Rol <erwin@erwinrol.com>
|
2020-08-31 17:46:01 +02:00
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2017 Exati Tecnologia Ltda.
|
|
|
|
* Copyright (c) 2020 STMicroelectronics.
|
2017-06-30 23:48:22 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-03 11:55:50 +02:00
|
|
|
#define DT_DRV_COMPAT st_stm32_rng
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/entropy.h>
|
|
|
|
#include <zephyr/random/rand32.h>
|
|
|
|
#include <zephyr/init.h>
|
|
|
|
#include <zephyr/sys/__assert.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
2017-06-30 23:48:22 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <soc.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/pm/policy.h>
|
2020-11-20 15:43:34 +01:00
|
|
|
#include <stm32_ll_bus.h>
|
|
|
|
#include <stm32_ll_rcc.h>
|
|
|
|
#include <stm32_ll_rng.h>
|
|
|
|
#include <stm32_ll_system.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/sys/printk.h>
|
|
|
|
#include <zephyr/drivers/clock_control.h>
|
|
|
|
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
|
2020-07-08 11:18:07 +02:00
|
|
|
#include "stm32_hsem.h"
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
#define IRQN DT_INST_IRQN(0)
|
|
|
|
#define IRQ_PRIO DT_INST_IRQ(0, priority)
|
|
|
|
|
2021-07-15 16:59:29 +02:00
|
|
|
#if defined(RNG_CR_CONDRST)
|
|
|
|
#define STM32_CONDRST_SUPPORT
|
|
|
|
#endif
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
/*
|
|
|
|
* This driver need to take into account all STM32 family:
|
2022-03-16 21:07:43 +00:00
|
|
|
* - simple rng without hardware fifo and no DMA.
|
2020-08-31 17:46:01 +02:00
|
|
|
* - Variable delay between two consecutive random numbers
|
|
|
|
* (depending on family and clock settings)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Due to the first byte in a stream of bytes being more costly on
|
|
|
|
* some platforms a "water system" inspired algorithm is used to
|
|
|
|
* amortize the cost of the first byte.
|
|
|
|
*
|
|
|
|
* The algorithm will delay generation of entropy until the amount of
|
|
|
|
* bytes goes below THRESHOLD, at which point it will generate entropy
|
|
|
|
* until the BUF_LEN limit is reached.
|
|
|
|
*
|
|
|
|
* The entropy level is checked at the end of every consumption of
|
|
|
|
* entropy.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct rng_pool {
|
|
|
|
uint8_t first_alloc;
|
|
|
|
uint8_t first_read;
|
|
|
|
uint8_t last;
|
|
|
|
uint8_t mask;
|
|
|
|
uint8_t threshold;
|
|
|
|
uint8_t buffer[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define RNG_POOL_DEFINE(name, len) uint8_t name[sizeof(struct rng_pool) + (len)]
|
|
|
|
|
|
|
|
BUILD_ASSERT((CONFIG_ENTROPY_STM32_ISR_POOL_SIZE &
|
|
|
|
(CONFIG_ENTROPY_STM32_ISR_POOL_SIZE - 1)) == 0,
|
|
|
|
"The CONFIG_ENTROPY_STM32_ISR_POOL_SIZE must be a power of 2!");
|
|
|
|
|
|
|
|
BUILD_ASSERT((CONFIG_ENTROPY_STM32_THR_POOL_SIZE &
|
|
|
|
(CONFIG_ENTROPY_STM32_THR_POOL_SIZE - 1)) == 0,
|
|
|
|
"The CONFIG_ENTROPY_STM32_THR_POOL_SIZE must be a power of 2!");
|
|
|
|
|
2017-10-13 16:30:55 -07:00
|
|
|
struct entropy_stm32_rng_dev_cfg {
|
2017-06-30 23:48:22 +02:00
|
|
|
struct stm32_pclken pclken;
|
|
|
|
};
|
|
|
|
|
2017-10-13 16:30:55 -07:00
|
|
|
struct entropy_stm32_rng_dev_data {
|
2017-06-30 23:48:22 +02:00
|
|
|
RNG_TypeDef *rng;
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *clock;
|
2020-08-31 17:46:01 +02:00
|
|
|
struct k_sem sem_lock;
|
|
|
|
struct k_sem sem_sync;
|
|
|
|
|
|
|
|
RNG_POOL_DEFINE(isr, CONFIG_ENTROPY_STM32_ISR_POOL_SIZE);
|
|
|
|
RNG_POOL_DEFINE(thr, CONFIG_ENTROPY_STM32_THR_POOL_SIZE);
|
2017-06-30 23:48:22 +02:00
|
|
|
};
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
static const struct entropy_stm32_rng_dev_cfg entropy_stm32_rng_config = {
|
|
|
|
.pclken = { .bus = DT_INST_CLOCKS_CELL(0, bus),
|
|
|
|
.enr = DT_INST_CLOCKS_CELL(0, bits) },
|
|
|
|
};
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
static struct entropy_stm32_rng_dev_data entropy_stm32_rng_data = {
|
|
|
|
.rng = (RNG_TypeDef *)DT_INST_REG_ADDR(0),
|
|
|
|
};
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2017-10-13 16:30:55 -07:00
|
|
|
static int entropy_stm32_got_error(RNG_TypeDef *rng)
|
2017-06-30 23:48:22 +02:00
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(rng != NULL);
|
|
|
|
|
|
|
|
if (LL_RNG_IsActiveFlag_CECS(rng)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:59:29 +02:00
|
|
|
if (LL_RNG_IsActiveFlag_SEIS(rng)) {
|
2017-06-30 23:48:22 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:59:29 +02:00
|
|
|
#if defined(STM32_CONDRST_SUPPORT)
|
|
|
|
/* SOCS w/ soft-reset support: execute the reset */
|
|
|
|
static int recover_seed_error(RNG_TypeDef *rng)
|
|
|
|
{
|
|
|
|
uint32_t count_timeout = 0;
|
|
|
|
|
|
|
|
LL_RNG_EnableCondReset(rng);
|
|
|
|
LL_RNG_DisableCondReset(rng);
|
|
|
|
/* When reset process is done cond reset bit is read 0
|
|
|
|
* This typically takes: 2 AHB clock cycles + 2 RNG clock cycles.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (LL_RNG_IsEnabledCondReset(rng) ||
|
|
|
|
LL_RNG_IsActiveFlag_SEIS(rng) ||
|
|
|
|
LL_RNG_IsActiveFlag_SECS(rng)) {
|
|
|
|
count_timeout++;
|
|
|
|
if (count_timeout == 10) {
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !STM32_CONDRST_SUPPORT */
|
|
|
|
/* SOCS w/o soft-reset support: flush pipeline */
|
|
|
|
static int recover_seed_error(RNG_TypeDef *rng)
|
|
|
|
{
|
|
|
|
LL_RNG_ClearFlag_SEIS(rng);
|
|
|
|
|
|
|
|
for (int i = 0; i < 12; ++i) {
|
|
|
|
LL_RNG_ReadRandData32(rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LL_RNG_IsActiveFlag_SEIS(rng) != 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* !STM32_CONDRST_SUPPORT */
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
static int random_byte_get(void)
|
2017-06-30 23:48:22 +02:00
|
|
|
{
|
2020-08-31 17:46:01 +02:00
|
|
|
int retval = -EAGAIN;
|
|
|
|
unsigned int key;
|
2021-07-15 16:59:29 +02:00
|
|
|
RNG_TypeDef *rng = entropy_stm32_rng_data.rng;
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
key = irq_lock();
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2021-07-15 16:59:29 +02:00
|
|
|
if (LL_RNG_IsActiveFlag_SEIS(rng) && (recover_seed_error(rng) < 0)) {
|
|
|
|
retval = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((LL_RNG_IsActiveFlag_DRDY(rng) == 1)) {
|
|
|
|
if (entropy_stm32_got_error(rng)) {
|
2020-08-31 17:46:01 +02:00
|
|
|
retval = -EIO;
|
2021-07-15 16:59:29 +02:00
|
|
|
goto out;
|
2017-06-30 23:48:22 +02:00
|
|
|
}
|
2021-07-15 16:59:29 +02:00
|
|
|
|
|
|
|
retval = LL_RNG_ReadRandData32(rng);
|
2021-07-19 17:11:24 +02:00
|
|
|
if (retval == 0) {
|
|
|
|
/* A seed error could have occurred between RNG_SR
|
|
|
|
* polling and RND_DR output reading.
|
|
|
|
*/
|
|
|
|
retval = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:59:29 +02:00
|
|
|
retval &= 0xFF;
|
2020-08-31 17:46:01 +02:00
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2021-07-15 16:59:29 +02:00
|
|
|
out:
|
2020-08-31 17:46:01 +02:00
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
#pragma GCC push_options
|
|
|
|
#if defined(CONFIG_BT_CTLR_FAST_ENC)
|
|
|
|
#pragma GCC optimize ("Ofast")
|
|
|
|
#endif
|
|
|
|
static uint16_t rng_pool_get(struct rng_pool *rngp, uint8_t *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
uint32_t last = rngp->last;
|
|
|
|
uint32_t mask = rngp->mask;
|
|
|
|
uint8_t *dst = buf;
|
|
|
|
uint32_t first, available;
|
|
|
|
uint32_t other_read_in_progress;
|
|
|
|
unsigned int key;
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
first = rngp->first_alloc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The other_read_in_progress is non-zero if rngp->first_read != first,
|
|
|
|
* which means that lower-priority code (which was interrupted by this
|
|
|
|
* call) already allocated area for read.
|
|
|
|
*/
|
|
|
|
other_read_in_progress = (rngp->first_read ^ first);
|
|
|
|
|
|
|
|
available = (last - first) & mask;
|
|
|
|
if (available < len) {
|
|
|
|
len = available;
|
2017-06-30 23:48:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
/*
|
|
|
|
* Move alloc index forward to signal, that part of the buffer is
|
|
|
|
* now reserved for this call.
|
|
|
|
*/
|
|
|
|
rngp->first_alloc = (first + len) & mask;
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
while (likely(len--)) {
|
|
|
|
*dst++ = rngp->buffer[first];
|
|
|
|
first = (first + 1) & mask;
|
2017-06-30 23:48:22 +02:00
|
|
|
}
|
2020-08-31 17:46:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this call is the last one accessing the pool, move read index
|
|
|
|
* to signal that all allocated regions are now read and could be
|
|
|
|
* overwritten.
|
|
|
|
*/
|
|
|
|
if (likely(!other_read_in_progress)) {
|
|
|
|
key = irq_lock();
|
|
|
|
rngp->first_read = rngp->first_alloc;
|
|
|
|
irq_unlock(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = dst - buf;
|
|
|
|
available = available - len;
|
2022-02-02 09:37:55 +01:00
|
|
|
if ((available <= rngp->threshold)
|
|
|
|
&& !LL_RNG_IsEnabledIT(entropy_stm32_rng_data.rng)) {
|
2022-04-07 13:06:12 +02:00
|
|
|
pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
|
2020-08-31 17:46:01 +02:00
|
|
|
LL_RNG_EnableIT(entropy_stm32_rng_data.rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
2017-06-30 23:48:22 +02:00
|
|
|
}
|
2020-08-31 17:46:01 +02:00
|
|
|
#pragma GCC pop_options
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
static int rng_pool_put(struct rng_pool *rngp, uint8_t byte)
|
2017-06-30 23:48:22 +02:00
|
|
|
{
|
2020-08-31 17:46:01 +02:00
|
|
|
uint8_t first = rngp->first_read;
|
|
|
|
uint8_t last = rngp->last;
|
|
|
|
uint8_t mask = rngp->mask;
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
/* Signal error if the pool is full. */
|
|
|
|
if (((last - first) & mask) == mask) {
|
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
rngp->buffer[last] = byte;
|
|
|
|
rngp->last = (last + 1) & mask;
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
static void rng_pool_init(struct rng_pool *rngp, uint16_t size,
|
|
|
|
uint8_t threshold)
|
|
|
|
{
|
|
|
|
rngp->first_alloc = 0U;
|
|
|
|
rngp->first_read = 0U;
|
|
|
|
rngp->last = 0U;
|
|
|
|
rngp->mask = size - 1;
|
|
|
|
rngp->threshold = threshold;
|
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
static void stm32_rng_isr(const void *arg)
|
2020-08-31 17:46:01 +02:00
|
|
|
{
|
|
|
|
int byte, ret;
|
|
|
|
|
|
|
|
ARG_UNUSED(arg);
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
byte = random_byte_get();
|
|
|
|
if (byte < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
ret = rng_pool_put((struct rng_pool *)(entropy_stm32_rng_data.isr),
|
|
|
|
byte);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = rng_pool_put(
|
|
|
|
(struct rng_pool *)(entropy_stm32_rng_data.thr),
|
|
|
|
byte);
|
|
|
|
if (ret < 0) {
|
|
|
|
LL_RNG_DisableIT(entropy_stm32_rng_data.rng);
|
2022-04-07 13:06:12 +02:00
|
|
|
pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
|
2020-08-31 17:46:01 +02:00
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
k_sem_give(&entropy_stm32_rng_data.sem_sync);
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2021-03-22 10:28:25 -04:00
|
|
|
static int entropy_stm32_rng_get_entropy(const struct device *dev,
|
2020-04-30 20:33:38 +02:00
|
|
|
uint8_t *buf,
|
|
|
|
uint16_t len)
|
2020-08-31 17:46:01 +02:00
|
|
|
{
|
|
|
|
/* Check if this API is called on correct driver instance. */
|
2022-01-18 15:37:08 +01:00
|
|
|
__ASSERT_NO_MSG(&entropy_stm32_rng_data == dev->data);
|
2020-08-31 17:46:01 +02:00
|
|
|
|
|
|
|
while (len) {
|
|
|
|
uint16_t bytes;
|
|
|
|
|
|
|
|
k_sem_take(&entropy_stm32_rng_data.sem_lock, K_FOREVER);
|
|
|
|
bytes = rng_pool_get(
|
|
|
|
(struct rng_pool *)(entropy_stm32_rng_data.thr),
|
|
|
|
buf, len);
|
|
|
|
k_sem_give(&entropy_stm32_rng_data.sem_lock);
|
|
|
|
|
|
|
|
if (bytes == 0U) {
|
|
|
|
/* Pool is empty: Sleep until next interrupt. */
|
|
|
|
k_sem_take(&entropy_stm32_rng_data.sem_sync, K_FOREVER);
|
|
|
|
continue;
|
2017-06-30 23:48:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
len -= bytes;
|
|
|
|
buf += bytes;
|
2017-06-30 23:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int entropy_stm32_rng_get_entropy_isr(const struct device *dev,
|
|
|
|
uint8_t *buf,
|
2020-08-31 17:46:01 +02:00
|
|
|
uint16_t len,
|
|
|
|
uint32_t flags)
|
|
|
|
{
|
|
|
|
uint16_t cnt = len;
|
|
|
|
|
|
|
|
/* Check if this API is called on correct driver instance. */
|
2022-01-18 15:37:08 +01:00
|
|
|
__ASSERT_NO_MSG(&entropy_stm32_rng_data == dev->data);
|
2020-08-31 17:46:01 +02:00
|
|
|
|
|
|
|
if (likely((flags & ENTROPY_BUSYWAIT) == 0U)) {
|
|
|
|
return rng_pool_get(
|
|
|
|
(struct rng_pool *)(entropy_stm32_rng_data.isr),
|
|
|
|
buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
unsigned int key;
|
|
|
|
int irq_enabled;
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
irq_enabled = irq_is_enabled(IRQN);
|
|
|
|
irq_disable(IRQN);
|
|
|
|
irq_unlock(key);
|
|
|
|
|
2022-02-22 17:30:42 +01:00
|
|
|
/* do not proceed if a Seed error occurred */
|
|
|
|
if (LL_RNG_IsActiveFlag_SECS(entropy_stm32_rng_data.rng) ||
|
|
|
|
LL_RNG_IsActiveFlag_SEIS(entropy_stm32_rng_data.rng)) {
|
|
|
|
|
|
|
|
(void)random_byte_get(); /* this will recover the error */
|
|
|
|
/* restore irq as we enter */
|
|
|
|
if (irq_enabled) {
|
|
|
|
irq_enable(IRQN);
|
|
|
|
}
|
|
|
|
return 0; /* return cnt is null : no random data available */
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
/* Clear NVIC pending bit. This ensures that a subsequent
|
|
|
|
* RNG event will set the Cortex-M single-bit event register
|
|
|
|
* to 1 (the bit is set when NVIC pending IRQ status is
|
|
|
|
* changed from 0 to 1)
|
|
|
|
*/
|
|
|
|
NVIC_ClearPendingIRQ(IRQN);
|
|
|
|
|
|
|
|
do {
|
|
|
|
int byte;
|
|
|
|
|
|
|
|
while (LL_RNG_IsActiveFlag_DRDY(
|
|
|
|
entropy_stm32_rng_data.rng) != 1) {
|
|
|
|
/*
|
|
|
|
* To guarantee waking up from the event, the
|
|
|
|
* SEV-On-Pend feature must be enabled (enabled
|
|
|
|
* during ARCH initialization).
|
|
|
|
*
|
|
|
|
* DSB is recommended by spec before WFE (to
|
|
|
|
* guarantee completion of memory transactions)
|
|
|
|
*/
|
|
|
|
__DSB();
|
|
|
|
__WFE();
|
|
|
|
__SEV();
|
|
|
|
__WFE();
|
|
|
|
}
|
|
|
|
|
|
|
|
byte = random_byte_get();
|
|
|
|
NVIC_ClearPendingIRQ(IRQN);
|
|
|
|
|
|
|
|
if (byte < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[--len] = byte;
|
|
|
|
} while (len);
|
|
|
|
|
|
|
|
if (irq_enabled) {
|
|
|
|
irq_enable(IRQN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int entropy_stm32_rng_init(const struct device *dev)
|
2017-06-30 23:48:22 +02:00
|
|
|
{
|
2017-10-13 16:30:55 -07:00
|
|
|
struct entropy_stm32_rng_dev_data *dev_data;
|
2020-05-12 07:14:53 -05:00
|
|
|
const struct entropy_stm32_rng_dev_cfg *dev_cfg;
|
2018-12-07 11:09:28 +01:00
|
|
|
int res;
|
2017-06-30 23:48:22 +02:00
|
|
|
|
|
|
|
__ASSERT_NO_MSG(dev != NULL);
|
|
|
|
|
2022-01-18 15:37:08 +01:00
|
|
|
dev_data = dev->data;
|
|
|
|
dev_cfg = dev->config;
|
2017-06-30 23:48:22 +02:00
|
|
|
|
|
|
|
__ASSERT_NO_MSG(dev_data != NULL);
|
|
|
|
__ASSERT_NO_MSG(dev_cfg != NULL);
|
|
|
|
|
2017-12-21 10:47:08 +01: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);
|
2021-07-04 16:03:33 +02:00
|
|
|
#elif CONFIG_SOC_SERIES_STM32WLX || CONFIG_SOC_SERIES_STM32G0X
|
|
|
|
LL_RCC_PLL_EnableDomain_RNG();
|
|
|
|
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_PLL);
|
2020-08-26 10:23:10 +02:00
|
|
|
#elif defined(RCC_CR2_HSI48ON) || defined(RCC_CR_HSI48ON) \
|
|
|
|
|| defined(RCC_CRRCR_HSI48ON)
|
2020-05-08 12:29:03 +01:00
|
|
|
|
|
|
|
#if CONFIG_SOC_SERIES_STM32L0X
|
|
|
|
/* We need SYSCFG to control VREFINT, so make sure it is clocked */
|
|
|
|
if (!LL_APB2_GRP1_IsEnabledClock(LL_APB2_GRP1_PERIPH_SYSCFG)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
/* HSI48 requires VREFINT (see RM0376 section 7.2.4). */
|
|
|
|
LL_SYSCFG_VREFINT_EnableHSI48();
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32L0X */
|
|
|
|
|
2020-07-08 11:18:07 +02:00
|
|
|
z_stm32_hsem_lock(CFG_HW_CLK48_CONFIG_SEMID, HSEM_LOCK_DEFAULT_RETRY);
|
2019-09-04 09:47:46 +01:00
|
|
|
/* Use the HSI48 for the RNG */
|
|
|
|
LL_RCC_HSI48_Enable();
|
|
|
|
while (!LL_RCC_HSI48_IsReady()) {
|
|
|
|
/* Wait for HSI48 to become ready */
|
|
|
|
}
|
|
|
|
|
2021-07-07 17:29:46 +05:30
|
|
|
#if defined(CONFIG_SOC_SERIES_STM32WBX)
|
|
|
|
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_CLK48);
|
|
|
|
LL_RCC_SetCLK48ClockSource(LL_RCC_CLK48_CLKSOURCE_HSI48);
|
2020-07-08 11:18:07 +02:00
|
|
|
|
2021-07-07 17:29:46 +05:30
|
|
|
/* Don't unlock the HSEM to prevent M0 core
|
2020-07-08 11:18:07 +02:00
|
|
|
* to disable HSI48 clock used for RNG.
|
|
|
|
*/
|
2021-07-07 17:29:46 +05:30
|
|
|
#else
|
|
|
|
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_HSI48);
|
|
|
|
|
|
|
|
/* Unlock the HSEM if it is not STM32WB */
|
2020-07-08 11:18:07 +02:00
|
|
|
z_stm32_hsem_unlock(CFG_HW_CLK48_CONFIG_SEMID);
|
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32WBX */
|
|
|
|
|
2017-12-21 10:47:08 +01:00
|
|
|
#endif /* CONFIG_SOC_SERIES_STM32L4X */
|
|
|
|
|
2021-02-11 11:49:24 -06:00
|
|
|
dev_data->clock = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2018-12-07 11:09:28 +01:00
|
|
|
res = clock_control_on(dev_data->clock,
|
2017-06-30 23:48:22 +02:00
|
|
|
(clock_control_subsys_t *)&dev_cfg->pclken);
|
2019-02-20 12:28:53 +01:00
|
|
|
__ASSERT_NO_MSG(res == 0);
|
2017-06-30 23:48:22 +02:00
|
|
|
|
2021-12-14 10:15:10 +01:00
|
|
|
|
2022-02-02 08:49:14 +01:00
|
|
|
#if DT_INST_NODE_HAS_PROP(0, health_test_config)
|
|
|
|
#if DT_INST_NODE_HAS_PROP(0, health_test_magic)
|
2021-12-14 10:15:10 +01:00
|
|
|
/* Write Magic number before writing configuration
|
|
|
|
* Not all stm32 series have a Magic number
|
|
|
|
*/
|
2022-02-02 08:49:14 +01:00
|
|
|
LL_RNG_SetHealthConfig(dev_data->rng, DT_INST_PROP(0, health_test_magic));
|
2021-12-14 10:15:10 +01:00
|
|
|
#endif
|
|
|
|
/* Write RNG HTCR configuration */
|
2022-02-02 08:49:14 +01:00
|
|
|
LL_RNG_SetHealthConfig(dev_data->rng, DT_INST_PROP(0, health_test_config));
|
2021-12-14 10:15:10 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-02 09:37:55 +01:00
|
|
|
/* Prevent the clocks to be stopped during the duration the
|
|
|
|
* rng pool is being populated. The ISR will release the constraint again
|
|
|
|
* when the rng pool is filled.
|
|
|
|
*/
|
2022-04-07 13:06:12 +02:00
|
|
|
pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
|
2022-02-02 09:37:55 +01:00
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
LL_RNG_EnableIT(dev_data->rng);
|
|
|
|
|
2017-06-30 23:48:22 +02:00
|
|
|
LL_RNG_Enable(dev_data->rng);
|
|
|
|
|
2020-08-31 17:46:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Locking semaphore initialized to 1 (unlocked) */
|
|
|
|
k_sem_init(&dev_data->sem_lock, 1, 1);
|
|
|
|
|
|
|
|
/* Synching semaphore */
|
|
|
|
k_sem_init(&dev_data->sem_sync, 0, 1);
|
|
|
|
|
|
|
|
rng_pool_init((struct rng_pool *)(dev_data->thr),
|
|
|
|
CONFIG_ENTROPY_STM32_THR_POOL_SIZE,
|
|
|
|
CONFIG_ENTROPY_STM32_THR_THRESHOLD);
|
|
|
|
rng_pool_init((struct rng_pool *)(dev_data->isr),
|
|
|
|
CONFIG_ENTROPY_STM32_ISR_POOL_SIZE,
|
|
|
|
CONFIG_ENTROPY_STM32_ISR_THRESHOLD);
|
|
|
|
|
|
|
|
IRQ_CONNECT(IRQN, IRQ_PRIO, stm32_rng_isr, &entropy_stm32_rng_data, 0);
|
|
|
|
irq_enable(IRQN);
|
|
|
|
|
2017-06-30 23:48:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-13 16:30:55 -07:00
|
|
|
static const struct entropy_driver_api entropy_stm32_rng_api = {
|
2020-08-31 17:46:01 +02:00
|
|
|
.get_entropy = entropy_stm32_rng_get_entropy,
|
|
|
|
.get_entropy_isr = entropy_stm32_rng_get_entropy_isr
|
2017-06-30 23:48:22 +02:00
|
|
|
};
|
|
|
|
|
2020-12-10 10:51:54 -06:00
|
|
|
DEVICE_DT_INST_DEFINE(0,
|
2021-04-28 10:39:21 +02:00
|
|
|
entropy_stm32_rng_init, NULL,
|
2017-10-13 16:30:55 -07:00
|
|
|
&entropy_stm32_rng_data, &entropy_stm32_rng_config,
|
2021-10-19 23:14:27 +02:00
|
|
|
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
|
2017-10-13 16:30:55 -07:00
|
|
|
&entropy_stm32_rng_api);
|