powernv-rng.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/random.h>
  14. #include <linux/hw_random.h>
  15. static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  16. {
  17. unsigned long *buf;
  18. int i, len;
  19. /* We rely on rng_buffer_size() being >= sizeof(unsigned long) */
  20. len = max / sizeof(unsigned long);
  21. buf = (unsigned long *)data;
  22. for (i = 0; i < len; i++)
  23. powernv_get_random_long(buf++);
  24. return len * sizeof(unsigned long);
  25. }
  26. static struct hwrng powernv_hwrng = {
  27. .name = "powernv-rng",
  28. .read = powernv_rng_read,
  29. };
  30. static int powernv_rng_remove(struct platform_device *pdev)
  31. {
  32. hwrng_unregister(&powernv_hwrng);
  33. return 0;
  34. }
  35. static int powernv_rng_probe(struct platform_device *pdev)
  36. {
  37. int rc;
  38. rc = hwrng_register(&powernv_hwrng);
  39. if (rc) {
  40. /* We only register one device, ignore any others */
  41. if (rc == -EEXIST)
  42. rc = -ENODEV;
  43. return rc;
  44. }
  45. pr_info("Registered powernv hwrng.\n");
  46. return 0;
  47. }
  48. static const struct of_device_id powernv_rng_match[] = {
  49. { .compatible = "ibm,power-rng",},
  50. {},
  51. };
  52. MODULE_DEVICE_TABLE(of, powernv_rng_match);
  53. static struct platform_driver powernv_rng_driver = {
  54. .driver = {
  55. .name = "powernv_rng",
  56. .of_match_table = powernv_rng_match,
  57. },
  58. .probe = powernv_rng_probe,
  59. .remove = powernv_rng_remove,
  60. };
  61. module_platform_driver(powernv_rng_driver);
  62. MODULE_LICENSE("GPL");
  63. MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above");