1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 2018-2023, STMicroelectronics 4 */ 5 6 #include <assert.h> 7 #include <drivers/clk.h> 8 #include <drivers/clk_dt.h> 9 #include <drivers/rstctrl.h> 10 #include <io.h> 11 #include <kernel/boot.h> 12 #include <kernel/delay.h> 13 #include <kernel/dt.h> 14 #include <kernel/dt_driver.h> 15 #include <kernel/panic.h> 16 #include <kernel/pm.h> 17 #include <kernel/thread.h> 18 #include <libfdt.h> 19 #include <mm/core_memprot.h> 20 #include <rng_support.h> 21 #include <stdbool.h> 22 #include <stm32_util.h> 23 #include <string.h> 24 #include <tee/tee_cryp_utl.h> 25 #include <util.h> 26 27 #define RNG_CR U(0x00) 28 #define RNG_SR U(0x04) 29 #define RNG_DR U(0x08) 30 #define RNG_NSCR U(0x0C) 31 #define RNG_HTCR U(0x10) 32 #define RNG_VERR U(0x3F4) 33 34 #define RNG_CR_RNGEN BIT(2) 35 #define RNG_CR_IE BIT(3) 36 #define RNG_CR_CED BIT(5) 37 #define RNG_CR_CONFIG1 GENMASK_32(11, 8) 38 #define RNG_CR_NISTC BIT(12) 39 #define RNG_CR_POWER_OPTIM BIT(13) 40 #define RNG_CR_CONFIG2 GENMASK_32(15, 13) 41 #define RNG_CR_CLKDIV GENMASK_32(19, 16) 42 #define RNG_CR_CLKDIV_SHIFT U(16) 43 #define RNG_CR_CONFIG3 GENMASK_32(25, 20) 44 #define RNG_CR_CONDRST BIT(30) 45 #define RNG_CR_ENTROPY_SRC_MASK (RNG_CR_CONFIG1 | RNG_CR_NISTC | \ 46 RNG_CR_CONFIG2 | RNG_CR_CONFIG3) 47 48 #define RNG_SR_DRDY BIT(0) 49 #define RNG_SR_CECS BIT(1) 50 #define RNG_SR_SECS BIT(2) 51 #define RNG_SR_CEIS BIT(5) 52 #define RNG_SR_SEIS BIT(6) 53 54 #define RNG_NSCR_MASK GENMASK_32(17, 0) 55 56 #define RNG_VERR_MINOR_MASK GENMASK_32(3, 0) 57 #define RNG_VERR_MAJOR_MASK GENMASK_32(7, 4) 58 #define RNG_VERR_MAJOR_SHIFT U(4) 59 60 #if TRACE_LEVEL > TRACE_DEBUG 61 #define RNG_READY_TIMEOUT_US U(100000) 62 #else 63 #define RNG_READY_TIMEOUT_US U(10000) 64 #endif 65 #define RNG_RESET_TIMEOUT_US U(1000) 66 67 #define RNG_FIFO_BYTE_DEPTH U(16) 68 69 #define RNG_CONFIG_MASK (RNG_CR_ENTROPY_SRC_MASK | RNG_CR_CED | \ 70 RNG_CR_CLKDIV) 71 72 struct stm32_rng_driver_data { 73 unsigned long max_noise_clk_freq; 74 unsigned long nb_clock; 75 uint32_t cr; 76 uint32_t nscr; 77 uint32_t htcr; 78 bool has_power_optim; 79 bool has_cond_reset; 80 }; 81 82 struct stm32_rng_instance { 83 struct io_pa_va base; 84 struct clk *clock; 85 struct clk *bus_clock; 86 struct rstctrl *rstctrl; 87 const struct stm32_rng_driver_data *ddata; 88 unsigned int lock; 89 uint64_t error_to_ref; 90 uint32_t pm_cr; 91 uint32_t pm_health; 92 uint32_t pm_noise_ctrl; 93 uint32_t health_test_conf; 94 uint32_t noise_ctrl_conf; 95 uint32_t rng_config; 96 bool release_post_boot; 97 bool clock_error; 98 bool error_conceal; 99 }; 100 101 /* Expect at most a single RNG instance */ 102 static struct stm32_rng_instance *stm32_rng; 103 104 static vaddr_t get_base(void) 105 { 106 assert(stm32_rng); 107 108 return io_pa_or_va(&stm32_rng->base, 1); 109 } 110 111 /* 112 * Extracts from the STM32 RNG specification when RNG supports CONDRST. 113 * 114 * When a noise source (or seed) error occurs, the RNG stops generating 115 * random numbers and sets to “1” both SEIS and SECS bits to indicate 116 * that a seed error occurred. (...) 117 * 118 * 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield 119 * description for details). This step is needed only if SECS is set. 120 * Indeed, when SEIS is set and SECS is cleared it means RNG performed 121 * the reset automatically (auto-reset). 122 * 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST 123 * to be cleared in the RNG_CR register, then confirm that SEIS is 124 * cleared in the RNG_SR register. Otherwise just clear SEIS bit in 125 * the RNG_SR register. 126 * 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be 127 * cleared by RNG. The random number generation is now back to normal. 128 */ 129 static void conceal_seed_error_cond_reset(void) 130 { 131 struct stm32_rng_instance *dev = stm32_rng; 132 vaddr_t rng_base = get_base(); 133 134 if (!dev->error_conceal) { 135 uint32_t sr = io_read32(rng_base + RNG_SR); 136 137 if (sr & RNG_SR_SECS) { 138 /* Conceal by resetting the subsystem (step 1.) */ 139 io_setbits32(rng_base + RNG_CR, RNG_CR_CONDRST); 140 io_clrbits32(rng_base + RNG_CR, RNG_CR_CONDRST); 141 142 /* Arm timeout for error_conceal sequence */ 143 dev->error_to_ref = 144 timeout_init_us(RNG_READY_TIMEOUT_US); 145 dev->error_conceal = true; 146 } else { 147 /* RNG auto-reset (step 2.) */ 148 io_clrbits32(rng_base + RNG_SR, RNG_SR_SEIS); 149 } 150 } else { 151 /* Measure time before possible reschedule */ 152 bool timed_out = timeout_elapsed(dev->error_to_ref); 153 154 /* Wait CONDRST is cleared (step 2.) */ 155 if (io_read32(rng_base + RNG_CR) & RNG_CR_CONDRST) { 156 if (timed_out) 157 panic(); 158 159 /* Wait subsystem reset cycle completes */ 160 return; 161 } 162 163 /* Check SEIS is cleared (step 2.) */ 164 if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) 165 panic(); 166 167 /* Wait SECS is cleared (step 3.) */ 168 if (io_read32(rng_base + RNG_SR) & RNG_SR_SECS) { 169 if (timed_out) 170 panic(); 171 172 /* Wait subsystem reset cycle completes */ 173 return; 174 } 175 176 dev->error_conceal = false; 177 } 178 } 179 180 /* 181 * Extracts from the STM32 RNG specification, when CONDRST is not supported 182 * 183 * When a noise source (or seed) error occurs, the RNG stops generating 184 * random numbers and sets to “1” both SEIS and SECS bits to indicate 185 * that a seed error occurred. (...) 186 * 187 * The following sequence shall be used to fully recover from a seed 188 * error after the RNG initialization: 189 * 1. Clear the SEIS bit by writing it to “0”. 190 * 2. Read out 12 words from the RNG_DR register, and discard each of 191 * them in order to clean the pipeline. 192 * 3. Confirm that SEIS is still cleared. Random number generation is 193 * back to normal. 194 */ 195 static void conceal_seed_error_sw_reset(void) 196 { 197 vaddr_t rng_base = get_base(); 198 size_t i = 0; 199 200 io_clrbits32(rng_base + RNG_SR, RNG_SR_SEIS); 201 202 for (i = 12; i != 0; i--) 203 (void)io_read32(rng_base + RNG_DR); 204 205 if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) 206 panic("RNG noise"); 207 } 208 209 static void conceal_seed_error(void) 210 { 211 if (stm32_rng->ddata->has_cond_reset) 212 conceal_seed_error_cond_reset(); 213 else 214 conceal_seed_error_sw_reset(); 215 } 216 217 static TEE_Result read_available(vaddr_t rng_base, uint8_t *out, size_t *size) 218 { 219 struct stm32_rng_instance *dev = stm32_rng; 220 uint8_t *buf = NULL; 221 size_t req_size = 0; 222 size_t len = 0; 223 224 if (dev->error_conceal || io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) 225 conceal_seed_error(); 226 227 if (!(io_read32(rng_base + RNG_SR) & RNG_SR_DRDY)) { 228 FMSG("RNG not ready"); 229 return TEE_ERROR_NO_DATA; 230 } 231 232 if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) { 233 FMSG("RNG noise error"); 234 return TEE_ERROR_NO_DATA; 235 } 236 237 buf = out; 238 req_size = MIN(RNG_FIFO_BYTE_DEPTH, *size); 239 len = req_size; 240 241 /* RNG is ready: read up to 4 32bit words */ 242 while (len) { 243 uint32_t data32 = 0; 244 size_t sz = MIN(len, sizeof(uint32_t)); 245 246 if (!(io_read32(rng_base + RNG_SR) & RNG_SR_DRDY)) 247 break; 248 data32 = io_read32(rng_base + RNG_DR); 249 250 /* Late seed error case: DR being 0 is an error status */ 251 if (!data32) { 252 conceal_seed_error(); 253 return TEE_ERROR_NO_DATA; 254 } 255 256 memcpy(buf, &data32, sz); 257 buf += sz; 258 len -= sz; 259 } 260 261 *size = req_size - len; 262 263 return TEE_SUCCESS; 264 } 265 266 static uint32_t stm32_rng_clock_freq_restrain(void) 267 { 268 struct stm32_rng_instance *dev = stm32_rng; 269 unsigned long clock_rate = 0; 270 uint32_t clock_div = 0; 271 272 clock_rate = clk_get_rate(dev->clock); 273 274 /* 275 * Get the exponent to apply on the CLKDIV field in RNG_CR register 276 * No need to handle the case when clock-div > 0xF as it is physically 277 * impossible 278 */ 279 while ((clock_rate >> clock_div) > dev->ddata->max_noise_clk_freq) 280 clock_div++; 281 282 DMSG("RNG clk rate : %lu", clk_get_rate(dev->clock) >> clock_div); 283 284 return clock_div; 285 } 286 287 static TEE_Result init_rng(void) 288 { 289 vaddr_t rng_base = get_base(); 290 uint32_t cr_ced_mask = 0; 291 uint32_t value = 0; 292 293 if (!stm32_rng->clock_error) 294 cr_ced_mask = RNG_CR_CED; 295 296 /* Clean error indications */ 297 io_write32(rng_base + RNG_SR, 0); 298 299 if (stm32_rng->ddata->has_cond_reset) { 300 uint32_t clock_div = stm32_rng_clock_freq_restrain(); 301 302 /* 303 * Keep default RNG configuration if none was specified. 304 * 0 is an invalid value as it disables all entropy sources. 305 */ 306 if (!stm32_rng->rng_config) 307 stm32_rng->rng_config = io_read32(rng_base + RNG_CR) & 308 RNG_CR_ENTROPY_SRC_MASK; 309 310 /* 311 * Configuration must be set in the same access that sets 312 * RNG_CR_CONDRST bit. Otherwise, the configuration setting is 313 * not taken into account. CONFIGLOCK bit is always cleared at 314 * this stage. 315 */ 316 io_clrsetbits32(rng_base + RNG_CR, RNG_CONFIG_MASK, 317 stm32_rng->rng_config | RNG_CR_CONDRST | 318 cr_ced_mask | 319 SHIFT_U32(clock_div, RNG_CR_CLKDIV_SHIFT)); 320 321 /* 322 * Write health test and noise source control configuration 323 * according to current RNG entropy source configuration 324 */ 325 if (stm32_rng->noise_ctrl_conf) 326 io_write32(rng_base + RNG_NSCR, 327 stm32_rng->noise_ctrl_conf); 328 329 if (stm32_rng->health_test_conf) 330 io_write32(rng_base + RNG_HTCR, 331 stm32_rng->health_test_conf); 332 333 io_clrsetbits32(rng_base + RNG_CR, RNG_CR_CONDRST, 334 RNG_CR_RNGEN); 335 336 if (IO_READ32_POLL_TIMEOUT(rng_base + RNG_CR, value, 337 !(value & RNG_CR_CONDRST), 0, 338 RNG_READY_TIMEOUT_US)) 339 panic(); 340 341 DMSG("RNG control register %#"PRIx32, 342 io_read32(rng_base + RNG_CR)); 343 DMSG("RNG noise source control register %#"PRIx32, 344 io_read32(rng_base + RNG_NSCR)); 345 DMSG("RNG health test register %#"PRIx32, 346 io_read32(rng_base + RNG_HTCR)); 347 } else { 348 io_setbits32(rng_base + RNG_CR, RNG_CR_RNGEN | cr_ced_mask); 349 } 350 351 if (IO_READ32_POLL_TIMEOUT(rng_base + RNG_SR, value, 352 value & RNG_SR_DRDY, 0, 353 RNG_READY_TIMEOUT_US)) 354 return TEE_ERROR_GENERIC; 355 356 return TEE_SUCCESS; 357 } 358 359 static TEE_Result stm32_rng_read(uint8_t *out, size_t size) 360 { 361 TEE_Result rc = TEE_ERROR_GENERIC; 362 bool burst_timeout = false; 363 uint64_t timeout_ref = 0; 364 uint32_t exceptions = 0; 365 uint8_t *out_ptr = out; 366 vaddr_t rng_base = 0; 367 size_t out_size = 0; 368 369 if (!stm32_rng) { 370 DMSG("No RNG"); 371 return TEE_ERROR_NOT_SUPPORTED; 372 } 373 374 rc = clk_enable(stm32_rng->clock); 375 if (rc) 376 return rc; 377 378 if (stm32_rng->bus_clock) { 379 rc = clk_enable(stm32_rng->bus_clock); 380 if (rc) { 381 clk_disable(stm32_rng->clock); 382 return rc; 383 } 384 } 385 386 rng_base = get_base(); 387 388 /* Arm timeout */ 389 timeout_ref = timeout_init_us(RNG_READY_TIMEOUT_US); 390 burst_timeout = false; 391 392 while (out_size < size) { 393 /* Read by chunks of the size the RNG FIFO depth */ 394 size_t sz = size - out_size; 395 396 exceptions = may_spin_lock(&stm32_rng->lock); 397 398 rc = read_available(rng_base, out_ptr, &sz); 399 400 /* Raise timeout only if we failed to get some samples */ 401 assert(!rc || rc == TEE_ERROR_NO_DATA); 402 if (rc) 403 burst_timeout = timeout_elapsed(timeout_ref); 404 405 may_spin_unlock(&stm32_rng->lock, exceptions); 406 407 if (burst_timeout) { 408 rc = TEE_ERROR_GENERIC; 409 goto out; 410 } 411 412 if (!rc) { 413 out_size += sz; 414 out_ptr += sz; 415 /* Re-arm timeout */ 416 timeout_ref = timeout_init_us(RNG_READY_TIMEOUT_US); 417 burst_timeout = false; 418 } 419 } 420 421 out: 422 assert(!rc || rc == TEE_ERROR_GENERIC); 423 clk_disable(stm32_rng->clock); 424 if (stm32_rng->bus_clock) 425 clk_disable(stm32_rng->bus_clock); 426 427 return rc; 428 } 429 430 #ifdef CFG_WITH_SOFTWARE_PRNG 431 /* Override weak plat_rng_init with platform handler to attempt to seed PRNG */ 432 void plat_rng_init(void) 433 { 434 uint8_t seed[RNG_FIFO_BYTE_DEPTH] = { }; 435 436 if (!stm32_rng) { 437 __plat_rng_init(); 438 DMSG("PRNG seeded without RNG"); 439 return; 440 } 441 442 if (stm32_rng_read(seed, sizeof(seed))) 443 panic(); 444 445 if (crypto_rng_init(seed, sizeof(seed))) 446 panic(); 447 448 DMSG("PRNG seeded with RNG"); 449 } 450 #else 451 TEE_Result hw_get_random_bytes(void *out, size_t size) 452 { 453 return stm32_rng_read(out, size); 454 } 455 456 void plat_rng_init(void) 457 { 458 } 459 #endif 460 461 static TEE_Result stm32_rng_pm_resume(void) 462 { 463 vaddr_t base = get_base(); 464 465 /* Clean error indications */ 466 io_write32(base + RNG_SR, 0); 467 468 if (stm32_rng->ddata->has_cond_reset) { 469 uint64_t timeout_ref = 0; 470 471 /* 472 * Configuration must be set in the same access that sets 473 * RNG_CR_CONDRST bit. Otherwise, the configuration setting is 474 * not taken into account. CONFIGLOCK bit is always cleared in 475 * this configuration. 476 */ 477 io_write32(base + RNG_CR, stm32_rng->pm_cr | RNG_CR_CONDRST); 478 479 /* Restore health test and noise control configuration */ 480 io_write32(base + RNG_NSCR, stm32_rng->pm_noise_ctrl); 481 io_write32(base + RNG_HTCR, stm32_rng->pm_health); 482 483 io_clrsetbits32(base + RNG_CR, RNG_CR_CONDRST, RNG_CR_RNGEN); 484 485 timeout_ref = timeout_init_us(RNG_READY_TIMEOUT_US); 486 while (io_read32(base + RNG_CR) & RNG_CR_CONDRST) 487 if (timeout_elapsed(timeout_ref)) 488 break; 489 if (io_read32(base + RNG_CR) & RNG_CR_CONDRST) 490 panic(); 491 } else { 492 io_write32(base + RNG_CR, RNG_CR_RNGEN | stm32_rng->pm_cr); 493 } 494 495 return TEE_SUCCESS; 496 } 497 498 static TEE_Result stm32_rng_pm_suspend(void) 499 { 500 vaddr_t rng_base = get_base(); 501 502 stm32_rng->pm_cr = io_read32(rng_base + RNG_CR); 503 504 if (stm32_rng->ddata->has_cond_reset) { 505 stm32_rng->pm_health = io_read32(rng_base + RNG_HTCR); 506 stm32_rng->pm_noise_ctrl = io_read32(rng_base + RNG_NSCR); 507 } 508 509 if (stm32_rng->ddata->has_power_optim) { 510 uint64_t timeout_ref = 0; 511 512 /* 513 * As per reference manual, it is recommended to set 514 * RNG_CONFIG2[bit0] when RNG power consumption is critical. 515 */ 516 io_setbits32(rng_base + RNG_CR, RNG_CR_POWER_OPTIM | 517 RNG_CR_CONDRST); 518 io_clrbits32(rng_base + RNG_CR, RNG_CR_CONDRST); 519 520 timeout_ref = timeout_init_us(RNG_READY_TIMEOUT_US); 521 while (io_read32(rng_base + RNG_CR) & RNG_CR_CONDRST) 522 if (timeout_elapsed(timeout_ref)) 523 break; 524 if (io_read32(rng_base + RNG_CR) & RNG_CR_CONDRST) 525 panic(); 526 } else { 527 io_clrbits32(rng_base + RNG_CR, RNG_CR_RNGEN); 528 } 529 530 return TEE_SUCCESS; 531 } 532 533 static TEE_Result 534 stm32_rng_pm(enum pm_op op, unsigned int pm_hint __unused, 535 const struct pm_callback_handle *pm_handle __unused) 536 { 537 TEE_Result res = TEE_ERROR_GENERIC; 538 539 assert(stm32_rng && (op == PM_OP_SUSPEND || op == PM_OP_RESUME)); 540 541 res = clk_enable(stm32_rng->clock); 542 if (res) 543 return res; 544 545 if (stm32_rng->bus_clock) { 546 res = clk_enable(stm32_rng->bus_clock); 547 if (res) { 548 clk_disable(stm32_rng->clock); 549 return res; 550 } 551 } 552 553 if (op == PM_OP_RESUME) 554 res = stm32_rng_pm_resume(); 555 else 556 res = stm32_rng_pm_suspend(); 557 558 clk_disable(stm32_rng->clock); 559 if (stm32_rng->bus_clock) 560 clk_disable(stm32_rng->bus_clock); 561 562 return res; 563 } 564 DECLARE_KEEP_PAGER(stm32_rng_pm); 565 566 static TEE_Result stm32_rng_parse_fdt(const void *fdt, int node) 567 { 568 TEE_Result res = TEE_ERROR_GENERIC; 569 struct dt_node_info dt_rng = { }; 570 571 fdt_fill_device_info(fdt, &dt_rng, node); 572 if (dt_rng.reg == DT_INFO_INVALID_REG) 573 return TEE_ERROR_BAD_PARAMETERS; 574 575 stm32_rng->base.pa = dt_rng.reg; 576 stm32_rng->base.va = io_pa_or_va_secure(&stm32_rng->base, 577 dt_rng.reg_size); 578 assert(stm32_rng->base.va); 579 580 res = rstctrl_dt_get_by_index(fdt, node, 0, &stm32_rng->rstctrl); 581 if (res != TEE_SUCCESS && res != TEE_ERROR_ITEM_NOT_FOUND) 582 return res; 583 584 if (stm32_rng->ddata->nb_clock > 1) { 585 res = clk_dt_get_by_name(fdt, node, "rng_clk", 586 &stm32_rng->clock); 587 if (res) 588 return res; 589 590 res = clk_dt_get_by_name(fdt, node, "rng_hclk", 591 &stm32_rng->bus_clock); 592 if (res) 593 return res; 594 } else { 595 res = clk_dt_get_by_index(fdt, node, 0, &stm32_rng->clock); 596 if (res) 597 return res; 598 } 599 600 if (fdt_getprop(fdt, node, "clock-error-detect", NULL)) 601 stm32_rng->clock_error = true; 602 603 /* Release device if not used after initialization */ 604 stm32_rng->release_post_boot = IS_ENABLED(CFG_WITH_SOFTWARE_PRNG); 605 606 stm32_rng->rng_config = stm32_rng->ddata->cr; 607 if (stm32_rng->rng_config & ~RNG_CR_ENTROPY_SRC_MASK) 608 panic("Incorrect entropy source configuration"); 609 stm32_rng->health_test_conf = stm32_rng->ddata->htcr; 610 stm32_rng->noise_ctrl_conf = stm32_rng->ddata->nscr; 611 if (stm32_rng->noise_ctrl_conf & ~RNG_NSCR_MASK) 612 panic("Incorrect noise source control configuration"); 613 614 return TEE_SUCCESS; 615 } 616 617 static TEE_Result stm32_rng_probe(const void *fdt, int offs, 618 const void *compat_data) 619 { 620 TEE_Result res = TEE_ERROR_GENERIC; 621 unsigned int __maybe_unused version = 0; 622 623 /* Expect a single RNG instance */ 624 assert(!stm32_rng); 625 626 stm32_rng = calloc(1, sizeof(*stm32_rng)); 627 if (!stm32_rng) 628 panic(); 629 630 stm32_rng->ddata = compat_data; 631 assert(stm32_rng->ddata); 632 633 res = stm32_rng_parse_fdt(fdt, offs); 634 if (res) 635 goto err; 636 637 res = clk_enable(stm32_rng->clock); 638 if (res) 639 goto err; 640 641 if (stm32_rng->bus_clock) { 642 res = clk_enable(stm32_rng->bus_clock); 643 if (res) { 644 clk_disable(stm32_rng->clock); 645 goto err; 646 } 647 } 648 649 version = io_read32(get_base() + RNG_VERR); 650 DMSG("RNG version Major %u, Minor %u", 651 (version & RNG_VERR_MAJOR_MASK) >> RNG_VERR_MAJOR_SHIFT, 652 version & RNG_VERR_MINOR_MASK); 653 654 if (stm32_rng->rstctrl && 655 rstctrl_assert_to(stm32_rng->rstctrl, RNG_RESET_TIMEOUT_US)) { 656 res = TEE_ERROR_GENERIC; 657 goto err_clk; 658 } 659 660 if (stm32_rng->rstctrl && 661 rstctrl_deassert_to(stm32_rng->rstctrl, RNG_RESET_TIMEOUT_US)) { 662 res = TEE_ERROR_GENERIC; 663 goto err_clk; 664 } 665 666 res = init_rng(); 667 if (res) 668 goto err_clk; 669 670 clk_disable(stm32_rng->clock); 671 if (stm32_rng->bus_clock) 672 clk_disable(stm32_rng->bus_clock); 673 674 if (stm32_rng->release_post_boot) 675 stm32mp_register_non_secure_periph_iomem(stm32_rng->base.pa); 676 else 677 stm32mp_register_secure_periph_iomem(stm32_rng->base.pa); 678 679 /* Power management implementation expects both or none are set */ 680 assert(stm32_rng->ddata->has_power_optim == 681 stm32_rng->ddata->has_cond_reset); 682 683 register_pm_core_service_cb(stm32_rng_pm, &stm32_rng, "rng-service"); 684 685 return TEE_SUCCESS; 686 687 err_clk: 688 clk_disable(stm32_rng->clock); 689 if (stm32_rng->bus_clock) 690 clk_disable(stm32_rng->bus_clock); 691 err: 692 free(stm32_rng); 693 stm32_rng = NULL; 694 695 return res; 696 } 697 698 static const struct stm32_rng_driver_data mp13_data[] = { 699 { 700 .max_noise_clk_freq = U(48000000), 701 .nb_clock = 1, 702 .has_cond_reset = true, 703 .has_power_optim = true, 704 .cr = 0x00F00D00, 705 .nscr = 0x2B5BB, 706 .htcr = 0x969D, 707 }, 708 }; 709 710 static const struct stm32_rng_driver_data mp15_data[] = { 711 { 712 .max_noise_clk_freq = U(48000000), 713 .nb_clock = 1, 714 .has_cond_reset = false, 715 .has_power_optim = false, 716 }, 717 }; 718 DECLARE_KEEP_PAGER(mp15_data); 719 720 static const struct stm32_rng_driver_data mp25_data[] = { 721 { 722 .max_noise_clk_freq = U(48000000), 723 .nb_clock = 2, 724 .has_cond_reset = true, 725 .has_power_optim = true, 726 .cr = 0x00F00D00, 727 .nscr = 0x2B5BB, 728 .htcr = 0x969D, 729 }, 730 }; 731 732 static const struct dt_device_match rng_match_table[] = { 733 { .compatible = "st,stm32-rng", .compat_data = &mp15_data }, 734 { .compatible = "st,stm32mp13-rng", .compat_data = &mp13_data }, 735 { .compatible = "st,stm32mp25-rng", .compat_data = &mp25_data }, 736 { } 737 }; 738 739 DEFINE_DT_DRIVER(stm32_rng_dt_driver) = { 740 .name = "stm32_rng", 741 .match_table = rng_match_table, 742 .probe = stm32_rng_probe, 743 }; 744 745 static TEE_Result stm32_rng_release(void) 746 { 747 if (stm32_rng && stm32_rng->release_post_boot) { 748 DMSG("Release RNG driver"); 749 free(stm32_rng); 750 stm32_rng = NULL; 751 } 752 753 return TEE_SUCCESS; 754 } 755 756 release_init_resource(stm32_rng_release); 757