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