162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * esb2rom.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Normal mappings of flash chips in physical memory
662306a36Sopenharmony_ci * through the Intel ESB2 Southbridge.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * This was derived from ichxrom.c in May 2006 by
962306a36Sopenharmony_ci *	Lew Glendenning <lglendenning@lnxi.com>
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * Eric Biederman, of course, was a major help in this effort.
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/types.h>
1662306a36Sopenharmony_ci#include <linux/kernel.h>
1762306a36Sopenharmony_ci#include <linux/init.h>
1862306a36Sopenharmony_ci#include <linux/slab.h>
1962306a36Sopenharmony_ci#include <asm/io.h>
2062306a36Sopenharmony_ci#include <linux/mtd/mtd.h>
2162306a36Sopenharmony_ci#include <linux/mtd/map.h>
2262306a36Sopenharmony_ci#include <linux/mtd/cfi.h>
2362306a36Sopenharmony_ci#include <linux/mtd/flashchip.h>
2462306a36Sopenharmony_ci#include <linux/pci.h>
2562306a36Sopenharmony_ci#include <linux/pci_ids.h>
2662306a36Sopenharmony_ci#include <linux/list.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define MOD_NAME KBUILD_BASENAME
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define ADDRESS_NAME_LEN 18
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define ROM_PROBE_STEP_SIZE (64*1024) /* 64KiB */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define BIOS_CNTL		0xDC
3562306a36Sopenharmony_ci#define BIOS_LOCK_ENABLE	0x02
3662306a36Sopenharmony_ci#define BIOS_WRITE_ENABLE	0x01
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* This became a 16-bit register, and EN2 has disappeared */
3962306a36Sopenharmony_ci#define FWH_DEC_EN1	0xD8
4062306a36Sopenharmony_ci#define FWH_F8_EN	0x8000
4162306a36Sopenharmony_ci#define FWH_F0_EN	0x4000
4262306a36Sopenharmony_ci#define FWH_E8_EN	0x2000
4362306a36Sopenharmony_ci#define FWH_E0_EN	0x1000
4462306a36Sopenharmony_ci#define FWH_D8_EN	0x0800
4562306a36Sopenharmony_ci#define FWH_D0_EN	0x0400
4662306a36Sopenharmony_ci#define FWH_C8_EN	0x0200
4762306a36Sopenharmony_ci#define FWH_C0_EN	0x0100
4862306a36Sopenharmony_ci#define FWH_LEGACY_F_EN	0x0080
4962306a36Sopenharmony_ci#define FWH_LEGACY_E_EN	0x0040
5062306a36Sopenharmony_ci/* reserved  0x0020 and 0x0010 */
5162306a36Sopenharmony_ci#define FWH_70_EN	0x0008
5262306a36Sopenharmony_ci#define FWH_60_EN	0x0004
5362306a36Sopenharmony_ci#define FWH_50_EN	0x0002
5462306a36Sopenharmony_ci#define FWH_40_EN	0x0001
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* these are 32-bit values */
5762306a36Sopenharmony_ci#define FWH_SEL1	0xD0
5862306a36Sopenharmony_ci#define FWH_SEL2	0xD4
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#define FWH_8MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
6162306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
6262306a36Sopenharmony_ci			 FWH_70_EN | FWH_60_EN | FWH_50_EN | FWH_40_EN)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define FWH_7MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
6562306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
6662306a36Sopenharmony_ci			 FWH_70_EN | FWH_60_EN | FWH_50_EN)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define FWH_6MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
6962306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
7062306a36Sopenharmony_ci			 FWH_70_EN | FWH_60_EN)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define FWH_5MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
7362306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN | \
7462306a36Sopenharmony_ci			 FWH_70_EN)
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#define FWH_4MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
7762306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN | FWH_C8_EN | FWH_C0_EN)
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#define FWH_3_5MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
8062306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN | FWH_C8_EN)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define FWH_3MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
8362306a36Sopenharmony_ci			 FWH_D8_EN | FWH_D0_EN)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define FWH_2_5MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN | \
8662306a36Sopenharmony_ci			 FWH_D8_EN)
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define FWH_2MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN | FWH_E0_EN)
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci#define FWH_1_5MiB	(FWH_F8_EN | FWH_F0_EN | FWH_E8_EN)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define FWH_1MiB	(FWH_F8_EN | FWH_F0_EN)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci#define FWH_0_5MiB	(FWH_F8_EN)
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistruct esb2rom_window {
9862306a36Sopenharmony_ci	void __iomem* virt;
9962306a36Sopenharmony_ci	unsigned long phys;
10062306a36Sopenharmony_ci	unsigned long size;
10162306a36Sopenharmony_ci	struct list_head maps;
10262306a36Sopenharmony_ci	struct resource rsrc;
10362306a36Sopenharmony_ci	struct pci_dev *pdev;
10462306a36Sopenharmony_ci};
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistruct esb2rom_map_info {
10762306a36Sopenharmony_ci	struct list_head list;
10862306a36Sopenharmony_ci	struct map_info map;
10962306a36Sopenharmony_ci	struct mtd_info *mtd;
11062306a36Sopenharmony_ci	struct resource rsrc;
11162306a36Sopenharmony_ci	char map_name[sizeof(MOD_NAME) + 2 + ADDRESS_NAME_LEN];
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic struct esb2rom_window esb2rom_window = {
11562306a36Sopenharmony_ci	.maps = LIST_HEAD_INIT(esb2rom_window.maps),
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic void esb2rom_cleanup(struct esb2rom_window *window)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	struct esb2rom_map_info *map, *scratch;
12162306a36Sopenharmony_ci	u8 byte;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* Disable writes through the rom window */
12462306a36Sopenharmony_ci	pci_read_config_byte(window->pdev, BIOS_CNTL, &byte);
12562306a36Sopenharmony_ci	pci_write_config_byte(window->pdev, BIOS_CNTL,
12662306a36Sopenharmony_ci		byte & ~BIOS_WRITE_ENABLE);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	/* Free all of the mtd devices */
12962306a36Sopenharmony_ci	list_for_each_entry_safe(map, scratch, &window->maps, list) {
13062306a36Sopenharmony_ci		if (map->rsrc.parent)
13162306a36Sopenharmony_ci			release_resource(&map->rsrc);
13262306a36Sopenharmony_ci		mtd_device_unregister(map->mtd);
13362306a36Sopenharmony_ci		map_destroy(map->mtd);
13462306a36Sopenharmony_ci		list_del(&map->list);
13562306a36Sopenharmony_ci		kfree(map);
13662306a36Sopenharmony_ci	}
13762306a36Sopenharmony_ci	if (window->rsrc.parent)
13862306a36Sopenharmony_ci		release_resource(&window->rsrc);
13962306a36Sopenharmony_ci	if (window->virt) {
14062306a36Sopenharmony_ci		iounmap(window->virt);
14162306a36Sopenharmony_ci		window->virt = NULL;
14262306a36Sopenharmony_ci		window->phys = 0;
14362306a36Sopenharmony_ci		window->size = 0;
14462306a36Sopenharmony_ci	}
14562306a36Sopenharmony_ci	pci_dev_put(window->pdev);
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic int __init esb2rom_init_one(struct pci_dev *pdev,
14962306a36Sopenharmony_ci				   const struct pci_device_id *ent)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	static char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
15262306a36Sopenharmony_ci	struct esb2rom_window *window = &esb2rom_window;
15362306a36Sopenharmony_ci	struct esb2rom_map_info *map = NULL;
15462306a36Sopenharmony_ci	unsigned long map_top;
15562306a36Sopenharmony_ci	u8 byte;
15662306a36Sopenharmony_ci	u16 word;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	/* For now I just handle the ecb2 and I assume there
15962306a36Sopenharmony_ci	 * are not a lot of resources up at the top of the address
16062306a36Sopenharmony_ci	 * space.  It is possible to handle other devices in the
16162306a36Sopenharmony_ci	 * top 16MiB but it is very painful.  Also since
16262306a36Sopenharmony_ci	 * you can only really attach a FWH to an ICHX there
16362306a36Sopenharmony_ci	 * a number of simplifications you can make.
16462306a36Sopenharmony_ci	 *
16562306a36Sopenharmony_ci	 * Also you can page firmware hubs if an 8MiB window isn't enough
16662306a36Sopenharmony_ci	 * but don't currently handle that case either.
16762306a36Sopenharmony_ci	 */
16862306a36Sopenharmony_ci	window->pdev = pci_dev_get(pdev);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	/* RLG:  experiment 2.  Force the window registers to the widest values */
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/*
17362306a36Sopenharmony_ci	pci_read_config_word(pdev, FWH_DEC_EN1, &word);
17462306a36Sopenharmony_ci	printk(KERN_DEBUG "Original FWH_DEC_EN1 : %x\n", word);
17562306a36Sopenharmony_ci	pci_write_config_byte(pdev, FWH_DEC_EN1, 0xff);
17662306a36Sopenharmony_ci	pci_read_config_byte(pdev, FWH_DEC_EN1, &byte);
17762306a36Sopenharmony_ci	printk(KERN_DEBUG "New FWH_DEC_EN1 : %x\n", byte);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
18062306a36Sopenharmony_ci	printk(KERN_DEBUG "Original FWH_DEC_EN2 : %x\n", byte);
18162306a36Sopenharmony_ci	pci_write_config_byte(pdev, FWH_DEC_EN2, 0x0f);
18262306a36Sopenharmony_ci	pci_read_config_byte(pdev, FWH_DEC_EN2, &byte);
18362306a36Sopenharmony_ci	printk(KERN_DEBUG "New FWH_DEC_EN2 : %x\n", byte);
18462306a36Sopenharmony_ci*/
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	/* Find a region continuous to the end of the ROM window  */
18762306a36Sopenharmony_ci	window->phys = 0;
18862306a36Sopenharmony_ci	pci_read_config_word(pdev, FWH_DEC_EN1, &word);
18962306a36Sopenharmony_ci	printk(KERN_DEBUG "pci_read_config_word : %x\n", word);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	if ((word & FWH_8MiB) == FWH_8MiB)
19262306a36Sopenharmony_ci		window->phys = 0xff400000;
19362306a36Sopenharmony_ci	else if ((word & FWH_7MiB) == FWH_7MiB)
19462306a36Sopenharmony_ci		window->phys = 0xff500000;
19562306a36Sopenharmony_ci	else if ((word & FWH_6MiB) == FWH_6MiB)
19662306a36Sopenharmony_ci		window->phys = 0xff600000;
19762306a36Sopenharmony_ci	else if ((word & FWH_5MiB) == FWH_5MiB)
19862306a36Sopenharmony_ci		window->phys = 0xFF700000;
19962306a36Sopenharmony_ci	else if ((word & FWH_4MiB) == FWH_4MiB)
20062306a36Sopenharmony_ci		window->phys = 0xffc00000;
20162306a36Sopenharmony_ci	else if ((word & FWH_3_5MiB) == FWH_3_5MiB)
20262306a36Sopenharmony_ci		window->phys = 0xffc80000;
20362306a36Sopenharmony_ci	else if ((word & FWH_3MiB) == FWH_3MiB)
20462306a36Sopenharmony_ci		window->phys = 0xffd00000;
20562306a36Sopenharmony_ci	else if ((word & FWH_2_5MiB) == FWH_2_5MiB)
20662306a36Sopenharmony_ci		window->phys = 0xffd80000;
20762306a36Sopenharmony_ci	else if ((word & FWH_2MiB) == FWH_2MiB)
20862306a36Sopenharmony_ci		window->phys = 0xffe00000;
20962306a36Sopenharmony_ci	else if ((word & FWH_1_5MiB) == FWH_1_5MiB)
21062306a36Sopenharmony_ci		window->phys = 0xffe80000;
21162306a36Sopenharmony_ci	else if ((word & FWH_1MiB) == FWH_1MiB)
21262306a36Sopenharmony_ci		window->phys = 0xfff00000;
21362306a36Sopenharmony_ci	else if ((word & FWH_0_5MiB) == FWH_0_5MiB)
21462306a36Sopenharmony_ci		window->phys = 0xfff80000;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	if (window->phys == 0) {
21762306a36Sopenharmony_ci		printk(KERN_ERR MOD_NAME ": Rom window is closed\n");
21862306a36Sopenharmony_ci		goto out;
21962306a36Sopenharmony_ci	}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	/* reserved  0x0020 and 0x0010 */
22262306a36Sopenharmony_ci	window->phys -= 0x400000UL;
22362306a36Sopenharmony_ci	window->size = (0xffffffffUL - window->phys) + 1UL;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* Enable writes through the rom window */
22662306a36Sopenharmony_ci	pci_read_config_byte(pdev, BIOS_CNTL, &byte);
22762306a36Sopenharmony_ci	if (!(byte & BIOS_WRITE_ENABLE)  && (byte & (BIOS_LOCK_ENABLE))) {
22862306a36Sopenharmony_ci		/* The BIOS will generate an error if I enable
22962306a36Sopenharmony_ci		 * this device, so don't even try.
23062306a36Sopenharmony_ci		 */
23162306a36Sopenharmony_ci		printk(KERN_ERR MOD_NAME ": firmware access control, I can't enable writes\n");
23262306a36Sopenharmony_ci		goto out;
23362306a36Sopenharmony_ci	}
23462306a36Sopenharmony_ci	pci_write_config_byte(pdev, BIOS_CNTL, byte | BIOS_WRITE_ENABLE);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	/*
23762306a36Sopenharmony_ci	 * Try to reserve the window mem region.  If this fails then
23862306a36Sopenharmony_ci	 * it is likely due to the window being "reserved" by the BIOS.
23962306a36Sopenharmony_ci	 */
24062306a36Sopenharmony_ci	window->rsrc.name = MOD_NAME;
24162306a36Sopenharmony_ci	window->rsrc.start = window->phys;
24262306a36Sopenharmony_ci	window->rsrc.end   = window->phys + window->size - 1;
24362306a36Sopenharmony_ci	window->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
24462306a36Sopenharmony_ci	if (request_resource(&iomem_resource, &window->rsrc)) {
24562306a36Sopenharmony_ci		window->rsrc.parent = NULL;
24662306a36Sopenharmony_ci		printk(KERN_DEBUG MOD_NAME ": "
24762306a36Sopenharmony_ci		       "%s(): Unable to register resource %pR - kernel bug?\n",
24862306a36Sopenharmony_ci			__func__, &window->rsrc);
24962306a36Sopenharmony_ci	}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/* Map the firmware hub into my address space. */
25262306a36Sopenharmony_ci	window->virt = ioremap(window->phys, window->size);
25362306a36Sopenharmony_ci	if (!window->virt) {
25462306a36Sopenharmony_ci		printk(KERN_ERR MOD_NAME ": ioremap(%08lx, %08lx) failed\n",
25562306a36Sopenharmony_ci			window->phys, window->size);
25662306a36Sopenharmony_ci		goto out;
25762306a36Sopenharmony_ci	}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	/* Get the first address to look for an rom chip at */
26062306a36Sopenharmony_ci	map_top = window->phys;
26162306a36Sopenharmony_ci	if ((window->phys & 0x3fffff) != 0) {
26262306a36Sopenharmony_ci		/* if not aligned on 4MiB, look 4MiB lower in address space */
26362306a36Sopenharmony_ci		map_top = window->phys + 0x400000;
26462306a36Sopenharmony_ci	}
26562306a36Sopenharmony_ci#if 1
26662306a36Sopenharmony_ci	/* The probe sequence run over the firmware hub lock
26762306a36Sopenharmony_ci	 * registers sets them to 0x7 (no access).
26862306a36Sopenharmony_ci	 * (Insane hardware design, but most copied Intel's.)
26962306a36Sopenharmony_ci	 * ==> Probe at most the last 4M of the address space.
27062306a36Sopenharmony_ci	 */
27162306a36Sopenharmony_ci	if (map_top < 0xffc00000)
27262306a36Sopenharmony_ci		map_top = 0xffc00000;
27362306a36Sopenharmony_ci#endif
27462306a36Sopenharmony_ci	/* Loop through and look for rom chips */
27562306a36Sopenharmony_ci	while ((map_top - 1) < 0xffffffffUL) {
27662306a36Sopenharmony_ci		struct cfi_private *cfi;
27762306a36Sopenharmony_ci		unsigned long offset;
27862306a36Sopenharmony_ci		int i;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci		if (!map) {
28162306a36Sopenharmony_ci			map = kmalloc(sizeof(*map), GFP_KERNEL);
28262306a36Sopenharmony_ci			if (!map)
28362306a36Sopenharmony_ci				goto out;
28462306a36Sopenharmony_ci		}
28562306a36Sopenharmony_ci		memset(map, 0, sizeof(*map));
28662306a36Sopenharmony_ci		INIT_LIST_HEAD(&map->list);
28762306a36Sopenharmony_ci		map->map.name = map->map_name;
28862306a36Sopenharmony_ci		map->map.phys = map_top;
28962306a36Sopenharmony_ci		offset = map_top - window->phys;
29062306a36Sopenharmony_ci		map->map.virt = (void __iomem *)
29162306a36Sopenharmony_ci			(((unsigned long)(window->virt)) + offset);
29262306a36Sopenharmony_ci		map->map.size = 0xffffffffUL - map_top + 1UL;
29362306a36Sopenharmony_ci		/* Set the name of the map to the address I am trying */
29462306a36Sopenharmony_ci		sprintf(map->map_name, "%s @%08Lx",
29562306a36Sopenharmony_ci			MOD_NAME, (unsigned long long)map->map.phys);
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci		/* Firmware hubs only use vpp when being programmed
29862306a36Sopenharmony_ci		 * in a factory setting.  So in-place programming
29962306a36Sopenharmony_ci		 * needs to use a different method.
30062306a36Sopenharmony_ci		 */
30162306a36Sopenharmony_ci		for(map->map.bankwidth = 32; map->map.bankwidth;
30262306a36Sopenharmony_ci			map->map.bankwidth >>= 1) {
30362306a36Sopenharmony_ci			char **probe_type;
30462306a36Sopenharmony_ci			/* Skip bankwidths that are not supported */
30562306a36Sopenharmony_ci			if (!map_bankwidth_supported(map->map.bankwidth))
30662306a36Sopenharmony_ci				continue;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci			/* Setup the map methods */
30962306a36Sopenharmony_ci			simple_map_init(&map->map);
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci			/* Try all of the probe methods */
31262306a36Sopenharmony_ci			probe_type = rom_probe_types;
31362306a36Sopenharmony_ci			for(; *probe_type; probe_type++) {
31462306a36Sopenharmony_ci				map->mtd = do_map_probe(*probe_type, &map->map);
31562306a36Sopenharmony_ci				if (map->mtd)
31662306a36Sopenharmony_ci					goto found;
31762306a36Sopenharmony_ci			}
31862306a36Sopenharmony_ci		}
31962306a36Sopenharmony_ci		map_top += ROM_PROBE_STEP_SIZE;
32062306a36Sopenharmony_ci		continue;
32162306a36Sopenharmony_ci	found:
32262306a36Sopenharmony_ci		/* Trim the size if we are larger than the map */
32362306a36Sopenharmony_ci		if (map->mtd->size > map->map.size) {
32462306a36Sopenharmony_ci			printk(KERN_WARNING MOD_NAME
32562306a36Sopenharmony_ci				" rom(%llu) larger than window(%lu). fixing...\n",
32662306a36Sopenharmony_ci				(unsigned long long)map->mtd->size, map->map.size);
32762306a36Sopenharmony_ci			map->mtd->size = map->map.size;
32862306a36Sopenharmony_ci		}
32962306a36Sopenharmony_ci		if (window->rsrc.parent) {
33062306a36Sopenharmony_ci			/*
33162306a36Sopenharmony_ci			 * Registering the MTD device in iomem may not be possible
33262306a36Sopenharmony_ci			 * if there is a BIOS "reserved" and BUSY range.  If this
33362306a36Sopenharmony_ci			 * fails then continue anyway.
33462306a36Sopenharmony_ci			 */
33562306a36Sopenharmony_ci			map->rsrc.name  = map->map_name;
33662306a36Sopenharmony_ci			map->rsrc.start = map->map.phys;
33762306a36Sopenharmony_ci			map->rsrc.end   = map->map.phys + map->mtd->size - 1;
33862306a36Sopenharmony_ci			map->rsrc.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
33962306a36Sopenharmony_ci			if (request_resource(&window->rsrc, &map->rsrc)) {
34062306a36Sopenharmony_ci				printk(KERN_ERR MOD_NAME
34162306a36Sopenharmony_ci					": cannot reserve MTD resource\n");
34262306a36Sopenharmony_ci				map->rsrc.parent = NULL;
34362306a36Sopenharmony_ci			}
34462306a36Sopenharmony_ci		}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci		/* Make the whole region visible in the map */
34762306a36Sopenharmony_ci		map->map.virt = window->virt;
34862306a36Sopenharmony_ci		map->map.phys = window->phys;
34962306a36Sopenharmony_ci		cfi = map->map.fldrv_priv;
35062306a36Sopenharmony_ci		for(i = 0; i < cfi->numchips; i++)
35162306a36Sopenharmony_ci			cfi->chips[i].start += offset;
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci		/* Now that the mtd devices is complete claim and export it */
35462306a36Sopenharmony_ci		map->mtd->owner = THIS_MODULE;
35562306a36Sopenharmony_ci		if (mtd_device_register(map->mtd, NULL, 0)) {
35662306a36Sopenharmony_ci			map_destroy(map->mtd);
35762306a36Sopenharmony_ci			map->mtd = NULL;
35862306a36Sopenharmony_ci			goto out;
35962306a36Sopenharmony_ci		}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci		/* Calculate the new value of map_top */
36262306a36Sopenharmony_ci		map_top += map->mtd->size;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci		/* File away the map structure */
36562306a36Sopenharmony_ci		list_add(&map->list, &window->maps);
36662306a36Sopenharmony_ci		map = NULL;
36762306a36Sopenharmony_ci	}
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci out:
37062306a36Sopenharmony_ci	/* Free any left over map structures */
37162306a36Sopenharmony_ci	kfree(map);
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	/* See if I have any map structures */
37462306a36Sopenharmony_ci	if (list_empty(&window->maps)) {
37562306a36Sopenharmony_ci		esb2rom_cleanup(window);
37662306a36Sopenharmony_ci		return -ENODEV;
37762306a36Sopenharmony_ci	}
37862306a36Sopenharmony_ci	return 0;
37962306a36Sopenharmony_ci}
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_cistatic void esb2rom_remove_one(struct pci_dev *pdev)
38262306a36Sopenharmony_ci{
38362306a36Sopenharmony_ci	struct esb2rom_window *window = &esb2rom_window;
38462306a36Sopenharmony_ci	esb2rom_cleanup(window);
38562306a36Sopenharmony_ci}
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_cistatic const struct pci_device_id esb2rom_pci_tbl[] = {
38862306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0,
38962306a36Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, },
39062306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0,
39162306a36Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, },
39262306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,
39362306a36Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, },
39462306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0,
39562306a36Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, },
39662306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_1,
39762306a36Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, },
39862306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB2_0,
39962306a36Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, },
40062306a36Sopenharmony_ci	{ 0, },
40162306a36Sopenharmony_ci};
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci#if 0
40462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(pci, esb2rom_pci_tbl);
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_cistatic struct pci_driver esb2rom_driver = {
40762306a36Sopenharmony_ci	.name =		MOD_NAME,
40862306a36Sopenharmony_ci	.id_table =	esb2rom_pci_tbl,
40962306a36Sopenharmony_ci	.probe =	esb2rom_init_one,
41062306a36Sopenharmony_ci	.remove =	esb2rom_remove_one,
41162306a36Sopenharmony_ci};
41262306a36Sopenharmony_ci#endif
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_cistatic int __init init_esb2rom(void)
41562306a36Sopenharmony_ci{
41662306a36Sopenharmony_ci	struct pci_dev *pdev;
41762306a36Sopenharmony_ci	const struct pci_device_id *id;
41862306a36Sopenharmony_ci	int retVal;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	pdev = NULL;
42162306a36Sopenharmony_ci	for (id = esb2rom_pci_tbl; id->vendor; id++) {
42262306a36Sopenharmony_ci		printk(KERN_DEBUG "device id = %x\n", id->device);
42362306a36Sopenharmony_ci		pdev = pci_get_device(id->vendor, id->device, NULL);
42462306a36Sopenharmony_ci		if (pdev) {
42562306a36Sopenharmony_ci			printk(KERN_DEBUG "matched device = %x\n", id->device);
42662306a36Sopenharmony_ci			break;
42762306a36Sopenharmony_ci		}
42862306a36Sopenharmony_ci	}
42962306a36Sopenharmony_ci	if (pdev) {
43062306a36Sopenharmony_ci		printk(KERN_DEBUG "matched device id %x\n", id->device);
43162306a36Sopenharmony_ci		retVal = esb2rom_init_one(pdev, &esb2rom_pci_tbl[0]);
43262306a36Sopenharmony_ci		pci_dev_put(pdev);
43362306a36Sopenharmony_ci		printk(KERN_DEBUG "retVal = %d\n", retVal);
43462306a36Sopenharmony_ci		return retVal;
43562306a36Sopenharmony_ci	}
43662306a36Sopenharmony_ci	return -ENXIO;
43762306a36Sopenharmony_ci#if 0
43862306a36Sopenharmony_ci	return pci_register_driver(&esb2rom_driver);
43962306a36Sopenharmony_ci#endif
44062306a36Sopenharmony_ci}
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_cistatic void __exit cleanup_esb2rom(void)
44362306a36Sopenharmony_ci{
44462306a36Sopenharmony_ci	esb2rom_remove_one(esb2rom_window.pdev);
44562306a36Sopenharmony_ci}
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_cimodule_init(init_esb2rom);
44862306a36Sopenharmony_cimodule_exit(cleanup_esb2rom);
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
45162306a36Sopenharmony_ciMODULE_AUTHOR("Lew Glendenning <lglendenning@lnxi.com>");
45262306a36Sopenharmony_ciMODULE_DESCRIPTION("MTD map driver for BIOS chips on the ESB2 southbridge");
453