aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/hw_random/bcm2835-rng.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/hw_random/bcm2835-rng.c')
-rw-r--r--drivers/char/hw_random/bcm2835-rng.c26
1 files changed, 18 insertions, 8 deletions
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;
}