blob: ed5ce41d1377bddb97cb21bfb22ac95e7d3cbcd6 [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;
798
799 retry:
800 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING
801 || mode == FL_OTP_WRITE || mode == FL_SHUTDOWN)) {
802 /*
803 * OK. We have possibility for contention on the write/erase
804 * operations which are global to the real chip and not per
805 * partition. So let's fight it over in the partition which
806 * currently has authority on the operation.
807 *
808 * The rules are as follows:
809 *
810 * - any write operation must own shared->writing.
811 *
812 * - any erase operation must own _both_ shared->writing and
813 * shared->erasing.
814 *
815 * - contention arbitration is handled in the owner's context.
816 *
817 * The 'shared' struct can be read and/or written only when
818 * its lock is taken.
819 */
820 struct flchip_shared *shared = chip->priv;
821 struct flchip *contender;
822 spin_lock(&shared->lock);
823 contender = shared->writing;
824 if (contender && contender != chip) {
825 /*
826 * The engine to perform desired operation on this
827 * partition is already in use by someone else.
828 * Let's fight over it in the context of the chip
829 * currently using it. If it is possible to suspend,
830 * that other partition will do just that, otherwise
831 * it'll happily send us to sleep. In any case, when
832 * get_chip returns success we're clear to go ahead.
833 */
834 ret = spin_trylock(contender->mutex);
835 spin_unlock(&shared->lock);
836 if (!ret)
837 goto retry;
838 spin_unlock(chip->mutex);
839 ret = chip_ready(map, contender, contender->start, mode);
840 spin_lock(chip->mutex);
841
842 if (ret == -EAGAIN) {
843 spin_unlock(contender->mutex);
844 goto retry;
845 }
846 if (ret) {
847 spin_unlock(contender->mutex);
848 return ret;
849 }
850 spin_lock(&shared->lock);
851 spin_unlock(contender->mutex);
852 }
853
854 /* We now own it */
855 shared->writing = chip;
856 if (mode == FL_ERASING)
857 shared->erasing = chip;
858 spin_unlock(&shared->lock);
859 }
860 ret = chip_ready(map, chip, adr, mode);
861 if (ret == -EAGAIN)
862 goto retry;
863
864 return ret;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
868{
869 struct cfi_private *cfi = map->fldrv_priv;
870
871 if (chip->priv) {
872 struct flchip_shared *shared = chip->priv;
873 spin_lock(&shared->lock);
874 if (shared->writing == chip && chip->oldstate == FL_READY) {
875 /* We own the ability to write, but we're done */
876 shared->writing = shared->erasing;
877 if (shared->writing && shared->writing != chip) {
878 /* give back ownership to who we loaned it from */
879 struct flchip *loaner = shared->writing;
880 spin_lock(loaner->mutex);
881 spin_unlock(&shared->lock);
882 spin_unlock(chip->mutex);
883 put_chip(map, loaner, loaner->start);
884 spin_lock(chip->mutex);
885 spin_unlock(loaner->mutex);
886 wake_up(&chip->wq);
887 return;
888 }
889 shared->erasing = NULL;
890 shared->writing = NULL;
891 } else if (shared->erasing == chip && shared->writing != chip) {
892 /*
893 * We own the ability to erase without the ability
894 * to write, which means the erase was suspended
895 * and some other partition is currently writing.
896 * Don't let the switch below mess things up since
897 * we don't have ownership to resume anything.
898 */
899 spin_unlock(&shared->lock);
900 wake_up(&chip->wq);
901 return;
902 }
903 spin_unlock(&shared->lock);
904 }
905
906 switch(chip->oldstate) {
907 case FL_ERASING:
908 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000909 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000911 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 errors to be returned from do_erase_oneblock because
913 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000914 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000916 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 do. */
918 map_write(map, CMD(0xd0), adr);
919 map_write(map, CMD(0x70), adr);
920 chip->oldstate = FL_READY;
921 chip->state = FL_ERASING;
922 break;
923
924 case FL_XIP_WHILE_ERASING:
925 chip->state = chip->oldstate;
926 chip->oldstate = FL_READY;
927 break;
928
929 case FL_READY:
930 case FL_STATUS:
931 case FL_JEDEC_QUERY:
932 /* We should really make set_vpp() count, rather than doing this */
933 DISABLE_VPP(map);
934 break;
935 default:
Nicolas Pitre48436532005-08-06 05:16:52 +0100936 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 wake_up(&chip->wq);
939}
940
941#ifdef CONFIG_MTD_XIP
942
943/*
944 * No interrupt what so ever can be serviced while the flash isn't in array
945 * mode. This is ensured by the xip_disable() and xip_enable() functions
946 * enclosing any code path where the flash is known not to be in array mode.
947 * And within a XIP disabled code path, only functions marked with __xipram
948 * may be called and nothing else (it's a good thing to inspect generated
949 * assembly to make sure inline functions were actually inlined and that gcc
950 * didn't emit calls to its own support functions). Also configuring MTD CFI
951 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
953
954static void xip_disable(struct map_info *map, struct flchip *chip,
955 unsigned long adr)
956{
957 /* TODO: chips with no XIP use should ignore and return */
958 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 local_irq_disable();
960}
961
962static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
963 unsigned long adr)
964{
965 struct cfi_private *cfi = map->fldrv_priv;
966 if (chip->state != FL_POINT && chip->state != FL_READY) {
967 map_write(map, CMD(0xff), adr);
968 chip->state = FL_READY;
969 }
970 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200971 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975/*
976 * When a delay is required for the flash operation to complete, the
Nicolas Pitrec1724712006-03-30 15:52:41 +0100977 * xip_wait_for_operation() function is polling for both the given timeout
978 * and pending (but still masked) hardware interrupts. Whenever there is an
979 * interrupt pending then the flash erase or write operation is suspended,
980 * array mode restored and interrupts unmasked. Task scheduling might also
981 * happen at that point. The CPU eventually returns from the interrupt or
982 * the call to schedule() and the suspended flash operation is resumed for
983 * the remaining of the delay period.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 *
985 * Warning: this function _will_ fool interrupt latency tracing tools.
986 */
987
Nicolas Pitrec1724712006-03-30 15:52:41 +0100988static int __xipram xip_wait_for_operation(
989 struct map_info *map, struct flchip *chip,
Alexey Korolev46a16522006-06-28 19:22:07 +0100990 unsigned long adr, unsigned int chip_op_time )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct cfi_private *cfi = map->fldrv_priv;
993 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
994 map_word status, OK = CMD(0x80);
Nicolas Pitrec1724712006-03-30 15:52:41 +0100995 unsigned long usec, suspended, start, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 flstate_t oldstate, newstate;
997
Nicolas Pitrec1724712006-03-30 15:52:41 +0100998 start = xip_currtime();
Alexey Korolev46a16522006-06-28 19:22:07 +0100999 usec = chip_op_time * 8;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001000 if (usec == 0)
1001 usec = 500000;
1002 done = 0;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 do {
1005 cpu_relax();
1006 if (xip_irqpending() && cfip &&
1007 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1008 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1009 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1010 /*
1011 * Let's suspend the erase or write operation when
1012 * supported. Note that we currently don't try to
1013 * suspend interleaved chips if there is already
1014 * another operation suspended (imagine what happens
1015 * when one chip was already done with the current
1016 * operation while another chip suspended it, then
1017 * we resume the whole thing at once). Yes, it
1018 * can happen!
1019 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001020 usec -= done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 map_write(map, CMD(0xb0), adr);
1022 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 suspended = xip_currtime();
1024 do {
1025 if (xip_elapsed_since(suspended) > 100000) {
1026 /*
1027 * The chip doesn't want to suspend
1028 * after waiting for 100 msecs.
1029 * This is a critical error but there
1030 * is not much we can do here.
1031 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001032 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 status = map_read(map, adr);
1035 } while (!map_word_andequal(map, status, OK, OK));
1036
1037 /* Suspend succeeded */
1038 oldstate = chip->state;
1039 if (oldstate == FL_ERASING) {
1040 if (!map_word_bitsset(map, status, CMD(0x40)))
1041 break;
1042 newstate = FL_XIP_WHILE_ERASING;
1043 chip->erase_suspended = 1;
1044 } else {
1045 if (!map_word_bitsset(map, status, CMD(0x04)))
1046 break;
1047 newstate = FL_XIP_WHILE_WRITING;
1048 chip->write_suspended = 1;
1049 }
1050 chip->state = newstate;
1051 map_write(map, CMD(0xff), adr);
1052 (void) map_read(map, adr);
1053 asm volatile (".rep 8; nop; .endr");
1054 local_irq_enable();
Nicolas Pitre6da70122005-05-19 18:05:47 +01001055 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 asm volatile (".rep 8; nop; .endr");
1057 cond_resched();
1058
1059 /*
1060 * We're back. However someone else might have
1061 * decided to go write to the chip if we are in
1062 * a suspended erase state. If so let's wait
1063 * until it's done.
1064 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001065 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 while (chip->state != newstate) {
1067 DECLARE_WAITQUEUE(wait, current);
1068 set_current_state(TASK_UNINTERRUPTIBLE);
1069 add_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001070 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 schedule();
1072 remove_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001073 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 /* Disallow XIP again */
1076 local_irq_disable();
1077
1078 /* Resume the write or erase operation */
1079 map_write(map, CMD(0xd0), adr);
1080 map_write(map, CMD(0x70), adr);
1081 chip->state = oldstate;
1082 start = xip_currtime();
1083 } else if (usec >= 1000000/HZ) {
1084 /*
1085 * Try to save on CPU power when waiting delay
1086 * is at least a system timer tick period.
1087 * No need to be extremely accurate here.
1088 */
1089 xip_cpu_idle();
1090 }
1091 status = map_read(map, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001092 done = xip_elapsed_since(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 } while (!map_word_andequal(map, status, OK, OK)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001094 && done < usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Nicolas Pitrec1724712006-03-30 15:52:41 +01001096 return (done >= usec) ? -ETIME : 0;
1097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099/*
1100 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1101 * the flash is actively programming or erasing since we have to poll for
1102 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001103 * a XIP setup so do it before the actual flash operation in this case
Nicolas Pitrec1724712006-03-30 15:52:41 +01001104 * and stub it out from INVAL_CACHE_AND_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001106#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1107 INVALIDATE_CACHED_RANGE(map, from, size)
1108
Alexey Korolev46a16522006-06-28 19:22:07 +01001109#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec) \
1110 xip_wait_for_operation(map, chip, cmd_adr, usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112#else
1113
1114#define xip_disable(map, chip, adr)
1115#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116#define XIP_INVAL_CACHED_RANGE(x...)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001117#define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Nicolas Pitrec1724712006-03-30 15:52:41 +01001119static int inval_cache_and_wait_for_operation(
1120 struct map_info *map, struct flchip *chip,
1121 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001122 unsigned int chip_op_time)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001123{
1124 struct cfi_private *cfi = map->fldrv_priv;
1125 map_word status, status_OK = CMD(0x80);
Alexey Korolev46a16522006-06-28 19:22:07 +01001126 int chip_state = chip->state;
1127 unsigned int timeo, sleep_time;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001128
Nicolas Pitrec1724712006-03-30 15:52:41 +01001129 spin_unlock(chip->mutex);
1130 if (inval_len)
1131 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001132 spin_lock(chip->mutex);
1133
Alexey Korolev46a16522006-06-28 19:22:07 +01001134 /* set our timeout to 8 times the expected delay */
1135 timeo = chip_op_time * 8;
1136 if (!timeo)
1137 timeo = 500000;
1138 sleep_time = chip_op_time / 2;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001139
Nicolas Pitrec1724712006-03-30 15:52:41 +01001140 for (;;) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001141 status = map_read(map, cmd_adr);
1142 if (map_word_andequal(map, status, status_OK, status_OK))
1143 break;
1144
Alexey Korolev46a16522006-06-28 19:22:07 +01001145 if (!timeo) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001146 map_write(map, CMD(0x70), cmd_adr);
1147 chip->state = FL_STATUS;
1148 return -ETIME;
1149 }
1150
Alexey Korolev46a16522006-06-28 19:22:07 +01001151 /* OK Still waiting. Drop the lock, wait a while and retry. */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001152 spin_unlock(chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001153 if (sleep_time >= 1000000/HZ) {
1154 /*
1155 * Half of the normal delay still remaining
1156 * can be performed with a sleeping delay instead
1157 * of busy waiting.
1158 */
1159 msleep(sleep_time/1000);
1160 timeo -= sleep_time;
1161 sleep_time = 1000000/HZ;
1162 } else {
1163 udelay(1);
1164 cond_resched();
1165 timeo--;
1166 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001167 spin_lock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001168
Joakim Tjernlund967bf622006-11-28 23:11:52 +00001169 while (chip->state != chip_state) {
Alexey Korolev46a16522006-06-28 19:22:07 +01001170 /* Someone's suspended the operation: sleep */
1171 DECLARE_WAITQUEUE(wait, current);
1172 set_current_state(TASK_UNINTERRUPTIBLE);
1173 add_wait_queue(&chip->wq, &wait);
1174 spin_unlock(chip->mutex);
1175 schedule();
1176 remove_wait_queue(&chip->wq, &wait);
1177 spin_lock(chip->mutex);
1178 }
1179 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001180
1181 /* Done and happy. */
1182 chip->state = FL_STATUS;
1183 return 0;
1184}
Nicolas Pitre6da70122005-05-19 18:05:47 +01001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186#endif
1187
Nicolas Pitrec1724712006-03-30 15:52:41 +01001188#define WAIT_TIMEOUT(map, chip, adr, udelay) \
Alexey Korolev46a16522006-06-28 19:22:07 +01001189 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001190
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1193{
1194 unsigned long cmd_addr;
1195 struct cfi_private *cfi = map->fldrv_priv;
1196 int ret = 0;
1197
1198 adr += chip->start;
1199
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001200 /* Ensure cmd read/writes are aligned. */
1201 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 spin_lock(chip->mutex);
1204
1205 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1206
1207 if (!ret) {
1208 if (chip->state != FL_POINT && chip->state != FL_READY)
1209 map_write(map, CMD(0xff), cmd_addr);
1210
1211 chip->state = FL_POINT;
1212 chip->ref_point_counter++;
1213 }
1214 spin_unlock(chip->mutex);
1215
1216 return ret;
1217}
1218
1219static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
1220{
1221 struct map_info *map = mtd->priv;
1222 struct cfi_private *cfi = map->fldrv_priv;
Andy Lowe097f2572007-01-12 18:05:10 -05001223 unsigned long ofs, last_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int chipnum;
1225 int ret = 0;
1226
1227 if (!map->virt || (from + len > mtd->size))
1228 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* Now lock the chip(s) to POINT state */
1231
1232 /* ofs: offset within the first chip that the first read should start */
1233 chipnum = (from >> cfi->chipshift);
1234 ofs = from - (chipnum << cfi->chipshift);
1235
Andy Lowe097f2572007-01-12 18:05:10 -05001236 *mtdbuf = (void *)map->virt + cfi->chips[chipnum].start + ofs;
1237 *retlen = 0;
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 while (len) {
1240 unsigned long thislen;
1241
1242 if (chipnum >= cfi->numchips)
1243 break;
1244
Andy Lowe097f2572007-01-12 18:05:10 -05001245 /* We cannot point across chips that are virtually disjoint */
1246 if (!last_end)
1247 last_end = cfi->chips[chipnum].start;
1248 else if (cfi->chips[chipnum].start != last_end)
1249 break;
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if ((len + ofs -1) >> cfi->chipshift)
1252 thislen = (1<<cfi->chipshift) - ofs;
1253 else
1254 thislen = len;
1255
1256 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1257 if (ret)
1258 break;
1259
1260 *retlen += thislen;
1261 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 ofs = 0;
Andy Lowe097f2572007-01-12 18:05:10 -05001264 last_end += 1 << cfi->chipshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 chipnum++;
1266 }
1267 return 0;
1268}
1269
1270static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
1271{
1272 struct map_info *map = mtd->priv;
1273 struct cfi_private *cfi = map->fldrv_priv;
1274 unsigned long ofs;
1275 int chipnum;
1276
1277 /* Now unlock the chip(s) POINT state */
1278
1279 /* ofs: offset within the first chip that the first read should start */
1280 chipnum = (from >> cfi->chipshift);
1281 ofs = from - (chipnum << cfi->chipshift);
1282
1283 while (len) {
1284 unsigned long thislen;
1285 struct flchip *chip;
1286
1287 chip = &cfi->chips[chipnum];
1288 if (chipnum >= cfi->numchips)
1289 break;
1290
1291 if ((len + ofs -1) >> cfi->chipshift)
1292 thislen = (1<<cfi->chipshift) - ofs;
1293 else
1294 thislen = len;
1295
1296 spin_lock(chip->mutex);
1297 if (chip->state == FL_POINT) {
1298 chip->ref_point_counter--;
1299 if(chip->ref_point_counter == 0)
1300 chip->state = FL_READY;
1301 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001302 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 -07001303
1304 put_chip(map, chip, chip->start);
1305 spin_unlock(chip->mutex);
1306
1307 len -= thislen;
1308 ofs = 0;
1309 chipnum++;
1310 }
1311}
1312
1313static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1314{
1315 unsigned long cmd_addr;
1316 struct cfi_private *cfi = map->fldrv_priv;
1317 int ret;
1318
1319 adr += chip->start;
1320
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001321 /* Ensure cmd read/writes are aligned. */
1322 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 spin_lock(chip->mutex);
1325 ret = get_chip(map, chip, cmd_addr, FL_READY);
1326 if (ret) {
1327 spin_unlock(chip->mutex);
1328 return ret;
1329 }
1330
1331 if (chip->state != FL_POINT && chip->state != FL_READY) {
1332 map_write(map, CMD(0xff), cmd_addr);
1333
1334 chip->state = FL_READY;
1335 }
1336
1337 map_copy_from(map, buf, adr, len);
1338
1339 put_chip(map, chip, cmd_addr);
1340
1341 spin_unlock(chip->mutex);
1342 return 0;
1343}
1344
1345static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1346{
1347 struct map_info *map = mtd->priv;
1348 struct cfi_private *cfi = map->fldrv_priv;
1349 unsigned long ofs;
1350 int chipnum;
1351 int ret = 0;
1352
1353 /* ofs: offset within the first chip that the first read should start */
1354 chipnum = (from >> cfi->chipshift);
1355 ofs = from - (chipnum << cfi->chipshift);
1356
1357 *retlen = 0;
1358
1359 while (len) {
1360 unsigned long thislen;
1361
1362 if (chipnum >= cfi->numchips)
1363 break;
1364
1365 if ((len + ofs -1) >> cfi->chipshift)
1366 thislen = (1<<cfi->chipshift) - ofs;
1367 else
1368 thislen = len;
1369
1370 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1371 if (ret)
1372 break;
1373
1374 *retlen += thislen;
1375 len -= thislen;
1376 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 ofs = 0;
1379 chipnum++;
1380 }
1381 return ret;
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001385 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001388 map_word status, write_cmd;
1389 int ret=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 adr += chip->start;
1392
Nicolas Pitref77814d2005-02-08 17:11:19 +00001393 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001394 case FL_WRITING:
1395 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0x40) : CMD(0x41);
1396 break;
1397 case FL_OTP_WRITE:
1398 write_cmd = CMD(0xc0);
1399 break;
1400 default:
1401 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 spin_lock(chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001405 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (ret) {
1407 spin_unlock(chip->mutex);
1408 return ret;
1409 }
1410
1411 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1412 ENABLE_VPP(map);
1413 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001414 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001416 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Nicolas Pitrec1724712006-03-30 15:52:41 +01001418 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1419 adr, map_bankwidth(map),
Alexey Korolev46a16522006-06-28 19:22:07 +01001420 chip->word_write_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001421 if (ret) {
1422 xip_enable(map, chip, adr);
1423 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Nicolas Pitre48436532005-08-06 05:16:52 +01001427 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001428 status = map_read(map, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001429 if (map_word_bitsset(map, status, CMD(0x1a))) {
1430 unsigned long chipstatus = MERGESTATUS(status);
1431
1432 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001435 xip_enable(map, chip, adr);
1436
1437 if (chipstatus & 0x02) {
1438 ret = -EROFS;
1439 } else if (chipstatus & 0x08) {
1440 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1441 ret = -EIO;
1442 } else {
1443 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1444 ret = -EINVAL;
1445 }
1446
1447 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
1450 xip_enable(map, chip, adr);
1451 out: put_chip(map, chip, adr);
1452 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return ret;
1454}
1455
1456
1457static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1458{
1459 struct map_info *map = mtd->priv;
1460 struct cfi_private *cfi = map->fldrv_priv;
1461 int ret = 0;
1462 int chipnum;
1463 unsigned long ofs;
1464
1465 *retlen = 0;
1466 if (!len)
1467 return 0;
1468
1469 chipnum = to >> cfi->chipshift;
1470 ofs = to - (chipnum << cfi->chipshift);
1471
1472 /* If it's not bus-aligned, do the first byte write */
1473 if (ofs & (map_bankwidth(map)-1)) {
1474 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1475 int gap = ofs - bus_ofs;
1476 int n;
1477 map_word datum;
1478
1479 n = min_t(int, len, map_bankwidth(map)-gap);
1480 datum = map_word_ff(map);
1481 datum = map_word_load_partial(map, datum, buf, gap, n);
1482
1483 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001484 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001485 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return ret;
1487
1488 len -= n;
1489 ofs += n;
1490 buf += n;
1491 (*retlen) += n;
1492
1493 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001494 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ofs = 0;
1496 if (chipnum == cfi->numchips)
1497 return 0;
1498 }
1499 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 while(len >= map_bankwidth(map)) {
1502 map_word datum = map_word_load(map, buf);
1503
1504 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001505 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (ret)
1507 return ret;
1508
1509 ofs += map_bankwidth(map);
1510 buf += map_bankwidth(map);
1511 (*retlen) += map_bankwidth(map);
1512 len -= map_bankwidth(map);
1513
1514 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001515 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 ofs = 0;
1517 if (chipnum == cfi->numchips)
1518 return 0;
1519 }
1520 }
1521
1522 if (len & (map_bankwidth(map)-1)) {
1523 map_word datum;
1524
1525 datum = map_word_ff(map);
1526 datum = map_word_load_partial(map, datum, buf, 0, len);
1527
1528 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001529 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001530 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 (*retlen) += len;
1534 }
1535
1536 return 0;
1537}
1538
1539
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001540static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001541 unsigned long adr, const struct kvec **pvec,
1542 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001545 map_word status, write_cmd, datum;
1546 unsigned long cmd_adr;
1547 int ret, wbufsize, word_gap, words;
Nicolas Pitree102d542005-08-06 05:46:59 +01001548 const struct kvec *vec;
1549 unsigned long vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1552 adr += chip->start;
1553 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* Let's determine this according to the interleave only once */
Nicolas Pitre638d9832005-08-06 05:40:46 +01001556 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 spin_lock(chip->mutex);
1559 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1560 if (ret) {
1561 spin_unlock(chip->mutex);
1562 return ret;
1563 }
1564
1565 XIP_INVAL_CACHED_RANGE(map, adr, len);
1566 ENABLE_VPP(map);
1567 xip_disable(map, chip, cmd_adr);
1568
David Woodhouse151e7652006-05-14 01:51:54 +01001569 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001570 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 So we must check here and reset those bits if they're set. Otherwise
1572 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001573 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001575 chip->state = FL_STATUS;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 status = map_read(map, cmd_adr);
1578 if (map_word_bitsset(map, status, CMD(0x30))) {
1579 xip_enable(map, chip, cmd_adr);
1580 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1581 xip_disable(map, chip, cmd_adr);
1582 map_write(map, CMD(0x50), cmd_adr);
1583 map_write(map, CMD(0x70), cmd_adr);
1584 }
1585
1586 chip->state = FL_WRITING_TO_BUFFER;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001587 map_write(map, write_cmd, cmd_adr);
1588 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0);
1589 if (ret) {
1590 /* Argh. Not ready for write to buffer */
1591 map_word Xstatus = map_read(map, cmd_adr);
1592 map_write(map, CMD(0x70), cmd_adr);
1593 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 status = map_read(map, cmd_adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001595 map_write(map, CMD(0x50), cmd_adr);
1596 map_write(map, CMD(0x70), cmd_adr);
1597 xip_enable(map, chip, cmd_adr);
1598 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1599 map->name, Xstatus.x[0], status.x[0]);
1600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
Nicolas Pitree102d542005-08-06 05:46:59 +01001603 /* Figure out the number of words to write */
1604 word_gap = (-adr & (map_bankwidth(map)-1));
1605 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
1606 if (!word_gap) {
1607 words--;
1608 } else {
1609 word_gap = map_bankwidth(map) - word_gap;
1610 adr -= word_gap;
1611 datum = map_word_ff(map);
1612 }
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001615 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001618 vec = *pvec;
1619 vec_seek = *pvec_seek;
1620 do {
1621 int n = map_bankwidth(map) - word_gap;
1622 if (n > vec->iov_len - vec_seek)
1623 n = vec->iov_len - vec_seek;
1624 if (n > len)
1625 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Nicolas Pitree102d542005-08-06 05:46:59 +01001627 if (!word_gap && len < map_bankwidth(map))
1628 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Nicolas Pitree102d542005-08-06 05:46:59 +01001630 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001631 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001632 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Nicolas Pitree102d542005-08-06 05:46:59 +01001634 len -= n;
1635 word_gap += n;
1636 if (!len || word_gap == map_bankwidth(map)) {
1637 map_write(map, datum, adr);
1638 adr += map_bankwidth(map);
1639 word_gap = 0;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Nicolas Pitree102d542005-08-06 05:46:59 +01001642 vec_seek += n;
1643 if (vec_seek == vec->iov_len) {
1644 vec++;
1645 vec_seek = 0;
1646 }
1647 } while (len);
1648 *pvec = vec;
1649 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /* GO GO GO */
1652 map_write(map, CMD(0xd0), cmd_adr);
1653 chip->state = FL_WRITING;
1654
Nicolas Pitrec1724712006-03-30 15:52:41 +01001655 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
1656 adr, len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001657 chip->buffer_write_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001658 if (ret) {
1659 map_write(map, CMD(0x70), cmd_adr);
1660 chip->state = FL_STATUS;
1661 xip_enable(map, chip, cmd_adr);
1662 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Nicolas Pitre48436532005-08-06 05:16:52 +01001666 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001667 status = map_read(map, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001668 if (map_word_bitsset(map, status, CMD(0x1a))) {
1669 unsigned long chipstatus = MERGESTATUS(status);
1670
1671 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001673 map_write(map, CMD(0x70), cmd_adr);
1674 xip_enable(map, chip, cmd_adr);
1675
1676 if (chipstatus & 0x02) {
1677 ret = -EROFS;
1678 } else if (chipstatus & 0x08) {
1679 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1680 ret = -EIO;
1681 } else {
1682 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1683 ret = -EINVAL;
1684 }
1685
1686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
1689 xip_enable(map, chip, cmd_adr);
1690 out: put_chip(map, chip, cmd_adr);
1691 spin_unlock(chip->mutex);
1692 return ret;
1693}
1694
Nicolas Pitree102d542005-08-06 05:46:59 +01001695static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1696 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct map_info *map = mtd->priv;
1699 struct cfi_private *cfi = map->fldrv_priv;
1700 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1701 int ret = 0;
1702 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001703 unsigned long ofs, vec_seek, i;
1704 size_t len = 0;
1705
1706 for (i = 0; i < count; i++)
1707 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 *retlen = 0;
1710 if (!len)
1711 return 0;
1712
1713 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001714 ofs = to - (chipnum << cfi->chipshift);
1715 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Nicolas Pitree102d542005-08-06 05:46:59 +01001717 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* We must not cross write block boundaries */
1719 int size = wbufsize - (ofs & (wbufsize-1));
1720
1721 if (size > len)
1722 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001723 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001724 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (ret)
1726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 (*retlen) += size;
1730 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 if (ofs >> cfi->chipshift) {
1733 chipnum ++;
1734 ofs = 0;
1735 if (chipnum == cfi->numchips)
1736 return 0;
1737 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001738
1739 /* Be nice and reschedule with the chip in a usable state for other
1740 processes. */
1741 cond_resched();
1742
Nicolas Pitree102d542005-08-06 05:46:59 +01001743 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return 0;
1746}
1747
Nicolas Pitree102d542005-08-06 05:46:59 +01001748static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1749 size_t len, size_t *retlen, const u_char *buf)
1750{
1751 struct kvec vec;
1752
1753 vec.iov_base = (void *) buf;
1754 vec.iov_len = len;
1755
1756 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1760 unsigned long adr, int len, void *thunk)
1761{
1762 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001763 map_word status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int retries = 3;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001765 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 adr += chip->start;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 retry:
1770 spin_lock(chip->mutex);
1771 ret = get_chip(map, chip, adr, FL_ERASING);
1772 if (ret) {
1773 spin_unlock(chip->mutex);
1774 return ret;
1775 }
1776
1777 XIP_INVAL_CACHED_RANGE(map, adr, len);
1778 ENABLE_VPP(map);
1779 xip_disable(map, chip, adr);
1780
1781 /* Clear the status register first */
1782 map_write(map, CMD(0x50), adr);
1783
1784 /* Now erase */
1785 map_write(map, CMD(0x20), adr);
1786 map_write(map, CMD(0xD0), adr);
1787 chip->state = FL_ERASING;
1788 chip->erase_suspended = 0;
1789
Nicolas Pitrec1724712006-03-30 15:52:41 +01001790 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1791 adr, len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001792 chip->erase_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001793 if (ret) {
1794 map_write(map, CMD(0x70), adr);
1795 chip->state = FL_STATUS;
1796 xip_enable(map, chip, adr);
1797 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
1801 /* We've broken this before. It doesn't hurt to be safe */
1802 map_write(map, CMD(0x70), adr);
1803 chip->state = FL_STATUS;
1804 status = map_read(map, adr);
1805
Nicolas Pitre48436532005-08-06 05:16:52 +01001806 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001808 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* Reset the error bits */
1811 map_write(map, CMD(0x50), adr);
1812 map_write(map, CMD(0x70), adr);
1813 xip_enable(map, chip, adr);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001816 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1817 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 } else if (chipstatus & 0x02) {
1819 /* Protection bit set */
1820 ret = -EROFS;
1821 } else if (chipstatus & 0x8) {
1822 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001823 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001825 } else if (chipstatus & 0x20 && retries--) {
1826 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
Nicolas Pitre48436532005-08-06 05:16:52 +01001827 put_chip(map, chip, adr);
1828 spin_unlock(chip->mutex);
1829 goto retry;
1830 } else {
1831 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 -07001832 ret = -EIO;
1833 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001834
1835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837
Nicolas Pitre48436532005-08-06 05:16:52 +01001838 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 out: put_chip(map, chip, adr);
1840 spin_unlock(chip->mutex);
1841 return ret;
1842}
1843
Ben Dooks029a9eb2007-05-28 20:11:37 +01001844static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 unsigned long ofs, len;
1847 int ret;
1848
1849 ofs = instr->addr;
1850 len = instr->len;
1851
1852 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1853 if (ret)
1854 return ret;
1855
1856 instr->state = MTD_ERASE_DONE;
1857 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return 0;
1860}
1861
1862static void cfi_intelext_sync (struct mtd_info *mtd)
1863{
1864 struct map_info *map = mtd->priv;
1865 struct cfi_private *cfi = map->fldrv_priv;
1866 int i;
1867 struct flchip *chip;
1868 int ret = 0;
1869
1870 for (i=0; !ret && i<cfi->numchips; i++) {
1871 chip = &cfi->chips[i];
1872
1873 spin_lock(chip->mutex);
1874 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1875
1876 if (!ret) {
1877 chip->oldstate = chip->state;
1878 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001879 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 * as the whole point is that nobody can do anything
1881 * with the chip now anyway.
1882 */
1883 }
1884 spin_unlock(chip->mutex);
1885 }
1886
1887 /* Unlock the chips again */
1888
1889 for (i--; i >=0; i--) {
1890 chip = &cfi->chips[i];
1891
1892 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (chip->state == FL_SYNCING) {
1895 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00001896 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 wake_up(&chip->wq);
1898 }
1899 spin_unlock(chip->mutex);
1900 }
1901}
1902
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001903static int __xipram do_getlockstatus_oneblock(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct flchip *chip,
1905 unsigned long adr,
1906 int len, void *thunk)
1907{
1908 struct cfi_private *cfi = map->fldrv_priv;
1909 int status, ofs_factor = cfi->interleave * cfi->device_type;
1910
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001911 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001913 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 chip->state = FL_JEDEC_QUERY;
1915 status = cfi_read_query(map, adr+(2*ofs_factor));
1916 xip_enable(map, chip, 0);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001917 return status;
1918}
1919
1920#ifdef DEBUG_LOCK_BITS
1921static int __xipram do_printlockstatus_oneblock(struct map_info *map,
1922 struct flchip *chip,
1923 unsigned long adr,
1924 int len, void *thunk)
1925{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001927 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return 0;
1929}
1930#endif
1931
1932#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1933#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1934
1935static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
1936 unsigned long adr, int len, void *thunk)
1937{
1938 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001939 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001940 int udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int ret;
1942
1943 adr += chip->start;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 spin_lock(chip->mutex);
1946 ret = get_chip(map, chip, adr, FL_LOCKING);
1947 if (ret) {
1948 spin_unlock(chip->mutex);
1949 return ret;
1950 }
1951
1952 ENABLE_VPP(map);
1953 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 map_write(map, CMD(0x60), adr);
1956 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
1957 map_write(map, CMD(0x01), adr);
1958 chip->state = FL_LOCKING;
1959 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
1960 map_write(map, CMD(0xD0), adr);
1961 chip->state = FL_UNLOCKING;
1962 } else
1963 BUG();
1964
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001965 /*
1966 * If Instant Individual Block Locking supported then no need
1967 * to delay.
1968 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001969 udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001970
Nicolas Pitrec1724712006-03-30 15:52:41 +01001971 ret = WAIT_TIMEOUT(map, chip, adr, udelay);
1972 if (ret) {
1973 map_write(map, CMD(0x70), adr);
1974 chip->state = FL_STATUS;
1975 xip_enable(map, chip, adr);
1976 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
1977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 xip_enable(map, chip, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001981out: put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 spin_unlock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
1986static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
1987{
1988 int ret;
1989
1990#ifdef DEBUG_LOCK_BITS
1991 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
1992 __FUNCTION__, ofs, len);
1993 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01001994 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995#endif
1996
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001997 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000#ifdef DEBUG_LOCK_BITS
2001 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2002 __FUNCTION__, ret);
2003 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002004 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#endif
2006
2007 return ret;
2008}
2009
2010static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2011{
2012 int ret;
2013
2014#ifdef DEBUG_LOCK_BITS
2015 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2016 __FUNCTION__, ofs, len);
2017 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002018 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019#endif
2020
2021 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2022 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024#ifdef DEBUG_LOCK_BITS
2025 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2026 __FUNCTION__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002027 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002028 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return ret;
2032}
2033
Nicolas Pitref77814d2005-02-08 17:11:19 +00002034#ifdef CONFIG_MTD_OTP
2035
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002036typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002037 u_long data_offset, u_char *buf, u_int size,
2038 u_long prot_offset, u_int groupno, u_int groupsize);
2039
2040static int __xipram
2041do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2042 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2043{
2044 struct cfi_private *cfi = map->fldrv_priv;
2045 int ret;
2046
2047 spin_lock(chip->mutex);
2048 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2049 if (ret) {
2050 spin_unlock(chip->mutex);
2051 return ret;
2052 }
2053
2054 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002055 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002056
2057 xip_disable(map, chip, chip->start);
2058 if (chip->state != FL_JEDEC_QUERY) {
2059 map_write(map, CMD(0x90), chip->start);
2060 chip->state = FL_JEDEC_QUERY;
2061 }
2062 map_copy_from(map, buf, chip->start + offset, size);
2063 xip_enable(map, chip, chip->start);
2064
2065 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002066 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002067
2068 put_chip(map, chip, chip->start);
2069 spin_unlock(chip->mutex);
2070 return 0;
2071}
2072
2073static int
2074do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2075 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2076{
2077 int ret;
2078
2079 while (size) {
2080 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2081 int gap = offset - bus_ofs;
2082 int n = min_t(int, size, map_bankwidth(map)-gap);
2083 map_word datum = map_word_ff(map);
2084
2085 datum = map_word_load_partial(map, datum, buf, gap, n);
2086 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002087 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002088 return ret;
2089
2090 offset += n;
2091 buf += n;
2092 size -= n;
2093 }
2094
2095 return 0;
2096}
2097
2098static int
2099do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2100 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2101{
2102 struct cfi_private *cfi = map->fldrv_priv;
2103 map_word datum;
2104
2105 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002106 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002107 return -EXDEV;
2108
2109 datum = map_word_ff(map);
2110 datum = map_word_clr(map, datum, CMD(1 << grpno));
2111 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2112}
2113
2114static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2115 size_t *retlen, u_char *buf,
2116 otp_op_t action, int user_regs)
2117{
2118 struct map_info *map = mtd->priv;
2119 struct cfi_private *cfi = map->fldrv_priv;
2120 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2121 struct flchip *chip;
2122 struct cfi_intelext_otpinfo *otp;
2123 u_long devsize, reg_prot_offset, data_offset;
2124 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2125 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2126 int ret;
2127
2128 *retlen = 0;
2129
2130 /* Check that we actually have some OTP registers */
2131 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2132 return -ENODATA;
2133
2134 /* we need real chips here not virtual ones */
2135 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2136 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002137 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002138
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002139 /* Some chips have OTP located in the _top_ partition only.
2140 For example: Intel 28F256L18T (T means top-parameter device) */
2141 if (cfi->mfr == MANUFACTURER_INTEL) {
2142 switch (cfi->id) {
2143 case 0x880b:
2144 case 0x880c:
2145 case 0x880d:
2146 chip_num = chip_step - 1;
2147 }
2148 }
2149
2150 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002151 chip = &cfi->chips[chip_num];
2152 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2153
2154 /* first OTP region */
2155 field = 0;
2156 reg_prot_offset = extp->ProtRegAddr;
2157 reg_fact_groups = 1;
2158 reg_fact_size = 1 << extp->FactProtRegSize;
2159 reg_user_groups = 1;
2160 reg_user_size = 1 << extp->UserProtRegSize;
2161
2162 while (len > 0) {
2163 /* flash geometry fixup */
2164 data_offset = reg_prot_offset + 1;
2165 data_offset *= cfi->interleave * cfi->device_type;
2166 reg_prot_offset *= cfi->interleave * cfi->device_type;
2167 reg_fact_size *= cfi->interleave;
2168 reg_user_size *= cfi->interleave;
2169
2170 if (user_regs) {
2171 groups = reg_user_groups;
2172 groupsize = reg_user_size;
2173 /* skip over factory reg area */
2174 groupno = reg_fact_groups;
2175 data_offset += reg_fact_groups * reg_fact_size;
2176 } else {
2177 groups = reg_fact_groups;
2178 groupsize = reg_fact_size;
2179 groupno = 0;
2180 }
2181
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002182 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002183 if (!action) {
2184 /*
2185 * Special case: if action is NULL
2186 * we fill buf with otp_info records.
2187 */
2188 struct otp_info *otpinfo;
2189 map_word lockword;
2190 len -= sizeof(struct otp_info);
2191 if (len <= 0)
2192 return -ENOSPC;
2193 ret = do_otp_read(map, chip,
2194 reg_prot_offset,
2195 (u_char *)&lockword,
2196 map_bankwidth(map),
2197 0, 0, 0);
2198 if (ret)
2199 return ret;
2200 otpinfo = (struct otp_info *)buf;
2201 otpinfo->start = from;
2202 otpinfo->length = groupsize;
2203 otpinfo->locked =
2204 !map_word_bitsset(map, lockword,
2205 CMD(1 << groupno));
2206 from += groupsize;
2207 buf += sizeof(*otpinfo);
2208 *retlen += sizeof(*otpinfo);
2209 } else if (from >= groupsize) {
2210 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002211 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002212 } else {
2213 int size = groupsize;
2214 data_offset += from;
2215 size -= from;
2216 from = 0;
2217 if (size > len)
2218 size = len;
2219 ret = action(map, chip, data_offset,
2220 buf, size, reg_prot_offset,
2221 groupno, groupsize);
2222 if (ret < 0)
2223 return ret;
2224 buf += size;
2225 len -= size;
2226 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002227 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002228 }
2229 groupno++;
2230 groups--;
2231 }
2232
2233 /* next OTP region */
2234 if (++field == extp->NumProtectionFields)
2235 break;
2236 reg_prot_offset = otp->ProtRegAddr;
2237 reg_fact_groups = otp->FactGroups;
2238 reg_fact_size = 1 << otp->FactProtRegSize;
2239 reg_user_groups = otp->UserGroups;
2240 reg_user_size = 1 << otp->UserProtRegSize;
2241 otp++;
2242 }
2243 }
2244
2245 return 0;
2246}
2247
2248static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2249 size_t len, size_t *retlen,
2250 u_char *buf)
2251{
2252 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2253 buf, do_otp_read, 0);
2254}
2255
2256static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2257 size_t len, size_t *retlen,
2258 u_char *buf)
2259{
2260 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2261 buf, do_otp_read, 1);
2262}
2263
2264static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2265 size_t len, size_t *retlen,
2266 u_char *buf)
2267{
2268 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2269 buf, do_otp_write, 1);
2270}
2271
2272static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2273 loff_t from, size_t len)
2274{
2275 size_t retlen;
2276 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2277 NULL, do_otp_lock, 1);
2278}
2279
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002280static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002281 struct otp_info *buf, size_t len)
2282{
2283 size_t retlen;
2284 int ret;
2285
2286 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2287 return ret ? : retlen;
2288}
2289
2290static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2291 struct otp_info *buf, size_t len)
2292{
2293 size_t retlen;
2294 int ret;
2295
2296 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2297 return ret ? : retlen;
2298}
2299
2300#endif
2301
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002302static void cfi_intelext_save_locks(struct mtd_info *mtd)
2303{
2304 struct mtd_erase_region_info *region;
2305 int block, status, i;
2306 unsigned long adr;
2307 size_t len;
2308
2309 for (i = 0; i < mtd->numeraseregions; i++) {
2310 region = &mtd->eraseregions[i];
2311 if (!region->lockmap)
2312 continue;
2313
2314 for (block = 0; block < region->numblocks; block++){
2315 len = region->erasesize;
2316 adr = region->offset + block * len;
2317
2318 status = cfi_varsize_frob(mtd,
Ben Dooks029a9eb2007-05-28 20:11:37 +01002319 do_getlockstatus_oneblock, adr, len, NULL);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002320 if (status)
2321 set_bit(block, region->lockmap);
2322 else
2323 clear_bit(block, region->lockmap);
2324 }
2325 }
2326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328static int cfi_intelext_suspend(struct mtd_info *mtd)
2329{
2330 struct map_info *map = mtd->priv;
2331 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002332 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int i;
2334 struct flchip *chip;
2335 int ret = 0;
2336
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002337 if ((mtd->flags & MTD_STUPID_LOCK)
2338 && extp && (extp->FeatureSupport & (1 << 5)))
2339 cfi_intelext_save_locks(mtd);
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 for (i=0; !ret && i<cfi->numchips; i++) {
2342 chip = &cfi->chips[i];
2343
2344 spin_lock(chip->mutex);
2345
2346 switch (chip->state) {
2347 case FL_READY:
2348 case FL_STATUS:
2349 case FL_CFI_QUERY:
2350 case FL_JEDEC_QUERY:
2351 if (chip->oldstate == FL_READY) {
David Andersa86aaa62006-10-19 19:33:19 +03002352 /* place the chip in a known state before suspend */
2353 map_write(map, CMD(0xFF), cfi->chips[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 chip->oldstate = chip->state;
2355 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002356 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * as the whole point is that nobody can do anything
2358 * with the chip now anyway.
2359 */
2360 } else {
2361 /* There seems to be an operation pending. We must wait for it. */
2362 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2363 ret = -EAGAIN;
2364 }
2365 break;
2366 default:
2367 /* Should we actually wait? Once upon a time these routines weren't
2368 allowed to. Or should we return -EAGAIN, because the upper layers
2369 ought to have already shut down anything which was using the device
2370 anyway? The latter for now. */
2371 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2372 ret = -EAGAIN;
2373 case FL_PM_SUSPENDED:
2374 break;
2375 }
2376 spin_unlock(chip->mutex);
2377 }
2378
2379 /* Unlock the chips again */
2380
2381 if (ret) {
2382 for (i--; i >=0; i--) {
2383 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (chip->state == FL_PM_SUSPENDED) {
2388 /* No need to force it into a known state here,
2389 because we're returning failure, and it didn't
2390 get power cycled */
2391 chip->state = chip->oldstate;
2392 chip->oldstate = FL_READY;
2393 wake_up(&chip->wq);
2394 }
2395 spin_unlock(chip->mutex);
2396 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002397 }
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return ret;
2400}
2401
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002402static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2403{
2404 struct mtd_erase_region_info *region;
2405 int block, i;
2406 unsigned long adr;
2407 size_t len;
2408
2409 for (i = 0; i < mtd->numeraseregions; i++) {
2410 region = &mtd->eraseregions[i];
2411 if (!region->lockmap)
2412 continue;
2413
2414 for (block = 0; block < region->numblocks; block++) {
2415 len = region->erasesize;
2416 adr = region->offset + block * len;
2417
2418 if (!test_bit(block, region->lockmap))
2419 cfi_intelext_unlock(mtd, adr, len);
2420 }
2421 }
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424static void cfi_intelext_resume(struct mtd_info *mtd)
2425{
2426 struct map_info *map = mtd->priv;
2427 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002428 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 int i;
2430 struct flchip *chip;
2431
2432 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 chip = &cfi->chips[i];
2435
2436 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* Go to known state. Chip may have been power cycled */
2439 if (chip->state == FL_PM_SUSPENDED) {
2440 map_write(map, CMD(0xFF), cfi->chips[i].start);
2441 chip->oldstate = chip->state = FL_READY;
2442 wake_up(&chip->wq);
2443 }
2444
2445 spin_unlock(chip->mutex);
2446 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002447
2448 if ((mtd->flags & MTD_STUPID_LOCK)
2449 && extp && (extp->FeatureSupport & (1 << 5)))
2450 cfi_intelext_restore_locks(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002453static int cfi_intelext_reset(struct mtd_info *mtd)
2454{
2455 struct map_info *map = mtd->priv;
2456 struct cfi_private *cfi = map->fldrv_priv;
2457 int i, ret;
2458
2459 for (i=0; i < cfi->numchips; i++) {
2460 struct flchip *chip = &cfi->chips[i];
2461
2462 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002463 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002464 flash is accessible for soft reboot. */
2465 spin_lock(chip->mutex);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002466 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002467 if (!ret) {
2468 map_write(map, CMD(0xff), chip->start);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002469 chip->state = FL_SHUTDOWN;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002470 }
2471 spin_unlock(chip->mutex);
2472 }
2473
2474 return 0;
2475}
2476
2477static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2478 void *v)
2479{
2480 struct mtd_info *mtd;
2481
2482 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2483 cfi_intelext_reset(mtd);
2484 return NOTIFY_DONE;
2485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487static void cfi_intelext_destroy(struct mtd_info *mtd)
2488{
2489 struct map_info *map = mtd->priv;
2490 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002491 struct mtd_erase_region_info *region;
2492 int i;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002493 cfi_intelext_reset(mtd);
2494 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 kfree(cfi->cmdset_priv);
2496 kfree(cfi->cfiq);
2497 kfree(cfi->chips[0].priv);
2498 kfree(cfi);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002499 for (i = 0; i < mtd->numeraseregions; i++) {
2500 region = &mtd->eraseregions[i];
2501 if (region->lockmap)
2502 kfree(region->lockmap);
2503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 kfree(mtd->eraseregions);
2505}
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507MODULE_LICENSE("GPL");
2508MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2509MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002510MODULE_ALIAS("cfi_cmdset_0003");
2511MODULE_ALIAS("cfi_cmdset_0200");