bcm2835-rng.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  4. * Copyright (c) 2013 Lubomir Rintel
  5. */
  6. #include <linux/hw_random.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/printk.h>
  13. #include <linux/clk.h>
  14. #include <linux/reset.h>
  15. #define RNG_CTRL 0x0
  16. #define RNG_STATUS 0x4
  17. #define RNG_DATA 0x8
  18. #define RNG_INT_MASK 0x10
  19. /* enable rng */
  20. #define RNG_RBGEN 0x1
  21. /* the initial numbers generated are "less random" so will be discarded */
  22. #define RNG_WARMUP_COUNT 0x40000
  23. #define RNG_INT_OFF 0x1
  24. struct bcm2835_rng_priv {
  25. struct hwrng rng;
  26. void __iomem *base;
  27. bool mask_interrupts;
  28. struct clk *clk;
  29. struct reset_control *reset;
  30. };
  31. static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
  32. {
  33. return container_of(rng, struct bcm2835_rng_priv, rng);
  34. }
  35. static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset)
  36. {
  37. /* MIPS chips strapped for BE will automagically configure the
  38. * peripheral registers for CPU-native byte order.
  39. */
  40. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  41. return __raw_readl(priv->base + offset);
  42. else
  43. return readl(priv->base + offset);
  44. }
  45. static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
  46. u32 offset)
  47. {
  48. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  49. __raw_writel(val, priv->base + offset);
  50. else
  51. writel(val, priv->base + offset);
  52. }
  53. static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
  54. bool wait)
  55. {
  56. struct bcm2835_rng_priv *priv = to_rng_priv(rng);
  57. u32 max_words = max / sizeof(u32);
  58. u32 num_words, count;
  59. while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
  60. if (!wait)
  61. return 0;
  62. hwrng_yield(rng);
  63. }
  64. num_words = rng_readl(priv, RNG_STATUS) >> 24;
  65. if (num_words > max_words)
  66. num_words = max_words;
  67. for (count = 0; count < num_words; count++)
  68. ((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
  69. return num_words * sizeof(u32);
  70. }
  71. static int bcm2835_rng_init(struct hwrng *rng)
  72. {
  73. struct bcm2835_rng_priv *priv = to_rng_priv(rng);
  74. int ret = 0;
  75. u32 val;
  76. ret = clk_prepare_enable(priv->clk);
  77. if (ret)
  78. return ret;
  79. ret = reset_control_reset(priv->reset);
  80. if (ret) {
  81. clk_disable_unprepare(priv->clk);
  82. return ret;
  83. }
  84. if (priv->mask_interrupts) {
  85. /* mask the interrupt */
  86. val = rng_readl(priv, RNG_INT_MASK);
  87. val |= RNG_INT_OFF;
  88. rng_writel(priv, val, RNG_INT_MASK);
  89. }
  90. /* set warm-up count & enable */
  91. rng_writel(priv, RNG_WARMUP_COUNT, RNG_STATUS);
  92. rng_writel(priv, RNG_RBGEN, RNG_CTRL);
  93. return ret;
  94. }
  95. static void bcm2835_rng_cleanup(struct hwrng *rng)
  96. {
  97. struct bcm2835_rng_priv *priv = to_rng_priv(rng);
  98. /* disable rng hardware */
  99. rng_writel(priv, 0, RNG_CTRL);
  100. clk_disable_unprepare(priv->clk);
  101. }
  102. struct bcm2835_rng_of_data {
  103. bool mask_interrupts;
  104. };
  105. static const struct bcm2835_rng_of_data nsp_rng_of_data = {
  106. .mask_interrupts = true,
  107. };
  108. static const struct of_device_id bcm2835_rng_of_match[] = {
  109. { .compatible = "brcm,bcm2835-rng"},
  110. { .compatible = "brcm,bcm-nsp-rng", .data = &nsp_rng_of_data },
  111. { .compatible = "brcm,bcm5301x-rng", .data = &nsp_rng_of_data },
  112. { .compatible = "brcm,bcm6368-rng"},
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
  116. static int bcm2835_rng_probe(struct platform_device *pdev)
  117. {
  118. struct device *dev = &pdev->dev;
  119. struct bcm2835_rng_priv *priv;
  120. int err;
  121. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  122. if (!priv)
  123. return -ENOMEM;
  124. /* map peripheral */
  125. priv->base = devm_platform_ioremap_resource(pdev, 0);
  126. if (IS_ERR(priv->base))
  127. return PTR_ERR(priv->base);
  128. /* Clock is optional on most platforms */
  129. priv->clk = devm_clk_get_optional(dev, NULL);
  130. if (IS_ERR(priv->clk))
  131. return PTR_ERR(priv->clk);
  132. priv->reset = devm_reset_control_get_optional_exclusive(dev, NULL);
  133. if (IS_ERR(priv->reset))
  134. return PTR_ERR(priv->reset);
  135. priv->rng.name = pdev->name;
  136. priv->rng.init = bcm2835_rng_init;
  137. priv->rng.read = bcm2835_rng_read;
  138. priv->rng.cleanup = bcm2835_rng_cleanup;
  139. if (dev_of_node(dev)) {
  140. const struct bcm2835_rng_of_data *of_data;
  141. /* Check for rng init function, execute it */
  142. of_data = of_device_get_match_data(dev);
  143. if (of_data)
  144. priv->mask_interrupts = of_data->mask_interrupts;
  145. }
  146. /* register driver */
  147. err = devm_hwrng_register(dev, &priv->rng);
  148. if (err)
  149. dev_err(dev, "hwrng registration failed\n");
  150. else
  151. dev_info(dev, "hwrng registered\n");
  152. return err;
  153. }
  154. static const struct platform_device_id bcm2835_rng_devtype[] = {
  155. { .name = "bcm2835-rng" },
  156. { .name = "bcm63xx-rng" },
  157. { /* sentinel */ }
  158. };
  159. MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype);
  160. static struct platform_driver bcm2835_rng_driver = {
  161. .driver = {
  162. .name = "bcm2835-rng",
  163. .of_match_table = bcm2835_rng_of_match,
  164. },
  165. .probe = bcm2835_rng_probe,
  166. .id_table = bcm2835_rng_devtype,
  167. };
  168. module_platform_driver(bcm2835_rng_driver);
  169. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  170. MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
  171. MODULE_LICENSE("GPL v2");