qcom-rng.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-18 Linaro Limited
  3. //
  4. // Based on msm-rng.c and downstream driver
  5. #include <crypto/internal/rng.h>
  6. #include <linux/acpi.h>
  7. #include <linux/clk.h>
  8. #include <linux/crypto.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. /* Device specific register offsets */
  13. #define PRNG_DATA_OUT 0x0000
  14. #define PRNG_STATUS 0x0004
  15. #define PRNG_LFSR_CFG 0x0100
  16. #define PRNG_CONFIG 0x0104
  17. /* Device specific register masks and config values */
  18. #define PRNG_LFSR_CFG_MASK 0x0000ffff
  19. #define PRNG_LFSR_CFG_CLOCKS 0x0000dddd
  20. #define PRNG_CONFIG_HW_ENABLE BIT(1)
  21. #define PRNG_STATUS_DATA_AVAIL BIT(0)
  22. #define WORD_SZ 4
  23. struct qcom_rng {
  24. struct mutex lock;
  25. void __iomem *base;
  26. struct clk *clk;
  27. unsigned int skip_init;
  28. };
  29. struct qcom_rng_ctx {
  30. struct qcom_rng *rng;
  31. };
  32. static struct qcom_rng *qcom_rng_dev;
  33. static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max)
  34. {
  35. unsigned int currsize = 0;
  36. u32 val;
  37. /* read random data from hardware */
  38. do {
  39. val = readl_relaxed(rng->base + PRNG_STATUS);
  40. if (!(val & PRNG_STATUS_DATA_AVAIL))
  41. break;
  42. val = readl_relaxed(rng->base + PRNG_DATA_OUT);
  43. if (!val)
  44. break;
  45. if ((max - currsize) >= WORD_SZ) {
  46. memcpy(data, &val, WORD_SZ);
  47. data += WORD_SZ;
  48. currsize += WORD_SZ;
  49. } else {
  50. /* copy only remaining bytes */
  51. memcpy(data, &val, max - currsize);
  52. break;
  53. }
  54. } while (currsize < max);
  55. return currsize;
  56. }
  57. static int qcom_rng_generate(struct crypto_rng *tfm,
  58. const u8 *src, unsigned int slen,
  59. u8 *dstn, unsigned int dlen)
  60. {
  61. struct qcom_rng_ctx *ctx = crypto_rng_ctx(tfm);
  62. struct qcom_rng *rng = ctx->rng;
  63. int ret;
  64. ret = clk_prepare_enable(rng->clk);
  65. if (ret)
  66. return ret;
  67. mutex_lock(&rng->lock);
  68. ret = qcom_rng_read(rng, dstn, dlen);
  69. mutex_unlock(&rng->lock);
  70. clk_disable_unprepare(rng->clk);
  71. return 0;
  72. }
  73. static int qcom_rng_seed(struct crypto_rng *tfm, const u8 *seed,
  74. unsigned int slen)
  75. {
  76. return 0;
  77. }
  78. static int qcom_rng_enable(struct qcom_rng *rng)
  79. {
  80. u32 val;
  81. int ret;
  82. ret = clk_prepare_enable(rng->clk);
  83. if (ret)
  84. return ret;
  85. /* Enable PRNG only if it is not already enabled */
  86. val = readl_relaxed(rng->base + PRNG_CONFIG);
  87. if (val & PRNG_CONFIG_HW_ENABLE)
  88. goto already_enabled;
  89. val = readl_relaxed(rng->base + PRNG_LFSR_CFG);
  90. val &= ~PRNG_LFSR_CFG_MASK;
  91. val |= PRNG_LFSR_CFG_CLOCKS;
  92. writel(val, rng->base + PRNG_LFSR_CFG);
  93. val = readl_relaxed(rng->base + PRNG_CONFIG);
  94. val |= PRNG_CONFIG_HW_ENABLE;
  95. writel(val, rng->base + PRNG_CONFIG);
  96. already_enabled:
  97. clk_disable_unprepare(rng->clk);
  98. return 0;
  99. }
  100. static int qcom_rng_init(struct crypto_tfm *tfm)
  101. {
  102. struct qcom_rng_ctx *ctx = crypto_tfm_ctx(tfm);
  103. ctx->rng = qcom_rng_dev;
  104. if (!ctx->rng->skip_init)
  105. return qcom_rng_enable(ctx->rng);
  106. return 0;
  107. }
  108. static struct rng_alg qcom_rng_alg = {
  109. .generate = qcom_rng_generate,
  110. .seed = qcom_rng_seed,
  111. .seedsize = 0,
  112. .base = {
  113. .cra_name = "stdrng",
  114. .cra_driver_name = "qcom-rng",
  115. .cra_flags = CRYPTO_ALG_TYPE_RNG,
  116. .cra_priority = 300,
  117. .cra_ctxsize = sizeof(struct qcom_rng_ctx),
  118. .cra_module = THIS_MODULE,
  119. .cra_init = qcom_rng_init,
  120. }
  121. };
  122. static int qcom_rng_probe(struct platform_device *pdev)
  123. {
  124. struct resource *res;
  125. struct qcom_rng *rng;
  126. int ret;
  127. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  128. if (!rng)
  129. return -ENOMEM;
  130. platform_set_drvdata(pdev, rng);
  131. mutex_init(&rng->lock);
  132. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. rng->base = devm_ioremap_resource(&pdev->dev, res);
  134. if (IS_ERR(rng->base))
  135. return PTR_ERR(rng->base);
  136. /* ACPI systems have clk already on, so skip clk_get */
  137. if (!has_acpi_companion(&pdev->dev)) {
  138. rng->clk = devm_clk_get(&pdev->dev, "core");
  139. if (IS_ERR(rng->clk))
  140. return PTR_ERR(rng->clk);
  141. }
  142. rng->skip_init = (unsigned long)device_get_match_data(&pdev->dev);
  143. qcom_rng_dev = rng;
  144. ret = crypto_register_rng(&qcom_rng_alg);
  145. if (ret) {
  146. dev_err(&pdev->dev, "Register crypto rng failed: %d\n", ret);
  147. qcom_rng_dev = NULL;
  148. }
  149. return ret;
  150. }
  151. static int qcom_rng_remove(struct platform_device *pdev)
  152. {
  153. crypto_unregister_rng(&qcom_rng_alg);
  154. qcom_rng_dev = NULL;
  155. return 0;
  156. }
  157. #if IS_ENABLED(CONFIG_ACPI)
  158. static const struct acpi_device_id qcom_rng_acpi_match[] = {
  159. { .id = "QCOM8160", .driver_data = 1 },
  160. {}
  161. };
  162. MODULE_DEVICE_TABLE(acpi, qcom_rng_acpi_match);
  163. #endif
  164. static const struct of_device_id qcom_rng_of_match[] = {
  165. { .compatible = "qcom,prng", .data = (void *)0},
  166. { .compatible = "qcom,prng-ee", .data = (void *)1},
  167. {}
  168. };
  169. MODULE_DEVICE_TABLE(of, qcom_rng_of_match);
  170. static struct platform_driver qcom_rng_driver = {
  171. .probe = qcom_rng_probe,
  172. .remove = qcom_rng_remove,
  173. .driver = {
  174. .name = KBUILD_MODNAME,
  175. .of_match_table = of_match_ptr(qcom_rng_of_match),
  176. .acpi_match_table = ACPI_PTR(qcom_rng_acpi_match),
  177. }
  178. };
  179. module_platform_driver(qcom_rng_driver);
  180. MODULE_ALIAS("platform:" KBUILD_MODNAME);
  181. MODULE_DESCRIPTION("Qualcomm random number generator driver");
  182. MODULE_LICENSE("GPL v2");