162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * MTD map driver for BIOS Flash on Intel SCB2 boards
462306a36Sopenharmony_ci * Copyright (C) 2002 Sun Microsystems, Inc.
562306a36Sopenharmony_ci * Tim Hockin <thockin@sun.com>
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * A few notes on this MTD map:
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This was developed with a small number of SCB2 boards to test on.
1062306a36Sopenharmony_ci * Hopefully, Intel has not introducted too many unaccounted variables in the
1162306a36Sopenharmony_ci * making of this board.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The BIOS marks its own memory region as 'reserved' in the e820 map.  We
1462306a36Sopenharmony_ci * try to request it here, but if it fails, we carry on anyway.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * This is how the chip is attached, so said the schematic:
1762306a36Sopenharmony_ci * * a 4 MiB (32 Mib) 16 bit chip
1862306a36Sopenharmony_ci * * a 1 MiB memory region
1962306a36Sopenharmony_ci * * A20 and A21 pulled up
2062306a36Sopenharmony_ci * * D8-D15 ignored
2162306a36Sopenharmony_ci * What this means is that, while we are addressing bytes linearly, we are
2262306a36Sopenharmony_ci * really addressing words, and discarding the other byte.  This means that
2362306a36Sopenharmony_ci * the chip MUST BE at least 2 MiB.  This also means that every block is
2462306a36Sopenharmony_ci * actually half as big as the chip reports.  It also means that accesses of
2562306a36Sopenharmony_ci * logical address 0 hit higher-address sections of the chip, not physical 0.
2662306a36Sopenharmony_ci * One can only hope that these 4MiB x16 chips were a lot cheaper than 1MiB x8
2762306a36Sopenharmony_ci * chips.
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * This driver assumes the chip is not write-protected by an external signal.
3062306a36Sopenharmony_ci * As of the this writing, that is true, but may change, just to spite me.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * The actual BIOS layout has been mostly reverse engineered.  Intel BIOS
3362306a36Sopenharmony_ci * updates for this board include 10 related (*.bio - &.bi9) binary files and
3462306a36Sopenharmony_ci * another separate (*.bbo) binary file.  The 10 files are 64k of data + a
3562306a36Sopenharmony_ci * small header.  If the headers are stripped off, the 10 64k files can be
3662306a36Sopenharmony_ci * concatenated into a 640k image.  This is your BIOS image, proper.  The
3762306a36Sopenharmony_ci * separate .bbo file also has a small header.  It is the 'Boot Block'
3862306a36Sopenharmony_ci * recovery BIOS.  Once the header is stripped, no further prep is needed.
3962306a36Sopenharmony_ci * As best I can tell, the BIOS is arranged as such:
4062306a36Sopenharmony_ci * offset 0x00000 to 0x4ffff (320k):  unknown - SCSI BIOS, etc?
4162306a36Sopenharmony_ci * offset 0x50000 to 0xeffff (640k):  BIOS proper
4262306a36Sopenharmony_ci * offset 0xf0000 ty 0xfffff (64k):   Boot Block region
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci * Intel's BIOS update program flashes the BIOS and Boot Block in separate
4562306a36Sopenharmony_ci * steps.  Probably a wise thing to do.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#include <linux/module.h>
4962306a36Sopenharmony_ci#include <linux/types.h>
5062306a36Sopenharmony_ci#include <linux/kernel.h>
5162306a36Sopenharmony_ci#include <asm/io.h>
5262306a36Sopenharmony_ci#include <linux/mtd/mtd.h>
5362306a36Sopenharmony_ci#include <linux/mtd/map.h>
5462306a36Sopenharmony_ci#include <linux/mtd/cfi.h>
5562306a36Sopenharmony_ci#include <linux/pci.h>
5662306a36Sopenharmony_ci#include <linux/pci_ids.h>
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#define MODNAME		"scb2_flash"
5962306a36Sopenharmony_ci#define SCB2_ADDR	0xfff00000
6062306a36Sopenharmony_ci#define SCB2_WINDOW	0x00100000
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic void __iomem *scb2_ioaddr;
6462306a36Sopenharmony_cistatic struct mtd_info *scb2_mtd;
6562306a36Sopenharmony_cistatic struct map_info scb2_map = {
6662306a36Sopenharmony_ci	.name =      "SCB2 BIOS Flash",
6762306a36Sopenharmony_ci	.size =      0,
6862306a36Sopenharmony_ci	.bankwidth =  1,
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_cistatic int region_fail;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic int scb2_fixup_mtd(struct mtd_info *mtd)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	int i;
7562306a36Sopenharmony_ci	int done = 0;
7662306a36Sopenharmony_ci	struct map_info *map = mtd->priv;
7762306a36Sopenharmony_ci	struct cfi_private *cfi = map->fldrv_priv;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* barf if this doesn't look right */
8062306a36Sopenharmony_ci	if (cfi->cfiq->InterfaceDesc != CFI_INTERFACE_X16_ASYNC) {
8162306a36Sopenharmony_ci		printk(KERN_ERR MODNAME ": unsupported InterfaceDesc: %#x\n",
8262306a36Sopenharmony_ci		    cfi->cfiq->InterfaceDesc);
8362306a36Sopenharmony_ci		return -1;
8462306a36Sopenharmony_ci	}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	/* I wasn't here. I didn't see. dwmw2. */
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/* the chip is sometimes bigger than the map - what a waste */
8962306a36Sopenharmony_ci	mtd->size = map->size;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/*
9262306a36Sopenharmony_ci	 * We only REALLY get half the chip, due to the way it is
9362306a36Sopenharmony_ci	 * wired up - D8-D15 are tossed away.  We read linear bytes,
9462306a36Sopenharmony_ci	 * but in reality we are getting 1/2 of each 16-bit read,
9562306a36Sopenharmony_ci	 * which LOOKS linear to us.  Because CFI code accounts for
9662306a36Sopenharmony_ci	 * things like lock/unlock/erase by eraseregions, we need to
9762306a36Sopenharmony_ci	 * fudge them to reflect this.  Erases go like this:
9862306a36Sopenharmony_ci	 *   * send an erase to an address
9962306a36Sopenharmony_ci	 *   * the chip samples the address and erases the block
10062306a36Sopenharmony_ci	 *   * add the block erasesize to the address and repeat
10162306a36Sopenharmony_ci	 *   -- the problem is that addresses are 16-bit addressable
10262306a36Sopenharmony_ci	 *   -- we end up erasing every-other block
10362306a36Sopenharmony_ci	 */
10462306a36Sopenharmony_ci	mtd->erasesize /= 2;
10562306a36Sopenharmony_ci	for (i = 0; i < mtd->numeraseregions; i++) {
10662306a36Sopenharmony_ci		struct mtd_erase_region_info *region = &mtd->eraseregions[i];
10762306a36Sopenharmony_ci		region->erasesize /= 2;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	/*
11162306a36Sopenharmony_ci	 * If the chip is bigger than the map, it is wired with the high
11262306a36Sopenharmony_ci	 * address lines pulled up.  This makes us access the top portion of
11362306a36Sopenharmony_ci	 * the chip, so all our erase-region info is wrong.  Start cutting from
11462306a36Sopenharmony_ci	 * the bottom.
11562306a36Sopenharmony_ci	 */
11662306a36Sopenharmony_ci	for (i = 0; !done && i < mtd->numeraseregions; i++) {
11762306a36Sopenharmony_ci		struct mtd_erase_region_info *region = &mtd->eraseregions[i];
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci		if (region->numblocks * region->erasesize > mtd->size) {
12062306a36Sopenharmony_ci			region->numblocks = ((unsigned long)mtd->size /
12162306a36Sopenharmony_ci						region->erasesize);
12262306a36Sopenharmony_ci			done = 1;
12362306a36Sopenharmony_ci		} else {
12462306a36Sopenharmony_ci			region->numblocks = 0;
12562306a36Sopenharmony_ci		}
12662306a36Sopenharmony_ci		region->offset = 0;
12762306a36Sopenharmony_ci	}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	return 0;
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/* CSB5's 'Function Control Register' has bits for decoding @ >= 0xffc00000 */
13362306a36Sopenharmony_ci#define CSB5_FCR	0x41
13462306a36Sopenharmony_ci#define CSB5_FCR_DECODE_ALL 0x0e
13562306a36Sopenharmony_cistatic int scb2_flash_probe(struct pci_dev *dev,
13662306a36Sopenharmony_ci			    const struct pci_device_id *ent)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	u8 reg;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* enable decoding of the flash region in the south bridge */
14162306a36Sopenharmony_ci	pci_read_config_byte(dev, CSB5_FCR, &reg);
14262306a36Sopenharmony_ci	pci_write_config_byte(dev, CSB5_FCR, reg | CSB5_FCR_DECODE_ALL);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	if (!request_mem_region(SCB2_ADDR, SCB2_WINDOW, scb2_map.name)) {
14562306a36Sopenharmony_ci		/*
14662306a36Sopenharmony_ci		 * The BIOS seems to mark the flash region as 'reserved'
14762306a36Sopenharmony_ci		 * in the e820 map.  Warn and go about our business.
14862306a36Sopenharmony_ci		 */
14962306a36Sopenharmony_ci		printk(KERN_WARNING MODNAME
15062306a36Sopenharmony_ci		    ": warning - can't reserve rom window, continuing\n");
15162306a36Sopenharmony_ci		region_fail = 1;
15262306a36Sopenharmony_ci	}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	/* remap the IO window (w/o caching) */
15562306a36Sopenharmony_ci	scb2_ioaddr = ioremap(SCB2_ADDR, SCB2_WINDOW);
15662306a36Sopenharmony_ci	if (!scb2_ioaddr) {
15762306a36Sopenharmony_ci		printk(KERN_ERR MODNAME ": Failed to ioremap window!\n");
15862306a36Sopenharmony_ci		if (!region_fail)
15962306a36Sopenharmony_ci			release_mem_region(SCB2_ADDR, SCB2_WINDOW);
16062306a36Sopenharmony_ci		return -ENOMEM;
16162306a36Sopenharmony_ci	}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	scb2_map.phys = SCB2_ADDR;
16462306a36Sopenharmony_ci	scb2_map.virt = scb2_ioaddr;
16562306a36Sopenharmony_ci	scb2_map.size = SCB2_WINDOW;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	simple_map_init(&scb2_map);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/* try to find a chip */
17062306a36Sopenharmony_ci	scb2_mtd = do_map_probe("cfi_probe", &scb2_map);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	if (!scb2_mtd) {
17362306a36Sopenharmony_ci		printk(KERN_ERR MODNAME ": flash probe failed!\n");
17462306a36Sopenharmony_ci		iounmap(scb2_ioaddr);
17562306a36Sopenharmony_ci		if (!region_fail)
17662306a36Sopenharmony_ci			release_mem_region(SCB2_ADDR, SCB2_WINDOW);
17762306a36Sopenharmony_ci		return -ENODEV;
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	scb2_mtd->owner = THIS_MODULE;
18162306a36Sopenharmony_ci	if (scb2_fixup_mtd(scb2_mtd) < 0) {
18262306a36Sopenharmony_ci		mtd_device_unregister(scb2_mtd);
18362306a36Sopenharmony_ci		map_destroy(scb2_mtd);
18462306a36Sopenharmony_ci		iounmap(scb2_ioaddr);
18562306a36Sopenharmony_ci		if (!region_fail)
18662306a36Sopenharmony_ci			release_mem_region(SCB2_ADDR, SCB2_WINDOW);
18762306a36Sopenharmony_ci		return -ENODEV;
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	printk(KERN_NOTICE MODNAME ": chip size 0x%llx at offset 0x%llx\n",
19162306a36Sopenharmony_ci	       (unsigned long long)scb2_mtd->size,
19262306a36Sopenharmony_ci	       (unsigned long long)(SCB2_WINDOW - scb2_mtd->size));
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	mtd_device_register(scb2_mtd, NULL, 0);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	return 0;
19762306a36Sopenharmony_ci}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic void scb2_flash_remove(struct pci_dev *dev)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	if (!scb2_mtd)
20262306a36Sopenharmony_ci		return;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	/* disable flash writes */
20562306a36Sopenharmony_ci	mtd_lock(scb2_mtd, 0, scb2_mtd->size);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	mtd_device_unregister(scb2_mtd);
20862306a36Sopenharmony_ci	map_destroy(scb2_mtd);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	iounmap(scb2_ioaddr);
21162306a36Sopenharmony_ci	scb2_ioaddr = NULL;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	if (!region_fail)
21462306a36Sopenharmony_ci		release_mem_region(SCB2_ADDR, SCB2_WINDOW);
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic struct pci_device_id scb2_flash_pci_ids[] = {
21862306a36Sopenharmony_ci	{
21962306a36Sopenharmony_ci	  .vendor = PCI_VENDOR_ID_SERVERWORKS,
22062306a36Sopenharmony_ci	  .device = PCI_DEVICE_ID_SERVERWORKS_CSB5,
22162306a36Sopenharmony_ci	  .subvendor = PCI_ANY_ID,
22262306a36Sopenharmony_ci	  .subdevice = PCI_ANY_ID
22362306a36Sopenharmony_ci	},
22462306a36Sopenharmony_ci	{ 0, }
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic struct pci_driver scb2_flash_driver = {
22862306a36Sopenharmony_ci	.name =     "Intel SCB2 BIOS Flash",
22962306a36Sopenharmony_ci	.id_table = scb2_flash_pci_ids,
23062306a36Sopenharmony_ci	.probe =    scb2_flash_probe,
23162306a36Sopenharmony_ci	.remove =   scb2_flash_remove,
23262306a36Sopenharmony_ci};
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cimodule_pci_driver(scb2_flash_driver);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ciMODULE_LICENSE("GPL");
23762306a36Sopenharmony_ciMODULE_AUTHOR("Tim Hockin <thockin@sun.com>");
23862306a36Sopenharmony_ciMODULE_DESCRIPTION("MTD map driver for Intel SCB2 BIOS Flash");
23962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(pci, scb2_flash_pci_ids);
240