timeriomem-rng.c 5.5 KB

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