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>
13 * Hardware driver for the AMD 768 Random Number Generator (RNG)
14 * (c) Copyright 2001 Red Hat Inc
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/delay.h>
28 #include <linux/hw_random.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
33 #define DRV_NAME "AMD768-HWRNG"
37 #define PMBASE_OFFSET 0xF0
41 * Data for PCI driver interface
43 * This data only exists for exporting the supported
44 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
45 * register a pci_driver, because someone else might one day
46 * want to register another driver on the same PCI id.
48 static const struct pci_device_id pci_tbl
[] = {
49 { PCI_VDEVICE(AMD
, 0x7443), 0, },
50 { PCI_VDEVICE(AMD
, 0x746b), 0, },
51 { 0, }, /* terminate list */
53 MODULE_DEVICE_TABLE(pci
, pci_tbl
);
57 struct pci_dev
*pcidev
;
61 static int amd_rng_read(struct hwrng
*rng
, void *buf
, size_t max
, bool wait
)
64 struct amd768_priv
*priv
= (struct amd768_priv
*)rng
->priv
;
66 /* We will wait at maximum one time per read */
67 int timeout
= max
/ 4 + 1;
70 * RNG data is available when RNGDONE is set to 1
71 * New random numbers are generated approximately 128 microseconds
72 * after RNGDATA is read
75 if (ioread32(priv
->iobase
+ RNGDONE
) == 0) {
77 /* Delay given by datasheet */
78 usleep_range(128, 196);
85 *data
= ioread32(priv
->iobase
+ RNGDATA
);
94 static int amd_rng_init(struct hwrng
*rng
)
96 struct amd768_priv
*priv
= (struct amd768_priv
*)rng
->priv
;
99 pci_read_config_byte(priv
->pcidev
, 0x40, &rnen
);
100 rnen
|= BIT(7); /* RNG on */
101 pci_write_config_byte(priv
->pcidev
, 0x40, rnen
);
103 pci_read_config_byte(priv
->pcidev
, 0x41, &rnen
);
104 rnen
|= BIT(7); /* PMIO enable */
105 pci_write_config_byte(priv
->pcidev
, 0x41, rnen
);
110 static void amd_rng_cleanup(struct hwrng
*rng
)
112 struct amd768_priv
*priv
= (struct amd768_priv
*)rng
->priv
;
115 pci_read_config_byte(priv
->pcidev
, 0x40, &rnen
);
116 rnen
&= ~BIT(7); /* RNG off */
117 pci_write_config_byte(priv
->pcidev
, 0x40, rnen
);
120 static struct hwrng amd_rng
= {
122 .init
= amd_rng_init
,
123 .cleanup
= amd_rng_cleanup
,
124 .read
= amd_rng_read
,
127 static int __init
mod_init(void)
130 struct pci_dev
*pdev
= NULL
;
131 const struct pci_device_id
*ent
;
133 struct amd768_priv
*priv
;
135 for_each_pci_dev(pdev
) {
136 ent
= pci_match_id(pci_tbl
, pdev
);
140 /* Device not found. */
144 err
= pci_read_config_dword(pdev
, 0x58, &pmbase
);
148 pmbase
&= 0x0000FF00;
152 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
156 if (!request_region(pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
, DRV_NAME
)) {
157 dev_err(&pdev
->dev
, DRV_NAME
" region 0x%x already in use!\n",
163 priv
->iobase
= ioport_map(pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
);
165 pr_err(DRV_NAME
"Cannot map ioport\n");
170 amd_rng
.priv
= (unsigned long)priv
;
171 priv
->pmbase
= pmbase
;
174 pr_info(DRV_NAME
" detected\n");
175 err
= hwrng_register(&amd_rng
);
177 pr_err(DRV_NAME
" registering failed (%d)\n", err
);
183 ioport_unmap(priv
->iobase
);
185 release_region(pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
);
191 static void __exit
mod_exit(void)
193 struct amd768_priv
*priv
;
195 priv
= (struct amd768_priv
*)amd_rng
.priv
;
197 hwrng_unregister(&amd_rng
);
199 ioport_unmap(priv
->iobase
);
201 release_region(priv
->pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
);
206 module_init(mod_init
);
207 module_exit(mod_exit
);
209 MODULE_AUTHOR("The Linux Kernel team");
210 MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
211 MODULE_LICENSE("GPL");