pic32-rng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PIC32 RNG driver
  4. *
  5. * Joshua Henderson <joshua.henderson@microchip.com>
  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/mod_devicetable.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #define RNGCON 0x04
  19. #define TRNGEN BIT(8)
  20. #define TRNGMOD BIT(11)
  21. #define RNGSEED1 0x18
  22. #define RNGSEED2 0x1C
  23. #define RNGRCNT 0x20
  24. #define RCNT_MASK 0x7F
  25. struct pic32_rng {
  26. void __iomem *base;
  27. struct hwrng rng;
  28. };
  29. /*
  30. * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
  31. * enough given the instructions in the loop and that the TRNG may not always
  32. * be at maximum rate.
  33. */
  34. #define RNG_TIMEOUT 500
  35. static int pic32_rng_init(struct hwrng *rng)
  36. {
  37. struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
  38. /* enable TRNG in enhanced mode */
  39. writel(TRNGEN | TRNGMOD, priv->base + RNGCON);
  40. return 0;
  41. }
  42. static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
  43. bool wait)
  44. {
  45. struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
  46. u64 *data = buf;
  47. u32 t;
  48. unsigned int timeout = RNG_TIMEOUT;
  49. do {
  50. t = readl(priv->base + RNGRCNT) & RCNT_MASK;
  51. if (t == 64) {
  52. /* TRNG value comes through the seed registers */
  53. *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
  54. readl(priv->base + RNGSEED1);
  55. return 8;
  56. }
  57. } while (wait && --timeout);
  58. return -EIO;
  59. }
  60. static void pic32_rng_cleanup(struct hwrng *rng)
  61. {
  62. struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
  63. writel(0, priv->base + RNGCON);
  64. }
  65. static int pic32_rng_probe(struct platform_device *pdev)
  66. {
  67. struct pic32_rng *priv;
  68. struct clk *clk;
  69. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  70. if (!priv)
  71. return -ENOMEM;
  72. priv->base = devm_platform_ioremap_resource(pdev, 0);
  73. if (IS_ERR(priv->base))
  74. return PTR_ERR(priv->base);
  75. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  76. if (IS_ERR(clk))
  77. return PTR_ERR(clk);
  78. priv->rng.name = pdev->name;
  79. priv->rng.init = pic32_rng_init;
  80. priv->rng.read = pic32_rng_read;
  81. priv->rng.cleanup = pic32_rng_cleanup;
  82. return devm_hwrng_register(&pdev->dev, &priv->rng);
  83. }
  84. static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
  85. { .compatible = "microchip,pic32mzda-rng", },
  86. { /* sentinel */ }
  87. };
  88. MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
  89. static struct platform_driver pic32_rng_driver = {
  90. .probe = pic32_rng_probe,
  91. .driver = {
  92. .name = "pic32-rng",
  93. .of_match_table = pic32_rng_of_match,
  94. },
  95. };
  96. module_platform_driver(pic32_rng_driver);
  97. MODULE_LICENSE("GPL");
  98. MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
  99. MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");