omap3-rom-rng.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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@kernel.org>
  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. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/random.h>
  16. #include <linux/hw_random.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #define RNG_RESET 0x01
  25. #define RNG_GEN_PRNG_HW_INIT 0x02
  26. #define RNG_GEN_HW 0x08
  27. struct omap_rom_rng {
  28. struct clk *clk;
  29. struct device *dev;
  30. struct hwrng ops;
  31. u32 (*rom_rng_call)(u32 ptr, u32 count, u32 flag);
  32. };
  33. static int omap3_rom_rng_read(struct hwrng *rng, void *data, size_t max, bool w)
  34. {
  35. struct omap_rom_rng *ddata;
  36. u32 ptr;
  37. int r;
  38. ddata = (struct omap_rom_rng *)rng->priv;
  39. r = pm_runtime_get_sync(ddata->dev);
  40. if (r < 0) {
  41. pm_runtime_put_noidle(ddata->dev);
  42. return r;
  43. }
  44. ptr = virt_to_phys(data);
  45. r = ddata->rom_rng_call(ptr, 4, RNG_GEN_HW);
  46. if (r != 0)
  47. r = -EINVAL;
  48. else
  49. r = 4;
  50. pm_runtime_mark_last_busy(ddata->dev);
  51. pm_runtime_put_autosuspend(ddata->dev);
  52. return r;
  53. }
  54. static int __maybe_unused omap_rom_rng_runtime_suspend(struct device *dev)
  55. {
  56. struct omap_rom_rng *ddata;
  57. int r;
  58. ddata = dev_get_drvdata(dev);
  59. r = ddata->rom_rng_call(0, 0, RNG_RESET);
  60. if (r != 0)
  61. dev_err(dev, "reset failed: %d\n", r);
  62. clk_disable_unprepare(ddata->clk);
  63. return 0;
  64. }
  65. static int __maybe_unused omap_rom_rng_runtime_resume(struct device *dev)
  66. {
  67. struct omap_rom_rng *ddata;
  68. int r;
  69. ddata = dev_get_drvdata(dev);
  70. r = clk_prepare_enable(ddata->clk);
  71. if (r < 0)
  72. return r;
  73. r = ddata->rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
  74. if (r != 0) {
  75. clk_disable_unprepare(ddata->clk);
  76. dev_err(dev, "HW init failed: %d\n", r);
  77. return -EIO;
  78. }
  79. return 0;
  80. }
  81. static void omap_rom_rng_finish(void *data)
  82. {
  83. struct omap_rom_rng *ddata = data;
  84. pm_runtime_dont_use_autosuspend(ddata->dev);
  85. pm_runtime_disable(ddata->dev);
  86. }
  87. static int omap3_rom_rng_probe(struct platform_device *pdev)
  88. {
  89. struct omap_rom_rng *ddata;
  90. int ret = 0;
  91. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  92. if (!ddata)
  93. return -ENOMEM;
  94. ddata->dev = &pdev->dev;
  95. ddata->ops.priv = (unsigned long)ddata;
  96. ddata->ops.name = "omap3-rom";
  97. ddata->ops.read = of_device_get_match_data(&pdev->dev);
  98. ddata->ops.quality = 900;
  99. if (!ddata->ops.read) {
  100. dev_err(&pdev->dev, "missing rom code handler\n");
  101. return -ENODEV;
  102. }
  103. dev_set_drvdata(ddata->dev, ddata);
  104. ddata->rom_rng_call = pdev->dev.platform_data;
  105. if (!ddata->rom_rng_call) {
  106. dev_err(ddata->dev, "rom_rng_call is NULL\n");
  107. return -EINVAL;
  108. }
  109. ddata->clk = devm_clk_get(ddata->dev, "ick");
  110. if (IS_ERR(ddata->clk)) {
  111. dev_err(ddata->dev, "unable to get RNG clock\n");
  112. return PTR_ERR(ddata->clk);
  113. }
  114. pm_runtime_enable(&pdev->dev);
  115. pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
  116. pm_runtime_use_autosuspend(&pdev->dev);
  117. ret = devm_add_action_or_reset(ddata->dev, omap_rom_rng_finish,
  118. ddata);
  119. if (ret)
  120. return ret;
  121. return devm_hwrng_register(ddata->dev, &ddata->ops);
  122. }
  123. static const struct of_device_id omap_rom_rng_match[] = {
  124. { .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
  125. { /* sentinel */ },
  126. };
  127. MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
  128. static const struct dev_pm_ops omap_rom_rng_pm_ops = {
  129. SET_SYSTEM_SLEEP_PM_OPS(omap_rom_rng_runtime_suspend,
  130. omap_rom_rng_runtime_resume)
  131. };
  132. static struct platform_driver omap3_rom_rng_driver = {
  133. .driver = {
  134. .name = "omap3-rom-rng",
  135. .of_match_table = omap_rom_rng_match,
  136. .pm = &omap_rom_rng_pm_ops,
  137. },
  138. .probe = omap3_rom_rng_probe,
  139. };
  140. module_platform_driver(omap3_rom_rng_driver);
  141. MODULE_ALIAS("platform:omap3-rom-rng");
  142. MODULE_AUTHOR("Juha Yrjola");
  143. MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
  144. MODULE_DESCRIPTION("RNG driver for TI OMAP3 CPU family");
  145. MODULE_LICENSE("GPL");