npcm-rng.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2019 Nuvoton Technology corporation.
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/io.h>
  6. #include <linux/iopoll.h>
  7. #include <linux/init.h>
  8. #include <linux/random.h>
  9. #include <linux/err.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/hw_random.h>
  12. #include <linux/delay.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/pm_runtime.h>
  15. #define NPCM_RNGCS_REG 0x00 /* Control and status register */
  16. #define NPCM_RNGD_REG 0x04 /* Data register */
  17. #define NPCM_RNGMODE_REG 0x08 /* Mode register */
  18. #define NPCM_RNG_CLK_SET_25MHZ GENMASK(4, 3) /* 20-25 MHz */
  19. #define NPCM_RNG_DATA_VALID BIT(1)
  20. #define NPCM_RNG_ENABLE BIT(0)
  21. #define NPCM_RNG_M1ROSEL BIT(1)
  22. #define NPCM_RNG_TIMEOUT_USEC 20000
  23. #define NPCM_RNG_POLL_USEC 1000
  24. #define to_npcm_rng(p) container_of(p, struct npcm_rng, rng)
  25. struct npcm_rng {
  26. void __iomem *base;
  27. struct hwrng rng;
  28. };
  29. static int npcm_rng_init(struct hwrng *rng)
  30. {
  31. struct npcm_rng *priv = to_npcm_rng(rng);
  32. writel(NPCM_RNG_CLK_SET_25MHZ | NPCM_RNG_ENABLE,
  33. priv->base + NPCM_RNGCS_REG);
  34. return 0;
  35. }
  36. static void npcm_rng_cleanup(struct hwrng *rng)
  37. {
  38. struct npcm_rng *priv = to_npcm_rng(rng);
  39. writel(NPCM_RNG_CLK_SET_25MHZ, priv->base + NPCM_RNGCS_REG);
  40. }
  41. static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  42. {
  43. struct npcm_rng *priv = to_npcm_rng(rng);
  44. int retval = 0;
  45. int ready;
  46. pm_runtime_get_sync((struct device *)priv->rng.priv);
  47. while (max) {
  48. if (wait) {
  49. if (readb_poll_timeout(priv->base + NPCM_RNGCS_REG,
  50. ready,
  51. ready & NPCM_RNG_DATA_VALID,
  52. NPCM_RNG_POLL_USEC,
  53. NPCM_RNG_TIMEOUT_USEC))
  54. break;
  55. } else {
  56. if ((readb(priv->base + NPCM_RNGCS_REG) &
  57. NPCM_RNG_DATA_VALID) == 0)
  58. break;
  59. }
  60. *(u8 *)buf = readb(priv->base + NPCM_RNGD_REG);
  61. retval++;
  62. buf++;
  63. max--;
  64. }
  65. pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
  66. pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
  67. return retval || !wait ? retval : -EIO;
  68. }
  69. static int npcm_rng_probe(struct platform_device *pdev)
  70. {
  71. struct npcm_rng *priv;
  72. int ret;
  73. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  74. if (!priv)
  75. return -ENOMEM;
  76. priv->base = devm_platform_ioremap_resource(pdev, 0);
  77. if (IS_ERR(priv->base))
  78. return PTR_ERR(priv->base);
  79. dev_set_drvdata(&pdev->dev, priv);
  80. pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
  81. pm_runtime_use_autosuspend(&pdev->dev);
  82. pm_runtime_enable(&pdev->dev);
  83. #ifndef CONFIG_PM
  84. priv->rng.init = npcm_rng_init;
  85. priv->rng.cleanup = npcm_rng_cleanup;
  86. #endif
  87. priv->rng.name = pdev->name;
  88. priv->rng.read = npcm_rng_read;
  89. priv->rng.priv = (unsigned long)&pdev->dev;
  90. priv->rng.quality = 1000;
  91. writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
  92. ret = devm_hwrng_register(&pdev->dev, &priv->rng);
  93. if (ret) {
  94. dev_err(&pdev->dev, "Failed to register rng device: %d\n",
  95. ret);
  96. pm_runtime_disable(&pdev->dev);
  97. pm_runtime_set_suspended(&pdev->dev);
  98. return ret;
  99. }
  100. return 0;
  101. }
  102. static int npcm_rng_remove(struct platform_device *pdev)
  103. {
  104. struct npcm_rng *priv = platform_get_drvdata(pdev);
  105. devm_hwrng_unregister(&pdev->dev, &priv->rng);
  106. pm_runtime_disable(&pdev->dev);
  107. pm_runtime_set_suspended(&pdev->dev);
  108. return 0;
  109. }
  110. #ifdef CONFIG_PM
  111. static int npcm_rng_runtime_suspend(struct device *dev)
  112. {
  113. struct npcm_rng *priv = dev_get_drvdata(dev);
  114. npcm_rng_cleanup(&priv->rng);
  115. return 0;
  116. }
  117. static int npcm_rng_runtime_resume(struct device *dev)
  118. {
  119. struct npcm_rng *priv = dev_get_drvdata(dev);
  120. return npcm_rng_init(&priv->rng);
  121. }
  122. #endif
  123. static const struct dev_pm_ops npcm_rng_pm_ops = {
  124. SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend,
  125. npcm_rng_runtime_resume, NULL)
  126. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  127. pm_runtime_force_resume)
  128. };
  129. static const struct of_device_id rng_dt_id[] __maybe_unused = {
  130. { .compatible = "nuvoton,npcm750-rng", },
  131. {},
  132. };
  133. MODULE_DEVICE_TABLE(of, rng_dt_id);
  134. static struct platform_driver npcm_rng_driver = {
  135. .driver = {
  136. .name = "npcm-rng",
  137. .pm = &npcm_rng_pm_ops,
  138. .of_match_table = of_match_ptr(rng_dt_id),
  139. },
  140. .probe = npcm_rng_probe,
  141. .remove = npcm_rng_remove,
  142. };
  143. module_platform_driver(npcm_rng_driver);
  144. MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver");
  145. MODULE_AUTHOR("Tomer Maimon <[email protected]>");
  146. MODULE_LICENSE("GPL v2");