meson-rng.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (c) 2016 BayLibre, SAS.
  8. * Author: Neil Armstrong <narmstrong@baylibre.com>
  9. * Copyright (C) 2014 Amlogic, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright (c) 2016 BayLibre, SAS.
  28. * Author: Neil Armstrong <narmstrong@baylibre.com>
  29. * Copyright (C) 2014 Amlogic, Inc.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions
  33. * are met:
  34. *
  35. * * Redistributions of source code must retain the above copyright
  36. * notice, this list of conditions and the following disclaimer.
  37. * * Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in
  39. * the documentation and/or other materials provided with the
  40. * distribution.
  41. * * Neither the name of Intel Corporation nor the names of its
  42. * contributors may be used to endorse or promote products derived
  43. * from this software without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  49. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  51. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  52. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  53. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  55. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. #include <linux/err.h>
  58. #include <linux/module.h>
  59. #include <linux/io.h>
  60. #include <linux/platform_device.h>
  61. #include <linux/hw_random.h>
  62. #include <linux/slab.h>
  63. #include <linux/types.h>
  64. #include <linux/of.h>
  65. #define RNG_DATA 0x00
  66. struct meson_rng_data {
  67. void __iomem *base;
  68. struct platform_device *pdev;
  69. struct hwrng rng;
  70. };
  71. static int meson_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  72. {
  73. struct meson_rng_data *data =
  74. container_of(rng, struct meson_rng_data, rng);
  75. *(u32 *)buf = readl_relaxed(data->base + RNG_DATA);
  76. return sizeof(u32);
  77. }
  78. static int meson_rng_probe(struct platform_device *pdev)
  79. {
  80. struct device *dev = &pdev->dev;
  81. struct meson_rng_data *data;
  82. struct resource *res;
  83. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  84. if (!data)
  85. return -ENOMEM;
  86. data->pdev = pdev;
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. data->base = devm_ioremap_resource(dev, res);
  89. if (IS_ERR(data->base))
  90. return PTR_ERR(data->base);
  91. data->rng.name = pdev->name;
  92. data->rng.read = meson_rng_read;
  93. platform_set_drvdata(pdev, data);
  94. return devm_hwrng_register(dev, &data->rng);
  95. }
  96. static const struct of_device_id meson_rng_of_match[] = {
  97. { .compatible = "amlogic,meson-rng", },
  98. {},
  99. };
  100. static struct platform_driver meson_rng_driver = {
  101. .probe = meson_rng_probe,
  102. .driver = {
  103. .name = "meson-rng",
  104. .of_match_table = meson_rng_of_match,
  105. },
  106. };
  107. module_platform_driver(meson_rng_driver);
  108. MODULE_ALIAS("platform:meson-rng");
  109. MODULE_DESCRIPTION("Meson H/W Random Number Generator driver");
  110. MODULE_AUTHOR("Lawrence Mok <lawrence.mok@amlogic.com>");
  111. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  112. MODULE_LICENSE("Dual BSD/GPL");