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