xref: /openbmc/linux/drivers/char/hw_random/bcm2835-rng.c (revision ec94bca7be6b29c1aebf2e1e95855ee6ca0d5d62)
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>
188c4196a2SLubomir Rintel 
198c4196a2SLubomir Rintel #define RNG_CTRL	0x0
208c4196a2SLubomir Rintel #define RNG_STATUS	0x4
218c4196a2SLubomir Rintel #define RNG_DATA	0x8
22422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_MASK	0x10
238c4196a2SLubomir Rintel 
248c4196a2SLubomir Rintel /* enable rng */
258c4196a2SLubomir Rintel #define RNG_RBGEN	0x1
268c4196a2SLubomir Rintel 
278c4196a2SLubomir Rintel /* the initial numbers generated are "less random" so will be discarded */
288c4196a2SLubomir Rintel #define RNG_WARMUP_COUNT 0x40000
298c4196a2SLubomir Rintel 
30422a7491SYendapally Reddy Dhananjaya Reddy #define RNG_INT_OFF	0x1
31422a7491SYendapally Reddy Dhananjaya Reddy 
32b788479fSFlorian Fainelli struct bcm2835_rng_priv {
33b788479fSFlorian Fainelli 	struct hwrng rng;
34b788479fSFlorian Fainelli 	void __iomem *base;
35b788479fSFlorian Fainelli };
36b788479fSFlorian Fainelli 
37422a7491SYendapally Reddy Dhananjaya Reddy static void __init nsp_rng_init(void __iomem *base)
38422a7491SYendapally Reddy Dhananjaya Reddy {
39422a7491SYendapally Reddy Dhananjaya Reddy 	u32 val;
40422a7491SYendapally Reddy Dhananjaya Reddy 
41422a7491SYendapally Reddy Dhananjaya Reddy 	/* mask the interrupt */
42422a7491SYendapally Reddy Dhananjaya Reddy 	val = readl(base + RNG_INT_MASK);
43422a7491SYendapally Reddy Dhananjaya Reddy 	val |= RNG_INT_OFF;
44422a7491SYendapally Reddy Dhananjaya Reddy 	writel(val, base + RNG_INT_MASK);
45422a7491SYendapally Reddy Dhananjaya Reddy }
46422a7491SYendapally Reddy Dhananjaya Reddy 
47b788479fSFlorian Fainelli static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
48b788479fSFlorian Fainelli {
49b788479fSFlorian Fainelli 	return container_of(rng, struct bcm2835_rng_priv, rng);
50b788479fSFlorian Fainelli }
51b788479fSFlorian Fainelli 
528c4196a2SLubomir Rintel static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
538c4196a2SLubomir Rintel 			       bool wait)
548c4196a2SLubomir Rintel {
55b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
564f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 max_words = max / sizeof(u32);
574f8de65bSYendapally Reddy Dhananjaya Reddy 	u32 num_words, count;
588c4196a2SLubomir Rintel 
59b788479fSFlorian Fainelli 	while ((__raw_readl(priv->base + RNG_STATUS) >> 24) == 0) {
608c4196a2SLubomir Rintel 		if (!wait)
618c4196a2SLubomir Rintel 			return 0;
628c4196a2SLubomir Rintel 		cpu_relax();
638c4196a2SLubomir Rintel 	}
648c4196a2SLubomir Rintel 
65b788479fSFlorian Fainelli 	num_words = readl(priv->base + RNG_STATUS) >> 24;
664f8de65bSYendapally Reddy Dhananjaya Reddy 	if (num_words > max_words)
674f8de65bSYendapally Reddy Dhananjaya Reddy 		num_words = max_words;
684f8de65bSYendapally Reddy Dhananjaya Reddy 
694f8de65bSYendapally Reddy Dhananjaya Reddy 	for (count = 0; count < num_words; count++)
70b788479fSFlorian Fainelli 		((u32 *)buf)[count] = readl(priv->base + RNG_DATA);
714f8de65bSYendapally Reddy Dhananjaya Reddy 
724f8de65bSYendapally Reddy Dhananjaya Reddy 	return num_words * sizeof(u32);
738c4196a2SLubomir Rintel }
748c4196a2SLubomir Rintel 
75a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng)
76a8157775SFlorian Fainelli {
77a8157775SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
78a8157775SFlorian Fainelli 
79a8157775SFlorian Fainelli 	/* set warm-up count & enable */
80a8157775SFlorian Fainelli 	__raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS);
81a8157775SFlorian Fainelli 	__raw_writel(RNG_RBGEN, priv->base + RNG_CTRL);
82a8157775SFlorian Fainelli 
83a8157775SFlorian Fainelli 	return 0;
84a8157775SFlorian Fainelli }
85a8157775SFlorian Fainelli 
86*ec94bca7SFlorian Fainelli static void bcm2835_rng_cleanup(struct hwrng *rng)
87*ec94bca7SFlorian Fainelli {
88*ec94bca7SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
89*ec94bca7SFlorian Fainelli 
90*ec94bca7SFlorian Fainelli 	/* disable rng hardware */
91*ec94bca7SFlorian Fainelli 	__raw_writel(0, priv->base + RNG_CTRL);
92*ec94bca7SFlorian Fainelli }
93*ec94bca7SFlorian Fainelli 
94422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = {
95422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm2835-rng"},
96422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
9752140993SFlorian Fainelli 	{ .compatible = "brcm,bcm5301x-rng", .data = nsp_rng_init},
98422a7491SYendapally Reddy Dhananjaya Reddy 	{},
99422a7491SYendapally Reddy Dhananjaya Reddy };
100422a7491SYendapally Reddy Dhananjaya Reddy 
1018c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev)
1028c4196a2SLubomir Rintel {
1038c4196a2SLubomir Rintel 	struct device *dev = &pdev->dev;
1048c4196a2SLubomir Rintel 	struct device_node *np = dev->of_node;
105422a7491SYendapally Reddy Dhananjaya Reddy 	void (*rng_setup)(void __iomem *base);
106422a7491SYendapally Reddy Dhananjaya Reddy 	const struct of_device_id *rng_id;
107b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv;
10821bb0ef4SFlorian Fainelli 	struct resource *r;
1098c4196a2SLubomir Rintel 	int err;
1108c4196a2SLubomir Rintel 
111b788479fSFlorian Fainelli 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
112b788479fSFlorian Fainelli 	if (!priv)
113b788479fSFlorian Fainelli 		return -ENOMEM;
114b788479fSFlorian Fainelli 
115b788479fSFlorian Fainelli 	platform_set_drvdata(pdev, priv);
116b788479fSFlorian Fainelli 
11721bb0ef4SFlorian Fainelli 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
11821bb0ef4SFlorian Fainelli 
1198c4196a2SLubomir Rintel 	/* map peripheral */
120b788479fSFlorian Fainelli 	priv->base = devm_ioremap_resource(dev, r);
121b788479fSFlorian Fainelli 	if (IS_ERR(priv->base)) {
1228c4196a2SLubomir Rintel 		dev_err(dev, "failed to remap rng regs");
123b788479fSFlorian Fainelli 		return PTR_ERR(priv->base);
1248c4196a2SLubomir Rintel 	}
125b788479fSFlorian Fainelli 
126b788479fSFlorian Fainelli 	priv->rng.name = "bcm2835-rng";
127a8157775SFlorian Fainelli 	priv->rng.init = bcm2835_rng_init;
128b788479fSFlorian Fainelli 	priv->rng.read = bcm2835_rng_read;
129*ec94bca7SFlorian Fainelli 	priv->rng.cleanup = bcm2835_rng_cleanup;
1308c4196a2SLubomir Rintel 
131422a7491SYendapally Reddy Dhananjaya Reddy 	rng_id = of_match_node(bcm2835_rng_of_match, np);
13221bb0ef4SFlorian Fainelli 	if (!rng_id)
133422a7491SYendapally Reddy Dhananjaya Reddy 		return -EINVAL;
13421bb0ef4SFlorian Fainelli 
135422a7491SYendapally Reddy Dhananjaya Reddy 	/* Check for rng init function, execute it */
136422a7491SYendapally Reddy Dhananjaya Reddy 	rng_setup = rng_id->data;
137422a7491SYendapally Reddy Dhananjaya Reddy 	if (rng_setup)
138b788479fSFlorian Fainelli 		rng_setup(priv->base);
139422a7491SYendapally Reddy Dhananjaya Reddy 
1408c4196a2SLubomir Rintel 	/* register driver */
141b788479fSFlorian Fainelli 	err = hwrng_register(&priv->rng);
14221bb0ef4SFlorian Fainelli 	if (err)
1438c4196a2SLubomir Rintel 		dev_err(dev, "hwrng registration failed\n");
14421bb0ef4SFlorian Fainelli 	else
1458c4196a2SLubomir Rintel 		dev_info(dev, "hwrng registered\n");
1468c4196a2SLubomir Rintel 
1478c4196a2SLubomir Rintel 	return err;
1488c4196a2SLubomir Rintel }
1498c4196a2SLubomir Rintel 
1508c4196a2SLubomir Rintel static int bcm2835_rng_remove(struct platform_device *pdev)
1518c4196a2SLubomir Rintel {
152b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = platform_get_drvdata(pdev);
1538c4196a2SLubomir Rintel 
1548c4196a2SLubomir Rintel 	/* unregister driver */
155b788479fSFlorian Fainelli 	hwrng_unregister(&priv->rng);
1568c4196a2SLubomir Rintel 
1578c4196a2SLubomir Rintel 	return 0;
1588c4196a2SLubomir Rintel }
1598c4196a2SLubomir Rintel 
1608c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
1618c4196a2SLubomir Rintel 
1628c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = {
1638c4196a2SLubomir Rintel 	.driver = {
1648c4196a2SLubomir Rintel 		.name = "bcm2835-rng",
1658c4196a2SLubomir Rintel 		.of_match_table = bcm2835_rng_of_match,
1668c4196a2SLubomir Rintel 	},
1678c4196a2SLubomir Rintel 	.probe		= bcm2835_rng_probe,
1688c4196a2SLubomir Rintel 	.remove		= bcm2835_rng_remove,
1698c4196a2SLubomir Rintel };
1708c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver);
1718c4196a2SLubomir Rintel 
1728c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
1738c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
1748c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2");
175