arm64: futex: Avoid copying out uninitialised stack in failed cmpxchg()
[linux/fpc-iii.git] / drivers / char / hw_random / stm32-rng.c
blob042860d97b1560a1943b9c28e57d48c1e77536b8
1 /*
2 * Copyright (c) 2015, Daniel Thompson
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.
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.
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/hw_random.h>
18 #include <linux/io.h>
19 #include <linux/iopoll.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/pm_runtime.h>
25 #include <linux/reset.h>
26 #include <linux/slab.h>
28 #define RNG_CR 0x00
29 #define RNG_CR_RNGEN BIT(2)
30 #define RNG_CR_CED BIT(5)
32 #define RNG_SR 0x04
33 #define RNG_SR_SEIS BIT(6)
34 #define RNG_SR_CEIS BIT(5)
35 #define RNG_SR_DRDY BIT(0)
37 #define RNG_DR 0x08
39 struct stm32_rng_private {
40 struct hwrng rng;
41 void __iomem *base;
42 struct clk *clk;
43 struct reset_control *rst;
44 bool ced;
47 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
49 struct stm32_rng_private *priv =
50 container_of(rng, struct stm32_rng_private, rng);
51 u32 sr;
52 int retval = 0;
54 pm_runtime_get_sync((struct device *) priv->rng.priv);
56 while (max > sizeof(u32)) {
57 sr = readl_relaxed(priv->base + RNG_SR);
58 /* Manage timeout which is based on timer and take */
59 /* care of initial delay time when enabling rng */
60 if (!sr && wait) {
61 retval = readl_relaxed_poll_timeout_atomic(priv->base
62 + RNG_SR,
63 sr, sr,
64 10, 50000);
65 if (retval)
66 dev_err((struct device *)priv->rng.priv,
67 "%s: timeout %x!\n", __func__, sr);
70 /* If error detected or data not ready... */
71 if (sr != RNG_SR_DRDY) {
72 if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
73 "bad RNG status - %x\n", sr))
74 writel_relaxed(0, priv->base + RNG_SR);
75 break;
78 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
80 retval += sizeof(u32);
81 data += sizeof(u32);
82 max -= sizeof(u32);
85 pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
86 pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
88 return retval || !wait ? retval : -EIO;
91 static int stm32_rng_init(struct hwrng *rng)
93 struct stm32_rng_private *priv =
94 container_of(rng, struct stm32_rng_private, rng);
95 int err;
97 err = clk_prepare_enable(priv->clk);
98 if (err)
99 return err;
101 if (priv->ced)
102 writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
103 else
104 writel_relaxed(RNG_CR_RNGEN | RNG_CR_CED,
105 priv->base + RNG_CR);
107 /* clear error indicators */
108 writel_relaxed(0, priv->base + RNG_SR);
110 return 0;
113 static void stm32_rng_cleanup(struct hwrng *rng)
115 struct stm32_rng_private *priv =
116 container_of(rng, struct stm32_rng_private, rng);
118 writel_relaxed(0, priv->base + RNG_CR);
119 clk_disable_unprepare(priv->clk);
122 static int stm32_rng_probe(struct platform_device *ofdev)
124 struct device *dev = &ofdev->dev;
125 struct device_node *np = ofdev->dev.of_node;
126 struct stm32_rng_private *priv;
127 struct resource res;
128 int err;
130 priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
131 if (!priv)
132 return -ENOMEM;
134 err = of_address_to_resource(np, 0, &res);
135 if (err)
136 return err;
138 priv->base = devm_ioremap_resource(dev, &res);
139 if (IS_ERR(priv->base))
140 return PTR_ERR(priv->base);
142 priv->clk = devm_clk_get(&ofdev->dev, NULL);
143 if (IS_ERR(priv->clk))
144 return PTR_ERR(priv->clk);
146 priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
147 if (!IS_ERR(priv->rst)) {
148 reset_control_assert(priv->rst);
149 udelay(2);
150 reset_control_deassert(priv->rst);
153 priv->ced = of_property_read_bool(np, "clock-error-detect");
155 dev_set_drvdata(dev, priv);
157 priv->rng.name = dev_driver_string(dev),
158 #ifndef CONFIG_PM
159 priv->rng.init = stm32_rng_init,
160 priv->rng.cleanup = stm32_rng_cleanup,
161 #endif
162 priv->rng.read = stm32_rng_read,
163 priv->rng.priv = (unsigned long) dev;
165 pm_runtime_set_autosuspend_delay(dev, 100);
166 pm_runtime_use_autosuspend(dev);
167 pm_runtime_enable(dev);
169 return devm_hwrng_register(dev, &priv->rng);
172 #ifdef CONFIG_PM
173 static int stm32_rng_runtime_suspend(struct device *dev)
175 struct stm32_rng_private *priv = dev_get_drvdata(dev);
177 stm32_rng_cleanup(&priv->rng);
179 return 0;
182 static int stm32_rng_runtime_resume(struct device *dev)
184 struct stm32_rng_private *priv = dev_get_drvdata(dev);
186 return stm32_rng_init(&priv->rng);
188 #endif
190 static const struct dev_pm_ops stm32_rng_pm_ops = {
191 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
192 stm32_rng_runtime_resume, NULL)
193 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
194 pm_runtime_force_resume)
198 static const struct of_device_id stm32_rng_match[] = {
200 .compatible = "st,stm32-rng",
204 MODULE_DEVICE_TABLE(of, stm32_rng_match);
206 static struct platform_driver stm32_rng_driver = {
207 .driver = {
208 .name = "stm32-rng",
209 .pm = &stm32_rng_pm_ops,
210 .of_match_table = stm32_rng_match,
212 .probe = stm32_rng_probe,
215 module_platform_driver(stm32_rng_driver);
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
219 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");