1 /*
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 $
5 */
6
7
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <asm/io.h>
12 #include <asm/byteorder.h>
13 #include <linux/errno.h>
14 #include <linux/malloc.h>
15
16 #include <linux/mtd/map.h>
17 #include <linux/mtd/cfi.h>
18
19
20 struct mtd_info *cfi_probe(struct map_info *);
21
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 *);
25
26 static const char im_name[] = "cfi_probe";
27
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.
34 */
35 struct mtd_info *cfi_probe(struct map_info *map)
36 {
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);
41
42 if (!cfi)
43 return NULL;
44
45 map->fldrv_priv = cfi;
46 map->im_name = im_name;
47
48 /* OK we liked it. Now find a driver for the command set it talks */
49
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 */
52
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.
56 */
57
58 if(cfi->cmdset_setup) {
59 mtd = cfi->cmdset_setup(map);
60
61 if (mtd)
62 return mtd;
63 inter_module_put(cfi->im_name);
64 }
65 printk("No supported Vendor Command Set found\n");
66
67 kfree(cfi);
68 map->fldrv_priv = NULL;
69 return NULL;
70
71 }
72
73 static int cfi_probe_new_chip(struct map_info *map, unsigned long base,
74 struct flchip *chips, struct cfi_private *cfi)
75 {
76 switch (map->buswidth) {
77
78 case 1: {
79 unsigned char tmp = map->read8(map, base + 0x55);
80
81 /* If there's a device there, put it in Query Mode */
82 map->write8(map, 0x98, base+0x55);
83
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);
88 if (chips) {
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);
93 }
94 return 1;
95 } else {
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);
99 }
100 return 0;
101 }
102 }
103
104 case 2: {
105 __u16 tmp = map->read16(map, base + 0xaa);
106
107 /* If there's a device there, put it into Query Mode */
108 map->write16(map, 0x9898, base+0xAA);
109
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);
114 if (chips) {
115 /* Check previous chips for aliases */
116 int i;
117
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);
139 return 1;
140 } /* else no, they're different, fall through. */
141 } else {
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);
148 return 1;
149 }
150 }
151 /* No, the old one didn't look like it's in query mode. Next. */
152 }
153
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 */
159 return 1;
160 }
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;
165 cfi->numchips++;
166
167 /* Put it back into Read Mode */
168 map->write16(map, 0xffff, base+0xaa);
169 }
170
171 return 1;
172 }
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",
177 map->name, base);
178 if (chips) {
179 /* Check previous chips for aliases */
180 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
181
182 /* Put it back into Read Mode */
183 map->write16(map, 0xffff, base+0xaa);
184 }
185
186 return 2;
187 } else {
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);
191 }
192 return 0;
193 }
194 }
195
196
197 case 4: {
198 __u32 tmp = map->read32(map, base+0x154);
199
200 /* If there's a device there, put it into Query Mode */
201 map->write32(map, 0x98989898, base+0x154);
202
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);
208 if (chips) {
209 /* Check previous chips for aliases */
210 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
211
212 /* Put it back into read mode */
213 map->write32(map, 0xffffffff, base+0x154);
214 }
215 return 1;
216 }
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);
221 if (chips) {
222 /* Check previous chips for aliases */
223 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
224
225 /* Put it back into read mode */
226 map->write32(map, 0xffffffff, base+0x154);
227 }
228 return 2;
229 }
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);
234 if (chips) {
235 /* Check previous chips for aliases */
236 printk("FIXME: Do alias check at line %d of cfi_probe.c\n", __LINE__);
237
238 /* Put it back into read mode */
239 map->write32(map, 0xffffffff, base+0x154);
240 }
241 return 4;
242 } else {
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);
246 }
247 return 0;
248 }
249 }
250 default:
251 printk(KERN_WARNING "cfi_probe called with strange buswidth %d\n", map->buswidth);
252 return 0;
253 }
254 }
255
256 static struct cfi_private *cfi_cfi_probe(struct map_info *map)
257 {
258 unsigned long base=0;
259 struct cfi_private cfi;
260 struct cfi_private *retcfi;
261 struct flchip chip[MAX_CFI_CHIPS];
262 int i;
263
264 memset(&cfi, 0, sizeof(cfi));
265
266 /* The first invocation (with chips == NULL) leaves the device in Query Mode */
267 cfi.interleave = cfi_probe_new_chip(map, 0, NULL, NULL);
268
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 */
272 return NULL;
273 }
274
275 /* Read the Basic Query Structure from the device */
276
277 for (i=0; i<sizeof(struct cfi_ident); i++) {
278 ((unsigned char *)&cfi.cfiq)[i] = map->read8(map,base + ((0x10 + i)*map->buswidth));
279 }
280
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);
288
289 #if 1
290 /* Dump the information therein */
291 print_cfi_ident(&cfi.cfiq);
292
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);
298
299 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
300 i, EraseRegionInfoSize * 256, EraseRegionInfoNum+1);
301 }
302
303 printk("\n");
304 #endif
305
306 /* Switch the chip back into Read Mode, to make the alias detection work */
307 switch(map->buswidth) {
308 case 1:
309 map->write8(map, 0xff, 0x55);
310 break;
311 case 2:
312 map->write16(map, 0xffff, 0xaa);
313 break;
314 case 4:
315 map->write32(map, 0xffffffff, 0x154);
316 break;
317 }
318
319 /* OK. We've worked out what it is and we're happy with it. Now see if there are others */
320
321 chip[0].start = 0;
322 chip[0].state = FL_READY;
323 chip[0].mutex = &chip[0]._spinlock;
324
325 cfi.chipshift = cfi.cfiq.DevSize;
326 cfi.numchips = 1;
327
328 if (!cfi.chipshift) {
329 printk("cfi.chipsize is zero. This is bad. cfi.cfiq.DevSize is %d\n", cfi.cfiq.DevSize);
330 return NULL;
331 }
332
333 for (base = (1<<cfi.chipshift); base < map->size; base += (1<<cfi.chipshift))
334 cfi_probe_new_chip(map, base, &chip[0], &cfi);
335
336 retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
337
338 if (!retcfi)
339 return NULL;
340
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);
346 }
347 return retcfi;
348 }
349
350 static char *vendorname(__u16 vendor)
351 {
352 switch (vendor) {
353 case P_ID_NONE:
354 return "None";
355
356 case P_ID_INTEL_EXT:
357 return "Intel/Sharp Extended";
358
359 case P_ID_AMD_STD:
360 return "AMD/Fujitsu Standard";
361
362 case P_ID_INTEL_STD:
363 return "Intel/Sharp Standard";
364
365 case P_ID_AMD_EXT:
366 return "AMD/Fujitsu Extended";
367
368 case P_ID_MITSUBISHI_STD:
369 return "Mitsubishi Standard";
370
371 case P_ID_MITSUBISHI_EXT:
372 return "Mitsubishi Extended";
373
374 case P_ID_RESERVED:
375 return "Not Allowed / Reserved for Future Use";
376
377 default:
378 return "Unknown";
379 }
380 }
381
382
383 static void print_cfi_ident(struct cfi_ident *cfip)
384 {
385 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
386 printk("Invalid CFI ident structure.\n");
387 return;
388 }
389
390 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
391 if (cfip->P_ADR)
392 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
393 else
394 printk("No Primary Algorithm Table\n");
395
396 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
397 if (cfip->A_ADR)
398 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
399 else
400 printk("No Alternate Algorithm Table\n");
401
402
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);
405 if (cfip->VppMin) {
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);
408 }
409 else
410 printk("No Vpp line\n");
411
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));
414
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));
418 }
419 else
420 printk("Full buffer write not supported\n");
421
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));
427 }
428 else
429 printk("Chip erase not supported\n");
430
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) {
434 case 0:
435 printk(" - x8-only asynchronous interface\n");
436 break;
437
438 case 1:
439 printk(" - x16-only asynchronous interface\n");
440 break;
441
442 case 2:
443 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
444 break;
445
446 case 3:
447 printk(" - x32-only asynchronous interface\n");
448 break;
449
450 case 65535:
451 printk(" - Not Allowed / Reserved\n");
452 break;
453
454 default:
455 printk(" - Unknown\n");
456 break;
457 }
458
459 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
460 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
461
462 }
463
464 static void check_cmd_set(struct map_info *map, int primary, unsigned long base)
465 {
466 __u16 adr;
467 struct cfi_private *cfi = map->fldrv_priv;
468 __u16 type = primary?cfi->cfiq.P_ID:cfi->cfiq.A_ID;
469 char probename[32];
470 void (*probe_function)(struct map_info *, int, unsigned long);
471
472 if (type == P_ID_NONE || type == P_ID_RESERVED)
473 return;
474
475 sprintf(probename, "cfi_cmdset_%4.4X", type);
476
477 probe_function = inter_module_get_request(probename, probename);
478 if (probe_function) {
479 (*probe_function)(map, primary, base);
480 return;
481 }
482
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;
485
486 if (!adr) {
487 printk(" No Extended Query Table\n");
488 }
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",
493 adr,
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)));
497 }
498 else {
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)));
502 }
503 }
504
505 static int __init cfi_probe_init(void)
506 {
507 inter_module_register(im_name, THIS_MODULE, &cfi_probe);
508 return 0;
509 }
510
511 static void __exit cfi_probe_exit(void)
512 {
513 inter_module_unregister(im_name);
514 }
515
516 module_init(cfi_probe_init);
517 module_exit(cfi_probe_exit);
518
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.