bcm2835-rng.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  3. * Copyright (c) 2013 Lubomir Rintel
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License ("GPL")
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. #include <linux/hw_random.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/printk.h>
  17. #include <linux/clk.h>
  18. #define RNG_CTRL 0x0
  19. #define RNG_STATUS 0x4
  20. #define RNG_DATA 0x8
  21. #define RNG_INT_MASK 0x10
  22. /* enable rng */
  23. #define RNG_RBGEN 0x1
  24. /* the initial numbers generated are "less random" so will be discarded */
  25. #define RNG_WARMUP_COUNT 0x40000
  26. #define RNG_INT_OFF 0x1
  27. struct bcm2835_rng_priv {
  28. struct hwrng rng;
  29. void __iomem *base;
  30. bool mask_interrupts;
  31. struct clk *clk;
  32. };
  33. static inline struct bcm2835_rng_priv *to_rng_priv(struct hwrng *rng)
  34. {
  35. return container_of(rng, struct bcm2835_rng_priv, rng);
  36. }
  37. static inline u32 rng_readl(struct bcm2835_rng_priv *priv, u32 offset)
  38. {
  39. /* MIPS chips strapped for BE will automagically configure the
  40. * peripheral registers for CPU-native byte order.
  41. */
  42. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  43. return __raw_readl(priv->base + offset);
  44. else
  45. return readl(priv->base + offset);
  46. }
  47. static inline void rng_writel(struct bcm2835_rng_priv *priv, u32 val,
  48. u32 offset)
  49. {
  50. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  51. __raw_writel(val, priv->base + offset);
  52. else
  53. writel(val, priv->base + offset);
  54. }
  55. static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
  56. bool wait)
  57. {
  58. struct bcm2835_rng_priv *priv = to_rng_priv(rng);
  59. u32 max_words = max / sizeof(u32);
  60. u32 num_words, count;
  61. while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
  62. if (!wait)
  63. return 0;
  64. cpu_relax();
  65. }
  66. num_words = rng_readl(priv, RNG_STATUS) >> 24;
  67. if (num_words > max_words)
  68. num_words = max_words;
  69. for (count = 0; count < num_words; count++)
  70. ((u32 *)buf)[count] = rng_readl(priv, RNG_DATA);
  71. return num_words * sizeof(u32);
  72. }
  73. static int bcm2835_rng_init(struct hwrng *rng)
  74. {
  75. struct bcm2835_rng_priv *priv = to_rng_priv(rng);
  76. int ret = 0;
  77. u32 val;
  78. if (!IS_ERR(priv->clk)) {
  79. ret = clk_prepare_enable(priv->clk);
  80. if (ret)
  81. return ret;
  82. }
  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. if (!IS_ERR(priv->clk))
  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. static int bcm2835_rng_probe(struct platform_device *pdev)
  116. {
  117. const struct bcm2835_rng_of_data *of_data;
  118. struct device *dev = &pdev->dev;
  119. struct device_node *np = dev->of_node;
  120. const struct of_device_id *rng_id;
  121. struct bcm2835_rng_priv *priv;
  122. struct resource *r;
  123. int err;
  124. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  125. if (!priv)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, priv);
  128. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. /* map peripheral */
  130. priv->base = devm_ioremap_resource(dev, r);
  131. if (IS_ERR(priv->base))
  132. return PTR_ERR(priv->base);
  133. /* Clock is optional on most platforms */
  134. priv->clk = devm_clk_get(dev, NULL);
  135. if (IS_ERR(priv->clk) && PTR_ERR(priv->clk) == -EPROBE_DEFER)
  136. return -EPROBE_DEFER;
  137. priv->rng.name = pdev->name;
  138. priv->rng.init = bcm2835_rng_init;
  139. priv->rng.read = bcm2835_rng_read;
  140. priv->rng.cleanup = bcm2835_rng_cleanup;
  141. if (dev_of_node(dev)) {
  142. rng_id = of_match_node(bcm2835_rng_of_match, np);
  143. if (!rng_id)
  144. return -EINVAL;
  145. /* Check for rng init function, execute it */
  146. of_data = rng_id->data;
  147. if (of_data)
  148. priv->mask_interrupts = of_data->mask_interrupts;
  149. }
  150. /* register driver */
  151. err = devm_hwrng_register(dev, &priv->rng);
  152. if (err)
  153. dev_err(dev, "hwrng registration failed\n");
  154. else
  155. dev_info(dev, "hwrng registered\n");
  156. return err;
  157. }
  158. MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
  159. static struct platform_device_id bcm2835_rng_devtype[] = {
  160. { .name = "bcm2835-rng" },
  161. { .name = "bcm63xx-rng" },
  162. { /* sentinel */ }
  163. };
  164. MODULE_DEVICE_TABLE(platform, bcm2835_rng_devtype);
  165. static struct platform_driver bcm2835_rng_driver = {
  166. .driver = {
  167. .name = "bcm2835-rng",
  168. .of_match_table = bcm2835_rng_of_match,
  169. },
  170. .probe = bcm2835_rng_probe,
  171. .id_table = bcm2835_rng_devtype,
  172. };
  173. module_platform_driver(bcm2835_rng_driver);
  174. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  175. MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
  176. MODULE_LICENSE("GPL v2");