st-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * ST Random Number Generator Driver ST's Platforms
  3. *
  4. * Author: Pankaj Dev: <pankaj.dev@st.com>
  5. * Lee Jones <lee.jones@linaro.org>
  6. *
  7. * Copyright (C) 2015 STMicroelectronics (R&D) Limited
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/hw_random.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. /* Registers */
  22. #define ST_RNG_STATUS_REG 0x20
  23. #define ST_RNG_DATA_REG 0x24
  24. /* Registers fields */
  25. #define ST_RNG_STATUS_BAD_SEQUENCE BIT(0)
  26. #define ST_RNG_STATUS_BAD_ALTERNANCE BIT(1)
  27. #define ST_RNG_STATUS_FIFO_FULL BIT(5)
  28. #define ST_RNG_SAMPLE_SIZE 2 /* 2 Byte (16bit) samples */
  29. #define ST_RNG_FIFO_DEPTH 4
  30. #define ST_RNG_FIFO_SIZE (ST_RNG_FIFO_DEPTH * ST_RNG_SAMPLE_SIZE)
  31. /*
  32. * Samples are documented to be available every 0.667us, so in theory
  33. * the 4 sample deep FIFO should take 2.668us to fill. However, during
  34. * thorough testing, it became apparent that filling the FIFO actually
  35. * takes closer to 12us. We then multiply by 2 in order to account for
  36. * the lack of udelay()'s reliability, suggested by Russell King.
  37. */
  38. #define ST_RNG_FILL_FIFO_TIMEOUT (12 * 2)
  39. struct st_rng_data {
  40. void __iomem *base;
  41. struct clk *clk;
  42. struct hwrng ops;
  43. };
  44. static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  45. {
  46. struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
  47. u32 status;
  48. int i;
  49. if (max < sizeof(u16))
  50. return -EINVAL;
  51. /* Wait until FIFO is full - max 4uS*/
  52. for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
  53. status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
  54. if (status & ST_RNG_STATUS_FIFO_FULL)
  55. break;
  56. udelay(1);
  57. }
  58. if (i == ST_RNG_FILL_FIFO_TIMEOUT)
  59. return 0;
  60. for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
  61. *(u16 *)(data + i) =
  62. readl_relaxed(ddata->base + ST_RNG_DATA_REG);
  63. return i; /* No of bytes read */
  64. }
  65. static int st_rng_probe(struct platform_device *pdev)
  66. {
  67. struct st_rng_data *ddata;
  68. struct resource *res;
  69. struct clk *clk;
  70. void __iomem *base;
  71. int ret;
  72. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  73. if (!ddata)
  74. return -ENOMEM;
  75. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  76. base = devm_ioremap_resource(&pdev->dev, res);
  77. if (IS_ERR(base))
  78. return PTR_ERR(base);
  79. clk = devm_clk_get(&pdev->dev, NULL);
  80. if (IS_ERR(clk))
  81. return PTR_ERR(clk);
  82. ret = clk_prepare_enable(clk);
  83. if (ret)
  84. return ret;
  85. ddata->ops.priv = (unsigned long)ddata;
  86. ddata->ops.read = st_rng_read;
  87. ddata->ops.name = pdev->name;
  88. ddata->base = base;
  89. ddata->clk = clk;
  90. dev_set_drvdata(&pdev->dev, ddata);
  91. ret = hwrng_register(&ddata->ops);
  92. if (ret) {
  93. dev_err(&pdev->dev, "Failed to register HW RNG\n");
  94. return ret;
  95. }
  96. dev_info(&pdev->dev, "Successfully registered HW RNG\n");
  97. return 0;
  98. }
  99. static int st_rng_remove(struct platform_device *pdev)
  100. {
  101. struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
  102. hwrng_unregister(&ddata->ops);
  103. clk_disable_unprepare(ddata->clk);
  104. return 0;
  105. }
  106. static const struct of_device_id st_rng_match[] = {
  107. { .compatible = "st,rng" },
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(of, st_rng_match);
  111. static struct platform_driver st_rng_driver = {
  112. .driver = {
  113. .name = "st-hwrandom",
  114. .of_match_table = of_match_ptr(st_rng_match),
  115. },
  116. .probe = st_rng_probe,
  117. .remove = st_rng_remove
  118. };
  119. module_platform_driver(st_rng_driver);
  120. MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
  121. MODULE_LICENSE("GPL v2");