1
2
3
4
5
6#include <linux/module.h>
7#include <linux/types.h>
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <asm/io.h>
11#include <asm/byteorder.h>
12#include <linux/errno.h>
13#include <linux/slab.h>
14#include <linux/interrupt.h>
15
16#include <linux/mtd/xip.h>
17#include <linux/mtd/map.h>
18#include <linux/mtd/cfi.h>
19#include <linux/mtd/gen_probe.h>
20
21
22
23#ifdef DEBUG_CFI
24static void print_cfi_ident(struct cfi_ident *);
25#endif
26
27static int cfi_probe_chip(struct map_info *map, __u32 base,
28 unsigned long *chip_map, struct cfi_private *cfi);
29static int cfi_chip_setup(struct map_info *map, struct cfi_private *cfi);
30
31struct mtd_info *cfi_probe(struct map_info *map);
32
33#ifdef CONFIG_MTD_XIP
34
35
36#define xip_disable() local_irq_disable()
37
38#define xip_allowed(base, map) \
39do { \
40 (void) map_read(map, base); \
41 xip_iprefetch(); \
42 local_irq_enable(); \
43} while (0)
44
45#define xip_enable(base, map, cfi) \
46do { \
47 cfi_qry_mode_off(base, map, cfi); \
48 xip_allowed(base, map); \
49} while (0)
50
51#define xip_disable_qry(base, map, cfi) \
52do { \
53 xip_disable(); \
54 cfi_qry_mode_on(base, map, cfi); \
55} while (0)
56
57#else
58
59#define xip_disable() do { } while (0)
60#define xip_allowed(base, map) do { } while (0)
61#define xip_enable(base, map, cfi) do { } while (0)
62#define xip_disable_qry(base, map, cfi) do { } while (0)
63
64#endif
65
66
67
68
69
70
71struct cfi_early_fixup {
72 uint16_t mfr;
73 uint16_t id;
74 void (*fixup)(struct cfi_private *cfi);
75};
76
77static void cfi_early_fixup(struct cfi_private *cfi,
78 const struct cfi_early_fixup *fixups)
79{
80 const struct cfi_early_fixup *f;
81
82 for (f = fixups; f->fixup; f++) {
83 if (((f->mfr == CFI_MFR_ANY) || (f->mfr == cfi->mfr)) &&
84 ((f->id == CFI_ID_ANY) || (f->id == cfi->id))) {
85 f->fixup(cfi);
86 }
87 }
88}
89
90
91
92
93
94
95static int __xipram cfi_probe_chip(struct map_info *map, __u32 base,
96 unsigned long *chip_map, struct cfi_private *cfi)
97{
98 int i;
99
100 if ((base + 0) >= map->size) {
101 printk(KERN_NOTICE
102 "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
103 (unsigned long)base, map->size -1);
104 return 0;
105 }
106 if ((base + 0xff) >= map->size) {
107 printk(KERN_NOTICE
108 "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
109 (unsigned long)base + 0x55, map->size -1);
110 return 0;
111 }
112
113 xip_disable();
114 if (!cfi_qry_mode_on(base, map, cfi)) {
115 xip_enable(base, map, cfi);
116 return 0;
117 }
118
119 if (!cfi->numchips) {
120
121
122 return cfi_chip_setup(map, cfi);
123 }
124
125
126 for (i=0; i < (base >> cfi->chipshift); i++) {
127 unsigned long start;
128 if(!test_bit(i, chip_map)) {
129
130 continue;
131 }
132 start = i << cfi->chipshift;
133
134
135 if (cfi_qry_present(map, start, cfi)) {
136
137
138 cfi_qry_mode_off(start, map, cfi);
139
140
141 if (!cfi_qry_present(map, start, cfi)) {
142 xip_allowed(base, map);
143 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
144 map->name, base, start);
145 return 0;
146 }
147
148
149
150
151 cfi_qry_mode_off(base, map, cfi);
152
153 if (cfi_qry_present(map, base, cfi)) {
154 xip_allowed(base, map);
155 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
156 map->name, base, start);
157 return 0;
158 }
159 }
160 }
161
162
163
164 set_bit((base >> cfi->chipshift), chip_map);
165 cfi->numchips++;
166
167
168 cfi_qry_mode_off(base, map, cfi);
169 xip_allowed(base, map);
170
171 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n",
172 map->name, cfi->interleave, cfi->device_type*8, base,
173 map->bankwidth*8);
174
175 return 1;
176}
177
178static void fixup_s70gl02gs_chips(struct cfi_private *cfi)
179{
180
181
182
183
184 cfi->cfiq->DevSize = 27;
185 cfi->cfiq->EraseRegionInfo[0] = 0x20003ff;
186 pr_warn("Bad S70GL02GS CFI data; adjust to detect 2 chips\n");
187}
188
189static const struct cfi_early_fixup cfi_early_fixup_table[] = {
190 { CFI_MFR_AMD, 0x4801, fixup_s70gl02gs_chips },
191 { },
192};
193
194static int __xipram cfi_chip_setup(struct map_info *map,
195 struct cfi_private *cfi)
196{
197 int ofs_factor = cfi->interleave*cfi->device_type;
198 __u32 base = 0;
199 int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
200 int i;
201 int extendedId1 = 0;
202 int extendedId2 = 0;
203 int extendedId3 = 0;
204 int addr_unlock1 = 0x555, addr_unlock2 = 0x2AA;
205
206 xip_enable(base, map, cfi);
207#ifdef DEBUG_CFI
208 printk("Number of erase regions: %d\n", num_erase_regions);
209#endif
210 if (!num_erase_regions)
211 return 0;
212
213 cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
214 if (!cfi->cfiq)
215 return 0;
216
217 memset(cfi->cfiq,0,sizeof(struct cfi_ident));
218
219 cfi->cfi_mode = CFI_MODE_CFI;
220
221 cfi->sector_erase_cmd = CMD(0x30);
222
223
224 xip_disable_qry(base, map, cfi);
225 for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++)
226 ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
227
228
229
230
231
232
233
234
235 cfi_send_gen_cmd(0xf0, 0, base, map, cfi, cfi->device_type, NULL);
236 cfi_send_gen_cmd(0xaa, 0x555, base, map, cfi, cfi->device_type, NULL);
237 cfi_send_gen_cmd(0x55, 0x2aa, base, map, cfi, cfi->device_type, NULL);
238 cfi_send_gen_cmd(0x90, 0x555, base, map, cfi, cfi->device_type, NULL);
239 cfi->mfr = cfi_read_query16(map, base);
240 cfi->id = cfi_read_query16(map, base + ofs_factor);
241
242
243 if ((cfi->mfr == CFI_MFR_INTEL || cfi->mfr == CFI_MFR_ST)
244 && ((cfi->id & 0xff) == 0x7e)
245 && (le16_to_cpu(cfi->cfiq->P_ID) == 0x0002)) {
246 extendedId1 = cfi_read_query16(map, base + 0x1 * ofs_factor);
247 extendedId2 = cfi_read_query16(map, base + 0xe * ofs_factor);
248 extendedId3 = cfi_read_query16(map, base + 0xf * ofs_factor);
249 }
250
251
252 if (cfi->mfr == CFI_MFR_AMD && (cfi->id & 0xff) == 0x7e)
253 cfi->id = cfi_read_query(map, base + 0xe * ofs_factor) << 8 |
254 cfi_read_query(map, base + 0xf * ofs_factor);
255
256
257 cfi_qry_mode_off(base, map, cfi);
258 xip_allowed(base, map);
259
260
261 cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
262
263 cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
264 cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
265 cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
266 cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
267 cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
268
269
270 if ((cfi->cfiq->MaxBufWriteSize > 0x8) && (cfi->mfr == CFI_MFR_INTEL ||
271 cfi->mfr == CFI_MFR_ST) && (extendedId1 == 0x7E) &&
272 (extendedId2 == 0x22 || extendedId2 == 0x23 || extendedId2 == 0x28) &&
273 (extendedId3 == 0x01)) {
274 cfi->cfiq->MaxBufWriteSize = 0x8;
275 pr_warning("Adjusted buffer size on Numonyx flash M29EW family");
276 pr_warning("in 8 bit mode\n");
277 }
278
279#ifdef DEBUG_CFI
280
281 print_cfi_ident(cfi->cfiq);
282#endif
283
284 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
285 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
286
287#ifdef DEBUG_CFI
288 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
289 i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
290 (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
291#endif
292 }
293
294 if (cfi->cfiq->P_ID == P_ID_SST_OLD) {
295 addr_unlock1 = 0x5555;
296 addr_unlock2 = 0x2AAA;
297 }
298
299
300
301
302
303
304
305
306
307 cfi_send_gen_cmd(0xf0, 0, base, map, cfi, cfi->device_type, NULL);
308 cfi_send_gen_cmd(0xaa, addr_unlock1, base, map, cfi, cfi->device_type, NULL);
309 cfi_send_gen_cmd(0x55, addr_unlock2, base, map, cfi, cfi->device_type, NULL);
310 cfi_send_gen_cmd(0x90, addr_unlock1, base, map, cfi, cfi->device_type, NULL);
311 cfi->mfr = cfi_read_query16(map, base);
312 cfi->id = cfi_read_query16(map, base + ofs_factor);
313
314
315 if (cfi->mfr == CFI_MFR_AMD && (cfi->id & 0xff) == 0x7e)
316 cfi->id = cfi_read_query(map, base + 0xe * ofs_factor) << 8 |
317 cfi_read_query(map, base + 0xf * ofs_factor);
318
319
320 cfi_qry_mode_off(base, map, cfi);
321 xip_allowed(base, map);
322
323 cfi_early_fixup(cfi, cfi_early_fixup_table);
324
325 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank. Manufacturer ID %#08x Chip ID %#08x\n",
326 map->name, cfi->interleave, cfi->device_type*8, base,
327 map->bankwidth*8, cfi->mfr, cfi->id);
328
329 return 1;
330}
331
332#ifdef DEBUG_CFI
333static char *vendorname(__u16 vendor)
334{
335 switch (vendor) {
336 case P_ID_NONE:
337 return "None";
338
339 case P_ID_INTEL_EXT:
340 return "Intel/Sharp Extended";
341
342 case P_ID_AMD_STD:
343 return "AMD/Fujitsu Standard";
344
345 case P_ID_INTEL_STD:
346 return "Intel/Sharp Standard";
347
348 case P_ID_AMD_EXT:
349 return "AMD/Fujitsu Extended";
350
351 case P_ID_WINBOND:
352 return "Winbond Standard";
353
354 case P_ID_ST_ADV:
355 return "ST Advanced";
356
357 case P_ID_MITSUBISHI_STD:
358 return "Mitsubishi Standard";
359
360 case P_ID_MITSUBISHI_EXT:
361 return "Mitsubishi Extended";
362
363 case P_ID_SST_PAGE:
364 return "SST Page Write";
365
366 case P_ID_SST_OLD:
367 return "SST 39VF160x/39VF320x";
368
369 case P_ID_INTEL_PERFORMANCE:
370 return "Intel Performance Code";
371
372 case P_ID_INTEL_DATA:
373 return "Intel Data";
374
375 case P_ID_RESERVED:
376 return "Not Allowed / Reserved for Future Use";
377
378 default:
379 return "Unknown";
380 }
381}
382
383
384static void print_cfi_ident(struct cfi_ident *cfip)
385{
386#if 0
387 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
388 printk("Invalid CFI ident structure.\n");
389 return;
390 }
391#endif
392 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
393 if (cfip->P_ADR)
394 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
395 else
396 printk("No Primary Algorithm Table\n");
397
398 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
399 if (cfip->A_ADR)
400 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
401 else
402 printk("No Alternate Algorithm Table\n");
403
404
405 printk("Vcc Minimum: %2d.%d V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
406 printk("Vcc Maximum: %2d.%d V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
407 if (cfip->VppMin) {
408 printk("Vpp Minimum: %2d.%d V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
409 printk("Vpp Maximum: %2d.%d V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
410 }
411 else
412 printk("No Vpp line\n");
413
414 printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
415 printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
416
417 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
418 printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
419 printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
420 }
421 else
422 printk("Full buffer write not supported\n");
423
424 printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
425 printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
426 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
427 printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
428 printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
429 }
430 else
431 printk("Chip erase not supported\n");
432
433 printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
434 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
435 switch(cfip->InterfaceDesc) {
436 case CFI_INTERFACE_X8_ASYNC:
437 printk(" - x8-only asynchronous interface\n");
438 break;
439
440 case CFI_INTERFACE_X16_ASYNC:
441 printk(" - x16-only asynchronous interface\n");
442 break;
443
444 case CFI_INTERFACE_X8_BY_X16_ASYNC:
445 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
446 break;
447
448 case CFI_INTERFACE_X32_ASYNC:
449 printk(" - x32-only asynchronous interface\n");
450 break;
451
452 case CFI_INTERFACE_X16_BY_X32_ASYNC:
453 printk(" - supports x16 and x32 via Word# with asynchronous interface\n");
454 break;
455
456 case CFI_INTERFACE_NOT_ALLOWED:
457 printk(" - Not Allowed / Reserved\n");
458 break;
459
460 default:
461 printk(" - Unknown\n");
462 break;
463 }
464
465 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
466 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
467
468}
469#endif
470
471static struct chip_probe cfi_chip_probe = {
472 .name = "CFI",
473 .probe_chip = cfi_probe_chip
474};
475
476struct mtd_info *cfi_probe(struct map_info *map)
477{
478
479
480
481
482 return mtd_do_chip_probe(map, &cfi_chip_probe);
483}
484
485static struct mtd_chip_driver cfi_chipdrv = {
486 .probe = cfi_probe,
487 .name = "cfi_probe",
488 .module = THIS_MODULE
489};
490
491static int __init cfi_probe_init(void)
492{
493 register_mtd_chip_driver(&cfi_chipdrv);
494 return 0;
495}
496
497static void __exit cfi_probe_exit(void)
498{
499 unregister_mtd_chip_driver(&cfi_chipdrv);
500}
501
502module_init(cfi_probe_init);
503module_exit(cfi_probe_exit);
504
505MODULE_LICENSE("GPL");
506MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
507MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");
508