2 * Common Flash Interface support:
3 * ST Advanced Architecture Command Set (ID 0x0020)
5 * (C) 2000 Red Hat. GPL'd
7 * $Id: cfi_cmdset_0020.c,v 1.22 2005/11/07 11:14:22 gleixner Exp $
9 * 10/10/2000 Nicolas Pitre <nico@cam.org>
10 * - completely revamped method functions so they are aware and
11 * independent of the flash geometry (buswidth, interleave, etc.)
12 * - scalability vs code size is completely set at compile-time
13 * (see include/linux/mtd/cfi.h for selection)
14 * - optimized write buffer method
15 * 06/21/2002 Joern Engel <joern@wh.fh-wedel.de> and others
16 * - modified Intel Command Set 0x0001 to support ST Advanced Architecture
17 * (command set 0x0020)
18 * - added a writev function
19 * 07/13/2005 Joern Engel <joern@wh.fh-wedel.de>
20 * - Plugged memory leak in cfi_staa_writev().
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/init.h>
29 #include <asm/byteorder.h>
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/interrupt.h>
35 #include <linux/mtd/map.h>
36 #include <linux/mtd/cfi.h>
37 #include <linux/mtd/mtd.h>
38 #include <linux/mtd/compatmac.h>
41 static int cfi_staa_read(struct mtd_info *, loff_t, size_t, size_t *, u_char *);
42 static int cfi_staa_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
43 static int cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs,
44 unsigned long count, loff_t to, size_t *retlen);
45 static int cfi_staa_erase_varsize(struct mtd_info *, struct erase_info *);
46 static void cfi_staa_sync (struct mtd_info *);
47 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
48 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
49 static int cfi_staa_suspend (struct mtd_info *);
50 static void cfi_staa_resume (struct mtd_info *);
52 static void cfi_staa_destroy(struct mtd_info *);
54 struct mtd_info *cfi_cmdset_0020(struct map_info *, int);
56 static struct mtd_info *cfi_staa_setup (struct map_info *);
58 static struct mtd_chip_driver cfi_staa_chipdrv = {
59 .probe = NULL, /* Not usable directly */
60 .destroy = cfi_staa_destroy,
61 .name = "cfi_cmdset_0020",
65 /* #define DEBUG_LOCK_BITS */
66 //#define DEBUG_CFI_FEATURES
68 #ifdef DEBUG_CFI_FEATURES
69 static void cfi_tell_features(struct cfi_pri_intelext *extp)
72 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
73 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
74 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
75 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
76 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
77 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
78 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
79 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
80 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
81 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
82 for (i=9; i<32; i++) {
83 if (extp->FeatureSupport & (1<<i))
84 printk(" - Unknown Bit %X: supported\n", i);
87 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
88 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
90 if (extp->SuspendCmdSupport & (1<<i))
91 printk(" - Unknown Bit %X: supported\n", i);
94 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
95 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
96 printk(" - Valid Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
97 for (i=2; i<16; i++) {
98 if (extp->BlkStatusRegMask & (1<<i))
99 printk(" - Unknown Bit %X Active: yes\n",i);
102 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
103 extp->VccOptimal >> 8, extp->VccOptimal & 0xf);
104 if (extp->VppOptimal)
105 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
106 extp->VppOptimal >> 8, extp->VppOptimal & 0xf);
110 /* This routine is made available to other mtd code via
111 * inter_module_register. It must only be accessed through
112 * inter_module_get which will bump the use count of this module. The
113 * addresses passed back in cfi are valid as long as the use count of
114 * this module is non-zero, i.e. between inter_module_get and
115 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
117 struct mtd_info *cfi_cmdset_0020(struct map_info *map, int primary)
119 struct cfi_private *cfi = map->fldrv_priv;
124 * It's a real CFI chip, not one for which the probe
125 * routine faked a CFI structure. So we read the feature
128 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
129 struct cfi_pri_intelext *extp;
131 extp = (struct cfi_pri_intelext*)cfi_read_pri(map, adr, sizeof(*extp), "ST Microelectronics");
135 if (extp->MajorVersion != '1' ||
136 (extp->MinorVersion < '0' || extp->MinorVersion > '3')) {
137 printk(KERN_ERR " Unknown ST Microelectronics"
138 " Extended Query version %c.%c.\n",
139 extp->MajorVersion, extp->MinorVersion);
144 /* Do some byteswapping if necessary */
145 extp->FeatureSupport = cfi32_to_cpu(extp->FeatureSupport);
146 extp->BlkStatusRegMask = cfi32_to_cpu(extp->BlkStatusRegMask);
148 #ifdef DEBUG_CFI_FEATURES
149 /* Tell the user about it in lots of lovely detail */
150 cfi_tell_features(extp);
153 /* Install our own private info structure */
154 cfi->cmdset_priv = extp;
157 for (i=0; i< cfi->numchips; i++) {
158 cfi->chips[i].word_write_time = 128;
159 cfi->chips[i].buffer_write_time = 128;
160 cfi->chips[i].erase_time = 1024;
163 return cfi_staa_setup(map);
165 EXPORT_SYMBOL_GPL(cfi_cmdset_0020);
167 static struct mtd_info *cfi_staa_setup(struct map_info *map)
169 struct cfi_private *cfi = map->fldrv_priv;
170 struct mtd_info *mtd;
171 unsigned long offset = 0;
173 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
175 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
176 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
179 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
180 kfree(cfi->cmdset_priv);
185 mtd->type = MTD_NORFLASH;
186 mtd->size = devsize * cfi->numchips;
188 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
189 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
190 * mtd->numeraseregions, GFP_KERNEL);
191 if (!mtd->eraseregions) {
192 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
193 kfree(cfi->cmdset_priv);
198 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
199 unsigned long ernum, ersize;
200 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
201 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
203 if (mtd->erasesize < ersize) {
204 mtd->erasesize = ersize;
206 for (j=0; j<cfi->numchips; j++) {
207 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
208 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
209 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
211 offset += (ersize * ernum);
214 if (offset != devsize) {
216 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
217 kfree(mtd->eraseregions);
218 kfree(cfi->cmdset_priv);
223 for (i=0; i<mtd->numeraseregions;i++){
224 printk(KERN_DEBUG "%d: offset=0x%x,size=0x%x,blocks=%d\n",
225 i,mtd->eraseregions[i].offset,
226 mtd->eraseregions[i].erasesize,
227 mtd->eraseregions[i].numblocks);
230 /* Also select the correct geometry setup too */
231 mtd->erase = cfi_staa_erase_varsize;
232 mtd->read = cfi_staa_read;
233 mtd->write = cfi_staa_write_buffers;
234 mtd->writev = cfi_staa_writev;
235 mtd->sync = cfi_staa_sync;
236 mtd->lock = cfi_staa_lock;
237 mtd->unlock = cfi_staa_unlock;
238 mtd->suspend = cfi_staa_suspend;
239 mtd->resume = cfi_staa_resume;
240 mtd->flags = MTD_CAP_NORFLASH & ~MTD_BIT_WRITEABLE;
241 mtd->writesize = 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */
242 map->fldrv = &cfi_staa_chipdrv;
243 __module_get(THIS_MODULE);
244 mtd->name = map->name;
249 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
251 map_word status, status_OK;
253 DECLARE_WAITQUEUE(wait, current);
255 unsigned long cmd_addr;
256 struct cfi_private *cfi = map->fldrv_priv;
260 /* Ensure cmd read/writes are aligned. */
261 cmd_addr = adr & ~(map_bankwidth(map)-1);
263 /* Let's determine this according to the interleave only once */
264 status_OK = CMD(0x80);
266 timeo = jiffies + HZ;
268 spin_lock_bh(chip->mutex);
270 /* Check that the chip's ready to talk to us.
271 * If it's in FL_ERASING state, suspend it and make it talk now.
273 switch (chip->state) {
275 if (!(((struct cfi_pri_intelext *)cfi->cmdset_priv)->FeatureSupport & 2))
276 goto sleep; /* We don't support erase suspend */
278 map_write (map, CMD(0xb0), cmd_addr);
279 /* If the flash has finished erasing, then 'erase suspend'
280 * appears to make some (28F320) flash devices switch to
281 * 'read' mode. Make sure that we switch to 'read status'
282 * mode so we get the right data. --rmk
284 map_write(map, CMD(0x70), cmd_addr);
285 chip->oldstate = FL_ERASING;
286 chip->state = FL_ERASE_SUSPENDING;
287 // printk("Erase suspending at 0x%lx\n", cmd_addr);
289 status = map_read(map, cmd_addr);
290 if (map_word_andequal(map, status, status_OK, status_OK))
293 if (time_after(jiffies, timeo)) {
295 map_write(map, CMD(0xd0), cmd_addr);
296 /* make sure we're in 'read status' mode */
297 map_write(map, CMD(0x70), cmd_addr);
298 chip->state = FL_ERASING;
299 spin_unlock_bh(chip->mutex);
300 printk(KERN_ERR "Chip not ready after erase "
301 "suspended: status = 0x%lx\n", status.x[0]);
305 spin_unlock_bh(chip->mutex);
307 spin_lock_bh(chip->mutex);
311 map_write(map, CMD(0xff), cmd_addr);
312 chip->state = FL_READY;
325 map_write(map, CMD(0x70), cmd_addr);
326 chip->state = FL_STATUS;
329 status = map_read(map, cmd_addr);
330 if (map_word_andequal(map, status, status_OK, status_OK)) {
331 map_write(map, CMD(0xff), cmd_addr);
332 chip->state = FL_READY;
336 /* Urgh. Chip not yet ready to talk to us. */
337 if (time_after(jiffies, timeo)) {
338 spin_unlock_bh(chip->mutex);
339 printk(KERN_ERR "waiting for chip to be ready timed out in read. WSM status = %lx\n", status.x[0]);
343 /* Latency issues. Drop the lock, wait a while and retry */
344 spin_unlock_bh(chip->mutex);
350 /* Stick ourselves on a wait queue to be woken when
351 someone changes the status */
352 set_current_state(TASK_UNINTERRUPTIBLE);
353 add_wait_queue(&chip->wq, &wait);
354 spin_unlock_bh(chip->mutex);
356 remove_wait_queue(&chip->wq, &wait);
357 timeo = jiffies + HZ;
361 map_copy_from(map, buf, adr, len);
364 chip->state = chip->oldstate;
365 /* What if one interleaved chip has finished and the
366 other hasn't? The old code would leave the finished
367 one in READY mode. That's bad, and caused -EROFS
368 errors to be returned from do_erase_oneblock because
369 that's the only bit it checked for at the time.
370 As the state machine appears to explicitly allow
371 sending the 0x70 (Read Status) command to an erasing
372 chip and expecting it to be ignored, that's what we
374 map_write(map, CMD(0xd0), cmd_addr);
375 map_write(map, CMD(0x70), cmd_addr);
379 spin_unlock_bh(chip->mutex);
383 static int cfi_staa_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
385 struct map_info *map = mtd->priv;
386 struct cfi_private *cfi = map->fldrv_priv;
391 /* ofs: offset within the first chip that the first read should start */
392 chipnum = (from >> cfi->chipshift);
393 ofs = from - (chipnum << cfi->chipshift);
398 unsigned long thislen;
400 if (chipnum >= cfi->numchips)
403 if ((len + ofs -1) >> cfi->chipshift)
404 thislen = (1<<cfi->chipshift) - ofs;
408 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
422 static inline int do_write_buffer(struct map_info *map, struct flchip *chip,
423 unsigned long adr, const u_char *buf, int len)
425 struct cfi_private *cfi = map->fldrv_priv;
426 map_word status, status_OK;
427 unsigned long cmd_adr, timeo;
428 DECLARE_WAITQUEUE(wait, current);
431 /* M58LW064A requires bus alignment for buffer wriets -- saw */
432 if (adr & (map_bankwidth(map)-1))
435 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
437 cmd_adr = adr & ~(wbufsize-1);
439 /* Let's determine this according to the interleave only once */
440 status_OK = CMD(0x80);
442 timeo = jiffies + HZ;
445 #ifdef DEBUG_CFI_FEATURES
446 printk("%s: chip->state[%d]\n", __FUNCTION__, chip->state);
448 spin_lock_bh(chip->mutex);
450 /* Check that the chip's ready to talk to us.
451 * Later, we can actually think about interrupting it
452 * if it's in FL_ERASING state.
453 * Not just yet, though.
455 switch (chip->state) {
461 map_write(map, CMD(0x70), cmd_adr);
462 chip->state = FL_STATUS;
463 #ifdef DEBUG_CFI_FEATURES
464 printk("%s: 1 status[%x]\n", __FUNCTION__, map_read(map, cmd_adr));
468 status = map_read(map, cmd_adr);
469 if (map_word_andequal(map, status, status_OK, status_OK))
471 /* Urgh. Chip not yet ready to talk to us. */
472 if (time_after(jiffies, timeo)) {
473 spin_unlock_bh(chip->mutex);
474 printk(KERN_ERR "waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
475 status.x[0], map_read(map, cmd_adr).x[0]);
479 /* Latency issues. Drop the lock, wait a while and retry */
480 spin_unlock_bh(chip->mutex);
485 /* Stick ourselves on a wait queue to be woken when
486 someone changes the status */
487 set_current_state(TASK_UNINTERRUPTIBLE);
488 add_wait_queue(&chip->wq, &wait);
489 spin_unlock_bh(chip->mutex);
491 remove_wait_queue(&chip->wq, &wait);
492 timeo = jiffies + HZ;
497 map_write(map, CMD(0xe8), cmd_adr);
498 chip->state = FL_WRITING_TO_BUFFER;
502 status = map_read(map, cmd_adr);
503 if (map_word_andequal(map, status, status_OK, status_OK))
506 spin_unlock_bh(chip->mutex);
508 spin_lock_bh(chip->mutex);
511 /* Argh. Not ready for write to buffer */
513 map_write(map, CMD(0x70), cmd_adr);
514 chip->state = FL_STATUS;
515 spin_unlock_bh(chip->mutex);
516 printk(KERN_ERR "Chip not ready for buffer write. Xstatus = %lx\n", status.x[0]);
521 /* Write length of data to come */
522 map_write(map, CMD(len/map_bankwidth(map)-1), cmd_adr );
526 z += map_bankwidth(map), buf += map_bankwidth(map)) {
528 d = map_word_load(map, buf);
529 map_write(map, d, adr+z);
532 map_write(map, CMD(0xd0), cmd_adr);
533 chip->state = FL_WRITING;
535 spin_unlock_bh(chip->mutex);
536 cfi_udelay(chip->buffer_write_time);
537 spin_lock_bh(chip->mutex);
539 timeo = jiffies + (HZ/2);
542 if (chip->state != FL_WRITING) {
543 /* Someone's suspended the write. Sleep */
544 set_current_state(TASK_UNINTERRUPTIBLE);
545 add_wait_queue(&chip->wq, &wait);
546 spin_unlock_bh(chip->mutex);
548 remove_wait_queue(&chip->wq, &wait);
549 timeo = jiffies + (HZ / 2); /* FIXME */
550 spin_lock_bh(chip->mutex);
554 status = map_read(map, cmd_adr);
555 if (map_word_andequal(map, status, status_OK, status_OK))
558 /* OK Still waiting */
559 if (time_after(jiffies, timeo)) {
561 map_write(map, CMD(0x50), cmd_adr);
562 /* put back into read status register mode */
563 map_write(map, CMD(0x70), adr);
564 chip->state = FL_STATUS;
566 spin_unlock_bh(chip->mutex);
567 printk(KERN_ERR "waiting for chip to be ready timed out in bufwrite\n");
571 /* Latency issues. Drop the lock, wait a while and retry */
572 spin_unlock_bh(chip->mutex);
575 spin_lock_bh(chip->mutex);
578 chip->buffer_write_time--;
579 if (!chip->buffer_write_time)
580 chip->buffer_write_time++;
583 chip->buffer_write_time++;
585 /* Done and happy. */
587 chip->state = FL_STATUS;
589 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
590 if (map_word_bitsset(map, status, CMD(0x3a))) {
591 #ifdef DEBUG_CFI_FEATURES
592 printk("%s: 2 status[%lx]\n", __FUNCTION__, status.x[0]);
595 map_write(map, CMD(0x50), cmd_adr);
596 /* put back into read status register mode */
597 map_write(map, CMD(0x70), adr);
599 spin_unlock_bh(chip->mutex);
600 return map_word_bitsset(map, status, CMD(0x02)) ? -EROFS : -EIO;
603 spin_unlock_bh(chip->mutex);
608 static int cfi_staa_write_buffers (struct mtd_info *mtd, loff_t to,
609 size_t len, size_t *retlen, const u_char *buf)
611 struct map_info *map = mtd->priv;
612 struct cfi_private *cfi = map->fldrv_priv;
613 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
622 chipnum = to >> cfi->chipshift;
623 ofs = to - (chipnum << cfi->chipshift);
625 #ifdef DEBUG_CFI_FEATURES
626 printk("%s: map_bankwidth(map)[%x]\n", __FUNCTION__, map_bankwidth(map));
627 printk("%s: chipnum[%x] wbufsize[%x]\n", __FUNCTION__, chipnum, wbufsize);
628 printk("%s: ofs[%x] len[%x]\n", __FUNCTION__, ofs, len);
631 /* Write buffer is worth it only if more than one word to write... */
633 /* We must not cross write block boundaries */
634 int size = wbufsize - (ofs & (wbufsize-1));
639 ret = do_write_buffer(map, &cfi->chips[chipnum],
649 if (ofs >> cfi->chipshift) {
652 if (chipnum == cfi->numchips)
661 * Writev for ECC-Flashes is a little more complicated. We need to maintain
662 * a small buffer for this.
663 * XXX: If the buffer size is not a multiple of 2, this will break
665 #define ECCBUF_SIZE (mtd->eccsize)
666 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
667 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
669 cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs,
670 unsigned long count, loff_t to, size_t *retlen)
673 size_t totlen = 0, thislen;
679 /* We should fall back to a general writev implementation.
680 * Until that is written, just break.
684 buffer = kmalloc(ECCBUF_SIZE, GFP_KERNEL);
688 for (i=0; i<count; i++) {
689 size_t elem_len = vecs[i].iov_len;
690 void *elem_base = vecs[i].iov_base;
691 if (!elem_len) /* FIXME: Might be unnecessary. Check that */
693 if (buflen) { /* cut off head */
694 if (buflen + elem_len < ECCBUF_SIZE) { /* just accumulate */
695 memcpy(buffer+buflen, elem_base, elem_len);
699 memcpy(buffer+buflen, elem_base, ECCBUF_SIZE-buflen);
700 ret = mtd->write(mtd, to, ECCBUF_SIZE, &thislen, buffer);
702 if (ret || thislen != ECCBUF_SIZE)
704 elem_len -= thislen-buflen;
705 elem_base += thislen-buflen;
708 if (ECCBUF_DIV(elem_len)) { /* write clean aligned data */
709 ret = mtd->write(mtd, to, ECCBUF_DIV(elem_len), &thislen, elem_base);
711 if (ret || thislen != ECCBUF_DIV(elem_len))
715 buflen = ECCBUF_MOD(elem_len); /* cut off tail */
717 memset(buffer, 0xff, ECCBUF_SIZE);
718 memcpy(buffer, elem_base + thislen, buflen);
721 if (buflen) { /* flush last page, even if not full */
722 /* This is sometimes intended behaviour, really */
723 ret = mtd->write(mtd, to, buflen, &thislen, buffer);
725 if (ret || thislen != ECCBUF_SIZE)
736 static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
738 struct cfi_private *cfi = map->fldrv_priv;
739 map_word status, status_OK;
742 DECLARE_WAITQUEUE(wait, current);
747 /* Let's determine this according to the interleave only once */
748 status_OK = CMD(0x80);
750 timeo = jiffies + HZ;
752 spin_lock_bh(chip->mutex);
754 /* Check that the chip's ready to talk to us. */
755 switch (chip->state) {
759 map_write(map, CMD(0x70), adr);
760 chip->state = FL_STATUS;
763 status = map_read(map, adr);
764 if (map_word_andequal(map, status, status_OK, status_OK))
767 /* Urgh. Chip not yet ready to talk to us. */
768 if (time_after(jiffies, timeo)) {
769 spin_unlock_bh(chip->mutex);
770 printk(KERN_ERR "waiting for chip to be ready timed out in erase\n");
774 /* Latency issues. Drop the lock, wait a while and retry */
775 spin_unlock_bh(chip->mutex);
780 /* Stick ourselves on a wait queue to be woken when
781 someone changes the status */
782 set_current_state(TASK_UNINTERRUPTIBLE);
783 add_wait_queue(&chip->wq, &wait);
784 spin_unlock_bh(chip->mutex);
786 remove_wait_queue(&chip->wq, &wait);
787 timeo = jiffies + HZ;
792 /* Clear the status register first */
793 map_write(map, CMD(0x50), adr);
796 map_write(map, CMD(0x20), adr);
797 map_write(map, CMD(0xD0), adr);
798 chip->state = FL_ERASING;
800 spin_unlock_bh(chip->mutex);
802 spin_lock_bh(chip->mutex);
804 /* FIXME. Use a timer to check this, and return immediately. */
805 /* Once the state machine's known to be working I'll do that */
807 timeo = jiffies + (HZ*20);
809 if (chip->state != FL_ERASING) {
810 /* Someone's suspended the erase. Sleep */
811 set_current_state(TASK_UNINTERRUPTIBLE);
812 add_wait_queue(&chip->wq, &wait);
813 spin_unlock_bh(chip->mutex);
815 remove_wait_queue(&chip->wq, &wait);
816 timeo = jiffies + (HZ*20); /* FIXME */
817 spin_lock_bh(chip->mutex);
821 status = map_read(map, adr);
822 if (map_word_andequal(map, status, status_OK, status_OK))
825 /* OK Still waiting */
826 if (time_after(jiffies, timeo)) {
827 map_write(map, CMD(0x70), adr);
828 chip->state = FL_STATUS;
829 printk(KERN_ERR "waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
831 spin_unlock_bh(chip->mutex);
835 /* Latency issues. Drop the lock, wait a while and retry */
836 spin_unlock_bh(chip->mutex);
838 spin_lock_bh(chip->mutex);
844 /* We've broken this before. It doesn't hurt to be safe */
845 map_write(map, CMD(0x70), adr);
846 chip->state = FL_STATUS;
847 status = map_read(map, adr);
849 /* check for lock bit */
850 if (map_word_bitsset(map, status, CMD(0x3a))) {
851 unsigned char chipstatus = status.x[0];
852 if (!map_word_equal(map, status, CMD(chipstatus))) {
854 for (w=0; w<map_words(map); w++) {
855 for (i = 0; i<cfi_interleave(cfi); i++) {
856 chipstatus |= status.x[w] >> (cfi->device_type * 8);
859 printk(KERN_WARNING "Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
860 status.x[0], chipstatus);
862 /* Reset the error bits */
863 map_write(map, CMD(0x50), adr);
864 map_write(map, CMD(0x70), adr);
866 if ((chipstatus & 0x30) == 0x30) {
867 printk(KERN_NOTICE "Chip reports improper command sequence: status 0x%x\n", chipstatus);
869 } else if (chipstatus & 0x02) {
870 /* Protection bit set */
872 } else if (chipstatus & 0x8) {
874 printk(KERN_WARNING "Chip reports voltage low on erase: status 0x%x\n", chipstatus);
876 } else if (chipstatus & 0x20) {
878 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr, chipstatus);
879 timeo = jiffies + HZ;
880 chip->state = FL_STATUS;
881 spin_unlock_bh(chip->mutex);
884 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x\n", adr, chipstatus);
890 spin_unlock_bh(chip->mutex);
894 int cfi_staa_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
895 { struct map_info *map = mtd->priv;
896 struct cfi_private *cfi = map->fldrv_priv;
897 unsigned long adr, len;
898 int chipnum, ret = 0;
900 struct mtd_erase_region_info *regions = mtd->eraseregions;
902 if (instr->addr > mtd->size)
905 if ((instr->len + instr->addr) > mtd->size)
908 /* Check that both start and end of the requested erase are
909 * aligned with the erasesize at the appropriate addresses.
914 /* Skip all erase regions which are ended before the start of
915 the requested erase. Actually, to save on the calculations,
916 we skip to the first erase region which starts after the
917 start of the requested erase, and then go back one.
920 while (i < mtd->numeraseregions && instr->addr >= regions[i].offset)
924 /* OK, now i is pointing at the erase region in which this
925 erase request starts. Check the start of the requested
926 erase range is aligned with the erase size which is in
930 if (instr->addr & (regions[i].erasesize-1))
933 /* Remember the erase region we start on */
936 /* Next, check that the end of the requested erase is aligned
937 * with the erase region at that address.
940 while (i<mtd->numeraseregions && (instr->addr + instr->len) >= regions[i].offset)
943 /* As before, drop back one to point at the region in which
944 the address actually falls
948 if ((instr->addr + instr->len) & (regions[i].erasesize-1))
951 chipnum = instr->addr >> cfi->chipshift;
952 adr = instr->addr - (chipnum << cfi->chipshift);
958 ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr);
963 adr += regions[i].erasesize;
964 len -= regions[i].erasesize;
966 if (adr % (1<< cfi->chipshift) == ((regions[i].offset + (regions[i].erasesize * regions[i].numblocks)) %( 1<< cfi->chipshift)))
969 if (adr >> cfi->chipshift) {
973 if (chipnum >= cfi->numchips)
978 instr->state = MTD_ERASE_DONE;
979 mtd_erase_callback(instr);
984 static void cfi_staa_sync (struct mtd_info *mtd)
986 struct map_info *map = mtd->priv;
987 struct cfi_private *cfi = map->fldrv_priv;
991 DECLARE_WAITQUEUE(wait, current);
993 for (i=0; !ret && i<cfi->numchips; i++) {
994 chip = &cfi->chips[i];
997 spin_lock_bh(chip->mutex);
999 switch(chip->state) {
1003 case FL_JEDEC_QUERY:
1004 chip->oldstate = chip->state;
1005 chip->state = FL_SYNCING;
1006 /* No need to wake_up() on this state change -
1007 * as the whole point is that nobody can do anything
1008 * with the chip now anyway.
1011 spin_unlock_bh(chip->mutex);
1015 /* Not an idle state */
1016 add_wait_queue(&chip->wq, &wait);
1018 spin_unlock_bh(chip->mutex);
1020 remove_wait_queue(&chip->wq, &wait);
1026 /* Unlock the chips again */
1028 for (i--; i >=0; i--) {
1029 chip = &cfi->chips[i];
1031 spin_lock_bh(chip->mutex);
1033 if (chip->state == FL_SYNCING) {
1034 chip->state = chip->oldstate;
1037 spin_unlock_bh(chip->mutex);
1041 static inline int do_lock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
1043 struct cfi_private *cfi = map->fldrv_priv;
1044 map_word status, status_OK;
1045 unsigned long timeo = jiffies + HZ;
1046 DECLARE_WAITQUEUE(wait, current);
1050 /* Let's determine this according to the interleave only once */
1051 status_OK = CMD(0x80);
1053 timeo = jiffies + HZ;
1055 spin_lock_bh(chip->mutex);
1057 /* Check that the chip's ready to talk to us. */
1058 switch (chip->state) {
1060 case FL_JEDEC_QUERY:
1062 map_write(map, CMD(0x70), adr);
1063 chip->state = FL_STATUS;
1066 status = map_read(map, adr);
1067 if (map_word_andequal(map, status, status_OK, status_OK))
1070 /* Urgh. Chip not yet ready to talk to us. */
1071 if (time_after(jiffies, timeo)) {
1072 spin_unlock_bh(chip->mutex);
1073 printk(KERN_ERR "waiting for chip to be ready timed out in lock\n");
1077 /* Latency issues. Drop the lock, wait a while and retry */
1078 spin_unlock_bh(chip->mutex);
1083 /* Stick ourselves on a wait queue to be woken when
1084 someone changes the status */
1085 set_current_state(TASK_UNINTERRUPTIBLE);
1086 add_wait_queue(&chip->wq, &wait);
1087 spin_unlock_bh(chip->mutex);
1089 remove_wait_queue(&chip->wq, &wait);
1090 timeo = jiffies + HZ;
1095 map_write(map, CMD(0x60), adr);
1096 map_write(map, CMD(0x01), adr);
1097 chip->state = FL_LOCKING;
1099 spin_unlock_bh(chip->mutex);
1101 spin_lock_bh(chip->mutex);
1103 /* FIXME. Use a timer to check this, and return immediately. */
1104 /* Once the state machine's known to be working I'll do that */
1106 timeo = jiffies + (HZ*2);
1109 status = map_read(map, adr);
1110 if (map_word_andequal(map, status, status_OK, status_OK))
1113 /* OK Still waiting */
1114 if (time_after(jiffies, timeo)) {
1115 map_write(map, CMD(0x70), adr);
1116 chip->state = FL_STATUS;
1117 printk(KERN_ERR "waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
1119 spin_unlock_bh(chip->mutex);
1123 /* Latency issues. Drop the lock, wait a while and retry */
1124 spin_unlock_bh(chip->mutex);
1126 spin_lock_bh(chip->mutex);
1129 /* Done and happy. */
1130 chip->state = FL_STATUS;
1133 spin_unlock_bh(chip->mutex);
1136 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1138 struct map_info *map = mtd->priv;
1139 struct cfi_private *cfi = map->fldrv_priv;
1141 int chipnum, ret = 0;
1142 #ifdef DEBUG_LOCK_BITS
1143 int ofs_factor = cfi->interleave * cfi->device_type;
1146 if (ofs & (mtd->erasesize - 1))
1149 if (len & (mtd->erasesize -1))
1152 if ((len + ofs) > mtd->size)
1155 chipnum = ofs >> cfi->chipshift;
1156 adr = ofs - (chipnum << cfi->chipshift);
1160 #ifdef DEBUG_LOCK_BITS
1161 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1162 printk("before lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1163 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1166 ret = do_lock_oneblock(map, &cfi->chips[chipnum], adr);
1168 #ifdef DEBUG_LOCK_BITS
1169 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1170 printk("after lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1171 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1177 adr += mtd->erasesize;
1178 len -= mtd->erasesize;
1180 if (adr >> cfi->chipshift) {
1184 if (chipnum >= cfi->numchips)
1190 static inline int do_unlock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
1192 struct cfi_private *cfi = map->fldrv_priv;
1193 map_word status, status_OK;
1194 unsigned long timeo = jiffies + HZ;
1195 DECLARE_WAITQUEUE(wait, current);
1199 /* Let's determine this according to the interleave only once */
1200 status_OK = CMD(0x80);
1202 timeo = jiffies + HZ;
1204 spin_lock_bh(chip->mutex);
1206 /* Check that the chip's ready to talk to us. */
1207 switch (chip->state) {
1209 case FL_JEDEC_QUERY:
1211 map_write(map, CMD(0x70), adr);
1212 chip->state = FL_STATUS;
1215 status = map_read(map, adr);
1216 if (map_word_andequal(map, status, status_OK, status_OK))
1219 /* Urgh. Chip not yet ready to talk to us. */
1220 if (time_after(jiffies, timeo)) {
1221 spin_unlock_bh(chip->mutex);
1222 printk(KERN_ERR "waiting for chip to be ready timed out in unlock\n");
1226 /* Latency issues. Drop the lock, wait a while and retry */
1227 spin_unlock_bh(chip->mutex);
1232 /* Stick ourselves on a wait queue to be woken when
1233 someone changes the status */
1234 set_current_state(TASK_UNINTERRUPTIBLE);
1235 add_wait_queue(&chip->wq, &wait);
1236 spin_unlock_bh(chip->mutex);
1238 remove_wait_queue(&chip->wq, &wait);
1239 timeo = jiffies + HZ;
1244 map_write(map, CMD(0x60), adr);
1245 map_write(map, CMD(0xD0), adr);
1246 chip->state = FL_UNLOCKING;
1248 spin_unlock_bh(chip->mutex);
1250 spin_lock_bh(chip->mutex);
1252 /* FIXME. Use a timer to check this, and return immediately. */
1253 /* Once the state machine's known to be working I'll do that */
1255 timeo = jiffies + (HZ*2);
1258 status = map_read(map, adr);
1259 if (map_word_andequal(map, status, status_OK, status_OK))
1262 /* OK Still waiting */
1263 if (time_after(jiffies, timeo)) {
1264 map_write(map, CMD(0x70), adr);
1265 chip->state = FL_STATUS;
1266 printk(KERN_ERR "waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
1268 spin_unlock_bh(chip->mutex);
1272 /* Latency issues. Drop the unlock, wait a while and retry */
1273 spin_unlock_bh(chip->mutex);
1275 spin_lock_bh(chip->mutex);
1278 /* Done and happy. */
1279 chip->state = FL_STATUS;
1282 spin_unlock_bh(chip->mutex);
1285 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1287 struct map_info *map = mtd->priv;
1288 struct cfi_private *cfi = map->fldrv_priv;
1290 int chipnum, ret = 0;
1291 #ifdef DEBUG_LOCK_BITS
1292 int ofs_factor = cfi->interleave * cfi->device_type;
1295 chipnum = ofs >> cfi->chipshift;
1296 adr = ofs - (chipnum << cfi->chipshift);
1298 #ifdef DEBUG_LOCK_BITS
1300 unsigned long temp_adr = adr;
1301 unsigned long temp_len = len;
1303 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1305 printk("before unlock %x: block status register is %x\n",temp_adr,cfi_read_query(map, temp_adr+(2*ofs_factor)));
1306 temp_adr += mtd->erasesize;
1307 temp_len -= mtd->erasesize;
1309 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1313 ret = do_unlock_oneblock(map, &cfi->chips[chipnum], adr);
1315 #ifdef DEBUG_LOCK_BITS
1316 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1317 printk("after unlock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1318 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1324 static int cfi_staa_suspend(struct mtd_info *mtd)
1326 struct map_info *map = mtd->priv;
1327 struct cfi_private *cfi = map->fldrv_priv;
1329 struct flchip *chip;
1332 for (i=0; !ret && i<cfi->numchips; i++) {
1333 chip = &cfi->chips[i];
1335 spin_lock_bh(chip->mutex);
1337 switch(chip->state) {
1341 case FL_JEDEC_QUERY:
1342 chip->oldstate = chip->state;
1343 chip->state = FL_PM_SUSPENDED;
1344 /* No need to wake_up() on this state change -
1345 * as the whole point is that nobody can do anything
1346 * with the chip now anyway.
1348 case FL_PM_SUSPENDED:
1355 spin_unlock_bh(chip->mutex);
1358 /* Unlock the chips again */
1361 for (i--; i >=0; i--) {
1362 chip = &cfi->chips[i];
1364 spin_lock_bh(chip->mutex);
1366 if (chip->state == FL_PM_SUSPENDED) {
1367 /* No need to force it into a known state here,
1368 because we're returning failure, and it didn't
1370 chip->state = chip->oldstate;
1373 spin_unlock_bh(chip->mutex);
1380 static void cfi_staa_resume(struct mtd_info *mtd)
1382 struct map_info *map = mtd->priv;
1383 struct cfi_private *cfi = map->fldrv_priv;
1385 struct flchip *chip;
1387 for (i=0; i<cfi->numchips; i++) {
1389 chip = &cfi->chips[i];
1391 spin_lock_bh(chip->mutex);
1393 /* Go to known state. Chip may have been power cycled */
1394 if (chip->state == FL_PM_SUSPENDED) {
1395 map_write(map, CMD(0xFF), 0);
1396 chip->state = FL_READY;
1400 spin_unlock_bh(chip->mutex);
1404 static void cfi_staa_destroy(struct mtd_info *mtd)
1406 struct map_info *map = mtd->priv;
1407 struct cfi_private *cfi = map->fldrv_priv;
1408 kfree(cfi->cmdset_priv);
1412 MODULE_LICENSE("GPL");