2 Common Flash Interface probe code.
3 (C) 2000 Red Hat. GPL'd.
4 $Id: cfi_probe.c,v 1.77 2004/07/14 08:38:44 dwmw2 Exp $
7 #include <linux/config.h>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
13 #include <asm/byteorder.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
18 #include <linux/mtd/map.h>
19 #include <linux/mtd/cfi.h>
20 #include <linux/mtd/gen_probe.h>
25 static void print_cfi_ident(struct cfi_ident *);
28 static int cfi_probe_chip(struct map_info *map, __u32 base,
29 unsigned long *chip_map, struct cfi_private *cfi);
30 static int cfi_chip_setup(struct map_info *map, struct cfi_private *cfi);
32 struct mtd_info *cfi_probe(struct map_info *map);
35 in: interleave,type,mode
36 ret: table index, <0 for error
38 static int qry_present(struct map_info *map, __u32 base,
39 struct cfi_private *cfi)
41 int osf = cfi->interleave * cfi->device_type; // scale factor
45 qry = cfi_build_cmd('Q', map, cfi);
46 val = map_read(map, base + osf*0x10);
48 if (!map_word_equal(map, qry, val))
51 qry = cfi_build_cmd('R', map, cfi);
52 val = map_read(map, base + osf*0x11);
54 if (!map_word_equal(map, qry, val))
57 qry = cfi_build_cmd('Y', map, cfi);
58 val = map_read(map, base + osf*0x12);
60 if (!map_word_equal(map, qry, val))
63 return 1; // nothing found
66 static int cfi_probe_chip(struct map_info *map, __u32 base,
67 unsigned long *chip_map, struct cfi_private *cfi)
71 if ((base + 0) >= map->size) {
73 "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
74 (unsigned long)base, map->size -1);
77 if ((base + 0xff) >= map->size) {
79 "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
80 (unsigned long)base + 0x55, map->size -1);
83 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
84 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
85 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
87 if (!qry_present(map,base,cfi))
91 /* This is the first time we're called. Set up the CFI
92 stuff accordingly and return */
93 return cfi_chip_setup(map, cfi);
96 /* Check each previous chip to see if it's an alias */
97 for (i=0; i < (base >> cfi->chipshift); i++) {
99 if(!test_bit(i, chip_map)) {
100 /* Skip location; no valid chip at this address */
103 start = i << cfi->chipshift;
104 /* This chip should be in read mode if it's one
105 we've already touched. */
106 if (qry_present(map, start, cfi)) {
107 /* Eep. This chip also had the QRY marker.
108 * Is it an alias for the new one? */
109 cfi_send_gen_cmd(0xF0, 0, start, map, cfi, cfi->device_type, NULL);
110 cfi_send_gen_cmd(0xFF, 0, start, map, cfi, cfi->device_type, NULL);
112 /* If the QRY marker goes away, it's an alias */
113 if (!qry_present(map, start, cfi)) {
114 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
115 map->name, base, start);
118 /* Yes, it's actually got QRY for data. Most
119 * unfortunate. Stick the new chip in read mode
120 * too and if it's the same, assume it's an alias. */
121 /* FIXME: Use other modes to do a proper check */
122 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
123 cfi_send_gen_cmd(0xFF, 0, start, map, cfi, cfi->device_type, NULL);
125 if (qry_present(map, base, cfi)) {
126 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
127 map->name, base, start);
133 /* OK, if we got to here, then none of the previous chips appear to
134 be aliases for the current one. */
135 set_bit((base >> cfi->chipshift), chip_map); /* Update chip map */
138 /* Put it back into Read Mode */
139 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
140 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
142 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n",
143 map->name, cfi->interleave, cfi->device_type*8, base,
149 static int cfi_chip_setup(struct map_info *map,
150 struct cfi_private *cfi)
152 int ofs_factor = cfi->interleave*cfi->device_type;
154 int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
158 printk("Number of erase regions: %d\n", num_erase_regions);
160 if (!num_erase_regions)
163 cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
165 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
169 memset(cfi->cfiq,0,sizeof(struct cfi_ident));
171 cfi->cfi_mode = CFI_MODE_CFI;
173 /* Read the CFI info structure */
174 for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++) {
175 ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
178 /* Do any necessary byteswapping */
179 cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
181 cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
182 cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
183 cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
184 cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
185 cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
188 /* Dump the information therein */
189 print_cfi_ident(cfi->cfiq);
192 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
193 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
196 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
197 i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
198 (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
202 /* Note we put the device back into Read Mode BEFORE going into Auto
203 * Select Mode, as some devices support nesting of modes, others
204 * don't. This way should always work.
205 * On cmdset 0001 the writes of 0xaa and 0x55 are not needed, and
206 * so should be treated as nops or illegal (and so put the device
207 * back into Read Mode, which is a nop in this case).
209 cfi_send_gen_cmd(0xf0, 0, base, map, cfi, cfi->device_type, NULL);
210 cfi_send_gen_cmd(0xaa, 0x555, base, map, cfi, cfi->device_type, NULL);
211 cfi_send_gen_cmd(0x55, 0x2aa, base, map, cfi, cfi->device_type, NULL);
212 cfi_send_gen_cmd(0x90, 0x555, base, map, cfi, cfi->device_type, NULL);
213 cfi->mfr = cfi_read_query(map, base);
214 cfi->id = cfi_read_query(map, base + ofs_factor);
216 /* Put it back into Read Mode */
217 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
218 /* ... even if it's an Intel chip */
219 cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
221 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n",
222 map->name, cfi->interleave, cfi->device_type*8, base,
229 static char *vendorname(__u16 vendor)
236 return "Intel/Sharp Extended";
239 return "AMD/Fujitsu Standard";
242 return "Intel/Sharp Standard";
245 return "AMD/Fujitsu Extended";
247 case P_ID_MITSUBISHI_STD:
248 return "Mitsubishi Standard";
250 case P_ID_MITSUBISHI_EXT:
251 return "Mitsubishi Extended";
254 return "Not Allowed / Reserved for Future Use";
262 static void print_cfi_ident(struct cfi_ident *cfip)
265 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
266 printk("Invalid CFI ident structure.\n");
270 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
272 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
274 printk("No Primary Algorithm Table\n");
276 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
278 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
280 printk("No Alternate Algorithm Table\n");
283 printk("Vcc Minimum: %2d.%d V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
284 printk("Vcc Maximum: %2d.%d V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
286 printk("Vpp Minimum: %2d.%d V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
287 printk("Vpp Maximum: %2d.%d V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
290 printk("No Vpp line\n");
292 printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
293 printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
295 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
296 printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
297 printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
300 printk("Full buffer write not supported\n");
302 printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
303 printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
304 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
305 printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
306 printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
309 printk("Chip erase not supported\n");
311 printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
312 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
313 switch(cfip->InterfaceDesc) {
315 printk(" - x8-only asynchronous interface\n");
319 printk(" - x16-only asynchronous interface\n");
323 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
327 printk(" - x32-only asynchronous interface\n");
331 printk(" - Not Allowed / Reserved\n");
335 printk(" - Unknown\n");
339 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
340 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
343 #endif /* DEBUG_CFI */
345 static struct chip_probe cfi_chip_probe = {
347 .probe_chip = cfi_probe_chip
350 struct mtd_info *cfi_probe(struct map_info *map)
353 * Just use the generic probe stuff to call our CFI-specific
354 * chip_probe routine in all the possible permutations, etc.
356 return mtd_do_chip_probe(map, &cfi_chip_probe);
359 static struct mtd_chip_driver cfi_chipdrv = {
362 .module = THIS_MODULE
365 int __init cfi_probe_init(void)
367 register_mtd_chip_driver(&cfi_chipdrv);
371 static void __exit cfi_probe_exit(void)
373 unregister_mtd_chip_driver(&cfi_chipdrv);
376 module_init(cfi_probe_init);
377 module_exit(cfi_probe_exit);
379 MODULE_LICENSE("GPL");
380 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
381 MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");