stm32-rng.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2015, Daniel Thompson
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/hw_random.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/reset.h>
  16. #include <linux/slab.h>
  17. #define RNG_CR 0x00
  18. #define RNG_CR_RNGEN BIT(2)
  19. #define RNG_CR_CED BIT(5)
  20. #define RNG_SR 0x04
  21. #define RNG_SR_SEIS BIT(6)
  22. #define RNG_SR_CEIS BIT(5)
  23. #define RNG_SR_DRDY BIT(0)
  24. #define RNG_DR 0x08
  25. struct stm32_rng_private {
  26. struct hwrng rng;
  27. void __iomem *base;
  28. struct clk *clk;
  29. struct reset_control *rst;
  30. bool ced;
  31. };
  32. static int stm32_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  33. {
  34. struct stm32_rng_private *priv =
  35. container_of(rng, struct stm32_rng_private, rng);
  36. u32 sr;
  37. int retval = 0;
  38. pm_runtime_get_sync((struct device *) priv->rng.priv);
  39. while (max > sizeof(u32)) {
  40. sr = readl_relaxed(priv->base + RNG_SR);
  41. /* Manage timeout which is based on timer and take */
  42. /* care of initial delay time when enabling rng */
  43. if (!sr && wait) {
  44. retval = readl_relaxed_poll_timeout_atomic(priv->base
  45. + RNG_SR,
  46. sr, sr,
  47. 10, 50000);
  48. if (retval)
  49. dev_err((struct device *)priv->rng.priv,
  50. "%s: timeout %x!\n", __func__, sr);
  51. }
  52. /* If error detected or data not ready... */
  53. if (sr != RNG_SR_DRDY) {
  54. if (WARN_ONCE(sr & (RNG_SR_SEIS | RNG_SR_CEIS),
  55. "bad RNG status - %x\n", sr))
  56. writel_relaxed(0, priv->base + RNG_SR);
  57. break;
  58. }
  59. *(u32 *)data = readl_relaxed(priv->base + RNG_DR);
  60. retval += sizeof(u32);
  61. data += sizeof(u32);
  62. max -= sizeof(u32);
  63. }
  64. pm_runtime_mark_last_busy((struct device *) priv->rng.priv);
  65. pm_runtime_put_sync_autosuspend((struct device *) priv->rng.priv);
  66. return retval || !wait ? retval : -EIO;
  67. }
  68. static int stm32_rng_init(struct hwrng *rng)
  69. {
  70. struct stm32_rng_private *priv =
  71. container_of(rng, struct stm32_rng_private, rng);
  72. int err;
  73. err = clk_prepare_enable(priv->clk);
  74. if (err)
  75. return err;
  76. if (priv->ced)
  77. writel_relaxed(RNG_CR_RNGEN, priv->base + RNG_CR);
  78. else
  79. writel_relaxed(RNG_CR_RNGEN | RNG_CR_CED,
  80. priv->base + RNG_CR);
  81. /* clear error indicators */
  82. writel_relaxed(0, priv->base + RNG_SR);
  83. return 0;
  84. }
  85. static void stm32_rng_cleanup(struct hwrng *rng)
  86. {
  87. struct stm32_rng_private *priv =
  88. container_of(rng, struct stm32_rng_private, rng);
  89. writel_relaxed(0, priv->base + RNG_CR);
  90. clk_disable_unprepare(priv->clk);
  91. }
  92. static int stm32_rng_probe(struct platform_device *ofdev)
  93. {
  94. struct device *dev = &ofdev->dev;
  95. struct device_node *np = ofdev->dev.of_node;
  96. struct stm32_rng_private *priv;
  97. struct resource res;
  98. int err;
  99. priv = devm_kzalloc(dev, sizeof(struct stm32_rng_private), GFP_KERNEL);
  100. if (!priv)
  101. return -ENOMEM;
  102. err = of_address_to_resource(np, 0, &res);
  103. if (err)
  104. return err;
  105. priv->base = devm_ioremap_resource(dev, &res);
  106. if (IS_ERR(priv->base))
  107. return PTR_ERR(priv->base);
  108. priv->clk = devm_clk_get(&ofdev->dev, NULL);
  109. if (IS_ERR(priv->clk))
  110. return PTR_ERR(priv->clk);
  111. priv->rst = devm_reset_control_get(&ofdev->dev, NULL);
  112. if (!IS_ERR(priv->rst)) {
  113. reset_control_assert(priv->rst);
  114. udelay(2);
  115. reset_control_deassert(priv->rst);
  116. }
  117. priv->ced = of_property_read_bool(np, "clock-error-detect");
  118. dev_set_drvdata(dev, priv);
  119. priv->rng.name = dev_driver_string(dev);
  120. #ifndef CONFIG_PM
  121. priv->rng.init = stm32_rng_init;
  122. priv->rng.cleanup = stm32_rng_cleanup;
  123. #endif
  124. priv->rng.read = stm32_rng_read;
  125. priv->rng.priv = (unsigned long) dev;
  126. priv->rng.quality = 900;
  127. pm_runtime_set_autosuspend_delay(dev, 100);
  128. pm_runtime_use_autosuspend(dev);
  129. pm_runtime_enable(dev);
  130. return devm_hwrng_register(dev, &priv->rng);
  131. }
  132. static int stm32_rng_remove(struct platform_device *ofdev)
  133. {
  134. pm_runtime_disable(&ofdev->dev);
  135. return 0;
  136. }
  137. #ifdef CONFIG_PM
  138. static int stm32_rng_runtime_suspend(struct device *dev)
  139. {
  140. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  141. stm32_rng_cleanup(&priv->rng);
  142. return 0;
  143. }
  144. static int stm32_rng_runtime_resume(struct device *dev)
  145. {
  146. struct stm32_rng_private *priv = dev_get_drvdata(dev);
  147. return stm32_rng_init(&priv->rng);
  148. }
  149. #endif
  150. static const struct dev_pm_ops stm32_rng_pm_ops = {
  151. SET_RUNTIME_PM_OPS(stm32_rng_runtime_suspend,
  152. stm32_rng_runtime_resume, NULL)
  153. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  154. pm_runtime_force_resume)
  155. };
  156. static const struct of_device_id stm32_rng_match[] = {
  157. {
  158. .compatible = "st,stm32-rng",
  159. },
  160. {},
  161. };
  162. MODULE_DEVICE_TABLE(of, stm32_rng_match);
  163. static struct platform_driver stm32_rng_driver = {
  164. .driver = {
  165. .name = "stm32-rng",
  166. .pm = &stm32_rng_pm_ops,
  167. .of_match_table = stm32_rng_match,
  168. },
  169. .probe = stm32_rng_probe,
  170. .remove = stm32_rng_remove,
  171. };
  172. module_platform_driver(stm32_rng_driver);
  173. MODULE_LICENSE("GPL");
  174. MODULE_AUTHOR("Daniel Thompson <[email protected]>");
  175. MODULE_DESCRIPTION("STMicroelectronics STM32 RNG device driver");