Skip to content

Commit 35b2237

Browse files
Markus Mayerherbertx
authored andcommitted
hwrng: bcm74110 - Add Broadcom BCM74110 RNG driver
Add a driver for the random number generator present on the Broadcom BCM74110 SoC. Signed-off-by: Markus Mayer <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent c0559d2 commit 35b2237

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

drivers/char/hw_random/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ config HW_RANDOM_BCM2835
112112

113113
If unsure, say Y.
114114

115+
config HW_RANDOM_BCM74110
116+
tristate "Broadcom BCM74110 Random Number Generator support"
117+
depends on ARCH_BRCMSTB || COMPILE_TEST
118+
default HW_RANDOM
119+
help
120+
This driver provides kernel-side support for the Random Number
121+
Generator hardware found on the Broadcom BCM74110 SoCs.
122+
123+
To compile this driver as a module, choose M here: the
124+
module will be called bcm74110-rng
125+
126+
If unsure, say Y.
127+
115128
config HW_RANDOM_IPROC_RNG200
116129
tristate "Broadcom iProc/STB RNG200 support"
117130
depends on ARCH_BCM_IPROC || ARCH_BCM2835 || ARCH_BCMBCA || ARCH_BRCMSTB || COMPILE_TEST

drivers/char/hw_random/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
3232
obj-$(CONFIG_HW_RANDOM_HISI) += hisi-rng.o
3333
obj-$(CONFIG_HW_RANDOM_HISTB) += histb-rng.o
3434
obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
35+
obj-$(CONFIG_HW_RANDOM_BCM74110) += bcm74110-rng.o
3536
obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
3637
obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
3738
obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o

drivers/char/hw_random/bcm74110-rng.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (c) 2024 Broadcom
4+
*/
5+
6+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7+
8+
#include <linux/module.h>
9+
#include <linux/mod_devicetable.h>
10+
#include <linux/kernel.h>
11+
#include <linux/io.h>
12+
#include <linux/delay.h>
13+
#include <linux/platform_device.h>
14+
#include <linux/random.h>
15+
#include <linux/hw_random.h>
16+
17+
#define HOST_REV_ID 0x00
18+
#define HOST_FIFO_DEPTH 0x04
19+
#define HOST_FIFO_COUNT 0x08
20+
#define HOST_FIFO_THRESHOLD 0x0c
21+
#define HOST_FIFO_DATA 0x10
22+
23+
#define HOST_FIFO_COUNT_MASK 0xffff
24+
25+
/* Delay range in microseconds */
26+
#define FIFO_DELAY_MIN_US 3
27+
#define FIFO_DELAY_MAX_US 7
28+
#define FIFO_DELAY_MAX_COUNT 10
29+
30+
struct bcm74110_priv {
31+
void __iomem *base;
32+
};
33+
34+
static inline int bcm74110_rng_fifo_count(void __iomem *mem)
35+
{
36+
return readl_relaxed(mem) & HOST_FIFO_COUNT_MASK;
37+
}
38+
39+
static int bcm74110_rng_read(struct hwrng *rng, void *buf, size_t max,
40+
bool wait)
41+
{
42+
struct bcm74110_priv *priv = (struct bcm74110_priv *)rng->priv;
43+
void __iomem *fc_addr = priv->base + HOST_FIFO_COUNT;
44+
void __iomem *fd_addr = priv->base + HOST_FIFO_DATA;
45+
unsigned underrun_count = 0;
46+
u32 max_words = max / sizeof(u32);
47+
u32 num_words;
48+
unsigned i;
49+
50+
/*
51+
* We need to check how many words are available in the RNG FIFO. If
52+
* there aren't any, we need to wait for some to become available.
53+
*/
54+
while ((num_words = bcm74110_rng_fifo_count(fc_addr)) == 0) {
55+
if (!wait)
56+
return 0;
57+
/*
58+
* As a precaution, limit how long we wait. If the FIFO doesn't
59+
* refill within the allotted time, return 0 (=no data) to the
60+
* caller.
61+
*/
62+
if (likely(underrun_count < FIFO_DELAY_MAX_COUNT))
63+
usleep_range(FIFO_DELAY_MIN_US, FIFO_DELAY_MAX_US);
64+
else
65+
return 0;
66+
underrun_count++;
67+
}
68+
if (num_words > max_words)
69+
num_words = max_words;
70+
71+
/* Bail early if we run out of random numbers unexpectedly */
72+
for (i = 0; i < num_words && bcm74110_rng_fifo_count(fc_addr) > 0; i++)
73+
((u32 *)buf)[i] = readl_relaxed(fd_addr);
74+
75+
return i * sizeof(u32);
76+
}
77+
78+
static struct hwrng bcm74110_hwrng = {
79+
.read = bcm74110_rng_read,
80+
};
81+
82+
static int bcm74110_rng_probe(struct platform_device *pdev)
83+
{
84+
struct device *dev = &pdev->dev;
85+
struct bcm74110_priv *priv;
86+
int rc;
87+
88+
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
89+
if (!priv)
90+
return -ENOMEM;
91+
92+
bcm74110_hwrng.name = pdev->name;
93+
bcm74110_hwrng.priv = (unsigned long)priv;
94+
95+
priv->base = devm_platform_ioremap_resource(pdev, 0);
96+
if (IS_ERR(priv->base))
97+
return PTR_ERR(priv->base);
98+
99+
rc = devm_hwrng_register(dev, &bcm74110_hwrng);
100+
if (rc)
101+
dev_err(dev, "hwrng registration failed (%d)\n", rc);
102+
else
103+
dev_info(dev, "hwrng registered\n");
104+
105+
return rc;
106+
}
107+
108+
static const struct of_device_id bcm74110_rng_match[] = {
109+
{ .compatible = "brcm,bcm74110-rng", },
110+
{},
111+
};
112+
MODULE_DEVICE_TABLE(of, bcm74110_rng_match);
113+
114+
static struct platform_driver bcm74110_rng_driver = {
115+
.driver = {
116+
.name = KBUILD_MODNAME,
117+
.of_match_table = bcm74110_rng_match,
118+
},
119+
.probe = bcm74110_rng_probe,
120+
};
121+
module_platform_driver(bcm74110_rng_driver);
122+
123+
MODULE_AUTHOR("Markus Mayer <[email protected]>");
124+
MODULE_DESCRIPTION("BCM 74110 Random Number Generator (RNG) driver");
125+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)