rng_plat.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2024, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <common/debug.h>
  10. #include <drivers/delay_timer.h>
  11. #include <lib/mmio.h>
  12. #include <lib/smccc.h>
  13. #include <lib/spinlock.h>
  14. #include <plat/common/platform.h>
  15. #include <platform_def.h>
  16. #include <services/trng_svc.h>
  17. #include <smccc_helpers.h>
  18. #include <mtk_mmap_pool.h>
  19. #include <mtk_sip_svc.h>
  20. #include "rng_plat.h"
  21. static spinlock_t rng_lock;
  22. static int trng_wait(uint32_t reg, uint32_t expected_value)
  23. {
  24. uint64_t timeout = timeout_init_us(TRNG_TIME_OUT);
  25. uint32_t value = 0;
  26. do {
  27. value = mmio_read_32(reg);
  28. if ((value & expected_value) == expected_value)
  29. return 0;
  30. udelay(10);
  31. } while (!timeout_elapsed(timeout));
  32. return -ETIMEDOUT;
  33. }
  34. static int trng_write(uint32_t reg, uint32_t value,
  35. uint32_t read_reg, uint32_t expected_value)
  36. {
  37. int retry = MTK_TRNG_MAX_ROUND;
  38. uint32_t read_value = 0;
  39. do {
  40. mmio_write_32(reg, value);
  41. read_value = mmio_read_32(read_reg);
  42. if ((read_value & value) == expected_value)
  43. return 0;
  44. udelay(10);
  45. } while (--retry > 0);
  46. return -ETIMEDOUT;
  47. }
  48. static uint32_t trng_prng(uint32_t *rand)
  49. {
  50. int32_t ret = 0;
  51. uint32_t seed[4] = {0};
  52. if (rand == NULL)
  53. return MTK_SIP_E_INVALID_PARAM;
  54. /* ungate */
  55. ret = trng_write(TRNG_PDN_CLR, TRNG_PDN_VALUE, TRNG_PDN_STATUS, 0);
  56. if (ret) {
  57. ERROR("%s: ungate fail\n", __func__);
  58. return MTK_SIP_E_NOT_SUPPORTED;
  59. }
  60. /* read random data once and drop it */
  61. seed[0] = mmio_read_32(TRNG_DATA);
  62. /* enable von-neumann extractor */
  63. mmio_setbits_32(TRNG_CONF, TRNG_CONF_VON_EN);
  64. /* start */
  65. mmio_setbits_32(TRNG_CTRL, TRNG_CTRL_START);
  66. /* get seeds from trng */
  67. for (int i = 0; i < ARRAY_SIZE(seed); i++) {
  68. ret = trng_wait(TRNG_CTRL, TRNG_CTRL_RDY);
  69. if (ret) {
  70. ERROR("%s: trng NOT ready\n", __func__);
  71. return MTK_SIP_E_NOT_SUPPORTED;
  72. }
  73. seed[i] = mmio_read_32(TRNG_DATA);
  74. }
  75. /* stop */
  76. mmio_clrbits_32(TRNG_CTRL, TRNG_CTRL_START);
  77. /* gate */
  78. ret = trng_write(TRNG_PDN_SET, TRNG_PDN_VALUE, TRNG_PDN_STATUS, TRNG_PDN_VALUE);
  79. if (ret) {
  80. ERROR("%s: gate fail\n", __func__);
  81. return MTK_SIP_E_NOT_SUPPORTED;
  82. }
  83. for (int i = 0; i < ARRAY_SIZE(seed); i++)
  84. rand[i] = seed[i];
  85. return 0;
  86. }
  87. static uint32_t get_true_rnd(uint32_t *val, uint32_t num)
  88. {
  89. uint32_t rand[4] = {0};
  90. uint32_t ret;
  91. if (val == NULL || num > ARRAY_SIZE(rand))
  92. return MTK_SIP_E_INVALID_PARAM;
  93. spin_lock(&rng_lock);
  94. ret = trng_prng(rand);
  95. spin_unlock(&rng_lock);
  96. for (int i = 0; i < num; i++)
  97. val[i] = rand[i];
  98. return ret;
  99. }
  100. /*
  101. * plat_get_entropy - get 64-bit random number data which is used form
  102. * atf early stage
  103. * output - out: output 64-bit entropy combine with 2 32-bit random number
  104. */
  105. bool plat_get_entropy(uint64_t *out)
  106. {
  107. uint32_t entropy_pool[2] = {0};
  108. uint32_t ret;
  109. assert(out);
  110. assert(!check_uptr_overflow((uintptr_t)out, sizeof(*out)));
  111. /* Get 2 32-bits entropy */
  112. ret = get_true_rnd(entropy_pool, ARRAY_SIZE(entropy_pool));
  113. if (ret)
  114. return false;
  115. /* Output 8 bytes entropy combine with 2 32-bit random number. */
  116. *out = ((uint64_t)entropy_pool[0] << 32) | entropy_pool[1];
  117. return true;
  118. }