omap3-rom-rng.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * omap3-rom-rng.c - RNG driver for TI OMAP3 CPU family
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Juha Yrjola <juha.yrjola@solidboot.com>
  6. *
  7. * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/random.h>
  17. #include <linux/hw_random.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #define RNG_RESET 0x01
  23. #define RNG_GEN_PRNG_HW_INIT 0x02
  24. #define RNG_GEN_HW 0x08
  25. /* param1: ptr, param2: count, param3: flag */
  26. static u32 (*omap3_rom_rng_call)(u32, u32, u32);
  27. static struct delayed_work idle_work;
  28. static int rng_idle;
  29. static struct clk *rng_clk;
  30. static void omap3_rom_rng_idle(struct work_struct *work)
  31. {
  32. int r;
  33. r = omap3_rom_rng_call(0, 0, RNG_RESET);
  34. if (r != 0) {
  35. pr_err("reset failed: %d\n", r);
  36. return;
  37. }
  38. clk_disable_unprepare(rng_clk);
  39. rng_idle = 1;
  40. }
  41. static int omap3_rom_rng_get_random(void *buf, unsigned int count)
  42. {
  43. u32 r;
  44. u32 ptr;
  45. cancel_delayed_work_sync(&idle_work);
  46. if (rng_idle) {
  47. r = clk_prepare_enable(rng_clk);
  48. if (r)
  49. return r;
  50. r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
  51. if (r != 0) {
  52. clk_disable_unprepare(rng_clk);
  53. pr_err("HW init failed: %d\n", r);
  54. return -EIO;
  55. }
  56. rng_idle = 0;
  57. }
  58. ptr = virt_to_phys(buf);
  59. r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
  60. schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
  61. if (r != 0)
  62. return -EINVAL;
  63. return 0;
  64. }
  65. static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
  66. {
  67. int r;
  68. r = omap3_rom_rng_get_random(data, 4);
  69. if (r < 0)
  70. return r;
  71. return 4;
  72. }
  73. static struct hwrng omap3_rom_rng_ops = {
  74. .name = "omap3-rom",
  75. .read = omap3_rom_rng_read,
  76. };
  77. static int omap3_rom_rng_probe(struct platform_device *pdev)
  78. {
  79. int ret = 0;
  80. pr_info("initializing\n");
  81. omap3_rom_rng_call = pdev->dev.platform_data;
  82. if (!omap3_rom_rng_call) {
  83. pr_err("omap3_rom_rng_call is NULL\n");
  84. return -EINVAL;
  85. }
  86. INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
  87. rng_clk = devm_clk_get(&pdev->dev, "ick");
  88. if (IS_ERR(rng_clk)) {
  89. pr_err("unable to get RNG clock\n");
  90. return PTR_ERR(rng_clk);
  91. }
  92. /* Leave the RNG in reset state. */
  93. ret = clk_prepare_enable(rng_clk);
  94. if (ret)
  95. return ret;
  96. omap3_rom_rng_idle(0);
  97. return hwrng_register(&omap3_rom_rng_ops);
  98. }
  99. static int omap3_rom_rng_remove(struct platform_device *pdev)
  100. {
  101. cancel_delayed_work_sync(&idle_work);
  102. hwrng_unregister(&omap3_rom_rng_ops);
  103. clk_disable_unprepare(rng_clk);
  104. return 0;
  105. }
  106. static struct platform_driver omap3_rom_rng_driver = {
  107. .driver = {
  108. .name = "omap3-rom-rng",
  109. },
  110. .probe = omap3_rom_rng_probe,
  111. .remove = omap3_rom_rng_remove,
  112. };
  113. module_platform_driver(omap3_rom_rng_driver);
  114. MODULE_ALIAS("platform:omap3-rom-rng");
  115. MODULE_AUTHOR("Juha Yrjola");
  116. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  117. MODULE_LICENSE("GPL");