blob: e1d421a36a138d6a5e30b16bf62208846b5372ab [file] [log] [blame]
Michael Bueschef5d8622006-06-26 00:25:01 -07001/*
2 * RNG driver for AMD Geode 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 Bueschef5d8622006-06-26 00:25:01 -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>
PrasannaKumar Muralidharane9afc742016-09-11 20:54:26 +053028#include <linux/hw_random.h>
29#include <linux/io.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/pci.h>
Michael Bueschef5d8622006-06-26 00:25:01 -070033
Prarit Bhargavac445f992017-03-14 07:36:02 -040034
35#define PFX KBUILD_MODNAME ": "
36
Michael Bueschef5d8622006-06-26 00:25:01 -070037#define GEODE_RNG_DATA_REG 0x50
38#define GEODE_RNG_STATUS_REG 0x54
39
40/*
41 * Data for PCI driver interface
42 *
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.
47 */
48static const struct pci_device_id pci_tbl[] = {
Joe Perches409a7362009-06-25 13:50:53 +080049 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), 0, },
Michael Bueschef5d8622006-06-26 00:25:01 -070050 { 0, }, /* terminate list */
51};
52MODULE_DEVICE_TABLE(pci, pci_tbl);
53
54
55static int geode_rng_data_read(struct hwrng *rng, u32 *data)
56{
57 void __iomem *mem = (void __iomem *)rng->priv;
58
59 *data = readl(mem + GEODE_RNG_DATA_REG);
60
61 return 4;
62}
63
Patrick McHardy984e9762007-11-21 12:24:45 +080064static int geode_rng_data_present(struct hwrng *rng, int wait)
Michael Bueschef5d8622006-06-26 00:25:01 -070065{
66 void __iomem *mem = (void __iomem *)rng->priv;
Patrick McHardy984e9762007-11-21 12:24:45 +080067 int data, i;
Michael Bueschef5d8622006-06-26 00:25:01 -070068
Patrick McHardy984e9762007-11-21 12:24:45 +080069 for (i = 0; i < 20; i++) {
70 data = !!(readl(mem + GEODE_RNG_STATUS_REG));
71 if (data || !wait)
72 break;
73 udelay(10);
74 }
75 return data;
Michael Bueschef5d8622006-06-26 00:25:01 -070076}
77
78
79static struct hwrng geode_rng = {
80 .name = "geode",
81 .data_present = geode_rng_data_present,
82 .data_read = geode_rng_data_read,
83};
84
85
86static int __init mod_init(void)
87{
Prarit Bhargavac445f992017-03-14 07:36:02 -040088 int err = -ENODEV;
Michael Bueschef5d8622006-06-26 00:25:01 -070089 struct pci_dev *pdev = NULL;
90 const struct pci_device_id *ent;
91 void __iomem *mem;
92 unsigned long rng_base;
93
94 for_each_pci_dev(pdev) {
95 ent = pci_match_id(pci_tbl, pdev);
Prarit Bhargavac445f992017-03-14 07:36:02 -040096 if (ent)
97 goto found;
Michael Bueschef5d8622006-06-26 00:25:01 -070098 }
99 /* Device not found. */
Prarit Bhargavac445f992017-03-14 07:36:02 -0400100 goto out;
101
102found:
103 rng_base = pci_resource_start(pdev, 0);
104 if (rng_base == 0)
105 goto out;
106 err = -ENOMEM;
107 mem = ioremap(rng_base, 0x58);
108 if (!mem)
109 goto out;
110 geode_rng.priv = (unsigned long)mem;
111
112 pr_info("AMD Geode RNG detected\n");
113 err = hwrng_register(&geode_rng);
114 if (err) {
115 pr_err(PFX "RNG registering failed (%d)\n",
116 err);
117 goto err_unmap;
118 }
119out:
120 return err;
121
122err_unmap:
123 iounmap(mem);
124 goto out;
Michael Bueschef5d8622006-06-26 00:25:01 -0700125}
126
127static void __exit mod_exit(void)
128{
Prarit Bhargavac445f992017-03-14 07:36:02 -0400129 void __iomem *mem = (void __iomem *)geode_rng.priv;
130
131 hwrng_unregister(&geode_rng);
132 iounmap(mem);
Michael Bueschef5d8622006-06-26 00:25:01 -0700133}
134
Michael Buesch56fb5fe2007-01-10 23:15:43 -0800135module_init(mod_init);
Michael Bueschef5d8622006-06-26 00:25:01 -0700136module_exit(mod_exit);
137
138MODULE_DESCRIPTION("H/W RNG driver for AMD Geode LX CPUs");
139MODULE_LICENSE("GPL");