GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / char / hw_random / stm32-rng.c
1 /*
2  * Copyright (c) 2015, Daniel Thompson
3  *
4  * This file is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/hw_random.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_platform.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/reset.h>
25 #include <linux/slab.h>
26
27 #define RNG_CR 0x00
28 #define RNG_CR_RNGEN BIT(2)
29
30 #define RNG_SR 0x04
31 #define RNG_SR_SEIS BIT(6)
32 #define RNG_SR_CEIS BIT(5)
33 #define RNG_SR_DRDY BIT(0)
34
35 #define RNG_DR 0x08
36
37 /*
38  * It takes 40 cycles @ 48MHz to generate each random number (e.g. <1us).
39  * At the time of writing STM32 parts max out at ~200MHz meaning a timeout
40  * of 500 leaves us a very comfortable margin for error. The loop to which
41  * the timeout applies takes at least 4 instructions per iteration so the
42  * timeout is enough to take us up to multi-GHz parts!
43  */
44 #define RNG_TIMEOUT 500
45
46 struct stm32_rng_private {
47         struct hwrng rng;
48         void __iomem *base;
49         struct clk *clk;
50         struct reset_control *rst;
51 };
52
53 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
54 {
55         struct stm32_rng_private *priv =
56             container_of(rng, struct stm32_rng_private, rng);
57         u32 sr;
58         int retval = 0;
59
60         pm_runtime_get_sync((struct device *) priv->rng.priv);
61
62         while (max > sizeof(u32)) {
63                 sr = readl_relaxed(priv->base + RNG_SR);
64                 if (!sr && wait) {
65                         unsigned int timeout = RNG_TIMEOUT;
66
67                         do {
68                                 cpu_relax();
69                                 sr = readl_relaxed(priv->base + RNG_SR);
70                         } while (!sr && --timeout);
71                 }
72
73                 /* If error detected or data not ready... */
74                 if (sr != RNG_SR_DRDY) {
75                         if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
76                                         "bad RNG status - %x\n", sr))
77                                 writel_relaxed(0, priv->base + RNG_SR);
78                         break;
79                 }
80
81                 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
82
83                 retval += sizeof(u32);
84                 data += sizeof(u32);
85                 max -= sizeof(u32);
86         }
87
88         pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
89         pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
90
91         return retval || !wait ? retval : -EIO;
92 }
93
94 static int stm32_rng_init(struct hwrng *rng)
95 {
96         struct stm32_rng_private *priv =
97             container_of(rng, struct stm32_rng_private, rng);
98         int err;
99
100         err = clk_prepare_enable(priv->clk);
101         if (err)
102                 return err;
103
104         writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
105
106         /* clear error indicators */
107         writel_relaxed(0, priv->base + RNG_SR);
108
109         return 0;
110 }
111
112 static void stm32_rng_cleanup(struct hwrng *rng)
113 {
114         struct stm32_rng_private *priv =
115             container_of(rng, struct stm32_rng_private, rng);
116
117         writel_relaxed(0, priv->base + RNG_CR);
118         clk_disable_unprepare(priv->clk);
119 }
120
121 static int stm32_rng_probe(struct platform_device *ofdev)
122 {
123         struct device *dev = &ofdev->dev;
124         struct device_node *np = ofdev->dev.of_node;
125         struct stm32_rng_private *priv;
126         struct resource res;
127         int err;
128
129         priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
130         if (!priv)
131                 return -ENOMEM;
132
133         err = of_address_to_resource(np, 0, &res);
134         if (err)
135                 return err;
136
137         priv->base = devm_ioremap_resource(dev, &res);
138         if (IS_ERR(priv->base))
139                 return PTR_ERR(priv->base);
140
141         priv->clk = devm_clk_get(&ofdev->dev, NULL);
142         if (IS_ERR(priv->clk))
143                 return PTR_ERR(priv->clk);
144
145         priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
146         if (!IS_ERR(priv->rst)) {
147                 reset_control_assert(priv->rst);
148                 udelay(2);
149                 reset_control_deassert(priv->rst);
150         }
151
152         dev_set_drvdata(dev, priv);
153
154         priv->rng.name = dev_driver_string(dev),
155 #ifndef CONFIG_PM
156         priv->rng.init = stm32_rng_init,
157         priv->rng.cleanup = stm32_rng_cleanup,
158 #endif
159         priv->rng.read = stm32_rng_read,
160         priv->rng.priv = (unsigned long) dev;
161
162         pm_runtime_set_autosuspend_delay(dev, 100);
163         pm_runtime_use_autosuspend(dev);
164         pm_runtime_enable(dev);
165
166         return devm_hwrng_register(dev, &priv->rng);
167 }
168
169 static int stm32_rng_remove(struct platform_device *ofdev)
170 {
171         pm_runtime_disable(&ofdev->dev);
172
173         return 0;
174 }
175
176 #ifdef CONFIG_PM
177 static int stm32_rng_runtime_suspend(struct device *dev)
178 {
179         struct stm32_rng_private *priv = dev_get_drvdata(dev);
180
181         stm32_rng_cleanup(&priv->rng);
182
183         return 0;
184 }
185
186 static int stm32_rng_runtime_resume(struct device *dev)
187 {
188         struct stm32_rng_private *priv = dev_get_drvdata(dev);
189
190         return stm32_rng_init(&priv->rng);
191 }
192 #endif
193
194 static UNIVERSAL_DEV_PM_OPS(stm32_rng_pm_ops, stm32_rng_runtime_suspend,
195                             stm32_rng_runtime_resume, NULL);
196
197 static const struct of_device_id stm32_rng_match[] = {
198         {
199                 .compatible = "st,stm32-rng",
200         },
201         {},
202 };
203 MODULE_DEVICE_TABLE(of, stm32_rng_match);
204
205 static struct platform_driver stm32_rng_driver = {
206         .driver = {
207                 .name = "stm32-rng",
208                 .pm = &stm32_rng_pm_ops,
209                 .of_match_table = stm32_rng_match,
210         },
211         .probe = stm32_rng_probe,
212         .remove = stm32_rng_remove,
213 };
214
215 module_platform_driver(stm32_rng_driver);
216
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
219 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");