2 * Common Flash Interface support:
3 * Intel Extended Vendor Command Set (ID 0x0001)
5 * (C) 2000 Red Hat. GPL'd
7 * $Id: cfi_cmdset_0001.c,v 1.160 2004/11/01 06:02:24 nico Exp $
8 * (+ suspend fix from v1.162)
9 * (+ partition detection fix from v1.163)
11 * 10/10/2000 Nicolas Pitre <nico@cam.org>
12 * - completely revamped method functions so they are aware and
13 * independent of the flash geometry (buswidth, interleave, etc.)
14 * - scalability vs code size is completely set at compile-time
15 * (see include/linux/mtd/cfi.h for selection)
16 * - optimized write buffer method
17 * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
18 * - reworked lock/unlock/erase support for var size flash
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/init.h>
27 #include <asm/byteorder.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/interrupt.h>
33 #include <linux/mtd/map.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/compatmac.h>
36 #include <linux/mtd/cfi.h>
38 /* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
40 // debugging, turns off buffer write mode if set to 1
41 #define FORCE_WORD_WRITE 0
43 #define MANUFACTURER_INTEL 0x0089
44 #define I82802AB 0x00ad
45 #define I82802AC 0x00ac
46 #define MANUFACTURER_ST 0x0020
47 #define M50LPW080 0x002F
49 static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
50 //static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
51 //static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
52 static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
53 static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54 static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
55 static void cfi_intelext_sync (struct mtd_info *);
56 static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
57 static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
58 static int cfi_intelext_suspend (struct mtd_info *);
59 static void cfi_intelext_resume (struct mtd_info *);
61 static void cfi_intelext_destroy(struct mtd_info *);
63 struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
65 static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
66 static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
68 static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
69 size_t *retlen, u_char **mtdbuf);
70 static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from,
73 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
74 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
80 * *********** SETUP AND PROBE BITS ***********
83 static struct mtd_chip_driver cfi_intelext_chipdrv = {
84 .probe = NULL, /* Not usable directly */
85 .destroy = cfi_intelext_destroy,
86 .name = "cfi_cmdset_0001",
90 /* #define DEBUG_LOCK_BITS */
91 /* #define DEBUG_CFI_FEATURES */
93 #ifdef DEBUG_CFI_FEATURES
94 static void cfi_tell_features(struct cfi_pri_intelext *extp)
97 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
98 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
99 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
100 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
101 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
102 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
103 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
104 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
105 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
106 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
107 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
108 for (i=10; i<32; i++) {
109 if (extp->FeatureSupport & (1<<i))
110 printk(" - Unknown Bit %X: supported\n", i);
113 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
114 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
115 for (i=1; i<8; i++) {
116 if (extp->SuspendCmdSupport & (1<<i))
117 printk(" - Unknown Bit %X: supported\n", i);
120 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
121 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
122 printk(" - Valid Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
123 for (i=2; i<16; i++) {
124 if (extp->BlkStatusRegMask & (1<<i))
125 printk(" - Unknown Bit %X Active: yes\n",i);
128 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
129 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
130 if (extp->VppOptimal)
131 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
132 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
136 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
137 /* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
138 static void fixup_intel_strataflash(struct mtd_info *mtd, void* param)
140 struct map_info *map = mtd->priv;
141 struct cfi_private *cfi = map->fldrv_priv;
142 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
144 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
145 "erase on write disabled.\n");
146 extp->SuspendCmdSupport &= ~1;
150 static void fixup_st_m28w320ct(struct mtd_info *mtd, void* param)
152 struct map_info *map = mtd->priv;
153 struct cfi_private *cfi = map->fldrv_priv;
155 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
156 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
159 static void fixup_st_m28w320cb(struct mtd_info *mtd, void* param)
161 struct map_info *map = mtd->priv;
162 struct cfi_private *cfi = map->fldrv_priv;
164 /* Note this is done after the region info is endian swapped */
165 cfi->cfiq->EraseRegionInfo[1] =
166 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
169 static void fixup_use_point(struct mtd_info *mtd, void *param)
171 struct map_info *map = mtd->priv;
172 if (!mtd->point && map_is_linear(map)) {
173 mtd->point = cfi_intelext_point;
174 mtd->unpoint = cfi_intelext_unpoint;
178 static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
180 struct map_info *map = mtd->priv;
181 struct cfi_private *cfi = map->fldrv_priv;
182 if (cfi->cfiq->BufWriteTimeoutTyp) {
183 printk(KERN_INFO "Using buffer write method\n" );
184 mtd->write = cfi_intelext_write_buffers;
188 static struct cfi_fixup cfi_fixup_table[] = {
189 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
190 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
192 #if !FORCE_WORD_WRITE
193 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL },
195 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
196 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
200 static struct cfi_fixup jedec_fixup_table[] = {
201 { MANUFACTURER_INTEL, I82802AB, fixup_use_fwh_lock, NULL, },
202 { MANUFACTURER_INTEL, I82802AC, fixup_use_fwh_lock, NULL, },
203 { MANUFACTURER_ST, M50LPW080, fixup_use_fwh_lock, NULL, },
206 static struct cfi_fixup fixup_table[] = {
207 /* The CFI vendor ids and the JEDEC vendor IDs appear
208 * to be common. It is like the devices id's are as
209 * well. This table is to pick all cases where
210 * we know that is the case.
212 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point, NULL },
216 static inline struct cfi_pri_intelext *
217 read_pri_intelext(struct map_info *map, __u16 adr)
219 struct cfi_pri_intelext *extp;
220 unsigned int extp_size = sizeof(*extp);
223 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
227 /* Do some byteswapping if necessary */
228 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
229 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
230 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
232 if (extp->MajorVersion == '1' && extp->MinorVersion == '3') {
233 unsigned int extra_size = 0;
236 /* Protection Register info */
237 extra_size += (extp->NumProtectionFields - 1) * (4 + 6);
239 /* Burst Read info */
242 /* Number of hardware-partitions */
244 if (extp_size < sizeof(*extp) + extra_size)
246 nb_parts = extp->extra[extra_size - 1];
248 for (i = 0; i < nb_parts; i++) {
249 struct cfi_intelext_regioninfo *rinfo;
250 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
251 extra_size += sizeof(*rinfo);
252 if (extp_size < sizeof(*extp) + extra_size)
254 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
255 extra_size += (rinfo->NumBlockTypes - 1)
256 * sizeof(struct cfi_intelext_blockinfo);
259 if (extp_size < sizeof(*extp) + extra_size) {
261 extp_size = sizeof(*extp) + extra_size;
263 if (extp_size > 4096) {
265 "%s: cfi_pri_intelext is too fat\n",
276 /* This routine is made available to other mtd code via
277 * inter_module_register. It must only be accessed through
278 * inter_module_get which will bump the use count of this module. The
279 * addresses passed back in cfi are valid as long as the use count of
280 * this module is non-zero, i.e. between inter_module_get and
281 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
283 struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
285 struct cfi_private *cfi = map->fldrv_priv;
286 struct mtd_info *mtd;
289 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
291 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
294 memset(mtd, 0, sizeof(*mtd));
296 mtd->type = MTD_NORFLASH;
298 /* Fill in the default mtd operations */
299 mtd->erase = cfi_intelext_erase_varsize;
300 mtd->read = cfi_intelext_read;
301 mtd->write = cfi_intelext_write_words;
302 mtd->sync = cfi_intelext_sync;
303 mtd->lock = cfi_intelext_lock;
304 mtd->unlock = cfi_intelext_unlock;
305 mtd->suspend = cfi_intelext_suspend;
306 mtd->resume = cfi_intelext_resume;
307 mtd->flags = MTD_CAP_NORFLASH;
308 mtd->name = map->name;
310 if (cfi->cfi_mode == CFI_MODE_CFI) {
312 * It's a real CFI chip, not one for which the probe
313 * routine faked a CFI structure. So we read the feature
316 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
317 struct cfi_pri_intelext *extp;
319 extp = read_pri_intelext(map, adr);
325 /* Install our own private info structure */
326 cfi->cmdset_priv = extp;
328 cfi_fixup(mtd, cfi_fixup_table);
330 #ifdef DEBUG_CFI_FEATURES
331 /* Tell the user about it in lots of lovely detail */
332 cfi_tell_features(extp);
335 if(extp->SuspendCmdSupport & 1) {
336 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
339 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
340 /* Apply jedec specific fixups */
341 cfi_fixup(mtd, jedec_fixup_table);
343 /* Apply generic fixups */
344 cfi_fixup(mtd, fixup_table);
346 for (i=0; i< cfi->numchips; i++) {
347 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
348 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
349 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
350 cfi->chips[i].ref_point_counter = 0;
353 map->fldrv = &cfi_intelext_chipdrv;
355 return cfi_intelext_setup(mtd);
358 static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
360 struct map_info *map = mtd->priv;
361 struct cfi_private *cfi = map->fldrv_priv;
362 unsigned long offset = 0;
364 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
366 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
368 mtd->size = devsize * cfi->numchips;
370 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
371 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
372 * mtd->numeraseregions, GFP_KERNEL);
373 if (!mtd->eraseregions) {
374 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
378 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
379 unsigned long ernum, ersize;
380 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
381 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
383 if (mtd->erasesize < ersize) {
384 mtd->erasesize = ersize;
386 for (j=0; j<cfi->numchips; j++) {
387 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
388 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
389 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
391 offset += (ersize * ernum);
394 if (offset != devsize) {
396 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
400 for (i=0; i<mtd->numeraseregions;i++){
401 printk(KERN_DEBUG "%d: offset=0x%x,size=0x%x,blocks=%d\n",
402 i,mtd->eraseregions[i].offset,
403 mtd->eraseregions[i].erasesize,
404 mtd->eraseregions[i].numblocks);
408 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
409 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
412 /* This function has the potential to distort the reality
413 a bit and therefore should be called last. */
414 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
417 __module_get(THIS_MODULE);
422 if(mtd->eraseregions)
423 kfree(mtd->eraseregions);
426 kfree(cfi->cmdset_priv);
430 static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
431 struct cfi_private **pcfi)
433 struct map_info *map = mtd->priv;
434 struct cfi_private *cfi = *pcfi;
435 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
438 * Probing of multi-partition flash ships.
440 * To support multiple partitions when available, we simply arrange
441 * for each of them to have their own flchip structure even if they
442 * are on the same physical chip. This means completely recreating
443 * a new cfi_private structure right here which is a blatent code
444 * layering violation, but this is still the least intrusive
445 * arrangement at this point. This can be rearranged in the future
446 * if someone feels motivated enough. --nico
448 if (extp && extp->MajorVersion == '1' && extp->MinorVersion == '3'
449 && extp->FeatureSupport & (1 << 9)) {
450 struct cfi_private *newcfi;
452 struct flchip_shared *shared;
453 int offs, numregions, numparts, partshift, numvirtchips, i, j;
455 /* Protection Register info */
456 offs = (extp->NumProtectionFields - 1) * (4 + 6);
458 /* Burst Read info */
461 /* Number of partition regions */
462 numregions = extp->extra[offs];
465 /* Number of hardware partitions */
467 for (i = 0; i < numregions; i++) {
468 struct cfi_intelext_regioninfo *rinfo;
469 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
470 numparts += rinfo->NumIdentPartitions;
471 offs += sizeof(*rinfo)
472 + (rinfo->NumBlockTypes - 1) *
473 sizeof(struct cfi_intelext_blockinfo);
477 * All functions below currently rely on all chips having
478 * the same geometry so we'll just assume that all hardware
479 * partitions are of the same size too.
481 partshift = cfi->chipshift - __ffs(numparts);
483 if ((1 << partshift) < mtd->erasesize) {
485 "%s: bad number of hw partitions (%d)\n",
486 __FUNCTION__, numparts);
490 numvirtchips = cfi->numchips * numparts;
491 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
494 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
499 memcpy(newcfi, cfi, sizeof(struct cfi_private));
500 newcfi->numchips = numvirtchips;
501 newcfi->chipshift = partshift;
503 chip = &newcfi->chips[0];
504 for (i = 0; i < cfi->numchips; i++) {
505 shared[i].writing = shared[i].erasing = NULL;
506 spin_lock_init(&shared[i].lock);
507 for (j = 0; j < numparts; j++) {
508 *chip = cfi->chips[i];
509 chip->start += j << partshift;
510 chip->priv = &shared[i];
511 /* those should be reset too since
512 they create memory references. */
513 init_waitqueue_head(&chip->wq);
514 spin_lock_init(&chip->_spinlock);
515 chip->mutex = &chip->_spinlock;
520 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
521 "--> %d partitions of %d KiB\n",
522 map->name, cfi->numchips, cfi->interleave,
523 newcfi->numchips, 1<<(newcfi->chipshift-10));
525 map->fldrv_priv = newcfi;
534 * *********** CHIP ACCESS FUNCTIONS ***********
537 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
539 DECLARE_WAITQUEUE(wait, current);
540 struct cfi_private *cfi = map->fldrv_priv;
541 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
543 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
546 timeo = jiffies + HZ;
548 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)) {
550 * OK. We have possibility for contension on the write/erase
551 * operations which are global to the real chip and not per
552 * partition. So let's fight it over in the partition which
553 * currently has authority on the operation.
555 * The rules are as follows:
557 * - any write operation must own shared->writing.
559 * - any erase operation must own _both_ shared->writing and
562 * - contension arbitration is handled in the owner's context.
564 * The 'shared' struct can be read when its lock is taken.
565 * However any writes to it can only be made when the current
566 * owner's lock is also held.
568 struct flchip_shared *shared = chip->priv;
569 struct flchip *contender;
570 spin_lock(&shared->lock);
571 contender = shared->writing;
572 if (contender && contender != chip) {
574 * The engine to perform desired operation on this
575 * partition is already in use by someone else.
576 * Let's fight over it in the context of the chip
577 * currently using it. If it is possible to suspend,
578 * that other partition will do just that, otherwise
579 * it'll happily send us to sleep. In any case, when
580 * get_chip returns success we're clear to go ahead.
582 int ret = spin_trylock(contender->mutex);
583 spin_unlock(&shared->lock);
586 spin_unlock(chip->mutex);
587 ret = get_chip(map, contender, contender->start, mode);
588 spin_lock(chip->mutex);
590 spin_unlock(contender->mutex);
593 timeo = jiffies + HZ;
594 spin_lock(&shared->lock);
598 shared->writing = chip;
599 if (mode == FL_ERASING)
600 shared->erasing = chip;
601 if (contender && contender != chip)
602 spin_unlock(contender->mutex);
603 spin_unlock(&shared->lock);
606 switch (chip->state) {
610 status = map_read(map, adr);
611 if (map_word_andequal(map, status, status_OK, status_OK))
614 /* At this point we're fine with write operations
615 in other partitions as they don't conflict. */
616 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
619 if (time_after(jiffies, timeo)) {
620 printk(KERN_ERR "Waiting for chip to be ready timed out. Status %lx\n",
624 spin_unlock(chip->mutex);
626 spin_lock(chip->mutex);
627 /* Someone else might have been playing with it. */
638 !(cfip->FeatureSupport & 2) ||
639 !(mode == FL_READY || mode == FL_POINT ||
640 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
645 map_write(map, CMD(0xB0), adr);
647 /* If the flash has finished erasing, then 'erase suspend'
648 * appears to make some (28F320) flash devices switch to
649 * 'read' mode. Make sure that we switch to 'read status'
650 * mode so we get the right data. --rmk
652 map_write(map, CMD(0x70), adr);
653 chip->oldstate = FL_ERASING;
654 chip->state = FL_ERASE_SUSPENDING;
655 chip->erase_suspended = 1;
657 status = map_read(map, adr);
658 if (map_word_andequal(map, status, status_OK, status_OK))
661 if (time_after(jiffies, timeo)) {
662 /* Urgh. Resume and pretend we weren't here. */
663 map_write(map, CMD(0xd0), adr);
664 /* Make sure we're in 'read status' mode if it had finished */
665 map_write(map, CMD(0x70), adr);
666 chip->state = FL_ERASING;
667 chip->oldstate = FL_READY;
668 printk(KERN_ERR "Chip not ready after erase "
669 "suspended: status = 0x%lx\n", status.x[0]);
673 spin_unlock(chip->mutex);
675 spin_lock(chip->mutex);
676 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
677 So we can just loop here. */
679 chip->state = FL_STATUS;
683 /* Only if there's no operation suspended... */
684 if (mode == FL_READY && chip->oldstate == FL_READY)
689 set_current_state(TASK_UNINTERRUPTIBLE);
690 add_wait_queue(&chip->wq, &wait);
691 spin_unlock(chip->mutex);
693 remove_wait_queue(&chip->wq, &wait);
694 spin_lock(chip->mutex);
699 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
701 struct cfi_private *cfi = map->fldrv_priv;
704 struct flchip_shared *shared = chip->priv;
705 spin_lock(&shared->lock);
706 if (shared->writing == chip) {
707 /* We own the ability to write, but we're done */
708 shared->writing = shared->erasing;
709 if (shared->writing && shared->writing != chip) {
710 /* give back ownership to who we loaned it from */
711 struct flchip *loaner = shared->writing;
712 spin_lock(loaner->mutex);
713 spin_unlock(&shared->lock);
714 spin_unlock(chip->mutex);
715 put_chip(map, loaner, loaner->start);
716 spin_lock(chip->mutex);
717 spin_unlock(loaner->mutex);
719 if (chip->oldstate != FL_ERASING) {
720 shared->erasing = NULL;
721 if (chip->oldstate != FL_WRITING)
722 shared->writing = NULL;
724 spin_unlock(&shared->lock);
727 spin_unlock(&shared->lock);
731 switch(chip->oldstate) {
733 chip->state = chip->oldstate;
734 /* What if one interleaved chip has finished and the
735 other hasn't? The old code would leave the finished
736 one in READY mode. That's bad, and caused -EROFS
737 errors to be returned from do_erase_oneblock because
738 that's the only bit it checked for at the time.
739 As the state machine appears to explicitly allow
740 sending the 0x70 (Read Status) command to an erasing
741 chip and expecting it to be ignored, that's what we
743 map_write(map, CMD(0xd0), adr);
744 map_write(map, CMD(0x70), adr);
745 chip->oldstate = FL_READY;
746 chip->state = FL_ERASING;
752 /* We should really make set_vpp() count, rather than doing this */
756 printk(KERN_ERR "put_chip() called with oldstate %d!!\n", chip->oldstate);
761 static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
763 unsigned long cmd_addr;
764 struct cfi_private *cfi = map->fldrv_priv;
769 /* Ensure cmd read/writes are aligned. */
770 cmd_addr = adr & ~(map_bankwidth(map)-1);
772 spin_lock(chip->mutex);
774 ret = get_chip(map, chip, cmd_addr, FL_POINT);
777 if (chip->state != FL_POINT && chip->state != FL_READY)
778 map_write(map, CMD(0xff), cmd_addr);
780 chip->state = FL_POINT;
781 chip->ref_point_counter++;
783 spin_unlock(chip->mutex);
788 static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
790 struct map_info *map = mtd->priv;
791 struct cfi_private *cfi = map->fldrv_priv;
796 if (!map->virt || (from + len > mtd->size))
799 *mtdbuf = (void *)map->virt + from;
802 /* Now lock the chip(s) to POINT state */
804 /* ofs: offset within the first chip that the first read should start */
805 chipnum = (from >> cfi->chipshift);
806 ofs = from - (chipnum << cfi->chipshift);
809 unsigned long thislen;
811 if (chipnum >= cfi->numchips)
814 if ((len + ofs -1) >> cfi->chipshift)
815 thislen = (1<<cfi->chipshift) - ofs;
819 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
832 static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
834 struct map_info *map = mtd->priv;
835 struct cfi_private *cfi = map->fldrv_priv;
839 /* Now unlock the chip(s) POINT state */
841 /* ofs: offset within the first chip that the first read should start */
842 chipnum = (from >> cfi->chipshift);
843 ofs = from - (chipnum << cfi->chipshift);
846 unsigned long thislen;
849 chip = &cfi->chips[chipnum];
850 if (chipnum >= cfi->numchips)
853 if ((len + ofs -1) >> cfi->chipshift)
854 thislen = (1<<cfi->chipshift) - ofs;
858 spin_lock(chip->mutex);
859 if (chip->state == FL_POINT) {
860 chip->ref_point_counter--;
861 if(chip->ref_point_counter == 0)
862 chip->state = FL_READY;
864 printk(KERN_ERR "Warning: unpoint called on non pointed region\n"); /* Should this give an error? */
866 put_chip(map, chip, chip->start);
867 spin_unlock(chip->mutex);
875 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
877 unsigned long cmd_addr;
878 struct cfi_private *cfi = map->fldrv_priv;
883 /* Ensure cmd read/writes are aligned. */
884 cmd_addr = adr & ~(map_bankwidth(map)-1);
886 spin_lock(chip->mutex);
887 ret = get_chip(map, chip, cmd_addr, FL_READY);
889 spin_unlock(chip->mutex);
893 if (chip->state != FL_POINT && chip->state != FL_READY) {
894 map_write(map, CMD(0xff), cmd_addr);
896 chip->state = FL_READY;
899 map_copy_from(map, buf, adr, len);
901 put_chip(map, chip, cmd_addr);
903 spin_unlock(chip->mutex);
907 static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
909 struct map_info *map = mtd->priv;
910 struct cfi_private *cfi = map->fldrv_priv;
915 /* ofs: offset within the first chip that the first read should start */
916 chipnum = (from >> cfi->chipshift);
917 ofs = from - (chipnum << cfi->chipshift);
922 unsigned long thislen;
924 if (chipnum >= cfi->numchips)
927 if ((len + ofs -1) >> cfi->chipshift)
928 thislen = (1<<cfi->chipshift) - ofs;
932 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
947 static int cfi_intelext_read_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf, int base_offst, int reg_sz)
949 struct map_info *map = mtd->priv;
950 struct cfi_private *cfi = map->fldrv_priv;
951 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
953 int ofs_factor = cfi->interleave * cfi->device_type;
958 chip_num = ((unsigned int)from/reg_sz);
959 offst = from - (reg_sz*chip_num)+base_offst;
962 /* Calculate which chip & protection register offset we need */
964 if (chip_num >= cfi->numchips)
967 chip = &cfi->chips[chip_num];
969 spin_lock(chip->mutex);
970 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
972 spin_unlock(chip->mutex);
973 return (len-count)?:ret;
976 if (chip->state != FL_JEDEC_QUERY) {
977 map_write(map, CMD(0x90), chip->start);
978 chip->state = FL_JEDEC_QUERY;
981 while (count && ((offst-base_offst) < reg_sz)) {
982 *buf = map_read8(map,(chip->start+((extp->ProtRegAddr+1)*ofs_factor)+offst));
988 put_chip(map, chip, chip->start);
989 spin_unlock(chip->mutex);
991 /* Move on to the next chip */
1000 static int cfi_intelext_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1002 struct map_info *map = mtd->priv;
1003 struct cfi_private *cfi = map->fldrv_priv;
1004 struct cfi_pri_intelext *extp=cfi->cmdset_priv;
1005 int base_offst,reg_sz;
1007 /* Check that we actually have some protection registers */
1008 if(!extp || !(extp->FeatureSupport&64)){
1009 printk(KERN_WARNING "%s: This flash device has no protection data to read!\n",map->name);
1013 base_offst=(1<<extp->FactProtRegSize);
1014 reg_sz=(1<<extp->UserProtRegSize);
1016 return cfi_intelext_read_prot_reg(mtd, from, len, retlen, buf, base_offst, reg_sz);
1019 static int cfi_intelext_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1021 struct map_info *map = mtd->priv;
1022 struct cfi_private *cfi = map->fldrv_priv;
1023 struct cfi_pri_intelext *extp=cfi->cmdset_priv;
1024 int base_offst,reg_sz;
1026 /* Check that we actually have some protection registers */
1027 if(!extp || !(extp->FeatureSupport&64)){
1028 printk(KERN_WARNING "%s: This flash device has no protection data to read!\n",map->name);
1033 reg_sz=(1<<extp->FactProtRegSize);
1035 return cfi_intelext_read_prot_reg(mtd, from, len, retlen, buf, base_offst, reg_sz);
1039 static int do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
1041 struct cfi_private *cfi = map->fldrv_priv;
1042 map_word status, status_OK;
1043 unsigned long timeo;
1048 /* Let's determine this according to the interleave only once */
1049 status_OK = CMD(0x80);
1051 spin_lock(chip->mutex);
1052 ret = get_chip(map, chip, adr, FL_WRITING);
1054 spin_unlock(chip->mutex);
1059 map_write(map, CMD(0x40), adr);
1060 map_write(map, datum, adr);
1061 chip->state = FL_WRITING;
1063 spin_unlock(chip->mutex);
1064 INVALIDATE_CACHED_RANGE(map, adr, map_bankwidth(map));
1065 cfi_udelay(chip->word_write_time);
1066 spin_lock(chip->mutex);
1068 timeo = jiffies + (HZ/2);
1071 if (chip->state != FL_WRITING) {
1072 /* Someone's suspended the write. Sleep */
1073 DECLARE_WAITQUEUE(wait, current);
1075 set_current_state(TASK_UNINTERRUPTIBLE);
1076 add_wait_queue(&chip->wq, &wait);
1077 spin_unlock(chip->mutex);
1079 remove_wait_queue(&chip->wq, &wait);
1080 timeo = jiffies + (HZ / 2); /* FIXME */
1081 spin_lock(chip->mutex);
1085 status = map_read(map, adr);
1086 if (map_word_andequal(map, status, status_OK, status_OK))
1089 /* OK Still waiting */
1090 if (time_after(jiffies, timeo)) {
1091 chip->state = FL_STATUS;
1092 printk(KERN_ERR "waiting for chip to be ready timed out in word write\n");
1097 /* Latency issues. Drop the lock, wait a while and retry */
1098 spin_unlock(chip->mutex);
1101 spin_lock(chip->mutex);
1104 chip->word_write_time--;
1105 if (!chip->word_write_time)
1106 chip->word_write_time++;
1109 chip->word_write_time++;
1111 /* Done and happy. */
1112 chip->state = FL_STATUS;
1114 /* check for lock bit */
1115 if (map_word_bitsset(map, status, CMD(0x02))) {
1117 map_write(map, CMD(0x50), adr);
1118 /* put back into read status register mode */
1119 map_write(map, CMD(0x70), adr);
1123 put_chip(map, chip, adr);
1124 spin_unlock(chip->mutex);
1130 static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1132 struct map_info *map = mtd->priv;
1133 struct cfi_private *cfi = map->fldrv_priv;
1142 chipnum = to >> cfi->chipshift;
1143 ofs = to - (chipnum << cfi->chipshift);
1145 /* If it's not bus-aligned, do the first byte write */
1146 if (ofs & (map_bankwidth(map)-1)) {
1147 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1148 int gap = ofs - bus_ofs;
1152 n = min_t(int, len, map_bankwidth(map)-gap);
1153 datum = map_word_ff(map);
1154 datum = map_word_load_partial(map, datum, buf, gap, n);
1156 ret = do_write_oneword(map, &cfi->chips[chipnum],
1166 if (ofs >> cfi->chipshift) {
1169 if (chipnum == cfi->numchips)
1174 while(len >= map_bankwidth(map)) {
1175 map_word datum = map_word_load(map, buf);
1177 ret = do_write_oneword(map, &cfi->chips[chipnum],
1182 ofs += map_bankwidth(map);
1183 buf += map_bankwidth(map);
1184 (*retlen) += map_bankwidth(map);
1185 len -= map_bankwidth(map);
1187 if (ofs >> cfi->chipshift) {
1190 if (chipnum == cfi->numchips)
1195 if (len & (map_bankwidth(map)-1)) {
1198 datum = map_word_ff(map);
1199 datum = map_word_load_partial(map, datum, buf, 0, len);
1201 ret = do_write_oneword(map, &cfi->chips[chipnum],
1213 static inline int do_write_buffer(struct map_info *map, struct flchip *chip,
1214 unsigned long adr, const u_char *buf, int len)
1216 struct cfi_private *cfi = map->fldrv_priv;
1217 map_word status, status_OK;
1218 unsigned long cmd_adr, timeo;
1219 int wbufsize, z, ret=0, bytes, words;
1221 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1223 cmd_adr = adr & ~(wbufsize-1);
1225 /* Let's determine this according to the interleave only once */
1226 status_OK = CMD(0x80);
1228 spin_lock(chip->mutex);
1229 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1231 spin_unlock(chip->mutex);
1235 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
1236 [...], the device will not accept any more Write to Buffer commands".
1237 So we must check here and reset those bits if they're set. Otherwise
1238 we're just pissing in the wind */
1239 if (chip->state != FL_STATUS)
1240 map_write(map, CMD(0x70), cmd_adr);
1241 status = map_read(map, cmd_adr);
1242 if (map_word_bitsset(map, status, CMD(0x30))) {
1243 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1244 map_write(map, CMD(0x50), cmd_adr);
1245 map_write(map, CMD(0x70), cmd_adr);
1249 chip->state = FL_WRITING_TO_BUFFER;
1253 map_write(map, CMD(0xe8), cmd_adr);
1255 status = map_read(map, cmd_adr);
1256 if (map_word_andequal(map, status, status_OK, status_OK))
1259 spin_unlock(chip->mutex);
1261 spin_lock(chip->mutex);
1264 /* Argh. Not ready for write to buffer */
1266 map_write(map, CMD(0x70), cmd_adr);
1267 chip->state = FL_STATUS;
1268 Xstatus = map_read(map, cmd_adr);
1269 /* Odd. Clear status bits */
1270 map_write(map, CMD(0x50), cmd_adr);
1271 map_write(map, CMD(0x70), cmd_adr);
1272 printk(KERN_ERR "Chip not ready for buffer write. status = %lx, Xstatus = %lx\n",
1273 status.x[0], Xstatus.x[0]);
1279 /* Write length of data to come */
1280 bytes = len & (map_bankwidth(map)-1);
1281 words = len / map_bankwidth(map);
1282 map_write(map, CMD(words - !bytes), cmd_adr );
1286 while(z < words * map_bankwidth(map)) {
1287 map_word datum = map_word_load(map, buf);
1288 map_write(map, datum, adr+z);
1290 z += map_bankwidth(map);
1291 buf += map_bankwidth(map);
1297 datum = map_word_ff(map);
1298 datum = map_word_load_partial(map, datum, buf, 0, bytes);
1299 map_write(map, datum, adr+z);
1303 map_write(map, CMD(0xd0), cmd_adr);
1304 chip->state = FL_WRITING;
1306 spin_unlock(chip->mutex);
1307 INVALIDATE_CACHED_RANGE(map, adr, len);
1308 cfi_udelay(chip->buffer_write_time);
1309 spin_lock(chip->mutex);
1311 timeo = jiffies + (HZ/2);
1314 if (chip->state != FL_WRITING) {
1315 /* Someone's suspended the write. Sleep */
1316 DECLARE_WAITQUEUE(wait, current);
1317 set_current_state(TASK_UNINTERRUPTIBLE);
1318 add_wait_queue(&chip->wq, &wait);
1319 spin_unlock(chip->mutex);
1321 remove_wait_queue(&chip->wq, &wait);
1322 timeo = jiffies + (HZ / 2); /* FIXME */
1323 spin_lock(chip->mutex);
1327 status = map_read(map, cmd_adr);
1328 if (map_word_andequal(map, status, status_OK, status_OK))
1331 /* OK Still waiting */
1332 if (time_after(jiffies, timeo)) {
1333 chip->state = FL_STATUS;
1334 printk(KERN_ERR "waiting for chip to be ready timed out in bufwrite\n");
1339 /* Latency issues. Drop the lock, wait a while and retry */
1340 spin_unlock(chip->mutex);
1343 spin_lock(chip->mutex);
1346 chip->buffer_write_time--;
1347 if (!chip->buffer_write_time)
1348 chip->buffer_write_time++;
1351 chip->buffer_write_time++;
1353 /* Done and happy. */
1354 chip->state = FL_STATUS;
1356 /* check for lock bit */
1357 if (map_word_bitsset(map, status, CMD(0x02))) {
1359 map_write(map, CMD(0x50), cmd_adr);
1360 /* put back into read status register mode */
1361 map_write(map, CMD(0x70), adr);
1366 put_chip(map, chip, cmd_adr);
1367 spin_unlock(chip->mutex);
1371 static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1372 size_t len, size_t *retlen, const u_char *buf)
1374 struct map_info *map = mtd->priv;
1375 struct cfi_private *cfi = map->fldrv_priv;
1376 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1385 chipnum = to >> cfi->chipshift;
1386 ofs = to - (chipnum << cfi->chipshift);
1388 /* If it's not bus-aligned, do the first word write */
1389 if (ofs & (map_bankwidth(map)-1)) {
1390 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1391 if (local_len > len)
1393 ret = cfi_intelext_write_words(mtd, to, local_len,
1401 if (ofs >> cfi->chipshift) {
1404 if (chipnum == cfi->numchips)
1410 /* We must not cross write block boundaries */
1411 int size = wbufsize - (ofs & (wbufsize-1));
1415 ret = do_write_buffer(map, &cfi->chips[chipnum],
1425 if (ofs >> cfi->chipshift) {
1428 if (chipnum == cfi->numchips)
1435 static int do_erase_oneblock(struct map_info *map, struct flchip *chip,
1436 unsigned long adr, int len, void *thunk)
1438 struct cfi_private *cfi = map->fldrv_priv;
1439 map_word status, status_OK;
1440 unsigned long timeo;
1442 DECLARE_WAITQUEUE(wait, current);
1447 /* Let's determine this according to the interleave only once */
1448 status_OK = CMD(0x80);
1451 spin_lock(chip->mutex);
1452 ret = get_chip(map, chip, adr, FL_ERASING);
1454 spin_unlock(chip->mutex);
1459 /* Clear the status register first */
1460 map_write(map, CMD(0x50), adr);
1463 map_write(map, CMD(0x20), adr);
1464 map_write(map, CMD(0xD0), adr);
1465 chip->state = FL_ERASING;
1466 chip->erase_suspended = 0;
1468 spin_unlock(chip->mutex);
1469 INVALIDATE_CACHED_RANGE(map, adr, len);
1470 msleep(chip->erase_time / 2);
1471 spin_lock(chip->mutex);
1473 /* FIXME. Use a timer to check this, and return immediately. */
1474 /* Once the state machine's known to be working I'll do that */
1476 timeo = jiffies + (HZ*20);
1478 if (chip->state != FL_ERASING) {
1479 /* Someone's suspended the erase. Sleep */
1480 set_current_state(TASK_UNINTERRUPTIBLE);
1481 add_wait_queue(&chip->wq, &wait);
1482 spin_unlock(chip->mutex);
1484 remove_wait_queue(&chip->wq, &wait);
1485 spin_lock(chip->mutex);
1488 if (chip->erase_suspended) {
1489 /* This erase was suspended and resumed.
1490 Adjust the timeout */
1491 timeo = jiffies + (HZ*20); /* FIXME */
1492 chip->erase_suspended = 0;
1495 status = map_read(map, adr);
1496 if (map_word_andequal(map, status, status_OK, status_OK))
1499 /* OK Still waiting */
1500 if (time_after(jiffies, timeo)) {
1502 map_write(map, CMD(0x70), adr);
1503 chip->state = FL_STATUS;
1504 Xstatus = map_read(map, adr);
1505 /* Clear status bits */
1506 map_write(map, CMD(0x50), adr);
1507 map_write(map, CMD(0x70), adr);
1508 printk(KERN_ERR "waiting for erase at %08lx to complete timed out. status = %lx, Xstatus = %lx.\n",
1509 adr, status.x[0], Xstatus.x[0]);
1514 /* Latency issues. Drop the lock, wait a while and retry */
1515 spin_unlock(chip->mutex);
1516 set_current_state(TASK_UNINTERRUPTIBLE);
1517 schedule_timeout(1);
1518 spin_lock(chip->mutex);
1521 /* We've broken this before. It doesn't hurt to be safe */
1522 map_write(map, CMD(0x70), adr);
1523 chip->state = FL_STATUS;
1524 status = map_read(map, adr);
1526 /* check for lock bit */
1527 if (map_word_bitsset(map, status, CMD(0x3a))) {
1528 unsigned char chipstatus;
1530 /* Reset the error bits */
1531 map_write(map, CMD(0x50), adr);
1532 map_write(map, CMD(0x70), adr);
1534 chipstatus = status.x[0];
1535 if (!map_word_equal(map, status, CMD(chipstatus))) {
1537 for (w=0; w<map_words(map); w++) {
1538 for (i = 0; i<cfi_interleave(cfi); i++) {
1539 chipstatus |= status.x[w] >> (cfi->device_type * 8);
1542 printk(KERN_WARNING "Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
1543 status.x[0], chipstatus);
1546 if ((chipstatus & 0x30) == 0x30) {
1547 printk(KERN_NOTICE "Chip reports improper command sequence: status 0x%x\n", chipstatus);
1549 } else if (chipstatus & 0x02) {
1550 /* Protection bit set */
1552 } else if (chipstatus & 0x8) {
1554 printk(KERN_WARNING "Chip reports voltage low on erase: status 0x%x\n", chipstatus);
1556 } else if (chipstatus & 0x20) {
1558 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr, chipstatus);
1559 timeo = jiffies + HZ;
1560 put_chip(map, chip, adr);
1561 spin_unlock(chip->mutex);
1564 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x\n", adr, chipstatus);
1571 out: put_chip(map, chip, adr);
1572 spin_unlock(chip->mutex);
1576 int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1578 unsigned long ofs, len;
1584 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1588 instr->state = MTD_ERASE_DONE;
1589 mtd_erase_callback(instr);
1594 static void cfi_intelext_sync (struct mtd_info *mtd)
1596 struct map_info *map = mtd->priv;
1597 struct cfi_private *cfi = map->fldrv_priv;
1599 struct flchip *chip;
1602 for (i=0; !ret && i<cfi->numchips; i++) {
1603 chip = &cfi->chips[i];
1605 spin_lock(chip->mutex);
1606 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1609 chip->oldstate = chip->state;
1610 chip->state = FL_SYNCING;
1611 /* No need to wake_up() on this state change -
1612 * as the whole point is that nobody can do anything
1613 * with the chip now anyway.
1616 spin_unlock(chip->mutex);
1619 /* Unlock the chips again */
1621 for (i--; i >=0; i--) {
1622 chip = &cfi->chips[i];
1624 spin_lock(chip->mutex);
1626 if (chip->state == FL_SYNCING) {
1627 chip->state = chip->oldstate;
1630 spin_unlock(chip->mutex);
1634 #ifdef DEBUG_LOCK_BITS
1635 static int do_printlockstatus_oneblock(struct map_info *map, struct flchip *chip,
1636 unsigned long adr, int len, void *thunk)
1638 struct cfi_private *cfi = map->fldrv_priv;
1639 int status, ofs_factor = cfi->interleave * cfi->device_type;
1641 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1642 chip->state = FL_JEDEC_QUERY;
1643 status = cfi_read_query(map, adr+(2*ofs_factor));
1644 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
1650 #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1651 #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1653 static int do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
1654 unsigned long adr, int len, void *thunk)
1656 struct cfi_private *cfi = map->fldrv_priv;
1657 map_word status, status_OK;
1658 unsigned long timeo = jiffies + HZ;
1663 /* Let's determine this according to the interleave only once */
1664 status_OK = CMD(0x80);
1666 spin_lock(chip->mutex);
1667 ret = get_chip(map, chip, adr, FL_LOCKING);
1669 spin_unlock(chip->mutex);
1674 map_write(map, CMD(0x60), adr);
1676 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
1677 map_write(map, CMD(0x01), adr);
1678 chip->state = FL_LOCKING;
1679 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
1680 map_write(map, CMD(0xD0), adr);
1681 chip->state = FL_UNLOCKING;
1685 spin_unlock(chip->mutex);
1686 schedule_timeout(HZ);
1687 spin_lock(chip->mutex);
1689 /* FIXME. Use a timer to check this, and return immediately. */
1690 /* Once the state machine's known to be working I'll do that */
1692 timeo = jiffies + (HZ*20);
1695 status = map_read(map, adr);
1696 if (map_word_andequal(map, status, status_OK, status_OK))
1699 /* OK Still waiting */
1700 if (time_after(jiffies, timeo)) {
1702 map_write(map, CMD(0x70), adr);
1703 chip->state = FL_STATUS;
1704 Xstatus = map_read(map, adr);
1705 printk(KERN_ERR "waiting for unlock to complete timed out. status = %lx, Xstatus = %lx.\n",
1706 status.x[0], Xstatus.x[0]);
1707 put_chip(map, chip, adr);
1708 spin_unlock(chip->mutex);
1712 /* Latency issues. Drop the lock, wait a while and retry */
1713 spin_unlock(chip->mutex);
1715 spin_lock(chip->mutex);
1718 /* Done and happy. */
1719 chip->state = FL_STATUS;
1720 put_chip(map, chip, adr);
1721 spin_unlock(chip->mutex);
1725 static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1729 #ifdef DEBUG_LOCK_BITS
1730 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
1731 __FUNCTION__, ofs, len);
1732 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
1736 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
1737 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
1739 #ifdef DEBUG_LOCK_BITS
1740 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
1742 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
1749 static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1753 #ifdef DEBUG_LOCK_BITS
1754 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
1755 __FUNCTION__, ofs, len);
1756 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
1760 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
1761 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
1763 #ifdef DEBUG_LOCK_BITS
1764 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
1766 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
1773 static int cfi_intelext_suspend(struct mtd_info *mtd)
1775 struct map_info *map = mtd->priv;
1776 struct cfi_private *cfi = map->fldrv_priv;
1778 struct flchip *chip;
1781 for (i=0; !ret && i<cfi->numchips; i++) {
1782 chip = &cfi->chips[i];
1784 spin_lock(chip->mutex);
1786 switch (chip->state) {
1790 case FL_JEDEC_QUERY:
1791 if (chip->oldstate == FL_READY) {
1792 chip->oldstate = chip->state;
1793 chip->state = FL_PM_SUSPENDED;
1794 /* No need to wake_up() on this state change -
1795 * as the whole point is that nobody can do anything
1796 * with the chip now anyway.
1799 /* There seems to be an operation pending. We must wait for it. */
1800 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
1805 /* Should we actually wait? Once upon a time these routines weren't
1806 allowed to. Or should we return -EAGAIN, because the upper layers
1807 ought to have already shut down anything which was using the device
1808 anyway? The latter for now. */
1809 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
1811 case FL_PM_SUSPENDED:
1814 spin_unlock(chip->mutex);
1817 /* Unlock the chips again */
1820 for (i--; i >=0; i--) {
1821 chip = &cfi->chips[i];
1823 spin_lock(chip->mutex);
1825 if (chip->state == FL_PM_SUSPENDED) {
1826 /* No need to force it into a known state here,
1827 because we're returning failure, and it didn't
1829 chip->state = chip->oldstate;
1830 chip->oldstate = FL_READY;
1833 spin_unlock(chip->mutex);
1840 static void cfi_intelext_resume(struct mtd_info *mtd)
1842 struct map_info *map = mtd->priv;
1843 struct cfi_private *cfi = map->fldrv_priv;
1845 struct flchip *chip;
1847 for (i=0; i<cfi->numchips; i++) {
1849 chip = &cfi->chips[i];
1851 spin_lock(chip->mutex);
1853 /* Go to known state. Chip may have been power cycled */
1854 if (chip->state == FL_PM_SUSPENDED) {
1855 map_write(map, CMD(0xFF), cfi->chips[i].start);
1856 chip->oldstate = chip->state = FL_READY;
1860 spin_unlock(chip->mutex);
1864 static void cfi_intelext_destroy(struct mtd_info *mtd)
1866 struct map_info *map = mtd->priv;
1867 struct cfi_private *cfi = map->fldrv_priv;
1868 kfree(cfi->cmdset_priv);
1870 kfree(cfi->chips[0].priv);
1872 kfree(mtd->eraseregions);
1875 static char im_name_1[]="cfi_cmdset_0001";
1876 static char im_name_3[]="cfi_cmdset_0003";
1878 int __init cfi_intelext_init(void)
1880 inter_module_register(im_name_1, THIS_MODULE, &cfi_cmdset_0001);
1881 inter_module_register(im_name_3, THIS_MODULE, &cfi_cmdset_0001);
1885 static void __exit cfi_intelext_exit(void)
1887 inter_module_unregister(im_name_1);
1888 inter_module_unregister(im_name_3);
1891 module_init(cfi_intelext_init);
1892 module_exit(cfi_intelext_exit);
1894 MODULE_LICENSE("GPL");
1895 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1896 MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");