1 /**
2  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
3  * Copyright (c) 2013 Lubomir Rintel
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License ("GPL")
7  * version 2, as published by the Free Software Foundation.
8  */
9 
10 #include <linux/hw_random.h>
11 #include <linux/io.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/printk.h>
18 
19 #define RNG_CTRL	0x0
20 #define RNG_STATUS	0x4
21 #define RNG_DATA	0x8
22 
23 /* enable rng */
24 #define RNG_RBGEN	0x1
25 
26 /* the initial numbers generated are "less random" so will be discarded */
27 #define RNG_WARMUP_COUNT 0x40000
28 
29 static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
30 			       bool wait)
31 {
32 	void __iomem *rng_base = (void __iomem *)rng->priv;
33 
34 	while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
35 		if (!wait)
36 			return 0;
37 		cpu_relax();
38 	}
39 
40 	*(u32 *)buf = __raw_readl(rng_base + RNG_DATA);
41 	return sizeof(u32);
42 }
43 
44 static struct hwrng bcm2835_rng_ops = {
45 	.name	= "bcm2835",
46 	.read	= bcm2835_rng_read,
47 };
48 
49 static int bcm2835_rng_probe(struct platform_device *pdev)
50 {
51 	struct device *dev = &pdev->dev;
52 	struct device_node *np = dev->of_node;
53 	void __iomem *rng_base;
54 	int err;
55 
56 	/* map peripheral */
57 	rng_base = of_iomap(np, 0);
58 	if (!rng_base) {
59 		dev_err(dev, "failed to remap rng regs");
60 		return -ENODEV;
61 	}
62 	bcm2835_rng_ops.priv = (unsigned long)rng_base;
63 
64 	/* set warm-up count & enable */
65 	__raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
66 	__raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
67 
68 	/* register driver */
69 	err = hwrng_register(&bcm2835_rng_ops);
70 	if (err) {
71 		dev_err(dev, "hwrng registration failed\n");
72 		iounmap(rng_base);
73 	} else
74 		dev_info(dev, "hwrng registered\n");
75 
76 	return err;
77 }
78 
79 static int bcm2835_rng_remove(struct platform_device *pdev)
80 {
81 	void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
82 
83 	/* disable rng hardware */
84 	__raw_writel(0, rng_base + RNG_CTRL);
85 
86 	/* unregister driver */
87 	hwrng_unregister(&bcm2835_rng_ops);
88 	iounmap(rng_base);
89 
90 	return 0;
91 }
92 
93 static const struct of_device_id bcm2835_rng_of_match[] = {
94 	{ .compatible = "brcm,bcm2835-rng", },
95 	{},
96 };
97 MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
98 
99 static struct platform_driver bcm2835_rng_driver = {
100 	.driver = {
101 		.name = "bcm2835-rng",
102 		.of_match_table = bcm2835_rng_of_match,
103 	},
104 	.probe		= bcm2835_rng_probe,
105 	.remove		= bcm2835_rng_remove,
106 };
107 module_platform_driver(bcm2835_rng_driver);
108 
109 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
110 MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
111 MODULE_LICENSE("GPL v2");
112