]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/mtd/chips/cfi_cmdset_0002.c
ea2a7f66ddf92052d4e61e17fbe027e98a75f7e6
[linux-2.6.git] / drivers / mtd / chips / cfi_cmdset_0002.c
1 /*
2  * Common Flash Interface support:
3  *   AMD & Fujitsu Standard Vendor Command Set (ID 0x0002)
4  *
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>
8  *
9  * 2_by_8 routines added by Simon Munton
10  *
11  * 4_by_16 work by Carolyn J. Smith
12  *
13  * XIP support hooks by Vitaly Wool (based on code for Intel flash
14  * by Nicolas Pitre)
15  *
16  * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
17  *
18  * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
19  *
20  * This code is GPL
21  */
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/init.h>
28 #include <asm/io.h>
29 #include <asm/byteorder.h>
30
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/interrupt.h>
35 #include <linux/mtd/compatmac.h>
36 #include <linux/mtd/map.h>
37 #include <linux/mtd/mtd.h>
38 #include <linux/mtd/cfi.h>
39 #include <linux/mtd/xip.h>
40
41 #define AMD_BOOTLOC_BUG
42 #define FORCE_WORD_WRITE 0
43
44 #define MAX_WORD_RETRIES 3
45
46 #define SST49LF004B             0x0060
47 #define SST49LF040B             0x0050
48 #define SST49LF008A             0x005a
49 #define AT49BV6416              0x00d6
50
51 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
52 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
53 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54 static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
55 static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
56 static void cfi_amdstd_sync (struct mtd_info *);
57 static int cfi_amdstd_suspend (struct mtd_info *);
58 static void cfi_amdstd_resume (struct mtd_info *);
59 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
60
61 static void cfi_amdstd_destroy(struct mtd_info *);
62
63 struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
64 static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
65
66 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
67 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
68 #include "fwh_lock.h"
69
70 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
71 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
72
73 static struct mtd_chip_driver cfi_amdstd_chipdrv = {
74         .probe          = NULL, /* Not usable directly */
75         .destroy        = cfi_amdstd_destroy,
76         .name           = "cfi_cmdset_0002",
77         .module         = THIS_MODULE
78 };
79
80
81 /* #define DEBUG_CFI_FEATURES */
82
83
84 #ifdef DEBUG_CFI_FEATURES
85 static void cfi_tell_features(struct cfi_pri_amdstd *extp)
86 {
87         const char* erase_suspend[3] = {
88                 "Not supported", "Read only", "Read/write"
89         };
90         const char* top_bottom[6] = {
91                 "No WP", "8x8KiB sectors at top & bottom, no WP",
92                 "Bottom boot", "Top boot",
93                 "Uniform, Bottom WP", "Uniform, Top WP"
94         };
95
96         printk("  Silicon revision: %d\n", extp->SiliconRevision >> 1);
97         printk("  Address sensitive unlock: %s\n",
98                (extp->SiliconRevision & 1) ? "Not required" : "Required");
99
100         if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
101                 printk("  Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
102         else
103                 printk("  Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
104
105         if (extp->BlkProt == 0)
106                 printk("  Block protection: Not supported\n");
107         else
108                 printk("  Block protection: %d sectors per group\n", extp->BlkProt);
109
110
111         printk("  Temporary block unprotect: %s\n",
112                extp->TmpBlkUnprotect ? "Supported" : "Not supported");
113         printk("  Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
114         printk("  Number of simultaneous operations: %d\n", extp->SimultaneousOps);
115         printk("  Burst mode: %s\n",
116                extp->BurstMode ? "Supported" : "Not supported");
117         if (extp->PageMode == 0)
118                 printk("  Page mode: Not supported\n");
119         else
120                 printk("  Page mode: %d word page\n", extp->PageMode << 2);
121
122         printk("  Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
123                extp->VppMin >> 4, extp->VppMin & 0xf);
124         printk("  Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
125                extp->VppMax >> 4, extp->VppMax & 0xf);
126
127         if (extp->TopBottom < ARRAY_SIZE(top_bottom))
128                 printk("  Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
129         else
130                 printk("  Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
131 }
132 #endif
133
134 #ifdef AMD_BOOTLOC_BUG
135 /* Wheee. Bring me the head of someone at AMD. */
136 static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
137 {
138         struct map_info *map = mtd->priv;
139         struct cfi_private *cfi = map->fldrv_priv;
140         struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
141         __u8 major = extp->MajorVersion;
142         __u8 minor = extp->MinorVersion;
143
144         if (((major << 8) | minor) < 0x3131) {
145                 /* CFI version 1.0 => don't trust bootloc */
146
147                 DEBUG(MTD_DEBUG_LEVEL1,
148                         "%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
149                         map->name, cfi->mfr, cfi->id);
150
151                 /* AFAICS all 29LV400 with a bottom boot block have a device ID
152                  * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
153                  * These were badly detected as they have the 0x80 bit set
154                  * so treat them as a special case.
155                  */
156                 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
157
158                         /* Macronix added CFI to their 2nd generation
159                          * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
160                          * Fujitsu, Spansion, EON, ESI and older Macronix)
161                          * has CFI.
162                          *
163                          * Therefore also check the manufacturer.
164                          * This reduces the risk of false detection due to
165                          * the 8-bit device ID.
166                          */
167                         (cfi->mfr == CFI_MFR_MACRONIX)) {
168                         DEBUG(MTD_DEBUG_LEVEL1,
169                                 "%s: Macronix MX29LV400C with bottom boot block"
170                                 " detected\n", map->name);
171                         extp->TopBottom = 2;    /* bottom boot */
172                 } else
173                 if (cfi->id & 0x80) {
174                         printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
175                         extp->TopBottom = 3;    /* top boot */
176                 } else {
177                         extp->TopBottom = 2;    /* bottom boot */
178                 }
179
180                 DEBUG(MTD_DEBUG_LEVEL1,
181                         "%s: AMD CFI PRI V%c.%c has no boot block field;"
182                         " deduced %s from Device ID\n", map->name, major, minor,
183                         extp->TopBottom == 2 ? "bottom" : "top");
184         }
185 }
186 #endif
187
188 static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
189 {
190         struct map_info *map = mtd->priv;
191         struct cfi_private *cfi = map->fldrv_priv;
192         if (cfi->cfiq->BufWriteTimeoutTyp) {
193                 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
194                 mtd->write = cfi_amdstd_write_buffers;
195         }
196 }
197
198 /* Atmel chips don't use the same PRI format as AMD chips */
199 static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
200 {
201         struct map_info *map = mtd->priv;
202         struct cfi_private *cfi = map->fldrv_priv;
203         struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
204         struct cfi_pri_atmel atmel_pri;
205
206         memcpy(&atmel_pri, extp, sizeof(atmel_pri));
207         memset((char *)extp + 5, 0, sizeof(*extp) - 5);
208
209         if (atmel_pri.Features & 0x02)
210                 extp->EraseSuspend = 2;
211
212         /* Some chips got it backwards... */
213         if (cfi->id == AT49BV6416) {
214                 if (atmel_pri.BottomBoot)
215                         extp->TopBottom = 3;
216                 else
217                         extp->TopBottom = 2;
218         } else {
219                 if (atmel_pri.BottomBoot)
220                         extp->TopBottom = 2;
221                 else
222                         extp->TopBottom = 3;
223         }
224
225         /* burst write mode not supported */
226         cfi->cfiq->BufWriteTimeoutTyp = 0;
227         cfi->cfiq->BufWriteTimeoutMax = 0;
228 }
229
230 static void fixup_use_secsi(struct mtd_info *mtd, void *param)
231 {
232         /* Setup for chips with a secsi area */
233         mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
234         mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
235 }
236
237 static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
238 {
239         struct map_info *map = mtd->priv;
240         struct cfi_private *cfi = map->fldrv_priv;
241         if ((cfi->cfiq->NumEraseRegions == 1) &&
242                 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
243                 mtd->erase = cfi_amdstd_erase_chip;
244         }
245
246 }
247
248 /*
249  * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
250  * locked by default.
251  */
252 static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
253 {
254         mtd->lock = cfi_atmel_lock;
255         mtd->unlock = cfi_atmel_unlock;
256         mtd->flags |= MTD_POWERUP_LOCK;
257 }
258
259 static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
260 {
261         struct map_info *map = mtd->priv;
262         struct cfi_private *cfi = map->fldrv_priv;
263
264         if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
265                 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
266                 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
267         }
268 }
269
270 static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
271 {
272         struct map_info *map = mtd->priv;
273         struct cfi_private *cfi = map->fldrv_priv;
274
275         if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
276                 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
277                 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
278         }
279 }
280
281 static struct cfi_fixup cfi_fixup_table[] = {
282         { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
283 #ifdef AMD_BOOTLOC_BUG
284         { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
285         { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
286 #endif
287         { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
288         { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
289         { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
290         { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
291         { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
292         { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
293         { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, },
294         { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, },
295         { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, },
296         { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, },
297 #if !FORCE_WORD_WRITE
298         { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
299 #endif
300         { 0, 0, NULL, NULL }
301 };
302 static struct cfi_fixup jedec_fixup_table[] = {
303         { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
304         { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
305         { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
306         { 0, 0, NULL, NULL }
307 };
308
309 static struct cfi_fixup fixup_table[] = {
310         /* The CFI vendor ids and the JEDEC vendor IDs appear
311          * to be common.  It is like the devices id's are as
312          * well.  This table is to pick all cases where
313          * we know that is the case.
314          */
315         { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
316         { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
317         { 0, 0, NULL, NULL }
318 };
319
320
321 static void cfi_fixup_major_minor(struct cfi_private *cfi,
322                                   struct cfi_pri_amdstd *extp)
323 {
324         if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
325             extp->MajorVersion == '0')
326                 extp->MajorVersion = '1';
327 }
328
329 struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
330 {
331         struct cfi_private *cfi = map->fldrv_priv;
332         struct mtd_info *mtd;
333         int i;
334
335         mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
336         if (!mtd) {
337                 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
338                 return NULL;
339         }
340         mtd->priv = map;
341         mtd->type = MTD_NORFLASH;
342
343         /* Fill in the default mtd operations */
344         mtd->erase   = cfi_amdstd_erase_varsize;
345         mtd->write   = cfi_amdstd_write_words;
346         mtd->read    = cfi_amdstd_read;
347         mtd->sync    = cfi_amdstd_sync;
348         mtd->suspend = cfi_amdstd_suspend;
349         mtd->resume  = cfi_amdstd_resume;
350         mtd->flags   = MTD_CAP_NORFLASH;
351         mtd->name    = map->name;
352         mtd->writesize = 1;
353
354         if (cfi->cfi_mode==CFI_MODE_CFI){
355                 unsigned char bootloc;
356                 /*
357                  * It's a real CFI chip, not one for which the probe
358                  * routine faked a CFI structure. So we read the feature
359                  * table from it.
360                  */
361                 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
362                 struct cfi_pri_amdstd *extp;
363
364                 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
365                 if (!extp) {
366                         kfree(mtd);
367                         return NULL;
368                 }
369
370                 cfi_fixup_major_minor(cfi, extp);
371
372                 if (extp->MajorVersion != '1' ||
373                     (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
374                         printk(KERN_ERR "  Unknown Amd/Fujitsu Extended Query "
375                                "version %c.%c.\n",  extp->MajorVersion,
376                                extp->MinorVersion);
377                         kfree(extp);
378                         kfree(mtd);
379                         return NULL;
380                 }
381
382                 /* Install our own private info structure */
383                 cfi->cmdset_priv = extp;
384
385                 /* Apply cfi device specific fixups */
386                 cfi_fixup(mtd, cfi_fixup_table);
387
388 #ifdef DEBUG_CFI_FEATURES
389                 /* Tell the user about it in lots of lovely detail */
390                 cfi_tell_features(extp);
391 #endif
392
393                 bootloc = extp->TopBottom;
394                 if ((bootloc != 2) && (bootloc != 3)) {
395                         printk(KERN_WARNING "%s: CFI does not contain boot "
396                                "bank location. Assuming top.\n", map->name);
397                         bootloc = 2;
398                 }
399
400                 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
401                         printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
402
403                         for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
404                                 int j = (cfi->cfiq->NumEraseRegions-1)-i;
405                                 __u32 swap;
406
407                                 swap = cfi->cfiq->EraseRegionInfo[i];
408                                 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
409                                 cfi->cfiq->EraseRegionInfo[j] = swap;
410                         }
411                 }
412                 /* Set the default CFI lock/unlock addresses */
413                 cfi->addr_unlock1 = 0x555;
414                 cfi->addr_unlock2 = 0x2aa;
415
416         } /* CFI mode */
417         else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
418                 /* Apply jedec specific fixups */
419                 cfi_fixup(mtd, jedec_fixup_table);
420         }
421         /* Apply generic fixups */
422         cfi_fixup(mtd, fixup_table);
423
424         for (i=0; i< cfi->numchips; i++) {
425                 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
426                 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
427                 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
428                 cfi->chips[i].ref_point_counter = 0;
429                 init_waitqueue_head(&(cfi->chips[i].wq));
430         }
431
432         map->fldrv = &cfi_amdstd_chipdrv;
433
434         return cfi_amdstd_setup(mtd);
435 }
436 EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
437
438 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
439 {
440         struct map_info *map = mtd->priv;
441         struct cfi_private *cfi = map->fldrv_priv;
442         unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
443         unsigned long offset = 0;
444         int i,j;
445
446         printk(KERN_NOTICE "number of %s chips: %d\n",
447                (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
448         /* Select the correct geometry setup */
449         mtd->size = devsize * cfi->numchips;
450
451         mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
452         mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
453                                     * mtd->numeraseregions, GFP_KERNEL);
454         if (!mtd->eraseregions) {
455                 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
456                 goto setup_err;
457         }
458
459         for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
460                 unsigned long ernum, ersize;
461                 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
462                 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
463
464                 if (mtd->erasesize < ersize) {
465                         mtd->erasesize = ersize;
466                 }
467                 for (j=0; j<cfi->numchips; j++) {
468                         mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
469                         mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
470                         mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
471                 }
472                 offset += (ersize * ernum);
473         }
474         if (offset != devsize) {
475                 /* Argh */
476                 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
477                 goto setup_err;
478         }
479 #if 0
480         // debug
481         for (i=0; i<mtd->numeraseregions;i++){
482                 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
483                        i,mtd->eraseregions[i].offset,
484                        mtd->eraseregions[i].erasesize,
485                        mtd->eraseregions[i].numblocks);
486         }
487 #endif
488
489         __module_get(THIS_MODULE);
490         return mtd;
491
492  setup_err:
493         kfree(mtd->eraseregions);
494         kfree(mtd);
495         kfree(cfi->cmdset_priv);
496         kfree(cfi->cfiq);
497         return NULL;
498 }
499
500 /*
501  * Return true if the chip is ready.
502  *
503  * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
504  * non-suspended sector) and is indicated by no toggle bits toggling.
505  *
506  * Note that anything more complicated than checking if no bits are toggling
507  * (including checking DQ5 for an error status) is tricky to get working
508  * correctly and is therefore not done  (particulary with interleaved chips
509  * as each chip must be checked independantly of the others).
510  */
511 static int __xipram chip_ready(struct map_info *map, unsigned long addr)
512 {
513         map_word d, t;
514
515         d = map_read(map, addr);
516         t = map_read(map, addr);
517
518         return map_word_equal(map, d, t);
519 }
520
521 /*
522  * Return true if the chip is ready and has the correct value.
523  *
524  * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
525  * non-suspended sector) and it is indicated by no bits toggling.
526  *
527  * Error are indicated by toggling bits or bits held with the wrong value,
528  * or with bits toggling.
529  *
530  * Note that anything more complicated than checking if no bits are toggling
531  * (including checking DQ5 for an error status) is tricky to get working
532  * correctly and is therefore not done  (particulary with interleaved chips
533  * as each chip must be checked independantly of the others).
534  *
535  */
536 static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
537 {
538         map_word oldd, curd;
539
540         oldd = map_read(map, addr);
541         curd = map_read(map, addr);
542
543         return  map_word_equal(map, oldd, curd) &&
544                 map_word_equal(map, curd, expected);
545 }
546
547 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
548 {
549         DECLARE_WAITQUEUE(wait, current);
550         struct cfi_private *cfi = map->fldrv_priv;
551         unsigned long timeo;
552         struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
553
554  resettime:
555         timeo = jiffies + HZ;
556  retry:
557         switch (chip->state) {
558
559         case FL_STATUS:
560                 for (;;) {
561                         if (chip_ready(map, adr))
562                                 break;
563
564                         if (time_after(jiffies, timeo)) {
565                                 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
566                                 return -EIO;
567                         }
568                         spin_unlock(chip->mutex);
569                         cfi_udelay(1);
570                         spin_lock(chip->mutex);
571                         /* Someone else might have been playing with it. */
572                         goto retry;
573                 }
574
575         case FL_READY:
576         case FL_CFI_QUERY:
577         case FL_JEDEC_QUERY:
578                 return 0;
579
580         case FL_ERASING:
581                 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
582                     !(mode == FL_READY || mode == FL_POINT ||
583                     (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
584                         goto sleep;
585
586                 /* We could check to see if we're trying to access the sector
587                  * that is currently being erased. However, no user will try
588                  * anything like that so we just wait for the timeout. */
589
590                 /* Erase suspend */
591                 /* It's harmless to issue the Erase-Suspend and Erase-Resume
592                  * commands when the erase algorithm isn't in progress. */
593                 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
594                 chip->oldstate = FL_ERASING;
595                 chip->state = FL_ERASE_SUSPENDING;
596                 chip->erase_suspended = 1;
597                 for (;;) {
598                         if (chip_ready(map, adr))
599                                 break;
600
601                         if (time_after(jiffies, timeo)) {
602                                 /* Should have suspended the erase by now.
603                                  * Send an Erase-Resume command as either
604                                  * there was an error (so leave the erase
605                                  * routine to recover from it) or we trying to
606                                  * use the erase-in-progress sector. */
607                                 map_write(map, CMD(0x30), chip->in_progress_block_addr);
608                                 chip->state = FL_ERASING;
609                                 chip->oldstate = FL_READY;
610                                 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
611                                 return -EIO;
612                         }
613
614                         spin_unlock(chip->mutex);
615                         cfi_udelay(1);
616                         spin_lock(chip->mutex);
617                         /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
618                            So we can just loop here. */
619                 }
620                 chip->state = FL_READY;
621                 return 0;
622
623         case FL_XIP_WHILE_ERASING:
624                 if (mode != FL_READY && mode != FL_POINT &&
625                     (!cfip || !(cfip->EraseSuspend&2)))
626                         goto sleep;
627                 chip->oldstate = chip->state;
628                 chip->state = FL_READY;
629                 return 0;
630
631         case FL_POINT:
632                 /* Only if there's no operation suspended... */
633                 if (mode == FL_READY && chip->oldstate == FL_READY)
634                         return 0;
635
636         default:
637         sleep:
638                 set_current_state(TASK_UNINTERRUPTIBLE);
639                 add_wait_queue(&chip->wq, &wait);
640                 spin_unlock(chip->mutex);
641                 schedule();
642                 remove_wait_queue(&chip->wq, &wait);
643                 spin_lock(chip->mutex);
644                 goto resettime;
645         }
646 }
647
648
649 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
650 {
651         struct cfi_private *cfi = map->fldrv_priv;
652
653         switch(chip->oldstate) {
654         case FL_ERASING:
655                 chip->state = chip->oldstate;
656                 map_write(map, CMD(0x30), chip->in_progress_block_addr);
657                 chip->oldstate = FL_READY;
658                 chip->state = FL_ERASING;
659                 break;
660
661         case FL_XIP_WHILE_ERASING:
662                 chip->state = chip->oldstate;
663                 chip->oldstate = FL_READY;
664                 break;
665
666         case FL_READY:
667         case FL_STATUS:
668                 /* We should really make set_vpp() count, rather than doing this */
669                 DISABLE_VPP(map);
670                 break;
671         default:
672                 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
673         }
674         wake_up(&chip->wq);
675 }
676
677 #ifdef CONFIG_MTD_XIP
678
679 /*
680  * No interrupt what so ever can be serviced while the flash isn't in array
681  * mode.  This is ensured by the xip_disable() and xip_enable() functions
682  * enclosing any code path where the flash is known not to be in array mode.
683  * And within a XIP disabled code path, only functions marked with __xipram
684  * may be called and nothing else (it's a good thing to inspect generated
685  * assembly to make sure inline functions were actually inlined and that gcc
686  * didn't emit calls to its own support functions). Also configuring MTD CFI
687  * support to a single buswidth and a single interleave is also recommended.
688  */
689
690 static void xip_disable(struct map_info *map, struct flchip *chip,
691                         unsigned long adr)
692 {
693         /* TODO: chips with no XIP use should ignore and return */
694         (void) map_read(map, adr); /* ensure mmu mapping is up to date */
695         local_irq_disable();
696 }
697
698 static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
699                                 unsigned long adr)
700 {
701         struct cfi_private *cfi = map->fldrv_priv;
702
703         if (chip->state != FL_POINT && chip->state != FL_READY) {
704                 map_write(map, CMD(0xf0), adr);
705                 chip->state = FL_READY;
706         }
707         (void) map_read(map, adr);
708         xip_iprefetch();
709         local_irq_enable();
710 }
711
712 /*
713  * When a delay is required for the flash operation to complete, the
714  * xip_udelay() function is polling for both the given timeout and pending
715  * (but still masked) hardware interrupts.  Whenever there is an interrupt
716  * pending then the flash erase operation is suspended, array mode restored
717  * and interrupts unmasked.  Task scheduling might also happen at that
718  * point.  The CPU eventually returns from the interrupt or the call to
719  * schedule() and the suspended flash operation is resumed for the remaining
720  * of the delay period.
721  *
722  * Warning: this function _will_ fool interrupt latency tracing tools.
723  */
724
725 static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
726                                 unsigned long adr, int usec)
727 {
728         struct cfi_private *cfi = map->fldrv_priv;
729         struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
730         map_word status, OK = CMD(0x80);
731         unsigned long suspended, start = xip_currtime();
732         flstate_t oldstate;
733
734         do {
735                 cpu_relax();
736                 if (xip_irqpending() && extp &&
737                     ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
738                     (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
739                         /*
740                          * Let's suspend the erase operation when supported.
741                          * Note that we currently don't try to suspend
742                          * interleaved chips if there is already another
743                          * operation suspended (imagine what happens
744                          * when one chip was already done with the current
745                          * operation while another chip suspended it, then
746                          * we resume the whole thing at once).  Yes, it
747                          * can happen!
748                          */
749                         map_write(map, CMD(0xb0), adr);
750                         usec -= xip_elapsed_since(start);
751                         suspended = xip_currtime();
752                         do {
753                                 if (xip_elapsed_since(suspended) > 100000) {
754                                         /*
755                                          * The chip doesn't want to suspend
756                                          * after waiting for 100 msecs.
757                                          * This is a critical error but there
758                                          * is not much we can do here.
759                                          */
760                                         return;
761                                 }
762                                 status = map_read(map, adr);
763                         } while (!map_word_andequal(map, status, OK, OK));
764
765                         /* Suspend succeeded */
766                         oldstate = chip->state;
767                         if (!map_word_bitsset(map, status, CMD(0x40)))
768                                 break;
769                         chip->state = FL_XIP_WHILE_ERASING;
770                         chip->erase_suspended = 1;
771                         map_write(map, CMD(0xf0), adr);
772                         (void) map_read(map, adr);
773                         xip_iprefetch();
774                         local_irq_enable();
775                         spin_unlock(chip->mutex);
776                         xip_iprefetch();
777                         cond_resched();
778
779                         /*
780                          * We're back.  However someone else might have
781                          * decided to go write to the chip if we are in
782                          * a suspended erase state.  If so let's wait
783                          * until it's done.
784                          */
785                         spin_lock(chip->mutex);
786                         while (chip->state != FL_XIP_WHILE_ERASING) {
787                                 DECLARE_WAITQUEUE(wait, current);
788                                 set_current_state(TASK_UNINTERRUPTIBLE);
789                                 add_wait_queue(&chip->wq, &wait);
790                                 spin_unlock(chip->mutex);
791                                 schedule();
792                                 remove_wait_queue(&chip->wq, &wait);
793                                 spin_lock(chip->mutex);
794                         }
795                         /* Disallow XIP again */
796                         local_irq_disable();
797
798                         /* Resume the write or erase operation */
799                         map_write(map, CMD(0x30), adr);
800                         chip->state = oldstate;
801                         start = xip_currtime();
802                 } else if (usec >= 1000000/HZ) {
803                         /*
804                          * Try to save on CPU power when waiting delay
805                          * is at least a system timer tick period.
806                          * No need to be extremely accurate here.
807                          */
808                         xip_cpu_idle();
809                 }
810                 status = map_read(map, adr);
811         } while (!map_word_andequal(map, status, OK, OK)
812                  && xip_elapsed_since(start) < usec);
813 }
814
815 #define UDELAY(map, chip, adr, usec)  xip_udelay(map, chip, adr, usec)
816
817 /*
818  * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
819  * the flash is actively programming or erasing since we have to poll for
820  * the operation to complete anyway.  We can't do that in a generic way with
821  * a XIP setup so do it before the actual flash operation in this case
822  * and stub it out from INVALIDATE_CACHE_UDELAY.
823  */
824 #define XIP_INVAL_CACHED_RANGE(map, from, size)  \
825         INVALIDATE_CACHED_RANGE(map, from, size)
826
827 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec)  \
828         UDELAY(map, chip, adr, usec)
829
830 /*
831  * Extra notes:
832  *
833  * Activating this XIP support changes the way the code works a bit.  For
834  * example the code to suspend the current process when concurrent access
835  * happens is never executed because xip_udelay() will always return with the
836  * same chip state as it was entered with.  This is why there is no care for
837  * the presence of add_wait_queue() or schedule() calls from within a couple
838  * xip_disable()'d  areas of code, like in do_erase_oneblock for example.
839  * The queueing and scheduling are always happening within xip_udelay().
840  *
841  * Similarly, get_chip() and put_chip() just happen to always be executed
842  * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
843  * is in array mode, therefore never executing many cases therein and not
844  * causing any problem with XIP.
845  */
846
847 #else
848
849 #define xip_disable(map, chip, adr)
850 #define xip_enable(map, chip, adr)
851 #define XIP_INVAL_CACHED_RANGE(x...)
852
853 #define UDELAY(map, chip, adr, usec)  \
854 do {  \
855         spin_unlock(chip->mutex);  \
856         cfi_udelay(usec);  \
857         spin_lock(chip->mutex);  \
858 } while (0)
859
860 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec)  \
861 do {  \
862         spin_unlock(chip->mutex);  \
863         INVALIDATE_CACHED_RANGE(map, adr, len);  \
864         cfi_udelay(usec);  \
865         spin_lock(chip->mutex);  \
866 } while (0)
867
868 #endif
869
870 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
871 {
872         unsigned long cmd_addr;
873         struct cfi_private *cfi = map->fldrv_priv;
874         int ret;
875
876         adr += chip->start;
877
878         /* Ensure cmd read/writes are aligned. */
879         cmd_addr = adr & ~(map_bankwidth(map)-1);
880
881         spin_lock(chip->mutex);
882         ret = get_chip(map, chip, cmd_addr, FL_READY);
883         if (ret) {
884                 spin_unlock(chip->mutex);
885                 return ret;
886         }
887
888         if (chip->state != FL_POINT && chip->state != FL_READY) {
889                 map_write(map, CMD(0xf0), cmd_addr);
890                 chip->state = FL_READY;
891         }
892
893         map_copy_from(map, buf, adr, len);
894
895         put_chip(map, chip, cmd_addr);
896
897         spin_unlock(chip->mutex);
898         return 0;
899 }
900
901
902 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
903 {
904         struct map_info *map = mtd->priv;
905         struct cfi_private *cfi = map->fldrv_priv;
906         unsigned long ofs;
907         int chipnum;
908         int ret = 0;
909
910         /* ofs: offset within the first chip that the first read should start */
911
912         chipnum = (from >> cfi->chipshift);
913         ofs = from - (chipnum <<  cfi->chipshift);
914
915
916         *retlen = 0;
917
918         while (len) {
919                 unsigned long thislen;
920
921                 if (chipnum >= cfi->numchips)
922                         break;
923
924                 if ((len + ofs -1) >> cfi->chipshift)
925                         thislen = (1<<cfi->chipshift) - ofs;
926                 else
927                         thislen = len;
928
929                 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
930                 if (ret)
931                         break;
932
933                 *retlen += thislen;
934                 len -= thislen;
935                 buf += thislen;
936
937                 ofs = 0;
938                 chipnum++;
939         }
940         return ret;
941 }
942
943
944 static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
945 {
946         DECLARE_WAITQUEUE(wait, current);
947         unsigned long timeo = jiffies + HZ;
948         struct cfi_private *cfi = map->fldrv_priv;
949
950  retry:
951         spin_lock(chip->mutex);
952
953         if (chip->state != FL_READY){
954 #if 0
955                 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
956 #endif
957                 set_current_state(TASK_UNINTERRUPTIBLE);
958                 add_wait_queue(&chip->wq, &wait);
959
960                 spin_unlock(chip->mutex);
961
962                 schedule();
963                 remove_wait_queue(&chip->wq, &wait);
964 #if 0
965                 if(signal_pending(current))
966                         return -EINTR;
967 #endif
968                 timeo = jiffies + HZ;
969
970                 goto retry;
971         }
972
973         adr += chip->start;
974
975         chip->state = FL_READY;
976
977         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
978         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
979         cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
980
981         map_copy_from(map, buf, adr, len);
982
983         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
984         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
985         cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
986         cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
987
988         wake_up(&chip->wq);
989         spin_unlock(chip->mutex);
990
991         return 0;
992 }
993
994 static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
995 {
996         struct map_info *map = mtd->priv;
997         struct cfi_private *cfi = map->fldrv_priv;
998         unsigned long ofs;
999         int chipnum;
1000         int ret = 0;
1001
1002
1003         /* ofs: offset within the first chip that the first read should start */
1004
1005         /* 8 secsi bytes per chip */
1006         chipnum=from>>3;
1007         ofs=from & 7;
1008
1009
1010         *retlen = 0;
1011
1012         while (len) {
1013                 unsigned long thislen;
1014
1015                 if (chipnum >= cfi->numchips)
1016                         break;
1017
1018                 if ((len + ofs -1) >> 3)
1019                         thislen = (1<<3) - ofs;
1020                 else
1021                         thislen = len;
1022
1023                 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1024                 if (ret)
1025                         break;
1026
1027                 *retlen += thislen;
1028                 len -= thislen;
1029                 buf += thislen;
1030
1031                 ofs = 0;
1032                 chipnum++;
1033         }
1034         return ret;
1035 }
1036
1037
1038 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
1039 {
1040         struct cfi_private *cfi = map->fldrv_priv;
1041         unsigned long timeo = jiffies + HZ;
1042         /*
1043          * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1044          * have a max write time of a few hundreds usec). However, we should
1045          * use the maximum timeout value given by the chip at probe time
1046          * instead.  Unfortunately, struct flchip does have a field for
1047          * maximum timeout, only for typical which can be far too short
1048          * depending of the conditions.  The ' + 1' is to avoid having a
1049          * timeout of 0 jiffies if HZ is smaller than 1000.
1050          */
1051         unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1052         int ret = 0;
1053         map_word oldd;
1054         int retry_cnt = 0;
1055
1056         adr += chip->start;
1057
1058         spin_lock(chip->mutex);
1059         ret = get_chip(map, chip, adr, FL_WRITING);
1060         if (ret) {
1061                 spin_unlock(chip->mutex);
1062                 return ret;
1063         }
1064
1065         DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1066                __func__, adr, datum.x[0] );
1067
1068         /*
1069          * Check for a NOP for the case when the datum to write is already
1070          * present - it saves time and works around buggy chips that corrupt
1071          * data at other locations when 0xff is written to a location that
1072          * already contains 0xff.
1073          */
1074         oldd = map_read(map, adr);
1075         if (map_word_equal(map, oldd, datum)) {
1076                 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1077                        __func__);
1078                 goto op_done;
1079         }
1080
1081         XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1082         ENABLE_VPP(map);
1083         xip_disable(map, chip, adr);
1084  retry:
1085         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1086         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1087         cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1088         map_write(map, datum, adr);
1089         chip->state = FL_WRITING;
1090
1091         INVALIDATE_CACHE_UDELAY(map, chip,
1092                                 adr, map_bankwidth(map),
1093                                 chip->word_write_time);
1094
1095         /* See comment above for timeout value. */
1096         timeo = jiffies + uWriteTimeout;
1097         for (;;) {
1098                 if (chip->state != FL_WRITING) {
1099                         /* Someone's suspended the write. Sleep */
1100                         DECLARE_WAITQUEUE(wait, current);
1101
1102                         set_current_state(TASK_UNINTERRUPTIBLE);
1103                         add_wait_queue(&chip->wq, &wait);
1104                         spin_unlock(chip->mutex);
1105                         schedule();
1106                         remove_wait_queue(&chip->wq, &wait);
1107                         timeo = jiffies + (HZ / 2); /* FIXME */
1108                         spin_lock(chip->mutex);
1109                         continue;
1110                 }
1111
1112                 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
1113                         xip_enable(map, chip, adr);
1114                         printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
1115                         xip_disable(map, chip, adr);
1116                         break;
1117                 }
1118
1119                 if (chip_ready(map, adr))
1120                         break;
1121
1122                 /* Latency issues. Drop the lock, wait a while and retry */
1123                 UDELAY(map, chip, adr, 1);
1124         }
1125         /* Did we succeed? */
1126         if (!chip_good(map, adr, datum)) {
1127                 /* reset on all failures. */
1128                 map_write( map, CMD(0xF0), chip->start );
1129                 /* FIXME - should have reset delay before continuing */
1130
1131                 if (++retry_cnt <= MAX_WORD_RETRIES)
1132                         goto retry;
1133
1134                 ret = -EIO;
1135         }
1136         xip_enable(map, chip, adr);
1137  op_done:
1138         chip->state = FL_READY;
1139         put_chip(map, chip, adr);
1140         spin_unlock(chip->mutex);
1141
1142         return ret;
1143 }
1144
1145
1146 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1147                                   size_t *retlen, const u_char *buf)
1148 {
1149         struct map_info *map = mtd->priv;
1150         struct cfi_private *cfi = map->fldrv_priv;
1151         int ret = 0;
1152         int chipnum;
1153         unsigned long ofs, chipstart;
1154         DECLARE_WAITQUEUE(wait, current);
1155
1156         *retlen = 0;
1157         if (!len)
1158                 return 0;
1159
1160         chipnum = to >> cfi->chipshift;
1161         ofs = to  - (chipnum << cfi->chipshift);
1162         chipstart = cfi->chips[chipnum].start;
1163
1164         /* If it's not bus-aligned, do the first byte write */
1165         if (ofs & (map_bankwidth(map)-1)) {
1166                 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1167                 int i = ofs - bus_ofs;
1168                 int n = 0;
1169                 map_word tmp_buf;
1170
1171  retry:
1172                 spin_lock(cfi->chips[chipnum].mutex);
1173
1174                 if (cfi->chips[chipnum].state != FL_READY) {
1175 #if 0
1176                         printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1177 #endif
1178                         set_current_state(TASK_UNINTERRUPTIBLE);
1179                         add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1180
1181                         spin_unlock(cfi->chips[chipnum].mutex);
1182
1183                         schedule();
1184                         remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1185 #if 0
1186                         if(signal_pending(current))
1187                                 return -EINTR;
1188 #endif
1189                         goto retry;
1190                 }
1191
1192                 /* Load 'tmp_buf' with old contents of flash */
1193                 tmp_buf = map_read(map, bus_ofs+chipstart);
1194
1195                 spin_unlock(cfi->chips[chipnum].mutex);
1196
1197                 /* Number of bytes to copy from buffer */
1198                 n = min_t(int, len, map_bankwidth(map)-i);
1199
1200                 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1201
1202                 ret = do_write_oneword(map, &cfi->chips[chipnum],
1203                                        bus_ofs, tmp_buf);
1204                 if (ret)
1205                         return ret;
1206
1207                 ofs += n;
1208                 buf += n;
1209                 (*retlen) += n;
1210                 len -= n;
1211
1212                 if (ofs >> cfi->chipshift) {
1213                         chipnum ++;
1214                         ofs = 0;
1215                         if (chipnum == cfi->numchips)
1216                                 return 0;
1217                 }
1218         }
1219
1220         /* We are now aligned, write as much as possible */
1221         while(len >= map_bankwidth(map)) {
1222                 map_word datum;
1223
1224                 datum = map_word_load(map, buf);
1225
1226                 ret = do_write_oneword(map, &cfi->chips[chipnum],
1227                                        ofs, datum);
1228                 if (ret)
1229                         return ret;
1230
1231                 ofs += map_bankwidth(map);
1232                 buf += map_bankwidth(map);
1233                 (*retlen) += map_bankwidth(map);
1234                 len -= map_bankwidth(map);
1235
1236                 if (ofs >> cfi->chipshift) {
1237                         chipnum ++;
1238                         ofs = 0;
1239                         if (chipnum == cfi->numchips)
1240                                 return 0;
1241                         chipstart = cfi->chips[chipnum].start;
1242                 }
1243         }
1244
1245         /* Write the trailing bytes if any */
1246         if (len & (map_bankwidth(map)-1)) {
1247                 map_word tmp_buf;
1248
1249  retry1:
1250                 spin_lock(cfi->chips[chipnum].mutex);
1251
1252                 if (cfi->chips[chipnum].state != FL_READY) {
1253 #if 0
1254                         printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1255 #endif
1256                         set_current_state(TASK_UNINTERRUPTIBLE);
1257                         add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1258
1259                         spin_unlock(cfi->chips[chipnum].mutex);
1260
1261                         schedule();
1262                         remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1263 #if 0
1264                         if(signal_pending(current))
1265                                 return -EINTR;
1266 #endif
1267                         goto retry1;
1268                 }
1269
1270                 tmp_buf = map_read(map, ofs + chipstart);
1271
1272                 spin_unlock(cfi->chips[chipnum].mutex);
1273
1274                 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1275
1276                 ret = do_write_oneword(map, &cfi->chips[chipnum],
1277                                 ofs, tmp_buf);
1278                 if (ret)
1279                         return ret;
1280
1281                 (*retlen) += len;
1282         }
1283
1284         return 0;
1285 }
1286
1287
1288 /*
1289  * FIXME: interleaved mode not tested, and probably not supported!
1290  */
1291 static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
1292                                     unsigned long adr, const u_char *buf,
1293                                     int len)
1294 {
1295         struct cfi_private *cfi = map->fldrv_priv;
1296         unsigned long timeo = jiffies + HZ;
1297         /* see comments in do_write_oneword() regarding uWriteTimeo. */
1298         unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1299         int ret = -EIO;
1300         unsigned long cmd_adr;
1301         int z, words;
1302         map_word datum;
1303
1304         adr += chip->start;
1305         cmd_adr = adr;
1306
1307         spin_lock(chip->mutex);
1308         ret = get_chip(map, chip, adr, FL_WRITING);
1309         if (ret) {
1310                 spin_unlock(chip->mutex);
1311                 return ret;
1312         }
1313
1314         datum = map_word_load(map, buf);
1315
1316         DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1317                __func__, adr, datum.x[0] );
1318
1319         XIP_INVAL_CACHED_RANGE(map, adr, len);
1320         ENABLE_VPP(map);
1321         xip_disable(map, chip, cmd_adr);
1322
1323         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1324         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1325         //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1326
1327         /* Write Buffer Load */
1328         map_write(map, CMD(0x25), cmd_adr);
1329
1330         chip->state = FL_WRITING_TO_BUFFER;
1331
1332         /* Write length of data to come */
1333         words = len / map_bankwidth(map);
1334         map_write(map, CMD(words - 1), cmd_adr);
1335         /* Write data */
1336         z = 0;
1337         while(z < words * map_bankwidth(map)) {
1338                 datum = map_word_load(map, buf);
1339                 map_write(map, datum, adr + z);
1340
1341                 z += map_bankwidth(map);
1342                 buf += map_bankwidth(map);
1343         }
1344         z -= map_bankwidth(map);
1345
1346         adr += z;
1347
1348         /* Write Buffer Program Confirm: GO GO GO */
1349         map_write(map, CMD(0x29), cmd_adr);
1350         chip->state = FL_WRITING;
1351
1352         INVALIDATE_CACHE_UDELAY(map, chip,
1353                                 adr, map_bankwidth(map),
1354                                 chip->word_write_time);
1355
1356         timeo = jiffies + uWriteTimeout;
1357
1358         for (;;) {
1359                 if (chip->state != FL_WRITING) {
1360                         /* Someone's suspended the write. Sleep */
1361                         DECLARE_WAITQUEUE(wait, current);
1362
1363                         set_current_state(TASK_UNINTERRUPTIBLE);
1364                         add_wait_queue(&chip->wq, &wait);
1365                         spin_unlock(chip->mutex);
1366                         schedule();
1367                         remove_wait_queue(&chip->wq, &wait);
1368                         timeo = jiffies + (HZ / 2); /* FIXME */
1369                         spin_lock(chip->mutex);
1370                         continue;
1371                 }
1372
1373                 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1374                         break;
1375
1376                 if (chip_ready(map, adr)) {
1377                         xip_enable(map, chip, adr);
1378                         goto op_done;
1379                 }
1380
1381                 /* Latency issues. Drop the lock, wait a while and retry */
1382                 UDELAY(map, chip, adr, 1);
1383         }
1384
1385         /* reset on all failures. */
1386         map_write( map, CMD(0xF0), chip->start );
1387         xip_enable(map, chip, adr);
1388         /* FIXME - should have reset delay before continuing */
1389
1390         printk(KERN_WARNING "MTD %s(): software timeout\n",
1391                __func__ );
1392
1393         ret = -EIO;
1394  op_done:
1395         chip->state = FL_READY;
1396         put_chip(map, chip, adr);
1397         spin_unlock(chip->mutex);
1398
1399         return ret;
1400 }
1401
1402
1403 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1404                                     size_t *retlen, const u_char *buf)
1405 {
1406         struct map_info *map = mtd->priv;
1407         struct cfi_private *cfi = map->fldrv_priv;
1408         int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1409         int ret = 0;
1410         int chipnum;
1411         unsigned long ofs;
1412
1413         *retlen = 0;
1414         if (!len)
1415                 return 0;
1416
1417         chipnum = to >> cfi->chipshift;
1418         ofs = to  - (chipnum << cfi->chipshift);
1419
1420         /* If it's not bus-aligned, do the first word write */
1421         if (ofs & (map_bankwidth(map)-1)) {
1422                 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1423                 if (local_len > len)
1424                         local_len = len;
1425                 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1426                                              local_len, retlen, buf);
1427                 if (ret)
1428                         return ret;
1429                 ofs += local_len;
1430                 buf += local_len;
1431                 len -= local_len;
1432
1433                 if (ofs >> cfi->chipshift) {
1434                         chipnum ++;
1435                         ofs = 0;
1436                         if (chipnum == cfi->numchips)
1437                                 return 0;
1438                 }
1439         }
1440
1441         /* Write buffer is worth it only if more than one word to write... */
1442         while (len >= map_bankwidth(map) * 2) {
1443                 /* We must not cross write block boundaries */
1444                 int size = wbufsize - (ofs & (wbufsize-1));
1445
1446                 if (size > len)
1447                         size = len;
1448                 if (size % map_bankwidth(map))
1449                         size -= size % map_bankwidth(map);
1450
1451                 ret = do_write_buffer(map, &cfi->chips[chipnum],
1452                                       ofs, buf, size);
1453                 if (ret)
1454                         return ret;
1455
1456                 ofs += size;
1457                 buf += size;
1458                 (*retlen) += size;
1459                 len -= size;
1460
1461                 if (ofs >> cfi->chipshift) {
1462                         chipnum ++;
1463                         ofs = 0;
1464                         if (chipnum == cfi->numchips)
1465                                 return 0;
1466                 }
1467         }
1468
1469         if (len) {
1470                 size_t retlen_dregs = 0;
1471
1472                 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1473                                              len, &retlen_dregs, buf);
1474
1475                 *retlen += retlen_dregs;
1476                 return ret;
1477         }
1478
1479         return 0;
1480 }
1481
1482
1483 /*
1484  * Handle devices with one erase region, that only implement
1485  * the chip erase command.
1486  */
1487 static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
1488 {
1489         struct cfi_private *cfi = map->fldrv_priv;
1490         unsigned long timeo = jiffies + HZ;
1491         unsigned long int adr;
1492         DECLARE_WAITQUEUE(wait, current);
1493         int ret = 0;
1494
1495         adr = cfi->addr_unlock1;
1496
1497         spin_lock(chip->mutex);
1498         ret = get_chip(map, chip, adr, FL_WRITING);
1499         if (ret) {
1500                 spin_unlock(chip->mutex);
1501                 return ret;
1502         }
1503
1504         DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1505                __func__, chip->start );
1506
1507         XIP_INVAL_CACHED_RANGE(map, adr, map->size);
1508         ENABLE_VPP(map);
1509         xip_disable(map, chip, adr);
1510
1511         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1512         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1513         cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1514         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1515         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1516         cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1517
1518         chip->state = FL_ERASING;
1519         chip->erase_suspended = 0;
1520         chip->in_progress_block_addr = adr;
1521
1522         INVALIDATE_CACHE_UDELAY(map, chip,
1523                                 adr, map->size,
1524                                 chip->erase_time*500);
1525
1526         timeo = jiffies + (HZ*20);
1527
1528         for (;;) {
1529                 if (chip->state != FL_ERASING) {
1530                         /* Someone's suspended the erase. Sleep */
1531                         set_current_state(TASK_UNINTERRUPTIBLE);
1532                         add_wait_queue(&chip->wq, &wait);
1533                         spin_unlock(chip->mutex);
1534                         schedule();
1535                         remove_wait_queue(&chip->wq, &wait);
1536                         spin_lock(chip->mutex);
1537                         continue;
1538                 }
1539                 if (chip->erase_suspended) {
1540                         /* This erase was suspended and resumed.
1541                            Adjust the timeout */
1542                         timeo = jiffies + (HZ*20); /* FIXME */
1543                         chip->erase_suspended = 0;
1544                 }
1545
1546                 if (chip_ready(map, adr))
1547                         break;
1548
1549                 if (time_after(jiffies, timeo)) {
1550                         printk(KERN_WARNING "MTD %s(): software timeout\n",
1551                                 __func__ );
1552                         break;
1553                 }
1554
1555                 /* Latency issues. Drop the lock, wait a while and retry */
1556                 UDELAY(map, chip, adr, 1000000/HZ);
1557         }
1558         /* Did we succeed? */
1559         if (!chip_good(map, adr, map_word_ff(map))) {
1560                 /* reset on all failures. */
1561                 map_write( map, CMD(0xF0), chip->start );
1562                 /* FIXME - should have reset delay before continuing */
1563
1564                 ret = -EIO;
1565         }
1566
1567         chip->state = FL_READY;
1568         xip_enable(map, chip, adr);
1569         put_chip(map, chip, adr);
1570         spin_unlock(chip->mutex);
1571
1572         return ret;
1573 }
1574
1575
1576 static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
1577 {
1578         struct cfi_private *cfi = map->fldrv_priv;
1579         unsigned long timeo = jiffies + HZ;
1580         DECLARE_WAITQUEUE(wait, current);
1581         int ret = 0;
1582
1583         adr += chip->start;
1584
1585         spin_lock(chip->mutex);
1586         ret = get_chip(map, chip, adr, FL_ERASING);
1587         if (ret) {
1588                 spin_unlock(chip->mutex);
1589                 return ret;
1590         }
1591
1592         DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1593                __func__, adr );
1594
1595         XIP_INVAL_CACHED_RANGE(map, adr, len);
1596         ENABLE_VPP(map);
1597         xip_disable(map, chip, adr);
1598
1599         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1600         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1601         cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1602         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1603         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1604         map_write(map, CMD(0x30), adr);
1605
1606         chip->state = FL_ERASING;
1607         chip->erase_suspended = 0;
1608         chip->in_progress_block_addr = adr;
1609
1610         INVALIDATE_CACHE_UDELAY(map, chip,
1611                                 adr, len,
1612                                 chip->erase_time*500);
1613
1614         timeo = jiffies + (HZ*20);
1615
1616         for (;;) {
1617                 if (chip->state != FL_ERASING) {
1618                         /* Someone's suspended the erase. Sleep */
1619                         set_current_state(TASK_UNINTERRUPTIBLE);
1620                         add_wait_queue(&chip->wq, &wait);
1621                         spin_unlock(chip->mutex);
1622                         schedule();
1623                         remove_wait_queue(&chip->wq, &wait);
1624                         spin_lock(chip->mutex);
1625                         continue;
1626                 }
1627                 if (chip->erase_suspended) {
1628                         /* This erase was suspended and resumed.
1629                            Adjust the timeout */
1630                         timeo = jiffies + (HZ*20); /* FIXME */
1631                         chip->erase_suspended = 0;
1632                 }
1633
1634                 if (chip_ready(map, adr)) {
1635                         xip_enable(map, chip, adr);
1636                         break;
1637                 }
1638
1639                 if (time_after(jiffies, timeo)) {
1640                         xip_enable(map, chip, adr);
1641                         printk(KERN_WARNING "MTD %s(): software timeout\n",
1642                                 __func__ );
1643                         break;
1644                 }
1645
1646                 /* Latency issues. Drop the lock, wait a while and retry */
1647                 UDELAY(map, chip, adr, 1000000/HZ);
1648         }
1649         /* Did we succeed? */
1650         if (!chip_good(map, adr, map_word_ff(map))) {
1651                 /* reset on all failures. */
1652                 map_write( map, CMD(0xF0), chip->start );
1653                 /* FIXME - should have reset delay before continuing */
1654
1655                 ret = -EIO;
1656         }
1657
1658         chip->state = FL_READY;
1659         put_chip(map, chip, adr);
1660         spin_unlock(chip->mutex);
1661         return ret;
1662 }
1663
1664
1665 static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1666 {
1667         unsigned long ofs, len;
1668         int ret;
1669
1670         ofs = instr->addr;
1671         len = instr->len;
1672
1673         ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1674         if (ret)
1675                 return ret;
1676
1677         instr->state = MTD_ERASE_DONE;
1678         mtd_erase_callback(instr);
1679
1680         return 0;
1681 }
1682
1683
1684 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1685 {
1686         struct map_info *map = mtd->priv;
1687         struct cfi_private *cfi = map->fldrv_priv;
1688         int ret = 0;
1689
1690         if (instr->addr != 0)
1691                 return -EINVAL;
1692
1693         if (instr->len != mtd->size)
1694                 return -EINVAL;
1695
1696         ret = do_erase_chip(map, &cfi->chips[0]);
1697         if (ret)
1698                 return ret;
1699
1700         instr->state = MTD_ERASE_DONE;
1701         mtd_erase_callback(instr);
1702
1703         return 0;
1704 }
1705
1706 static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1707                          unsigned long adr, int len, void *thunk)
1708 {
1709         struct cfi_private *cfi = map->fldrv_priv;
1710         int ret;
1711
1712         spin_lock(chip->mutex);
1713         ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1714         if (ret)
1715                 goto out_unlock;
1716         chip->state = FL_LOCKING;
1717
1718         DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1719               __func__, adr, len);
1720
1721         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1722                          cfi->device_type, NULL);
1723         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1724                          cfi->device_type, NULL);
1725         cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1726                          cfi->device_type, NULL);
1727         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1728                          cfi->device_type, NULL);
1729         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1730                          cfi->device_type, NULL);
1731         map_write(map, CMD(0x40), chip->start + adr);
1732
1733         chip->state = FL_READY;
1734         put_chip(map, chip, adr + chip->start);
1735         ret = 0;
1736
1737 out_unlock:
1738         spin_unlock(chip->mutex);
1739         return ret;
1740 }
1741
1742 static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1743                            unsigned long adr, int len, void *thunk)
1744 {
1745         struct cfi_private *cfi = map->fldrv_priv;
1746         int ret;
1747
1748         spin_lock(chip->mutex);
1749         ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1750         if (ret)
1751                 goto out_unlock;
1752         chip->state = FL_UNLOCKING;
1753
1754         DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1755               __func__, adr, len);
1756
1757         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1758                          cfi->device_type, NULL);
1759         map_write(map, CMD(0x70), adr);
1760
1761         chip->state = FL_READY;
1762         put_chip(map, chip, adr + chip->start);
1763         ret = 0;
1764
1765 out_unlock:
1766         spin_unlock(chip->mutex);
1767         return ret;
1768 }
1769
1770 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1771 {
1772         return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1773 }
1774
1775 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1776 {
1777         return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1778 }
1779
1780
1781 static void cfi_amdstd_sync (struct mtd_info *mtd)
1782 {
1783         struct map_info *map = mtd->priv;
1784         struct cfi_private *cfi = map->fldrv_priv;
1785         int i;
1786         struct flchip *chip;
1787         int ret = 0;
1788         DECLARE_WAITQUEUE(wait, current);
1789
1790         for (i=0; !ret && i<cfi->numchips; i++) {
1791                 chip = &cfi->chips[i];
1792
1793         retry:
1794                 spin_lock(chip->mutex);
1795
1796                 switch(chip->state) {
1797                 case FL_READY:
1798                 case FL_STATUS:
1799                 case FL_CFI_QUERY:
1800                 case FL_JEDEC_QUERY:
1801                         chip->oldstate = chip->state;
1802                         chip->state = FL_SYNCING;
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.
1806                          */
1807                 case FL_SYNCING:
1808                         spin_unlock(chip->mutex);
1809                         break;
1810
1811                 default:
1812                         /* Not an idle state */
1813                         set_current_state(TASK_UNINTERRUPTIBLE);
1814                         add_wait_queue(&chip->wq, &wait);
1815
1816                         spin_unlock(chip->mutex);
1817
1818                         schedule();
1819
1820                         remove_wait_queue(&chip->wq, &wait);
1821
1822                         goto retry;
1823                 }
1824         }
1825
1826         /* Unlock the chips again */
1827
1828         for (i--; i >=0; i--) {
1829                 chip = &cfi->chips[i];
1830
1831                 spin_lock(chip->mutex);
1832
1833                 if (chip->state == FL_SYNCING) {
1834                         chip->state = chip->oldstate;
1835                         wake_up(&chip->wq);
1836                 }
1837                 spin_unlock(chip->mutex);
1838         }
1839 }
1840
1841
1842 static int cfi_amdstd_suspend(struct mtd_info *mtd)
1843 {
1844         struct map_info *map = mtd->priv;
1845         struct cfi_private *cfi = map->fldrv_priv;
1846         int i;
1847         struct flchip *chip;
1848         int ret = 0;
1849
1850         for (i=0; !ret && i<cfi->numchips; i++) {
1851                 chip = &cfi->chips[i];
1852
1853                 spin_lock(chip->mutex);
1854
1855                 switch(chip->state) {
1856                 case FL_READY:
1857                 case FL_STATUS:
1858                 case FL_CFI_QUERY:
1859                 case FL_JEDEC_QUERY:
1860                         chip->oldstate = chip->state;
1861                         chip->state = FL_PM_SUSPENDED;
1862                         /* No need to wake_up() on this state change -
1863                          * as the whole point is that nobody can do anything
1864                          * with the chip now anyway.
1865                          */
1866                 case FL_PM_SUSPENDED:
1867                         break;
1868
1869                 default:
1870                         ret = -EAGAIN;
1871                         break;
1872                 }
1873                 spin_unlock(chip->mutex);
1874         }
1875
1876         /* Unlock the chips again */
1877
1878         if (ret) {
1879                 for (i--; i >=0; i--) {
1880                         chip = &cfi->chips[i];
1881
1882                         spin_lock(chip->mutex);
1883
1884                         if (chip->state == FL_PM_SUSPENDED) {
1885                                 chip->state = chip->oldstate;
1886                                 wake_up(&chip->wq);
1887                         }
1888                         spin_unlock(chip->mutex);
1889                 }
1890         }
1891
1892         return ret;
1893 }
1894
1895
1896 static void cfi_amdstd_resume(struct mtd_info *mtd)
1897 {
1898         struct map_info *map = mtd->priv;
1899         struct cfi_private *cfi = map->fldrv_priv;
1900         int i;
1901         struct flchip *chip;
1902
1903         for (i=0; i<cfi->numchips; i++) {
1904
1905                 chip = &cfi->chips[i];
1906
1907                 spin_lock(chip->mutex);
1908
1909                 if (chip->state == FL_PM_SUSPENDED) {
1910                         chip->state = FL_READY;
1911                         map_write(map, CMD(0xF0), chip->start);
1912                         wake_up(&chip->wq);
1913                 }
1914                 else
1915                         printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1916
1917                 spin_unlock(chip->mutex);
1918         }
1919 }
1920
1921 static void cfi_amdstd_destroy(struct mtd_info *mtd)
1922 {
1923         struct map_info *map = mtd->priv;
1924         struct cfi_private *cfi = map->fldrv_priv;
1925
1926         kfree(cfi->cmdset_priv);
1927         kfree(cfi->cfiq);
1928         kfree(cfi);
1929         kfree(mtd->eraseregions);
1930 }
1931
1932 MODULE_LICENSE("GPL");
1933 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1934 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");