amd-rng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * RNG driver for AMD 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. * Data for PCI driver interface
  34. *
  35. * This data only exists for exporting the supported
  36. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  37. * register a pci_driver, because someone else might one day
  38. * want to register another driver on the same PCI id.
  39. */
  40. static const struct pci_device_id pci_tbl[] = {
  41. { 0x1022, 0x7443, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  42. { 0x1022, 0x746b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  43. { 0, }, /* terminate list */
  44. };
  45. MODULE_DEVICE_TABLE(pci, pci_tbl);
  46. static struct pci_dev *amd_pdev;
  47. static int amd_rng_data_present(struct hwrng *rng)
  48. {
  49. u32 pmbase = (u32)rng->priv;
  50. return !!(inl(pmbase + 0xF4) & 1);
  51. }
  52. static int amd_rng_data_read(struct hwrng *rng, u32 *data)
  53. {
  54. u32 pmbase = (u32)rng->priv;
  55. *data = inl(pmbase + 0xF0);
  56. return 4;
  57. }
  58. static int amd_rng_init(struct hwrng *rng)
  59. {
  60. u8 rnen;
  61. pci_read_config_byte(amd_pdev, 0x40, &rnen);
  62. rnen |= (1 << 7); /* RNG on */
  63. pci_write_config_byte(amd_pdev, 0x40, rnen);
  64. pci_read_config_byte(amd_pdev, 0x41, &rnen);
  65. rnen |= (1 << 7); /* PMIO enable */
  66. pci_write_config_byte(amd_pdev, 0x41, rnen);
  67. return 0;
  68. }
  69. static void amd_rng_cleanup(struct hwrng *rng)
  70. {
  71. u8 rnen;
  72. pci_read_config_byte(amd_pdev, 0x40, &rnen);
  73. rnen &= ~(1 << 7); /* RNG off */
  74. pci_write_config_byte(amd_pdev, 0x40, rnen);
  75. }
  76. static struct hwrng amd_rng = {
  77. .name = "amd",
  78. .init = amd_rng_init,
  79. .cleanup = amd_rng_cleanup,
  80. .data_present = amd_rng_data_present,
  81. .data_read = amd_rng_data_read,
  82. };
  83. static int __init mod_init(void)
  84. {
  85. int err = -ENODEV;
  86. struct pci_dev *pdev = NULL;
  87. const struct pci_device_id *ent;
  88. u32 pmbase;
  89. for_each_pci_dev(pdev) {
  90. ent = pci_match_id(pci_tbl, pdev);
  91. if (ent)
  92. goto found;
  93. }
  94. /* Device not found. */
  95. goto out;
  96. found:
  97. err = pci_read_config_dword(pdev, 0x58, &pmbase);
  98. if (err)
  99. goto out;
  100. err = -EIO;
  101. pmbase &= 0x0000FF00;
  102. if (pmbase == 0)
  103. goto out;
  104. amd_rng.priv = (unsigned long)pmbase;
  105. amd_pdev = pdev;
  106. printk(KERN_INFO "AMD768 RNG detected\n");
  107. err = hwrng_register(&amd_rng);
  108. if (err) {
  109. printk(KERN_ERR PFX "RNG registering failed (%d)\n",
  110. err);
  111. goto out;
  112. }
  113. out:
  114. return err;
  115. }
  116. static void __exit mod_exit(void)
  117. {
  118. hwrng_unregister(&amd_rng);
  119. }
  120. subsys_initcall(mod_init);
  121. module_exit(mod_exit);
  122. MODULE_AUTHOR("The Linux Kernel team");
  123. MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
  124. MODULE_LICENSE("GPL");