allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / mtd / chips / cfi_cmdset_0020.c
blob3ac8e466027937e7c63d2f431687d8b1f33f7d8f
1 /*
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>
28 #include <asm/io.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",
62 .module = THIS_MODULE
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)
71 int i;
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");
89 for (i=1; i<8; i++) {
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);
108 #endif
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;
120 int i;
122 if (cfi->cfi_mode) {
124 * It's a real CFI chip, not one for which the probe
125 * routine faked a CFI structure. So we read the feature
126 * table from it.
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");
132 if (!extp)
133 return NULL;
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);
140 kfree(extp);
141 return NULL;
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);
151 #endif
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;
161 cfi->chips[i].ref_point_counter = 0;
162 init_waitqueue_head(&(cfi->chips[i].wq));
165 return cfi_staa_setup(map);
167 EXPORT_SYMBOL_GPL(cfi_cmdset_0020);
169 static struct mtd_info *cfi_staa_setup(struct map_info *map)
171 struct cfi_private *cfi = map->fldrv_priv;
172 struct mtd_info *mtd;
173 unsigned long offset = 0;
174 int i,j;
175 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
177 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
178 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
180 if (!mtd) {
181 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
182 kfree(cfi->cmdset_priv);
183 return NULL;
186 mtd->priv = map;
187 mtd->type = MTD_NORFLASH;
188 mtd->size = devsize * cfi->numchips;
190 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
191 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
192 * mtd->numeraseregions, GFP_KERNEL);
193 if (!mtd->eraseregions) {
194 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
195 kfree(cfi->cmdset_priv);
196 kfree(mtd);
197 return NULL;
200 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
201 unsigned long ernum, ersize;
202 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
203 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
205 if (mtd->erasesize < ersize) {
206 mtd->erasesize = ersize;
208 for (j=0; j<cfi->numchips; j++) {
209 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
210 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
211 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
213 offset += (ersize * ernum);
216 if (offset != devsize) {
217 /* Argh */
218 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
219 kfree(mtd->eraseregions);
220 kfree(cfi->cmdset_priv);
221 kfree(mtd);
222 return NULL;
225 for (i=0; i<mtd->numeraseregions;i++){
226 printk(KERN_DEBUG "%d: offset=0x%x,size=0x%x,blocks=%d\n",
227 i,mtd->eraseregions[i].offset,
228 mtd->eraseregions[i].erasesize,
229 mtd->eraseregions[i].numblocks);
232 /* Also select the correct geometry setup too */
233 mtd->erase = cfi_staa_erase_varsize;
234 mtd->read = cfi_staa_read;
235 mtd->write = cfi_staa_write_buffers;
236 mtd->writev = cfi_staa_writev;
237 mtd->sync = cfi_staa_sync;
238 mtd->lock = cfi_staa_lock;
239 mtd->unlock = cfi_staa_unlock;
240 mtd->suspend = cfi_staa_suspend;
241 mtd->resume = cfi_staa_resume;
242 mtd->flags = MTD_CAP_NORFLASH & ~MTD_BIT_WRITEABLE;
243 mtd->writesize = 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */
244 map->fldrv = &cfi_staa_chipdrv;
245 __module_get(THIS_MODULE);
246 mtd->name = map->name;
247 return mtd;
251 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
253 map_word status, status_OK;
254 unsigned long timeo;
255 DECLARE_WAITQUEUE(wait, current);
256 int suspended = 0;
257 unsigned long cmd_addr;
258 struct cfi_private *cfi = map->fldrv_priv;
260 adr += chip->start;
262 /* Ensure cmd read/writes are aligned. */
263 cmd_addr = adr & ~(map_bankwidth(map)-1);
265 /* Let's determine this according to the interleave only once */
266 status_OK = CMD(0x80);
268 timeo = jiffies + HZ;
269 retry:
270 spin_lock_bh(chip->mutex);
272 /* Check that the chip's ready to talk to us.
273 * If it's in FL_ERASING state, suspend it and make it talk now.
275 switch (chip->state) {
276 case FL_ERASING:
277 if (!(((struct cfi_pri_intelext *)cfi->cmdset_priv)->FeatureSupport & 2))
278 goto sleep; /* We don't support erase suspend */
280 map_write (map, CMD(0xb0), cmd_addr);
281 /* If the flash has finished erasing, then 'erase suspend'
282 * appears to make some (28F320) flash devices switch to
283 * 'read' mode. Make sure that we switch to 'read status'
284 * mode so we get the right data. --rmk
286 map_write(map, CMD(0x70), cmd_addr);
287 chip->oldstate = FL_ERASING;
288 chip->state = FL_ERASE_SUSPENDING;
289 // printk("Erase suspending at 0x%lx\n", cmd_addr);
290 for (;;) {
291 status = map_read(map, cmd_addr);
292 if (map_word_andequal(map, status, status_OK, status_OK))
293 break;
295 if (time_after(jiffies, timeo)) {
296 /* Urgh */
297 map_write(map, CMD(0xd0), cmd_addr);
298 /* make sure we're in 'read status' mode */
299 map_write(map, CMD(0x70), cmd_addr);
300 chip->state = FL_ERASING;
301 wake_up(&chip->wq);
302 spin_unlock_bh(chip->mutex);
303 printk(KERN_ERR "Chip not ready after erase "
304 "suspended: status = 0x%lx\n", status.x[0]);
305 return -EIO;
308 spin_unlock_bh(chip->mutex);
309 cfi_udelay(1);
310 spin_lock_bh(chip->mutex);
313 suspended = 1;
314 map_write(map, CMD(0xff), cmd_addr);
315 chip->state = FL_READY;
316 break;
318 #if 0
319 case FL_WRITING:
320 /* Not quite yet */
321 #endif
323 case FL_READY:
324 break;
326 case FL_CFI_QUERY:
327 case FL_JEDEC_QUERY:
328 map_write(map, CMD(0x70), cmd_addr);
329 chip->state = FL_STATUS;
331 case FL_STATUS:
332 status = map_read(map, cmd_addr);
333 if (map_word_andequal(map, status, status_OK, status_OK)) {
334 map_write(map, CMD(0xff), cmd_addr);
335 chip->state = FL_READY;
336 break;
339 /* Urgh. Chip not yet ready to talk to us. */
340 if (time_after(jiffies, timeo)) {
341 spin_unlock_bh(chip->mutex);
342 printk(KERN_ERR "waiting for chip to be ready timed out in read. WSM status = %lx\n", status.x[0]);
343 return -EIO;
346 /* Latency issues. Drop the lock, wait a while and retry */
347 spin_unlock_bh(chip->mutex);
348 cfi_udelay(1);
349 goto retry;
351 default:
352 sleep:
353 /* Stick ourselves on a wait queue to be woken when
354 someone changes the status */
355 set_current_state(TASK_UNINTERRUPTIBLE);
356 add_wait_queue(&chip->wq, &wait);
357 spin_unlock_bh(chip->mutex);
358 schedule();
359 remove_wait_queue(&chip->wq, &wait);
360 timeo = jiffies + HZ;
361 goto retry;
364 map_copy_from(map, buf, adr, len);
366 if (suspended) {
367 chip->state = chip->oldstate;
368 /* What if one interleaved chip has finished and the
369 other hasn't? The old code would leave the finished
370 one in READY mode. That's bad, and caused -EROFS
371 errors to be returned from do_erase_oneblock because
372 that's the only bit it checked for at the time.
373 As the state machine appears to explicitly allow
374 sending the 0x70 (Read Status) command to an erasing
375 chip and expecting it to be ignored, that's what we
376 do. */
377 map_write(map, CMD(0xd0), cmd_addr);
378 map_write(map, CMD(0x70), cmd_addr);
381 wake_up(&chip->wq);
382 spin_unlock_bh(chip->mutex);
383 return 0;
386 static int cfi_staa_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
388 struct map_info *map = mtd->priv;
389 struct cfi_private *cfi = map->fldrv_priv;
390 unsigned long ofs;
391 int chipnum;
392 int ret = 0;
394 /* ofs: offset within the first chip that the first read should start */
395 chipnum = (from >> cfi->chipshift);
396 ofs = from - (chipnum << cfi->chipshift);
398 *retlen = 0;
400 while (len) {
401 unsigned long thislen;
403 if (chipnum >= cfi->numchips)
404 break;
406 if ((len + ofs -1) >> cfi->chipshift)
407 thislen = (1<<cfi->chipshift) - ofs;
408 else
409 thislen = len;
411 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
412 if (ret)
413 break;
415 *retlen += thislen;
416 len -= thislen;
417 buf += thislen;
419 ofs = 0;
420 chipnum++;
422 return ret;
425 static inline int do_write_buffer(struct map_info *map, struct flchip *chip,
426 unsigned long adr, const u_char *buf, int len)
428 struct cfi_private *cfi = map->fldrv_priv;
429 map_word status, status_OK;
430 unsigned long cmd_adr, timeo;
431 DECLARE_WAITQUEUE(wait, current);
432 int wbufsize, z;
434 /* M58LW064A requires bus alignment for buffer wriets -- saw */
435 if (adr & (map_bankwidth(map)-1))
436 return -EINVAL;
438 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
439 adr += chip->start;
440 cmd_adr = adr & ~(wbufsize-1);
442 /* Let's determine this according to the interleave only once */
443 status_OK = CMD(0x80);
445 timeo = jiffies + HZ;
446 retry:
448 #ifdef DEBUG_CFI_FEATURES
449 printk("%s: chip->state[%d]\n", __FUNCTION__, chip->state);
450 #endif
451 spin_lock_bh(chip->mutex);
453 /* Check that the chip's ready to talk to us.
454 * Later, we can actually think about interrupting it
455 * if it's in FL_ERASING state.
456 * Not just yet, though.
458 switch (chip->state) {
459 case FL_READY:
460 break;
462 case FL_CFI_QUERY:
463 case FL_JEDEC_QUERY:
464 map_write(map, CMD(0x70), cmd_adr);
465 chip->state = FL_STATUS;
466 #ifdef DEBUG_CFI_FEATURES
467 printk("%s: 1 status[%x]\n", __FUNCTION__, map_read(map, cmd_adr));
468 #endif
470 case FL_STATUS:
471 status = map_read(map, cmd_adr);
472 if (map_word_andequal(map, status, status_OK, status_OK))
473 break;
474 /* Urgh. Chip not yet ready to talk to us. */
475 if (time_after(jiffies, timeo)) {
476 spin_unlock_bh(chip->mutex);
477 printk(KERN_ERR "waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
478 status.x[0], map_read(map, cmd_adr).x[0]);
479 return -EIO;
482 /* Latency issues. Drop the lock, wait a while and retry */
483 spin_unlock_bh(chip->mutex);
484 cfi_udelay(1);
485 goto retry;
487 default:
488 /* Stick ourselves on a wait queue to be woken when
489 someone changes the status */
490 set_current_state(TASK_UNINTERRUPTIBLE);
491 add_wait_queue(&chip->wq, &wait);
492 spin_unlock_bh(chip->mutex);
493 schedule();
494 remove_wait_queue(&chip->wq, &wait);
495 timeo = jiffies + HZ;
496 goto retry;
499 ENABLE_VPP(map);
500 map_write(map, CMD(0xe8), cmd_adr);
501 chip->state = FL_WRITING_TO_BUFFER;
503 z = 0;
504 for (;;) {
505 status = map_read(map, cmd_adr);
506 if (map_word_andequal(map, status, status_OK, status_OK))
507 break;
509 spin_unlock_bh(chip->mutex);
510 cfi_udelay(1);
511 spin_lock_bh(chip->mutex);
513 if (++z > 100) {
514 /* Argh. Not ready for write to buffer */
515 DISABLE_VPP(map);
516 map_write(map, CMD(0x70), cmd_adr);
517 chip->state = FL_STATUS;
518 spin_unlock_bh(chip->mutex);
519 printk(KERN_ERR "Chip not ready for buffer write. Xstatus = %lx\n", status.x[0]);
520 return -EIO;
524 /* Write length of data to come */
525 map_write(map, CMD(len/map_bankwidth(map)-1), cmd_adr );
527 /* Write data */
528 for (z = 0; z < len;
529 z += map_bankwidth(map), buf += map_bankwidth(map)) {
530 map_word d;
531 d = map_word_load(map, buf);
532 map_write(map, d, adr+z);
534 /* GO GO GO */
535 map_write(map, CMD(0xd0), cmd_adr);
536 chip->state = FL_WRITING;
538 spin_unlock_bh(chip->mutex);
539 cfi_udelay(chip->buffer_write_time);
540 spin_lock_bh(chip->mutex);
542 timeo = jiffies + (HZ/2);
543 z = 0;
544 for (;;) {
545 if (chip->state != FL_WRITING) {
546 /* Someone's suspended the write. Sleep */
547 set_current_state(TASK_UNINTERRUPTIBLE);
548 add_wait_queue(&chip->wq, &wait);
549 spin_unlock_bh(chip->mutex);
550 schedule();
551 remove_wait_queue(&chip->wq, &wait);
552 timeo = jiffies + (HZ / 2); /* FIXME */
553 spin_lock_bh(chip->mutex);
554 continue;
557 status = map_read(map, cmd_adr);
558 if (map_word_andequal(map, status, status_OK, status_OK))
559 break;
561 /* OK Still waiting */
562 if (time_after(jiffies, timeo)) {
563 /* clear status */
564 map_write(map, CMD(0x50), cmd_adr);
565 /* put back into read status register mode */
566 map_write(map, CMD(0x70), adr);
567 chip->state = FL_STATUS;
568 DISABLE_VPP(map);
569 spin_unlock_bh(chip->mutex);
570 printk(KERN_ERR "waiting for chip to be ready timed out in bufwrite\n");
571 return -EIO;
574 /* Latency issues. Drop the lock, wait a while and retry */
575 spin_unlock_bh(chip->mutex);
576 cfi_udelay(1);
577 z++;
578 spin_lock_bh(chip->mutex);
580 if (!z) {
581 chip->buffer_write_time--;
582 if (!chip->buffer_write_time)
583 chip->buffer_write_time++;
585 if (z > 1)
586 chip->buffer_write_time++;
588 /* Done and happy. */
589 DISABLE_VPP(map);
590 chip->state = FL_STATUS;
592 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
593 if (map_word_bitsset(map, status, CMD(0x3a))) {
594 #ifdef DEBUG_CFI_FEATURES
595 printk("%s: 2 status[%lx]\n", __FUNCTION__, status.x[0]);
596 #endif
597 /* clear status */
598 map_write(map, CMD(0x50), cmd_adr);
599 /* put back into read status register mode */
600 map_write(map, CMD(0x70), adr);
601 wake_up(&chip->wq);
602 spin_unlock_bh(chip->mutex);
603 return map_word_bitsset(map, status, CMD(0x02)) ? -EROFS : -EIO;
605 wake_up(&chip->wq);
606 spin_unlock_bh(chip->mutex);
608 return 0;
611 static int cfi_staa_write_buffers (struct mtd_info *mtd, loff_t to,
612 size_t len, size_t *retlen, const u_char *buf)
614 struct map_info *map = mtd->priv;
615 struct cfi_private *cfi = map->fldrv_priv;
616 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
617 int ret = 0;
618 int chipnum;
619 unsigned long ofs;
621 *retlen = 0;
622 if (!len)
623 return 0;
625 chipnum = to >> cfi->chipshift;
626 ofs = to - (chipnum << cfi->chipshift);
628 #ifdef DEBUG_CFI_FEATURES
629 printk("%s: map_bankwidth(map)[%x]\n", __FUNCTION__, map_bankwidth(map));
630 printk("%s: chipnum[%x] wbufsize[%x]\n", __FUNCTION__, chipnum, wbufsize);
631 printk("%s: ofs[%x] len[%x]\n", __FUNCTION__, ofs, len);
632 #endif
634 /* Write buffer is worth it only if more than one word to write... */
635 while (len > 0) {
636 /* We must not cross write block boundaries */
637 int size = wbufsize - (ofs & (wbufsize-1));
639 if (size > len)
640 size = len;
642 ret = do_write_buffer(map, &cfi->chips[chipnum],
643 ofs, buf, size);
644 if (ret)
645 return ret;
647 ofs += size;
648 buf += size;
649 (*retlen) += size;
650 len -= size;
652 if (ofs >> cfi->chipshift) {
653 chipnum ++;
654 ofs = 0;
655 if (chipnum == cfi->numchips)
656 return 0;
660 return 0;
664 * Writev for ECC-Flashes is a little more complicated. We need to maintain
665 * a small buffer for this.
666 * XXX: If the buffer size is not a multiple of 2, this will break
668 #define ECCBUF_SIZE (mtd->writesize)
669 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
670 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
671 static int
672 cfi_staa_writev(struct mtd_info *mtd, const struct kvec *vecs,
673 unsigned long count, loff_t to, size_t *retlen)
675 unsigned long i;
676 size_t totlen = 0, thislen;
677 int ret = 0;
678 size_t buflen = 0;
679 static char *buffer;
681 if (!ECCBUF_SIZE) {
682 /* We should fall back to a general writev implementation.
683 * Until that is written, just break.
685 return -EIO;
687 buffer = kmalloc(ECCBUF_SIZE, GFP_KERNEL);
688 if (!buffer)
689 return -ENOMEM;
691 for (i=0; i<count; i++) {
692 size_t elem_len = vecs[i].iov_len;
693 void *elem_base = vecs[i].iov_base;
694 if (!elem_len) /* FIXME: Might be unnecessary. Check that */
695 continue;
696 if (buflen) { /* cut off head */
697 if (buflen + elem_len < ECCBUF_SIZE) { /* just accumulate */
698 memcpy(buffer+buflen, elem_base, elem_len);
699 buflen += elem_len;
700 continue;
702 memcpy(buffer+buflen, elem_base, ECCBUF_SIZE-buflen);
703 ret = mtd->write(mtd, to, ECCBUF_SIZE, &thislen, buffer);
704 totlen += thislen;
705 if (ret || thislen != ECCBUF_SIZE)
706 goto write_error;
707 elem_len -= thislen-buflen;
708 elem_base += thislen-buflen;
709 to += ECCBUF_SIZE;
711 if (ECCBUF_DIV(elem_len)) { /* write clean aligned data */
712 ret = mtd->write(mtd, to, ECCBUF_DIV(elem_len), &thislen, elem_base);
713 totlen += thislen;
714 if (ret || thislen != ECCBUF_DIV(elem_len))
715 goto write_error;
716 to += thislen;
718 buflen = ECCBUF_MOD(elem_len); /* cut off tail */
719 if (buflen) {
720 memset(buffer, 0xff, ECCBUF_SIZE);
721 memcpy(buffer, elem_base + thislen, buflen);
724 if (buflen) { /* flush last page, even if not full */
725 /* This is sometimes intended behaviour, really */
726 ret = mtd->write(mtd, to, buflen, &thislen, buffer);
727 totlen += thislen;
728 if (ret || thislen != ECCBUF_SIZE)
729 goto write_error;
731 write_error:
732 if (retlen)
733 *retlen = totlen;
734 kfree(buffer);
735 return ret;
739 static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
741 struct cfi_private *cfi = map->fldrv_priv;
742 map_word status, status_OK;
743 unsigned long timeo;
744 int retries = 3;
745 DECLARE_WAITQUEUE(wait, current);
746 int ret = 0;
748 adr += chip->start;
750 /* Let's determine this according to the interleave only once */
751 status_OK = CMD(0x80);
753 timeo = jiffies + HZ;
754 retry:
755 spin_lock_bh(chip->mutex);
757 /* Check that the chip's ready to talk to us. */
758 switch (chip->state) {
759 case FL_CFI_QUERY:
760 case FL_JEDEC_QUERY:
761 case FL_READY:
762 map_write(map, CMD(0x70), adr);
763 chip->state = FL_STATUS;
765 case FL_STATUS:
766 status = map_read(map, adr);
767 if (map_word_andequal(map, status, status_OK, status_OK))
768 break;
770 /* Urgh. Chip not yet ready to talk to us. */
771 if (time_after(jiffies, timeo)) {
772 spin_unlock_bh(chip->mutex);
773 printk(KERN_ERR "waiting for chip to be ready timed out in erase\n");
774 return -EIO;
777 /* Latency issues. Drop the lock, wait a while and retry */
778 spin_unlock_bh(chip->mutex);
779 cfi_udelay(1);
780 goto retry;
782 default:
783 /* Stick ourselves on a wait queue to be woken when
784 someone changes the status */
785 set_current_state(TASK_UNINTERRUPTIBLE);
786 add_wait_queue(&chip->wq, &wait);
787 spin_unlock_bh(chip->mutex);
788 schedule();
789 remove_wait_queue(&chip->wq, &wait);
790 timeo = jiffies + HZ;
791 goto retry;
794 ENABLE_VPP(map);
795 /* Clear the status register first */
796 map_write(map, CMD(0x50), adr);
798 /* Now erase */
799 map_write(map, CMD(0x20), adr);
800 map_write(map, CMD(0xD0), adr);
801 chip->state = FL_ERASING;
803 spin_unlock_bh(chip->mutex);
804 msleep(1000);
805 spin_lock_bh(chip->mutex);
807 /* FIXME. Use a timer to check this, and return immediately. */
808 /* Once the state machine's known to be working I'll do that */
810 timeo = jiffies + (HZ*20);
811 for (;;) {
812 if (chip->state != FL_ERASING) {
813 /* Someone's suspended the erase. Sleep */
814 set_current_state(TASK_UNINTERRUPTIBLE);
815 add_wait_queue(&chip->wq, &wait);
816 spin_unlock_bh(chip->mutex);
817 schedule();
818 remove_wait_queue(&chip->wq, &wait);
819 timeo = jiffies + (HZ*20); /* FIXME */
820 spin_lock_bh(chip->mutex);
821 continue;
824 status = map_read(map, adr);
825 if (map_word_andequal(map, status, status_OK, status_OK))
826 break;
828 /* OK Still waiting */
829 if (time_after(jiffies, timeo)) {
830 map_write(map, CMD(0x70), adr);
831 chip->state = FL_STATUS;
832 printk(KERN_ERR "waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
833 DISABLE_VPP(map);
834 spin_unlock_bh(chip->mutex);
835 return -EIO;
838 /* Latency issues. Drop the lock, wait a while and retry */
839 spin_unlock_bh(chip->mutex);
840 cfi_udelay(1);
841 spin_lock_bh(chip->mutex);
844 DISABLE_VPP(map);
845 ret = 0;
847 /* We've broken this before. It doesn't hurt to be safe */
848 map_write(map, CMD(0x70), adr);
849 chip->state = FL_STATUS;
850 status = map_read(map, adr);
852 /* check for lock bit */
853 if (map_word_bitsset(map, status, CMD(0x3a))) {
854 unsigned char chipstatus = status.x[0];
855 if (!map_word_equal(map, status, CMD(chipstatus))) {
856 int i, w;
857 for (w=0; w<map_words(map); w++) {
858 for (i = 0; i<cfi_interleave(cfi); i++) {
859 chipstatus |= status.x[w] >> (cfi->device_type * 8);
862 printk(KERN_WARNING "Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
863 status.x[0], chipstatus);
865 /* Reset the error bits */
866 map_write(map, CMD(0x50), adr);
867 map_write(map, CMD(0x70), adr);
869 if ((chipstatus & 0x30) == 0x30) {
870 printk(KERN_NOTICE "Chip reports improper command sequence: status 0x%x\n", chipstatus);
871 ret = -EIO;
872 } else if (chipstatus & 0x02) {
873 /* Protection bit set */
874 ret = -EROFS;
875 } else if (chipstatus & 0x8) {
876 /* Voltage */
877 printk(KERN_WARNING "Chip reports voltage low on erase: status 0x%x\n", chipstatus);
878 ret = -EIO;
879 } else if (chipstatus & 0x20) {
880 if (retries--) {
881 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr, chipstatus);
882 timeo = jiffies + HZ;
883 chip->state = FL_STATUS;
884 spin_unlock_bh(chip->mutex);
885 goto retry;
887 printk(KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x\n", adr, chipstatus);
888 ret = -EIO;
892 wake_up(&chip->wq);
893 spin_unlock_bh(chip->mutex);
894 return ret;
897 int cfi_staa_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
898 { struct map_info *map = mtd->priv;
899 struct cfi_private *cfi = map->fldrv_priv;
900 unsigned long adr, len;
901 int chipnum, ret = 0;
902 int i, first;
903 struct mtd_erase_region_info *regions = mtd->eraseregions;
905 if (instr->addr > mtd->size)
906 return -EINVAL;
908 if ((instr->len + instr->addr) > mtd->size)
909 return -EINVAL;
911 /* Check that both start and end of the requested erase are
912 * aligned with the erasesize at the appropriate addresses.
915 i = 0;
917 /* Skip all erase regions which are ended before the start of
918 the requested erase. Actually, to save on the calculations,
919 we skip to the first erase region which starts after the
920 start of the requested erase, and then go back one.
923 while (i < mtd->numeraseregions && instr->addr >= regions[i].offset)
924 i++;
925 i--;
927 /* OK, now i is pointing at the erase region in which this
928 erase request starts. Check the start of the requested
929 erase range is aligned with the erase size which is in
930 effect here.
933 if (instr->addr & (regions[i].erasesize-1))
934 return -EINVAL;
936 /* Remember the erase region we start on */
937 first = i;
939 /* Next, check that the end of the requested erase is aligned
940 * with the erase region at that address.
943 while (i<mtd->numeraseregions && (instr->addr + instr->len) >= regions[i].offset)
944 i++;
946 /* As before, drop back one to point at the region in which
947 the address actually falls
949 i--;
951 if ((instr->addr + instr->len) & (regions[i].erasesize-1))
952 return -EINVAL;
954 chipnum = instr->addr >> cfi->chipshift;
955 adr = instr->addr - (chipnum << cfi->chipshift);
956 len = instr->len;
958 i=first;
960 while(len) {
961 ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr);
963 if (ret)
964 return ret;
966 adr += regions[i].erasesize;
967 len -= regions[i].erasesize;
969 if (adr % (1<< cfi->chipshift) == ((regions[i].offset + (regions[i].erasesize * regions[i].numblocks)) %( 1<< cfi->chipshift)))
970 i++;
972 if (adr >> cfi->chipshift) {
973 adr = 0;
974 chipnum++;
976 if (chipnum >= cfi->numchips)
977 break;
981 instr->state = MTD_ERASE_DONE;
982 mtd_erase_callback(instr);
984 return 0;
987 static void cfi_staa_sync (struct mtd_info *mtd)
989 struct map_info *map = mtd->priv;
990 struct cfi_private *cfi = map->fldrv_priv;
991 int i;
992 struct flchip *chip;
993 int ret = 0;
994 DECLARE_WAITQUEUE(wait, current);
996 for (i=0; !ret && i<cfi->numchips; i++) {
997 chip = &cfi->chips[i];
999 retry:
1000 spin_lock_bh(chip->mutex);
1002 switch(chip->state) {
1003 case FL_READY:
1004 case FL_STATUS:
1005 case FL_CFI_QUERY:
1006 case FL_JEDEC_QUERY:
1007 chip->oldstate = chip->state;
1008 chip->state = FL_SYNCING;
1009 /* No need to wake_up() on this state change -
1010 * as the whole point is that nobody can do anything
1011 * with the chip now anyway.
1013 case FL_SYNCING:
1014 spin_unlock_bh(chip->mutex);
1015 break;
1017 default:
1018 /* Not an idle state */
1019 set_current_state(TASK_UNINTERRUPTIBLE);
1020 add_wait_queue(&chip->wq, &wait);
1022 spin_unlock_bh(chip->mutex);
1023 schedule();
1024 remove_wait_queue(&chip->wq, &wait);
1026 goto retry;
1030 /* Unlock the chips again */
1032 for (i--; i >=0; i--) {
1033 chip = &cfi->chips[i];
1035 spin_lock_bh(chip->mutex);
1037 if (chip->state == FL_SYNCING) {
1038 chip->state = chip->oldstate;
1039 wake_up(&chip->wq);
1041 spin_unlock_bh(chip->mutex);
1045 static inline int do_lock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
1047 struct cfi_private *cfi = map->fldrv_priv;
1048 map_word status, status_OK;
1049 unsigned long timeo = jiffies + HZ;
1050 DECLARE_WAITQUEUE(wait, current);
1052 adr += chip->start;
1054 /* Let's determine this according to the interleave only once */
1055 status_OK = CMD(0x80);
1057 timeo = jiffies + HZ;
1058 retry:
1059 spin_lock_bh(chip->mutex);
1061 /* Check that the chip's ready to talk to us. */
1062 switch (chip->state) {
1063 case FL_CFI_QUERY:
1064 case FL_JEDEC_QUERY:
1065 case FL_READY:
1066 map_write(map, CMD(0x70), adr);
1067 chip->state = FL_STATUS;
1069 case FL_STATUS:
1070 status = map_read(map, adr);
1071 if (map_word_andequal(map, status, status_OK, status_OK))
1072 break;
1074 /* Urgh. Chip not yet ready to talk to us. */
1075 if (time_after(jiffies, timeo)) {
1076 spin_unlock_bh(chip->mutex);
1077 printk(KERN_ERR "waiting for chip to be ready timed out in lock\n");
1078 return -EIO;
1081 /* Latency issues. Drop the lock, wait a while and retry */
1082 spin_unlock_bh(chip->mutex);
1083 cfi_udelay(1);
1084 goto retry;
1086 default:
1087 /* Stick ourselves on a wait queue to be woken when
1088 someone changes the status */
1089 set_current_state(TASK_UNINTERRUPTIBLE);
1090 add_wait_queue(&chip->wq, &wait);
1091 spin_unlock_bh(chip->mutex);
1092 schedule();
1093 remove_wait_queue(&chip->wq, &wait);
1094 timeo = jiffies + HZ;
1095 goto retry;
1098 ENABLE_VPP(map);
1099 map_write(map, CMD(0x60), adr);
1100 map_write(map, CMD(0x01), adr);
1101 chip->state = FL_LOCKING;
1103 spin_unlock_bh(chip->mutex);
1104 msleep(1000);
1105 spin_lock_bh(chip->mutex);
1107 /* FIXME. Use a timer to check this, and return immediately. */
1108 /* Once the state machine's known to be working I'll do that */
1110 timeo = jiffies + (HZ*2);
1111 for (;;) {
1113 status = map_read(map, adr);
1114 if (map_word_andequal(map, status, status_OK, status_OK))
1115 break;
1117 /* OK Still waiting */
1118 if (time_after(jiffies, timeo)) {
1119 map_write(map, CMD(0x70), adr);
1120 chip->state = FL_STATUS;
1121 printk(KERN_ERR "waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
1122 DISABLE_VPP(map);
1123 spin_unlock_bh(chip->mutex);
1124 return -EIO;
1127 /* Latency issues. Drop the lock, wait a while and retry */
1128 spin_unlock_bh(chip->mutex);
1129 cfi_udelay(1);
1130 spin_lock_bh(chip->mutex);
1133 /* Done and happy. */
1134 chip->state = FL_STATUS;
1135 DISABLE_VPP(map);
1136 wake_up(&chip->wq);
1137 spin_unlock_bh(chip->mutex);
1138 return 0;
1140 static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1142 struct map_info *map = mtd->priv;
1143 struct cfi_private *cfi = map->fldrv_priv;
1144 unsigned long adr;
1145 int chipnum, ret = 0;
1146 #ifdef DEBUG_LOCK_BITS
1147 int ofs_factor = cfi->interleave * cfi->device_type;
1148 #endif
1150 if (ofs & (mtd->erasesize - 1))
1151 return -EINVAL;
1153 if (len & (mtd->erasesize -1))
1154 return -EINVAL;
1156 if ((len + ofs) > mtd->size)
1157 return -EINVAL;
1159 chipnum = ofs >> cfi->chipshift;
1160 adr = ofs - (chipnum << cfi->chipshift);
1162 while(len) {
1164 #ifdef DEBUG_LOCK_BITS
1165 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1166 printk("before lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1167 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1168 #endif
1170 ret = do_lock_oneblock(map, &cfi->chips[chipnum], adr);
1172 #ifdef DEBUG_LOCK_BITS
1173 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1174 printk("after lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1175 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1176 #endif
1178 if (ret)
1179 return ret;
1181 adr += mtd->erasesize;
1182 len -= mtd->erasesize;
1184 if (adr >> cfi->chipshift) {
1185 adr = 0;
1186 chipnum++;
1188 if (chipnum >= cfi->numchips)
1189 break;
1192 return 0;
1194 static inline int do_unlock_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr)
1196 struct cfi_private *cfi = map->fldrv_priv;
1197 map_word status, status_OK;
1198 unsigned long timeo = jiffies + HZ;
1199 DECLARE_WAITQUEUE(wait, current);
1201 adr += chip->start;
1203 /* Let's determine this according to the interleave only once */
1204 status_OK = CMD(0x80);
1206 timeo = jiffies + HZ;
1207 retry:
1208 spin_lock_bh(chip->mutex);
1210 /* Check that the chip's ready to talk to us. */
1211 switch (chip->state) {
1212 case FL_CFI_QUERY:
1213 case FL_JEDEC_QUERY:
1214 case FL_READY:
1215 map_write(map, CMD(0x70), adr);
1216 chip->state = FL_STATUS;
1218 case FL_STATUS:
1219 status = map_read(map, adr);
1220 if (map_word_andequal(map, status, status_OK, status_OK))
1221 break;
1223 /* Urgh. Chip not yet ready to talk to us. */
1224 if (time_after(jiffies, timeo)) {
1225 spin_unlock_bh(chip->mutex);
1226 printk(KERN_ERR "waiting for chip to be ready timed out in unlock\n");
1227 return -EIO;
1230 /* Latency issues. Drop the lock, wait a while and retry */
1231 spin_unlock_bh(chip->mutex);
1232 cfi_udelay(1);
1233 goto retry;
1235 default:
1236 /* Stick ourselves on a wait queue to be woken when
1237 someone changes the status */
1238 set_current_state(TASK_UNINTERRUPTIBLE);
1239 add_wait_queue(&chip->wq, &wait);
1240 spin_unlock_bh(chip->mutex);
1241 schedule();
1242 remove_wait_queue(&chip->wq, &wait);
1243 timeo = jiffies + HZ;
1244 goto retry;
1247 ENABLE_VPP(map);
1248 map_write(map, CMD(0x60), adr);
1249 map_write(map, CMD(0xD0), adr);
1250 chip->state = FL_UNLOCKING;
1252 spin_unlock_bh(chip->mutex);
1253 msleep(1000);
1254 spin_lock_bh(chip->mutex);
1256 /* FIXME. Use a timer to check this, and return immediately. */
1257 /* Once the state machine's known to be working I'll do that */
1259 timeo = jiffies + (HZ*2);
1260 for (;;) {
1262 status = map_read(map, adr);
1263 if (map_word_andequal(map, status, status_OK, status_OK))
1264 break;
1266 /* OK Still waiting */
1267 if (time_after(jiffies, timeo)) {
1268 map_write(map, CMD(0x70), adr);
1269 chip->state = FL_STATUS;
1270 printk(KERN_ERR "waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status.x[0], map_read(map, adr).x[0]);
1271 DISABLE_VPP(map);
1272 spin_unlock_bh(chip->mutex);
1273 return -EIO;
1276 /* Latency issues. Drop the unlock, wait a while and retry */
1277 spin_unlock_bh(chip->mutex);
1278 cfi_udelay(1);
1279 spin_lock_bh(chip->mutex);
1282 /* Done and happy. */
1283 chip->state = FL_STATUS;
1284 DISABLE_VPP(map);
1285 wake_up(&chip->wq);
1286 spin_unlock_bh(chip->mutex);
1287 return 0;
1289 static int cfi_staa_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1291 struct map_info *map = mtd->priv;
1292 struct cfi_private *cfi = map->fldrv_priv;
1293 unsigned long adr;
1294 int chipnum, ret = 0;
1295 #ifdef DEBUG_LOCK_BITS
1296 int ofs_factor = cfi->interleave * cfi->device_type;
1297 #endif
1299 chipnum = ofs >> cfi->chipshift;
1300 adr = ofs - (chipnum << cfi->chipshift);
1302 #ifdef DEBUG_LOCK_BITS
1304 unsigned long temp_adr = adr;
1305 unsigned long temp_len = len;
1307 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1308 while (temp_len) {
1309 printk("before unlock %x: block status register is %x\n",temp_adr,cfi_read_query(map, temp_adr+(2*ofs_factor)));
1310 temp_adr += mtd->erasesize;
1311 temp_len -= mtd->erasesize;
1313 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1315 #endif
1317 ret = do_unlock_oneblock(map, &cfi->chips[chipnum], adr);
1319 #ifdef DEBUG_LOCK_BITS
1320 cfi_send_gen_cmd(0x90, 0x55, 0, map, cfi, cfi->device_type, NULL);
1321 printk("after unlock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1322 cfi_send_gen_cmd(0xff, 0x55, 0, map, cfi, cfi->device_type, NULL);
1323 #endif
1325 return ret;
1328 static int cfi_staa_suspend(struct mtd_info *mtd)
1330 struct map_info *map = mtd->priv;
1331 struct cfi_private *cfi = map->fldrv_priv;
1332 int i;
1333 struct flchip *chip;
1334 int ret = 0;
1336 for (i=0; !ret && i<cfi->numchips; i++) {
1337 chip = &cfi->chips[i];
1339 spin_lock_bh(chip->mutex);
1341 switch(chip->state) {
1342 case FL_READY:
1343 case FL_STATUS:
1344 case FL_CFI_QUERY:
1345 case FL_JEDEC_QUERY:
1346 chip->oldstate = chip->state;
1347 chip->state = FL_PM_SUSPENDED;
1348 /* No need to wake_up() on this state change -
1349 * as the whole point is that nobody can do anything
1350 * with the chip now anyway.
1352 case FL_PM_SUSPENDED:
1353 break;
1355 default:
1356 ret = -EAGAIN;
1357 break;
1359 spin_unlock_bh(chip->mutex);
1362 /* Unlock the chips again */
1364 if (ret) {
1365 for (i--; i >=0; i--) {
1366 chip = &cfi->chips[i];
1368 spin_lock_bh(chip->mutex);
1370 if (chip->state == FL_PM_SUSPENDED) {
1371 /* No need to force it into a known state here,
1372 because we're returning failure, and it didn't
1373 get power cycled */
1374 chip->state = chip->oldstate;
1375 wake_up(&chip->wq);
1377 spin_unlock_bh(chip->mutex);
1381 return ret;
1384 static void cfi_staa_resume(struct mtd_info *mtd)
1386 struct map_info *map = mtd->priv;
1387 struct cfi_private *cfi = map->fldrv_priv;
1388 int i;
1389 struct flchip *chip;
1391 for (i=0; i<cfi->numchips; i++) {
1393 chip = &cfi->chips[i];
1395 spin_lock_bh(chip->mutex);
1397 /* Go to known state. Chip may have been power cycled */
1398 if (chip->state == FL_PM_SUSPENDED) {
1399 map_write(map, CMD(0xFF), 0);
1400 chip->state = FL_READY;
1401 wake_up(&chip->wq);
1404 spin_unlock_bh(chip->mutex);
1408 static void cfi_staa_destroy(struct mtd_info *mtd)
1410 struct map_info *map = mtd->priv;
1411 struct cfi_private *cfi = map->fldrv_priv;
1412 kfree(cfi->cmdset_priv);
1413 kfree(cfi);
1416 MODULE_LICENSE("GPL");