tx4939-rng.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * RNG driver for TX4939 Random Number Generators (RNG)
  3. *
  4. * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/hw_random.h>
  18. #include <linux/gfp.h>
  19. #define TX4939_RNG_RCSR 0x00000000
  20. #define TX4939_RNG_ROR(n) (0x00000018 + (n) * 8)
  21. #define TX4939_RNG_RCSR_INTE 0x00000008
  22. #define TX4939_RNG_RCSR_RST 0x00000004
  23. #define TX4939_RNG_RCSR_FIN 0x00000002
  24. #define TX4939_RNG_RCSR_ST 0x00000001
  25. struct tx4939_rng {
  26. struct hwrng rng;
  27. void __iomem *base;
  28. u64 databuf[3];
  29. unsigned int data_avail;
  30. };
  31. static void rng_io_start(void)
  32. {
  33. #ifndef CONFIG_64BIT
  34. /*
  35. * readq is reading a 64-bit register using a 64-bit load. On
  36. * a 32-bit kernel however interrupts or any other processor
  37. * exception would clobber the upper 32-bit of the processor
  38. * register so interrupts need to be disabled.
  39. */
  40. local_irq_disable();
  41. #endif
  42. }
  43. static void rng_io_end(void)
  44. {
  45. #ifndef CONFIG_64BIT
  46. local_irq_enable();
  47. #endif
  48. }
  49. static u64 read_rng(void __iomem *base, unsigned int offset)
  50. {
  51. return ____raw_readq(base + offset);
  52. }
  53. static void write_rng(u64 val, void __iomem *base, unsigned int offset)
  54. {
  55. return ____raw_writeq(val, base + offset);
  56. }
  57. static int tx4939_rng_data_present(struct hwrng *rng, int wait)
  58. {
  59. struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
  60. int i;
  61. if (rngdev->data_avail)
  62. return rngdev->data_avail;
  63. for (i = 0; i < 20; i++) {
  64. rng_io_start();
  65. if (!(read_rng(rngdev->base, TX4939_RNG_RCSR)
  66. & TX4939_RNG_RCSR_ST)) {
  67. rngdev->databuf[0] =
  68. read_rng(rngdev->base, TX4939_RNG_ROR(0));
  69. rngdev->databuf[1] =
  70. read_rng(rngdev->base, TX4939_RNG_ROR(1));
  71. rngdev->databuf[2] =
  72. read_rng(rngdev->base, TX4939_RNG_ROR(2));
  73. rngdev->data_avail =
  74. sizeof(rngdev->databuf) / sizeof(u32);
  75. /* Start RNG */
  76. write_rng(TX4939_RNG_RCSR_ST,
  77. rngdev->base, TX4939_RNG_RCSR);
  78. wait = 0;
  79. }
  80. rng_io_end();
  81. if (!wait)
  82. break;
  83. /* 90 bus clock cycles by default for generation */
  84. ndelay(90 * 5);
  85. }
  86. return rngdev->data_avail;
  87. }
  88. static int tx4939_rng_data_read(struct hwrng *rng, u32 *buffer)
  89. {
  90. struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
  91. rngdev->data_avail--;
  92. *buffer = *((u32 *)&rngdev->databuf + rngdev->data_avail);
  93. return sizeof(u32);
  94. }
  95. static int __init tx4939_rng_probe(struct platform_device *dev)
  96. {
  97. struct tx4939_rng *rngdev;
  98. struct resource *r;
  99. int i;
  100. rngdev = devm_kzalloc(&dev->dev, sizeof(*rngdev), GFP_KERNEL);
  101. if (!rngdev)
  102. return -ENOMEM;
  103. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  104. rngdev->base = devm_ioremap_resource(&dev->dev, r);
  105. if (IS_ERR(rngdev->base))
  106. return PTR_ERR(rngdev->base);
  107. rngdev->rng.name = dev_name(&dev->dev);
  108. rngdev->rng.data_present = tx4939_rng_data_present;
  109. rngdev->rng.data_read = tx4939_rng_data_read;
  110. rng_io_start();
  111. /* Reset RNG */
  112. write_rng(TX4939_RNG_RCSR_RST, rngdev->base, TX4939_RNG_RCSR);
  113. write_rng(0, rngdev->base, TX4939_RNG_RCSR);
  114. /* Start RNG */
  115. write_rng(TX4939_RNG_RCSR_ST, rngdev->base, TX4939_RNG_RCSR);
  116. rng_io_end();
  117. /*
  118. * Drop first two results. From the datasheet:
  119. * The quality of the random numbers generated immediately
  120. * after reset can be insufficient. Therefore, do not use
  121. * random numbers obtained from the first and second
  122. * generations; use the ones from the third or subsequent
  123. * generation.
  124. */
  125. for (i = 0; i < 2; i++) {
  126. rngdev->data_avail = 0;
  127. if (!tx4939_rng_data_present(&rngdev->rng, 1))
  128. return -EIO;
  129. }
  130. platform_set_drvdata(dev, rngdev);
  131. return hwrng_register(&rngdev->rng);
  132. }
  133. static int __exit tx4939_rng_remove(struct platform_device *dev)
  134. {
  135. struct tx4939_rng *rngdev = platform_get_drvdata(dev);
  136. hwrng_unregister(&rngdev->rng);
  137. return 0;
  138. }
  139. static struct platform_driver tx4939_rng_driver = {
  140. .driver = {
  141. .name = "tx4939-rng",
  142. },
  143. .remove = tx4939_rng_remove,
  144. };
  145. module_platform_driver_probe(tx4939_rng_driver, tx4939_rng_probe);
  146. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
  147. MODULE_LICENSE("GPL");