amd-rng.c 4.2 KB
Newer Older
M
Michael Buesch 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * RNG driver for AMD RNGs
 *
 * Copyright 2005 (c) MontaVista Software, Inc.
 *
 * with the majority of the code coming from:
 *
 * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
 * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
 *
 * derived from
 *
 * Hardware driver for the AMD 768 Random Number Generator (RNG)
A
Alan Cox 已提交
14
 * (c) Copyright 2001 Red Hat Inc
M
Michael Buesch 已提交
15 16 17 18 19 20 21 22 23 24 25 26
 *
 * derived from
 *
 * Hardware driver for Intel i810 Random Number Generator (RNG)
 * Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
 * Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
 *
 * This file is licensed under  the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

C
Corentin LABBE 已提交
27 28
#include <linux/delay.h>
#include <linux/hw_random.h>
M
Michael Buesch 已提交
29
#include <linux/kernel.h>
C
Corentin LABBE 已提交
30
#include <linux/module.h>
M
Michael Buesch 已提交
31 32
#include <linux/pci.h>

33
#define DRV_NAME "AMD768-HWRNG"
M
Michael Buesch 已提交
34

35 36 37 38 39
#define RNGDATA		0x00
#define RNGDONE		0x04
#define PMBASE_OFFSET	0xF0
#define PMBASE_SIZE	8

M
Michael Buesch 已提交
40 41 42 43 44 45 46 47 48
/*
 * Data for PCI driver interface
 *
 * This data only exists for exporting the supported
 * PCI ids via MODULE_DEVICE_TABLE.  We do not actually
 * register a pci_driver, because someone else might one day
 * want to register another driver on the same PCI id.
 */
static const struct pci_device_id pci_tbl[] = {
J
Joe Perches 已提交
49 50
	{ PCI_VDEVICE(AMD, 0x7443), 0, },
	{ PCI_VDEVICE(AMD, 0x746b), 0, },
M
Michael Buesch 已提交
51 52 53 54
	{ 0, },	/* terminate list */
};
MODULE_DEVICE_TABLE(pci, pci_tbl);

55
struct amd768_priv {
56
	void __iomem *iobase;
57 58
	struct pci_dev *pcidev;
};
M
Michael Buesch 已提交
59

60
static int amd_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
M
Michael Buesch 已提交
61
{
62
	u32 *data = buf;
63
	struct amd768_priv *priv = (struct amd768_priv *)rng->priv;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	size_t read = 0;
	/* We will wait at maximum one time per read */
	int timeout = max / 4 + 1;

	/*
	 * RNG data is available when RNGDONE is set to 1
	 * New random numbers are generated approximately 128 microseconds
	 * after RNGDATA is read
	 */
	while (read < max) {
		if (ioread32(priv->iobase + RNGDONE) == 0) {
			if (wait) {
				/* Delay given by datasheet */
				usleep_range(128, 196);
				if (timeout-- == 0)
					return read;
			} else {
				return 0;
			}
		} else {
			*data = ioread32(priv->iobase + RNGDATA);
			data++;
			read += 4;
		}
88
	}
M
Michael Buesch 已提交
89

90
	return read;
M
Michael Buesch 已提交
91 92 93 94
}

static int amd_rng_init(struct hwrng *rng)
{
95
	struct amd768_priv *priv = (struct amd768_priv *)rng->priv;
M
Michael Buesch 已提交
96 97
	u8 rnen;

98
	pci_read_config_byte(priv->pcidev, 0x40, &rnen);
C
Corentin LABBE 已提交
99
	rnen |= BIT(7);	/* RNG on */
100
	pci_write_config_byte(priv->pcidev, 0x40, rnen);
M
Michael Buesch 已提交
101

102
	pci_read_config_byte(priv->pcidev, 0x41, &rnen);
C
Corentin LABBE 已提交
103
	rnen |= BIT(7);	/* PMIO enable */
104
	pci_write_config_byte(priv->pcidev, 0x41, rnen);
M
Michael Buesch 已提交
105 106 107 108 109 110

	return 0;
}

static void amd_rng_cleanup(struct hwrng *rng)
{
111
	struct amd768_priv *priv = (struct amd768_priv *)rng->priv;
M
Michael Buesch 已提交
112 113
	u8 rnen;

114
	pci_read_config_byte(priv->pcidev, 0x40, &rnen);
C
Corentin LABBE 已提交
115
	rnen &= ~BIT(7);	/* RNG off */
116
	pci_write_config_byte(priv->pcidev, 0x40, rnen);
M
Michael Buesch 已提交
117 118 119 120 121 122
}

static struct hwrng amd_rng = {
	.name		= "amd",
	.init		= amd_rng_init,
	.cleanup	= amd_rng_cleanup,
123
	.read		= amd_rng_read,
M
Michael Buesch 已提交
124 125 126 127 128 129 130 131
};

static int __init mod_init(void)
{
	int err = -ENODEV;
	struct pci_dev *pdev = NULL;
	const struct pci_device_id *ent;
	u32 pmbase;
132
	struct amd768_priv *priv;
M
Michael Buesch 已提交
133 134 135 136 137 138 139

	for_each_pci_dev(pdev) {
		ent = pci_match_id(pci_tbl, pdev);
		if (ent)
			goto found;
	}
	/* Device not found. */
140
	return -ENODEV;
M
Michael Buesch 已提交
141 142 143 144

found:
	err = pci_read_config_dword(pdev, 0x58, &pmbase);
	if (err)
145 146
		return err;

M
Michael Buesch 已提交
147 148
	pmbase &= 0x0000FF00;
	if (pmbase == 0)
149 150
		return -EIO;

151
	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
152 153
	if (!priv)
		return -ENOMEM;
154

155 156
	if (!devm_request_region(&pdev->dev, pmbase + PMBASE_OFFSET,
				PMBASE_SIZE, DRV_NAME)) {
157
		dev_err(&pdev->dev, DRV_NAME " region 0x%x already in use!\n",
158
			pmbase + 0xF0);
159
		return -EBUSY;
160
	}
161

162 163
	priv->iobase = devm_ioport_map(&pdev->dev, pmbase + PMBASE_OFFSET,
			PMBASE_SIZE);
164
	if (!priv->iobase) {
165
		pr_err(DRV_NAME "Cannot map ioport\n");
166
		return -ENOMEM;
167 168
	}

169 170
	amd_rng.priv = (unsigned long)priv;
	priv->pcidev = pdev;
M
Michael Buesch 已提交
171

172
	pr_info(DRV_NAME " detected\n");
173
	return devm_hwrng_register(&pdev->dev, &amd_rng);
M
Michael Buesch 已提交
174 175 176 177 178 179
}

static void __exit mod_exit(void)
{
}

180
module_init(mod_init);
M
Michael Buesch 已提交
181 182 183 184 185
module_exit(mod_exit);

MODULE_AUTHOR("The Linux Kernel team");
MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets");
MODULE_LICENSE("GPL");