pic32-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * PIC32 RNG driver
  3. *
  4. * Joshua Henderson <joshua.henderson@microchip.com>
  5. * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
  6. *
  7. * This program is free software; you can distribute it and/or modify it
  8. * under the terms of the GNU General Public License (Version 2) as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/err.h>
  19. #include <linux/hw_random.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #define RNGCON 0x04
  28. #define TRNGEN BIT(8)
  29. #define PRNGEN BIT(9)
  30. #define PRNGCONT BIT(10)
  31. #define TRNGMOD BIT(11)
  32. #define SEEDLOAD BIT(12)
  33. #define RNGPOLY1 0x08
  34. #define RNGPOLY2 0x0C
  35. #define RNGNUMGEN1 0x10
  36. #define RNGNUMGEN2 0x14
  37. #define RNGSEED1 0x18
  38. #define RNGSEED2 0x1C
  39. #define RNGRCNT 0x20
  40. #define RCNT_MASK 0x7F
  41. struct pic32_rng {
  42. void __iomem *base;
  43. struct hwrng rng;
  44. struct clk *clk;
  45. };
  46. /*
  47. * The TRNG can generate up to 24Mbps. This is a timeout that should be safe
  48. * enough given the instructions in the loop and that the TRNG may not always
  49. * be at maximum rate.
  50. */
  51. #define RNG_TIMEOUT 500
  52. static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
  53. bool wait)
  54. {
  55. struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
  56. u64 *data = buf;
  57. u32 t;
  58. unsigned int timeout = RNG_TIMEOUT;
  59. if (max < 8)
  60. return 0;
  61. do {
  62. t = readl(priv->base + RNGRCNT) & RCNT_MASK;
  63. if (t == 64) {
  64. /* TRNG value comes through the seed registers */
  65. *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
  66. readl(priv->base + RNGSEED1);
  67. return 8;
  68. }
  69. } while (wait && --timeout);
  70. return -EIO;
  71. }
  72. static int pic32_rng_probe(struct platform_device *pdev)
  73. {
  74. struct pic32_rng *priv;
  75. struct resource *res;
  76. u32 v;
  77. int ret;
  78. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  79. if (!priv)
  80. return -ENOMEM;
  81. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  82. priv->base = devm_ioremap_resource(&pdev->dev, res);
  83. if (IS_ERR(priv->base))
  84. return PTR_ERR(priv->base);
  85. priv->clk = devm_clk_get(&pdev->dev, NULL);
  86. if (IS_ERR(priv->clk))
  87. return PTR_ERR(priv->clk);
  88. ret = clk_prepare_enable(priv->clk);
  89. if (ret)
  90. return ret;
  91. /* enable TRNG in enhanced mode */
  92. v = TRNGEN | TRNGMOD;
  93. writel(v, priv->base + RNGCON);
  94. priv->rng.name = pdev->name;
  95. priv->rng.read = pic32_rng_read;
  96. ret = hwrng_register(&priv->rng);
  97. if (ret)
  98. goto err_register;
  99. platform_set_drvdata(pdev, priv);
  100. return 0;
  101. err_register:
  102. clk_disable_unprepare(priv->clk);
  103. return ret;
  104. }
  105. static int pic32_rng_remove(struct platform_device *pdev)
  106. {
  107. struct pic32_rng *rng = platform_get_drvdata(pdev);
  108. hwrng_unregister(&rng->rng);
  109. writel(0, rng->base + RNGCON);
  110. clk_disable_unprepare(rng->clk);
  111. return 0;
  112. }
  113. static const struct of_device_id pic32_rng_of_match[] = {
  114. { .compatible = "microchip,pic32mzda-rng", },
  115. { /* sentinel */ }
  116. };
  117. MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
  118. static struct platform_driver pic32_rng_driver = {
  119. .probe = pic32_rng_probe,
  120. .remove = pic32_rng_remove,
  121. .driver = {
  122. .name = "pic32-rng",
  123. .of_match_table = of_match_ptr(pic32_rng_of_match),
  124. },
  125. };
  126. module_platform_driver(pic32_rng_driver);
  127. MODULE_LICENSE("GPL");
  128. MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
  129. MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");