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 {
75 struct reset_control *rst;
76 struct stm32_rng_config pm_conf;
77 const struct stm32_rng_data *data;
83 * Extracts from the STM32 RNG specification when RNG supports CONDRST.
85 * When a noise source (or seed) error occurs, the RNG stops generating
86 * random numbers and sets to “1” both SEIS and SECS bits to indicate
87 * that a seed error occurred. (...)
89 * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield
90 * description for details). This step is needed only if SECS is set.
91 * Indeed, when SEIS is set and SECS is cleared it means RNG performed
92 * the reset automatically (auto-reset).
93 * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST
94 * to be cleared in the RNG_CR register, then confirm that SEIS is
95 * cleared in the RNG_SR register. Otherwise just clear SEIS bit in
96 * the RNG_SR register.
97 * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be
98 * cleared by RNG. The random number generation is now back to normal.
100 static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
102 struct device *dev = (struct device *)priv->rng.priv;
103 u32 sr = readl_relaxed(priv->base + RNG_SR);
104 u32 cr = readl_relaxed(priv->base + RNG_CR);
107 if (sr & RNG_SR_SECS) {
108 /* Conceal by resetting the subsystem (step 1.) */
109 writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR);
110 writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR);
112 /* RNG auto-reset (step 2.) */
113 writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
117 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10,
120 dev_err(dev, "%s: timeout %x\n", __func__, sr);
124 /* Check SEIS is cleared (step 2.) */
125 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
128 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10,
131 dev_err(dev, "%s: timeout %x\n", __func__, sr);
140 * Extracts from the STM32 RNG specification, when CONDRST is not supported
142 * When a noise source (or seed) error occurs, the RNG stops generating
143 * random numbers and sets to “1” both SEIS and SECS bits to indicate
144 * that a seed error occurred. (...)
146 * The following sequence shall be used to fully recover from a seed
147 * error after the RNG initialization:
148 * 1. Clear the SEIS bit by writing it to “0”.
149 * 2. Read out 12 words from the RNG_DR register, and discard each of
150 * them in order to clean the pipeline.
151 * 3. Confirm that SEIS is still cleared. Random number generation is
154 static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv)
157 u32 sr = readl_relaxed(priv->base + RNG_SR);
159 writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
161 for (i = 12; i != 0; i--)
162 (void)readl_relaxed(priv->base + RNG_DR);
164 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
170 static int stm32_rng_conceal_seed_error(struct hwrng *rng)
172 struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
174 dev_dbg((struct device *)priv->rng.priv, "Concealing seed error\n");
176 if (priv->data->has_cond_reset)
177 return stm32_rng_conceal_seed_error_cond_reset(priv);
179 return stm32_rng_conceal_seed_error_sw_reset(priv);
183 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
185 struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
187 int retval = 0, err = 0;
190 pm_runtime_get_sync((struct device *) priv->rng.priv);
192 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
193 stm32_rng_conceal_seed_error(rng);
195 while (max >= sizeof(u32)) {
196 sr = readl_relaxed(priv->base + RNG_SR);
198 * Manage timeout which is based on timer and take
199 * care of initial delay time when enabling the RNG.
202 err = readl_relaxed_poll_timeout_atomic(priv->base
207 dev_err((struct device *)priv->rng.priv,
208 "%s: timeout %x!\n", __func__, sr);
212 /* The FIFO is being filled up */
216 if (sr != RNG_SR_DRDY) {
217 if (sr & RNG_SR_SEIS) {
218 err = stm32_rng_conceal_seed_error(rng);
220 if (err && i > RNG_NB_RECOVER_TRIES) {
221 dev_err((struct device *)priv->rng.priv,
222 "Couldn't recover from seed error\n");
223 return -ENOTRECOVERABLE;
229 if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr))
230 writel_relaxed(0, priv->base + RNG_SR);
233 /* Late seed error case: DR being 0 is an error status */
234 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
236 err = stm32_rng_conceal_seed_error(rng);
238 if (err && i > RNG_NB_RECOVER_TRIES) {
239 dev_err((struct device *)priv->rng.priv,
240 "Couldn't recover from seed error");
241 return -ENOTRECOVERABLE;
248 retval += sizeof(u32);
253 pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
254 pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
256 return retval || !wait ? retval : -EIO;
259 static uint stm32_rng_clock_freq_restrain(struct hwrng *rng)
261 struct stm32_rng_private *priv =
262 container_of(rng, struct stm32_rng_private, rng);
263 unsigned long clock_rate = 0;
266 clock_rate = clk_get_rate(priv->clk);
269 * Get the exponent to apply on the CLKDIV field in RNG_CR register
270 * No need to handle the case when clock-div > 0xF as it is physically
273 while ((clock_rate >> clock_div) > priv->data->max_clock_rate)
276 pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk) >> clock_div);
281 static int stm32_rng_init(struct hwrng *rng)
283 struct stm32_rng_private *priv =
284 container_of(rng, struct stm32_rng_private, rng);
288 err = clk_prepare_enable(priv->clk);
292 /* clear error indicators */
293 writel_relaxed(0, priv->base + RNG_SR);
295 reg = readl_relaxed(priv->base + RNG_CR);
298 * Keep default RNG configuration if none was specified.
299 * 0 is an invalid value as it disables all entropy sources.
301 if (priv->data->has_cond_reset && priv->data->cr) {
302 uint clock_div = stm32_rng_clock_freq_restrain(rng);
304 reg &= ~RNG_CR_CONFIG_MASK;
305 reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) |
306 (clock_div << RNG_CR_CLKDIV_SHIFT);
311 writel_relaxed(reg, priv->base + RNG_CR);
313 /* Health tests and noise control registers */
314 writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR);
315 writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR);
317 reg &= ~RNG_CR_CONDRST;
320 reg |= RNG_CR_CONFLOCK;
322 writel_relaxed(reg, priv->base + RNG_CR);
324 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
325 (!(reg & RNG_CR_CONDRST)),
328 clk_disable_unprepare(priv->clk);
329 dev_err((struct device *)priv->rng.priv,
330 "%s: timeout %x!\n", __func__, reg);
334 /* Handle all RNG versions by checking if conditional reset should be set */
335 if (priv->data->has_cond_reset)
336 reg |= RNG_CR_CONDRST;
343 writel_relaxed(reg, priv->base + RNG_CR);
345 if (priv->data->has_cond_reset)
346 reg &= ~RNG_CR_CONDRST;
350 writel_relaxed(reg, priv->base + RNG_CR);
353 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg,
356 if (err | (reg & ~RNG_SR_DRDY)) {
357 clk_disable_unprepare(priv->clk);
358 dev_err((struct device *)priv->rng.priv,
359 "%s: timeout:%x SR: %x!\n", __func__, err, reg);
366 static void stm32_rng_remove(struct platform_device *ofdev)
368 pm_runtime_disable(&ofdev->dev);
371 static int __maybe_unused stm32_rng_runtime_suspend(struct device *dev)
373 struct stm32_rng_private *priv = dev_get_drvdata(dev);
376 reg = readl_relaxed(priv->base + RNG_CR);
377 reg &= ~RNG_CR_RNGEN;
378 writel_relaxed(reg, priv->base + RNG_CR);
379 clk_disable_unprepare(priv->clk);
384 static int __maybe_unused stm32_rng_suspend(struct device *dev)
386 struct stm32_rng_private *priv = dev_get_drvdata(dev);
388 if (priv->data->has_cond_reset) {
389 priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR);
390 priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR);
393 /* Do not save that RNG is enabled as it will be handled at resume */
394 priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN;
396 writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR);
398 clk_disable_unprepare(priv->clk);
403 static int __maybe_unused stm32_rng_runtime_resume(struct device *dev)
405 struct stm32_rng_private *priv = dev_get_drvdata(dev);
409 err = clk_prepare_enable(priv->clk);
413 /* Clean error indications */
414 writel_relaxed(0, priv->base + RNG_SR);
416 reg = readl_relaxed(priv->base + RNG_CR);
418 writel_relaxed(reg, priv->base + RNG_CR);
423 static int __maybe_unused stm32_rng_resume(struct device *dev)
425 struct stm32_rng_private *priv = dev_get_drvdata(dev);
429 err = clk_prepare_enable(priv->clk);
433 /* Clean error indications */
434 writel_relaxed(0, priv->base + RNG_SR);
436 if (priv->data->has_cond_reset) {
438 * Correct configuration in bits [29:4] must be set in the same
439 * access that set RNG_CR_CONDRST bit. Else config setting is
440 * not taken into account. CONFIGLOCK bit must also be unset but
441 * it is not handled at the moment.
443 writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR);
445 writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR);
446 writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR);
448 reg = readl_relaxed(priv->base + RNG_CR);
450 reg &= ~RNG_CR_CONDRST;
451 writel_relaxed(reg, priv->base + RNG_CR);
453 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
454 reg & ~RNG_CR_CONDRST, 10, 100000);
457 clk_disable_unprepare(priv->clk);
458 dev_err((struct device *)priv->rng.priv,
459 "%s: timeout:%x CR: %x!\n", __func__, err, reg);
463 reg = priv->pm_conf.cr;
465 writel_relaxed(reg, priv->base + RNG_CR);
471 static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = {
472 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
473 stm32_rng_runtime_resume, NULL)
474 SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend,
478 static const struct stm32_rng_data stm32mp13_rng_data = {
479 .has_cond_reset = true,
480 .max_clock_rate = 48000000,
486 static const struct stm32_rng_data stm32_rng_data = {
487 .has_cond_reset = false,
488 .max_clock_rate = 3000000,
491 static const struct of_device_id stm32_rng_match[] = {
493 .compatible = "st,stm32mp13-rng",
494 .data = &stm32mp13_rng_data,
497 .compatible = "st,stm32-rng",
498 .data = &stm32_rng_data,
502 MODULE_DEVICE_TABLE(of, stm32_rng_match);
504 static int stm32_rng_probe(struct platform_device *ofdev)
506 struct device *dev = &ofdev->dev;
507 struct device_node *np = ofdev->dev.of_node;
508 struct stm32_rng_private *priv;
509 struct resource *res;
511 priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
515 priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res);
516 if (IS_ERR(priv->base))
517 return PTR_ERR(priv->base);
519 priv->clk = devm_clk_get(&ofdev->dev, NULL);
520 if (IS_ERR(priv->clk))
521 return PTR_ERR(priv->clk);
523 priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
524 if (!IS_ERR(priv->rst)) {
525 reset_control_assert(priv->rst);
527 reset_control_deassert(priv->rst);
530 priv->ced = of_property_read_bool(np, "clock-error-detect");
531 priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
533 priv->data = of_device_get_match_data(dev);
537 dev_set_drvdata(dev, priv);
539 priv->rng.name = dev_driver_string(dev);
540 priv->rng.init = stm32_rng_init;
541 priv->rng.read = stm32_rng_read;
542 priv->rng.priv = (unsigned long) dev;
543 priv->rng.quality = 900;
545 pm_runtime_set_autosuspend_delay(dev, 100);
546 pm_runtime_use_autosuspend(dev);
547 pm_runtime_enable(dev);
549 return devm_hwrng_register(dev, &priv->rng);
552 static struct platform_driver stm32_rng_driver = {
555 .pm = pm_ptr(&stm32_rng_pm_ops),
556 .of_match_table = stm32_rng_match,
558 .probe = stm32_rng_probe,
559 .remove_new = stm32_rng_remove,
562 module_platform_driver(stm32_rng_driver);
564 MODULE_LICENSE("GPL");
566 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");