stm32-rng.c 5.4 KB

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