blob: d0042f5d857d7232d47f1a19fe4bd7c4899d2bdf [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
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/pci.h>
30#include <linux/hw_random.h>
Patrick McHardy984e9762007-11-21 12:24:45 +080031#include <linux/delay.h>
Michael Buesch96d63c02006-06-26 00:25:00 -070032#include <asm/io.h>
33
Michael Buesch96d63c02006-06-26 00:25:00 -070034#define PFX KBUILD_MODNAME ": "
35
Michael Buesch96d63c02006-06-26 00:25:00 -070036/*
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 */
44static const struct pci_device_id pci_tbl[] = {
Joe Perches409a7362009-06-25 13:50:53 +080045 { PCI_VDEVICE(AMD, 0x7443), 0, },
46 { PCI_VDEVICE(AMD, 0x746b), 0, },
Michael Buesch96d63c02006-06-26 00:25:00 -070047 { 0, }, /* terminate list */
48};
49MODULE_DEVICE_TABLE(pci, pci_tbl);
50
51static struct pci_dev *amd_pdev;
52
Patrick McHardy984e9762007-11-21 12:24:45 +080053static int amd_rng_data_present(struct hwrng *rng, int wait)
Michael Buesch96d63c02006-06-26 00:25:00 -070054{
55 u32 pmbase = (u32)rng->priv;
Patrick McHardy984e9762007-11-21 12:24:45 +080056 int data, i;
Michael Buesch96d63c02006-06-26 00:25:00 -070057
Patrick McHardy984e9762007-11-21 12:24:45 +080058 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;
Michael Buesch96d63c02006-06-26 00:25:00 -070065}
66
67static 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
76static int amd_rng_init(struct hwrng *rng)
77{
78 u8 rnen;
79
80 pci_read_config_byte(amd_pdev, 0x40, &rnen);
Corentin LABBE1c335d42016-08-26 13:11:30 +020081 rnen |= BIT(7); /* RNG on */
Michael Buesch96d63c02006-06-26 00:25:00 -070082 pci_write_config_byte(amd_pdev, 0x40, rnen);
83
84 pci_read_config_byte(amd_pdev, 0x41, &rnen);
Corentin LABBE1c335d42016-08-26 13:11:30 +020085 rnen |= BIT(7); /* PMIO enable */
Michael Buesch96d63c02006-06-26 00:25:00 -070086 pci_write_config_byte(amd_pdev, 0x41, rnen);
87
88 return 0;
89}
90
91static void amd_rng_cleanup(struct hwrng *rng)
92{
93 u8 rnen;
94
95 pci_read_config_byte(amd_pdev, 0x40, &rnen);
Corentin LABBE1c335d42016-08-26 13:11:30 +020096 rnen &= ~BIT(7); /* RNG off */
Michael Buesch96d63c02006-06-26 00:25:00 -070097 pci_write_config_byte(amd_pdev, 0x40, rnen);
98}
99
Michael Buesch96d63c02006-06-26 00:25:00 -0700100static 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
Michael Buesch96d63c02006-06-26 00:25:00 -0700108static 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
123found:
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;
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400131 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 }
Michael Buesch96d63c02006-06-26 00:25:00 -0700137 amd_rng.priv = (unsigned long)pmbase;
138 amd_pdev = pdev;
139
Sudip Mukherjee7a1ae9c2014-09-15 20:31:20 +0530140 pr_info("AMD768 RNG detected\n");
Michael Buesch96d63c02006-06-26 00:25:00 -0700141 err = hwrng_register(&amd_rng);
142 if (err) {
Sudip Mukherjee7a1ae9c2014-09-15 20:31:20 +0530143 pr_err(PFX "RNG registering failed (%d)\n",
Michael Buesch96d63c02006-06-26 00:25:00 -0700144 err);
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400145 release_region(pmbase + 0xF0, 8);
Michael Buesch96d63c02006-06-26 00:25:00 -0700146 goto out;
147 }
148out:
149 return err;
150}
151
152static void __exit mod_exit(void)
153{
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400154 u32 pmbase = (unsigned long)amd_rng.priv;
Corentin LABBE60207212016-08-26 13:11:29 +0200155
Dmitry Eremin-Solenikovbd68ccb2011-04-27 23:21:15 +0400156 release_region(pmbase + 0xF0, 8);
Michael Buesch96d63c02006-06-26 00:25:00 -0700157 hwrng_unregister(&amd_rng);
158}
159
Michael Buesch56fb5fe2007-01-10 23:15:43 -0800160module_init(mod_init);
Michael Buesch96d63c02006-06-26 00:25:00 -0700161module_exit(mod_exit);
162
163MODULE_AUTHOR("The Linux Kernel team");
164MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
165MODULE_LICENSE("GPL");