xref: /openbmc/linux/drivers/char/hw_random/bcm2835-rng.c (revision e5f9f41d5e62004c913bfd4ddf06abe032f5ce1c)
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>
118c4196a2SLubomir Rintel #include <linux/of_address.h>
128c4196a2SLubomir Rintel #include <linux/of_platform.h>
138c4196a2SLubomir Rintel #include <linux/platform_device.h>
148c4196a2SLubomir Rintel #include <linux/printk.h>
15791af4f4SFlorian Fainelli #include <linux/clk.h>
16*e5f9f41dSÁlvaro Fernández Rojas #include <linux/reset.h>
178c4196a2SLubomir Rintel 
188c4196a2SLubomir Rintel #define RNG_CTRL	0x0
198c4196a2SLubomir Rintel #define RNG_STATUS	0x4
208c4196a2SLubomir Rintel #define RNG_DATA	0x8
21422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_MASK	0x10
228c4196a2SLubomir Rintel 
238c4196a2SLubomir Rintel /* enable rng */
248c4196a2SLubomir Rintel #define RNG_RBGEN	0x1
258c4196a2SLubomir Rintel 
268c4196a2SLubomir Rintel /* the initial numbers generated are "less random" so will be discarded */
278c4196a2SLubomir Rintel #define RNG_WARMUP_COUNT 0x40000
288c4196a2SLubomir Rintel 
29422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_OFF	0x1
30422a7491SYendapally Reddy Dhananjaya Reddy 
31b788479fSFlorian Fainelli struct bcm2835_rng_priv {
32b788479fSFlorian Fainelli 	struct hwrng rng;
33b788479fSFlorian Fainelli 	void __iomem *base;
3404b154faSFlorian Fainelli 	bool mask_interrupts;
35791af4f4SFlorian Fainelli 	struct clk *clk;
36*e5f9f41dSÁlvaro Fernández Rojas 	struct reset_control *reset;
37b788479fSFlorian Fainelli };
38b788479fSFlorian Fainelli 
39b788479fSFlorian Fainelli static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
40b788479fSFlorian Fainelli {
41b788479fSFlorian Fainelli 	return container_of(rng, struct bcm2835_rng_priv, rng);
42b788479fSFlorian Fainelli }
43b788479fSFlorian Fainelli 
44abd42026SFlorian Fainelli static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset)
45abd42026SFlorian Fainelli {
466f09359aSFlorian Fainelli 	/* MIPS chips strapped for BE will automagically configure the
476f09359aSFlorian Fainelli 	 * peripheral registers for CPU-native byte order.
486f09359aSFlorian Fainelli 	 */
496f09359aSFlorian Fainelli 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
506f09359aSFlorian Fainelli 		return __raw_readl(priv->base + offset);
516f09359aSFlorian Fainelli 	else
52abd42026SFlorian Fainelli 		return readl(priv->base + offset);
53abd42026SFlorian Fainelli }
54abd42026SFlorian Fainelli 
55abd42026SFlorian Fainelli static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
56abd42026SFlorian Fainelli 			      u32 offset)
57abd42026SFlorian Fainelli {
586f09359aSFlorian Fainelli 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
596f09359aSFlorian Fainelli 		__raw_writel(val, priv->base + offset);
606f09359aSFlorian Fainelli 	else
61abd42026SFlorian Fainelli 		writel(val, priv->base + offset);
62abd42026SFlorian Fainelli }
63abd42026SFlorian Fainelli 
648c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
658c4196a2SLubomir Rintel 			       bool wait)
668c4196a2SLubomir Rintel {
67b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
684f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 max_words = max / sizeof(u32);
694f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 num_words, count;
708c4196a2SLubomir Rintel 
71abd42026SFlorian Fainelli 	while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
728c4196a2SLubomir Rintel 		if (!wait)
738c4196a2SLubomir Rintel 			return 0;
748c4196a2SLubomir Rintel 		cpu_relax();
758c4196a2SLubomir Rintel 	}
768c4196a2SLubomir Rintel 
77abd42026SFlorian Fainelli 	num_words = rng_readl(priv, RNG_STATUS) >> 24;
784f8de65bSYendapally Reddy Dhananjaya Reddy 	if (num_words > max_words)
794f8de65bSYendapally Reddy Dhananjaya Reddy 		num_words = max_words;
804f8de65bSYendapally Reddy Dhananjaya Reddy 
814f8de65bSYendapally Reddy Dhananjaya Reddy 	for (count = 0; count < num_words; count++)
82abd42026SFlorian Fainelli 		((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
834f8de65bSYendapally Reddy Dhananjaya Reddy 
844f8de65bSYendapally Reddy Dhananjaya Reddy 	return num_words * sizeof(u32);
858c4196a2SLubomir Rintel }
868c4196a2SLubomir Rintel 
87a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng)
88a8157775SFlorian Fainelli {
89a8157775SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
90791af4f4SFlorian Fainelli 	int ret = 0;
9104b154faSFlorian Fainelli 	u32 val;
9204b154faSFlorian Fainelli 
93791af4f4SFlorian Fainelli 	ret = clk_prepare_enable(priv->clk);
94791af4f4SFlorian Fainelli 	if (ret)
95791af4f4SFlorian Fainelli 		return ret;
96791af4f4SFlorian Fainelli 
97*e5f9f41dSÁlvaro Fernández Rojas 	ret = reset_control_reset(priv->reset);
98*e5f9f41dSÁlvaro Fernández Rojas 	if (ret)
99*e5f9f41dSÁlvaro Fernández Rojas 		return ret;
100*e5f9f41dSÁlvaro Fernández Rojas 
10104b154faSFlorian Fainelli 	if (priv->mask_interrupts) {
10204b154faSFlorian Fainelli 		/* mask the interrupt */
103abd42026SFlorian Fainelli 		val = rng_readl(priv, RNG_INT_MASK);
10404b154faSFlorian Fainelli 		val |= RNG_INT_OFF;
105abd42026SFlorian Fainelli 		rng_writel(priv, val, RNG_INT_MASK);
10604b154faSFlorian Fainelli 	}
107a8157775SFlorian Fainelli 
108a8157775SFlorian Fainelli 	/* set warm-up count & enable */
109abd42026SFlorian Fainelli 	rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS);
110abd42026SFlorian Fainelli 	rng_writel(priv, RNG_RBGEN, RNG_CTRL);
111a8157775SFlorian Fainelli 
112791af4f4SFlorian Fainelli 	return ret;
113a8157775SFlorian Fainelli }
114a8157775SFlorian Fainelli 
115ec94bca7SFlorian Fainelli static void bcm2835_rng_cleanup(struct hwrng *rng)
116ec94bca7SFlorian Fainelli {
117ec94bca7SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
118ec94bca7SFlorian Fainelli 
119ec94bca7SFlorian Fainelli 	/* disable rng hardware */
120abd42026SFlorian Fainelli 	rng_writel(priv, 0, RNG_CTRL);
121791af4f4SFlorian Fainelli 
122791af4f4SFlorian Fainelli 	clk_disable_unprepare(priv->clk);
123ec94bca7SFlorian Fainelli }
124ec94bca7SFlorian Fainelli 
12504b154faSFlorian Fainelli struct bcm2835_rng_of_data {
12604b154faSFlorian Fainelli 	bool mask_interrupts;
12704b154faSFlorian Fainelli };
12804b154faSFlorian Fainelli 
12904b154faSFlorian Fainelli static const struct bcm2835_rng_of_data nsp_rng_of_data = {
13004b154faSFlorian Fainelli 	.mask_interrupts = true,
13104b154faSFlorian Fainelli };
13204b154faSFlorian Fainelli 
133422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = {
134422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm2835-rng"},
13504b154faSFlorian Fainelli 	{ .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data },
13604b154faSFlorian Fainelli 	{ .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data },
1378705f24fSFlorian Fainelli 	{ .compatible = "brcm,bcm6368-rng"},
138422a7491SYendapally Reddy Dhananjaya Reddy 	{},
139422a7491SYendapally Reddy Dhananjaya Reddy };
140422a7491SYendapally Reddy Dhananjaya Reddy 
1418c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev)
1428c4196a2SLubomir Rintel {
14304b154faSFlorian Fainelli 	const struct bcm2835_rng_of_data *of_data;
1448c4196a2SLubomir Rintel 	struct device *dev = &pdev->dev;
145422a7491SYendapally Reddy Dhananjaya Reddy 	const struct of_device_id *rng_id;
146b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv;
1478c4196a2SLubomir Rintel 	int err;
1488c4196a2SLubomir Rintel 
149b788479fSFlorian Fainelli 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
150b788479fSFlorian Fainelli 	if (!priv)
151b788479fSFlorian Fainelli 		return -ENOMEM;
152b788479fSFlorian Fainelli 
153b788479fSFlorian Fainelli 	platform_set_drvdata(pdev, priv);
154b788479fSFlorian Fainelli 
1558c4196a2SLubomir Rintel 	/* map peripheral */
1563e46bd34SYueHaibing 	priv->base = devm_platform_ioremap_resource(pdev, 0);
157c4fc5d6dSweiyongjun \(A\) 	if (IS_ERR(priv->base))
158b788479fSFlorian Fainelli 		return PTR_ERR(priv->base);
159b788479fSFlorian Fainelli 
160791af4f4SFlorian Fainelli 	/* Clock is optional on most platforms */
161e06da499STian Tao 	priv->clk = devm_clk_get_optional(dev, NULL);
162e06da499STian Tao 	if (IS_ERR(priv->clk))
163e06da499STian Tao 		return PTR_ERR(priv->clk);
164791af4f4SFlorian Fainelli 
165*e5f9f41dSÁlvaro Fernández Rojas 	priv->reset = devm_reset_control_get_optional_exclusive(dev, NULL);
166*e5f9f41dSÁlvaro Fernández Rojas 	if (IS_ERR(priv->reset))
167*e5f9f41dSÁlvaro Fernández Rojas 		return PTR_ERR(priv->reset);
168*e5f9f41dSÁlvaro Fernández Rojas 
1698705f24fSFlorian Fainelli 	priv->rng.name = pdev->name;
170a8157775SFlorian Fainelli 	priv->rng.init = bcm2835_rng_init;
171b788479fSFlorian Fainelli 	priv->rng.read = bcm2835_rng_read;
172ec94bca7SFlorian Fainelli 	priv->rng.cleanup = bcm2835_rng_cleanup;
1738c4196a2SLubomir Rintel 
174984798deSJonas Gorski 	if (dev_of_node(dev)) {
17564f8153aSHerbert Xu 		rng_id = of_match_node(bcm2835_rng_of_match, dev->of_node);
17621bb0ef4SFlorian Fainelli 		if (!rng_id)
177422a7491SYendapally Reddy Dhananjaya Reddy 			return -EINVAL;
17821bb0ef4SFlorian Fainelli 
179422a7491SYendapally Reddy Dhananjaya Reddy 		/* Check for rng init function, execute it */
18004b154faSFlorian Fainelli 		of_data = rng_id->data;
18104b154faSFlorian Fainelli 		if (of_data)
18204b154faSFlorian Fainelli 			priv->mask_interrupts = of_data->mask_interrupts;
183984798deSJonas Gorski 	}
184422a7491SYendapally Reddy Dhananjaya Reddy 
1858c4196a2SLubomir Rintel 	/* register driver */
18616a4c04bSFlorian Fainelli 	err = devm_hwrng_register(dev, &priv->rng);
18721bb0ef4SFlorian Fainelli 	if (err)
1888c4196a2SLubomir Rintel 		dev_err(dev, "hwrng registration failed\n");
18921bb0ef4SFlorian Fainelli 	else
1908c4196a2SLubomir Rintel 		dev_info(dev, "hwrng registered\n");
1918c4196a2SLubomir Rintel 
1928c4196a2SLubomir Rintel 	return err;
1938c4196a2SLubomir Rintel }
1948c4196a2SLubomir Rintel 
1958c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
1968c4196a2SLubomir Rintel 
197d4f6d923SRikard Falkeborn static const struct platform_device_id bcm2835_rng_devtype[] = {
1988705f24fSFlorian Fainelli 	{ .name = "bcm2835-rng" },
1998705f24fSFlorian Fainelli 	{ .name = "bcm63xx-rng" },
2008705f24fSFlorian Fainelli 	{ /* sentinel */ }
2018705f24fSFlorian Fainelli };
2028705f24fSFlorian Fainelli MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype);
2038705f24fSFlorian Fainelli 
2048c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = {
2058c4196a2SLubomir Rintel 	.driver = {
2068c4196a2SLubomir Rintel 		.name = "bcm2835-rng",
2078c4196a2SLubomir Rintel 		.of_match_table = bcm2835_rng_of_match,
2088c4196a2SLubomir Rintel 	},
2098c4196a2SLubomir Rintel 	.probe		= bcm2835_rng_probe,
2108705f24fSFlorian Fainelli 	.id_table	= bcm2835_rng_devtype,
2118c4196a2SLubomir Rintel };
2128c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver);
2138c4196a2SLubomir Rintel 
2148c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
2158c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
2168c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2");
217