kernel: update 3.9 to 3.9.11
[openwrt/svn-archive/archive.git] / target / linux / brcm2708 / patches-3.10 / 007-bcm2708_hw_random-driver.patch
1 diff -urN linux-3.10/drivers/char/hw_random/bcm2708-rng.c linux-rpi-3.10.y/drivers/char/hw_random/bcm2708-rng.c
2 --- linux-3.10/drivers/char/hw_random/bcm2708-rng.c 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-rpi-3.10.y/drivers/char/hw_random/bcm2708-rng.c 2013-07-06 15:25:50.000000000 +0100
4 @@ -0,0 +1,117 @@
5 +/**
6 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
7 + *
8 + * Redistribution and use in source and binary forms, with or without
9 + * modification, are permitted provided that the following conditions
10 + * are met:
11 + * 1. Redistributions of source code must retain the above copyright
12 + * notice, this list of conditions, and the following disclaimer,
13 + * without modification.
14 + * 2. Redistributions in binary form must reproduce the above copyright
15 + * notice, this list of conditions and the following disclaimer in the
16 + * documentation and/or other materials provided with the distribution.
17 + * 3. The names of the above-listed copyright holders may not be used
18 + * to endorse or promote products derived from this software without
19 + * specific prior written permission.
20 + *
21 + * ALTERNATIVELY, this software may be distributed under the terms of the
22 + * GNU General Public License ("GPL") version 2, as published by the Free
23 + * Software Foundation.
24 + *
25 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 + */
37 +
38 +#include <linux/kernel.h>
39 +#include <linux/module.h>
40 +#include <linux/init.h>
41 +#include <linux/hw_random.h>
42 +#include <linux/printk.h>
43 +
44 +#include <asm/io.h>
45 +#include <mach/hardware.h>
46 +#include <mach/platform.h>
47 +
48 +#define RNG_CTRL (0x0)
49 +#define RNG_STATUS (0x4)
50 +#define RNG_DATA (0x8)
51 +#define RNG_FF_THRESHOLD (0xc)
52 +
53 +/* enable rng */
54 +#define RNG_RBGEN 0x1
55 +/* double speed, less random mode */
56 +#define RNG_RBG2X 0x2
57 +
58 +/* the initial numbers generated are "less random" so will be discarded */
59 +#define RNG_WARMUP_COUNT 0x40000
60 +
61 +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
62 +{
63 + void __iomem *rng_base = (void __iomem *)rng->priv;
64 + unsigned words;
65 + /* wait for a random number to be in fifo */
66 + do {
67 + words = __raw_readl(rng_base + RNG_STATUS)>>24;
68 + }
69 + while (words == 0);
70 + /* read the random number */
71 + *buffer = __raw_readl(rng_base + RNG_DATA);
72 + return 4;
73 +}
74 +
75 +static struct hwrng bcm2708_rng_ops = {
76 + .name = "bcm2708",
77 + .data_read = bcm2708_rng_data_read,
78 +};
79 +
80 +static int __init bcm2708_rng_init(void)
81 +{
82 + void __iomem *rng_base;
83 + int err;
84 +
85 + /* map peripheral */
86 + rng_base = ioremap(RNG_BASE, 0x10);
87 + pr_info("bcm2708_rng_init=%p\n", rng_base);
88 + if (!rng_base) {
89 + pr_err("bcm2708_rng_init failed to ioremap\n");
90 + return -ENOMEM;
91 + }
92 + bcm2708_rng_ops.priv = (unsigned long)rng_base;
93 + /* register driver */
94 + err = hwrng_register(&bcm2708_rng_ops);
95 + if (err) {
96 + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
97 + iounmap(rng_base);
98 + } else {
99 + /* set warm-up count & enable */
100 + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
101 + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
102 + }
103 + return err;
104 +}
105 +
106 +static void __exit bcm2708_rng_exit(void)
107 +{
108 + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
109 + pr_info("bcm2708_rng_exit\n");
110 + /* disable rng hardware */
111 + __raw_writel(0, rng_base + RNG_CTRL);
112 + /* unregister driver */
113 + hwrng_unregister(&bcm2708_rng_ops);
114 + iounmap(rng_base);
115 +}
116 +
117 +module_init(bcm2708_rng_init);
118 +module_exit(bcm2708_rng_exit);
119 +
120 +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
121 +MODULE_LICENSE("GPL and additional rights");
122 diff -urN linux-3.10/drivers/char/hw_random/Kconfig linux-rpi-3.10.y/drivers/char/hw_random/Kconfig
123 --- linux-3.10/drivers/char/hw_random/Kconfig 2013-06-30 23:13:29.000000000 +0100
124 +++ linux-rpi-3.10.y/drivers/char/hw_random/Kconfig 2013-07-06 15:25:50.000000000 +0100
125 @@ -314,3 +314,15 @@
126 module will be called tpm-rng.
127
128 If unsure, say Y.
129 +
130 +config HW_RANDOM_BCM2708
131 + tristate "BCM2708 generic true random number generator support"
132 + depends on HW_RANDOM && ARCH_BCM2708
133 + ---help---
134 + This driver provides the kernel-side support for the BCM2708 hardware.
135 +
136 + To compile this driver as a module, choose M here: the
137 + module will be called bcm2708-rng.
138 +
139 + If unsure, say N.
140 +
141 diff -urN linux-3.10/drivers/char/hw_random/Makefile linux-rpi-3.10.y/drivers/char/hw_random/Makefile
142 --- linux-3.10/drivers/char/hw_random/Makefile 2013-06-30 23:13:29.000000000 +0100
143 +++ linux-rpi-3.10.y/drivers/char/hw_random/Makefile 2013-07-06 15:25:50.000000000 +0100
144 @@ -27,3 +27,4 @@
145 obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
146 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
147 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
148 +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o