exynos-rng.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * exynos-rng.c - Random Number Generator driver for the exynos
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Jonghwa Lee <jonghwa3.lee@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation;
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/hw_random.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/io.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/clk.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/err.h>
  29. #define EXYNOS_PRNG_STATUS_OFFSET 0x10
  30. #define EXYNOS_PRNG_SEED_OFFSET 0x140
  31. #define EXYNOS_PRNG_OUT1_OFFSET 0x160
  32. #define SEED_SETTING_DONE BIT(1)
  33. #define PRNG_START 0x18
  34. #define PRNG_DONE BIT(5)
  35. #define EXYNOS_AUTOSUSPEND_DELAY 100
  36. struct exynos_rng {
  37. struct device *dev;
  38. struct hwrng rng;
  39. void __iomem *mem;
  40. struct clk *clk;
  41. };
  42. static u32 exynos_rng_readl(struct exynos_rng *rng, u32 offset)
  43. {
  44. return readl_relaxed(rng->mem + offset);
  45. }
  46. static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset)
  47. {
  48. writel_relaxed(val, rng->mem + offset);
  49. }
  50. static int exynos_rng_configure(struct exynos_rng *exynos_rng)
  51. {
  52. int i;
  53. int ret = 0;
  54. for (i = 0 ; i < 5 ; i++)
  55. exynos_rng_writel(exynos_rng, jiffies,
  56. EXYNOS_PRNG_SEED_OFFSET + 4*i);
  57. if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET)
  58. & SEED_SETTING_DONE))
  59. ret = -EIO;
  60. return ret;
  61. }
  62. static int exynos_init(struct hwrng *rng)
  63. {
  64. struct exynos_rng *exynos_rng = container_of(rng,
  65. struct exynos_rng, rng);
  66. int ret = 0;
  67. pm_runtime_get_sync(exynos_rng->dev);
  68. ret = exynos_rng_configure(exynos_rng);
  69. pm_runtime_mark_last_busy(exynos_rng->dev);
  70. pm_runtime_put_autosuspend(exynos_rng->dev);
  71. return ret;
  72. }
  73. static int exynos_read(struct hwrng *rng, void *buf,
  74. size_t max, bool wait)
  75. {
  76. struct exynos_rng *exynos_rng = container_of(rng,
  77. struct exynos_rng, rng);
  78. u32 *data = buf;
  79. int retry = 100;
  80. int ret = 4;
  81. pm_runtime_get_sync(exynos_rng->dev);
  82. exynos_rng_writel(exynos_rng, PRNG_START, 0);
  83. while (!(exynos_rng_readl(exynos_rng,
  84. EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE) && --retry)
  85. cpu_relax();
  86. if (!retry) {
  87. ret = -ETIMEDOUT;
  88. goto out;
  89. }
  90. exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
  91. *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
  92. out:
  93. pm_runtime_mark_last_busy(exynos_rng->dev);
  94. pm_runtime_put_sync_autosuspend(exynos_rng->dev);
  95. return ret;
  96. }
  97. static int exynos_rng_probe(struct platform_device *pdev)
  98. {
  99. struct exynos_rng *exynos_rng;
  100. struct resource *res;
  101. int ret;
  102. exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
  103. GFP_KERNEL);
  104. if (!exynos_rng)
  105. return -ENOMEM;
  106. exynos_rng->dev = &pdev->dev;
  107. exynos_rng->rng.name = "exynos";
  108. exynos_rng->rng.init = exynos_init;
  109. exynos_rng->rng.read = exynos_read;
  110. exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
  111. if (IS_ERR(exynos_rng->clk)) {
  112. dev_err(&pdev->dev, "Couldn't get clock.\n");
  113. return -ENOENT;
  114. }
  115. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  116. exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  117. if (IS_ERR(exynos_rng->mem))
  118. return PTR_ERR(exynos_rng->mem);
  119. platform_set_drvdata(pdev, exynos_rng);
  120. pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
  121. pm_runtime_use_autosuspend(&pdev->dev);
  122. pm_runtime_enable(&pdev->dev);
  123. ret = devm_hwrng_register(&pdev->dev, &exynos_rng->rng);
  124. if (ret) {
  125. pm_runtime_dont_use_autosuspend(&pdev->dev);
  126. pm_runtime_disable(&pdev->dev);
  127. }
  128. return ret;
  129. }
  130. static int exynos_rng_remove(struct platform_device *pdev)
  131. {
  132. pm_runtime_dont_use_autosuspend(&pdev->dev);
  133. pm_runtime_disable(&pdev->dev);
  134. return 0;
  135. }
  136. static int __maybe_unused exynos_rng_runtime_suspend(struct device *dev)
  137. {
  138. struct platform_device *pdev = to_platform_device(dev);
  139. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  140. clk_disable_unprepare(exynos_rng->clk);
  141. return 0;
  142. }
  143. static int __maybe_unused exynos_rng_runtime_resume(struct device *dev)
  144. {
  145. struct platform_device *pdev = to_platform_device(dev);
  146. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  147. return clk_prepare_enable(exynos_rng->clk);
  148. }
  149. static int __maybe_unused exynos_rng_suspend(struct device *dev)
  150. {
  151. return pm_runtime_force_suspend(dev);
  152. }
  153. static int __maybe_unused exynos_rng_resume(struct device *dev)
  154. {
  155. struct platform_device *pdev = to_platform_device(dev);
  156. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  157. int ret;
  158. ret = pm_runtime_force_resume(dev);
  159. if (ret)
  160. return ret;
  161. return exynos_rng_configure(exynos_rng);
  162. }
  163. static const struct dev_pm_ops exynos_rng_pm_ops = {
  164. SET_SYSTEM_SLEEP_PM_OPS(exynos_rng_suspend, exynos_rng_resume)
  165. SET_RUNTIME_PM_OPS(exynos_rng_runtime_suspend,
  166. exynos_rng_runtime_resume, NULL)
  167. };
  168. static const struct of_device_id exynos_rng_dt_match[] = {
  169. {
  170. .compatible = "samsung,exynos4-rng",
  171. },
  172. { },
  173. };
  174. MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
  175. static struct platform_driver exynos_rng_driver = {
  176. .driver = {
  177. .name = "exynos-rng",
  178. .pm = &exynos_rng_pm_ops,
  179. .of_match_table = exynos_rng_dt_match,
  180. },
  181. .probe = exynos_rng_probe,
  182. .remove = exynos_rng_remove,
  183. };
  184. module_platform_driver(exynos_rng_driver);
  185. MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
  186. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  187. MODULE_LICENSE("GPL");