blob: 8901c4412daf173c21406e076ed2713d1e433e89 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Todd Poynor02b15e32005-06-07 00:04:39 +01007 * Copyright (C) 2005 MontaVista Software Inc. <source@mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * 2_by_8 routines added by Simon Munton
10 *
11 * 4_by_16 work by Carolyn J. Smith
12 *
Thomas Gleixner1f948b42005-11-07 11:15:37 +000013 * XIP support hooks by Vitaly Wool (based on code for Intel flash
Todd Poynor02b15e32005-06-07 00:04:39 +010014 * by Nicolas Pitre)
Thomas Gleixner1f948b42005-11-07 11:15:37 +000015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
17 *
18 * This code is GPL
19 *
Thomas Gleixner1f948b42005-11-07 11:15:37 +000020 * $Id: cfi_cmdset_0002.c,v 1.122 2005/11/07 11:14:22 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/sched.h>
28#include <linux/init.h>
29#include <asm/io.h>
30#include <asm/byteorder.h>
31
32#include <linux/errno.h>
33#include <linux/slab.h>
34#include <linux/delay.h>
35#include <linux/interrupt.h>
36#include <linux/mtd/compatmac.h>
37#include <linux/mtd/map.h>
38#include <linux/mtd/mtd.h>
39#include <linux/mtd/cfi.h>
Todd Poynor02b15e32005-06-07 00:04:39 +010040#include <linux/mtd/xip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define AMD_BOOTLOC_BUG
43#define FORCE_WORD_WRITE 0
44
45#define MAX_WORD_RETRIES 3
46
47#define MANUFACTURER_AMD 0x0001
48#define MANUFACTURER_SST 0x00BF
49#define SST49LF004B 0x0060
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010050#define SST49LF008A 0x005a
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
53static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
55static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
56static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
57static void cfi_amdstd_sync (struct mtd_info *);
58static int cfi_amdstd_suspend (struct mtd_info *);
59static void cfi_amdstd_resume (struct mtd_info *);
60static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
61
62static void cfi_amdstd_destroy(struct mtd_info *);
63
64struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
65static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
66
67static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
68static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
69#include "fwh_lock.h"
70
71static struct mtd_chip_driver cfi_amdstd_chipdrv = {
72 .probe = NULL, /* Not usable directly */
73 .destroy = cfi_amdstd_destroy,
74 .name = "cfi_cmdset_0002",
75 .module = THIS_MODULE
76};
77
78
79/* #define DEBUG_CFI_FEATURES */
80
81
82#ifdef DEBUG_CFI_FEATURES
83static void cfi_tell_features(struct cfi_pri_amdstd *extp)
84{
85 const char* erase_suspend[3] = {
86 "Not supported", "Read only", "Read/write"
87 };
88 const char* top_bottom[6] = {
89 "No WP", "8x8KiB sectors at top & bottom, no WP",
90 "Bottom boot", "Top boot",
91 "Uniform, Bottom WP", "Uniform, Top WP"
92 };
93
94 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +000095 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 (extp->SiliconRevision & 1) ? "Not required" : "Required");
97
98 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
99 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
100 else
101 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
102
103 if (extp->BlkProt == 0)
104 printk(" Block protection: Not supported\n");
105 else
106 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
107
108
109 printk(" Temporary block unprotect: %s\n",
110 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
111 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
112 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
113 printk(" Burst mode: %s\n",
114 extp->BurstMode ? "Supported" : "Not supported");
115 if (extp->PageMode == 0)
116 printk(" Page mode: Not supported\n");
117 else
118 printk(" Page mode: %d word page\n", extp->PageMode << 2);
119
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000120 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000122 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 extp->VppMax >> 4, extp->VppMax & 0xf);
124
125 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
126 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
127 else
128 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
129}
130#endif
131
132#ifdef AMD_BOOTLOC_BUG
133/* Wheee. Bring me the head of someone at AMD. */
134static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
135{
136 struct map_info *map = mtd->priv;
137 struct cfi_private *cfi = map->fldrv_priv;
138 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
139 __u8 major = extp->MajorVersion;
140 __u8 minor = extp->MinorVersion;
141
142 if (((major << 8) | minor) < 0x3131) {
143 /* CFI version 1.0 => don't trust bootloc */
144 if (cfi->id & 0x80) {
145 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
146 extp->TopBottom = 3; /* top boot */
147 } else {
148 extp->TopBottom = 2; /* bottom boot */
149 }
150 }
151}
152#endif
153
154static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
155{
156 struct map_info *map = mtd->priv;
157 struct cfi_private *cfi = map->fldrv_priv;
158 if (cfi->cfiq->BufWriteTimeoutTyp) {
159 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
160 mtd->write = cfi_amdstd_write_buffers;
161 }
162}
163
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200164/* Atmel chips don't use the same PRI format as AMD chips */
165static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
166{
167 struct map_info *map = mtd->priv;
168 struct cfi_private *cfi = map->fldrv_priv;
169 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
170 struct cfi_pri_atmel atmel_pri;
171
172 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
173 memset(extp + 5, 0, sizeof(*extp) - 5);
174
175 if (atmel_pri.Features & 0x02)
176 extp->EraseSuspend = 2;
177
178 if (atmel_pri.BottomBoot)
179 extp->TopBottom = 2;
180 else
181 extp->TopBottom = 3;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static void fixup_use_secsi(struct mtd_info *mtd, void *param)
185{
186 /* Setup for chips with a secsi area */
187 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
188 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
189}
190
191static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
192{
193 struct map_info *map = mtd->priv;
194 struct cfi_private *cfi = map->fldrv_priv;
195 if ((cfi->cfiq->NumEraseRegions == 1) &&
196 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
197 mtd->erase = cfi_amdstd_erase_chip;
198 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static struct cfi_fixup cfi_fixup_table[] = {
203#ifdef AMD_BOOTLOC_BUG
204 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
205#endif
206 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
207 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
208 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
209 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
210 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
211 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
212#if !FORCE_WORD_WRITE
213 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
214#endif
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200215 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 { 0, 0, NULL, NULL }
217};
218static struct cfi_fixup jedec_fixup_table[] = {
219 { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100220 { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 { 0, 0, NULL, NULL }
222};
223
224static struct cfi_fixup fixup_table[] = {
225 /* The CFI vendor ids and the JEDEC vendor IDs appear
226 * to be common. It is like the devices id's are as
227 * well. This table is to pick all cases where
228 * we know that is the case.
229 */
230 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
231 { 0, 0, NULL, NULL }
232};
233
234
235struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
236{
237 struct cfi_private *cfi = map->fldrv_priv;
238 struct mtd_info *mtd;
239 int i;
240
241 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
242 if (!mtd) {
243 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
244 return NULL;
245 }
246 memset(mtd, 0, sizeof(*mtd));
247 mtd->priv = map;
248 mtd->type = MTD_NORFLASH;
249
250 /* Fill in the default mtd operations */
251 mtd->erase = cfi_amdstd_erase_varsize;
252 mtd->write = cfi_amdstd_write_words;
253 mtd->read = cfi_amdstd_read;
254 mtd->sync = cfi_amdstd_sync;
255 mtd->suspend = cfi_amdstd_suspend;
256 mtd->resume = cfi_amdstd_resume;
257 mtd->flags = MTD_CAP_NORFLASH;
258 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400259 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (cfi->cfi_mode==CFI_MODE_CFI){
262 unsigned char bootloc;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000263 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * It's a real CFI chip, not one for which the probe
265 * routine faked a CFI structure. So we read the feature
266 * table from it.
267 */
268 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
269 struct cfi_pri_amdstd *extp;
270
271 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
272 if (!extp) {
273 kfree(mtd);
274 return NULL;
275 }
276
Todd Poynord88f9772005-07-20 22:01:17 +0100277 if (extp->MajorVersion != '1' ||
278 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
279 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
280 "version %c.%c.\n", extp->MajorVersion,
281 extp->MinorVersion);
282 kfree(extp);
283 kfree(mtd);
284 return NULL;
285 }
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000288 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Apply cfi device specific fixups */
291 cfi_fixup(mtd, cfi_fixup_table);
292
293#ifdef DEBUG_CFI_FEATURES
294 /* Tell the user about it in lots of lovely detail */
295 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 bootloc = extp->TopBottom;
299 if ((bootloc != 2) && (bootloc != 3)) {
300 printk(KERN_WARNING "%s: CFI does not contain boot "
301 "bank location. Assuming top.\n", map->name);
302 bootloc = 2;
303 }
304
305 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
306 printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
309 int j = (cfi->cfiq->NumEraseRegions-1)-i;
310 __u32 swap;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 swap = cfi->cfiq->EraseRegionInfo[i];
313 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
314 cfi->cfiq->EraseRegionInfo[j] = swap;
315 }
316 }
317 /* Set the default CFI lock/unlock addresses */
318 cfi->addr_unlock1 = 0x555;
319 cfi->addr_unlock2 = 0x2aa;
320 /* Modify the unlock address if we are in compatibility mode */
321 if ( /* x16 in x8 mode */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000322 ((cfi->device_type == CFI_DEVICETYPE_X8) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 (cfi->cfiq->InterfaceDesc == 2)) ||
324 /* x32 in x16 mode */
325 ((cfi->device_type == CFI_DEVICETYPE_X16) &&
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000326 (cfi->cfiq->InterfaceDesc == 4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 {
328 cfi->addr_unlock1 = 0xaaa;
329 cfi->addr_unlock2 = 0x555;
330 }
331
332 } /* CFI mode */
333 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
334 /* Apply jedec specific fixups */
335 cfi_fixup(mtd, jedec_fixup_table);
336 }
337 /* Apply generic fixups */
338 cfi_fixup(mtd, fixup_table);
339
340 for (i=0; i< cfi->numchips; i++) {
341 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
342 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
343 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000344 }
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return cfi_amdstd_setup(mtd);
349}
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100350EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
353{
354 struct map_info *map = mtd->priv;
355 struct cfi_private *cfi = map->fldrv_priv;
356 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
357 unsigned long offset = 0;
358 int i,j;
359
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000360 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000362 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 mtd->size = devsize * cfi->numchips;
364
365 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
366 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
367 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000368 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
370 goto setup_err;
371 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
374 unsigned long ernum, ersize;
375 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
376 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (mtd->erasesize < ersize) {
379 mtd->erasesize = ersize;
380 }
381 for (j=0; j<cfi->numchips; j++) {
382 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
383 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
384 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
385 }
386 offset += (ersize * ernum);
387 }
388 if (offset != devsize) {
389 /* Argh */
390 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
391 goto setup_err;
392 }
393#if 0
394 // debug
395 for (i=0; i<mtd->numeraseregions;i++){
396 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
397 i,mtd->eraseregions[i].offset,
398 mtd->eraseregions[i].erasesize,
399 mtd->eraseregions[i].numblocks);
400 }
401#endif
402
403 /* FIXME: erase-suspend-program is broken. See
404 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
405 printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
406
407 __module_get(THIS_MODULE);
408 return mtd;
409
410 setup_err:
411 if(mtd) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800412 kfree(mtd->eraseregions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kfree(mtd);
414 }
415 kfree(cfi->cmdset_priv);
416 kfree(cfi->cfiq);
417 return NULL;
418}
419
420/*
421 * Return true if the chip is ready.
422 *
423 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
424 * non-suspended sector) and is indicated by no toggle bits toggling.
425 *
426 * Note that anything more complicated than checking if no bits are toggling
427 * (including checking DQ5 for an error status) is tricky to get working
428 * correctly and is therefore not done (particulary with interleaved chips
429 * as each chip must be checked independantly of the others).
430 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100431static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 map_word d, t;
434
435 d = map_read(map, addr);
436 t = map_read(map, addr);
437
438 return map_word_equal(map, d, t);
439}
440
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100441/*
442 * Return true if the chip is ready and has the correct value.
443 *
444 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
445 * non-suspended sector) and it is indicated by no bits toggling.
446 *
447 * Error are indicated by toggling bits or bits held with the wrong value,
448 * or with bits toggling.
449 *
450 * Note that anything more complicated than checking if no bits are toggling
451 * (including checking DQ5 for an error status) is tricky to get working
452 * correctly and is therefore not done (particulary with interleaved chips
453 * as each chip must be checked independantly of the others).
454 *
455 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100456static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100457{
458 map_word oldd, curd;
459
460 oldd = map_read(map, addr);
461 curd = map_read(map, addr);
462
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000463 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100464 map_word_equal(map, curd, expected);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
468{
469 DECLARE_WAITQUEUE(wait, current);
470 struct cfi_private *cfi = map->fldrv_priv;
471 unsigned long timeo;
472 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
473
474 resettime:
475 timeo = jiffies + HZ;
476 retry:
477 switch (chip->state) {
478
479 case FL_STATUS:
480 for (;;) {
481 if (chip_ready(map, adr))
482 break;
483
484 if (time_after(jiffies, timeo)) {
485 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Todd Poynor02b15e32005-06-07 00:04:39 +0100486 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EIO;
488 }
Todd Poynor02b15e32005-06-07 00:04:39 +0100489 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 cfi_udelay(1);
Todd Poynor02b15e32005-06-07 00:04:39 +0100491 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Someone else might have been playing with it. */
493 goto retry;
494 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 case FL_READY:
497 case FL_CFI_QUERY:
498 case FL_JEDEC_QUERY:
499 return 0;
500
501 case FL_ERASING:
502 if (mode == FL_WRITING) /* FIXME: Erase-suspend-program appears broken. */
503 goto sleep;
504
505 if (!(mode == FL_READY || mode == FL_POINT
506 || !cfip
507 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))
508 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x1))))
509 goto sleep;
510
511 /* We could check to see if we're trying to access the sector
512 * that is currently being erased. However, no user will try
513 * anything like that so we just wait for the timeout. */
514
515 /* Erase suspend */
516 /* It's harmless to issue the Erase-Suspend and Erase-Resume
517 * commands when the erase algorithm isn't in progress. */
518 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
519 chip->oldstate = FL_ERASING;
520 chip->state = FL_ERASE_SUSPENDING;
521 chip->erase_suspended = 1;
522 for (;;) {
523 if (chip_ready(map, adr))
524 break;
525
526 if (time_after(jiffies, timeo)) {
527 /* Should have suspended the erase by now.
528 * Send an Erase-Resume command as either
529 * there was an error (so leave the erase
530 * routine to recover from it) or we trying to
531 * use the erase-in-progress sector. */
532 map_write(map, CMD(0x30), chip->in_progress_block_addr);
533 chip->state = FL_ERASING;
534 chip->oldstate = FL_READY;
535 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
536 return -EIO;
537 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000538
Todd Poynor02b15e32005-06-07 00:04:39 +0100539 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 cfi_udelay(1);
Todd Poynor02b15e32005-06-07 00:04:39 +0100541 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
543 So we can just loop here. */
544 }
545 chip->state = FL_READY;
546 return 0;
547
Todd Poynor02b15e32005-06-07 00:04:39 +0100548 case FL_XIP_WHILE_ERASING:
549 if (mode != FL_READY && mode != FL_POINT &&
550 (!cfip || !(cfip->EraseSuspend&2)))
551 goto sleep;
552 chip->oldstate = chip->state;
553 chip->state = FL_READY;
554 return 0;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 case FL_POINT:
557 /* Only if there's no operation suspended... */
558 if (mode == FL_READY && chip->oldstate == FL_READY)
559 return 0;
560
561 default:
562 sleep:
563 set_current_state(TASK_UNINTERRUPTIBLE);
564 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +0100565 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 schedule();
567 remove_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +0100568 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto resettime;
570 }
571}
572
573
574static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
575{
576 struct cfi_private *cfi = map->fldrv_priv;
577
578 switch(chip->oldstate) {
579 case FL_ERASING:
580 chip->state = chip->oldstate;
581 map_write(map, CMD(0x30), chip->in_progress_block_addr);
582 chip->oldstate = FL_READY;
583 chip->state = FL_ERASING;
584 break;
585
Todd Poynor02b15e32005-06-07 00:04:39 +0100586 case FL_XIP_WHILE_ERASING:
587 chip->state = chip->oldstate;
588 chip->oldstate = FL_READY;
589 break;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 case FL_READY:
592 case FL_STATUS:
593 /* We should really make set_vpp() count, rather than doing this */
594 DISABLE_VPP(map);
595 break;
596 default:
597 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
598 }
599 wake_up(&chip->wq);
600}
601
Todd Poynor02b15e32005-06-07 00:04:39 +0100602#ifdef CONFIG_MTD_XIP
603
604/*
605 * No interrupt what so ever can be serviced while the flash isn't in array
606 * mode. This is ensured by the xip_disable() and xip_enable() functions
607 * enclosing any code path where the flash is known not to be in array mode.
608 * And within a XIP disabled code path, only functions marked with __xipram
609 * may be called and nothing else (it's a good thing to inspect generated
610 * assembly to make sure inline functions were actually inlined and that gcc
611 * didn't emit calls to its own support functions). Also configuring MTD CFI
612 * support to a single buswidth and a single interleave is also recommended.
613 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200614
Todd Poynor02b15e32005-06-07 00:04:39 +0100615static void xip_disable(struct map_info *map, struct flchip *chip,
616 unsigned long adr)
617{
618 /* TODO: chips with no XIP use should ignore and return */
619 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
620 local_irq_disable();
621}
622
623static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
624 unsigned long adr)
625{
626 struct cfi_private *cfi = map->fldrv_priv;
627
628 if (chip->state != FL_POINT && chip->state != FL_READY) {
629 map_write(map, CMD(0xf0), adr);
630 chip->state = FL_READY;
631 }
632 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200633 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100634 local_irq_enable();
635}
636
637/*
638 * When a delay is required for the flash operation to complete, the
639 * xip_udelay() function is polling for both the given timeout and pending
640 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000641 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100642 * and interrupts unmasked. Task scheduling might also happen at that
643 * point. The CPU eventually returns from the interrupt or the call to
644 * schedule() and the suspended flash operation is resumed for the remaining
645 * of the delay period.
646 *
647 * Warning: this function _will_ fool interrupt latency tracing tools.
648 */
649
650static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
651 unsigned long adr, int usec)
652{
653 struct cfi_private *cfi = map->fldrv_priv;
654 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
655 map_word status, OK = CMD(0x80);
656 unsigned long suspended, start = xip_currtime();
657 flstate_t oldstate;
658
659 do {
660 cpu_relax();
661 if (xip_irqpending() && extp &&
662 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
663 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
664 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000665 * Let's suspend the erase operation when supported.
666 * Note that we currently don't try to suspend
667 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100668 * operation suspended (imagine what happens
669 * when one chip was already done with the current
670 * operation while another chip suspended it, then
671 * we resume the whole thing at once). Yes, it
672 * can happen!
673 */
674 map_write(map, CMD(0xb0), adr);
675 usec -= xip_elapsed_since(start);
676 suspended = xip_currtime();
677 do {
678 if (xip_elapsed_since(suspended) > 100000) {
679 /*
680 * The chip doesn't want to suspend
681 * after waiting for 100 msecs.
682 * This is a critical error but there
683 * is not much we can do here.
684 */
685 return;
686 }
687 status = map_read(map, adr);
688 } while (!map_word_andequal(map, status, OK, OK));
689
690 /* Suspend succeeded */
691 oldstate = chip->state;
692 if (!map_word_bitsset(map, status, CMD(0x40)))
693 break;
694 chip->state = FL_XIP_WHILE_ERASING;
695 chip->erase_suspended = 1;
696 map_write(map, CMD(0xf0), adr);
697 (void) map_read(map, adr);
698 asm volatile (".rep 8; nop; .endr");
699 local_irq_enable();
700 spin_unlock(chip->mutex);
701 asm volatile (".rep 8; nop; .endr");
702 cond_resched();
703
704 /*
705 * We're back. However someone else might have
706 * decided to go write to the chip if we are in
707 * a suspended erase state. If so let's wait
708 * until it's done.
709 */
710 spin_lock(chip->mutex);
711 while (chip->state != FL_XIP_WHILE_ERASING) {
712 DECLARE_WAITQUEUE(wait, current);
713 set_current_state(TASK_UNINTERRUPTIBLE);
714 add_wait_queue(&chip->wq, &wait);
715 spin_unlock(chip->mutex);
716 schedule();
717 remove_wait_queue(&chip->wq, &wait);
718 spin_lock(chip->mutex);
719 }
720 /* Disallow XIP again */
721 local_irq_disable();
722
723 /* Resume the write or erase operation */
724 map_write(map, CMD(0x30), adr);
725 chip->state = oldstate;
726 start = xip_currtime();
727 } else if (usec >= 1000000/HZ) {
728 /*
729 * Try to save on CPU power when waiting delay
730 * is at least a system timer tick period.
731 * No need to be extremely accurate here.
732 */
733 xip_cpu_idle();
734 }
735 status = map_read(map, adr);
736 } while (!map_word_andequal(map, status, OK, OK)
737 && xip_elapsed_since(start) < usec);
738}
739
740#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
741
742/*
743 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
744 * the flash is actively programming or erasing since we have to poll for
745 * the operation to complete anyway. We can't do that in a generic way with
746 * a XIP setup so do it before the actual flash operation in this case
747 * and stub it out from INVALIDATE_CACHE_UDELAY.
748 */
749#define XIP_INVAL_CACHED_RANGE(map, from, size) \
750 INVALIDATE_CACHED_RANGE(map, from, size)
751
752#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
753 UDELAY(map, chip, adr, usec)
754
755/*
756 * Extra notes:
757 *
758 * Activating this XIP support changes the way the code works a bit. For
759 * example the code to suspend the current process when concurrent access
760 * happens is never executed because xip_udelay() will always return with the
761 * same chip state as it was entered with. This is why there is no care for
762 * the presence of add_wait_queue() or schedule() calls from within a couple
763 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
764 * The queueing and scheduling are always happening within xip_udelay().
765 *
766 * Similarly, get_chip() and put_chip() just happen to always be executed
767 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
768 * is in array mode, therefore never executing many cases therein and not
769 * causing any problem with XIP.
770 */
771
772#else
773
774#define xip_disable(map, chip, adr)
775#define xip_enable(map, chip, adr)
776#define XIP_INVAL_CACHED_RANGE(x...)
777
778#define UDELAY(map, chip, adr, usec) \
779do { \
780 spin_unlock(chip->mutex); \
781 cfi_udelay(usec); \
782 spin_lock(chip->mutex); \
783} while (0)
784
785#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
786do { \
787 spin_unlock(chip->mutex); \
788 INVALIDATE_CACHED_RANGE(map, adr, len); \
789 cfi_udelay(usec); \
790 spin_lock(chip->mutex); \
791} while (0)
792
793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
796{
797 unsigned long cmd_addr;
798 struct cfi_private *cfi = map->fldrv_priv;
799 int ret;
800
801 adr += chip->start;
802
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000803 /* Ensure cmd read/writes are aligned. */
804 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Todd Poynor02b15e32005-06-07 00:04:39 +0100806 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ret = get_chip(map, chip, cmd_addr, FL_READY);
808 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +0100809 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return ret;
811 }
812
813 if (chip->state != FL_POINT && chip->state != FL_READY) {
814 map_write(map, CMD(0xf0), cmd_addr);
815 chip->state = FL_READY;
816 }
817
818 map_copy_from(map, buf, adr, len);
819
820 put_chip(map, chip, cmd_addr);
821
Todd Poynor02b15e32005-06-07 00:04:39 +0100822 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824}
825
826
827static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
828{
829 struct map_info *map = mtd->priv;
830 struct cfi_private *cfi = map->fldrv_priv;
831 unsigned long ofs;
832 int chipnum;
833 int ret = 0;
834
835 /* ofs: offset within the first chip that the first read should start */
836
837 chipnum = (from >> cfi->chipshift);
838 ofs = from - (chipnum << cfi->chipshift);
839
840
841 *retlen = 0;
842
843 while (len) {
844 unsigned long thislen;
845
846 if (chipnum >= cfi->numchips)
847 break;
848
849 if ((len + ofs -1) >> cfi->chipshift)
850 thislen = (1<<cfi->chipshift) - ofs;
851 else
852 thislen = len;
853
854 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
855 if (ret)
856 break;
857
858 *retlen += thislen;
859 len -= thislen;
860 buf += thislen;
861
862 ofs = 0;
863 chipnum++;
864 }
865 return ret;
866}
867
868
869static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
870{
871 DECLARE_WAITQUEUE(wait, current);
872 unsigned long timeo = jiffies + HZ;
873 struct cfi_private *cfi = map->fldrv_priv;
874
875 retry:
Todd Poynor02b15e32005-06-07 00:04:39 +0100876 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (chip->state != FL_READY){
879#if 0
880 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
881#endif
882 set_current_state(TASK_UNINTERRUPTIBLE);
883 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000884
Todd Poynor02b15e32005-06-07 00:04:39 +0100885 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 schedule();
888 remove_wait_queue(&chip->wq, &wait);
889#if 0
890 if(signal_pending(current))
891 return -EINTR;
892#endif
893 timeo = jiffies + HZ;
894
895 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 adr += chip->start;
899
900 chip->state = FL_READY;
901
902 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
903 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
904 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 map_copy_from(map, buf, adr, len);
907
908 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
909 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
910 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
911 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 wake_up(&chip->wq);
Todd Poynor02b15e32005-06-07 00:04:39 +0100914 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 return 0;
917}
918
919static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
920{
921 struct map_info *map = mtd->priv;
922 struct cfi_private *cfi = map->fldrv_priv;
923 unsigned long ofs;
924 int chipnum;
925 int ret = 0;
926
927
928 /* ofs: offset within the first chip that the first read should start */
929
930 /* 8 secsi bytes per chip */
931 chipnum=from>>3;
932 ofs=from & 7;
933
934
935 *retlen = 0;
936
937 while (len) {
938 unsigned long thislen;
939
940 if (chipnum >= cfi->numchips)
941 break;
942
943 if ((len + ofs -1) >> 3)
944 thislen = (1<<3) - ofs;
945 else
946 thislen = len;
947
948 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
949 if (ret)
950 break;
951
952 *retlen += thislen;
953 len -= thislen;
954 buf += thislen;
955
956 ofs = 0;
957 chipnum++;
958 }
959 return ret;
960}
961
962
Todd Poynor02b15e32005-06-07 00:04:39 +0100963static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip, unsigned long adr, map_word datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct cfi_private *cfi = map->fldrv_priv;
966 unsigned long timeo = jiffies + HZ;
967 /*
968 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
969 * have a max write time of a few hundreds usec). However, we should
970 * use the maximum timeout value given by the chip at probe time
971 * instead. Unfortunately, struct flchip does have a field for
972 * maximum timeout, only for typical which can be far too short
973 * depending of the conditions. The ' + 1' is to avoid having a
974 * timeout of 0 jiffies if HZ is smaller than 1000.
975 */
976 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
977 int ret = 0;
978 map_word oldd;
979 int retry_cnt = 0;
980
981 adr += chip->start;
982
Todd Poynor02b15e32005-06-07 00:04:39 +0100983 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ret = get_chip(map, chip, adr, FL_WRITING);
985 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +0100986 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return ret;
988 }
989
990 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
991 __func__, adr, datum.x[0] );
992
993 /*
994 * Check for a NOP for the case when the datum to write is already
995 * present - it saves time and works around buggy chips that corrupt
996 * data at other locations when 0xff is written to a location that
997 * already contains 0xff.
998 */
999 oldd = map_read(map, adr);
1000 if (map_word_equal(map, oldd, datum)) {
1001 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1002 __func__);
1003 goto op_done;
1004 }
1005
Todd Poynor02b15e32005-06-07 00:04:39 +01001006 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001008 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 retry:
1010 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1011 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1012 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1013 map_write(map, datum, adr);
1014 chip->state = FL_WRITING;
1015
Todd Poynor02b15e32005-06-07 00:04:39 +01001016 INVALIDATE_CACHE_UDELAY(map, chip,
1017 adr, map_bankwidth(map),
1018 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001021 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 for (;;) {
1023 if (chip->state != FL_WRITING) {
1024 /* Someone's suspended the write. Sleep */
1025 DECLARE_WAITQUEUE(wait, current);
1026
1027 set_current_state(TASK_UNINTERRUPTIBLE);
1028 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001029 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 schedule();
1031 remove_wait_queue(&chip->wq, &wait);
1032 timeo = jiffies + (HZ / 2); /* FIXME */
Todd Poynor02b15e32005-06-07 00:04:39 +01001033 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 continue;
1035 }
1036
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001037 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001038 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001039 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001040 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001041 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001044 if (chip_ready(map, adr))
1045 break;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001048 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001050 /* Did we succeed? */
1051 if (!chip_good(map, adr, datum)) {
1052 /* reset on all failures. */
1053 map_write( map, CMD(0xF0), chip->start );
1054 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001056 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001057 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001059 ret = -EIO;
1060 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001061 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 op_done:
1063 chip->state = FL_READY;
1064 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001065 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return ret;
1068}
1069
1070
1071static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1072 size_t *retlen, const u_char *buf)
1073{
1074 struct map_info *map = mtd->priv;
1075 struct cfi_private *cfi = map->fldrv_priv;
1076 int ret = 0;
1077 int chipnum;
1078 unsigned long ofs, chipstart;
1079 DECLARE_WAITQUEUE(wait, current);
1080
1081 *retlen = 0;
1082 if (!len)
1083 return 0;
1084
1085 chipnum = to >> cfi->chipshift;
1086 ofs = to - (chipnum << cfi->chipshift);
1087 chipstart = cfi->chips[chipnum].start;
1088
1089 /* If it's not bus-aligned, do the first byte write */
1090 if (ofs & (map_bankwidth(map)-1)) {
1091 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1092 int i = ofs - bus_ofs;
1093 int n = 0;
1094 map_word tmp_buf;
1095
1096 retry:
Todd Poynor02b15e32005-06-07 00:04:39 +01001097 spin_lock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (cfi->chips[chipnum].state != FL_READY) {
1100#if 0
1101 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1102#endif
1103 set_current_state(TASK_UNINTERRUPTIBLE);
1104 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1105
Todd Poynor02b15e32005-06-07 00:04:39 +01001106 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 schedule();
1109 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1110#if 0
1111 if(signal_pending(current))
1112 return -EINTR;
1113#endif
1114 goto retry;
1115 }
1116
1117 /* Load 'tmp_buf' with old contents of flash */
1118 tmp_buf = map_read(map, bus_ofs+chipstart);
1119
Todd Poynor02b15e32005-06-07 00:04:39 +01001120 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /* Number of bytes to copy from buffer */
1123 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1126
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001127 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001129 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ofs += n;
1133 buf += n;
1134 (*retlen) += n;
1135 len -= n;
1136
1137 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001138 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ofs = 0;
1140 if (chipnum == cfi->numchips)
1141 return 0;
1142 }
1143 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* We are now aligned, write as much as possible */
1146 while(len >= map_bankwidth(map)) {
1147 map_word datum;
1148
1149 datum = map_word_load(map, buf);
1150
1151 ret = do_write_oneword(map, &cfi->chips[chipnum],
1152 ofs, datum);
1153 if (ret)
1154 return ret;
1155
1156 ofs += map_bankwidth(map);
1157 buf += map_bankwidth(map);
1158 (*retlen) += map_bankwidth(map);
1159 len -= map_bankwidth(map);
1160
1161 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001162 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ofs = 0;
1164 if (chipnum == cfi->numchips)
1165 return 0;
1166 chipstart = cfi->chips[chipnum].start;
1167 }
1168 }
1169
1170 /* Write the trailing bytes if any */
1171 if (len & (map_bankwidth(map)-1)) {
1172 map_word tmp_buf;
1173
1174 retry1:
Todd Poynor02b15e32005-06-07 00:04:39 +01001175 spin_lock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (cfi->chips[chipnum].state != FL_READY) {
1178#if 0
1179 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1180#endif
1181 set_current_state(TASK_UNINTERRUPTIBLE);
1182 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1183
Todd Poynor02b15e32005-06-07 00:04:39 +01001184 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 schedule();
1187 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1188#if 0
1189 if(signal_pending(current))
1190 return -EINTR;
1191#endif
1192 goto retry1;
1193 }
1194
1195 tmp_buf = map_read(map, ofs + chipstart);
1196
Todd Poynor02b15e32005-06-07 00:04:39 +01001197 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001200
1201 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001203 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 (*retlen) += len;
1207 }
1208
1209 return 0;
1210}
1211
1212
1213/*
1214 * FIXME: interleaved mode not tested, and probably not supported!
1215 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001216static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001217 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001218 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct cfi_private *cfi = map->fldrv_priv;
1221 unsigned long timeo = jiffies + HZ;
1222 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1223 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1224 int ret = -EIO;
1225 unsigned long cmd_adr;
1226 int z, words;
1227 map_word datum;
1228
1229 adr += chip->start;
1230 cmd_adr = adr;
1231
Todd Poynor02b15e32005-06-07 00:04:39 +01001232 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 ret = get_chip(map, chip, adr, FL_WRITING);
1234 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001235 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return ret;
1237 }
1238
1239 datum = map_word_load(map, buf);
1240
1241 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1242 __func__, adr, datum.x[0] );
1243
Todd Poynor02b15e32005-06-07 00:04:39 +01001244 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001246 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1249 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1250 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1251
1252 /* Write Buffer Load */
1253 map_write(map, CMD(0x25), cmd_adr);
1254
1255 chip->state = FL_WRITING_TO_BUFFER;
1256
1257 /* Write length of data to come */
1258 words = len / map_bankwidth(map);
1259 map_write(map, CMD(words - 1), cmd_adr);
1260 /* Write data */
1261 z = 0;
1262 while(z < words * map_bankwidth(map)) {
1263 datum = map_word_load(map, buf);
1264 map_write(map, datum, adr + z);
1265
1266 z += map_bankwidth(map);
1267 buf += map_bankwidth(map);
1268 }
1269 z -= map_bankwidth(map);
1270
1271 adr += z;
1272
1273 /* Write Buffer Program Confirm: GO GO GO */
1274 map_write(map, CMD(0x29), cmd_adr);
1275 chip->state = FL_WRITING;
1276
Todd Poynor02b15e32005-06-07 00:04:39 +01001277 INVALIDATE_CACHE_UDELAY(map, chip,
1278 adr, map_bankwidth(map),
1279 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001281 timeo = jiffies + uWriteTimeout;
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 for (;;) {
1284 if (chip->state != FL_WRITING) {
1285 /* Someone's suspended the write. Sleep */
1286 DECLARE_WAITQUEUE(wait, current);
1287
1288 set_current_state(TASK_UNINTERRUPTIBLE);
1289 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001290 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 schedule();
1292 remove_wait_queue(&chip->wq, &wait);
1293 timeo = jiffies + (HZ / 2); /* FIXME */
Todd Poynor02b15e32005-06-07 00:04:39 +01001294 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 continue;
1296 }
1297
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001298 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1299 break;
1300
Todd Poynor02b15e32005-06-07 00:04:39 +01001301 if (chip_ready(map, adr)) {
1302 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001307 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /* reset on all failures. */
1311 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001312 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* FIXME - should have reset delay before continuing */
1314
Todd Poynor02b15e32005-06-07 00:04:39 +01001315 printk(KERN_WARNING "MTD %s(): software timeout\n",
1316 __func__ );
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 ret = -EIO;
1319 op_done:
1320 chip->state = FL_READY;
1321 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001322 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 return ret;
1325}
1326
1327
1328static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1329 size_t *retlen, const u_char *buf)
1330{
1331 struct map_info *map = mtd->priv;
1332 struct cfi_private *cfi = map->fldrv_priv;
1333 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1334 int ret = 0;
1335 int chipnum;
1336 unsigned long ofs;
1337
1338 *retlen = 0;
1339 if (!len)
1340 return 0;
1341
1342 chipnum = to >> cfi->chipshift;
1343 ofs = to - (chipnum << cfi->chipshift);
1344
1345 /* If it's not bus-aligned, do the first word write */
1346 if (ofs & (map_bankwidth(map)-1)) {
1347 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1348 if (local_len > len)
1349 local_len = len;
1350 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1351 local_len, retlen, buf);
1352 if (ret)
1353 return ret;
1354 ofs += local_len;
1355 buf += local_len;
1356 len -= local_len;
1357
1358 if (ofs >> cfi->chipshift) {
1359 chipnum ++;
1360 ofs = 0;
1361 if (chipnum == cfi->numchips)
1362 return 0;
1363 }
1364 }
1365
1366 /* Write buffer is worth it only if more than one word to write... */
1367 while (len >= map_bankwidth(map) * 2) {
1368 /* We must not cross write block boundaries */
1369 int size = wbufsize - (ofs & (wbufsize-1));
1370
1371 if (size > len)
1372 size = len;
1373 if (size % map_bankwidth(map))
1374 size -= size % map_bankwidth(map);
1375
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001376 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ofs, buf, size);
1378 if (ret)
1379 return ret;
1380
1381 ofs += size;
1382 buf += size;
1383 (*retlen) += size;
1384 len -= size;
1385
1386 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001387 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ofs = 0;
1389 if (chipnum == cfi->numchips)
1390 return 0;
1391 }
1392 }
1393
1394 if (len) {
1395 size_t retlen_dregs = 0;
1396
1397 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1398 len, &retlen_dregs, buf);
1399
1400 *retlen += retlen_dregs;
1401 return ret;
1402 }
1403
1404 return 0;
1405}
1406
1407
1408/*
1409 * Handle devices with one erase region, that only implement
1410 * the chip erase command.
1411 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001412static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct cfi_private *cfi = map->fldrv_priv;
1415 unsigned long timeo = jiffies + HZ;
1416 unsigned long int adr;
1417 DECLARE_WAITQUEUE(wait, current);
1418 int ret = 0;
1419
1420 adr = cfi->addr_unlock1;
1421
Todd Poynor02b15e32005-06-07 00:04:39 +01001422 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ret = get_chip(map, chip, adr, FL_WRITING);
1424 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001425 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return ret;
1427 }
1428
1429 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1430 __func__, chip->start );
1431
Todd Poynor02b15e32005-06-07 00:04:39 +01001432 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001434 xip_disable(map, chip, adr);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1437 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1438 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1439 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1440 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1441 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1442
1443 chip->state = FL_ERASING;
1444 chip->erase_suspended = 0;
1445 chip->in_progress_block_addr = adr;
1446
Todd Poynor02b15e32005-06-07 00:04:39 +01001447 INVALIDATE_CACHE_UDELAY(map, chip,
1448 adr, map->size,
1449 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 timeo = jiffies + (HZ*20);
1452
1453 for (;;) {
1454 if (chip->state != FL_ERASING) {
1455 /* Someone's suspended the erase. Sleep */
1456 set_current_state(TASK_UNINTERRUPTIBLE);
1457 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001458 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 schedule();
1460 remove_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001461 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 continue;
1463 }
1464 if (chip->erase_suspended) {
1465 /* This erase was suspended and resumed.
1466 Adjust the timeout */
1467 timeo = jiffies + (HZ*20); /* FIXME */
1468 chip->erase_suspended = 0;
1469 }
1470
1471 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001474 if (time_after(jiffies, timeo)) {
1475 printk(KERN_WARNING "MTD %s(): software timeout\n",
1476 __func__ );
1477 break;
1478 }
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001481 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001483 /* Did we succeed? */
1484 if (!chip_good(map, adr, map_word_ff(map))) {
1485 /* reset on all failures. */
1486 map_write( map, CMD(0xF0), chip->start );
1487 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001489 ret = -EIO;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001493 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001495 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 return ret;
1498}
1499
1500
Todd Poynor02b15e32005-06-07 00:04:39 +01001501static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr, int len, void *thunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct cfi_private *cfi = map->fldrv_priv;
1504 unsigned long timeo = jiffies + HZ;
1505 DECLARE_WAITQUEUE(wait, current);
1506 int ret = 0;
1507
1508 adr += chip->start;
1509
Todd Poynor02b15e32005-06-07 00:04:39 +01001510 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 ret = get_chip(map, chip, adr, FL_ERASING);
1512 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001513 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return ret;
1515 }
1516
1517 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1518 __func__, adr );
1519
Todd Poynor02b15e32005-06-07 00:04:39 +01001520 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001522 xip_disable(map, chip, adr);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1525 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1526 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1527 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1528 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1529 map_write(map, CMD(0x30), adr);
1530
1531 chip->state = FL_ERASING;
1532 chip->erase_suspended = 0;
1533 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001534
1535 INVALIDATE_CACHE_UDELAY(map, chip,
1536 adr, len,
1537 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 timeo = jiffies + (HZ*20);
1540
1541 for (;;) {
1542 if (chip->state != FL_ERASING) {
1543 /* Someone's suspended the erase. Sleep */
1544 set_current_state(TASK_UNINTERRUPTIBLE);
1545 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001546 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 schedule();
1548 remove_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001549 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 continue;
1551 }
1552 if (chip->erase_suspended) {
1553 /* This erase was suspended and resumed.
1554 Adjust the timeout */
1555 timeo = jiffies + (HZ*20); /* FIXME */
1556 chip->erase_suspended = 0;
1557 }
1558
Todd Poynor02b15e32005-06-07 00:04:39 +01001559 if (chip_ready(map, adr)) {
1560 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001564 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001565 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001566 printk(KERN_WARNING "MTD %s(): software timeout\n",
1567 __func__ );
1568 break;
1569 }
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001572 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001574 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001575 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001576 /* reset on all failures. */
1577 map_write( map, CMD(0xF0), chip->start );
1578 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001580 ret = -EIO;
1581 }
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 chip->state = FL_READY;
1584 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001585 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return ret;
1587}
1588
1589
1590int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1591{
1592 unsigned long ofs, len;
1593 int ret;
1594
1595 ofs = instr->addr;
1596 len = instr->len;
1597
1598 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1599 if (ret)
1600 return ret;
1601
1602 instr->state = MTD_ERASE_DONE;
1603 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return 0;
1606}
1607
1608
1609static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1610{
1611 struct map_info *map = mtd->priv;
1612 struct cfi_private *cfi = map->fldrv_priv;
1613 int ret = 0;
1614
1615 if (instr->addr != 0)
1616 return -EINVAL;
1617
1618 if (instr->len != mtd->size)
1619 return -EINVAL;
1620
1621 ret = do_erase_chip(map, &cfi->chips[0]);
1622 if (ret)
1623 return ret;
1624
1625 instr->state = MTD_ERASE_DONE;
1626 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629}
1630
1631
1632static void cfi_amdstd_sync (struct mtd_info *mtd)
1633{
1634 struct map_info *map = mtd->priv;
1635 struct cfi_private *cfi = map->fldrv_priv;
1636 int i;
1637 struct flchip *chip;
1638 int ret = 0;
1639 DECLARE_WAITQUEUE(wait, current);
1640
1641 for (i=0; !ret && i<cfi->numchips; i++) {
1642 chip = &cfi->chips[i];
1643
1644 retry:
Todd Poynor02b15e32005-06-07 00:04:39 +01001645 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 switch(chip->state) {
1648 case FL_READY:
1649 case FL_STATUS:
1650 case FL_CFI_QUERY:
1651 case FL_JEDEC_QUERY:
1652 chip->oldstate = chip->state;
1653 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001654 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * as the whole point is that nobody can do anything
1656 * with the chip now anyway.
1657 */
1658 case FL_SYNCING:
Todd Poynor02b15e32005-06-07 00:04:39 +01001659 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661
1662 default:
1663 /* Not an idle state */
1664 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001665
Todd Poynor02b15e32005-06-07 00:04:39 +01001666 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 schedule();
1669
1670 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto retry;
1673 }
1674 }
1675
1676 /* Unlock the chips again */
1677
1678 for (i--; i >=0; i--) {
1679 chip = &cfi->chips[i];
1680
Todd Poynor02b15e32005-06-07 00:04:39 +01001681 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (chip->state == FL_SYNCING) {
1684 chip->state = chip->oldstate;
1685 wake_up(&chip->wq);
1686 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001687 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689}
1690
1691
1692static int cfi_amdstd_suspend(struct mtd_info *mtd)
1693{
1694 struct map_info *map = mtd->priv;
1695 struct cfi_private *cfi = map->fldrv_priv;
1696 int i;
1697 struct flchip *chip;
1698 int ret = 0;
1699
1700 for (i=0; !ret && i<cfi->numchips; i++) {
1701 chip = &cfi->chips[i];
1702
Todd Poynor02b15e32005-06-07 00:04:39 +01001703 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 switch(chip->state) {
1706 case FL_READY:
1707 case FL_STATUS:
1708 case FL_CFI_QUERY:
1709 case FL_JEDEC_QUERY:
1710 chip->oldstate = chip->state;
1711 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001712 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * as the whole point is that nobody can do anything
1714 * with the chip now anyway.
1715 */
1716 case FL_PM_SUSPENDED:
1717 break;
1718
1719 default:
1720 ret = -EAGAIN;
1721 break;
1722 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001723 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
1726 /* Unlock the chips again */
1727
1728 if (ret) {
1729 for (i--; i >=0; i--) {
1730 chip = &cfi->chips[i];
1731
Todd Poynor02b15e32005-06-07 00:04:39 +01001732 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (chip->state == FL_PM_SUSPENDED) {
1735 chip->state = chip->oldstate;
1736 wake_up(&chip->wq);
1737 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001738 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return ret;
1743}
1744
1745
1746static void cfi_amdstd_resume(struct mtd_info *mtd)
1747{
1748 struct map_info *map = mtd->priv;
1749 struct cfi_private *cfi = map->fldrv_priv;
1750 int i;
1751 struct flchip *chip;
1752
1753 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 chip = &cfi->chips[i];
1756
Todd Poynor02b15e32005-06-07 00:04:39 +01001757 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (chip->state == FL_PM_SUSPENDED) {
1760 chip->state = FL_READY;
1761 map_write(map, CMD(0xF0), chip->start);
1762 wake_up(&chip->wq);
1763 }
1764 else
1765 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1766
Todd Poynor02b15e32005-06-07 00:04:39 +01001767 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769}
1770
1771static void cfi_amdstd_destroy(struct mtd_info *mtd)
1772{
1773 struct map_info *map = mtd->priv;
1774 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 kfree(cfi->cmdset_priv);
1777 kfree(cfi->cfiq);
1778 kfree(cfi);
1779 kfree(mtd->eraseregions);
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782MODULE_LICENSE("GPL");
1783MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1784MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");