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