exynos-rng.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/init.h>
  25. #include <linux/io.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/clk.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/err.h>
  30. #define EXYNOS_PRNG_STATUS_OFFSET 0x10
  31. #define EXYNOS_PRNG_SEED_OFFSET 0x140
  32. #define EXYNOS_PRNG_OUT1_OFFSET 0x160
  33. #define SEED_SETTING_DONE BIT(1)
  34. #define PRNG_START 0x18
  35. #define PRNG_DONE BIT(5)
  36. #define EXYNOS_AUTOSUSPEND_DELAY 100
  37. struct exynos_rng {
  38. struct device *dev;
  39. struct hwrng rng;
  40. void __iomem *mem;
  41. struct clk *clk;
  42. };
  43. static u32 exynos_rng_readl(struct exynos_rng *rng, u32 offset)
  44. {
  45. return __raw_readl(rng->mem + offset);
  46. }
  47. static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset)
  48. {
  49. __raw_writel(val, rng->mem + offset);
  50. }
  51. static int exynos_init(struct hwrng *rng)
  52. {
  53. struct exynos_rng *exynos_rng = container_of(rng,
  54. struct exynos_rng, rng);
  55. int i;
  56. int ret = 0;
  57. pm_runtime_get_sync(exynos_rng->dev);
  58. for (i = 0 ; i < 5 ; i++)
  59. exynos_rng_writel(exynos_rng, jiffies,
  60. EXYNOS_PRNG_SEED_OFFSET + 4*i);
  61. if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET)
  62. & SEED_SETTING_DONE))
  63. ret = -EIO;
  64. pm_runtime_put_noidle(exynos_rng->dev);
  65. return ret;
  66. }
  67. static int exynos_read(struct hwrng *rng, void *buf,
  68. size_t max, bool wait)
  69. {
  70. struct exynos_rng *exynos_rng = container_of(rng,
  71. struct exynos_rng, rng);
  72. u32 *data = buf;
  73. pm_runtime_get_sync(exynos_rng->dev);
  74. exynos_rng_writel(exynos_rng, PRNG_START, 0);
  75. while (!(exynos_rng_readl(exynos_rng,
  76. EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE))
  77. cpu_relax();
  78. exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
  79. *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
  80. pm_runtime_mark_last_busy(exynos_rng->dev);
  81. pm_runtime_autosuspend(exynos_rng->dev);
  82. return 4;
  83. }
  84. static int exynos_rng_probe(struct platform_device *pdev)
  85. {
  86. struct exynos_rng *exynos_rng;
  87. struct resource *res;
  88. exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
  89. GFP_KERNEL);
  90. if (!exynos_rng)
  91. return -ENOMEM;
  92. exynos_rng->dev = &pdev->dev;
  93. exynos_rng->rng.name = "exynos";
  94. exynos_rng->rng.init = exynos_init;
  95. exynos_rng->rng.read = exynos_read;
  96. exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
  97. if (IS_ERR(exynos_rng->clk)) {
  98. dev_err(&pdev->dev, "Couldn't get clock.\n");
  99. return -ENOENT;
  100. }
  101. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  102. exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  103. if (IS_ERR(exynos_rng->mem))
  104. return PTR_ERR(exynos_rng->mem);
  105. platform_set_drvdata(pdev, exynos_rng);
  106. pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
  107. pm_runtime_use_autosuspend(&pdev->dev);
  108. pm_runtime_enable(&pdev->dev);
  109. return hwrng_register(&exynos_rng->rng);
  110. }
  111. static int exynos_rng_remove(struct platform_device *pdev)
  112. {
  113. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  114. hwrng_unregister(&exynos_rng->rng);
  115. return 0;
  116. }
  117. static int exynos_rng_runtime_suspend(struct device *dev)
  118. {
  119. struct platform_device *pdev = to_platform_device(dev);
  120. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  121. clk_disable_unprepare(exynos_rng->clk);
  122. return 0;
  123. }
  124. static int exynos_rng_runtime_resume(struct device *dev)
  125. {
  126. struct platform_device *pdev = to_platform_device(dev);
  127. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  128. return clk_prepare_enable(exynos_rng->clk);
  129. }
  130. static UNIVERSAL_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_runtime_suspend,
  131. exynos_rng_runtime_resume, NULL);
  132. static struct platform_driver exynos_rng_driver = {
  133. .driver = {
  134. .name = "exynos-rng",
  135. .owner = THIS_MODULE,
  136. .pm = &exynos_rng_pm_ops,
  137. },
  138. .probe = exynos_rng_probe,
  139. .remove = exynos_rng_remove,
  140. };
  141. module_platform_driver(exynos_rng_driver);
  142. MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
  143. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  144. MODULE_LICENSE("GPL");