bcm2835-rng.c 5.1 KB

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