omap3-rom-rng.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_put_autosuspend(ddata->dev);
  51. return r;
  52. }
  53. static int __maybe_unused omap_rom_rng_runtime_suspend(struct device *dev)
  54. {
  55. struct omap_rom_rng *ddata;
  56. int r;
  57. ddata = dev_get_drvdata(dev);
  58. r = ddata->rom_rng_call(0, 0, RNG_RESET);
  59. if (r != 0)
  60. dev_err(dev, "reset failed: %d\n", r);
  61. clk_disable_unprepare(ddata->clk);
  62. return 0;
  63. }
  64. static int __maybe_unused omap_rom_rng_runtime_resume(struct device *dev)
  65. {
  66. struct omap_rom_rng *ddata;
  67. int r;
  68. ddata = dev_get_drvdata(dev);
  69. r = clk_prepare_enable(ddata->clk);
  70. if (r < 0)
  71. return r;
  72. r = ddata->rom_rng_call(0, 0, RNG_GEN_PRNG_HW_INIT);
  73. if (r != 0) {
  74. clk_disable_unprepare(ddata->clk);
  75. dev_err(dev, "HW init failed: %d\n", r);
  76. return -EIO;
  77. }
  78. return 0;
  79. }
  80. static void omap_rom_rng_finish(void *data)
  81. {
  82. struct omap_rom_rng *ddata = data;
  83. pm_runtime_dont_use_autosuspend(ddata->dev);
  84. pm_runtime_disable(ddata->dev);
  85. }
  86. static int omap3_rom_rng_probe(struct platform_device *pdev)
  87. {
  88. struct omap_rom_rng *ddata;
  89. int ret = 0;
  90. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  91. if (!ddata)
  92. return -ENOMEM;
  93. ddata->dev = &pdev->dev;
  94. ddata->ops.priv = (unsigned long)ddata;
  95. ddata->ops.name = "omap3-rom";
  96. ddata->ops.read = of_device_get_match_data(&pdev->dev);
  97. ddata->ops.quality = 900;
  98. if (!ddata->ops.read) {
  99. dev_err(&pdev->dev, "missing rom code handler\n");
  100. return -ENODEV;
  101. }
  102. dev_set_drvdata(ddata->dev, ddata);
  103. ddata->rom_rng_call = pdev->dev.platform_data;
  104. if (!ddata->rom_rng_call) {
  105. dev_err(ddata->dev, "rom_rng_call is NULL\n");
  106. return -EINVAL;
  107. }
  108. ddata->clk = devm_clk_get(ddata->dev, "ick");
  109. if (IS_ERR(ddata->clk)) {
  110. dev_err(ddata->dev, "unable to get RNG clock\n");
  111. return PTR_ERR(ddata->clk);
  112. }
  113. pm_runtime_enable(&pdev->dev);
  114. pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
  115. pm_runtime_use_autosuspend(&pdev->dev);
  116. ret = devm_add_action_or_reset(ddata->dev, omap_rom_rng_finish,
  117. ddata);
  118. if (ret)
  119. return ret;
  120. return devm_hwrng_register(ddata->dev, &ddata->ops);
  121. }
  122. static const struct of_device_id omap_rom_rng_match[] = {
  123. { .compatible = "nokia,n900-rom-rng", .data = omap3_rom_rng_read, },
  124. { /* sentinel */ },
  125. };
  126. MODULE_DEVICE_TABLE(of, omap_rom_rng_match);
  127. static const struct dev_pm_ops omap_rom_rng_pm_ops = {
  128. SET_SYSTEM_SLEEP_PM_OPS(omap_rom_rng_runtime_suspend,
  129. omap_rom_rng_runtime_resume)
  130. };
  131. static struct platform_driver omap3_rom_rng_driver = {
  132. .driver = {
  133. .name = "omap3-rom-rng",
  134. .of_match_table = omap_rom_rng_match,
  135. .pm = &omap_rom_rng_pm_ops,
  136. },
  137. .probe = omap3_rom_rng_probe,
  138. };
  139. module_platform_driver(omap3_rom_rng_driver);
  140. MODULE_ALIAS("platform:omap3-rom-rng");
  141. MODULE_AUTHOR("Juha Yrjola");
  142. MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
  143. MODULE_DESCRIPTION("RNG driver for TI OMAP3 CPU family");
  144. MODULE_LICENSE("GPL");