powernv-rng.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/kernel.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/random.h>
  11. #include <linux/hw_random.h>
  12. static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  13. {
  14. unsigned long *buf;
  15. int i, len;
  16. /* We rely on rng_buffer_size() being >= sizeof(unsigned long) */
  17. len = max / sizeof(unsigned long);
  18. buf = (unsigned long *)data;
  19. for (i = 0; i < len; i++)
  20. pnv_get_random_long(buf++);
  21. return len * sizeof(unsigned long);
  22. }
  23. static struct hwrng powernv_hwrng = {
  24. .name = "powernv-rng",
  25. .read = powernv_rng_read,
  26. };
  27. static int powernv_rng_probe(struct platform_device *pdev)
  28. {
  29. int rc;
  30. rc = devm_hwrng_register(&pdev->dev, &powernv_hwrng);
  31. if (rc) {
  32. /* We only register one device, ignore any others */
  33. if (rc == -EEXIST)
  34. rc = -ENODEV;
  35. return rc;
  36. }
  37. pr_info("Registered powernv hwrng.\n");
  38. return 0;
  39. }
  40. static const struct of_device_id powernv_rng_match[] = {
  41. { .compatible = "ibm,power-rng",},
  42. {},
  43. };
  44. MODULE_DEVICE_TABLE(of, powernv_rng_match);
  45. static struct platform_driver powernv_rng_driver = {
  46. .driver = {
  47. .name = "powernv_rng",
  48. .of_match_table = powernv_rng_match,
  49. },
  50. .probe = powernv_rng_probe,
  51. };
  52. module_platform_driver(powernv_rng_driver);
  53. MODULE_LICENSE("GPL");
  54. MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above");