2 * Common Flash Interface support:
3 * AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
5 * Copyright (C) 2000 Crossnet Co. <info@crossnet.co.jp>
6 * Copyright (C) 2004 Arcom Control Systems Ltd <linux@arcom.com>
7 * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
9 * 2_by_8 routines added by Simon Munton
11 * 4_by_16 work by Carolyn J. Smith
13 * XIP support hooks by Vitaly Wool (based on code for Intel flash
16 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
20 * $Id: cfi_cmdset_0002.c,v 1.122 2005/11/07 11:14:22 gleixner Exp $
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/init.h>
30 #include <asm/byteorder.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
36 #include <linux/mtd/compatmac.h>
37 #include <linux/mtd/map.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/cfi.h>
40 #include <linux/mtd/xip.h>
42 #define AMD_BOOTLOC_BUG
43 #define FORCE_WORD_WRITE 0
45 #define MAX_WORD_RETRIES 3
47 #define MANUFACTURER_AMD 0x0001
48 #define MANUFACTURER_ATMEL 0x001F
49 #define MANUFACTURER_SST 0x00BF
50 #define SST49LF004B 0x0060
51 #define SST49LF008A 0x005a
52 #define AT49BV6416 0x00d6
54 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
55 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
56 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
57 static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
58 static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
59 static void cfi_amdstd_sync (struct mtd_info *);
60 static int cfi_amdstd_suspend (struct mtd_info *);
61 static void cfi_amdstd_resume (struct mtd_info *);
62 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
64 static void cfi_amdstd_destroy(struct mtd_info *);
66 struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
67 static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
69 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
70 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
73 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
74 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
76 static struct mtd_chip_driver cfi_amdstd_chipdrv = {
77 .probe = NULL, /* Not usable directly */
78 .destroy = cfi_amdstd_destroy,
79 .name = "cfi_cmdset_0002",
84 /* #define DEBUG_CFI_FEATURES */
87 #ifdef DEBUG_CFI_FEATURES
88 static void cfi_tell_features(struct cfi_pri_amdstd *extp)
90 const char* erase_suspend[3] = {
91 "Not supported", "Read only", "Read/write"
93 const char* top_bottom[6] = {
94 "No WP", "8x8KiB sectors at top & bottom, no WP",
95 "Bottom boot", "Top boot",
96 "Uniform, Bottom WP", "Uniform, Top WP"
99 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
100 printk(" Address sensitive unlock: %s\n",
101 (extp->SiliconRevision & 1) ? "Not required" : "Required");
103 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
104 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
106 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
108 if (extp->BlkProt == 0)
109 printk(" Block protection: Not supported\n");
111 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
114 printk(" Temporary block unprotect: %s\n",
115 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
116 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
117 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
118 printk(" Burst mode: %s\n",
119 extp->BurstMode ? "Supported" : "Not supported");
120 if (extp->PageMode == 0)
121 printk(" Page mode: Not supported\n");
123 printk(" Page mode: %d word page\n", extp->PageMode << 2);
125 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
126 extp->VppMin >> 4, extp->VppMin & 0xf);
127 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
128 extp->VppMax >> 4, extp->VppMax & 0xf);
130 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
131 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
133 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
137 #ifdef AMD_BOOTLOC_BUG
138 /* Wheee. Bring me the head of someone at AMD. */
139 static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
141 struct map_info *map = mtd->priv;
142 struct cfi_private *cfi = map->fldrv_priv;
143 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
144 __u8 major = extp->MajorVersion;
145 __u8 minor = extp->MinorVersion;
147 if (((major << 8) | minor) < 0x3131) {
148 /* CFI version 1.0 => don't trust bootloc */
149 if (cfi->id & 0x80) {
150 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
151 extp->TopBottom = 3; /* top boot */
153 extp->TopBottom = 2; /* bottom boot */
159 static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
161 struct map_info *map = mtd->priv;
162 struct cfi_private *cfi = map->fldrv_priv;
163 if (cfi->cfiq->BufWriteTimeoutTyp) {
164 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
165 mtd->write = cfi_amdstd_write_buffers;
169 /* Atmel chips don't use the same PRI format as AMD chips */
170 static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
172 struct map_info *map = mtd->priv;
173 struct cfi_private *cfi = map->fldrv_priv;
174 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
175 struct cfi_pri_atmel atmel_pri;
177 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
178 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
180 if (atmel_pri.Features & 0x02)
181 extp->EraseSuspend = 2;
183 if (atmel_pri.BottomBoot)
189 static void fixup_use_secsi(struct mtd_info *mtd, void *param)
191 /* Setup for chips with a secsi area */
192 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
193 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
196 static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
198 struct map_info *map = mtd->priv;
199 struct cfi_private *cfi = map->fldrv_priv;
200 if ((cfi->cfiq->NumEraseRegions == 1) &&
201 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
202 mtd->erase = cfi_amdstd_erase_chip;
208 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
211 static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
213 mtd->lock = cfi_atmel_lock;
214 mtd->unlock = cfi_atmel_unlock;
215 mtd->flags |= MTD_STUPID_LOCK;
218 static struct cfi_fixup cfi_fixup_table[] = {
219 #ifdef AMD_BOOTLOC_BUG
220 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
222 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
223 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
224 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
225 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
226 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
227 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
228 #if !FORCE_WORD_WRITE
229 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
231 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
234 static struct cfi_fixup jedec_fixup_table[] = {
235 { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
236 { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
240 static struct cfi_fixup fixup_table[] = {
241 /* The CFI vendor ids and the JEDEC vendor IDs appear
242 * to be common. It is like the devices id's are as
243 * well. This table is to pick all cases where
244 * we know that is the case.
246 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
247 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
252 struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
254 struct cfi_private *cfi = map->fldrv_priv;
255 struct mtd_info *mtd;
258 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
260 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
263 memset(mtd, 0, sizeof(*mtd));
265 mtd->type = MTD_NORFLASH;
267 /* Fill in the default mtd operations */
268 mtd->erase = cfi_amdstd_erase_varsize;
269 mtd->write = cfi_amdstd_write_words;
270 mtd->read = cfi_amdstd_read;
271 mtd->sync = cfi_amdstd_sync;
272 mtd->suspend = cfi_amdstd_suspend;
273 mtd->resume = cfi_amdstd_resume;
274 mtd->flags = MTD_CAP_NORFLASH;
275 mtd->name = map->name;
278 if (cfi->cfi_mode==CFI_MODE_CFI){
279 unsigned char bootloc;
281 * It's a real CFI chip, not one for which the probe
282 * routine faked a CFI structure. So we read the feature
285 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
286 struct cfi_pri_amdstd *extp;
288 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
294 if (extp->MajorVersion != '1' ||
295 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
296 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
297 "version %c.%c.\n", extp->MajorVersion,
304 /* Install our own private info structure */
305 cfi->cmdset_priv = extp;
307 /* Apply cfi device specific fixups */
308 cfi_fixup(mtd, cfi_fixup_table);
310 #ifdef DEBUG_CFI_FEATURES
311 /* Tell the user about it in lots of lovely detail */
312 cfi_tell_features(extp);
315 bootloc = extp->TopBottom;
316 if ((bootloc != 2) && (bootloc != 3)) {
317 printk(KERN_WARNING "%s: CFI does not contain boot "
318 "bank location. Assuming top.\n", map->name);
322 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
323 printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
325 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
326 int j = (cfi->cfiq->NumEraseRegions-1)-i;
329 swap = cfi->cfiq->EraseRegionInfo[i];
330 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
331 cfi->cfiq->EraseRegionInfo[j] = swap;
334 /* Set the default CFI lock/unlock addresses */
335 cfi->addr_unlock1 = 0x555;
336 cfi->addr_unlock2 = 0x2aa;
337 /* Modify the unlock address if we are in compatibility mode */
338 if ( /* x16 in x8 mode */
339 ((cfi->device_type == CFI_DEVICETYPE_X8) &&
340 (cfi->cfiq->InterfaceDesc == 2)) ||
341 /* x32 in x16 mode */
342 ((cfi->device_type == CFI_DEVICETYPE_X16) &&
343 (cfi->cfiq->InterfaceDesc == 4)))
345 cfi->addr_unlock1 = 0xaaa;
346 cfi->addr_unlock2 = 0x555;
350 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
351 /* Apply jedec specific fixups */
352 cfi_fixup(mtd, jedec_fixup_table);
354 /* Apply generic fixups */
355 cfi_fixup(mtd, fixup_table);
357 for (i=0; i< cfi->numchips; i++) {
358 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
359 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
360 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
363 map->fldrv = &cfi_amdstd_chipdrv;
365 return cfi_amdstd_setup(mtd);
367 EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
369 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
371 struct map_info *map = mtd->priv;
372 struct cfi_private *cfi = map->fldrv_priv;
373 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
374 unsigned long offset = 0;
377 printk(KERN_NOTICE "number of %s chips: %d\n",
378 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
379 /* Select the correct geometry setup */
380 mtd->size = devsize * cfi->numchips;
382 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
383 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
384 * mtd->numeraseregions, GFP_KERNEL);
385 if (!mtd->eraseregions) {
386 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
390 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
391 unsigned long ernum, ersize;
392 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
393 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
395 if (mtd->erasesize < ersize) {
396 mtd->erasesize = ersize;
398 for (j=0; j<cfi->numchips; j++) {
399 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
400 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
401 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
403 offset += (ersize * ernum);
405 if (offset != devsize) {
407 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
412 for (i=0; i<mtd->numeraseregions;i++){
413 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
414 i,mtd->eraseregions[i].offset,
415 mtd->eraseregions[i].erasesize,
416 mtd->eraseregions[i].numblocks);
420 /* FIXME: erase-suspend-program is broken. See
421 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
422 printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
424 __module_get(THIS_MODULE);
429 kfree(mtd->eraseregions);
432 kfree(cfi->cmdset_priv);
438 * Return true if the chip is ready.
440 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
441 * non-suspended sector) and is indicated by no toggle bits toggling.
443 * Note that anything more complicated than checking if no bits are toggling
444 * (including checking DQ5 for an error status) is tricky to get working
445 * correctly and is therefore not done (particulary with interleaved chips
446 * as each chip must be checked independantly of the others).
448 static int __xipram chip_ready(struct map_info *map, unsigned long addr)
452 d = map_read(map, addr);
453 t = map_read(map, addr);
455 return map_word_equal(map, d, t);
459 * Return true if the chip is ready and has the correct value.
461 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
462 * non-suspended sector) and it is indicated by no bits toggling.
464 * Error are indicated by toggling bits or bits held with the wrong value,
465 * or with bits toggling.
467 * Note that anything more complicated than checking if no bits are toggling
468 * (including checking DQ5 for an error status) is tricky to get working
469 * correctly and is therefore not done (particulary with interleaved chips
470 * as each chip must be checked independantly of the others).
473 static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
477 oldd = map_read(map, addr);
478 curd = map_read(map, addr);
480 return map_word_equal(map, oldd, curd) &&
481 map_word_equal(map, curd, expected);
484 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
486 DECLARE_WAITQUEUE(wait, current);
487 struct cfi_private *cfi = map->fldrv_priv;
489 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
492 timeo = jiffies + HZ;
494 switch (chip->state) {
498 if (chip_ready(map, adr))
501 if (time_after(jiffies, timeo)) {
502 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
503 spin_unlock(chip->mutex);
506 spin_unlock(chip->mutex);
508 spin_lock(chip->mutex);
509 /* Someone else might have been playing with it. */
519 if (mode == FL_WRITING) /* FIXME: Erase-suspend-program appears broken. */
522 if (!(mode == FL_READY || mode == FL_POINT
524 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))
525 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x1))))
528 /* We could check to see if we're trying to access the sector
529 * that is currently being erased. However, no user will try
530 * anything like that so we just wait for the timeout. */
533 /* It's harmless to issue the Erase-Suspend and Erase-Resume
534 * commands when the erase algorithm isn't in progress. */
535 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
536 chip->oldstate = FL_ERASING;
537 chip->state = FL_ERASE_SUSPENDING;
538 chip->erase_suspended = 1;
540 if (chip_ready(map, adr))
543 if (time_after(jiffies, timeo)) {
544 /* Should have suspended the erase by now.
545 * Send an Erase-Resume command as either
546 * there was an error (so leave the erase
547 * routine to recover from it) or we trying to
548 * use the erase-in-progress sector. */
549 map_write(map, CMD(0x30), chip->in_progress_block_addr);
550 chip->state = FL_ERASING;
551 chip->oldstate = FL_READY;
552 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
556 spin_unlock(chip->mutex);
558 spin_lock(chip->mutex);
559 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
560 So we can just loop here. */
562 chip->state = FL_READY;
565 case FL_XIP_WHILE_ERASING:
566 if (mode != FL_READY && mode != FL_POINT &&
567 (!cfip || !(cfip->EraseSuspend&2)))
569 chip->oldstate = chip->state;
570 chip->state = FL_READY;
574 /* Only if there's no operation suspended... */
575 if (mode == FL_READY && chip->oldstate == FL_READY)
580 set_current_state(TASK_UNINTERRUPTIBLE);
581 add_wait_queue(&chip->wq, &wait);
582 spin_unlock(chip->mutex);
584 remove_wait_queue(&chip->wq, &wait);
585 spin_lock(chip->mutex);
591 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
593 struct cfi_private *cfi = map->fldrv_priv;
595 switch(chip->oldstate) {
597 chip->state = chip->oldstate;
598 map_write(map, CMD(0x30), chip->in_progress_block_addr);
599 chip->oldstate = FL_READY;
600 chip->state = FL_ERASING;
603 case FL_XIP_WHILE_ERASING:
604 chip->state = chip->oldstate;
605 chip->oldstate = FL_READY;
610 /* We should really make set_vpp() count, rather than doing this */
614 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
619 #ifdef CONFIG_MTD_XIP
622 * No interrupt what so ever can be serviced while the flash isn't in array
623 * mode. This is ensured by the xip_disable() and xip_enable() functions
624 * enclosing any code path where the flash is known not to be in array mode.
625 * And within a XIP disabled code path, only functions marked with __xipram
626 * may be called and nothing else (it's a good thing to inspect generated
627 * assembly to make sure inline functions were actually inlined and that gcc
628 * didn't emit calls to its own support functions). Also configuring MTD CFI
629 * support to a single buswidth and a single interleave is also recommended.
632 static void xip_disable(struct map_info *map, struct flchip *chip,
635 /* TODO: chips with no XIP use should ignore and return */
636 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
640 static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
643 struct cfi_private *cfi = map->fldrv_priv;
645 if (chip->state != FL_POINT && chip->state != FL_READY) {
646 map_write(map, CMD(0xf0), adr);
647 chip->state = FL_READY;
649 (void) map_read(map, adr);
655 * When a delay is required for the flash operation to complete, the
656 * xip_udelay() function is polling for both the given timeout and pending
657 * (but still masked) hardware interrupts. Whenever there is an interrupt
658 * pending then the flash erase operation is suspended, array mode restored
659 * and interrupts unmasked. Task scheduling might also happen at that
660 * point. The CPU eventually returns from the interrupt or the call to
661 * schedule() and the suspended flash operation is resumed for the remaining
662 * of the delay period.
664 * Warning: this function _will_ fool interrupt latency tracing tools.
667 static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
668 unsigned long adr, int usec)
670 struct cfi_private *cfi = map->fldrv_priv;
671 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
672 map_word status, OK = CMD(0x80);
673 unsigned long suspended, start = xip_currtime();
678 if (xip_irqpending() && extp &&
679 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
680 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
682 * Let's suspend the erase operation when supported.
683 * Note that we currently don't try to suspend
684 * interleaved chips if there is already another
685 * operation suspended (imagine what happens
686 * when one chip was already done with the current
687 * operation while another chip suspended it, then
688 * we resume the whole thing at once). Yes, it
691 map_write(map, CMD(0xb0), adr);
692 usec -= xip_elapsed_since(start);
693 suspended = xip_currtime();
695 if (xip_elapsed_since(suspended) > 100000) {
697 * The chip doesn't want to suspend
698 * after waiting for 100 msecs.
699 * This is a critical error but there
700 * is not much we can do here.
704 status = map_read(map, adr);
705 } while (!map_word_andequal(map, status, OK, OK));
707 /* Suspend succeeded */
708 oldstate = chip->state;
709 if (!map_word_bitsset(map, status, CMD(0x40)))
711 chip->state = FL_XIP_WHILE_ERASING;
712 chip->erase_suspended = 1;
713 map_write(map, CMD(0xf0), adr);
714 (void) map_read(map, adr);
715 asm volatile (".rep 8; nop; .endr");
717 spin_unlock(chip->mutex);
718 asm volatile (".rep 8; nop; .endr");
722 * We're back. However someone else might have
723 * decided to go write to the chip if we are in
724 * a suspended erase state. If so let's wait
727 spin_lock(chip->mutex);
728 while (chip->state != FL_XIP_WHILE_ERASING) {
729 DECLARE_WAITQUEUE(wait, current);
730 set_current_state(TASK_UNINTERRUPTIBLE);
731 add_wait_queue(&chip->wq, &wait);
732 spin_unlock(chip->mutex);
734 remove_wait_queue(&chip->wq, &wait);
735 spin_lock(chip->mutex);
737 /* Disallow XIP again */
740 /* Resume the write or erase operation */
741 map_write(map, CMD(0x30), adr);
742 chip->state = oldstate;
743 start = xip_currtime();
744 } else if (usec >= 1000000/HZ) {
746 * Try to save on CPU power when waiting delay
747 * is at least a system timer tick period.
748 * No need to be extremely accurate here.
752 status = map_read(map, adr);
753 } while (!map_word_andequal(map, status, OK, OK)
754 && xip_elapsed_since(start) < usec);
757 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
760 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
761 * the flash is actively programming or erasing since we have to poll for
762 * the operation to complete anyway. We can't do that in a generic way with
763 * a XIP setup so do it before the actual flash operation in this case
764 * and stub it out from INVALIDATE_CACHE_UDELAY.
766 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
767 INVALIDATE_CACHED_RANGE(map, from, size)
769 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
770 UDELAY(map, chip, adr, usec)
775 * Activating this XIP support changes the way the code works a bit. For
776 * example the code to suspend the current process when concurrent access
777 * happens is never executed because xip_udelay() will always return with the
778 * same chip state as it was entered with. This is why there is no care for
779 * the presence of add_wait_queue() or schedule() calls from within a couple
780 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
781 * The queueing and scheduling are always happening within xip_udelay().
783 * Similarly, get_chip() and put_chip() just happen to always be executed
784 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
785 * is in array mode, therefore never executing many cases therein and not
786 * causing any problem with XIP.
791 #define xip_disable(map, chip, adr)
792 #define xip_enable(map, chip, adr)
793 #define XIP_INVAL_CACHED_RANGE(x...)
795 #define UDELAY(map, chip, adr, usec) \
797 spin_unlock(chip->mutex); \
799 spin_lock(chip->mutex); \
802 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
804 spin_unlock(chip->mutex); \
805 INVALIDATE_CACHED_RANGE(map, adr, len); \
807 spin_lock(chip->mutex); \
812 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
814 unsigned long cmd_addr;
815 struct cfi_private *cfi = map->fldrv_priv;
820 /* Ensure cmd read/writes are aligned. */
821 cmd_addr = adr & ~(map_bankwidth(map)-1);
823 spin_lock(chip->mutex);
824 ret = get_chip(map, chip, cmd_addr, FL_READY);
826 spin_unlock(chip->mutex);
830 if (chip->state != FL_POINT && chip->state != FL_READY) {
831 map_write(map, CMD(0xf0), cmd_addr);
832 chip->state = FL_READY;
835 map_copy_from(map, buf, adr, len);
837 put_chip(map, chip, cmd_addr);
839 spin_unlock(chip->mutex);
844 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
846 struct map_info *map = mtd->priv;
847 struct cfi_private *cfi = map->fldrv_priv;
852 /* ofs: offset within the first chip that the first read should start */
854 chipnum = (from >> cfi->chipshift);
855 ofs = from - (chipnum << cfi->chipshift);
861 unsigned long thislen;
863 if (chipnum >= cfi->numchips)
866 if ((len + ofs -1) >> cfi->chipshift)
867 thislen = (1<<cfi->chipshift) - ofs;
871 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
886 static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
888 DECLARE_WAITQUEUE(wait, current);
889 unsigned long timeo = jiffies + HZ;
890 struct cfi_private *cfi = map->fldrv_priv;
893 spin_lock(chip->mutex);
895 if (chip->state != FL_READY){
897 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
899 set_current_state(TASK_UNINTERRUPTIBLE);
900 add_wait_queue(&chip->wq, &wait);
902 spin_unlock(chip->mutex);
905 remove_wait_queue(&chip->wq, &wait);
907 if(signal_pending(current))
910 timeo = jiffies + HZ;
917 chip->state = FL_READY;
919 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
920 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
921 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
923 map_copy_from(map, buf, adr, len);
925 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
926 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
927 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
928 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
931 spin_unlock(chip->mutex);
936 static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
938 struct map_info *map = mtd->priv;
939 struct cfi_private *cfi = map->fldrv_priv;
945 /* ofs: offset within the first chip that the first read should start */
947 /* 8 secsi bytes per chip */
955 unsigned long thislen;
957 if (chipnum >= cfi->numchips)
960 if ((len + ofs -1) >> 3)
961 thislen = (1<<3) - ofs;
965 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
980 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
982 struct cfi_private *cfi = map->fldrv_priv;
983 unsigned long timeo = jiffies + HZ;
985 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
986 * have a max write time of a few hundreds usec). However, we should
987 * use the maximum timeout value given by the chip at probe time
988 * instead. Unfortunately, struct flchip does have a field for
989 * maximum timeout, only for typical which can be far too short
990 * depending of the conditions. The ' + 1' is to avoid having a
991 * timeout of 0 jiffies if HZ is smaller than 1000.
993 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1000 spin_lock(chip->mutex);
1001 ret = get_chip(map, chip, adr, FL_WRITING);
1003 spin_unlock(chip->mutex);
1007 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1008 __func__, adr, datum.x[0] );
1011 * Check for a NOP for the case when the datum to write is already
1012 * present - it saves time and works around buggy chips that corrupt
1013 * data at other locations when 0xff is written to a location that
1014 * already contains 0xff.
1016 oldd = map_read(map, adr);
1017 if (map_word_equal(map, oldd, datum)) {
1018 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1023 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1025 xip_disable(map, chip, adr);
1027 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1028 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1029 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1030 map_write(map, datum, adr);
1031 chip->state = FL_WRITING;
1033 INVALIDATE_CACHE_UDELAY(map, chip,
1034 adr, map_bankwidth(map),
1035 chip->word_write_time);
1037 /* See comment above for timeout value. */
1038 timeo = jiffies + uWriteTimeout;
1040 if (chip->state != FL_WRITING) {
1041 /* Someone's suspended the write. Sleep */
1042 DECLARE_WAITQUEUE(wait, current);
1044 set_current_state(TASK_UNINTERRUPTIBLE);
1045 add_wait_queue(&chip->wq, &wait);
1046 spin_unlock(chip->mutex);
1048 remove_wait_queue(&chip->wq, &wait);
1049 timeo = jiffies + (HZ / 2); /* FIXME */
1050 spin_lock(chip->mutex);
1054 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
1055 xip_enable(map, chip, adr);
1056 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
1057 xip_disable(map, chip, adr);
1061 if (chip_ready(map, adr))
1064 /* Latency issues. Drop the lock, wait a while and retry */
1065 UDELAY(map, chip, adr, 1);
1067 /* Did we succeed? */
1068 if (!chip_good(map, adr, datum)) {
1069 /* reset on all failures. */
1070 map_write( map, CMD(0xF0), chip->start );
1071 /* FIXME - should have reset delay before continuing */
1073 if (++retry_cnt <= MAX_WORD_RETRIES)
1078 xip_enable(map, chip, adr);
1080 chip->state = FL_READY;
1081 put_chip(map, chip, adr);
1082 spin_unlock(chip->mutex);
1088 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1089 size_t *retlen, const u_char *buf)
1091 struct map_info *map = mtd->priv;
1092 struct cfi_private *cfi = map->fldrv_priv;
1095 unsigned long ofs, chipstart;
1096 DECLARE_WAITQUEUE(wait, current);
1102 chipnum = to >> cfi->chipshift;
1103 ofs = to - (chipnum << cfi->chipshift);
1104 chipstart = cfi->chips[chipnum].start;
1106 /* If it's not bus-aligned, do the first byte write */
1107 if (ofs & (map_bankwidth(map)-1)) {
1108 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1109 int i = ofs - bus_ofs;
1114 spin_lock(cfi->chips[chipnum].mutex);
1116 if (cfi->chips[chipnum].state != FL_READY) {
1118 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1120 set_current_state(TASK_UNINTERRUPTIBLE);
1121 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1123 spin_unlock(cfi->chips[chipnum].mutex);
1126 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1128 if(signal_pending(current))
1134 /* Load 'tmp_buf' with old contents of flash */
1135 tmp_buf = map_read(map, bus_ofs+chipstart);
1137 spin_unlock(cfi->chips[chipnum].mutex);
1139 /* Number of bytes to copy from buffer */
1140 n = min_t(int, len, map_bankwidth(map)-i);
1142 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1144 ret = do_write_oneword(map, &cfi->chips[chipnum],
1154 if (ofs >> cfi->chipshift) {
1157 if (chipnum == cfi->numchips)
1162 /* We are now aligned, write as much as possible */
1163 while(len >= map_bankwidth(map)) {
1166 datum = map_word_load(map, buf);
1168 ret = do_write_oneword(map, &cfi->chips[chipnum],
1173 ofs += map_bankwidth(map);
1174 buf += map_bankwidth(map);
1175 (*retlen) += map_bankwidth(map);
1176 len -= map_bankwidth(map);
1178 if (ofs >> cfi->chipshift) {
1181 if (chipnum == cfi->numchips)
1183 chipstart = cfi->chips[chipnum].start;
1187 /* Write the trailing bytes if any */
1188 if (len & (map_bankwidth(map)-1)) {
1192 spin_lock(cfi->chips[chipnum].mutex);
1194 if (cfi->chips[chipnum].state != FL_READY) {
1196 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1198 set_current_state(TASK_UNINTERRUPTIBLE);
1199 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1201 spin_unlock(cfi->chips[chipnum].mutex);
1204 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1206 if(signal_pending(current))
1212 tmp_buf = map_read(map, ofs + chipstart);
1214 spin_unlock(cfi->chips[chipnum].mutex);
1216 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1218 ret = do_write_oneword(map, &cfi->chips[chipnum],
1231 * FIXME: interleaved mode not tested, and probably not supported!
1233 static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
1234 unsigned long adr, const u_char *buf,
1237 struct cfi_private *cfi = map->fldrv_priv;
1238 unsigned long timeo = jiffies + HZ;
1239 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1240 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1242 unsigned long cmd_adr;
1249 spin_lock(chip->mutex);
1250 ret = get_chip(map, chip, adr, FL_WRITING);
1252 spin_unlock(chip->mutex);
1256 datum = map_word_load(map, buf);
1258 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1259 __func__, adr, datum.x[0] );
1261 XIP_INVAL_CACHED_RANGE(map, adr, len);
1263 xip_disable(map, chip, cmd_adr);
1265 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1266 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1267 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1269 /* Write Buffer Load */
1270 map_write(map, CMD(0x25), cmd_adr);
1272 chip->state = FL_WRITING_TO_BUFFER;
1274 /* Write length of data to come */
1275 words = len / map_bankwidth(map);
1276 map_write(map, CMD(words - 1), cmd_adr);
1279 while(z < words * map_bankwidth(map)) {
1280 datum = map_word_load(map, buf);
1281 map_write(map, datum, adr + z);
1283 z += map_bankwidth(map);
1284 buf += map_bankwidth(map);
1286 z -= map_bankwidth(map);
1290 /* Write Buffer Program Confirm: GO GO GO */
1291 map_write(map, CMD(0x29), cmd_adr);
1292 chip->state = FL_WRITING;
1294 INVALIDATE_CACHE_UDELAY(map, chip,
1295 adr, map_bankwidth(map),
1296 chip->word_write_time);
1298 timeo = jiffies + uWriteTimeout;
1301 if (chip->state != FL_WRITING) {
1302 /* Someone's suspended the write. Sleep */
1303 DECLARE_WAITQUEUE(wait, current);
1305 set_current_state(TASK_UNINTERRUPTIBLE);
1306 add_wait_queue(&chip->wq, &wait);
1307 spin_unlock(chip->mutex);
1309 remove_wait_queue(&chip->wq, &wait);
1310 timeo = jiffies + (HZ / 2); /* FIXME */
1311 spin_lock(chip->mutex);
1315 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1318 if (chip_ready(map, adr)) {
1319 xip_enable(map, chip, adr);
1323 /* Latency issues. Drop the lock, wait a while and retry */
1324 UDELAY(map, chip, adr, 1);
1327 /* reset on all failures. */
1328 map_write( map, CMD(0xF0), chip->start );
1329 xip_enable(map, chip, adr);
1330 /* FIXME - should have reset delay before continuing */
1332 printk(KERN_WARNING "MTD %s(): software timeout\n",
1337 chip->state = FL_READY;
1338 put_chip(map, chip, adr);
1339 spin_unlock(chip->mutex);
1345 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1346 size_t *retlen, const u_char *buf)
1348 struct map_info *map = mtd->priv;
1349 struct cfi_private *cfi = map->fldrv_priv;
1350 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1359 chipnum = to >> cfi->chipshift;
1360 ofs = to - (chipnum << cfi->chipshift);
1362 /* If it's not bus-aligned, do the first word write */
1363 if (ofs & (map_bankwidth(map)-1)) {
1364 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1365 if (local_len > len)
1367 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1368 local_len, retlen, buf);
1375 if (ofs >> cfi->chipshift) {
1378 if (chipnum == cfi->numchips)
1383 /* Write buffer is worth it only if more than one word to write... */
1384 while (len >= map_bankwidth(map) * 2) {
1385 /* We must not cross write block boundaries */
1386 int size = wbufsize - (ofs & (wbufsize-1));
1390 if (size % map_bankwidth(map))
1391 size -= size % map_bankwidth(map);
1393 ret = do_write_buffer(map, &cfi->chips[chipnum],
1403 if (ofs >> cfi->chipshift) {
1406 if (chipnum == cfi->numchips)
1412 size_t retlen_dregs = 0;
1414 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1415 len, &retlen_dregs, buf);
1417 *retlen += retlen_dregs;
1426 * Handle devices with one erase region, that only implement
1427 * the chip erase command.
1429 static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
1431 struct cfi_private *cfi = map->fldrv_priv;
1432 unsigned long timeo = jiffies + HZ;
1433 unsigned long int adr;
1434 DECLARE_WAITQUEUE(wait, current);
1437 adr = cfi->addr_unlock1;
1439 spin_lock(chip->mutex);
1440 ret = get_chip(map, chip, adr, FL_WRITING);
1442 spin_unlock(chip->mutex);
1446 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1447 __func__, chip->start );
1449 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
1451 xip_disable(map, chip, adr);
1453 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1454 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1455 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1456 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1457 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1458 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1460 chip->state = FL_ERASING;
1461 chip->erase_suspended = 0;
1462 chip->in_progress_block_addr = adr;
1464 INVALIDATE_CACHE_UDELAY(map, chip,
1466 chip->erase_time*500);
1468 timeo = jiffies + (HZ*20);
1471 if (chip->state != FL_ERASING) {
1472 /* Someone's suspended the erase. Sleep */
1473 set_current_state(TASK_UNINTERRUPTIBLE);
1474 add_wait_queue(&chip->wq, &wait);
1475 spin_unlock(chip->mutex);
1477 remove_wait_queue(&chip->wq, &wait);
1478 spin_lock(chip->mutex);
1481 if (chip->erase_suspended) {
1482 /* This erase was suspended and resumed.
1483 Adjust the timeout */
1484 timeo = jiffies + (HZ*20); /* FIXME */
1485 chip->erase_suspended = 0;
1488 if (chip_ready(map, adr))
1491 if (time_after(jiffies, timeo)) {
1492 printk(KERN_WARNING "MTD %s(): software timeout\n",
1497 /* Latency issues. Drop the lock, wait a while and retry */
1498 UDELAY(map, chip, adr, 1000000/HZ);
1500 /* Did we succeed? */
1501 if (!chip_good(map, adr, map_word_ff(map))) {
1502 /* reset on all failures. */
1503 map_write( map, CMD(0xF0), chip->start );
1504 /* FIXME - should have reset delay before continuing */
1509 chip->state = FL_READY;
1510 xip_enable(map, chip, adr);
1511 put_chip(map, chip, adr);
1512 spin_unlock(chip->mutex);
1518 static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
1520 struct cfi_private *cfi = map->fldrv_priv;
1521 unsigned long timeo = jiffies + HZ;
1522 DECLARE_WAITQUEUE(wait, current);
1527 spin_lock(chip->mutex);
1528 ret = get_chip(map, chip, adr, FL_ERASING);
1530 spin_unlock(chip->mutex);
1534 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1537 XIP_INVAL_CACHED_RANGE(map, adr, len);
1539 xip_disable(map, chip, adr);
1541 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1542 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1543 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1544 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1545 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1546 map_write(map, CMD(0x30), adr);
1548 chip->state = FL_ERASING;
1549 chip->erase_suspended = 0;
1550 chip->in_progress_block_addr = adr;
1552 INVALIDATE_CACHE_UDELAY(map, chip,
1554 chip->erase_time*500);
1556 timeo = jiffies + (HZ*20);
1559 if (chip->state != FL_ERASING) {
1560 /* Someone's suspended the erase. Sleep */
1561 set_current_state(TASK_UNINTERRUPTIBLE);
1562 add_wait_queue(&chip->wq, &wait);
1563 spin_unlock(chip->mutex);
1565 remove_wait_queue(&chip->wq, &wait);
1566 spin_lock(chip->mutex);
1569 if (chip->erase_suspended) {
1570 /* This erase was suspended and resumed.
1571 Adjust the timeout */
1572 timeo = jiffies + (HZ*20); /* FIXME */
1573 chip->erase_suspended = 0;
1576 if (chip_ready(map, adr)) {
1577 xip_enable(map, chip, adr);
1581 if (time_after(jiffies, timeo)) {
1582 xip_enable(map, chip, adr);
1583 printk(KERN_WARNING "MTD %s(): software timeout\n",
1588 /* Latency issues. Drop the lock, wait a while and retry */
1589 UDELAY(map, chip, adr, 1000000/HZ);
1591 /* Did we succeed? */
1592 if (!chip_good(map, adr, map_word_ff(map))) {
1593 /* reset on all failures. */
1594 map_write( map, CMD(0xF0), chip->start );
1595 /* FIXME - should have reset delay before continuing */
1600 chip->state = FL_READY;
1601 put_chip(map, chip, adr);
1602 spin_unlock(chip->mutex);
1607 int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1609 unsigned long ofs, len;
1615 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1619 instr->state = MTD_ERASE_DONE;
1620 mtd_erase_callback(instr);
1626 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1628 struct map_info *map = mtd->priv;
1629 struct cfi_private *cfi = map->fldrv_priv;
1632 if (instr->addr != 0)
1635 if (instr->len != mtd->size)
1638 ret = do_erase_chip(map, &cfi->chips[0]);
1642 instr->state = MTD_ERASE_DONE;
1643 mtd_erase_callback(instr);
1648 static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1649 unsigned long adr, int len, void *thunk)
1651 struct cfi_private *cfi = map->fldrv_priv;
1654 spin_lock(chip->mutex);
1655 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1658 chip->state = FL_LOCKING;
1660 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1661 __func__, adr, len);
1663 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1664 cfi->device_type, NULL);
1665 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1666 cfi->device_type, NULL);
1667 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1668 cfi->device_type, NULL);
1669 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1670 cfi->device_type, NULL);
1671 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1672 cfi->device_type, NULL);
1673 map_write(map, CMD(0x40), chip->start + adr);
1675 chip->state = FL_READY;
1676 put_chip(map, chip, adr + chip->start);
1680 spin_unlock(chip->mutex);
1684 static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1685 unsigned long adr, int len, void *thunk)
1687 struct cfi_private *cfi = map->fldrv_priv;
1690 spin_lock(chip->mutex);
1691 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1694 chip->state = FL_UNLOCKING;
1696 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1697 __func__, adr, len);
1699 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1700 cfi->device_type, NULL);
1701 map_write(map, CMD(0x70), adr);
1703 chip->state = FL_READY;
1704 put_chip(map, chip, adr + chip->start);
1708 spin_unlock(chip->mutex);
1712 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1714 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1717 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1719 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1723 static void cfi_amdstd_sync (struct mtd_info *mtd)
1725 struct map_info *map = mtd->priv;
1726 struct cfi_private *cfi = map->fldrv_priv;
1728 struct flchip *chip;
1730 DECLARE_WAITQUEUE(wait, current);
1732 for (i=0; !ret && i<cfi->numchips; i++) {
1733 chip = &cfi->chips[i];
1736 spin_lock(chip->mutex);
1738 switch(chip->state) {
1742 case FL_JEDEC_QUERY:
1743 chip->oldstate = chip->state;
1744 chip->state = FL_SYNCING;
1745 /* No need to wake_up() on this state change -
1746 * as the whole point is that nobody can do anything
1747 * with the chip now anyway.
1750 spin_unlock(chip->mutex);
1754 /* Not an idle state */
1755 add_wait_queue(&chip->wq, &wait);
1757 spin_unlock(chip->mutex);
1761 remove_wait_queue(&chip->wq, &wait);
1767 /* Unlock the chips again */
1769 for (i--; i >=0; i--) {
1770 chip = &cfi->chips[i];
1772 spin_lock(chip->mutex);
1774 if (chip->state == FL_SYNCING) {
1775 chip->state = chip->oldstate;
1778 spin_unlock(chip->mutex);
1783 static int cfi_amdstd_suspend(struct mtd_info *mtd)
1785 struct map_info *map = mtd->priv;
1786 struct cfi_private *cfi = map->fldrv_priv;
1788 struct flchip *chip;
1791 for (i=0; !ret && i<cfi->numchips; i++) {
1792 chip = &cfi->chips[i];
1794 spin_lock(chip->mutex);
1796 switch(chip->state) {
1800 case FL_JEDEC_QUERY:
1801 chip->oldstate = chip->state;
1802 chip->state = FL_PM_SUSPENDED;
1803 /* No need to wake_up() on this state change -
1804 * as the whole point is that nobody can do anything
1805 * with the chip now anyway.
1807 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 chip->state = chip->oldstate;
1829 spin_unlock(chip->mutex);
1837 static void cfi_amdstd_resume(struct mtd_info *mtd)
1839 struct map_info *map = mtd->priv;
1840 struct cfi_private *cfi = map->fldrv_priv;
1842 struct flchip *chip;
1844 for (i=0; i<cfi->numchips; i++) {
1846 chip = &cfi->chips[i];
1848 spin_lock(chip->mutex);
1850 if (chip->state == FL_PM_SUSPENDED) {
1851 chip->state = FL_READY;
1852 map_write(map, CMD(0xF0), chip->start);
1856 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1858 spin_unlock(chip->mutex);
1862 static void cfi_amdstd_destroy(struct mtd_info *mtd)
1864 struct map_info *map = mtd->priv;
1865 struct cfi_private *cfi = map->fldrv_priv;
1867 kfree(cfi->cmdset_priv);
1870 kfree(mtd->eraseregions);
1873 MODULE_LICENSE("GPL");
1874 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1875 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");