timeriomem-rng.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/of.h>
  26. #include <linux/hw_random.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/timeriomem-rng.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/sched.h>
  32. #include <linux/timer.h>
  33. #include <linux/completion.h>
  34. struct timeriomem_rng_private_data {
  35. void __iomem *io_base;
  36. unsigned int expires;
  37. unsigned int period;
  38. unsigned int present:1;
  39. struct timer_list timer;
  40. struct completion completion;
  41. struct hwrng timeriomem_rng_ops;
  42. };
  43. #define to_rng_priv(rng) \
  44. ((struct timeriomem_rng_private_data *)rng->priv)
  45. /*
  46. * have data return 1, however return 0 if we have nothing
  47. */
  48. static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
  49. {
  50. struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
  51. if (!wait || priv->present)
  52. return priv->present;
  53. wait_for_completion(&priv->completion);
  54. return 1;
  55. }
  56. static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
  57. {
  58. struct timeriomem_rng_private_data *priv = to_rng_priv(rng);
  59. unsigned long cur;
  60. s32 delay;
  61. *data = readl(priv->io_base);
  62. cur = jiffies;
  63. delay = cur - priv->expires;
  64. delay = priv->period - (delay % priv->period);
  65. priv->expires = cur + delay;
  66. priv->present = 0;
  67. reinit_completion(&priv->completion);
  68. mod_timer(&priv->timer, priv->expires);
  69. return 4;
  70. }
  71. static void timeriomem_rng_trigger(unsigned long data)
  72. {
  73. struct timeriomem_rng_private_data *priv
  74. = (struct timeriomem_rng_private_data *)data;
  75. priv->present = 1;
  76. complete(&priv->completion);
  77. }
  78. static int timeriomem_rng_probe(struct platform_device *pdev)
  79. {
  80. struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
  81. struct timeriomem_rng_private_data *priv;
  82. struct resource *res;
  83. int err = 0;
  84. int period;
  85. if (!pdev->dev.of_node && !pdata) {
  86. dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
  87. return -EINVAL;
  88. }
  89. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  90. if (!res)
  91. return -ENXIO;
  92. if (res->start % 4 != 0 || resource_size(res) != 4) {
  93. dev_err(&pdev->dev,
  94. "address must be four bytes wide and aligned\n");
  95. return -EINVAL;
  96. }
  97. /* Allocate memory for the device structure (and zero it) */
  98. priv = devm_kzalloc(&pdev->dev,
  99. sizeof(struct timeriomem_rng_private_data), GFP_KERNEL);
  100. if (!priv)
  101. return -ENOMEM;
  102. platform_set_drvdata(pdev, priv);
  103. if (pdev->dev.of_node) {
  104. int i;
  105. if (!of_property_read_u32(pdev->dev.of_node,
  106. "period", &i))
  107. period = i;
  108. else {
  109. dev_err(&pdev->dev, "missing period\n");
  110. return -EINVAL;
  111. }
  112. } else {
  113. period = pdata->period;
  114. }
  115. priv->period = usecs_to_jiffies(period);
  116. if (priv->period < 1) {
  117. dev_err(&pdev->dev, "period is less than one jiffy\n");
  118. return -EINVAL;
  119. }
  120. priv->expires = jiffies;
  121. priv->present = 1;
  122. init_completion(&priv->completion);
  123. complete(&priv->completion);
  124. setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
  125. priv->timeriomem_rng_ops.name = dev_name(&pdev->dev);
  126. priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present;
  127. priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read;
  128. priv->timeriomem_rng_ops.priv = (unsigned long)priv;
  129. priv->io_base = devm_ioremap_resource(&pdev->dev, res);
  130. if (IS_ERR(priv->io_base)) {
  131. err = PTR_ERR(priv->io_base);
  132. goto out_timer;
  133. }
  134. err = hwrng_register(&priv->timeriomem_rng_ops);
  135. if (err) {
  136. dev_err(&pdev->dev, "problem registering\n");
  137. goto out_timer;
  138. }
  139. dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
  140. priv->io_base, period);
  141. return 0;
  142. out_timer:
  143. del_timer_sync(&priv->timer);
  144. return err;
  145. }
  146. static int timeriomem_rng_remove(struct platform_device *pdev)
  147. {
  148. struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev);
  149. hwrng_unregister(&priv->timeriomem_rng_ops);
  150. del_timer_sync(&priv->timer);
  151. return 0;
  152. }
  153. static const struct of_device_id timeriomem_rng_match[] = {
  154. { .compatible = "timeriomem_rng" },
  155. {},
  156. };
  157. MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
  158. static struct platform_driver timeriomem_rng_driver = {
  159. .driver = {
  160. .name = "timeriomem_rng",
  161. .of_match_table = timeriomem_rng_match,
  162. },
  163. .probe = timeriomem_rng_probe,
  164. .remove = timeriomem_rng_remove,
  165. };
  166. module_platform_driver(timeriomem_rng_driver);
  167. MODULE_LICENSE("GPL");
  168. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  169. MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");