bcm63xx-rng.c 3.4 KB

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