omap3-rom-rng.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #define RNG_RESET 0x01
  25. #define RNG_GEN_PRNG_HW_INIT 0x02
  26. #define RNG_GEN_HW 0x08
  27. /* param1: ptr, param2: count, param3: flag */
  28. static u32 (*omap3_rom_rng_call)(u32, u32, u32);
  29. static struct delayed_work idle_work;
  30. static int rng_idle;
  31. static struct clk *rng_clk;
  32. static void omap3_rom_rng_idle(struct work_struct *work)
  33. {
  34. int r;
  35. r = omap3_rom_rng_call(0, 0, RNG_RESET);
  36. if (r != 0) {
  37. pr_err("reset failed: %d\n", r);
  38. return;
  39. }
  40. clk_disable_unprepare(rng_clk);
  41. rng_idle = 1;
  42. }
  43. static int omap3_rom_rng_get_random(void *buf, unsigned int count)
  44. {
  45. u32 r;
  46. u32 ptr;
  47. cancel_delayed_work_sync(&idle_work);
  48. if (rng_idle) {
  49. r = clk_prepare_enable(rng_clk);
  50. if (r)
  51. return r;
  52. r = omap3_rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
  53. if (r != 0) {
  54. clk_disable_unprepare(rng_clk);
  55. pr_err("HW init failed: %d\n", r);
  56. return -EIO;
  57. }
  58. rng_idle = 0;
  59. }
  60. ptr = virt_to_phys(buf);
  61. r = omap3_rom_rng_call(ptr, count, RNG_GEN_HW);
  62. schedule_delayed_work(&idle_work, msecs_to_jiffies(500));
  63. if (r != 0)
  64. return -EINVAL;
  65. return 0;
  66. }
  67. static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
  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. };
  78. static int omap3_rom_rng_probe(struct platform_device *pdev)
  79. {
  80. int ret = 0;
  81. omap3_rom_rng_ops.read = of_device_get_match_data(&pdev->dev);
  82. if (!omap3_rom_rng_ops.read) {
  83. dev_err(&pdev->dev, "missing rom code handler\n");
  84. return -ENODEV;
  85. }
  86. omap3_rom_rng_call = pdev->dev.platform_data;
  87. if (!omap3_rom_rng_call) {
  88. pr_err("omap3_rom_rng_call is NULL\n");
  89. return -EINVAL;
  90. }
  91. INIT_DELAYED_WORK(&idle_work, omap3_rom_rng_idle);
  92. rng_clk = devm_clk_get(&pdev->dev, "ick");
  93. if (IS_ERR(rng_clk)) {
  94. pr_err("unable to get RNG clock\n");
  95. return PTR_ERR(rng_clk);
  96. }
  97. /* Leave the RNG in reset state. */
  98. ret = clk_prepare_enable(rng_clk);
  99. if (ret)
  100. return ret;
  101. omap3_rom_rng_idle(0);
  102. return hwrng_register(&omap3_rom_rng_ops);
  103. }
  104. static int omap3_rom_rng_remove(struct platform_device *pdev)
  105. {
  106. cancel_delayed_work_sync(&idle_work);
  107. hwrng_unregister(&omap3_rom_rng_ops);
  108. if (!rng_idle)
  109. clk_disable_unprepare(rng_clk);
  110. return 0;
  111. }
  112. static const struct of_device_id omap_rom_rng_match[] = {
  113. { .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
  114. { /* sentinel */ },
  115. };
  116. MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
  117. static struct platform_driver omap3_rom_rng_driver = {
  118. .driver = {
  119. .name = "omap3-rom-rng",
  120. .of_match_table = omap_rom_rng_match,
  121. },
  122. .probe = omap3_rom_rng_probe,
  123. .remove = omap3_rom_rng_remove,
  124. };
  125. module_platform_driver(omap3_rom_rng_driver);
  126. MODULE_ALIAS("platform:omap3-rom-rng");
  127. MODULE_AUTHOR("Juha Yrjola");
  128. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  129. MODULE_LICENSE("GPL");