18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * RNG driver for Intel RNGs
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2005 (c) MontaVista Software, Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * with the majority of the code coming from:
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
98c2ecf20Sopenharmony_ci * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * derived from
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Hardware driver for the AMD 768 Random Number Generator (RNG)
148c2ecf20Sopenharmony_ci * (c) Copyright 2001 Red Hat Inc
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * derived from
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Hardware driver for Intel i810 Random Number Generator (RNG)
198c2ecf20Sopenharmony_ci * Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
208c2ecf20Sopenharmony_ci * Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * This file is licensed under  the terms of the GNU General Public
238c2ecf20Sopenharmony_ci * License version 2. This program is licensed "as is" without any
248c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/hw_random.h>
288c2ecf20Sopenharmony_ci#include <linux/kernel.h>
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci#include <linux/pci.h>
318c2ecf20Sopenharmony_ci#include <linux/stop_machine.h>
328c2ecf20Sopenharmony_ci#include <linux/delay.h>
338c2ecf20Sopenharmony_ci#include <linux/slab.h>
348c2ecf20Sopenharmony_ci#include <asm/io.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define PFX	KBUILD_MODNAME ": "
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*
408c2ecf20Sopenharmony_ci * RNG registers
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci#define INTEL_RNG_HW_STATUS			0
438c2ecf20Sopenharmony_ci#define         INTEL_RNG_PRESENT		0x40
448c2ecf20Sopenharmony_ci#define         INTEL_RNG_ENABLED		0x01
458c2ecf20Sopenharmony_ci#define INTEL_RNG_STATUS			1
468c2ecf20Sopenharmony_ci#define         INTEL_RNG_DATA_PRESENT		0x01
478c2ecf20Sopenharmony_ci#define INTEL_RNG_DATA				2
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Magic address at which Intel PCI bridges locate the RNG
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci#define INTEL_RNG_ADDR				0xFFBC015F
538c2ecf20Sopenharmony_ci#define INTEL_RNG_ADDR_LEN			3
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * LPC bridge PCI config space registers
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci#define FWH_DEC_EN1_REG_OLD			0xe3
598c2ecf20Sopenharmony_ci#define FWH_DEC_EN1_REG_NEW			0xd9 /* high byte of 16-bit register */
608c2ecf20Sopenharmony_ci#define FWH_F8_EN_MASK				0x80
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define BIOS_CNTL_REG_OLD			0x4e
638c2ecf20Sopenharmony_ci#define BIOS_CNTL_REG_NEW			0xdc
648c2ecf20Sopenharmony_ci#define BIOS_CNTL_WRITE_ENABLE_MASK		0x01
658c2ecf20Sopenharmony_ci#define BIOS_CNTL_LOCK_ENABLE_MASK		0x02
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Magic address at which Intel Firmware Hubs get accessed
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ci#define INTEL_FWH_ADDR				0xffff0000
718c2ecf20Sopenharmony_ci#define INTEL_FWH_ADDR_LEN			2
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Intel Firmware Hub command codes (write to any address inside the device)
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ci#define INTEL_FWH_RESET_CMD			0xff /* aka READ_ARRAY */
778c2ecf20Sopenharmony_ci#define INTEL_FWH_READ_ID_CMD			0x90
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Intel Firmware Hub Read ID command result addresses
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#define INTEL_FWH_MANUFACTURER_CODE_ADDRESS	0x000000
838c2ecf20Sopenharmony_ci#define INTEL_FWH_DEVICE_CODE_ADDRESS		0x000001
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Intel Firmware Hub Read ID command result values
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_ci#define INTEL_FWH_MANUFACTURER_CODE		0x89
898c2ecf20Sopenharmony_ci#define INTEL_FWH_DEVICE_CODE_8M		0xac
908c2ecf20Sopenharmony_ci#define INTEL_FWH_DEVICE_CODE_4M		0xad
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * Data for PCI driver interface
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci * This data only exists for exporting the supported
968c2ecf20Sopenharmony_ci * PCI ids via MODULE_DEVICE_TABLE.  We do not actually
978c2ecf20Sopenharmony_ci * register a pci_driver, because someone else might one day
988c2ecf20Sopenharmony_ci * want to register another driver on the same PCI id.
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_cistatic const struct pci_device_id pci_tbl[] = {
1018c2ecf20Sopenharmony_ci/* AA
1028c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2418) }, */
1038c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2410) }, /* AA */
1048c2ecf20Sopenharmony_ci/* AB
1058c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2428) }, */
1068c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2420) }, /* AB */
1078c2ecf20Sopenharmony_ci/* ??
1088c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2430) }, */
1098c2ecf20Sopenharmony_ci/* BAM, CAM, DBM, FBM, GxM
1108c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2448) }, */
1118c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x244c) }, /* BAM */
1128c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x248c) }, /* CAM */
1138c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x24cc) }, /* DBM */
1148c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2641) }, /* FBM */
1158c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x27b9) }, /* GxM */
1168c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x27bd) }, /* GxM DH */
1178c2ecf20Sopenharmony_ci/* BA, CA, DB, Ex, 6300, Fx, 631x/632x, Gx
1188c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x244e) }, */
1198c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2440) }, /* BA */
1208c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2480) }, /* CA */
1218c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x24c0) }, /* DB */
1228c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x24d0) }, /* Ex */
1238c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x25a1) }, /* 6300 */
1248c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2640) }, /* Fx */
1258c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2670) }, /* 631x/632x */
1268c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2671) }, /* 631x/632x */
1278c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2672) }, /* 631x/632x */
1288c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2673) }, /* 631x/632x */
1298c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2674) }, /* 631x/632x */
1308c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2675) }, /* 631x/632x */
1318c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2676) }, /* 631x/632x */
1328c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2677) }, /* 631x/632x */
1338c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2678) }, /* 631x/632x */
1348c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2679) }, /* 631x/632x */
1358c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x267a) }, /* 631x/632x */
1368c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x267b) }, /* 631x/632x */
1378c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x267c) }, /* 631x/632x */
1388c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x267d) }, /* 631x/632x */
1398c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x267e) }, /* 631x/632x */
1408c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x267f) }, /* 631x/632x */
1418c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x27b8) }, /* Gx */
1428c2ecf20Sopenharmony_ci/* E
1438c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x245e) }, */
1448c2ecf20Sopenharmony_ci	{ PCI_DEVICE(0x8086, 0x2450) }, /* E  */
1458c2ecf20Sopenharmony_ci	{ 0, },	/* terminate list */
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, pci_tbl);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic __initdata int no_fwh_detect;
1508c2ecf20Sopenharmony_cimodule_param(no_fwh_detect, int, 0);
1518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(no_fwh_detect, "Skip FWH detection:\n"
1528c2ecf20Sopenharmony_ci                                " positive value - skip if FWH space locked read-only\n"
1538c2ecf20Sopenharmony_ci                                " negative value - skip always");
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic inline u8 hwstatus_get(void __iomem *mem)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	return readb(mem + INTEL_RNG_HW_STATUS);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic inline u8 hwstatus_set(void __iomem *mem,
1618c2ecf20Sopenharmony_ci			      u8 hw_status)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	writeb(hw_status, mem + INTEL_RNG_HW_STATUS);
1648c2ecf20Sopenharmony_ci	return hwstatus_get(mem);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int intel_rng_data_present(struct hwrng *rng, int wait)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	void __iomem *mem = (void __iomem *)rng->priv;
1708c2ecf20Sopenharmony_ci	int data, i;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	for (i = 0; i < 20; i++) {
1738c2ecf20Sopenharmony_ci		data = !!(readb(mem + INTEL_RNG_STATUS) &
1748c2ecf20Sopenharmony_ci			  INTEL_RNG_DATA_PRESENT);
1758c2ecf20Sopenharmony_ci		if (data || !wait)
1768c2ecf20Sopenharmony_ci			break;
1778c2ecf20Sopenharmony_ci		udelay(10);
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci	return data;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic int intel_rng_data_read(struct hwrng *rng, u32 *data)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	void __iomem *mem = (void __iomem *)rng->priv;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	*data = readb(mem + INTEL_RNG_DATA);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return 1;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int intel_rng_init(struct hwrng *rng)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	void __iomem *mem = (void __iomem *)rng->priv;
1948c2ecf20Sopenharmony_ci	u8 hw_status;
1958c2ecf20Sopenharmony_ci	int err = -EIO;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	hw_status = hwstatus_get(mem);
1988c2ecf20Sopenharmony_ci	/* turn RNG h/w on, if it's off */
1998c2ecf20Sopenharmony_ci	if ((hw_status & INTEL_RNG_ENABLED) == 0)
2008c2ecf20Sopenharmony_ci		hw_status = hwstatus_set(mem, hw_status | INTEL_RNG_ENABLED);
2018c2ecf20Sopenharmony_ci	if ((hw_status & INTEL_RNG_ENABLED) == 0) {
2028c2ecf20Sopenharmony_ci		pr_err(PFX "cannot enable RNG, aborting\n");
2038c2ecf20Sopenharmony_ci		goto out;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci	err = 0;
2068c2ecf20Sopenharmony_ciout:
2078c2ecf20Sopenharmony_ci	return err;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic void intel_rng_cleanup(struct hwrng *rng)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	void __iomem *mem = (void __iomem *)rng->priv;
2138c2ecf20Sopenharmony_ci	u8 hw_status;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	hw_status = hwstatus_get(mem);
2168c2ecf20Sopenharmony_ci	if (hw_status & INTEL_RNG_ENABLED)
2178c2ecf20Sopenharmony_ci		hwstatus_set(mem, hw_status & ~INTEL_RNG_ENABLED);
2188c2ecf20Sopenharmony_ci	else
2198c2ecf20Sopenharmony_ci		pr_warn(PFX "unusual: RNG already disabled\n");
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic struct hwrng intel_rng = {
2248c2ecf20Sopenharmony_ci	.name		= "intel",
2258c2ecf20Sopenharmony_ci	.init		= intel_rng_init,
2268c2ecf20Sopenharmony_ci	.cleanup	= intel_rng_cleanup,
2278c2ecf20Sopenharmony_ci	.data_present	= intel_rng_data_present,
2288c2ecf20Sopenharmony_ci	.data_read	= intel_rng_data_read,
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistruct intel_rng_hw {
2328c2ecf20Sopenharmony_ci	struct pci_dev *dev;
2338c2ecf20Sopenharmony_ci	void __iomem *mem;
2348c2ecf20Sopenharmony_ci	u8 bios_cntl_off;
2358c2ecf20Sopenharmony_ci	u8 bios_cntl_val;
2368c2ecf20Sopenharmony_ci	u8 fwh_dec_en1_off;
2378c2ecf20Sopenharmony_ci	u8 fwh_dec_en1_val;
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic int __init intel_rng_hw_init(void *_intel_rng_hw)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct intel_rng_hw *intel_rng_hw = _intel_rng_hw;
2438c2ecf20Sopenharmony_ci	u8 mfc, dvc;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/* interrupts disabled in stop_machine call */
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (!(intel_rng_hw->fwh_dec_en1_val & FWH_F8_EN_MASK))
2488c2ecf20Sopenharmony_ci		pci_write_config_byte(intel_rng_hw->dev,
2498c2ecf20Sopenharmony_ci		                      intel_rng_hw->fwh_dec_en1_off,
2508c2ecf20Sopenharmony_ci		                      intel_rng_hw->fwh_dec_en1_val |
2518c2ecf20Sopenharmony_ci				      FWH_F8_EN_MASK);
2528c2ecf20Sopenharmony_ci	if (!(intel_rng_hw->bios_cntl_val & BIOS_CNTL_WRITE_ENABLE_MASK))
2538c2ecf20Sopenharmony_ci		pci_write_config_byte(intel_rng_hw->dev,
2548c2ecf20Sopenharmony_ci		                      intel_rng_hw->bios_cntl_off,
2558c2ecf20Sopenharmony_ci		                      intel_rng_hw->bios_cntl_val |
2568c2ecf20Sopenharmony_ci				      BIOS_CNTL_WRITE_ENABLE_MASK);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	writeb(INTEL_FWH_RESET_CMD, intel_rng_hw->mem);
2598c2ecf20Sopenharmony_ci	writeb(INTEL_FWH_READ_ID_CMD, intel_rng_hw->mem);
2608c2ecf20Sopenharmony_ci	mfc = readb(intel_rng_hw->mem + INTEL_FWH_MANUFACTURER_CODE_ADDRESS);
2618c2ecf20Sopenharmony_ci	dvc = readb(intel_rng_hw->mem + INTEL_FWH_DEVICE_CODE_ADDRESS);
2628c2ecf20Sopenharmony_ci	writeb(INTEL_FWH_RESET_CMD, intel_rng_hw->mem);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	if (!(intel_rng_hw->bios_cntl_val &
2658c2ecf20Sopenharmony_ci	      (BIOS_CNTL_LOCK_ENABLE_MASK|BIOS_CNTL_WRITE_ENABLE_MASK)))
2668c2ecf20Sopenharmony_ci		pci_write_config_byte(intel_rng_hw->dev,
2678c2ecf20Sopenharmony_ci				      intel_rng_hw->bios_cntl_off,
2688c2ecf20Sopenharmony_ci				      intel_rng_hw->bios_cntl_val);
2698c2ecf20Sopenharmony_ci	if (!(intel_rng_hw->fwh_dec_en1_val & FWH_F8_EN_MASK))
2708c2ecf20Sopenharmony_ci		pci_write_config_byte(intel_rng_hw->dev,
2718c2ecf20Sopenharmony_ci				      intel_rng_hw->fwh_dec_en1_off,
2728c2ecf20Sopenharmony_ci				      intel_rng_hw->fwh_dec_en1_val);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if (mfc != INTEL_FWH_MANUFACTURER_CODE ||
2758c2ecf20Sopenharmony_ci	    (dvc != INTEL_FWH_DEVICE_CODE_8M &&
2768c2ecf20Sopenharmony_ci	     dvc != INTEL_FWH_DEVICE_CODE_4M)) {
2778c2ecf20Sopenharmony_ci		pr_notice(PFX "FWH not detected\n");
2788c2ecf20Sopenharmony_ci		return -ENODEV;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return 0;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic int __init intel_init_hw_struct(struct intel_rng_hw *intel_rng_hw,
2858c2ecf20Sopenharmony_ci					struct pci_dev *dev)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	intel_rng_hw->bios_cntl_val = 0xff;
2888c2ecf20Sopenharmony_ci	intel_rng_hw->fwh_dec_en1_val = 0xff;
2898c2ecf20Sopenharmony_ci	intel_rng_hw->dev = dev;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Check for Intel 82802 */
2928c2ecf20Sopenharmony_ci	if (dev->device < 0x2640) {
2938c2ecf20Sopenharmony_ci		intel_rng_hw->fwh_dec_en1_off = FWH_DEC_EN1_REG_OLD;
2948c2ecf20Sopenharmony_ci		intel_rng_hw->bios_cntl_off = BIOS_CNTL_REG_OLD;
2958c2ecf20Sopenharmony_ci	} else {
2968c2ecf20Sopenharmony_ci		intel_rng_hw->fwh_dec_en1_off = FWH_DEC_EN1_REG_NEW;
2978c2ecf20Sopenharmony_ci		intel_rng_hw->bios_cntl_off = BIOS_CNTL_REG_NEW;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, intel_rng_hw->fwh_dec_en1_off,
3018c2ecf20Sopenharmony_ci			     &intel_rng_hw->fwh_dec_en1_val);
3028c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, intel_rng_hw->bios_cntl_off,
3038c2ecf20Sopenharmony_ci			     &intel_rng_hw->bios_cntl_val);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if ((intel_rng_hw->bios_cntl_val &
3068c2ecf20Sopenharmony_ci	     (BIOS_CNTL_LOCK_ENABLE_MASK|BIOS_CNTL_WRITE_ENABLE_MASK))
3078c2ecf20Sopenharmony_ci	    == BIOS_CNTL_LOCK_ENABLE_MASK) {
3088c2ecf20Sopenharmony_ci		static __initdata /*const*/ char warning[] =
3098c2ecf20Sopenharmony_ciPFX "Firmware space is locked read-only. If you can't or\n"
3108c2ecf20Sopenharmony_ciPFX "don't want to disable this in firmware setup, and if\n"
3118c2ecf20Sopenharmony_ciPFX "you are certain that your system has a functional\n"
3128c2ecf20Sopenharmony_ciPFX "RNG, try using the 'no_fwh_detect' option.\n";
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci		if (no_fwh_detect)
3158c2ecf20Sopenharmony_ci			return -ENODEV;
3168c2ecf20Sopenharmony_ci		pr_warn("%s", warning);
3178c2ecf20Sopenharmony_ci		return -EBUSY;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	intel_rng_hw->mem = ioremap(INTEL_FWH_ADDR, INTEL_FWH_ADDR_LEN);
3218c2ecf20Sopenharmony_ci	if (intel_rng_hw->mem == NULL)
3228c2ecf20Sopenharmony_ci		return -EBUSY;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	return 0;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int __init mod_init(void)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	int err = -ENODEV;
3318c2ecf20Sopenharmony_ci	int i;
3328c2ecf20Sopenharmony_ci	struct pci_dev *dev = NULL;
3338c2ecf20Sopenharmony_ci	void __iomem *mem;
3348c2ecf20Sopenharmony_ci	u8 hw_status;
3358c2ecf20Sopenharmony_ci	struct intel_rng_hw *intel_rng_hw;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	for (i = 0; !dev && pci_tbl[i].vendor; ++i)
3388c2ecf20Sopenharmony_ci		dev = pci_get_device(pci_tbl[i].vendor, pci_tbl[i].device,
3398c2ecf20Sopenharmony_ci				     NULL);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (!dev)
3428c2ecf20Sopenharmony_ci		goto out; /* Device not found. */
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (no_fwh_detect < 0) {
3458c2ecf20Sopenharmony_ci		pci_dev_put(dev);
3468c2ecf20Sopenharmony_ci		goto fwh_done;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	intel_rng_hw = kmalloc(sizeof(*intel_rng_hw), GFP_KERNEL);
3508c2ecf20Sopenharmony_ci	if (!intel_rng_hw) {
3518c2ecf20Sopenharmony_ci		pci_dev_put(dev);
3528c2ecf20Sopenharmony_ci		goto out;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	err = intel_init_hw_struct(intel_rng_hw, dev);
3568c2ecf20Sopenharmony_ci	if (err) {
3578c2ecf20Sopenharmony_ci		pci_dev_put(dev);
3588c2ecf20Sopenharmony_ci		kfree(intel_rng_hw);
3598c2ecf20Sopenharmony_ci		if (err == -ENODEV)
3608c2ecf20Sopenharmony_ci			goto fwh_done;
3618c2ecf20Sopenharmony_ci		goto out;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/*
3658c2ecf20Sopenharmony_ci	 * Since the BIOS code/data is going to disappear from its normal
3668c2ecf20Sopenharmony_ci	 * location with the Read ID command, all activity on the system
3678c2ecf20Sopenharmony_ci	 * must be stopped until the state is back to normal.
3688c2ecf20Sopenharmony_ci	 *
3698c2ecf20Sopenharmony_ci	 * Use stop_machine because IPIs can be blocked by disabling
3708c2ecf20Sopenharmony_ci	 * interrupts.
3718c2ecf20Sopenharmony_ci	 */
3728c2ecf20Sopenharmony_ci	err = stop_machine(intel_rng_hw_init, intel_rng_hw, NULL);
3738c2ecf20Sopenharmony_ci	pci_dev_put(dev);
3748c2ecf20Sopenharmony_ci	iounmap(intel_rng_hw->mem);
3758c2ecf20Sopenharmony_ci	kfree(intel_rng_hw);
3768c2ecf20Sopenharmony_ci	if (err)
3778c2ecf20Sopenharmony_ci		goto out;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cifwh_done:
3808c2ecf20Sopenharmony_ci	err = -ENOMEM;
3818c2ecf20Sopenharmony_ci	mem = ioremap(INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
3828c2ecf20Sopenharmony_ci	if (!mem)
3838c2ecf20Sopenharmony_ci		goto out;
3848c2ecf20Sopenharmony_ci	intel_rng.priv = (unsigned long)mem;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* Check for Random Number Generator */
3878c2ecf20Sopenharmony_ci	err = -ENODEV;
3888c2ecf20Sopenharmony_ci	hw_status = hwstatus_get(mem);
3898c2ecf20Sopenharmony_ci	if ((hw_status & INTEL_RNG_PRESENT) == 0) {
3908c2ecf20Sopenharmony_ci		iounmap(mem);
3918c2ecf20Sopenharmony_ci		goto out;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	pr_info("Intel 82802 RNG detected\n");
3958c2ecf20Sopenharmony_ci	err = hwrng_register(&intel_rng);
3968c2ecf20Sopenharmony_ci	if (err) {
3978c2ecf20Sopenharmony_ci		pr_err(PFX "RNG registering failed (%d)\n",
3988c2ecf20Sopenharmony_ci		       err);
3998c2ecf20Sopenharmony_ci		iounmap(mem);
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ciout:
4028c2ecf20Sopenharmony_ci	return err;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic void __exit mod_exit(void)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	void __iomem *mem = (void __iomem *)intel_rng.priv;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	hwrng_unregister(&intel_rng);
4118c2ecf20Sopenharmony_ci	iounmap(mem);
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cimodule_init(mod_init);
4158c2ecf20Sopenharmony_cimodule_exit(mod_exit);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("H/W RNG driver for Intel chipsets");
4188c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
419