bcm2835-rng.c 2.6 KB

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