2 Common Flash Interface probe code.
3 (C) 2000 Red Hat. GPL'd.
4 $Id: cfi_probe.c,v 1.69 2002/05/11 22:13:03 dwmw2 Exp $
7 #include <linux/config.h>
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/slab.h>
15 #include <linux/interrupt.h>
17 #include <linux/mtd/map.h>
18 #include <linux/mtd/cfi.h>
19 #include <linux/mtd/gen_probe.h>
24 static void print_cfi_ident(struct cfi_ident *);
27 static int cfi_probe_chip(struct map_info *map, __u32 base,
28 struct flchip *chips, struct cfi_private *cfi);
29 static int cfi_chip_setup(struct map_info *map, struct cfi_private *cfi);
31 struct mtd_info *cfi_probe(struct map_info *map);
34 in: interleave,type,mode
35 ret: table index, <0 for error
37 static inline int qry_present(struct map_info *map, __u32 base,
38 struct cfi_private *cfi)
40 int osf = cfi->interleave * cfi->device_type; // scale factor
42 if (cfi_read(map,base+osf*0x10)==cfi_build_cmd('Q',map,cfi) &&
43 cfi_read(map,base+osf*0x11)==cfi_build_cmd('R',map,cfi) &&
44 cfi_read(map,base+osf*0x12)==cfi_build_cmd('Y',map,cfi))
47 return 0; // nothing found
50 static int cfi_probe_chip(struct map_info *map, __u32 base,
51 struct flchip *chips, struct cfi_private *cfi)
55 if ((base + 0) >= map->size) {
57 "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
58 (unsigned long)base, map->size -1);
61 if ((base + 0xff) >= map->size) {
63 "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
64 (unsigned long)base + 0x55, map->size -1);
67 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
68 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
70 if (!qry_present(map,base,cfi))
74 /* This is the first time we're called. Set up the CFI
75 stuff accordingly and return */
76 return cfi_chip_setup(map, cfi);
79 /* Check each previous chip to see if it's an alias */
80 for (i=0; i<cfi->numchips; i++) {
81 /* This chip should be in read mode if it's one
82 we've already touched. */
83 if (qry_present(map,chips[i].start,cfi)) {
84 /* Eep. This chip also had the QRY marker.
85 * Is it an alias for the new one? */
86 cfi_send_gen_cmd(0xF0, 0, chips[i].start, map, cfi, cfi->device_type, NULL);
88 /* If the QRY marker goes away, it's an alias */
89 if (!qry_present(map, chips[i].start, cfi)) {
90 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
91 map->name, base, chips[i].start);
94 /* Yes, it's actually got QRY for data. Most
95 * unfortunate. Stick the new chip in read mode
96 * too and if it's the same, assume it's an alias. */
97 /* FIXME: Use other modes to do a proper check */
98 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
100 if (qry_present(map, base, cfi)) {
101 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
102 map->name, base, chips[i].start);
108 /* OK, if we got to here, then none of the previous chips appear to
109 be aliases for the current one. */
110 if (cfi->numchips == MAX_CFI_CHIPS) {
111 printk(KERN_WARNING"%s: Too many flash chips detected. Increase MAX_CFI_CHIPS from %d.\n", map->name, MAX_CFI_CHIPS);
112 /* Doesn't matter about resetting it to Read Mode - we're not going to talk to it anyway */
115 chips[cfi->numchips].start = base;
116 chips[cfi->numchips].state = FL_READY;
119 /* Put it back into Read Mode */
120 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
122 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit mode\n",
123 map->name, cfi->interleave, cfi->device_type*8, base,
129 static int cfi_chip_setup(struct map_info *map,
130 struct cfi_private *cfi)
132 int ofs_factor = cfi->interleave*cfi->device_type;
134 int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
138 printk("Number of erase regions: %d\n", num_erase_regions);
140 if (!num_erase_regions)
143 cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
145 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
149 memset(cfi->cfiq,0,sizeof(struct cfi_ident));
151 cfi->cfi_mode = CFI_MODE_CFI;
152 cfi->fast_prog=1; /* CFI supports fast programming */
154 /* Read the CFI info structure */
155 for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++) {
156 ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
159 /* Do any necessary byteswapping */
160 cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
162 cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
163 cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
164 cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
165 cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
166 cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
169 /* Dump the information therein */
170 print_cfi_ident(cfi->cfiq);
173 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
174 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
177 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
178 i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
179 (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
182 /* Put it back into Read Mode */
183 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
189 static char *vendorname(__u16 vendor)
196 return "Intel/Sharp Extended";
199 return "AMD/Fujitsu Standard";
202 return "Intel/Sharp Standard";
205 return "AMD/Fujitsu Extended";
207 case P_ID_MITSUBISHI_STD:
208 return "Mitsubishi Standard";
210 case P_ID_MITSUBISHI_EXT:
211 return "Mitsubishi Extended";
214 return "Not Allowed / Reserved for Future Use";
222 static void print_cfi_ident(struct cfi_ident *cfip)
225 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
226 printk("Invalid CFI ident structure.\n");
230 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
232 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
234 printk("No Primary Algorithm Table\n");
236 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
238 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
240 printk("No Alternate Algorithm Table\n");
243 printk("Vcc Minimum: %x.%x V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
244 printk("Vcc Maximum: %x.%x V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
246 printk("Vpp Minimum: %x.%x V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
247 printk("Vpp Maximum: %x.%x V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
250 printk("No Vpp line\n");
252 printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
253 printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
255 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
256 printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
257 printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
260 printk("Full buffer write not supported\n");
262 printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
263 printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
264 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
265 printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
266 printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
269 printk("Chip erase not supported\n");
271 printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
272 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
273 switch(cfip->InterfaceDesc) {
275 printk(" - x8-only asynchronous interface\n");
279 printk(" - x16-only asynchronous interface\n");
283 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
287 printk(" - x32-only asynchronous interface\n");
291 printk(" - Not Allowed / Reserved\n");
295 printk(" - Unknown\n");
299 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
300 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
303 #endif /* DEBUG_CFI */
305 static struct chip_probe cfi_chip_probe = {
307 probe_chip: cfi_probe_chip
310 struct mtd_info *cfi_probe(struct map_info *map)
313 * Just use the generic probe stuff to call our CFI-specific
314 * chip_probe routine in all the possible permutations, etc.
316 return mtd_do_chip_probe(map, &cfi_chip_probe);
319 static struct mtd_chip_driver cfi_chipdrv = {
325 int __init cfi_probe_init(void)
327 register_mtd_chip_driver(&cfi_chipdrv);
331 static void __exit cfi_probe_exit(void)
333 unregister_mtd_chip_driver(&cfi_chipdrv);
336 module_init(cfi_probe_init);
337 module_exit(cfi_probe_exit);
339 MODULE_LICENSE("GPL");
340 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
341 MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");