ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / chips / gen_probe.c
1 /*
2  * Routines common to all CFI-type probes.
3  * (C) 2001-2003 Red Hat, Inc.
4  * GPL'd
5  * $Id: gen_probe.c,v 1.13 2003/06/25 11:50:37 dwmw2 Exp $
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/map.h>
13 #include <linux/mtd/cfi.h>
14 #include <linux/mtd/gen_probe.h>
15
16 static struct mtd_info *check_cmd_set(struct map_info *, int);
17 static struct cfi_private *genprobe_ident_chips(struct map_info *map,
18                                                 struct chip_probe *cp);
19 static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
20                              struct cfi_private *cfi);
21
22 struct mtd_info *mtd_do_chip_probe(struct map_info *map, struct chip_probe *cp)
23 {
24         struct mtd_info *mtd = NULL;
25         struct cfi_private *cfi;
26
27         /* First probe the map to see if we have CFI stuff there. */
28         cfi = genprobe_ident_chips(map, cp);
29         
30         if (!cfi)
31                 return NULL;
32
33         map->fldrv_priv = cfi;
34         /* OK we liked it. Now find a driver for the command set it talks */
35
36         mtd = check_cmd_set(map, 1); /* First the primary cmdset */
37         if (!mtd)
38                 mtd = check_cmd_set(map, 0); /* Then the secondary */
39         
40         if (mtd)
41                 return mtd;
42
43         printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
44         
45         kfree(cfi->cfiq);
46         kfree(cfi);
47         map->fldrv_priv = NULL;
48         return NULL;
49 }
50 EXPORT_SYMBOL(mtd_do_chip_probe);
51
52
53 struct cfi_private *genprobe_ident_chips(struct map_info *map, struct chip_probe *cp)
54 {
55         unsigned long base=0;
56         struct cfi_private cfi;
57         struct cfi_private *retcfi;
58         struct flchip chip[MAX_CFI_CHIPS];
59         int i;
60
61         memset(&cfi, 0, sizeof(cfi));
62         memset(&chip[0], 0, sizeof(chip));
63
64         /* Call the probetype-specific code with all permutations of 
65            interleave and device type, etc. */
66         if (!genprobe_new_chip(map, cp, &cfi)) {
67                 /* The probe didn't like it */
68                 printk(KERN_WARNING "%s: Found no %s device at location zero\n",
69                        cp->name, map->name);
70                 return NULL;
71         }               
72
73 #if 0 /* Let the CFI probe routine do this sanity check. The Intel and AMD
74          probe routines won't ever return a broken CFI structure anyway,
75          because they make them up themselves.
76       */
77         if (cfi.cfiq->NumEraseRegions == 0) {
78                 printk(KERN_WARNING "Number of erase regions is zero\n");
79                 kfree(cfi.cfiq);
80                 return NULL;
81         }
82 #endif
83         chip[0].start = 0;
84         chip[0].state = FL_READY;
85         cfi.chipshift = cfi.cfiq->DevSize;
86
87         switch(cfi.interleave) {
88 #ifdef CFIDEV_INTERLEAVE_1
89         case 1:
90                 break;
91 #endif
92 #ifdef CFIDEV_INTERLEAVE_2
93         case 2:
94                 cfi.chipshift++;
95                 break;
96 #endif
97 #ifdef CFIDEV_INTERLEAVE_4
98         case 4:
99                 cfi.chipshift+=2;
100                 break;
101 #endif
102         default:
103                 BUG();
104         }
105                 
106         cfi.numchips = 1;
107
108         /*
109          * Now probe for other chips, checking sensibly for aliases while
110          * we're at it. The new_chip probe above should have let the first
111          * chip in read mode.
112          *
113          * NOTE: Here, we're checking if there is room for another chip
114          *       the same size within the mapping. Therefore, 
115          *       base + chipsize <= map->size is the correct thing to do, 
116          *       because, base + chipsize would be the  _first_ byte of the
117          *       next chip, not the one we're currently pondering.
118          */
119
120         for (base = (1<<cfi.chipshift); base + (1<<cfi.chipshift) <= map->size;
121              base += (1<<cfi.chipshift))
122                 cp->probe_chip(map, base, &chip[0], &cfi);
123
124         /*
125          * Now allocate the space for the structures we need to return to 
126          * our caller, and copy the appropriate data into them.
127          */
128
129         retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
130
131         if (!retcfi) {
132                 printk(KERN_WARNING "%s: kmalloc failed for CFI private structure\n", map->name);
133                 kfree(cfi.cfiq);
134                 return NULL;
135         }
136
137         memcpy(retcfi, &cfi, sizeof(cfi));
138         memcpy(&retcfi->chips[0], chip, sizeof(struct flchip) * cfi.numchips);
139
140         /* Fix up the stuff that breaks when you move it */
141         for (i=0; i< retcfi->numchips; i++) {
142                 init_waitqueue_head(&retcfi->chips[i].wq);
143                 spin_lock_init(&retcfi->chips[i]._spinlock);
144                 retcfi->chips[i].mutex = &retcfi->chips[i]._spinlock;
145         }
146
147         return retcfi;
148 }
149
150         
151 static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
152                              struct cfi_private *cfi)
153 {
154         switch (map->buswidth) {
155 #ifdef CFIDEV_BUSWIDTH_1                
156         case CFIDEV_BUSWIDTH_1:
157                 cfi->interleave = CFIDEV_INTERLEAVE_1;
158
159                 cfi->device_type = CFI_DEVICETYPE_X8;
160                 if (cp->probe_chip(map, 0, NULL, cfi))
161                         return 1;
162
163                 cfi->device_type = CFI_DEVICETYPE_X16;
164                 if (cp->probe_chip(map, 0, NULL, cfi))
165                         return 1;
166                 break;                  
167 #endif /* CFIDEV_BUSWITDH_1 */
168
169 #ifdef CFIDEV_BUSWIDTH_2                
170         case CFIDEV_BUSWIDTH_2:
171 #ifdef CFIDEV_INTERLEAVE_1
172                 cfi->interleave = CFIDEV_INTERLEAVE_1;
173
174                 cfi->device_type = CFI_DEVICETYPE_X16;
175                 if (cp->probe_chip(map, 0, NULL, cfi))
176                         return 1;
177 #endif /* CFIDEV_INTERLEAVE_1 */
178 #ifdef CFIDEV_INTERLEAVE_2
179                 cfi->interleave = CFIDEV_INTERLEAVE_2;
180
181                 cfi->device_type = CFI_DEVICETYPE_X8;
182                 if (cp->probe_chip(map, 0, NULL, cfi))
183                         return 1;
184
185                 cfi->device_type = CFI_DEVICETYPE_X16;
186                 if (cp->probe_chip(map, 0, NULL, cfi))
187                         return 1;
188 #endif /* CFIDEV_INTERLEAVE_2 */
189                 break;                  
190 #endif /* CFIDEV_BUSWIDTH_2 */
191
192 #ifdef CFIDEV_BUSWIDTH_4
193         case CFIDEV_BUSWIDTH_4:
194 #if defined(CFIDEV_INTERLEAVE_1) && defined(SOMEONE_ACTUALLY_MAKES_THESE)
195                 cfi->interleave = CFIDEV_INTERLEAVE_1;
196
197                 cfi->device_type = CFI_DEVICETYPE_X32;
198                 if (cp->probe_chip(map, 0, NULL, cfi))
199                         return 1;
200 #endif /* CFIDEV_INTERLEAVE_1 */
201 #ifdef CFIDEV_INTERLEAVE_2
202                 cfi->interleave = CFIDEV_INTERLEAVE_2;
203
204 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
205                 cfi->device_type = CFI_DEVICETYPE_X32;
206                 if (cp->probe_chip(map, 0, NULL, cfi))
207                         return 1;
208 #endif
209                 cfi->device_type = CFI_DEVICETYPE_X16;
210                 if (cp->probe_chip(map, 0, NULL, cfi))
211                         return 1;
212
213                 cfi->device_type = CFI_DEVICETYPE_X8;
214                 if (cp->probe_chip(map, 0, NULL, cfi))
215                         return 1;
216 #endif /* CFIDEV_INTERLEAVE_2 */
217 #ifdef CFIDEV_INTERLEAVE_4
218                 cfi->interleave = CFIDEV_INTERLEAVE_4;
219
220 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
221                 cfi->device_type = CFI_DEVICETYPE_X32;
222                 if (cp->probe_chip(map, 0, NULL, cfi))
223                         return 1;
224 #endif
225                 cfi->device_type = CFI_DEVICETYPE_X16;
226                 if (cp->probe_chip(map, 0, NULL, cfi))
227                         return 1;
228
229                 cfi->device_type = CFI_DEVICETYPE_X8;
230                 if (cp->probe_chip(map, 0, NULL, cfi))
231                         return 1;
232 #endif /* CFIDEV_INTERLEAVE_4 */
233                 break;
234 #endif /* CFIDEV_BUSWIDTH_4 */
235
236 #ifdef CFIDEV_BUSWIDTH_8
237         case CFIDEV_BUSWIDTH_8:
238 #if defined(CFIDEV_INTERLEAVE_2) && defined(SOMEONE_ACTUALLY_MAKES_THESE)
239                 cfi->interleave = CFIDEV_INTERLEAVE_2;
240
241                 cfi->device_type = CFI_DEVICETYPE_X32;
242                 if (cp->probe_chip(map, 0, NULL, cfi))
243                         return 1;
244 #endif /* CFIDEV_INTERLEAVE_2 */
245 #ifdef CFIDEV_INTERLEAVE_4
246                 cfi->interleave = CFIDEV_INTERLEAVE_4;
247
248 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
249                 cfi->device_type = CFI_DEVICETYPE_X32;
250                 if (cp->probe_chip(map, 0, NULL, cfi))
251                         return 1;
252 #endif
253                 cfi->device_type = CFI_DEVICETYPE_X16;
254                 if (cp->probe_chip(map, 0, NULL, cfi))
255                         return 1;
256 #endif /* CFIDEV_INTERLEAVE_4 */
257 #ifdef CFIDEV_INTERLEAVE_8
258                 cfi->interleave = CFIDEV_INTERLEAVE_8;
259
260                 cfi->device_type = CFI_DEVICETYPE_X16;
261                 if (cp->probe_chip(map, 0, NULL, cfi))
262                         return 1;
263
264                 cfi->device_type = CFI_DEVICETYPE_X8;
265                 if (cp->probe_chip(map, 0, NULL, cfi))
266                         return 1;
267 #endif /* CFIDEV_INTERLEAVE_8 */
268                 break;
269 #endif /* CFIDEV_BUSWIDTH_8 */
270
271         default:
272                 printk(KERN_WARNING "genprobe_new_chip called with unsupported buswidth %d\n", map->buswidth);
273                 return 0;
274         }
275         return 0;
276 }
277
278
279 typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);
280
281 extern cfi_cmdset_fn_t cfi_cmdset_0001;
282 extern cfi_cmdset_fn_t cfi_cmdset_0002;
283 extern cfi_cmdset_fn_t cfi_cmdset_0020;
284
285 static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map, 
286                                                   int primary)
287 {
288         struct cfi_private *cfi = map->fldrv_priv;
289         __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
290 #if defined(CONFIG_MODULES) && defined(HAVE_INTER_MODULE)
291         char probename[32];
292         cfi_cmdset_fn_t *probe_function;
293
294         sprintf(probename, "cfi_cmdset_%4.4X", type);
295                 
296         probe_function = inter_module_get_request(probename, probename);
297
298         if (probe_function) {
299                 struct mtd_info *mtd;
300
301                 mtd = (*probe_function)(map, primary);
302                 /* If it was happy, it'll have increased its own use count */
303                 inter_module_put(probename);
304                 return mtd;
305         }
306 #endif
307         printk(KERN_NOTICE "Support for command set %04X not present\n",
308                type);
309
310         return NULL;
311 }
312
313 static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
314 {
315         struct cfi_private *cfi = map->fldrv_priv;
316         __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
317         
318         if (type == P_ID_NONE || type == P_ID_RESERVED)
319                 return NULL;
320
321         switch(type){
322                 /* Urgh. Ifdefs. The version with weak symbols was
323                  * _much_ nicer. Shame it didn't seem to work on
324                  * anything but x86, really.
325                  * But we can't rely in inter_module_get() because
326                  * that'd mean we depend on link order.
327                  */
328 #ifdef CONFIG_MTD_CFI_INTELEXT
329         case 0x0001:
330         case 0x0003:
331                 return cfi_cmdset_0001(map, primary);
332 #endif
333 #ifdef CONFIG_MTD_CFI_AMDSTD
334         case 0x0002:
335                 return cfi_cmdset_0002(map, primary);
336 #endif
337 #ifdef CONFIG_MTD_CFI_STAA
338         case 0x0020:
339                 return cfi_cmdset_0020(map, primary);
340 #endif
341         }
342
343         return cfi_cmdset_unknown(map, primary);
344 }
345
346 MODULE_LICENSE("GPL");
347 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
348 MODULE_DESCRIPTION("Helper routines for flash chip probe code");