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)
23 #define RNG_CR_CONFIG1 GENMASK(11, 8)
24 #define RNG_CR_NISTC BIT(12)
25 #define RNG_CR_CONFIG2 GENMASK(15, 13)
26 #define RNG_CR_CLKDIV_SHIFT 16
27 #define RNG_CR_CLKDIV GENMASK(19, 16)
28 #define RNG_CR_CONFIG3 GENMASK(25, 20)
29 #define RNG_CR_CONDRST BIT(30)
30 #define RNG_CR_CONFLOCK BIT(31)
31 #define RNG_CR_ENTROPY_SRC_MASK (RNG_CR_CONFIG1 | RNG_CR_NISTC | RNG_CR_CONFIG2 | RNG_CR_CONFIG3)
32 #define RNG_CR_CONFIG_MASK (RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED | RNG_CR_CLKDIV)
35 #define RNG_SR_DRDY BIT(0)
36 #define RNG_SR_CECS BIT(1)
37 #define RNG_SR_SECS BIT(2)
38 #define RNG_SR_CEIS BIT(5)
39 #define RNG_SR_SEIS BIT(6)
44 #define RNG_NSCR_MASK GENMASK(17, 0)
48 #define RNG_NB_RECOVER_TRIES 3
50 struct stm32_rng_data {
59 * struct stm32_rng_config - RNG configuration data
61 * @cr: RNG configuration. 0 means default hardware RNG configuration
62 * @nscr: Noise sources control configuration.
63 * @htcr: Health tests configuration.
65 struct stm32_rng_config {
71 struct stm32_rng_private {
76 struct reset_control *rst;
77 struct stm32_rng_config pm_conf;
78 const struct stm32_rng_data *data;
84 * Extracts from the STM32 RNG specification when RNG supports CONDRST.
86 * When a noise source (or seed) error occurs, the RNG stops generating
87 * random numbers and sets to “1” both SEIS and SECS bits to indicate
88 * that a seed error occurred. (...)
90 * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield
91 * description for details). This step is needed only if SECS is set.
92 * Indeed, when SEIS is set and SECS is cleared it means RNG performed
93 * the reset automatically (auto-reset).
94 * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST
95 * to be cleared in the RNG_CR register, then confirm that SEIS is
96 * cleared in the RNG_SR register. Otherwise just clear SEIS bit in
97 * the RNG_SR register.
98 * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be
99 * cleared by RNG. The random number generation is now back to normal.
101 static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
103 struct device *dev = priv->dev;
104 u32 sr = readl_relaxed(priv->base + RNG_SR);
105 u32 cr = readl_relaxed(priv->base + RNG_CR);
108 if (sr & RNG_SR_SECS) {
109 /* Conceal by resetting the subsystem (step 1.) */
110 writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR);
111 writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR);
113 /* RNG auto-reset (step 2.) */
114 writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
118 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10,
121 dev_err(dev, "%s: timeout %x\n", __func__, sr);
125 /* Check SEIS is cleared (step 2.) */
126 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
129 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10,
132 dev_err(dev, "%s: timeout %x\n", __func__, sr);
141 * Extracts from the STM32 RNG specification, when CONDRST is not supported
143 * When a noise source (or seed) error occurs, the RNG stops generating
144 * random numbers and sets to “1” both SEIS and SECS bits to indicate
145 * that a seed error occurred. (...)
147 * The following sequence shall be used to fully recover from a seed
148 * error after the RNG initialization:
149 * 1. Clear the SEIS bit by writing it to “0”.
150 * 2. Read out 12 words from the RNG_DR register, and discard each of
151 * them in order to clean the pipeline.
152 * 3. Confirm that SEIS is still cleared. Random number generation is
155 static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv)
158 u32 sr = readl_relaxed(priv->base + RNG_SR);
160 writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
162 for (i = 12; i != 0; i--)
163 (void)readl_relaxed(priv->base + RNG_DR);
165 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
171 static int stm32_rng_conceal_seed_error(struct hwrng *rng)
173 struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
175 dev_dbg(priv->dev, "Concealing seed error\n");
177 if (priv->data->has_cond_reset)
178 return stm32_rng_conceal_seed_error_cond_reset(priv);
180 return stm32_rng_conceal_seed_error_sw_reset(priv);
184 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
186 struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
188 int retval = 0, err = 0;
191 retval = pm_runtime_resume_and_get(priv->dev);
195 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
196 stm32_rng_conceal_seed_error(rng);
198 while (max >= sizeof(u32)) {
199 sr = readl_relaxed(priv->base + RNG_SR);
201 * Manage timeout which is based on timer and take
202 * care of initial delay time when enabling the RNG.
205 err = readl_relaxed_poll_timeout_atomic(priv->base
210 dev_err(priv->dev, "%s: timeout %x!\n", __func__, sr);
214 /* The FIFO is being filled up */
218 if (sr != RNG_SR_DRDY) {
219 if (sr & RNG_SR_SEIS) {
220 err = stm32_rng_conceal_seed_error(rng);
222 if (err && i > RNG_NB_RECOVER_TRIES) {
223 dev_err(priv->dev, "Couldn't recover from seed error\n");
224 retval = -ENOTRECOVERABLE;
231 if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr))
232 writel_relaxed(0, priv->base + RNG_SR);
235 /* Late seed error case: DR being 0 is an error status */
236 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
238 err = stm32_rng_conceal_seed_error(rng);
240 if (err && i > RNG_NB_RECOVER_TRIES) {
241 dev_err(priv->dev, "Couldn't recover from seed error");
242 retval = -ENOTRECOVERABLE;
250 retval += sizeof(u32);
256 pm_runtime_mark_last_busy(priv->dev);
257 pm_runtime_put_sync_autosuspend(priv->dev);
259 return retval || !wait ? retval : -EIO;
262 static uint stm32_rng_clock_freq_restrain(struct hwrng *rng)
264 struct stm32_rng_private *priv =
265 container_of(rng, struct stm32_rng_private, rng);
266 unsigned long clock_rate = 0;
269 clock_rate = clk_get_rate(priv->clk);
272 * Get the exponent to apply on the CLKDIV field in RNG_CR register
273 * No need to handle the case when clock-div > 0xF as it is physically
276 while ((clock_rate >> clock_div) > priv->data->max_clock_rate)
279 pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk) >> clock_div);
284 static int stm32_rng_init(struct hwrng *rng)
286 struct stm32_rng_private *priv =
287 container_of(rng, struct stm32_rng_private, rng);
291 err = clk_prepare_enable(priv->clk);
295 /* clear error indicators */
296 writel_relaxed(0, priv->base + RNG_SR);
298 reg = readl_relaxed(priv->base + RNG_CR);
301 * Keep default RNG configuration if none was specified.
302 * 0 is an invalid value as it disables all entropy sources.
304 if (priv->data->has_cond_reset && priv->data->cr) {
305 uint clock_div = stm32_rng_clock_freq_restrain(rng);
307 reg &= ~RNG_CR_CONFIG_MASK;
308 reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) |
309 (clock_div << RNG_CR_CLKDIV_SHIFT);
314 writel_relaxed(reg, priv->base + RNG_CR);
316 /* Health tests and noise control registers */
317 writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR);
318 writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR);
320 reg &= ~RNG_CR_CONDRST;
323 reg |= RNG_CR_CONFLOCK;
325 writel_relaxed(reg, priv->base + RNG_CR);
327 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
328 (!(reg & RNG_CR_CONDRST)),
331 clk_disable_unprepare(priv->clk);
332 dev_err(priv->dev, "%s: timeout %x!\n", __func__, reg);
336 /* Handle all RNG versions by checking if conditional reset should be set */
337 if (priv->data->has_cond_reset)
338 reg |= RNG_CR_CONDRST;
345 writel_relaxed(reg, priv->base + RNG_CR);
347 if (priv->data->has_cond_reset)
348 reg &= ~RNG_CR_CONDRST;
352 writel_relaxed(reg, priv->base + RNG_CR);
355 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg,
358 if (err || (reg & ~RNG_SR_DRDY)) {
359 clk_disable_unprepare(priv->clk);
360 dev_err(priv->dev, "%s: timeout:%x SR: %x!\n", __func__, err, reg);
364 clk_disable_unprepare(priv->clk);
369 static void stm32_rng_remove(struct platform_device *ofdev)
371 pm_runtime_disable(&ofdev->dev);
374 static int __maybe_unused stm32_rng_runtime_suspend(struct device *dev)
376 struct stm32_rng_private *priv = dev_get_drvdata(dev);
379 reg = readl_relaxed(priv->base + RNG_CR);
380 reg &= ~RNG_CR_RNGEN;
381 writel_relaxed(reg, priv->base + RNG_CR);
382 clk_disable_unprepare(priv->clk);
387 static int __maybe_unused stm32_rng_suspend(struct device *dev)
389 struct stm32_rng_private *priv = dev_get_drvdata(dev);
392 err = clk_prepare_enable(priv->clk);
396 if (priv->data->has_cond_reset) {
397 priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR);
398 priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR);
401 /* Do not save that RNG is enabled as it will be handled at resume */
402 priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN;
404 writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR);
406 clk_disable_unprepare(priv->clk);
411 static int __maybe_unused stm32_rng_runtime_resume(struct device *dev)
413 struct stm32_rng_private *priv = dev_get_drvdata(dev);
417 err = clk_prepare_enable(priv->clk);
421 /* Clean error indications */
422 writel_relaxed(0, priv->base + RNG_SR);
424 reg = readl_relaxed(priv->base + RNG_CR);
426 writel_relaxed(reg, priv->base + RNG_CR);
431 static int __maybe_unused stm32_rng_resume(struct device *dev)
433 struct stm32_rng_private *priv = dev_get_drvdata(dev);
437 err = clk_prepare_enable(priv->clk);
441 /* Clean error indications */
442 writel_relaxed(0, priv->base + RNG_SR);
444 if (priv->data->has_cond_reset) {
446 * Correct configuration in bits [29:4] must be set in the same
447 * access that set RNG_CR_CONDRST bit. Else config setting is
448 * not taken into account. CONFIGLOCK bit must also be unset but
449 * it is not handled at the moment.
451 writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR);
453 writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR);
454 writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR);
456 reg = readl_relaxed(priv->base + RNG_CR);
458 reg &= ~RNG_CR_CONDRST;
459 writel_relaxed(reg, priv->base + RNG_CR);
461 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
462 reg & ~RNG_CR_CONDRST, 10, 100000);
465 clk_disable_unprepare(priv->clk);
466 dev_err(priv->dev, "%s: timeout:%x CR: %x!\n", __func__, err, reg);
470 reg = priv->pm_conf.cr;
472 writel_relaxed(reg, priv->base + RNG_CR);
475 clk_disable_unprepare(priv->clk);
480 static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = {
481 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
482 stm32_rng_runtime_resume, NULL)
483 SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend,
487 static const struct stm32_rng_data stm32mp13_rng_data = {
488 .has_cond_reset = true,
489 .max_clock_rate = 48000000,
495 static const struct stm32_rng_data stm32_rng_data = {
496 .has_cond_reset = false,
497 .max_clock_rate = 3000000,
500 static const struct of_device_id stm32_rng_match[] = {
502 .compatible = "st,stm32mp13-rng",
503 .data = &stm32mp13_rng_data,
506 .compatible = "st,stm32-rng",
507 .data = &stm32_rng_data,
511 MODULE_DEVICE_TABLE(of, stm32_rng_match);
513 static int stm32_rng_probe(struct platform_device *ofdev)
515 struct device *dev = &ofdev->dev;
516 struct device_node *np = ofdev->dev.of_node;
517 struct stm32_rng_private *priv;
518 struct resource *res;
520 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
524 priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res);
525 if (IS_ERR(priv->base))
526 return PTR_ERR(priv->base);
528 priv->clk = devm_clk_get(&ofdev->dev, NULL);
529 if (IS_ERR(priv->clk))
530 return PTR_ERR(priv->clk);
532 priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
533 if (!IS_ERR(priv->rst)) {
534 reset_control_assert(priv->rst);
536 reset_control_deassert(priv->rst);
539 priv->ced = of_property_read_bool(np, "clock-error-detect");
540 priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
543 priv->data = of_device_get_match_data(dev);
547 dev_set_drvdata(dev, priv);
549 priv->rng.name = dev_driver_string(dev);
550 priv->rng.init = stm32_rng_init;
551 priv->rng.read = stm32_rng_read;
552 priv->rng.quality = 900;
554 pm_runtime_set_autosuspend_delay(dev, 100);
555 pm_runtime_use_autosuspend(dev);
556 pm_runtime_enable(dev);
558 return devm_hwrng_register(dev, &priv->rng);
561 static struct platform_driver stm32_rng_driver = {
564 .pm = pm_ptr(&stm32_rng_pm_ops),
565 .of_match_table = stm32_rng_match,
567 .probe = stm32_rng_probe,
568 .remove_new = stm32_rng_remove,
571 module_platform_driver(stm32_rng_driver);
573 MODULE_LICENSE("GPL");
575 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");