2 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
3 * Copyright (c) 2013 Lubomir Rintel
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.
10 #include <linux/hw_random.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>
20 #define RNG_STATUS 0x4
22 #define RNG_INT_MASK 0x10
27 /* the initial numbers generated are "less random" so will be discarded */
28 #define RNG_WARMUP_COUNT 0x40000
30 #define RNG_INT_OFF 0x1
32 static void __init nsp_rng_init(void __iomem *base)
36 /* mask the interrupt */
37 val = readl(base + RNG_INT_MASK);
39 writel(val, base + RNG_INT_MASK);
42 static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
45 void __iomem *rng_base = (void __iomem *)rng->priv;
47 while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
53 *(u32 *)buf = __raw_readl(rng_base + RNG_DATA);
57 static struct hwrng bcm2835_rng_ops = {
59 .read = bcm2835_rng_read,
62 static const struct of_device_id bcm2835_rng_of_match[] = {
63 { .compatible = "brcm,bcm2835-rng"},
64 { .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
68 static int bcm2835_rng_probe(struct platform_device *pdev)
70 struct device *dev = &pdev->dev;
71 struct device_node *np = dev->of_node;
72 void (*rng_setup)(void __iomem *base);
73 const struct of_device_id *rng_id;
74 void __iomem *rng_base;
78 rng_base = of_iomap(np, 0);
80 dev_err(dev, "failed to remap rng regs");
83 bcm2835_rng_ops.priv = (unsigned long)rng_base;
85 rng_id = of_match_node(bcm2835_rng_of_match, np);
89 /* Check for rng init function, execute it */
90 rng_setup = rng_id->data;
94 /* set warm-up count & enable */
95 __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
96 __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
99 err = hwrng_register(&bcm2835_rng_ops);
101 dev_err(dev, "hwrng registration failed\n");
104 dev_info(dev, "hwrng registered\n");
109 static int bcm2835_rng_remove(struct platform_device *pdev)
111 void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
113 /* disable rng hardware */
114 __raw_writel(0, rng_base + RNG_CTRL);
116 /* unregister driver */
117 hwrng_unregister(&bcm2835_rng_ops);
123 MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
125 static struct platform_driver bcm2835_rng_driver = {
127 .name = "bcm2835-rng",
128 .of_match_table = bcm2835_rng_of_match,
130 .probe = bcm2835_rng_probe,
131 .remove = bcm2835_rng_remove,
133 module_platform_driver(bcm2835_rng_driver);
135 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
136 MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
137 MODULE_LICENSE("GPL v2");