blob: 350671ec52263982a2f7532551c48a3272a33711 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common Flash Interface support:
3 * Intel Extended Vendor Command Set (ID 0x0001)
4 *
5 * (C) 2000 Red Hat. GPL'd
6 *
Nicolas Pitre8bc3b382005-11-23 22:07:56 +00007 * $Id: cfi_cmdset_0001.c,v 1.186 2005/11/23 22:07:52 nico Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Thomas Gleixner1f948b42005-11-07 11:15:37 +00009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 10/10/2000 Nicolas Pitre <nico@cam.org>
11 * - completely revamped method functions so they are aware and
12 * independent of the flash geometry (buswidth, interleave, etc.)
13 * - scalability vs code size is completely set at compile-time
14 * (see include/linux/mtd/cfi.h for selection)
15 * - optimized write buffer method
16 * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
17 * - reworked lock/unlock/erase support for var size flash
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -080018 * 21/03/2007 Rodolfo Giometti <giometti@linux.it>
19 * - auto unlock sectors on resume for auto locking flash on power up
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/kernel.h>
25#include <linux/sched.h>
26#include <linux/init.h>
27#include <asm/io.h>
28#include <asm/byteorder.h>
29
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/delay.h>
33#include <linux/interrupt.h>
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010034#include <linux/reboot.h>
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -080035#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mtd/xip.h>
37#include <linux/mtd/map.h>
38#include <linux/mtd/mtd.h>
39#include <linux/mtd/compatmac.h>
40#include <linux/mtd/cfi.h>
41
42/* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
43/* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
44
45// debugging, turns off buffer write mode if set to 1
46#define FORCE_WORD_WRITE 0
47
48#define MANUFACTURER_INTEL 0x0089
49#define I82802AB 0x00ad
50#define I82802AC 0x00ac
51#define MANUFACTURER_ST 0x0020
52#define M50LPW080 0x002F
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +010053#define AT49BV640D 0x02de
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
57static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
Nicolas Pitree102d542005-08-06 05:46:59 +010058static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
60static void cfi_intelext_sync (struct mtd_info *);
61static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
62static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
Todd Poynor8048d2f2005-03-31 00:57:33 +010063#ifdef CONFIG_MTD_OTP
Nicolas Pitref77814d2005-02-08 17:11:19 +000064static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
65static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
66static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
67static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
68static int cfi_intelext_get_fact_prot_info (struct mtd_info *,
69 struct otp_info *, size_t);
70static int cfi_intelext_get_user_prot_info (struct mtd_info *,
71 struct otp_info *, size_t);
Todd Poynor8048d2f2005-03-31 00:57:33 +010072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int cfi_intelext_suspend (struct mtd_info *);
74static void cfi_intelext_resume (struct mtd_info *);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010075static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static void cfi_intelext_destroy(struct mtd_info *);
78
79struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
80
81static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
82static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
83
84static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
85 size_t *retlen, u_char **mtdbuf);
86static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from,
87 size_t len);
88
Alexey Korolev5a37cf12007-10-22 17:55:20 +010089static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
91static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
92#include "fwh_lock.h"
93
94
95
96/*
97 * *********** SETUP AND PROBE BITS ***********
98 */
99
100static struct mtd_chip_driver cfi_intelext_chipdrv = {
101 .probe = NULL, /* Not usable directly */
102 .destroy = cfi_intelext_destroy,
103 .name = "cfi_cmdset_0001",
104 .module = THIS_MODULE
105};
106
107/* #define DEBUG_LOCK_BITS */
108/* #define DEBUG_CFI_FEATURES */
109
110#ifdef DEBUG_CFI_FEATURES
111static void cfi_tell_features(struct cfi_pri_intelext *extp)
112{
113 int i;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100114 printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
116 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
117 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
118 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
119 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
120 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
121 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
122 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
123 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
124 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
125 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100126 printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
127 for (i=11; i<32; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000128 if (extp->FeatureSupport & (1<<i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 printk(" - Unknown Bit %X: supported\n", i);
130 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
133 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
134 for (i=1; i<8; i++) {
135 if (extp->SuspendCmdSupport & (1<<i))
136 printk(" - Unknown Bit %X: supported\n", i);
137 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
140 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100141 printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
142 for (i=2; i<3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (extp->BlkStatusRegMask & (1<<i))
144 printk(" - Unknown Bit %X Active: yes\n",i);
145 }
Nicolas Pitre638d9832005-08-06 05:40:46 +0100146 printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
147 printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
148 for (i=6; i<16; i++) {
149 if (extp->BlkStatusRegMask & (1<<i))
150 printk(" - Unknown Bit %X Active: yes\n",i);
151 }
152
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000153 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
155 if (extp->VppOptimal)
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000156 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
158}
159#endif
160
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100161/* Atmel chips don't use the same PRI format as Intel chips */
162static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
163{
164 struct map_info *map = mtd->priv;
165 struct cfi_private *cfi = map->fldrv_priv;
166 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
167 struct cfi_pri_atmel atmel_pri;
168 uint32_t features = 0;
169
170 /* Reverse byteswapping */
171 extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
172 extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
173 extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
174
175 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
176 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
177
178 printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
179
180 if (atmel_pri.Features & 0x01) /* chip erase supported */
181 features |= (1<<0);
182 if (atmel_pri.Features & 0x02) /* erase suspend supported */
183 features |= (1<<1);
184 if (atmel_pri.Features & 0x04) /* program suspend supported */
185 features |= (1<<2);
186 if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
187 features |= (1<<9);
188 if (atmel_pri.Features & 0x20) /* page mode read supported */
189 features |= (1<<7);
190 if (atmel_pri.Features & 0x40) /* queued erase supported */
191 features |= (1<<4);
192 if (atmel_pri.Features & 0x80) /* Protection bits supported */
193 features |= (1<<6);
194
195 extp->FeatureSupport = features;
196
197 /* burst write mode not supported */
198 cfi->cfiq->BufWriteTimeoutTyp = 0;
199 cfi->cfiq->BufWriteTimeoutMax = 0;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000203/* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static void fixup_intel_strataflash(struct mtd_info *mtd, void* param)
205{
206 struct map_info *map = mtd->priv;
207 struct cfi_private *cfi = map->fldrv_priv;
208 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
209
210 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
211 "erase on write disabled.\n");
212 extp->SuspendCmdSupport &= ~1;
213}
214#endif
215
216#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
217static void fixup_no_write_suspend(struct mtd_info *mtd, void* param)
218{
219 struct map_info *map = mtd->priv;
220 struct cfi_private *cfi = map->fldrv_priv;
221 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
222
223 if (cfip && (cfip->FeatureSupport&4)) {
224 cfip->FeatureSupport &= ~4;
225 printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
226 }
227}
228#endif
229
230static void fixup_st_m28w320ct(struct mtd_info *mtd, void* param)
231{
232 struct map_info *map = mtd->priv;
233 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
236 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
237}
238
239static void fixup_st_m28w320cb(struct mtd_info *mtd, void* param)
240{
241 struct map_info *map = mtd->priv;
242 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Note this is done after the region info is endian swapped */
245 cfi->cfiq->EraseRegionInfo[1] =
246 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
247};
248
249static void fixup_use_point(struct mtd_info *mtd, void *param)
250{
251 struct map_info *map = mtd->priv;
252 if (!mtd->point && map_is_linear(map)) {
253 mtd->point = cfi_intelext_point;
254 mtd->unpoint = cfi_intelext_unpoint;
255 }
256}
257
258static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
259{
260 struct map_info *map = mtd->priv;
261 struct cfi_private *cfi = map->fldrv_priv;
262 if (cfi->cfiq->BufWriteTimeoutTyp) {
263 printk(KERN_INFO "Using buffer write method\n" );
264 mtd->write = cfi_intelext_write_buffers;
Nicolas Pitree102d542005-08-06 05:46:59 +0100265 mtd->writev = cfi_intelext_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267}
268
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800269/*
270 * Some chips power-up with all sectors locked by default.
271 */
272static void fixup_use_powerup_lock(struct mtd_info *mtd, void *param)
273{
274 printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
275 mtd->flags |= MTD_STUPID_LOCK;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100279 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000281 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#endif
283#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
284 { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend, NULL },
285#endif
286#if !FORCE_WORD_WRITE
287 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL },
288#endif
289 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
290 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800291 { MANUFACTURER_INTEL, 0x891c, fixup_use_powerup_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 { 0, 0, NULL, NULL }
293};
294
295static struct cfi_fixup jedec_fixup_table[] = {
296 { MANUFACTURER_INTEL, I82802AB, fixup_use_fwh_lock, NULL, },
297 { MANUFACTURER_INTEL, I82802AC, fixup_use_fwh_lock, NULL, },
298 { MANUFACTURER_ST, M50LPW080, fixup_use_fwh_lock, NULL, },
299 { 0, 0, NULL, NULL }
300};
301static struct cfi_fixup fixup_table[] = {
302 /* The CFI vendor ids and the JEDEC vendor IDs appear
303 * to be common. It is like the devices id's are as
304 * well. This table is to pick all cases where
305 * we know that is the case.
306 */
307 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point, NULL },
308 { 0, 0, NULL, NULL }
309};
310
311static inline struct cfi_pri_intelext *
312read_pri_intelext(struct map_info *map, __u16 adr)
313{
314 struct cfi_pri_intelext *extp;
315 unsigned int extp_size = sizeof(*extp);
316
317 again:
318 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
319 if (!extp)
320 return NULL;
321
Todd Poynord88f9772005-07-20 22:01:17 +0100322 if (extp->MajorVersion != '1' ||
Alexey Korolevb1c9c9b2007-11-23 09:31:56 +0000323 (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
Todd Poynord88f9772005-07-20 22:01:17 +0100324 printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
325 "version %c.%c.\n", extp->MajorVersion,
326 extp->MinorVersion);
327 kfree(extp);
328 return NULL;
329 }
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /* Do some byteswapping if necessary */
332 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
333 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
334 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
335
Nicolas Pitre638d9832005-08-06 05:40:46 +0100336 if (extp->MajorVersion == '1' && extp->MinorVersion >= '3') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned int extra_size = 0;
338 int nb_parts, i;
339
340 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000341 extra_size += (extp->NumProtectionFields - 1) *
342 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100345 extra_size += 2;
346 if (extp_size < sizeof(*extp) + extra_size)
347 goto need_more;
348 extra_size += extp->extra[extra_size-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* Number of hardware-partitions */
351 extra_size += 1;
352 if (extp_size < sizeof(*extp) + extra_size)
353 goto need_more;
354 nb_parts = extp->extra[extra_size - 1];
355
Nicolas Pitre638d9832005-08-06 05:40:46 +0100356 /* skip the sizeof(partregion) field in CFI 1.4 */
357 if (extp->MinorVersion >= '4')
358 extra_size += 2;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 for (i = 0; i < nb_parts; i++) {
361 struct cfi_intelext_regioninfo *rinfo;
362 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
363 extra_size += sizeof(*rinfo);
364 if (extp_size < sizeof(*extp) + extra_size)
365 goto need_more;
366 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
367 extra_size += (rinfo->NumBlockTypes - 1)
368 * sizeof(struct cfi_intelext_blockinfo);
369 }
370
Nicolas Pitre638d9832005-08-06 05:40:46 +0100371 if (extp->MinorVersion >= '4')
372 extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (extp_size < sizeof(*extp) + extra_size) {
375 need_more:
376 extp_size = sizeof(*extp) + extra_size;
377 kfree(extp);
378 if (extp_size > 4096) {
379 printk(KERN_ERR
380 "%s: cfi_pri_intelext is too fat\n",
381 __FUNCTION__);
382 return NULL;
383 }
384 goto again;
385 }
386 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return extp;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
392{
393 struct cfi_private *cfi = map->fldrv_priv;
394 struct mtd_info *mtd;
395 int i;
396
Burman Yan95b93a02006-11-15 21:10:29 +0200397 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!mtd) {
399 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
400 return NULL;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 mtd->priv = map;
403 mtd->type = MTD_NORFLASH;
404
405 /* Fill in the default mtd operations */
406 mtd->erase = cfi_intelext_erase_varsize;
407 mtd->read = cfi_intelext_read;
408 mtd->write = cfi_intelext_write_words;
409 mtd->sync = cfi_intelext_sync;
410 mtd->lock = cfi_intelext_lock;
411 mtd->unlock = cfi_intelext_unlock;
412 mtd->suspend = cfi_intelext_suspend;
413 mtd->resume = cfi_intelext_resume;
414 mtd->flags = MTD_CAP_NORFLASH;
415 mtd->name = map->name;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400416 mtd->writesize = 1;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100417
418 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (cfi->cfi_mode == CFI_MODE_CFI) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000421 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * It's a real CFI chip, not one for which the probe
423 * routine faked a CFI structure. So we read the feature
424 * table from it.
425 */
426 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
427 struct cfi_pri_intelext *extp;
428
429 extp = read_pri_intelext(map, adr);
430 if (!extp) {
431 kfree(mtd);
432 return NULL;
433 }
434
435 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000436 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 cfi_fixup(mtd, cfi_fixup_table);
439
440#ifdef DEBUG_CFI_FEATURES
441 /* Tell the user about it in lots of lovely detail */
442 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if(extp->SuspendCmdSupport & 1) {
446 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
447 }
448 }
449 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
450 /* Apply jedec specific fixups */
451 cfi_fixup(mtd, jedec_fixup_table);
452 }
453 /* Apply generic fixups */
454 cfi_fixup(mtd, fixup_table);
455
456 for (i=0; i< cfi->numchips; i++) {
David Woodhouse2a5bd592007-02-09 14:39:10 +0000457 if (cfi->cfiq->WordWriteTimeoutTyp)
458 cfi->chips[i].word_write_time =
459 1<<cfi->cfiq->WordWriteTimeoutTyp;
460 else
461 cfi->chips[i].word_write_time = 50000;
462
463 if (cfi->cfiq->BufWriteTimeoutTyp)
464 cfi->chips[i].buffer_write_time =
465 1<<cfi->cfiq->BufWriteTimeoutTyp;
466 /* No default; if it isn't specified, we won't use it */
467
468 if (cfi->cfiq->BlockEraseTimeoutTyp)
469 cfi->chips[i].erase_time =
470 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
471 else
472 cfi->chips[i].erase_time = 2000000;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 cfi->chips[i].ref_point_counter = 0;
Simon Voglc314b6f2006-02-24 13:04:09 -0800475 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 map->fldrv = &cfi_intelext_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return cfi_intelext_setup(mtd);
481}
David Woodhousea15bdee2006-05-08 22:35:05 +0100482struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
483struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
484EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
485EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
486EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
489{
490 struct map_info *map = mtd->priv;
491 struct cfi_private *cfi = map->fldrv_priv;
492 unsigned long offset = 0;
493 int i,j;
494 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
495
496 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
497
498 mtd->size = devsize * cfi->numchips;
499
500 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000501 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000503 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
505 goto setup_err;
506 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
509 unsigned long ernum, ersize;
510 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
511 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
512
513 if (mtd->erasesize < ersize) {
514 mtd->erasesize = ersize;
515 }
516 for (j=0; j<cfi->numchips; j++) {
517 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
518 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
519 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800520 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 offset += (ersize * ernum);
523 }
524
525 if (offset != devsize) {
526 /* Argh */
527 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
528 goto setup_err;
529 }
530
531 for (i=0; i<mtd->numeraseregions;i++){
Nicolas Pitre48436532005-08-06 05:16:52 +0100532 printk(KERN_DEBUG "erase region %d: offset=0x%x,size=0x%x,blocks=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 i,mtd->eraseregions[i].offset,
534 mtd->eraseregions[i].erasesize,
535 mtd->eraseregions[i].numblocks);
536 }
537
Nicolas Pitref77814d2005-02-08 17:11:19 +0000538#ifdef CONFIG_MTD_OTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000540 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
541 mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
542 mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
543 mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
544 mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545#endif
546
547 /* This function has the potential to distort the reality
548 a bit and therefore should be called last. */
549 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
550 goto setup_err;
551
552 __module_get(THIS_MODULE);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100553 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return mtd;
555
556 setup_err:
557 if(mtd) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800558 kfree(mtd->eraseregions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 kfree(mtd);
560 }
561 kfree(cfi->cmdset_priv);
562 return NULL;
563}
564
565static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
566 struct cfi_private **pcfi)
567{
568 struct map_info *map = mtd->priv;
569 struct cfi_private *cfi = *pcfi;
570 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
571
572 /*
Jesper Juhl8f1a8662007-07-05 02:18:34 +0200573 * Probing of multi-partition flash chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *
575 * To support multiple partitions when available, we simply arrange
576 * for each of them to have their own flchip structure even if they
577 * are on the same physical chip. This means completely recreating
578 * a new cfi_private structure right here which is a blatent code
579 * layering violation, but this is still the least intrusive
580 * arrangement at this point. This can be rearranged in the future
581 * if someone feels motivated enough. --nico
582 */
Nicolas Pitre638d9832005-08-06 05:40:46 +0100583 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 && extp->FeatureSupport & (1 << 9)) {
585 struct cfi_private *newcfi;
586 struct flchip *chip;
587 struct flchip_shared *shared;
588 int offs, numregions, numparts, partshift, numvirtchips, i, j;
589
590 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000591 offs = (extp->NumProtectionFields - 1) *
592 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100595 offs += extp->extra[offs+1]+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Number of partition regions */
598 numregions = extp->extra[offs];
599 offs += 1;
600
Nicolas Pitre638d9832005-08-06 05:40:46 +0100601 /* skip the sizeof(partregion) field in CFI 1.4 */
602 if (extp->MinorVersion >= '4')
603 offs += 2;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Number of hardware partitions */
606 numparts = 0;
607 for (i = 0; i < numregions; i++) {
608 struct cfi_intelext_regioninfo *rinfo;
609 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
610 numparts += rinfo->NumIdentPartitions;
611 offs += sizeof(*rinfo)
612 + (rinfo->NumBlockTypes - 1) *
613 sizeof(struct cfi_intelext_blockinfo);
614 }
615
Nicolas Pitre638d9832005-08-06 05:40:46 +0100616 /* Programming Region info */
617 if (extp->MinorVersion >= '4') {
618 struct cfi_intelext_programming_regioninfo *prinfo;
619 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
Joern Engel28318772006-05-22 23:18:05 +0200620 mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
Joern Engel5fa43392006-05-22 23:18:29 +0200621 mtd->flags &= ~MTD_BIT_WRITEABLE;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100622 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
Joern Engel28318772006-05-22 23:18:05 +0200623 map->name, mtd->writesize,
Artem Bityutskiyd4160852007-01-30 10:45:55 +0200624 cfi->interleave * prinfo->ControlValid,
625 cfi->interleave * prinfo->ControlInvalid);
Nicolas Pitre638d9832005-08-06 05:40:46 +0100626 }
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * All functions below currently rely on all chips having
630 * the same geometry so we'll just assume that all hardware
631 * partitions are of the same size too.
632 */
633 partshift = cfi->chipshift - __ffs(numparts);
634
635 if ((1 << partshift) < mtd->erasesize) {
636 printk( KERN_ERR
637 "%s: bad number of hw partitions (%d)\n",
638 __FUNCTION__, numparts);
639 return -EINVAL;
640 }
641
642 numvirtchips = cfi->numchips * numparts;
643 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
644 if (!newcfi)
645 return -ENOMEM;
646 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
647 if (!shared) {
648 kfree(newcfi);
649 return -ENOMEM;
650 }
651 memcpy(newcfi, cfi, sizeof(struct cfi_private));
652 newcfi->numchips = numvirtchips;
653 newcfi->chipshift = partshift;
654
655 chip = &newcfi->chips[0];
656 for (i = 0; i < cfi->numchips; i++) {
657 shared[i].writing = shared[i].erasing = NULL;
658 spin_lock_init(&shared[i].lock);
659 for (j = 0; j < numparts; j++) {
660 *chip = cfi->chips[i];
661 chip->start += j << partshift;
662 chip->priv = &shared[i];
663 /* those should be reset too since
664 they create memory references. */
665 init_waitqueue_head(&chip->wq);
666 spin_lock_init(&chip->_spinlock);
667 chip->mutex = &chip->_spinlock;
668 chip++;
669 }
670 }
671
672 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
673 "--> %d partitions of %d KiB\n",
674 map->name, cfi->numchips, cfi->interleave,
675 newcfi->numchips, 1<<(newcfi->chipshift-10));
676
677 map->fldrv_priv = newcfi;
678 *pcfi = newcfi;
679 kfree(cfi);
680 }
681
682 return 0;
683}
684
685/*
686 * *********** CHIP ACCESS FUNCTIONS ***********
687 */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100688static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 DECLARE_WAITQUEUE(wait, current);
691 struct cfi_private *cfi = map->fldrv_priv;
692 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100694 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 switch (chip->state) {
697
698 case FL_STATUS:
699 for (;;) {
700 status = map_read(map, adr);
701 if (map_word_andequal(map, status, status_OK, status_OK))
702 break;
703
704 /* At this point we're fine with write operations
705 in other partitions as they don't conflict. */
706 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
707 break;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_unlock(chip->mutex);
710 cfi_udelay(1);
711 spin_lock(chip->mutex);
712 /* Someone else might have been playing with it. */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100713 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case FL_READY:
717 case FL_CFI_QUERY:
718 case FL_JEDEC_QUERY:
719 return 0;
720
721 case FL_ERASING:
722 if (!cfip ||
723 !(cfip->FeatureSupport & 2) ||
724 !(mode == FL_READY || mode == FL_POINT ||
725 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
726 goto sleep;
727
728
729 /* Erase suspend */
730 map_write(map, CMD(0xB0), adr);
731
732 /* If the flash has finished erasing, then 'erase suspend'
733 * appears to make some (28F320) flash devices switch to
734 * 'read' mode. Make sure that we switch to 'read status'
735 * mode so we get the right data. --rmk
736 */
737 map_write(map, CMD(0x70), adr);
738 chip->oldstate = FL_ERASING;
739 chip->state = FL_ERASE_SUSPENDING;
740 chip->erase_suspended = 1;
741 for (;;) {
742 status = map_read(map, adr);
743 if (map_word_andequal(map, status, status_OK, status_OK))
744 break;
745
746 if (time_after(jiffies, timeo)) {
747 /* Urgh. Resume and pretend we weren't here. */
748 map_write(map, CMD(0xd0), adr);
749 /* Make sure we're in 'read status' mode if it had finished */
750 map_write(map, CMD(0x70), adr);
751 chip->state = FL_ERASING;
752 chip->oldstate = FL_READY;
Nicolas Pitre48436532005-08-06 05:16:52 +0100753 printk(KERN_ERR "%s: Chip not ready after erase "
754 "suspended: status = 0x%lx\n", map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EIO;
756 }
757
758 spin_unlock(chip->mutex);
759 cfi_udelay(1);
760 spin_lock(chip->mutex);
761 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
762 So we can just loop here. */
763 }
764 chip->state = FL_STATUS;
765 return 0;
766
767 case FL_XIP_WHILE_ERASING:
768 if (mode != FL_READY && mode != FL_POINT &&
769 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
770 goto sleep;
771 chip->oldstate = chip->state;
772 chip->state = FL_READY;
773 return 0;
774
775 case FL_POINT:
776 /* Only if there's no operation suspended... */
777 if (mode == FL_READY && chip->oldstate == FL_READY)
778 return 0;
779
Kevin Haoc4a9f882007-10-02 13:56:04 -0700780 case FL_SHUTDOWN:
781 /* The machine is rebooting now,so no one can get chip anymore */
782 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 default:
784 sleep:
785 set_current_state(TASK_UNINTERRUPTIBLE);
786 add_wait_queue(&chip->wq, &wait);
787 spin_unlock(chip->mutex);
788 schedule();
789 remove_wait_queue(&chip->wq, &wait);
790 spin_lock(chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100791 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793}
794
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100795static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
796{
797 int ret;
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300798 DECLARE_WAITQUEUE(wait, current);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100799
800 retry:
801 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING
802 || mode == FL_OTP_WRITE || mode == FL_SHUTDOWN)) {
803 /*
804 * OK. We have possibility for contention on the write/erase
805 * operations which are global to the real chip and not per
806 * partition. So let's fight it over in the partition which
807 * currently has authority on the operation.
808 *
809 * The rules are as follows:
810 *
811 * - any write operation must own shared->writing.
812 *
813 * - any erase operation must own _both_ shared->writing and
814 * shared->erasing.
815 *
816 * - contention arbitration is handled in the owner's context.
817 *
818 * The 'shared' struct can be read and/or written only when
819 * its lock is taken.
820 */
821 struct flchip_shared *shared = chip->priv;
822 struct flchip *contender;
823 spin_lock(&shared->lock);
824 contender = shared->writing;
825 if (contender && contender != chip) {
826 /*
827 * The engine to perform desired operation on this
828 * partition is already in use by someone else.
829 * Let's fight over it in the context of the chip
830 * currently using it. If it is possible to suspend,
831 * that other partition will do just that, otherwise
832 * it'll happily send us to sleep. In any case, when
833 * get_chip returns success we're clear to go ahead.
834 */
835 ret = spin_trylock(contender->mutex);
836 spin_unlock(&shared->lock);
837 if (!ret)
838 goto retry;
839 spin_unlock(chip->mutex);
840 ret = chip_ready(map, contender, contender->start, mode);
841 spin_lock(chip->mutex);
842
843 if (ret == -EAGAIN) {
844 spin_unlock(contender->mutex);
845 goto retry;
846 }
847 if (ret) {
848 spin_unlock(contender->mutex);
849 return ret;
850 }
851 spin_lock(&shared->lock);
852 spin_unlock(contender->mutex);
853 }
854
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300855 /* Check if we already have suspended erase
856 * on this chip. Sleep. */
857 if (mode == FL_ERASING && shared->erasing
858 && shared->erasing->oldstate == FL_ERASING) {
859 spin_unlock(&shared->lock);
860 set_current_state(TASK_UNINTERRUPTIBLE);
861 add_wait_queue(&chip->wq, &wait);
862 spin_unlock(chip->mutex);
863 schedule();
864 remove_wait_queue(&chip->wq, &wait);
865 spin_lock(chip->mutex);
866 goto retry;
867 }
868
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100869 /* We now own it */
870 shared->writing = chip;
871 if (mode == FL_ERASING)
872 shared->erasing = chip;
873 spin_unlock(&shared->lock);
874 }
875 ret = chip_ready(map, chip, adr, mode);
876 if (ret == -EAGAIN)
877 goto retry;
878
879 return ret;
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
883{
884 struct cfi_private *cfi = map->fldrv_priv;
885
886 if (chip->priv) {
887 struct flchip_shared *shared = chip->priv;
888 spin_lock(&shared->lock);
889 if (shared->writing == chip && chip->oldstate == FL_READY) {
890 /* We own the ability to write, but we're done */
891 shared->writing = shared->erasing;
892 if (shared->writing && shared->writing != chip) {
893 /* give back ownership to who we loaned it from */
894 struct flchip *loaner = shared->writing;
895 spin_lock(loaner->mutex);
896 spin_unlock(&shared->lock);
897 spin_unlock(chip->mutex);
898 put_chip(map, loaner, loaner->start);
899 spin_lock(chip->mutex);
900 spin_unlock(loaner->mutex);
901 wake_up(&chip->wq);
902 return;
903 }
904 shared->erasing = NULL;
905 shared->writing = NULL;
906 } else if (shared->erasing == chip && shared->writing != chip) {
907 /*
908 * We own the ability to erase without the ability
909 * to write, which means the erase was suspended
910 * and some other partition is currently writing.
911 * Don't let the switch below mess things up since
912 * we don't have ownership to resume anything.
913 */
914 spin_unlock(&shared->lock);
915 wake_up(&chip->wq);
916 return;
917 }
918 spin_unlock(&shared->lock);
919 }
920
921 switch(chip->oldstate) {
922 case FL_ERASING:
923 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000924 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000926 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 errors to be returned from do_erase_oneblock because
928 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000929 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000931 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 do. */
933 map_write(map, CMD(0xd0), adr);
934 map_write(map, CMD(0x70), adr);
935 chip->oldstate = FL_READY;
936 chip->state = FL_ERASING;
937 break;
938
939 case FL_XIP_WHILE_ERASING:
940 chip->state = chip->oldstate;
941 chip->oldstate = FL_READY;
942 break;
943
944 case FL_READY:
945 case FL_STATUS:
946 case FL_JEDEC_QUERY:
947 /* We should really make set_vpp() count, rather than doing this */
948 DISABLE_VPP(map);
949 break;
950 default:
Nicolas Pitre48436532005-08-06 05:16:52 +0100951 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 wake_up(&chip->wq);
954}
955
956#ifdef CONFIG_MTD_XIP
957
958/*
959 * No interrupt what so ever can be serviced while the flash isn't in array
960 * mode. This is ensured by the xip_disable() and xip_enable() functions
961 * enclosing any code path where the flash is known not to be in array mode.
962 * And within a XIP disabled code path, only functions marked with __xipram
963 * may be called and nothing else (it's a good thing to inspect generated
964 * assembly to make sure inline functions were actually inlined and that gcc
965 * didn't emit calls to its own support functions). Also configuring MTD CFI
966 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968
969static void xip_disable(struct map_info *map, struct flchip *chip,
970 unsigned long adr)
971{
972 /* TODO: chips with no XIP use should ignore and return */
973 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 local_irq_disable();
975}
976
977static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
978 unsigned long adr)
979{
980 struct cfi_private *cfi = map->fldrv_priv;
981 if (chip->state != FL_POINT && chip->state != FL_READY) {
982 map_write(map, CMD(0xff), adr);
983 chip->state = FL_READY;
984 }
985 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200986 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990/*
991 * When a delay is required for the flash operation to complete, the
Nicolas Pitrec1724712006-03-30 15:52:41 +0100992 * xip_wait_for_operation() function is polling for both the given timeout
993 * and pending (but still masked) hardware interrupts. Whenever there is an
994 * interrupt pending then the flash erase or write operation is suspended,
995 * array mode restored and interrupts unmasked. Task scheduling might also
996 * happen at that point. The CPU eventually returns from the interrupt or
997 * the call to schedule() and the suspended flash operation is resumed for
998 * the remaining of the delay period.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 *
1000 * Warning: this function _will_ fool interrupt latency tracing tools.
1001 */
1002
Nicolas Pitrec1724712006-03-30 15:52:41 +01001003static int __xipram xip_wait_for_operation(
1004 struct map_info *map, struct flchip *chip,
Alexey Korolev46a16522006-06-28 19:22:07 +01001005 unsigned long adr, unsigned int chip_op_time )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct cfi_private *cfi = map->fldrv_priv;
1008 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
1009 map_word status, OK = CMD(0x80);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001010 unsigned long usec, suspended, start, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 flstate_t oldstate, newstate;
1012
Nicolas Pitrec1724712006-03-30 15:52:41 +01001013 start = xip_currtime();
Alexey Korolev46a16522006-06-28 19:22:07 +01001014 usec = chip_op_time * 8;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001015 if (usec == 0)
1016 usec = 500000;
1017 done = 0;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 do {
1020 cpu_relax();
1021 if (xip_irqpending() && cfip &&
1022 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1023 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1024 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1025 /*
1026 * Let's suspend the erase or write operation when
1027 * supported. Note that we currently don't try to
1028 * suspend interleaved chips if there is already
1029 * another operation suspended (imagine what happens
1030 * when one chip was already done with the current
1031 * operation while another chip suspended it, then
1032 * we resume the whole thing at once). Yes, it
1033 * can happen!
1034 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001035 usec -= done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 map_write(map, CMD(0xb0), adr);
1037 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 suspended = xip_currtime();
1039 do {
1040 if (xip_elapsed_since(suspended) > 100000) {
1041 /*
1042 * The chip doesn't want to suspend
1043 * after waiting for 100 msecs.
1044 * This is a critical error but there
1045 * is not much we can do here.
1046 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001047 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 status = map_read(map, adr);
1050 } while (!map_word_andequal(map, status, OK, OK));
1051
1052 /* Suspend succeeded */
1053 oldstate = chip->state;
1054 if (oldstate == FL_ERASING) {
1055 if (!map_word_bitsset(map, status, CMD(0x40)))
1056 break;
1057 newstate = FL_XIP_WHILE_ERASING;
1058 chip->erase_suspended = 1;
1059 } else {
1060 if (!map_word_bitsset(map, status, CMD(0x04)))
1061 break;
1062 newstate = FL_XIP_WHILE_WRITING;
1063 chip->write_suspended = 1;
1064 }
1065 chip->state = newstate;
1066 map_write(map, CMD(0xff), adr);
1067 (void) map_read(map, adr);
1068 asm volatile (".rep 8; nop; .endr");
1069 local_irq_enable();
Nicolas Pitre6da70122005-05-19 18:05:47 +01001070 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 asm volatile (".rep 8; nop; .endr");
1072 cond_resched();
1073
1074 /*
1075 * We're back. However someone else might have
1076 * decided to go write to the chip if we are in
1077 * a suspended erase state. If so let's wait
1078 * until it's done.
1079 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001080 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 while (chip->state != newstate) {
1082 DECLARE_WAITQUEUE(wait, current);
1083 set_current_state(TASK_UNINTERRUPTIBLE);
1084 add_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001085 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 schedule();
1087 remove_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001088 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 /* Disallow XIP again */
1091 local_irq_disable();
1092
1093 /* Resume the write or erase operation */
1094 map_write(map, CMD(0xd0), adr);
1095 map_write(map, CMD(0x70), adr);
1096 chip->state = oldstate;
1097 start = xip_currtime();
1098 } else if (usec >= 1000000/HZ) {
1099 /*
1100 * Try to save on CPU power when waiting delay
1101 * is at least a system timer tick period.
1102 * No need to be extremely accurate here.
1103 */
1104 xip_cpu_idle();
1105 }
1106 status = map_read(map, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001107 done = xip_elapsed_since(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 } while (!map_word_andequal(map, status, OK, OK)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001109 && done < usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Nicolas Pitrec1724712006-03-30 15:52:41 +01001111 return (done >= usec) ? -ETIME : 0;
1112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114/*
1115 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1116 * the flash is actively programming or erasing since we have to poll for
1117 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001118 * a XIP setup so do it before the actual flash operation in this case
Nicolas Pitrec1724712006-03-30 15:52:41 +01001119 * and stub it out from INVAL_CACHE_AND_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001121#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1122 INVALIDATE_CACHED_RANGE(map, from, size)
1123
Alexey Korolev46a16522006-06-28 19:22:07 +01001124#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec) \
1125 xip_wait_for_operation(map, chip, cmd_adr, usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127#else
1128
1129#define xip_disable(map, chip, adr)
1130#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131#define XIP_INVAL_CACHED_RANGE(x...)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001132#define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Nicolas Pitrec1724712006-03-30 15:52:41 +01001134static int inval_cache_and_wait_for_operation(
1135 struct map_info *map, struct flchip *chip,
1136 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001137 unsigned int chip_op_time)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001138{
1139 struct cfi_private *cfi = map->fldrv_priv;
1140 map_word status, status_OK = CMD(0x80);
Alexey Korolev46a16522006-06-28 19:22:07 +01001141 int chip_state = chip->state;
1142 unsigned int timeo, sleep_time;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001143
Nicolas Pitrec1724712006-03-30 15:52:41 +01001144 spin_unlock(chip->mutex);
1145 if (inval_len)
1146 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001147 spin_lock(chip->mutex);
1148
Alexey Korolev46a16522006-06-28 19:22:07 +01001149 /* set our timeout to 8 times the expected delay */
1150 timeo = chip_op_time * 8;
1151 if (!timeo)
1152 timeo = 500000;
1153 sleep_time = chip_op_time / 2;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001154
Nicolas Pitrec1724712006-03-30 15:52:41 +01001155 for (;;) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001156 status = map_read(map, cmd_adr);
1157 if (map_word_andequal(map, status, status_OK, status_OK))
1158 break;
1159
Alexey Korolev46a16522006-06-28 19:22:07 +01001160 if (!timeo) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001161 map_write(map, CMD(0x70), cmd_adr);
1162 chip->state = FL_STATUS;
1163 return -ETIME;
1164 }
1165
Alexey Korolev46a16522006-06-28 19:22:07 +01001166 /* OK Still waiting. Drop the lock, wait a while and retry. */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001167 spin_unlock(chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001168 if (sleep_time >= 1000000/HZ) {
1169 /*
1170 * Half of the normal delay still remaining
1171 * can be performed with a sleeping delay instead
1172 * of busy waiting.
1173 */
1174 msleep(sleep_time/1000);
1175 timeo -= sleep_time;
1176 sleep_time = 1000000/HZ;
1177 } else {
1178 udelay(1);
1179 cond_resched();
1180 timeo--;
1181 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001182 spin_lock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001183
Joakim Tjernlund967bf622006-11-28 23:11:52 +00001184 while (chip->state != chip_state) {
Alexey Korolev46a16522006-06-28 19:22:07 +01001185 /* Someone's suspended the operation: sleep */
1186 DECLARE_WAITQUEUE(wait, current);
1187 set_current_state(TASK_UNINTERRUPTIBLE);
1188 add_wait_queue(&chip->wq, &wait);
1189 spin_unlock(chip->mutex);
1190 schedule();
1191 remove_wait_queue(&chip->wq, &wait);
1192 spin_lock(chip->mutex);
1193 }
1194 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001195
1196 /* Done and happy. */
1197 chip->state = FL_STATUS;
1198 return 0;
1199}
Nicolas Pitre6da70122005-05-19 18:05:47 +01001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#endif
1202
Nicolas Pitrec1724712006-03-30 15:52:41 +01001203#define WAIT_TIMEOUT(map, chip, adr, udelay) \
Alexey Korolev46a16522006-06-28 19:22:07 +01001204 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001205
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1208{
1209 unsigned long cmd_addr;
1210 struct cfi_private *cfi = map->fldrv_priv;
1211 int ret = 0;
1212
1213 adr += chip->start;
1214
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001215 /* Ensure cmd read/writes are aligned. */
1216 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 spin_lock(chip->mutex);
1219
1220 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1221
1222 if (!ret) {
1223 if (chip->state != FL_POINT && chip->state != FL_READY)
1224 map_write(map, CMD(0xff), cmd_addr);
1225
1226 chip->state = FL_POINT;
1227 chip->ref_point_counter++;
1228 }
1229 spin_unlock(chip->mutex);
1230
1231 return ret;
1232}
1233
1234static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
1235{
1236 struct map_info *map = mtd->priv;
1237 struct cfi_private *cfi = map->fldrv_priv;
Andy Lowe097f2572007-01-12 18:05:10 -05001238 unsigned long ofs, last_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int chipnum;
1240 int ret = 0;
1241
1242 if (!map->virt || (from + len > mtd->size))
1243 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* Now lock the chip(s) to POINT state */
1246
1247 /* ofs: offset within the first chip that the first read should start */
1248 chipnum = (from >> cfi->chipshift);
1249 ofs = from - (chipnum << cfi->chipshift);
1250
Andy Lowe097f2572007-01-12 18:05:10 -05001251 *mtdbuf = (void *)map->virt + cfi->chips[chipnum].start + ofs;
1252 *retlen = 0;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 while (len) {
1255 unsigned long thislen;
1256
1257 if (chipnum >= cfi->numchips)
1258 break;
1259
Andy Lowe097f2572007-01-12 18:05:10 -05001260 /* We cannot point across chips that are virtually disjoint */
1261 if (!last_end)
1262 last_end = cfi->chips[chipnum].start;
1263 else if (cfi->chips[chipnum].start != last_end)
1264 break;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if ((len + ofs -1) >> cfi->chipshift)
1267 thislen = (1<<cfi->chipshift) - ofs;
1268 else
1269 thislen = len;
1270
1271 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1272 if (ret)
1273 break;
1274
1275 *retlen += thislen;
1276 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 ofs = 0;
Andy Lowe097f2572007-01-12 18:05:10 -05001279 last_end += 1 << cfi->chipshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 chipnum++;
1281 }
1282 return 0;
1283}
1284
1285static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
1286{
1287 struct map_info *map = mtd->priv;
1288 struct cfi_private *cfi = map->fldrv_priv;
1289 unsigned long ofs;
1290 int chipnum;
1291
1292 /* Now unlock the chip(s) POINT state */
1293
1294 /* ofs: offset within the first chip that the first read should start */
1295 chipnum = (from >> cfi->chipshift);
1296 ofs = from - (chipnum << cfi->chipshift);
1297
1298 while (len) {
1299 unsigned long thislen;
1300 struct flchip *chip;
1301
1302 chip = &cfi->chips[chipnum];
1303 if (chipnum >= cfi->numchips)
1304 break;
1305
1306 if ((len + ofs -1) >> cfi->chipshift)
1307 thislen = (1<<cfi->chipshift) - ofs;
1308 else
1309 thislen = len;
1310
1311 spin_lock(chip->mutex);
1312 if (chip->state == FL_POINT) {
1313 chip->ref_point_counter--;
1314 if(chip->ref_point_counter == 0)
1315 chip->state = FL_READY;
1316 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001317 printk(KERN_ERR "%s: Warning: unpoint called on non pointed region\n", map->name); /* Should this give an error? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 put_chip(map, chip, chip->start);
1320 spin_unlock(chip->mutex);
1321
1322 len -= thislen;
1323 ofs = 0;
1324 chipnum++;
1325 }
1326}
1327
1328static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1329{
1330 unsigned long cmd_addr;
1331 struct cfi_private *cfi = map->fldrv_priv;
1332 int ret;
1333
1334 adr += chip->start;
1335
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001336 /* Ensure cmd read/writes are aligned. */
1337 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 spin_lock(chip->mutex);
1340 ret = get_chip(map, chip, cmd_addr, FL_READY);
1341 if (ret) {
1342 spin_unlock(chip->mutex);
1343 return ret;
1344 }
1345
1346 if (chip->state != FL_POINT && chip->state != FL_READY) {
1347 map_write(map, CMD(0xff), cmd_addr);
1348
1349 chip->state = FL_READY;
1350 }
1351
1352 map_copy_from(map, buf, adr, len);
1353
1354 put_chip(map, chip, cmd_addr);
1355
1356 spin_unlock(chip->mutex);
1357 return 0;
1358}
1359
1360static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1361{
1362 struct map_info *map = mtd->priv;
1363 struct cfi_private *cfi = map->fldrv_priv;
1364 unsigned long ofs;
1365 int chipnum;
1366 int ret = 0;
1367
1368 /* ofs: offset within the first chip that the first read should start */
1369 chipnum = (from >> cfi->chipshift);
1370 ofs = from - (chipnum << cfi->chipshift);
1371
1372 *retlen = 0;
1373
1374 while (len) {
1375 unsigned long thislen;
1376
1377 if (chipnum >= cfi->numchips)
1378 break;
1379
1380 if ((len + ofs -1) >> cfi->chipshift)
1381 thislen = (1<<cfi->chipshift) - ofs;
1382 else
1383 thislen = len;
1384
1385 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1386 if (ret)
1387 break;
1388
1389 *retlen += thislen;
1390 len -= thislen;
1391 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ofs = 0;
1394 chipnum++;
1395 }
1396 return ret;
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001400 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001403 map_word status, write_cmd;
1404 int ret=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 adr += chip->start;
1407
Nicolas Pitref77814d2005-02-08 17:11:19 +00001408 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001409 case FL_WRITING:
1410 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0x40) : CMD(0x41);
1411 break;
1412 case FL_OTP_WRITE:
1413 write_cmd = CMD(0xc0);
1414 break;
1415 default:
1416 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 spin_lock(chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001420 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (ret) {
1422 spin_unlock(chip->mutex);
1423 return ret;
1424 }
1425
1426 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1427 ENABLE_VPP(map);
1428 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001429 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001431 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Nicolas Pitrec1724712006-03-30 15:52:41 +01001433 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1434 adr, map_bankwidth(map),
Alexey Korolev46a16522006-06-28 19:22:07 +01001435 chip->word_write_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001436 if (ret) {
1437 xip_enable(map, chip, adr);
1438 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Nicolas Pitre48436532005-08-06 05:16:52 +01001442 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001443 status = map_read(map, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001444 if (map_word_bitsset(map, status, CMD(0x1a))) {
1445 unsigned long chipstatus = MERGESTATUS(status);
1446
1447 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001450 xip_enable(map, chip, adr);
1451
1452 if (chipstatus & 0x02) {
1453 ret = -EROFS;
1454 } else if (chipstatus & 0x08) {
1455 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1456 ret = -EIO;
1457 } else {
1458 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1459 ret = -EINVAL;
1460 }
1461
1462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
1465 xip_enable(map, chip, adr);
1466 out: put_chip(map, chip, adr);
1467 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469}
1470
1471
1472static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1473{
1474 struct map_info *map = mtd->priv;
1475 struct cfi_private *cfi = map->fldrv_priv;
1476 int ret = 0;
1477 int chipnum;
1478 unsigned long ofs;
1479
1480 *retlen = 0;
1481 if (!len)
1482 return 0;
1483
1484 chipnum = to >> cfi->chipshift;
1485 ofs = to - (chipnum << cfi->chipshift);
1486
1487 /* If it's not bus-aligned, do the first byte write */
1488 if (ofs & (map_bankwidth(map)-1)) {
1489 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1490 int gap = ofs - bus_ofs;
1491 int n;
1492 map_word datum;
1493
1494 n = min_t(int, len, map_bankwidth(map)-gap);
1495 datum = map_word_ff(map);
1496 datum = map_word_load_partial(map, datum, buf, gap, n);
1497
1498 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001499 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001500 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return ret;
1502
1503 len -= n;
1504 ofs += n;
1505 buf += n;
1506 (*retlen) += n;
1507
1508 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001509 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 ofs = 0;
1511 if (chipnum == cfi->numchips)
1512 return 0;
1513 }
1514 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 while(len >= map_bankwidth(map)) {
1517 map_word datum = map_word_load(map, buf);
1518
1519 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001520 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (ret)
1522 return ret;
1523
1524 ofs += map_bankwidth(map);
1525 buf += map_bankwidth(map);
1526 (*retlen) += map_bankwidth(map);
1527 len -= map_bankwidth(map);
1528
1529 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001530 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 ofs = 0;
1532 if (chipnum == cfi->numchips)
1533 return 0;
1534 }
1535 }
1536
1537 if (len & (map_bankwidth(map)-1)) {
1538 map_word datum;
1539
1540 datum = map_word_ff(map);
1541 datum = map_word_load_partial(map, datum, buf, 0, len);
1542
1543 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001544 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001545 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 (*retlen) += len;
1549 }
1550
1551 return 0;
1552}
1553
1554
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001555static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001556 unsigned long adr, const struct kvec **pvec,
1557 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001560 map_word status, write_cmd, datum;
1561 unsigned long cmd_adr;
1562 int ret, wbufsize, word_gap, words;
Nicolas Pitree102d542005-08-06 05:46:59 +01001563 const struct kvec *vec;
1564 unsigned long vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1567 adr += chip->start;
1568 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* Let's determine this according to the interleave only once */
Nicolas Pitre638d9832005-08-06 05:40:46 +01001571 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 spin_lock(chip->mutex);
1574 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1575 if (ret) {
1576 spin_unlock(chip->mutex);
1577 return ret;
1578 }
1579
1580 XIP_INVAL_CACHED_RANGE(map, adr, len);
1581 ENABLE_VPP(map);
1582 xip_disable(map, chip, cmd_adr);
1583
David Woodhouse151e7652006-05-14 01:51:54 +01001584 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001585 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 So we must check here and reset those bits if they're set. Otherwise
1587 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001588 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001590 chip->state = FL_STATUS;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 status = map_read(map, cmd_adr);
1593 if (map_word_bitsset(map, status, CMD(0x30))) {
1594 xip_enable(map, chip, cmd_adr);
1595 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1596 xip_disable(map, chip, cmd_adr);
1597 map_write(map, CMD(0x50), cmd_adr);
1598 map_write(map, CMD(0x70), cmd_adr);
1599 }
1600
1601 chip->state = FL_WRITING_TO_BUFFER;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001602 map_write(map, write_cmd, cmd_adr);
1603 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0);
1604 if (ret) {
1605 /* Argh. Not ready for write to buffer */
1606 map_word Xstatus = map_read(map, cmd_adr);
1607 map_write(map, CMD(0x70), cmd_adr);
1608 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 status = map_read(map, cmd_adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001610 map_write(map, CMD(0x50), cmd_adr);
1611 map_write(map, CMD(0x70), cmd_adr);
1612 xip_enable(map, chip, cmd_adr);
1613 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1614 map->name, Xstatus.x[0], status.x[0]);
1615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
Nicolas Pitree102d542005-08-06 05:46:59 +01001618 /* Figure out the number of words to write */
1619 word_gap = (-adr & (map_bankwidth(map)-1));
1620 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
1621 if (!word_gap) {
1622 words--;
1623 } else {
1624 word_gap = map_bankwidth(map) - word_gap;
1625 adr -= word_gap;
1626 datum = map_word_ff(map);
1627 }
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001630 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001633 vec = *pvec;
1634 vec_seek = *pvec_seek;
1635 do {
1636 int n = map_bankwidth(map) - word_gap;
1637 if (n > vec->iov_len - vec_seek)
1638 n = vec->iov_len - vec_seek;
1639 if (n > len)
1640 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Nicolas Pitree102d542005-08-06 05:46:59 +01001642 if (!word_gap && len < map_bankwidth(map))
1643 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Nicolas Pitree102d542005-08-06 05:46:59 +01001645 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001646 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001647 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Nicolas Pitree102d542005-08-06 05:46:59 +01001649 len -= n;
1650 word_gap += n;
1651 if (!len || word_gap == map_bankwidth(map)) {
1652 map_write(map, datum, adr);
1653 adr += map_bankwidth(map);
1654 word_gap = 0;
1655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Nicolas Pitree102d542005-08-06 05:46:59 +01001657 vec_seek += n;
1658 if (vec_seek == vec->iov_len) {
1659 vec++;
1660 vec_seek = 0;
1661 }
1662 } while (len);
1663 *pvec = vec;
1664 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /* GO GO GO */
1667 map_write(map, CMD(0xd0), cmd_adr);
1668 chip->state = FL_WRITING;
1669
Nicolas Pitrec1724712006-03-30 15:52:41 +01001670 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
1671 adr, len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001672 chip->buffer_write_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001673 if (ret) {
1674 map_write(map, CMD(0x70), cmd_adr);
1675 chip->state = FL_STATUS;
1676 xip_enable(map, chip, cmd_adr);
1677 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Nicolas Pitre48436532005-08-06 05:16:52 +01001681 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001682 status = map_read(map, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001683 if (map_word_bitsset(map, status, CMD(0x1a))) {
1684 unsigned long chipstatus = MERGESTATUS(status);
1685
1686 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001688 map_write(map, CMD(0x70), cmd_adr);
1689 xip_enable(map, chip, cmd_adr);
1690
1691 if (chipstatus & 0x02) {
1692 ret = -EROFS;
1693 } else if (chipstatus & 0x08) {
1694 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1695 ret = -EIO;
1696 } else {
1697 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1698 ret = -EINVAL;
1699 }
1700
1701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
1704 xip_enable(map, chip, cmd_adr);
1705 out: put_chip(map, chip, cmd_adr);
1706 spin_unlock(chip->mutex);
1707 return ret;
1708}
1709
Nicolas Pitree102d542005-08-06 05:46:59 +01001710static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1711 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct map_info *map = mtd->priv;
1714 struct cfi_private *cfi = map->fldrv_priv;
1715 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1716 int ret = 0;
1717 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001718 unsigned long ofs, vec_seek, i;
1719 size_t len = 0;
1720
1721 for (i = 0; i < count; i++)
1722 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 *retlen = 0;
1725 if (!len)
1726 return 0;
1727
1728 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001729 ofs = to - (chipnum << cfi->chipshift);
1730 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Nicolas Pitree102d542005-08-06 05:46:59 +01001732 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* We must not cross write block boundaries */
1734 int size = wbufsize - (ofs & (wbufsize-1));
1735
1736 if (size > len)
1737 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001738 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001739 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (ret)
1741 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 (*retlen) += size;
1745 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 if (ofs >> cfi->chipshift) {
1748 chipnum ++;
1749 ofs = 0;
1750 if (chipnum == cfi->numchips)
1751 return 0;
1752 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001753
1754 /* Be nice and reschedule with the chip in a usable state for other
1755 processes. */
1756 cond_resched();
1757
Nicolas Pitree102d542005-08-06 05:46:59 +01001758 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
1761}
1762
Nicolas Pitree102d542005-08-06 05:46:59 +01001763static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1764 size_t len, size_t *retlen, const u_char *buf)
1765{
1766 struct kvec vec;
1767
1768 vec.iov_base = (void *) buf;
1769 vec.iov_len = len;
1770
1771 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1775 unsigned long adr, int len, void *thunk)
1776{
1777 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001778 map_word status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int retries = 3;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 adr += chip->start;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 retry:
1785 spin_lock(chip->mutex);
1786 ret = get_chip(map, chip, adr, FL_ERASING);
1787 if (ret) {
1788 spin_unlock(chip->mutex);
1789 return ret;
1790 }
1791
1792 XIP_INVAL_CACHED_RANGE(map, adr, len);
1793 ENABLE_VPP(map);
1794 xip_disable(map, chip, adr);
1795
1796 /* Clear the status register first */
1797 map_write(map, CMD(0x50), adr);
1798
1799 /* Now erase */
1800 map_write(map, CMD(0x20), adr);
1801 map_write(map, CMD(0xD0), adr);
1802 chip->state = FL_ERASING;
1803 chip->erase_suspended = 0;
1804
Nicolas Pitrec1724712006-03-30 15:52:41 +01001805 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1806 adr, len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001807 chip->erase_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001808 if (ret) {
1809 map_write(map, CMD(0x70), adr);
1810 chip->state = FL_STATUS;
1811 xip_enable(map, chip, adr);
1812 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815
1816 /* We've broken this before. It doesn't hurt to be safe */
1817 map_write(map, CMD(0x70), adr);
1818 chip->state = FL_STATUS;
1819 status = map_read(map, adr);
1820
Nicolas Pitre48436532005-08-06 05:16:52 +01001821 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001823 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* Reset the error bits */
1826 map_write(map, CMD(0x50), adr);
1827 map_write(map, CMD(0x70), adr);
1828 xip_enable(map, chip, adr);
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001831 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1832 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 } else if (chipstatus & 0x02) {
1834 /* Protection bit set */
1835 ret = -EROFS;
1836 } else if (chipstatus & 0x8) {
1837 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001838 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001840 } else if (chipstatus & 0x20 && retries--) {
1841 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
Nicolas Pitre48436532005-08-06 05:16:52 +01001842 put_chip(map, chip, adr);
1843 spin_unlock(chip->mutex);
1844 goto retry;
1845 } else {
1846 printk(KERN_ERR "%s: block erase failed at 0x%08lx (status 0x%lx)\n", map->name, adr, chipstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 ret = -EIO;
1848 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001849
1850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
Nicolas Pitre48436532005-08-06 05:16:52 +01001853 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 out: put_chip(map, chip, adr);
1855 spin_unlock(chip->mutex);
1856 return ret;
1857}
1858
Ben Dooks029a9eb2007-05-28 20:11:37 +01001859static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 unsigned long ofs, len;
1862 int ret;
1863
1864 ofs = instr->addr;
1865 len = instr->len;
1866
1867 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1868 if (ret)
1869 return ret;
1870
1871 instr->state = MTD_ERASE_DONE;
1872 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 0;
1875}
1876
1877static void cfi_intelext_sync (struct mtd_info *mtd)
1878{
1879 struct map_info *map = mtd->priv;
1880 struct cfi_private *cfi = map->fldrv_priv;
1881 int i;
1882 struct flchip *chip;
1883 int ret = 0;
1884
1885 for (i=0; !ret && i<cfi->numchips; i++) {
1886 chip = &cfi->chips[i];
1887
1888 spin_lock(chip->mutex);
1889 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1890
1891 if (!ret) {
1892 chip->oldstate = chip->state;
1893 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001894 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 * as the whole point is that nobody can do anything
1896 * with the chip now anyway.
1897 */
1898 }
1899 spin_unlock(chip->mutex);
1900 }
1901
1902 /* Unlock the chips again */
1903
1904 for (i--; i >=0; i--) {
1905 chip = &cfi->chips[i];
1906
1907 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (chip->state == FL_SYNCING) {
1910 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00001911 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 wake_up(&chip->wq);
1913 }
1914 spin_unlock(chip->mutex);
1915 }
1916}
1917
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001918static int __xipram do_getlockstatus_oneblock(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct flchip *chip,
1920 unsigned long adr,
1921 int len, void *thunk)
1922{
1923 struct cfi_private *cfi = map->fldrv_priv;
1924 int status, ofs_factor = cfi->interleave * cfi->device_type;
1925
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001926 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001928 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 chip->state = FL_JEDEC_QUERY;
1930 status = cfi_read_query(map, adr+(2*ofs_factor));
1931 xip_enable(map, chip, 0);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001932 return status;
1933}
1934
1935#ifdef DEBUG_LOCK_BITS
1936static int __xipram do_printlockstatus_oneblock(struct map_info *map,
1937 struct flchip *chip,
1938 unsigned long adr,
1939 int len, void *thunk)
1940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001942 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return 0;
1944}
1945#endif
1946
1947#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1948#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1949
1950static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
1951 unsigned long adr, int len, void *thunk)
1952{
1953 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001954 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001955 int udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int ret;
1957
1958 adr += chip->start;
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 spin_lock(chip->mutex);
1961 ret = get_chip(map, chip, adr, FL_LOCKING);
1962 if (ret) {
1963 spin_unlock(chip->mutex);
1964 return ret;
1965 }
1966
1967 ENABLE_VPP(map);
1968 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 map_write(map, CMD(0x60), adr);
1971 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
1972 map_write(map, CMD(0x01), adr);
1973 chip->state = FL_LOCKING;
1974 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
1975 map_write(map, CMD(0xD0), adr);
1976 chip->state = FL_UNLOCKING;
1977 } else
1978 BUG();
1979
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001980 /*
1981 * If Instant Individual Block Locking supported then no need
1982 * to delay.
1983 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001984 udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001985
Nicolas Pitrec1724712006-03-30 15:52:41 +01001986 ret = WAIT_TIMEOUT(map, chip, adr, udelay);
1987 if (ret) {
1988 map_write(map, CMD(0x70), adr);
1989 chip->state = FL_STATUS;
1990 xip_enable(map, chip, adr);
1991 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
1992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 xip_enable(map, chip, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001996out: put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 spin_unlock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001998 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2002{
2003 int ret;
2004
2005#ifdef DEBUG_LOCK_BITS
2006 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2007 __FUNCTION__, ofs, len);
2008 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002009 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010#endif
2011
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002012 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015#ifdef DEBUG_LOCK_BITS
2016 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2017 __FUNCTION__, ret);
2018 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002019 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020#endif
2021
2022 return ret;
2023}
2024
2025static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2026{
2027 int ret;
2028
2029#ifdef DEBUG_LOCK_BITS
2030 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2031 __FUNCTION__, ofs, len);
2032 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002033 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034#endif
2035
2036 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2037 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039#ifdef DEBUG_LOCK_BITS
2040 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2041 __FUNCTION__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002042 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002043 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return ret;
2047}
2048
Nicolas Pitref77814d2005-02-08 17:11:19 +00002049#ifdef CONFIG_MTD_OTP
2050
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002051typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002052 u_long data_offset, u_char *buf, u_int size,
2053 u_long prot_offset, u_int groupno, u_int groupsize);
2054
2055static int __xipram
2056do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2057 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2058{
2059 struct cfi_private *cfi = map->fldrv_priv;
2060 int ret;
2061
2062 spin_lock(chip->mutex);
2063 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2064 if (ret) {
2065 spin_unlock(chip->mutex);
2066 return ret;
2067 }
2068
2069 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002070 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002071
2072 xip_disable(map, chip, chip->start);
2073 if (chip->state != FL_JEDEC_QUERY) {
2074 map_write(map, CMD(0x90), chip->start);
2075 chip->state = FL_JEDEC_QUERY;
2076 }
2077 map_copy_from(map, buf, chip->start + offset, size);
2078 xip_enable(map, chip, chip->start);
2079
2080 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002081 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002082
2083 put_chip(map, chip, chip->start);
2084 spin_unlock(chip->mutex);
2085 return 0;
2086}
2087
2088static int
2089do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2090 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2091{
2092 int ret;
2093
2094 while (size) {
2095 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2096 int gap = offset - bus_ofs;
2097 int n = min_t(int, size, map_bankwidth(map)-gap);
2098 map_word datum = map_word_ff(map);
2099
2100 datum = map_word_load_partial(map, datum, buf, gap, n);
2101 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002102 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002103 return ret;
2104
2105 offset += n;
2106 buf += n;
2107 size -= n;
2108 }
2109
2110 return 0;
2111}
2112
2113static int
2114do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2115 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2116{
2117 struct cfi_private *cfi = map->fldrv_priv;
2118 map_word datum;
2119
2120 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002121 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002122 return -EXDEV;
2123
2124 datum = map_word_ff(map);
2125 datum = map_word_clr(map, datum, CMD(1 << grpno));
2126 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2127}
2128
2129static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2130 size_t *retlen, u_char *buf,
2131 otp_op_t action, int user_regs)
2132{
2133 struct map_info *map = mtd->priv;
2134 struct cfi_private *cfi = map->fldrv_priv;
2135 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2136 struct flchip *chip;
2137 struct cfi_intelext_otpinfo *otp;
2138 u_long devsize, reg_prot_offset, data_offset;
2139 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2140 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2141 int ret;
2142
2143 *retlen = 0;
2144
2145 /* Check that we actually have some OTP registers */
2146 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2147 return -ENODATA;
2148
2149 /* we need real chips here not virtual ones */
2150 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2151 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002152 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002153
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002154 /* Some chips have OTP located in the _top_ partition only.
2155 For example: Intel 28F256L18T (T means top-parameter device) */
2156 if (cfi->mfr == MANUFACTURER_INTEL) {
2157 switch (cfi->id) {
2158 case 0x880b:
2159 case 0x880c:
2160 case 0x880d:
2161 chip_num = chip_step - 1;
2162 }
2163 }
2164
2165 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002166 chip = &cfi->chips[chip_num];
2167 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2168
2169 /* first OTP region */
2170 field = 0;
2171 reg_prot_offset = extp->ProtRegAddr;
2172 reg_fact_groups = 1;
2173 reg_fact_size = 1 << extp->FactProtRegSize;
2174 reg_user_groups = 1;
2175 reg_user_size = 1 << extp->UserProtRegSize;
2176
2177 while (len > 0) {
2178 /* flash geometry fixup */
2179 data_offset = reg_prot_offset + 1;
2180 data_offset *= cfi->interleave * cfi->device_type;
2181 reg_prot_offset *= cfi->interleave * cfi->device_type;
2182 reg_fact_size *= cfi->interleave;
2183 reg_user_size *= cfi->interleave;
2184
2185 if (user_regs) {
2186 groups = reg_user_groups;
2187 groupsize = reg_user_size;
2188 /* skip over factory reg area */
2189 groupno = reg_fact_groups;
2190 data_offset += reg_fact_groups * reg_fact_size;
2191 } else {
2192 groups = reg_fact_groups;
2193 groupsize = reg_fact_size;
2194 groupno = 0;
2195 }
2196
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002197 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002198 if (!action) {
2199 /*
2200 * Special case: if action is NULL
2201 * we fill buf with otp_info records.
2202 */
2203 struct otp_info *otpinfo;
2204 map_word lockword;
2205 len -= sizeof(struct otp_info);
2206 if (len <= 0)
2207 return -ENOSPC;
2208 ret = do_otp_read(map, chip,
2209 reg_prot_offset,
2210 (u_char *)&lockword,
2211 map_bankwidth(map),
2212 0, 0, 0);
2213 if (ret)
2214 return ret;
2215 otpinfo = (struct otp_info *)buf;
2216 otpinfo->start = from;
2217 otpinfo->length = groupsize;
2218 otpinfo->locked =
2219 !map_word_bitsset(map, lockword,
2220 CMD(1 << groupno));
2221 from += groupsize;
2222 buf += sizeof(*otpinfo);
2223 *retlen += sizeof(*otpinfo);
2224 } else if (from >= groupsize) {
2225 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002226 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002227 } else {
2228 int size = groupsize;
2229 data_offset += from;
2230 size -= from;
2231 from = 0;
2232 if (size > len)
2233 size = len;
2234 ret = action(map, chip, data_offset,
2235 buf, size, reg_prot_offset,
2236 groupno, groupsize);
2237 if (ret < 0)
2238 return ret;
2239 buf += size;
2240 len -= size;
2241 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002242 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002243 }
2244 groupno++;
2245 groups--;
2246 }
2247
2248 /* next OTP region */
2249 if (++field == extp->NumProtectionFields)
2250 break;
2251 reg_prot_offset = otp->ProtRegAddr;
2252 reg_fact_groups = otp->FactGroups;
2253 reg_fact_size = 1 << otp->FactProtRegSize;
2254 reg_user_groups = otp->UserGroups;
2255 reg_user_size = 1 << otp->UserProtRegSize;
2256 otp++;
2257 }
2258 }
2259
2260 return 0;
2261}
2262
2263static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2264 size_t len, size_t *retlen,
2265 u_char *buf)
2266{
2267 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2268 buf, do_otp_read, 0);
2269}
2270
2271static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2272 size_t len, size_t *retlen,
2273 u_char *buf)
2274{
2275 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2276 buf, do_otp_read, 1);
2277}
2278
2279static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2280 size_t len, size_t *retlen,
2281 u_char *buf)
2282{
2283 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2284 buf, do_otp_write, 1);
2285}
2286
2287static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2288 loff_t from, size_t len)
2289{
2290 size_t retlen;
2291 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2292 NULL, do_otp_lock, 1);
2293}
2294
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002295static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002296 struct otp_info *buf, size_t len)
2297{
2298 size_t retlen;
2299 int ret;
2300
2301 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2302 return ret ? : retlen;
2303}
2304
2305static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2306 struct otp_info *buf, size_t len)
2307{
2308 size_t retlen;
2309 int ret;
2310
2311 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2312 return ret ? : retlen;
2313}
2314
2315#endif
2316
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002317static void cfi_intelext_save_locks(struct mtd_info *mtd)
2318{
2319 struct mtd_erase_region_info *region;
2320 int block, status, i;
2321 unsigned long adr;
2322 size_t len;
2323
2324 for (i = 0; i < mtd->numeraseregions; i++) {
2325 region = &mtd->eraseregions[i];
2326 if (!region->lockmap)
2327 continue;
2328
2329 for (block = 0; block < region->numblocks; block++){
2330 len = region->erasesize;
2331 adr = region->offset + block * len;
2332
2333 status = cfi_varsize_frob(mtd,
Ben Dooks029a9eb2007-05-28 20:11:37 +01002334 do_getlockstatus_oneblock, adr, len, NULL);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002335 if (status)
2336 set_bit(block, region->lockmap);
2337 else
2338 clear_bit(block, region->lockmap);
2339 }
2340 }
2341}
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343static int cfi_intelext_suspend(struct mtd_info *mtd)
2344{
2345 struct map_info *map = mtd->priv;
2346 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002347 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 int i;
2349 struct flchip *chip;
2350 int ret = 0;
2351
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002352 if ((mtd->flags & MTD_STUPID_LOCK)
2353 && extp && (extp->FeatureSupport & (1 << 5)))
2354 cfi_intelext_save_locks(mtd);
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 for (i=0; !ret && i<cfi->numchips; i++) {
2357 chip = &cfi->chips[i];
2358
2359 spin_lock(chip->mutex);
2360
2361 switch (chip->state) {
2362 case FL_READY:
2363 case FL_STATUS:
2364 case FL_CFI_QUERY:
2365 case FL_JEDEC_QUERY:
2366 if (chip->oldstate == FL_READY) {
David Andersa86aaa62006-10-19 19:33:19 +03002367 /* place the chip in a known state before suspend */
2368 map_write(map, CMD(0xFF), cfi->chips[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 chip->oldstate = chip->state;
2370 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002371 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 * as the whole point is that nobody can do anything
2373 * with the chip now anyway.
2374 */
2375 } else {
2376 /* There seems to be an operation pending. We must wait for it. */
2377 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2378 ret = -EAGAIN;
2379 }
2380 break;
2381 default:
2382 /* Should we actually wait? Once upon a time these routines weren't
2383 allowed to. Or should we return -EAGAIN, because the upper layers
2384 ought to have already shut down anything which was using the device
2385 anyway? The latter for now. */
2386 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2387 ret = -EAGAIN;
2388 case FL_PM_SUSPENDED:
2389 break;
2390 }
2391 spin_unlock(chip->mutex);
2392 }
2393
2394 /* Unlock the chips again */
2395
2396 if (ret) {
2397 for (i--; i >=0; i--) {
2398 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (chip->state == FL_PM_SUSPENDED) {
2403 /* No need to force it into a known state here,
2404 because we're returning failure, and it didn't
2405 get power cycled */
2406 chip->state = chip->oldstate;
2407 chip->oldstate = FL_READY;
2408 wake_up(&chip->wq);
2409 }
2410 spin_unlock(chip->mutex);
2411 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002412 }
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return ret;
2415}
2416
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002417static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2418{
2419 struct mtd_erase_region_info *region;
2420 int block, i;
2421 unsigned long adr;
2422 size_t len;
2423
2424 for (i = 0; i < mtd->numeraseregions; i++) {
2425 region = &mtd->eraseregions[i];
2426 if (!region->lockmap)
2427 continue;
2428
2429 for (block = 0; block < region->numblocks; block++) {
2430 len = region->erasesize;
2431 adr = region->offset + block * len;
2432
2433 if (!test_bit(block, region->lockmap))
2434 cfi_intelext_unlock(mtd, adr, len);
2435 }
2436 }
2437}
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static void cfi_intelext_resume(struct mtd_info *mtd)
2440{
2441 struct map_info *map = mtd->priv;
2442 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002443 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int i;
2445 struct flchip *chip;
2446
2447 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 chip = &cfi->chips[i];
2450
2451 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* Go to known state. Chip may have been power cycled */
2454 if (chip->state == FL_PM_SUSPENDED) {
2455 map_write(map, CMD(0xFF), cfi->chips[i].start);
2456 chip->oldstate = chip->state = FL_READY;
2457 wake_up(&chip->wq);
2458 }
2459
2460 spin_unlock(chip->mutex);
2461 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002462
2463 if ((mtd->flags & MTD_STUPID_LOCK)
2464 && extp && (extp->FeatureSupport & (1 << 5)))
2465 cfi_intelext_restore_locks(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002468static int cfi_intelext_reset(struct mtd_info *mtd)
2469{
2470 struct map_info *map = mtd->priv;
2471 struct cfi_private *cfi = map->fldrv_priv;
2472 int i, ret;
2473
2474 for (i=0; i < cfi->numchips; i++) {
2475 struct flchip *chip = &cfi->chips[i];
2476
2477 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002478 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002479 flash is accessible for soft reboot. */
2480 spin_lock(chip->mutex);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002481 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002482 if (!ret) {
2483 map_write(map, CMD(0xff), chip->start);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002484 chip->state = FL_SHUTDOWN;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002485 }
2486 spin_unlock(chip->mutex);
2487 }
2488
2489 return 0;
2490}
2491
2492static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2493 void *v)
2494{
2495 struct mtd_info *mtd;
2496
2497 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2498 cfi_intelext_reset(mtd);
2499 return NOTIFY_DONE;
2500}
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502static void cfi_intelext_destroy(struct mtd_info *mtd)
2503{
2504 struct map_info *map = mtd->priv;
2505 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002506 struct mtd_erase_region_info *region;
2507 int i;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002508 cfi_intelext_reset(mtd);
2509 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 kfree(cfi->cmdset_priv);
2511 kfree(cfi->cfiq);
2512 kfree(cfi->chips[0].priv);
2513 kfree(cfi);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002514 for (i = 0; i < mtd->numeraseregions; i++) {
2515 region = &mtd->eraseregions[i];
2516 if (region->lockmap)
2517 kfree(region->lockmap);
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 kfree(mtd->eraseregions);
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522MODULE_LICENSE("GPL");
2523MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2524MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002525MODULE_ALIAS("cfi_cmdset_0003");
2526MODULE_ALIAS("cfi_cmdset_0200");