2 Common Flash Interface probe code.
3 (C) 2000 Red Hat. GPL'd.
4 $Id: cfi_probe.c,v 1.71 2003/05/28 12:51:48 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 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 if ((base + 0) >= map->size) {
58 "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
59 (unsigned long)base, map->size -1);
62 if ((base + 0xff) >= map->size) {
64 "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
65 (unsigned long)base + 0x55, map->size -1);
68 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
69 cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
71 if (!qry_present(map,base,cfi))
75 /* This is the first time we're called. Set up the CFI
76 stuff accordingly and return */
77 return cfi_chip_setup(map, cfi);
80 /* Check each previous chip to see if it's an alias */
81 for (i=0; i<cfi->numchips; i++) {
82 /* This chip should be in read mode if it's one
83 we've already touched. */
84 if (qry_present(map,chips[i].start,cfi)) {
85 /* Eep. This chip also had the QRY marker.
86 * Is it an alias for the new one? */
87 cfi_send_gen_cmd(0xF0, 0, chips[i].start, map, cfi, cfi->device_type, NULL);
89 /* If the QRY marker goes away, it's an alias */
90 if (!qry_present(map, chips[i].start, cfi)) {
91 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
92 map->name, base, chips[i].start);
95 /* Yes, it's actually got QRY for data. Most
96 * unfortunate. Stick the new chip in read mode
97 * too and if it's the same, assume it's an alias. */
98 /* FIXME: Use other modes to do a proper check */
99 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
101 if (qry_present(map, base, cfi)) {
102 printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
103 map->name, base, chips[i].start);
109 /* OK, if we got to here, then none of the previous chips appear to
110 be aliases for the current one. */
111 if (cfi->numchips == MAX_CFI_CHIPS) {
112 printk(KERN_WARNING"%s: Too many flash chips detected. Increase MAX_CFI_CHIPS from %d.\n", map->name, MAX_CFI_CHIPS);
113 /* Doesn't matter about resetting it to Read Mode - we're not going to talk to it anyway */
116 chips[cfi->numchips].start = base;
117 chips[cfi->numchips].state = FL_READY;
120 /* Put it back into Read Mode */
121 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
123 printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit mode\n",
124 map->name, cfi->interleave, cfi->device_type*8, base,
130 static int cfi_chip_setup(struct map_info *map,
131 struct cfi_private *cfi)
133 int ofs_factor = cfi->interleave*cfi->device_type;
135 int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
139 printk("Number of erase regions: %d\n", num_erase_regions);
141 if (!num_erase_regions)
144 cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
146 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
150 memset(cfi->cfiq,0,sizeof(struct cfi_ident));
152 cfi->cfi_mode = CFI_MODE_CFI;
153 cfi->fast_prog=1; /* CFI supports fast programming */
155 /* Read the CFI info structure */
156 for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++) {
157 ((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
160 /* Do any necessary byteswapping */
161 cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
163 cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
164 cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
165 cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
166 cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
167 cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
170 /* Dump the information therein */
171 print_cfi_ident(cfi->cfiq);
174 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
175 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
178 printk(" Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
179 i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
180 (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
183 /* Put it back into Read Mode */
184 cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
190 static char *vendorname(__u16 vendor)
197 return "Intel/Sharp Extended";
200 return "AMD/Fujitsu Standard";
203 return "Intel/Sharp Standard";
206 return "AMD/Fujitsu Extended";
208 case P_ID_MITSUBISHI_STD:
209 return "Mitsubishi Standard";
211 case P_ID_MITSUBISHI_EXT:
212 return "Mitsubishi Extended";
215 return "Not Allowed / Reserved for Future Use";
223 static void print_cfi_ident(struct cfi_ident *cfip)
226 if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
227 printk("Invalid CFI ident structure.\n");
231 printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
233 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
235 printk("No Primary Algorithm Table\n");
237 printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
239 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
241 printk("No Alternate Algorithm Table\n");
244 printk("Vcc Minimum: %x.%x V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
245 printk("Vcc Maximum: %x.%x V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
247 printk("Vpp Minimum: %x.%x V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
248 printk("Vpp Maximum: %x.%x V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
251 printk("No Vpp line\n");
253 printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
254 printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
256 if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
257 printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
258 printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
261 printk("Full buffer write not supported\n");
263 printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
264 printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
265 if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
266 printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
267 printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
270 printk("Chip erase not supported\n");
272 printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
273 printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
274 switch(cfip->InterfaceDesc) {
276 printk(" - x8-only asynchronous interface\n");
280 printk(" - x16-only asynchronous interface\n");
284 printk(" - supports x8 and x16 via BYTE# with asynchronous interface\n");
288 printk(" - x32-only asynchronous interface\n");
292 printk(" - Not Allowed / Reserved\n");
296 printk(" - Unknown\n");
300 printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
301 printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
304 #endif /* DEBUG_CFI */
306 static struct chip_probe cfi_chip_probe = {
308 .probe_chip = cfi_probe_chip
311 struct mtd_info *cfi_probe(struct map_info *map)
314 * Just use the generic probe stuff to call our CFI-specific
315 * chip_probe routine in all the possible permutations, etc.
317 return mtd_do_chip_probe(map, &cfi_chip_probe);
320 static struct mtd_chip_driver cfi_chipdrv = {
323 .module = THIS_MODULE
326 int __init cfi_probe_init(void)
328 register_mtd_chip_driver(&cfi_chipdrv);
332 static void __exit cfi_probe_exit(void)
334 unregister_mtd_chip_driver(&cfi_chipdrv);
337 module_init(cfi_probe_init);
338 module_exit(cfi_probe_exit);
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
342 MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");