blob: ca0882b5819f4dbcd6fd929770c088e3363f62c0 [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
Haavard Skinnemoen01655082006-08-09 11:06:07 +020048#define MANUFACTURER_ATMEL 0x001F
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define MANUFACTURER_SST 0x00BF
50#define SST49LF004B 0x0060
Ryan Jackson89072ef2006-10-20 14:41:03 -070051#define SST49LF040B 0x0050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010052#define SST49LF008A 0x005a
Haavard Skinnemoen01655082006-08-09 11:06:07 +020053#define AT49BV6416 0x00d6
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
56static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
57static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
58static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
59static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
60static void cfi_amdstd_sync (struct mtd_info *);
61static int cfi_amdstd_suspend (struct mtd_info *);
62static void cfi_amdstd_resume (struct mtd_info *);
63static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
64
65static void cfi_amdstd_destroy(struct mtd_info *);
66
67struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
68static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
69
70static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
71static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
72#include "fwh_lock.h"
73
Haavard Skinnemoen01655082006-08-09 11:06:07 +020074static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
75static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct mtd_chip_driver cfi_amdstd_chipdrv = {
78 .probe = NULL, /* Not usable directly */
79 .destroy = cfi_amdstd_destroy,
80 .name = "cfi_cmdset_0002",
81 .module = THIS_MODULE
82};
83
84
85/* #define DEBUG_CFI_FEATURES */
86
87
88#ifdef DEBUG_CFI_FEATURES
89static void cfi_tell_features(struct cfi_pri_amdstd *extp)
90{
91 const char* erase_suspend[3] = {
92 "Not supported", "Read only", "Read/write"
93 };
94 const char* top_bottom[6] = {
95 "No WP", "8x8KiB sectors at top & bottom, no WP",
96 "Bottom boot", "Top boot",
97 "Uniform, Bottom WP", "Uniform, Top WP"
98 };
99
100 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000101 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 (extp->SiliconRevision & 1) ? "Not required" : "Required");
103
104 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
105 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
106 else
107 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
108
109 if (extp->BlkProt == 0)
110 printk(" Block protection: Not supported\n");
111 else
112 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
113
114
115 printk(" Temporary block unprotect: %s\n",
116 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
117 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
118 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
119 printk(" Burst mode: %s\n",
120 extp->BurstMode ? "Supported" : "Not supported");
121 if (extp->PageMode == 0)
122 printk(" Page mode: Not supported\n");
123 else
124 printk(" Page mode: %d word page\n", extp->PageMode << 2);
125
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000126 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000128 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 extp->VppMax >> 4, extp->VppMax & 0xf);
130
131 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
132 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
133 else
134 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
135}
136#endif
137
138#ifdef AMD_BOOTLOC_BUG
139/* Wheee. Bring me the head of someone at AMD. */
140static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
141{
142 struct map_info *map = mtd->priv;
143 struct cfi_private *cfi = map->fldrv_priv;
144 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
145 __u8 major = extp->MajorVersion;
146 __u8 minor = extp->MinorVersion;
147
148 if (((major << 8) | minor) < 0x3131) {
149 /* CFI version 1.0 => don't trust bootloc */
150 if (cfi->id & 0x80) {
151 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
152 extp->TopBottom = 3; /* top boot */
153 } else {
154 extp->TopBottom = 2; /* bottom boot */
155 }
156 }
157}
158#endif
159
160static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
161{
162 struct map_info *map = mtd->priv;
163 struct cfi_private *cfi = map->fldrv_priv;
164 if (cfi->cfiq->BufWriteTimeoutTyp) {
165 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
166 mtd->write = cfi_amdstd_write_buffers;
167 }
168}
169
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200170/* Atmel chips don't use the same PRI format as AMD chips */
171static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
172{
173 struct map_info *map = mtd->priv;
174 struct cfi_private *cfi = map->fldrv_priv;
175 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
176 struct cfi_pri_atmel atmel_pri;
177
178 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
HÃ¥vard Skinnemoende591da2006-09-15 17:19:31 +0200179 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200180
181 if (atmel_pri.Features & 0x02)
182 extp->EraseSuspend = 2;
183
184 if (atmel_pri.BottomBoot)
185 extp->TopBottom = 2;
186 else
187 extp->TopBottom = 3;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static void fixup_use_secsi(struct mtd_info *mtd, void *param)
191{
192 /* Setup for chips with a secsi area */
193 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
194 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
195}
196
197static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
198{
199 struct map_info *map = mtd->priv;
200 struct cfi_private *cfi = map->fldrv_priv;
201 if ((cfi->cfiq->NumEraseRegions == 1) &&
202 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
203 mtd->erase = cfi_amdstd_erase_chip;
204 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200208/*
209 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
210 * locked by default.
211 */
212static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
213{
214 mtd->lock = cfi_atmel_lock;
215 mtd->unlock = cfi_atmel_unlock;
HÃ¥vard Skinnemoen187ef152006-09-22 10:07:08 +0100216 mtd->flags |= MTD_STUPID_LOCK;
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static struct cfi_fixup cfi_fixup_table[] = {
220#ifdef AMD_BOOTLOC_BUG
221 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
222#endif
223 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
224 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
225 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
226 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
227 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
228 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
229#if !FORCE_WORD_WRITE
230 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
231#endif
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200232 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 { 0, 0, NULL, NULL }
234};
235static struct cfi_fixup jedec_fixup_table[] = {
236 { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
Ryan Jackson89072ef2006-10-20 14:41:03 -0700237 { MANUFACTURER_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100238 { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 { 0, 0, NULL, NULL }
240};
241
242static struct cfi_fixup fixup_table[] = {
243 /* The CFI vendor ids and the JEDEC vendor IDs appear
244 * to be common. It is like the devices id's are as
245 * well. This table is to pick all cases where
246 * we know that is the case.
247 */
248 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200249 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 { 0, 0, NULL, NULL }
251};
252
253
254struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
255{
256 struct cfi_private *cfi = map->fldrv_priv;
257 struct mtd_info *mtd;
258 int i;
259
260 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
261 if (!mtd) {
262 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
263 return NULL;
264 }
265 memset(mtd, 0, sizeof(*mtd));
266 mtd->priv = map;
267 mtd->type = MTD_NORFLASH;
268
269 /* Fill in the default mtd operations */
270 mtd->erase = cfi_amdstd_erase_varsize;
271 mtd->write = cfi_amdstd_write_words;
272 mtd->read = cfi_amdstd_read;
273 mtd->sync = cfi_amdstd_sync;
274 mtd->suspend = cfi_amdstd_suspend;
275 mtd->resume = cfi_amdstd_resume;
276 mtd->flags = MTD_CAP_NORFLASH;
277 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400278 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (cfi->cfi_mode==CFI_MODE_CFI){
281 unsigned char bootloc;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000282 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * It's a real CFI chip, not one for which the probe
284 * routine faked a CFI structure. So we read the feature
285 * table from it.
286 */
287 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
288 struct cfi_pri_amdstd *extp;
289
290 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
291 if (!extp) {
292 kfree(mtd);
293 return NULL;
294 }
295
Todd Poynord88f9772005-07-20 22:01:17 +0100296 if (extp->MajorVersion != '1' ||
297 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
298 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
299 "version %c.%c.\n", extp->MajorVersion,
300 extp->MinorVersion);
301 kfree(extp);
302 kfree(mtd);
303 return NULL;
304 }
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000307 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Apply cfi device specific fixups */
310 cfi_fixup(mtd, cfi_fixup_table);
311
312#ifdef DEBUG_CFI_FEATURES
313 /* Tell the user about it in lots of lovely detail */
314 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 bootloc = extp->TopBottom;
318 if ((bootloc != 2) && (bootloc != 3)) {
319 printk(KERN_WARNING "%s: CFI does not contain boot "
320 "bank location. Assuming top.\n", map->name);
321 bootloc = 2;
322 }
323
324 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
325 printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
328 int j = (cfi->cfiq->NumEraseRegions-1)-i;
329 __u32 swap;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 swap = cfi->cfiq->EraseRegionInfo[i];
332 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
333 cfi->cfiq->EraseRegionInfo[j] = swap;
334 }
335 }
336 /* Set the default CFI lock/unlock addresses */
337 cfi->addr_unlock1 = 0x555;
338 cfi->addr_unlock2 = 0x2aa;
339 /* Modify the unlock address if we are in compatibility mode */
340 if ( /* x16 in x8 mode */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000341 ((cfi->device_type == CFI_DEVICETYPE_X8) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 (cfi->cfiq->InterfaceDesc == 2)) ||
343 /* x32 in x16 mode */
344 ((cfi->device_type == CFI_DEVICETYPE_X16) &&
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000345 (cfi->cfiq->InterfaceDesc == 4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 {
347 cfi->addr_unlock1 = 0xaaa;
348 cfi->addr_unlock2 = 0x555;
349 }
350
351 } /* CFI mode */
352 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
353 /* Apply jedec specific fixups */
354 cfi_fixup(mtd, jedec_fixup_table);
355 }
356 /* Apply generic fixups */
357 cfi_fixup(mtd, fixup_table);
358
359 for (i=0; i< cfi->numchips; i++) {
360 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
361 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
362 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return cfi_amdstd_setup(mtd);
368}
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100369EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
372{
373 struct map_info *map = mtd->priv;
374 struct cfi_private *cfi = map->fldrv_priv;
375 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
376 unsigned long offset = 0;
377 int i,j;
378
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000379 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000381 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mtd->size = devsize * cfi->numchips;
383
384 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
385 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
386 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000387 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
389 goto setup_err;
390 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
393 unsigned long ernum, ersize;
394 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
395 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (mtd->erasesize < ersize) {
398 mtd->erasesize = ersize;
399 }
400 for (j=0; j<cfi->numchips; j++) {
401 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
402 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
403 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
404 }
405 offset += (ersize * ernum);
406 }
407 if (offset != devsize) {
408 /* Argh */
409 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
410 goto setup_err;
411 }
412#if 0
413 // debug
414 for (i=0; i<mtd->numeraseregions;i++){
415 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
416 i,mtd->eraseregions[i].offset,
417 mtd->eraseregions[i].erasesize,
418 mtd->eraseregions[i].numblocks);
419 }
420#endif
421
422 /* FIXME: erase-suspend-program is broken. See
423 http://lists.infradead.org/pipermail/linux-mtd/2003-December/009001.html */
424 printk(KERN_NOTICE "cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.\n");
425
426 __module_get(THIS_MODULE);
427 return mtd;
428
429 setup_err:
430 if(mtd) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800431 kfree(mtd->eraseregions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 kfree(mtd);
433 }
434 kfree(cfi->cmdset_priv);
435 kfree(cfi->cfiq);
436 return NULL;
437}
438
439/*
440 * Return true if the chip is ready.
441 *
442 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
443 * non-suspended sector) and is indicated by no toggle bits toggling.
444 *
445 * Note that anything more complicated than checking if no bits are toggling
446 * (including checking DQ5 for an error status) is tricky to get working
447 * correctly and is therefore not done (particulary with interleaved chips
448 * as each chip must be checked independantly of the others).
449 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100450static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 map_word d, t;
453
454 d = map_read(map, addr);
455 t = map_read(map, addr);
456
457 return map_word_equal(map, d, t);
458}
459
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100460/*
461 * Return true if the chip is ready and has the correct value.
462 *
463 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
464 * non-suspended sector) and it is indicated by no bits toggling.
465 *
466 * Error are indicated by toggling bits or bits held with the wrong value,
467 * or with bits toggling.
468 *
469 * Note that anything more complicated than checking if no bits are toggling
470 * (including checking DQ5 for an error status) is tricky to get working
471 * correctly and is therefore not done (particulary with interleaved chips
472 * as each chip must be checked independantly of the others).
473 *
474 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100475static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100476{
477 map_word oldd, curd;
478
479 oldd = map_read(map, addr);
480 curd = map_read(map, addr);
481
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000482 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100483 map_word_equal(map, curd, expected);
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
487{
488 DECLARE_WAITQUEUE(wait, current);
489 struct cfi_private *cfi = map->fldrv_priv;
490 unsigned long timeo;
491 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
492
493 resettime:
494 timeo = jiffies + HZ;
495 retry:
496 switch (chip->state) {
497
498 case FL_STATUS:
499 for (;;) {
500 if (chip_ready(map, adr))
501 break;
502
503 if (time_after(jiffies, timeo)) {
504 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Todd Poynor02b15e32005-06-07 00:04:39 +0100505 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -EIO;
507 }
Todd Poynor02b15e32005-06-07 00:04:39 +0100508 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 cfi_udelay(1);
Todd Poynor02b15e32005-06-07 00:04:39 +0100510 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Someone else might have been playing with it. */
512 goto retry;
513 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 case FL_READY:
516 case FL_CFI_QUERY:
517 case FL_JEDEC_QUERY:
518 return 0;
519
520 case FL_ERASING:
521 if (mode == FL_WRITING) /* FIXME: Erase-suspend-program appears broken. */
522 goto sleep;
523
Ryan Jackson89072ef2006-10-20 14:41:03 -0700524 if (!( mode == FL_READY
525 || mode == FL_POINT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 || !cfip
527 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))
Ryan Jackson89072ef2006-10-20 14:41:03 -0700528 || (mode == FL_WRITING && (cfip->EraseSuspend & 0x1)
529 )))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto sleep;
531
532 /* We could check to see if we're trying to access the sector
533 * that is currently being erased. However, no user will try
534 * anything like that so we just wait for the timeout. */
535
536 /* Erase suspend */
537 /* It's harmless to issue the Erase-Suspend and Erase-Resume
538 * commands when the erase algorithm isn't in progress. */
539 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
540 chip->oldstate = FL_ERASING;
541 chip->state = FL_ERASE_SUSPENDING;
542 chip->erase_suspended = 1;
543 for (;;) {
544 if (chip_ready(map, adr))
545 break;
546
547 if (time_after(jiffies, timeo)) {
548 /* Should have suspended the erase by now.
549 * Send an Erase-Resume command as either
550 * there was an error (so leave the erase
551 * routine to recover from it) or we trying to
552 * use the erase-in-progress sector. */
553 map_write(map, CMD(0x30), chip->in_progress_block_addr);
554 chip->state = FL_ERASING;
555 chip->oldstate = FL_READY;
556 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
557 return -EIO;
558 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000559
Todd Poynor02b15e32005-06-07 00:04:39 +0100560 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 cfi_udelay(1);
Todd Poynor02b15e32005-06-07 00:04:39 +0100562 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
564 So we can just loop here. */
565 }
566 chip->state = FL_READY;
567 return 0;
568
Todd Poynor02b15e32005-06-07 00:04:39 +0100569 case FL_XIP_WHILE_ERASING:
570 if (mode != FL_READY && mode != FL_POINT &&
571 (!cfip || !(cfip->EraseSuspend&2)))
572 goto sleep;
573 chip->oldstate = chip->state;
574 chip->state = FL_READY;
575 return 0;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case FL_POINT:
578 /* Only if there's no operation suspended... */
579 if (mode == FL_READY && chip->oldstate == FL_READY)
580 return 0;
581
582 default:
583 sleep:
584 set_current_state(TASK_UNINTERRUPTIBLE);
585 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +0100586 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 schedule();
588 remove_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +0100589 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto resettime;
591 }
592}
593
594
595static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
596{
597 struct cfi_private *cfi = map->fldrv_priv;
598
599 switch(chip->oldstate) {
600 case FL_ERASING:
601 chip->state = chip->oldstate;
602 map_write(map, CMD(0x30), chip->in_progress_block_addr);
603 chip->oldstate = FL_READY;
604 chip->state = FL_ERASING;
605 break;
606
Todd Poynor02b15e32005-06-07 00:04:39 +0100607 case FL_XIP_WHILE_ERASING:
608 chip->state = chip->oldstate;
609 chip->oldstate = FL_READY;
610 break;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 case FL_READY:
613 case FL_STATUS:
614 /* We should really make set_vpp() count, rather than doing this */
615 DISABLE_VPP(map);
616 break;
617 default:
618 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
619 }
620 wake_up(&chip->wq);
621}
622
Todd Poynor02b15e32005-06-07 00:04:39 +0100623#ifdef CONFIG_MTD_XIP
624
625/*
626 * No interrupt what so ever can be serviced while the flash isn't in array
627 * mode. This is ensured by the xip_disable() and xip_enable() functions
628 * enclosing any code path where the flash is known not to be in array mode.
629 * And within a XIP disabled code path, only functions marked with __xipram
630 * may be called and nothing else (it's a good thing to inspect generated
631 * assembly to make sure inline functions were actually inlined and that gcc
632 * didn't emit calls to its own support functions). Also configuring MTD CFI
633 * support to a single buswidth and a single interleave is also recommended.
634 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200635
Todd Poynor02b15e32005-06-07 00:04:39 +0100636static void xip_disable(struct map_info *map, struct flchip *chip,
637 unsigned long adr)
638{
639 /* TODO: chips with no XIP use should ignore and return */
640 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
641 local_irq_disable();
642}
643
644static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
645 unsigned long adr)
646{
647 struct cfi_private *cfi = map->fldrv_priv;
648
649 if (chip->state != FL_POINT && chip->state != FL_READY) {
650 map_write(map, CMD(0xf0), adr);
651 chip->state = FL_READY;
652 }
653 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200654 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100655 local_irq_enable();
656}
657
658/*
659 * When a delay is required for the flash operation to complete, the
660 * xip_udelay() function is polling for both the given timeout and pending
661 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000662 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100663 * and interrupts unmasked. Task scheduling might also happen at that
664 * point. The CPU eventually returns from the interrupt or the call to
665 * schedule() and the suspended flash operation is resumed for the remaining
666 * of the delay period.
667 *
668 * Warning: this function _will_ fool interrupt latency tracing tools.
669 */
670
671static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
672 unsigned long adr, int usec)
673{
674 struct cfi_private *cfi = map->fldrv_priv;
675 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
676 map_word status, OK = CMD(0x80);
677 unsigned long suspended, start = xip_currtime();
678 flstate_t oldstate;
679
680 do {
681 cpu_relax();
682 if (xip_irqpending() && extp &&
683 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
684 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
685 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000686 * Let's suspend the erase operation when supported.
687 * Note that we currently don't try to suspend
688 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100689 * operation suspended (imagine what happens
690 * when one chip was already done with the current
691 * operation while another chip suspended it, then
692 * we resume the whole thing at once). Yes, it
693 * can happen!
694 */
695 map_write(map, CMD(0xb0), adr);
696 usec -= xip_elapsed_since(start);
697 suspended = xip_currtime();
698 do {
699 if (xip_elapsed_since(suspended) > 100000) {
700 /*
701 * The chip doesn't want to suspend
702 * after waiting for 100 msecs.
703 * This is a critical error but there
704 * is not much we can do here.
705 */
706 return;
707 }
708 status = map_read(map, adr);
709 } while (!map_word_andequal(map, status, OK, OK));
710
711 /* Suspend succeeded */
712 oldstate = chip->state;
713 if (!map_word_bitsset(map, status, CMD(0x40)))
714 break;
715 chip->state = FL_XIP_WHILE_ERASING;
716 chip->erase_suspended = 1;
717 map_write(map, CMD(0xf0), adr);
718 (void) map_read(map, adr);
719 asm volatile (".rep 8; nop; .endr");
720 local_irq_enable();
721 spin_unlock(chip->mutex);
722 asm volatile (".rep 8; nop; .endr");
723 cond_resched();
724
725 /*
726 * We're back. However someone else might have
727 * decided to go write to the chip if we are in
728 * a suspended erase state. If so let's wait
729 * until it's done.
730 */
731 spin_lock(chip->mutex);
732 while (chip->state != FL_XIP_WHILE_ERASING) {
733 DECLARE_WAITQUEUE(wait, current);
734 set_current_state(TASK_UNINTERRUPTIBLE);
735 add_wait_queue(&chip->wq, &wait);
736 spin_unlock(chip->mutex);
737 schedule();
738 remove_wait_queue(&chip->wq, &wait);
739 spin_lock(chip->mutex);
740 }
741 /* Disallow XIP again */
742 local_irq_disable();
743
744 /* Resume the write or erase operation */
745 map_write(map, CMD(0x30), adr);
746 chip->state = oldstate;
747 start = xip_currtime();
748 } else if (usec >= 1000000/HZ) {
749 /*
750 * Try to save on CPU power when waiting delay
751 * is at least a system timer tick period.
752 * No need to be extremely accurate here.
753 */
754 xip_cpu_idle();
755 }
756 status = map_read(map, adr);
757 } while (!map_word_andequal(map, status, OK, OK)
758 && xip_elapsed_since(start) < usec);
759}
760
761#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
762
763/*
764 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
765 * the flash is actively programming or erasing since we have to poll for
766 * the operation to complete anyway. We can't do that in a generic way with
767 * a XIP setup so do it before the actual flash operation in this case
768 * and stub it out from INVALIDATE_CACHE_UDELAY.
769 */
770#define XIP_INVAL_CACHED_RANGE(map, from, size) \
771 INVALIDATE_CACHED_RANGE(map, from, size)
772
773#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
774 UDELAY(map, chip, adr, usec)
775
776/*
777 * Extra notes:
778 *
779 * Activating this XIP support changes the way the code works a bit. For
780 * example the code to suspend the current process when concurrent access
781 * happens is never executed because xip_udelay() will always return with the
782 * same chip state as it was entered with. This is why there is no care for
783 * the presence of add_wait_queue() or schedule() calls from within a couple
784 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
785 * The queueing and scheduling are always happening within xip_udelay().
786 *
787 * Similarly, get_chip() and put_chip() just happen to always be executed
788 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
789 * is in array mode, therefore never executing many cases therein and not
790 * causing any problem with XIP.
791 */
792
793#else
794
795#define xip_disable(map, chip, adr)
796#define xip_enable(map, chip, adr)
797#define XIP_INVAL_CACHED_RANGE(x...)
798
799#define UDELAY(map, chip, adr, usec) \
800do { \
801 spin_unlock(chip->mutex); \
802 cfi_udelay(usec); \
803 spin_lock(chip->mutex); \
804} while (0)
805
806#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
807do { \
808 spin_unlock(chip->mutex); \
809 INVALIDATE_CACHED_RANGE(map, adr, len); \
810 cfi_udelay(usec); \
811 spin_lock(chip->mutex); \
812} while (0)
813
814#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
817{
818 unsigned long cmd_addr;
819 struct cfi_private *cfi = map->fldrv_priv;
820 int ret;
821
822 adr += chip->start;
823
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000824 /* Ensure cmd read/writes are aligned. */
825 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Todd Poynor02b15e32005-06-07 00:04:39 +0100827 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ret = get_chip(map, chip, cmd_addr, FL_READY);
829 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +0100830 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return ret;
832 }
833
834 if (chip->state != FL_POINT && chip->state != FL_READY) {
835 map_write(map, CMD(0xf0), cmd_addr);
836 chip->state = FL_READY;
837 }
838
839 map_copy_from(map, buf, adr, len);
840
841 put_chip(map, chip, cmd_addr);
842
Todd Poynor02b15e32005-06-07 00:04:39 +0100843 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845}
846
847
848static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
849{
850 struct map_info *map = mtd->priv;
851 struct cfi_private *cfi = map->fldrv_priv;
852 unsigned long ofs;
853 int chipnum;
854 int ret = 0;
855
856 /* ofs: offset within the first chip that the first read should start */
857
858 chipnum = (from >> cfi->chipshift);
859 ofs = from - (chipnum << cfi->chipshift);
860
861
862 *retlen = 0;
863
864 while (len) {
865 unsigned long thislen;
866
867 if (chipnum >= cfi->numchips)
868 break;
869
870 if ((len + ofs -1) >> cfi->chipshift)
871 thislen = (1<<cfi->chipshift) - ofs;
872 else
873 thislen = len;
874
875 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
876 if (ret)
877 break;
878
879 *retlen += thislen;
880 len -= thislen;
881 buf += thislen;
882
883 ofs = 0;
884 chipnum++;
885 }
886 return ret;
887}
888
889
890static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
891{
892 DECLARE_WAITQUEUE(wait, current);
893 unsigned long timeo = jiffies + HZ;
894 struct cfi_private *cfi = map->fldrv_priv;
895
896 retry:
Todd Poynor02b15e32005-06-07 00:04:39 +0100897 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (chip->state != FL_READY){
900#if 0
901 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
902#endif
903 set_current_state(TASK_UNINTERRUPTIBLE);
904 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000905
Todd Poynor02b15e32005-06-07 00:04:39 +0100906 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 schedule();
909 remove_wait_queue(&chip->wq, &wait);
910#if 0
911 if(signal_pending(current))
912 return -EINTR;
913#endif
914 timeo = jiffies + HZ;
915
916 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 adr += chip->start;
920
921 chip->state = FL_READY;
922
923 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
924 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
925 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 map_copy_from(map, buf, adr, len);
928
929 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
930 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
931 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
932 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 wake_up(&chip->wq);
Todd Poynor02b15e32005-06-07 00:04:39 +0100935 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 return 0;
938}
939
940static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
941{
942 struct map_info *map = mtd->priv;
943 struct cfi_private *cfi = map->fldrv_priv;
944 unsigned long ofs;
945 int chipnum;
946 int ret = 0;
947
948
949 /* ofs: offset within the first chip that the first read should start */
950
951 /* 8 secsi bytes per chip */
952 chipnum=from>>3;
953 ofs=from & 7;
954
955
956 *retlen = 0;
957
958 while (len) {
959 unsigned long thislen;
960
961 if (chipnum >= cfi->numchips)
962 break;
963
964 if ((len + ofs -1) >> 3)
965 thislen = (1<<3) - ofs;
966 else
967 thislen = len;
968
969 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
970 if (ret)
971 break;
972
973 *retlen += thislen;
974 len -= thislen;
975 buf += thislen;
976
977 ofs = 0;
978 chipnum++;
979 }
980 return ret;
981}
982
983
Todd Poynor02b15e32005-06-07 00:04:39 +0100984static 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 -0700985{
986 struct cfi_private *cfi = map->fldrv_priv;
987 unsigned long timeo = jiffies + HZ;
988 /*
989 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
990 * have a max write time of a few hundreds usec). However, we should
991 * use the maximum timeout value given by the chip at probe time
992 * instead. Unfortunately, struct flchip does have a field for
993 * maximum timeout, only for typical which can be far too short
994 * depending of the conditions. The ' + 1' is to avoid having a
995 * timeout of 0 jiffies if HZ is smaller than 1000.
996 */
997 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
998 int ret = 0;
999 map_word oldd;
1000 int retry_cnt = 0;
1001
1002 adr += chip->start;
1003
Todd Poynor02b15e32005-06-07 00:04:39 +01001004 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ret = get_chip(map, chip, adr, FL_WRITING);
1006 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001007 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return ret;
1009 }
1010
1011 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1012 __func__, adr, datum.x[0] );
1013
1014 /*
1015 * Check for a NOP for the case when the datum to write is already
1016 * present - it saves time and works around buggy chips that corrupt
1017 * data at other locations when 0xff is written to a location that
1018 * already contains 0xff.
1019 */
1020 oldd = map_read(map, adr);
1021 if (map_word_equal(map, oldd, datum)) {
1022 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1023 __func__);
1024 goto op_done;
1025 }
1026
Todd Poynor02b15e32005-06-07 00:04:39 +01001027 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001029 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 retry:
1031 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1032 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1033 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1034 map_write(map, datum, adr);
1035 chip->state = FL_WRITING;
1036
Todd Poynor02b15e32005-06-07 00:04:39 +01001037 INVALIDATE_CACHE_UDELAY(map, chip,
1038 adr, map_bankwidth(map),
1039 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001042 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 for (;;) {
1044 if (chip->state != FL_WRITING) {
1045 /* Someone's suspended the write. Sleep */
1046 DECLARE_WAITQUEUE(wait, current);
1047
1048 set_current_state(TASK_UNINTERRUPTIBLE);
1049 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001050 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 schedule();
1052 remove_wait_queue(&chip->wq, &wait);
1053 timeo = jiffies + (HZ / 2); /* FIXME */
Todd Poynor02b15e32005-06-07 00:04:39 +01001054 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 continue;
1056 }
1057
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001058 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001059 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001060 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001061 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001062 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001065 if (chip_ready(map, adr))
1066 break;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001069 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001071 /* Did we succeed? */
1072 if (!chip_good(map, adr, datum)) {
1073 /* reset on all failures. */
1074 map_write( map, CMD(0xF0), chip->start );
1075 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001077 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001078 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001080 ret = -EIO;
1081 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001082 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 op_done:
1084 chip->state = FL_READY;
1085 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001086 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 return ret;
1089}
1090
1091
1092static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1093 size_t *retlen, const u_char *buf)
1094{
1095 struct map_info *map = mtd->priv;
1096 struct cfi_private *cfi = map->fldrv_priv;
1097 int ret = 0;
1098 int chipnum;
1099 unsigned long ofs, chipstart;
1100 DECLARE_WAITQUEUE(wait, current);
1101
1102 *retlen = 0;
1103 if (!len)
1104 return 0;
1105
1106 chipnum = to >> cfi->chipshift;
1107 ofs = to - (chipnum << cfi->chipshift);
1108 chipstart = cfi->chips[chipnum].start;
1109
1110 /* If it's not bus-aligned, do the first byte write */
1111 if (ofs & (map_bankwidth(map)-1)) {
1112 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1113 int i = ofs - bus_ofs;
1114 int n = 0;
1115 map_word tmp_buf;
1116
1117 retry:
Todd Poynor02b15e32005-06-07 00:04:39 +01001118 spin_lock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (cfi->chips[chipnum].state != FL_READY) {
1121#if 0
1122 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1123#endif
1124 set_current_state(TASK_UNINTERRUPTIBLE);
1125 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1126
Todd Poynor02b15e32005-06-07 00:04:39 +01001127 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 schedule();
1130 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1131#if 0
1132 if(signal_pending(current))
1133 return -EINTR;
1134#endif
1135 goto retry;
1136 }
1137
1138 /* Load 'tmp_buf' with old contents of flash */
1139 tmp_buf = map_read(map, bus_ofs+chipstart);
1140
Todd Poynor02b15e32005-06-07 00:04:39 +01001141 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /* Number of bytes to copy from buffer */
1144 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1147
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001148 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001150 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ofs += n;
1154 buf += n;
1155 (*retlen) += n;
1156 len -= n;
1157
1158 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001159 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ofs = 0;
1161 if (chipnum == cfi->numchips)
1162 return 0;
1163 }
1164 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* We are now aligned, write as much as possible */
1167 while(len >= map_bankwidth(map)) {
1168 map_word datum;
1169
1170 datum = map_word_load(map, buf);
1171
1172 ret = do_write_oneword(map, &cfi->chips[chipnum],
1173 ofs, datum);
1174 if (ret)
1175 return ret;
1176
1177 ofs += map_bankwidth(map);
1178 buf += map_bankwidth(map);
1179 (*retlen) += map_bankwidth(map);
1180 len -= map_bankwidth(map);
1181
1182 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001183 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ofs = 0;
1185 if (chipnum == cfi->numchips)
1186 return 0;
1187 chipstart = cfi->chips[chipnum].start;
1188 }
1189 }
1190
1191 /* Write the trailing bytes if any */
1192 if (len & (map_bankwidth(map)-1)) {
1193 map_word tmp_buf;
1194
1195 retry1:
Todd Poynor02b15e32005-06-07 00:04:39 +01001196 spin_lock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (cfi->chips[chipnum].state != FL_READY) {
1199#if 0
1200 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1201#endif
1202 set_current_state(TASK_UNINTERRUPTIBLE);
1203 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1204
Todd Poynor02b15e32005-06-07 00:04:39 +01001205 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 schedule();
1208 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1209#if 0
1210 if(signal_pending(current))
1211 return -EINTR;
1212#endif
1213 goto retry1;
1214 }
1215
1216 tmp_buf = map_read(map, ofs + chipstart);
1217
Todd Poynor02b15e32005-06-07 00:04:39 +01001218 spin_unlock(cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001221
1222 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001224 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 (*retlen) += len;
1228 }
1229
1230 return 0;
1231}
1232
1233
1234/*
1235 * FIXME: interleaved mode not tested, and probably not supported!
1236 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001237static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001238 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001239 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct cfi_private *cfi = map->fldrv_priv;
1242 unsigned long timeo = jiffies + HZ;
1243 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1244 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1245 int ret = -EIO;
1246 unsigned long cmd_adr;
1247 int z, words;
1248 map_word datum;
1249
1250 adr += chip->start;
1251 cmd_adr = adr;
1252
Todd Poynor02b15e32005-06-07 00:04:39 +01001253 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ret = get_chip(map, chip, adr, FL_WRITING);
1255 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001256 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return ret;
1258 }
1259
1260 datum = map_word_load(map, buf);
1261
1262 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1263 __func__, adr, datum.x[0] );
1264
Todd Poynor02b15e32005-06-07 00:04:39 +01001265 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001267 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1270 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1271 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1272
1273 /* Write Buffer Load */
1274 map_write(map, CMD(0x25), cmd_adr);
1275
1276 chip->state = FL_WRITING_TO_BUFFER;
1277
1278 /* Write length of data to come */
1279 words = len / map_bankwidth(map);
1280 map_write(map, CMD(words - 1), cmd_adr);
1281 /* Write data */
1282 z = 0;
1283 while(z < words * map_bankwidth(map)) {
1284 datum = map_word_load(map, buf);
1285 map_write(map, datum, adr + z);
1286
1287 z += map_bankwidth(map);
1288 buf += map_bankwidth(map);
1289 }
1290 z -= map_bankwidth(map);
1291
1292 adr += z;
1293
1294 /* Write Buffer Program Confirm: GO GO GO */
1295 map_write(map, CMD(0x29), cmd_adr);
1296 chip->state = FL_WRITING;
1297
Todd Poynor02b15e32005-06-07 00:04:39 +01001298 INVALIDATE_CACHE_UDELAY(map, chip,
1299 adr, map_bankwidth(map),
1300 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001302 timeo = jiffies + uWriteTimeout;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 for (;;) {
1305 if (chip->state != FL_WRITING) {
1306 /* Someone's suspended the write. Sleep */
1307 DECLARE_WAITQUEUE(wait, current);
1308
1309 set_current_state(TASK_UNINTERRUPTIBLE);
1310 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001311 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 schedule();
1313 remove_wait_queue(&chip->wq, &wait);
1314 timeo = jiffies + (HZ / 2); /* FIXME */
Todd Poynor02b15e32005-06-07 00:04:39 +01001315 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 continue;
1317 }
1318
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001319 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1320 break;
1321
Todd Poynor02b15e32005-06-07 00:04:39 +01001322 if (chip_ready(map, adr)) {
1323 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001328 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* reset on all failures. */
1332 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001333 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* FIXME - should have reset delay before continuing */
1335
Todd Poynor02b15e32005-06-07 00:04:39 +01001336 printk(KERN_WARNING "MTD %s(): software timeout\n",
1337 __func__ );
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 ret = -EIO;
1340 op_done:
1341 chip->state = FL_READY;
1342 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001343 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 return ret;
1346}
1347
1348
1349static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1350 size_t *retlen, const u_char *buf)
1351{
1352 struct map_info *map = mtd->priv;
1353 struct cfi_private *cfi = map->fldrv_priv;
1354 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1355 int ret = 0;
1356 int chipnum;
1357 unsigned long ofs;
1358
1359 *retlen = 0;
1360 if (!len)
1361 return 0;
1362
1363 chipnum = to >> cfi->chipshift;
1364 ofs = to - (chipnum << cfi->chipshift);
1365
1366 /* If it's not bus-aligned, do the first word write */
1367 if (ofs & (map_bankwidth(map)-1)) {
1368 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1369 if (local_len > len)
1370 local_len = len;
1371 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1372 local_len, retlen, buf);
1373 if (ret)
1374 return ret;
1375 ofs += local_len;
1376 buf += local_len;
1377 len -= local_len;
1378
1379 if (ofs >> cfi->chipshift) {
1380 chipnum ++;
1381 ofs = 0;
1382 if (chipnum == cfi->numchips)
1383 return 0;
1384 }
1385 }
1386
1387 /* Write buffer is worth it only if more than one word to write... */
1388 while (len >= map_bankwidth(map) * 2) {
1389 /* We must not cross write block boundaries */
1390 int size = wbufsize - (ofs & (wbufsize-1));
1391
1392 if (size > len)
1393 size = len;
1394 if (size % map_bankwidth(map))
1395 size -= size % map_bankwidth(map);
1396
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001397 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ofs, buf, size);
1399 if (ret)
1400 return ret;
1401
1402 ofs += size;
1403 buf += size;
1404 (*retlen) += size;
1405 len -= size;
1406
1407 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001408 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 ofs = 0;
1410 if (chipnum == cfi->numchips)
1411 return 0;
1412 }
1413 }
1414
1415 if (len) {
1416 size_t retlen_dregs = 0;
1417
1418 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1419 len, &retlen_dregs, buf);
1420
1421 *retlen += retlen_dregs;
1422 return ret;
1423 }
1424
1425 return 0;
1426}
1427
1428
1429/*
1430 * Handle devices with one erase region, that only implement
1431 * the chip erase command.
1432 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001433static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 struct cfi_private *cfi = map->fldrv_priv;
1436 unsigned long timeo = jiffies + HZ;
1437 unsigned long int adr;
1438 DECLARE_WAITQUEUE(wait, current);
1439 int ret = 0;
1440
1441 adr = cfi->addr_unlock1;
1442
Todd Poynor02b15e32005-06-07 00:04:39 +01001443 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ret = get_chip(map, chip, adr, FL_WRITING);
1445 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001446 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return ret;
1448 }
1449
1450 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1451 __func__, chip->start );
1452
Todd Poynor02b15e32005-06-07 00:04:39 +01001453 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001455 xip_disable(map, chip, adr);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1458 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1459 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1460 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1461 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1462 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1463
1464 chip->state = FL_ERASING;
1465 chip->erase_suspended = 0;
1466 chip->in_progress_block_addr = adr;
1467
Todd Poynor02b15e32005-06-07 00:04:39 +01001468 INVALIDATE_CACHE_UDELAY(map, chip,
1469 adr, map->size,
1470 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 timeo = jiffies + (HZ*20);
1473
1474 for (;;) {
1475 if (chip->state != FL_ERASING) {
1476 /* Someone's suspended the erase. Sleep */
1477 set_current_state(TASK_UNINTERRUPTIBLE);
1478 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001479 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 schedule();
1481 remove_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001482 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 continue;
1484 }
1485 if (chip->erase_suspended) {
1486 /* This erase was suspended and resumed.
1487 Adjust the timeout */
1488 timeo = jiffies + (HZ*20); /* FIXME */
1489 chip->erase_suspended = 0;
1490 }
1491
1492 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001495 if (time_after(jiffies, timeo)) {
1496 printk(KERN_WARNING "MTD %s(): software timeout\n",
1497 __func__ );
1498 break;
1499 }
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001502 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001504 /* Did we succeed? */
1505 if (!chip_good(map, adr, map_word_ff(map))) {
1506 /* reset on all failures. */
1507 map_write( map, CMD(0xF0), chip->start );
1508 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001510 ret = -EIO;
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001514 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001516 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 return ret;
1519}
1520
1521
Todd Poynor02b15e32005-06-07 00:04:39 +01001522static 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 -07001523{
1524 struct cfi_private *cfi = map->fldrv_priv;
1525 unsigned long timeo = jiffies + HZ;
1526 DECLARE_WAITQUEUE(wait, current);
1527 int ret = 0;
1528
1529 adr += chip->start;
1530
Todd Poynor02b15e32005-06-07 00:04:39 +01001531 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ret = get_chip(map, chip, adr, FL_ERASING);
1533 if (ret) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001534 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return ret;
1536 }
1537
1538 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1539 __func__, adr );
1540
Todd Poynor02b15e32005-06-07 00:04:39 +01001541 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001543 xip_disable(map, chip, adr);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1546 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1547 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1548 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1549 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1550 map_write(map, CMD(0x30), adr);
1551
1552 chip->state = FL_ERASING;
1553 chip->erase_suspended = 0;
1554 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001555
1556 INVALIDATE_CACHE_UDELAY(map, chip,
1557 adr, len,
1558 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 timeo = jiffies + (HZ*20);
1561
1562 for (;;) {
1563 if (chip->state != FL_ERASING) {
1564 /* Someone's suspended the erase. Sleep */
1565 set_current_state(TASK_UNINTERRUPTIBLE);
1566 add_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001567 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 schedule();
1569 remove_wait_queue(&chip->wq, &wait);
Todd Poynor02b15e32005-06-07 00:04:39 +01001570 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 continue;
1572 }
1573 if (chip->erase_suspended) {
1574 /* This erase was suspended and resumed.
1575 Adjust the timeout */
1576 timeo = jiffies + (HZ*20); /* FIXME */
1577 chip->erase_suspended = 0;
1578 }
1579
Todd Poynor02b15e32005-06-07 00:04:39 +01001580 if (chip_ready(map, adr)) {
1581 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001585 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001586 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001587 printk(KERN_WARNING "MTD %s(): software timeout\n",
1588 __func__ );
1589 break;
1590 }
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001593 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001595 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001596 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001597 /* reset on all failures. */
1598 map_write( map, CMD(0xF0), chip->start );
1599 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001601 ret = -EIO;
1602 }
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 chip->state = FL_READY;
1605 put_chip(map, chip, adr);
Todd Poynor02b15e32005-06-07 00:04:39 +01001606 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return ret;
1608}
1609
1610
1611int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1612{
1613 unsigned long ofs, len;
1614 int ret;
1615
1616 ofs = instr->addr;
1617 len = instr->len;
1618
1619 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1620 if (ret)
1621 return ret;
1622
1623 instr->state = MTD_ERASE_DONE;
1624 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627}
1628
1629
1630static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1631{
1632 struct map_info *map = mtd->priv;
1633 struct cfi_private *cfi = map->fldrv_priv;
1634 int ret = 0;
1635
1636 if (instr->addr != 0)
1637 return -EINVAL;
1638
1639 if (instr->len != mtd->size)
1640 return -EINVAL;
1641
1642 ret = do_erase_chip(map, &cfi->chips[0]);
1643 if (ret)
1644 return ret;
1645
1646 instr->state = MTD_ERASE_DONE;
1647 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650}
1651
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001652static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1653 unsigned long adr, int len, void *thunk)
1654{
1655 struct cfi_private *cfi = map->fldrv_priv;
1656 int ret;
1657
1658 spin_lock(chip->mutex);
1659 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1660 if (ret)
1661 goto out_unlock;
1662 chip->state = FL_LOCKING;
1663
1664 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1665 __func__, adr, len);
1666
1667 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1668 cfi->device_type, NULL);
1669 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1670 cfi->device_type, NULL);
1671 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1672 cfi->device_type, NULL);
1673 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1674 cfi->device_type, NULL);
1675 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1676 cfi->device_type, NULL);
1677 map_write(map, CMD(0x40), chip->start + adr);
1678
1679 chip->state = FL_READY;
1680 put_chip(map, chip, adr + chip->start);
1681 ret = 0;
1682
1683out_unlock:
1684 spin_unlock(chip->mutex);
1685 return ret;
1686}
1687
1688static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1689 unsigned long adr, int len, void *thunk)
1690{
1691 struct cfi_private *cfi = map->fldrv_priv;
1692 int ret;
1693
1694 spin_lock(chip->mutex);
1695 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1696 if (ret)
1697 goto out_unlock;
1698 chip->state = FL_UNLOCKING;
1699
1700 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1701 __func__, adr, len);
1702
1703 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1704 cfi->device_type, NULL);
1705 map_write(map, CMD(0x70), adr);
1706
1707 chip->state = FL_READY;
1708 put_chip(map, chip, adr + chip->start);
1709 ret = 0;
1710
1711out_unlock:
1712 spin_unlock(chip->mutex);
1713 return ret;
1714}
1715
1716static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1717{
1718 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1719}
1720
1721static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
1722{
1723 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727static void cfi_amdstd_sync (struct mtd_info *mtd)
1728{
1729 struct map_info *map = mtd->priv;
1730 struct cfi_private *cfi = map->fldrv_priv;
1731 int i;
1732 struct flchip *chip;
1733 int ret = 0;
1734 DECLARE_WAITQUEUE(wait, current);
1735
1736 for (i=0; !ret && i<cfi->numchips; i++) {
1737 chip = &cfi->chips[i];
1738
1739 retry:
Todd Poynor02b15e32005-06-07 00:04:39 +01001740 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 switch(chip->state) {
1743 case FL_READY:
1744 case FL_STATUS:
1745 case FL_CFI_QUERY:
1746 case FL_JEDEC_QUERY:
1747 chip->oldstate = chip->state;
1748 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001749 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * as the whole point is that nobody can do anything
1751 * with the chip now anyway.
1752 */
1753 case FL_SYNCING:
Todd Poynor02b15e32005-06-07 00:04:39 +01001754 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 break;
1756
1757 default:
1758 /* Not an idle state */
1759 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001760
Todd Poynor02b15e32005-06-07 00:04:39 +01001761 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 schedule();
1764
1765 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto retry;
1768 }
1769 }
1770
1771 /* Unlock the chips again */
1772
1773 for (i--; i >=0; i--) {
1774 chip = &cfi->chips[i];
1775
Todd Poynor02b15e32005-06-07 00:04:39 +01001776 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (chip->state == FL_SYNCING) {
1779 chip->state = chip->oldstate;
1780 wake_up(&chip->wq);
1781 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001782 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784}
1785
1786
1787static int cfi_amdstd_suspend(struct mtd_info *mtd)
1788{
1789 struct map_info *map = mtd->priv;
1790 struct cfi_private *cfi = map->fldrv_priv;
1791 int i;
1792 struct flchip *chip;
1793 int ret = 0;
1794
1795 for (i=0; !ret && i<cfi->numchips; i++) {
1796 chip = &cfi->chips[i];
1797
Todd Poynor02b15e32005-06-07 00:04:39 +01001798 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 switch(chip->state) {
1801 case FL_READY:
1802 case FL_STATUS:
1803 case FL_CFI_QUERY:
1804 case FL_JEDEC_QUERY:
1805 chip->oldstate = chip->state;
1806 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001807 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * as the whole point is that nobody can do anything
1809 * with the chip now anyway.
1810 */
1811 case FL_PM_SUSPENDED:
1812 break;
1813
1814 default:
1815 ret = -EAGAIN;
1816 break;
1817 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001818 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
1821 /* Unlock the chips again */
1822
1823 if (ret) {
1824 for (i--; i >=0; i--) {
1825 chip = &cfi->chips[i];
1826
Todd Poynor02b15e32005-06-07 00:04:39 +01001827 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (chip->state == FL_PM_SUSPENDED) {
1830 chip->state = chip->oldstate;
1831 wake_up(&chip->wq);
1832 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001833 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return ret;
1838}
1839
1840
1841static void cfi_amdstd_resume(struct mtd_info *mtd)
1842{
1843 struct map_info *map = mtd->priv;
1844 struct cfi_private *cfi = map->fldrv_priv;
1845 int i;
1846 struct flchip *chip;
1847
1848 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 chip = &cfi->chips[i];
1851
Todd Poynor02b15e32005-06-07 00:04:39 +01001852 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (chip->state == FL_PM_SUSPENDED) {
1855 chip->state = FL_READY;
1856 map_write(map, CMD(0xF0), chip->start);
1857 wake_up(&chip->wq);
1858 }
1859 else
1860 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1861
Todd Poynor02b15e32005-06-07 00:04:39 +01001862 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864}
1865
1866static void cfi_amdstd_destroy(struct mtd_info *mtd)
1867{
1868 struct map_info *map = mtd->priv;
1869 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 kfree(cfi->cmdset_priv);
1872 kfree(cfi->cfiq);
1873 kfree(cfi);
1874 kfree(mtd->eraseregions);
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877MODULE_LICENSE("GPL");
1878MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1879MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");