2 * Routines common to all CFI-type probes.
3 * (C) 2001-2003 Red Hat, Inc.
5 * $Id: gen_probe.c,v 1.13 2003/06/25 11:50:37 dwmw2 Exp $
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>
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);
22 struct mtd_info *mtd_do_chip_probe(struct map_info *map, struct chip_probe *cp)
24 struct mtd_info *mtd = NULL;
25 struct cfi_private *cfi;
27 /* First probe the map to see if we have CFI stuff there. */
28 cfi = genprobe_ident_chips(map, cp);
33 map->fldrv_priv = cfi;
34 /* OK we liked it. Now find a driver for the command set it talks */
36 mtd = check_cmd_set(map, 1); /* First the primary cmdset */
38 mtd = check_cmd_set(map, 0); /* Then the secondary */
43 printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
47 map->fldrv_priv = NULL;
50 EXPORT_SYMBOL(mtd_do_chip_probe);
53 struct cfi_private *genprobe_ident_chips(struct map_info *map, struct chip_probe *cp)
56 struct cfi_private cfi;
57 struct cfi_private *retcfi;
58 struct flchip chip[MAX_CFI_CHIPS];
61 memset(&cfi, 0, sizeof(cfi));
62 memset(&chip[0], 0, sizeof(chip));
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",
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.
77 if (cfi.cfiq->NumEraseRegions == 0) {
78 printk(KERN_WARNING "Number of erase regions is zero\n");
84 chip[0].state = FL_READY;
85 cfi.chipshift = cfi.cfiq->DevSize;
87 switch(cfi.interleave) {
88 #ifdef CFIDEV_INTERLEAVE_1
92 #ifdef CFIDEV_INTERLEAVE_2
97 #ifdef CFIDEV_INTERLEAVE_4
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
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.
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);
125 * Now allocate the space for the structures we need to return to
126 * our caller, and copy the appropriate data into them.
129 retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);
132 printk(KERN_WARNING "%s: kmalloc failed for CFI private structure\n", map->name);
137 memcpy(retcfi, &cfi, sizeof(cfi));
138 memcpy(&retcfi->chips[0], chip, sizeof(struct flchip) * cfi.numchips);
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;
151 static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
152 struct cfi_private *cfi)
154 switch (map->buswidth) {
155 #ifdef CFIDEV_BUSWIDTH_1
156 case CFIDEV_BUSWIDTH_1:
157 cfi->interleave = CFIDEV_INTERLEAVE_1;
159 cfi->device_type = CFI_DEVICETYPE_X8;
160 if (cp->probe_chip(map, 0, NULL, cfi))
163 cfi->device_type = CFI_DEVICETYPE_X16;
164 if (cp->probe_chip(map, 0, NULL, cfi))
167 #endif /* CFIDEV_BUSWITDH_1 */
169 #ifdef CFIDEV_BUSWIDTH_2
170 case CFIDEV_BUSWIDTH_2:
171 #ifdef CFIDEV_INTERLEAVE_1
172 cfi->interleave = CFIDEV_INTERLEAVE_1;
174 cfi->device_type = CFI_DEVICETYPE_X16;
175 if (cp->probe_chip(map, 0, NULL, cfi))
177 #endif /* CFIDEV_INTERLEAVE_1 */
178 #ifdef CFIDEV_INTERLEAVE_2
179 cfi->interleave = CFIDEV_INTERLEAVE_2;
181 cfi->device_type = CFI_DEVICETYPE_X8;
182 if (cp->probe_chip(map, 0, NULL, cfi))
185 cfi->device_type = CFI_DEVICETYPE_X16;
186 if (cp->probe_chip(map, 0, NULL, cfi))
188 #endif /* CFIDEV_INTERLEAVE_2 */
190 #endif /* CFIDEV_BUSWIDTH_2 */
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;
197 cfi->device_type = CFI_DEVICETYPE_X32;
198 if (cp->probe_chip(map, 0, NULL, cfi))
200 #endif /* CFIDEV_INTERLEAVE_1 */
201 #ifdef CFIDEV_INTERLEAVE_2
202 cfi->interleave = CFIDEV_INTERLEAVE_2;
204 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
205 cfi->device_type = CFI_DEVICETYPE_X32;
206 if (cp->probe_chip(map, 0, NULL, cfi))
209 cfi->device_type = CFI_DEVICETYPE_X16;
210 if (cp->probe_chip(map, 0, NULL, cfi))
213 cfi->device_type = CFI_DEVICETYPE_X8;
214 if (cp->probe_chip(map, 0, NULL, cfi))
216 #endif /* CFIDEV_INTERLEAVE_2 */
217 #ifdef CFIDEV_INTERLEAVE_4
218 cfi->interleave = CFIDEV_INTERLEAVE_4;
220 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
221 cfi->device_type = CFI_DEVICETYPE_X32;
222 if (cp->probe_chip(map, 0, NULL, cfi))
225 cfi->device_type = CFI_DEVICETYPE_X16;
226 if (cp->probe_chip(map, 0, NULL, cfi))
229 cfi->device_type = CFI_DEVICETYPE_X8;
230 if (cp->probe_chip(map, 0, NULL, cfi))
232 #endif /* CFIDEV_INTERLEAVE_4 */
234 #endif /* CFIDEV_BUSWIDTH_4 */
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;
241 cfi->device_type = CFI_DEVICETYPE_X32;
242 if (cp->probe_chip(map, 0, NULL, cfi))
244 #endif /* CFIDEV_INTERLEAVE_2 */
245 #ifdef CFIDEV_INTERLEAVE_4
246 cfi->interleave = CFIDEV_INTERLEAVE_4;
248 #ifdef SOMEONE_ACTUALLY_MAKES_THESE
249 cfi->device_type = CFI_DEVICETYPE_X32;
250 if (cp->probe_chip(map, 0, NULL, cfi))
253 cfi->device_type = CFI_DEVICETYPE_X16;
254 if (cp->probe_chip(map, 0, NULL, cfi))
256 #endif /* CFIDEV_INTERLEAVE_4 */
257 #ifdef CFIDEV_INTERLEAVE_8
258 cfi->interleave = CFIDEV_INTERLEAVE_8;
260 cfi->device_type = CFI_DEVICETYPE_X16;
261 if (cp->probe_chip(map, 0, NULL, cfi))
264 cfi->device_type = CFI_DEVICETYPE_X8;
265 if (cp->probe_chip(map, 0, NULL, cfi))
267 #endif /* CFIDEV_INTERLEAVE_8 */
269 #endif /* CFIDEV_BUSWIDTH_8 */
272 printk(KERN_WARNING "genprobe_new_chip called with unsupported buswidth %d\n", map->buswidth);
279 typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);
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;
285 static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map,
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)
292 cfi_cmdset_fn_t *probe_function;
294 sprintf(probename, "cfi_cmdset_%4.4X", type);
296 probe_function = inter_module_get_request(probename, probename);
298 if (probe_function) {
299 struct mtd_info *mtd;
301 mtd = (*probe_function)(map, primary);
302 /* If it was happy, it'll have increased its own use count */
303 inter_module_put(probename);
307 printk(KERN_NOTICE "Support for command set %04X not present\n",
313 static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
315 struct cfi_private *cfi = map->fldrv_priv;
316 __u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
318 if (type == P_ID_NONE || type == P_ID_RESERVED)
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.
328 #ifdef CONFIG_MTD_CFI_INTELEXT
331 return cfi_cmdset_0001(map, primary);
333 #ifdef CONFIG_MTD_CFI_AMDSTD
335 return cfi_cmdset_0002(map, primary);
337 #ifdef CONFIG_MTD_CFI_STAA
339 return cfi_cmdset_0020(map, primary);
343 return cfi_cmdset_unknown(map, primary);
346 MODULE_LICENSE("GPL");
347 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
348 MODULE_DESCRIPTION("Helper routines for flash chip probe code");