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>
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>
29 #define RNG_CR_RNGEN BIT(2)
30 #define RNG_CR_CED BIT(5)
33 #define RNG_SR_SEIS BIT(6)
34 #define RNG_SR_CEIS BIT(5)
35 #define RNG_SR_DRDY BIT(0)
39 struct stm32_rng_private {
43 struct reset_control *rst;
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);
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 */
61 retval = readl_relaxed_poll_timeout_atomic(priv->base
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);
78 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
80 retval += 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);
97 err = clk_prepare_enable(priv->clk);
102 writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
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);
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;
130 priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
134 err = of_address_to_resource(np, 0, &res);
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);
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),
159 priv->rng.init = stm32_rng_init,
160 priv->rng.cleanup = stm32_rng_cleanup,
162 priv->rng.read = stm32_rng_read,
163 priv->rng.priv = (unsigned long) dev;
164 priv->rng.quality = 900;
166 pm_runtime_set_autosuspend_delay(dev, 100);
167 pm_runtime_use_autosuspend(dev);
168 pm_runtime_enable(dev);
170 return devm_hwrng_register(dev, &priv->rng);
173 static int stm32_rng_remove(struct platform_device *ofdev)
175 pm_runtime_disable(&ofdev->dev);
181 static int stm32_rng_runtime_suspend(struct device *dev)
183 struct stm32_rng_private *priv = dev_get_drvdata(dev);
185 stm32_rng_cleanup(&priv->rng);
190 static int stm32_rng_runtime_resume(struct device *dev)
192 struct stm32_rng_private *priv = dev_get_drvdata(dev);
194 return stm32_rng_init(&priv->rng);
198 static const struct dev_pm_ops stm32_rng_pm_ops = {
199 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
200 stm32_rng_runtime_resume, NULL)
201 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
202 pm_runtime_force_resume)
206 static const struct of_device_id stm32_rng_match[] = {
208 .compatible = "st,stm32-rng",
212 MODULE_DEVICE_TABLE(of, stm32_rng_match);
214 static struct platform_driver stm32_rng_driver = {
217 .pm = &stm32_rng_pm_ops,
218 .of_match_table = stm32_rng_match,
220 .probe = stm32_rng_probe,
221 .remove = stm32_rng_remove,
224 module_platform_driver(stm32_rng_driver);
226 MODULE_LICENSE("GPL");
227 MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
228 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");