GNU Linux-libre 4.9.299-gnu1
[releases.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 <asm/io.h>
28 #include <asm/byteorder.h>
29
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/reboot.h>
35 #include <linux/of.h>
36 #include <linux/of_platform.h>
37 #include <linux/mtd/map.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/cfi.h>
40 #include <linux/mtd/xip.h>
41
42 #define AMD_BOOTLOC_BUG
43 #define FORCE_WORD_WRITE 0
44
45 #define MAX_RETRIES 3
46
47 #define SST49LF004B             0x0060
48 #define SST49LF040B             0x0050
49 #define SST49LF008A             0x005a
50 #define AT49BV6416              0x00d6
51
52 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
53 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
55 static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
56 static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
57 static void cfi_amdstd_sync (struct mtd_info *);
58 static int cfi_amdstd_suspend (struct mtd_info *);
59 static void cfi_amdstd_resume (struct mtd_info *);
60 static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
61 static int cfi_amdstd_get_fact_prot_info(struct mtd_info *, size_t,
62                                          size_t *, struct otp_info *);
63 static int cfi_amdstd_get_user_prot_info(struct mtd_info *, size_t,
64                                          size_t *, struct otp_info *);
65 static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
66 static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *, loff_t, size_t,
67                                          size_t *, u_char *);
68 static int cfi_amdstd_read_user_prot_reg(struct mtd_info *, loff_t, size_t,
69                                          size_t *, u_char *);
70 static int cfi_amdstd_write_user_prot_reg(struct mtd_info *, loff_t, size_t,
71                                           size_t *, u_char *);
72 static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *, loff_t, size_t);
73
74 static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
75                                   size_t *retlen, const u_char *buf);
76
77 static void cfi_amdstd_destroy(struct mtd_info *);
78
79 struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
80 static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
81
82 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
83 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
84 #include "fwh_lock.h"
85
86 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
87 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
88
89 static int cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
90 static int cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
91 static int cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
92
93 static struct mtd_chip_driver cfi_amdstd_chipdrv = {
94         .probe          = NULL, /* Not usable directly */
95         .destroy        = cfi_amdstd_destroy,
96         .name           = "cfi_cmdset_0002",
97         .module         = THIS_MODULE
98 };
99
100
101 /* #define DEBUG_CFI_FEATURES */
102
103
104 #ifdef DEBUG_CFI_FEATURES
105 static void cfi_tell_features(struct cfi_pri_amdstd *extp)
106 {
107         const char* erase_suspend[3] = {
108                 "Not supported", "Read only", "Read/write"
109         };
110         const char* top_bottom[6] = {
111                 "No WP", "8x8KiB sectors at top & bottom, no WP",
112                 "Bottom boot", "Top boot",
113                 "Uniform, Bottom WP", "Uniform, Top WP"
114         };
115
116         printk("  Silicon revision: %d\n", extp->SiliconRevision >> 1);
117         printk("  Address sensitive unlock: %s\n",
118                (extp->SiliconRevision & 1) ? "Not required" : "Required");
119
120         if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
121                 printk("  Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
122         else
123                 printk("  Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
124
125         if (extp->BlkProt == 0)
126                 printk("  Block protection: Not supported\n");
127         else
128                 printk("  Block protection: %d sectors per group\n", extp->BlkProt);
129
130
131         printk("  Temporary block unprotect: %s\n",
132                extp->TmpBlkUnprotect ? "Supported" : "Not supported");
133         printk("  Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
134         printk("  Number of simultaneous operations: %d\n", extp->SimultaneousOps);
135         printk("  Burst mode: %s\n",
136                extp->BurstMode ? "Supported" : "Not supported");
137         if (extp->PageMode == 0)
138                 printk("  Page mode: Not supported\n");
139         else
140                 printk("  Page mode: %d word page\n", extp->PageMode << 2);
141
142         printk("  Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
143                extp->VppMin >> 4, extp->VppMin & 0xf);
144         printk("  Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
145                extp->VppMax >> 4, extp->VppMax & 0xf);
146
147         if (extp->TopBottom < ARRAY_SIZE(top_bottom))
148                 printk("  Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
149         else
150                 printk("  Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
151 }
152 #endif
153
154 #ifdef AMD_BOOTLOC_BUG
155 /* Wheee. Bring me the head of someone at AMD. */
156 static void fixup_amd_bootblock(struct mtd_info *mtd)
157 {
158         struct map_info *map = mtd->priv;
159         struct cfi_private *cfi = map->fldrv_priv;
160         struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
161         __u8 major = extp->MajorVersion;
162         __u8 minor = extp->MinorVersion;
163
164         if (((major << 8) | minor) < 0x3131) {
165                 /* CFI version 1.0 => don't trust bootloc */
166
167                 pr_debug("%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
168                         map->name, cfi->mfr, cfi->id);
169
170                 /* AFAICS all 29LV400 with a bottom boot block have a device ID
171                  * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
172                  * These were badly detected as they have the 0x80 bit set
173                  * so treat them as a special case.
174                  */
175                 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
176
177                         /* Macronix added CFI to their 2nd generation
178                          * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
179                          * Fujitsu, Spansion, EON, ESI and older Macronix)
180                          * has CFI.
181                          *
182                          * Therefore also check the manufacturer.
183                          * This reduces the risk of false detection due to
184                          * the 8-bit device ID.
185                          */
186                         (cfi->mfr == CFI_MFR_MACRONIX)) {
187                         pr_debug("%s: Macronix MX29LV400C with bottom boot block"
188                                 " detected\n", map->name);
189                         extp->TopBottom = 2;    /* bottom boot */
190                 } else
191                 if (cfi->id & 0x80) {
192                         printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
193                         extp->TopBottom = 3;    /* top boot */
194                 } else {
195                         extp->TopBottom = 2;    /* bottom boot */
196                 }
197
198                 pr_debug("%s: AMD CFI PRI V%c.%c has no boot block field;"
199                         " deduced %s from Device ID\n", map->name, major, minor,
200                         extp->TopBottom == 2 ? "bottom" : "top");
201         }
202 }
203 #endif
204
205 static void fixup_use_write_buffers(struct mtd_info *mtd)
206 {
207         struct map_info *map = mtd->priv;
208         struct cfi_private *cfi = map->fldrv_priv;
209         if (cfi->cfiq->BufWriteTimeoutTyp) {
210                 pr_debug("Using buffer write method\n" );
211                 mtd->_write = cfi_amdstd_write_buffers;
212         }
213 }
214
215 /* Atmel chips don't use the same PRI format as AMD chips */
216 static void fixup_convert_atmel_pri(struct mtd_info *mtd)
217 {
218         struct map_info *map = mtd->priv;
219         struct cfi_private *cfi = map->fldrv_priv;
220         struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
221         struct cfi_pri_atmel atmel_pri;
222
223         memcpy(&atmel_pri, extp, sizeof(atmel_pri));
224         memset((char *)extp + 5, 0, sizeof(*extp) - 5);
225
226         if (atmel_pri.Features & 0x02)
227                 extp->EraseSuspend = 2;
228
229         /* Some chips got it backwards... */
230         if (cfi->id == AT49BV6416) {
231                 if (atmel_pri.BottomBoot)
232                         extp->TopBottom = 3;
233                 else
234                         extp->TopBottom = 2;
235         } else {
236                 if (atmel_pri.BottomBoot)
237                         extp->TopBottom = 2;
238                 else
239                         extp->TopBottom = 3;
240         }
241
242         /* burst write mode not supported */
243         cfi->cfiq->BufWriteTimeoutTyp = 0;
244         cfi->cfiq->BufWriteTimeoutMax = 0;
245 }
246
247 static void fixup_use_secsi(struct mtd_info *mtd)
248 {
249         /* Setup for chips with a secsi area */
250         mtd->_read_user_prot_reg = cfi_amdstd_secsi_read;
251         mtd->_read_fact_prot_reg = cfi_amdstd_secsi_read;
252 }
253
254 static void fixup_use_erase_chip(struct mtd_info *mtd)
255 {
256         struct map_info *map = mtd->priv;
257         struct cfi_private *cfi = map->fldrv_priv;
258         if ((cfi->cfiq->NumEraseRegions == 1) &&
259                 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
260                 mtd->_erase = cfi_amdstd_erase_chip;
261         }
262
263 }
264
265 /*
266  * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
267  * locked by default.
268  */
269 static void fixup_use_atmel_lock(struct mtd_info *mtd)
270 {
271         mtd->_lock = cfi_atmel_lock;
272         mtd->_unlock = cfi_atmel_unlock;
273         mtd->flags |= MTD_POWERUP_LOCK;
274 }
275
276 static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
277 {
278         struct map_info *map = mtd->priv;
279         struct cfi_private *cfi = map->fldrv_priv;
280
281         /*
282          * These flashes report two separate eraseblock regions based on the
283          * sector_erase-size and block_erase-size, although they both operate on the
284          * same memory. This is not allowed according to CFI, so we just pick the
285          * sector_erase-size.
286          */
287         cfi->cfiq->NumEraseRegions = 1;
288 }
289
290 static void fixup_sst39vf(struct mtd_info *mtd)
291 {
292         struct map_info *map = mtd->priv;
293         struct cfi_private *cfi = map->fldrv_priv;
294
295         fixup_old_sst_eraseregion(mtd);
296
297         cfi->addr_unlock1 = 0x5555;
298         cfi->addr_unlock2 = 0x2AAA;
299 }
300
301 static void fixup_sst39vf_rev_b(struct mtd_info *mtd)
302 {
303         struct map_info *map = mtd->priv;
304         struct cfi_private *cfi = map->fldrv_priv;
305
306         fixup_old_sst_eraseregion(mtd);
307
308         cfi->addr_unlock1 = 0x555;
309         cfi->addr_unlock2 = 0x2AA;
310
311         cfi->sector_erase_cmd = CMD(0x50);
312 }
313
314 static void fixup_sst38vf640x_sectorsize(struct mtd_info *mtd)
315 {
316         struct map_info *map = mtd->priv;
317         struct cfi_private *cfi = map->fldrv_priv;
318
319         fixup_sst39vf_rev_b(mtd);
320
321         /*
322          * CFI reports 1024 sectors (0x03ff+1) of 64KBytes (0x0100*256) where
323          * it should report a size of 8KBytes (0x0020*256).
324          */
325         cfi->cfiq->EraseRegionInfo[0] = 0x002003ff;
326         pr_warning("%s: Bad 38VF640x CFI data; adjusting sector size from 64 to 8KiB\n", mtd->name);
327 }
328
329 static void fixup_s29gl064n_sectors(struct mtd_info *mtd)
330 {
331         struct map_info *map = mtd->priv;
332         struct cfi_private *cfi = map->fldrv_priv;
333
334         if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
335                 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
336                 pr_warning("%s: Bad S29GL064N CFI data; adjust from 64 to 128 sectors\n", mtd->name);
337         }
338 }
339
340 static void fixup_s29gl032n_sectors(struct mtd_info *mtd)
341 {
342         struct map_info *map = mtd->priv;
343         struct cfi_private *cfi = map->fldrv_priv;
344
345         if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
346                 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
347                 pr_warning("%s: Bad S29GL032N CFI data; adjust from 127 to 63 sectors\n", mtd->name);
348         }
349 }
350
351 static void fixup_s29ns512p_sectors(struct mtd_info *mtd)
352 {
353         struct map_info *map = mtd->priv;
354         struct cfi_private *cfi = map->fldrv_priv;
355
356         /*
357          *  S29NS512P flash uses more than 8bits to report number of sectors,
358          * which is not permitted by CFI.
359          */
360         cfi->cfiq->EraseRegionInfo[0] = 0x020001ff;
361         pr_warning("%s: Bad S29NS512P CFI data; adjust to 512 sectors\n", mtd->name);
362 }
363
364 /* Used to fix CFI-Tables of chips without Extended Query Tables */
365 static struct cfi_fixup cfi_nopri_fixup_table[] = {
366         { CFI_MFR_SST, 0x234a, fixup_sst39vf }, /* SST39VF1602 */
367         { CFI_MFR_SST, 0x234b, fixup_sst39vf }, /* SST39VF1601 */
368         { CFI_MFR_SST, 0x235a, fixup_sst39vf }, /* SST39VF3202 */
369         { CFI_MFR_SST, 0x235b, fixup_sst39vf }, /* SST39VF3201 */
370         { CFI_MFR_SST, 0x235c, fixup_sst39vf_rev_b }, /* SST39VF3202B */
371         { CFI_MFR_SST, 0x235d, fixup_sst39vf_rev_b }, /* SST39VF3201B */
372         { CFI_MFR_SST, 0x236c, fixup_sst39vf_rev_b }, /* SST39VF6402B */
373         { CFI_MFR_SST, 0x236d, fixup_sst39vf_rev_b }, /* SST39VF6401B */
374         { 0, 0, NULL }
375 };
376
377 static struct cfi_fixup cfi_fixup_table[] = {
378         { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
379 #ifdef AMD_BOOTLOC_BUG
380         { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock },
381         { CFI_MFR_AMIC, CFI_ID_ANY, fixup_amd_bootblock },
382         { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock },
383 #endif
384         { CFI_MFR_AMD, 0x0050, fixup_use_secsi },
385         { CFI_MFR_AMD, 0x0053, fixup_use_secsi },
386         { CFI_MFR_AMD, 0x0055, fixup_use_secsi },
387         { CFI_MFR_AMD, 0x0056, fixup_use_secsi },
388         { CFI_MFR_AMD, 0x005C, fixup_use_secsi },
389         { CFI_MFR_AMD, 0x005F, fixup_use_secsi },
390         { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors },
391         { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors },
392         { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors },
393         { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors },
394         { CFI_MFR_AMD, 0x3f00, fixup_s29ns512p_sectors },
395         { CFI_MFR_SST, 0x536a, fixup_sst38vf640x_sectorsize }, /* SST38VF6402 */
396         { CFI_MFR_SST, 0x536b, fixup_sst38vf640x_sectorsize }, /* SST38VF6401 */
397         { CFI_MFR_SST, 0x536c, fixup_sst38vf640x_sectorsize }, /* SST38VF6404 */
398         { CFI_MFR_SST, 0x536d, fixup_sst38vf640x_sectorsize }, /* SST38VF6403 */
399 #if !FORCE_WORD_WRITE
400         { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
401 #endif
402         { 0, 0, NULL }
403 };
404 static struct cfi_fixup jedec_fixup_table[] = {
405         { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock },
406         { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock },
407         { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock },
408         { 0, 0, NULL }
409 };
410
411 static struct cfi_fixup fixup_table[] = {
412         /* The CFI vendor ids and the JEDEC vendor IDs appear
413          * to be common.  It is like the devices id's are as
414          * well.  This table is to pick all cases where
415          * we know that is the case.
416          */
417         { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip },
418         { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock },
419         { 0, 0, NULL }
420 };
421
422
423 static void cfi_fixup_major_minor(struct cfi_private *cfi,
424                                   struct cfi_pri_amdstd *extp)
425 {
426         if (cfi->mfr == CFI_MFR_SAMSUNG) {
427                 if ((extp->MajorVersion == '0' && extp->MinorVersion == '0') ||
428                     (extp->MajorVersion == '3' && extp->MinorVersion == '3')) {
429                         /*
430                          * Samsung K8P2815UQB and K8D6x16UxM chips
431                          * report major=0 / minor=0.
432                          * K8D3x16UxC chips report major=3 / minor=3.
433                          */
434                         printk(KERN_NOTICE "  Fixing Samsung's Amd/Fujitsu"
435                                " Extended Query version to 1.%c\n",
436                                extp->MinorVersion);
437                         extp->MajorVersion = '1';
438                 }
439         }
440
441         /*
442          * SST 38VF640x chips report major=0xFF / minor=0xFF.
443          */
444         if (cfi->mfr == CFI_MFR_SST && (cfi->id >> 4) == 0x0536) {
445                 extp->MajorVersion = '1';
446                 extp->MinorVersion = '0';
447         }
448 }
449
450 static int is_m29ew(struct cfi_private *cfi)
451 {
452         if (cfi->mfr == CFI_MFR_INTEL &&
453             ((cfi->device_type == CFI_DEVICETYPE_X8 && (cfi->id & 0xff) == 0x7e) ||
454              (cfi->device_type == CFI_DEVICETYPE_X16 && cfi->id == 0x227e)))
455                 return 1;
456         return 0;
457 }
458
459 /*
460  * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 20:
461  * Some revisions of the M29EW suffer from erase suspend hang ups. In
462  * particular, it can occur when the sequence
463  * Erase Confirm -> Suspend -> Program -> Resume
464  * causes a lockup due to internal timing issues. The consequence is that the
465  * erase cannot be resumed without inserting a dummy command after programming
466  * and prior to resuming. [...] The work-around is to issue a dummy write cycle
467  * that writes an F0 command code before the RESUME command.
468  */
469 static void cfi_fixup_m29ew_erase_suspend(struct map_info *map,
470                                           unsigned long adr)
471 {
472         struct cfi_private *cfi = map->fldrv_priv;
473         /* before resume, insert a dummy 0xF0 cycle for Micron M29EW devices */
474         if (is_m29ew(cfi))
475                 map_write(map, CMD(0xF0), adr);
476 }
477
478 /*
479  * From TN-13-07: Patching the Linux Kernel and U-Boot for M29 Flash, page 22:
480  *
481  * Some revisions of the M29EW (for example, A1 and A2 step revisions)
482  * are affected by a problem that could cause a hang up when an ERASE SUSPEND
483  * command is issued after an ERASE RESUME operation without waiting for a
484  * minimum delay.  The result is that once the ERASE seems to be completed
485  * (no bits are toggling), the contents of the Flash memory block on which
486  * the erase was ongoing could be inconsistent with the expected values
487  * (typically, the array value is stuck to the 0xC0, 0xC4, 0x80, or 0x84
488  * values), causing a consequent failure of the ERASE operation.
489  * The occurrence of this issue could be high, especially when file system
490  * operations on the Flash are intensive.  As a result, it is recommended
491  * that a patch be applied.  Intensive file system operations can cause many
492  * calls to the garbage routine to free Flash space (also by erasing physical
493  * Flash blocks) and as a result, many consecutive SUSPEND and RESUME
494  * commands can occur.  The problem disappears when a delay is inserted after
495  * the RESUME command by using the udelay() function available in Linux.
496  * The DELAY value must be tuned based on the customer's platform.
497  * The maximum value that fixes the problem in all cases is 500us.
498  * But, in our experience, a delay of 30 Âµs to 50 Âµs is sufficient
499  * in most cases.
500  * We have chosen 500µs because this latency is acceptable.
501  */
502 static void cfi_fixup_m29ew_delay_after_resume(struct cfi_private *cfi)
503 {
504         /*
505          * Resolving the Delay After Resume Issue see Micron TN-13-07
506          * Worst case delay must be 500µs but 30-50µs should be ok as well
507          */
508         if (is_m29ew(cfi))
509                 cfi_udelay(500);
510 }
511
512 struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
513 {
514         struct cfi_private *cfi = map->fldrv_priv;
515         struct device_node __maybe_unused *np = map->device_node;
516         struct mtd_info *mtd;
517         int i;
518
519         mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
520         if (!mtd)
521                 return NULL;
522         mtd->priv = map;
523         mtd->type = MTD_NORFLASH;
524
525         /* Fill in the default mtd operations */
526         mtd->_erase   = cfi_amdstd_erase_varsize;
527         mtd->_write   = cfi_amdstd_write_words;
528         mtd->_read    = cfi_amdstd_read;
529         mtd->_sync    = cfi_amdstd_sync;
530         mtd->_suspend = cfi_amdstd_suspend;
531         mtd->_resume  = cfi_amdstd_resume;
532         mtd->_read_user_prot_reg = cfi_amdstd_read_user_prot_reg;
533         mtd->_read_fact_prot_reg = cfi_amdstd_read_fact_prot_reg;
534         mtd->_get_fact_prot_info = cfi_amdstd_get_fact_prot_info;
535         mtd->_get_user_prot_info = cfi_amdstd_get_user_prot_info;
536         mtd->_write_user_prot_reg = cfi_amdstd_write_user_prot_reg;
537         mtd->_lock_user_prot_reg = cfi_amdstd_lock_user_prot_reg;
538         mtd->flags   = MTD_CAP_NORFLASH;
539         mtd->name    = map->name;
540         mtd->writesize = 1;
541         mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
542
543         pr_debug("MTD %s(): write buffer size %d\n", __func__,
544                         mtd->writebufsize);
545
546         mtd->_panic_write = cfi_amdstd_panic_write;
547         mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
548
549         if (cfi->cfi_mode==CFI_MODE_CFI){
550                 unsigned char bootloc;
551                 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
552                 struct cfi_pri_amdstd *extp;
553
554                 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
555                 if (extp) {
556                         /*
557                          * It's a real CFI chip, not one for which the probe
558                          * routine faked a CFI structure.
559                          */
560                         cfi_fixup_major_minor(cfi, extp);
561
562                         /*
563                          * Valid primary extension versions are: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5
564                          * see: http://cs.ozerki.net/zap/pub/axim-x5/docs/cfi_r20.pdf, page 19 
565                          *      http://www.spansion.com/Support/AppNotes/cfi_100_20011201.pdf
566                          *      http://www.spansion.com/Support/Datasheets/s29ws-p_00_a12_e.pdf
567                          *      http://www.spansion.com/Support/Datasheets/S29GL_128S_01GS_00_02_e.pdf
568                          */
569                         if (extp->MajorVersion != '1' ||
570                             (extp->MajorVersion == '1' && (extp->MinorVersion < '0' || extp->MinorVersion > '5'))) {
571                                 printk(KERN_ERR "  Unknown Amd/Fujitsu Extended Query "
572                                        "version %c.%c (%#02x/%#02x).\n",
573                                        extp->MajorVersion, extp->MinorVersion,
574                                        extp->MajorVersion, extp->MinorVersion);
575                                 kfree(extp);
576                                 kfree(mtd);
577                                 return NULL;
578                         }
579
580                         printk(KERN_INFO "  Amd/Fujitsu Extended Query version %c.%c.\n",
581                                extp->MajorVersion, extp->MinorVersion);
582
583                         /* Install our own private info structure */
584                         cfi->cmdset_priv = extp;
585
586                         /* Apply cfi device specific fixups */
587                         cfi_fixup(mtd, cfi_fixup_table);
588
589 #ifdef DEBUG_CFI_FEATURES
590                         /* Tell the user about it in lots of lovely detail */
591                         cfi_tell_features(extp);
592 #endif
593
594 #ifdef CONFIG_OF
595                         if (np && of_property_read_bool(
596                                     np, "use-advanced-sector-protection")
597                             && extp->BlkProtUnprot == 8) {
598                                 printk(KERN_INFO "  Advanced Sector Protection (PPB Locking) supported\n");
599                                 mtd->_lock = cfi_ppb_lock;
600                                 mtd->_unlock = cfi_ppb_unlock;
601                                 mtd->_is_locked = cfi_ppb_is_locked;
602                         }
603 #endif
604
605                         bootloc = extp->TopBottom;
606                         if ((bootloc < 2) || (bootloc > 5)) {
607                                 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
608                                        "bank location (%d). Assuming bottom.\n",
609                                        map->name, bootloc);
610                                 bootloc = 2;
611                         }
612
613                         if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
614                                 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
615
616                                 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
617                                         int j = (cfi->cfiq->NumEraseRegions-1)-i;
618
619                                         swap(cfi->cfiq->EraseRegionInfo[i],
620                                              cfi->cfiq->EraseRegionInfo[j]);
621                                 }
622                         }
623                         /* Set the default CFI lock/unlock addresses */
624                         cfi->addr_unlock1 = 0x555;
625                         cfi->addr_unlock2 = 0x2aa;
626                 }
627                 cfi_fixup(mtd, cfi_nopri_fixup_table);
628
629                 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
630                         kfree(mtd);
631                         return NULL;
632                 }
633
634         } /* CFI mode */
635         else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
636                 /* Apply jedec specific fixups */
637                 cfi_fixup(mtd, jedec_fixup_table);
638         }
639         /* Apply generic fixups */
640         cfi_fixup(mtd, fixup_table);
641
642         for (i=0; i< cfi->numchips; i++) {
643                 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
644                 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
645                 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
646                 /*
647                  * First calculate the timeout max according to timeout field
648                  * of struct cfi_ident that probed from chip's CFI aera, if
649                  * available. Specify a minimum of 2000us, in case the CFI data
650                  * is wrong.
651                  */
652                 if (cfi->cfiq->BufWriteTimeoutTyp &&
653                     cfi->cfiq->BufWriteTimeoutMax)
654                         cfi->chips[i].buffer_write_time_max =
655                                 1 << (cfi->cfiq->BufWriteTimeoutTyp +
656                                       cfi->cfiq->BufWriteTimeoutMax);
657                 else
658                         cfi->chips[i].buffer_write_time_max = 0;
659
660                 cfi->chips[i].buffer_write_time_max =
661                         max(cfi->chips[i].buffer_write_time_max, 2000);
662
663                 cfi->chips[i].ref_point_counter = 0;
664                 init_waitqueue_head(&(cfi->chips[i].wq));
665         }
666
667         map->fldrv = &cfi_amdstd_chipdrv;
668
669         return cfi_amdstd_setup(mtd);
670 }
671 struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
672 struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
673 EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
674 EXPORT_SYMBOL_GPL(cfi_cmdset_0006);
675 EXPORT_SYMBOL_GPL(cfi_cmdset_0701);
676
677 static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
678 {
679         struct map_info *map = mtd->priv;
680         struct cfi_private *cfi = map->fldrv_priv;
681         unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
682         unsigned long offset = 0;
683         int i,j;
684
685         printk(KERN_NOTICE "number of %s chips: %d\n",
686                (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
687         /* Select the correct geometry setup */
688         mtd->size = devsize * cfi->numchips;
689
690         mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
691         mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
692                                     * mtd->numeraseregions, GFP_KERNEL);
693         if (!mtd->eraseregions)
694                 goto setup_err;
695
696         for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
697                 unsigned long ernum, ersize;
698                 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
699                 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
700
701                 if (mtd->erasesize < ersize) {
702                         mtd->erasesize = ersize;
703                 }
704                 for (j=0; j<cfi->numchips; j++) {
705                         mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
706                         mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
707                         mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
708                 }
709                 offset += (ersize * ernum);
710         }
711         if (offset != devsize) {
712                 /* Argh */
713                 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
714                 goto setup_err;
715         }
716
717         __module_get(THIS_MODULE);
718         register_reboot_notifier(&mtd->reboot_notifier);
719         return mtd;
720
721  setup_err:
722         kfree(mtd->eraseregions);
723         kfree(mtd);
724         kfree(cfi->cmdset_priv);
725         return NULL;
726 }
727
728 /*
729  * Return true if the chip is ready.
730  *
731  * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
732  * non-suspended sector) and is indicated by no toggle bits toggling.
733  *
734  * Note that anything more complicated than checking if no bits are toggling
735  * (including checking DQ5 for an error status) is tricky to get working
736  * correctly and is therefore not done  (particularly with interleaved chips
737  * as each chip must be checked independently of the others).
738  */
739 static int __xipram chip_ready(struct map_info *map, unsigned long addr)
740 {
741         map_word d, t;
742
743         d = map_read(map, addr);
744         t = map_read(map, addr);
745
746         return map_word_equal(map, d, t);
747 }
748
749 /*
750  * Return true if the chip is ready and has the correct value.
751  *
752  * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
753  * non-suspended sector) and it is indicated by no bits toggling.
754  *
755  * Error are indicated by toggling bits or bits held with the wrong value,
756  * or with bits toggling.
757  *
758  * Note that anything more complicated than checking if no bits are toggling
759  * (including checking DQ5 for an error status) is tricky to get working
760  * correctly and is therefore not done  (particularly with interleaved chips
761  * as each chip must be checked independently of the others).
762  *
763  */
764 static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
765 {
766         map_word oldd, curd;
767
768         oldd = map_read(map, addr);
769         curd = map_read(map, addr);
770
771         return  map_word_equal(map, oldd, curd) &&
772                 map_word_equal(map, curd, expected);
773 }
774
775 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
776 {
777         DECLARE_WAITQUEUE(wait, current);
778         struct cfi_private *cfi = map->fldrv_priv;
779         unsigned long timeo;
780         struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
781
782  resettime:
783         timeo = jiffies + HZ;
784  retry:
785         switch (chip->state) {
786
787         case FL_STATUS:
788                 for (;;) {
789                         if (chip_ready(map, adr))
790                                 break;
791
792                         if (time_after(jiffies, timeo)) {
793                                 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
794                                 return -EIO;
795                         }
796                         mutex_unlock(&chip->mutex);
797                         cfi_udelay(1);
798                         mutex_lock(&chip->mutex);
799                         /* Someone else might have been playing with it. */
800                         goto retry;
801                 }
802
803         case FL_READY:
804         case FL_CFI_QUERY:
805         case FL_JEDEC_QUERY:
806                 return 0;
807
808         case FL_ERASING:
809                 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
810                     !(mode == FL_READY || mode == FL_POINT ||
811                     (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
812                         goto sleep;
813
814                 /* Do not allow suspend iff read/write to EB address */
815                 if ((adr & chip->in_progress_block_mask) ==
816                     chip->in_progress_block_addr)
817                         goto sleep;
818
819                 /* Erase suspend */
820                 /* It's harmless to issue the Erase-Suspend and Erase-Resume
821                  * commands when the erase algorithm isn't in progress. */
822                 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
823                 chip->oldstate = FL_ERASING;
824                 chip->state = FL_ERASE_SUSPENDING;
825                 chip->erase_suspended = 1;
826                 for (;;) {
827                         if (chip_ready(map, adr))
828                                 break;
829
830                         if (time_after(jiffies, timeo)) {
831                                 /* Should have suspended the erase by now.
832                                  * Send an Erase-Resume command as either
833                                  * there was an error (so leave the erase
834                                  * routine to recover from it) or we trying to
835                                  * use the erase-in-progress sector. */
836                                 put_chip(map, chip, adr);
837                                 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
838                                 return -EIO;
839                         }
840
841                         mutex_unlock(&chip->mutex);
842                         cfi_udelay(1);
843                         mutex_lock(&chip->mutex);
844                         /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
845                            So we can just loop here. */
846                 }
847                 chip->state = FL_READY;
848                 return 0;
849
850         case FL_XIP_WHILE_ERASING:
851                 if (mode != FL_READY && mode != FL_POINT &&
852                     (!cfip || !(cfip->EraseSuspend&2)))
853                         goto sleep;
854                 chip->oldstate = chip->state;
855                 chip->state = FL_READY;
856                 return 0;
857
858         case FL_SHUTDOWN:
859                 /* The machine is rebooting */
860                 return -EIO;
861
862         case FL_POINT:
863                 /* Only if there's no operation suspended... */
864                 if (mode == FL_READY && chip->oldstate == FL_READY)
865                         return 0;
866
867         default:
868         sleep:
869                 set_current_state(TASK_UNINTERRUPTIBLE);
870                 add_wait_queue(&chip->wq, &wait);
871                 mutex_unlock(&chip->mutex);
872                 schedule();
873                 remove_wait_queue(&chip->wq, &wait);
874                 mutex_lock(&chip->mutex);
875                 goto resettime;
876         }
877 }
878
879
880 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
881 {
882         struct cfi_private *cfi = map->fldrv_priv;
883
884         switch(chip->oldstate) {
885         case FL_ERASING:
886                 cfi_fixup_m29ew_erase_suspend(map,
887                         chip->in_progress_block_addr);
888                 map_write(map, cfi->sector_erase_cmd, chip->in_progress_block_addr);
889                 cfi_fixup_m29ew_delay_after_resume(cfi);
890                 chip->oldstate = FL_READY;
891                 chip->state = FL_ERASING;
892                 break;
893
894         case FL_XIP_WHILE_ERASING:
895                 chip->state = chip->oldstate;
896                 chip->oldstate = FL_READY;
897                 break;
898
899         case FL_READY:
900         case FL_STATUS:
901                 break;
902         default:
903                 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
904         }
905         wake_up(&chip->wq);
906 }
907
908 #ifdef CONFIG_MTD_XIP
909
910 /*
911  * No interrupt what so ever can be serviced while the flash isn't in array
912  * mode.  This is ensured by the xip_disable() and xip_enable() functions
913  * enclosing any code path where the flash is known not to be in array mode.
914  * And within a XIP disabled code path, only functions marked with __xipram
915  * may be called and nothing else (it's a good thing to inspect generated
916  * assembly to make sure inline functions were actually inlined and that gcc
917  * didn't emit calls to its own support functions). Also configuring MTD CFI
918  * support to a single buswidth and a single interleave is also recommended.
919  */
920
921 static void xip_disable(struct map_info *map, struct flchip *chip,
922                         unsigned long adr)
923 {
924         /* TODO: chips with no XIP use should ignore and return */
925         (void) map_read(map, adr); /* ensure mmu mapping is up to date */
926         local_irq_disable();
927 }
928
929 static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
930                                 unsigned long adr)
931 {
932         struct cfi_private *cfi = map->fldrv_priv;
933
934         if (chip->state != FL_POINT && chip->state != FL_READY) {
935                 map_write(map, CMD(0xf0), adr);
936                 chip->state = FL_READY;
937         }
938         (void) map_read(map, adr);
939         xip_iprefetch();
940         local_irq_enable();
941 }
942
943 /*
944  * When a delay is required for the flash operation to complete, the
945  * xip_udelay() function is polling for both the given timeout and pending
946  * (but still masked) hardware interrupts.  Whenever there is an interrupt
947  * pending then the flash erase operation is suspended, array mode restored
948  * and interrupts unmasked.  Task scheduling might also happen at that
949  * point.  The CPU eventually returns from the interrupt or the call to
950  * schedule() and the suspended flash operation is resumed for the remaining
951  * of the delay period.
952  *
953  * Warning: this function _will_ fool interrupt latency tracing tools.
954  */
955
956 static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
957                                 unsigned long adr, int usec)
958 {
959         struct cfi_private *cfi = map->fldrv_priv;
960         struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
961         map_word status, OK = CMD(0x80);
962         unsigned long suspended, start = xip_currtime();
963         flstate_t oldstate;
964
965         do {
966                 cpu_relax();
967                 if (xip_irqpending() && extp &&
968                     ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
969                     (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
970                         /*
971                          * Let's suspend the erase operation when supported.
972                          * Note that we currently don't try to suspend
973                          * interleaved chips if there is already another
974                          * operation suspended (imagine what happens
975                          * when one chip was already done with the current
976                          * operation while another chip suspended it, then
977                          * we resume the whole thing at once).  Yes, it
978                          * can happen!
979                          */
980                         map_write(map, CMD(0xb0), adr);
981                         usec -= xip_elapsed_since(start);
982                         suspended = xip_currtime();
983                         do {
984                                 if (xip_elapsed_since(suspended) > 100000) {
985                                         /*
986                                          * The chip doesn't want to suspend
987                                          * after waiting for 100 msecs.
988                                          * This is a critical error but there
989                                          * is not much we can do here.
990                                          */
991                                         return;
992                                 }
993                                 status = map_read(map, adr);
994                         } while (!map_word_andequal(map, status, OK, OK));
995
996                         /* Suspend succeeded */
997                         oldstate = chip->state;
998                         if (!map_word_bitsset(map, status, CMD(0x40)))
999                                 break;
1000                         chip->state = FL_XIP_WHILE_ERASING;
1001                         chip->erase_suspended = 1;
1002                         map_write(map, CMD(0xf0), adr);
1003                         (void) map_read(map, adr);
1004                         xip_iprefetch();
1005                         local_irq_enable();
1006                         mutex_unlock(&chip->mutex);
1007                         xip_iprefetch();
1008                         cond_resched();
1009
1010                         /*
1011                          * We're back.  However someone else might have
1012                          * decided to go write to the chip if we are in
1013                          * a suspended erase state.  If so let's wait
1014                          * until it's done.
1015                          */
1016                         mutex_lock(&chip->mutex);
1017                         while (chip->state != FL_XIP_WHILE_ERASING) {
1018                                 DECLARE_WAITQUEUE(wait, current);
1019                                 set_current_state(TASK_UNINTERRUPTIBLE);
1020                                 add_wait_queue(&chip->wq, &wait);
1021                                 mutex_unlock(&chip->mutex);
1022                                 schedule();
1023                                 remove_wait_queue(&chip->wq, &wait);
1024                                 mutex_lock(&chip->mutex);
1025                         }
1026                         /* Disallow XIP again */
1027                         local_irq_disable();
1028
1029                         /* Correct Erase Suspend Hangups for M29EW */
1030                         cfi_fixup_m29ew_erase_suspend(map, adr);
1031                         /* Resume the write or erase operation */
1032                         map_write(map, cfi->sector_erase_cmd, adr);
1033                         chip->state = oldstate;
1034                         start = xip_currtime();
1035                 } else if (usec >= 1000000/HZ) {
1036                         /*
1037                          * Try to save on CPU power when waiting delay
1038                          * is at least a system timer tick period.
1039                          * No need to be extremely accurate here.
1040                          */
1041                         xip_cpu_idle();
1042                 }
1043                 status = map_read(map, adr);
1044         } while (!map_word_andequal(map, status, OK, OK)
1045                  && xip_elapsed_since(start) < usec);
1046 }
1047
1048 #define UDELAY(map, chip, adr, usec)  xip_udelay(map, chip, adr, usec)
1049
1050 /*
1051  * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1052  * the flash is actively programming or erasing since we have to poll for
1053  * the operation to complete anyway.  We can't do that in a generic way with
1054  * a XIP setup so do it before the actual flash operation in this case
1055  * and stub it out from INVALIDATE_CACHE_UDELAY.
1056  */
1057 #define XIP_INVAL_CACHED_RANGE(map, from, size)  \
1058         INVALIDATE_CACHED_RANGE(map, from, size)
1059
1060 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec)  \
1061         UDELAY(map, chip, adr, usec)
1062
1063 /*
1064  * Extra notes:
1065  *
1066  * Activating this XIP support changes the way the code works a bit.  For
1067  * example the code to suspend the current process when concurrent access
1068  * happens is never executed because xip_udelay() will always return with the
1069  * same chip state as it was entered with.  This is why there is no care for
1070  * the presence of add_wait_queue() or schedule() calls from within a couple
1071  * xip_disable()'d  areas of code, like in do_erase_oneblock for example.
1072  * The queueing and scheduling are always happening within xip_udelay().
1073  *
1074  * Similarly, get_chip() and put_chip() just happen to always be executed
1075  * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1076  * is in array mode, therefore never executing many cases therein and not
1077  * causing any problem with XIP.
1078  */
1079
1080 #else
1081
1082 #define xip_disable(map, chip, adr)
1083 #define xip_enable(map, chip, adr)
1084 #define XIP_INVAL_CACHED_RANGE(x...)
1085
1086 #define UDELAY(map, chip, adr, usec)  \
1087 do {  \
1088         mutex_unlock(&chip->mutex);  \
1089         cfi_udelay(usec);  \
1090         mutex_lock(&chip->mutex);  \
1091 } while (0)
1092
1093 #define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec)  \
1094 do {  \
1095         mutex_unlock(&chip->mutex);  \
1096         INVALIDATE_CACHED_RANGE(map, adr, len);  \
1097         cfi_udelay(usec);  \
1098         mutex_lock(&chip->mutex);  \
1099 } while (0)
1100
1101 #endif
1102
1103 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1104 {
1105         unsigned long cmd_addr;
1106         struct cfi_private *cfi = map->fldrv_priv;
1107         int ret;
1108
1109         adr += chip->start;
1110
1111         /* Ensure cmd read/writes are aligned. */
1112         cmd_addr = adr & ~(map_bankwidth(map)-1);
1113
1114         mutex_lock(&chip->mutex);
1115         ret = get_chip(map, chip, cmd_addr, FL_READY);
1116         if (ret) {
1117                 mutex_unlock(&chip->mutex);
1118                 return ret;
1119         }
1120
1121         if (chip->state != FL_POINT && chip->state != FL_READY) {
1122                 map_write(map, CMD(0xf0), cmd_addr);
1123                 chip->state = FL_READY;
1124         }
1125
1126         map_copy_from(map, buf, adr, len);
1127
1128         put_chip(map, chip, cmd_addr);
1129
1130         mutex_unlock(&chip->mutex);
1131         return 0;
1132 }
1133
1134
1135 static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1136 {
1137         struct map_info *map = mtd->priv;
1138         struct cfi_private *cfi = map->fldrv_priv;
1139         unsigned long ofs;
1140         int chipnum;
1141         int ret = 0;
1142
1143         /* ofs: offset within the first chip that the first read should start */
1144         chipnum = (from >> cfi->chipshift);
1145         ofs = from - (chipnum <<  cfi->chipshift);
1146
1147         while (len) {
1148                 unsigned long thislen;
1149
1150                 if (chipnum >= cfi->numchips)
1151                         break;
1152
1153                 if ((len + ofs -1) >> cfi->chipshift)
1154                         thislen = (1<<cfi->chipshift) - ofs;
1155                 else
1156                         thislen = len;
1157
1158                 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1159                 if (ret)
1160                         break;
1161
1162                 *retlen += thislen;
1163                 len -= thislen;
1164                 buf += thislen;
1165
1166                 ofs = 0;
1167                 chipnum++;
1168         }
1169         return ret;
1170 }
1171
1172 typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
1173                         loff_t adr, size_t len, u_char *buf, size_t grouplen);
1174
1175 static inline void otp_enter(struct map_info *map, struct flchip *chip,
1176                              loff_t adr, size_t len)
1177 {
1178         struct cfi_private *cfi = map->fldrv_priv;
1179
1180         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1181                          cfi->device_type, NULL);
1182         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1183                          cfi->device_type, NULL);
1184         cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi,
1185                          cfi->device_type, NULL);
1186
1187         INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1188 }
1189
1190 static inline void otp_exit(struct map_info *map, struct flchip *chip,
1191                             loff_t adr, size_t len)
1192 {
1193         struct cfi_private *cfi = map->fldrv_priv;
1194
1195         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1196                          cfi->device_type, NULL);
1197         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1198                          cfi->device_type, NULL);
1199         cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi,
1200                          cfi->device_type, NULL);
1201         cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi,
1202                          cfi->device_type, NULL);
1203
1204         INVALIDATE_CACHED_RANGE(map, chip->start + adr, len);
1205 }
1206
1207 static inline int do_read_secsi_onechip(struct map_info *map,
1208                                         struct flchip *chip, loff_t adr,
1209                                         size_t len, u_char *buf,
1210                                         size_t grouplen)
1211 {
1212         DECLARE_WAITQUEUE(wait, current);
1213         unsigned long timeo = jiffies + HZ;
1214
1215  retry:
1216         mutex_lock(&chip->mutex);
1217
1218         if (chip->state != FL_READY){
1219                 set_current_state(TASK_UNINTERRUPTIBLE);
1220                 add_wait_queue(&chip->wq, &wait);
1221
1222                 mutex_unlock(&chip->mutex);
1223
1224                 schedule();
1225                 remove_wait_queue(&chip->wq, &wait);
1226                 timeo = jiffies + HZ;
1227
1228                 goto retry;
1229         }
1230
1231         adr += chip->start;
1232
1233         chip->state = FL_READY;
1234
1235         otp_enter(map, chip, adr, len);
1236         map_copy_from(map, buf, adr, len);
1237         otp_exit(map, chip, adr, len);
1238
1239         wake_up(&chip->wq);
1240         mutex_unlock(&chip->mutex);
1241
1242         return 0;
1243 }
1244
1245 static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1246 {
1247         struct map_info *map = mtd->priv;
1248         struct cfi_private *cfi = map->fldrv_priv;
1249         unsigned long ofs;
1250         int chipnum;
1251         int ret = 0;
1252
1253         /* ofs: offset within the first chip that the first read should start */
1254         /* 8 secsi bytes per chip */
1255         chipnum=from>>3;
1256         ofs=from & 7;
1257
1258         while (len) {
1259                 unsigned long thislen;
1260
1261                 if (chipnum >= cfi->numchips)
1262                         break;
1263
1264                 if ((len + ofs -1) >> 3)
1265                         thislen = (1<<3) - ofs;
1266                 else
1267                         thislen = len;
1268
1269                 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs,
1270                                             thislen, buf, 0);
1271                 if (ret)
1272                         break;
1273
1274                 *retlen += thislen;
1275                 len -= thislen;
1276                 buf += thislen;
1277
1278                 ofs = 0;
1279                 chipnum++;
1280         }
1281         return ret;
1282 }
1283
1284 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1285                                      unsigned long adr, map_word datum,
1286                                      int mode);
1287
1288 static int do_otp_write(struct map_info *map, struct flchip *chip, loff_t adr,
1289                         size_t len, u_char *buf, size_t grouplen)
1290 {
1291         int ret;
1292         while (len) {
1293                 unsigned long bus_ofs = adr & ~(map_bankwidth(map)-1);
1294                 int gap = adr - bus_ofs;
1295                 int n = min_t(int, len, map_bankwidth(map) - gap);
1296                 map_word datum = map_word_ff(map);
1297
1298                 if (n != map_bankwidth(map)) {
1299                         /* partial write of a word, load old contents */
1300                         otp_enter(map, chip, bus_ofs, map_bankwidth(map));
1301                         datum = map_read(map, bus_ofs);
1302                         otp_exit(map, chip, bus_ofs, map_bankwidth(map));
1303                 }
1304
1305                 datum = map_word_load_partial(map, datum, buf, gap, n);
1306                 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
1307                 if (ret)
1308                         return ret;
1309
1310                 adr += n;
1311                 buf += n;
1312                 len -= n;
1313         }
1314
1315         return 0;
1316 }
1317
1318 static int do_otp_lock(struct map_info *map, struct flchip *chip, loff_t adr,
1319                        size_t len, u_char *buf, size_t grouplen)
1320 {
1321         struct cfi_private *cfi = map->fldrv_priv;
1322         uint8_t lockreg;
1323         unsigned long timeo;
1324         int ret;
1325
1326         /* make sure area matches group boundaries */
1327         if ((adr != 0) || (len != grouplen))
1328                 return -EINVAL;
1329
1330         mutex_lock(&chip->mutex);
1331         ret = get_chip(map, chip, chip->start, FL_LOCKING);
1332         if (ret) {
1333                 mutex_unlock(&chip->mutex);
1334                 return ret;
1335         }
1336         chip->state = FL_LOCKING;
1337
1338         /* Enter lock register command */
1339         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1340                          cfi->device_type, NULL);
1341         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1342                          cfi->device_type, NULL);
1343         cfi_send_gen_cmd(0x40, cfi->addr_unlock1, chip->start, map, cfi,
1344                          cfi->device_type, NULL);
1345
1346         /* read lock register */
1347         lockreg = cfi_read_query(map, 0);
1348
1349         /* set bit 0 to protect extended memory block */
1350         lockreg &= ~0x01;
1351
1352         /* set bit 0 to protect extended memory block */
1353         /* write lock register */
1354         map_write(map, CMD(0xA0), chip->start);
1355         map_write(map, CMD(lockreg), chip->start);
1356
1357         /* wait for chip to become ready */
1358         timeo = jiffies + msecs_to_jiffies(2);
1359         for (;;) {
1360                 if (chip_ready(map, adr))
1361                         break;
1362
1363                 if (time_after(jiffies, timeo)) {
1364                         pr_err("Waiting for chip to be ready timed out.\n");
1365                         ret = -EIO;
1366                         break;
1367                 }
1368                 UDELAY(map, chip, 0, 1);
1369         }
1370
1371         /* exit protection commands */
1372         map_write(map, CMD(0x90), chip->start);
1373         map_write(map, CMD(0x00), chip->start);
1374
1375         chip->state = FL_READY;
1376         put_chip(map, chip, chip->start);
1377         mutex_unlock(&chip->mutex);
1378
1379         return ret;
1380 }
1381
1382 static int cfi_amdstd_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
1383                                size_t *retlen, u_char *buf,
1384                                otp_op_t action, int user_regs)
1385 {
1386         struct map_info *map = mtd->priv;
1387         struct cfi_private *cfi = map->fldrv_priv;
1388         int ofs_factor = cfi->interleave * cfi->device_type;
1389         unsigned long base;
1390         int chipnum;
1391         struct flchip *chip;
1392         uint8_t otp, lockreg;
1393         int ret;
1394
1395         size_t user_size, factory_size, otpsize;
1396         loff_t user_offset, factory_offset, otpoffset;
1397         int user_locked = 0, otplocked;
1398
1399         *retlen = 0;
1400
1401         for (chipnum = 0; chipnum < cfi->numchips; chipnum++) {
1402                 chip = &cfi->chips[chipnum];
1403                 factory_size = 0;
1404                 user_size = 0;
1405
1406                 /* Micron M29EW family */
1407                 if (is_m29ew(cfi)) {
1408                         base = chip->start;
1409
1410                         /* check whether secsi area is factory locked
1411                            or user lockable */
1412                         mutex_lock(&chip->mutex);
1413                         ret = get_chip(map, chip, base, FL_CFI_QUERY);
1414                         if (ret) {
1415                                 mutex_unlock(&chip->mutex);
1416                                 return ret;
1417                         }
1418                         cfi_qry_mode_on(base, map, cfi);
1419                         otp = cfi_read_query(map, base + 0x3 * ofs_factor);
1420                         cfi_qry_mode_off(base, map, cfi);
1421                         put_chip(map, chip, base);
1422                         mutex_unlock(&chip->mutex);
1423
1424                         if (otp & 0x80) {
1425                                 /* factory locked */
1426                                 factory_offset = 0;
1427                                 factory_size = 0x100;
1428                         } else {
1429                                 /* customer lockable */
1430                                 user_offset = 0;
1431                                 user_size = 0x100;
1432
1433                                 mutex_lock(&chip->mutex);
1434                                 ret = get_chip(map, chip, base, FL_LOCKING);
1435                                 if (ret) {
1436                                         mutex_unlock(&chip->mutex);
1437                                         return ret;
1438                                 }
1439
1440                                 /* Enter lock register command */
1441                                 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1,
1442                                                  chip->start, map, cfi,
1443                                                  cfi->device_type, NULL);
1444                                 cfi_send_gen_cmd(0x55, cfi->addr_unlock2,
1445                                                  chip->start, map, cfi,
1446                                                  cfi->device_type, NULL);
1447                                 cfi_send_gen_cmd(0x40, cfi->addr_unlock1,
1448                                                  chip->start, map, cfi,
1449                                                  cfi->device_type, NULL);
1450                                 /* read lock register */
1451                                 lockreg = cfi_read_query(map, 0);
1452                                 /* exit protection commands */
1453                                 map_write(map, CMD(0x90), chip->start);
1454                                 map_write(map, CMD(0x00), chip->start);
1455                                 put_chip(map, chip, chip->start);
1456                                 mutex_unlock(&chip->mutex);
1457
1458                                 user_locked = ((lockreg & 0x01) == 0x00);
1459                         }
1460                 }
1461
1462                 otpsize = user_regs ? user_size : factory_size;
1463                 if (!otpsize)
1464                         continue;
1465                 otpoffset = user_regs ? user_offset : factory_offset;
1466                 otplocked = user_regs ? user_locked : 1;
1467
1468                 if (!action) {
1469                         /* return otpinfo */
1470                         struct otp_info *otpinfo;
1471                         len -= sizeof(*otpinfo);
1472                         if (len <= 0)
1473                                 return -ENOSPC;
1474                         otpinfo = (struct otp_info *)buf;
1475                         otpinfo->start = from;
1476                         otpinfo->length = otpsize;
1477                         otpinfo->locked = otplocked;
1478                         buf += sizeof(*otpinfo);
1479                         *retlen += sizeof(*otpinfo);
1480                         from += otpsize;
1481                 } else if ((from < otpsize) && (len > 0)) {
1482                         size_t size;
1483                         size = (len < otpsize - from) ? len : otpsize - from;
1484                         ret = action(map, chip, otpoffset + from, size, buf,
1485                                      otpsize);
1486                         if (ret < 0)
1487                                 return ret;
1488
1489                         buf += size;
1490                         len -= size;
1491                         *retlen += size;
1492                         from = 0;
1493                 } else {
1494                         from -= otpsize;
1495                 }
1496         }
1497         return 0;
1498 }
1499
1500 static int cfi_amdstd_get_fact_prot_info(struct mtd_info *mtd, size_t len,
1501                                          size_t *retlen, struct otp_info *buf)
1502 {
1503         return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1504                                    NULL, 0);
1505 }
1506
1507 static int cfi_amdstd_get_user_prot_info(struct mtd_info *mtd, size_t len,
1508                                          size_t *retlen, struct otp_info *buf)
1509 {
1510         return cfi_amdstd_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
1511                                    NULL, 1);
1512 }
1513
1514 static int cfi_amdstd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
1515                                          size_t len, size_t *retlen,
1516                                          u_char *buf)
1517 {
1518         return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1519                                    buf, do_read_secsi_onechip, 0);
1520 }
1521
1522 static int cfi_amdstd_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
1523                                          size_t len, size_t *retlen,
1524                                          u_char *buf)
1525 {
1526         return cfi_amdstd_otp_walk(mtd, from, len, retlen,
1527                                    buf, do_read_secsi_onechip, 1);
1528 }
1529
1530 static int cfi_amdstd_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
1531                                           size_t len, size_t *retlen,
1532                                           u_char *buf)
1533 {
1534         return cfi_amdstd_otp_walk(mtd, from, len, retlen, buf,
1535                                    do_otp_write, 1);
1536 }
1537
1538 static int cfi_amdstd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
1539                                          size_t len)
1540 {
1541         size_t retlen;
1542         return cfi_amdstd_otp_walk(mtd, from, len, &retlen, NULL,
1543                                    do_otp_lock, 1);
1544 }
1545
1546 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1547                                      unsigned long adr, map_word datum,
1548                                      int mode)
1549 {
1550         struct cfi_private *cfi = map->fldrv_priv;
1551         unsigned long timeo = jiffies + HZ;
1552         /*
1553          * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1554          * have a max write time of a few hundreds usec). However, we should
1555          * use the maximum timeout value given by the chip at probe time
1556          * instead.  Unfortunately, struct flchip does have a field for
1557          * maximum timeout, only for typical which can be far too short
1558          * depending of the conditions.  The ' + 1' is to avoid having a
1559          * timeout of 0 jiffies if HZ is smaller than 1000.
1560          */
1561         unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1562         int ret = 0;
1563         map_word oldd;
1564         int retry_cnt = 0;
1565
1566         adr += chip->start;
1567
1568         mutex_lock(&chip->mutex);
1569         ret = get_chip(map, chip, adr, mode);
1570         if (ret) {
1571                 mutex_unlock(&chip->mutex);
1572                 return ret;
1573         }
1574
1575         pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1576                __func__, adr, datum.x[0] );
1577
1578         if (mode == FL_OTP_WRITE)
1579                 otp_enter(map, chip, adr, map_bankwidth(map));
1580
1581         /*
1582          * Check for a NOP for the case when the datum to write is already
1583          * present - it saves time and works around buggy chips that corrupt
1584          * data at other locations when 0xff is written to a location that
1585          * already contains 0xff.
1586          */
1587         oldd = map_read(map, adr);
1588         if (map_word_equal(map, oldd, datum)) {
1589                 pr_debug("MTD %s(): NOP\n",
1590                        __func__);
1591                 goto op_done;
1592         }
1593
1594         XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1595         ENABLE_VPP(map);
1596         xip_disable(map, chip, adr);
1597
1598  retry:
1599         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1600         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1601         cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1602         map_write(map, datum, adr);
1603         chip->state = mode;
1604
1605         INVALIDATE_CACHE_UDELAY(map, chip,
1606                                 adr, map_bankwidth(map),
1607                                 chip->word_write_time);
1608
1609         /* See comment above for timeout value. */
1610         timeo = jiffies + uWriteTimeout;
1611         for (;;) {
1612                 if (chip->state != mode) {
1613                         /* Someone's suspended the write. Sleep */
1614                         DECLARE_WAITQUEUE(wait, current);
1615
1616                         set_current_state(TASK_UNINTERRUPTIBLE);
1617                         add_wait_queue(&chip->wq, &wait);
1618                         mutex_unlock(&chip->mutex);
1619                         schedule();
1620                         remove_wait_queue(&chip->wq, &wait);
1621                         timeo = jiffies + (HZ / 2); /* FIXME */
1622                         mutex_lock(&chip->mutex);
1623                         continue;
1624                 }
1625
1626                 /*
1627                  * We check "time_after" and "!chip_good" before checking
1628                  * "chip_good" to avoid the failure due to scheduling.
1629                  */
1630                 if (time_after(jiffies, timeo) && !chip_good(map, adr, datum)) {
1631                         xip_enable(map, chip, adr);
1632                         printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
1633                         xip_disable(map, chip, adr);
1634                         ret = -EIO;
1635                         break;
1636                 }
1637
1638                 if (chip_good(map, adr, datum))
1639                         break;
1640
1641                 /* Latency issues. Drop the lock, wait a while and retry */
1642                 UDELAY(map, chip, adr, 1);
1643         }
1644
1645         /* Did we succeed? */
1646         if (ret) {
1647                 /* reset on all failures. */
1648                 map_write( map, CMD(0xF0), chip->start );
1649                 /* FIXME - should have reset delay before continuing */
1650
1651                 if (++retry_cnt <= MAX_RETRIES) {
1652                         ret = 0;
1653                         goto retry;
1654                 }
1655         }
1656         xip_enable(map, chip, adr);
1657  op_done:
1658         if (mode == FL_OTP_WRITE)
1659                 otp_exit(map, chip, adr, map_bankwidth(map));
1660         chip->state = FL_READY;
1661         DISABLE_VPP(map);
1662         put_chip(map, chip, adr);
1663         mutex_unlock(&chip->mutex);
1664
1665         return ret;
1666 }
1667
1668
1669 static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1670                                   size_t *retlen, const u_char *buf)
1671 {
1672         struct map_info *map = mtd->priv;
1673         struct cfi_private *cfi = map->fldrv_priv;
1674         int ret = 0;
1675         int chipnum;
1676         unsigned long ofs, chipstart;
1677         DECLARE_WAITQUEUE(wait, current);
1678
1679         chipnum = to >> cfi->chipshift;
1680         ofs = to  - (chipnum << cfi->chipshift);
1681         chipstart = cfi->chips[chipnum].start;
1682
1683         /* If it's not bus-aligned, do the first byte write */
1684         if (ofs & (map_bankwidth(map)-1)) {
1685                 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1686                 int i = ofs - bus_ofs;
1687                 int n = 0;
1688                 map_word tmp_buf;
1689
1690  retry:
1691                 mutex_lock(&cfi->chips[chipnum].mutex);
1692
1693                 if (cfi->chips[chipnum].state != FL_READY) {
1694                         set_current_state(TASK_UNINTERRUPTIBLE);
1695                         add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1696
1697                         mutex_unlock(&cfi->chips[chipnum].mutex);
1698
1699                         schedule();
1700                         remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1701                         goto retry;
1702                 }
1703
1704                 /* Load 'tmp_buf' with old contents of flash */
1705                 tmp_buf = map_read(map, bus_ofs+chipstart);
1706
1707                 mutex_unlock(&cfi->chips[chipnum].mutex);
1708
1709                 /* Number of bytes to copy from buffer */
1710                 n = min_t(int, len, map_bankwidth(map)-i);
1711
1712                 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1713
1714                 ret = do_write_oneword(map, &cfi->chips[chipnum],
1715                                        bus_ofs, tmp_buf, FL_WRITING);
1716                 if (ret)
1717                         return ret;
1718
1719                 ofs += n;
1720                 buf += n;
1721                 (*retlen) += n;
1722                 len -= n;
1723
1724                 if (ofs >> cfi->chipshift) {
1725                         chipnum ++;
1726                         ofs = 0;
1727                         if (chipnum == cfi->numchips)
1728                                 return 0;
1729                 }
1730         }
1731
1732         /* We are now aligned, write as much as possible */
1733         while(len >= map_bankwidth(map)) {
1734                 map_word datum;
1735
1736                 datum = map_word_load(map, buf);
1737
1738                 ret = do_write_oneword(map, &cfi->chips[chipnum],
1739                                        ofs, datum, FL_WRITING);
1740                 if (ret)
1741                         return ret;
1742
1743                 ofs += map_bankwidth(map);
1744                 buf += map_bankwidth(map);
1745                 (*retlen) += map_bankwidth(map);
1746                 len -= map_bankwidth(map);
1747
1748                 if (ofs >> cfi->chipshift) {
1749                         chipnum ++;
1750                         ofs = 0;
1751                         if (chipnum == cfi->numchips)
1752                                 return 0;
1753                         chipstart = cfi->chips[chipnum].start;
1754                 }
1755         }
1756
1757         /* Write the trailing bytes if any */
1758         if (len & (map_bankwidth(map)-1)) {
1759                 map_word tmp_buf;
1760
1761  retry1:
1762                 mutex_lock(&cfi->chips[chipnum].mutex);
1763
1764                 if (cfi->chips[chipnum].state != FL_READY) {
1765                         set_current_state(TASK_UNINTERRUPTIBLE);
1766                         add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1767
1768                         mutex_unlock(&cfi->chips[chipnum].mutex);
1769
1770                         schedule();
1771                         remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1772                         goto retry1;
1773                 }
1774
1775                 tmp_buf = map_read(map, ofs + chipstart);
1776
1777                 mutex_unlock(&cfi->chips[chipnum].mutex);
1778
1779                 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
1780
1781                 ret = do_write_oneword(map, &cfi->chips[chipnum],
1782                                        ofs, tmp_buf, FL_WRITING);
1783                 if (ret)
1784                         return ret;
1785
1786                 (*retlen) += len;
1787         }
1788
1789         return 0;
1790 }
1791
1792
1793 /*
1794  * FIXME: interleaved mode not tested, and probably not supported!
1795  */
1796 static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
1797                                     unsigned long adr, const u_char *buf,
1798                                     int len)
1799 {
1800         struct cfi_private *cfi = map->fldrv_priv;
1801         unsigned long timeo = jiffies + HZ;
1802         /*
1803          * Timeout is calculated according to CFI data, if available.
1804          * See more comments in cfi_cmdset_0002().
1805          */
1806         unsigned long uWriteTimeout =
1807                                 usecs_to_jiffies(chip->buffer_write_time_max);
1808         int ret = -EIO;
1809         unsigned long cmd_adr;
1810         int z, words;
1811         map_word datum;
1812
1813         adr += chip->start;
1814         cmd_adr = adr;
1815
1816         mutex_lock(&chip->mutex);
1817         ret = get_chip(map, chip, adr, FL_WRITING);
1818         if (ret) {
1819                 mutex_unlock(&chip->mutex);
1820                 return ret;
1821         }
1822
1823         datum = map_word_load(map, buf);
1824
1825         pr_debug("MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1826                __func__, adr, datum.x[0] );
1827
1828         XIP_INVAL_CACHED_RANGE(map, adr, len);
1829         ENABLE_VPP(map);
1830         xip_disable(map, chip, cmd_adr);
1831
1832         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1833         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1834
1835         /* Write Buffer Load */
1836         map_write(map, CMD(0x25), cmd_adr);
1837
1838         chip->state = FL_WRITING_TO_BUFFER;
1839
1840         /* Write length of data to come */
1841         words = len / map_bankwidth(map);
1842         map_write(map, CMD(words - 1), cmd_adr);
1843         /* Write data */
1844         z = 0;
1845         while(z < words * map_bankwidth(map)) {
1846                 datum = map_word_load(map, buf);
1847                 map_write(map, datum, adr + z);
1848
1849                 z += map_bankwidth(map);
1850                 buf += map_bankwidth(map);
1851         }
1852         z -= map_bankwidth(map);
1853
1854         adr += z;
1855
1856         /* Write Buffer Program Confirm: GO GO GO */
1857         map_write(map, CMD(0x29), cmd_adr);
1858         chip->state = FL_WRITING;
1859
1860         INVALIDATE_CACHE_UDELAY(map, chip,
1861                                 adr, map_bankwidth(map),
1862                                 chip->word_write_time);
1863
1864         timeo = jiffies + uWriteTimeout;
1865
1866         for (;;) {
1867                 if (chip->state != FL_WRITING) {
1868                         /* Someone's suspended the write. Sleep */
1869                         DECLARE_WAITQUEUE(wait, current);
1870
1871                         set_current_state(TASK_UNINTERRUPTIBLE);
1872                         add_wait_queue(&chip->wq, &wait);
1873                         mutex_unlock(&chip->mutex);
1874                         schedule();
1875                         remove_wait_queue(&chip->wq, &wait);
1876                         timeo = jiffies + (HZ / 2); /* FIXME */
1877                         mutex_lock(&chip->mutex);
1878                         continue;
1879                 }
1880
1881                 /*
1882                  * We check "time_after" and "!chip_good" before checking "chip_good" to avoid
1883                  * the failure due to scheduling.
1884                  */
1885                 if (time_after(jiffies, timeo) && !chip_good(map, adr, datum))
1886                         break;
1887
1888                 if (chip_good(map, adr, datum)) {
1889                         xip_enable(map, chip, adr);
1890                         goto op_done;
1891                 }
1892
1893                 /* Latency issues. Drop the lock, wait a while and retry */
1894                 UDELAY(map, chip, adr, 1);
1895         }
1896
1897         /*
1898          * Recovery from write-buffer programming failures requires
1899          * the write-to-buffer-reset sequence.  Since the last part
1900          * of the sequence also works as a normal reset, we can run
1901          * the same commands regardless of why we are here.
1902          * See e.g.
1903          * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf
1904          */
1905         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1906                          cfi->device_type, NULL);
1907         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1908                          cfi->device_type, NULL);
1909         cfi_send_gen_cmd(0xF0, cfi->addr_unlock1, chip->start, map, cfi,
1910                          cfi->device_type, NULL);
1911         xip_enable(map, chip, adr);
1912         /* FIXME - should have reset delay before continuing */
1913
1914         printk(KERN_WARNING "MTD %s(): software timeout, address:0x%.8lx.\n",
1915                __func__, adr);
1916
1917         ret = -EIO;
1918  op_done:
1919         chip->state = FL_READY;
1920         DISABLE_VPP(map);
1921         put_chip(map, chip, adr);
1922         mutex_unlock(&chip->mutex);
1923
1924         return ret;
1925 }
1926
1927
1928 static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1929                                     size_t *retlen, const u_char *buf)
1930 {
1931         struct map_info *map = mtd->priv;
1932         struct cfi_private *cfi = map->fldrv_priv;
1933         int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1934         int ret = 0;
1935         int chipnum;
1936         unsigned long ofs;
1937
1938         chipnum = to >> cfi->chipshift;
1939         ofs = to  - (chipnum << cfi->chipshift);
1940
1941         /* If it's not bus-aligned, do the first word write */
1942         if (ofs & (map_bankwidth(map)-1)) {
1943                 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1944                 if (local_len > len)
1945                         local_len = len;
1946                 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1947                                              local_len, retlen, buf);
1948                 if (ret)
1949                         return ret;
1950                 ofs += local_len;
1951                 buf += local_len;
1952                 len -= local_len;
1953
1954                 if (ofs >> cfi->chipshift) {
1955                         chipnum ++;
1956                         ofs = 0;
1957                         if (chipnum == cfi->numchips)
1958                                 return 0;
1959                 }
1960         }
1961
1962         /* Write buffer is worth it only if more than one word to write... */
1963         while (len >= map_bankwidth(map) * 2) {
1964                 /* We must not cross write block boundaries */
1965                 int size = wbufsize - (ofs & (wbufsize-1));
1966
1967                 if (size > len)
1968                         size = len;
1969                 if (size % map_bankwidth(map))
1970                         size -= size % map_bankwidth(map);
1971
1972                 ret = do_write_buffer(map, &cfi->chips[chipnum],
1973                                       ofs, buf, size);
1974                 if (ret)
1975                         return ret;
1976
1977                 ofs += size;
1978                 buf += size;
1979                 (*retlen) += size;
1980                 len -= size;
1981
1982                 if (ofs >> cfi->chipshift) {
1983                         chipnum ++;
1984                         ofs = 0;
1985                         if (chipnum == cfi->numchips)
1986                                 return 0;
1987                 }
1988         }
1989
1990         if (len) {
1991                 size_t retlen_dregs = 0;
1992
1993                 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1994                                              len, &retlen_dregs, buf);
1995
1996                 *retlen += retlen_dregs;
1997                 return ret;
1998         }
1999
2000         return 0;
2001 }
2002
2003 /*
2004  * Wait for the flash chip to become ready to write data
2005  *
2006  * This is only called during the panic_write() path. When panic_write()
2007  * is called, the kernel is in the process of a panic, and will soon be
2008  * dead. Therefore we don't take any locks, and attempt to get access
2009  * to the chip as soon as possible.
2010  */
2011 static int cfi_amdstd_panic_wait(struct map_info *map, struct flchip *chip,
2012                                  unsigned long adr)
2013 {
2014         struct cfi_private *cfi = map->fldrv_priv;
2015         int retries = 10;
2016         int i;
2017
2018         /*
2019          * If the driver thinks the chip is idle, and no toggle bits
2020          * are changing, then the chip is actually idle for sure.
2021          */
2022         if (chip->state == FL_READY && chip_ready(map, adr))
2023                 return 0;
2024
2025         /*
2026          * Try several times to reset the chip and then wait for it
2027          * to become idle. The upper limit of a few milliseconds of
2028          * delay isn't a big problem: the kernel is dying anyway. It
2029          * is more important to save the messages.
2030          */
2031         while (retries > 0) {
2032                 const unsigned long timeo = (HZ / 1000) + 1;
2033
2034                 /* send the reset command */
2035                 map_write(map, CMD(0xF0), chip->start);
2036
2037                 /* wait for the chip to become ready */
2038                 for (i = 0; i < jiffies_to_usecs(timeo); i++) {
2039                         if (chip_ready(map, adr))
2040                                 return 0;
2041
2042                         udelay(1);
2043                 }
2044
2045                 retries--;
2046         }
2047
2048         /* the chip never became ready */
2049         return -EBUSY;
2050 }
2051
2052 /*
2053  * Write out one word of data to a single flash chip during a kernel panic
2054  *
2055  * This is only called during the panic_write() path. When panic_write()
2056  * is called, the kernel is in the process of a panic, and will soon be
2057  * dead. Therefore we don't take any locks, and attempt to get access
2058  * to the chip as soon as possible.
2059  *
2060  * The implementation of this routine is intentionally similar to
2061  * do_write_oneword(), in order to ease code maintenance.
2062  */
2063 static int do_panic_write_oneword(struct map_info *map, struct flchip *chip,
2064                                   unsigned long adr, map_word datum)
2065 {
2066         const unsigned long uWriteTimeout = (HZ / 1000) + 1;
2067         struct cfi_private *cfi = map->fldrv_priv;
2068         int retry_cnt = 0;
2069         map_word oldd;
2070         int ret = 0;
2071         int i;
2072
2073         adr += chip->start;
2074
2075         ret = cfi_amdstd_panic_wait(map, chip, adr);
2076         if (ret)
2077                 return ret;
2078
2079         pr_debug("MTD %s(): PANIC WRITE 0x%.8lx(0x%.8lx)\n",
2080                         __func__, adr, datum.x[0]);
2081
2082         /*
2083          * Check for a NOP for the case when the datum to write is already
2084          * present - it saves time and works around buggy chips that corrupt
2085          * data at other locations when 0xff is written to a location that
2086          * already contains 0xff.
2087          */
2088         oldd = map_read(map, adr);
2089         if (map_word_equal(map, oldd, datum)) {
2090                 pr_debug("MTD %s(): NOP\n", __func__);
2091                 goto op_done;
2092         }
2093
2094         ENABLE_VPP(map);
2095
2096 retry:
2097         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2098         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2099         cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2100         map_write(map, datum, adr);
2101
2102         for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) {
2103                 if (chip_ready(map, adr))
2104                         break;
2105
2106                 udelay(1);
2107         }
2108
2109         if (!chip_good(map, adr, datum)) {
2110                 /* reset on all failures. */
2111                 map_write(map, CMD(0xF0), chip->start);
2112                 /* FIXME - should have reset delay before continuing */
2113
2114                 if (++retry_cnt <= MAX_RETRIES)
2115                         goto retry;
2116
2117                 ret = -EIO;
2118         }
2119
2120 op_done:
2121         DISABLE_VPP(map);
2122         return ret;
2123 }
2124
2125 /*
2126  * Write out some data during a kernel panic
2127  *
2128  * This is used by the mtdoops driver to save the dying messages from a
2129  * kernel which has panic'd.
2130  *
2131  * This routine ignores all of the locking used throughout the rest of the
2132  * driver, in order to ensure that the data gets written out no matter what
2133  * state this driver (and the flash chip itself) was in when the kernel crashed.
2134  *
2135  * The implementation of this routine is intentionally similar to
2136  * cfi_amdstd_write_words(), in order to ease code maintenance.
2137  */
2138 static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
2139                                   size_t *retlen, const u_char *buf)
2140 {
2141         struct map_info *map = mtd->priv;
2142         struct cfi_private *cfi = map->fldrv_priv;
2143         unsigned long ofs, chipstart;
2144         int ret = 0;
2145         int chipnum;
2146
2147         chipnum = to >> cfi->chipshift;
2148         ofs = to - (chipnum << cfi->chipshift);
2149         chipstart = cfi->chips[chipnum].start;
2150
2151         /* If it's not bus aligned, do the first byte write */
2152         if (ofs & (map_bankwidth(map) - 1)) {
2153                 unsigned long bus_ofs = ofs & ~(map_bankwidth(map) - 1);
2154                 int i = ofs - bus_ofs;
2155                 int n = 0;
2156                 map_word tmp_buf;
2157
2158                 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], bus_ofs);
2159                 if (ret)
2160                         return ret;
2161
2162                 /* Load 'tmp_buf' with old contents of flash */
2163                 tmp_buf = map_read(map, bus_ofs + chipstart);
2164
2165                 /* Number of bytes to copy from buffer */
2166                 n = min_t(int, len, map_bankwidth(map) - i);
2167
2168                 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
2169
2170                 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2171                                              bus_ofs, tmp_buf);
2172                 if (ret)
2173                         return ret;
2174
2175                 ofs += n;
2176                 buf += n;
2177                 (*retlen) += n;
2178                 len -= n;
2179
2180                 if (ofs >> cfi->chipshift) {
2181                         chipnum++;
2182                         ofs = 0;
2183                         if (chipnum == cfi->numchips)
2184                                 return 0;
2185                 }
2186         }
2187
2188         /* We are now aligned, write as much as possible */
2189         while (len >= map_bankwidth(map)) {
2190                 map_word datum;
2191
2192                 datum = map_word_load(map, buf);
2193
2194                 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2195                                              ofs, datum);
2196                 if (ret)
2197                         return ret;
2198
2199                 ofs += map_bankwidth(map);
2200                 buf += map_bankwidth(map);
2201                 (*retlen) += map_bankwidth(map);
2202                 len -= map_bankwidth(map);
2203
2204                 if (ofs >> cfi->chipshift) {
2205                         chipnum++;
2206                         ofs = 0;
2207                         if (chipnum == cfi->numchips)
2208                                 return 0;
2209
2210                         chipstart = cfi->chips[chipnum].start;
2211                 }
2212         }
2213
2214         /* Write the trailing bytes if any */
2215         if (len & (map_bankwidth(map) - 1)) {
2216                 map_word tmp_buf;
2217
2218                 ret = cfi_amdstd_panic_wait(map, &cfi->chips[chipnum], ofs);
2219                 if (ret)
2220                         return ret;
2221
2222                 tmp_buf = map_read(map, ofs + chipstart);
2223
2224                 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
2225
2226                 ret = do_panic_write_oneword(map, &cfi->chips[chipnum],
2227                                              ofs, tmp_buf);
2228                 if (ret)
2229                         return ret;
2230
2231                 (*retlen) += len;
2232         }
2233
2234         return 0;
2235 }
2236
2237
2238 /*
2239  * Handle devices with one erase region, that only implement
2240  * the chip erase command.
2241  */
2242 static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
2243 {
2244         struct cfi_private *cfi = map->fldrv_priv;
2245         unsigned long timeo = jiffies + HZ;
2246         unsigned long int adr;
2247         DECLARE_WAITQUEUE(wait, current);
2248         int ret = 0;
2249         int retry_cnt = 0;
2250
2251         adr = cfi->addr_unlock1;
2252
2253         mutex_lock(&chip->mutex);
2254         ret = get_chip(map, chip, adr, FL_WRITING);
2255         if (ret) {
2256                 mutex_unlock(&chip->mutex);
2257                 return ret;
2258         }
2259
2260         pr_debug("MTD %s(): ERASE 0x%.8lx\n",
2261                __func__, chip->start );
2262
2263         XIP_INVAL_CACHED_RANGE(map, adr, map->size);
2264         ENABLE_VPP(map);
2265         xip_disable(map, chip, adr);
2266
2267  retry:
2268         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2269         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2270         cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2271         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2272         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2273         cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2274
2275         chip->state = FL_ERASING;
2276         chip->erase_suspended = 0;
2277         chip->in_progress_block_addr = adr;
2278         chip->in_progress_block_mask = ~(map->size - 1);
2279
2280         INVALIDATE_CACHE_UDELAY(map, chip,
2281                                 adr, map->size,
2282                                 chip->erase_time*500);
2283
2284         timeo = jiffies + (HZ*20);
2285
2286         for (;;) {
2287                 if (chip->state != FL_ERASING) {
2288                         /* Someone's suspended the erase. Sleep */
2289                         set_current_state(TASK_UNINTERRUPTIBLE);
2290                         add_wait_queue(&chip->wq, &wait);
2291                         mutex_unlock(&chip->mutex);
2292                         schedule();
2293                         remove_wait_queue(&chip->wq, &wait);
2294                         mutex_lock(&chip->mutex);
2295                         continue;
2296                 }
2297                 if (chip->erase_suspended) {
2298                         /* This erase was suspended and resumed.
2299                            Adjust the timeout */
2300                         timeo = jiffies + (HZ*20); /* FIXME */
2301                         chip->erase_suspended = 0;
2302                 }
2303
2304                 if (chip_good(map, adr, map_word_ff(map)))
2305                         break;
2306
2307                 if (time_after(jiffies, timeo)) {
2308                         printk(KERN_WARNING "MTD %s(): software timeout\n",
2309                                 __func__ );
2310                         ret = -EIO;
2311                         break;
2312                 }
2313
2314                 /* Latency issues. Drop the lock, wait a while and retry */
2315                 UDELAY(map, chip, adr, 1000000/HZ);
2316         }
2317         /* Did we succeed? */
2318         if (ret) {
2319                 /* reset on all failures. */
2320                 map_write( map, CMD(0xF0), chip->start );
2321                 /* FIXME - should have reset delay before continuing */
2322
2323                 if (++retry_cnt <= MAX_RETRIES) {
2324                         ret = 0;
2325                         goto retry;
2326                 }
2327         }
2328
2329         chip->state = FL_READY;
2330         xip_enable(map, chip, adr);
2331         DISABLE_VPP(map);
2332         put_chip(map, chip, adr);
2333         mutex_unlock(&chip->mutex);
2334
2335         return ret;
2336 }
2337
2338
2339 static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
2340 {
2341         struct cfi_private *cfi = map->fldrv_priv;
2342         unsigned long timeo = jiffies + HZ;
2343         DECLARE_WAITQUEUE(wait, current);
2344         int ret = 0;
2345         int retry_cnt = 0;
2346
2347         adr += chip->start;
2348
2349         mutex_lock(&chip->mutex);
2350         ret = get_chip(map, chip, adr, FL_ERASING);
2351         if (ret) {
2352                 mutex_unlock(&chip->mutex);
2353                 return ret;
2354         }
2355
2356         pr_debug("MTD %s(): ERASE 0x%.8lx\n",
2357                __func__, adr );
2358
2359         XIP_INVAL_CACHED_RANGE(map, adr, len);
2360         ENABLE_VPP(map);
2361         xip_disable(map, chip, adr);
2362
2363  retry:
2364         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2365         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2366         cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2367         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
2368         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
2369         map_write(map, cfi->sector_erase_cmd, adr);
2370
2371         chip->state = FL_ERASING;
2372         chip->erase_suspended = 0;
2373         chip->in_progress_block_addr = adr;
2374         chip->in_progress_block_mask = ~(len - 1);
2375
2376         INVALIDATE_CACHE_UDELAY(map, chip,
2377                                 adr, len,
2378                                 chip->erase_time*500);
2379
2380         timeo = jiffies + (HZ*20);
2381
2382         for (;;) {
2383                 if (chip->state != FL_ERASING) {
2384                         /* Someone's suspended the erase. Sleep */
2385                         set_current_state(TASK_UNINTERRUPTIBLE);
2386                         add_wait_queue(&chip->wq, &wait);
2387                         mutex_unlock(&chip->mutex);
2388                         schedule();
2389                         remove_wait_queue(&chip->wq, &wait);
2390                         mutex_lock(&chip->mutex);
2391                         continue;
2392                 }
2393                 if (chip->erase_suspended) {
2394                         /* This erase was suspended and resumed.
2395                            Adjust the timeout */
2396                         timeo = jiffies + (HZ*20); /* FIXME */
2397                         chip->erase_suspended = 0;
2398                 }
2399
2400                 if (chip_good(map, adr, map_word_ff(map))) {
2401                         xip_enable(map, chip, adr);
2402                         break;
2403                 }
2404
2405                 if (time_after(jiffies, timeo)) {
2406                         xip_enable(map, chip, adr);
2407                         printk(KERN_WARNING "MTD %s(): software timeout\n",
2408                                 __func__ );
2409                         ret = -EIO;
2410                         break;
2411                 }
2412
2413                 /* Latency issues. Drop the lock, wait a while and retry */
2414                 UDELAY(map, chip, adr, 1000000/HZ);
2415         }
2416         /* Did we succeed? */
2417         if (ret) {
2418                 /* reset on all failures. */
2419                 map_write( map, CMD(0xF0), chip->start );
2420                 /* FIXME - should have reset delay before continuing */
2421
2422                 if (++retry_cnt <= MAX_RETRIES) {
2423                         ret = 0;
2424                         goto retry;
2425                 }
2426         }
2427
2428         chip->state = FL_READY;
2429         DISABLE_VPP(map);
2430         put_chip(map, chip, adr);
2431         mutex_unlock(&chip->mutex);
2432         return ret;
2433 }
2434
2435
2436 static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
2437 {
2438         unsigned long ofs, len;
2439         int ret;
2440
2441         ofs = instr->addr;
2442         len = instr->len;
2443
2444         ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
2445         if (ret)
2446                 return ret;
2447
2448         instr->state = MTD_ERASE_DONE;
2449         mtd_erase_callback(instr);
2450
2451         return 0;
2452 }
2453
2454
2455 static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
2456 {
2457         struct map_info *map = mtd->priv;
2458         struct cfi_private *cfi = map->fldrv_priv;
2459         int ret = 0;
2460
2461         if (instr->addr != 0)
2462                 return -EINVAL;
2463
2464         if (instr->len != mtd->size)
2465                 return -EINVAL;
2466
2467         ret = do_erase_chip(map, &cfi->chips[0]);
2468         if (ret)
2469                 return ret;
2470
2471         instr->state = MTD_ERASE_DONE;
2472         mtd_erase_callback(instr);
2473
2474         return 0;
2475 }
2476
2477 static int do_atmel_lock(struct map_info *map, struct flchip *chip,
2478                          unsigned long adr, int len, void *thunk)
2479 {
2480         struct cfi_private *cfi = map->fldrv_priv;
2481         int ret;
2482
2483         mutex_lock(&chip->mutex);
2484         ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
2485         if (ret)
2486                 goto out_unlock;
2487         chip->state = FL_LOCKING;
2488
2489         pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
2490
2491         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2492                          cfi->device_type, NULL);
2493         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2494                          cfi->device_type, NULL);
2495         cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
2496                          cfi->device_type, NULL);
2497         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2498                          cfi->device_type, NULL);
2499         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2500                          cfi->device_type, NULL);
2501         map_write(map, CMD(0x40), chip->start + adr);
2502
2503         chip->state = FL_READY;
2504         put_chip(map, chip, adr + chip->start);
2505         ret = 0;
2506
2507 out_unlock:
2508         mutex_unlock(&chip->mutex);
2509         return ret;
2510 }
2511
2512 static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
2513                            unsigned long adr, int len, void *thunk)
2514 {
2515         struct cfi_private *cfi = map->fldrv_priv;
2516         int ret;
2517
2518         mutex_lock(&chip->mutex);
2519         ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
2520         if (ret)
2521                 goto out_unlock;
2522         chip->state = FL_UNLOCKING;
2523
2524         pr_debug("MTD %s(): LOCK 0x%08lx len %d\n", __func__, adr, len);
2525
2526         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2527                          cfi->device_type, NULL);
2528         map_write(map, CMD(0x70), adr);
2529
2530         chip->state = FL_READY;
2531         put_chip(map, chip, adr + chip->start);
2532         ret = 0;
2533
2534 out_unlock:
2535         mutex_unlock(&chip->mutex);
2536         return ret;
2537 }
2538
2539 static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2540 {
2541         return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
2542 }
2543
2544 static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2545 {
2546         return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
2547 }
2548
2549 /*
2550  * Advanced Sector Protection - PPB (Persistent Protection Bit) locking
2551  */
2552
2553 struct ppb_lock {
2554         struct flchip *chip;
2555         unsigned long adr;
2556         int locked;
2557 };
2558
2559 #define MAX_SECTORS                     512
2560
2561 #define DO_XXLOCK_ONEBLOCK_LOCK         ((void *)1)
2562 #define DO_XXLOCK_ONEBLOCK_UNLOCK       ((void *)2)
2563 #define DO_XXLOCK_ONEBLOCK_GETLOCK      ((void *)3)
2564
2565 static int __maybe_unused do_ppb_xxlock(struct map_info *map,
2566                                         struct flchip *chip,
2567                                         unsigned long adr, int len, void *thunk)
2568 {
2569         struct cfi_private *cfi = map->fldrv_priv;
2570         unsigned long timeo;
2571         int ret;
2572
2573         adr += chip->start;
2574         mutex_lock(&chip->mutex);
2575         ret = get_chip(map, chip, adr, FL_LOCKING);
2576         if (ret) {
2577                 mutex_unlock(&chip->mutex);
2578                 return ret;
2579         }
2580
2581         pr_debug("MTD %s(): XXLOCK 0x%08lx len %d\n", __func__, adr, len);
2582
2583         cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
2584                          cfi->device_type, NULL);
2585         cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
2586                          cfi->device_type, NULL);
2587         /* PPB entry command */
2588         cfi_send_gen_cmd(0xC0, cfi->addr_unlock1, chip->start, map, cfi,
2589                          cfi->device_type, NULL);
2590
2591         if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2592                 chip->state = FL_LOCKING;
2593                 map_write(map, CMD(0xA0), adr);
2594                 map_write(map, CMD(0x00), adr);
2595         } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2596                 /*
2597                  * Unlocking of one specific sector is not supported, so we
2598                  * have to unlock all sectors of this device instead
2599                  */
2600                 chip->state = FL_UNLOCKING;
2601                 map_write(map, CMD(0x80), chip->start);
2602                 map_write(map, CMD(0x30), chip->start);
2603         } else if (thunk == DO_XXLOCK_ONEBLOCK_GETLOCK) {
2604                 chip->state = FL_JEDEC_QUERY;
2605                 /* Return locked status: 0->locked, 1->unlocked */
2606                 ret = !cfi_read_query(map, adr);
2607         } else
2608                 BUG();
2609
2610         /*
2611          * Wait for some time as unlocking of all sectors takes quite long
2612          */
2613         timeo = jiffies + msecs_to_jiffies(2000);       /* 2s max (un)locking */
2614         for (;;) {
2615                 if (chip_ready(map, adr))
2616                         break;
2617
2618                 if (time_after(jiffies, timeo)) {
2619                         printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
2620                         ret = -EIO;
2621                         break;
2622                 }
2623
2624                 UDELAY(map, chip, adr, 1);
2625         }
2626
2627         /* Exit BC commands */
2628         map_write(map, CMD(0x90), chip->start);
2629         map_write(map, CMD(0x00), chip->start);
2630
2631         chip->state = FL_READY;
2632         put_chip(map, chip, adr);
2633         mutex_unlock(&chip->mutex);
2634
2635         return ret;
2636 }
2637
2638 static int __maybe_unused cfi_ppb_lock(struct mtd_info *mtd, loff_t ofs,
2639                                        uint64_t len)
2640 {
2641         return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2642                                 DO_XXLOCK_ONEBLOCK_LOCK);
2643 }
2644
2645 static int __maybe_unused cfi_ppb_unlock(struct mtd_info *mtd, loff_t ofs,
2646                                          uint64_t len)
2647 {
2648         struct mtd_erase_region_info *regions = mtd->eraseregions;
2649         struct map_info *map = mtd->priv;
2650         struct cfi_private *cfi = map->fldrv_priv;
2651         struct ppb_lock *sect;
2652         unsigned long adr;
2653         loff_t offset;
2654         uint64_t length;
2655         int chipnum;
2656         int i;
2657         int sectors;
2658         int ret;
2659
2660         /*
2661          * PPB unlocking always unlocks all sectors of the flash chip.
2662          * We need to re-lock all previously locked sectors. So lets
2663          * first check the locking status of all sectors and save
2664          * it for future use.
2665          */
2666         sect = kzalloc(MAX_SECTORS * sizeof(struct ppb_lock), GFP_KERNEL);
2667         if (!sect)
2668                 return -ENOMEM;
2669
2670         /*
2671          * This code to walk all sectors is a slightly modified version
2672          * of the cfi_varsize_frob() code.
2673          */
2674         i = 0;
2675         chipnum = 0;
2676         adr = 0;
2677         sectors = 0;
2678         offset = 0;
2679         length = mtd->size;
2680
2681         while (length) {
2682                 int size = regions[i].erasesize;
2683
2684                 /*
2685                  * Only test sectors that shall not be unlocked. The other
2686                  * sectors shall be unlocked, so lets keep their locking
2687                  * status at "unlocked" (locked=0) for the final re-locking.
2688                  */
2689                 if ((offset < ofs) || (offset >= (ofs + len))) {
2690                         sect[sectors].chip = &cfi->chips[chipnum];
2691                         sect[sectors].adr = adr;
2692                         sect[sectors].locked = do_ppb_xxlock(
2693                                 map, &cfi->chips[chipnum], adr, 0,
2694                                 DO_XXLOCK_ONEBLOCK_GETLOCK);
2695                 }
2696
2697                 adr += size;
2698                 offset += size;
2699                 length -= size;
2700
2701                 if (offset == regions[i].offset + size * regions[i].numblocks)
2702                         i++;
2703
2704                 if (adr >> cfi->chipshift) {
2705                         if (offset >= (ofs + len))
2706                                 break;
2707                         adr = 0;
2708                         chipnum++;
2709
2710                         if (chipnum >= cfi->numchips)
2711                                 break;
2712                 }
2713
2714                 sectors++;
2715                 if (sectors >= MAX_SECTORS) {
2716                         printk(KERN_ERR "Only %d sectors for PPB locking supported!\n",
2717                                MAX_SECTORS);
2718                         kfree(sect);
2719                         return -EINVAL;
2720                 }
2721         }
2722
2723         /* Now unlock the whole chip */
2724         ret = cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2725                                DO_XXLOCK_ONEBLOCK_UNLOCK);
2726         if (ret) {
2727                 kfree(sect);
2728                 return ret;
2729         }
2730
2731         /*
2732          * PPB unlocking always unlocks all sectors of the flash chip.
2733          * We need to re-lock all previously locked sectors.
2734          */
2735         for (i = 0; i < sectors; i++) {
2736                 if (sect[i].locked)
2737                         do_ppb_xxlock(map, sect[i].chip, sect[i].adr, 0,
2738                                       DO_XXLOCK_ONEBLOCK_LOCK);
2739         }
2740
2741         kfree(sect);
2742         return ret;
2743 }
2744
2745 static int __maybe_unused cfi_ppb_is_locked(struct mtd_info *mtd, loff_t ofs,
2746                                             uint64_t len)
2747 {
2748         return cfi_varsize_frob(mtd, do_ppb_xxlock, ofs, len,
2749                                 DO_XXLOCK_ONEBLOCK_GETLOCK) ? 1 : 0;
2750 }
2751
2752 static void cfi_amdstd_sync (struct mtd_info *mtd)
2753 {
2754         struct map_info *map = mtd->priv;
2755         struct cfi_private *cfi = map->fldrv_priv;
2756         int i;
2757         struct flchip *chip;
2758         int ret = 0;
2759         DECLARE_WAITQUEUE(wait, current);
2760
2761         for (i=0; !ret && i<cfi->numchips; i++) {
2762                 chip = &cfi->chips[i];
2763
2764         retry:
2765                 mutex_lock(&chip->mutex);
2766
2767                 switch(chip->state) {
2768                 case FL_READY:
2769                 case FL_STATUS:
2770                 case FL_CFI_QUERY:
2771                 case FL_JEDEC_QUERY:
2772                         chip->oldstate = chip->state;
2773                         chip->state = FL_SYNCING;
2774                         /* No need to wake_up() on this state change -
2775                          * as the whole point is that nobody can do anything
2776                          * with the chip now anyway.
2777                          */
2778                 case FL_SYNCING:
2779                         mutex_unlock(&chip->mutex);
2780                         break;
2781
2782                 default:
2783                         /* Not an idle state */
2784                         set_current_state(TASK_UNINTERRUPTIBLE);
2785                         add_wait_queue(&chip->wq, &wait);
2786
2787                         mutex_unlock(&chip->mutex);
2788
2789                         schedule();
2790
2791                         remove_wait_queue(&chip->wq, &wait);
2792
2793                         goto retry;
2794                 }
2795         }
2796
2797         /* Unlock the chips again */
2798
2799         for (i--; i >=0; i--) {
2800                 chip = &cfi->chips[i];
2801
2802                 mutex_lock(&chip->mutex);
2803
2804                 if (chip->state == FL_SYNCING) {
2805                         chip->state = chip->oldstate;
2806                         wake_up(&chip->wq);
2807                 }
2808                 mutex_unlock(&chip->mutex);
2809         }
2810 }
2811
2812
2813 static int cfi_amdstd_suspend(struct mtd_info *mtd)
2814 {
2815         struct map_info *map = mtd->priv;
2816         struct cfi_private *cfi = map->fldrv_priv;
2817         int i;
2818         struct flchip *chip;
2819         int ret = 0;
2820
2821         for (i=0; !ret && i<cfi->numchips; i++) {
2822                 chip = &cfi->chips[i];
2823
2824                 mutex_lock(&chip->mutex);
2825
2826                 switch(chip->state) {
2827                 case FL_READY:
2828                 case FL_STATUS:
2829                 case FL_CFI_QUERY:
2830                 case FL_JEDEC_QUERY:
2831                         chip->oldstate = chip->state;
2832                         chip->state = FL_PM_SUSPENDED;
2833                         /* No need to wake_up() on this state change -
2834                          * as the whole point is that nobody can do anything
2835                          * with the chip now anyway.
2836                          */
2837                 case FL_PM_SUSPENDED:
2838                         break;
2839
2840                 default:
2841                         ret = -EAGAIN;
2842                         break;
2843                 }
2844                 mutex_unlock(&chip->mutex);
2845         }
2846
2847         /* Unlock the chips again */
2848
2849         if (ret) {
2850                 for (i--; i >=0; i--) {
2851                         chip = &cfi->chips[i];
2852
2853                         mutex_lock(&chip->mutex);
2854
2855                         if (chip->state == FL_PM_SUSPENDED) {
2856                                 chip->state = chip->oldstate;
2857                                 wake_up(&chip->wq);
2858                         }
2859                         mutex_unlock(&chip->mutex);
2860                 }
2861         }
2862
2863         return ret;
2864 }
2865
2866
2867 static void cfi_amdstd_resume(struct mtd_info *mtd)
2868 {
2869         struct map_info *map = mtd->priv;
2870         struct cfi_private *cfi = map->fldrv_priv;
2871         int i;
2872         struct flchip *chip;
2873
2874         for (i=0; i<cfi->numchips; i++) {
2875
2876                 chip = &cfi->chips[i];
2877
2878                 mutex_lock(&chip->mutex);
2879
2880                 if (chip->state == FL_PM_SUSPENDED) {
2881                         chip->state = FL_READY;
2882                         map_write(map, CMD(0xF0), chip->start);
2883                         wake_up(&chip->wq);
2884                 }
2885                 else
2886                         printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
2887
2888                 mutex_unlock(&chip->mutex);
2889         }
2890 }
2891
2892
2893 /*
2894  * Ensure that the flash device is put back into read array mode before
2895  * unloading the driver or rebooting.  On some systems, rebooting while
2896  * the flash is in query/program/erase mode will prevent the CPU from
2897  * fetching the bootloader code, requiring a hard reset or power cycle.
2898  */
2899 static int cfi_amdstd_reset(struct mtd_info *mtd)
2900 {
2901         struct map_info *map = mtd->priv;
2902         struct cfi_private *cfi = map->fldrv_priv;
2903         int i, ret;
2904         struct flchip *chip;
2905
2906         for (i = 0; i < cfi->numchips; i++) {
2907
2908                 chip = &cfi->chips[i];
2909
2910                 mutex_lock(&chip->mutex);
2911
2912                 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2913                 if (!ret) {
2914                         map_write(map, CMD(0xF0), chip->start);
2915                         chip->state = FL_SHUTDOWN;
2916                         put_chip(map, chip, chip->start);
2917                 }
2918
2919                 mutex_unlock(&chip->mutex);
2920         }
2921
2922         return 0;
2923 }
2924
2925
2926 static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2927                                void *v)
2928 {
2929         struct mtd_info *mtd;
2930
2931         mtd = container_of(nb, struct mtd_info, reboot_notifier);
2932         cfi_amdstd_reset(mtd);
2933         return NOTIFY_DONE;
2934 }
2935
2936
2937 static void cfi_amdstd_destroy(struct mtd_info *mtd)
2938 {
2939         struct map_info *map = mtd->priv;
2940         struct cfi_private *cfi = map->fldrv_priv;
2941
2942         cfi_amdstd_reset(mtd);
2943         unregister_reboot_notifier(&mtd->reboot_notifier);
2944         kfree(cfi->cmdset_priv);
2945         kfree(cfi->cfiq);
2946         kfree(cfi);
2947         kfree(mtd->eraseregions);
2948 }
2949
2950 MODULE_LICENSE("GPL");
2951 MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2952 MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");
2953 MODULE_ALIAS("cfi_cmdset_0006");
2954 MODULE_ALIAS("cfi_cmdset_0701");