xiphera-trng.c 3.8 KB

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