blob: c93e47d21ce0864ab4792b515945214f51626094 [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 *
Christopher Moore87e92c02008-10-17 05:32:22 +020016 * 25/09/2008 Christopher Moore: TopBottom fixup for many Macronix with CFI V1.0
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Occasionally maintained by Thayne Harbaugh tharbaugh at lnxi dot com
19 *
20 * This code is GPL
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/init.h>
28#include <asm/io.h>
29#include <asm/byteorder.h>
30
31#include <linux/errno.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include <linux/interrupt.h>
35#include <linux/mtd/compatmac.h>
36#include <linux/mtd/map.h>
37#include <linux/mtd/mtd.h>
38#include <linux/mtd/cfi.h>
Todd Poynor02b15e32005-06-07 00:04:39 +010039#include <linux/mtd/xip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define AMD_BOOTLOC_BUG
42#define FORCE_WORD_WRITE 0
43
44#define MAX_WORD_RETRIES 3
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define SST49LF004B 0x0060
Ryan Jackson89072ef2006-10-20 14:41:03 -070047#define SST49LF040B 0x0050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010048#define SST49LF008A 0x005a
Haavard Skinnemoen01655082006-08-09 11:06:07 +020049#define AT49BV6416 0x00d6
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
52static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
53static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
55static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
56static void cfi_amdstd_sync (struct mtd_info *);
57static int cfi_amdstd_suspend (struct mtd_info *);
58static void cfi_amdstd_resume (struct mtd_info *);
59static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
60
61static void cfi_amdstd_destroy(struct mtd_info *);
62
63struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
64static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
65
66static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
67static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
68#include "fwh_lock.h"
69
Adrian Hunter69423d92008-12-10 13:37:21 +000070static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
71static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +020072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static struct mtd_chip_driver cfi_amdstd_chipdrv = {
74 .probe = NULL, /* Not usable directly */
75 .destroy = cfi_amdstd_destroy,
76 .name = "cfi_cmdset_0002",
77 .module = THIS_MODULE
78};
79
80
81/* #define DEBUG_CFI_FEATURES */
82
83
84#ifdef DEBUG_CFI_FEATURES
85static void cfi_tell_features(struct cfi_pri_amdstd *extp)
86{
87 const char* erase_suspend[3] = {
88 "Not supported", "Read only", "Read/write"
89 };
90 const char* top_bottom[6] = {
91 "No WP", "8x8KiB sectors at top & bottom, no WP",
92 "Bottom boot", "Top boot",
93 "Uniform, Bottom WP", "Uniform, Top WP"
94 };
95
96 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +000097 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 (extp->SiliconRevision & 1) ? "Not required" : "Required");
99
100 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
101 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
102 else
103 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
104
105 if (extp->BlkProt == 0)
106 printk(" Block protection: Not supported\n");
107 else
108 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
109
110
111 printk(" Temporary block unprotect: %s\n",
112 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
113 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
114 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
115 printk(" Burst mode: %s\n",
116 extp->BurstMode ? "Supported" : "Not supported");
117 if (extp->PageMode == 0)
118 printk(" Page mode: Not supported\n");
119 else
120 printk(" Page mode: %d word page\n", extp->PageMode << 2);
121
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000122 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000124 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 extp->VppMax >> 4, extp->VppMax & 0xf);
126
127 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
128 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
129 else
130 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
131}
132#endif
133
134#ifdef AMD_BOOTLOC_BUG
135/* Wheee. Bring me the head of someone at AMD. */
136static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
137{
138 struct map_info *map = mtd->priv;
139 struct cfi_private *cfi = map->fldrv_priv;
140 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
141 __u8 major = extp->MajorVersion;
142 __u8 minor = extp->MinorVersion;
143
144 if (((major << 8) | minor) < 0x3131) {
145 /* CFI version 1.0 => don't trust bootloc */
Christopher Moore87e92c02008-10-17 05:32:22 +0200146
147 DEBUG(MTD_DEBUG_LEVEL1,
148 "%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
149 map->name, cfi->mfr, cfi->id);
150
151 /* AFAICS all 29LV400 with a bottom boot block have a device ID
152 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
153 * These were badly detected as they have the 0x80 bit set
154 * so treat them as a special case.
155 */
156 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
157
158 /* Macronix added CFI to their 2nd generation
159 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
160 * Fujitsu, Spansion, EON, ESI and older Macronix)
161 * has CFI.
162 *
163 * Therefore also check the manufacturer.
164 * This reduces the risk of false detection due to
165 * the 8-bit device ID.
166 */
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100167 (cfi->mfr == CFI_MFR_MACRONIX)) {
Christopher Moore87e92c02008-10-17 05:32:22 +0200168 DEBUG(MTD_DEBUG_LEVEL1,
169 "%s: Macronix MX29LV400C with bottom boot block"
170 " detected\n", map->name);
171 extp->TopBottom = 2; /* bottom boot */
172 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (cfi->id & 0x80) {
174 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
175 extp->TopBottom = 3; /* top boot */
176 } else {
177 extp->TopBottom = 2; /* bottom boot */
178 }
Christopher Moore87e92c02008-10-17 05:32:22 +0200179
180 DEBUG(MTD_DEBUG_LEVEL1,
181 "%s: AMD CFI PRI V%c.%c has no boot block field;"
182 " deduced %s from Device ID\n", map->name, major, minor,
183 extp->TopBottom == 2 ? "bottom" : "top");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185}
186#endif
187
188static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
189{
190 struct map_info *map = mtd->priv;
191 struct cfi_private *cfi = map->fldrv_priv;
192 if (cfi->cfiq->BufWriteTimeoutTyp) {
193 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
194 mtd->write = cfi_amdstd_write_buffers;
195 }
196}
197
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200198/* Atmel chips don't use the same PRI format as AMD chips */
199static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
200{
201 struct map_info *map = mtd->priv;
202 struct cfi_private *cfi = map->fldrv_priv;
203 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
204 struct cfi_pri_atmel atmel_pri;
205
206 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
HÃ¥vard Skinnemoende591da2006-09-15 17:19:31 +0200207 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200208
209 if (atmel_pri.Features & 0x02)
210 extp->EraseSuspend = 2;
211
Haavard Skinnemoenbe8f78b2008-09-30 13:55:33 +0200212 /* Some chips got it backwards... */
213 if (cfi->id == AT49BV6416) {
214 if (atmel_pri.BottomBoot)
215 extp->TopBottom = 3;
216 else
217 extp->TopBottom = 2;
218 } else {
219 if (atmel_pri.BottomBoot)
220 extp->TopBottom = 2;
221 else
222 extp->TopBottom = 3;
223 }
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100224
225 /* burst write mode not supported */
226 cfi->cfiq->BufWriteTimeoutTyp = 0;
227 cfi->cfiq->BufWriteTimeoutMax = 0;
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static void fixup_use_secsi(struct mtd_info *mtd, void *param)
231{
232 /* Setup for chips with a secsi area */
233 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
234 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
235}
236
237static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
238{
239 struct map_info *map = mtd->priv;
240 struct cfi_private *cfi = map->fldrv_priv;
241 if ((cfi->cfiq->NumEraseRegions == 1) &&
242 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
243 mtd->erase = cfi_amdstd_erase_chip;
244 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200248/*
249 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
250 * locked by default.
251 */
252static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
253{
254 mtd->lock = cfi_atmel_lock;
255 mtd->unlock = cfi_atmel_unlock;
Justin Treone619a752008-01-30 10:25:49 -0800256 mtd->flags |= MTD_POWERUP_LOCK;
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200257}
258
Trent Piepho70b07252008-03-30 21:19:30 -0700259static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
260{
261 struct map_info *map = mtd->priv;
262 struct cfi_private *cfi = map->fldrv_priv;
263
264 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
265 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
266 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
267 }
268}
269
270static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
271{
272 struct map_info *map = mtd->priv;
273 struct cfi_private *cfi = map->fldrv_priv;
274
275 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
276 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
277 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
278 }
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100282 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#ifdef AMD_BOOTLOC_BUG
284 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100285 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#endif
287 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
288 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
289 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
290 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
291 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
292 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
Trent Piepho70b07252008-03-30 21:19:30 -0700293 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, },
294 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, },
295 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, },
296 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#if !FORCE_WORD_WRITE
298 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
299#endif
300 { 0, 0, NULL, NULL }
301};
302static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100303 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
304 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
305 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 { 0, 0, NULL, NULL }
307};
308
309static struct cfi_fixup fixup_table[] = {
310 /* The CFI vendor ids and the JEDEC vendor IDs appear
311 * to be common. It is like the devices id's are as
312 * well. This table is to pick all cases where
313 * we know that is the case.
314 */
315 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200316 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { 0, 0, NULL, NULL }
318};
319
320
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100321static void cfi_fixup_major_minor(struct cfi_private *cfi,
322 struct cfi_pri_amdstd *extp)
323{
324 if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
325 extp->MajorVersion == '0')
326 extp->MajorVersion = '1';
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
330{
331 struct cfi_private *cfi = map->fldrv_priv;
332 struct mtd_info *mtd;
333 int i;
334
Burman Yan95b93a02006-11-15 21:10:29 +0200335 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!mtd) {
337 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
338 return NULL;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 mtd->priv = map;
341 mtd->type = MTD_NORFLASH;
342
343 /* Fill in the default mtd operations */
344 mtd->erase = cfi_amdstd_erase_varsize;
345 mtd->write = cfi_amdstd_write_words;
346 mtd->read = cfi_amdstd_read;
347 mtd->sync = cfi_amdstd_sync;
348 mtd->suspend = cfi_amdstd_suspend;
349 mtd->resume = cfi_amdstd_resume;
350 mtd->flags = MTD_CAP_NORFLASH;
351 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400352 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (cfi->cfi_mode==CFI_MODE_CFI){
355 unsigned char bootloc;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000356 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * It's a real CFI chip, not one for which the probe
358 * routine faked a CFI structure. So we read the feature
359 * table from it.
360 */
361 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
362 struct cfi_pri_amdstd *extp;
363
364 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
365 if (!extp) {
366 kfree(mtd);
367 return NULL;
368 }
369
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100370 cfi_fixup_major_minor(cfi, extp);
371
Todd Poynord88f9772005-07-20 22:01:17 +0100372 if (extp->MajorVersion != '1' ||
373 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
374 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
375 "version %c.%c.\n", extp->MajorVersion,
376 extp->MinorVersion);
377 kfree(extp);
378 kfree(mtd);
379 return NULL;
380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000383 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* Apply cfi device specific fixups */
386 cfi_fixup(mtd, cfi_fixup_table);
387
388#ifdef DEBUG_CFI_FEATURES
389 /* Tell the user about it in lots of lovely detail */
390 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000391#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 bootloc = extp->TopBottom;
394 if ((bootloc != 2) && (bootloc != 3)) {
395 printk(KERN_WARNING "%s: CFI does not contain boot "
396 "bank location. Assuming top.\n", map->name);
397 bootloc = 2;
398 }
399
400 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
401 printk(KERN_WARNING "%s: Swapping erase regions for broken CFI table.\n", map->name);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
404 int j = (cfi->cfiq->NumEraseRegions-1)-i;
405 __u32 swap;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 swap = cfi->cfiq->EraseRegionInfo[i];
408 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
409 cfi->cfiq->EraseRegionInfo[j] = swap;
410 }
411 }
412 /* Set the default CFI lock/unlock addresses */
413 cfi->addr_unlock1 = 0x555;
414 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 } /* CFI mode */
417 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
418 /* Apply jedec specific fixups */
419 cfi_fixup(mtd, jedec_fixup_table);
420 }
421 /* Apply generic fixups */
422 cfi_fixup(mtd, fixup_table);
423
424 for (i=0; i< cfi->numchips; i++) {
425 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
426 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
427 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800428 cfi->chips[i].ref_point_counter = 0;
429 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000430 }
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return cfi_amdstd_setup(mtd);
435}
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100436EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
439{
440 struct map_info *map = mtd->priv;
441 struct cfi_private *cfi = map->fldrv_priv;
442 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
443 unsigned long offset = 0;
444 int i,j;
445
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000446 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000448 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 mtd->size = devsize * cfi->numchips;
450
451 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
452 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
453 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000454 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
456 goto setup_err;
457 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
460 unsigned long ernum, ersize;
461 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
462 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (mtd->erasesize < ersize) {
465 mtd->erasesize = ersize;
466 }
467 for (j=0; j<cfi->numchips; j++) {
468 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
469 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
470 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
471 }
472 offset += (ersize * ernum);
473 }
474 if (offset != devsize) {
475 /* Argh */
476 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
477 goto setup_err;
478 }
479#if 0
480 // debug
481 for (i=0; i<mtd->numeraseregions;i++){
482 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
483 i,mtd->eraseregions[i].offset,
484 mtd->eraseregions[i].erasesize,
485 mtd->eraseregions[i].numblocks);
486 }
487#endif
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 __module_get(THIS_MODULE);
490 return mtd;
491
492 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100493 kfree(mtd->eraseregions);
494 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 kfree(cfi->cmdset_priv);
496 kfree(cfi->cfiq);
497 return NULL;
498}
499
500/*
501 * Return true if the chip is ready.
502 *
503 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
504 * non-suspended sector) and is indicated by no toggle bits toggling.
505 *
506 * Note that anything more complicated than checking if no bits are toggling
507 * (including checking DQ5 for an error status) is tricky to get working
508 * correctly and is therefore not done (particulary with interleaved chips
509 * as each chip must be checked independantly of the others).
510 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100511static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 map_word d, t;
514
515 d = map_read(map, addr);
516 t = map_read(map, addr);
517
518 return map_word_equal(map, d, t);
519}
520
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100521/*
522 * Return true if the chip is ready and has the correct value.
523 *
524 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
525 * non-suspended sector) and it is indicated by no bits toggling.
526 *
527 * Error are indicated by toggling bits or bits held with the wrong value,
528 * or with bits toggling.
529 *
530 * Note that anything more complicated than checking if no bits are toggling
531 * (including checking DQ5 for an error status) is tricky to get working
532 * correctly and is therefore not done (particulary with interleaved chips
533 * as each chip must be checked independantly of the others).
534 *
535 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100536static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100537{
538 map_word oldd, curd;
539
540 oldd = map_read(map, addr);
541 curd = map_read(map, addr);
542
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000543 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100544 map_word_equal(map, curd, expected);
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
548{
549 DECLARE_WAITQUEUE(wait, current);
550 struct cfi_private *cfi = map->fldrv_priv;
551 unsigned long timeo;
552 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
553
554 resettime:
555 timeo = jiffies + HZ;
556 retry:
557 switch (chip->state) {
558
559 case FL_STATUS:
560 for (;;) {
561 if (chip_ready(map, adr))
562 break;
563
564 if (time_after(jiffies, timeo)) {
565 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -EIO;
567 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200568 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200570 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Someone else might have been playing with it. */
572 goto retry;
573 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 case FL_READY:
576 case FL_CFI_QUERY:
577 case FL_JEDEC_QUERY:
578 return 0;
579
580 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100581 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
582 !(mode == FL_READY || mode == FL_POINT ||
583 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto sleep;
585
586 /* We could check to see if we're trying to access the sector
587 * that is currently being erased. However, no user will try
588 * anything like that so we just wait for the timeout. */
589
590 /* Erase suspend */
591 /* It's harmless to issue the Erase-Suspend and Erase-Resume
592 * commands when the erase algorithm isn't in progress. */
593 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
594 chip->oldstate = FL_ERASING;
595 chip->state = FL_ERASE_SUSPENDING;
596 chip->erase_suspended = 1;
597 for (;;) {
598 if (chip_ready(map, adr))
599 break;
600
601 if (time_after(jiffies, timeo)) {
602 /* Should have suspended the erase by now.
603 * Send an Erase-Resume command as either
604 * there was an error (so leave the erase
605 * routine to recover from it) or we trying to
606 * use the erase-in-progress sector. */
607 map_write(map, CMD(0x30), chip->in_progress_block_addr);
608 chip->state = FL_ERASING;
609 chip->oldstate = FL_READY;
610 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
611 return -EIO;
612 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000613
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200614 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200616 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
618 So we can just loop here. */
619 }
620 chip->state = FL_READY;
621 return 0;
622
Todd Poynor02b15e32005-06-07 00:04:39 +0100623 case FL_XIP_WHILE_ERASING:
624 if (mode != FL_READY && mode != FL_POINT &&
625 (!cfip || !(cfip->EraseSuspend&2)))
626 goto sleep;
627 chip->oldstate = chip->state;
628 chip->state = FL_READY;
629 return 0;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 case FL_POINT:
632 /* Only if there's no operation suspended... */
633 if (mode == FL_READY && chip->oldstate == FL_READY)
634 return 0;
635
636 default:
637 sleep:
638 set_current_state(TASK_UNINTERRUPTIBLE);
639 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200640 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 schedule();
642 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200643 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto resettime;
645 }
646}
647
648
649static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
650{
651 struct cfi_private *cfi = map->fldrv_priv;
652
653 switch(chip->oldstate) {
654 case FL_ERASING:
655 chip->state = chip->oldstate;
656 map_write(map, CMD(0x30), chip->in_progress_block_addr);
657 chip->oldstate = FL_READY;
658 chip->state = FL_ERASING;
659 break;
660
Todd Poynor02b15e32005-06-07 00:04:39 +0100661 case FL_XIP_WHILE_ERASING:
662 chip->state = chip->oldstate;
663 chip->oldstate = FL_READY;
664 break;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 case FL_READY:
667 case FL_STATUS:
668 /* We should really make set_vpp() count, rather than doing this */
669 DISABLE_VPP(map);
670 break;
671 default:
672 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
673 }
674 wake_up(&chip->wq);
675}
676
Todd Poynor02b15e32005-06-07 00:04:39 +0100677#ifdef CONFIG_MTD_XIP
678
679/*
680 * No interrupt what so ever can be serviced while the flash isn't in array
681 * mode. This is ensured by the xip_disable() and xip_enable() functions
682 * enclosing any code path where the flash is known not to be in array mode.
683 * And within a XIP disabled code path, only functions marked with __xipram
684 * may be called and nothing else (it's a good thing to inspect generated
685 * assembly to make sure inline functions were actually inlined and that gcc
686 * didn't emit calls to its own support functions). Also configuring MTD CFI
687 * support to a single buswidth and a single interleave is also recommended.
688 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200689
Todd Poynor02b15e32005-06-07 00:04:39 +0100690static void xip_disable(struct map_info *map, struct flchip *chip,
691 unsigned long adr)
692{
693 /* TODO: chips with no XIP use should ignore and return */
694 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
695 local_irq_disable();
696}
697
698static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
699 unsigned long adr)
700{
701 struct cfi_private *cfi = map->fldrv_priv;
702
703 if (chip->state != FL_POINT && chip->state != FL_READY) {
704 map_write(map, CMD(0xf0), adr);
705 chip->state = FL_READY;
706 }
707 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200708 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100709 local_irq_enable();
710}
711
712/*
713 * When a delay is required for the flash operation to complete, the
714 * xip_udelay() function is polling for both the given timeout and pending
715 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000716 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100717 * and interrupts unmasked. Task scheduling might also happen at that
718 * point. The CPU eventually returns from the interrupt or the call to
719 * schedule() and the suspended flash operation is resumed for the remaining
720 * of the delay period.
721 *
722 * Warning: this function _will_ fool interrupt latency tracing tools.
723 */
724
725static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
726 unsigned long adr, int usec)
727{
728 struct cfi_private *cfi = map->fldrv_priv;
729 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
730 map_word status, OK = CMD(0x80);
731 unsigned long suspended, start = xip_currtime();
732 flstate_t oldstate;
733
734 do {
735 cpu_relax();
736 if (xip_irqpending() && extp &&
737 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
738 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
739 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000740 * Let's suspend the erase operation when supported.
741 * Note that we currently don't try to suspend
742 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100743 * operation suspended (imagine what happens
744 * when one chip was already done with the current
745 * operation while another chip suspended it, then
746 * we resume the whole thing at once). Yes, it
747 * can happen!
748 */
749 map_write(map, CMD(0xb0), adr);
750 usec -= xip_elapsed_since(start);
751 suspended = xip_currtime();
752 do {
753 if (xip_elapsed_since(suspended) > 100000) {
754 /*
755 * The chip doesn't want to suspend
756 * after waiting for 100 msecs.
757 * This is a critical error but there
758 * is not much we can do here.
759 */
760 return;
761 }
762 status = map_read(map, adr);
763 } while (!map_word_andequal(map, status, OK, OK));
764
765 /* Suspend succeeded */
766 oldstate = chip->state;
767 if (!map_word_bitsset(map, status, CMD(0x40)))
768 break;
769 chip->state = FL_XIP_WHILE_ERASING;
770 chip->erase_suspended = 1;
771 map_write(map, CMD(0xf0), adr);
772 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200773 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100774 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200775 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200776 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100777 cond_resched();
778
779 /*
780 * We're back. However someone else might have
781 * decided to go write to the chip if we are in
782 * a suspended erase state. If so let's wait
783 * until it's done.
784 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200785 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100786 while (chip->state != FL_XIP_WHILE_ERASING) {
787 DECLARE_WAITQUEUE(wait, current);
788 set_current_state(TASK_UNINTERRUPTIBLE);
789 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200790 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100791 schedule();
792 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200793 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100794 }
795 /* Disallow XIP again */
796 local_irq_disable();
797
798 /* Resume the write or erase operation */
799 map_write(map, CMD(0x30), adr);
800 chip->state = oldstate;
801 start = xip_currtime();
802 } else if (usec >= 1000000/HZ) {
803 /*
804 * Try to save on CPU power when waiting delay
805 * is at least a system timer tick period.
806 * No need to be extremely accurate here.
807 */
808 xip_cpu_idle();
809 }
810 status = map_read(map, adr);
811 } while (!map_word_andequal(map, status, OK, OK)
812 && xip_elapsed_since(start) < usec);
813}
814
815#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
816
817/*
818 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
819 * the flash is actively programming or erasing since we have to poll for
820 * the operation to complete anyway. We can't do that in a generic way with
821 * a XIP setup so do it before the actual flash operation in this case
822 * and stub it out from INVALIDATE_CACHE_UDELAY.
823 */
824#define XIP_INVAL_CACHED_RANGE(map, from, size) \
825 INVALIDATE_CACHED_RANGE(map, from, size)
826
827#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
828 UDELAY(map, chip, adr, usec)
829
830/*
831 * Extra notes:
832 *
833 * Activating this XIP support changes the way the code works a bit. For
834 * example the code to suspend the current process when concurrent access
835 * happens is never executed because xip_udelay() will always return with the
836 * same chip state as it was entered with. This is why there is no care for
837 * the presence of add_wait_queue() or schedule() calls from within a couple
838 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
839 * The queueing and scheduling are always happening within xip_udelay().
840 *
841 * Similarly, get_chip() and put_chip() just happen to always be executed
842 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
843 * is in array mode, therefore never executing many cases therein and not
844 * causing any problem with XIP.
845 */
846
847#else
848
849#define xip_disable(map, chip, adr)
850#define xip_enable(map, chip, adr)
851#define XIP_INVAL_CACHED_RANGE(x...)
852
853#define UDELAY(map, chip, adr, usec) \
854do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200855 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100856 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200857 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100858} while (0)
859
860#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
861do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200862 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100863 INVALIDATE_CACHED_RANGE(map, adr, len); \
864 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200865 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100866} while (0)
867
868#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
871{
872 unsigned long cmd_addr;
873 struct cfi_private *cfi = map->fldrv_priv;
874 int ret;
875
876 adr += chip->start;
877
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000878 /* Ensure cmd read/writes are aligned. */
879 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200881 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ret = get_chip(map, chip, cmd_addr, FL_READY);
883 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200884 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return ret;
886 }
887
888 if (chip->state != FL_POINT && chip->state != FL_READY) {
889 map_write(map, CMD(0xf0), cmd_addr);
890 chip->state = FL_READY;
891 }
892
893 map_copy_from(map, buf, adr, len);
894
895 put_chip(map, chip, cmd_addr);
896
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200897 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 0;
899}
900
901
902static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
903{
904 struct map_info *map = mtd->priv;
905 struct cfi_private *cfi = map->fldrv_priv;
906 unsigned long ofs;
907 int chipnum;
908 int ret = 0;
909
910 /* ofs: offset within the first chip that the first read should start */
911
912 chipnum = (from >> cfi->chipshift);
913 ofs = from - (chipnum << cfi->chipshift);
914
915
916 *retlen = 0;
917
918 while (len) {
919 unsigned long thislen;
920
921 if (chipnum >= cfi->numchips)
922 break;
923
924 if ((len + ofs -1) >> cfi->chipshift)
925 thislen = (1<<cfi->chipshift) - ofs;
926 else
927 thislen = len;
928
929 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
930 if (ret)
931 break;
932
933 *retlen += thislen;
934 len -= thislen;
935 buf += thislen;
936
937 ofs = 0;
938 chipnum++;
939 }
940 return ret;
941}
942
943
944static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
945{
946 DECLARE_WAITQUEUE(wait, current);
947 unsigned long timeo = jiffies + HZ;
948 struct cfi_private *cfi = map->fldrv_priv;
949
950 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200951 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (chip->state != FL_READY){
954#if 0
955 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
956#endif
957 set_current_state(TASK_UNINTERRUPTIBLE);
958 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000959
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200960 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 schedule();
963 remove_wait_queue(&chip->wq, &wait);
964#if 0
965 if(signal_pending(current))
966 return -EINTR;
967#endif
968 timeo = jiffies + HZ;
969
970 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 adr += chip->start;
974
975 chip->state = FL_READY;
976
977 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
978 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
979 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 map_copy_from(map, buf, adr, len);
982
983 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
984 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
985 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
986 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200989 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return 0;
992}
993
994static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
995{
996 struct map_info *map = mtd->priv;
997 struct cfi_private *cfi = map->fldrv_priv;
998 unsigned long ofs;
999 int chipnum;
1000 int ret = 0;
1001
1002
1003 /* ofs: offset within the first chip that the first read should start */
1004
1005 /* 8 secsi bytes per chip */
1006 chipnum=from>>3;
1007 ofs=from & 7;
1008
1009
1010 *retlen = 0;
1011
1012 while (len) {
1013 unsigned long thislen;
1014
1015 if (chipnum >= cfi->numchips)
1016 break;
1017
1018 if ((len + ofs -1) >> 3)
1019 thislen = (1<<3) - ofs;
1020 else
1021 thislen = len;
1022
1023 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1024 if (ret)
1025 break;
1026
1027 *retlen += thislen;
1028 len -= thislen;
1029 buf += thislen;
1030
1031 ofs = 0;
1032 chipnum++;
1033 }
1034 return ret;
1035}
1036
1037
Todd Poynor02b15e32005-06-07 00:04:39 +01001038static 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 -07001039{
1040 struct cfi_private *cfi = map->fldrv_priv;
1041 unsigned long timeo = jiffies + HZ;
1042 /*
1043 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1044 * have a max write time of a few hundreds usec). However, we should
1045 * use the maximum timeout value given by the chip at probe time
1046 * instead. Unfortunately, struct flchip does have a field for
1047 * maximum timeout, only for typical which can be far too short
1048 * depending of the conditions. The ' + 1' is to avoid having a
1049 * timeout of 0 jiffies if HZ is smaller than 1000.
1050 */
1051 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1052 int ret = 0;
1053 map_word oldd;
1054 int retry_cnt = 0;
1055
1056 adr += chip->start;
1057
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001058 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ret = get_chip(map, chip, adr, FL_WRITING);
1060 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001061 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return ret;
1063 }
1064
1065 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1066 __func__, adr, datum.x[0] );
1067
1068 /*
1069 * Check for a NOP for the case when the datum to write is already
1070 * present - it saves time and works around buggy chips that corrupt
1071 * data at other locations when 0xff is written to a location that
1072 * already contains 0xff.
1073 */
1074 oldd = map_read(map, adr);
1075 if (map_word_equal(map, oldd, datum)) {
1076 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1077 __func__);
1078 goto op_done;
1079 }
1080
Todd Poynor02b15e32005-06-07 00:04:39 +01001081 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001083 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 retry:
1085 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1086 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1087 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1088 map_write(map, datum, adr);
1089 chip->state = FL_WRITING;
1090
Todd Poynor02b15e32005-06-07 00:04:39 +01001091 INVALIDATE_CACHE_UDELAY(map, chip,
1092 adr, map_bankwidth(map),
1093 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001096 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 for (;;) {
1098 if (chip->state != FL_WRITING) {
1099 /* Someone's suspended the write. Sleep */
1100 DECLARE_WAITQUEUE(wait, current);
1101
1102 set_current_state(TASK_UNINTERRUPTIBLE);
1103 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001104 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 schedule();
1106 remove_wait_queue(&chip->wq, &wait);
1107 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001108 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 continue;
1110 }
1111
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001112 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001113 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001114 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001115 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001116 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001119 if (chip_ready(map, adr))
1120 break;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001123 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001125 /* Did we succeed? */
1126 if (!chip_good(map, adr, datum)) {
1127 /* reset on all failures. */
1128 map_write( map, CMD(0xF0), chip->start );
1129 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001131 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001132 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001134 ret = -EIO;
1135 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001136 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 op_done:
1138 chip->state = FL_READY;
1139 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001140 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 return ret;
1143}
1144
1145
1146static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1147 size_t *retlen, const u_char *buf)
1148{
1149 struct map_info *map = mtd->priv;
1150 struct cfi_private *cfi = map->fldrv_priv;
1151 int ret = 0;
1152 int chipnum;
1153 unsigned long ofs, chipstart;
1154 DECLARE_WAITQUEUE(wait, current);
1155
1156 *retlen = 0;
1157 if (!len)
1158 return 0;
1159
1160 chipnum = to >> cfi->chipshift;
1161 ofs = to - (chipnum << cfi->chipshift);
1162 chipstart = cfi->chips[chipnum].start;
1163
1164 /* If it's not bus-aligned, do the first byte write */
1165 if (ofs & (map_bankwidth(map)-1)) {
1166 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1167 int i = ofs - bus_ofs;
1168 int n = 0;
1169 map_word tmp_buf;
1170
1171 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001172 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (cfi->chips[chipnum].state != FL_READY) {
1175#if 0
1176 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1177#endif
1178 set_current_state(TASK_UNINTERRUPTIBLE);
1179 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1180
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001181 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 schedule();
1184 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1185#if 0
1186 if(signal_pending(current))
1187 return -EINTR;
1188#endif
1189 goto retry;
1190 }
1191
1192 /* Load 'tmp_buf' with old contents of flash */
1193 tmp_buf = map_read(map, bus_ofs+chipstart);
1194
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001195 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /* Number of bytes to copy from buffer */
1198 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1201
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001202 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001204 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ofs += n;
1208 buf += n;
1209 (*retlen) += n;
1210 len -= n;
1211
1212 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001213 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ofs = 0;
1215 if (chipnum == cfi->numchips)
1216 return 0;
1217 }
1218 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* We are now aligned, write as much as possible */
1221 while(len >= map_bankwidth(map)) {
1222 map_word datum;
1223
1224 datum = map_word_load(map, buf);
1225
1226 ret = do_write_oneword(map, &cfi->chips[chipnum],
1227 ofs, datum);
1228 if (ret)
1229 return ret;
1230
1231 ofs += map_bankwidth(map);
1232 buf += map_bankwidth(map);
1233 (*retlen) += map_bankwidth(map);
1234 len -= map_bankwidth(map);
1235
1236 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001237 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ofs = 0;
1239 if (chipnum == cfi->numchips)
1240 return 0;
1241 chipstart = cfi->chips[chipnum].start;
1242 }
1243 }
1244
1245 /* Write the trailing bytes if any */
1246 if (len & (map_bankwidth(map)-1)) {
1247 map_word tmp_buf;
1248
1249 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001250 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (cfi->chips[chipnum].state != FL_READY) {
1253#if 0
1254 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1255#endif
1256 set_current_state(TASK_UNINTERRUPTIBLE);
1257 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1258
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001259 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 schedule();
1262 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1263#if 0
1264 if(signal_pending(current))
1265 return -EINTR;
1266#endif
1267 goto retry1;
1268 }
1269
1270 tmp_buf = map_read(map, ofs + chipstart);
1271
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001272 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001275
1276 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001278 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 (*retlen) += len;
1282 }
1283
1284 return 0;
1285}
1286
1287
1288/*
1289 * FIXME: interleaved mode not tested, and probably not supported!
1290 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001291static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001292 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001293 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct cfi_private *cfi = map->fldrv_priv;
1296 unsigned long timeo = jiffies + HZ;
1297 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1298 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1299 int ret = -EIO;
1300 unsigned long cmd_adr;
1301 int z, words;
1302 map_word datum;
1303
1304 adr += chip->start;
1305 cmd_adr = adr;
1306
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001307 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ret = get_chip(map, chip, adr, FL_WRITING);
1309 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001310 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return ret;
1312 }
1313
1314 datum = map_word_load(map, buf);
1315
1316 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1317 __func__, adr, datum.x[0] );
1318
Todd Poynor02b15e32005-06-07 00:04:39 +01001319 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001321 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1324 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1325 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1326
1327 /* Write Buffer Load */
1328 map_write(map, CMD(0x25), cmd_adr);
1329
1330 chip->state = FL_WRITING_TO_BUFFER;
1331
1332 /* Write length of data to come */
1333 words = len / map_bankwidth(map);
1334 map_write(map, CMD(words - 1), cmd_adr);
1335 /* Write data */
1336 z = 0;
1337 while(z < words * map_bankwidth(map)) {
1338 datum = map_word_load(map, buf);
1339 map_write(map, datum, adr + z);
1340
1341 z += map_bankwidth(map);
1342 buf += map_bankwidth(map);
1343 }
1344 z -= map_bankwidth(map);
1345
1346 adr += z;
1347
1348 /* Write Buffer Program Confirm: GO GO GO */
1349 map_write(map, CMD(0x29), cmd_adr);
1350 chip->state = FL_WRITING;
1351
Todd Poynor02b15e32005-06-07 00:04:39 +01001352 INVALIDATE_CACHE_UDELAY(map, chip,
1353 adr, map_bankwidth(map),
1354 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001356 timeo = jiffies + uWriteTimeout;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 for (;;) {
1359 if (chip->state != FL_WRITING) {
1360 /* Someone's suspended the write. Sleep */
1361 DECLARE_WAITQUEUE(wait, current);
1362
1363 set_current_state(TASK_UNINTERRUPTIBLE);
1364 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001365 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 schedule();
1367 remove_wait_queue(&chip->wq, &wait);
1368 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001369 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 continue;
1371 }
1372
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001373 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1374 break;
1375
Todd Poynor02b15e32005-06-07 00:04:39 +01001376 if (chip_ready(map, adr)) {
1377 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001382 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* reset on all failures. */
1386 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001387 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* FIXME - should have reset delay before continuing */
1389
Todd Poynor02b15e32005-06-07 00:04:39 +01001390 printk(KERN_WARNING "MTD %s(): software timeout\n",
1391 __func__ );
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ret = -EIO;
1394 op_done:
1395 chip->state = FL_READY;
1396 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001397 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return ret;
1400}
1401
1402
1403static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1404 size_t *retlen, const u_char *buf)
1405{
1406 struct map_info *map = mtd->priv;
1407 struct cfi_private *cfi = map->fldrv_priv;
1408 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1409 int ret = 0;
1410 int chipnum;
1411 unsigned long ofs;
1412
1413 *retlen = 0;
1414 if (!len)
1415 return 0;
1416
1417 chipnum = to >> cfi->chipshift;
1418 ofs = to - (chipnum << cfi->chipshift);
1419
1420 /* If it's not bus-aligned, do the first word write */
1421 if (ofs & (map_bankwidth(map)-1)) {
1422 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1423 if (local_len > len)
1424 local_len = len;
1425 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1426 local_len, retlen, buf);
1427 if (ret)
1428 return ret;
1429 ofs += local_len;
1430 buf += local_len;
1431 len -= local_len;
1432
1433 if (ofs >> cfi->chipshift) {
1434 chipnum ++;
1435 ofs = 0;
1436 if (chipnum == cfi->numchips)
1437 return 0;
1438 }
1439 }
1440
1441 /* Write buffer is worth it only if more than one word to write... */
1442 while (len >= map_bankwidth(map) * 2) {
1443 /* We must not cross write block boundaries */
1444 int size = wbufsize - (ofs & (wbufsize-1));
1445
1446 if (size > len)
1447 size = len;
1448 if (size % map_bankwidth(map))
1449 size -= size % map_bankwidth(map);
1450
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001451 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 ofs, buf, size);
1453 if (ret)
1454 return ret;
1455
1456 ofs += size;
1457 buf += size;
1458 (*retlen) += size;
1459 len -= size;
1460
1461 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001462 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ofs = 0;
1464 if (chipnum == cfi->numchips)
1465 return 0;
1466 }
1467 }
1468
1469 if (len) {
1470 size_t retlen_dregs = 0;
1471
1472 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1473 len, &retlen_dregs, buf);
1474
1475 *retlen += retlen_dregs;
1476 return ret;
1477 }
1478
1479 return 0;
1480}
1481
1482
1483/*
1484 * Handle devices with one erase region, that only implement
1485 * the chip erase command.
1486 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001487static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct cfi_private *cfi = map->fldrv_priv;
1490 unsigned long timeo = jiffies + HZ;
1491 unsigned long int adr;
1492 DECLARE_WAITQUEUE(wait, current);
1493 int ret = 0;
1494
1495 adr = cfi->addr_unlock1;
1496
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001497 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ret = get_chip(map, chip, adr, FL_WRITING);
1499 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001500 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return ret;
1502 }
1503
1504 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1505 __func__, chip->start );
1506
Todd Poynor02b15e32005-06-07 00:04:39 +01001507 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001509 xip_disable(map, chip, adr);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1512 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1513 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1514 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1515 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1516 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1517
1518 chip->state = FL_ERASING;
1519 chip->erase_suspended = 0;
1520 chip->in_progress_block_addr = adr;
1521
Todd Poynor02b15e32005-06-07 00:04:39 +01001522 INVALIDATE_CACHE_UDELAY(map, chip,
1523 adr, map->size,
1524 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 timeo = jiffies + (HZ*20);
1527
1528 for (;;) {
1529 if (chip->state != FL_ERASING) {
1530 /* Someone's suspended the erase. Sleep */
1531 set_current_state(TASK_UNINTERRUPTIBLE);
1532 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001533 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 schedule();
1535 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001536 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 continue;
1538 }
1539 if (chip->erase_suspended) {
1540 /* This erase was suspended and resumed.
1541 Adjust the timeout */
1542 timeo = jiffies + (HZ*20); /* FIXME */
1543 chip->erase_suspended = 0;
1544 }
1545
1546 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 break;
1548
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001549 if (time_after(jiffies, timeo)) {
1550 printk(KERN_WARNING "MTD %s(): software timeout\n",
1551 __func__ );
1552 break;
1553 }
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001556 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001558 /* Did we succeed? */
1559 if (!chip_good(map, adr, map_word_ff(map))) {
1560 /* reset on all failures. */
1561 map_write( map, CMD(0xF0), chip->start );
1562 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001564 ret = -EIO;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001568 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001570 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 return ret;
1573}
1574
1575
Todd Poynor02b15e32005-06-07 00:04:39 +01001576static 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 -07001577{
1578 struct cfi_private *cfi = map->fldrv_priv;
1579 unsigned long timeo = jiffies + HZ;
1580 DECLARE_WAITQUEUE(wait, current);
1581 int ret = 0;
1582
1583 adr += chip->start;
1584
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001585 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 ret = get_chip(map, chip, adr, FL_ERASING);
1587 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001588 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return ret;
1590 }
1591
1592 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1593 __func__, adr );
1594
Todd Poynor02b15e32005-06-07 00:04:39 +01001595 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001597 xip_disable(map, chip, adr);
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1600 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1601 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1602 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1603 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1604 map_write(map, CMD(0x30), adr);
1605
1606 chip->state = FL_ERASING;
1607 chip->erase_suspended = 0;
1608 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001609
1610 INVALIDATE_CACHE_UDELAY(map, chip,
1611 adr, len,
1612 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 timeo = jiffies + (HZ*20);
1615
1616 for (;;) {
1617 if (chip->state != FL_ERASING) {
1618 /* Someone's suspended the erase. Sleep */
1619 set_current_state(TASK_UNINTERRUPTIBLE);
1620 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001621 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 schedule();
1623 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001624 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 continue;
1626 }
1627 if (chip->erase_suspended) {
1628 /* This erase was suspended and resumed.
1629 Adjust the timeout */
1630 timeo = jiffies + (HZ*20); /* FIXME */
1631 chip->erase_suspended = 0;
1632 }
1633
Todd Poynor02b15e32005-06-07 00:04:39 +01001634 if (chip_ready(map, adr)) {
1635 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001639 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001640 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001641 printk(KERN_WARNING "MTD %s(): software timeout\n",
1642 __func__ );
1643 break;
1644 }
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001647 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001649 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001650 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001651 /* reset on all failures. */
1652 map_write( map, CMD(0xF0), chip->start );
1653 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001655 ret = -EIO;
1656 }
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 chip->state = FL_READY;
1659 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001660 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return ret;
1662}
1663
1664
Ben Dooksce0f33a2007-05-28 19:59:00 +01001665static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 unsigned long ofs, len;
1668 int ret;
1669
1670 ofs = instr->addr;
1671 len = instr->len;
1672
1673 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1674 if (ret)
1675 return ret;
1676
1677 instr->state = MTD_ERASE_DONE;
1678 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return 0;
1681}
1682
1683
1684static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1685{
1686 struct map_info *map = mtd->priv;
1687 struct cfi_private *cfi = map->fldrv_priv;
1688 int ret = 0;
1689
1690 if (instr->addr != 0)
1691 return -EINVAL;
1692
1693 if (instr->len != mtd->size)
1694 return -EINVAL;
1695
1696 ret = do_erase_chip(map, &cfi->chips[0]);
1697 if (ret)
1698 return ret;
1699
1700 instr->state = MTD_ERASE_DONE;
1701 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704}
1705
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001706static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1707 unsigned long adr, int len, void *thunk)
1708{
1709 struct cfi_private *cfi = map->fldrv_priv;
1710 int ret;
1711
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001712 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001713 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1714 if (ret)
1715 goto out_unlock;
1716 chip->state = FL_LOCKING;
1717
1718 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1719 __func__, adr, len);
1720
1721 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1722 cfi->device_type, NULL);
1723 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1724 cfi->device_type, NULL);
1725 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1726 cfi->device_type, NULL);
1727 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1728 cfi->device_type, NULL);
1729 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1730 cfi->device_type, NULL);
1731 map_write(map, CMD(0x40), chip->start + adr);
1732
1733 chip->state = FL_READY;
1734 put_chip(map, chip, adr + chip->start);
1735 ret = 0;
1736
1737out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001738 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001739 return ret;
1740}
1741
1742static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1743 unsigned long adr, int len, void *thunk)
1744{
1745 struct cfi_private *cfi = map->fldrv_priv;
1746 int ret;
1747
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001748 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001749 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1750 if (ret)
1751 goto out_unlock;
1752 chip->state = FL_UNLOCKING;
1753
1754 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1755 __func__, adr, len);
1756
1757 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1758 cfi->device_type, NULL);
1759 map_write(map, CMD(0x70), adr);
1760
1761 chip->state = FL_READY;
1762 put_chip(map, chip, adr + chip->start);
1763 ret = 0;
1764
1765out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001766 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001767 return ret;
1768}
1769
Adrian Hunter69423d92008-12-10 13:37:21 +00001770static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001771{
1772 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1773}
1774
Adrian Hunter69423d92008-12-10 13:37:21 +00001775static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001776{
1777 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1778}
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781static void cfi_amdstd_sync (struct mtd_info *mtd)
1782{
1783 struct map_info *map = mtd->priv;
1784 struct cfi_private *cfi = map->fldrv_priv;
1785 int i;
1786 struct flchip *chip;
1787 int ret = 0;
1788 DECLARE_WAITQUEUE(wait, current);
1789
1790 for (i=0; !ret && i<cfi->numchips; i++) {
1791 chip = &cfi->chips[i];
1792
1793 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001794 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 switch(chip->state) {
1797 case FL_READY:
1798 case FL_STATUS:
1799 case FL_CFI_QUERY:
1800 case FL_JEDEC_QUERY:
1801 chip->oldstate = chip->state;
1802 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001803 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * as the whole point is that nobody can do anything
1805 * with the chip now anyway.
1806 */
1807 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001808 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 break;
1810
1811 default:
1812 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07001813 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001815
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001816 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 schedule();
1819
1820 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 goto retry;
1823 }
1824 }
1825
1826 /* Unlock the chips again */
1827
1828 for (i--; i >=0; i--) {
1829 chip = &cfi->chips[i];
1830
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001831 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (chip->state == FL_SYNCING) {
1834 chip->state = chip->oldstate;
1835 wake_up(&chip->wq);
1836 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001837 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839}
1840
1841
1842static int cfi_amdstd_suspend(struct mtd_info *mtd)
1843{
1844 struct map_info *map = mtd->priv;
1845 struct cfi_private *cfi = map->fldrv_priv;
1846 int i;
1847 struct flchip *chip;
1848 int ret = 0;
1849
1850 for (i=0; !ret && i<cfi->numchips; i++) {
1851 chip = &cfi->chips[i];
1852
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001853 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 switch(chip->state) {
1856 case FL_READY:
1857 case FL_STATUS:
1858 case FL_CFI_QUERY:
1859 case FL_JEDEC_QUERY:
1860 chip->oldstate = chip->state;
1861 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001862 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * as the whole point is that nobody can do anything
1864 * with the chip now anyway.
1865 */
1866 case FL_PM_SUSPENDED:
1867 break;
1868
1869 default:
1870 ret = -EAGAIN;
1871 break;
1872 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001873 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 /* Unlock the chips again */
1877
1878 if (ret) {
1879 for (i--; i >=0; i--) {
1880 chip = &cfi->chips[i];
1881
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001882 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (chip->state == FL_PM_SUSPENDED) {
1885 chip->state = chip->oldstate;
1886 wake_up(&chip->wq);
1887 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001888 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return ret;
1893}
1894
1895
1896static void cfi_amdstd_resume(struct mtd_info *mtd)
1897{
1898 struct map_info *map = mtd->priv;
1899 struct cfi_private *cfi = map->fldrv_priv;
1900 int i;
1901 struct flchip *chip;
1902
1903 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 chip = &cfi->chips[i];
1906
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001907 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (chip->state == FL_PM_SUSPENDED) {
1910 chip->state = FL_READY;
1911 map_write(map, CMD(0xF0), chip->start);
1912 wake_up(&chip->wq);
1913 }
1914 else
1915 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1916
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001917 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919}
1920
1921static void cfi_amdstd_destroy(struct mtd_info *mtd)
1922{
1923 struct map_info *map = mtd->priv;
1924 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 kfree(cfi->cmdset_priv);
1927 kfree(cfi->cfiq);
1928 kfree(cfi);
1929 kfree(mtd->eraseregions);
1930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932MODULE_LICENSE("GPL");
1933MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1934MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");