blob: 383e1974234dac82969d5d3e9903315298c2118c [file] [log] [blame]
Michael Buesch96d63c02006-06-26 00:25:00 -07001/*
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)
Alan Cox77122d02008-10-27 15:10:23 +000014 * (c) Copyright 2001 Red Hat Inc
Michael Buesch96d63c02006-06-26 00:25:00 -070015 *
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
Patrick McHardy984e9762007-11-21 12:24:45 +080027#include <linux/delay.h>
Corentin LABBE055ae892016-08-26 13:11:32 +020028#include <linux/hw_random.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/pci.h>
Michael Buesch96d63c02006-06-26 00:25:00 -070032
Corentin LABBEf8169bf2016-08-26 13:11:31 +020033#define DRV_NAME "AMD768-HWRNG"
Michael Buesch96d63c02006-06-26 00:25:00 -070034
Michael Buesch96d63c02006-06-26 00:25:00 -070035/*
36 * Data for PCI driver interface
37 *
38 * This data only exists for exporting the supported
39 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
40 * register a pci_driver, because someone else might one day
41 * want to register another driver on the same PCI id.
42 */
43static const struct pci_device_id pci_tbl[] = {
Joe Perches409a7362009-06-25 13:50:53 +080044 { PCI_VDEVICE(AMD, 0x7443), 0, },
45 { PCI_VDEVICE(AMD, 0x746b), 0, },
Michael Buesch96d63c02006-06-26 00:25:00 -070046 { 0, }, /* terminate list */
47};
48MODULE_DEVICE_TABLE(pci, pci_tbl);
49
50static struct pci_dev *amd_pdev;
51
Patrick McHardy984e9762007-11-21 12:24:45 +080052static int amd_rng_data_present(struct hwrng *rng, int wait)
Michael Buesch96d63c02006-06-26 00:25:00 -070053{
54 u32 pmbase = (u32)rng->priv;
Patrick McHardy984e9762007-11-21 12:24:45 +080055 int data, i;
Michael Buesch96d63c02006-06-26 00:25:00 -070056
Patrick McHardy984e9762007-11-21 12:24:45 +080057 for (i = 0; i < 20; i++) {
58 data = !!(inl(pmbase + 0xF4) & 1);
59 if (data || !wait)
60 break;
61 udelay(10);
62 }
63 return data;
Michael Buesch96d63c02006-06-26 00:25:00 -070064}
65
66static int amd_rng_data_read(struct hwrng *rng, u32 *data)
67{
68 u32 pmbase = (u32)rng->priv;
69
70 *data = inl(pmbase + 0xF0);
71
72 return 4;
73}
74
75static int amd_rng_init(struct hwrng *rng)
76{
77 u8 rnen;
78
79 pci_read_config_byte(amd_pdev, 0x40, &rnen);
Corentin LABBE1c335d42016-08-26 13:11:30 +020080 rnen |= BIT(7); /* RNG on */
Michael Buesch96d63c02006-06-26 00:25:00 -070081 pci_write_config_byte(amd_pdev, 0x40, rnen);
82
83 pci_read_config_byte(amd_pdev, 0x41, &rnen);
Corentin LABBE1c335d42016-08-26 13:11:30 +020084 rnen |= BIT(7); /* PMIO enable */
Michael Buesch96d63c02006-06-26 00:25:00 -070085 pci_write_config_byte(amd_pdev, 0x41, rnen);
86
87 return 0;
88}
89
90static void amd_rng_cleanup(struct hwrng *rng)
91{
92 u8 rnen;
93
94 pci_read_config_byte(amd_pdev, 0x40, &rnen);
Corentin LABBE1c335d42016-08-26 13:11:30 +020095 rnen &= ~BIT(7); /* RNG off */
Michael Buesch96d63c02006-06-26 00:25:00 -070096 pci_write_config_byte(amd_pdev, 0x40, rnen);
97}
98
Michael Buesch96d63c02006-06-26 00:25:00 -070099static struct hwrng amd_rng = {
100 .name = "amd",
101 .init = amd_rng_init,
102 .cleanup = amd_rng_cleanup,
103 .data_present = amd_rng_data_present,
104 .data_read = amd_rng_data_read,
105};
106
Michael Buesch96d63c02006-06-26 00:25:00 -0700107static int __init mod_init(void)
108{
109 int err = -ENODEV;
110 struct pci_dev *pdev = NULL;
111 const struct pci_device_id *ent;
112 u32 pmbase;
113
114 for_each_pci_dev(pdev) {
115 ent = pci_match_id(pci_tbl, pdev);
116 if (ent)
117 goto found;
118 }
119 /* Device not found. */
120 goto out;
121
122found:
123 err = pci_read_config_dword(pdev, 0x58, &pmbase);
124 if (err)
125 goto out;
126 err = -EIO;
127 pmbase &= 0x0000FF00;
128 if (pmbase == 0)
129 goto out;
Corentin LABBEf8169bf2016-08-26 13:11:31 +0200130 if (!request_region(pmbase + 0xF0, 8, DRV_NAME)) {
131 dev_err(&pdev->dev, DRV_NAME " region 0x%x already in use!\n",
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400132 pmbase + 0xF0);
133 err = -EBUSY;
134 goto out;
135 }
Michael Buesch96d63c02006-06-26 00:25:00 -0700136 amd_rng.priv = (unsigned long)pmbase;
137 amd_pdev = pdev;
138
Corentin LABBEf8169bf2016-08-26 13:11:31 +0200139 pr_info(DRV_NAME " detected\n");
Michael Buesch96d63c02006-06-26 00:25:00 -0700140 err = hwrng_register(&amd_rng);
141 if (err) {
Corentin LABBEf8169bf2016-08-26 13:11:31 +0200142 pr_err(DRV_NAME " registering failed (%d)\n", err);
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400143 release_region(pmbase + 0xF0, 8);
Michael Buesch96d63c02006-06-26 00:25:00 -0700144 goto out;
145 }
146out:
147 return err;
148}
149
150static void __exit mod_exit(void)
151{
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400152 u32 pmbase = (unsigned long)amd_rng.priv;
Corentin LABBE60207212016-08-26 13:11:29 +0200153
Michael Buesch96d63c02006-06-26 00:25:00 -0700154 hwrng_unregister(&amd_rng);
Corentin LABBEfdec60d2016-08-26 13:11:33 +0200155
156 release_region(pmbase + 0xF0, 8);
Michael Buesch96d63c02006-06-26 00:25:00 -0700157}
158
Michael Buesch56fb5fe2007-01-10 23:15:43 -0800159module_init(mod_init);
Michael Buesch96d63c02006-06-26 00:25:00 -0700160module_exit(mod_exit);
161
162MODULE_AUTHOR("The Linux Kernel team");
163MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
164MODULE_LICENSE("GPL");