1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4 * Copyright (c) 2013 Lubomir Rintel
7 #include <linux/hw_random.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/printk.h>
15 #include <linux/clk.h>
18 #define RNG_STATUS 0x4
20 #define RNG_INT_MASK 0x10
25 /* the initial numbers generated are "less random" so will be discarded */
26 #define RNG_WARMUP_COUNT 0x40000
28 #define RNG_INT_OFF 0x1
30 struct bcm2835_rng_priv
{
37 static inline struct bcm2835_rng_priv
*to_rng_priv(struct hwrng
*rng
)
39 return container_of(rng
, struct bcm2835_rng_priv
, rng
);
42 static inline u32
rng_readl(struct bcm2835_rng_priv
*priv
, u32 offset
)
44 /* MIPS chips strapped for BE will automagically configure the
45 * peripheral registers for CPU-native byte order.
47 if (IS_ENABLED(CONFIG_MIPS
) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
48 return __raw_readl(priv
->base
+ offset
);
50 return readl(priv
->base
+ offset
);
53 static inline void rng_writel(struct bcm2835_rng_priv
*priv
, u32 val
,
56 if (IS_ENABLED(CONFIG_MIPS
) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
57 __raw_writel(val
, priv
->base
+ offset
);
59 writel(val
, priv
->base
+ offset
);
62 static int bcm2835_rng_read(struct hwrng
*rng
, void *buf
, size_t max
,
65 struct bcm2835_rng_priv
*priv
= to_rng_priv(rng
);
66 u32 max_words
= max
/ sizeof(u32
);
69 while ((rng_readl(priv
, RNG_STATUS
) >> 24) == 0) {
75 num_words
= rng_readl(priv
, RNG_STATUS
) >> 24;
76 if (num_words
> max_words
)
77 num_words
= max_words
;
79 for (count
= 0; count
< num_words
; count
++)
80 ((u32
*)buf
)[count
] = rng_readl(priv
, RNG_DATA
);
82 return num_words
* sizeof(u32
);
85 static int bcm2835_rng_init(struct hwrng
*rng
)
87 struct bcm2835_rng_priv
*priv
= to_rng_priv(rng
);
91 if (!IS_ERR(priv
->clk
)) {
92 ret
= clk_prepare_enable(priv
->clk
);
97 if (priv
->mask_interrupts
) {
98 /* mask the interrupt */
99 val
= rng_readl(priv
, RNG_INT_MASK
);
101 rng_writel(priv
, val
, RNG_INT_MASK
);
104 /* set warm-up count & enable */
105 rng_writel(priv
, RNG_WARMUP_COUNT
, RNG_STATUS
);
106 rng_writel(priv
, RNG_RBGEN
, RNG_CTRL
);
111 static void bcm2835_rng_cleanup(struct hwrng
*rng
)
113 struct bcm2835_rng_priv
*priv
= to_rng_priv(rng
);
115 /* disable rng hardware */
116 rng_writel(priv
, 0, RNG_CTRL
);
118 if (!IS_ERR(priv
->clk
))
119 clk_disable_unprepare(priv
->clk
);
122 struct bcm2835_rng_of_data
{
123 bool mask_interrupts
;
126 static const struct bcm2835_rng_of_data nsp_rng_of_data
= {
127 .mask_interrupts
= true,
130 static const struct of_device_id bcm2835_rng_of_match
[] = {
131 { .compatible
= "brcm,bcm2835-rng"},
132 { .compatible
= "brcm,bcm-nsp-rng", .data
= &nsp_rng_of_data
},
133 { .compatible
= "brcm,bcm5301x-rng", .data
= &nsp_rng_of_data
},
134 { .compatible
= "brcm,bcm6368-rng"},
138 static int bcm2835_rng_probe(struct platform_device
*pdev
)
140 const struct bcm2835_rng_of_data
*of_data
;
141 struct device
*dev
= &pdev
->dev
;
142 struct device_node
*np
= dev
->of_node
;
143 const struct of_device_id
*rng_id
;
144 struct bcm2835_rng_priv
*priv
;
148 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
152 platform_set_drvdata(pdev
, priv
);
154 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
157 priv
->base
= devm_ioremap_resource(dev
, r
);
158 if (IS_ERR(priv
->base
))
159 return PTR_ERR(priv
->base
);
161 /* Clock is optional on most platforms */
162 priv
->clk
= devm_clk_get(dev
, NULL
);
163 if (IS_ERR(priv
->clk
) && PTR_ERR(priv
->clk
) == -EPROBE_DEFER
)
164 return -EPROBE_DEFER
;
166 priv
->rng
.name
= pdev
->name
;
167 priv
->rng
.init
= bcm2835_rng_init
;
168 priv
->rng
.read
= bcm2835_rng_read
;
169 priv
->rng
.cleanup
= bcm2835_rng_cleanup
;
171 if (dev_of_node(dev
)) {
172 rng_id
= of_match_node(bcm2835_rng_of_match
, np
);
176 /* Check for rng init function, execute it */
177 of_data
= rng_id
->data
;
179 priv
->mask_interrupts
= of_data
->mask_interrupts
;
182 /* register driver */
183 err
= devm_hwrng_register(dev
, &priv
->rng
);
185 dev_err(dev
, "hwrng registration failed\n");
187 dev_info(dev
, "hwrng registered\n");
192 MODULE_DEVICE_TABLE(of
, bcm2835_rng_of_match
);
194 static struct platform_device_id bcm2835_rng_devtype
[] = {
195 { .name
= "bcm2835-rng" },
196 { .name
= "bcm63xx-rng" },
199 MODULE_DEVICE_TABLE(platform
, bcm2835_rng_devtype
);
201 static struct platform_driver bcm2835_rng_driver
= {
203 .name
= "bcm2835-rng",
204 .of_match_table
= bcm2835_rng_of_match
,
206 .probe
= bcm2835_rng_probe
,
207 .id_table
= bcm2835_rng_devtype
,
209 module_platform_driver(bcm2835_rng_driver
);
211 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
212 MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
213 MODULE_LICENSE("GPL v2");