summaryrefslogtreecommitdiff
path: root/target/linux/brcm2708/patches-3.14/0011-Add-hwrng-hardware-random-number-generator-driver.patch
blob: 3a68ab2778838b27630d719aed044d37806fa1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
From 2bae9a662d169ecb7d3e1c39ebbcbc80b7dc845f Mon Sep 17 00:00:00 2001
From: popcornmix <popcornmix@gmail.com>
Date: Wed, 3 Jul 2013 00:51:55 +0100
Subject: [PATCH 11/54] Add hwrng (hardware random number generator) driver

---
 arch/arm/mach-bcm2708/include/mach/platform.h |   1 +
 drivers/char/hw_random/Kconfig                |  11 +++
 drivers/char/hw_random/Makefile               |   1 +
 drivers/char/hw_random/bcm2708-rng.c          | 117 ++++++++++++++++++++++++++
 4 files changed, 130 insertions(+)
 create mode 100755 drivers/char/hw_random/bcm2708-rng.c

--- a/arch/arm/mach-bcm2708/include/mach/platform.h
+++ b/arch/arm/mach-bcm2708/include/mach/platform.h
@@ -60,6 +60,7 @@
 #define DMA_BASE		 (BCM2708_PERI_BASE + 0x7000)	/* DMA controller */
 #define ARM_BASE                 (BCM2708_PERI_BASE + 0xB000)	 /* BCM2708 ARM control block */
 #define PM_BASE			 (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
+#define RNG_BASE                 (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
 #define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO */
 #define UART0_BASE               (BCM2708_PERI_BASE + 0x201000)	/* Uart 0 */
 #define MMCI0_BASE               (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -341,6 +341,17 @@ config HW_RANDOM_TPM
 
 	  If unsure, say Y.
 
+config HW_RANDOM_BCM2708
+	tristate "BCM2708 generic true random number generator support"
+	depends on HW_RANDOM && ARCH_BCM2708
+	---help---
+	 This driver provides the kernel-side support for the BCM2708 hardware.
+
+	 To compile this driver as a module, choose M here: the
+	 module will be called bcm2708-rng.
+
+	 If unsure, say N.
+
 config HW_RANDOM_MSM
 	tristate "Qualcomm MSM Random Number Generator support"
 	depends on HW_RANDOM && ARCH_MSM
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -29,4 +29,5 @@ obj-$(CONFIG_HW_RANDOM_POWERNV) += power
 obj-$(CONFIG_HW_RANDOM_EXYNOS)	+= exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
+obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
 obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
--- /dev/null
+++ b/drivers/char/hw_random/bcm2708-rng.c
@@ -0,0 +1,117 @@
+/**
+ * Copyright (c) 2010-2012 Broadcom. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The names of the above-listed copyright holders may not be used
+ *    to endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * ALTERNATIVELY, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2, as published by the Free
+ * Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/hw_random.h>
+#include <linux/printk.h>
+
+#include <asm/io.h>
+#include <mach/hardware.h>
+#include <mach/platform.h>
+
+#define RNG_CTRL		(0x0)
+#define RNG_STATUS		(0x4)
+#define RNG_DATA		(0x8)
+#define RNG_FF_THRESHOLD	(0xc)
+
+/* enable rng */
+#define RNG_RBGEN               0x1
+/* double speed, less random mode */
+#define RNG_RBG2X               0x2
+
+/* the initial numbers generated are "less random" so will be discarded */
+#define RNG_WARMUP_COUNT      0x40000
+
+static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
+{
+	void __iomem *rng_base = (void __iomem *)rng->priv;
+	unsigned words;
+	/* wait for a random number to be in fifo */
+        do {
+		words = __raw_readl(rng_base + RNG_STATUS)>>24;
+	}
+        while (words == 0);
+	/* read the random number */
+	*buffer = __raw_readl(rng_base + RNG_DATA);
+	return 4;
+}
+
+static struct hwrng bcm2708_rng_ops = {
+	.name		= "bcm2708",
+	.data_read	= bcm2708_rng_data_read,
+};
+
+static int __init bcm2708_rng_init(void)
+{
+	void __iomem *rng_base;
+	int err;
+
+	/* map peripheral */
+	rng_base = ioremap(RNG_BASE, 0x10);
+	pr_info("bcm2708_rng_init=%p\n", rng_base);
+	if (!rng_base) {
+		pr_err("bcm2708_rng_init failed to ioremap\n");
+		return -ENOMEM;
+	}
+	bcm2708_rng_ops.priv = (unsigned long)rng_base;
+	/* register driver */
+	err = hwrng_register(&bcm2708_rng_ops);
+	if (err) {
+		pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
+		iounmap(rng_base);
+	} else {
+		/* set warm-up count & enable */
+		__raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
+		__raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
+	}
+	return err;
+}
+
+static void __exit bcm2708_rng_exit(void)
+{
+	void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
+	pr_info("bcm2708_rng_exit\n");
+	/* disable rng hardware */
+	__raw_writel(0, rng_base + RNG_CTRL);
+	/* unregister driver */
+	hwrng_unregister(&bcm2708_rng_ops);
+	iounmap(rng_base);
+}
+
+module_init(bcm2708_rng_init);
+module_exit(bcm2708_rng_exit);
+
+MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
+MODULE_LICENSE("GPL and additional rights");