st-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /* Wait until FIFO is full - max 4uS*/
  50. for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
  51. status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
  52. if (status & ST_RNG_STATUS_FIFO_FULL)
  53. break;
  54. udelay(1);
  55. }
  56. if (i == ST_RNG_FILL_FIFO_TIMEOUT)
  57. return 0;
  58. for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
  59. *(u16 *)(data + i) =
  60. readl_relaxed(ddata->base + ST_RNG_DATA_REG);
  61. return i; /* No of bytes read */
  62. }
  63. static int st_rng_probe(struct platform_device *pdev)
  64. {
  65. struct st_rng_data *ddata;
  66. struct resource *res;
  67. struct clk *clk;
  68. void __iomem *base;
  69. int ret;
  70. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  71. if (!ddata)
  72. return -ENOMEM;
  73. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  74. base = devm_ioremap_resource(&pdev->dev, res);
  75. if (IS_ERR(base))
  76. return PTR_ERR(base);
  77. clk = devm_clk_get(&pdev->dev, NULL);
  78. if (IS_ERR(clk))
  79. return PTR_ERR(clk);
  80. ret = clk_prepare_enable(clk);
  81. if (ret)
  82. return ret;
  83. ddata->ops.priv = (unsigned long)ddata;
  84. ddata->ops.read = st_rng_read;
  85. ddata->ops.name = pdev->name;
  86. ddata->base = base;
  87. ddata->clk = clk;
  88. dev_set_drvdata(&pdev->dev, ddata);
  89. ret = hwrng_register(&ddata->ops);
  90. if (ret) {
  91. dev_err(&pdev->dev, "Failed to register HW RNG\n");
  92. clk_disable_unprepare(clk);
  93. return ret;
  94. }
  95. dev_info(&pdev->dev, "Successfully registered HW RNG\n");
  96. return 0;
  97. }
  98. static int st_rng_remove(struct platform_device *pdev)
  99. {
  100. struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
  101. hwrng_unregister(&ddata->ops);
  102. clk_disable_unprepare(ddata->clk);
  103. return 0;
  104. }
  105. static const struct of_device_id st_rng_match[] = {
  106. { .compatible = "st,rng" },
  107. {},
  108. };
  109. MODULE_DEVICE_TABLE(of, st_rng_match);
  110. static struct platform_driver st_rng_driver = {
  111. .driver = {
  112. .name = "st-hwrandom",
  113. .of_match_table = of_match_ptr(st_rng_match),
  114. },
  115. .probe = st_rng_probe,
  116. .remove = st_rng_remove
  117. };
  118. module_platform_driver(st_rng_driver);
  119. MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
  120. MODULE_LICENSE("GPL v2");