blob: c27dd1c936cd445b9bc88818fc931685ea50af2c [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>
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070035#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define SST49LF004B 0x0060
Ryan Jackson89072ef2006-10-20 14:41:03 -070048#define SST49LF040B 0x0050
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +010049#define SST49LF008A 0x005a
Haavard Skinnemoen01655082006-08-09 11:06:07 +020050#define AT49BV6416 0x00d6
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
53static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
54static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
55static int cfi_amdstd_erase_chip(struct mtd_info *, struct erase_info *);
56static int cfi_amdstd_erase_varsize(struct mtd_info *, struct erase_info *);
57static void cfi_amdstd_sync (struct mtd_info *);
58static int cfi_amdstd_suspend (struct mtd_info *);
59static void cfi_amdstd_resume (struct mtd_info *);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -070060static int cfi_amdstd_reboot(struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int cfi_amdstd_secsi_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
62
63static void cfi_amdstd_destroy(struct mtd_info *);
64
65struct mtd_info *cfi_cmdset_0002(struct map_info *, int);
66static struct mtd_info *cfi_amdstd_setup (struct mtd_info *);
67
68static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
69static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
70#include "fwh_lock.h"
71
Adrian Hunter69423d92008-12-10 13:37:21 +000072static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
73static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Haavard Skinnemoen01655082006-08-09 11:06:07 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct mtd_chip_driver cfi_amdstd_chipdrv = {
76 .probe = NULL, /* Not usable directly */
77 .destroy = cfi_amdstd_destroy,
78 .name = "cfi_cmdset_0002",
79 .module = THIS_MODULE
80};
81
82
83/* #define DEBUG_CFI_FEATURES */
84
85
86#ifdef DEBUG_CFI_FEATURES
87static void cfi_tell_features(struct cfi_pri_amdstd *extp)
88{
89 const char* erase_suspend[3] = {
90 "Not supported", "Read only", "Read/write"
91 };
92 const char* top_bottom[6] = {
93 "No WP", "8x8KiB sectors at top & bottom, no WP",
94 "Bottom boot", "Top boot",
95 "Uniform, Bottom WP", "Uniform, Top WP"
96 };
97
98 printk(" Silicon revision: %d\n", extp->SiliconRevision >> 1);
Thomas Gleixner1f948b42005-11-07 11:15:37 +000099 printk(" Address sensitive unlock: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 (extp->SiliconRevision & 1) ? "Not required" : "Required");
101
102 if (extp->EraseSuspend < ARRAY_SIZE(erase_suspend))
103 printk(" Erase Suspend: %s\n", erase_suspend[extp->EraseSuspend]);
104 else
105 printk(" Erase Suspend: Unknown value %d\n", extp->EraseSuspend);
106
107 if (extp->BlkProt == 0)
108 printk(" Block protection: Not supported\n");
109 else
110 printk(" Block protection: %d sectors per group\n", extp->BlkProt);
111
112
113 printk(" Temporary block unprotect: %s\n",
114 extp->TmpBlkUnprotect ? "Supported" : "Not supported");
115 printk(" Block protect/unprotect scheme: %d\n", extp->BlkProtUnprot);
116 printk(" Number of simultaneous operations: %d\n", extp->SimultaneousOps);
117 printk(" Burst mode: %s\n",
118 extp->BurstMode ? "Supported" : "Not supported");
119 if (extp->PageMode == 0)
120 printk(" Page mode: Not supported\n");
121 else
122 printk(" Page mode: %d word page\n", extp->PageMode << 2);
123
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000124 printk(" Vpp Supply Minimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 extp->VppMin >> 4, extp->VppMin & 0xf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000126 printk(" Vpp Supply Maximum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 extp->VppMax >> 4, extp->VppMax & 0xf);
128
129 if (extp->TopBottom < ARRAY_SIZE(top_bottom))
130 printk(" Top/Bottom Boot Block: %s\n", top_bottom[extp->TopBottom]);
131 else
132 printk(" Top/Bottom Boot Block: Unknown value %d\n", extp->TopBottom);
133}
134#endif
135
136#ifdef AMD_BOOTLOC_BUG
137/* Wheee. Bring me the head of someone at AMD. */
138static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
139{
140 struct map_info *map = mtd->priv;
141 struct cfi_private *cfi = map->fldrv_priv;
142 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
143 __u8 major = extp->MajorVersion;
144 __u8 minor = extp->MinorVersion;
145
146 if (((major << 8) | minor) < 0x3131) {
147 /* CFI version 1.0 => don't trust bootloc */
Christopher Moore87e92c02008-10-17 05:32:22 +0200148
149 DEBUG(MTD_DEBUG_LEVEL1,
150 "%s: JEDEC Vendor ID is 0x%02X Device ID is 0x%02X\n",
151 map->name, cfi->mfr, cfi->id);
152
153 /* AFAICS all 29LV400 with a bottom boot block have a device ID
154 * of 0x22BA in 16-bit mode and 0xBA in 8-bit mode.
155 * These were badly detected as they have the 0x80 bit set
156 * so treat them as a special case.
157 */
158 if (((cfi->id == 0xBA) || (cfi->id == 0x22BA)) &&
159
160 /* Macronix added CFI to their 2nd generation
161 * MX29LV400C B/T but AFAICS no other 29LV400 (AMD,
162 * Fujitsu, Spansion, EON, ESI and older Macronix)
163 * has CFI.
164 *
165 * Therefore also check the manufacturer.
166 * This reduces the risk of false detection due to
167 * the 8-bit device ID.
168 */
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100169 (cfi->mfr == CFI_MFR_MACRONIX)) {
Christopher Moore87e92c02008-10-17 05:32:22 +0200170 DEBUG(MTD_DEBUG_LEVEL1,
171 "%s: Macronix MX29LV400C with bottom boot block"
172 " detected\n", map->name);
173 extp->TopBottom = 2; /* bottom boot */
174 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (cfi->id & 0x80) {
176 printk(KERN_WARNING "%s: JEDEC Device ID is 0x%02X. Assuming broken CFI table.\n", map->name, cfi->id);
177 extp->TopBottom = 3; /* top boot */
178 } else {
179 extp->TopBottom = 2; /* bottom boot */
180 }
Christopher Moore87e92c02008-10-17 05:32:22 +0200181
182 DEBUG(MTD_DEBUG_LEVEL1,
183 "%s: AMD CFI PRI V%c.%c has no boot block field;"
184 " deduced %s from Device ID\n", map->name, major, minor,
185 extp->TopBottom == 2 ? "bottom" : "top");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187}
188#endif
189
190static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
191{
192 struct map_info *map = mtd->priv;
193 struct cfi_private *cfi = map->fldrv_priv;
194 if (cfi->cfiq->BufWriteTimeoutTyp) {
195 DEBUG(MTD_DEBUG_LEVEL1, "Using buffer write method\n" );
196 mtd->write = cfi_amdstd_write_buffers;
197 }
198}
199
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200200/* Atmel chips don't use the same PRI format as AMD chips */
201static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
202{
203 struct map_info *map = mtd->priv;
204 struct cfi_private *cfi = map->fldrv_priv;
205 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
206 struct cfi_pri_atmel atmel_pri;
207
208 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
HÃ¥vard Skinnemoende591da2006-09-15 17:19:31 +0200209 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200210
211 if (atmel_pri.Features & 0x02)
212 extp->EraseSuspend = 2;
213
Haavard Skinnemoenbe8f78b2008-09-30 13:55:33 +0200214 /* Some chips got it backwards... */
215 if (cfi->id == AT49BV6416) {
216 if (atmel_pri.BottomBoot)
217 extp->TopBottom = 3;
218 else
219 extp->TopBottom = 2;
220 } else {
221 if (atmel_pri.BottomBoot)
222 extp->TopBottom = 2;
223 else
224 extp->TopBottom = 3;
225 }
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100226
227 /* burst write mode not supported */
228 cfi->cfiq->BufWriteTimeoutTyp = 0;
229 cfi->cfiq->BufWriteTimeoutMax = 0;
Haavard Skinnemoen5b0c5c22006-08-09 10:54:44 +0200230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static void fixup_use_secsi(struct mtd_info *mtd, void *param)
233{
234 /* Setup for chips with a secsi area */
235 mtd->read_user_prot_reg = cfi_amdstd_secsi_read;
236 mtd->read_fact_prot_reg = cfi_amdstd_secsi_read;
237}
238
239static void fixup_use_erase_chip(struct mtd_info *mtd, void *param)
240{
241 struct map_info *map = mtd->priv;
242 struct cfi_private *cfi = map->fldrv_priv;
243 if ((cfi->cfiq->NumEraseRegions == 1) &&
244 ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0)) {
245 mtd->erase = cfi_amdstd_erase_chip;
246 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200250/*
251 * Some Atmel chips (e.g. the AT49BV6416) power-up with all sectors
252 * locked by default.
253 */
254static void fixup_use_atmel_lock(struct mtd_info *mtd, void *param)
255{
256 mtd->lock = cfi_atmel_lock;
257 mtd->unlock = cfi_atmel_unlock;
Justin Treone619a752008-01-30 10:25:49 -0800258 mtd->flags |= MTD_POWERUP_LOCK;
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200259}
260
Trent Piepho70b07252008-03-30 21:19:30 -0700261static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
262{
263 struct map_info *map = mtd->priv;
264 struct cfi_private *cfi = map->fldrv_priv;
265
266 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
267 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
268 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
269 }
270}
271
272static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
273{
274 struct map_info *map = mtd->priv;
275 struct cfi_private *cfi = map->fldrv_priv;
276
277 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
278 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
279 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
280 }
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100284 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#ifdef AMD_BOOTLOC_BUG
286 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100287 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#endif
289 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
290 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
291 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
292 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
293 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
294 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
Trent Piepho70b07252008-03-30 21:19:30 -0700295 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, },
296 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, },
297 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, },
298 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#if !FORCE_WORD_WRITE
300 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
301#endif
302 { 0, 0, NULL, NULL }
303};
304static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100305 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
306 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
307 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 { 0, 0, NULL, NULL }
309};
310
311static struct cfi_fixup fixup_table[] = {
312 /* The CFI vendor ids and the JEDEC vendor IDs appear
313 * to be common. It is like the devices id's are as
314 * well. This table is to pick all cases where
315 * we know that is the case.
316 */
317 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200318 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 { 0, 0, NULL, NULL }
320};
321
322
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100323static void cfi_fixup_major_minor(struct cfi_private *cfi,
324 struct cfi_pri_amdstd *extp)
325{
326 if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
327 extp->MajorVersion == '0')
328 extp->MajorVersion = '1';
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
332{
333 struct cfi_private *cfi = map->fldrv_priv;
334 struct mtd_info *mtd;
335 int i;
336
Burman Yan95b93a02006-11-15 21:10:29 +0200337 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!mtd) {
339 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
340 return NULL;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 mtd->priv = map;
343 mtd->type = MTD_NORFLASH;
344
345 /* Fill in the default mtd operations */
346 mtd->erase = cfi_amdstd_erase_varsize;
347 mtd->write = cfi_amdstd_write_words;
348 mtd->read = cfi_amdstd_read;
349 mtd->sync = cfi_amdstd_sync;
350 mtd->suspend = cfi_amdstd_suspend;
351 mtd->resume = cfi_amdstd_resume;
352 mtd->flags = MTD_CAP_NORFLASH;
353 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400354 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700356 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (cfi->cfi_mode==CFI_MODE_CFI){
359 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
361 struct cfi_pri_amdstd *extp;
362
363 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200364 if (extp) {
365 /*
366 * It's a real CFI chip, not one for which the probe
367 * routine faked a CFI structure.
368 */
369 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Guillaume LECERF564b8492010-04-24 17:58:17 +0200371 if (extp->MajorVersion != '1' ||
372 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
373 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
374 "version %c.%c.\n", extp->MajorVersion,
375 extp->MinorVersion);
376 kfree(extp);
377 kfree(mtd);
378 return NULL;
379 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100380
Guillaume LECERF564b8492010-04-24 17:58:17 +0200381 /* Install our own private info structure */
382 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100383
Guillaume LECERF564b8492010-04-24 17:58:17 +0200384 /* Apply cfi device specific fixups */
385 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200388 /* Tell the user about it in lots of lovely detail */
389 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Guillaume LECERF564b8492010-04-24 17:58:17 +0200392 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100393 if ((bootloc < 2) || (bootloc > 5)) {
394 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
395 "bank location (%d). Assuming bottom.\n",
396 bootloc, map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200397 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200399
400 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100401 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200402
403 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
404 int j = (cfi->cfiq->NumEraseRegions-1)-i;
405 __u32 swap;
406
407 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 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200416
417 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
418 kfree(mtd);
419 return NULL;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 } /* CFI mode */
423 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
424 /* Apply jedec specific fixups */
425 cfi_fixup(mtd, jedec_fixup_table);
426 }
427 /* Apply generic fixups */
428 cfi_fixup(mtd, fixup_table);
429
430 for (i=0; i< cfi->numchips; i++) {
431 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
432 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
433 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800434 cfi->chips[i].ref_point_counter = 0;
435 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return cfi_amdstd_setup(mtd);
441}
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100442EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
445{
446 struct map_info *map = mtd->priv;
447 struct cfi_private *cfi = map->fldrv_priv;
448 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
449 unsigned long offset = 0;
450 int i,j;
451
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000452 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000454 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 mtd->size = devsize * cfi->numchips;
456
457 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
458 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
459 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000460 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
462 goto setup_err;
463 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
466 unsigned long ernum, ersize;
467 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
468 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (mtd->erasesize < ersize) {
471 mtd->erasesize = ersize;
472 }
473 for (j=0; j<cfi->numchips; j++) {
474 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
475 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
476 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
477 }
478 offset += (ersize * ernum);
479 }
480 if (offset != devsize) {
481 /* Argh */
482 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
483 goto setup_err;
484 }
485#if 0
486 // debug
487 for (i=0; i<mtd->numeraseregions;i++){
488 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
489 i,mtd->eraseregions[i].offset,
490 mtd->eraseregions[i].erasesize,
491 mtd->eraseregions[i].numblocks);
492 }
493#endif
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700496 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return mtd;
498
499 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100500 kfree(mtd->eraseregions);
501 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kfree(cfi->cmdset_priv);
503 kfree(cfi->cfiq);
504 return NULL;
505}
506
507/*
508 * Return true if the chip is ready.
509 *
510 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
511 * non-suspended sector) and is indicated by no toggle bits toggling.
512 *
513 * Note that anything more complicated than checking if no bits are toggling
514 * (including checking DQ5 for an error status) is tricky to get working
515 * correctly and is therefore not done (particulary with interleaved chips
516 * as each chip must be checked independantly of the others).
517 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100518static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 map_word d, t;
521
522 d = map_read(map, addr);
523 t = map_read(map, addr);
524
525 return map_word_equal(map, d, t);
526}
527
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100528/*
529 * Return true if the chip is ready and has the correct value.
530 *
531 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
532 * non-suspended sector) and it is indicated by no bits toggling.
533 *
534 * Error are indicated by toggling bits or bits held with the wrong value,
535 * or with bits toggling.
536 *
537 * Note that anything more complicated than checking if no bits are toggling
538 * (including checking DQ5 for an error status) is tricky to get working
539 * correctly and is therefore not done (particulary with interleaved chips
540 * as each chip must be checked independantly of the others).
541 *
542 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100543static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100544{
545 map_word oldd, curd;
546
547 oldd = map_read(map, addr);
548 curd = map_read(map, addr);
549
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000550 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100551 map_word_equal(map, curd, expected);
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
555{
556 DECLARE_WAITQUEUE(wait, current);
557 struct cfi_private *cfi = map->fldrv_priv;
558 unsigned long timeo;
559 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
560
561 resettime:
562 timeo = jiffies + HZ;
563 retry:
564 switch (chip->state) {
565
566 case FL_STATUS:
567 for (;;) {
568 if (chip_ready(map, adr))
569 break;
570
571 if (time_after(jiffies, timeo)) {
572 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -EIO;
574 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200575 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200577 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Someone else might have been playing with it. */
579 goto retry;
580 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 case FL_READY:
583 case FL_CFI_QUERY:
584 case FL_JEDEC_QUERY:
585 return 0;
586
587 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100588 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
589 !(mode == FL_READY || mode == FL_POINT ||
590 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto sleep;
592
593 /* We could check to see if we're trying to access the sector
594 * that is currently being erased. However, no user will try
595 * anything like that so we just wait for the timeout. */
596
597 /* Erase suspend */
598 /* It's harmless to issue the Erase-Suspend and Erase-Resume
599 * commands when the erase algorithm isn't in progress. */
600 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
601 chip->oldstate = FL_ERASING;
602 chip->state = FL_ERASE_SUSPENDING;
603 chip->erase_suspended = 1;
604 for (;;) {
605 if (chip_ready(map, adr))
606 break;
607
608 if (time_after(jiffies, timeo)) {
609 /* Should have suspended the erase by now.
610 * Send an Erase-Resume command as either
611 * there was an error (so leave the erase
612 * routine to recover from it) or we trying to
613 * use the erase-in-progress sector. */
614 map_write(map, CMD(0x30), chip->in_progress_block_addr);
615 chip->state = FL_ERASING;
616 chip->oldstate = FL_READY;
617 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
618 return -EIO;
619 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000620
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200621 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200623 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
625 So we can just loop here. */
626 }
627 chip->state = FL_READY;
628 return 0;
629
Todd Poynor02b15e32005-06-07 00:04:39 +0100630 case FL_XIP_WHILE_ERASING:
631 if (mode != FL_READY && mode != FL_POINT &&
632 (!cfip || !(cfip->EraseSuspend&2)))
633 goto sleep;
634 chip->oldstate = chip->state;
635 chip->state = FL_READY;
636 return 0;
637
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700638 case FL_SHUTDOWN:
639 /* The machine is rebooting */
640 return -EIO;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 case FL_POINT:
643 /* Only if there's no operation suspended... */
644 if (mode == FL_READY && chip->oldstate == FL_READY)
645 return 0;
646
647 default:
648 sleep:
649 set_current_state(TASK_UNINTERRUPTIBLE);
650 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200651 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 schedule();
653 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200654 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto resettime;
656 }
657}
658
659
660static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
661{
662 struct cfi_private *cfi = map->fldrv_priv;
663
664 switch(chip->oldstate) {
665 case FL_ERASING:
666 chip->state = chip->oldstate;
667 map_write(map, CMD(0x30), chip->in_progress_block_addr);
668 chip->oldstate = FL_READY;
669 chip->state = FL_ERASING;
670 break;
671
Todd Poynor02b15e32005-06-07 00:04:39 +0100672 case FL_XIP_WHILE_ERASING:
673 chip->state = chip->oldstate;
674 chip->oldstate = FL_READY;
675 break;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 case FL_READY:
678 case FL_STATUS:
679 /* We should really make set_vpp() count, rather than doing this */
680 DISABLE_VPP(map);
681 break;
682 default:
683 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
684 }
685 wake_up(&chip->wq);
686}
687
Todd Poynor02b15e32005-06-07 00:04:39 +0100688#ifdef CONFIG_MTD_XIP
689
690/*
691 * No interrupt what so ever can be serviced while the flash isn't in array
692 * mode. This is ensured by the xip_disable() and xip_enable() functions
693 * enclosing any code path where the flash is known not to be in array mode.
694 * And within a XIP disabled code path, only functions marked with __xipram
695 * may be called and nothing else (it's a good thing to inspect generated
696 * assembly to make sure inline functions were actually inlined and that gcc
697 * didn't emit calls to its own support functions). Also configuring MTD CFI
698 * support to a single buswidth and a single interleave is also recommended.
699 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200700
Todd Poynor02b15e32005-06-07 00:04:39 +0100701static void xip_disable(struct map_info *map, struct flchip *chip,
702 unsigned long adr)
703{
704 /* TODO: chips with no XIP use should ignore and return */
705 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
706 local_irq_disable();
707}
708
709static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
710 unsigned long adr)
711{
712 struct cfi_private *cfi = map->fldrv_priv;
713
714 if (chip->state != FL_POINT && chip->state != FL_READY) {
715 map_write(map, CMD(0xf0), adr);
716 chip->state = FL_READY;
717 }
718 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200719 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100720 local_irq_enable();
721}
722
723/*
724 * When a delay is required for the flash operation to complete, the
725 * xip_udelay() function is polling for both the given timeout and pending
726 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000727 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100728 * and interrupts unmasked. Task scheduling might also happen at that
729 * point. The CPU eventually returns from the interrupt or the call to
730 * schedule() and the suspended flash operation is resumed for the remaining
731 * of the delay period.
732 *
733 * Warning: this function _will_ fool interrupt latency tracing tools.
734 */
735
736static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
737 unsigned long adr, int usec)
738{
739 struct cfi_private *cfi = map->fldrv_priv;
740 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
741 map_word status, OK = CMD(0x80);
742 unsigned long suspended, start = xip_currtime();
743 flstate_t oldstate;
744
745 do {
746 cpu_relax();
747 if (xip_irqpending() && extp &&
748 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
749 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
750 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000751 * Let's suspend the erase operation when supported.
752 * Note that we currently don't try to suspend
753 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100754 * operation suspended (imagine what happens
755 * when one chip was already done with the current
756 * operation while another chip suspended it, then
757 * we resume the whole thing at once). Yes, it
758 * can happen!
759 */
760 map_write(map, CMD(0xb0), adr);
761 usec -= xip_elapsed_since(start);
762 suspended = xip_currtime();
763 do {
764 if (xip_elapsed_since(suspended) > 100000) {
765 /*
766 * The chip doesn't want to suspend
767 * after waiting for 100 msecs.
768 * This is a critical error but there
769 * is not much we can do here.
770 */
771 return;
772 }
773 status = map_read(map, adr);
774 } while (!map_word_andequal(map, status, OK, OK));
775
776 /* Suspend succeeded */
777 oldstate = chip->state;
778 if (!map_word_bitsset(map, status, CMD(0x40)))
779 break;
780 chip->state = FL_XIP_WHILE_ERASING;
781 chip->erase_suspended = 1;
782 map_write(map, CMD(0xf0), adr);
783 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200784 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100785 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200786 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200787 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100788 cond_resched();
789
790 /*
791 * We're back. However someone else might have
792 * decided to go write to the chip if we are in
793 * a suspended erase state. If so let's wait
794 * until it's done.
795 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200796 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100797 while (chip->state != FL_XIP_WHILE_ERASING) {
798 DECLARE_WAITQUEUE(wait, current);
799 set_current_state(TASK_UNINTERRUPTIBLE);
800 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200801 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100802 schedule();
803 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200804 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100805 }
806 /* Disallow XIP again */
807 local_irq_disable();
808
809 /* Resume the write or erase operation */
810 map_write(map, CMD(0x30), adr);
811 chip->state = oldstate;
812 start = xip_currtime();
813 } else if (usec >= 1000000/HZ) {
814 /*
815 * Try to save on CPU power when waiting delay
816 * is at least a system timer tick period.
817 * No need to be extremely accurate here.
818 */
819 xip_cpu_idle();
820 }
821 status = map_read(map, adr);
822 } while (!map_word_andequal(map, status, OK, OK)
823 && xip_elapsed_since(start) < usec);
824}
825
826#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
827
828/*
829 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
830 * the flash is actively programming or erasing since we have to poll for
831 * the operation to complete anyway. We can't do that in a generic way with
832 * a XIP setup so do it before the actual flash operation in this case
833 * and stub it out from INVALIDATE_CACHE_UDELAY.
834 */
835#define XIP_INVAL_CACHED_RANGE(map, from, size) \
836 INVALIDATE_CACHED_RANGE(map, from, size)
837
838#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
839 UDELAY(map, chip, adr, usec)
840
841/*
842 * Extra notes:
843 *
844 * Activating this XIP support changes the way the code works a bit. For
845 * example the code to suspend the current process when concurrent access
846 * happens is never executed because xip_udelay() will always return with the
847 * same chip state as it was entered with. This is why there is no care for
848 * the presence of add_wait_queue() or schedule() calls from within a couple
849 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
850 * The queueing and scheduling are always happening within xip_udelay().
851 *
852 * Similarly, get_chip() and put_chip() just happen to always be executed
853 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
854 * is in array mode, therefore never executing many cases therein and not
855 * causing any problem with XIP.
856 */
857
858#else
859
860#define xip_disable(map, chip, adr)
861#define xip_enable(map, chip, adr)
862#define XIP_INVAL_CACHED_RANGE(x...)
863
864#define UDELAY(map, chip, adr, usec) \
865do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200866 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100867 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200868 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100869} while (0)
870
871#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
872do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200873 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100874 INVALIDATE_CACHED_RANGE(map, adr, len); \
875 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200876 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100877} while (0)
878
879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
882{
883 unsigned long cmd_addr;
884 struct cfi_private *cfi = map->fldrv_priv;
885 int ret;
886
887 adr += chip->start;
888
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000889 /* Ensure cmd read/writes are aligned. */
890 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200892 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 ret = get_chip(map, chip, cmd_addr, FL_READY);
894 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200895 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
897 }
898
899 if (chip->state != FL_POINT && chip->state != FL_READY) {
900 map_write(map, CMD(0xf0), cmd_addr);
901 chip->state = FL_READY;
902 }
903
904 map_copy_from(map, buf, adr, len);
905
906 put_chip(map, chip, cmd_addr);
907
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200908 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
910}
911
912
913static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
914{
915 struct map_info *map = mtd->priv;
916 struct cfi_private *cfi = map->fldrv_priv;
917 unsigned long ofs;
918 int chipnum;
919 int ret = 0;
920
921 /* ofs: offset within the first chip that the first read should start */
922
923 chipnum = (from >> cfi->chipshift);
924 ofs = from - (chipnum << cfi->chipshift);
925
926
927 *retlen = 0;
928
929 while (len) {
930 unsigned long thislen;
931
932 if (chipnum >= cfi->numchips)
933 break;
934
935 if ((len + ofs -1) >> cfi->chipshift)
936 thislen = (1<<cfi->chipshift) - ofs;
937 else
938 thislen = len;
939
940 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
941 if (ret)
942 break;
943
944 *retlen += thislen;
945 len -= thislen;
946 buf += thislen;
947
948 ofs = 0;
949 chipnum++;
950 }
951 return ret;
952}
953
954
955static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
956{
957 DECLARE_WAITQUEUE(wait, current);
958 unsigned long timeo = jiffies + HZ;
959 struct cfi_private *cfi = map->fldrv_priv;
960
961 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200962 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (chip->state != FL_READY){
965#if 0
966 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
967#endif
968 set_current_state(TASK_UNINTERRUPTIBLE);
969 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000970
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200971 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 schedule();
974 remove_wait_queue(&chip->wq, &wait);
975#if 0
976 if(signal_pending(current))
977 return -EINTR;
978#endif
979 timeo = jiffies + HZ;
980
981 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 adr += chip->start;
985
986 chip->state = FL_READY;
987
988 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
989 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
990 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 map_copy_from(map, buf, adr, len);
993
994 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
995 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
996 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
997 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001000 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 return 0;
1003}
1004
1005static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1006{
1007 struct map_info *map = mtd->priv;
1008 struct cfi_private *cfi = map->fldrv_priv;
1009 unsigned long ofs;
1010 int chipnum;
1011 int ret = 0;
1012
1013
1014 /* ofs: offset within the first chip that the first read should start */
1015
1016 /* 8 secsi bytes per chip */
1017 chipnum=from>>3;
1018 ofs=from & 7;
1019
1020
1021 *retlen = 0;
1022
1023 while (len) {
1024 unsigned long thislen;
1025
1026 if (chipnum >= cfi->numchips)
1027 break;
1028
1029 if ((len + ofs -1) >> 3)
1030 thislen = (1<<3) - ofs;
1031 else
1032 thislen = len;
1033
1034 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1035 if (ret)
1036 break;
1037
1038 *retlen += thislen;
1039 len -= thislen;
1040 buf += thislen;
1041
1042 ofs = 0;
1043 chipnum++;
1044 }
1045 return ret;
1046}
1047
1048
Todd Poynor02b15e32005-06-07 00:04:39 +01001049static 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 -07001050{
1051 struct cfi_private *cfi = map->fldrv_priv;
1052 unsigned long timeo = jiffies + HZ;
1053 /*
1054 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1055 * have a max write time of a few hundreds usec). However, we should
1056 * use the maximum timeout value given by the chip at probe time
1057 * instead. Unfortunately, struct flchip does have a field for
1058 * maximum timeout, only for typical which can be far too short
1059 * depending of the conditions. The ' + 1' is to avoid having a
1060 * timeout of 0 jiffies if HZ is smaller than 1000.
1061 */
1062 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1063 int ret = 0;
1064 map_word oldd;
1065 int retry_cnt = 0;
1066
1067 adr += chip->start;
1068
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001069 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ret = get_chip(map, chip, adr, FL_WRITING);
1071 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001072 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return ret;
1074 }
1075
1076 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1077 __func__, adr, datum.x[0] );
1078
1079 /*
1080 * Check for a NOP for the case when the datum to write is already
1081 * present - it saves time and works around buggy chips that corrupt
1082 * data at other locations when 0xff is written to a location that
1083 * already contains 0xff.
1084 */
1085 oldd = map_read(map, adr);
1086 if (map_word_equal(map, oldd, datum)) {
1087 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1088 __func__);
1089 goto op_done;
1090 }
1091
Todd Poynor02b15e32005-06-07 00:04:39 +01001092 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001094 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 retry:
1096 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1097 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1098 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1099 map_write(map, datum, adr);
1100 chip->state = FL_WRITING;
1101
Todd Poynor02b15e32005-06-07 00:04:39 +01001102 INVALIDATE_CACHE_UDELAY(map, chip,
1103 adr, map_bankwidth(map),
1104 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001107 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 for (;;) {
1109 if (chip->state != FL_WRITING) {
1110 /* Someone's suspended the write. Sleep */
1111 DECLARE_WAITQUEUE(wait, current);
1112
1113 set_current_state(TASK_UNINTERRUPTIBLE);
1114 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001115 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 schedule();
1117 remove_wait_queue(&chip->wq, &wait);
1118 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001119 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 continue;
1121 }
1122
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001123 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001124 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001125 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001126 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001127 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001130 if (chip_ready(map, adr))
1131 break;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001134 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001136 /* Did we succeed? */
1137 if (!chip_good(map, adr, datum)) {
1138 /* reset on all failures. */
1139 map_write( map, CMD(0xF0), chip->start );
1140 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001142 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001143 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001145 ret = -EIO;
1146 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001147 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 op_done:
1149 chip->state = FL_READY;
1150 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001151 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 return ret;
1154}
1155
1156
1157static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1158 size_t *retlen, const u_char *buf)
1159{
1160 struct map_info *map = mtd->priv;
1161 struct cfi_private *cfi = map->fldrv_priv;
1162 int ret = 0;
1163 int chipnum;
1164 unsigned long ofs, chipstart;
1165 DECLARE_WAITQUEUE(wait, current);
1166
1167 *retlen = 0;
1168 if (!len)
1169 return 0;
1170
1171 chipnum = to >> cfi->chipshift;
1172 ofs = to - (chipnum << cfi->chipshift);
1173 chipstart = cfi->chips[chipnum].start;
1174
1175 /* If it's not bus-aligned, do the first byte write */
1176 if (ofs & (map_bankwidth(map)-1)) {
1177 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1178 int i = ofs - bus_ofs;
1179 int n = 0;
1180 map_word tmp_buf;
1181
1182 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001183 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (cfi->chips[chipnum].state != FL_READY) {
1186#if 0
1187 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1188#endif
1189 set_current_state(TASK_UNINTERRUPTIBLE);
1190 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1191
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001192 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 schedule();
1195 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1196#if 0
1197 if(signal_pending(current))
1198 return -EINTR;
1199#endif
1200 goto retry;
1201 }
1202
1203 /* Load 'tmp_buf' with old contents of flash */
1204 tmp_buf = map_read(map, bus_ofs+chipstart);
1205
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001206 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /* Number of bytes to copy from buffer */
1209 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1212
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001213 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001215 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ofs += n;
1219 buf += n;
1220 (*retlen) += n;
1221 len -= n;
1222
1223 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001224 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 ofs = 0;
1226 if (chipnum == cfi->numchips)
1227 return 0;
1228 }
1229 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* We are now aligned, write as much as possible */
1232 while(len >= map_bankwidth(map)) {
1233 map_word datum;
1234
1235 datum = map_word_load(map, buf);
1236
1237 ret = do_write_oneword(map, &cfi->chips[chipnum],
1238 ofs, datum);
1239 if (ret)
1240 return ret;
1241
1242 ofs += map_bankwidth(map);
1243 buf += map_bankwidth(map);
1244 (*retlen) += map_bankwidth(map);
1245 len -= map_bankwidth(map);
1246
1247 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001248 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 ofs = 0;
1250 if (chipnum == cfi->numchips)
1251 return 0;
1252 chipstart = cfi->chips[chipnum].start;
1253 }
1254 }
1255
1256 /* Write the trailing bytes if any */
1257 if (len & (map_bankwidth(map)-1)) {
1258 map_word tmp_buf;
1259
1260 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001261 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (cfi->chips[chipnum].state != FL_READY) {
1264#if 0
1265 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1266#endif
1267 set_current_state(TASK_UNINTERRUPTIBLE);
1268 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1269
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001270 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 schedule();
1273 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1274#if 0
1275 if(signal_pending(current))
1276 return -EINTR;
1277#endif
1278 goto retry1;
1279 }
1280
1281 tmp_buf = map_read(map, ofs + chipstart);
1282
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001283 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001286
1287 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001289 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 (*retlen) += len;
1293 }
1294
1295 return 0;
1296}
1297
1298
1299/*
1300 * FIXME: interleaved mode not tested, and probably not supported!
1301 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001302static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001303 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001304 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct cfi_private *cfi = map->fldrv_priv;
1307 unsigned long timeo = jiffies + HZ;
1308 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1309 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1310 int ret = -EIO;
1311 unsigned long cmd_adr;
1312 int z, words;
1313 map_word datum;
1314
1315 adr += chip->start;
1316 cmd_adr = adr;
1317
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001318 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ret = get_chip(map, chip, adr, FL_WRITING);
1320 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001321 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return ret;
1323 }
1324
1325 datum = map_word_load(map, buf);
1326
1327 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1328 __func__, adr, datum.x[0] );
1329
Todd Poynor02b15e32005-06-07 00:04:39 +01001330 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001332 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1335 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1336 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1337
1338 /* Write Buffer Load */
1339 map_write(map, CMD(0x25), cmd_adr);
1340
1341 chip->state = FL_WRITING_TO_BUFFER;
1342
1343 /* Write length of data to come */
1344 words = len / map_bankwidth(map);
1345 map_write(map, CMD(words - 1), cmd_adr);
1346 /* Write data */
1347 z = 0;
1348 while(z < words * map_bankwidth(map)) {
1349 datum = map_word_load(map, buf);
1350 map_write(map, datum, adr + z);
1351
1352 z += map_bankwidth(map);
1353 buf += map_bankwidth(map);
1354 }
1355 z -= map_bankwidth(map);
1356
1357 adr += z;
1358
1359 /* Write Buffer Program Confirm: GO GO GO */
1360 map_write(map, CMD(0x29), cmd_adr);
1361 chip->state = FL_WRITING;
1362
Todd Poynor02b15e32005-06-07 00:04:39 +01001363 INVALIDATE_CACHE_UDELAY(map, chip,
1364 adr, map_bankwidth(map),
1365 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001367 timeo = jiffies + uWriteTimeout;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 for (;;) {
1370 if (chip->state != FL_WRITING) {
1371 /* Someone's suspended the write. Sleep */
1372 DECLARE_WAITQUEUE(wait, current);
1373
1374 set_current_state(TASK_UNINTERRUPTIBLE);
1375 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001376 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 schedule();
1378 remove_wait_queue(&chip->wq, &wait);
1379 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001380 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 continue;
1382 }
1383
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001384 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1385 break;
1386
Todd Poynor02b15e32005-06-07 00:04:39 +01001387 if (chip_ready(map, adr)) {
1388 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001393 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* reset on all failures. */
1397 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001398 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* FIXME - should have reset delay before continuing */
1400
Todd Poynor02b15e32005-06-07 00:04:39 +01001401 printk(KERN_WARNING "MTD %s(): software timeout\n",
1402 __func__ );
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 ret = -EIO;
1405 op_done:
1406 chip->state = FL_READY;
1407 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001408 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 return ret;
1411}
1412
1413
1414static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1415 size_t *retlen, const u_char *buf)
1416{
1417 struct map_info *map = mtd->priv;
1418 struct cfi_private *cfi = map->fldrv_priv;
1419 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1420 int ret = 0;
1421 int chipnum;
1422 unsigned long ofs;
1423
1424 *retlen = 0;
1425 if (!len)
1426 return 0;
1427
1428 chipnum = to >> cfi->chipshift;
1429 ofs = to - (chipnum << cfi->chipshift);
1430
1431 /* If it's not bus-aligned, do the first word write */
1432 if (ofs & (map_bankwidth(map)-1)) {
1433 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1434 if (local_len > len)
1435 local_len = len;
1436 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1437 local_len, retlen, buf);
1438 if (ret)
1439 return ret;
1440 ofs += local_len;
1441 buf += local_len;
1442 len -= local_len;
1443
1444 if (ofs >> cfi->chipshift) {
1445 chipnum ++;
1446 ofs = 0;
1447 if (chipnum == cfi->numchips)
1448 return 0;
1449 }
1450 }
1451
1452 /* Write buffer is worth it only if more than one word to write... */
1453 while (len >= map_bankwidth(map) * 2) {
1454 /* We must not cross write block boundaries */
1455 int size = wbufsize - (ofs & (wbufsize-1));
1456
1457 if (size > len)
1458 size = len;
1459 if (size % map_bankwidth(map))
1460 size -= size % map_bankwidth(map);
1461
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001462 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ofs, buf, size);
1464 if (ret)
1465 return ret;
1466
1467 ofs += size;
1468 buf += size;
1469 (*retlen) += size;
1470 len -= size;
1471
1472 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001473 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 ofs = 0;
1475 if (chipnum == cfi->numchips)
1476 return 0;
1477 }
1478 }
1479
1480 if (len) {
1481 size_t retlen_dregs = 0;
1482
1483 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1484 len, &retlen_dregs, buf);
1485
1486 *retlen += retlen_dregs;
1487 return ret;
1488 }
1489
1490 return 0;
1491}
1492
1493
1494/*
1495 * Handle devices with one erase region, that only implement
1496 * the chip erase command.
1497 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001498static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct cfi_private *cfi = map->fldrv_priv;
1501 unsigned long timeo = jiffies + HZ;
1502 unsigned long int adr;
1503 DECLARE_WAITQUEUE(wait, current);
1504 int ret = 0;
1505
1506 adr = cfi->addr_unlock1;
1507
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001508 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 ret = get_chip(map, chip, adr, FL_WRITING);
1510 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001511 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return ret;
1513 }
1514
1515 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1516 __func__, chip->start );
1517
Todd Poynor02b15e32005-06-07 00:04:39 +01001518 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001520 xip_disable(map, chip, adr);
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1523 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1524 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1525 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1526 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1527 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1528
1529 chip->state = FL_ERASING;
1530 chip->erase_suspended = 0;
1531 chip->in_progress_block_addr = adr;
1532
Todd Poynor02b15e32005-06-07 00:04:39 +01001533 INVALIDATE_CACHE_UDELAY(map, chip,
1534 adr, map->size,
1535 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 timeo = jiffies + (HZ*20);
1538
1539 for (;;) {
1540 if (chip->state != FL_ERASING) {
1541 /* Someone's suspended the erase. Sleep */
1542 set_current_state(TASK_UNINTERRUPTIBLE);
1543 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001544 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 schedule();
1546 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001547 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 continue;
1549 }
1550 if (chip->erase_suspended) {
1551 /* This erase was suspended and resumed.
1552 Adjust the timeout */
1553 timeo = jiffies + (HZ*20); /* FIXME */
1554 chip->erase_suspended = 0;
1555 }
1556
1557 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
1559
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001560 if (time_after(jiffies, timeo)) {
1561 printk(KERN_WARNING "MTD %s(): software timeout\n",
1562 __func__ );
1563 break;
1564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001567 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001569 /* Did we succeed? */
1570 if (!chip_good(map, adr, map_word_ff(map))) {
1571 /* reset on all failures. */
1572 map_write( map, CMD(0xF0), chip->start );
1573 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001575 ret = -EIO;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001579 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001581 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 return ret;
1584}
1585
1586
Todd Poynor02b15e32005-06-07 00:04:39 +01001587static 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 -07001588{
1589 struct cfi_private *cfi = map->fldrv_priv;
1590 unsigned long timeo = jiffies + HZ;
1591 DECLARE_WAITQUEUE(wait, current);
1592 int ret = 0;
1593
1594 adr += chip->start;
1595
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001596 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ret = get_chip(map, chip, adr, FL_ERASING);
1598 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001599 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return ret;
1601 }
1602
1603 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1604 __func__, adr );
1605
Todd Poynor02b15e32005-06-07 00:04:39 +01001606 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001608 xip_disable(map, chip, adr);
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1611 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1612 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1613 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1614 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1615 map_write(map, CMD(0x30), adr);
1616
1617 chip->state = FL_ERASING;
1618 chip->erase_suspended = 0;
1619 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001620
1621 INVALIDATE_CACHE_UDELAY(map, chip,
1622 adr, len,
1623 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 timeo = jiffies + (HZ*20);
1626
1627 for (;;) {
1628 if (chip->state != FL_ERASING) {
1629 /* Someone's suspended the erase. Sleep */
1630 set_current_state(TASK_UNINTERRUPTIBLE);
1631 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001632 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 schedule();
1634 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001635 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 continue;
1637 }
1638 if (chip->erase_suspended) {
1639 /* This erase was suspended and resumed.
1640 Adjust the timeout */
1641 timeo = jiffies + (HZ*20); /* FIXME */
1642 chip->erase_suspended = 0;
1643 }
1644
Todd Poynor02b15e32005-06-07 00:04:39 +01001645 if (chip_ready(map, adr)) {
1646 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001650 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001651 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001652 printk(KERN_WARNING "MTD %s(): software timeout\n",
1653 __func__ );
1654 break;
1655 }
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001658 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001660 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001661 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001662 /* reset on all failures. */
1663 map_write( map, CMD(0xF0), chip->start );
1664 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001666 ret = -EIO;
1667 }
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 chip->state = FL_READY;
1670 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001671 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return ret;
1673}
1674
1675
Ben Dooksce0f33a2007-05-28 19:59:00 +01001676static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 unsigned long ofs, len;
1679 int ret;
1680
1681 ofs = instr->addr;
1682 len = instr->len;
1683
1684 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1685 if (ret)
1686 return ret;
1687
1688 instr->state = MTD_ERASE_DONE;
1689 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return 0;
1692}
1693
1694
1695static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1696{
1697 struct map_info *map = mtd->priv;
1698 struct cfi_private *cfi = map->fldrv_priv;
1699 int ret = 0;
1700
1701 if (instr->addr != 0)
1702 return -EINVAL;
1703
1704 if (instr->len != mtd->size)
1705 return -EINVAL;
1706
1707 ret = do_erase_chip(map, &cfi->chips[0]);
1708 if (ret)
1709 return ret;
1710
1711 instr->state = MTD_ERASE_DONE;
1712 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715}
1716
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001717static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1718 unsigned long adr, int len, void *thunk)
1719{
1720 struct cfi_private *cfi = map->fldrv_priv;
1721 int ret;
1722
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001723 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001724 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1725 if (ret)
1726 goto out_unlock;
1727 chip->state = FL_LOCKING;
1728
1729 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1730 __func__, adr, len);
1731
1732 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1733 cfi->device_type, NULL);
1734 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1735 cfi->device_type, NULL);
1736 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1737 cfi->device_type, NULL);
1738 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1739 cfi->device_type, NULL);
1740 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1741 cfi->device_type, NULL);
1742 map_write(map, CMD(0x40), chip->start + adr);
1743
1744 chip->state = FL_READY;
1745 put_chip(map, chip, adr + chip->start);
1746 ret = 0;
1747
1748out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001749 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001750 return ret;
1751}
1752
1753static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1754 unsigned long adr, int len, void *thunk)
1755{
1756 struct cfi_private *cfi = map->fldrv_priv;
1757 int ret;
1758
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001759 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001760 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1761 if (ret)
1762 goto out_unlock;
1763 chip->state = FL_UNLOCKING;
1764
1765 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1766 __func__, adr, len);
1767
1768 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1769 cfi->device_type, NULL);
1770 map_write(map, CMD(0x70), adr);
1771
1772 chip->state = FL_READY;
1773 put_chip(map, chip, adr + chip->start);
1774 ret = 0;
1775
1776out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001777 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001778 return ret;
1779}
1780
Adrian Hunter69423d92008-12-10 13:37:21 +00001781static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001782{
1783 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1784}
1785
Adrian Hunter69423d92008-12-10 13:37:21 +00001786static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001787{
1788 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792static void cfi_amdstd_sync (struct mtd_info *mtd)
1793{
1794 struct map_info *map = mtd->priv;
1795 struct cfi_private *cfi = map->fldrv_priv;
1796 int i;
1797 struct flchip *chip;
1798 int ret = 0;
1799 DECLARE_WAITQUEUE(wait, current);
1800
1801 for (i=0; !ret && i<cfi->numchips; i++) {
1802 chip = &cfi->chips[i];
1803
1804 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001805 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 switch(chip->state) {
1808 case FL_READY:
1809 case FL_STATUS:
1810 case FL_CFI_QUERY:
1811 case FL_JEDEC_QUERY:
1812 chip->oldstate = chip->state;
1813 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001814 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * as the whole point is that nobody can do anything
1816 * with the chip now anyway.
1817 */
1818 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001819 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821
1822 default:
1823 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07001824 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001826
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001827 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 schedule();
1830
1831 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 goto retry;
1834 }
1835 }
1836
1837 /* Unlock the chips again */
1838
1839 for (i--; i >=0; i--) {
1840 chip = &cfi->chips[i];
1841
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001842 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (chip->state == FL_SYNCING) {
1845 chip->state = chip->oldstate;
1846 wake_up(&chip->wq);
1847 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001848 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850}
1851
1852
1853static int cfi_amdstd_suspend(struct mtd_info *mtd)
1854{
1855 struct map_info *map = mtd->priv;
1856 struct cfi_private *cfi = map->fldrv_priv;
1857 int i;
1858 struct flchip *chip;
1859 int ret = 0;
1860
1861 for (i=0; !ret && i<cfi->numchips; i++) {
1862 chip = &cfi->chips[i];
1863
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001864 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 switch(chip->state) {
1867 case FL_READY:
1868 case FL_STATUS:
1869 case FL_CFI_QUERY:
1870 case FL_JEDEC_QUERY:
1871 chip->oldstate = chip->state;
1872 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001873 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * as the whole point is that nobody can do anything
1875 * with the chip now anyway.
1876 */
1877 case FL_PM_SUSPENDED:
1878 break;
1879
1880 default:
1881 ret = -EAGAIN;
1882 break;
1883 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001884 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
1887 /* Unlock the chips again */
1888
1889 if (ret) {
1890 for (i--; i >=0; i--) {
1891 chip = &cfi->chips[i];
1892
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001893 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (chip->state == FL_PM_SUSPENDED) {
1896 chip->state = chip->oldstate;
1897 wake_up(&chip->wq);
1898 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001899 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return ret;
1904}
1905
1906
1907static void cfi_amdstd_resume(struct mtd_info *mtd)
1908{
1909 struct map_info *map = mtd->priv;
1910 struct cfi_private *cfi = map->fldrv_priv;
1911 int i;
1912 struct flchip *chip;
1913
1914 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 chip = &cfi->chips[i];
1917
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001918 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (chip->state == FL_PM_SUSPENDED) {
1921 chip->state = FL_READY;
1922 map_write(map, CMD(0xF0), chip->start);
1923 wake_up(&chip->wq);
1924 }
1925 else
1926 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1927
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001928 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930}
1931
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07001932
1933/*
1934 * Ensure that the flash device is put back into read array mode before
1935 * unloading the driver or rebooting. On some systems, rebooting while
1936 * the flash is in query/program/erase mode will prevent the CPU from
1937 * fetching the bootloader code, requiring a hard reset or power cycle.
1938 */
1939static int cfi_amdstd_reset(struct mtd_info *mtd)
1940{
1941 struct map_info *map = mtd->priv;
1942 struct cfi_private *cfi = map->fldrv_priv;
1943 int i, ret;
1944 struct flchip *chip;
1945
1946 for (i = 0; i < cfi->numchips; i++) {
1947
1948 chip = &cfi->chips[i];
1949
1950 mutex_lock(&chip->mutex);
1951
1952 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
1953 if (!ret) {
1954 map_write(map, CMD(0xF0), chip->start);
1955 chip->state = FL_SHUTDOWN;
1956 put_chip(map, chip, chip->start);
1957 }
1958
1959 mutex_unlock(&chip->mutex);
1960 }
1961
1962 return 0;
1963}
1964
1965
1966static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
1967 void *v)
1968{
1969 struct mtd_info *mtd;
1970
1971 mtd = container_of(nb, struct mtd_info, reboot_notifier);
1972 cfi_amdstd_reset(mtd);
1973 return NOTIFY_DONE;
1974}
1975
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977static void cfi_amdstd_destroy(struct mtd_info *mtd)
1978{
1979 struct map_info *map = mtd->priv;
1980 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08001981
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07001982 cfi_amdstd_reset(mtd);
1983 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 kfree(cfi->cmdset_priv);
1985 kfree(cfi->cfiq);
1986 kfree(cfi);
1987 kfree(mtd->eraseregions);
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990MODULE_LICENSE("GPL");
1991MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
1992MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");