1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2015, Daniel Thompson
7 #include <linux/delay.h>
8 #include <linux/hw_random.h>
10 #include <linux/iopoll.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/reset.h>
18 #include <linux/slab.h>
21 #define RNG_CR_RNGEN BIT(2)
22 #define RNG_CR_CED BIT(5)
25 #define RNG_SR_SEIS BIT(6)
26 #define RNG_SR_CEIS BIT(5)
27 #define RNG_SR_DRDY BIT(0)
31 struct stm32_rng_private {
35 struct reset_control *rst;
39 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
41 struct stm32_rng_private *priv =
42 container_of(rng, struct stm32_rng_private, rng);
46 pm_runtime_get_sync((struct device *) priv->rng.priv);
48 while (max >= sizeof(u32)) {
49 sr = readl_relaxed(priv->base + RNG_SR);
50 /* Manage timeout which is based on timer and take */
51 /* care of initial delay time when enabling rng */
55 err = readl_relaxed_poll_timeout_atomic(priv->base
60 dev_err((struct device *)priv->rng.priv,
61 "%s: timeout %x!\n", __func__, sr);
64 /* If error detected or data not ready... */
65 if (sr != RNG_SR_DRDY) {
66 if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
67 "bad RNG status - %x\n", sr))
68 writel_relaxed(0, priv->base + RNG_SR);
72 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
74 retval += sizeof(u32);
79 pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
80 pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
82 return retval || !wait ? retval : -EIO;
85 static int stm32_rng_init(struct hwrng *rng)
87 struct stm32_rng_private *priv =
88 container_of(rng, struct stm32_rng_private, rng);
91 err = clk_prepare_enable(priv->clk);
96 writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
98 writel_relaxed(RNG_CR_RNGEN | RNG_CR_CED,
101 /* clear error indicators */
102 writel_relaxed(0, priv->base + RNG_SR);
107 static void stm32_rng_cleanup(struct hwrng *rng)
109 struct stm32_rng_private *priv =
110 container_of(rng, struct stm32_rng_private, rng);
112 writel_relaxed(0, priv->base + RNG_CR);
113 clk_disable_unprepare(priv->clk);
116 static int stm32_rng_probe(struct platform_device *ofdev)
118 struct device *dev = &ofdev->dev;
119 struct device_node *np = ofdev->dev.of_node;
120 struct stm32_rng_private *priv;
124 priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
128 err = of_address_to_resource(np, 0, &res);
132 priv->base = devm_ioremap_resource(dev, &res);
133 if (IS_ERR(priv->base))
134 return PTR_ERR(priv->base);
136 priv->clk = devm_clk_get(&ofdev->dev, NULL);
137 if (IS_ERR(priv->clk))
138 return PTR_ERR(priv->clk);
140 priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
141 if (!IS_ERR(priv->rst)) {
142 reset_control_assert(priv->rst);
144 reset_control_deassert(priv->rst);
147 priv->ced = of_property_read_bool(np, "clock-error-detect");
149 dev_set_drvdata(dev, priv);
151 priv->rng.name = dev_driver_string(dev);
153 priv->rng.init = stm32_rng_init;
154 priv->rng.cleanup = stm32_rng_cleanup;
156 priv->rng.read = stm32_rng_read;
157 priv->rng.priv = (unsigned long) dev;
158 priv->rng.quality = 900;
160 pm_runtime_set_autosuspend_delay(dev, 100);
161 pm_runtime_use_autosuspend(dev);
162 pm_runtime_enable(dev);
164 return devm_hwrng_register(dev, &priv->rng);
167 static int stm32_rng_remove(struct platform_device *ofdev)
169 pm_runtime_disable(&ofdev->dev);
175 static int stm32_rng_runtime_suspend(struct device *dev)
177 struct stm32_rng_private *priv = dev_get_drvdata(dev);
179 stm32_rng_cleanup(&priv->rng);
184 static int stm32_rng_runtime_resume(struct device *dev)
186 struct stm32_rng_private *priv = dev_get_drvdata(dev);
188 return stm32_rng_init(&priv->rng);
192 static const struct dev_pm_ops stm32_rng_pm_ops = {
193 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
194 stm32_rng_runtime_resume, NULL)
195 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
196 pm_runtime_force_resume)
200 static const struct of_device_id stm32_rng_match[] = {
202 .compatible = "st,stm32-rng",
206 MODULE_DEVICE_TABLE(of, stm32_rng_match);
208 static struct platform_driver stm32_rng_driver = {
211 .pm = &stm32_rng_pm_ops,
212 .of_match_table = stm32_rng_match,
214 .probe = stm32_rng_probe,
215 .remove = stm32_rng_remove,
218 module_platform_driver(stm32_rng_driver);
220 MODULE_LICENSE("GPL");
222 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");