aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/hw_random
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/hw_random')
-rw-r--r--drivers/char/hw_random/Kconfig2
-rw-r--r--drivers/char/hw_random/bcm2835-rng.c26
-rw-r--r--drivers/char/hw_random/iproc-rng200.c79
3 files changed, 95 insertions, 12 deletions
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 492a2a61a65b..dbc993fb9e01 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -131,7 +131,7 @@ config HW_RANDOM_IPROC_RNG200
default HW_RANDOM
help
This driver provides kernel-side support for the RNG200
- hardware found on the Broadcom iProc and STB SoCs.
+ hardware found on the Broadcom iProc, BCM2711 and STB SoCs.
To compile this driver as a module, choose M here: the
module will be called iproc-rng200
diff --git a/drivers/char/hw_random/bcm2835-rng.c b/drivers/char/hw_random/bcm2835-rng.c
index aa2b135e3ee2..05a4569ce28f 100644
--- a/drivers/char/hw_random/bcm2835-rng.c
+++ b/drivers/char/hw_random/bcm2835-rng.c
@@ -13,6 +13,7 @@
#include <linux/printk.h>
#include <linux/clk.h>
#include <linux/reset.h>
+#include <linux/delay.h>
#define RNG_CTRL 0x0
#define RNG_STATUS 0x4
@@ -27,6 +28,9 @@
#define RNG_INT_OFF 0x1
+#define RNG_FIFO_WORDS 4
+#define RNG_US_PER_WORD 34 /* Tuned for throughput */
+
struct bcm2835_rng_priv {
struct hwrng rng;
void __iomem *base;
@@ -63,19 +67,23 @@ static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
bool wait)
{
+ u32 retries = 1000000/(RNG_FIFO_WORDS * RNG_US_PER_WORD);
struct bcm2835_rng_priv *priv = to_rng_priv(rng);
u32 max_words = max / sizeof(u32);
u32 num_words, count;
- while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
- if (!wait)
+ num_words = rng_readl(priv, RNG_STATUS) >> 24;
+
+ while (!num_words) {
+ if (!wait || !retries)
return 0;
- hwrng_yield(rng);
+ retries--;
+ usleep_range((u32)RNG_US_PER_WORD,
+ (u32)RNG_US_PER_WORD * RNG_FIFO_WORDS);
+ num_words = rng_readl(priv, RNG_STATUS) >> 24;
}
- num_words = rng_readl(priv, RNG_STATUS) >> 24;
- if (num_words > max_words)
- num_words = max_words;
+ num_words = min(num_words, max_words);
for (count = 0; count < num_words; count++)
((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
@@ -107,8 +115,10 @@ static int bcm2835_rng_init(struct hwrng *rng)
}
/* set warm-up count & enable */
- rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS);
- rng_writel(priv, RNG_RBGEN, RNG_CTRL);
+ if (!(rng_readl(priv, RNG_CTRL) & RNG_RBGEN)) {
+ rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS);
+ rng_writel(priv, RNG_RBGEN, RNG_CTRL);
+ }
return ret;
}
diff --git a/drivers/char/hw_random/iproc-rng200.c b/drivers/char/hw_random/iproc-rng200.c
index 440fe28bddc0..33bc28f429f6 100644
--- a/drivers/char/hw_random/iproc-rng200.c
+++ b/drivers/char/hw_random/iproc-rng200.c
@@ -13,6 +13,7 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
+#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
@@ -20,6 +21,7 @@
#define RNG_CTRL_OFFSET 0x00
#define RNG_CTRL_RNG_RBGEN_MASK 0x00001FFF
#define RNG_CTRL_RNG_RBGEN_ENABLE 0x00000001
+#define RNG_CTRL_RNG_DIV_CTRL_SHIFT 13
#define RNG_SOFT_RESET_OFFSET 0x04
#define RNG_SOFT_RESET 0x00000001
@@ -27,16 +29,23 @@
#define RBG_SOFT_RESET_OFFSET 0x08
#define RBG_SOFT_RESET 0x00000001
+#define RNG_TOTAL_BIT_COUNT_OFFSET 0x0C
+
+#define RNG_TOTAL_BIT_COUNT_THRESHOLD_OFFSET 0x10
+
#define RNG_INT_STATUS_OFFSET 0x18
#define RNG_INT_STATUS_MASTER_FAIL_LOCKOUT_IRQ_MASK 0x80000000
#define RNG_INT_STATUS_STARTUP_TRANSITIONS_MET_IRQ_MASK 0x00020000
#define RNG_INT_STATUS_NIST_FAIL_IRQ_MASK 0x00000020
#define RNG_INT_STATUS_TOTAL_BITS_COUNT_IRQ_MASK 0x00000001
+#define RNG_INT_ENABLE_OFFSET 0x1C
+
#define RNG_FIFO_DATA_OFFSET 0x20
#define RNG_FIFO_COUNT_OFFSET 0x24
#define RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK 0x000000FF
+#define RNG_FIFO_COUNT_RNG_FIFO_THRESHOLD_SHIFT 8
struct iproc_rng200_dev {
struct hwrng rng;
@@ -157,6 +166,64 @@ static int iproc_rng200_init(struct hwrng *rng)
return 0;
}
+static int bcm2711_rng200_read(struct hwrng *rng, void *buf, size_t max,
+ bool wait)
+{
+ struct iproc_rng200_dev *priv = to_rng_priv(rng);
+ u32 max_words = max / sizeof(u32);
+ u32 num_words, count, val;
+
+ /* ensure warm up period has elapsed */
+ while (1) {
+ val = ioread32(priv->base + RNG_TOTAL_BIT_COUNT_OFFSET);
+ if (val > 16)
+ break;
+ cpu_relax();
+ }
+
+ /* ensure fifo is not empty */
+ while (1) {
+ num_words = ioread32(priv->base + RNG_FIFO_COUNT_OFFSET) &
+ RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK;
+ if (num_words)
+ break;
+ if (!wait)
+ return 0;
+ cpu_relax();
+ }
+
+ if (num_words > max_words)
+ num_words = max_words;
+
+ for (count = 0; count < num_words; count++) {
+ ((u32 *)buf)[count] = ioread32(priv->base +
+ RNG_FIFO_DATA_OFFSET);
+ }
+
+ return num_words * sizeof(u32);
+}
+
+static int bcm2711_rng200_init(struct hwrng *rng)
+{
+ struct iproc_rng200_dev *priv = to_rng_priv(rng);
+ uint32_t val;
+
+ if (ioread32(priv->base + RNG_CTRL_OFFSET) & RNG_CTRL_RNG_RBGEN_MASK)
+ return 0;
+
+ /* initial numbers generated are "less random" so will be discarded */
+ val = 0x40000;
+ iowrite32(val, priv->base + RNG_TOTAL_BIT_COUNT_THRESHOLD_OFFSET);
+ /* min fifo count to generate full interrupt */
+ val = 2 << RNG_FIFO_COUNT_RNG_FIFO_THRESHOLD_SHIFT;
+ iowrite32(val, priv->base + RNG_FIFO_COUNT_OFFSET);
+ /* enable the rng - 1Mhz sample rate */
+ val = (0x3 << RNG_CTRL_RNG_DIV_CTRL_SHIFT) | RNG_CTRL_RNG_RBGEN_MASK;
+ iowrite32(val, priv->base + RNG_CTRL_OFFSET);
+
+ return 0;
+}
+
static void iproc_rng200_cleanup(struct hwrng *rng)
{
struct iproc_rng200_dev *priv = to_rng_priv(rng);
@@ -183,11 +250,17 @@ static int iproc_rng200_probe(struct platform_device *pdev)
dev_set_drvdata(dev, priv);
- priv->rng.name = "iproc-rng200";
- priv->rng.read = iproc_rng200_read;
- priv->rng.init = iproc_rng200_init;
+ priv->rng.name = pdev->name;
priv->rng.cleanup = iproc_rng200_cleanup;
+ if (of_device_is_compatible(dev->of_node, "brcm,bcm2711-rng200")) {
+ priv->rng.init = bcm2711_rng200_init;
+ priv->rng.read = bcm2711_rng200_read;
+ } else {
+ priv->rng.init = iproc_rng200_init;
+ priv->rng.read = iproc_rng200_read;
+ }
+
/* Register driver */
ret = devm_hwrng_register(dev, &priv->rng);
if (ret) {