gen_probe.c 9.03 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4
/*
 * Routines common to all CFI-type probes.
 * (C) 2001, 2001 Red Hat, Inc.
 * GPL'd
David Woodhouse's avatar
David Woodhouse committed
5
 * $Id: gen_probe.c,v 1.11 2003/05/21 15:15:05 dwmw2 Exp $
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8
 */

#include <linux/kernel.h>
David Woodhouse's avatar
David Woodhouse committed
9 10
#include <linux/slab.h>
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/cfi.h>
David Woodhouse's avatar
David Woodhouse committed
14
#include <linux/mtd/mtd.h>
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <linux/mtd/gen_probe.h>

static struct mtd_info *check_cmd_set(struct map_info *, int);
static struct cfi_private *genprobe_ident_chips(struct map_info *map,
						struct chip_probe *cp);
static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
			     struct cfi_private *cfi);

struct mtd_info *mtd_do_chip_probe(struct map_info *map, struct chip_probe *cp)
{
	struct mtd_info *mtd = NULL;
	struct cfi_private *cfi;

	/* First probe the map to see if we have CFI stuff there. */
	cfi = genprobe_ident_chips(map, cp);
	
	if (!cfi)
		return NULL;

	map->fldrv_priv = cfi;
	/* OK we liked it. Now find a driver for the command set it talks */

	mtd = check_cmd_set(map, 1); /* First the primary cmdset */
	if (!mtd)
		mtd = check_cmd_set(map, 0); /* Then the secondary */
	
	if (mtd)
		return mtd;

David Woodhouse's avatar
David Woodhouse committed
44
	printk(KERN_WARNING"gen_probe: No supported Vendor Command Set found\n");
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	
	kfree(cfi->cfiq);
	kfree(cfi);
	map->fldrv_priv = NULL;
	return NULL;
}
EXPORT_SYMBOL(mtd_do_chip_probe);


struct cfi_private *genprobe_ident_chips(struct map_info *map, struct chip_probe *cp)
{
	unsigned long base=0;
	struct cfi_private cfi;
	struct cfi_private *retcfi;
	struct flchip chip[MAX_CFI_CHIPS];
	int i;

	memset(&cfi, 0, sizeof(cfi));
David Woodhouse's avatar
David Woodhouse committed
63
	memset(&chip[0], 0, sizeof(chip));
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	/* Call the probetype-specific code with all permutations of 
	   interleave and device type, etc. */
	if (!genprobe_new_chip(map, cp, &cfi)) {
		/* The probe didn't like it */
		printk(KERN_WARNING "%s: Found no %s device at location zero\n",
		       cp->name, map->name);
		return NULL;
	}		

#if 0 /* Let the CFI probe routine do this sanity check. The Intel and AMD
	 probe routines won't ever return a broken CFI structure anyway,
	 because they make them up themselves.
      */
	if (cfi.cfiq->NumEraseRegions == 0) {
		printk(KERN_WARNING "Number of erase regions is zero\n");
		kfree(cfi.cfiq);
		return NULL;
	}
#endif
	chip[0].start = 0;
	chip[0].state = FL_READY;
	cfi.chipshift = cfi.cfiq->DevSize;

	switch(cfi.interleave) {
#ifdef CFIDEV_INTERLEAVE_1
	case 1:
		break;
#endif
#ifdef CFIDEV_INTERLEAVE_2
	case 2:
		cfi.chipshift++;
		break;
#endif
#ifdef CFIDEV_INTERLEAVE_4
	case 4:
		cfi.chipshift+=2;
		break;
#endif
	default:
		BUG();
	}
		
	cfi.numchips = 1;

	/*
	 * Now probe for other chips, checking sensibly for aliases while
	 * we're at it. The new_chip probe above should have let the first
	 * chip in read mode.
David Woodhouse's avatar
David Woodhouse committed
113 114 115 116 117 118
	 *
	 * NOTE: Here, we're checking if there is room for another chip
	 *       the same size within the mapping. Therefore, 
	 *       base + chipsize <= map->size is the correct thing to do, 
	 *       because, base + chipsize would be the  _first_ byte of the
	 *       next chip, not the one we're currently pondering.
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	 */

	for (base = (1<<cfi.chipshift); base + (1<<cfi.chipshift) <= map->size;
	     base += (1<<cfi.chipshift))
		cp->probe_chip(map, base, &chip[0], &cfi);

	/*
	 * Now allocate the space for the structures we need to return to 
	 * our caller, and copy the appropriate data into them.
	 */

	retcfi = kmalloc(sizeof(struct cfi_private) + cfi.numchips * sizeof(struct flchip), GFP_KERNEL);

	if (!retcfi) {
		printk(KERN_WARNING "%s: kmalloc failed for CFI private structure\n", map->name);
		kfree(cfi.cfiq);
		return NULL;
	}

	memcpy(retcfi, &cfi, sizeof(cfi));
	memcpy(&retcfi->chips[0], chip, sizeof(struct flchip) * cfi.numchips);

	/* Fix up the stuff that breaks when you move it */
	for (i=0; i< retcfi->numchips; i++) {
		init_waitqueue_head(&retcfi->chips[i].wq);
		spin_lock_init(&retcfi->chips[i]._spinlock);
		retcfi->chips[i].mutex = &retcfi->chips[i]._spinlock;
	}

