timeriomem-rng.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/char/hw_random/timeriomem-rng.c
  4. *
  5. * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  6. *
  7. * Derived from drivers/char/hw_random/omap-rng.c
  8. * Copyright 2005 (c) MontaVista Software, Inc.
  9. * Author: Deepak Saxena <dsaxena@plexity.net>
  10. *
  11. * Overview:
  12. * This driver is useful for platforms that have an IO range that provides
  13. * periodic random data from a single IO memory address. All the platform
  14. * has to do is provide the address and 'wait time' that new data becomes
  15. * available.
  16. *
  17. * TODO: add support for reading sizes other than 32bits and masking
  18. */
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/hrtimer.h>
  22. #include <linux/hw_random.h>
  23. #include <linux/io.h>
  24. #include <linux/ktime.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/time.h>
  30. #include <linux/timeriomem-rng.h>
  31. struct timeriomem_rng_private {
  32. void __iomem *io_base;
  33. ktime_t period;
  34. unsigned int present:1;
  35. struct hrtimer timer;
  36. struct completion completion;
  37. struct hwrng rng_ops;
  38. };
  39. static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
  40. size_t max, bool wait)
  41. {
  42. struct timeriomem_rng_private *priv =
  43. container_of(hwrng, struct timeriomem_rng_private, rng_ops);
  44. int retval = 0;
  45. int period_us = ktime_to_us(priv->period);
  46. /*
  47. * There may not have been enough time for new data to be generated
  48. * since the last request. If the caller doesn't want to wait, let them
  49. * bail out. Otherwise, wait for the completion. If the new data has
  50. * already been generated, the completion should already be available.
  51. */
  52. if (!wait && !priv->present)
  53. return 0;
  54. wait_for_completion(&priv->completion);
  55. do {
  56. /*
  57. * After the first read, all additional reads will need to wait
  58. * for the RNG to generate new data. Since the period can have
  59. * a wide range of values (1us to 1s have been observed), allow
  60. * for 1% tolerance in the sleep time rather than a fixed value.
  61. */
  62. if (retval > 0)
  63. usleep_range(period_us,
  64. period_us + max(1, period_us / 100));
  65. *(u32 *)data = readl(priv->io_base);
  66. retval += sizeof(u32);
  67. data += sizeof(u32);
  68. max -= sizeof(u32);
  69. } while (wait && max > sizeof(u32));
  70. /*
  71. * Block any new callers until the RNG has had time to generate new
  72. * data.
  73. */
  74. priv->present = 0;
  75. reinit_completion(&priv->completion);
  76. hrtimer_forward_now(&priv->timer, priv->period);
  77. hrtimer_restart(&priv->timer);
  78. return retval;
  79. }
  80. static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
  81. {
  82. struct timeriomem_rng_private *priv
  83. = container_of(timer, struct timeriomem_rng_private, timer);
  84. priv->present = 1;
  85. complete(&priv->completion);
  86. return HRTIMER_NORESTART;
  87. }
  88. static int timeriomem_rng_probe(struct platform_device *pdev)
  89. {
  90. struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
  91. struct timeriomem_rng_private *priv;
  92. struct resource *res;
  93. int err = 0;
  94. int period;
  95. if (!pdev->dev.of_node && !pdata) {
  96. dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
  97. return -EINVAL;
  98. }
  99. /* Allocate memory for the device structure (and zero it) */
  100. priv = devm_kzalloc(&pdev->dev,
  101. sizeof(struct timeriomem_rng_private), GFP_KERNEL);
  102. if (!priv)
  103. return -ENOMEM;
  104. platform_set_drvdata(pdev, priv);
  105. priv->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  106. if (IS_ERR(priv->io_base))
  107. return PTR_ERR(priv->io_base);
  108. if (res->start % 4 != 0 || resource_size(res) < 4) {
  109. dev_err(&pdev->dev,
  110. "address must be at least four bytes wide and 32-bit aligned\n");
  111. return -EINVAL;
  112. }
  113. if (pdev->dev.of_node) {
  114. int i;
  115. if (!of_property_read_u32(pdev->dev.of_node,
  116. "period", &i))
  117. period = i;
  118. else {
  119. dev_err(&pdev->dev, "missing period\n");
  120. return -EINVAL;
  121. }
  122. if (!of_property_read_u32(pdev->dev.of_node,
  123. "quality", &i))
  124. priv->rng_ops.quality = i;
  125. } else {
  126. period = pdata->period;
  127. priv->rng_ops.quality = pdata->quality;
  128. }
  129. priv->period = ns_to_ktime(period * NSEC_PER_USEC);
  130. init_completion(&priv->completion);
  131. hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  132. priv->timer.function = timeriomem_rng_trigger;
  133. priv->rng_ops.name = dev_name(&pdev->dev);
  134. priv->rng_ops.read = timeriomem_rng_read;
  135. /* Assume random data is already available. */
  136. priv->present = 1;
  137. complete(&priv->completion);
  138. err = devm_hwrng_register(&pdev->dev, &priv->rng_ops);
  139. if (err) {
  140. dev_err(&pdev->dev, "problem registering\n");
  141. return err;
  142. }
  143. dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
  144. priv->io_base, period);
  145. return 0;
  146. }
  147. static void timeriomem_rng_remove(struct platform_device *pdev)
  148. {
  149. struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
  150. hrtimer_cancel(&priv->timer);
  151. }
  152. static const struct of_device_id timeriomem_rng_match[] = {
  153. { .compatible = "timeriomem_rng" },
  154. {},
  155. };
  156. MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
  157. static struct platform_driver timeriomem_rng_driver = {
  158. .driver = {
  159. .name = "timeriomem_rng",
  160. .of_match_table = timeriomem_rng_match,
  161. },
  162. .probe = timeriomem_rng_probe,
  163. .remove_new = timeriomem_rng_remove,
  164. };
  165. module_platform_driver(timeriomem_rng_driver);
  166. MODULE_LICENSE("GPL");
  167. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  168. MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");