pic32-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. do {
  60. t = readl(priv->base + RNGRCNT) & RCNT_MASK;
  61. if (t == 64) {
  62. /* TRNG value comes through the seed registers */
  63. *data = ((u64)readl(priv->base + RNGSEED2) << 32) +
  64. readl(priv->base + RNGSEED1);
  65. return 8;
  66. }
  67. } while (wait && --timeout);
  68. return -EIO;
  69. }
  70. static int pic32_rng_probe(struct platform_device *pdev)
  71. {
  72. struct pic32_rng *priv;
  73. struct resource *res;
  74. u32 v;
  75. int ret;
  76. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  77. if (!priv)
  78. return -ENOMEM;
  79. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  80. priv->base = devm_ioremap_resource(&pdev->dev, res);
  81. if (IS_ERR(priv->base))
  82. return PTR_ERR(priv->base);
  83. priv->clk = devm_clk_get(&pdev->dev, NULL);
  84. if (IS_ERR(priv->clk))
  85. return PTR_ERR(priv->clk);
  86. ret = clk_prepare_enable(priv->clk);
  87. if (ret)
  88. return ret;
  89. /* enable TRNG in enhanced mode */
  90. v = TRNGEN | TRNGMOD;
  91. writel(v, priv->base + RNGCON);
  92. priv->rng.name = pdev->name;
  93. priv->rng.read = pic32_rng_read;
  94. ret = hwrng_register(&priv->rng);
  95. if (ret)
  96. goto err_register;
  97. platform_set_drvdata(pdev, priv);
  98. return 0;
  99. err_register:
  100. clk_disable_unprepare(priv->clk);
  101. return ret;
  102. }
  103. static int pic32_rng_remove(struct platform_device *pdev)
  104. {
  105. struct pic32_rng *rng = platform_get_drvdata(pdev);
  106. hwrng_unregister(&rng->rng);
  107. writel(0, rng->base + RNGCON);
  108. clk_disable_unprepare(rng->clk);
  109. return 0;
  110. }
  111. static const struct of_device_id pic32_rng_of_match[] = {
  112. { .compatible = "microchip,pic32mzda-rng", },
  113. { /* sentinel */ }
  114. };
  115. MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
  116. static struct platform_driver pic32_rng_driver = {
  117. .probe = pic32_rng_probe,
  118. .remove = pic32_rng_remove,
  119. .driver = {
  120. .name = "pic32-rng",
  121. .of_match_table = of_match_ptr(pic32_rng_of_match),
  122. },
  123. };
  124. module_platform_driver(pic32_rng_driver);
  125. MODULE_LICENSE("GPL");
  126. MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
  127. MODULE_DESCRIPTION("Microchip PIC32 RNG Driver");