exynos-rng.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * exynos-rng.c - Random Number Generator driver for the exynos
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * Jonghwa Lee <jonghwa3.lee@smasung.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 __raw_readl(rng->mem + offset);
  45. }
  46. static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset)
  47. {
  48. __raw_writel(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_put_noidle(exynos_rng->dev);
  70. return ret;
  71. }
  72. static int exynos_read(struct hwrng *rng, void *buf,
  73. size_t max, bool wait)
  74. {
  75. struct exynos_rng *exynos_rng = container_of(rng,
  76. struct exynos_rng, rng);
  77. u32 *data = buf;
  78. int retry = 100;
  79. int ret = 4;
  80. pm_runtime_get_sync(exynos_rng->dev);
  81. exynos_rng_writel(exynos_rng, PRNG_START, 0);
  82. while (!(exynos_rng_readl(exynos_rng,
  83. EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE) && --retry)
  84. cpu_relax();
  85. if (!retry) {
  86. ret = -ETIMEDOUT;
  87. goto out;
  88. }
  89. exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
  90. *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
  91. out:
  92. pm_runtime_mark_last_busy(exynos_rng->dev);
  93. pm_runtime_put_sync_autosuspend(exynos_rng->dev);
  94. return ret;
  95. }
  96. static int exynos_rng_probe(struct platform_device *pdev)
  97. {
  98. struct exynos_rng *exynos_rng;
  99. struct resource *res;
  100. int ret;
  101. exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
  102. GFP_KERNEL);
  103. if (!exynos_rng)
  104. return -ENOMEM;
  105. exynos_rng->dev = &pdev->dev;
  106. exynos_rng->rng.name = "exynos";
  107. exynos_rng->rng.init = exynos_init;
  108. exynos_rng->rng.read = exynos_read;
  109. exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
  110. if (IS_ERR(exynos_rng->clk)) {
  111. dev_err(&pdev->dev, "Couldn't get clock.\n");
  112. return -ENOENT;
  113. }
  114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  116. if (IS_ERR(exynos_rng->mem))
  117. return PTR_ERR(exynos_rng->mem);
  118. platform_set_drvdata(pdev, exynos_rng);
  119. pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
  120. pm_runtime_use_autosuspend(&pdev->dev);
  121. pm_runtime_enable(&pdev->dev);
  122. ret = devm_hwrng_register(&pdev->dev, &exynos_rng->rng);
  123. if (ret) {
  124. pm_runtime_dont_use_autosuspend(&pdev->dev);
  125. pm_runtime_disable(&pdev->dev);
  126. }
  127. return ret;
  128. }
  129. static int __maybe_unused exynos_rng_runtime_suspend(struct device *dev)
  130. {
  131. struct platform_device *pdev = to_platform_device(dev);
  132. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  133. clk_disable_unprepare(exynos_rng->clk);
  134. return 0;
  135. }
  136. static int __maybe_unused exynos_rng_runtime_resume(struct device *dev)
  137. {
  138. struct platform_device *pdev = to_platform_device(dev);
  139. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  140. return clk_prepare_enable(exynos_rng->clk);
  141. }
  142. static int __maybe_unused exynos_rng_suspend(struct device *dev)
  143. {
  144. return pm_runtime_force_suspend(dev);
  145. }
  146. static int __maybe_unused exynos_rng_resume(struct device *dev)
  147. {
  148. struct platform_device *pdev = to_platform_device(dev);
  149. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  150. int ret;
  151. ret = pm_runtime_force_resume(dev);
  152. if (ret)
  153. return ret;
  154. return exynos_rng_configure(exynos_rng);
  155. }
  156. static const struct dev_pm_ops exynos_rng_pm_ops = {
  157. SET_SYSTEM_SLEEP_PM_OPS(exynos_rng_suspend, exynos_rng_resume)
  158. SET_RUNTIME_PM_OPS(exynos_rng_runtime_suspend,
  159. exynos_rng_runtime_resume, NULL)
  160. };
  161. static const struct of_device_id exynos_rng_dt_match[] = {
  162. {
  163. .compatible = "samsung,exynos4-rng",
  164. },
  165. { },
  166. };
  167. MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
  168. static struct platform_driver exynos_rng_driver = {
  169. .driver = {
  170. .name = "exynos-rng",
  171. .pm = &exynos_rng_pm_ops,
  172. .of_match_table = exynos_rng_dt_match,
  173. },
  174. .probe = exynos_rng_probe,
  175. };
  176. module_platform_driver(exynos_rng_driver);
  177. MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
  178. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  179. MODULE_LICENSE("GPL");