pic32-rng.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PIC32 RNG driver
  4. *
  5. * Joshua Henderson <[email protected]>
  6. * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clkdev.h>
  10. #include <linux/err.h>
  11. #include <linux/hw_random.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #define RNGCON 0x04
  20. #define TRNGEN BIT(8)
  21. #define PRNGEN BIT(9)
  22. #define PRNGCONT BIT(10)
  23. #define TRNGMOD BIT(11)
  24. #define SEEDLOAD BIT(12)
  25. #define RNGPOLY1 0x08
  26. #define RNGPOLY2 0x0C
  27. #define RNGNUMGEN1 0x10
  28. #define RNGNUMGEN2 0x14
  29. #define RNGSEED1 0x18
  30. #define RNGSEED2 0x1C
  31. #define RNGRCNT 0x20
  32. #define RCNT_MASK 0x7F
  33. struct pic32_rng {
  34. void __iomem *base;
  35. struct hwrng rng;
  36. };
  37. /*
  38. * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
  39. * enough given the instructions in the loop and that the TRNG may not always
  40. * be at maximum rate.
  41. */
  42. #define RNG_TIMEOUT 500
  43. static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
  44. bool wait)
  45. {
  46. struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
  47. u64 *data = buf;
  48. u32 t;
  49. unsigned int timeout = RNG_TIMEOUT;
  50. do {
  51. t = readl(priv->base + RNGRCNT) & RCNT_MASK;
  52. if (t == 64) {
  53. /* TRNG value comes through the seed registers */
  54. *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
  55. readl(priv->base + RNGSEED1);
  56. return 8;
  57. }
  58. } while (wait && --timeout);
  59. return -EIO;
  60. }
  61. static int pic32_rng_probe(struct platform_device *pdev)
  62. {
  63. struct pic32_rng *priv;
  64. struct clk *clk;
  65. u32 v;
  66. int ret;
  67. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  68. if (!priv)
  69. return -ENOMEM;
  70. priv->base = devm_platform_ioremap_resource(pdev, 0);
  71. if (IS_ERR(priv->base))
  72. return PTR_ERR(priv->base);
  73. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  74. if (IS_ERR(clk))
  75. return PTR_ERR(clk);
  76. /* enable TRNG in enhanced mode */
  77. v = TRNGEN | TRNGMOD;
  78. writel(v, priv->base + RNGCON);
  79. priv->rng.name = pdev->name;
  80. priv->rng.read = pic32_rng_read;
  81. ret = devm_hwrng_register(&pdev->dev, &priv->rng);
  82. if (ret)
  83. return ret;
  84. platform_set_drvdata(pdev, priv);
  85. return 0;
  86. }
  87. static int pic32_rng_remove(struct platform_device *pdev)
  88. {
  89. struct pic32_rng *rng = platform_get_drvdata(pdev);
  90. writel(0, rng->base + RNGCON);
  91. return 0;
  92. }
  93. static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
  94. { .compatible = "microchip,pic32mzda-rng", },
  95. { /* sentinel */ }
  96. };
  97. MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
  98. static struct platform_driver pic32_rng_driver = {
  99. .probe = pic32_rng_probe,
  100. .remove = pic32_rng_remove,
  101. .driver = {
  102. .name = "pic32-rng",
  103. .of_match_table = of_match_ptr(pic32_rng_of_match),
  104. },
  105. };
  106. module_platform_driver(pic32_rng_driver);
  107. MODULE_LICENSE("GPL");
  108. MODULE_AUTHOR("Joshua Henderson <[email protected]>");
  109. MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");