bcm2835-rng.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #define RNG_CTRL 0x0
  18. #define RNG_STATUS 0x4
  19. #define RNG_DATA 0x8
  20. #define RNG_INT_MASK 0x10
  21. /* enable rng */
  22. #define RNG_RBGEN 0x1
  23. /* the initial numbers generated are "less random" so will be discarded */
  24. #define RNG_WARMUP_COUNT 0x40000
  25. #define RNG_INT_OFF 0x1
  26. static void __init nsp_rng_init(void __iomem *base)
  27. {
  28. u32 val;
  29. /* mask the interrupt */
  30. val = readl(base + RNG_INT_MASK);
  31. val |= RNG_INT_OFF;
  32. writel(val, base + RNG_INT_MASK);
  33. }
  34. static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
  35. bool wait)
  36. {
  37. void __iomem *rng_base = (void __iomem *)rng->priv;
  38. u32 max_words = max / sizeof(u32);
  39. u32 num_words, count;
  40. while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
  41. if (!wait)
  42. return 0;
  43. cpu_relax();
  44. }
  45. num_words = readl(rng_base + RNG_STATUS) >> 24;
  46. if (num_words > max_words)
  47. num_words = max_words;
  48. for (count = 0; count < num_words; count++)
  49. ((u32 *)buf)[count] = readl(rng_base + RNG_DATA);
  50. return num_words * sizeof(u32);
  51. }
  52. static struct hwrng bcm2835_rng_ops = {
  53. .name = "bcm2835",
  54. .read = bcm2835_rng_read,
  55. };
  56. static const struct of_device_id bcm2835_rng_of_match[] = {
  57. { .compatible = "brcm,bcm2835-rng"},
  58. { .compatible = "brcm,bcm-nsp-rng", .data = nsp_rng_init},
  59. { .compatible = "brcm,bcm5301x-rng", .data = nsp_rng_init},
  60. {},
  61. };
  62. static int bcm2835_rng_probe(struct platform_device *pdev)
  63. {
  64. struct device *dev = &pdev->dev;
  65. struct device_node *np = dev->of_node;
  66. void (*rng_setup)(void __iomem *base);
  67. const struct of_device_id *rng_id;
  68. void __iomem *rng_base;
  69. int err;
  70. /* map peripheral */
  71. rng_base = of_iomap(np, 0);
  72. if (!rng_base) {
  73. dev_err(dev, "failed to remap rng regs");
  74. return -ENODEV;
  75. }
  76. bcm2835_rng_ops.priv = (unsigned long)rng_base;
  77. rng_id = of_match_node(bcm2835_rng_of_match, np);
  78. if (!rng_id) {
  79. iounmap(rng_base);
  80. return -EINVAL;
  81. }
  82. /* Check for rng init function, execute it */
  83. rng_setup = rng_id->data;
  84. if (rng_setup)
  85. rng_setup(rng_base);
  86. /* set warm-up count & enable */
  87. __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  88. __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  89. /* register driver */
  90. err = hwrng_register(&bcm2835_rng_ops);
  91. if (err) {
  92. dev_err(dev, "hwrng registration failed\n");
  93. iounmap(rng_base);
  94. } else
  95. dev_info(dev, "hwrng registered\n");
  96. return err;
  97. }
  98. static int bcm2835_rng_remove(struct platform_device *pdev)
  99. {
  100. void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
  101. /* disable rng hardware */
  102. __raw_writel(0, rng_base + RNG_CTRL);
  103. /* unregister driver */
  104. hwrng_unregister(&bcm2835_rng_ops);
  105. iounmap(rng_base);
  106. return 0;
  107. }
  108. MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
  109. static struct platform_driver bcm2835_rng_driver = {
  110. .driver = {
  111. .name = "bcm2835-rng",
  112. .of_match_table = bcm2835_rng_of_match,
  113. },
  114. .probe = bcm2835_rng_probe,
  115. .remove = bcm2835_rng_remove,
  116. };
  117. module_platform_driver(bcm2835_rng_driver);
  118. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  119. MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
  120. MODULE_LICENSE("GPL v2");