xref: /openbmc/linux/drivers/char/hw_random/bcm2835-rng.c (revision a815777553826079929e0910fcf3fda9a1e8438b)
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 
75*a8157775SFlorian Fainelli static int bcm2835_rng_init(struct hwrng *rng)
76*a8157775SFlorian Fainelli {
77*a8157775SFlorian Fainelli 	struct bcm2835_rng_priv *priv = to_rng_priv(rng);
78*a8157775SFlorian Fainelli 
79*a8157775SFlorian Fainelli 	/* set warm-up count & enable */
80*a8157775SFlorian Fainelli 	__raw_writel(RNG_WARMUP_COUNT, priv->base + RNG_STATUS);
81*a8157775SFlorian Fainelli 	__raw_writel(RNG_RBGEN, priv->base + RNG_CTRL);
82*a8157775SFlorian Fainelli 
83*a8157775SFlorian Fainelli 	return 0;
84*a8157775SFlorian Fainelli }
85*a8157775SFlorian Fainelli 
86422a7491SYendapally Reddy Dhananjaya Reddy static const struct of_device_id bcm2835_rng_of_match[] = {
87422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm2835-rng"},
88422a7491SYendapally Reddy Dhananjaya Reddy 	{ .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
8952140993SFlorian Fainelli 	{ .compatible = "brcm,bcm5301x-rng", .data = nsp_rng_init},
90422a7491SYendapally Reddy Dhananjaya Reddy 	{},
91422a7491SYendapally Reddy Dhananjaya Reddy };
92422a7491SYendapally Reddy Dhananjaya Reddy 
938c4196a2SLubomir Rintel static int bcm2835_rng_probe(struct platform_device *pdev)
948c4196a2SLubomir Rintel {
958c4196a2SLubomir Rintel 	struct device *dev = &pdev->dev;
968c4196a2SLubomir Rintel 	struct device_node *np = dev->of_node;
97422a7491SYendapally Reddy Dhananjaya Reddy 	void (*rng_setup)(void __iomem *base);
98422a7491SYendapally Reddy Dhananjaya Reddy 	const struct of_device_id *rng_id;
99b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv;
10021bb0ef4SFlorian Fainelli 	struct resource *r;
1018c4196a2SLubomir Rintel 	int err;
1028c4196a2SLubomir Rintel 
103b788479fSFlorian Fainelli 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
104b788479fSFlorian Fainelli 	if (!priv)
105b788479fSFlorian Fainelli 		return -ENOMEM;
106b788479fSFlorian Fainelli 
107b788479fSFlorian Fainelli 	platform_set_drvdata(pdev, priv);
108b788479fSFlorian Fainelli 
10921bb0ef4SFlorian Fainelli 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
11021bb0ef4SFlorian Fainelli 
1118c4196a2SLubomir Rintel 	/* map peripheral */
112b788479fSFlorian Fainelli 	priv->base = devm_ioremap_resource(dev, r);
113b788479fSFlorian Fainelli 	if (IS_ERR(priv->base)) {
1148c4196a2SLubomir Rintel 		dev_err(dev, "failed to remap rng regs");
115b788479fSFlorian Fainelli 		return PTR_ERR(priv->base);
1168c4196a2SLubomir Rintel 	}
117b788479fSFlorian Fainelli 
118b788479fSFlorian Fainelli 	priv->rng.name = "bcm2835-rng";
119*a8157775SFlorian Fainelli 	priv->rng.init = bcm2835_rng_init;
120b788479fSFlorian Fainelli 	priv->rng.read = bcm2835_rng_read;
1218c4196a2SLubomir Rintel 
122422a7491SYendapally Reddy Dhananjaya Reddy 	rng_id = of_match_node(bcm2835_rng_of_match, np);
12321bb0ef4SFlorian Fainelli 	if (!rng_id)
124422a7491SYendapally Reddy Dhananjaya Reddy 		return -EINVAL;
12521bb0ef4SFlorian Fainelli 
126422a7491SYendapally Reddy Dhananjaya Reddy 	/* Check for rng init function, execute it */
127422a7491SYendapally Reddy Dhananjaya Reddy 	rng_setup = rng_id->data;
128422a7491SYendapally Reddy Dhananjaya Reddy 	if (rng_setup)
129b788479fSFlorian Fainelli 		rng_setup(priv->base);
130422a7491SYendapally Reddy Dhananjaya Reddy 
1318c4196a2SLubomir Rintel 	/* register driver */
132b788479fSFlorian Fainelli 	err = hwrng_register(&priv->rng);
13321bb0ef4SFlorian Fainelli 	if (err)
1348c4196a2SLubomir Rintel 		dev_err(dev, "hwrng registration failed\n");
13521bb0ef4SFlorian Fainelli 	else
1368c4196a2SLubomir Rintel 		dev_info(dev, "hwrng registered\n");
1378c4196a2SLubomir Rintel 
1388c4196a2SLubomir Rintel 	return err;
1398c4196a2SLubomir Rintel }
1408c4196a2SLubomir Rintel 
1418c4196a2SLubomir Rintel static int bcm2835_rng_remove(struct platform_device *pdev)
1428c4196a2SLubomir Rintel {
143b788479fSFlorian Fainelli 	struct bcm2835_rng_priv *priv = platform_get_drvdata(pdev);
1448c4196a2SLubomir Rintel 
1458c4196a2SLubomir Rintel 	/* disable rng hardware */
146b788479fSFlorian Fainelli 	__raw_writel(0, priv->base + RNG_CTRL);
1478c4196a2SLubomir Rintel 
1488c4196a2SLubomir Rintel 	/* unregister driver */
149b788479fSFlorian Fainelli 	hwrng_unregister(&priv->rng);
1508c4196a2SLubomir Rintel 
1518c4196a2SLubomir Rintel 	return 0;
1528c4196a2SLubomir Rintel }
1538c4196a2SLubomir Rintel 
1548c4196a2SLubomir Rintel MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
1558c4196a2SLubomir Rintel 
1568c4196a2SLubomir Rintel static struct platform_driver bcm2835_rng_driver = {
1578c4196a2SLubomir Rintel 	.driver = {
1588c4196a2SLubomir Rintel 		.name = "bcm2835-rng",
1598c4196a2SLubomir Rintel 		.of_match_table = bcm2835_rng_of_match,
1608c4196a2SLubomir Rintel 	},
1618c4196a2SLubomir Rintel 	.probe		= bcm2835_rng_probe,
1628c4196a2SLubomir Rintel 	.remove		= bcm2835_rng_remove,
1638c4196a2SLubomir Rintel };
1648c4196a2SLubomir Rintel module_platform_driver(bcm2835_rng_driver);
1658c4196a2SLubomir Rintel 
1668c4196a2SLubomir Rintel MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
1678c4196a2SLubomir Rintel MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
1688c4196a2SLubomir Rintel MODULE_LICENSE("GPL v2");
169