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>
13 #include <linux/of_address.h>
14 #include <linux/of_platform.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/slab.h>
20 #define RNG_CR_RNGEN BIT(2)
21 #define RNG_CR_CED BIT(5)
24 #define RNG_SR_SEIS BIT(6)
25 #define RNG_SR_CEIS BIT(5)
26 #define RNG_SR_DRDY BIT(0)
30 struct stm32_rng_private {
34 struct reset_control *rst;
38 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
40 struct stm32_rng_private *priv =
41 container_of(rng, struct stm32_rng_private, rng);
45 pm_runtime_get_sync((struct device *) priv->rng.priv);
47 while (max >= sizeof(u32)) {
48 sr = readl_relaxed(priv->base + RNG_SR);
49 /* Manage timeout which is based on timer and take */
50 /* care of initial delay time when enabling rng */
54 err = readl_relaxed_poll_timeout_atomic(priv->base
59 dev_err((struct device *)priv->rng.priv,
60 "%s: timeout %x!\n", __func__, sr);
63 /* If error detected or data not ready... */
64 if (sr != RNG_SR_DRDY) {
65 if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
66 "bad RNG status - %x\n", sr))
67 writel_relaxed(0, priv->base + RNG_SR);
71 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
73 retval += sizeof(u32);
78 pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
79 pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
81 return retval || !wait ? retval : -EIO;
84 static int stm32_rng_init(struct hwrng *rng)
86 struct stm32_rng_private *priv =
87 container_of(rng, struct stm32_rng_private, rng);
90 err = clk_prepare_enable(priv->clk);
95 writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
97 writel_relaxed(RNG_CR_RNGEN | RNG_CR_CED,
100 /* clear error indicators */
101 writel_relaxed(0, priv->base + RNG_SR);
106 static void stm32_rng_cleanup(struct hwrng *rng)
108 struct stm32_rng_private *priv =
109 container_of(rng, struct stm32_rng_private, rng);
111 writel_relaxed(0, priv->base + RNG_CR);
112 clk_disable_unprepare(priv->clk);
115 static int stm32_rng_probe(struct platform_device *ofdev)
117 struct device *dev = &ofdev->dev;
118 struct device_node *np = ofdev->dev.of_node;
119 struct stm32_rng_private *priv;
123 priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
127 err = of_address_to_resource(np, 0, &res);
131 priv->base = devm_ioremap_resource(dev, &res);
132 if (IS_ERR(priv->base))
133 return PTR_ERR(priv->base);
135 priv->clk = devm_clk_get(&ofdev->dev, NULL);
136 if (IS_ERR(priv->clk))
137 return PTR_ERR(priv->clk);
139 priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
140 if (!IS_ERR(priv->rst)) {
141 reset_control_assert(priv->rst);
143 reset_control_deassert(priv->rst);
146 priv->ced = of_property_read_bool(np, "clock-error-detect");
148 dev_set_drvdata(dev, priv);
150 priv->rng.name = dev_driver_string(dev);
152 priv->rng.init = stm32_rng_init;
153 priv->rng.cleanup = stm32_rng_cleanup;
155 priv->rng.read = stm32_rng_read;
156 priv->rng.priv = (unsigned long) dev;
157 priv->rng.quality = 900;
159 pm_runtime_set_autosuspend_delay(dev, 100);
160 pm_runtime_use_autosuspend(dev);
161 pm_runtime_enable(dev);
163 return devm_hwrng_register(dev, &priv->rng);
166 static int stm32_rng_remove(struct platform_device *ofdev)
168 pm_runtime_disable(&ofdev->dev);
174 static int stm32_rng_runtime_suspend(struct device *dev)
176 struct stm32_rng_private *priv = dev_get_drvdata(dev);
178 stm32_rng_cleanup(&priv->rng);
183 static int stm32_rng_runtime_resume(struct device *dev)
185 struct stm32_rng_private *priv = dev_get_drvdata(dev);
187 return stm32_rng_init(&priv->rng);
191 static const struct dev_pm_ops stm32_rng_pm_ops = {
192 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
193 stm32_rng_runtime_resume, NULL)
194 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
195 pm_runtime_force_resume)
199 static const struct of_device_id stm32_rng_match[] = {
201 .compatible = "st,stm32-rng",
205 MODULE_DEVICE_TABLE(of, stm32_rng_match);
207 static struct platform_driver stm32_rng_driver = {
210 .pm = &stm32_rng_pm_ops,
211 .of_match_table = stm32_rng_match,
213 .probe = stm32_rng_probe,
214 .remove = stm32_rng_remove,
217 module_platform_driver(stm32_rng_driver);
219 MODULE_LICENSE("GPL");
221 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");