mcs814x-rng.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * RNG driver for Moschip MCS814x SoC
  3. *
  4. * Copyright 2012 (C), Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/hw_random.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #define STAT 0x00
  19. #define RND 0x04
  20. struct mcs814x_rng_priv {
  21. void __iomem *regs;
  22. };
  23. static int mcs814x_rng_data_read(struct hwrng *rng, u32 *buffer)
  24. {
  25. struct mcs814x_rng_priv *priv = (struct mcs814x_rng_priv *)rng->priv;
  26. *buffer = readl_relaxed(priv->regs + RND);
  27. return 4;
  28. }
  29. static int mcs814x_rng_probe(struct platform_device *pdev)
  30. {
  31. struct resource *res;
  32. struct mcs814x_rng_priv *priv;
  33. struct hwrng *rng;
  34. int ret;
  35. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  36. if (!res)
  37. return -ENODEV;
  38. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  39. if (!priv) {
  40. ret = -ENOMEM;
  41. goto out;
  42. }
  43. rng = kzalloc(sizeof(*rng), GFP_KERNEL);
  44. if (!rng) {
  45. ret = -ENOMEM;
  46. goto out_priv;
  47. }
  48. platform_set_drvdata(pdev, rng);
  49. rng->priv = (unsigned long)priv;
  50. rng->name = pdev->name;
  51. rng->data_read = mcs814x_rng_data_read;
  52. priv->regs = devm_ioremap_resource(&pdev->dev, res);
  53. if (!priv->regs) {
  54. ret = -ENOMEM;
  55. goto out_rng;
  56. }
  57. ret = hwrng_register(rng);
  58. if (ret) {
  59. dev_err(&pdev->dev, "failed to register hwrng driver\n");
  60. goto out;
  61. }
  62. dev_info(&pdev->dev, "registered\n");
  63. return ret;
  64. out_rng:
  65. platform_set_drvdata(pdev, NULL);
  66. kfree(rng);
  67. out_priv:
  68. kfree(priv);
  69. out:
  70. return ret;
  71. }
  72. static int mcs814x_rng_remove(struct platform_device *pdev)
  73. {
  74. struct hwrng *rng = platform_get_drvdata(pdev);
  75. struct mcs814x_rng_priv *priv = (struct mcs814x_rng_priv *)rng->priv;
  76. hwrng_unregister(rng);
  77. kfree(priv);
  78. kfree(rng);
  79. platform_set_drvdata(pdev, NULL);
  80. return 0;
  81. }
  82. static const struct of_device_id mcs814x_rng_ids[] = {
  83. { .compatible = "moschip,mcs814x-rng", },
  84. { /* sentinel */ },
  85. };
  86. static struct platform_driver mcs814x_rng_driver = {
  87. .driver = {
  88. .name = "mcs814x-rng",
  89. .owner = THIS_MODULE,
  90. .of_match_table = mcs814x_rng_ids,
  91. },
  92. .probe = mcs814x_rng_probe,
  93. .remove = mcs814x_rng_remove,
  94. };
  95. module_platform_driver(mcs814x_rng_driver);
  96. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  97. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) for Moschip MCS814x");
  98. MODULE_LICENSE("GPL");