airoha-trng.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2024 Christian Marangi */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/bitfield.h>
  7. #include <linux/delay.h>
  8. #include <linux/hw_random.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/platform_device.h>
  13. #define TRNG_IP_RDY 0x800
  14. #define CNT_TRANS GENMASK(15, 8)
  15. #define SAMPLE_RDY BIT(0)
  16. #define TRNG_NS_SEK_AND_DAT_EN 0x804
  17. #define RNG_EN BIT(31) /* referenced as ring_en */
  18. #define RAW_DATA_EN BIT(16)
  19. #define TRNG_HEALTH_TEST_SW_RST 0x808
  20. #define SW_RST BIT(0) /* Active High */
  21. #define TRNG_INTR_EN 0x818
  22. #define INTR_MASK BIT(16)
  23. #define CONTINUOUS_HEALTH_INITR_EN BIT(2)
  24. #define SW_STARTUP_INITR_EN BIT(1)
  25. #define RST_STARTUP_INITR_EN BIT(0)
  26. /* Notice that Health Test are done only out of Reset and with RNG_EN */
  27. #define TRNG_HEALTH_TEST_STATUS 0x824
  28. #define CONTINUOUS_HEALTH_AP_TEST_FAIL BIT(23)
  29. #define CONTINUOUS_HEALTH_RC_TEST_FAIL BIT(22)
  30. #define SW_STARTUP_TEST_DONE BIT(21)
  31. #define SW_STARTUP_AP_TEST_FAIL BIT(20)
  32. #define SW_STARTUP_RC_TEST_FAIL BIT(19)
  33. #define RST_STARTUP_TEST_DONE BIT(18)
  34. #define RST_STARTUP_AP_TEST_FAIL BIT(17)
  35. #define RST_STARTUP_RC_TEST_FAIL BIT(16)
  36. #define RAW_DATA_VALID BIT(7)
  37. #define TRNG_RAW_DATA_OUT 0x828
  38. #define TRNG_CNT_TRANS_VALID 0x80
  39. #define BUSY_LOOP_SLEEP 10
  40. #define BUSY_LOOP_TIMEOUT (BUSY_LOOP_SLEEP * 10000)
  41. struct airoha_trng {
  42. void __iomem *base;
  43. struct hwrng rng;
  44. struct device *dev;
  45. struct completion rng_op_done;
  46. };
  47. static int airoha_trng_irq_mask(struct airoha_trng *trng)
  48. {
  49. u32 val;
  50. val = readl(trng->base + TRNG_INTR_EN);
  51. val |= INTR_MASK;
  52. writel(val, trng->base + TRNG_INTR_EN);
  53. return 0;
  54. }
  55. static int airoha_trng_irq_unmask(struct airoha_trng *trng)
  56. {
  57. u32 val;
  58. val = readl(trng->base + TRNG_INTR_EN);
  59. val &= ~INTR_MASK;
  60. writel(val, trng->base + TRNG_INTR_EN);
  61. return 0;
  62. }
  63. static int airoha_trng_init(struct hwrng *rng)
  64. {
  65. struct airoha_trng *trng = container_of(rng, struct airoha_trng, rng);
  66. int ret;
  67. u32 val;
  68. val = readl(trng->base + TRNG_NS_SEK_AND_DAT_EN);
  69. val |= RNG_EN;
  70. writel(val, trng->base + TRNG_NS_SEK_AND_DAT_EN);
  71. /* Set out of SW Reset */
  72. airoha_trng_irq_unmask(trng);
  73. writel(0, trng->base + TRNG_HEALTH_TEST_SW_RST);
  74. ret = wait_for_completion_timeout(&trng->rng_op_done, BUSY_LOOP_TIMEOUT);
  75. if (ret <= 0) {
  76. dev_err(trng->dev, "Timeout waiting for Health Check\n");
  77. airoha_trng_irq_mask(trng);
  78. return -ENODEV;
  79. }
  80. /* Check if Health Test Failed */
  81. val = readl(trng->base + TRNG_HEALTH_TEST_STATUS);
  82. if (val & (RST_STARTUP_AP_TEST_FAIL | RST_STARTUP_RC_TEST_FAIL)) {
  83. dev_err(trng->dev, "Health Check fail: %s test fail\n",
  84. val & RST_STARTUP_AP_TEST_FAIL ? "AP" : "RC");
  85. return -ENODEV;
  86. }
  87. /* Check if IP is ready */
  88. ret = readl_poll_timeout(trng->base + TRNG_IP_RDY, val,
  89. val & SAMPLE_RDY, 10, 1000);
  90. if (ret < 0) {
  91. dev_err(trng->dev, "Timeout waiting for IP ready");
  92. return -ENODEV;
  93. }
  94. /* CNT_TRANS must be 0x80 for IP to be considered ready */
  95. ret = readl_poll_timeout(trng->base + TRNG_IP_RDY, val,
  96. FIELD_GET(CNT_TRANS, val) == TRNG_CNT_TRANS_VALID,
  97. 10, 1000);
  98. if (ret < 0) {
  99. dev_err(trng->dev, "Timeout waiting for IP ready");
  100. return -ENODEV;
  101. }
  102. return 0;
  103. }
  104. static void airoha_trng_cleanup(struct hwrng *rng)
  105. {
  106. struct airoha_trng *trng = container_of(rng, struct airoha_trng, rng);
  107. u32 val;
  108. val = readl(trng->base + TRNG_NS_SEK_AND_DAT_EN);
  109. val &= ~RNG_EN;
  110. writel(val, trng->base + TRNG_NS_SEK_AND_DAT_EN);
  111. /* Put it in SW Reset */
  112. writel(SW_RST, trng->base + TRNG_HEALTH_TEST_SW_RST);
  113. }
  114. static int airoha_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  115. {
  116. struct airoha_trng *trng = container_of(rng, struct airoha_trng, rng);
  117. u32 *data = buf;
  118. u32 status;
  119. int ret;
  120. ret = readl_poll_timeout(trng->base + TRNG_HEALTH_TEST_STATUS, status,
  121. status & RAW_DATA_VALID, 10, 1000);
  122. if (ret < 0) {
  123. dev_err(trng->dev, "Timeout waiting for TRNG RAW Data valid\n");
  124. return ret;
  125. }
  126. *data = readl(trng->base + TRNG_RAW_DATA_OUT);
  127. return 4;
  128. }
  129. static irqreturn_t airoha_trng_irq(int irq, void *priv)
  130. {
  131. struct airoha_trng *trng = (struct airoha_trng *)priv;
  132. airoha_trng_irq_mask(trng);
  133. /* Just complete the task, we will read the value later */
  134. complete(&trng->rng_op_done);
  135. return IRQ_HANDLED;
  136. }
  137. static int airoha_trng_probe(struct platform_device *pdev)
  138. {
  139. struct device *dev = &pdev->dev;
  140. struct airoha_trng *trng;
  141. int irq, ret;
  142. u32 val;
  143. trng = devm_kzalloc(dev, sizeof(*trng), GFP_KERNEL);
  144. if (!trng)
  145. return -ENOMEM;
  146. trng->base = devm_platform_ioremap_resource(pdev, 0);
  147. if (IS_ERR(trng->base))
  148. return PTR_ERR(trng->base);
  149. irq = platform_get_irq(pdev, 0);
  150. if (irq < 0)
  151. return irq;
  152. airoha_trng_irq_mask(trng);
  153. ret = devm_request_irq(&pdev->dev, irq, airoha_trng_irq, 0,
  154. pdev->name, (void *)trng);
  155. if (ret) {
  156. dev_err(dev, "Can't get interrupt working.\n");
  157. return ret;
  158. }
  159. init_completion(&trng->rng_op_done);
  160. /* Enable interrupt for SW reset Health Check */
  161. val = readl(trng->base + TRNG_INTR_EN);
  162. val |= RST_STARTUP_INITR_EN;
  163. writel(val, trng->base + TRNG_INTR_EN);
  164. /* Set output to raw data */
  165. val = readl(trng->base + TRNG_NS_SEK_AND_DAT_EN);
  166. val |= RAW_DATA_EN;
  167. writel(val, trng->base + TRNG_NS_SEK_AND_DAT_EN);
  168. /* Put it in SW Reset */
  169. writel(SW_RST, trng->base + TRNG_HEALTH_TEST_SW_RST);
  170. trng->dev = dev;
  171. trng->rng.name = pdev->name;
  172. trng->rng.init = airoha_trng_init;
  173. trng->rng.cleanup = airoha_trng_cleanup;
  174. trng->rng.read = airoha_trng_read;
  175. trng->rng.quality = 900;
  176. ret = devm_hwrng_register(dev, &trng->rng);
  177. if (ret) {
  178. dev_err(dev, "failed to register rng device: %d\n", ret);
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static const struct of_device_id airoha_trng_of_match[] = {
  184. { .compatible = "airoha,en7581-trng", },
  185. {},
  186. };
  187. MODULE_DEVICE_TABLE(of, airoha_trng_of_match);
  188. static struct platform_driver airoha_trng_driver = {
  189. .driver = {
  190. .name = "airoha-trng",
  191. .of_match_table = airoha_trng_of_match,
  192. },
  193. .probe = airoha_trng_probe,
  194. };
  195. module_platform_driver(airoha_trng_driver);
  196. MODULE_LICENSE("GPL");
  197. MODULE_AUTHOR("Christian Marangi <ansuelsmth@gmail.com>");
  198. MODULE_DESCRIPTION("Airoha True Random Number Generator driver");