1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2015, Daniel Thompson
7 #include <linux/clk-provider.h>
8 #include <linux/delay.h>
9 #include <linux/hw_random.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
19 #include <linux/slab.h>
22 #define RNG_CR_RNGEN BIT(2)
23 #define RNG_CR_CED BIT(5)
24 #define RNG_CR_CONFIG1 GENMASK(11, 8)
25 #define RNG_CR_NISTC BIT(12)
26 #define RNG_CR_CONFIG2 GENMASK(15, 13)
27 #define RNG_CR_CLKDIV_SHIFT 16
28 #define RNG_CR_CLKDIV GENMASK(19, 16)
29 #define RNG_CR_CONFIG3 GENMASK(25, 20)
30 #define RNG_CR_CONDRST BIT(30)
31 #define RNG_CR_CONFLOCK BIT(31)
32 #define RNG_CR_ENTROPY_SRC_MASK (RNG_CR_CONFIG1 | RNG_CR_NISTC | RNG_CR_CONFIG2 | RNG_CR_CONFIG3)
33 #define RNG_CR_CONFIG_MASK (RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED | RNG_CR_CLKDIV)
36 #define RNG_SR_DRDY BIT(0)
37 #define RNG_SR_CECS BIT(1)
38 #define RNG_SR_SECS BIT(2)
39 #define RNG_SR_CEIS BIT(5)
40 #define RNG_SR_SEIS BIT(6)
45 #define RNG_NSCR_MASK GENMASK(17, 0)
49 #define RNG_NB_RECOVER_TRIES 3
51 struct stm32_rng_data {
61 * struct stm32_rng_config - RNG configuration data
63 * @cr: RNG configuration. 0 means default hardware RNG configuration
64 * @nscr: Noise sources control configuration.
65 * @htcr: Health tests configuration.
67 struct stm32_rng_config {
73 struct stm32_rng_private {
77 struct clk_bulk_data *clk_bulk;
78 struct reset_control *rst;
79 struct stm32_rng_config pm_conf;
80 const struct stm32_rng_data *data;
86 * Extracts from the STM32 RNG specification when RNG supports CONDRST.
88 * When a noise source (or seed) error occurs, the RNG stops generating
89 * random numbers and sets to “1” both SEIS and SECS bits to indicate
90 * that a seed error occurred. (...)
92 * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield
93 * description for details). This step is needed only if SECS is set.
94 * Indeed, when SEIS is set and SECS is cleared it means RNG performed
95 * the reset automatically (auto-reset).
96 * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST
97 * to be cleared in the RNG_CR register, then confirm that SEIS is
98 * cleared in the RNG_SR register. Otherwise just clear SEIS bit in
99 * the RNG_SR register.
100 * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be
101 * cleared by RNG. The random number generation is now back to normal.
103 static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
105 struct device *dev = priv->dev;
106 u32 sr = readl_relaxed(priv->base + RNG_SR);
107 u32 cr = readl_relaxed(priv->base + RNG_CR);
110 if (sr & RNG_SR_SECS) {
111 /* Conceal by resetting the subsystem (step 1.) */
112 writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR);
113 writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR);
115 /* RNG auto-reset (step 2.) */
116 writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
120 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10,
123 dev_err(dev, "%s: timeout %x\n", __func__, sr);
127 /* Check SEIS is cleared (step 2.) */
128 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
131 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10,
134 dev_err(dev, "%s: timeout %x\n", __func__, sr);
143 * Extracts from the STM32 RNG specification, when CONDRST is not supported
145 * When a noise source (or seed) error occurs, the RNG stops generating
146 * random numbers and sets to “1” both SEIS and SECS bits to indicate
147 * that a seed error occurred. (...)
149 * The following sequence shall be used to fully recover from a seed
150 * error after the RNG initialization:
151 * 1. Clear the SEIS bit by writing it to “0”.
152 * 2. Read out 12 words from the RNG_DR register, and discard each of
153 * them in order to clean the pipeline.
154 * 3. Confirm that SEIS is still cleared. Random number generation is
157 static int stm32_rng_conceal_seed_error_sw_reset(struct stm32_rng_private *priv)
160 u32 sr = readl_relaxed(priv->base + RNG_SR);
162 writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
164 for (i = 12; i != 0; i--)
165 (void)readl_relaxed(priv->base + RNG_DR);
167 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
173 static int stm32_rng_conceal_seed_error(struct hwrng *rng)
175 struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
177 dev_dbg(priv->dev, "Concealing seed error\n");
179 if (priv->data->has_cond_reset)
180 return stm32_rng_conceal_seed_error_cond_reset(priv);
182 return stm32_rng_conceal_seed_error_sw_reset(priv);
186 static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
188 struct stm32_rng_private *priv = container_of(rng, struct stm32_rng_private, rng);
190 int retval = 0, err = 0;
193 retval = pm_runtime_resume_and_get(priv->dev);
197 if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
198 stm32_rng_conceal_seed_error(rng);
200 while (max >= sizeof(u32)) {
201 sr = readl_relaxed(priv->base + RNG_SR);
203 * Manage timeout which is based on timer and take
204 * care of initial delay time when enabling the RNG.
207 err = readl_relaxed_poll_timeout_atomic(priv->base
212 dev_err(priv->dev, "%s: timeout %x!\n", __func__, sr);
216 /* The FIFO is being filled up */
220 if (sr != RNG_SR_DRDY) {
221 if (sr & RNG_SR_SEIS) {
222 err = stm32_rng_conceal_seed_error(rng);
224 if (err && i > RNG_NB_RECOVER_TRIES) {
225 dev_err(priv->dev, "Couldn't recover from seed error\n");
226 retval = -ENOTRECOVERABLE;
233 if (WARN_ONCE((sr & RNG_SR_CEIS), "RNG clock too slow - %x\n", sr))
234 writel_relaxed(0, priv->base + RNG_SR);
237 /* Late seed error case: DR being 0 is an error status */
238 *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
240 err = stm32_rng_conceal_seed_error(rng);
242 if (err && i > RNG_NB_RECOVER_TRIES) {
243 dev_err(priv->dev, "Couldn't recover from seed error");
244 retval = -ENOTRECOVERABLE;
252 retval += sizeof(u32);
258 pm_runtime_mark_last_busy(priv->dev);
259 pm_runtime_put_sync_autosuspend(priv->dev);
261 return retval || !wait ? retval : -EIO;
264 static uint stm32_rng_clock_freq_restrain(struct hwrng *rng)
266 struct stm32_rng_private *priv =
267 container_of(rng, struct stm32_rng_private, rng);
268 unsigned long clock_rate = 0;
271 clock_rate = clk_get_rate(priv->clk_bulk[0].clk);
274 * Get the exponent to apply on the CLKDIV field in RNG_CR register
275 * No need to handle the case when clock-div > 0xF as it is physically
278 while ((clock_rate >> clock_div) > priv->data->max_clock_rate)
281 pr_debug("RNG clk rate : %lu\n", clk_get_rate(priv->clk_bulk[0].clk) >> clock_div);
286 static int stm32_rng_init(struct hwrng *rng)
288 struct stm32_rng_private *priv =
289 container_of(rng, struct stm32_rng_private, rng);
293 err = clk_bulk_prepare_enable(priv->data->nb_clock, priv->clk_bulk);
297 /* clear error indicators */
298 writel_relaxed(0, priv->base + RNG_SR);
300 reg = readl_relaxed(priv->base + RNG_CR);
303 * Keep default RNG configuration if none was specified.
304 * 0 is an invalid value as it disables all entropy sources.
306 if (priv->data->has_cond_reset && priv->data->cr) {
307 uint clock_div = stm32_rng_clock_freq_restrain(rng);
309 reg &= ~RNG_CR_CONFIG_MASK;
310 reg |= RNG_CR_CONDRST | (priv->data->cr & RNG_CR_ENTROPY_SRC_MASK) |
311 (clock_div << RNG_CR_CLKDIV_SHIFT);
316 writel_relaxed(reg, priv->base + RNG_CR);
318 /* Health tests and noise control registers */
319 writel_relaxed(priv->data->htcr, priv->base + RNG_HTCR);
320 writel_relaxed(priv->data->nscr & RNG_NSCR_MASK, priv->base + RNG_NSCR);
322 reg &= ~RNG_CR_CONDRST;
325 reg |= RNG_CR_CONFLOCK;
327 writel_relaxed(reg, priv->base + RNG_CR);
329 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
330 (!(reg & RNG_CR_CONDRST)),
333 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
334 dev_err(priv->dev, "%s: timeout %x!\n", __func__, reg);
338 /* Handle all RNG versions by checking if conditional reset should be set */
339 if (priv->data->has_cond_reset)
340 reg |= RNG_CR_CONDRST;
347 writel_relaxed(reg, priv->base + RNG_CR);
349 if (priv->data->has_cond_reset)
350 reg &= ~RNG_CR_CONDRST;
354 writel_relaxed(reg, priv->base + RNG_CR);
357 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, reg,
360 if (err || (reg & ~RNG_SR_DRDY)) {
361 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
362 dev_err(priv->dev, "%s: timeout:%x SR: %x!\n", __func__, err, reg);
367 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
372 static void stm32_rng_remove(struct platform_device *ofdev)
374 pm_runtime_disable(&ofdev->dev);
377 static int __maybe_unused stm32_rng_runtime_suspend(struct device *dev)
379 struct stm32_rng_private *priv = dev_get_drvdata(dev);
382 reg = readl_relaxed(priv->base + RNG_CR);
383 reg &= ~RNG_CR_RNGEN;
384 writel_relaxed(reg, priv->base + RNG_CR);
386 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
391 static int __maybe_unused stm32_rng_suspend(struct device *dev)
393 struct stm32_rng_private *priv = dev_get_drvdata(dev);
396 err = clk_bulk_prepare_enable(priv->data->nb_clock, priv->clk_bulk);
400 if (priv->data->has_cond_reset) {
401 priv->pm_conf.nscr = readl_relaxed(priv->base + RNG_NSCR);
402 priv->pm_conf.htcr = readl_relaxed(priv->base + RNG_HTCR);
405 /* Do not save that RNG is enabled as it will be handled at resume */
406 priv->pm_conf.cr = readl_relaxed(priv->base + RNG_CR) & ~RNG_CR_RNGEN;
408 writel_relaxed(priv->pm_conf.cr, priv->base + RNG_CR);
410 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
415 static int __maybe_unused stm32_rng_runtime_resume(struct device *dev)
417 struct stm32_rng_private *priv = dev_get_drvdata(dev);
421 err = clk_bulk_prepare_enable(priv->data->nb_clock, priv->clk_bulk);
425 /* Clean error indications */
426 writel_relaxed(0, priv->base + RNG_SR);
428 reg = readl_relaxed(priv->base + RNG_CR);
430 writel_relaxed(reg, priv->base + RNG_CR);
435 static int __maybe_unused stm32_rng_resume(struct device *dev)
437 struct stm32_rng_private *priv = dev_get_drvdata(dev);
441 err = clk_bulk_prepare_enable(priv->data->nb_clock, priv->clk_bulk);
445 /* Clean error indications */
446 writel_relaxed(0, priv->base + RNG_SR);
448 if (priv->data->has_cond_reset) {
450 * Correct configuration in bits [29:4] must be set in the same
451 * access that set RNG_CR_CONDRST bit. Else config setting is
452 * not taken into account. CONFIGLOCK bit must also be unset but
453 * it is not handled at the moment.
455 writel_relaxed(priv->pm_conf.cr | RNG_CR_CONDRST, priv->base + RNG_CR);
457 writel_relaxed(priv->pm_conf.nscr, priv->base + RNG_NSCR);
458 writel_relaxed(priv->pm_conf.htcr, priv->base + RNG_HTCR);
460 reg = readl_relaxed(priv->base + RNG_CR);
462 reg &= ~RNG_CR_CONDRST;
463 writel_relaxed(reg, priv->base + RNG_CR);
465 err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, reg,
466 reg & ~RNG_CR_CONDRST, 10, 100000);
469 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
470 dev_err(priv->dev, "%s: timeout:%x CR: %x!\n", __func__, err, reg);
474 reg = priv->pm_conf.cr;
476 writel_relaxed(reg, priv->base + RNG_CR);
479 clk_bulk_disable_unprepare(priv->data->nb_clock, priv->clk_bulk);
484 static const struct dev_pm_ops __maybe_unused stm32_rng_pm_ops = {
485 SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
486 stm32_rng_runtime_resume, NULL)
487 SET_SYSTEM_SLEEP_PM_OPS(stm32_rng_suspend,
491 static const struct stm32_rng_data stm32mp25_rng_data = {
492 .has_cond_reset = true,
493 .max_clock_rate = 48000000,
500 static const struct stm32_rng_data stm32mp13_rng_data = {
501 .has_cond_reset = true,
502 .max_clock_rate = 48000000,
509 static const struct stm32_rng_data stm32_rng_data = {
510 .has_cond_reset = false,
511 .max_clock_rate = 48000000,
515 static const struct of_device_id stm32_rng_match[] = {
517 .compatible = "st,stm32mp25-rng",
518 .data = &stm32mp25_rng_data,
521 .compatible = "st,stm32mp13-rng",
522 .data = &stm32mp13_rng_data,
525 .compatible = "st,stm32-rng",
526 .data = &stm32_rng_data,
530 MODULE_DEVICE_TABLE(of, stm32_rng_match);
532 static int stm32_rng_probe(struct platform_device *ofdev)
534 struct device *dev = &ofdev->dev;
535 struct device_node *np = ofdev->dev.of_node;
536 struct stm32_rng_private *priv;
537 struct resource *res;
540 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
544 priv->base = devm_platform_get_and_ioremap_resource(ofdev, 0, &res);
545 if (IS_ERR(priv->base))
546 return PTR_ERR(priv->base);
548 priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
549 if (!IS_ERR(priv->rst)) {
550 reset_control_assert(priv->rst);
552 reset_control_deassert(priv->rst);
555 priv->ced = of_property_read_bool(np, "clock-error-detect");
556 priv->lock_conf = of_property_read_bool(np, "st,rng-lock-conf");
559 priv->data = of_device_get_match_data(dev);
563 dev_set_drvdata(dev, priv);
565 priv->rng.name = dev_driver_string(dev);
566 priv->rng.init = stm32_rng_init;
567 priv->rng.read = stm32_rng_read;
568 priv->rng.quality = 900;
570 if (!priv->data->nb_clock || priv->data->nb_clock > 2)
573 ret = devm_clk_bulk_get_all(dev, &priv->clk_bulk);
574 if (ret != priv->data->nb_clock)
575 return dev_err_probe(dev, -EINVAL, "Failed to get clocks: %d\n", ret);
577 if (priv->data->nb_clock == 2) {
578 const char *id = priv->clk_bulk[1].id;
579 struct clk *clk = priv->clk_bulk[1].clk;
581 if (!priv->clk_bulk[0].id || !priv->clk_bulk[1].id)
582 return dev_err_probe(dev, -EINVAL, "Missing clock name\n");
584 if (strcmp(priv->clk_bulk[0].id, "core")) {
585 priv->clk_bulk[1].id = priv->clk_bulk[0].id;
586 priv->clk_bulk[1].clk = priv->clk_bulk[0].clk;
587 priv->clk_bulk[0].id = id;
588 priv->clk_bulk[0].clk = clk;
592 pm_runtime_set_autosuspend_delay(dev, 100);
593 pm_runtime_use_autosuspend(dev);
594 pm_runtime_enable(dev);
596 return devm_hwrng_register(dev, &priv->rng);
599 static struct platform_driver stm32_rng_driver = {
602 .pm = pm_ptr(&stm32_rng_pm_ops),
603 .of_match_table = stm32_rng_match,
605 .probe = stm32_rng_probe,
606 .remove = stm32_rng_remove,
609 module_platform_driver(stm32_rng_driver);
611 MODULE_LICENSE("GPL");
613 MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");