omap3-rom-rng.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/timer.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 timer_list idle_timer;
  28. static int rng_idle;
  29. static struct clk *rng_clk;
  30. static void omap3_rom_rng_idle(unsigned long data)
  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. del_timer_sync(&idle_timer);
  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. mod_timer(&idle_timer, jiffies + msecs_to_jiffies(500));
  59. if (r != 0)
  60. return -EINVAL;
  61. return 0;
  62. }
  63. static int omap3_rom_rng_data_present(struct hwrng *rng, int wait)
  64. {
  65. return 1;
  66. }
  67. static int omap3_rom_rng_data_read(struct hwrng *rng, u32 *data)
  68. {
  69. int r;
  70. r = omap3_rom_rng_get_random(data, 4);
  71. if (r < 0)
  72. return r;
  73. return 4;
  74. }
  75. static struct hwrng omap3_rom_rng_ops = {
  76. .name = "omap3-rom",
  77. .data_present = omap3_rom_rng_data_present,
  78. .data_read = omap3_rom_rng_data_read,
  79. };
  80. static int omap3_rom_rng_probe(struct platform_device *pdev)
  81. {
  82. pr_info("initializing\n");
  83. omap3_rom_rng_call = pdev->dev.platform_data;
  84. if (!omap3_rom_rng_call) {
  85. pr_err("omap3_rom_rng_call is NULL\n");
  86. return -EINVAL;
  87. }
  88. setup_timer(&idle_timer, omap3_rom_rng_idle, 0);
  89. rng_clk = devm_clk_get(&pdev->dev, "ick");
  90. if (IS_ERR(rng_clk)) {
  91. pr_err("unable to get RNG clock\n");
  92. return PTR_ERR(rng_clk);
  93. }
  94. /* Leave the RNG in reset state. */
  95. clk_prepare_enable(rng_clk);
  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. hwrng_unregister(&omap3_rom_rng_ops);
  102. clk_disable_unprepare(rng_clk);
  103. return 0;
  104. }
  105. static struct platform_driver omap3_rom_rng_driver = {
  106. .driver = {
  107. .name = "omap3-rom-rng",
  108. },
  109. .probe = omap3_rom_rng_probe,
  110. .remove = omap3_rom_rng_remove,
  111. };
  112. module_platform_driver(omap3_rom_rng_driver);
  113. MODULE_ALIAS("platform:omap3-rom-rng");
  114. MODULE_AUTHOR("Juha Yrjola");
  115. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  116. MODULE_LICENSE("GPL");