stm32-rng.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/reset.h>
  24. #include <linux/slab.h>
  25. #define RNG_CR 0x00
  26. #define RNG_CR_RNGEN BIT(2)
  27. #define RNG_SR 0x04
  28. #define RNG_SR_SEIS BIT(6)
  29. #define RNG_SR_CEIS BIT(5)
  30. #define RNG_SR_DRDY BIT(0)
  31. #define RNG_DR 0x08
  32. /*
  33. * It takes 40 cycles @ 48MHz to generate each random number (e.g. <1us).
  34. * At the time of writing STM32 parts max out at ~200MHz meaning a timeout
  35. * of 500 leaves us a very comfortable margin for error. The loop to which
  36. * the timeout applies takes at least 4 instructions per iteration so the
  37. * timeout is enough to take us up to multi-GHz parts!
  38. */
  39. #define RNG_TIMEOUT 500
  40. struct stm32_rng_private {
  41. struct hwrng rng;
  42. void __iomem *base;
  43. struct clk *clk;
  44. struct reset_control *rst;
  45. };
  46. static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  47. {
  48. struct stm32_rng_private *priv =
  49. container_of(rng, struct stm32_rng_private, rng);
  50. u32 sr;
  51. int retval = 0;
  52. pm_runtime_get_sync((struct device *) priv->rng.priv);
  53. while (max > sizeof(u32)) {
  54. sr = readl_relaxed(priv->base + RNG_SR);
  55. if (!sr && wait) {
  56. unsigned int timeout = RNG_TIMEOUT;
  57. do {
  58. cpu_relax();
  59. sr = readl_relaxed(priv->base + RNG_SR);
  60. } while (!sr && --timeout);
  61. }
  62. /* If error detected or data not ready... */
  63. if (sr != RNG_SR_DRDY)
  64. break;
  65. *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
  66. retval += sizeof(u32);
  67. data += sizeof(u32);
  68. max -= sizeof(u32);
  69. }
  70. if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
  71. "bad RNG status - %x\n", sr))
  72. writel_relaxed(0, priv->base + RNG_SR);
  73. pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
  74. pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
  75. return retval || !wait ? retval : -EIO;
  76. }
  77. static int stm32_rng_init(struct hwrng *rng)
  78. {
  79. struct stm32_rng_private *priv =
  80. container_of(rng, struct stm32_rng_private, rng);
  81. int err;
  82. err = clk_prepare_enable(priv->clk);
  83. if (err)
  84. return err;
  85. writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
  86. /* clear error indicators */
  87. writel_relaxed(0, priv->base + RNG_SR);
  88. return 0;
  89. }
  90. static void stm32_rng_cleanup(struct hwrng *rng)
  91. {
  92. struct stm32_rng_private *priv =
  93. container_of(rng, struct stm32_rng_private, rng);
  94. writel_relaxed(0, priv->base + RNG_CR);
  95. clk_disable_unprepare(priv->clk);
  96. }
  97. static int stm32_rng_probe(struct platform_device *ofdev)
  98. {
  99. struct device *dev = &ofdev->dev;
  100. struct device_node *np = ofdev->dev.of_node;
  101. struct stm32_rng_private *priv;
  102. struct resource res;
  103. int err;
  104. priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
  105. if (!priv)
  106. return -ENOMEM;
  107. err = of_address_to_resource(np, 0, &res);
  108. if (err)
  109. return err;
  110. priv->base = devm_ioremap_resource(dev, &res);
  111. if (IS_ERR(priv->base))
  112. return PTR_ERR(priv->base);
  113. priv->clk = devm_clk_get(&ofdev->dev, NULL);
  114. if (IS_ERR(priv->clk))
  115. return PTR_ERR(priv->clk);
  116. priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
  117. if (!IS_ERR(priv->rst)) {
  118. reset_control_assert(priv->rst);
  119. udelay(2);
  120. reset_control_deassert(priv->rst);
  121. }
  122. dev_set_drvdata(dev, priv);
  123. priv->rng.name = dev_driver_string(dev),
  124. #ifndef CONFIG_PM
  125. priv->rng.init = stm32_rng_init,
  126. priv->rng.cleanup = stm32_rng_cleanup,
  127. #endif
  128. priv->rng.read = stm32_rng_read,
  129. priv->rng.priv = (unsigned long) dev;
  130. pm_runtime_set_autosuspend_delay(dev, 100);
  131. pm_runtime_use_autosuspend(dev);
  132. pm_runtime_enable(dev);
  133. return devm_hwrng_register(dev, &priv->rng);
  134. }
  135. #ifdef CONFIG_PM
  136. static int stm32_rng_runtime_suspend(struct device *dev)
  137. {
  138. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  139. stm32_rng_cleanup(&priv->rng);
  140. return 0;
  141. }
  142. static int stm32_rng_runtime_resume(struct device *dev)
  143. {
  144. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  145. return stm32_rng_init(&priv->rng);
  146. }
  147. #endif
  148. static UNIVERSAL_DEV_PM_OPS(stm32_rng_pm_ops, stm32_rng_runtime_suspend,
  149. stm32_rng_runtime_resume, NULL);
  150. static const struct of_device_id stm32_rng_match[] = {
  151. {
  152. .compatible = "st,stm32-rng",
  153. },
  154. {},
  155. };
  156. MODULE_DEVICE_TABLE(of, stm32_rng_match);
  157. static struct platform_driver stm32_rng_driver = {
  158. .driver = {
  159. .name = "stm32-rng",
  160. .pm = &stm32_rng_pm_ops,
  161. .of_match_table = stm32_rng_match,
  162. },
  163. .probe = stm32_rng_probe,
  164. };
  165. module_platform_driver(stm32_rng_driver);
  166. MODULE_LICENSE("GPL");
  167. MODULE_AUTHOR("Daniel Thompson <daniel.thompson@linaro.org>");
  168. MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");