bcm2835-rng.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/printk.h>
  18. #define RNG_CTRL 0x0
  19. #define RNG_STATUS 0x4
  20. #define RNG_DATA 0x8
  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. static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
  26. bool wait)
  27. {
  28. void __iomem *rng_base = (void __iomem *)rng->priv;
  29. while ((__raw_readl(rng_base + RNG_STATUS) >> 24) == 0) {
  30. if (!wait)
  31. return 0;
  32. cpu_relax();
  33. }
  34. *(u32 *)buf = __raw_readl(rng_base + RNG_DATA);
  35. return sizeof(u32);
  36. }
  37. static struct hwrng bcm2835_rng_ops = {
  38. .name = "bcm2835",
  39. .read = bcm2835_rng_read,
  40. };
  41. static int bcm2835_rng_probe(struct platform_device *pdev)
  42. {
  43. struct device *dev = &pdev->dev;
  44. struct device_node *np = dev->of_node;
  45. void __iomem *rng_base;
  46. int err;
  47. /* map peripheral */
  48. rng_base = of_iomap(np, 0);
  49. if (!rng_base) {
  50. dev_err(dev, "failed to remap rng regs");
  51. return -ENODEV;
  52. }
  53. bcm2835_rng_ops.priv = (unsigned long)rng_base;
  54. /* register driver */
  55. err = hwrng_register(&bcm2835_rng_ops);
  56. if (err) {
  57. dev_err(dev, "hwrng registration failed\n");
  58. iounmap(rng_base);
  59. } else {
  60. dev_info(dev, "hwrng registered\n");
  61. /* set warm-up count & enable */
  62. __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  63. __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  64. }
  65. return err;
  66. }
  67. static int bcm2835_rng_remove(struct platform_device *pdev)
  68. {
  69. void __iomem *rng_base = (void __iomem *)bcm2835_rng_ops.priv;
  70. /* disable rng hardware */
  71. __raw_writel(0, rng_base + RNG_CTRL);
  72. /* unregister driver */
  73. hwrng_unregister(&bcm2835_rng_ops);
  74. iounmap(rng_base);
  75. return 0;
  76. }
  77. static const struct of_device_id bcm2835_rng_of_match[] = {
  78. { .compatible = "brcm,bcm2835-rng", },
  79. {},
  80. };
  81. MODULE_DEVICE_TABLE(of, bcm2835_rng_of_match);
  82. static struct platform_driver bcm2835_rng_driver = {
  83. .driver = {
  84. .name = "bcm2835-rng",
  85. .owner = THIS_MODULE,
  86. .of_match_table = bcm2835_rng_of_match,
  87. },
  88. .probe = bcm2835_rng_probe,
  89. .remove = bcm2835_rng_remove,
  90. };
  91. module_platform_driver(bcm2835_rng_driver);
  92. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  93. MODULE_DESCRIPTION("BCM2835 Random Number Generator (RNG) driver");
  94. MODULE_LICENSE("GPL v2");