2 Common Flash Interface probe code.
3 (C) 2000 Red Hat. GPL'd.
4 $Id: cfi_probe.c,v 1.12 2000/07/03 13:29:16 dwmw2 Exp $
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
12 #include <asm/byteorder.h>
13 #include <linux/errno.h>
14 #include <linux/malloc.h>
16 #include <linux/mtd/map.h>
17 #include <linux/mtd/cfi.h>
20 struct mtd_info *cfi_probe(struct map_info *);
22 static void print_cfi_ident(struct cfi_ident *);
23 static void check_cmd_set(struct map_info *, int, unsigned long);
24 static struct cfi_private *cfi_cfi_probe(struct map_info *);
26 static const char im_name[] = "cfi_probe";
28 /* This routine is made available to other mtd code via
29 * inter_module_register. It must only be accessed through
30 * inter_module_get which will bump the use count of this module. The
31 * addresses passed back in mtd are valid as long as the use count of
32 * this module is non-zero, i.e. between inter_module_get and
33 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
35 struct mtd_info *cfi_probe(struct map_info *map)
37 struct mtd_info *mtd = NULL;
38 struct cfi_private *cfi;
39 /* First probe the map to see if we have CFI stuff there. */
40 cfi = cfi_cfi_probe(map);
45 map->fldrv_priv = cfi;
46 map->im_name = im_name;
48 /* OK we liked it. Now find a driver for the command set it talks */
50 check_cmd_set(map, 1, cfi->chips[0].start); /* First the primary cmdset */
51 check_cmd_set(map, 0, cfi->chips[0].start); /* Then the secondary */
53 /* check_cmd_set() will have used inter_module_get to increase
54 the use count of the module which provides the command set
55 driver. If we're quitting, we have to decrease it again.
58 if(cfi->cmdset_setup) {
59 mtd = cfi->cmdset_setup(map);
63 inter_module_put(cfi->im_name);
65 printk("No supported Vendor Command Set found\n");
68 map->fldrv_priv = NULL;
73 static int cfi_probe_new_chip(struct map_info *map, unsigned long base,
74 struct flchip *chips, struct cfi_private *cfi)
76 switch (map->buswidth) {
79 unsigned char tmp = map->read8(map, base + 0x55);
81 /* If there's a device there, put it in Query Mode */
82 map->write8(map, 0x98, base+0x55);
84 if (map->read8(map,base+0x10) == 'Q' &&
85 map->read8(map,base+0x11) == 'R' &&
86 map->read8(map,base+0x12) == 'Y') {
87 printk("%s: Found a CFI device at 0x%lx in 8 bit mode\n", map->name, base);
89 /* Check previous chips for aliases */
90 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
91 /* Put it back into Read Mode */
92 map->write8(map, 0x98, base+0x55);
96 if (map->read8(map, base + 0x55) == 0x98) {
97 /* It looks like RAM. Put back the stuff we overwrote */
98 map->write8(map, tmp, base+0x55);
105 __u16 tmp = map->read16(map, base + 0xaa);
107 /* If there's a device there, put it into Query Mode */
108 map->write16(map, 0x9898, base+0xAA);
110 if (map->read16(map, base+0x20) == cpu_to_le16(0x0051) &&
111 map->read16(map, base+0x22) == cpu_to_le16(0x0052) &&
112 map->read16(map, base+0x24) == cpu_to_le16(0x0059)) {
113 printk("%s: Found a CFI device at 0x%lx in 16 bit mode\n", map->name, base);
115 /* Check previous chips for aliases */
118 for (i=0; i < cfi->numchips; i++) {
119 /* This chip should be in read mode if it's one
120 we've already touched. */
121 if (map->read16(map, chips[i].start+0x20) == cpu_to_le16(0x0051) &&
122 map->read16(map, chips[i].start+0x22) == cpu_to_le16(0x0052) &&
123 map->read16(map, chips[i].start+0x24) == cpu_to_le16(0x0059)){
124 /* Either the old chip has got 'Q''R''Y' in a most
125 unfortunate place, or it's an alias of the new
126 chip. Double-check that it's in read mode, and check. */
127 map->write16(map, 0xffff, chips[i].start+0x20);
128 if (map->read16(map, chips[i].start+0x20) == cpu_to_le16(0x0051) &&
129 map->read16(map, chips[i].start+0x22) == cpu_to_le16(0x0052) &&
130 map->read16(map, chips[i].start+0x24) == cpu_to_le16(0x0059)) {
131 /* Yes it's got QRY for data. Most unfortunate.
132 Stick the old one in read mode too. */
133 map->write16(map, 0xffff, base);
134 if (map->read16(map, base+0x20) == cpu_to_le16(0x0051) &&
135 map->read16(map, base+0x22) == cpu_to_le16(0x0052) &&
136 map->read16(map, base+0x24) == cpu_to_le16(0x0059)) {
137 /* OK, so has the new one. Assume it's an alias */
138 printk("T'was probably an alias for the chip at 0x%lx\n", chips[i].start);
140 } /* else no, they're different, fall through. */
142 /* No the old one hasn't got QRY for data. Therefore,
143 it's an alias of the new one. */
144 map->write16(map, 0xffff, base+0xaa);
145 /* Just to be paranoid. */
146 map->write16(map, 0xffff, chips[i].start+0xaa);
147 printk("T'was an alias for the chip at 0x%lx\n", chips[i].start);
151 /* No, the old one didn't look like it's in query mode. Next. */
154 /* OK, if we got to here, then none of the previous chips appear to
155 be aliases for the current one. */
156 if (cfi->numchips == MAX_CFI_CHIPS) {
157 printk("%s: Too many flash chips detected. Increase MAX_CFI_CHIPS from %d.\n", map->name, MAX_CFI_CHIPS);
158 /* Doesn't matter about resetting it to Read Mode - we're not going to talk to it anyway */
161 printk("Not an alias. Adding\n");
162 chips[cfi->numchips].start = base;
163 chips[cfi->numchips].state = FL_READY;
164 chips[cfi->numchips].mutex = &chips[cfi->numchips]._spinlock;
167 /* Put it back into Read Mode */
168 map->write16(map, 0xffff, base+0xaa);
173 else if (map->read16(map, base+0x20) == 0x5151 &&
174 map->read16(map, base+0x22) == 0x5252 &&
175 map->read16(map, base+0x24) == 0x5959) {
176 printk("%s: Found a coupled pair of CFI devices at %lx in 8 bit mode\n",
179 /* Check previous chips for aliases */
180 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
182 /* Put it back into Read Mode */
183 map->write16(map, 0xffff, base+0xaa);
188 if (map->read16(map, base+0xaa) == 0x9898) {
189 /* It looks like RAM. Put back the stuff we overwrote */
190 map->write16(map, tmp, base+0xaa);
198 __u32 tmp = map->read32(map, base+0x154);
200 /* If there's a device there, put it into Query Mode */
201 map->write32(map, 0x98989898, base+0x154);
203 if (map->read32(map, base+0x40) == cpu_to_le32(0x00000051) &&
204 map->read32(map, base+0x44) == cpu_to_le32(0x00000052) &&
205 map->read32(map, base+0x48) == cpu_to_le32(0x00000059)) {
206 /* This isn't actually in the CFI spec - only x8 and x16 are. */
207 printk("%s: Found a CFI device at %lx in 32 bit mode\n", map->name, base);
209 /* Check previous chips for aliases */
210 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
212 /* Put it back into read mode */
213 map->write32(map, 0xffffffff, base+0x154);
217 else if (map->read32(map, base+0x40) == cpu_to_le32(0x00510051) &&
218 map->read32(map, base+0x44) == cpu_to_le32(0x00520052) &&
219 map->read32(map, base+0x48) == cpu_to_le32(0x00590059)) {
220 printk("%s: Found a coupled pair of CFI devices at location %lx in 16 bit mode\n", map->name, base);
222 /* Check previous chips for aliases */
223 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
225 /* Put it back into read mode */
226 map->write32(map, 0xffffffff, base+0x154);
230 else if (map->read32(map, base+0x40) == 0x51515151 &&
231 map->read32(map, base+0x44) == 0x52525252 &&
232 map->read32(map, base+0x48) == 0x59595959) {
233 printk("%s: Found four side-by-side CFI devices at location %lx in 8 bit mode\n", map->name, base);
235 /* Check previous chips for aliases */
236 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
238 /* Put it back into read mode */
239 map->write32(map, 0xffffffff, base+0x154);
243 if (map->read32(map, base+0x154) == 0x98989898) {
244 /* It looks like RAM. Put back the stuff we overwrote */
245 map->write32(map, tmp, base+0x154);
251 printk(KERN_WARNING "cfi_probe called with strange buswidth %d\n", map->buswidth);
256 static struct cfi_private *cfi_cfi_probe(struct map_info *map)
258 unsigned long base=0;
259 struct cfi_private cfi;
260 struct cfi_private *retcfi;
261 struct flchip chip[MAX_CFI_CHIPS];
264 memset(&cfi, 0, sizeof(cfi));
266 /* The first invocation (with chips == NULL) leaves the device in Query Mode */
267 cfi.interleave = cfi_probe_new_chip(map, 0, NULL, NULL);
269 if (!cfi.interleave) {
270 printk("%s: Found no CFI device at location zero\n", map->name);
271 /* Doesn't appear to be CFI-compliant at all */
275 /* Read the Basic Query Structure from the device */
277 for (i=0; i<sizeof(struct cfi_ident); i++) {
278 ((unsigned char *)&cfi.cfiq)[i] = map->read8(map,base + ((0x10 + i)*map->buswidth));
281 /* Do any necessary byteswapping */
282 cfi.cfiq.P_ID = le16_to_cpu(cfi.cfiq.P_ID);
283 cfi.cfiq.P_ADR = le16_to_cpu(cfi.cfiq.P_ADR);
284 cfi.cfiq.A_ID = le16_to_cpu(cfi.cfiq.A_ID);
285 cfi.cfiq.A_ADR = le16_to_cpu(cfi.cfiq.A_ADR);
286 cfi.cfiq.InterfaceDesc = le16_to_cpu(cfi.cfiq.InterfaceDesc);
287 cfi.cfiq.MaxBufWriteSize = le16_to_cpu(cfi.cfiq.MaxBufWriteSize);
290 /* Dump the information therein */
291 print_cfi_ident(&cfi.cfiq);
293 for (i=0; i<cfi.cfiq.NumEraseRegions; i++) {
294 __u16 EraseRegionInfoNum = (map->read8(map,base + ((0x2d + (4*i))*map->buswidth))) +
295 (((map->read8(map,(0x2e + (4*i))*map->buswidth)) << 8));
296 __u16 EraseRegionInfoSize = (map->read8(map, base + ((0x2f + (4*i))*map->buswidth))) +
297 (map->read8(map, base + ((0x30 + (4*i))*map->buswidth)) << 8);
299 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
300 i, EraseRegionInfoSize * 256, EraseRegionInfoNum+1);
306 /* Switch the chip back into Read Mode, to make the alias detection work */
307 switch(map->buswidth) {
309 map->write8(map, 0xff, 0x55);
312 map->write16(map, 0xffff, 0xaa);
315 map->write32(map, 0xffffffff, 0x154);
319 /* OK. We've worked out what it is and we're happy with it. Now see if there are others */
322 chip[0].state = FL_READY;
323 chip[0].mutex = &chip[0]._spinlock;
325 cfi.chipshift = cfi.cfiq.DevSize;
328 if (!cfi.chipshift) {
329 printk("cfi.chipsize is zero. This is bad. cfi.cfiq.DevSize is %d\n", cfi.cfiq.DevSize);
333 for (base = (1<<cfi.chipshift); base < map->size; base += (1<<cfi.chipshift))
334 cfi_probe_new_chip(map, base, &chip[0], &cfi);
336 retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
341 memcpy(retcfi, &cfi, sizeof(cfi));
342 memcpy(&retcfi->chips[0], chip, sizeof(struct flchip) * cfi.numchips);
343 for (i=0; i< retcfi->numchips; i++) {
344 init_waitqueue_head(&retcfi->chips[i].wq);
345 spin_lock_init(&retcfi->chips[i]._spinlock);
350 static char *vendorname(__u16 vendor)
357 return "Intel/Sharp Extended";
360 return "AMD/Fujitsu Standard";
363 return "Intel/Sharp Standard";
366 return "AMD/Fujitsu Extended";
368 case P_ID_MITSUBISHI_STD:
369 return "Mitsubishi Standard";
371 case P_ID_MITSUBISHI_EXT:
372 return "Mitsubishi Extended";
375 return "Not Allowed / Reserved for Future Use";
383 static void print_cfi_ident(struct cfi_ident *cfip)
385 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
386 printk("Invalid CFI ident structure.\n");
390 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
392 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
394 printk("No Primary Algorithm Table\n");
396 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
398 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
400 printk("No Alternate Algorithm Table\n");
403 printk("Vcc Minimum: %x.%x V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
404 printk("Vcc Maximum: %x.%x V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
406 printk("Vpp Minimum: %x.%x V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
407 printk("Vpp Maximum: %x.%x V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
410 printk("No Vpp line\n");
412 printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
413 printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
415 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
416 printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
417 printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
420 printk("Full buffer write not supported\n");
422 printk("Typical block erase timeout: %d µs\n", 1<<cfip->BlockEraseTimeoutTyp);
423 printk("Maximum block erase timeout: %d µs\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
424 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
425 printk("Typical chip erase timeout: %d µs\n", 1<<cfip->ChipEraseTimeoutTyp);
426 printk("Maximum chip erase timeout: %d µs\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
429 printk("Chip erase not supported\n");
431 printk("Device size: 0x%X bytes (%d Mb)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
432 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
433 switch(cfip->InterfaceDesc) {
435 printk(" - x8-only asynchronous interface\n");
439 printk(" - x16-only asynchronous interface\n");
443 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
447 printk(" - x32-only asynchronous interface\n");
451 printk(" - Not Allowed / Reserved\n");
455 printk(" - Unknown\n");
459 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
460 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
464 static void check_cmd_set(struct map_info *map, int primary, unsigned long base)
467 struct cfi_private *cfi = map->fldrv_priv;
468 __u16 type = primary?cfi->cfiq.P_ID:cfi->cfiq.A_ID;
470 void (*probe_function)(struct map_info *, int, unsigned long);
472 if (type == P_ID_NONE || type == P_ID_RESERVED)
475 sprintf(probename, "cfi_cmdset_%4.4X", type);
477 probe_function = inter_module_get_request(probename, probename);
478 if (probe_function) {
479 (*probe_function)(map, primary, base);
483 /* This was a command set we don't know about. Print only the basic info */
484 adr = primary?cfi->cfiq.P_ADR:cfi->cfiq.A_ADR;
487 printk(" No Extended Query Table\n");
489 else if (map->read8(map,base+(adr*map->buswidth)) != (primary?'P':'A') ||
490 map->read8(map,base+((adr+1)*map->buswidth)) != (primary?'R':'L') ||
491 map->read8(map,base+((adr+2)*map->buswidth)) != (primary?'I':'T')) {
492 printk ("Invalid Extended Query Table at %4.4X: %2.2X %2.2X %2.2X\n",
494 map->read8(map,base+(adr*map->buswidth)),
495 map->read8(map,base+((adr+1)*map->buswidth)),
496 map->read8(map,base+((adr+2)*map->buswidth)));
499 printk(" Extended Query Table version %c.%c\n",
500 map->read8(map,base+((adr+3)*map->buswidth)),
501 map->read8(map,base+((adr+4)*map->buswidth)));
505 static int __init cfi_probe_init(void)
507 inter_module_register(im_name, THIS_MODULE, &cfi_probe);
511 static void __exit cfi_probe_exit(void)
513 inter_module_unregister(im_name);
516 module_init(cfi_probe_init);
517 module_exit(cfi_probe_exit);