xiphera-trng.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2020 Xiphera Ltd. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/err.h>
  7. #include <linux/io.h>
  8. #include <linux/hw_random.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/delay.h>
  12. #define CONTROL_REG 0x00000000
  13. #define STATUS_REG 0x00000004
  14. #define RAND_REG 0x00000000
  15. #define HOST_TO_TRNG_RESET 0x00000001
  16. #define HOST_TO_TRNG_RELEASE_RESET 0x00000002
  17. #define HOST_TO_TRNG_ENABLE 0x80000000
  18. #define HOST_TO_TRNG_ZEROIZE 0x80000004
  19. #define HOST_TO_TRNG_ACK_ZEROIZE 0x80000008
  20. #define HOST_TO_TRNG_READ 0x8000000F
  21. /* trng statuses */
  22. #define TRNG_ACK_RESET 0x000000AC
  23. #define TRNG_SUCCESSFUL_STARTUP 0x00000057
  24. #define TRNG_FAILED_STARTUP 0x000000FA
  25. #define TRNG_NEW_RAND_AVAILABLE 0x000000ED
  26. struct xiphera_trng {
  27. void __iomem *mem;
  28. struct hwrng rng;
  29. };
  30. static int xiphera_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  31. {
  32. struct xiphera_trng *trng = container_of(rng, struct xiphera_trng, rng);
  33. int ret = 0;
  34. while (max >= sizeof(u32)) {
  35. /* check for data */
  36. if (readl(trng->mem + STATUS_REG) == TRNG_NEW_RAND_AVAILABLE) {
  37. *(u32 *)buf = readl(trng->mem + RAND_REG);
  38. /*
  39. * Inform the trng of the read
  40. * and re-enable it to produce a new random number
  41. */
  42. writel(HOST_TO_TRNG_READ, trng->mem + CONTROL_REG);
  43. writel(HOST_TO_TRNG_ENABLE, trng->mem + CONTROL_REG);
  44. ret += sizeof(u32);
  45. buf += sizeof(u32);
  46. max -= sizeof(u32);
  47. } else {
  48. break;
  49. }
  50. }
  51. return ret;
  52. }
  53. static int xiphera_trng_probe(struct platform_device *pdev)
  54. {
  55. int ret;
  56. struct xiphera_trng *trng;
  57. struct device *dev = &pdev->dev;
  58. trng = devm_kzalloc(dev, sizeof(*trng), GFP_KERNEL);
  59. if (!trng)
  60. return -ENOMEM;
  61. trng->mem = devm_platform_ioremap_resource(pdev, 0);
  62. if (IS_ERR(trng->mem))
  63. return PTR_ERR(trng->mem);
  64. /*
  65. * the trng needs to be reset first which might not happen in time,
  66. * hence we incorporate a small delay to ensure proper behaviour
  67. */
  68. writel(HOST_TO_TRNG_RESET, trng->mem + CONTROL_REG);
  69. usleep_range(100, 200);
  70. if (readl(trng->mem + STATUS_REG) != TRNG_ACK_RESET) {
  71. /*
  72. * there is a small chance the trng is just not ready yet,
  73. * so we try one more time. If the second time fails, we give up
  74. */
  75. usleep_range(100, 200);
  76. if (readl(trng->mem + STATUS_REG) != TRNG_ACK_RESET) {
  77. dev_err(dev, "failed to reset the trng ip\n");
  78. return -ENODEV;
  79. }
  80. }
  81. /*
  82. * once again, to ensure proper behaviour we sleep
  83. * for a while after zeroizing the trng
  84. */
  85. writel(HOST_TO_TRNG_RELEASE_RESET, trng->mem + CONTROL_REG);
  86. writel(HOST_TO_TRNG_ENABLE, trng->mem + CONTROL_REG);
  87. writel(HOST_TO_TRNG_ZEROIZE, trng->mem + CONTROL_REG);
  88. msleep(20);
  89. if (readl(trng->mem + STATUS_REG) != TRNG_SUCCESSFUL_STARTUP) {
  90. /* diagnose the reason for the failure */
  91. if (readl(trng->mem + STATUS_REG) == TRNG_FAILED_STARTUP) {
  92. dev_err(dev, "trng ip startup-tests failed\n");
  93. return -ENODEV;
  94. }
  95. dev_err(dev, "startup-tests yielded no response\n");
  96. return -ENODEV;
  97. }
  98. writel(HOST_TO_TRNG_ACK_ZEROIZE, trng->mem + CONTROL_REG);
  99. trng->rng.name = pdev->name;
  100. trng->rng.read = xiphera_trng_read;
  101. trng->rng.quality = 900;
  102. ret = devm_hwrng_register(dev, &trng->rng);
  103. if (ret) {
  104. dev_err(dev, "failed to register rng device: %d\n", ret);
  105. return ret;
  106. }
  107. platform_set_drvdata(pdev, trng);
  108. return 0;
  109. }
  110. static const struct of_device_id xiphera_trng_of_match[] = {
  111. { .compatible = "xiphera,xip8001b-trng", },
  112. {},
  113. };
  114. MODULE_DEVICE_TABLE(of, xiphera_trng_of_match);
  115. static struct platform_driver xiphera_trng_driver = {
  116. .driver = {
  117. .name = "xiphera-trng",
  118. .of_match_table = xiphera_trng_of_match,
  119. },
  120. .probe = xiphera_trng_probe,
  121. };
  122. module_platform_driver(xiphera_trng_driver);
  123. MODULE_LICENSE("GPL");
  124. MODULE_AUTHOR("Atte Tommiska");
  125. MODULE_DESCRIPTION("Xiphera FPGA-based true random number generator driver");