timeriomem-rng.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. INIT_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 = kzalloc(sizeof(struct timeriomem_rng_private_data), GFP_KERNEL);
  99. if (!priv) {
  100. dev_err(&pdev->dev, "failed to allocate device structure.\n");
  101. return -ENOMEM;
  102. }
  103. platform_set_drvdata(pdev, priv);
  104. if (pdev->dev.of_node) {
  105. int i;
  106. if (!of_property_read_u32(pdev->dev.of_node,
  107. "period", &i))
  108. period = i;
  109. else {
  110. dev_err(&pdev->dev, "missing period\n");
  111. err = -EINVAL;
  112. goto out_free;
  113. }
  114. } else
  115. period = pdata->period;
  116. priv->period = usecs_to_jiffies(period);
  117. if (priv->period < 1) {
  118. dev_err(&pdev->dev, "period is less than one jiffy\n");
  119. err = -EINVAL;
  120. goto out_free;
  121. }
  122. priv->expires = jiffies;
  123. priv->present = 1;
  124. init_completion(&priv->completion);
  125. complete(&priv->completion);
  126. setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv);
  127. priv->timeriomem_rng_ops.name = dev_name(&pdev->dev);
  128. priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present;
  129. priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read;
  130. priv->timeriomem_rng_ops.priv = (unsigned long)priv;
  131. if (!request_mem_region(res->start, resource_size(res),
  132. dev_name(&pdev->dev))) {
  133. dev_err(&pdev->dev, "request_mem_region failed\n");
  134. err = -EBUSY;
  135. goto out_timer;
  136. }
  137. priv->io_base = ioremap(res->start, resource_size(res));
  138. if (priv->io_base == NULL) {
  139. dev_err(&pdev->dev, "ioremap failed\n");
  140. err = -EIO;
  141. goto out_release_io;
  142. }
  143. err = hwrng_register(&priv->timeriomem_rng_ops);
  144. if (err) {
  145. dev_err(&pdev->dev, "problem registering\n");
  146. goto out;
  147. }
  148. dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
  149. priv->io_base, period);
  150. return 0;
  151. out:
  152. iounmap(priv->io_base);
  153. out_release_io:
  154. release_mem_region(res->start, resource_size(res));
  155. out_timer:
  156. del_timer_sync(&priv->timer);
  157. out_free:
  158. kfree(priv);
  159. return err;
  160. }
  161. static int timeriomem_rng_remove(struct platform_device *pdev)
  162. {
  163. struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev);
  164. struct resource *res;
  165. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. hwrng_unregister(&priv->timeriomem_rng_ops);
  167. del_timer_sync(&priv->timer);
  168. iounmap(priv->io_base);
  169. release_mem_region(res->start, resource_size(res));
  170. kfree(priv);
  171. return 0;
  172. }
  173. static const struct of_device_id timeriomem_rng_match[] = {
  174. { .compatible = "timeriomem_rng" },
  175. {},
  176. };
  177. MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
  178. static struct platform_driver timeriomem_rng_driver = {
  179. .driver = {
  180. .name = "timeriomem_rng",
  181. .owner = THIS_MODULE,
  182. .of_match_table = timeriomem_rng_match,
  183. },
  184. .probe = timeriomem_rng_probe,
  185. .remove = timeriomem_rng_remove,
  186. };
  187. module_platform_driver(timeriomem_rng_driver);
  188. MODULE_LICENSE("GPL");
  189. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  190. MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");