Advertisement
Guest User

Untitled

a guest
Feb 6th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. --- a/drivers/char/hw_random/Kconfig
  2. +++ b/drivers/char/hw_random/Kconfig
  3. @@ -47,6 +47,18 @@
  4.  
  5. If unsure, say Y.
  6.  
  7. +config HW_RANDOM_EXYNOS
  8. + tristate "EXYNOS HW random number generator support"
  9. + depends on HW_RANDOM && HAS_IOMEM && HAVE_CLK
  10. + ---help---
  11. + This driver provides kernel-side support for the Random Number
  12. + Generator hardware found on EXYNOS SOCs.
  13. +
  14. + To compile this driver as a module, choose M here: the
  15. + module will be called exynos-rng.
  16. +
  17. + If unsure, say Y.
  18. +
  19. config HW_RANDOM_AMD
  20. tristate "AMD HW Random Number Generator support"
  21. depends on HW_RANDOM && (X86 || PPC_MAPLE) && PCI
  22. --- a/drivers/char/hw_random/Makefile
  23. +++ b/drivers/char/hw_random/Makefile
  24. @@ -6,6 +6,7 @@
  25. rng-core-y := core.o
  26. obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += timeriomem-rng.o
  27. obj-$(CONFIG_HW_RANDOM_INTEL) += intel-rng.o
  28. +obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  29. obj-$(CONFIG_HW_RANDOM_AMD) += amd-rng.o
  30. obj-$(CONFIG_HW_RANDOM_ATMEL) += atmel-rng.o
  31. obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
  32. --- /dev/null
  33. +++ b/drivers/char/hw_random/exynos-rng.c
  34. @@ -0,0 +1,182 @@
  35. +/*
  36. + * exynos-rng.c - Random Number Generator driver for the exynos
  37. + *
  38. + * Copyright (C) 2012 Samsung Electronics
  39. + * Jonghwa Lee <jonghwa3.lee@smasung.com>
  40. + *
  41. + * This program is free software; you can redistribute it and/or modify
  42. + * it under the terms of the GNU General Public License as published by
  43. + * the Free Software Foundation;
  44. + *
  45. + * This program is distributed in the hope that it will be useful,
  46. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  47. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  48. + * GNU General Public License for more details.
  49. + *
  50. + * You should have received a copy of the GNU General Public License
  51. + * along with this program; if not, write to the Free Software
  52. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  53. + *
  54. + */
  55. +
  56. +#include <linux/hw_random.h>
  57. +#include <linux/kernel.h>
  58. +#include <linux/module.h>
  59. +#include <linux/io.h>
  60. +#include <linux/platform_device.h>
  61. +#include <linux/clk.h>
  62. +#include <linux/pm_runtime.h>
  63. +#include <linux/err.h>
  64. +
  65. +#define EXYNOS_PRNG_STATUS_OFFSET 0x10
  66. +#define EXYNOS_PRNG_SEED_OFFSET 0x140
  67. +#define EXYNOS_PRNG_OUT1_OFFSET 0x160
  68. +#define SEED_SETTING_DONE BIT(1)
  69. +#define PRNG_START 0x18
  70. +#define PRNG_DONE BIT(5)
  71. +#define EXYNOS_AUTOSUSPEND_DELAY 100
  72. +
  73. +struct exynos_rng {
  74. + struct device *dev;
  75. + struct hwrng rng;
  76. + void __iomem *mem;
  77. + struct clk *clk;
  78. +};
  79. +
  80. +static u32 exynos_rng_readl(struct exynos_rng *rng, u32 offset)
  81. +{
  82. + return __raw_readl(rng->mem + offset);
  83. +}
  84. +
  85. +static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset)
  86. +{
  87. + __raw_writel(val, rng->mem + offset);
  88. +}
  89. +
  90. +static int exynos_init(struct hwrng *rng)
  91. +{
  92. + struct exynos_rng *exynos_rng = container_of(rng,
  93. + struct exynos_rng, rng);
  94. + int i;
  95. + int ret = 0;
  96. +
  97. + pm_runtime_get_sync(exynos_rng->dev);
  98. +
  99. + for (i = 0 ; i < 5 ; i++)
  100. + exynos_rng_writel(exynos_rng, jiffies,
  101. + EXYNOS_PRNG_SEED_OFFSET + 4*i);
  102. +
  103. + if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET)
  104. + & SEED_SETTING_DONE))
  105. + ret = -EIO;
  106. +
  107. + pm_runtime_put_noidle(exynos_rng->dev);
  108. +
  109. + return ret;
  110. +}
  111. +
  112. +static int exynos_read(struct hwrng *rng, void *buf,
  113. + size_t max, bool wait)
  114. +{
  115. + struct exynos_rng *exynos_rng = container_of(rng,
  116. + struct exynos_rng, rng);
  117. + u32 *data = buf;
  118. +
  119. + pm_runtime_get_sync(exynos_rng->dev);
  120. +
  121. + exynos_rng_writel(exynos_rng, PRNG_START, 0);
  122. +
  123. + while (!(exynos_rng_readl(exynos_rng,
  124. + EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE))
  125. + cpu_relax();
  126. +
  127. + exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
  128. +
  129. + *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
  130. +
  131. + pm_runtime_mark_last_busy(exynos_rng->dev);
  132. + pm_runtime_autosuspend(exynos_rng->dev);
  133. +
  134. + return 4;
  135. +}
  136. +
  137. +static int exynos_rng_probe(struct platform_device *pdev)
  138. +{
  139. + struct exynos_rng *exynos_rng;
  140. + struct resource *res;
  141. +
  142. + exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
  143. + GFP_KERNEL);
  144. + if (!exynos_rng)
  145. + return -ENOMEM;
  146. +
  147. + exynos_rng->dev = &pdev->dev;
  148. + exynos_rng->rng.name = "exynos";
  149. + exynos_rng->rng.init = exynos_init;
  150. + exynos_rng->rng.read = exynos_read;
  151. + exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
  152. + if (IS_ERR(exynos_rng->clk)) {
  153. + dev_err(&pdev->dev, "Couldn't get clock.\n");
  154. + return -ENOENT;
  155. + }
  156. +
  157. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. + exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
  159. + if (IS_ERR(exynos_rng->mem))
  160. + return PTR_ERR(exynos_rng->mem);
  161. +
  162. + platform_set_drvdata(pdev, exynos_rng);
  163. +
  164. + pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
  165. + pm_runtime_use_autosuspend(&pdev->dev);
  166. + pm_runtime_enable(&pdev->dev);
  167. +
  168. + return hwrng_register(&exynos_rng->rng);
  169. +}
  170. +
  171. +static int exynos_rng_remove(struct platform_device *pdev)
  172. +{
  173. + struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  174. +
  175. + hwrng_unregister(&exynos_rng->rng);
  176. +
  177. + return 0;
  178. +}
  179. +
  180. +#ifdef CONFIG_PM
  181. +static int exynos_rng_runtime_suspend(struct device *dev)
  182. +{
  183. + struct platform_device *pdev = to_platform_device(dev);
  184. + struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  185. +
  186. + clk_disable_unprepare(exynos_rng->clk);
  187. +
  188. + return 0;
  189. +}
  190. +
  191. +static int exynos_rng_runtime_resume(struct device *dev)
  192. +{
  193. + struct platform_device *pdev = to_platform_device(dev);
  194. + struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
  195. +
  196. + return clk_prepare_enable(exynos_rng->clk);
  197. +}
  198. +#endif
  199. +
  200. +static UNIVERSAL_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_runtime_suspend,
  201. + exynos_rng_runtime_resume, NULL);
  202. +
  203. +static struct platform_driver exynos_rng_driver = {
  204. + .driver = {
  205. + .name = "exynos-rng",
  206. + .pm = &exynos_rng_pm_ops,
  207. + },
  208. + .probe = exynos_rng_probe,
  209. + .remove = exynos_rng_remove,
  210. +};
  211. +
  212. +module_platform_driver(exynos_rng_driver);
  213. +
  214. +MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
  215. +MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
  216. +MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement