octeon-rng.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Hardware Random Number Generator support for Cavium Networks
  3. * Octeon processor family.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * Copyright (C) 2009 Cavium Networks
  10. */
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/device.h>
  14. #include <linux/hw_random.h>
  15. #include <linux/io.h>
  16. #include <linux/gfp.h>
  17. #include <asm/octeon/octeon.h>
  18. #include <asm/octeon/cvmx-rnm-defs.h>
  19. struct octeon_rng {
  20. struct hwrng ops;
  21. void __iomem *control_status;
  22. void __iomem *result;
  23. };
  24. static int octeon_rng_init(struct hwrng *rng)
  25. {
  26. union cvmx_rnm_ctl_status ctl;
  27. struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
  28. ctl.u64 = 0;
  29. ctl.s.ent_en = 1; /* Enable the entropy source. */
  30. ctl.s.rng_en = 1; /* Enable the RNG hardware. */
  31. cvmx_write_csr((u64)p->control_status, ctl.u64);
  32. return 0;
  33. }
  34. static void octeon_rng_cleanup(struct hwrng *rng)
  35. {
  36. union cvmx_rnm_ctl_status ctl;
  37. struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
  38. ctl.u64 = 0;
  39. /* Disable everything. */
  40. cvmx_write_csr((u64)p->control_status, ctl.u64);
  41. }
  42. static int octeon_rng_data_read(struct hwrng *rng, u32 *data)
  43. {
  44. struct octeon_rng *p = container_of(rng, struct octeon_rng, ops);
  45. *data = cvmx_read64_uint32((u64)p->result);
  46. return sizeof(u32);
  47. }
  48. static int octeon_rng_probe(struct platform_device *pdev)
  49. {
  50. struct resource *res_ports;
  51. struct resource *res_result;
  52. struct octeon_rng *rng;
  53. int ret;
  54. struct hwrng ops = {
  55. .name = "octeon",
  56. .init = octeon_rng_init,
  57. .cleanup = octeon_rng_cleanup,
  58. .data_read = octeon_rng_data_read
  59. };
  60. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  61. if (!rng)
  62. return -ENOMEM;
  63. res_ports = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  64. if (!res_ports)
  65. return -ENOENT;
  66. res_result = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  67. if (!res_result)
  68. return -ENOENT;
  69. rng->control_status = devm_ioremap_nocache(&pdev->dev,
  70. res_ports->start,
  71. sizeof(u64));
  72. if (!rng->control_status)
  73. return -ENOENT;
  74. rng->result = devm_ioremap_nocache(&pdev->dev,
  75. res_result->start,
  76. sizeof(u64));
  77. if (!rng->result)
  78. return -ENOENT;
  79. rng->ops = ops;
  80. platform_set_drvdata(pdev, &rng->ops);
  81. ret = devm_hwrng_register(&pdev->dev, &rng->ops);
  82. if (ret)
  83. return -ENOENT;
  84. dev_info(&pdev->dev, "Octeon Random Number Generator\n");
  85. return 0;
  86. }
  87. static struct platform_driver octeon_rng_driver = {
  88. .driver = {
  89. .name = "octeon_rng",
  90. },
  91. .probe = octeon_rng_probe,
  92. };
  93. module_platform_driver(octeon_rng_driver);
  94. MODULE_AUTHOR("David Daney");
  95. MODULE_LICENSE("GPL");