ingenic-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ingenic Random Number Generator driver
  4. * Copyright (c) 2017 PrasannaKumar Muralidharan <[email protected]>
  5. * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <[email protected]>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/kernel.h>
  9. #include <linux/hw_random.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. /* RNG register offsets */
  17. #define RNG_REG_ERNG_OFFSET 0x0
  18. #define RNG_REG_RNG_OFFSET 0x4
  19. /* bits within the ERND register */
  20. #define ERNG_READY BIT(31)
  21. #define ERNG_ENABLE BIT(0)
  22. enum ingenic_rng_version {
  23. ID_JZ4780,
  24. ID_X1000,
  25. };
  26. /* Device associated memory */
  27. struct ingenic_rng {
  28. enum ingenic_rng_version version;
  29. void __iomem *base;
  30. struct hwrng rng;
  31. };
  32. static int ingenic_rng_init(struct hwrng *rng)
  33. {
  34. struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
  35. writel(ERNG_ENABLE, priv->base + RNG_REG_ERNG_OFFSET);
  36. return 0;
  37. }
  38. static void ingenic_rng_cleanup(struct hwrng *rng)
  39. {
  40. struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
  41. writel(0, priv->base + RNG_REG_ERNG_OFFSET);
  42. }
  43. static int ingenic_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  44. {
  45. struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
  46. u32 *data = buf;
  47. u32 status;
  48. int ret;
  49. if (priv->version >= ID_X1000) {
  50. ret = readl_poll_timeout(priv->base + RNG_REG_ERNG_OFFSET, status,
  51. status & ERNG_READY, 10, 1000);
  52. if (ret == -ETIMEDOUT) {
  53. pr_err("%s: Wait for RNG data ready timeout\n", __func__);
  54. return ret;
  55. }
  56. } else {
  57. /*
  58. * A delay is required so that the current RNG data is not bit shifted
  59. * version of previous RNG data which could happen if random data is
  60. * read continuously from this device.
  61. */
  62. udelay(20);
  63. }
  64. *data = readl(priv->base + RNG_REG_RNG_OFFSET);
  65. return 4;
  66. }
  67. static int ingenic_rng_probe(struct platform_device *pdev)
  68. {
  69. struct ingenic_rng *priv;
  70. int ret;
  71. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  72. if (!priv)
  73. return -ENOMEM;
  74. priv->base = devm_platform_ioremap_resource(pdev, 0);
  75. if (IS_ERR(priv->base)) {
  76. pr_err("%s: Failed to map RNG registers\n", __func__);
  77. return PTR_ERR(priv->base);
  78. }
  79. priv->version = (enum ingenic_rng_version)of_device_get_match_data(&pdev->dev);
  80. priv->rng.name = pdev->name;
  81. priv->rng.init = ingenic_rng_init;
  82. priv->rng.cleanup = ingenic_rng_cleanup;
  83. priv->rng.read = ingenic_rng_read;
  84. ret = hwrng_register(&priv->rng);
  85. if (ret) {
  86. dev_err(&pdev->dev, "Failed to register hwrng\n");
  87. return ret;
  88. }
  89. platform_set_drvdata(pdev, priv);
  90. dev_info(&pdev->dev, "Ingenic RNG driver registered\n");
  91. return 0;
  92. }
  93. static int ingenic_rng_remove(struct platform_device *pdev)
  94. {
  95. struct ingenic_rng *priv = platform_get_drvdata(pdev);
  96. hwrng_unregister(&priv->rng);
  97. writel(0, priv->base + RNG_REG_ERNG_OFFSET);
  98. return 0;
  99. }
  100. static const struct of_device_id ingenic_rng_of_match[] = {
  101. { .compatible = "ingenic,jz4780-rng", .data = (void *) ID_JZ4780 },
  102. { .compatible = "ingenic,x1000-rng", .data = (void *) ID_X1000 },
  103. { /* sentinel */ }
  104. };
  105. MODULE_DEVICE_TABLE(of, ingenic_rng_of_match);
  106. static struct platform_driver ingenic_rng_driver = {
  107. .probe = ingenic_rng_probe,
  108. .remove = ingenic_rng_remove,
  109. .driver = {
  110. .name = "ingenic-rng",
  111. .of_match_table = ingenic_rng_of_match,
  112. },
  113. };
  114. module_platform_driver(ingenic_rng_driver);
  115. MODULE_LICENSE("GPL");
  116. MODULE_AUTHOR("PrasannaKumar Muralidharan <[email protected]>");
  117. MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <[email protected]>");
  118. MODULE_DESCRIPTION("Ingenic Random Number Generator driver");