st-rng.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ST Random Number Generator Driver ST's Platforms
  4. *
  5. * Author: Pankaj Dev: <[email protected]>
  6. * Lee Jones <[email protected]>
  7. *
  8. * Copyright (C) 2015 STMicroelectronics (R&D) Limited
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/hw_random.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. /* Registers */
  20. #define ST_RNG_STATUS_REG 0x20
  21. #define ST_RNG_DATA_REG 0x24
  22. /* Registers fields */
  23. #define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
  24. #define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
  25. #define ST_RNG_STATUS_FIFO_FULL BIT(5)
  26. #define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
  27. #define ST_RNG_FIFO_DEPTH 4
  28. #define ST_RNG_FIFO_SIZE (ST_RNG_FIFO_DEPTH * ST_RNG_SAMPLE_SIZE)
  29. /*
  30. * Samples are documented to be available every 0.667us, so in theory
  31. * the 4 sample deep FIFO should take 2.668us to fill. However, during
  32. * thorough testing, it became apparent that filling the FIFO actually
  33. * takes closer to 12us. We then multiply by 2 in order to account for
  34. * the lack of udelay()'s reliability, suggested by Russell King.
  35. */
  36. #define ST_RNG_FILL_FIFO_TIMEOUT (12 * 2)
  37. struct st_rng_data {
  38. void __iomem *base;
  39. struct hwrng ops;
  40. };
  41. static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  42. {
  43. struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
  44. u32 status;
  45. int i;
  46. /* Wait until FIFO is full - max 4uS*/
  47. for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
  48. status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
  49. if (status & ST_RNG_STATUS_FIFO_FULL)
  50. break;
  51. udelay(1);
  52. }
  53. if (i == ST_RNG_FILL_FIFO_TIMEOUT)
  54. return 0;
  55. for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
  56. *(u16 *)(data + i) =
  57. readl_relaxed(ddata->base + ST_RNG_DATA_REG);
  58. return i; /* No of bytes read */
  59. }
  60. static int st_rng_probe(struct platform_device *pdev)
  61. {
  62. struct st_rng_data *ddata;
  63. struct clk *clk;
  64. void __iomem *base;
  65. int ret;
  66. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  67. if (!ddata)
  68. return -ENOMEM;
  69. base = devm_platform_ioremap_resource(pdev, 0);
  70. if (IS_ERR(base))
  71. return PTR_ERR(base);
  72. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  73. if (IS_ERR(clk))
  74. return PTR_ERR(clk);
  75. ddata->ops.priv = (unsigned long)ddata;
  76. ddata->ops.read = st_rng_read;
  77. ddata->ops.name = pdev->name;
  78. ddata->base = base;
  79. ret = devm_hwrng_register(&pdev->dev, &ddata->ops);
  80. if (ret) {
  81. dev_err(&pdev->dev, "Failed to register HW RNG\n");
  82. return ret;
  83. }
  84. dev_info(&pdev->dev, "Successfully registered HW RNG\n");
  85. return 0;
  86. }
  87. static const struct of_device_id st_rng_match[] __maybe_unused = {
  88. { .compatible = "st,rng" },
  89. {},
  90. };
  91. MODULE_DEVICE_TABLE(of, st_rng_match);
  92. static struct platform_driver st_rng_driver = {
  93. .driver = {
  94. .name = "st-hwrandom",
  95. .of_match_table = of_match_ptr(st_rng_match),
  96. },
  97. .probe = st_rng_probe,
  98. };
  99. module_platform_driver(st_rng_driver);
  100. MODULE_AUTHOR("Pankaj Dev <[email protected]>");
  101. MODULE_LICENSE("GPL v2");