2 Common Flash Interface probe code.
3 (C) 2000 Red Hat. GPL'd.
4 $Id: cfi_probe.c,v 1.1.1.1 2005/04/11 02:50:25 jack 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>
25 static void print_cfi_ident(struct cfi_ident *);
28 static int cfi_probe_chip(struct map_info *map, __u32 base,
29 struct flchip *chips, 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 inline int qry_present(struct map_info *map, __u32 base,
39 struct cfi_private *cfi)
41 int osf = cfi->interleave * cfi->device_type; // scale factor
43 if (cfi_read(map,base+osf*0x10)==cfi_build_cmd('Q',map,cfi) &&
44 cfi_read(map,base+osf*0x11)==cfi_build_cmd('R',map,cfi) &&
45 cfi_read(map,base+osf*0x12)==cfi_build_cmd('Y',map,cfi))
48 return 0; // nothing found
51 static int cfi_probe_chip(struct map_info *map, __u32 base,
52 struct flchip *chips, struct cfi_private *cfi)
56 printk("Enter cfi_probe_chip.\n");
58 if ((base + 0) >= map->size) {
60 "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
61 (unsigned long)base, map->size -1);
64 if ((base + 0xff) >= map->size) {
66 "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
67 (unsigned long)base + 0x55, map->size -1);
70 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
71 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
73 if (!qry_present(map,base,cfi))
77 /* This is the first time we're called. Set up the CFI
78 stuff accordingly and return */
79 return cfi_chip_setup(map, cfi);
82 /* Check each previous chip to see if it's an alias */
83 for (i=0; i<cfi->numchips; i++) {
84 /* This chip should be in read mode if it's one
85 we've already touched. */
86 if (qry_present(map,chips[i].start,cfi)) {
87 /* Eep. This chip also had the QRY marker.
88 * Is it an alias for the new one? */
89 cfi_send_gen_cmd(0xF0, 0, chips[i].start, map, cfi, cfi->device_type, NULL);
91 /* If the QRY marker goes away, it's an alias */
92 if (!qry_present(map, chips[i].start, cfi)) {
93 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
94 map->name, base, chips[i].start);
97 /* Yes, it's actually got QRY for data. Most
98 * unfortunate. Stick the new chip in read mode
99 * too and if it's the same, assume it's an alias. */
100 /* FIXME: Use other modes to do a proper check */
101 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
103 if (qry_present(map, base, cfi)) {
104 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
105 map->name, base, chips[i].start);
111 /* OK, if we got to here, then none of the previous chips appear to
112 be aliases for the current one. */
113 if (cfi->numchips == MAX_CFI_CHIPS) {
114 printk(KERN_WARNING"%s: Too many flash chips detected. Increase MAX_CFI_CHIPS from %d.\n", map->name, MAX_CFI_CHIPS);
115 /* Doesn't matter about resetting it to Read Mode - we're not going to talk to it anyway */
118 chips[cfi->numchips].start = base;
119 chips[cfi->numchips].state = FL_READY;
122 /* Put it back into Read Mode */
123 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
125 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit mode\n",
126 map->name, cfi->interleave, cfi->device_type*8, base,
132 static int cfi_chip_setup(struct map_info *map,
133 struct cfi_private *cfi)
135 int ofs_factor = cfi->interleave*cfi->device_type;
137 int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
141 printk("Number of erase regions: %d\n", num_erase_regions);
143 if (!num_erase_regions)
146 cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
148 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
152 memset(cfi->cfiq,0,sizeof(struct cfi_ident));
154 cfi->cfi_mode = CFI_MODE_CFI;
155 cfi->fast_prog=1; /* CFI supports fast programming */
157 /* Read the CFI info structure */
158 for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++) {
159 ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
162 /* Do any necessary byteswapping */
163 cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
165 cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
166 cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
167 cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
168 cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
169 cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
172 /* Dump the information therein */
173 print_cfi_ident(cfi->cfiq);
176 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
177 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
180 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
181 i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
182 (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
185 /* Put it back into Read Mode */
186 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
192 static char *vendorname(__u16 vendor)
199 return "Intel/Sharp Extended";
202 return "AMD/Fujitsu Standard";
205 return "Intel/Sharp Standard";
208 return "AMD/Fujitsu Extended";
210 case P_ID_MITSUBISHI_STD:
211 return "Mitsubishi Standard";
213 case P_ID_MITSUBISHI_EXT:
214 return "Mitsubishi Extended";
217 return "Not Allowed / Reserved for Future Use";
225 static void print_cfi_ident(struct cfi_ident *cfip)
228 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
229 printk("Invalid CFI ident structure.\n");
233 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
235 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
237 printk("No Primary Algorithm Table\n");
239 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
241 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
243 printk("No Alternate Algorithm Table\n");
246 printk("Vcc Minimum: %x.%x V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
247 printk("Vcc Maximum: %x.%x V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
249 printk("Vpp Minimum: %x.%x V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
250 printk("Vpp Maximum: %x.%x V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
253 printk("No Vpp line\n");
255 printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
256 printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
258 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
259 printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
260 printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
263 printk("Full buffer write not supported\n");
265 printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
266 printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
267 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
268 printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
269 printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
272 printk("Chip erase not supported\n");
274 printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
275 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
276 switch(cfip->InterfaceDesc) {
278 printk(" - x8-only asynchronous interface\n");
282 printk(" - x16-only asynchronous interface\n");
286 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
290 printk(" - x32-only asynchronous interface\n");
294 printk(" - Not Allowed / Reserved\n");
298 printk(" - Unknown\n");
302 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
303 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
306 #endif /* DEBUG_CFI */
308 static struct chip_probe cfi_chip_probe = {
310 probe_chip: cfi_probe_chip
313 struct mtd_info *cfi_probe(struct map_info *map)
316 * Just use the generic probe stuff to call our CFI-specific
317 * chip_probe routine in all the possible permutations, etc.
319 return mtd_do_chip_probe(map, &cfi_chip_probe);
322 static struct mtd_chip_driver cfi_chipdrv = {
328 int __init cfi_probe_init(void)
330 register_mtd_chip_driver(&cfi_chipdrv);
334 static void __exit cfi_probe_exit(void)
336 unregister_mtd_chip_driver(&cfi_chipdrv);
339 module_init(cfi_probe_init);
340 module_exit(cfi_probe_exit);
342 MODULE_LICENSE("GPL");
343 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
344 MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");