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