ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / chips / cfi_probe.c
1 /* 
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 $
5 */
6
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>
12 #include <asm/io.h>
13 #include <asm/byteorder.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17
18 #include <linux/mtd/map.h>
19 #include <linux/mtd/cfi.h>
20 #include <linux/mtd/gen_probe.h>
21
22 //#define DEBUG_CFI 
23
24 #ifdef DEBUG_CFI
25 static void print_cfi_ident(struct cfi_ident *);
26 #endif
27
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);
31
32 struct mtd_info *cfi_probe(struct map_info *map);
33
34 /* check for QRY.
35    in: interleave,type,mode
36    ret: table index, <0 for error
37  */
38 static inline int qry_present(struct map_info *map, __u32 base,
39                                 struct cfi_private *cfi)
40 {
41         int osf = cfi->interleave * cfi->device_type;   // scale factor
42
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))
46                 return 1;       // ok !
47
48         return 0;       // nothing found
49 }
50
51 static int cfi_probe_chip(struct map_info *map, __u32 base,
52                           struct flchip *chips, struct cfi_private *cfi)
53 {
54         int i;
55         
56         if ((base + 0) >= map->size) {
57                 printk(KERN_NOTICE
58                         "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
59                         (unsigned long)base, map->size -1);
60                 return 0;
61         }
62         if ((base + 0xff) >= map->size) {
63                 printk(KERN_NOTICE
64                         "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
65                         (unsigned long)base + 0x55, map->size -1);
66                 return 0;
67         }
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);
70
71         if (!qry_present(map,base,cfi))
72                 return 0;
73
74         if (!cfi->numchips) {
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);
78         }
79
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);
88
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);
93                                 return 0;
94                         }
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);
100                         
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);
104                                 return 0;
105                         }
106                 }
107         }
108         
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 */
114                 return -1;
115         }
116         chips[cfi->numchips].start = base;
117         chips[cfi->numchips].state = FL_READY;
118         cfi->numchips++;
119         
120         /* Put it back into Read Mode */
121         cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
122
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,
125                map->buswidth*8);
126         
127         return 1;
128 }
129
130 static int cfi_chip_setup(struct map_info *map, 
131                    struct cfi_private *cfi)
132 {
133         int ofs_factor = cfi->interleave*cfi->device_type;
134         __u32 base = 0;
135         int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
136         int i;
137
138 #ifdef DEBUG_CFI
139         printk("Number of erase regions: %d\n", num_erase_regions);
140 #endif
141         if (!num_erase_regions)
142                 return 0;
143
144         cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
145         if (!cfi->cfiq) {
146                 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
147                 return 0;
148         }
149         
150         memset(cfi->cfiq,0,sizeof(struct cfi_ident));   
151         
152         cfi->cfi_mode = CFI_MODE_CFI;
153         cfi->fast_prog=1;               /* CFI supports fast programming */
154         
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);
158         }
159         
160         /* Do any necessary byteswapping */
161         cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
162         
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);
168
169 #ifdef DEBUG_CFI
170         /* Dump the information therein */
171         print_cfi_ident(cfi->cfiq);
172 #endif
173
174         for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
175                 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
176                 
177 #ifdef DEBUG_CFI                
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);
181 #endif
182         }
183         /* Put it back into Read Mode */
184         cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
185
186         return 1;
187 }
188
189 #ifdef DEBUG_CFI
190 static char *vendorname(__u16 vendor) 
191 {
192         switch (vendor) {
193         case P_ID_NONE:
194                 return "None";
195                 
196         case P_ID_INTEL_EXT:
197                 return "Intel/Sharp Extended";
198                 
199         case P_ID_AMD_STD:
200                 return "AMD/Fujitsu Standard";
201                 
202         case P_ID_INTEL_STD:
203                 return "Intel/Sharp Standard";
204                 
205         case P_ID_AMD_EXT:
206                 return "AMD/Fujitsu Extended";
207                 
208         case P_ID_MITSUBISHI_STD:
209                 return "Mitsubishi Standard";
210                 
211         case P_ID_MITSUBISHI_EXT:
212                 return "Mitsubishi Extended";
213                 
214         case P_ID_RESERVED:
215                 return "Not Allowed / Reserved for Future Use";
216                 
217         default:
218                 return "Unknown";
219         }
220 }
221
222
223 static void print_cfi_ident(struct cfi_ident *cfip)
224 {
225 #if 0
226         if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
227                 printk("Invalid CFI ident structure.\n");
228                 return;
229         }       
230 #endif          
231         printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
232         if (cfip->P_ADR)
233                 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
234         else
235                 printk("No Primary Algorithm Table\n");
236         
237         printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
238         if (cfip->A_ADR)
239                 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
240         else
241                 printk("No Alternate Algorithm Table\n");
242                 
243                 
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);
246         if (cfip->VppMin) {
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);
249         }
250         else
251                 printk("No Vpp line\n");
252         
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));
255         
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));
259         }
260         else
261                 printk("Full buffer write not supported\n");
262         
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));
268         }
269         else
270                 printk("Chip erase not supported\n");
271         
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) {
275         case 0:
276                 printk("  - x8-only asynchronous interface\n");
277                 break;
278                 
279         case 1:
280                 printk("  - x16-only asynchronous interface\n");
281                 break;
282                 
283         case 2:
284                 printk("  - supports x8 and x16 via BYTE# with asynchronous interface\n");
285                 break;
286                 
287         case 3:
288                 printk("  - x32-only asynchronous interface\n");
289                 break;
290                 
291         case 65535:
292                 printk("  - Not Allowed / Reserved\n");
293                 break;
294                 
295         default:
296                 printk("  - Unknown\n");
297                 break;
298         }
299         
300         printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
301         printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
302         
303 }
304 #endif /* DEBUG_CFI */
305
306 static struct chip_probe cfi_chip_probe = {
307         .name           = "CFI",
308         .probe_chip     = cfi_probe_chip
309 };
310
311 struct mtd_info *cfi_probe(struct map_info *map)
312 {
313         /*
314          * Just use the generic probe stuff to call our CFI-specific
315          * chip_probe routine in all the possible permutations, etc.
316          */
317         return mtd_do_chip_probe(map, &cfi_chip_probe);
318 }
319
320 static struct mtd_chip_driver cfi_chipdrv = {
321         .probe          = cfi_probe,
322         .name           = "cfi_probe",
323         .module         = THIS_MODULE
324 };
325
326 int __init cfi_probe_init(void)
327 {
328         register_mtd_chip_driver(&cfi_chipdrv);
329         return 0;
330 }
331
332 static void __exit cfi_probe_exit(void)
333 {
334         unregister_mtd_chip_driver(&cfi_chipdrv);
335 }
336
337 module_init(cfi_probe_init);
338 module_exit(cfi_probe_exit);
339
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
342 MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");