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