2 * Routines common to all CFI-type probes.
3 * (C) 2001, 2001 Red Hat, Inc.
5 * $Id: gen_probe.c,v 1.1.1.1 2005/04/11 02:50:25 jack Exp $
8 #include <linux/kernel.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/map.h>
11 #include <linux/mtd/cfi.h>
12 #include <linux/mtd/gen_probe.h>
14 static struct mtd_info *check_cmd_set(struct map_info *, int);
15 static struct cfi_private *genprobe_ident_chips(struct map_info *map,
16 struct chip_probe *cp);
17 static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
18 struct cfi_private *cfi);
20 struct mtd_info *mtd_do_chip_probe(struct map_info *map, struct chip_probe *cp)
22 struct mtd_info *mtd = NULL;
23 struct cfi_private *cfi;
25 /* First probe the map to see if we have CFI stuff there. */
26 cfi = genprobe_ident_chips(map, cp);
31 map->fldrv_priv = cfi;
32 /* OK we liked it. Now find a driver for the command set it talks */
34 mtd = check_cmd_set(map, 1); /* First the primary cmdset */
36 mtd = check_cmd_set(map, 0); /* Then the secondary */
41 printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
45 map->fldrv_priv = NULL;
48 EXPORT_SYMBOL(mtd_do_chip_probe);
51 struct cfi_private *genprobe_ident_chips(struct map_info *map, struct chip_probe *cp)
54 struct cfi_private cfi;
55 struct cfi_private *retcfi;
56 struct flchip chip[MAX_CFI_CHIPS];
59 memset(&cfi, 0, sizeof(cfi));
61 /* Call the probetype-specific code with all permutations of
62 interleave and device type, etc. */
63 if (!genprobe_new_chip(map, cp, &cfi)) {
64 /* The probe didn't like it */
65 printk(KERN_WARNING "%s: Found no %s device at location zero\n",
70 #if 0 /* Let the CFI probe routine do this sanity check. The Intel and AMD
71 probe routines won't ever return a broken CFI structure anyway,
72 because they make them up themselves.
74 if (cfi.cfiq->NumEraseRegions == 0) {
75 printk(KERN_WARNING "Number of erase regions is zero\n");
81 chip[0].state = FL_READY;
82 cfi.chipshift = cfi.cfiq->DevSize;
84 switch(cfi.interleave) {
85 #ifdef CFIDEV_INTERLEAVE_1
89 #ifdef CFIDEV_INTERLEAVE_2
94 #ifdef CFIDEV_INTERLEAVE_4
106 * Now probe for other chips, checking sensibly for aliases while
107 * we're at it. The new_chip probe above should have let the first
110 * NOTE: Here, we're checking if there is room for another chip
111 * the same size within the mapping. Therefore,
112 * base + chipsize <= map->size is the correct thing to do,
113 * because, base + chipsize would be the _first_ byte of the
114 * next chip, not the one we're currently pondering.
117 for (base = (1<<cfi.chipshift); base + (1<<cfi.chipshift) <= map->size;
118 base += (1<<cfi.chipshift))
119 cp->probe_chip(map, base, &chip[0], &cfi);
122 * Now allocate the space for the structures we need to return to
123 * our caller, and copy the appropriate data into them.
126 retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
129 printk(KERN_WARNING "%s: kmalloc failed for CFI private structure\n", map->name);
134 memcpy(retcfi, &cfi, sizeof(cfi));
135 memcpy(&retcfi->chips[0], chip, sizeof(struct flchip) * cfi.numchips);
137 /* Fix up the stuff that breaks when you move it */
138 for (i=0; i< retcfi->numchips; i++) {
139 init_waitqueue_head(&retcfi->chips[i].wq);
140 spin_lock_init(&retcfi->chips[i]._spinlock);
141 retcfi->chips[i].mutex = &retcfi->chips[i]._spinlock;
148 static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
149 struct cfi_private *cfi)
151 switch (map->buswidth) {
152 #ifdef CFIDEV_BUSWIDTH_1
153 case CFIDEV_BUSWIDTH_1:
154 cfi->interleave = CFIDEV_INTERLEAVE_1;
156 cfi->device_type = CFI_DEVICETYPE_X8;
157 if (cp->probe_chip(map, 0, NULL, cfi))
160 cfi->device_type = CFI_DEVICETYPE_X16;
161 if (cp->probe_chip(map, 0, NULL, cfi))
164 #endif /* CFIDEV_BUSWITDH_1 */
166 #ifdef CFIDEV_BUSWIDTH_2
167 case CFIDEV_BUSWIDTH_2:
168 #ifdef CFIDEV_INTERLEAVE_1
169 cfi->interleave = CFIDEV_INTERLEAVE_1;
171 cfi->device_type = CFI_DEVICETYPE_X16;
172 if (cp->probe_chip(map, 0, NULL, cfi))
174 #endif /* CFIDEV_INTERLEAVE_1 */
175 #ifdef CFIDEV_INTERLEAVE_2
176 cfi->interleave = CFIDEV_INTERLEAVE_2;
178 cfi->device_type = CFI_DEVICETYPE_X8;
179 if (cp->probe_chip(map, 0, NULL, cfi))
182 cfi->device_type = CFI_DEVICETYPE_X16;
183 if (cp->probe_chip(map, 0, NULL, cfi))
185 #endif /* CFIDEV_INTERLEAVE_2 */
187 #endif /* CFIDEV_BUSWIDTH_2 */
189 #ifdef CFIDEV_BUSWIDTH_4
190 case CFIDEV_BUSWIDTH_4:
191 #if defined(CFIDEV_INTERLEAVE_1) && defined(SOMEONE_ACTUALLY_MAKES_THESE)
192 cfi->interleave = CFIDEV_INTERLEAVE_1;
194 cfi->device_type = CFI_DEVICETYPE_X32;
195 if (cp->probe_chip(map, 0, NULL, cfi))
197 #endif /* CFIDEV_INTERLEAVE_1 */
198 #ifdef CFIDEV_INTERLEAVE_2
199 cfi->interleave = CFIDEV_INTERLEAVE_2;
201 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
202 cfi->device_type = CFI_DEVICETYPE_X32;
203 if (cp->probe_chip(map, 0, NULL, cfi))
206 cfi->device_type = CFI_DEVICETYPE_X16;
207 if (cp->probe_chip(map, 0, NULL, cfi))
210 cfi->device_type = CFI_DEVICETYPE_X8;
211 if (cp->probe_chip(map, 0, NULL, cfi))
213 #endif /* CFIDEV_INTERLEAVE_2 */
214 #ifdef CFIDEV_INTERLEAVE_4
215 cfi->interleave = CFIDEV_INTERLEAVE_4;
217 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
218 cfi->device_type = CFI_DEVICETYPE_X32;
219 if (cp->probe_chip(map, 0, NULL, cfi))
222 cfi->device_type = CFI_DEVICETYPE_X16;
223 if (cp->probe_chip(map, 0, NULL, cfi))
226 cfi->device_type = CFI_DEVICETYPE_X8;
227 if (cp->probe_chip(map, 0, NULL, cfi))
229 #endif /* CFIDEV_INTERLEAVE_4 */
231 #endif /* CFIDEV_BUSWIDTH_4 */
233 #ifdef CFIDEV_BUSWIDTH_8
234 case CFIDEV_BUSWIDTH_8:
235 #if defined(CFIDEV_INTERLEAVE_2) && defined(SOMEONE_ACTUALLY_MAKES_THESE)
236 cfi->interleave = CFIDEV_INTERLEAVE_2;
238 cfi->device_type = CFI_DEVICETYPE_X32;
239 if (cp->probe_chip(map, 0, NULL, cfi))
241 #endif /* CFIDEV_INTERLEAVE_2 */
242 #ifdef CFIDEV_INTERLEAVE_4
243 cfi->interleave = CFIDEV_INTERLEAVE_4;
245 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
246 cfi->device_type = CFI_DEVICETYPE_X32;
247 if (cp->probe_chip(map, 0, NULL, cfi))
250 cfi->device_type = CFI_DEVICETYPE_X16;
251 if (cp->probe_chip(map, 0, NULL, cfi))
253 #endif /* CFIDEV_INTERLEAVE_4 */
254 #ifdef CFIDEV_INTERLEAVE_8
255 cfi->interleave = CFIDEV_INTERLEAVE_8;
257 cfi->device_type = CFI_DEVICETYPE_X16;
258 if (cp->probe_chip(map, 0, NULL, cfi))
261 cfi->device_type = CFI_DEVICETYPE_X8;
262 if (cp->probe_chip(map, 0, NULL, cfi))
264 #endif /* CFIDEV_INTERLEAVE_8 */
266 #endif /* CFIDEV_BUSWIDTH_8 */
269 printk(KERN_WARNING "genprobe_new_chip called with unsupported buswidth %d\n", map->buswidth);
276 typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);
278 extern cfi_cmdset_fn_t cfi_cmdset_0001;
279 extern cfi_cmdset_fn_t cfi_cmdset_0002;
281 static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map,
284 struct cfi_private *cfi = map->fldrv_priv;
285 __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
286 #if defined(CONFIG_MODULES) && defined(HAVE_INTER_MODULE)
288 cfi_cmdset_fn_t *probe_function;
290 sprintf(probename, "cfi_cmdset_%4.4X", type);
292 probe_function = inter_module_get_request(probename, probename);
294 if (probe_function) {
295 struct mtd_info *mtd;
297 mtd = (*probe_function)(map, primary);
298 /* If it was happy, it'll have increased its own use count */
299 inter_module_put(probename);
303 printk(KERN_NOTICE "Support for command set %04X not present\n",
309 static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
311 struct cfi_private *cfi = map->fldrv_priv;
312 __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
314 if (type == P_ID_NONE || type == P_ID_RESERVED)
318 /* Urgh. Ifdefs. The version with weak symbols was
319 * _much_ nicer. Shame it didn't seem to work on
320 * anything but x86, really.
321 * But we can't rely in inter_module_get() because
322 * that'd mean we depend on link order.
324 #ifdef CONFIG_MTD_CFI_INTELEXT
327 return cfi_cmdset_0001(map, primary);
329 #ifdef CONFIG_MTD_CFI_AMDSTD
331 return cfi_cmdset_0002(map, primary);
333 #ifdef CONFIG_MTD_CFI_STAA
335 return cfi_cmdset_0020(map, primary);
339 return cfi_cmdset_unknown(map, primary);
342 MODULE_LICENSE("GPL");
343 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
344 MODULE_DESCRIPTION("Helper routines for flash chip probe code");