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