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 SST49LF040B 0x0050
52 #define SST49LF008A 0x005a
53 #define AT49BV6416 0x00d6
55 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
56 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
57 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
58 static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
59 static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
60 static void cfi_amdstd_sync (struct mtd_info *);
61 static int cfi_amdstd_suspend (struct mtd_info *);
62 static void cfi_amdstd_resume (struct mtd_info *);
63 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
65 static void cfi_amdstd_destroy(struct mtd_info *);
67 struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
68 static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
70 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
71 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
74 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
75 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
77 static struct mtd_chip_driver cfi_amdstd_chipdrv = {
78 .probe = NULL, /* Not usable directly */
79 .destroy = cfi_amdstd_destroy,
80 .name = "cfi_cmdset_0002",
85 /* #define DEBUG_CFI_FEATURES */
88 #ifdef DEBUG_CFI_FEATURES
89 static void cfi_tell_features(struct cfi_pri_amdstd *extp)
91 const char* erase_suspend[3] = {
92 "Not supported", "Read only", "Read/write"
94 const char* top_bottom[6] = {
95 "No WP", "8x8KiB sectors at top & bottom, no WP",
96 "Bottom boot", "Top boot",
97 "Uniform, Bottom WP", "Uniform, Top WP"
100 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
101 printk(" Address sensitive unlock: %s\n",
102 (extp->SiliconRevision & 1) ? "Not required" : "Required");
104 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
105 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
107 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
109 if (extp->BlkProt == 0)
110 printk(" Block protection: Not supported\n");
112 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
115 printk(" Temporary block unprotect: %s\n",
116 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
117 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
118 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
119 printk(" Burst mode: %s\n",
120 extp->BurstMode ? "Supported" : "Not supported");
121 if (extp->PageMode == 0)
122 printk(" Page mode: Not supported\n");
124 printk(" Page mode: %d word page\n", extp->PageMode << 2);
126 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
127 extp->VppMin >> 4, extp->VppMin & 0xf);
128 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
129 extp->VppMax >> 4, extp->VppMax & 0xf);
131 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
132 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
134 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
138 #ifdef AMD_BOOTLOC_BUG
139 /* Wheee. Bring me the head of someone at AMD. */
140 static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
142 struct map_info *map = mtd->priv;
143 struct cfi_private *cfi = map->fldrv_priv;
144 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
145 __u8 major = extp->MajorVersion;
146 __u8 minor = extp->MinorVersion;
148 if (((major << 8) | minor) < 0x3131) {
149 /* CFI version 1.0 => don't trust bootloc */
150 if (cfi->id & 0x80) {
151 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
152 extp->TopBottom = 3; /* top boot */
154 extp->TopBottom = 2; /* bottom boot */
160 static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
162 struct map_info *map = mtd->priv;
163 struct cfi_private *cfi = map->fldrv_priv;
164 if (cfi->cfiq->BufWriteTimeoutTyp) {
165 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
166 mtd->write = cfi_amdstd_write_buffers;
170 /* Atmel chips don't use the same PRI format as AMD chips */
171 static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
173 struct map_info *map = mtd->priv;
174 struct cfi_private *cfi = map->fldrv_priv;
175 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
176 struct cfi_pri_atmel atmel_pri;
178 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
179 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
181 if (atmel_pri.Features & 0x02)
182 extp->EraseSuspend = 2;
184 if (atmel_pri.BottomBoot)
190 static void fixup_use_secsi(struct mtd_info *mtd, void *param)
192 /* Setup for chips with a secsi area */
193 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
194 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
197 static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
199 struct map_info *map = mtd->priv;
200 struct cfi_private *cfi = map->fldrv_priv;
201 if ((cfi->cfiq->NumEraseRegions == 1) &&
202 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
203 mtd->erase = cfi_amdstd_erase_chip;
209 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
212 static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
214 mtd->lock = cfi_atmel_lock;
215 mtd->unlock = cfi_atmel_unlock;
216 mtd->flags |= MTD_STUPID_LOCK;
219 static struct cfi_fixup cfi_fixup_table[] = {
220 #ifdef AMD_BOOTLOC_BUG
221 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
223 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
224 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
225 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
226 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
227 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
228 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
229 #if !FORCE_WORD_WRITE
230 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
232 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
235 static struct cfi_fixup jedec_fixup_table[] = {
236 { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
237 { MANUFACTURER_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
238 { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
242 static struct cfi_fixup fixup_table[] = {
243 /* The CFI vendor ids and the JEDEC vendor IDs appear
244 * to be common. It is like the devices id's are as
245 * well. This table is to pick all cases where
246 * we know that is the case.
248 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
249 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
254 struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
256 struct cfi_private *cfi = map->fldrv_priv;
257 struct mtd_info *mtd;
260 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
262 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
265 memset(mtd, 0, sizeof(*mtd));
267 mtd->type = MTD_NORFLASH;
269 /* Fill in the default mtd operations */
270 mtd->erase = cfi_amdstd_erase_varsize;
271 mtd->write = cfi_amdstd_write_words;
272 mtd->read = cfi_amdstd_read;
273 mtd->sync = cfi_amdstd_sync;
274 mtd->suspend = cfi_amdstd_suspend;
275 mtd->resume = cfi_amdstd_resume;
276 mtd->flags = MTD_CAP_NORFLASH;
277 mtd->name = map->name;
280 if (cfi->cfi_mode==CFI_MODE_CFI){
281 unsigned char bootloc;
283 * It's a real CFI chip, not one for which the probe
284 * routine faked a CFI structure. So we read the feature
287 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
288 struct cfi_pri_amdstd *extp;
290 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
296 if (extp->MajorVersion != '1' ||
297 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
298 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
299 "version %c.%c.\n", extp->MajorVersion,
306 /* Install our own private info structure */
307 cfi->cmdset_priv = extp;
309 /* Apply cfi device specific fixups */
310 cfi_fixup(mtd, cfi_fixup_table);
312 #ifdef DEBUG_CFI_FEATURES
313 /* Tell the user about it in lots of lovely detail */
314 cfi_tell_features(extp);
317 bootloc = extp->TopBottom;
318 if ((bootloc != 2) && (bootloc != 3)) {
319 printk(KERN_WARNING "%s: CFI does not contain boot "
320 "bank location. Assuming top.\n", map->name);
324 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
325 printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
327 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
328 int j = (cfi->cfiq->NumEraseRegions-1)-i;
331 swap = cfi->cfiq->EraseRegionInfo[i];
332 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
333 cfi->cfiq->EraseRegionInfo[j] = swap;
336 /* Set the default CFI lock/unlock addresses */
337 cfi->addr_unlock1 = 0x555;
338 cfi->addr_unlock2 = 0x2aa;
339 /* Modify the unlock address if we are in compatibility mode */
340 if ( /* x16 in x8 mode */
341 ((cfi->device_type == CFI_DEVICETYPE_X8) &&
342 (cfi->cfiq->InterfaceDesc == 2)) ||
343 /* x32 in x16 mode */
344 ((cfi->device_type == CFI_DEVICETYPE_X16) &&
345 (cfi->cfiq->InterfaceDesc == 4)))
347 cfi->addr_unlock1 = 0xaaa;
348 cfi->addr_unlock2 = 0x555;
352 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
353 /* Apply jedec specific fixups */
354 cfi_fixup(mtd, jedec_fixup_table);
356 /* Apply generic fixups */
357 cfi_fixup(mtd, fixup_table);
359 for (i=0; i< cfi->numchips; i++) {
360 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
361 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
362 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
365 map->fldrv = &cfi_amdstd_chipdrv;
367 return cfi_amdstd_setup(mtd);
369 EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
371 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
373 struct map_info *map = mtd->priv;
374 struct cfi_private *cfi = map->fldrv_priv;
375 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
376 unsigned long offset = 0;
379 printk(KERN_NOTICE "number of %s chips: %d\n",
380 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
381 /* Select the correct geometry setup */
382 mtd->size = devsize * cfi->numchips;
384 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
385 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
386 * mtd->numeraseregions, GFP_KERNEL);
387 if (!mtd->eraseregions) {
388 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
392 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
393 unsigned long ernum, ersize;
394 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
395 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
397 if (mtd->erasesize < ersize) {
398 mtd->erasesize = ersize;
400 for (j=0; j<cfi->numchips; j++) {
401 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
402 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
403 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
405 offset += (ersize * ernum);
407 if (offset != devsize) {
409 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
414 for (i=0; i<mtd->numeraseregions;i++){
415 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
416 i,mtd->eraseregions[i].offset,
417 mtd->eraseregions[i].erasesize,
418 mtd->eraseregions[i].numblocks);
422 /* FIXME: erase-suspend-program is broken. See
423 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
424 printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
426 __module_get(THIS_MODULE);
431 kfree(mtd->eraseregions);
434 kfree(cfi->cmdset_priv);
440 * Return true if the chip is ready.
442 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
443 * non-suspended sector) and is indicated by no toggle bits toggling.
445 * Note that anything more complicated than checking if no bits are toggling
446 * (including checking DQ5 for an error status) is tricky to get working
447 * correctly and is therefore not done (particulary with interleaved chips
448 * as each chip must be checked independantly of the others).
450 static int __xipram chip_ready(struct map_info *map, unsigned long addr)
454 d = map_read(map, addr);
455 t = map_read(map, addr);
457 return map_word_equal(map, d, t);
461 * Return true if the chip is ready and has the correct value.
463 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
464 * non-suspended sector) and it is indicated by no bits toggling.
466 * Error are indicated by toggling bits or bits held with the wrong value,
467 * or with bits toggling.
469 * Note that anything more complicated than checking if no bits are toggling
470 * (including checking DQ5 for an error status) is tricky to get working
471 * correctly and is therefore not done (particulary with interleaved chips
472 * as each chip must be checked independantly of the others).
475 static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
479 oldd = map_read(map, addr);
480 curd = map_read(map, addr);
482 return map_word_equal(map, oldd, curd) &&
483 map_word_equal(map, curd, expected);
486 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
488 DECLARE_WAITQUEUE(wait, current);
489 struct cfi_private *cfi = map->fldrv_priv;
491 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
494 timeo = jiffies + HZ;
496 switch (chip->state) {
500 if (chip_ready(map, adr))
503 if (time_after(jiffies, timeo)) {
504 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
505 spin_unlock(chip->mutex);
508 spin_unlock(chip->mutex);
510 spin_lock(chip->mutex);
511 /* Someone else might have been playing with it. */
521 if (mode == FL_WRITING) /* FIXME: Erase-suspend-program appears broken. */
524 if (!( mode == FL_READY
527 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))
528 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x1)
532 /* We could check to see if we're trying to access the sector
533 * that is currently being erased. However, no user will try
534 * anything like that so we just wait for the timeout. */
537 /* It's harmless to issue the Erase-Suspend and Erase-Resume
538 * commands when the erase algorithm isn't in progress. */
539 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
540 chip->oldstate = FL_ERASING;
541 chip->state = FL_ERASE_SUSPENDING;
542 chip->erase_suspended = 1;
544 if (chip_ready(map, adr))
547 if (time_after(jiffies, timeo)) {
548 /* Should have suspended the erase by now.
549 * Send an Erase-Resume command as either
550 * there was an error (so leave the erase
551 * routine to recover from it) or we trying to
552 * use the erase-in-progress sector. */
553 map_write(map, CMD(0x30), chip->in_progress_block_addr);
554 chip->state = FL_ERASING;
555 chip->oldstate = FL_READY;
556 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
560 spin_unlock(chip->mutex);
562 spin_lock(chip->mutex);
563 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
564 So we can just loop here. */
566 chip->state = FL_READY;
569 case FL_XIP_WHILE_ERASING:
570 if (mode != FL_READY && mode != FL_POINT &&
571 (!cfip || !(cfip->EraseSuspend&2)))
573 chip->oldstate = chip->state;
574 chip->state = FL_READY;
578 /* Only if there's no operation suspended... */
579 if (mode == FL_READY && chip->oldstate == FL_READY)
584 set_current_state(TASK_UNINTERRUPTIBLE);
585 add_wait_queue(&chip->wq, &wait);
586 spin_unlock(chip->mutex);
588 remove_wait_queue(&chip->wq, &wait);
589 spin_lock(chip->mutex);
595 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
597 struct cfi_private *cfi = map->fldrv_priv;
599 switch(chip->oldstate) {
601 chip->state = chip->oldstate;
602 map_write(map, CMD(0x30), chip->in_progress_block_addr);
603 chip->oldstate = FL_READY;
604 chip->state = FL_ERASING;
607 case FL_XIP_WHILE_ERASING:
608 chip->state = chip->oldstate;
609 chip->oldstate = FL_READY;
614 /* We should really make set_vpp() count, rather than doing this */
618 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
623 #ifdef CONFIG_MTD_XIP
626 * No interrupt what so ever can be serviced while the flash isn't in array
627 * mode. This is ensured by the xip_disable() and xip_enable() functions
628 * enclosing any code path where the flash is known not to be in array mode.
629 * And within a XIP disabled code path, only functions marked with __xipram
630 * may be called and nothing else (it's a good thing to inspect generated
631 * assembly to make sure inline functions were actually inlined and that gcc
632 * didn't emit calls to its own support functions). Also configuring MTD CFI
633 * support to a single buswidth and a single interleave is also recommended.
636 static void xip_disable(struct map_info *map, struct flchip *chip,
639 /* TODO: chips with no XIP use should ignore and return */
640 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
644 static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
647 struct cfi_private *cfi = map->fldrv_priv;
649 if (chip->state != FL_POINT && chip->state != FL_READY) {
650 map_write(map, CMD(0xf0), adr);
651 chip->state = FL_READY;
653 (void) map_read(map, adr);
659 * When a delay is required for the flash operation to complete, the
660 * xip_udelay() function is polling for both the given timeout and pending
661 * (but still masked) hardware interrupts. Whenever there is an interrupt
662 * pending then the flash erase operation is suspended, array mode restored
663 * and interrupts unmasked. Task scheduling might also happen at that
664 * point. The CPU eventually returns from the interrupt or the call to
665 * schedule() and the suspended flash operation is resumed for the remaining
666 * of the delay period.
668 * Warning: this function _will_ fool interrupt latency tracing tools.
671 static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
672 unsigned long adr, int usec)
674 struct cfi_private *cfi = map->fldrv_priv;
675 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
676 map_word status, OK = CMD(0x80);
677 unsigned long suspended, start = xip_currtime();
682 if (xip_irqpending() && extp &&
683 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
684 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
686 * Let's suspend the erase operation when supported.
687 * Note that we currently don't try to suspend
688 * interleaved chips if there is already another
689 * operation suspended (imagine what happens
690 * when one chip was already done with the current
691 * operation while another chip suspended it, then
692 * we resume the whole thing at once). Yes, it
695 map_write(map, CMD(0xb0), adr);
696 usec -= xip_elapsed_since(start);
697 suspended = xip_currtime();
699 if (xip_elapsed_since(suspended) > 100000) {
701 * The chip doesn't want to suspend
702 * after waiting for 100 msecs.
703 * This is a critical error but there
704 * is not much we can do here.
708 status = map_read(map, adr);
709 } while (!map_word_andequal(map, status, OK, OK));
711 /* Suspend succeeded */
712 oldstate = chip->state;
713 if (!map_word_bitsset(map, status, CMD(0x40)))
715 chip->state = FL_XIP_WHILE_ERASING;
716 chip->erase_suspended = 1;
717 map_write(map, CMD(0xf0), adr);
718 (void) map_read(map, adr);
719 asm volatile (".rep 8; nop; .endr");
721 spin_unlock(chip->mutex);
722 asm volatile (".rep 8; nop; .endr");
726 * We're back. However someone else might have
727 * decided to go write to the chip if we are in
728 * a suspended erase state. If so let's wait
731 spin_lock(chip->mutex);
732 while (chip->state != FL_XIP_WHILE_ERASING) {
733 DECLARE_WAITQUEUE(wait, current);
734 set_current_state(TASK_UNINTERRUPTIBLE);
735 add_wait_queue(&chip->wq, &wait);
736 spin_unlock(chip->mutex);
738 remove_wait_queue(&chip->wq, &wait);
739 spin_lock(chip->mutex);
741 /* Disallow XIP again */
744 /* Resume the write or erase operation */
745 map_write(map, CMD(0x30), adr);
746 chip->state = oldstate;
747 start = xip_currtime();
748 } else if (usec >= 1000000/HZ) {
750 * Try to save on CPU power when waiting delay
751 * is at least a system timer tick period.
752 * No need to be extremely accurate here.
756 status = map_read(map, adr);
757 } while (!map_word_andequal(map, status, OK, OK)
758 && xip_elapsed_since(start) < usec);
761 #define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
764 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
765 * the flash is actively programming or erasing since we have to poll for
766 * the operation to complete anyway. We can't do that in a generic way with
767 * a XIP setup so do it before the actual flash operation in this case
768 * and stub it out from INVALIDATE_CACHE_UDELAY.
770 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
771 INVALIDATE_CACHED_RANGE(map, from, size)
773 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
774 UDELAY(map, chip, adr, usec)
779 * Activating this XIP support changes the way the code works a bit. For
780 * example the code to suspend the current process when concurrent access
781 * happens is never executed because xip_udelay() will always return with the
782 * same chip state as it was entered with. This is why there is no care for
783 * the presence of add_wait_queue() or schedule() calls from within a couple
784 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
785 * The queueing and scheduling are always happening within xip_udelay().
787 * Similarly, get_chip() and put_chip() just happen to always be executed
788 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
789 * is in array mode, therefore never executing many cases therein and not
790 * causing any problem with XIP.
795 #define xip_disable(map, chip, adr)
796 #define xip_enable(map, chip, adr)
797 #define XIP_INVAL_CACHED_RANGE(x...)
799 #define UDELAY(map, chip, adr, usec) \
801 spin_unlock(chip->mutex); \
803 spin_lock(chip->mutex); \
806 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
808 spin_unlock(chip->mutex); \
809 INVALIDATE_CACHED_RANGE(map, adr, len); \
811 spin_lock(chip->mutex); \
816 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
818 unsigned long cmd_addr;
819 struct cfi_private *cfi = map->fldrv_priv;
824 /* Ensure cmd read/writes are aligned. */
825 cmd_addr = adr & ~(map_bankwidth(map)-1);
827 spin_lock(chip->mutex);
828 ret = get_chip(map, chip, cmd_addr, FL_READY);
830 spin_unlock(chip->mutex);
834 if (chip->state != FL_POINT && chip->state != FL_READY) {
835 map_write(map, CMD(0xf0), cmd_addr);
836 chip->state = FL_READY;
839 map_copy_from(map, buf, adr, len);
841 put_chip(map, chip, cmd_addr);
843 spin_unlock(chip->mutex);
848 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
850 struct map_info *map = mtd->priv;
851 struct cfi_private *cfi = map->fldrv_priv;
856 /* ofs: offset within the first chip that the first read should start */
858 chipnum = (from >> cfi->chipshift);
859 ofs = from - (chipnum << cfi->chipshift);
865 unsigned long thislen;
867 if (chipnum >= cfi->numchips)
870 if ((len + ofs -1) >> cfi->chipshift)
871 thislen = (1<<cfi->chipshift) - ofs;
875 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
890 static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
892 DECLARE_WAITQUEUE(wait, current);
893 unsigned long timeo = jiffies + HZ;
894 struct cfi_private *cfi = map->fldrv_priv;
897 spin_lock(chip->mutex);
899 if (chip->state != FL_READY){
901 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
903 set_current_state(TASK_UNINTERRUPTIBLE);
904 add_wait_queue(&chip->wq, &wait);
906 spin_unlock(chip->mutex);
909 remove_wait_queue(&chip->wq, &wait);
911 if(signal_pending(current))
914 timeo = jiffies + HZ;
921 chip->state = FL_READY;
923 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
924 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
925 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
927 map_copy_from(map, buf, adr, len);
929 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
930 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
931 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
932 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
935 spin_unlock(chip->mutex);
940 static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
942 struct map_info *map = mtd->priv;
943 struct cfi_private *cfi = map->fldrv_priv;
949 /* ofs: offset within the first chip that the first read should start */
951 /* 8 secsi bytes per chip */
959 unsigned long thislen;
961 if (chipnum >= cfi->numchips)
964 if ((len + ofs -1) >> 3)
965 thislen = (1<<3) - ofs;
969 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
984 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
986 struct cfi_private *cfi = map->fldrv_priv;
987 unsigned long timeo = jiffies + HZ;
989 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
990 * have a max write time of a few hundreds usec). However, we should
991 * use the maximum timeout value given by the chip at probe time
992 * instead. Unfortunately, struct flchip does have a field for
993 * maximum timeout, only for typical which can be far too short
994 * depending of the conditions. The ' + 1' is to avoid having a
995 * timeout of 0 jiffies if HZ is smaller than 1000.
997 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1004 spin_lock(chip->mutex);
1005 ret = get_chip(map, chip, adr, FL_WRITING);
1007 spin_unlock(chip->mutex);
1011 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1012 __func__, adr, datum.x[0] );
1015 * Check for a NOP for the case when the datum to write is already
1016 * present - it saves time and works around buggy chips that corrupt
1017 * data at other locations when 0xff is written to a location that
1018 * already contains 0xff.
1020 oldd = map_read(map, adr);
1021 if (map_word_equal(map, oldd, datum)) {
1022 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1027 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1029 xip_disable(map, chip, adr);
1031 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1032 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1033 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1034 map_write(map, datum, adr);
1035 chip->state = FL_WRITING;
1037 INVALIDATE_CACHE_UDELAY(map, chip,
1038 adr, map_bankwidth(map),
1039 chip->word_write_time);
1041 /* See comment above for timeout value. */
1042 timeo = jiffies + uWriteTimeout;
1044 if (chip->state != FL_WRITING) {
1045 /* Someone's suspended the write. Sleep */
1046 DECLARE_WAITQUEUE(wait, current);
1048 set_current_state(TASK_UNINTERRUPTIBLE);
1049 add_wait_queue(&chip->wq, &wait);
1050 spin_unlock(chip->mutex);
1052 remove_wait_queue(&chip->wq, &wait);
1053 timeo = jiffies + (HZ / 2); /* FIXME */
1054 spin_lock(chip->mutex);
1058 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
1059 xip_enable(map, chip, adr);
1060 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
1061 xip_disable(map, chip, adr);
1065 if (chip_ready(map, adr))
1068 /* Latency issues. Drop the lock, wait a while and retry */
1069 UDELAY(map, chip, adr, 1);
1071 /* Did we succeed? */
1072 if (!chip_good(map, adr, datum)) {
1073 /* reset on all failures. */
1074 map_write( map, CMD(0xF0), chip->start );
1075 /* FIXME - should have reset delay before continuing */
1077 if (++retry_cnt <= MAX_WORD_RETRIES)
1082 xip_enable(map, chip, adr);
1084 chip->state = FL_READY;
1085 put_chip(map, chip, adr);
1086 spin_unlock(chip->mutex);
1092 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1093 size_t *retlen, const u_char *buf)
1095 struct map_info *map = mtd->priv;
1096 struct cfi_private *cfi = map->fldrv_priv;
1099 unsigned long ofs, chipstart;
1100 DECLARE_WAITQUEUE(wait, current);
1106 chipnum = to >> cfi->chipshift;
1107 ofs = to - (chipnum << cfi->chipshift);
1108 chipstart = cfi->chips[chipnum].start;
1110 /* If it's not bus-aligned, do the first byte write */
1111 if (ofs & (map_bankwidth(map)-1)) {
1112 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1113 int i = ofs - bus_ofs;
1118 spin_lock(cfi->chips[chipnum].mutex);
1120 if (cfi->chips[chipnum].state != FL_READY) {
1122 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1124 set_current_state(TASK_UNINTERRUPTIBLE);
1125 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1127 spin_unlock(cfi->chips[chipnum].mutex);
1130 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1132 if(signal_pending(current))
1138 /* Load 'tmp_buf' with old contents of flash */
1139 tmp_buf = map_read(map, bus_ofs+chipstart);
1141 spin_unlock(cfi->chips[chipnum].mutex);
1143 /* Number of bytes to copy from buffer */
1144 n = min_t(int, len, map_bankwidth(map)-i);
1146 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1148 ret = do_write_oneword(map, &cfi->chips[chipnum],
1158 if (ofs >> cfi->chipshift) {
1161 if (chipnum == cfi->numchips)
1166 /* We are now aligned, write as much as possible */
1167 while(len >= map_bankwidth(map)) {
1170 datum = map_word_load(map, buf);
1172 ret = do_write_oneword(map, &cfi->chips[chipnum],
1177 ofs += map_bankwidth(map);
1178 buf += map_bankwidth(map);
1179 (*retlen) += map_bankwidth(map);
1180 len -= map_bankwidth(map);
1182 if (ofs >> cfi->chipshift) {
1185 if (chipnum == cfi->numchips)
1187 chipstart = cfi->chips[chipnum].start;
1191 /* Write the trailing bytes if any */
1192 if (len & (map_bankwidth(map)-1)) {
1196 spin_lock(cfi->chips[chipnum].mutex);
1198 if (cfi->chips[chipnum].state != FL_READY) {
1200 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1202 set_current_state(TASK_UNINTERRUPTIBLE);
1203 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1205 spin_unlock(cfi->chips[chipnum].mutex);
1208 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1210 if(signal_pending(current))
1216 tmp_buf = map_read(map, ofs + chipstart);
1218 spin_unlock(cfi->chips[chipnum].mutex);
1220 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1222 ret = do_write_oneword(map, &cfi->chips[chipnum],
1235 * FIXME: interleaved mode not tested, and probably not supported!
1237 static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
1238 unsigned long adr, const u_char *buf,
1241 struct cfi_private *cfi = map->fldrv_priv;
1242 unsigned long timeo = jiffies + HZ;
1243 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1244 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1246 unsigned long cmd_adr;
1253 spin_lock(chip->mutex);
1254 ret = get_chip(map, chip, adr, FL_WRITING);
1256 spin_unlock(chip->mutex);
1260 datum = map_word_load(map, buf);
1262 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1263 __func__, adr, datum.x[0] );
1265 XIP_INVAL_CACHED_RANGE(map, adr, len);
1267 xip_disable(map, chip, cmd_adr);
1269 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1270 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1271 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1273 /* Write Buffer Load */
1274 map_write(map, CMD(0x25), cmd_adr);
1276 chip->state = FL_WRITING_TO_BUFFER;
1278 /* Write length of data to come */
1279 words = len / map_bankwidth(map);
1280 map_write(map, CMD(words - 1), cmd_adr);
1283 while(z < words * map_bankwidth(map)) {
1284 datum = map_word_load(map, buf);
1285 map_write(map, datum, adr + z);
1287 z += map_bankwidth(map);
1288 buf += map_bankwidth(map);
1290 z -= map_bankwidth(map);
1294 /* Write Buffer Program Confirm: GO GO GO */
1295 map_write(map, CMD(0x29), cmd_adr);
1296 chip->state = FL_WRITING;
1298 INVALIDATE_CACHE_UDELAY(map, chip,
1299 adr, map_bankwidth(map),
1300 chip->word_write_time);
1302 timeo = jiffies + uWriteTimeout;
1305 if (chip->state != FL_WRITING) {
1306 /* Someone's suspended the write. Sleep */
1307 DECLARE_WAITQUEUE(wait, current);
1309 set_current_state(TASK_UNINTERRUPTIBLE);
1310 add_wait_queue(&chip->wq, &wait);
1311 spin_unlock(chip->mutex);
1313 remove_wait_queue(&chip->wq, &wait);
1314 timeo = jiffies + (HZ / 2); /* FIXME */
1315 spin_lock(chip->mutex);
1319 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1322 if (chip_ready(map, adr)) {
1323 xip_enable(map, chip, adr);
1327 /* Latency issues. Drop the lock, wait a while and retry */
1328 UDELAY(map, chip, adr, 1);
1331 /* reset on all failures. */
1332 map_write( map, CMD(0xF0), chip->start );
1333 xip_enable(map, chip, adr);
1334 /* FIXME - should have reset delay before continuing */
1336 printk(KERN_WARNING "MTD %s(): software timeout\n",
1341 chip->state = FL_READY;
1342 put_chip(map, chip, adr);
1343 spin_unlock(chip->mutex);
1349 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1350 size_t *retlen, const u_char *buf)
1352 struct map_info *map = mtd->priv;
1353 struct cfi_private *cfi = map->fldrv_priv;
1354 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1363 chipnum = to >> cfi->chipshift;
1364 ofs = to - (chipnum << cfi->chipshift);
1366 /* If it's not bus-aligned, do the first word write */
1367 if (ofs & (map_bankwidth(map)-1)) {
1368 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1369 if (local_len > len)
1371 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1372 local_len, retlen, buf);
1379 if (ofs >> cfi->chipshift) {
1382 if (chipnum == cfi->numchips)
1387 /* Write buffer is worth it only if more than one word to write... */
1388 while (len >= map_bankwidth(map) * 2) {
1389 /* We must not cross write block boundaries */
1390 int size = wbufsize - (ofs & (wbufsize-1));
1394 if (size % map_bankwidth(map))
1395 size -= size % map_bankwidth(map);
1397 ret = do_write_buffer(map, &cfi->chips[chipnum],
1407 if (ofs >> cfi->chipshift) {
1410 if (chipnum == cfi->numchips)
1416 size_t retlen_dregs = 0;
1418 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1419 len, &retlen_dregs, buf);
1421 *retlen += retlen_dregs;
1430 * Handle devices with one erase region, that only implement
1431 * the chip erase command.
1433 static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
1435 struct cfi_private *cfi = map->fldrv_priv;
1436 unsigned long timeo = jiffies + HZ;
1437 unsigned long int adr;
1438 DECLARE_WAITQUEUE(wait, current);
1441 adr = cfi->addr_unlock1;
1443 spin_lock(chip->mutex);
1444 ret = get_chip(map, chip, adr, FL_WRITING);
1446 spin_unlock(chip->mutex);
1450 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1451 __func__, chip->start );
1453 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
1455 xip_disable(map, chip, adr);
1457 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1458 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1459 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1460 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1461 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1462 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1464 chip->state = FL_ERASING;
1465 chip->erase_suspended = 0;
1466 chip->in_progress_block_addr = adr;
1468 INVALIDATE_CACHE_UDELAY(map, chip,
1470 chip->erase_time*500);
1472 timeo = jiffies + (HZ*20);
1475 if (chip->state != FL_ERASING) {
1476 /* Someone's suspended the erase. Sleep */
1477 set_current_state(TASK_UNINTERRUPTIBLE);
1478 add_wait_queue(&chip->wq, &wait);
1479 spin_unlock(chip->mutex);
1481 remove_wait_queue(&chip->wq, &wait);
1482 spin_lock(chip->mutex);
1485 if (chip->erase_suspended) {
1486 /* This erase was suspended and resumed.
1487 Adjust the timeout */
1488 timeo = jiffies + (HZ*20); /* FIXME */
1489 chip->erase_suspended = 0;
1492 if (chip_ready(map, adr))
1495 if (time_after(jiffies, timeo)) {
1496 printk(KERN_WARNING "MTD %s(): software timeout\n",
1501 /* Latency issues. Drop the lock, wait a while and retry */
1502 UDELAY(map, chip, adr, 1000000/HZ);
1504 /* Did we succeed? */
1505 if (!chip_good(map, adr, map_word_ff(map))) {
1506 /* reset on all failures. */
1507 map_write( map, CMD(0xF0), chip->start );
1508 /* FIXME - should have reset delay before continuing */
1513 chip->state = FL_READY;
1514 xip_enable(map, chip, adr);
1515 put_chip(map, chip, adr);
1516 spin_unlock(chip->mutex);
1522 static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
1524 struct cfi_private *cfi = map->fldrv_priv;
1525 unsigned long timeo = jiffies + HZ;
1526 DECLARE_WAITQUEUE(wait, current);
1531 spin_lock(chip->mutex);
1532 ret = get_chip(map, chip, adr, FL_ERASING);
1534 spin_unlock(chip->mutex);
1538 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1541 XIP_INVAL_CACHED_RANGE(map, adr, len);
1543 xip_disable(map, chip, adr);
1545 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1546 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1547 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1548 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1549 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1550 map_write(map, CMD(0x30), adr);
1552 chip->state = FL_ERASING;
1553 chip->erase_suspended = 0;
1554 chip->in_progress_block_addr = adr;
1556 INVALIDATE_CACHE_UDELAY(map, chip,
1558 chip->erase_time*500);
1560 timeo = jiffies + (HZ*20);
1563 if (chip->state != FL_ERASING) {
1564 /* Someone's suspended the erase. Sleep */
1565 set_current_state(TASK_UNINTERRUPTIBLE);
1566 add_wait_queue(&chip->wq, &wait);
1567 spin_unlock(chip->mutex);
1569 remove_wait_queue(&chip->wq, &wait);
1570 spin_lock(chip->mutex);
1573 if (chip->erase_suspended) {
1574 /* This erase was suspended and resumed.
1575 Adjust the timeout */
1576 timeo = jiffies + (HZ*20); /* FIXME */
1577 chip->erase_suspended = 0;
1580 if (chip_ready(map, adr)) {
1581 xip_enable(map, chip, adr);
1585 if (time_after(jiffies, timeo)) {
1586 xip_enable(map, chip, adr);
1587 printk(KERN_WARNING "MTD %s(): software timeout\n",
1592 /* Latency issues. Drop the lock, wait a while and retry */
1593 UDELAY(map, chip, adr, 1000000/HZ);
1595 /* Did we succeed? */
1596 if (!chip_good(map, adr, map_word_ff(map))) {
1597 /* reset on all failures. */
1598 map_write( map, CMD(0xF0), chip->start );
1599 /* FIXME - should have reset delay before continuing */
1604 chip->state = FL_READY;
1605 put_chip(map, chip, adr);
1606 spin_unlock(chip->mutex);
1611 int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1613 unsigned long ofs, len;
1619 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1623 instr->state = MTD_ERASE_DONE;
1624 mtd_erase_callback(instr);
1630 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1632 struct map_info *map = mtd->priv;
1633 struct cfi_private *cfi = map->fldrv_priv;
1636 if (instr->addr != 0)
1639 if (instr->len != mtd->size)
1642 ret = do_erase_chip(map, &cfi->chips[0]);
1646 instr->state = MTD_ERASE_DONE;
1647 mtd_erase_callback(instr);
1652 static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1653 unsigned long adr, int len, void *thunk)
1655 struct cfi_private *cfi = map->fldrv_priv;
1658 spin_lock(chip->mutex);
1659 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1662 chip->state = FL_LOCKING;
1664 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1665 __func__, adr, len);
1667 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1668 cfi->device_type, NULL);
1669 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1670 cfi->device_type, NULL);
1671 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1672 cfi->device_type, NULL);
1673 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1674 cfi->device_type, NULL);
1675 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1676 cfi->device_type, NULL);
1677 map_write(map, CMD(0x40), chip->start + adr);
1679 chip->state = FL_READY;
1680 put_chip(map, chip, adr + chip->start);
1684 spin_unlock(chip->mutex);
1688 static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1689 unsigned long adr, int len, void *thunk)
1691 struct cfi_private *cfi = map->fldrv_priv;
1694 spin_lock(chip->mutex);
1695 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1698 chip->state = FL_UNLOCKING;
1700 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1701 __func__, adr, len);
1703 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1704 cfi->device_type, NULL);
1705 map_write(map, CMD(0x70), adr);
1707 chip->state = FL_READY;
1708 put_chip(map, chip, adr + chip->start);
1712 spin_unlock(chip->mutex);
1716 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1718 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1721 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1723 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1727 static void cfi_amdstd_sync (struct mtd_info *mtd)
1729 struct map_info *map = mtd->priv;
1730 struct cfi_private *cfi = map->fldrv_priv;
1732 struct flchip *chip;
1734 DECLARE_WAITQUEUE(wait, current);
1736 for (i=0; !ret && i<cfi->numchips; i++) {
1737 chip = &cfi->chips[i];
1740 spin_lock(chip->mutex);
1742 switch(chip->state) {
1746 case FL_JEDEC_QUERY:
1747 chip->oldstate = chip->state;
1748 chip->state = FL_SYNCING;
1749 /* No need to wake_up() on this state change -
1750 * as the whole point is that nobody can do anything
1751 * with the chip now anyway.
1754 spin_unlock(chip->mutex);
1758 /* Not an idle state */
1759 add_wait_queue(&chip->wq, &wait);
1761 spin_unlock(chip->mutex);
1765 remove_wait_queue(&chip->wq, &wait);
1771 /* Unlock the chips again */
1773 for (i--; i >=0; i--) {
1774 chip = &cfi->chips[i];
1776 spin_lock(chip->mutex);
1778 if (chip->state == FL_SYNCING) {
1779 chip->state = chip->oldstate;
1782 spin_unlock(chip->mutex);
1787 static int cfi_amdstd_suspend(struct mtd_info *mtd)
1789 struct map_info *map = mtd->priv;
1790 struct cfi_private *cfi = map->fldrv_priv;
1792 struct flchip *chip;
1795 for (i=0; !ret && i<cfi->numchips; i++) {
1796 chip = &cfi->chips[i];
1798 spin_lock(chip->mutex);
1800 switch(chip->state) {
1804 case FL_JEDEC_QUERY:
1805 chip->oldstate = chip->state;
1806 chip->state = FL_PM_SUSPENDED;
1807 /* No need to wake_up() on this state change -
1808 * as the whole point is that nobody can do anything
1809 * with the chip now anyway.
1811 case FL_PM_SUSPENDED:
1818 spin_unlock(chip->mutex);
1821 /* Unlock the chips again */
1824 for (i--; i >=0; i--) {
1825 chip = &cfi->chips[i];
1827 spin_lock(chip->mutex);
1829 if (chip->state == FL_PM_SUSPENDED) {
1830 chip->state = chip->oldstate;
1833 spin_unlock(chip->mutex);
1841 static void cfi_amdstd_resume(struct mtd_info *mtd)
1843 struct map_info *map = mtd->priv;
1844 struct cfi_private *cfi = map->fldrv_priv;
1846 struct flchip *chip;
1848 for (i=0; i<cfi->numchips; i++) {
1850 chip = &cfi->chips[i];
1852 spin_lock(chip->mutex);
1854 if (chip->state == FL_PM_SUSPENDED) {
1855 chip->state = FL_READY;
1856 map_write(map, CMD(0xF0), chip->start);
1860 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1862 spin_unlock(chip->mutex);
1866 static void cfi_amdstd_destroy(struct mtd_info *mtd)
1868 struct map_info *map = mtd->priv;
1869 struct cfi_private *cfi = map->fldrv_priv;
1871 kfree(cfi->cmdset_priv);
1874 kfree(mtd->eraseregions);
1877 MODULE_LICENSE("GPL");
1878 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1879 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");