2  * RNG driver for Intel RNGs
 
   4  * Copyright 2005 (c) MontaVista Software, Inc.
 
   6  * with the majority of the code coming from:
 
   8  * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
 
   9  * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
 
  13  * Hardware driver for the AMD 768 Random Number Generator (RNG)
 
  14  * (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
 
  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>
 
  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.
 
  27 #include <linux/module.h>
 
  28 #include <linux/kernel.h>
 
  29 #include <linux/pci.h>
 
  30 #include <linux/hw_random.h>
 
  34 #define PFX     KBUILD_MODNAME ": "
 
  39 #define INTEL_RNG_HW_STATUS                     0
 
  40 #define         INTEL_RNG_PRESENT               0x40
 
  41 #define         INTEL_RNG_ENABLED               0x01
 
  42 #define INTEL_RNG_STATUS                        1
 
  43 #define         INTEL_RNG_DATA_PRESENT          0x01
 
  44 #define INTEL_RNG_DATA                          2
 
  47  * Magic address at which Intel PCI bridges locate the RNG
 
  49 #define INTEL_RNG_ADDR                          0xFFBC015F
 
  50 #define INTEL_RNG_ADDR_LEN                      3
 
  53  * Data for PCI driver interface
 
  55  * This data only exists for exporting the supported
 
  56  * PCI ids via MODULE_DEVICE_TABLE.  We do not actually
 
  57  * register a pci_driver, because someone else might one day
 
  58  * want to register another driver on the same PCI id.
 
  60 static const struct pci_device_id pci_tbl[] = {
 
  61         { 0x8086, 0x2418, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
 
  62         { 0x8086, 0x2428, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
 
  63         { 0x8086, 0x2430, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
 
  64         { 0x8086, 0x2448, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
 
  65         { 0x8086, 0x244e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
 
  66         { 0x8086, 0x245e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
 
  67         { 0, }, /* terminate list */
 
  69 MODULE_DEVICE_TABLE(pci, pci_tbl);
 
  72 static inline u8 hwstatus_get(void __iomem *mem)
 
  74         return readb(mem + INTEL_RNG_HW_STATUS);
 
  77 static inline u8 hwstatus_set(void __iomem *mem,
 
  80         writeb(hw_status, mem + INTEL_RNG_HW_STATUS);
 
  81         return hwstatus_get(mem);
 
  84 static int intel_rng_data_present(struct hwrng *rng)
 
  86         void __iomem *mem = (void __iomem *)rng->priv;
 
  88         return !!(readb(mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT);
 
  91 static int intel_rng_data_read(struct hwrng *rng, u32 *data)
 
  93         void __iomem *mem = (void __iomem *)rng->priv;
 
  95         *data = readb(mem + INTEL_RNG_DATA);
 
 100 static int intel_rng_init(struct hwrng *rng)
 
 102         void __iomem *mem = (void __iomem *)rng->priv;
 
 106         hw_status = hwstatus_get(mem);
 
 107         /* turn RNG h/w on, if it's off */
 
 108         if ((hw_status & INTEL_RNG_ENABLED) == 0)
 
 109                 hw_status = hwstatus_set(mem, hw_status | INTEL_RNG_ENABLED);
 
 110         if ((hw_status & INTEL_RNG_ENABLED) == 0) {
 
 111                 printk(KERN_ERR PFX "cannot enable RNG, aborting\n");
 
 119 static void intel_rng_cleanup(struct hwrng *rng)
 
 121         void __iomem *mem = (void __iomem *)rng->priv;
 
 124         hw_status = hwstatus_get(mem);
 
 125         if (hw_status & INTEL_RNG_ENABLED)
 
 126                 hwstatus_set(mem, hw_status & ~INTEL_RNG_ENABLED);
 
 128                 printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
 
 132 static struct hwrng intel_rng = {
 
 134         .init           = intel_rng_init,
 
 135         .cleanup        = intel_rng_cleanup,
 
 136         .data_present   = intel_rng_data_present,
 
 137         .data_read      = intel_rng_data_read,
 
 141 static int __init mod_init(void)
 
 147         if (!pci_dev_present(pci_tbl))
 
 148                 goto out; /* Device not found. */
 
 151         mem = ioremap(INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
 
 154         intel_rng.priv = (unsigned long)mem;
 
 156         /* Check for Intel 82802 */
 
 158         hw_status = hwstatus_get(mem);
 
 159         if ((hw_status & INTEL_RNG_PRESENT) == 0)
 
 162         printk(KERN_INFO "Intel 82802 RNG detected\n");
 
 163         err = hwrng_register(&intel_rng);
 
 165                 printk(KERN_ERR PFX "RNG registering failed (%d)\n",
 
 177 static void __exit mod_exit(void)
 
 179         void __iomem *mem = (void __iomem *)intel_rng.priv;
 
 181         hwrng_unregister(&intel_rng);
 
 185 subsys_initcall(mod_init);
 
 186 module_exit(mod_exit);
 
 188 MODULE_DESCRIPTION("H/W RNG driver for Intel chipsets");
 
 189 MODULE_LICENSE("GPL");