mtk-rng.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Driver for Mediatek Hardware Random Number Generator
  3. *
  4. * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define MTK_RNG_DEV KBUILD_MODNAME
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/hw_random.h>
  21. #include <linux/io.h>
  22. #include <linux/iopoll.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #define USEC_POLL 2
  28. #define TIMEOUT_POLL 20
  29. #define RNG_CTRL 0x00
  30. #define RNG_EN BIT(0)
  31. #define RNG_READY BIT(31)
  32. #define RNG_DATA 0x08
  33. #define to_mtk_rng(p) container_of(p, struct mtk_rng, rng)
  34. struct mtk_rng {
  35. void __iomem *base;
  36. struct clk *clk;
  37. struct hwrng rng;
  38. };
  39. static int mtk_rng_init(struct hwrng *rng)
  40. {
  41. struct mtk_rng *priv = to_mtk_rng(rng);
  42. u32 val;
  43. int err;
  44. err = clk_prepare_enable(priv->clk);
  45. if (err)
  46. return err;
  47. val = readl(priv->base + RNG_CTRL);
  48. val |= RNG_EN;
  49. writel(val, priv->base + RNG_CTRL);
  50. return 0;
  51. }
  52. static void mtk_rng_cleanup(struct hwrng *rng)
  53. {
  54. struct mtk_rng *priv = to_mtk_rng(rng);
  55. u32 val;
  56. val = readl(priv->base + RNG_CTRL);
  57. val &= ~RNG_EN;
  58. writel(val, priv->base + RNG_CTRL);
  59. clk_disable_unprepare(priv->clk);
  60. }
  61. static bool mtk_rng_wait_ready(struct hwrng *rng, bool wait)
  62. {
  63. struct mtk_rng *priv = to_mtk_rng(rng);
  64. int ready;
  65. ready = readl(priv->base + RNG_CTRL) & RNG_READY;
  66. if (!ready && wait)
  67. readl_poll_timeout_atomic(priv->base + RNG_CTRL, ready,
  68. ready & RNG_READY, USEC_POLL,
  69. TIMEOUT_POLL);
  70. return !!ready;
  71. }
  72. static int mtk_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  73. {
  74. struct mtk_rng *priv = to_mtk_rng(rng);
  75. int retval = 0;
  76. while (max >= sizeof(u32)) {
  77. if (!mtk_rng_wait_ready(rng, wait))
  78. break;
  79. *(u32 *)buf = readl(priv->base + RNG_DATA);
  80. retval += sizeof(u32);
  81. buf += sizeof(u32);
  82. max -= sizeof(u32);
  83. }
  84. return retval || !wait ? retval : -EIO;
  85. }
  86. static int mtk_rng_probe(struct platform_device *pdev)
  87. {
  88. struct resource *res;
  89. int ret;
  90. struct mtk_rng *priv;
  91. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  92. if (!res) {
  93. dev_err(&pdev->dev, "no iomem resource\n");
  94. return -ENXIO;
  95. }
  96. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  97. if (!priv)
  98. return -ENOMEM;
  99. priv->rng.name = pdev->name;
  100. priv->rng.init = mtk_rng_init;
  101. priv->rng.cleanup = mtk_rng_cleanup;
  102. priv->rng.read = mtk_rng_read;
  103. priv->clk = devm_clk_get(&pdev->dev, "rng");
  104. if (IS_ERR(priv->clk)) {
  105. ret = PTR_ERR(priv->clk);
  106. dev_err(&pdev->dev, "no clock for device: %d\n", ret);
  107. return ret;
  108. }
  109. priv->base = devm_ioremap_resource(&pdev->dev, res);
  110. if (IS_ERR(priv->base))
  111. return PTR_ERR(priv->base);
  112. ret = devm_hwrng_register(&pdev->dev, &priv->rng);
  113. if (ret) {
  114. dev_err(&pdev->dev, "failed to register rng device: %d\n",
  115. ret);
  116. return ret;
  117. }
  118. dev_info(&pdev->dev, "registered RNG driver\n");
  119. return 0;
  120. }
  121. static const struct of_device_id mtk_rng_match[] = {
  122. { .compatible = "mediatek,mt7623-rng" },
  123. {},
  124. };
  125. MODULE_DEVICE_TABLE(of, mtk_rng_match);
  126. static struct platform_driver mtk_rng_driver = {
  127. .probe = mtk_rng_probe,
  128. .driver = {
  129. .name = MTK_RNG_DEV,
  130. .of_match_table = mtk_rng_match,
  131. },
  132. };
  133. module_platform_driver(mtk_rng_driver);
  134. MODULE_DESCRIPTION("Mediatek Random Number Generator Driver");
  135. MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
  136. MODULE_LICENSE("GPL");