hwrng: amd - use the BIT macro
[cascardo/linux.git] / drivers / char / hw_random / amd-rng.c
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
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
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/pci.h>
30 #include <linux/hw_random.h>
31 #include <linux/delay.h>
32 #include <asm/io.h>
33
34 #define PFX     KBUILD_MODNAME ": "
35
36 /*
37  * Data for PCI driver interface
38  *
39  * This data only exists for exporting the supported
40  * PCI ids via MODULE_DEVICE_TABLE.  We do not actually
41  * register a pci_driver, because someone else might one day
42  * want to register another driver on the same PCI id.
43  */
44 static const struct pci_device_id pci_tbl[] = {
45         { PCI_VDEVICE(AMD, 0x7443), 0, },
46         { PCI_VDEVICE(AMD, 0x746b), 0, },
47         { 0, }, /* terminate list */
48 };
49 MODULE_DEVICE_TABLE(pci, pci_tbl);
50
51 static struct pci_dev *amd_pdev;
52
53 static int amd_rng_data_present(struct hwrng *rng, int wait)
54 {
55         u32 pmbase = (u32)rng->priv;
56         int data, i;
57
58         for (i = 0; i < 20; i++) {
59                 data = !!(inl(pmbase + 0xF4) & 1);
60                 if (data || !wait)
61                         break;
62                 udelay(10);
63         }
64         return data;
65 }
66
67 static int amd_rng_data_read(struct hwrng *rng, u32 *data)
68 {
69         u32 pmbase = (u32)rng->priv;
70
71         *data = inl(pmbase + 0xF0);
72
73         return 4;
74 }
75
76 static int amd_rng_init(struct hwrng *rng)
77 {
78         u8 rnen;
79
80         pci_read_config_byte(amd_pdev, 0x40, &rnen);
81         rnen |= BIT(7); /* RNG on */
82         pci_write_config_byte(amd_pdev, 0x40, rnen);
83
84         pci_read_config_byte(amd_pdev, 0x41, &rnen);
85         rnen |= BIT(7); /* PMIO enable */
86         pci_write_config_byte(amd_pdev, 0x41, rnen);
87
88         return 0;
89 }
90
91 static void amd_rng_cleanup(struct hwrng *rng)
92 {
93         u8 rnen;
94
95         pci_read_config_byte(amd_pdev, 0x40, &rnen);
96         rnen &= ~BIT(7);        /* RNG off */
97         pci_write_config_byte(amd_pdev, 0x40, rnen);
98 }
99
100 static struct hwrng amd_rng = {
101         .name           = "amd",
102         .init           = amd_rng_init,
103         .cleanup        = amd_rng_cleanup,
104         .data_present   = amd_rng_data_present,
105         .data_read      = amd_rng_data_read,
106 };
107
108 static int __init mod_init(void)
109 {
110         int err = -ENODEV;
111         struct pci_dev *pdev = NULL;
112         const struct pci_device_id *ent;
113         u32 pmbase;
114
115         for_each_pci_dev(pdev) {
116                 ent = pci_match_id(pci_tbl, pdev);
117                 if (ent)
118                         goto found;
119         }
120         /* Device not found. */
121         goto out;
122
123 found:
124         err = pci_read_config_dword(pdev, 0x58, &pmbase);
125         if (err)
126                 goto out;
127         err = -EIO;
128         pmbase &= 0x0000FF00;
129         if (pmbase == 0)
130                 goto out;
131         if (!request_region(pmbase + 0xF0, 8, "AMD HWRNG")) {
132                 dev_err(&pdev->dev, "AMD HWRNG region 0x%x already in use!\n",
133                         pmbase + 0xF0);
134                 err = -EBUSY;
135                 goto out;
136         }
137         amd_rng.priv = (unsigned long)pmbase;
138         amd_pdev = pdev;
139
140         pr_info("AMD768 RNG detected\n");
141         err = hwrng_register(&amd_rng);
142         if (err) {
143                 pr_err(PFX "RNG registering failed (%d)\n",
144                        err);
145                 release_region(pmbase + 0xF0, 8);
146                 goto out;
147         }
148 out:
149         return err;
150 }
151
152 static void __exit mod_exit(void)
153 {
154         u32 pmbase = (unsigned long)amd_rng.priv;
155
156         release_region(pmbase + 0xF0, 8);
157         hwrng_unregister(&amd_rng);
158 }
159
160 module_init(mod_init);
161 module_exit(mod_exit);
162
163 MODULE_AUTHOR("The Linux Kernel team");
164 MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
165 MODULE_LICENSE("GPL");