arm64: futex: Avoid copying out uninitialised stack in failed cmpxchg()
[linux/fpc-iii.git] / drivers / char / hw_random / iproc-rng200.c
blob8b5a20b3529300f8bf4be914051ffe22c4e8c3c7
1 /*
2 * Copyright (C) 2015 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 * DESCRIPTION: The Broadcom iProc RNG200 Driver
17 #include <linux/hw_random.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of_address.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/delay.h>
27 /* Registers */
28 #define RNG_CTRL_OFFSET 0x00
29 #define RNG_CTRL_RNG_RBGEN_MASK 0x00001FFF
30 #define RNG_CTRL_RNG_RBGEN_ENABLE 0x00000001
31 #define RNG_CTRL_RNG_RBGEN_DISABLE 0x00000000
33 #define RNG_SOFT_RESET_OFFSET 0x04
34 #define RNG_SOFT_RESET 0x00000001
36 #define RBG_SOFT_RESET_OFFSET 0x08
37 #define RBG_SOFT_RESET 0x00000001
39 #define RNG_INT_STATUS_OFFSET 0x18
40 #define RNG_INT_STATUS_MASTER_FAIL_LOCKOUT_IRQ_MASK 0x80000000
41 #define RNG_INT_STATUS_STARTUP_TRANSITIONS_MET_IRQ_MASK 0x00020000
42 #define RNG_INT_STATUS_NIST_FAIL_IRQ_MASK 0x00000020
43 #define RNG_INT_STATUS_TOTAL_BITS_COUNT_IRQ_MASK 0x00000001
45 #define RNG_FIFO_DATA_OFFSET 0x20
47 #define RNG_FIFO_COUNT_OFFSET 0x24
48 #define RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK 0x000000FF
50 struct iproc_rng200_dev {
51 struct hwrng rng;
52 void __iomem *base;
55 #define to_rng_priv(rng) container_of(rng, struct iproc_rng200_dev, rng)
57 static void iproc_rng200_restart(void __iomem *rng_base)
59 uint32_t val;
61 /* Disable RBG */
62 val = ioread32(rng_base + RNG_CTRL_OFFSET);
63 val &= ~RNG_CTRL_RNG_RBGEN_MASK;
64 val |= RNG_CTRL_RNG_RBGEN_DISABLE;
65 iowrite32(val, rng_base + RNG_CTRL_OFFSET);
67 /* Clear all interrupt status */
68 iowrite32(0xFFFFFFFFUL, rng_base + RNG_INT_STATUS_OFFSET);
70 /* Reset RNG and RBG */
71 val = ioread32(rng_base + RBG_SOFT_RESET_OFFSET);
72 val |= RBG_SOFT_RESET;
73 iowrite32(val, rng_base + RBG_SOFT_RESET_OFFSET);
75 val = ioread32(rng_base + RNG_SOFT_RESET_OFFSET);
76 val |= RNG_SOFT_RESET;
77 iowrite32(val, rng_base + RNG_SOFT_RESET_OFFSET);
79 val = ioread32(rng_base + RNG_SOFT_RESET_OFFSET);
80 val &= ~RNG_SOFT_RESET;
81 iowrite32(val, rng_base + RNG_SOFT_RESET_OFFSET);
83 val = ioread32(rng_base + RBG_SOFT_RESET_OFFSET);
84 val &= ~RBG_SOFT_RESET;
85 iowrite32(val, rng_base + RBG_SOFT_RESET_OFFSET);
87 /* Enable RBG */
88 val = ioread32(rng_base + RNG_CTRL_OFFSET);
89 val &= ~RNG_CTRL_RNG_RBGEN_MASK;
90 val |= RNG_CTRL_RNG_RBGEN_ENABLE;
91 iowrite32(val, rng_base + RNG_CTRL_OFFSET);
94 static int iproc_rng200_read(struct hwrng *rng, void *buf, size_t max,
95 bool wait)
97 struct iproc_rng200_dev *priv = to_rng_priv(rng);
98 uint32_t num_remaining = max;
99 uint32_t status;
101 #define MAX_RESETS_PER_READ 1
102 uint32_t num_resets = 0;
104 #define MAX_IDLE_TIME (1 * HZ)
105 unsigned long idle_endtime = jiffies + MAX_IDLE_TIME;
107 while ((num_remaining > 0) && time_before(jiffies, idle_endtime)) {
109 /* Is RNG sane? If not, reset it. */
110 status = ioread32(priv->base + RNG_INT_STATUS_OFFSET);
111 if ((status & (RNG_INT_STATUS_MASTER_FAIL_LOCKOUT_IRQ_MASK |
112 RNG_INT_STATUS_NIST_FAIL_IRQ_MASK)) != 0) {
114 if (num_resets >= MAX_RESETS_PER_READ)
115 return max - num_remaining;
117 iproc_rng200_restart(priv->base);
118 num_resets++;
121 /* Are there any random numbers available? */
122 if ((ioread32(priv->base + RNG_FIFO_COUNT_OFFSET) &
123 RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK) > 0) {
125 if (num_remaining >= sizeof(uint32_t)) {
126 /* Buffer has room to store entire word */
127 *(uint32_t *)buf = ioread32(priv->base +
128 RNG_FIFO_DATA_OFFSET);
129 buf += sizeof(uint32_t);
130 num_remaining -= sizeof(uint32_t);
131 } else {
132 /* Buffer can only store partial word */
133 uint32_t rnd_number = ioread32(priv->base +
134 RNG_FIFO_DATA_OFFSET);
135 memcpy(buf, &rnd_number, num_remaining);
136 buf += num_remaining;
137 num_remaining = 0;
140 /* Reset the IDLE timeout */
141 idle_endtime = jiffies + MAX_IDLE_TIME;
142 } else {
143 if (!wait)
144 /* Cannot wait, return immediately */
145 return max - num_remaining;
147 /* Can wait, give others chance to run */
148 usleep_range(min(num_remaining * 10, 500U), 500);
152 return max - num_remaining;
155 static int iproc_rng200_init(struct hwrng *rng)
157 struct iproc_rng200_dev *priv = to_rng_priv(rng);
158 uint32_t val;
160 /* Setup RNG. */
161 val = ioread32(priv->base + RNG_CTRL_OFFSET);
162 val &= ~RNG_CTRL_RNG_RBGEN_MASK;
163 val |= RNG_CTRL_RNG_RBGEN_ENABLE;
164 iowrite32(val, priv->base + RNG_CTRL_OFFSET);
166 return 0;
169 static void iproc_rng200_cleanup(struct hwrng *rng)
171 struct iproc_rng200_dev *priv = to_rng_priv(rng);
172 uint32_t val;
174 /* Disable RNG hardware */
175 val = ioread32(priv->base + RNG_CTRL_OFFSET);
176 val &= ~RNG_CTRL_RNG_RBGEN_MASK;
177 val |= RNG_CTRL_RNG_RBGEN_DISABLE;
178 iowrite32(val, priv->base + RNG_CTRL_OFFSET);
181 static int iproc_rng200_probe(struct platform_device *pdev)
183 struct iproc_rng200_dev *priv;
184 struct resource *res;
185 struct device *dev = &pdev->dev;
186 int ret;
188 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
189 if (!priv)
190 return -ENOMEM;
192 /* Map peripheral */
193 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
194 if (!res) {
195 dev_err(dev, "failed to get rng resources\n");
196 return -EINVAL;
199 priv->base = devm_ioremap_resource(dev, res);
200 if (IS_ERR(priv->base)) {
201 dev_err(dev, "failed to remap rng regs\n");
202 return PTR_ERR(priv->base);
205 priv->rng.name = "iproc-rng200",
206 priv->rng.read = iproc_rng200_read,
207 priv->rng.init = iproc_rng200_init,
208 priv->rng.cleanup = iproc_rng200_cleanup,
210 /* Register driver */
211 ret = devm_hwrng_register(dev, &priv->rng);
212 if (ret) {
213 dev_err(dev, "hwrng registration failed\n");
214 return ret;
217 dev_info(dev, "hwrng registered\n");
219 return 0;
222 static const struct of_device_id iproc_rng200_of_match[] = {
223 { .compatible = "brcm,bcm7278-rng200", },
224 { .compatible = "brcm,iproc-rng200", },
227 MODULE_DEVICE_TABLE(of, iproc_rng200_of_match);
229 static struct platform_driver iproc_rng200_driver = {
230 .driver = {
231 .name = "iproc-rng200",
232 .of_match_table = iproc_rng200_of_match,
234 .probe = iproc_rng200_probe,
236 module_platform_driver(iproc_rng200_driver);
238 MODULE_AUTHOR("Broadcom");
239 MODULE_DESCRIPTION("iProc RNG200 Random Number Generator driver");
240 MODULE_LICENSE("GPL v2");