Skip to content

Commit 8d7ea27

Browse files
committed
Add hwrng (hardware random number generator) driver
1 parent 5e7307f commit 8d7ea27

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

‎arch/arm/mach-bcm2708/include/mach/platform.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
6161
#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
6262
#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
63+
#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
6364
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
6465
#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
6566
#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */

‎drivers/char/hw_random/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ config HW_RANDOM_TPM
341341

342342
If unsure, say Y.
343343

344+
config HW_RANDOM_BCM2708
345+
tristate "BCM2708 generic true random number generator support"
346+
depends on HW_RANDOM && ARCH_BCM2708
347+
---help---
348+
This driver provides the kernel-side support for the BCM2708 hardware.
349+
350+
To compile this driver as a module, choose M here: the
351+
module will be called bcm2708-rng.
352+
353+
If unsure, say N.
354+
344355
config HW_RANDOM_MSM
345356
tristate "Qualcomm MSM Random Number Generator support"
346357
depends on HW_RANDOM && ARCH_MSM

‎drivers/char/hw_random/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
2929
obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
3030
obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
3131
obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
32+
obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
3233
obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright (c) 2010-2012 Broadcom. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions, and the following disclaimer,
9+
* without modification.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. The names of the above-listed copyright holders may not be used
14+
* to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* ALTERNATIVELY, this software may be distributed under the terms of the
18+
* GNU General Public License ("GPL") version 2, as published by the Free
19+
* Software Foundation.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*/
33+
34+
#include <linux/kernel.h>
35+
#include <linux/module.h>
36+
#include <linux/init.h>
37+
#include <linux/hw_random.h>
38+
#include <linux/printk.h>
39+
40+
#include <asm/io.h>
41+
#include <mach/hardware.h>
42+
#include <mach/platform.h>
43+
44+
#define RNG_CTRL (0x0)
45+
#define RNG_STATUS (0x4)
46+
#define RNG_DATA (0x8)
47+
#define RNG_FF_THRESHOLD (0xc)
48+
49+
/* enable rng */
50+
#define RNG_RBGEN 0x1
51+
/* double speed, less random mode */
52+
#define RNG_RBG2X 0x2
53+
54+
/* the initial numbers generated are "less random" so will be discarded */
55+
#define RNG_WARMUP_COUNT 0x40000
56+
57+
static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
58+
{
59+
void __iomem *rng_base = (void __iomem *)rng->priv;
60+
unsigned words;
61+
/* wait for a random number to be in fifo */
62+
do {
63+
words = __raw_readl(rng_base + RNG_STATUS)>>24;
64+
}
65+
while (words == 0);
66+
/* read the random number */
67+
*buffer = __raw_readl(rng_base + RNG_DATA);
68+
return 4;
69+
}
70+
71+
static struct hwrng bcm2708_rng_ops = {
72+
.name = "bcm2708",
73+
.data_read = bcm2708_rng_data_read,
74+
};
75+
76+
static int __init bcm2708_rng_init(void)
77+
{
78+
void __iomem *rng_base;
79+
int err;
80+
81+
/* map peripheral */
82+
rng_base = ioremap(RNG_BASE, 0x10);
83+
pr_info("bcm2708_rng_init=%p\n", rng_base);
84+
if (!rng_base) {
85+
pr_err("bcm2708_rng_init failed to ioremap\n");
86+
return -ENOMEM;
87+
}
88+
bcm2708_rng_ops.priv = (unsigned long)rng_base;
89+
/* register driver */
90+
err = hwrng_register(&bcm2708_rng_ops);
91+
if (err) {
92+
pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
93+
iounmap(rng_base);
94+
} else {
95+
/* set warm-up count & enable */
96+
__raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
97+
__raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
98+
}
99+
return err;
100+
}
101+
102+
static void __exit bcm2708_rng_exit(void)
103+
{
104+
void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
105+
pr_info("bcm2708_rng_exit\n");
106+
/* disable rng hardware */
107+
__raw_writel(0, rng_base + RNG_CTRL);
108+
/* unregister driver */
109+
hwrng_unregister(&bcm2708_rng_ops);
110+
iounmap(rng_base);
111+
}
112+
113+
module_init(bcm2708_rng_init);
114+
module_exit(bcm2708_rng_exit);
115+
116+
MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
117+
MODULE_LICENSE("GPL and additional rights");

0 commit comments

Comments
 (0)