exynos-rng.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_init(struct hwrng *rng)
  51. {
  52. struct exynos_rng *exynos_rng = container_of(rng,
  53. struct exynos_rng, rng);
  54. int i;
  55. int ret = 0;
  56. pm_runtime_get_sync(exynos_rng->dev);
  57. for (i = 0 ; i < 5 ; i++)
  58. exynos_rng_writel(exynos_rng, jiffies,
  59. EXYNOS_PRNG_SEED_OFFSET + 4*i);
  60. if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET)
  61. & SEED_SETTING_DONE))
  62. ret = -EIO;
  63. pm_runtime_put_noidle(exynos_rng->dev);
  64. return ret;
  65. }
  66. static int exynos_read(struct hwrng *rng, void *buf,
  67. size_t max, bool wait)
  68. {
  69. struct exynos_rng *exynos_rng = container_of(rng,
  70. struct exynos_rng, rng);
  71. u32 *data = buf;
  72. pm_runtime_get_sync(exynos_rng->dev);
  73. exynos_rng_writel(exynos_rng, PRNG_START, 0);
  74. while (!(exynos_rng_readl(exynos_rng,
  75. EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE))
  76. cpu_relax();
  77. exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
  78. *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
  79. pm_runtime_mark_last_busy(exynos_rng->dev);
  80. pm_runtime_autosuspend(exynos_rng->dev);
  81. return 4;
  82. }
  83. static int exynos_rng_probe(struct platform_device *pdev)
  84. {
  85. struct exynos_rng *exynos_rng;
  86. struct resource *res;
  87. exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
  88. GFP_KERNEL);
  89. if (!exynos_rng)
  90. return -ENOMEM;
  91. exynos_rng->dev = &pdev->dev;
  92. exynos_rng->rng.name = "exynos";
  93. exynos_rng->rng.init = exynos_init;
  94. exynos_rng->rng.read = exynos_read;
  95. exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
  96. if (IS_ERR(exynos_rng->clk)) {
  97. dev_err(&pdev->dev, "Couldn't get clock.\n");
  98. return -ENOENT;
  99. }
  100. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  101. exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  102. if (IS_ERR(exynos_rng->mem))
  103. return PTR_ERR(exynos_rng->mem);
  104. platform_set_drvdata(pdev, exynos_rng);
  105. pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
  106. pm_runtime_use_autosuspend(&pdev->dev);
  107. pm_runtime_enable(&pdev->dev);
  108. return devm_hwrng_register(&pdev->dev, &exynos_rng->rng);
  109. }
  110. #ifdef CONFIG_PM
  111. static int exynos_rng_runtime_suspend(struct device *dev)
  112. {
  113. struct platform_device *pdev = to_platform_device(dev);
  114. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  115. clk_disable_unprepare(exynos_rng->clk);
  116. return 0;
  117. }
  118. static int exynos_rng_runtime_resume(struct device *dev)
  119. {
  120. struct platform_device *pdev = to_platform_device(dev);
  121. struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  122. return clk_prepare_enable(exynos_rng->clk);
  123. }
  124. #endif
  125. static UNIVERSAL_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_runtime_suspend,
  126. exynos_rng_runtime_resume, NULL);
  127. static struct platform_driver exynos_rng_driver = {
  128. .driver = {
  129. .name = "exynos-rng",
  130. .pm = &exynos_rng_pm_ops,
  131. },
  132. .probe = exynos_rng_probe,
  133. };
  134. module_platform_driver(exynos_rng_driver);
  135. MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
  136. MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  137. MODULE_LICENSE("GPL");