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. break;
  63. *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
  64. retval += sizeof(u32);
  65. data += sizeof(u32);
  66. max -= sizeof(u32);
  67. }
  68. if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
  69. "bad RNG status - %x\n", sr))
  70. writel_relaxed(0, priv->base + RNG_SR);
  71. pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
  72. pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
  73. return retval || !wait ? retval : -EIO;
  74. }
  75. static int stm32_rng_init(struct hwrng *rng)
  76. {
  77. struct stm32_rng_private *priv =
  78. container_of(rng, struct stm32_rng_private, rng);
  79. int err;
  80. err = clk_prepare_enable(priv->clk);
  81. if (err)
  82. return err;
  83. writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
  84. /* clear error indicators */
  85. writel_relaxed(0, priv->base + RNG_SR);
  86. return 0;
  87. }
  88. static void stm32_rng_cleanup(struct hwrng *rng)
  89. {
  90. struct stm32_rng_private *priv =
  91. container_of(rng, struct stm32_rng_private, rng);
  92. writel_relaxed(0, priv->base + RNG_CR);
  93. clk_disable_unprepare(priv->clk);
  94. }
  95. static int stm32_rng_probe(struct platform_device *ofdev)
  96. {
  97. struct device *dev = &ofdev->dev;
  98. struct device_node *np = ofdev->dev.of_node;
  99. struct stm32_rng_private *priv;
  100. struct resource res;
  101. int err;
  102. priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
  103. if (!priv)
  104. return -ENOMEM;
  105. err = of_address_to_resource(np, 0, &res);
  106. if (err)
  107. return err;
  108. priv->base = devm_ioremap_resource(dev, &res);
  109. if (IS_ERR(priv->base))
  110. return PTR_ERR(priv->base);
  111. priv->clk = devm_clk_get(&ofdev->dev, NULL);
  112. if (IS_ERR(priv->clk))
  113. return PTR_ERR(priv->clk);
  114. dev_set_drvdata(dev, priv);
  115. priv->rng.name = dev_driver_string(dev),
  116. #ifndef CONFIG_PM
  117. priv->rng.init = stm32_rng_init,
  118. priv->rng.cleanup = stm32_rng_cleanup,
  119. #endif
  120. priv->rng.read = stm32_rng_read,
  121. priv->rng.priv = (unsigned long) dev;
  122. pm_runtime_set_autosuspend_delay(dev, 100);
  123. pm_runtime_use_autosuspend(dev);
  124. pm_runtime_enable(dev);
  125. return devm_hwrng_register(dev, &priv->rng);
  126. }
  127. #ifdef CONFIG_PM
  128. static int stm32_rng_runtime_suspend(struct device *dev)
  129. {
  130. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  131. stm32_rng_cleanup(&priv->rng);
  132. return 0;
  133. }
  134. static int stm32_rng_runtime_resume(struct device *dev)
  135. {
  136. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  137. return stm32_rng_init(&priv->rng);
  138. }
  139. #endif
  140. static UNIVERSAL_DEV_PM_OPS(stm32_rng_pm_ops, stm32_rng_runtime_suspend,
  141. stm32_rng_runtime_resume, NULL);
  142. static const struct of_device_id stm32_rng_match[] = {
  143. {
  144. .compatible = "st,stm32-rng",
  145. },
  146. {},
  147. };
  148. MODULE_DEVICE_TABLE(of, stm32_rng_match);
  149. static struct platform_driver stm32_rng_driver = {
  150. .driver = {
  151. .name = "stm32-rng",
  152. .pm = &stm32_rng_pm_ops,
  153. .of_match_table = stm32_rng_match,
  154. },
  155. .probe = stm32_rng_probe,
  156. };
  157. module_platform_driver(stm32_rng_driver);
  158. MODULE_LICENSE("GPL");
  159. MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
  160. MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");