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>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/pci.h>
34 #define DRV_NAME "AMD768-HWRNG"
38 #define PMBASE_OFFSET 0xF0
42 * Data for PCI driver interface
44 * This data only exists for exporting the supported
45 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
46 * register a pci_driver, because someone else might one day
47 * want to register another driver on the same PCI id.
49 static const struct pci_device_id pci_tbl
[] = {
50 { PCI_VDEVICE(AMD
, 0x7443), 0, },
51 { PCI_VDEVICE(AMD
, 0x746b), 0, },
52 { 0, }, /* terminate list */
54 MODULE_DEVICE_TABLE(pci
, pci_tbl
);
58 struct pci_dev
*pcidev
;
62 static int amd_rng_read(struct hwrng
*rng
, void *buf
, size_t max
, bool wait
)
65 struct amd768_priv
*priv
= (struct amd768_priv
*)rng
->priv
;
67 /* We will wait at maximum one time per read */
68 int timeout
= max
/ 4 + 1;
71 * RNG data is available when RNGDONE is set to 1
72 * New random numbers are generated approximately 128 microseconds
73 * after RNGDATA is read
76 if (ioread32(priv
->iobase
+ RNGDONE
) == 0) {
78 /* Delay given by datasheet */
79 usleep_range(128, 196);
86 *data
= ioread32(priv
->iobase
+ RNGDATA
);
95 static int amd_rng_init(struct hwrng
*rng
)
97 struct amd768_priv
*priv
= (struct amd768_priv
*)rng
->priv
;
100 pci_read_config_byte(priv
->pcidev
, 0x40, &rnen
);
101 rnen
|= BIT(7); /* RNG on */
102 pci_write_config_byte(priv
->pcidev
, 0x40, rnen
);
104 pci_read_config_byte(priv
->pcidev
, 0x41, &rnen
);
105 rnen
|= BIT(7); /* PMIO enable */
106 pci_write_config_byte(priv
->pcidev
, 0x41, rnen
);
111 static void amd_rng_cleanup(struct hwrng
*rng
)
113 struct amd768_priv
*priv
= (struct amd768_priv
*)rng
->priv
;
116 pci_read_config_byte(priv
->pcidev
, 0x40, &rnen
);
117 rnen
&= ~BIT(7); /* RNG off */
118 pci_write_config_byte(priv
->pcidev
, 0x40, rnen
);
121 static struct hwrng amd_rng
= {
123 .init
= amd_rng_init
,
124 .cleanup
= amd_rng_cleanup
,
125 .read
= amd_rng_read
,
128 static int __init
amd_rng_mod_init(void)
131 struct pci_dev
*pdev
= NULL
;
132 const struct pci_device_id
*ent
;
134 struct amd768_priv
*priv
;
136 for_each_pci_dev(pdev
) {
137 ent
= pci_match_id(pci_tbl
, pdev
);
141 /* Device not found. */
145 err
= pci_read_config_dword(pdev
, 0x58, &pmbase
);
147 err
= pcibios_err_to_errno(err
);
151 pmbase
&= 0x0000FF00;
157 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
163 if (!request_region(pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
, DRV_NAME
)) {
164 dev_err(&pdev
->dev
, DRV_NAME
" region 0x%x already in use!\n",
170 priv
->iobase
= ioport_map(pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
);
172 pr_err(DRV_NAME
"Cannot map ioport\n");
177 amd_rng
.priv
= (unsigned long)priv
;
178 priv
->pmbase
= pmbase
;
181 pr_info(DRV_NAME
" detected\n");
182 err
= hwrng_register(&amd_rng
);
184 pr_err(DRV_NAME
" registering failed (%d)\n", err
);
190 ioport_unmap(priv
->iobase
);
192 release_region(pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
);
200 static void __exit
amd_rng_mod_exit(void)
202 struct amd768_priv
*priv
;
204 priv
= (struct amd768_priv
*)amd_rng
.priv
;
206 hwrng_unregister(&amd_rng
);
208 ioport_unmap(priv
->iobase
);
210 release_region(priv
->pmbase
+ PMBASE_OFFSET
, PMBASE_SIZE
);
212 pci_dev_put(priv
->pcidev
);
217 module_init(amd_rng_mod_init
);
218 module_exit(amd_rng_mod_exit
);
220 MODULE_AUTHOR("The Linux Kernel team");
221 MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
222 MODULE_LICENSE("GPL");