omap3-rom-rng.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. clk_prepare_enable(rng_clk);
  48. r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
  49. if (r != 0) {
  50. clk_disable_unprepare(rng_clk);
  51. pr_err("HW init failed: %d\n", r);
  52. return -EIO;
  53. }
  54. rng_idle = 0;
  55. }
  56. ptr = virt_to_phys(buf);
  57. r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
  58. schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
  59. if (r != 0)
  60. return -EINVAL;
  61. return 0;
  62. }
  63. static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
  64. {
  65. int r;
  66. r = omap3_rom_rng_get_random(data, 4);
  67. if (r < 0)
  68. return r;
  69. return 4;
  70. }
  71. static struct hwrng omap3_rom_rng_ops = {
  72. .name = "omap3-rom",
  73. .read = omap3_rom_rng_read,
  74. };
  75. static int omap3_rom_rng_probe(struct platform_device *pdev)
  76. {
  77. pr_info("initializing\n");
  78. omap3_rom_rng_call = pdev->dev.platform_data;
  79. if (!omap3_rom_rng_call) {
  80. pr_err("omap3_rom_rng_call is NULL\n");
  81. return -EINVAL;
  82. }
  83. INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
  84. rng_clk = devm_clk_get(&pdev->dev, "ick");
  85. if (IS_ERR(rng_clk)) {
  86. pr_err("unable to get RNG clock\n");
  87. return PTR_ERR(rng_clk);
  88. }
  89. /* Leave the RNG in reset state. */
  90. clk_prepare_enable(rng_clk);
  91. omap3_rom_rng_idle(0);
  92. return hwrng_register(&omap3_rom_rng_ops);
  93. }
  94. static int omap3_rom_rng_remove(struct platform_device *pdev)
  95. {
  96. cancel_delayed_work_sync(&idle_work);
  97. hwrng_unregister(&omap3_rom_rng_ops);
  98. clk_disable_unprepare(rng_clk);
  99. return 0;
  100. }
  101. static struct platform_driver omap3_rom_rng_driver = {
  102. .driver = {
  103. .name = "omap3-rom-rng",
  104. },
  105. .probe = omap3_rom_rng_probe,
  106. .remove = omap3_rom_rng_remove,
  107. };
  108. module_platform_driver(omap3_rom_rng_driver);
  109. MODULE_ALIAS("platform:omap3-rom-rng");
  110. MODULE_AUTHOR("Juha Yrjola");
  111. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  112. MODULE_LICENSE("GPL");