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>
11 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/printk.h>
21 #define RNG_STATUS 0x4
27 /* the initial numbers generated are "less random" so will be discarded */
28 #define RNG_WARMUP_COUNT 0x40000
30 static int bcm2835_rng_read(struct hwrng
*rng
, void *buf
, size_t max
,
33 void __iomem
*rng_base
= (void __iomem
*)rng
->priv
;
35 while ((__raw_readl(rng_base
+ RNG_STATUS
) >> 24) == 0) {
41 *(u32
*)buf
= __raw_readl(rng_base
+ RNG_DATA
);
45 static struct hwrng bcm2835_rng_ops
= {
47 .read
= bcm2835_rng_read
,
50 static int bcm2835_rng_probe(struct platform_device
*pdev
)
52 struct device
*dev
= &pdev
->dev
;
53 struct device_node
*np
= dev
->of_node
;
54 void __iomem
*rng_base
;
58 rng_base
= of_iomap(np
, 0);
60 dev_err(dev
, "failed to remap rng regs");
63 bcm2835_rng_ops
.priv
= (unsigned long)rng_base
;
66 err
= hwrng_register(&bcm2835_rng_ops
);
68 dev_err(dev
, "hwrng registration failed\n");
71 dev_info(dev
, "hwrng registered\n");
73 /* set warm-up count & enable */
74 __raw_writel(RNG_WARMUP_COUNT
, rng_base
+ RNG_STATUS
);
75 __raw_writel(RNG_RBGEN
, rng_base
+ RNG_CTRL
);
80 static int bcm2835_rng_remove(struct platform_device
*pdev
)
82 void __iomem
*rng_base
= (void __iomem
*)bcm2835_rng_ops
.priv
;
84 /* disable rng hardware */
85 __raw_writel(0, rng_base
+ RNG_CTRL
);
87 /* unregister driver */
88 hwrng_unregister(&bcm2835_rng_ops
);
94 static const struct of_device_id bcm2835_rng_of_match
[] = {
95 { .compatible
= "brcm,bcm2835-rng", },
98 MODULE_DEVICE_TABLE(of
, bcm2835_rng_of_match
);
100 static struct platform_driver bcm2835_rng_driver
= {
102 .name
= "bcm2835-rng",
103 .owner
= THIS_MODULE
,
104 .of_match_table
= bcm2835_rng_of_match
,
106 .probe
= bcm2835_rng_probe
,
107 .remove
= bcm2835_rng_remove
,
109 module_platform_driver(bcm2835_rng_driver
);
111 MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
112 MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
113 MODULE_LICENSE("GPL v2");