	return retcfi;
}

	
static int genprobe_new_chip(struct map_info *map, struct chip_probe *cp,
			     struct cfi_private *cfi)
{
	switch (map->buswidth) {
#ifdef CFIDEV_BUSWIDTH_1		
	case CFIDEV_BUSWIDTH_1:
		cfi->interleave = CFIDEV_INTERLEAVE_1;

		cfi->device_type = CFI_DEVICETYPE_X8;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;

		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
		break;			
#endif /* CFIDEV_BUSWITDH_1 */

#ifdef CFIDEV_BUSWIDTH_2		
	case CFIDEV_BUSWIDTH_2:
#ifdef CFIDEV_INTERLEAVE_1
		cfi->interleave = CFIDEV_INTERLEAVE_1;

		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_1 */
#ifdef CFIDEV_INTERLEAVE_2
		cfi->interleave = CFIDEV_INTERLEAVE_2;

		cfi->device_type = CFI_DEVICETYPE_X8;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;

		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_2 */
		break;			
#endif /* CFIDEV_BUSWIDTH_2 */

#ifdef CFIDEV_BUSWIDTH_4
	case CFIDEV_BUSWIDTH_4:
#if defined(CFIDEV_INTERLEAVE_1) && defined(SOMEONE_ACTUALLY_MAKES_THESE)
                cfi->interleave = CFIDEV_INTERLEAVE_1;

                cfi->device_type = CFI_DEVICETYPE_X32;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_1 */
#ifdef CFIDEV_INTERLEAVE_2
		cfi->interleave = CFIDEV_INTERLEAVE_2;

#ifdef SOMEONE_ACTUALLY_MAKES_THESE
		cfi->device_type = CFI_DEVICETYPE_X32;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif
		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;

		cfi->device_type = CFI_DEVICETYPE_X8;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_2 */
#ifdef CFIDEV_INTERLEAVE_4
		cfi->interleave = CFIDEV_INTERLEAVE_4;

#ifdef SOMEONE_ACTUALLY_MAKES_THESE
		cfi->device_type = CFI_DEVICETYPE_X32;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif
		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;

		cfi->device_type = CFI_DEVICETYPE_X8;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_4 */
		break;
#endif /* CFIDEV_BUSWIDTH_4 */

David Woodhouse's avatar
David Woodhouse committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
#ifdef CFIDEV_BUSWIDTH_8
	case CFIDEV_BUSWIDTH_8:
#if defined(CFIDEV_INTERLEAVE_2) && defined(SOMEONE_ACTUALLY_MAKES_THESE)
                cfi->interleave = CFIDEV_INTERLEAVE_2;

                cfi->device_type = CFI_DEVICETYPE_X32;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_2 */
#ifdef CFIDEV_INTERLEAVE_4
		cfi->interleave = CFIDEV_INTERLEAVE_4;

#ifdef SOMEONE_ACTUALLY_MAKES_THESE
		cfi->device_type = CFI_DEVICETYPE_X32;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif
		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_4 */
#ifdef CFIDEV_INTERLEAVE_8
		cfi->interleave = CFIDEV_INTERLEAVE_8;

		cfi->device_type = CFI_DEVICETYPE_X16;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;

		cfi->device_type = CFI_DEVICETYPE_X8;
		if (cp->probe_chip(map, 0, NULL, cfi))
			return 1;
#endif /* CFIDEV_INTERLEAVE_8 */
		break;
#endif /* CFIDEV_BUSWIDTH_8 */

Linus Torvalds's avatar
Linus Torvalds committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	default:
		printk(KERN_WARNING "genprobe_new_chip called with unsupported buswidth %d\n", map->buswidth);
		return 0;
	}
	return 0;
}


typedef struct mtd_info *cfi_cmdset_fn_t(struct map_info *, int);

extern cfi_cmdset_fn_t cfi_cmdset_0001;
extern cfi_cmdset_fn_t cfi_cmdset_0002;

static inline struct mtd_info *cfi_cmdset_unknown(struct map_info *map, 
						  int primary)
{
	struct cfi_private *cfi = map->fldrv_priv;
	__u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
#if defined(CONFIG_MODULES) && defined(HAVE_INTER_MODULE)
	char probename[32];
	cfi_cmdset_fn_t *probe_function;

	sprintf(probename, "cfi_cmdset_%4.4X", type);
		
	probe_function = inter_module_get_request(probename, probename);

	if (probe_function) {
		struct mtd_info *mtd;

		mtd = (*probe_function)(map, primary);
		/* If it was happy, it'll have increased its own use count */
		inter_module_put(probename);
		return mtd;
	}
#endif
	printk(KERN_NOTICE "Support for command set %04X not present\n",
	       type);

	return NULL;
}

static struct mtd_info *check_cmd_set(struct map_info *map, int primary)
{
	struct cfi_private *cfi = map->fldrv_priv;
	__u16 type = primary?cfi->cfiq->P_ID:cfi->cfiq->A_ID;
	
	if (type == P_ID_NONE || type == P_ID_RESERVED)
		return NULL;

	switch(type){
		/* Urgh. Ifdefs. The version with weak symbols was
		 * _much_ nicer. Shame it didn't seem to work on
		 * anything but x86, really.
		 * But we can't rely in inter_module_get() because
		 * that'd mean we depend on link order.
		 */
#ifdef CONFIG_MTD_CFI_INTELEXT
	case 0x0001:
	case 0x0003:
		return cfi_cmdset_0001(map, primary);
#endif
#ifdef CONFIG_MTD_CFI_AMDSTD
	case 0x0002:
		return cfi_cmdset_0002(map, primary);
David Woodhouse's avatar
David Woodhouse committed
336 337 338 339
#endif
#ifdef CONFIG_MTD_CFI_STAA
        case 0x0020:
		return cfi_cmdset_0020(map, primary);
Linus Torvalds's avatar
Linus Torvalds committed
340 341 342 343 344 345 346 347 348
#endif
	}

	return cfi_cmdset_unknown(map, primary);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
MODULE_DESCRIPTION("Helper routines for flash chip probe code");