timeriomem-rng.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/char/hw_random/timeriomem-rng.c
  4. *
  5. * Copyright (C) 2009 Alexander Clouter <[email protected]>
  6. *
  7. * Derived from drivers/char/hw_random/omap-rng.c
  8. * Copyright 2005 (c) MontaVista Software, Inc.
  9. * Author: Deepak Saxena <[email protected]>
  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. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  100. if (!res)
  101. return -ENXIO;
  102. if (res->start % 4 != 0 || resource_size(res) < 4) {
  103. dev_err(&pdev->dev,
  104. "address must be at least four bytes wide and 32-bit aligned\n");
  105. return -EINVAL;
  106. }
  107. /* Allocate memory for the device structure (and zero it) */
  108. priv = devm_kzalloc(&pdev->dev,
  109. sizeof(struct timeriomem_rng_private), GFP_KERNEL);
  110. if (!priv)
  111. return -ENOMEM;
  112. platform_set_drvdata(pdev, priv);
  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. priv->rng_ops.quality = 0;
  127. } else {
  128. period = pdata->period;
  129. priv->rng_ops.quality = pdata->quality;
  130. }
  131. priv->period = ns_to_ktime(period * NSEC_PER_USEC);
  132. init_completion(&priv->completion);
  133. hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  134. priv->timer.function = timeriomem_rng_trigger;
  135. priv->rng_ops.name = dev_name(&pdev->dev);
  136. priv->rng_ops.read = timeriomem_rng_read;
  137. priv->io_base = devm_ioremap_resource(&pdev->dev, res);
  138. if (IS_ERR(priv->io_base)) {
  139. return PTR_ERR(priv->io_base);
  140. }
  141. /* Assume random data is already available. */
  142. priv->present = 1;
  143. complete(&priv->completion);
  144. err = devm_hwrng_register(&pdev->dev, &priv->rng_ops);
  145. if (err) {
  146. dev_err(&pdev->dev, "problem registering\n");
  147. return err;
  148. }
  149. dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
  150. priv->io_base, period);
  151. return 0;
  152. }
  153. static int timeriomem_rng_remove(struct platform_device *pdev)
  154. {
  155. struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
  156. hrtimer_cancel(&priv->timer);
  157. return 0;
  158. }
  159. static const struct of_device_id timeriomem_rng_match[] = {
  160. { .compatible = "timeriomem_rng" },
  161. {},
  162. };
  163. MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
  164. static struct platform_driver timeriomem_rng_driver = {
  165. .driver = {
  166. .name = "timeriomem_rng",
  167. .of_match_table = timeriomem_rng_match,
  168. },
  169. .probe = timeriomem_rng_probe,
  170. .remove = timeriomem_rng_remove,
  171. };
  172. module_platform_driver(timeriomem_rng_driver);
  173. MODULE_LICENSE("GPL");
  174. MODULE_AUTHOR("Alexander Clouter <[email protected]>");
  175. MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");