bcm63xx-rng.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. int ret;
  65. struct bcm63xx_rng_priv *priv;
  66. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  67. if (!r) {
  68. dev_err(&pdev->dev, "no iomem resource\n");
  69. return -ENXIO;
  70. }
  71. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  72. if (!priv)
  73. return -ENOMEM;
  74. priv->rng.name = pdev->name;
  75. priv->rng.init = bcm63xx_rng_init;
  76. priv->rng.cleanup = bcm63xx_rng_cleanup;
  77. priv->rng.data_present = bcm63xx_rng_data_present;
  78. priv->rng.data_read = bcm63xx_rng_data_read;
  79. priv->clk = devm_clk_get(&pdev->dev, "ipsec");
  80. if (IS_ERR(priv->clk)) {
  81. ret = PTR_ERR(priv->clk);
  82. dev_err(&pdev->dev, "no clock for device: %d\n", ret);
  83. return ret;
  84. }
  85. if (!devm_request_mem_region(&pdev->dev, r->start,
  86. resource_size(r), pdev->name)) {
  87. dev_err(&pdev->dev, "request mem failed");
  88. return -EBUSY;
  89. }
  90. priv->regs = devm_ioremap_nocache(&pdev->dev, r->start,
  91. resource_size(r));
  92. if (!priv->regs) {
  93. dev_err(&pdev->dev, "ioremap failed");
  94. return -ENOMEM;
  95. }
  96. ret = devm_hwrng_register(&pdev->dev, &priv->rng);
  97. if (ret) {
  98. dev_err(&pdev->dev, "failed to register rng device: %d\n",
  99. ret);
  100. return ret;
  101. }
  102. dev_info(&pdev->dev, "registered RNG driver\n");
  103. return 0;
  104. }
  105. #ifdef CONFIG_OF
  106. static const struct of_device_id bcm63xx_rng_of_match[] = {
  107. { .compatible = "brcm,bcm6368-rng", },
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(of, bcm63xx_rng_of_match);
  111. #endif
  112. static struct platform_driver bcm63xx_rng_driver = {
  113. .probe = bcm63xx_rng_probe,
  114. .driver = {
  115. .name = "bcm63xx-rng",
  116. .of_match_table = of_match_ptr(bcm63xx_rng_of_match),
  117. },
  118. };
  119. module_platform_driver(bcm63xx_rng_driver);
  120. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  121. MODULE_DESCRIPTION("Broadcom BCM63xx RNG driver");
  122. MODULE_LICENSE("GPL");