xref: /openbmc/linux/drivers/char/hw_random/bcm2835-rng.c (revision 791af4f4907a8bee879713cb19fa90e6369f8386)
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>
18*791af4f4SFlorian 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;
37*791af4f4SFlorian 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 
458c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
468c4196a2SLubomir Rintel 			       bool wait)
478c4196a2SLubomir Rintel {
48b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
494f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 max_words = max / sizeof(u32);
504f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 num_words, count;
518c4196a2SLubomir Rintel 
52b788479fSFlorian Fainelli 	while ((__raw_readl(priv->base + RNG_STATUS) >> 24) == 0) {
538c4196a2SLubomir Rintel 		if (!wait)
548c4196a2SLubomir Rintel 			return 0;
558c4196a2SLubomir Rintel 		cpu_relax();
568c4196a2SLubomir Rintel 	}
578c4196a2SLubomir Rintel 
58b788479fSFlorian Fainelli 	num_words = readl(priv->base + RNG_STATUS) >> 24;
594f8de65bSYendapally Reddy Dhananjaya Reddy 	if (num_words > max_words)
604f8de65bSYendapally Reddy Dhananjaya Reddy 		num_words = max_words;
614f8de65bSYendapally Reddy Dhananjaya Reddy 
624f8de65bSYendapally Reddy Dhananjaya Reddy 	for (count = 0; count < num_words; count++)
63b788479fSFlorian Fainelli 		((u32 *)buf)[count] = readl(priv->base + RNG_DATA);
644f8de65bSYendapally Reddy Dhananjaya Reddy 
654f8de65bSYendapally Reddy Dhananjaya Reddy 	return num_words * sizeof(u32);
668c4196a2SLubomir Rintel }
678c4196a2SLubomir Rintel 
68a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng)
69a8157775SFlorian Fainelli {
70a8157775SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
71*791af4f4SFlorian Fainelli 	int ret = 0;
7204b154faSFlorian Fainelli 	u32 val;
7304b154faSFlorian Fainelli 
74*791af4f4SFlorian Fainelli 	if (!IS_ERR(priv->clk)) {
75*791af4f4SFlorian Fainelli 		ret = clk_prepare_enable(priv->clk);
76*791af4f4SFlorian Fainelli 		if (ret)
77*791af4f4SFlorian Fainelli 			return ret;
78*791af4f4SFlorian Fainelli 	}
79*791af4f4SFlorian Fainelli 
8004b154faSFlorian Fainelli 	if (priv->mask_interrupts) {
8104b154faSFlorian Fainelli 		/* mask the interrupt */
8204b154faSFlorian Fainelli 		val = readl(priv->base + RNG_INT_MASK);
8304b154faSFlorian Fainelli 		val |= RNG_INT_OFF;
8404b154faSFlorian Fainelli 		writel(val, priv->base + RNG_INT_MASK);
8504b154faSFlorian Fainelli 	}
86a8157775SFlorian Fainelli 
87a8157775SFlorian Fainelli 	/* set warm-up count & enable */
88a8157775SFlorian Fainelli 	__raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS);
89a8157775SFlorian Fainelli 	__raw_writel(RNG_RBGEN, priv->base + RNG_CTRL);
90a8157775SFlorian Fainelli 
91*791af4f4SFlorian Fainelli 	return ret;
92a8157775SFlorian Fainelli }
93a8157775SFlorian Fainelli 
94ec94bca7SFlorian Fainelli static void bcm2835_rng_cleanup(struct hwrng *rng)
95ec94bca7SFlorian Fainelli {
96ec94bca7SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
97ec94bca7SFlorian Fainelli 
98ec94bca7SFlorian Fainelli 	/* disable rng hardware */
99ec94bca7SFlorian Fainelli 	__raw_writel(0, priv->base + RNG_CTRL);
100*791af4f4SFlorian Fainelli 
101*791af4f4SFlorian Fainelli 	if (!IS_ERR(priv->clk))
102*791af4f4SFlorian Fainelli 		clk_disable_unprepare(priv->clk);
103ec94bca7SFlorian Fainelli }
104ec94bca7SFlorian Fainelli 
10504b154faSFlorian Fainelli struct bcm2835_rng_of_data {
10604b154faSFlorian Fainelli 	bool mask_interrupts;
10704b154faSFlorian Fainelli };
10804b154faSFlorian Fainelli 
10904b154faSFlorian Fainelli static const struct bcm2835_rng_of_data nsp_rng_of_data = {
11004b154faSFlorian Fainelli 	.mask_interrupts = true,
11104b154faSFlorian Fainelli };
11204b154faSFlorian Fainelli 
113422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = {
114422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm2835-rng"},
11504b154faSFlorian Fainelli 	{ .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data },
11604b154faSFlorian Fainelli 	{ .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data },
117422a7491SYendapally Reddy Dhananjaya Reddy 	{},
118422a7491SYendapally Reddy Dhananjaya Reddy };
119422a7491SYendapally Reddy Dhananjaya Reddy 
1208c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev)
1218c4196a2SLubomir Rintel {
12204b154faSFlorian Fainelli 	const struct bcm2835_rng_of_data *of_data;
1238c4196a2SLubomir Rintel 	struct device *dev = &pdev->dev;
1248c4196a2SLubomir Rintel 	struct device_node *np = dev->of_node;
125422a7491SYendapally Reddy Dhananjaya Reddy 	const struct of_device_id *rng_id;
126b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv;
12721bb0ef4SFlorian Fainelli 	struct resource *r;
1288c4196a2SLubomir Rintel 	int err;
1298c4196a2SLubomir Rintel 
130b788479fSFlorian Fainelli 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
131b788479fSFlorian Fainelli 	if (!priv)
132b788479fSFlorian Fainelli 		return -ENOMEM;
133b788479fSFlorian Fainelli 
134b788479fSFlorian Fainelli 	platform_set_drvdata(pdev, priv);
135b788479fSFlorian Fainelli 
13621bb0ef4SFlorian Fainelli 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13721bb0ef4SFlorian Fainelli 
1388c4196a2SLubomir Rintel 	/* map peripheral */
139b788479fSFlorian Fainelli 	priv->base = devm_ioremap_resource(dev, r);
140b788479fSFlorian Fainelli 	if (IS_ERR(priv->base)) {
1418c4196a2SLubomir Rintel 		dev_err(dev, "failed to remap rng regs");
142b788479fSFlorian Fainelli 		return PTR_ERR(priv->base);
1438c4196a2SLubomir Rintel 	}
144b788479fSFlorian Fainelli 
145*791af4f4SFlorian Fainelli 	/* Clock is optional on most platforms */
146*791af4f4SFlorian Fainelli 	priv->clk = devm_clk_get(dev, NULL);
147*791af4f4SFlorian Fainelli 
148b788479fSFlorian Fainelli 	priv->rng.name = "bcm2835-rng";
149a8157775SFlorian Fainelli 	priv->rng.init = bcm2835_rng_init;
150b788479fSFlorian Fainelli 	priv->rng.read = bcm2835_rng_read;
151ec94bca7SFlorian Fainelli 	priv->rng.cleanup = bcm2835_rng_cleanup;
1528c4196a2SLubomir Rintel 
153422a7491SYendapally Reddy Dhananjaya Reddy 	rng_id = of_match_node(bcm2835_rng_of_match, np);
15421bb0ef4SFlorian Fainelli 	if (!rng_id)
155422a7491SYendapally Reddy Dhananjaya Reddy 		return -EINVAL;
15621bb0ef4SFlorian Fainelli 
157422a7491SYendapally Reddy Dhananjaya Reddy 	/* Check for rng init function, execute it */
15804b154faSFlorian Fainelli 	of_data = rng_id->data;
15904b154faSFlorian Fainelli 	if (of_data)
16004b154faSFlorian Fainelli 		priv->mask_interrupts = of_data->mask_interrupts;
161422a7491SYendapally Reddy Dhananjaya Reddy 
1628c4196a2SLubomir Rintel 	/* register driver */
16316a4c04bSFlorian Fainelli 	err = devm_hwrng_register(dev, &priv->rng);
16421bb0ef4SFlorian Fainelli 	if (err)
1658c4196a2SLubomir Rintel 		dev_err(dev, "hwrng registration failed\n");
16621bb0ef4SFlorian Fainelli 	else
1678c4196a2SLubomir Rintel 		dev_info(dev, "hwrng registered\n");
1688c4196a2SLubomir Rintel 
1698c4196a2SLubomir Rintel 	return err;
1708c4196a2SLubomir Rintel }
1718c4196a2SLubomir Rintel 
1728c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
1738c4196a2SLubomir Rintel 
1748c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = {
1758c4196a2SLubomir Rintel 	.driver = {
1768c4196a2SLubomir Rintel 		.name = "bcm2835-rng",
1778c4196a2SLubomir Rintel 		.of_match_table = bcm2835_rng_of_match,
1788c4196a2SLubomir Rintel 	},
1798c4196a2SLubomir Rintel 	.probe		= bcm2835_rng_probe,
1808c4196a2SLubomir Rintel };
1818c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver);
1828c4196a2SLubomir Rintel 
1838c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
1848c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
1858c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2");
186