xref: /openbmc/linux/drivers/char/hw_random/bcm2835-rng.c (revision 0788257aeebee9b8413c1c29e7d2029329b1a82e)
105ba8846SStefan Wahren // SPDX-License-Identifier: GPL-2.0
205ba8846SStefan Wahren /*
38c4196a2SLubomir Rintel  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
48c4196a2SLubomir Rintel  * Copyright (c) 2013 Lubomir Rintel
58c4196a2SLubomir Rintel  */
68c4196a2SLubomir Rintel 
78c4196a2SLubomir Rintel #include <linux/hw_random.h>
88c4196a2SLubomir Rintel #include <linux/io.h>
98c4196a2SLubomir Rintel #include <linux/kernel.h>
108c4196a2SLubomir Rintel #include <linux/module.h>
11*0788257aSRob Herring #include <linux/of.h>
128c4196a2SLubomir Rintel #include <linux/platform_device.h>
138c4196a2SLubomir Rintel #include <linux/printk.h>
14791af4f4SFlorian Fainelli #include <linux/clk.h>
15e5f9f41dSÁlvaro Fernández Rojas #include <linux/reset.h>
168c4196a2SLubomir Rintel 
178c4196a2SLubomir Rintel #define RNG_CTRL	0x0
188c4196a2SLubomir Rintel #define RNG_STATUS	0x4
198c4196a2SLubomir Rintel #define RNG_DATA	0x8
20422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_MASK	0x10
218c4196a2SLubomir Rintel 
228c4196a2SLubomir Rintel /* enable rng */
238c4196a2SLubomir Rintel #define RNG_RBGEN	0x1
248c4196a2SLubomir Rintel 
258c4196a2SLubomir Rintel /* the initial numbers generated are "less random" so will be discarded */
268c4196a2SLubomir Rintel #define RNG_WARMUP_COUNT 0x40000
278c4196a2SLubomir Rintel 
28422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_OFF	0x1
29422a7491SYendapally Reddy Dhananjaya Reddy 
30b788479fSFlorian Fainelli struct bcm2835_rng_priv {
31b788479fSFlorian Fainelli 	struct hwrng rng;
32b788479fSFlorian Fainelli 	void __iomem *base;
3304b154faSFlorian Fainelli 	bool mask_interrupts;
34791af4f4SFlorian Fainelli 	struct clk *clk;
35e5f9f41dSÁlvaro Fernández Rojas 	struct reset_control *reset;
36b788479fSFlorian Fainelli };
37b788479fSFlorian Fainelli 
38b788479fSFlorian Fainelli static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
39b788479fSFlorian Fainelli {
40b788479fSFlorian Fainelli 	return container_of(rng, struct bcm2835_rng_priv, rng);
41b788479fSFlorian Fainelli }
42b788479fSFlorian Fainelli 
43abd42026SFlorian Fainelli static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset)
44abd42026SFlorian Fainelli {
456f09359aSFlorian Fainelli 	/* MIPS chips strapped for BE will automagically configure the
466f09359aSFlorian Fainelli 	 * peripheral registers for CPU-native byte order.
476f09359aSFlorian Fainelli 	 */
486f09359aSFlorian Fainelli 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
496f09359aSFlorian Fainelli 		return __raw_readl(priv->base + offset);
506f09359aSFlorian Fainelli 	else
51abd42026SFlorian Fainelli 		return readl(priv->base + offset);
52abd42026SFlorian Fainelli }
53abd42026SFlorian Fainelli 
54abd42026SFlorian Fainelli static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
55abd42026SFlorian Fainelli 			      u32 offset)
56abd42026SFlorian Fainelli {
576f09359aSFlorian Fainelli 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
586f09359aSFlorian Fainelli 		__raw_writel(val, priv->base + offset);
596f09359aSFlorian Fainelli 	else
60abd42026SFlorian Fainelli 		writel(val, priv->base + offset);
61abd42026SFlorian Fainelli }
62abd42026SFlorian Fainelli 
638c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
648c4196a2SLubomir Rintel 			       bool wait)
658c4196a2SLubomir Rintel {
66b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
674f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 max_words = max / sizeof(u32);
684f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 num_words, count;
698c4196a2SLubomir Rintel 
70abd42026SFlorian Fainelli 	while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
718c4196a2SLubomir Rintel 		if (!wait)
728c4196a2SLubomir Rintel 			return 0;
7396cb9d05SJason A. Donenfeld 		hwrng_msleep(rng, 1000);
748c4196a2SLubomir Rintel 	}
758c4196a2SLubomir Rintel 
76abd42026SFlorian Fainelli 	num_words = rng_readl(priv, RNG_STATUS) >> 24;
774f8de65bSYendapally Reddy Dhananjaya Reddy 	if (num_words > max_words)
784f8de65bSYendapally Reddy Dhananjaya Reddy 		num_words = max_words;
794f8de65bSYendapally Reddy Dhananjaya Reddy 
804f8de65bSYendapally Reddy Dhananjaya Reddy 	for (count = 0; count < num_words; count++)
81abd42026SFlorian Fainelli 		((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
824f8de65bSYendapally Reddy Dhananjaya Reddy 
834f8de65bSYendapally Reddy Dhananjaya Reddy 	return num_words * sizeof(u32);
848c4196a2SLubomir Rintel }
858c4196a2SLubomir Rintel 
86a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng)
87a8157775SFlorian Fainelli {
88a8157775SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
89791af4f4SFlorian Fainelli 	int ret = 0;
9004b154faSFlorian Fainelli 	u32 val;
9104b154faSFlorian Fainelli 
92791af4f4SFlorian Fainelli 	ret = clk_prepare_enable(priv->clk);
93791af4f4SFlorian Fainelli 	if (ret)
94791af4f4SFlorian Fainelli 		return ret;
95791af4f4SFlorian Fainelli 
96e5f9f41dSÁlvaro Fernández Rojas 	ret = reset_control_reset(priv->reset);
97e5f9f41dSÁlvaro Fernández Rojas 	if (ret)
98e5f9f41dSÁlvaro Fernández Rojas 		return ret;
99e5f9f41dSÁlvaro Fernández Rojas 
10004b154faSFlorian Fainelli 	if (priv->mask_interrupts) {
10104b154faSFlorian Fainelli 		/* mask the interrupt */
102abd42026SFlorian Fainelli 		val = rng_readl(priv, RNG_INT_MASK);
10304b154faSFlorian Fainelli 		val |= RNG_INT_OFF;
104abd42026SFlorian Fainelli 		rng_writel(priv, val, RNG_INT_MASK);
10504b154faSFlorian Fainelli 	}
106a8157775SFlorian Fainelli 
107a8157775SFlorian Fainelli 	/* set warm-up count & enable */
108abd42026SFlorian Fainelli 	rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS);
109abd42026SFlorian Fainelli 	rng_writel(priv, RNG_RBGEN, RNG_CTRL);
110a8157775SFlorian Fainelli 
111791af4f4SFlorian Fainelli 	return ret;
112a8157775SFlorian Fainelli }
113a8157775SFlorian Fainelli 
114ec94bca7SFlorian Fainelli static void bcm2835_rng_cleanup(struct hwrng *rng)
115ec94bca7SFlorian Fainelli {
116ec94bca7SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
117ec94bca7SFlorian Fainelli 
118ec94bca7SFlorian Fainelli 	/* disable rng hardware */
119abd42026SFlorian Fainelli 	rng_writel(priv, 0, RNG_CTRL);
120791af4f4SFlorian Fainelli 
121791af4f4SFlorian Fainelli 	clk_disable_unprepare(priv->clk);
122ec94bca7SFlorian Fainelli }
123ec94bca7SFlorian Fainelli 
12404b154faSFlorian Fainelli struct bcm2835_rng_of_data {
12504b154faSFlorian Fainelli 	bool mask_interrupts;
12604b154faSFlorian Fainelli };
12704b154faSFlorian Fainelli 
12804b154faSFlorian Fainelli static const struct bcm2835_rng_of_data nsp_rng_of_data = {
12904b154faSFlorian Fainelli 	.mask_interrupts = true,
13004b154faSFlorian Fainelli };
13104b154faSFlorian Fainelli 
132422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = {
133422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm2835-rng"},
13404b154faSFlorian Fainelli 	{ .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data },
13504b154faSFlorian Fainelli 	{ .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data },
1368705f24fSFlorian Fainelli 	{ .compatible = "brcm,bcm6368-rng"},
137422a7491SYendapally Reddy Dhananjaya Reddy 	{},
138422a7491SYendapally Reddy Dhananjaya Reddy };
139422a7491SYendapally Reddy Dhananjaya Reddy 
1408c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev)
1418c4196a2SLubomir Rintel {
14204b154faSFlorian Fainelli 	const struct bcm2835_rng_of_data *of_data;
1438c4196a2SLubomir Rintel 	struct device *dev = &pdev->dev;
144422a7491SYendapally Reddy Dhananjaya Reddy 	const struct of_device_id *rng_id;
145b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv;
1468c4196a2SLubomir Rintel 	int err;
1478c4196a2SLubomir Rintel 
148b788479fSFlorian Fainelli 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
149b788479fSFlorian Fainelli 	if (!priv)
150b788479fSFlorian Fainelli 		return -ENOMEM;
151b788479fSFlorian Fainelli 
152b788479fSFlorian Fainelli 	platform_set_drvdata(pdev, priv);
153b788479fSFlorian Fainelli 
1548c4196a2SLubomir Rintel 	/* map peripheral */
1553e46bd34SYueHaibing 	priv->base = devm_platform_ioremap_resource(pdev, 0);
156c4fc5d6dSweiyongjun \(A\) 	if (IS_ERR(priv->base))
157b788479fSFlorian Fainelli 		return PTR_ERR(priv->base);
158b788479fSFlorian Fainelli 
159791af4f4SFlorian Fainelli 	/* Clock is optional on most platforms */
160e06da499STian Tao 	priv->clk = devm_clk_get_optional(dev, NULL);
161e06da499STian Tao 	if (IS_ERR(priv->clk))
162e06da499STian Tao 		return PTR_ERR(priv->clk);
163791af4f4SFlorian Fainelli 
164e5f9f41dSÁlvaro Fernández Rojas 	priv->reset = devm_reset_control_get_optional_exclusive(dev, NULL);
165e5f9f41dSÁlvaro Fernández Rojas 	if (IS_ERR(priv->reset))
166e5f9f41dSÁlvaro Fernández Rojas 		return PTR_ERR(priv->reset);
167e5f9f41dSÁlvaro Fernández Rojas 
1688705f24fSFlorian Fainelli 	priv->rng.name = pdev->name;
169a8157775SFlorian Fainelli 	priv->rng.init = bcm2835_rng_init;
170b788479fSFlorian Fainelli 	priv->rng.read = bcm2835_rng_read;
171ec94bca7SFlorian Fainelli 	priv->rng.cleanup = bcm2835_rng_cleanup;
1728c4196a2SLubomir Rintel 
173984798deSJonas Gorski 	if (dev_of_node(dev)) {
17464f8153aSHerbert Xu 		rng_id = of_match_node(bcm2835_rng_of_match, dev->of_node);
17521bb0ef4SFlorian Fainelli 		if (!rng_id)
176422a7491SYendapally Reddy Dhananjaya Reddy 			return -EINVAL;
17721bb0ef4SFlorian Fainelli 
178422a7491SYendapally Reddy Dhananjaya Reddy 		/* Check for rng init function, execute it */
17904b154faSFlorian Fainelli 		of_data = rng_id->data;
18004b154faSFlorian Fainelli 		if (of_data)
18104b154faSFlorian Fainelli 			priv->mask_interrupts = of_data->mask_interrupts;
182984798deSJonas Gorski 	}
183422a7491SYendapally Reddy Dhananjaya Reddy 
1848c4196a2SLubomir Rintel 	/* register driver */
18516a4c04bSFlorian Fainelli 	err = devm_hwrng_register(dev, &priv->rng);
18621bb0ef4SFlorian Fainelli 	if (err)
1878c4196a2SLubomir Rintel 		dev_err(dev, "hwrng registration failed\n");
18821bb0ef4SFlorian Fainelli 	else
1898c4196a2SLubomir Rintel 		dev_info(dev, "hwrng registered\n");
1908c4196a2SLubomir Rintel 
1918c4196a2SLubomir Rintel 	return err;
1928c4196a2SLubomir Rintel }
1938c4196a2SLubomir Rintel 
1948c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
1958c4196a2SLubomir Rintel 
196d4f6d923SRikard Falkeborn static const struct platform_device_id bcm2835_rng_devtype[] = {
1978705f24fSFlorian Fainelli 	{ .name = "bcm2835-rng" },
1988705f24fSFlorian Fainelli 	{ .name = "bcm63xx-rng" },
1998705f24fSFlorian Fainelli 	{ /* sentinel */ }
2008705f24fSFlorian Fainelli };
2018705f24fSFlorian Fainelli MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype);
2028705f24fSFlorian Fainelli 
2038c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = {
2048c4196a2SLubomir Rintel 	.driver = {
2058c4196a2SLubomir Rintel 		.name = "bcm2835-rng",
2068c4196a2SLubomir Rintel 		.of_match_table = bcm2835_rng_of_match,
2078c4196a2SLubomir Rintel 	},
2088c4196a2SLubomir Rintel 	.probe		= bcm2835_rng_probe,
2098705f24fSFlorian Fainelli 	.id_table	= bcm2835_rng_devtype,
2108c4196a2SLubomir Rintel };
2118c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver);
2128c4196a2SLubomir Rintel 
2138c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
2148c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
2158c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2");
216