blob: 87e86e93ebf98d244f9a7df2a8270dc790136772 [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
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200261static void fixup_old_sst_eraseregion(struct mtd_info *mtd)
262{
263 struct map_info *map = mtd->priv;
264 struct cfi_private *cfi = map->fldrv_priv;
265
266 /*
267 * These flashes report two seperate eraseblock regions based on the
268 * sector_erase-size and block_erase-size, although they both operate on the
269 * same memory. This is not allowed according to CFI, so we just pick the
270 * sector_erase-size.
271 */
272 cfi->cfiq->NumEraseRegions = 1;
273}
274
275static void fixup_sst39vf(struct mtd_info *mtd, void *param)
276{
277 struct map_info *map = mtd->priv;
278 struct cfi_private *cfi = map->fldrv_priv;
279
280 fixup_old_sst_eraseregion(mtd);
281
282 cfi->addr_unlock1 = 0x5555;
283 cfi->addr_unlock2 = 0x2AAA;
284}
285
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200286static void fixup_sst39vf_rev_b(struct mtd_info *mtd, void *param)
287{
288 struct map_info *map = mtd->priv;
289 struct cfi_private *cfi = map->fldrv_priv;
290
291 fixup_old_sst_eraseregion(mtd);
292
293 cfi->addr_unlock1 = 0x555;
294 cfi->addr_unlock2 = 0x2AA;
295}
296
Trent Piepho70b07252008-03-30 21:19:30 -0700297static void fixup_s29gl064n_sectors(struct mtd_info *mtd, void *param)
298{
299 struct map_info *map = mtd->priv;
300 struct cfi_private *cfi = map->fldrv_priv;
301
302 if ((cfi->cfiq->EraseRegionInfo[0] & 0xffff) == 0x003f) {
303 cfi->cfiq->EraseRegionInfo[0] |= 0x0040;
304 pr_warning("%s: Bad S29GL064N CFI data, adjust from 64 to 128 sectors\n", mtd->name);
305 }
306}
307
308static void fixup_s29gl032n_sectors(struct mtd_info *mtd, void *param)
309{
310 struct map_info *map = mtd->priv;
311 struct cfi_private *cfi = map->fldrv_priv;
312
313 if ((cfi->cfiq->EraseRegionInfo[1] & 0xffff) == 0x007e) {
314 cfi->cfiq->EraseRegionInfo[1] &= ~0x0040;
315 pr_warning("%s: Bad S29GL032N CFI data, adjust from 127 to 63 sectors\n", mtd->name);
316 }
317}
318
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200319/* Used to fix CFI-Tables of chips without Extended Query Tables */
320static struct cfi_fixup cfi_nopri_fixup_table[] = {
321 { CFI_MFR_SST, 0x234A, fixup_sst39vf, NULL, }, // SST39VF1602
322 { CFI_MFR_SST, 0x234B, fixup_sst39vf, NULL, }, // SST39VF1601
323 { CFI_MFR_SST, 0x235A, fixup_sst39vf, NULL, }, // SST39VF3202
324 { CFI_MFR_SST, 0x235B, fixup_sst39vf, NULL, }, // SST39VF3201
Guillaume LECERF5a0563f2010-04-24 17:58:27 +0200325 { CFI_MFR_SST, 0x235C, fixup_sst39vf_rev_b, NULL, }, // SST39VF3202B
326 { CFI_MFR_SST, 0x235D, fixup_sst39vf_rev_b, NULL, }, // SST39VF3201B
327 { CFI_MFR_SST, 0x236C, fixup_sst39vf_rev_b, NULL, }, // SST39VF6402B
328 { CFI_MFR_SST, 0x236D, fixup_sst39vf_rev_b, NULL, }, // SST39VF6401B
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200329 { 0, 0, NULL, NULL }
330};
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100333 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef AMD_BOOTLOC_BUG
335 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100336 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#endif
338 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
339 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
340 { CFI_MFR_AMD, 0x0055, fixup_use_secsi, NULL, },
341 { CFI_MFR_AMD, 0x0056, fixup_use_secsi, NULL, },
342 { CFI_MFR_AMD, 0x005C, fixup_use_secsi, NULL, },
343 { CFI_MFR_AMD, 0x005F, fixup_use_secsi, NULL, },
Trent Piepho70b07252008-03-30 21:19:30 -0700344 { CFI_MFR_AMD, 0x0c01, fixup_s29gl064n_sectors, NULL, },
345 { CFI_MFR_AMD, 0x1301, fixup_s29gl064n_sectors, NULL, },
346 { CFI_MFR_AMD, 0x1a00, fixup_s29gl032n_sectors, NULL, },
347 { CFI_MFR_AMD, 0x1a01, fixup_s29gl032n_sectors, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#if !FORCE_WORD_WRITE
349 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL, },
350#endif
351 { 0, 0, NULL, NULL }
352};
353static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFf3e69c62009-12-15 23:01:06 +0100354 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
355 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
356 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 { 0, 0, NULL, NULL }
358};
359
360static struct cfi_fixup fixup_table[] = {
361 /* The CFI vendor ids and the JEDEC vendor IDs appear
362 * to be common. It is like the devices id's are as
363 * well. This table is to pick all cases where
364 * we know that is the case.
365 */
366 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_erase_chip, NULL },
Haavard Skinnemoen01655082006-08-09 11:06:07 +0200367 { CFI_MFR_ATMEL, AT49BV6416, fixup_use_atmel_lock, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 { 0, 0, NULL, NULL }
369};
370
371
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100372static void cfi_fixup_major_minor(struct cfi_private *cfi,
373 struct cfi_pri_amdstd *extp)
374{
375 if (cfi->mfr == CFI_MFR_SAMSUNG && cfi->id == 0x257e &&
376 extp->MajorVersion == '0')
377 extp->MajorVersion = '1';
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
381{
382 struct cfi_private *cfi = map->fldrv_priv;
383 struct mtd_info *mtd;
384 int i;
385
Burman Yan95b93a02006-11-15 21:10:29 +0200386 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!mtd) {
388 printk(KERN_WARNING "Failed to allocate memory for MTD device\n");
389 return NULL;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 mtd->priv = map;
392 mtd->type = MTD_NORFLASH;
393
394 /* Fill in the default mtd operations */
395 mtd->erase = cfi_amdstd_erase_varsize;
396 mtd->write = cfi_amdstd_write_words;
397 mtd->read = cfi_amdstd_read;
398 mtd->sync = cfi_amdstd_sync;
399 mtd->suspend = cfi_amdstd_suspend;
400 mtd->resume = cfi_amdstd_resume;
401 mtd->flags = MTD_CAP_NORFLASH;
402 mtd->name = map->name;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400403 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700405 mtd->reboot_notifier.notifier_call = cfi_amdstd_reboot;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (cfi->cfi_mode==CFI_MODE_CFI){
408 unsigned char bootloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
410 struct cfi_pri_amdstd *extp;
411
412 extp = (struct cfi_pri_amdstd*)cfi_read_pri(map, adr, sizeof(*extp), "Amd/Fujitsu");
Guillaume LECERF564b8492010-04-24 17:58:17 +0200413 if (extp) {
414 /*
415 * It's a real CFI chip, not one for which the probe
416 * routine faked a CFI structure.
417 */
418 cfi_fixup_major_minor(cfi, extp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Guillaume LECERF564b8492010-04-24 17:58:17 +0200420 if (extp->MajorVersion != '1' ||
421 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
422 printk(KERN_ERR " Unknown Amd/Fujitsu Extended Query "
423 "version %c.%c.\n", extp->MajorVersion,
424 extp->MinorVersion);
425 kfree(extp);
426 kfree(mtd);
427 return NULL;
428 }
Wolfgang Grandeggerfefae482009-01-08 19:21:27 +0100429
Guillaume LECERF564b8492010-04-24 17:58:17 +0200430 /* Install our own private info structure */
431 cfi->cmdset_priv = extp;
Todd Poynord88f9772005-07-20 22:01:17 +0100432
Guillaume LECERF564b8492010-04-24 17:58:17 +0200433 /* Apply cfi device specific fixups */
434 cfi_fixup(mtd, cfi_fixup_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436#ifdef DEBUG_CFI_FEATURES
Guillaume LECERF564b8492010-04-24 17:58:17 +0200437 /* Tell the user about it in lots of lovely detail */
438 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Guillaume LECERF564b8492010-04-24 17:58:17 +0200441 bootloc = extp->TopBottom;
David Woodhouse412da2f2010-05-14 01:35:54 +0100442 if ((bootloc < 2) || (bootloc > 5)) {
443 printk(KERN_WARNING "%s: CFI contains unrecognised boot "
444 "bank location (%d). Assuming bottom.\n",
David Woodhouseabab7eb2010-05-14 09:14:24 +0100445 map->name, bootloc);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200446 bootloc = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Guillaume LECERF564b8492010-04-24 17:58:17 +0200448
449 if (bootloc == 3 && cfi->cfiq->NumEraseRegions > 1) {
David Woodhouse412da2f2010-05-14 01:35:54 +0100450 printk(KERN_WARNING "%s: Swapping erase regions for top-boot CFI table.\n", map->name);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200451
452 for (i=0; i<cfi->cfiq->NumEraseRegions / 2; i++) {
453 int j = (cfi->cfiq->NumEraseRegions-1)-i;
454 __u32 swap;
455
456 swap = cfi->cfiq->EraseRegionInfo[i];
457 cfi->cfiq->EraseRegionInfo[i] = cfi->cfiq->EraseRegionInfo[j];
458 cfi->cfiq->EraseRegionInfo[j] = swap;
459 }
460 }
461 /* Set the default CFI lock/unlock addresses */
462 cfi->addr_unlock1 = 0x555;
463 cfi->addr_unlock2 = 0x2aa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Guillaume LECERF83dcd3b2010-04-24 17:58:22 +0200465 cfi_fixup(mtd, cfi_nopri_fixup_table);
Guillaume LECERF564b8492010-04-24 17:58:17 +0200466
467 if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
468 kfree(mtd);
469 return NULL;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 } /* CFI mode */
473 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
474 /* Apply jedec specific fixups */
475 cfi_fixup(mtd, jedec_fixup_table);
476 }
477 /* Apply generic fixups */
478 cfi_fixup(mtd, fixup_table);
479
480 for (i=0; i< cfi->numchips; i++) {
481 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
482 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
483 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
Vijay Sampath83d48092007-03-06 02:39:44 -0800484 cfi->chips[i].ref_point_counter = 0;
485 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 map->fldrv = &cfi_amdstd_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return cfi_amdstd_setup(mtd);
491}
David Woodhouse83ea4ef2006-05-08 22:58:25 +0100492EXPORT_SYMBOL_GPL(cfi_cmdset_0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
495{
496 struct map_info *map = mtd->priv;
497 struct cfi_private *cfi = map->fldrv_priv;
498 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
499 unsigned long offset = 0;
500 int i,j;
501
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000502 printk(KERN_NOTICE "number of %s chips: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 (cfi->cfi_mode == CFI_MODE_CFI)?"CFI":"JEDEC",cfi->numchips);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000504 /* Select the correct geometry setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 mtd->size = devsize * cfi->numchips;
506
507 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
508 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
509 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000510 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 printk(KERN_WARNING "Failed to allocate memory for MTD erase region info\n");
512 goto setup_err;
513 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
516 unsigned long ernum, ersize;
517 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
518 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (mtd->erasesize < ersize) {
521 mtd->erasesize = ersize;
522 }
523 for (j=0; j<cfi->numchips; j++) {
524 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
525 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
526 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
527 }
528 offset += (ersize * ernum);
529 }
530 if (offset != devsize) {
531 /* Argh */
532 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
533 goto setup_err;
534 }
535#if 0
536 // debug
537 for (i=0; i<mtd->numeraseregions;i++){
538 printk("%d: offset=0x%x,size=0x%x,blocks=%d\n",
539 i,mtd->eraseregions[i].offset,
540 mtd->eraseregions[i].erasesize,
541 mtd->eraseregions[i].numblocks);
542 }
543#endif
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 __module_get(THIS_MODULE);
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700546 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return mtd;
548
549 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100550 kfree(mtd->eraseregions);
551 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 kfree(cfi->cmdset_priv);
553 kfree(cfi->cfiq);
554 return NULL;
555}
556
557/*
558 * Return true if the chip is ready.
559 *
560 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
561 * non-suspended sector) and is indicated by no toggle bits toggling.
562 *
563 * Note that anything more complicated than checking if no bits are toggling
564 * (including checking DQ5 for an error status) is tricky to get working
565 * correctly and is therefore not done (particulary with interleaved chips
566 * as each chip must be checked independantly of the others).
567 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100568static int __xipram chip_ready(struct map_info *map, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 map_word d, t;
571
572 d = map_read(map, addr);
573 t = map_read(map, addr);
574
575 return map_word_equal(map, d, t);
576}
577
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100578/*
579 * Return true if the chip is ready and has the correct value.
580 *
581 * Ready is one of: read mode, query mode, erase-suspend-read mode (in any
582 * non-suspended sector) and it is indicated by no bits toggling.
583 *
584 * Error are indicated by toggling bits or bits held with the wrong value,
585 * or with bits toggling.
586 *
587 * Note that anything more complicated than checking if no bits are toggling
588 * (including checking DQ5 for an error status) is tricky to get working
589 * correctly and is therefore not done (particulary with interleaved chips
590 * as each chip must be checked independantly of the others).
591 *
592 */
Todd Poynor02b15e32005-06-07 00:04:39 +0100593static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100594{
595 map_word oldd, curd;
596
597 oldd = map_read(map, addr);
598 curd = map_read(map, addr);
599
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000600 return map_word_equal(map, oldd, curd) &&
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +0100601 map_word_equal(map, curd, expected);
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
605{
606 DECLARE_WAITQUEUE(wait, current);
607 struct cfi_private *cfi = map->fldrv_priv;
608 unsigned long timeo;
609 struct cfi_pri_amdstd *cfip = (struct cfi_pri_amdstd *)cfi->cmdset_priv;
610
611 resettime:
612 timeo = jiffies + HZ;
613 retry:
614 switch (chip->state) {
615
616 case FL_STATUS:
617 for (;;) {
618 if (chip_ready(map, adr))
619 break;
620
621 if (time_after(jiffies, timeo)) {
622 printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EIO;
624 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200625 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200627 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* Someone else might have been playing with it. */
629 goto retry;
630 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 case FL_READY:
633 case FL_CFI_QUERY:
634 case FL_JEDEC_QUERY:
635 return 0;
636
637 case FL_ERASING:
Joakim Tjernlund2695eab2009-11-19 12:01:58 +0100638 if (!cfip || !(cfip->EraseSuspend & (0x1|0x2)) ||
639 !(mode == FL_READY || mode == FL_POINT ||
640 (mode == FL_WRITING && (cfip->EraseSuspend & 0x2))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto sleep;
642
643 /* We could check to see if we're trying to access the sector
644 * that is currently being erased. However, no user will try
645 * anything like that so we just wait for the timeout. */
646
647 /* Erase suspend */
648 /* It's harmless to issue the Erase-Suspend and Erase-Resume
649 * commands when the erase algorithm isn't in progress. */
650 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
651 chip->oldstate = FL_ERASING;
652 chip->state = FL_ERASE_SUSPENDING;
653 chip->erase_suspended = 1;
654 for (;;) {
655 if (chip_ready(map, adr))
656 break;
657
658 if (time_after(jiffies, timeo)) {
659 /* Should have suspended the erase by now.
660 * Send an Erase-Resume command as either
661 * there was an error (so leave the erase
662 * routine to recover from it) or we trying to
663 * use the erase-in-progress sector. */
664 map_write(map, CMD(0x30), chip->in_progress_block_addr);
665 chip->state = FL_ERASING;
666 chip->oldstate = FL_READY;
667 printk(KERN_ERR "MTD %s(): chip not ready after erase suspend\n", __func__);
668 return -EIO;
669 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000670
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200671 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200673 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
675 So we can just loop here. */
676 }
677 chip->state = FL_READY;
678 return 0;
679
Todd Poynor02b15e32005-06-07 00:04:39 +0100680 case FL_XIP_WHILE_ERASING:
681 if (mode != FL_READY && mode != FL_POINT &&
682 (!cfip || !(cfip->EraseSuspend&2)))
683 goto sleep;
684 chip->oldstate = chip->state;
685 chip->state = FL_READY;
686 return 0;
687
Kevin Cernekeeeafe1312010-04-29 10:26:56 -0700688 case FL_SHUTDOWN:
689 /* The machine is rebooting */
690 return -EIO;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 case FL_POINT:
693 /* Only if there's no operation suspended... */
694 if (mode == FL_READY && chip->oldstate == FL_READY)
695 return 0;
696
697 default:
698 sleep:
699 set_current_state(TASK_UNINTERRUPTIBLE);
700 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200701 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 schedule();
703 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200704 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto resettime;
706 }
707}
708
709
710static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
711{
712 struct cfi_private *cfi = map->fldrv_priv;
713
714 switch(chip->oldstate) {
715 case FL_ERASING:
716 chip->state = chip->oldstate;
717 map_write(map, CMD(0x30), chip->in_progress_block_addr);
718 chip->oldstate = FL_READY;
719 chip->state = FL_ERASING;
720 break;
721
Todd Poynor02b15e32005-06-07 00:04:39 +0100722 case FL_XIP_WHILE_ERASING:
723 chip->state = chip->oldstate;
724 chip->oldstate = FL_READY;
725 break;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 case FL_READY:
728 case FL_STATUS:
729 /* We should really make set_vpp() count, rather than doing this */
730 DISABLE_VPP(map);
731 break;
732 default:
733 printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
734 }
735 wake_up(&chip->wq);
736}
737
Todd Poynor02b15e32005-06-07 00:04:39 +0100738#ifdef CONFIG_MTD_XIP
739
740/*
741 * No interrupt what so ever can be serviced while the flash isn't in array
742 * mode. This is ensured by the xip_disable() and xip_enable() functions
743 * enclosing any code path where the flash is known not to be in array mode.
744 * And within a XIP disabled code path, only functions marked with __xipram
745 * may be called and nothing else (it's a good thing to inspect generated
746 * assembly to make sure inline functions were actually inlined and that gcc
747 * didn't emit calls to its own support functions). Also configuring MTD CFI
748 * support to a single buswidth and a single interleave is also recommended.
749 */
Thomas Gleixnerf8eb3212005-07-05 01:03:06 +0200750
Todd Poynor02b15e32005-06-07 00:04:39 +0100751static void xip_disable(struct map_info *map, struct flchip *chip,
752 unsigned long adr)
753{
754 /* TODO: chips with no XIP use should ignore and return */
755 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
756 local_irq_disable();
757}
758
759static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
760 unsigned long adr)
761{
762 struct cfi_private *cfi = map->fldrv_priv;
763
764 if (chip->state != FL_POINT && chip->state != FL_READY) {
765 map_write(map, CMD(0xf0), adr);
766 chip->state = FL_READY;
767 }
768 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200769 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100770 local_irq_enable();
771}
772
773/*
774 * When a delay is required for the flash operation to complete, the
775 * xip_udelay() function is polling for both the given timeout and pending
776 * (but still masked) hardware interrupts. Whenever there is an interrupt
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000777 * pending then the flash erase operation is suspended, array mode restored
Todd Poynor02b15e32005-06-07 00:04:39 +0100778 * and interrupts unmasked. Task scheduling might also happen at that
779 * point. The CPU eventually returns from the interrupt or the call to
780 * schedule() and the suspended flash operation is resumed for the remaining
781 * of the delay period.
782 *
783 * Warning: this function _will_ fool interrupt latency tracing tools.
784 */
785
786static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
787 unsigned long adr, int usec)
788{
789 struct cfi_private *cfi = map->fldrv_priv;
790 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
791 map_word status, OK = CMD(0x80);
792 unsigned long suspended, start = xip_currtime();
793 flstate_t oldstate;
794
795 do {
796 cpu_relax();
797 if (xip_irqpending() && extp &&
798 ((chip->state == FL_ERASING && (extp->EraseSuspend & 2))) &&
799 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
800 /*
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000801 * Let's suspend the erase operation when supported.
802 * Note that we currently don't try to suspend
803 * interleaved chips if there is already another
Todd Poynor02b15e32005-06-07 00:04:39 +0100804 * operation suspended (imagine what happens
805 * when one chip was already done with the current
806 * operation while another chip suspended it, then
807 * we resume the whole thing at once). Yes, it
808 * can happen!
809 */
810 map_write(map, CMD(0xb0), adr);
811 usec -= xip_elapsed_since(start);
812 suspended = xip_currtime();
813 do {
814 if (xip_elapsed_since(suspended) > 100000) {
815 /*
816 * The chip doesn't want to suspend
817 * after waiting for 100 msecs.
818 * This is a critical error but there
819 * is not much we can do here.
820 */
821 return;
822 }
823 status = map_read(map, adr);
824 } while (!map_word_andequal(map, status, OK, OK));
825
826 /* Suspend succeeded */
827 oldstate = chip->state;
828 if (!map_word_bitsset(map, status, CMD(0x40)))
829 break;
830 chip->state = FL_XIP_WHILE_ERASING;
831 chip->erase_suspended = 1;
832 map_write(map, CMD(0xf0), adr);
833 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200834 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100835 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200836 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +0200837 xip_iprefetch();
Todd Poynor02b15e32005-06-07 00:04:39 +0100838 cond_resched();
839
840 /*
841 * We're back. However someone else might have
842 * decided to go write to the chip if we are in
843 * a suspended erase state. If so let's wait
844 * until it's done.
845 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200846 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100847 while (chip->state != FL_XIP_WHILE_ERASING) {
848 DECLARE_WAITQUEUE(wait, current);
849 set_current_state(TASK_UNINTERRUPTIBLE);
850 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200851 mutex_unlock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100852 schedule();
853 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200854 mutex_lock(&chip->mutex);
Todd Poynor02b15e32005-06-07 00:04:39 +0100855 }
856 /* Disallow XIP again */
857 local_irq_disable();
858
859 /* Resume the write or erase operation */
860 map_write(map, CMD(0x30), adr);
861 chip->state = oldstate;
862 start = xip_currtime();
863 } else if (usec >= 1000000/HZ) {
864 /*
865 * Try to save on CPU power when waiting delay
866 * is at least a system timer tick period.
867 * No need to be extremely accurate here.
868 */
869 xip_cpu_idle();
870 }
871 status = map_read(map, adr);
872 } while (!map_word_andequal(map, status, OK, OK)
873 && xip_elapsed_since(start) < usec);
874}
875
876#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
877
878/*
879 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
880 * the flash is actively programming or erasing since we have to poll for
881 * the operation to complete anyway. We can't do that in a generic way with
882 * a XIP setup so do it before the actual flash operation in this case
883 * and stub it out from INVALIDATE_CACHE_UDELAY.
884 */
885#define XIP_INVAL_CACHED_RANGE(map, from, size) \
886 INVALIDATE_CACHED_RANGE(map, from, size)
887
888#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
889 UDELAY(map, chip, adr, usec)
890
891/*
892 * Extra notes:
893 *
894 * Activating this XIP support changes the way the code works a bit. For
895 * example the code to suspend the current process when concurrent access
896 * happens is never executed because xip_udelay() will always return with the
897 * same chip state as it was entered with. This is why there is no care for
898 * the presence of add_wait_queue() or schedule() calls from within a couple
899 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
900 * The queueing and scheduling are always happening within xip_udelay().
901 *
902 * Similarly, get_chip() and put_chip() just happen to always be executed
903 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
904 * is in array mode, therefore never executing many cases therein and not
905 * causing any problem with XIP.
906 */
907
908#else
909
910#define xip_disable(map, chip, adr)
911#define xip_enable(map, chip, adr)
912#define XIP_INVAL_CACHED_RANGE(x...)
913
914#define UDELAY(map, chip, adr, usec) \
915do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200916 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100917 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200918 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100919} while (0)
920
921#define INVALIDATE_CACHE_UDELAY(map, chip, adr, len, usec) \
922do { \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200923 mutex_unlock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100924 INVALIDATE_CACHED_RANGE(map, adr, len); \
925 cfi_udelay(usec); \
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200926 mutex_lock(&chip->mutex); \
Todd Poynor02b15e32005-06-07 00:04:39 +0100927} while (0)
928
929#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
932{
933 unsigned long cmd_addr;
934 struct cfi_private *cfi = map->fldrv_priv;
935 int ret;
936
937 adr += chip->start;
938
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000939 /* Ensure cmd read/writes are aligned. */
940 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200942 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 ret = get_chip(map, chip, cmd_addr, FL_READY);
944 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200945 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return ret;
947 }
948
949 if (chip->state != FL_POINT && chip->state != FL_READY) {
950 map_write(map, CMD(0xf0), cmd_addr);
951 chip->state = FL_READY;
952 }
953
954 map_copy_from(map, buf, adr, len);
955
956 put_chip(map, chip, cmd_addr);
957
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200958 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960}
961
962
963static int cfi_amdstd_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
964{
965 struct map_info *map = mtd->priv;
966 struct cfi_private *cfi = map->fldrv_priv;
967 unsigned long ofs;
968 int chipnum;
969 int ret = 0;
970
971 /* ofs: offset within the first chip that the first read should start */
972
973 chipnum = (from >> cfi->chipshift);
974 ofs = from - (chipnum << cfi->chipshift);
975
976
977 *retlen = 0;
978
979 while (len) {
980 unsigned long thislen;
981
982 if (chipnum >= cfi->numchips)
983 break;
984
985 if ((len + ofs -1) >> cfi->chipshift)
986 thislen = (1<<cfi->chipshift) - ofs;
987 else
988 thislen = len;
989
990 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
991 if (ret)
992 break;
993
994 *retlen += thislen;
995 len -= thislen;
996 buf += thislen;
997
998 ofs = 0;
999 chipnum++;
1000 }
1001 return ret;
1002}
1003
1004
1005static inline int do_read_secsi_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1006{
1007 DECLARE_WAITQUEUE(wait, current);
1008 unsigned long timeo = jiffies + HZ;
1009 struct cfi_private *cfi = map->fldrv_priv;
1010
1011 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001012 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 if (chip->state != FL_READY){
1015#if 0
1016 printk(KERN_DEBUG "Waiting for chip to read, status = %d\n", chip->state);
1017#endif
1018 set_current_state(TASK_UNINTERRUPTIBLE);
1019 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001020
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001021 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 schedule();
1024 remove_wait_queue(&chip->wq, &wait);
1025#if 0
1026 if(signal_pending(current))
1027 return -EINTR;
1028#endif
1029 timeo = jiffies + HZ;
1030
1031 goto retry;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 adr += chip->start;
1035
1036 chip->state = FL_READY;
1037
1038 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1039 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1040 cfi_send_gen_cmd(0x88, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 map_copy_from(map, buf, adr, len);
1043
1044 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1045 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1046 cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1047 cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 wake_up(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001050 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 return 0;
1053}
1054
1055static int cfi_amdstd_secsi_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1056{
1057 struct map_info *map = mtd->priv;
1058 struct cfi_private *cfi = map->fldrv_priv;
1059 unsigned long ofs;
1060 int chipnum;
1061 int ret = 0;
1062
1063
1064 /* ofs: offset within the first chip that the first read should start */
1065
1066 /* 8 secsi bytes per chip */
1067 chipnum=from>>3;
1068 ofs=from & 7;
1069
1070
1071 *retlen = 0;
1072
1073 while (len) {
1074 unsigned long thislen;
1075
1076 if (chipnum >= cfi->numchips)
1077 break;
1078
1079 if ((len + ofs -1) >> 3)
1080 thislen = (1<<3) - ofs;
1081 else
1082 thislen = len;
1083
1084 ret = do_read_secsi_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1085 if (ret)
1086 break;
1087
1088 *retlen += thislen;
1089 len -= thislen;
1090 buf += thislen;
1091
1092 ofs = 0;
1093 chipnum++;
1094 }
1095 return ret;
1096}
1097
1098
Todd Poynor02b15e32005-06-07 00:04:39 +01001099static 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 -07001100{
1101 struct cfi_private *cfi = map->fldrv_priv;
1102 unsigned long timeo = jiffies + HZ;
1103 /*
1104 * We use a 1ms + 1 jiffies generic timeout for writes (most devices
1105 * have a max write time of a few hundreds usec). However, we should
1106 * use the maximum timeout value given by the chip at probe time
1107 * instead. Unfortunately, struct flchip does have a field for
1108 * maximum timeout, only for typical which can be far too short
1109 * depending of the conditions. The ' + 1' is to avoid having a
1110 * timeout of 0 jiffies if HZ is smaller than 1000.
1111 */
1112 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1113 int ret = 0;
1114 map_word oldd;
1115 int retry_cnt = 0;
1116
1117 adr += chip->start;
1118
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001119 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ret = get_chip(map, chip, adr, FL_WRITING);
1121 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001122 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return ret;
1124 }
1125
1126 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1127 __func__, adr, datum.x[0] );
1128
1129 /*
1130 * Check for a NOP for the case when the datum to write is already
1131 * present - it saves time and works around buggy chips that corrupt
1132 * data at other locations when 0xff is written to a location that
1133 * already contains 0xff.
1134 */
1135 oldd = map_read(map, adr);
1136 if (map_word_equal(map, oldd, datum)) {
1137 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): NOP\n",
1138 __func__);
1139 goto op_done;
1140 }
1141
Todd Poynor02b15e32005-06-07 00:04:39 +01001142 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001144 xip_disable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 retry:
1146 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1147 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1148 cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1149 map_write(map, datum, adr);
1150 chip->state = FL_WRITING;
1151
Todd Poynor02b15e32005-06-07 00:04:39 +01001152 INVALIDATE_CACHE_UDELAY(map, chip,
1153 adr, map_bankwidth(map),
1154 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /* See comment above for timeout value. */
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001157 timeo = jiffies + uWriteTimeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 for (;;) {
1159 if (chip->state != FL_WRITING) {
1160 /* Someone's suspended the write. Sleep */
1161 DECLARE_WAITQUEUE(wait, current);
1162
1163 set_current_state(TASK_UNINTERRUPTIBLE);
1164 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001165 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 schedule();
1167 remove_wait_queue(&chip->wq, &wait);
1168 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001169 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 continue;
1171 }
1172
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001173 if (time_after(jiffies, timeo) && !chip_ready(map, adr)){
Todd Poynor02b15e32005-06-07 00:04:39 +01001174 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001175 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__);
Todd Poynor02b15e32005-06-07 00:04:39 +01001176 xip_disable(map, chip, adr);
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001177 break;
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001180 if (chip_ready(map, adr))
1181 break;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001184 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001186 /* Did we succeed? */
1187 if (!chip_good(map, adr, datum)) {
1188 /* reset on all failures. */
1189 map_write( map, CMD(0xF0), chip->start );
1190 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001192 if (++retry_cnt <= MAX_WORD_RETRIES)
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001193 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001195 ret = -EIO;
1196 }
Todd Poynor02b15e32005-06-07 00:04:39 +01001197 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 op_done:
1199 chip->state = FL_READY;
1200 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001201 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 return ret;
1204}
1205
1206
1207static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
1208 size_t *retlen, const u_char *buf)
1209{
1210 struct map_info *map = mtd->priv;
1211 struct cfi_private *cfi = map->fldrv_priv;
1212 int ret = 0;
1213 int chipnum;
1214 unsigned long ofs, chipstart;
1215 DECLARE_WAITQUEUE(wait, current);
1216
1217 *retlen = 0;
1218 if (!len)
1219 return 0;
1220
1221 chipnum = to >> cfi->chipshift;
1222 ofs = to - (chipnum << cfi->chipshift);
1223 chipstart = cfi->chips[chipnum].start;
1224
1225 /* If it's not bus-aligned, do the first byte write */
1226 if (ofs & (map_bankwidth(map)-1)) {
1227 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1228 int i = ofs - bus_ofs;
1229 int n = 0;
1230 map_word tmp_buf;
1231
1232 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001233 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (cfi->chips[chipnum].state != FL_READY) {
1236#if 0
1237 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1238#endif
1239 set_current_state(TASK_UNINTERRUPTIBLE);
1240 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1241
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001242 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 schedule();
1245 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1246#if 0
1247 if(signal_pending(current))
1248 return -EINTR;
1249#endif
1250 goto retry;
1251 }
1252
1253 /* Load 'tmp_buf' with old contents of flash */
1254 tmp_buf = map_read(map, bus_ofs+chipstart);
1255
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001256 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* Number of bytes to copy from buffer */
1259 n = min_t(int, len, map_bankwidth(map)-i);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 tmp_buf = map_word_load_partial(map, tmp_buf, buf, i, n);
1262
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001263 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 bus_ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001265 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ofs += n;
1269 buf += n;
1270 (*retlen) += n;
1271 len -= n;
1272
1273 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001274 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 ofs = 0;
1276 if (chipnum == cfi->numchips)
1277 return 0;
1278 }
1279 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* We are now aligned, write as much as possible */
1282 while(len >= map_bankwidth(map)) {
1283 map_word datum;
1284
1285 datum = map_word_load(map, buf);
1286
1287 ret = do_write_oneword(map, &cfi->chips[chipnum],
1288 ofs, datum);
1289 if (ret)
1290 return ret;
1291
1292 ofs += map_bankwidth(map);
1293 buf += map_bankwidth(map);
1294 (*retlen) += map_bankwidth(map);
1295 len -= map_bankwidth(map);
1296
1297 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001298 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ofs = 0;
1300 if (chipnum == cfi->numchips)
1301 return 0;
1302 chipstart = cfi->chips[chipnum].start;
1303 }
1304 }
1305
1306 /* Write the trailing bytes if any */
1307 if (len & (map_bankwidth(map)-1)) {
1308 map_word tmp_buf;
1309
1310 retry1:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001311 mutex_lock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 if (cfi->chips[chipnum].state != FL_READY) {
1314#if 0
1315 printk(KERN_DEBUG "Waiting for chip to write, status = %d\n", cfi->chips[chipnum].state);
1316#endif
1317 set_current_state(TASK_UNINTERRUPTIBLE);
1318 add_wait_queue(&cfi->chips[chipnum].wq, &wait);
1319
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001320 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 schedule();
1323 remove_wait_queue(&cfi->chips[chipnum].wq, &wait);
1324#if 0
1325 if(signal_pending(current))
1326 return -EINTR;
1327#endif
1328 goto retry1;
1329 }
1330
1331 tmp_buf = map_read(map, ofs + chipstart);
1332
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001333 mutex_unlock(&cfi->chips[chipnum].mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 tmp_buf = map_word_load_partial(map, tmp_buf, buf, 0, len);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001336
1337 ret = do_write_oneword(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ofs, tmp_buf);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001339 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 (*retlen) += len;
1343 }
1344
1345 return 0;
1346}
1347
1348
1349/*
1350 * FIXME: interleaved mode not tested, and probably not supported!
1351 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001352static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001353 unsigned long adr, const u_char *buf,
Todd Poynor02b15e32005-06-07 00:04:39 +01001354 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct cfi_private *cfi = map->fldrv_priv;
1357 unsigned long timeo = jiffies + HZ;
1358 /* see comments in do_write_oneword() regarding uWriteTimeo. */
1359 unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
1360 int ret = -EIO;
1361 unsigned long cmd_adr;
1362 int z, words;
1363 map_word datum;
1364
1365 adr += chip->start;
1366 cmd_adr = adr;
1367
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001368 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ret = get_chip(map, chip, adr, FL_WRITING);
1370 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001371 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return ret;
1373 }
1374
1375 datum = map_word_load(map, buf);
1376
1377 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): WRITE 0x%.8lx(0x%.8lx)\n",
1378 __func__, adr, datum.x[0] );
1379
Todd Poynor02b15e32005-06-07 00:04:39 +01001380 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001382 xip_disable(map, chip, cmd_adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1385 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1386 //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1387
1388 /* Write Buffer Load */
1389 map_write(map, CMD(0x25), cmd_adr);
1390
1391 chip->state = FL_WRITING_TO_BUFFER;
1392
1393 /* Write length of data to come */
1394 words = len / map_bankwidth(map);
1395 map_write(map, CMD(words - 1), cmd_adr);
1396 /* Write data */
1397 z = 0;
1398 while(z < words * map_bankwidth(map)) {
1399 datum = map_word_load(map, buf);
1400 map_write(map, datum, adr + z);
1401
1402 z += map_bankwidth(map);
1403 buf += map_bankwidth(map);
1404 }
1405 z -= map_bankwidth(map);
1406
1407 adr += z;
1408
1409 /* Write Buffer Program Confirm: GO GO GO */
1410 map_write(map, CMD(0x29), cmd_adr);
1411 chip->state = FL_WRITING;
1412
Todd Poynor02b15e32005-06-07 00:04:39 +01001413 INVALIDATE_CACHE_UDELAY(map, chip,
1414 adr, map_bankwidth(map),
1415 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001417 timeo = jiffies + uWriteTimeout;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (;;) {
1420 if (chip->state != FL_WRITING) {
1421 /* Someone's suspended the write. Sleep */
1422 DECLARE_WAITQUEUE(wait, current);
1423
1424 set_current_state(TASK_UNINTERRUPTIBLE);
1425 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001426 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 schedule();
1428 remove_wait_queue(&chip->wq, &wait);
1429 timeo = jiffies + (HZ / 2); /* FIXME */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001430 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 continue;
1432 }
1433
Konstantin Baidarovb95f9602005-11-07 09:00:05 +00001434 if (time_after(jiffies, timeo) && !chip_ready(map, adr))
1435 break;
1436
Todd Poynor02b15e32005-06-07 00:04:39 +01001437 if (chip_ready(map, adr)) {
1438 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto op_done;
Todd Poynor02b15e32005-06-07 00:04:39 +01001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001443 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* reset on all failures. */
1447 map_write( map, CMD(0xF0), chip->start );
Todd Poynor02b15e32005-06-07 00:04:39 +01001448 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* FIXME - should have reset delay before continuing */
1450
Todd Poynor02b15e32005-06-07 00:04:39 +01001451 printk(KERN_WARNING "MTD %s(): software timeout\n",
1452 __func__ );
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 ret = -EIO;
1455 op_done:
1456 chip->state = FL_READY;
1457 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001458 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 return ret;
1461}
1462
1463
1464static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
1465 size_t *retlen, const u_char *buf)
1466{
1467 struct map_info *map = mtd->priv;
1468 struct cfi_private *cfi = map->fldrv_priv;
1469 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1470 int ret = 0;
1471 int chipnum;
1472 unsigned long ofs;
1473
1474 *retlen = 0;
1475 if (!len)
1476 return 0;
1477
1478 chipnum = to >> cfi->chipshift;
1479 ofs = to - (chipnum << cfi->chipshift);
1480
1481 /* If it's not bus-aligned, do the first word write */
1482 if (ofs & (map_bankwidth(map)-1)) {
1483 size_t local_len = (-ofs)&(map_bankwidth(map)-1);
1484 if (local_len > len)
1485 local_len = len;
1486 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1487 local_len, retlen, buf);
1488 if (ret)
1489 return ret;
1490 ofs += local_len;
1491 buf += local_len;
1492 len -= local_len;
1493
1494 if (ofs >> cfi->chipshift) {
1495 chipnum ++;
1496 ofs = 0;
1497 if (chipnum == cfi->numchips)
1498 return 0;
1499 }
1500 }
1501
1502 /* Write buffer is worth it only if more than one word to write... */
1503 while (len >= map_bankwidth(map) * 2) {
1504 /* We must not cross write block boundaries */
1505 int size = wbufsize - (ofs & (wbufsize-1));
1506
1507 if (size > len)
1508 size = len;
1509 if (size % map_bankwidth(map))
1510 size -= size % map_bankwidth(map);
1511
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001512 ret = do_write_buffer(map, &cfi->chips[chipnum],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 ofs, buf, size);
1514 if (ret)
1515 return ret;
1516
1517 ofs += size;
1518 buf += size;
1519 (*retlen) += size;
1520 len -= size;
1521
1522 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001523 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 ofs = 0;
1525 if (chipnum == cfi->numchips)
1526 return 0;
1527 }
1528 }
1529
1530 if (len) {
1531 size_t retlen_dregs = 0;
1532
1533 ret = cfi_amdstd_write_words(mtd, ofs + (chipnum<<cfi->chipshift),
1534 len, &retlen_dregs, buf);
1535
1536 *retlen += retlen_dregs;
1537 return ret;
1538 }
1539
1540 return 0;
1541}
1542
1543
1544/*
1545 * Handle devices with one erase region, that only implement
1546 * the chip erase command.
1547 */
Todd Poynor02b15e32005-06-07 00:04:39 +01001548static int __xipram do_erase_chip(struct map_info *map, struct flchip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 struct cfi_private *cfi = map->fldrv_priv;
1551 unsigned long timeo = jiffies + HZ;
1552 unsigned long int adr;
1553 DECLARE_WAITQUEUE(wait, current);
1554 int ret = 0;
1555
1556 adr = cfi->addr_unlock1;
1557
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001558 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ret = get_chip(map, chip, adr, FL_WRITING);
1560 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001561 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return ret;
1563 }
1564
1565 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1566 __func__, chip->start );
1567
Todd Poynor02b15e32005-06-07 00:04:39 +01001568 XIP_INVAL_CACHED_RANGE(map, adr, map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001570 xip_disable(map, chip, adr);
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1573 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1574 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1575 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1576 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1577 cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1578
1579 chip->state = FL_ERASING;
1580 chip->erase_suspended = 0;
1581 chip->in_progress_block_addr = adr;
1582
Todd Poynor02b15e32005-06-07 00:04:39 +01001583 INVALIDATE_CACHE_UDELAY(map, chip,
1584 adr, map->size,
1585 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 timeo = jiffies + (HZ*20);
1588
1589 for (;;) {
1590 if (chip->state != FL_ERASING) {
1591 /* Someone's suspended the erase. Sleep */
1592 set_current_state(TASK_UNINTERRUPTIBLE);
1593 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001594 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 schedule();
1596 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001597 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 continue;
1599 }
1600 if (chip->erase_suspended) {
1601 /* This erase was suspended and resumed.
1602 Adjust the timeout */
1603 timeo = jiffies + (HZ*20); /* FIXME */
1604 chip->erase_suspended = 0;
1605 }
1606
1607 if (chip_ready(map, adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001610 if (time_after(jiffies, timeo)) {
1611 printk(KERN_WARNING "MTD %s(): software timeout\n",
1612 __func__ );
1613 break;
1614 }
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001617 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001619 /* Did we succeed? */
1620 if (!chip_good(map, adr, map_word_ff(map))) {
1621 /* reset on all failures. */
1622 map_write( map, CMD(0xF0), chip->start );
1623 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001625 ret = -EIO;
1626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 chip->state = FL_READY;
Todd Poynor02b15e32005-06-07 00:04:39 +01001629 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001631 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 return ret;
1634}
1635
1636
Todd Poynor02b15e32005-06-07 00:04:39 +01001637static 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 -07001638{
1639 struct cfi_private *cfi = map->fldrv_priv;
1640 unsigned long timeo = jiffies + HZ;
1641 DECLARE_WAITQUEUE(wait, current);
1642 int ret = 0;
1643
1644 adr += chip->start;
1645
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001646 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 ret = get_chip(map, chip, adr, FL_ERASING);
1648 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001649 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return ret;
1651 }
1652
1653 DEBUG( MTD_DEBUG_LEVEL3, "MTD %s(): ERASE 0x%.8lx\n",
1654 __func__, adr );
1655
Todd Poynor02b15e32005-06-07 00:04:39 +01001656 XIP_INVAL_CACHED_RANGE(map, adr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ENABLE_VPP(map);
Todd Poynor02b15e32005-06-07 00:04:39 +01001658 xip_disable(map, chip, adr);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1661 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1662 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1663 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
1664 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
1665 map_write(map, CMD(0x30), adr);
1666
1667 chip->state = FL_ERASING;
1668 chip->erase_suspended = 0;
1669 chip->in_progress_block_addr = adr;
Todd Poynor02b15e32005-06-07 00:04:39 +01001670
1671 INVALIDATE_CACHE_UDELAY(map, chip,
1672 adr, len,
1673 chip->erase_time*500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 timeo = jiffies + (HZ*20);
1676
1677 for (;;) {
1678 if (chip->state != FL_ERASING) {
1679 /* Someone's suspended the erase. Sleep */
1680 set_current_state(TASK_UNINTERRUPTIBLE);
1681 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001682 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 schedule();
1684 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001685 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 continue;
1687 }
1688 if (chip->erase_suspended) {
1689 /* This erase was suspended and resumed.
1690 Adjust the timeout */
1691 timeo = jiffies + (HZ*20); /* FIXME */
1692 chip->erase_suspended = 0;
1693 }
1694
Todd Poynor02b15e32005-06-07 00:04:39 +01001695 if (chip_ready(map, adr)) {
1696 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
Todd Poynor02b15e32005-06-07 00:04:39 +01001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001700 if (time_after(jiffies, timeo)) {
Todd Poynor02b15e32005-06-07 00:04:39 +01001701 xip_enable(map, chip, adr);
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001702 printk(KERN_WARNING "MTD %s(): software timeout\n",
1703 __func__ );
1704 break;
1705 }
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /* Latency issues. Drop the lock, wait a while and retry */
Todd Poynor02b15e32005-06-07 00:04:39 +01001708 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001710 /* Did we succeed? */
Thomas Gleixner22fd9a82005-05-24 15:33:49 +02001711 if (!chip_good(map, adr, map_word_ff(map))) {
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001712 /* reset on all failures. */
1713 map_write( map, CMD(0xF0), chip->start );
1714 /* FIXME - should have reset delay before continuing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Eric W. Biedermannfb4a90b2005-05-20 04:28:26 +01001716 ret = -EIO;
1717 }
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 chip->state = FL_READY;
1720 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001721 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return ret;
1723}
1724
1725
Ben Dooksce0f33a2007-05-28 19:59:00 +01001726static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 unsigned long ofs, len;
1729 int ret;
1730
1731 ofs = instr->addr;
1732 len = instr->len;
1733
1734 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1735 if (ret)
1736 return ret;
1737
1738 instr->state = MTD_ERASE_DONE;
1739 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return 0;
1742}
1743
1744
1745static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr)
1746{
1747 struct map_info *map = mtd->priv;
1748 struct cfi_private *cfi = map->fldrv_priv;
1749 int ret = 0;
1750
1751 if (instr->addr != 0)
1752 return -EINVAL;
1753
1754 if (instr->len != mtd->size)
1755 return -EINVAL;
1756
1757 ret = do_erase_chip(map, &cfi->chips[0]);
1758 if (ret)
1759 return ret;
1760
1761 instr->state = MTD_ERASE_DONE;
1762 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 0;
1765}
1766
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001767static int do_atmel_lock(struct map_info *map, struct flchip *chip,
1768 unsigned long adr, int len, void *thunk)
1769{
1770 struct cfi_private *cfi = map->fldrv_priv;
1771 int ret;
1772
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001773 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001774 ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
1775 if (ret)
1776 goto out_unlock;
1777 chip->state = FL_LOCKING;
1778
1779 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1780 __func__, adr, len);
1781
1782 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1783 cfi->device_type, NULL);
1784 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1785 cfi->device_type, NULL);
1786 cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi,
1787 cfi->device_type, NULL);
1788 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1789 cfi->device_type, NULL);
1790 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi,
1791 cfi->device_type, NULL);
1792 map_write(map, CMD(0x40), chip->start + adr);
1793
1794 chip->state = FL_READY;
1795 put_chip(map, chip, adr + chip->start);
1796 ret = 0;
1797
1798out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001799 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001800 return ret;
1801}
1802
1803static int do_atmel_unlock(struct map_info *map, struct flchip *chip,
1804 unsigned long adr, int len, void *thunk)
1805{
1806 struct cfi_private *cfi = map->fldrv_priv;
1807 int ret;
1808
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001809 mutex_lock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001810 ret = get_chip(map, chip, adr + chip->start, FL_UNLOCKING);
1811 if (ret)
1812 goto out_unlock;
1813 chip->state = FL_UNLOCKING;
1814
1815 DEBUG(MTD_DEBUG_LEVEL3, "MTD %s(): LOCK 0x%08lx len %d\n",
1816 __func__, adr, len);
1817
1818 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi,
1819 cfi->device_type, NULL);
1820 map_write(map, CMD(0x70), adr);
1821
1822 chip->state = FL_READY;
1823 put_chip(map, chip, adr + chip->start);
1824 ret = 0;
1825
1826out_unlock:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001827 mutex_unlock(&chip->mutex);
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001828 return ret;
1829}
1830
Adrian Hunter69423d92008-12-10 13:37:21 +00001831static int cfi_atmel_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001832{
1833 return cfi_varsize_frob(mtd, do_atmel_lock, ofs, len, NULL);
1834}
1835
Adrian Hunter69423d92008-12-10 13:37:21 +00001836static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Haavard Skinnemoen01655082006-08-09 11:06:07 +02001837{
1838 return cfi_varsize_frob(mtd, do_atmel_unlock, ofs, len, NULL);
1839}
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842static void cfi_amdstd_sync (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 DECLARE_WAITQUEUE(wait, current);
1850
1851 for (i=0; !ret && i<cfi->numchips; i++) {
1852 chip = &cfi->chips[i];
1853
1854 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001855 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 switch(chip->state) {
1858 case FL_READY:
1859 case FL_STATUS:
1860 case FL_CFI_QUERY:
1861 case FL_JEDEC_QUERY:
1862 chip->oldstate = chip->state;
1863 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001864 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * as the whole point is that nobody can do anything
1866 * with the chip now anyway.
1867 */
1868 case FL_SYNCING:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001869 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 break;
1871
1872 default:
1873 /* Not an idle state */
Dmitry Adamushkof8e30e42008-04-08 17:41:59 -07001874 set_current_state(TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 add_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001876
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001877 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 schedule();
1880
1881 remove_wait_queue(&chip->wq, &wait);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto retry;
1884 }
1885 }
1886
1887 /* Unlock the chips again */
1888
1889 for (i--; i >=0; i--) {
1890 chip = &cfi->chips[i];
1891
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001892 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (chip->state == FL_SYNCING) {
1895 chip->state = chip->oldstate;
1896 wake_up(&chip->wq);
1897 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001898 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900}
1901
1902
1903static int cfi_amdstd_suspend(struct mtd_info *mtd)
1904{
1905 struct map_info *map = mtd->priv;
1906 struct cfi_private *cfi = map->fldrv_priv;
1907 int i;
1908 struct flchip *chip;
1909 int ret = 0;
1910
1911 for (i=0; !ret && i<cfi->numchips; i++) {
1912 chip = &cfi->chips[i];
1913
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001914 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 switch(chip->state) {
1917 case FL_READY:
1918 case FL_STATUS:
1919 case FL_CFI_QUERY:
1920 case FL_JEDEC_QUERY:
1921 chip->oldstate = chip->state;
1922 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001923 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * as the whole point is that nobody can do anything
1925 * with the chip now anyway.
1926 */
1927 case FL_PM_SUSPENDED:
1928 break;
1929
1930 default:
1931 ret = -EAGAIN;
1932 break;
1933 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001934 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
1937 /* Unlock the chips again */
1938
1939 if (ret) {
1940 for (i--; i >=0; i--) {
1941 chip = &cfi->chips[i];
1942
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001943 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (chip->state == FL_PM_SUSPENDED) {
1946 chip->state = chip->oldstate;
1947 wake_up(&chip->wq);
1948 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001949 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return ret;
1954}
1955
1956
1957static void cfi_amdstd_resume(struct mtd_info *mtd)
1958{
1959 struct map_info *map = mtd->priv;
1960 struct cfi_private *cfi = map->fldrv_priv;
1961 int i;
1962 struct flchip *chip;
1963
1964 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 chip = &cfi->chips[i];
1967
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001968 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (chip->state == FL_PM_SUSPENDED) {
1971 chip->state = FL_READY;
1972 map_write(map, CMD(0xF0), chip->start);
1973 wake_up(&chip->wq);
1974 }
1975 else
1976 printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");
1977
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001978 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980}
1981
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07001982
1983/*
1984 * Ensure that the flash device is put back into read array mode before
1985 * unloading the driver or rebooting. On some systems, rebooting while
1986 * the flash is in query/program/erase mode will prevent the CPU from
1987 * fetching the bootloader code, requiring a hard reset or power cycle.
1988 */
1989static int cfi_amdstd_reset(struct mtd_info *mtd)
1990{
1991 struct map_info *map = mtd->priv;
1992 struct cfi_private *cfi = map->fldrv_priv;
1993 int i, ret;
1994 struct flchip *chip;
1995
1996 for (i = 0; i < cfi->numchips; i++) {
1997
1998 chip = &cfi->chips[i];
1999
2000 mutex_lock(&chip->mutex);
2001
2002 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2003 if (!ret) {
2004 map_write(map, CMD(0xF0), chip->start);
2005 chip->state = FL_SHUTDOWN;
2006 put_chip(map, chip, chip->start);
2007 }
2008
2009 mutex_unlock(&chip->mutex);
2010 }
2011
2012 return 0;
2013}
2014
2015
2016static int cfi_amdstd_reboot(struct notifier_block *nb, unsigned long val,
2017 void *v)
2018{
2019 struct mtd_info *mtd;
2020
2021 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2022 cfi_amdstd_reset(mtd);
2023 return NOTIFY_DONE;
2024}
2025
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027static void cfi_amdstd_destroy(struct mtd_info *mtd)
2028{
2029 struct map_info *map = mtd->priv;
2030 struct cfi_private *cfi = map->fldrv_priv;
Jesper Juhlfa671642005-11-07 01:01:27 -08002031
Kevin Cernekeeeafe1312010-04-29 10:26:56 -07002032 cfi_amdstd_reset(mtd);
2033 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 kfree(cfi->cmdset_priv);
2035 kfree(cfi->cfiq);
2036 kfree(cfi);
2037 kfree(mtd->eraseregions);
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040MODULE_LICENSE("GPL");
2041MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");
2042MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");