msm-rng.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/hw_random.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. /* Device specific register offsets */
  22. #define PRNG_DATA_OUT 0x0000
  23. #define PRNG_STATUS 0x0004
  24. #define PRNG_LFSR_CFG 0x0100
  25. #define PRNG_CONFIG 0x0104
  26. /* Device specific register masks and config values */
  27. #define PRNG_LFSR_CFG_MASK 0x0000ffff
  28. #define PRNG_LFSR_CFG_CLOCKS 0x0000dddd
  29. #define PRNG_CONFIG_HW_ENABLE BIT(1)
  30. #define PRNG_STATUS_DATA_AVAIL BIT(0)
  31. #define MAX_HW_FIFO_DEPTH 16
  32. #define MAX_HW_FIFO_SIZE (MAX_HW_FIFO_DEPTH * 4)
  33. #define WORD_SZ 4
  34. struct msm_rng {
  35. void __iomem *base;
  36. struct clk *clk;
  37. struct hwrng hwrng;
  38. };
  39. #define to_msm_rng(p) container_of(p, struct msm_rng, hwrng)
  40. static int msm_rng_enable(struct hwrng *hwrng, int enable)
  41. {
  42. struct msm_rng *rng = to_msm_rng(hwrng);
  43. u32 val;
  44. int ret;
  45. ret = clk_prepare_enable(rng->clk);
  46. if (ret)
  47. return ret;
  48. if (enable) {
  49. /* Enable PRNG only if it is not already enabled */
  50. val = readl_relaxed(rng->base + PRNG_CONFIG);
  51. if (val & PRNG_CONFIG_HW_ENABLE)
  52. goto already_enabled;
  53. val = readl_relaxed(rng->base + PRNG_LFSR_CFG);
  54. val &= ~PRNG_LFSR_CFG_MASK;
  55. val |= PRNG_LFSR_CFG_CLOCKS;
  56. writel(val, rng->base + PRNG_LFSR_CFG);
  57. val = readl_relaxed(rng->base + PRNG_CONFIG);
  58. val |= PRNG_CONFIG_HW_ENABLE;
  59. writel(val, rng->base + PRNG_CONFIG);
  60. } else {
  61. val = readl_relaxed(rng->base + PRNG_CONFIG);
  62. val &= ~PRNG_CONFIG_HW_ENABLE;
  63. writel(val, rng->base + PRNG_CONFIG);
  64. }
  65. already_enabled:
  66. clk_disable_unprepare(rng->clk);
  67. return 0;
  68. }
  69. static int msm_rng_read(struct hwrng *hwrng, void *data, size_t max, bool wait)
  70. {
  71. struct msm_rng *rng = to_msm_rng(hwrng);
  72. size_t currsize = 0;
  73. u32 *retdata = data;
  74. size_t maxsize;
  75. int ret;
  76. u32 val;
  77. /* calculate max size bytes to transfer back to caller */
  78. maxsize = min_t(size_t, MAX_HW_FIFO_SIZE, max);
  79. /* no room for word data */
  80. if (maxsize < WORD_SZ)
  81. return 0;
  82. ret = clk_prepare_enable(rng->clk);
  83. if (ret)
  84. return ret;
  85. /* read random data from hardware */
  86. do {
  87. val = readl_relaxed(rng->base + PRNG_STATUS);
  88. if (!(val & PRNG_STATUS_DATA_AVAIL))
  89. break;
  90. val = readl_relaxed(rng->base + PRNG_DATA_OUT);
  91. if (!val)
  92. break;
  93. *retdata++ = val;
  94. currsize += WORD_SZ;
  95. /* make sure we stay on 32bit boundary */
  96. if ((maxsize - currsize) < WORD_SZ)
  97. break;
  98. } while (currsize < maxsize);
  99. clk_disable_unprepare(rng->clk);
  100. return currsize;
  101. }
  102. static int msm_rng_init(struct hwrng *hwrng)
  103. {
  104. return msm_rng_enable(hwrng, 1);
  105. }
  106. static void msm_rng_cleanup(struct hwrng *hwrng)
  107. {
  108. msm_rng_enable(hwrng, 0);
  109. }
  110. static int msm_rng_probe(struct platform_device *pdev)
  111. {
  112. struct resource *res;
  113. struct msm_rng *rng;
  114. int ret;
  115. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  116. if (!rng)
  117. return -ENOMEM;
  118. platform_set_drvdata(pdev, rng);
  119. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  120. rng->base = devm_ioremap_resource(&pdev->dev, res);
  121. if (IS_ERR(rng->base))
  122. return PTR_ERR(rng->base);
  123. rng->clk = devm_clk_get(&pdev->dev, "core");
  124. if (IS_ERR(rng->clk))
  125. return PTR_ERR(rng->clk);
  126. rng->hwrng.name = KBUILD_MODNAME,
  127. rng->hwrng.init = msm_rng_init,
  128. rng->hwrng.cleanup = msm_rng_cleanup,
  129. rng->hwrng.read = msm_rng_read,
  130. ret = devm_hwrng_register(&pdev->dev, &rng->hwrng);
  131. if (ret) {
  132. dev_err(&pdev->dev, "failed to register hwrng\n");
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. static const struct of_device_id msm_rng_of_match[] = {
  138. { .compatible = "qcom,prng", },
  139. {}
  140. };
  141. MODULE_DEVICE_TABLE(of, msm_rng_of_match);
  142. static struct platform_driver msm_rng_driver = {
  143. .probe = msm_rng_probe,
  144. .driver = {
  145. .name = KBUILD_MODNAME,
  146. .of_match_table = of_match_ptr(msm_rng_of_match),
  147. }
  148. };
  149. module_platform_driver(msm_rng_driver);
  150. MODULE_ALIAS("platform:" KBUILD_MODNAME);
  151. MODULE_AUTHOR("The Linux Foundation");
  152. MODULE_DESCRIPTION("Qualcomm MSM random number generator driver");
  153. MODULE_LICENSE("GPL v2");