stm32-rng.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2015, Daniel Thompson
  3. *
  4. * This file is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/hw_random.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/slab.h>
  24. #define RNG_CR 0x00
  25. #define RNG_CR_RNGEN BIT(2)
  26. #define RNG_SR 0x04
  27. #define RNG_SR_SEIS BIT(6)
  28. #define RNG_SR_CEIS BIT(5)
  29. #define RNG_SR_DRDY BIT(0)
  30. #define RNG_DR 0x08
  31. /*
  32. * It takes 40 cycles @ 48MHz to generate each random number (e.g. <1us).
  33. * At the time of writing STM32 parts max out at ~200MHz meaning a timeout
  34. * of 500 leaves us a very comfortable margin for error. The loop to which
  35. * the timeout applies takes at least 4 instructions per iteration so the
  36. * timeout is enough to take us up to multi-GHz parts!
  37. */
  38. #define RNG_TIMEOUT 500
  39. struct stm32_rng_private {
  40. struct hwrng rng;
  41. void __iomem *base;
  42. struct clk *clk;
  43. };
  44. static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  45. {
  46. struct stm32_rng_private *priv =
  47. container_of(rng, struct stm32_rng_private, rng);
  48. u32 sr;
  49. int retval = 0;
  50. pm_runtime_get_sync((struct device *) priv->rng.priv);
  51. while (max > sizeof(u32)) {
  52. sr = readl_relaxed(priv->base + RNG_SR);
  53. if (!sr && wait) {
  54. unsigned int timeout = RNG_TIMEOUT;
  55. do {
  56. cpu_relax();
  57. sr = readl_relaxed(priv->base + RNG_SR);
  58. } while (!sr && --timeout);
  59. }
  60. /* If error detected or data not ready... */
  61. if (sr != RNG_SR_DRDY) {
  62. if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
  63. "bad RNG status - %x\n", sr))
  64. writel_relaxed(0, priv->base + RNG_SR);
  65. break;
  66. }
  67. *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
  68. retval += sizeof(u32);
  69. data += sizeof(u32);
  70. max -= sizeof(u32);
  71. }
  72. pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
  73. pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
  74. return retval || !wait ? retval : -EIO;
  75. }
  76. static int stm32_rng_init(struct hwrng *rng)
  77. {
  78. struct stm32_rng_private *priv =
  79. container_of(rng, struct stm32_rng_private, rng);
  80. int err;
  81. err = clk_prepare_enable(priv->clk);
  82. if (err)
  83. return err;
  84. writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
  85. /* clear error indicators */
  86. writel_relaxed(0, priv->base + RNG_SR);
  87. return 0;
  88. }
  89. static void stm32_rng_cleanup(struct hwrng *rng)
  90. {
  91. struct stm32_rng_private *priv =
  92. container_of(rng, struct stm32_rng_private, rng);
  93. writel_relaxed(0, priv->base + RNG_CR);
  94. clk_disable_unprepare(priv->clk);
  95. }
  96. static int stm32_rng_probe(struct platform_device *ofdev)
  97. {
  98. struct device *dev = &ofdev->dev;
  99. struct device_node *np = ofdev->dev.of_node;
  100. struct stm32_rng_private *priv;
  101. struct resource res;
  102. int err;
  103. priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
  104. if (!priv)
  105. return -ENOMEM;
  106. err = of_address_to_resource(np, 0, &res);
  107. if (err)
  108. return err;
  109. priv->base = devm_ioremap_resource(dev, &res);
  110. if (IS_ERR(priv->base))
  111. return PTR_ERR(priv->base);
  112. priv->clk = devm_clk_get(&ofdev->dev, NULL);
  113. if (IS_ERR(priv->clk))
  114. return PTR_ERR(priv->clk);
  115. dev_set_drvdata(dev, priv);
  116. priv->rng.name = dev_driver_string(dev),
  117. #ifndef CONFIG_PM
  118. priv->rng.init = stm32_rng_init,
  119. priv->rng.cleanup = stm32_rng_cleanup,
  120. #endif
  121. priv->rng.read = stm32_rng_read,
  122. priv->rng.priv = (unsigned long) dev;
  123. pm_runtime_set_autosuspend_delay(dev, 100);
  124. pm_runtime_use_autosuspend(dev);
  125. pm_runtime_enable(dev);
  126. return devm_hwrng_register(dev, &priv->rng);
  127. }
  128. #ifdef CONFIG_PM
  129. static int stm32_rng_runtime_suspend(struct device *dev)
  130. {
  131. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  132. stm32_rng_cleanup(&priv->rng);
  133. return 0;
  134. }
  135. static int stm32_rng_runtime_resume(struct device *dev)
  136. {
  137. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  138. return stm32_rng_init(&priv->rng);
  139. }
  140. #endif
  141. static UNIVERSAL_DEV_PM_OPS(stm32_rng_pm_ops, stm32_rng_runtime_suspend,
  142. stm32_rng_runtime_resume, NULL);
  143. static const struct of_device_id stm32_rng_match[] = {
  144. {
  145. .compatible = "st,stm32-rng",
  146. },
  147. {},
  148. };
  149. MODULE_DEVICE_TABLE(of, stm32_rng_match);
  150. static struct platform_driver stm32_rng_driver = {
  151. .driver = {
  152. .name = "stm32-rng",
  153. .pm = &stm32_rng_pm_ops,
  154. .of_match_table = stm32_rng_match,
  155. },
  156. .probe = stm32_rng_probe,
  157. };
  158. module_platform_driver(stm32_rng_driver);
  159. MODULE_LICENSE("GPL");
  160. MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
  161. MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");