[PATCH] severing module.h->sched.h
[linux-2.6/kvm.git] / drivers / char / hw_random / amd-rng.c
blob71e4e0f3fd54ea404ac9658117d863de50eb131f
1 /*
2 * RNG driver for AMD 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>
11 * derived from
13 * Hardware driver for the AMD 768 Random Number Generator (RNG)
14 * (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
16 * derived from
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>
31 #include <asm/io.h>
34 #define PFX KBUILD_MODNAME ": "
38 * Data for PCI driver interface
40 * This data only exists for exporting the supported
41 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
42 * register a pci_driver, because someone else might one day
43 * want to register another driver on the same PCI id.
45 static const struct pci_device_id pci_tbl[] = {
46 { 0x1022, 0x7443, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
47 { 0x1022, 0x746b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
48 { 0, }, /* terminate list */
50 MODULE_DEVICE_TABLE(pci, pci_tbl);
52 static struct pci_dev *amd_pdev;
55 static int amd_rng_data_present(struct hwrng *rng)
57 u32 pmbase = (u32)rng->priv;
59 return !!(inl(pmbase + 0xF4) & 1);
62 static int amd_rng_data_read(struct hwrng *rng, u32 *data)
64 u32 pmbase = (u32)rng->priv;
66 *data = inl(pmbase + 0xF0);
68 return 4;
71 static int amd_rng_init(struct hwrng *rng)
73 u8 rnen;
75 pci_read_config_byte(amd_pdev, 0x40, &rnen);
76 rnen |= (1 << 7); /* RNG on */
77 pci_write_config_byte(amd_pdev, 0x40, rnen);
79 pci_read_config_byte(amd_pdev, 0x41, &rnen);
80 rnen |= (1 << 7); /* PMIO enable */
81 pci_write_config_byte(amd_pdev, 0x41, rnen);
83 return 0;
86 static void amd_rng_cleanup(struct hwrng *rng)
88 u8 rnen;
90 pci_read_config_byte(amd_pdev, 0x40, &rnen);
91 rnen &= ~(1 << 7); /* RNG off */
92 pci_write_config_byte(amd_pdev, 0x40, rnen);
96 static struct hwrng amd_rng = {
97 .name = "amd",
98 .init = amd_rng_init,
99 .cleanup = amd_rng_cleanup,
100 .data_present = amd_rng_data_present,
101 .data_read = amd_rng_data_read,
105 static int __init mod_init(void)
107 int err = -ENODEV;
108 struct pci_dev *pdev = NULL;
109 const struct pci_device_id *ent;
110 u32 pmbase;
112 for_each_pci_dev(pdev) {
113 ent = pci_match_id(pci_tbl, pdev);
114 if (ent)
115 goto found;
117 /* Device not found. */
118 goto out;
120 found:
121 err = pci_read_config_dword(pdev, 0x58, &pmbase);
122 if (err)
123 goto out;
124 err = -EIO;
125 pmbase &= 0x0000FF00;
126 if (pmbase == 0)
127 goto out;
128 amd_rng.priv = (unsigned long)pmbase;
129 amd_pdev = pdev;
131 printk(KERN_INFO "AMD768 RNG detected\n");
132 err = hwrng_register(&amd_rng);
133 if (err) {
134 printk(KERN_ERR PFX "RNG registering failed (%d)\n",
135 err);
136 goto out;
138 out:
139 return err;
142 static void __exit mod_exit(void)
144 hwrng_unregister(&amd_rng);
147 subsys_initcall(mod_init);
148 module_exit(mod_exit);
150 MODULE_AUTHOR("The Linux Kernel team");
151 MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
152 MODULE_LICENSE("GPL");