xref: /OK3568_Linux_fs/kernel/drivers/mtd/maps/scb2_flash.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * MTD map driver for BIOS Flash on Intel SCB2 boards
4*4882a593Smuzhiyun  * Copyright (C) 2002 Sun Microsystems, Inc.
5*4882a593Smuzhiyun  * Tim Hockin <thockin@sun.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * A few notes on this MTD map:
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This was developed with a small number of SCB2 boards to test on.
10*4882a593Smuzhiyun  * Hopefully, Intel has not introducted too many unaccounted variables in the
11*4882a593Smuzhiyun  * making of this board.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * The BIOS marks its own memory region as 'reserved' in the e820 map.  We
14*4882a593Smuzhiyun  * try to request it here, but if it fails, we carry on anyway.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * This is how the chip is attached, so said the schematic:
17*4882a593Smuzhiyun  * * a 4 MiB (32 Mib) 16 bit chip
18*4882a593Smuzhiyun  * * a 1 MiB memory region
19*4882a593Smuzhiyun  * * A20 and A21 pulled up
20*4882a593Smuzhiyun  * * D8-D15 ignored
21*4882a593Smuzhiyun  * What this means is that, while we are addressing bytes linearly, we are
22*4882a593Smuzhiyun  * really addressing words, and discarding the other byte.  This means that
23*4882a593Smuzhiyun  * the chip MUST BE at least 2 MiB.  This also means that every block is
24*4882a593Smuzhiyun  * actually half as big as the chip reports.  It also means that accesses of
25*4882a593Smuzhiyun  * logical address 0 hit higher-address sections of the chip, not physical 0.
26*4882a593Smuzhiyun  * One can only hope that these 4MiB x16 chips were a lot cheaper than 1MiB x8
27*4882a593Smuzhiyun  * chips.
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * This driver assumes the chip is not write-protected by an external signal.
30*4882a593Smuzhiyun  * As of the this writing, that is true, but may change, just to spite me.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * The actual BIOS layout has been mostly reverse engineered.  Intel BIOS
33*4882a593Smuzhiyun  * updates for this board include 10 related (*.bio - &.bi9) binary files and
34*4882a593Smuzhiyun  * another separate (*.bbo) binary file.  The 10 files are 64k of data + a
35*4882a593Smuzhiyun  * small header.  If the headers are stripped off, the 10 64k files can be
36*4882a593Smuzhiyun  * concatenated into a 640k image.  This is your BIOS image, proper.  The
37*4882a593Smuzhiyun  * separate .bbo file also has a small header.  It is the 'Boot Block'
38*4882a593Smuzhiyun  * recovery BIOS.  Once the header is stripped, no further prep is needed.
39*4882a593Smuzhiyun  * As best I can tell, the BIOS is arranged as such:
40*4882a593Smuzhiyun  * offset 0x00000 to 0x4ffff (320k):  unknown - SCSI BIOS, etc?
41*4882a593Smuzhiyun  * offset 0x50000 to 0xeffff (640k):  BIOS proper
42*4882a593Smuzhiyun  * offset 0xf0000 ty 0xfffff (64k):   Boot Block region
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * Intel's BIOS update program flashes the BIOS and Boot Block in separate
45*4882a593Smuzhiyun  * steps.  Probably a wise thing to do.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <linux/module.h>
49*4882a593Smuzhiyun #include <linux/types.h>
50*4882a593Smuzhiyun #include <linux/kernel.h>
51*4882a593Smuzhiyun #include <asm/io.h>
52*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
53*4882a593Smuzhiyun #include <linux/mtd/map.h>
54*4882a593Smuzhiyun #include <linux/mtd/cfi.h>
55*4882a593Smuzhiyun #include <linux/pci.h>
56*4882a593Smuzhiyun #include <linux/pci_ids.h>
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define MODNAME		"scb2_flash"
59*4882a593Smuzhiyun #define SCB2_ADDR	0xfff00000
60*4882a593Smuzhiyun #define SCB2_WINDOW	0x00100000
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static void __iomem *scb2_ioaddr;
64*4882a593Smuzhiyun static struct mtd_info *scb2_mtd;
65*4882a593Smuzhiyun static struct map_info scb2_map = {
66*4882a593Smuzhiyun 	.name =      "SCB2 BIOS Flash",
67*4882a593Smuzhiyun 	.size =      0,
68*4882a593Smuzhiyun 	.bankwidth =  1,
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun static int region_fail;
71*4882a593Smuzhiyun 
scb2_fixup_mtd(struct mtd_info * mtd)72*4882a593Smuzhiyun static int scb2_fixup_mtd(struct mtd_info *mtd)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	int i;
75*4882a593Smuzhiyun 	int done = 0;
76*4882a593Smuzhiyun 	struct map_info *map = mtd->priv;
77*4882a593Smuzhiyun 	struct cfi_private *cfi = map->fldrv_priv;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* barf if this doesn't look right */
80*4882a593Smuzhiyun 	if (cfi->cfiq->InterfaceDesc != CFI_INTERFACE_X16_ASYNC) {
81*4882a593Smuzhiyun 		printk(KERN_ERR MODNAME ": unsupported InterfaceDesc: %#x\n",
82*4882a593Smuzhiyun 		    cfi->cfiq->InterfaceDesc);
83*4882a593Smuzhiyun 		return -1;
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/* I wasn't here. I didn't see. dwmw2. */
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/* the chip is sometimes bigger than the map - what a waste */
89*4882a593Smuzhiyun 	mtd->size = map->size;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/*
92*4882a593Smuzhiyun 	 * We only REALLY get half the chip, due to the way it is
93*4882a593Smuzhiyun 	 * wired up - D8-D15 are tossed away.  We read linear bytes,
94*4882a593Smuzhiyun 	 * but in reality we are getting 1/2 of each 16-bit read,
95*4882a593Smuzhiyun 	 * which LOOKS linear to us.  Because CFI code accounts for
96*4882a593Smuzhiyun 	 * things like lock/unlock/erase by eraseregions, we need to
97*4882a593Smuzhiyun 	 * fudge them to reflect this.  Erases go like this:
98*4882a593Smuzhiyun 	 *   * send an erase to an address
99*4882a593Smuzhiyun 	 *   * the chip samples the address and erases the block
100*4882a593Smuzhiyun 	 *   * add the block erasesize to the address and repeat
101*4882a593Smuzhiyun 	 *   -- the problem is that addresses are 16-bit addressable
102*4882a593Smuzhiyun 	 *   -- we end up erasing every-other block
103*4882a593Smuzhiyun 	 */
104*4882a593Smuzhiyun 	mtd->erasesize /= 2;
105*4882a593Smuzhiyun 	for (i = 0; i < mtd->numeraseregions; i++) {
106*4882a593Smuzhiyun 		struct mtd_erase_region_info *region = &mtd->eraseregions[i];
107*4882a593Smuzhiyun 		region->erasesize /= 2;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/*
111*4882a593Smuzhiyun 	 * If the chip is bigger than the map, it is wired with the high
112*4882a593Smuzhiyun 	 * address lines pulled up.  This makes us access the top portion of
113*4882a593Smuzhiyun 	 * the chip, so all our erase-region info is wrong.  Start cutting from
114*4882a593Smuzhiyun 	 * the bottom.
115*4882a593Smuzhiyun 	 */
116*4882a593Smuzhiyun 	for (i = 0; !done && i < mtd->numeraseregions; i++) {
117*4882a593Smuzhiyun 		struct mtd_erase_region_info *region = &mtd->eraseregions[i];
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 		if (region->numblocks * region->erasesize > mtd->size) {
120*4882a593Smuzhiyun 			region->numblocks = ((unsigned long)mtd->size /
121*4882a593Smuzhiyun 						region->erasesize);
122*4882a593Smuzhiyun 			done = 1;
123*4882a593Smuzhiyun 		} else {
124*4882a593Smuzhiyun 			region->numblocks = 0;
125*4882a593Smuzhiyun 		}
126*4882a593Smuzhiyun 		region->offset = 0;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /* CSB5's 'Function Control Register' has bits for decoding @ >= 0xffc00000 */
133*4882a593Smuzhiyun #define CSB5_FCR	0x41
134*4882a593Smuzhiyun #define CSB5_FCR_DECODE_ALL 0x0e
scb2_flash_probe(struct pci_dev * dev,const struct pci_device_id * ent)135*4882a593Smuzhiyun static int scb2_flash_probe(struct pci_dev *dev,
136*4882a593Smuzhiyun 			    const struct pci_device_id *ent)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	u8 reg;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	/* enable decoding of the flash region in the south bridge */
141*4882a593Smuzhiyun 	pci_read_config_byte(dev, CSB5_FCR, &reg);
142*4882a593Smuzhiyun 	pci_write_config_byte(dev, CSB5_FCR, reg | CSB5_FCR_DECODE_ALL);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (!request_mem_region(SCB2_ADDR, SCB2_WINDOW, scb2_map.name)) {
145*4882a593Smuzhiyun 		/*
146*4882a593Smuzhiyun 		 * The BIOS seems to mark the flash region as 'reserved'
147*4882a593Smuzhiyun 		 * in the e820 map.  Warn and go about our business.
148*4882a593Smuzhiyun 		 */
149*4882a593Smuzhiyun 		printk(KERN_WARNING MODNAME
150*4882a593Smuzhiyun 		    ": warning - can't reserve rom window, continuing\n");
151*4882a593Smuzhiyun 		region_fail = 1;
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* remap the IO window (w/o caching) */
155*4882a593Smuzhiyun 	scb2_ioaddr = ioremap(SCB2_ADDR, SCB2_WINDOW);
156*4882a593Smuzhiyun 	if (!scb2_ioaddr) {
157*4882a593Smuzhiyun 		printk(KERN_ERR MODNAME ": Failed to ioremap window!\n");
158*4882a593Smuzhiyun 		if (!region_fail)
159*4882a593Smuzhiyun 			release_mem_region(SCB2_ADDR, SCB2_WINDOW);
160*4882a593Smuzhiyun 		return -ENOMEM;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	scb2_map.phys = SCB2_ADDR;
164*4882a593Smuzhiyun 	scb2_map.virt = scb2_ioaddr;
165*4882a593Smuzhiyun 	scb2_map.size = SCB2_WINDOW;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	simple_map_init(&scb2_map);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* try to find a chip */
170*4882a593Smuzhiyun 	scb2_mtd = do_map_probe("cfi_probe", &scb2_map);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (!scb2_mtd) {
173*4882a593Smuzhiyun 		printk(KERN_ERR MODNAME ": flash probe failed!\n");
174*4882a593Smuzhiyun 		iounmap(scb2_ioaddr);
175*4882a593Smuzhiyun 		if (!region_fail)
176*4882a593Smuzhiyun 			release_mem_region(SCB2_ADDR, SCB2_WINDOW);
177*4882a593Smuzhiyun 		return -ENODEV;
178*4882a593Smuzhiyun 	}
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	scb2_mtd->owner = THIS_MODULE;
181*4882a593Smuzhiyun 	if (scb2_fixup_mtd(scb2_mtd) < 0) {
182*4882a593Smuzhiyun 		mtd_device_unregister(scb2_mtd);
183*4882a593Smuzhiyun 		map_destroy(scb2_mtd);
184*4882a593Smuzhiyun 		iounmap(scb2_ioaddr);
185*4882a593Smuzhiyun 		if (!region_fail)
186*4882a593Smuzhiyun 			release_mem_region(SCB2_ADDR, SCB2_WINDOW);
187*4882a593Smuzhiyun 		return -ENODEV;
188*4882a593Smuzhiyun 	}
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	printk(KERN_NOTICE MODNAME ": chip size 0x%llx at offset 0x%llx\n",
191*4882a593Smuzhiyun 	       (unsigned long long)scb2_mtd->size,
192*4882a593Smuzhiyun 	       (unsigned long long)(SCB2_WINDOW - scb2_mtd->size));
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	mtd_device_register(scb2_mtd, NULL, 0);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	return 0;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
scb2_flash_remove(struct pci_dev * dev)199*4882a593Smuzhiyun static void scb2_flash_remove(struct pci_dev *dev)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	if (!scb2_mtd)
202*4882a593Smuzhiyun 		return;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/* disable flash writes */
205*4882a593Smuzhiyun 	mtd_lock(scb2_mtd, 0, scb2_mtd->size);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	mtd_device_unregister(scb2_mtd);
208*4882a593Smuzhiyun 	map_destroy(scb2_mtd);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	iounmap(scb2_ioaddr);
211*4882a593Smuzhiyun 	scb2_ioaddr = NULL;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (!region_fail)
214*4882a593Smuzhiyun 		release_mem_region(SCB2_ADDR, SCB2_WINDOW);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun static struct pci_device_id scb2_flash_pci_ids[] = {
218*4882a593Smuzhiyun 	{
219*4882a593Smuzhiyun 	  .vendor = PCI_VENDOR_ID_SERVERWORKS,
220*4882a593Smuzhiyun 	  .device = PCI_DEVICE_ID_SERVERWORKS_CSB5,
221*4882a593Smuzhiyun 	  .subvendor = PCI_ANY_ID,
222*4882a593Smuzhiyun 	  .subdevice = PCI_ANY_ID
223*4882a593Smuzhiyun 	},
224*4882a593Smuzhiyun 	{ 0, }
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun static struct pci_driver scb2_flash_driver = {
228*4882a593Smuzhiyun 	.name =     "Intel SCB2 BIOS Flash",
229*4882a593Smuzhiyun 	.id_table = scb2_flash_pci_ids,
230*4882a593Smuzhiyun 	.probe =    scb2_flash_probe,
231*4882a593Smuzhiyun 	.remove =   scb2_flash_remove,
232*4882a593Smuzhiyun };
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun module_pci_driver(scb2_flash_driver);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun MODULE_LICENSE("GPL");
237*4882a593Smuzhiyun MODULE_AUTHOR("Tim Hockin <thockin@sun.com>");
238*4882a593Smuzhiyun MODULE_DESCRIPTION("MTD map driver for Intel SCB2 BIOS Flash");
239*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, scb2_flash_pci_ids);
240