xref: /openbmc/linux/drivers/char/hw_random/bcm2835-rng.c (revision 7b4c5d30d0bd2b22c09d4d993a76e0973a873891)
18c4196a2SLubomir Rintel /**
28c4196a2SLubomir Rintel  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
38c4196a2SLubomir Rintel  * Copyright (c) 2013 Lubomir Rintel
48c4196a2SLubomir Rintel  *
58c4196a2SLubomir Rintel  * This program is free software; you can redistribute it and/or
68c4196a2SLubomir Rintel  * modify it under the terms of the GNU General Public License ("GPL")
78c4196a2SLubomir Rintel  * version 2, as published by the Free Software Foundation.
88c4196a2SLubomir Rintel  */
98c4196a2SLubomir Rintel 
108c4196a2SLubomir Rintel #include <linux/hw_random.h>
118c4196a2SLubomir Rintel #include <linux/io.h>
128c4196a2SLubomir Rintel #include <linux/kernel.h>
138c4196a2SLubomir Rintel #include <linux/module.h>
148c4196a2SLubomir Rintel #include <linux/of_address.h>
158c4196a2SLubomir Rintel #include <linux/of_platform.h>
168c4196a2SLubomir Rintel #include <linux/platform_device.h>
178c4196a2SLubomir Rintel #include <linux/printk.h>
18791af4f4SFlorian Fainelli #include <linux/clk.h>
198c4196a2SLubomir Rintel 
208c4196a2SLubomir Rintel #define RNG_CTRL	0x0
218c4196a2SLubomir Rintel #define RNG_STATUS	0x4
228c4196a2SLubomir Rintel #define RNG_DATA	0x8
23422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_MASK	0x10
248c4196a2SLubomir Rintel 
258c4196a2SLubomir Rintel /* enable rng */
268c4196a2SLubomir Rintel #define RNG_RBGEN	0x1
278c4196a2SLubomir Rintel 
288c4196a2SLubomir Rintel /* the initial numbers generated are "less random" so will be discarded */
298c4196a2SLubomir Rintel #define RNG_WARMUP_COUNT 0x40000
308c4196a2SLubomir Rintel 
31422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_OFF	0x1
32422a7491SYendapally Reddy Dhananjaya Reddy 
33b788479fSFlorian Fainelli struct bcm2835_rng_priv {
34b788479fSFlorian Fainelli 	struct hwrng rng;
35b788479fSFlorian Fainelli 	void __iomem *base;
3604b154faSFlorian Fainelli 	bool mask_interrupts;
37791af4f4SFlorian Fainelli 	struct clk *clk;
38b788479fSFlorian Fainelli };
39b788479fSFlorian Fainelli 
40b788479fSFlorian Fainelli static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
41b788479fSFlorian Fainelli {
42b788479fSFlorian Fainelli 	return container_of(rng, struct bcm2835_rng_priv, rng);
43b788479fSFlorian Fainelli }
44b788479fSFlorian Fainelli 
45abd42026SFlorian Fainelli static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset)
46abd42026SFlorian Fainelli {
476f09359aSFlorian Fainelli 	/* MIPS chips strapped for BE will automagically configure the
486f09359aSFlorian Fainelli 	 * peripheral registers for CPU-native byte order.
496f09359aSFlorian Fainelli 	 */
506f09359aSFlorian Fainelli 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
516f09359aSFlorian Fainelli 		return __raw_readl(priv->base + offset);
526f09359aSFlorian Fainelli 	else
53abd42026SFlorian Fainelli 		return readl(priv->base + offset);
54abd42026SFlorian Fainelli }
55abd42026SFlorian Fainelli 
56abd42026SFlorian Fainelli static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
57abd42026SFlorian Fainelli 			      u32 offset)
58abd42026SFlorian Fainelli {
596f09359aSFlorian Fainelli 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
606f09359aSFlorian Fainelli 		__raw_writel(val, priv->base + offset);
616f09359aSFlorian Fainelli 	else
62abd42026SFlorian Fainelli 		writel(val, priv->base + offset);
63abd42026SFlorian Fainelli }
64abd42026SFlorian Fainelli 
658c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
668c4196a2SLubomir Rintel 			       bool wait)
678c4196a2SLubomir Rintel {
68b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
694f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 max_words = max / sizeof(u32);
704f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 num_words, count;
718c4196a2SLubomir Rintel 
72abd42026SFlorian Fainelli 	while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
738c4196a2SLubomir Rintel 		if (!wait)
748c4196a2SLubomir Rintel 			return 0;
758c4196a2SLubomir Rintel 		cpu_relax();
768c4196a2SLubomir Rintel 	}
778c4196a2SLubomir Rintel 
78abd42026SFlorian Fainelli 	num_words = rng_readl(priv, RNG_STATUS) >> 24;
794f8de65bSYendapally Reddy Dhananjaya Reddy 	if (num_words > max_words)
804f8de65bSYendapally Reddy Dhananjaya Reddy 		num_words = max_words;
814f8de65bSYendapally Reddy Dhananjaya Reddy 
824f8de65bSYendapally Reddy Dhananjaya Reddy 	for (count = 0; count < num_words; count++)
83abd42026SFlorian Fainelli 		((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
844f8de65bSYendapally Reddy Dhananjaya Reddy 
854f8de65bSYendapally Reddy Dhananjaya Reddy 	return num_words * sizeof(u32);
868c4196a2SLubomir Rintel }
878c4196a2SLubomir Rintel 
88a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng)
89a8157775SFlorian Fainelli {
90a8157775SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
91791af4f4SFlorian Fainelli 	int ret = 0;
9204b154faSFlorian Fainelli 	u32 val;
9304b154faSFlorian Fainelli 
94791af4f4SFlorian Fainelli 	if (!IS_ERR(priv->clk)) {
95791af4f4SFlorian Fainelli 		ret = clk_prepare_enable(priv->clk);
96791af4f4SFlorian Fainelli 		if (ret)
97791af4f4SFlorian Fainelli 			return ret;
98791af4f4SFlorian Fainelli 	}
99791af4f4SFlorian Fainelli 
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 	if (!IS_ERR(priv->clk))
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;
1458c4196a2SLubomir Rintel 	struct device_node *np = dev->of_node;
146422a7491SYendapally Reddy Dhananjaya Reddy 	const struct of_device_id *rng_id;
147b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv;
14821bb0ef4SFlorian Fainelli 	struct resource *r;
1498c4196a2SLubomir Rintel 	int err;
1508c4196a2SLubomir Rintel 
151b788479fSFlorian Fainelli 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
152b788479fSFlorian Fainelli 	if (!priv)
153b788479fSFlorian Fainelli 		return -ENOMEM;
154b788479fSFlorian Fainelli 
155b788479fSFlorian Fainelli 	platform_set_drvdata(pdev, priv);
156b788479fSFlorian Fainelli 
15721bb0ef4SFlorian Fainelli 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
15821bb0ef4SFlorian Fainelli 
1598c4196a2SLubomir Rintel 	/* map peripheral */
160b788479fSFlorian Fainelli 	priv->base = devm_ioremap_resource(dev, r);
161c4fc5d6dSweiyongjun \(A\) 	if (IS_ERR(priv->base))
162b788479fSFlorian Fainelli 		return PTR_ERR(priv->base);
163b788479fSFlorian Fainelli 
164791af4f4SFlorian Fainelli 	/* Clock is optional on most platforms */
165791af4f4SFlorian Fainelli 	priv->clk = devm_clk_get(dev, NULL);
166*7b4c5d30SStefan Wahren 	if (IS_ERR(priv->clk) && PTR_ERR(priv->clk) == -EPROBE_DEFER)
167*7b4c5d30SStefan Wahren 		return -EPROBE_DEFER;
168791af4f4SFlorian Fainelli 
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 
174422a7491SYendapally Reddy Dhananjaya Reddy 	rng_id = of_match_node(bcm2835_rng_of_match, np);
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;
182422a7491SYendapally Reddy Dhananjaya Reddy 
1838c4196a2SLubomir Rintel 	/* register driver */
18416a4c04bSFlorian Fainelli 	err = devm_hwrng_register(dev, &priv->rng);
18521bb0ef4SFlorian Fainelli 	if (err)
1868c4196a2SLubomir Rintel 		dev_err(dev, "hwrng registration failed\n");
18721bb0ef4SFlorian Fainelli 	else
1888c4196a2SLubomir Rintel 		dev_info(dev, "hwrng registered\n");
1898c4196a2SLubomir Rintel 
1908c4196a2SLubomir Rintel 	return err;
1918c4196a2SLubomir Rintel }
1928c4196a2SLubomir Rintel 
1938c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
1948c4196a2SLubomir Rintel 
1958705f24fSFlorian Fainelli static struct platform_device_id bcm2835_rng_devtype[] = {
1968705f24fSFlorian Fainelli 	{ .name = "bcm2835-rng" },
1978705f24fSFlorian Fainelli 	{ .name = "bcm63xx-rng" },
1988705f24fSFlorian Fainelli 	{ /* sentinel */ }
1998705f24fSFlorian Fainelli };
2008705f24fSFlorian Fainelli MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype);
2018705f24fSFlorian Fainelli 
2028c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = {
2038c4196a2SLubomir Rintel 	.driver = {
2048c4196a2SLubomir Rintel 		.name = "bcm2835-rng",
2058c4196a2SLubomir Rintel 		.of_match_table = bcm2835_rng_of_match,
2068c4196a2SLubomir Rintel 	},
2078c4196a2SLubomir Rintel 	.probe		= bcm2835_rng_probe,
2088705f24fSFlorian Fainelli 	.id_table	= bcm2835_rng_devtype,
2098c4196a2SLubomir Rintel };
2108c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver);
2118c4196a2SLubomir Rintel 
2128c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
2138c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
2148c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2");
215