2019-12-28 18:28:29 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, Linaro Limited
|
|
|
|
*/
|
|
|
|
|
2020-09-17 14:49:02 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_RNG
|
|
|
|
|
2019-12-28 18:28:29 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <clk.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2019-12-28 18:28:29 +00:00
|
|
|
#include <reset.h>
|
|
|
|
#include <rng.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2019-12-28 18:28:29 +00:00
|
|
|
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <linux/iopoll.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
#define RNG_CR 0x00
|
|
|
|
#define RNG_CR_RNGEN BIT(2)
|
|
|
|
#define RNG_CR_CED BIT(5)
|
|
|
|
#define RNG_CR_CONDRST BIT(30)
|
2019-12-28 18:28:29 +00:00
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
#define RNG_SR 0x04
|
|
|
|
#define RNG_SR_SEIS BIT(6)
|
|
|
|
#define RNG_SR_CEIS BIT(5)
|
|
|
|
#define RNG_SR_SECS BIT(2)
|
|
|
|
#define RNG_SR_DRDY BIT(0)
|
2019-12-28 18:28:29 +00:00
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
#define RNG_DR 0x08
|
|
|
|
|
|
|
|
struct stm32_rng_data {
|
|
|
|
bool has_cond_reset;
|
|
|
|
};
|
2019-12-28 18:28:29 +00:00
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32_rng_plat {
|
2019-12-28 18:28:29 +00:00
|
|
|
fdt_addr_t base;
|
|
|
|
struct clk clk;
|
|
|
|
struct reset_ctl rst;
|
2022-06-30 08:20:15 +00:00
|
|
|
const struct stm32_rng_data *data;
|
2019-12-28 18:28:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32_rng_read(struct udevice *dev, void *data, size_t len)
|
|
|
|
{
|
2020-02-16 09:11:18 +00:00
|
|
|
int retval, i;
|
2019-12-28 18:28:29 +00:00
|
|
|
u32 sr, count, reg;
|
|
|
|
size_t increment;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32_rng_plat *pdata = dev_get_plat(dev);
|
2019-12-28 18:28:29 +00:00
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
retval = readl_poll_timeout(pdata->base + RNG_SR, sr,
|
|
|
|
sr & RNG_SR_DRDY, 10000);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (sr & (RNG_SR_SEIS | RNG_SR_SECS)) {
|
|
|
|
/* As per SoC TRM */
|
|
|
|
clrbits_le32(pdata->base + RNG_SR, RNG_SR_SEIS);
|
|
|
|
for (i = 0; i < 12; i++)
|
|
|
|
readl(pdata->base + RNG_DR);
|
|
|
|
if (readl(pdata->base + RNG_SR) & RNG_SR_SEIS) {
|
2020-09-17 14:49:02 +00:00
|
|
|
log_err("RNG Noise");
|
2019-12-28 18:28:29 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
/* start again */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Once the DRDY bit is set, the RNG_DR register can
|
|
|
|
* be read four consecutive times.
|
|
|
|
*/
|
|
|
|
count = 4;
|
|
|
|
while (len && count) {
|
|
|
|
reg = readl(pdata->base + RNG_DR);
|
|
|
|
memcpy(data, ®, min(len, sizeof(u32)));
|
|
|
|
increment = min(len, sizeof(u32));
|
|
|
|
data += increment;
|
|
|
|
len -= increment;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static int stm32_rng_init(struct stm32_rng_plat *pdata)
|
2019-12-28 18:28:29 +00:00
|
|
|
{
|
|
|
|
int err;
|
2022-06-30 08:20:15 +00:00
|
|
|
u32 cr, sr;
|
2019-12-28 18:28:29 +00:00
|
|
|
|
|
|
|
err = clk_enable(&pdata->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
cr = readl(pdata->base + RNG_CR);
|
|
|
|
|
2019-12-28 18:28:29 +00:00
|
|
|
/* Disable CED */
|
2022-06-30 08:20:15 +00:00
|
|
|
cr |= RNG_CR_CED;
|
|
|
|
if (pdata->data->has_cond_reset) {
|
|
|
|
cr |= RNG_CR_CONDRST;
|
|
|
|
writel(cr, pdata->base + RNG_CR);
|
|
|
|
cr &= ~RNG_CR_CONDRST;
|
|
|
|
writel(cr, pdata->base + RNG_CR);
|
|
|
|
err = readl_poll_timeout(pdata->base + RNG_CR, cr,
|
|
|
|
(!(cr & RNG_CR_CONDRST)), 10000);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2019-12-28 18:28:29 +00:00
|
|
|
|
|
|
|
/* clear error indicators */
|
|
|
|
writel(0, pdata->base + RNG_SR);
|
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
cr |= RNG_CR_RNGEN;
|
|
|
|
writel(cr, pdata->base + RNG_CR);
|
|
|
|
|
|
|
|
err = readl_poll_timeout(pdata->base + RNG_SR, sr,
|
|
|
|
sr & RNG_SR_DRDY, 10000);
|
|
|
|
return err;
|
2019-12-28 18:28:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static int stm32_rng_cleanup(struct stm32_rng_plat *pdata)
|
2019-12-28 18:28:29 +00:00
|
|
|
{
|
|
|
|
writel(0, pdata->base + RNG_CR);
|
|
|
|
|
|
|
|
return clk_disable(&pdata->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_rng_probe(struct udevice *dev)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32_rng_plat *pdata = dev_get_plat(dev);
|
2019-12-28 18:28:29 +00:00
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
pdata->data = (struct stm32_rng_data *)dev_get_driver_data(dev);
|
|
|
|
|
2019-12-28 18:28:29 +00:00
|
|
|
reset_assert(&pdata->rst);
|
|
|
|
udelay(20);
|
|
|
|
reset_deassert(&pdata->rst);
|
|
|
|
|
|
|
|
return stm32_rng_init(pdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_rng_remove(struct udevice *dev)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32_rng_plat *pdata = dev_get_plat(dev);
|
2019-12-28 18:28:29 +00:00
|
|
|
|
|
|
|
return stm32_rng_cleanup(pdata);
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int stm32_rng_of_to_plat(struct udevice *dev)
|
2019-12-28 18:28:29 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct stm32_rng_plat *pdata = dev_get_plat(dev);
|
2019-12-28 18:28:29 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
pdata->base = dev_read_addr(dev);
|
|
|
|
if (!pdata->base)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = clk_get_by_index(dev, 0, &pdata->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = reset_get_by_index(dev, 0, &pdata->rst);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_rng_ops stm32_rng_ops = {
|
|
|
|
.read = stm32_rng_read,
|
|
|
|
};
|
|
|
|
|
2022-06-30 08:20:15 +00:00
|
|
|
static const struct stm32_rng_data stm32mp13_rng_data = {
|
|
|
|
.has_cond_reset = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct stm32_rng_data stm32_rng_data = {
|
|
|
|
.has_cond_reset = false,
|
|
|
|
};
|
|
|
|
|
2019-12-28 18:28:29 +00:00
|
|
|
static const struct udevice_id stm32_rng_match[] = {
|
2022-06-30 08:20:15 +00:00
|
|
|
{.compatible = "st,stm32mp13-rng", .data = (ulong)&stm32mp13_rng_data},
|
|
|
|
{.compatible = "st,stm32-rng", .data = (ulong)&stm32_rng_data},
|
2019-12-28 18:28:29 +00:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(stm32_rng) = {
|
|
|
|
.name = "stm32-rng",
|
|
|
|
.id = UCLASS_RNG,
|
|
|
|
.of_match = stm32_rng_match,
|
|
|
|
.ops = &stm32_rng_ops,
|
|
|
|
.probe = stm32_rng_probe,
|
|
|
|
.remove = stm32_rng_remove,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct stm32_rng_plat),
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = stm32_rng_of_to_plat,
|
2019-12-28 18:28:29 +00:00
|
|
|
};
|