geode-rng.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * RNG driver for AMD Geode 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
  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 <linux/delay.h>
  31. #include <asm/io.h>
  32. #define PFX KBUILD_MODNAME ": "
  33. #define GEODE_RNG_DATA_REG 0x50
  34. #define GEODE_RNG_STATUS_REG 0x54
  35. /*
  36. * Data for PCI driver interface
  37. *
  38. * This data only exists for exporting the supported
  39. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  40. * register a pci_driver, because someone else might one day
  41. * want to register another driver on the same PCI id.
  42. */
  43. static const struct pci_device_id pci_tbl[] = {
  44. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), 0, },
  45. { 0, }, /* terminate list */
  46. };
  47. MODULE_DEVICE_TABLE(pci, pci_tbl);
  48. static int geode_rng_data_read(struct hwrng *rng, u32 *data)
  49. {
  50. void __iomem *mem = (void __iomem *)rng->priv;
  51. *data = readl(mem + GEODE_RNG_DATA_REG);
  52. return 4;
  53. }
  54. static int geode_rng_data_present(struct hwrng *rng, int wait)
  55. {
  56. void __iomem *mem = (void __iomem *)rng->priv;
  57. int data, i;
  58. for (i = 0; i < 20; i++) {
  59. data = !!(readl(mem + GEODE_RNG_STATUS_REG));
  60. if (data || !wait)
  61. break;
  62. udelay(10);
  63. }
  64. return data;
  65. }
  66. static struct hwrng geode_rng = {
  67. .name = "geode",
  68. .data_present = geode_rng_data_present,
  69. .data_read = geode_rng_data_read,
  70. };
  71. static int __init mod_init(void)
  72. {
  73. int err = -ENODEV;
  74. struct pci_dev *pdev = NULL;
  75. const struct pci_device_id *ent;
  76. void __iomem *mem;
  77. unsigned long rng_base;
  78. for_each_pci_dev(pdev) {
  79. ent = pci_match_id(pci_tbl, pdev);
  80. if (ent)
  81. goto found;
  82. }
  83. /* Device not found. */
  84. goto out;
  85. found:
  86. rng_base = pci_resource_start(pdev, 0);
  87. if (rng_base == 0)
  88. goto out;
  89. err = -ENOMEM;
  90. mem = ioremap(rng_base, 0x58);
  91. if (!mem)
  92. goto out;
  93. geode_rng.priv = (unsigned long)mem;
  94. pr_info("AMD Geode RNG detected\n");
  95. err = hwrng_register(&geode_rng);
  96. if (err) {
  97. pr_err(PFX "RNG registering failed (%d)\n",
  98. err);
  99. goto err_unmap;
  100. }
  101. out:
  102. return err;
  103. err_unmap:
  104. iounmap(mem);
  105. goto out;
  106. }
  107. static void __exit mod_exit(void)
  108. {
  109. void __iomem *mem = (void __iomem *)geode_rng.priv;
  110. hwrng_unregister(&geode_rng);
  111. iounmap(mem);
  112. }
  113. module_init(mod_init);
  114. module_exit(mod_exit);
  115. MODULE_DESCRIPTION("H/W RNG driver for AMD Geode LX CPUs");
  116. MODULE_LICENSE("GPL");