Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Cox | 77122d0 | 2008-10-27 15:10:23 +0000 | [diff] [blame] | 14 | * (c) Copyright 2001 Red Hat Inc |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 15 | * |
| 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 McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 27 | #include <linux/delay.h> |
Corentin LABBE | 055ae89 | 2016-08-26 13:11:32 +0200 | [diff] [blame] | 28 | #include <linux/hw_random.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/pci.h> |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 32 | |
Corentin LABBE | f8169bf | 2016-08-26 13:11:31 +0200 | [diff] [blame] | 33 | #define DRV_NAME "AMD768-HWRNG" |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 34 | |
Corentin LABBE | 3c343a3 | 2016-08-26 13:11:35 +0200 | [diff] [blame] | 35 | #define RNGDATA 0x00 |
| 36 | #define RNGDONE 0x04 |
| 37 | #define PMBASE_OFFSET 0xF0 |
| 38 | #define PMBASE_SIZE 8 |
| 39 | |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 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 | */ |
| 48 | static const struct pci_device_id pci_tbl[] = { |
Joe Perches | 409a736 | 2009-06-25 13:50:53 +0800 | [diff] [blame] | 49 | { PCI_VDEVICE(AMD, 0x7443), 0, }, |
| 50 | { PCI_VDEVICE(AMD, 0x746b), 0, }, |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 51 | { 0, }, /* terminate list */ |
| 52 | }; |
| 53 | MODULE_DEVICE_TABLE(pci, pci_tbl); |
| 54 | |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 55 | struct amd768_priv { |
Corentin LABBE | 3c343a3 | 2016-08-26 13:11:35 +0200 | [diff] [blame] | 56 | void __iomem *iobase; |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 57 | struct pci_dev *pcidev; |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 58 | u32 pmbase; |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 59 | }; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 60 | |
Corentin LABBE | 85962d2 | 2016-08-26 13:11:36 +0200 | [diff] [blame] | 61 | static int amd_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 62 | { |
Corentin LABBE | 85962d2 | 2016-08-26 13:11:36 +0200 | [diff] [blame] | 63 | u32 *data = buf; |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 64 | struct amd768_priv *priv = (struct amd768_priv *)rng->priv; |
Corentin LABBE | 85962d2 | 2016-08-26 13:11:36 +0200 | [diff] [blame] | 65 | size_t read = 0; |
| 66 | /* We will wait at maximum one time per read */ |
| 67 | int timeout = max / 4 + 1; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 68 | |
Corentin LABBE | 85962d2 | 2016-08-26 13:11:36 +0200 | [diff] [blame] | 69 | /* |
| 70 | * RNG data is available when RNGDONE is set to 1 |
| 71 | * New random numbers are generated approximately 128 microseconds |
| 72 | * after RNGDATA is read |
| 73 | */ |
| 74 | while (read < max) { |
| 75 | if (ioread32(priv->iobase + RNGDONE) == 0) { |
| 76 | if (wait) { |
| 77 | /* Delay given by datasheet */ |
| 78 | usleep_range(128, 196); |
| 79 | if (timeout-- == 0) |
| 80 | return read; |
| 81 | } else { |
| 82 | return 0; |
| 83 | } |
| 84 | } else { |
| 85 | *data = ioread32(priv->iobase + RNGDATA); |
| 86 | data++; |
| 87 | read += 4; |
| 88 | } |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 89 | } |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 90 | |
Corentin LABBE | 85962d2 | 2016-08-26 13:11:36 +0200 | [diff] [blame] | 91 | return read; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int amd_rng_init(struct hwrng *rng) |
| 95 | { |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 96 | struct amd768_priv *priv = (struct amd768_priv *)rng->priv; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 97 | u8 rnen; |
| 98 | |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 99 | pci_read_config_byte(priv->pcidev, 0x40, &rnen); |
Corentin LABBE | 1c335d4 | 2016-08-26 13:11:30 +0200 | [diff] [blame] | 100 | rnen |= BIT(7); /* RNG on */ |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 101 | pci_write_config_byte(priv->pcidev, 0x40, rnen); |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 102 | |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 103 | pci_read_config_byte(priv->pcidev, 0x41, &rnen); |
Corentin LABBE | 1c335d4 | 2016-08-26 13:11:30 +0200 | [diff] [blame] | 104 | rnen |= BIT(7); /* PMIO enable */ |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 105 | pci_write_config_byte(priv->pcidev, 0x41, rnen); |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void amd_rng_cleanup(struct hwrng *rng) |
| 111 | { |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 112 | struct amd768_priv *priv = (struct amd768_priv *)rng->priv; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 113 | u8 rnen; |
| 114 | |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 115 | pci_read_config_byte(priv->pcidev, 0x40, &rnen); |
Corentin LABBE | 1c335d4 | 2016-08-26 13:11:30 +0200 | [diff] [blame] | 116 | rnen &= ~BIT(7); /* RNG off */ |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 117 | pci_write_config_byte(priv->pcidev, 0x40, rnen); |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 120 | static struct hwrng amd_rng = { |
| 121 | .name = "amd", |
| 122 | .init = amd_rng_init, |
| 123 | .cleanup = amd_rng_cleanup, |
Corentin LABBE | 85962d2 | 2016-08-26 13:11:36 +0200 | [diff] [blame] | 124 | .read = amd_rng_read, |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 127 | static int __init mod_init(void) |
| 128 | { |
| 129 | int err = -ENODEV; |
| 130 | struct pci_dev *pdev = NULL; |
| 131 | const struct pci_device_id *ent; |
| 132 | u32 pmbase; |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 133 | struct amd768_priv *priv; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 134 | |
| 135 | for_each_pci_dev(pdev) { |
| 136 | ent = pci_match_id(pci_tbl, pdev); |
| 137 | if (ent) |
| 138 | goto found; |
| 139 | } |
| 140 | /* Device not found. */ |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 141 | return -ENODEV; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 142 | |
| 143 | found: |
| 144 | err = pci_read_config_dword(pdev, 0x58, &pmbase); |
| 145 | if (err) |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 146 | return err; |
| 147 | |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 148 | pmbase &= 0x0000FF00; |
| 149 | if (pmbase == 0) |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 150 | return -EIO; |
| 151 | |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 152 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Wei Yongjun | f7eca27 | 2016-09-16 01:49:41 +0000 | [diff] [blame] | 153 | if (!priv) |
| 154 | return -ENOMEM; |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 155 | |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 156 | if (!request_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE, DRV_NAME)) { |
Corentin LABBE | f8169bf | 2016-08-26 13:11:31 +0200 | [diff] [blame] | 157 | dev_err(&pdev->dev, DRV_NAME " region 0x%x already in use!\n", |
Dmitry Eremin-Solenikov | bd68ccb | 2011-04-27 23:21:15 +0400 | [diff] [blame] | 158 | pmbase + 0xF0); |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 159 | err = -EBUSY; |
| 160 | goto out; |
Dmitry Eremin-Solenikov | bd68ccb | 2011-04-27 23:21:15 +0400 | [diff] [blame] | 161 | } |
Corentin LABBE | 3c343a3 | 2016-08-26 13:11:35 +0200 | [diff] [blame] | 162 | |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 163 | priv->iobase = ioport_map(pmbase + PMBASE_OFFSET, PMBASE_SIZE); |
Wei Yongjun | f7eca27 | 2016-09-16 01:49:41 +0000 | [diff] [blame] | 164 | if (!priv->iobase) { |
Corentin LABBE | 3c343a3 | 2016-08-26 13:11:35 +0200 | [diff] [blame] | 165 | pr_err(DRV_NAME "Cannot map ioport\n"); |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 166 | err = -EINVAL; |
| 167 | goto err_iomap; |
Corentin LABBE | 3c343a3 | 2016-08-26 13:11:35 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 170 | amd_rng.priv = (unsigned long)priv; |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 171 | priv->pmbase = pmbase; |
Corentin LABBE | 7bad2cc | 2016-08-26 13:11:34 +0200 | [diff] [blame] | 172 | priv->pcidev = pdev; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 173 | |
Corentin LABBE | f8169bf | 2016-08-26 13:11:31 +0200 | [diff] [blame] | 174 | pr_info(DRV_NAME " detected\n"); |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 175 | err = hwrng_register(&amd_rng); |
| 176 | if (err) { |
| 177 | pr_err(DRV_NAME " registering failed (%d)\n", err); |
| 178 | goto err_hwrng; |
| 179 | } |
| 180 | return 0; |
| 181 | |
| 182 | err_hwrng: |
| 183 | ioport_unmap(priv->iobase); |
| 184 | err_iomap: |
| 185 | release_region(pmbase + PMBASE_OFFSET, PMBASE_SIZE); |
| 186 | out: |
| 187 | kfree(priv); |
| 188 | return err; |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void __exit mod_exit(void) |
| 192 | { |
Prarit Bhargava | 69db700 | 2017-03-14 07:36:01 -0400 | [diff] [blame] | 193 | struct amd768_priv *priv; |
| 194 | |
| 195 | priv = (struct amd768_priv *)amd_rng.priv; |
| 196 | |
| 197 | hwrng_unregister(&amd_rng); |
| 198 | |
| 199 | ioport_unmap(priv->iobase); |
| 200 | |
| 201 | release_region(priv->pmbase + PMBASE_OFFSET, PMBASE_SIZE); |
| 202 | |
| 203 | kfree(priv); |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Michael Buesch | 56fb5fe | 2007-01-10 23:15:43 -0800 | [diff] [blame] | 206 | module_init(mod_init); |
Michael Buesch | 96d63c0 | 2006-06-26 00:25:00 -0700 | [diff] [blame] | 207 | module_exit(mod_exit); |
| 208 | |
| 209 | MODULE_AUTHOR("The Linux Kernel team"); |
| 210 | MODULE_DESCRIPTION("H/W RNG driver for AMD chipsets"); |
| 211 | MODULE_LICENSE("GPL"); |