cleanup
[linux-2.4.21-pre4.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.1.1.1 2005/04/11 02:50:25 jack 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 <asm/io.h>
12 #include <asm/byteorder.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16
17 #include <linux/mtd/map.h>
18 #include <linux/mtd/cfi.h>
19 #include <linux/mtd/gen_probe.h>
20
21 //REX:
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 #ifdef DEBUG_CFI
56         printk("Enter cfi_probe_chip.\n");
57 #endif
58         if ((base + 0) >= map->size) {
59                 printk(KERN_NOTICE
60                         "Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
61                         (unsigned long)base, map->size -1);
62                 return 0;
63         }
64         if ((base + 0xff) >= map->size) {
65                 printk(KERN_NOTICE
66                         "Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
67                         (unsigned long)base + 0x55, map->size -1);
68                 return 0;
69         }
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);
72
73         if (!qry_present(map,base,cfi))
74                 return 0;
75
76         if (!cfi->numchips) {
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);
80         }
81
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);
90
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);
95                                 return 0;
96                         }
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);
102                         
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);
106                                 return 0;
107                         }
108                 }
109         }
110         
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 */
116                 return -1;
117         }
118         chips[cfi->numchips].start = base;
119         chips[cfi->numchips].state = FL_READY;
120         cfi->numchips++;
121         
122         /* Put it back into Read Mode */
123         cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
124
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,
127                map->buswidth*8);
128         
129         return 1;
130 }
131
132 static int cfi_chip_setup(struct map_info *map, 
133                    struct cfi_private *cfi)
134 {
135         int ofs_factor = cfi->interleave*cfi->device_type;
136         __u32 base = 0;
137         int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
138         int i;
139
140 #ifdef DEBUG_CFI
141         printk("Number of erase regions: %d\n", num_erase_regions);
142 #endif
143         if (!num_erase_regions)
144                 return 0;
145
146         cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
147         if (!cfi->cfiq) {
148                 printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
149                 return 0;
150         }
151         
152         memset(cfi->cfiq,0,sizeof(struct cfi_ident));   
153         
154         cfi->cfi_mode = CFI_MODE_CFI;
155         cfi->fast_prog=1;               /* CFI supports fast programming */
156         
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);
160         }
161         
162         /* Do any necessary byteswapping */
163         cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
164         
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);
170
171 #ifdef DEBUG_CFI
172         /* Dump the information therein */
173         print_cfi_ident(cfi->cfiq);
174 #endif
175
176         for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
177                 cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
178                 
179 #ifdef DEBUG_CFI                
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);
183 #endif
184         }
185         /* Put it back into Read Mode */
186         cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
187
188         return 1;
189 }
190
191 #ifdef DEBUG_CFI
192 static char *vendorname(__u16 vendor) 
193 {
194         switch (vendor) {
195         case P_ID_NONE:
196                 return "None";
197                 
198         case P_ID_INTEL_EXT:
199                 return "Intel/Sharp Extended";
200                 
201         case P_ID_AMD_STD:
202                 return "AMD/Fujitsu Standard";
203                 
204         case P_ID_INTEL_STD:
205                 return "Intel/Sharp Standard";
206                 
207         case P_ID_AMD_EXT:
208                 return "AMD/Fujitsu Extended";
209                 
210         case P_ID_MITSUBISHI_STD:
211                 return "Mitsubishi Standard";
212                 
213         case P_ID_MITSUBISHI_EXT:
214                 return "Mitsubishi Extended";
215                 
216         case P_ID_RESERVED:
217                 return "Not Allowed / Reserved for Future Use";
218                 
219         default:
220                 return "Unknown";
221         }
222 }
223
224
225 static void print_cfi_ident(struct cfi_ident *cfip)
226 {
227 #if 0
228         if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
229                 printk("Invalid CFI ident structure.\n");
230                 return;
231         }       
232 #endif          
233         printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
234         if (cfip->P_ADR)
235                 printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
236         else
237                 printk("No Primary Algorithm Table\n");
238         
239         printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
240         if (cfip->A_ADR)
241                 printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
242         else
243                 printk("No Alternate Algorithm Table\n");
244                 
245                 
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);
248         if (cfip->VppMin) {
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);
251         }
252         else
253                 printk("No Vpp line\n");
254         
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));
257         
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));
261         }
262         else
263                 printk("Full buffer write not supported\n");
264         
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));
270         }
271         else
272                 printk("Chip erase not supported\n");
273         
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) {
277         case 0:
278                 printk("  - x8-only asynchronous interface\n");
279                 break;
280                 
281         case 1:
282                 printk("  - x16-only asynchronous interface\n");
283                 break;
284                 
285         case 2:
286                 printk("  - supports x8 and x16 via BYTE# with asynchronous interface\n");
287                 break;
288                 
289         case 3:
290                 printk("  - x32-only asynchronous interface\n");
291                 break;
292                 
293         case 65535:
294                 printk("  - Not Allowed / Reserved\n");
295                 break;
296                 
297         default:
298                 printk("  - Unknown\n");
299                 break;
300         }
301         
302         printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
303         printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
304         
305 }
306 #endif /* DEBUG_CFI */
307
308 static struct chip_probe cfi_chip_probe = {
309         name: "CFI",
310         probe_chip: cfi_probe_chip
311 };
312
313 struct mtd_info *cfi_probe(struct map_info *map)
314 {
315         /*
316          * Just use the generic probe stuff to call our CFI-specific
317          * chip_probe routine in all the possible permutations, etc.
318          */
319         return mtd_do_chip_probe(map, &cfi_chip_probe);
320 }
321
322 static struct mtd_chip_driver cfi_chipdrv = {
323         probe: cfi_probe,
324         name: "cfi_probe",
325         module: THIS_MODULE
326 };
327
328 int __init cfi_probe_init(void)
329 {
330         register_mtd_chip_driver(&cfi_chipdrv);
331         return 0;
332 }
333
334 static void __exit cfi_probe_exit(void)
335 {
336         unregister_mtd_chip_driver(&cfi_chipdrv);
337 }
338
339 module_init(cfi_probe_init);
340 module_exit(cfi_probe_exit);
341
342 MODULE_LICENSE("GPL");
343 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
344 MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");