bcm63xx-rng.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Broadcom BCM63xx Random Number Generator support
  3. *
  4. * Copyright (C) 2011, Florian Fainelli <florian@openwrt.org>
  5. * Copyright (C) 2009, Broadcom Corporation
  6. *
  7. */
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/io.h>
  11. #include <linux/err.h>
  12. #include <linux/clk.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/hw_random.h>
  15. #define RNG_CTRL 0x00
  16. #define RNG_EN (1 << 0)
  17. #define RNG_STAT 0x04
  18. #define RNG_AVAIL_MASK (0xff000000)
  19. #define RNG_DATA 0x08
  20. #define RNG_THRES 0x0c
  21. #define RNG_MASK 0x10
  22. struct bcm63xx_rng_priv {
  23. struct hwrng rng;
  24. struct clk *clk;
  25. void __iomem *regs;
  26. };
  27. #define to_rng_priv(rng) container_of(rng, struct bcm63xx_rng_priv, rng)
  28. static int bcm63xx_rng_init(struct hwrng *rng)
  29. {
  30. struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
  31. u32 val;
  32. int error;
  33. error = clk_prepare_enable(priv->clk);
  34. if (error)
  35. return error;
  36. val = __raw_readl(priv->regs + RNG_CTRL);
  37. val |= RNG_EN;
  38. __raw_writel(val, priv->regs + RNG_CTRL);
  39. return 0;
  40. }
  41. static void bcm63xx_rng_cleanup(struct hwrng *rng)
  42. {
  43. struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
  44. u32 val;
  45. val = __raw_readl(priv->regs + RNG_CTRL);
  46. val &= ~RNG_EN;
  47. __raw_writel(val, priv->regs + RNG_CTRL);
  48. clk_disable_unprepare(priv->clk);
  49. }
  50. static int bcm63xx_rng_data_present(struct hwrng *rng, int wait)
  51. {
  52. struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
  53. return __raw_readl(priv->regs + RNG_STAT) & RNG_AVAIL_MASK;
  54. }
  55. static int bcm63xx_rng_data_read(struct hwrng *rng, u32 *data)
  56. {
  57. struct bcm63xx_rng_priv *priv = to_rng_priv(rng);
  58. *data = __raw_readl(priv->regs + RNG_DATA);
  59. return 4;
  60. }
  61. static int bcm63xx_rng_probe(struct platform_device *pdev)
  62. {
  63. struct resource *r;
  64. struct clk *clk;
  65. int ret;
  66. struct bcm63xx_rng_priv *priv;
  67. struct hwrng *rng;
  68. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69. if (!r) {
  70. dev_err(&pdev->dev, "no iomem resource\n");
  71. return -ENXIO;
  72. }
  73. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  74. if (!priv)
  75. return -ENOMEM;
  76. priv->rng.name = pdev->name;
  77. priv->rng.init = bcm63xx_rng_init;
  78. priv->rng.cleanup = bcm63xx_rng_cleanup;
  79. priv->rng.data_present = bcm63xx_rng_data_present;
  80. priv->rng.data_read = bcm63xx_rng_data_read;
  81. priv->clk = devm_clk_get(&pdev->dev, "ipsec");
  82. if (IS_ERR(priv->clk)) {
  83. ret = PTR_ERR(priv->clk);
  84. dev_err(&pdev->dev, "no clock for device: %d\n", ret);
  85. return ret;
  86. }
  87. if (!devm_request_mem_region(&pdev->dev, r->start,
  88. resource_size(r), pdev->name)) {
  89. dev_err(&pdev->dev, "request mem failed");
  90. return -EBUSY;
  91. }
  92. priv->regs = devm_ioremap_nocache(&pdev->dev, r->start,
  93. resource_size(r));
  94. if (!priv->regs) {
  95. dev_err(&pdev->dev, "ioremap failed");
  96. return -ENOMEM;
  97. }
  98. ret = devm_hwrng_register(&pdev->dev, &priv->rng);
  99. if (ret) {
  100. dev_err(&pdev->dev, "failed to register rng device: %d\n",
  101. ret);
  102. return ret;
  103. }
  104. dev_info(&pdev->dev, "registered RNG driver\n");
  105. return 0;
  106. }
  107. static struct platform_driver bcm63xx_rng_driver = {
  108. .probe = bcm63xx_rng_probe,
  109. .driver = {
  110. .name = "bcm63xx-rng",
  111. },
  112. };
  113. module_platform_driver(bcm63xx_rng_driver);
  114. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  115. MODULE_DESCRIPTION("Broadcom BCM63xx RNG driver");
  116. MODULE_LICENSE("GPL");