ia64/linux-2.6.18-xen.hg

diff drivers/mtd/chips/cfi_probe.c @ 0:831230e53067

Import 2.6.18 from kernel.org tarball.
author Ian Campbell <ian.campbell@xensource.com>
date Wed Apr 11 14:15:44 2007 +0100 (2007-04-11)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/mtd/chips/cfi_probe.c	Wed Apr 11 14:15:44 2007 +0100
     1.3 @@ -0,0 +1,444 @@
     1.4 +/*
     1.5 +   Common Flash Interface probe code.
     1.6 +   (C) 2000 Red Hat. GPL'd.
     1.7 +   $Id: cfi_probe.c,v 1.86 2005/11/29 14:48:31 gleixner Exp $
     1.8 +*/
     1.9 +
    1.10 +#include <linux/module.h>
    1.11 +#include <linux/types.h>
    1.12 +#include <linux/kernel.h>
    1.13 +#include <linux/init.h>
    1.14 +#include <asm/io.h>
    1.15 +#include <asm/byteorder.h>
    1.16 +#include <linux/errno.h>
    1.17 +#include <linux/slab.h>
    1.18 +#include <linux/interrupt.h>
    1.19 +
    1.20 +#include <linux/mtd/xip.h>
    1.21 +#include <linux/mtd/map.h>
    1.22 +#include <linux/mtd/cfi.h>
    1.23 +#include <linux/mtd/gen_probe.h>
    1.24 +
    1.25 +//#define DEBUG_CFI
    1.26 +
    1.27 +#ifdef DEBUG_CFI
    1.28 +static void print_cfi_ident(struct cfi_ident *);
    1.29 +#endif
    1.30 +
    1.31 +static int cfi_probe_chip(struct map_info *map, __u32 base,
    1.32 +			  unsigned long *chip_map, struct cfi_private *cfi);
    1.33 +static int cfi_chip_setup(struct map_info *map, struct cfi_private *cfi);
    1.34 +
    1.35 +struct mtd_info *cfi_probe(struct map_info *map);
    1.36 +
    1.37 +#ifdef CONFIG_MTD_XIP
    1.38 +
    1.39 +/* only needed for short periods, so this is rather simple */
    1.40 +#define xip_disable()	local_irq_disable()
    1.41 +
    1.42 +#define xip_allowed(base, map) \
    1.43 +do { \
    1.44 +	(void) map_read(map, base); \
    1.45 +	asm volatile (".rep 8; nop; .endr"); \
    1.46 +	local_irq_enable(); \
    1.47 +} while (0)
    1.48 +
    1.49 +#define xip_enable(base, map, cfi) \
    1.50 +do { \
    1.51 +	cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL); \
    1.52 +	cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL); \
    1.53 +	xip_allowed(base, map); \
    1.54 +} while (0)
    1.55 +
    1.56 +#define xip_disable_qry(base, map, cfi) \
    1.57 +do { \
    1.58 +	xip_disable(); \
    1.59 +	cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL); \
    1.60 +	cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL); \
    1.61 +	cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL); \
    1.62 +} while (0)
    1.63 +
    1.64 +#else
    1.65 +
    1.66 +#define xip_disable()			do { } while (0)
    1.67 +#define xip_allowed(base, map)		do { } while (0)
    1.68 +#define xip_enable(base, map, cfi)	do { } while (0)
    1.69 +#define xip_disable_qry(base, map, cfi) do { } while (0)
    1.70 +
    1.71 +#endif
    1.72 +
    1.73 +/* check for QRY.
    1.74 +   in: interleave,type,mode
    1.75 +   ret: table index, <0 for error
    1.76 + */
    1.77 +static int __xipram qry_present(struct map_info *map, __u32 base,
    1.78 +				struct cfi_private *cfi)
    1.79 +{
    1.80 +	int osf = cfi->interleave * cfi->device_type;	// scale factor
    1.81 +	map_word val[3];
    1.82 +	map_word qry[3];
    1.83 +
    1.84 +	qry[0] = cfi_build_cmd('Q', map, cfi);
    1.85 +	qry[1] = cfi_build_cmd('R', map, cfi);
    1.86 +	qry[2] = cfi_build_cmd('Y', map, cfi);
    1.87 +
    1.88 +	val[0] = map_read(map, base + osf*0x10);
    1.89 +	val[1] = map_read(map, base + osf*0x11);
    1.90 +	val[2] = map_read(map, base + osf*0x12);
    1.91 +
    1.92 +	if (!map_word_equal(map, qry[0], val[0]))
    1.93 +		return 0;
    1.94 +
    1.95 +	if (!map_word_equal(map, qry[1], val[1]))
    1.96 +		return 0;
    1.97 +
    1.98 +	if (!map_word_equal(map, qry[2], val[2]))
    1.99 +		return 0;
   1.100 +
   1.101 +	return 1; 	// "QRY" found
   1.102 +}
   1.103 +
   1.104 +static int __xipram cfi_probe_chip(struct map_info *map, __u32 base,
   1.105 +				   unsigned long *chip_map, struct cfi_private *cfi)
   1.106 +{
   1.107 +	int i;
   1.108 +
   1.109 +	if ((base + 0) >= map->size) {
   1.110 +		printk(KERN_NOTICE
   1.111 +			"Probe at base[0x00](0x%08lx) past the end of the map(0x%08lx)\n",
   1.112 +			(unsigned long)base, map->size -1);
   1.113 +		return 0;
   1.114 +	}
   1.115 +	if ((base + 0xff) >= map->size) {
   1.116 +		printk(KERN_NOTICE
   1.117 +			"Probe at base[0x55](0x%08lx) past the end of the map(0x%08lx)\n",
   1.118 +			(unsigned long)base + 0x55, map->size -1);
   1.119 +		return 0;
   1.120 +	}
   1.121 +
   1.122 +	xip_disable();
   1.123 +	cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
   1.124 +	cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
   1.125 +	cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
   1.126 +
   1.127 +	if (!qry_present(map,base,cfi)) {
   1.128 +		xip_enable(base, map, cfi);
   1.129 +		return 0;
   1.130 +	}
   1.131 +
   1.132 +	if (!cfi->numchips) {
   1.133 +		/* This is the first time we're called. Set up the CFI
   1.134 +		   stuff accordingly and return */
   1.135 +		return cfi_chip_setup(map, cfi);
   1.136 +	}
   1.137 +
   1.138 +	/* Check each previous chip to see if it's an alias */
   1.139 + 	for (i=0; i < (base >> cfi->chipshift); i++) {
   1.140 + 		unsigned long start;
   1.141 + 		if(!test_bit(i, chip_map)) {
   1.142 +			/* Skip location; no valid chip at this address */
   1.143 + 			continue;
   1.144 + 		}
   1.145 + 		start = i << cfi->chipshift;
   1.146 +		/* This chip should be in read mode if it's one
   1.147 +		   we've already touched. */
   1.148 +		if (qry_present(map, start, cfi)) {
   1.149 +			/* Eep. This chip also had the QRY marker.
   1.150 +			 * Is it an alias for the new one? */
   1.151 +			cfi_send_gen_cmd(0xF0, 0, start, map, cfi, cfi->device_type, NULL);
   1.152 +			cfi_send_gen_cmd(0xFF, 0, start, map, cfi, cfi->device_type, NULL);
   1.153 +
   1.154 +			/* If the QRY marker goes away, it's an alias */
   1.155 +			if (!qry_present(map, start, cfi)) {
   1.156 +				xip_allowed(base, map);
   1.157 +				printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
   1.158 +				       map->name, base, start);
   1.159 +				return 0;
   1.160 +			}
   1.161 +			/* Yes, it's actually got QRY for data. Most
   1.162 +			 * unfortunate. Stick the new chip in read mode
   1.163 +			 * too and if it's the same, assume it's an alias. */
   1.164 +			/* FIXME: Use other modes to do a proper check */
   1.165 +			cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
   1.166 +			cfi_send_gen_cmd(0xFF, 0, start, map, cfi, cfi->device_type, NULL);
   1.167 +
   1.168 +			if (qry_present(map, base, cfi)) {
   1.169 +				xip_allowed(base, map);
   1.170 +				printk(KERN_DEBUG "%s: Found an alias at 0x%x for the chip at 0x%lx\n",
   1.171 +				       map->name, base, start);
   1.172 +				return 0;
   1.173 +			}
   1.174 +		}
   1.175 +	}
   1.176 +
   1.177 +	/* OK, if we got to here, then none of the previous chips appear to
   1.178 +	   be aliases for the current one. */
   1.179 +	set_bit((base >> cfi->chipshift), chip_map); /* Update chip map */
   1.180 +	cfi->numchips++;
   1.181 +
   1.182 +	/* Put it back into Read Mode */
   1.183 +	cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
   1.184 +	cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
   1.185 +	xip_allowed(base, map);
   1.186 +
   1.187 +	printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n",
   1.188 +	       map->name, cfi->interleave, cfi->device_type*8, base,
   1.189 +	       map->bankwidth*8);
   1.190 +
   1.191 +	return 1;
   1.192 +}
   1.193 +
   1.194 +static int __xipram cfi_chip_setup(struct map_info *map,
   1.195 +				   struct cfi_private *cfi)
   1.196 +{
   1.197 +	int ofs_factor = cfi->interleave*cfi->device_type;
   1.198 +	__u32 base = 0;
   1.199 +	int num_erase_regions = cfi_read_query(map, base + (0x10 + 28)*ofs_factor);
   1.200 +	int i;
   1.201 +
   1.202 +	xip_enable(base, map, cfi);
   1.203 +#ifdef DEBUG_CFI
   1.204 +	printk("Number of erase regions: %d\n", num_erase_regions);
   1.205 +#endif
   1.206 +	if (!num_erase_regions)
   1.207 +		return 0;
   1.208 +
   1.209 +	cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
   1.210 +	if (!cfi->cfiq) {
   1.211 +		printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
   1.212 +		return 0;
   1.213 +	}
   1.214 +
   1.215 +	memset(cfi->cfiq,0,sizeof(struct cfi_ident));
   1.216 +
   1.217 +	cfi->cfi_mode = CFI_MODE_CFI;
   1.218 +
   1.219 +	/* Read the CFI info structure */
   1.220 +	xip_disable_qry(base, map, cfi);
   1.221 +	for (i=0; i<(sizeof(struct cfi_ident) + num_erase_regions * 4); i++)
   1.222 +		((unsigned char *)cfi->cfiq)[i] = cfi_read_query(map,base + (0x10 + i)*ofs_factor);
   1.223 +
   1.224 +	/* Note we put the device back into Read Mode BEFORE going into Auto
   1.225 +	 * Select Mode, as some devices support nesting of modes, others
   1.226 +	 * don't. This way should always work.
   1.227 +	 * On cmdset 0001 the writes of 0xaa and 0x55 are not needed, and
   1.228 +	 * so should be treated as nops or illegal (and so put the device
   1.229 +	 * back into Read Mode, which is a nop in this case).
   1.230 +	 */
   1.231 +	cfi_send_gen_cmd(0xf0,     0, base, map, cfi, cfi->device_type, NULL);
   1.232 +	cfi_send_gen_cmd(0xaa, 0x555, base, map, cfi, cfi->device_type, NULL);
   1.233 +	cfi_send_gen_cmd(0x55, 0x2aa, base, map, cfi, cfi->device_type, NULL);
   1.234 +	cfi_send_gen_cmd(0x90, 0x555, base, map, cfi, cfi->device_type, NULL);
   1.235 +	cfi->mfr = cfi_read_query16(map, base);
   1.236 +	cfi->id = cfi_read_query16(map, base + ofs_factor);
   1.237 +
   1.238 +	/* Put it back into Read Mode */
   1.239 +	cfi_send_gen_cmd(0xF0, 0, base, map, cfi, cfi->device_type, NULL);
   1.240 +	/* ... even if it's an Intel chip */
   1.241 +	cfi_send_gen_cmd(0xFF, 0, base, map, cfi, cfi->device_type, NULL);
   1.242 +	xip_allowed(base, map);
   1.243 +
   1.244 +	/* Do any necessary byteswapping */
   1.245 +	cfi->cfiq->P_ID = le16_to_cpu(cfi->cfiq->P_ID);
   1.246 +
   1.247 +	cfi->cfiq->P_ADR = le16_to_cpu(cfi->cfiq->P_ADR);
   1.248 +	cfi->cfiq->A_ID = le16_to_cpu(cfi->cfiq->A_ID);
   1.249 +	cfi->cfiq->A_ADR = le16_to_cpu(cfi->cfiq->A_ADR);
   1.250 +	cfi->cfiq->InterfaceDesc = le16_to_cpu(cfi->cfiq->InterfaceDesc);
   1.251 +	cfi->cfiq->MaxBufWriteSize = le16_to_cpu(cfi->cfiq->MaxBufWriteSize);
   1.252 +
   1.253 +#ifdef DEBUG_CFI
   1.254 +	/* Dump the information therein */
   1.255 +	print_cfi_ident(cfi->cfiq);
   1.256 +#endif
   1.257 +
   1.258 +	for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
   1.259 +		cfi->cfiq->EraseRegionInfo[i] = le32_to_cpu(cfi->cfiq->EraseRegionInfo[i]);
   1.260 +
   1.261 +#ifdef DEBUG_CFI
   1.262 +		printk("  Erase Region #%d: BlockSize 0x%4.4X bytes, %d blocks\n",
   1.263 +		       i, (cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff,
   1.264 +		       (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1);
   1.265 +#endif
   1.266 +	}
   1.267 +
   1.268 +	printk(KERN_INFO "%s: Found %d x%d devices at 0x%x in %d-bit bank\n",
   1.269 +	       map->name, cfi->interleave, cfi->device_type*8, base,
   1.270 +	       map->bankwidth*8);
   1.271 +
   1.272 +	return 1;
   1.273 +}
   1.274 +
   1.275 +#ifdef DEBUG_CFI
   1.276 +static char *vendorname(__u16 vendor)
   1.277 +{
   1.278 +	switch (vendor) {
   1.279 +	case P_ID_NONE:
   1.280 +		return "None";
   1.281 +
   1.282 +	case P_ID_INTEL_EXT:
   1.283 +		return "Intel/Sharp Extended";
   1.284 +
   1.285 +	case P_ID_AMD_STD:
   1.286 +		return "AMD/Fujitsu Standard";
   1.287 +
   1.288 +	case P_ID_INTEL_STD:
   1.289 +		return "Intel/Sharp Standard";
   1.290 +
   1.291 +	case P_ID_AMD_EXT:
   1.292 +		return "AMD/Fujitsu Extended";
   1.293 +
   1.294 +	case P_ID_WINBOND:
   1.295 +		return "Winbond Standard";
   1.296 +
   1.297 +	case P_ID_ST_ADV:
   1.298 +		return "ST Advanced";
   1.299 +
   1.300 +	case P_ID_MITSUBISHI_STD:
   1.301 +		return "Mitsubishi Standard";
   1.302 +
   1.303 +	case P_ID_MITSUBISHI_EXT:
   1.304 +		return "Mitsubishi Extended";
   1.305 +
   1.306 +	case P_ID_SST_PAGE:
   1.307 +		return "SST Page Write";
   1.308 +
   1.309 +	case P_ID_INTEL_PERFORMANCE:
   1.310 +		return "Intel Performance Code";
   1.311 +
   1.312 +	case P_ID_INTEL_DATA:
   1.313 +		return "Intel Data";
   1.314 +
   1.315 +	case P_ID_RESERVED:
   1.316 +		return "Not Allowed / Reserved for Future Use";
   1.317 +
   1.318 +	default:
   1.319 +		return "Unknown";
   1.320 +	}
   1.321 +}
   1.322 +
   1.323 +
   1.324 +static void print_cfi_ident(struct cfi_ident *cfip)
   1.325 +{
   1.326 +#if 0
   1.327 +	if (cfip->qry[0] != 'Q' || cfip->qry[1] != 'R' || cfip->qry[2] != 'Y') {
   1.328 +		printk("Invalid CFI ident structure.\n");
   1.329 +		return;
   1.330 +	}
   1.331 +#endif
   1.332 +	printk("Primary Vendor Command Set: %4.4X (%s)\n", cfip->P_ID, vendorname(cfip->P_ID));
   1.333 +	if (cfip->P_ADR)
   1.334 +		printk("Primary Algorithm Table at %4.4X\n", cfip->P_ADR);
   1.335 +	else
   1.336 +		printk("No Primary Algorithm Table\n");
   1.337 +
   1.338 +	printk("Alternative Vendor Command Set: %4.4X (%s)\n", cfip->A_ID, vendorname(cfip->A_ID));
   1.339 +	if (cfip->A_ADR)
   1.340 +		printk("Alternate Algorithm Table at %4.4X\n", cfip->A_ADR);
   1.341 +	else
   1.342 +		printk("No Alternate Algorithm Table\n");
   1.343 +
   1.344 +
   1.345 +	printk("Vcc Minimum: %2d.%d V\n", cfip->VccMin >> 4, cfip->VccMin & 0xf);
   1.346 +	printk("Vcc Maximum: %2d.%d V\n", cfip->VccMax >> 4, cfip->VccMax & 0xf);
   1.347 +	if (cfip->VppMin) {
   1.348 +		printk("Vpp Minimum: %2d.%d V\n", cfip->VppMin >> 4, cfip->VppMin & 0xf);
   1.349 +		printk("Vpp Maximum: %2d.%d V\n", cfip->VppMax >> 4, cfip->VppMax & 0xf);
   1.350 +	}
   1.351 +	else
   1.352 +		printk("No Vpp line\n");
   1.353 +
   1.354 +	printk("Typical byte/word write timeout: %d µs\n", 1<<cfip->WordWriteTimeoutTyp);
   1.355 +	printk("Maximum byte/word write timeout: %d µs\n", (1<<cfip->WordWriteTimeoutMax) * (1<<cfip->WordWriteTimeoutTyp));
   1.356 +
   1.357 +	if (cfip->BufWriteTimeoutTyp || cfip->BufWriteTimeoutMax) {
   1.358 +		printk("Typical full buffer write timeout: %d µs\n", 1<<cfip->BufWriteTimeoutTyp);
   1.359 +		printk("Maximum full buffer write timeout: %d µs\n", (1<<cfip->BufWriteTimeoutMax) * (1<<cfip->BufWriteTimeoutTyp));
   1.360 +	}
   1.361 +	else
   1.362 +		printk("Full buffer write not supported\n");
   1.363 +
   1.364 +	printk("Typical block erase timeout: %d ms\n", 1<<cfip->BlockEraseTimeoutTyp);
   1.365 +	printk("Maximum block erase timeout: %d ms\n", (1<<cfip->BlockEraseTimeoutMax) * (1<<cfip->BlockEraseTimeoutTyp));
   1.366 +	if (cfip->ChipEraseTimeoutTyp || cfip->ChipEraseTimeoutMax) {
   1.367 +		printk("Typical chip erase timeout: %d ms\n", 1<<cfip->ChipEraseTimeoutTyp);
   1.368 +		printk("Maximum chip erase timeout: %d ms\n", (1<<cfip->ChipEraseTimeoutMax) * (1<<cfip->ChipEraseTimeoutTyp));
   1.369 +	}
   1.370 +	else
   1.371 +		printk("Chip erase not supported\n");
   1.372 +
   1.373 +	printk("Device size: 0x%X bytes (%d MiB)\n", 1 << cfip->DevSize, 1<< (cfip->DevSize - 20));
   1.374 +	printk("Flash Device Interface description: 0x%4.4X\n", cfip->InterfaceDesc);
   1.375 +	switch(cfip->InterfaceDesc) {
   1.376 +	case 0:
   1.377 +		printk("  - x8-only asynchronous interface\n");
   1.378 +		break;
   1.379 +
   1.380 +	case 1:
   1.381 +		printk("  - x16-only asynchronous interface\n");
   1.382 +		break;
   1.383 +
   1.384 +	case 2:
   1.385 +		printk("  - supports x8 and x16 via BYTE# with asynchronous interface\n");
   1.386 +		break;
   1.387 +
   1.388 +	case 3:
   1.389 +		printk("  - x32-only asynchronous interface\n");
   1.390 +		break;
   1.391 +
   1.392 +	case 4:
   1.393 +		printk("  - supports x16 and x32 via Word# with asynchronous interface\n");
   1.394 +		break;
   1.395 +
   1.396 +	case 65535:
   1.397 +		printk("  - Not Allowed / Reserved\n");
   1.398 +		break;
   1.399 +
   1.400 +	default:
   1.401 +		printk("  - Unknown\n");
   1.402 +		break;
   1.403 +	}
   1.404 +
   1.405 +	printk("Max. bytes in buffer write: 0x%x\n", 1<< cfip->MaxBufWriteSize);
   1.406 +	printk("Number of Erase Block Regions: %d\n", cfip->NumEraseRegions);
   1.407 +
   1.408 +}
   1.409 +#endif /* DEBUG_CFI */
   1.410 +
   1.411 +static struct chip_probe cfi_chip_probe = {
   1.412 +	.name		= "CFI",
   1.413 +	.probe_chip	= cfi_probe_chip
   1.414 +};
   1.415 +
   1.416 +struct mtd_info *cfi_probe(struct map_info *map)
   1.417 +{
   1.418 +	/*
   1.419 +	 * Just use the generic probe stuff to call our CFI-specific
   1.420 +	 * chip_probe routine in all the possible permutations, etc.
   1.421 +	 */
   1.422 +	return mtd_do_chip_probe(map, &cfi_chip_probe);
   1.423 +}
   1.424 +
   1.425 +static struct mtd_chip_driver cfi_chipdrv = {
   1.426 +	.probe		= cfi_probe,
   1.427 +	.name		= "cfi_probe",
   1.428 +	.module		= THIS_MODULE
   1.429 +};
   1.430 +
   1.431 +static int __init cfi_probe_init(void)
   1.432 +{
   1.433 +	register_mtd_chip_driver(&cfi_chipdrv);
   1.434 +	return 0;
   1.435 +}
   1.436 +
   1.437 +static void __exit cfi_probe_exit(void)
   1.438 +{
   1.439 +	unregister_mtd_chip_driver(&cfi_chipdrv);
   1.440 +}
   1.441 +
   1.442 +module_init(cfi_probe_init);
   1.443 +module_exit(cfi_probe_exit);
   1.444 +
   1.445 +MODULE_LICENSE("GPL");
   1.446 +MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
   1.447 +MODULE_DESCRIPTION("Probe code for CFI-compliant flash chips");