intel-rng.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * RNG driver for Intel RNGs
  3. *
  4. * Copyright 2005 (c) MontaVista Software, Inc.
  5. *
  6. * with the majority of the code coming from:
  7. *
  8. * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  9. * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  10. *
  11. * derived from
  12. *
  13. * Hardware driver for the AMD 768 Random Number Generator (RNG)
  14. * (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
  15. *
  16. * derived from
  17. *
  18. * Hardware driver for Intel i810 Random Number Generator (RNG)
  19. * Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  20. * Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  21. *
  22. * This file is licensed under the terms of the GNU General Public
  23. * License version 2. This program is licensed "as is" without any
  24. * warranty of any kind, whether express or implied.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/pci.h>
  29. #include <linux/hw_random.h>
  30. #include <asm/io.h>
  31. #define PFX KBUILD_MODNAME ": "
  32. /*
  33. * RNG registers
  34. */
  35. #define INTEL_RNG_HW_STATUS 0
  36. #define INTEL_RNG_PRESENT 0x40
  37. #define INTEL_RNG_ENABLED 0x01
  38. #define INTEL_RNG_STATUS 1
  39. #define INTEL_RNG_DATA_PRESENT 0x01
  40. #define INTEL_RNG_DATA 2
  41. /*
  42. * Magic address at which Intel PCI bridges locate the RNG
  43. */
  44. #define INTEL_RNG_ADDR 0xFFBC015F
  45. #define INTEL_RNG_ADDR_LEN 3
  46. /*
  47. * Data for PCI driver interface
  48. *
  49. * This data only exists for exporting the supported
  50. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  51. * register a pci_driver, because someone else might one day
  52. * want to register another driver on the same PCI id.
  53. */
  54. static const struct pci_device_id pci_tbl[] = {
  55. { 0x8086, 0x2418, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  56. { 0x8086, 0x2428, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  57. { 0x8086, 0x2430, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  58. { 0x8086, 0x2448, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  59. { 0x8086, 0x244e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  60. { 0x8086, 0x245e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  61. { 0, }, /* terminate list */
  62. };
  63. MODULE_DEVICE_TABLE(pci, pci_tbl);
  64. static inline u8 hwstatus_get(void __iomem *mem)
  65. {
  66. return readb(mem + INTEL_RNG_HW_STATUS);
  67. }
  68. static inline u8 hwstatus_set(void __iomem *mem,
  69. u8 hw_status)
  70. {
  71. writeb(hw_status, mem + INTEL_RNG_HW_STATUS);
  72. return hwstatus_get(mem);
  73. }
  74. static int intel_rng_data_present(struct hwrng *rng)
  75. {
  76. void __iomem *mem = (void __iomem *)rng->priv;
  77. return !!(readb(mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT);
  78. }
  79. static int intel_rng_data_read(struct hwrng *rng, u32 *data)
  80. {
  81. void __iomem *mem = (void __iomem *)rng->priv;
  82. *data = readb(mem + INTEL_RNG_DATA);
  83. return 1;
  84. }
  85. static int intel_rng_init(struct hwrng *rng)
  86. {
  87. void __iomem *mem = (void __iomem *)rng->priv;
  88. u8 hw_status;
  89. int err = -EIO;
  90. hw_status = hwstatus_get(mem);
  91. /* turn RNG h/w on, if it's off */
  92. if ((hw_status & INTEL_RNG_ENABLED) == 0)
  93. hw_status = hwstatus_set(mem, hw_status | INTEL_RNG_ENABLED);
  94. if ((hw_status & INTEL_RNG_ENABLED) == 0) {
  95. printk(KERN_ERR PFX "cannot enable RNG, aborting\n");
  96. goto out;
  97. }
  98. err = 0;
  99. out:
  100. return err;
  101. }
  102. static void intel_rng_cleanup(struct hwrng *rng)
  103. {
  104. void __iomem *mem = (void __iomem *)rng->priv;
  105. u8 hw_status;
  106. hw_status = hwstatus_get(mem);
  107. if (hw_status & INTEL_RNG_ENABLED)
  108. hwstatus_set(mem, hw_status & ~INTEL_RNG_ENABLED);
  109. else
  110. printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
  111. }
  112. static struct hwrng intel_rng = {
  113. .name = "intel",
  114. .init = intel_rng_init,
  115. .cleanup = intel_rng_cleanup,
  116. .data_present = intel_rng_data_present,
  117. .data_read = intel_rng_data_read,
  118. };
  119. static int __init mod_init(void)
  120. {
  121. int err = -ENODEV;
  122. void __iomem *mem;
  123. u8 hw_status;
  124. if (!pci_dev_present(pci_tbl))
  125. goto out; /* Device not found. */
  126. err = -ENOMEM;
  127. mem = ioremap(INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
  128. if (!mem)
  129. goto out;
  130. intel_rng.priv = (unsigned long)mem;
  131. /* Check for Intel 82802 */
  132. err = -ENODEV;
  133. hw_status = hwstatus_get(mem);
  134. if ((hw_status & INTEL_RNG_PRESENT) == 0)
  135. goto err_unmap;
  136. printk(KERN_INFO "Intel 82802 RNG detected\n");
  137. err = hwrng_register(&intel_rng);
  138. if (err) {
  139. printk(KERN_ERR PFX "RNG registering failed (%d)\n",
  140. err);
  141. goto err_unmap;
  142. }
  143. out:
  144. return err;
  145. err_unmap:
  146. iounmap(mem);
  147. goto out;
  148. }
  149. static void __exit mod_exit(void)
  150. {
  151. void __iomem *mem = (void __iomem *)intel_rng.priv;
  152. hwrng_unregister(&intel_rng);
  153. iounmap(mem);
  154. }
  155. subsys_initcall(mod_init);
  156. module_exit(mod_exit);
  157. MODULE_DESCRIPTION("H/W RNG driver for Intel chipsets");
  158. MODULE_LICENSE("GPL");