blob: cc6b7bb6de02797846f1c4431b0e9a320214f911 [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
Nate Casedeb1a5f2008-05-13 14:45:29 -050053#define M50FLW080A 0x0080
54#define M50FLW080B 0x0081
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +010055#define AT49BV640D 0x02de
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
59static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
Nicolas Pitree102d542005-08-06 05:46:59 +010060static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
62static void cfi_intelext_sync (struct mtd_info *);
63static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
64static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
Todd Poynor8048d2f2005-03-31 00:57:33 +010065#ifdef CONFIG_MTD_OTP
Nicolas Pitref77814d2005-02-08 17:11:19 +000066static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
67static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
68static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
69static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
70static int cfi_intelext_get_fact_prot_info (struct mtd_info *,
71 struct otp_info *, size_t);
72static int cfi_intelext_get_user_prot_info (struct mtd_info *,
73 struct otp_info *, size_t);
Todd Poynor8048d2f2005-03-31 00:57:33 +010074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int cfi_intelext_suspend (struct mtd_info *);
76static void cfi_intelext_resume (struct mtd_info *);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010077static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static void cfi_intelext_destroy(struct mtd_info *);
80
81struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
82
83static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
84static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
85
86static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
Jared Hulberta98889f2008-04-29 23:26:49 -070087 size_t *retlen, void **virt, resource_size_t *phys);
88static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Alexey Korolev5a37cf12007-10-22 17:55:20 +010090static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
92static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
93#include "fwh_lock.h"
94
95
96
97/*
98 * *********** SETUP AND PROBE BITS ***********
99 */
100
101static struct mtd_chip_driver cfi_intelext_chipdrv = {
102 .probe = NULL, /* Not usable directly */
103 .destroy = cfi_intelext_destroy,
104 .name = "cfi_cmdset_0001",
105 .module = THIS_MODULE
106};
107
108/* #define DEBUG_LOCK_BITS */
109/* #define DEBUG_CFI_FEATURES */
110
111#ifdef DEBUG_CFI_FEATURES
112static void cfi_tell_features(struct cfi_pri_intelext *extp)
113{
114 int i;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100115 printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
117 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
118 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
119 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
120 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
121 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
122 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
123 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
124 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
125 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
126 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100127 printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
128 for (i=11; i<32; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000129 if (extp->FeatureSupport & (1<<i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printk(" - Unknown Bit %X: supported\n", i);
131 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
134 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
135 for (i=1; i<8; i++) {
136 if (extp->SuspendCmdSupport & (1<<i))
137 printk(" - Unknown Bit %X: supported\n", i);
138 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
141 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100142 printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
143 for (i=2; i<3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (extp->BlkStatusRegMask & (1<<i))
145 printk(" - Unknown Bit %X Active: yes\n",i);
146 }
Nicolas Pitre638d9832005-08-06 05:40:46 +0100147 printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
148 printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
149 for (i=6; i<16; i++) {
150 if (extp->BlkStatusRegMask & (1<<i))
151 printk(" - Unknown Bit %X Active: yes\n",i);
152 }
153
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000154 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
156 if (extp->VppOptimal)
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000157 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
159}
160#endif
161
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100162/* Atmel chips don't use the same PRI format as Intel chips */
163static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
164{
165 struct map_info *map = mtd->priv;
166 struct cfi_private *cfi = map->fldrv_priv;
167 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
168 struct cfi_pri_atmel atmel_pri;
169 uint32_t features = 0;
170
171 /* Reverse byteswapping */
172 extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
173 extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
174 extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
175
176 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
177 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
178
179 printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
180
181 if (atmel_pri.Features & 0x01) /* chip erase supported */
182 features |= (1<<0);
183 if (atmel_pri.Features & 0x02) /* erase suspend supported */
184 features |= (1<<1);
185 if (atmel_pri.Features & 0x04) /* program suspend supported */
186 features |= (1<<2);
187 if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
188 features |= (1<<9);
189 if (atmel_pri.Features & 0x20) /* page mode read supported */
190 features |= (1<<7);
191 if (atmel_pri.Features & 0x40) /* queued erase supported */
192 features |= (1<<4);
193 if (atmel_pri.Features & 0x80) /* Protection bits supported */
194 features |= (1<<6);
195
196 extp->FeatureSupport = features;
197
198 /* burst write mode not supported */
199 cfi->cfiq->BufWriteTimeoutTyp = 0;
200 cfi->cfiq->BufWriteTimeoutMax = 0;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000204/* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static void fixup_intel_strataflash(struct mtd_info *mtd, void* param)
206{
207 struct map_info *map = mtd->priv;
208 struct cfi_private *cfi = map->fldrv_priv;
Alexander Belyakov91949d62008-05-04 14:32:58 +0400209 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
212 "erase on write disabled.\n");
213 extp->SuspendCmdSupport &= ~1;
214}
215#endif
216
217#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
218static void fixup_no_write_suspend(struct mtd_info *mtd, void* param)
219{
220 struct map_info *map = mtd->priv;
221 struct cfi_private *cfi = map->fldrv_priv;
222 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
223
224 if (cfip && (cfip->FeatureSupport&4)) {
225 cfip->FeatureSupport &= ~4;
226 printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
227 }
228}
229#endif
230
231static void fixup_st_m28w320ct(struct mtd_info *mtd, void* param)
232{
233 struct map_info *map = mtd->priv;
234 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
237 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
238}
239
240static void fixup_st_m28w320cb(struct mtd_info *mtd, void* param)
241{
242 struct map_info *map = mtd->priv;
243 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Note this is done after the region info is endian swapped */
246 cfi->cfiq->EraseRegionInfo[1] =
247 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
248};
249
250static void fixup_use_point(struct mtd_info *mtd, void *param)
251{
252 struct map_info *map = mtd->priv;
253 if (!mtd->point && map_is_linear(map)) {
254 mtd->point = cfi_intelext_point;
255 mtd->unpoint = cfi_intelext_unpoint;
256 }
257}
258
259static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
260{
261 struct map_info *map = mtd->priv;
262 struct cfi_private *cfi = map->fldrv_priv;
263 if (cfi->cfiq->BufWriteTimeoutTyp) {
264 printk(KERN_INFO "Using buffer write method\n" );
265 mtd->write = cfi_intelext_write_buffers;
Nicolas Pitree102d542005-08-06 05:46:59 +0100266 mtd->writev = cfi_intelext_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268}
269
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800270/*
271 * Some chips power-up with all sectors locked by default.
272 */
Justin Treone619a752008-01-30 10:25:49 -0800273static void fixup_unlock_powerup_lock(struct mtd_info *mtd, void *param)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800274{
Justin Treone619a752008-01-30 10:25:49 -0800275 struct map_info *map = mtd->priv;
276 struct cfi_private *cfi = map->fldrv_priv;
277 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
278
279 if (cfip->FeatureSupport&32) {
280 printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
281 mtd->flags |= MTD_POWERUP_LOCK;
282 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100286 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000288 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#endif
290#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
291 { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend, NULL },
292#endif
293#if !FORCE_WORD_WRITE
294 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL },
295#endif
296 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
297 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
Justin Treone619a752008-01-30 10:25:49 -0800298 { MANUFACTURER_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 { 0, 0, NULL, NULL }
300};
301
302static struct cfi_fixup jedec_fixup_table[] = {
303 { MANUFACTURER_INTEL, I82802AB, fixup_use_fwh_lock, NULL, },
304 { MANUFACTURER_INTEL, I82802AC, fixup_use_fwh_lock, NULL, },
305 { MANUFACTURER_ST, M50LPW080, fixup_use_fwh_lock, NULL, },
Nate Casedeb1a5f2008-05-13 14:45:29 -0500306 { MANUFACTURER_ST, M50FLW080A, fixup_use_fwh_lock, NULL, },
307 { MANUFACTURER_ST, M50FLW080B, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 { 0, 0, NULL, NULL }
309};
310static struct cfi_fixup fixup_table[] = {
311 /* The CFI vendor ids and the JEDEC vendor IDs appear
312 * to be common. It is like the devices id's are as
313 * well. This table is to pick all cases where
314 * we know that is the case.
315 */
316 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point, NULL },
317 { 0, 0, NULL, NULL }
318};
319
320static inline struct cfi_pri_intelext *
321read_pri_intelext(struct map_info *map, __u16 adr)
322{
323 struct cfi_pri_intelext *extp;
324 unsigned int extp_size = sizeof(*extp);
325
326 again:
327 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
328 if (!extp)
329 return NULL;
330
Todd Poynord88f9772005-07-20 22:01:17 +0100331 if (extp->MajorVersion != '1' ||
Alexey Korolevb1c9c9b2007-11-23 09:31:56 +0000332 (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
Todd Poynord88f9772005-07-20 22:01:17 +0100333 printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
334 "version %c.%c.\n", extp->MajorVersion,
335 extp->MinorVersion);
336 kfree(extp);
337 return NULL;
338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Do some byteswapping if necessary */
341 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
342 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
343 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
344
Nicolas Pitre638d9832005-08-06 05:40:46 +0100345 if (extp->MajorVersion == '1' && extp->MinorVersion >= '3') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned int extra_size = 0;
347 int nb_parts, i;
348
349 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000350 extra_size += (extp->NumProtectionFields - 1) *
351 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100354 extra_size += 2;
355 if (extp_size < sizeof(*extp) + extra_size)
356 goto need_more;
357 extra_size += extp->extra[extra_size-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* Number of hardware-partitions */
360 extra_size += 1;
361 if (extp_size < sizeof(*extp) + extra_size)
362 goto need_more;
363 nb_parts = extp->extra[extra_size - 1];
364
Nicolas Pitre638d9832005-08-06 05:40:46 +0100365 /* skip the sizeof(partregion) field in CFI 1.4 */
366 if (extp->MinorVersion >= '4')
367 extra_size += 2;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 for (i = 0; i < nb_parts; i++) {
370 struct cfi_intelext_regioninfo *rinfo;
371 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
372 extra_size += sizeof(*rinfo);
373 if (extp_size < sizeof(*extp) + extra_size)
374 goto need_more;
375 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
376 extra_size += (rinfo->NumBlockTypes - 1)
377 * sizeof(struct cfi_intelext_blockinfo);
378 }
379
Nicolas Pitre638d9832005-08-06 05:40:46 +0100380 if (extp->MinorVersion >= '4')
381 extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (extp_size < sizeof(*extp) + extra_size) {
384 need_more:
385 extp_size = sizeof(*extp) + extra_size;
386 kfree(extp);
387 if (extp_size > 4096) {
388 printk(KERN_ERR
389 "%s: cfi_pri_intelext is too fat\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700390 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return NULL;
392 }
393 goto again;
394 }
395 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return extp;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
401{
402 struct cfi_private *cfi = map->fldrv_priv;
403 struct mtd_info *mtd;
404 int i;
405
Burman Yan95b93a02006-11-15 21:10:29 +0200406 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!mtd) {
408 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
409 return NULL;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 mtd->priv = map;
412 mtd->type = MTD_NORFLASH;
413
414 /* Fill in the default mtd operations */
415 mtd->erase = cfi_intelext_erase_varsize;
416 mtd->read = cfi_intelext_read;
417 mtd->write = cfi_intelext_write_words;
418 mtd->sync = cfi_intelext_sync;
419 mtd->lock = cfi_intelext_lock;
420 mtd->unlock = cfi_intelext_unlock;
421 mtd->suspend = cfi_intelext_suspend;
422 mtd->resume = cfi_intelext_resume;
423 mtd->flags = MTD_CAP_NORFLASH;
424 mtd->name = map->name;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400425 mtd->writesize = 1;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100426
427 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (cfi->cfi_mode == CFI_MODE_CFI) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000430 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * It's a real CFI chip, not one for which the probe
432 * routine faked a CFI structure. So we read the feature
433 * table from it.
434 */
435 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
436 struct cfi_pri_intelext *extp;
437
438 extp = read_pri_intelext(map, adr);
439 if (!extp) {
440 kfree(mtd);
441 return NULL;
442 }
443
444 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000445 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 cfi_fixup(mtd, cfi_fixup_table);
448
449#ifdef DEBUG_CFI_FEATURES
450 /* Tell the user about it in lots of lovely detail */
451 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if(extp->SuspendCmdSupport & 1) {
455 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
456 }
457 }
458 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
459 /* Apply jedec specific fixups */
460 cfi_fixup(mtd, jedec_fixup_table);
461 }
462 /* Apply generic fixups */
463 cfi_fixup(mtd, fixup_table);
464
465 for (i=0; i< cfi->numchips; i++) {
David Woodhouse2a5bd592007-02-09 14:39:10 +0000466 if (cfi->cfiq->WordWriteTimeoutTyp)
467 cfi->chips[i].word_write_time =
468 1<<cfi->cfiq->WordWriteTimeoutTyp;
469 else
470 cfi->chips[i].word_write_time = 50000;
471
472 if (cfi->cfiq->BufWriteTimeoutTyp)
473 cfi->chips[i].buffer_write_time =
474 1<<cfi->cfiq->BufWriteTimeoutTyp;
475 /* No default; if it isn't specified, we won't use it */
476
477 if (cfi->cfiq->BlockEraseTimeoutTyp)
478 cfi->chips[i].erase_time =
479 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
480 else
481 cfi->chips[i].erase_time = 2000000;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 cfi->chips[i].ref_point_counter = 0;
Simon Voglc314b6f2006-02-24 13:04:09 -0800484 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 map->fldrv = &cfi_intelext_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return cfi_intelext_setup(mtd);
490}
David Woodhousea15bdee2006-05-08 22:35:05 +0100491struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
492struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
493EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
494EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
495EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
498{
499 struct map_info *map = mtd->priv;
500 struct cfi_private *cfi = map->fldrv_priv;
501 unsigned long offset = 0;
502 int i,j;
503 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
504
505 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
506
507 mtd->size = devsize * cfi->numchips;
508
509 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000510 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000512 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
514 goto setup_err;
515 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
518 unsigned long ernum, ersize;
519 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
520 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
521
522 if (mtd->erasesize < ersize) {
523 mtd->erasesize = ersize;
524 }
525 for (j=0; j<cfi->numchips; j++) {
526 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
527 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
528 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800529 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 offset += (ersize * ernum);
532 }
533
534 if (offset != devsize) {
535 /* Argh */
536 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
537 goto setup_err;
538 }
539
540 for (i=0; i<mtd->numeraseregions;i++){
Nicolas Pitre48436532005-08-06 05:16:52 +0100541 printk(KERN_DEBUG "erase region %d: offset=0x%x,size=0x%x,blocks=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 i,mtd->eraseregions[i].offset,
543 mtd->eraseregions[i].erasesize,
544 mtd->eraseregions[i].numblocks);
545 }
546
Nicolas Pitref77814d2005-02-08 17:11:19 +0000547#ifdef CONFIG_MTD_OTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000549 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
550 mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
551 mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
552 mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
553 mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555
556 /* This function has the potential to distort the reality
557 a bit and therefore should be called last. */
558 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
559 goto setup_err;
560
561 __module_get(THIS_MODULE);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100562 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return mtd;
564
565 setup_err:
566 if(mtd) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800567 kfree(mtd->eraseregions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 kfree(mtd);
569 }
570 kfree(cfi->cmdset_priv);
571 return NULL;
572}
573
574static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
575 struct cfi_private **pcfi)
576{
577 struct map_info *map = mtd->priv;
578 struct cfi_private *cfi = *pcfi;
579 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
580
581 /*
Jesper Juhl8f1a8662007-07-05 02:18:34 +0200582 * Probing of multi-partition flash chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
584 * To support multiple partitions when available, we simply arrange
585 * for each of them to have their own flchip structure even if they
586 * are on the same physical chip. This means completely recreating
587 * a new cfi_private structure right here which is a blatent code
588 * layering violation, but this is still the least intrusive
589 * arrangement at this point. This can be rearranged in the future
590 * if someone feels motivated enough. --nico
591 */
Nicolas Pitre638d9832005-08-06 05:40:46 +0100592 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 && extp->FeatureSupport & (1 << 9)) {
594 struct cfi_private *newcfi;
595 struct flchip *chip;
596 struct flchip_shared *shared;
597 int offs, numregions, numparts, partshift, numvirtchips, i, j;
598
599 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000600 offs = (extp->NumProtectionFields - 1) *
601 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100604 offs += extp->extra[offs+1]+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Number of partition regions */
607 numregions = extp->extra[offs];
608 offs += 1;
609
Nicolas Pitre638d9832005-08-06 05:40:46 +0100610 /* skip the sizeof(partregion) field in CFI 1.4 */
611 if (extp->MinorVersion >= '4')
612 offs += 2;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Number of hardware partitions */
615 numparts = 0;
616 for (i = 0; i < numregions; i++) {
617 struct cfi_intelext_regioninfo *rinfo;
618 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
619 numparts += rinfo->NumIdentPartitions;
620 offs += sizeof(*rinfo)
621 + (rinfo->NumBlockTypes - 1) *
622 sizeof(struct cfi_intelext_blockinfo);
623 }
624
Thomas Kunzefe224662008-04-23 01:40:52 +0200625 if (!numparts)
626 numparts = 1;
627
Nicolas Pitre638d9832005-08-06 05:40:46 +0100628 /* Programming Region info */
629 if (extp->MinorVersion >= '4') {
630 struct cfi_intelext_programming_regioninfo *prinfo;
631 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
Joern Engel28318772006-05-22 23:18:05 +0200632 mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
Joern Engel5fa43392006-05-22 23:18:29 +0200633 mtd->flags &= ~MTD_BIT_WRITEABLE;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100634 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
Joern Engel28318772006-05-22 23:18:05 +0200635 map->name, mtd->writesize,
Artem Bityutskiyd4160852007-01-30 10:45:55 +0200636 cfi->interleave * prinfo->ControlValid,
637 cfi->interleave * prinfo->ControlInvalid);
Nicolas Pitre638d9832005-08-06 05:40:46 +0100638 }
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * All functions below currently rely on all chips having
642 * the same geometry so we'll just assume that all hardware
643 * partitions are of the same size too.
644 */
645 partshift = cfi->chipshift - __ffs(numparts);
646
647 if ((1 << partshift) < mtd->erasesize) {
648 printk( KERN_ERR
649 "%s: bad number of hw partitions (%d)\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700650 __func__, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -EINVAL;
652 }
653
654 numvirtchips = cfi->numchips * numparts;
655 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
656 if (!newcfi)
657 return -ENOMEM;
658 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
659 if (!shared) {
660 kfree(newcfi);
661 return -ENOMEM;
662 }
663 memcpy(newcfi, cfi, sizeof(struct cfi_private));
664 newcfi->numchips = numvirtchips;
665 newcfi->chipshift = partshift;
666
667 chip = &newcfi->chips[0];
668 for (i = 0; i < cfi->numchips; i++) {
669 shared[i].writing = shared[i].erasing = NULL;
670 spin_lock_init(&shared[i].lock);
671 for (j = 0; j < numparts; j++) {
672 *chip = cfi->chips[i];
673 chip->start += j << partshift;
674 chip->priv = &shared[i];
675 /* those should be reset too since
676 they create memory references. */
677 init_waitqueue_head(&chip->wq);
678 spin_lock_init(&chip->_spinlock);
679 chip->mutex = &chip->_spinlock;
680 chip++;
681 }
682 }
683
684 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
685 "--> %d partitions of %d KiB\n",
686 map->name, cfi->numchips, cfi->interleave,
687 newcfi->numchips, 1<<(newcfi->chipshift-10));
688
689 map->fldrv_priv = newcfi;
690 *pcfi = newcfi;
691 kfree(cfi);
692 }
693
694 return 0;
695}
696
697/*
698 * *********** CHIP ACCESS FUNCTIONS ***********
699 */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100700static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 DECLARE_WAITQUEUE(wait, current);
703 struct cfi_private *cfi = map->fldrv_priv;
704 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100706 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 switch (chip->state) {
709
710 case FL_STATUS:
711 for (;;) {
712 status = map_read(map, adr);
713 if (map_word_andequal(map, status, status_OK, status_OK))
714 break;
715
716 /* At this point we're fine with write operations
717 in other partitions as they don't conflict. */
718 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
719 break;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 spin_unlock(chip->mutex);
722 cfi_udelay(1);
723 spin_lock(chip->mutex);
724 /* Someone else might have been playing with it. */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100725 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700727 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 case FL_READY:
729 case FL_CFI_QUERY:
730 case FL_JEDEC_QUERY:
731 return 0;
732
733 case FL_ERASING:
734 if (!cfip ||
735 !(cfip->FeatureSupport & 2) ||
736 !(mode == FL_READY || mode == FL_POINT ||
737 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
738 goto sleep;
739
740
741 /* Erase suspend */
742 map_write(map, CMD(0xB0), adr);
743
744 /* If the flash has finished erasing, then 'erase suspend'
745 * appears to make some (28F320) flash devices switch to
746 * 'read' mode. Make sure that we switch to 'read status'
747 * mode so we get the right data. --rmk
748 */
749 map_write(map, CMD(0x70), adr);
750 chip->oldstate = FL_ERASING;
751 chip->state = FL_ERASE_SUSPENDING;
752 chip->erase_suspended = 1;
753 for (;;) {
754 status = map_read(map, adr);
755 if (map_word_andequal(map, status, status_OK, status_OK))
756 break;
757
758 if (time_after(jiffies, timeo)) {
759 /* Urgh. Resume and pretend we weren't here. */
760 map_write(map, CMD(0xd0), adr);
761 /* Make sure we're in 'read status' mode if it had finished */
762 map_write(map, CMD(0x70), adr);
763 chip->state = FL_ERASING;
764 chip->oldstate = FL_READY;
Nicolas Pitre48436532005-08-06 05:16:52 +0100765 printk(KERN_ERR "%s: Chip not ready after erase "
766 "suspended: status = 0x%lx\n", map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EIO;
768 }
769
770 spin_unlock(chip->mutex);
771 cfi_udelay(1);
772 spin_lock(chip->mutex);
773 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
774 So we can just loop here. */
775 }
776 chip->state = FL_STATUS;
777 return 0;
778
779 case FL_XIP_WHILE_ERASING:
780 if (mode != FL_READY && mode != FL_POINT &&
781 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
782 goto sleep;
783 chip->oldstate = chip->state;
784 chip->state = FL_READY;
785 return 0;
786
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700787 case FL_SHUTDOWN:
788 /* The machine is rebooting now,so no one can get chip anymore */
789 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case FL_POINT:
791 /* Only if there's no operation suspended... */
792 if (mode == FL_READY && chip->oldstate == FL_READY)
793 return 0;
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700794 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 default:
796 sleep:
797 set_current_state(TASK_UNINTERRUPTIBLE);
798 add_wait_queue(&chip->wq, &wait);
799 spin_unlock(chip->mutex);
800 schedule();
801 remove_wait_queue(&chip->wq, &wait);
802 spin_lock(chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100803 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805}
806
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100807static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
808{
809 int ret;
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300810 DECLARE_WAITQUEUE(wait, current);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100811
812 retry:
813 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING
814 || mode == FL_OTP_WRITE || mode == FL_SHUTDOWN)) {
815 /*
816 * OK. We have possibility for contention on the write/erase
817 * operations which are global to the real chip and not per
818 * partition. So let's fight it over in the partition which
819 * currently has authority on the operation.
820 *
821 * The rules are as follows:
822 *
823 * - any write operation must own shared->writing.
824 *
825 * - any erase operation must own _both_ shared->writing and
826 * shared->erasing.
827 *
828 * - contention arbitration is handled in the owner's context.
829 *
830 * The 'shared' struct can be read and/or written only when
831 * its lock is taken.
832 */
833 struct flchip_shared *shared = chip->priv;
834 struct flchip *contender;
835 spin_lock(&shared->lock);
836 contender = shared->writing;
837 if (contender && contender != chip) {
838 /*
839 * The engine to perform desired operation on this
840 * partition is already in use by someone else.
841 * Let's fight over it in the context of the chip
842 * currently using it. If it is possible to suspend,
843 * that other partition will do just that, otherwise
844 * it'll happily send us to sleep. In any case, when
845 * get_chip returns success we're clear to go ahead.
846 */
847 ret = spin_trylock(contender->mutex);
848 spin_unlock(&shared->lock);
849 if (!ret)
850 goto retry;
851 spin_unlock(chip->mutex);
852 ret = chip_ready(map, contender, contender->start, mode);
853 spin_lock(chip->mutex);
854
855 if (ret == -EAGAIN) {
856 spin_unlock(contender->mutex);
857 goto retry;
858 }
859 if (ret) {
860 spin_unlock(contender->mutex);
861 return ret;
862 }
863 spin_lock(&shared->lock);
864 spin_unlock(contender->mutex);
865 }
866
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300867 /* Check if we already have suspended erase
868 * on this chip. Sleep. */
869 if (mode == FL_ERASING && shared->erasing
870 && shared->erasing->oldstate == FL_ERASING) {
871 spin_unlock(&shared->lock);
872 set_current_state(TASK_UNINTERRUPTIBLE);
873 add_wait_queue(&chip->wq, &wait);
874 spin_unlock(chip->mutex);
875 schedule();
876 remove_wait_queue(&chip->wq, &wait);
877 spin_lock(chip->mutex);
878 goto retry;
879 }
880
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100881 /* We now own it */
882 shared->writing = chip;
883 if (mode == FL_ERASING)
884 shared->erasing = chip;
885 spin_unlock(&shared->lock);
886 }
887 ret = chip_ready(map, chip, adr, mode);
888 if (ret == -EAGAIN)
889 goto retry;
890
891 return ret;
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
895{
896 struct cfi_private *cfi = map->fldrv_priv;
897
898 if (chip->priv) {
899 struct flchip_shared *shared = chip->priv;
900 spin_lock(&shared->lock);
901 if (shared->writing == chip && chip->oldstate == FL_READY) {
902 /* We own the ability to write, but we're done */
903 shared->writing = shared->erasing;
904 if (shared->writing && shared->writing != chip) {
905 /* give back ownership to who we loaned it from */
906 struct flchip *loaner = shared->writing;
907 spin_lock(loaner->mutex);
908 spin_unlock(&shared->lock);
909 spin_unlock(chip->mutex);
910 put_chip(map, loaner, loaner->start);
911 spin_lock(chip->mutex);
912 spin_unlock(loaner->mutex);
913 wake_up(&chip->wq);
914 return;
915 }
916 shared->erasing = NULL;
917 shared->writing = NULL;
918 } else if (shared->erasing == chip && shared->writing != chip) {
919 /*
920 * We own the ability to erase without the ability
921 * to write, which means the erase was suspended
922 * and some other partition is currently writing.
923 * Don't let the switch below mess things up since
924 * we don't have ownership to resume anything.
925 */
926 spin_unlock(&shared->lock);
927 wake_up(&chip->wq);
928 return;
929 }
930 spin_unlock(&shared->lock);
931 }
932
933 switch(chip->oldstate) {
934 case FL_ERASING:
935 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000936 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000938 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 errors to be returned from do_erase_oneblock because
940 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000941 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000943 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 do. */
945 map_write(map, CMD(0xd0), adr);
946 map_write(map, CMD(0x70), adr);
947 chip->oldstate = FL_READY;
948 chip->state = FL_ERASING;
949 break;
950
951 case FL_XIP_WHILE_ERASING:
952 chip->state = chip->oldstate;
953 chip->oldstate = FL_READY;
954 break;
955
956 case FL_READY:
957 case FL_STATUS:
958 case FL_JEDEC_QUERY:
959 /* We should really make set_vpp() count, rather than doing this */
960 DISABLE_VPP(map);
961 break;
962 default:
Nicolas Pitre48436532005-08-06 05:16:52 +0100963 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 wake_up(&chip->wq);
966}
967
968#ifdef CONFIG_MTD_XIP
969
970/*
971 * No interrupt what so ever can be serviced while the flash isn't in array
972 * mode. This is ensured by the xip_disable() and xip_enable() functions
973 * enclosing any code path where the flash is known not to be in array mode.
974 * And within a XIP disabled code path, only functions marked with __xipram
975 * may be called and nothing else (it's a good thing to inspect generated
976 * assembly to make sure inline functions were actually inlined and that gcc
977 * didn't emit calls to its own support functions). Also configuring MTD CFI
978 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
980
981static void xip_disable(struct map_info *map, struct flchip *chip,
982 unsigned long adr)
983{
984 /* TODO: chips with no XIP use should ignore and return */
985 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 local_irq_disable();
987}
988
989static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
990 unsigned long adr)
991{
992 struct cfi_private *cfi = map->fldrv_priv;
993 if (chip->state != FL_POINT && chip->state != FL_READY) {
994 map_write(map, CMD(0xff), adr);
995 chip->state = FL_READY;
996 }
997 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200998 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002/*
1003 * When a delay is required for the flash operation to complete, the
Nicolas Pitrec1724712006-03-30 15:52:41 +01001004 * xip_wait_for_operation() function is polling for both the given timeout
1005 * and pending (but still masked) hardware interrupts. Whenever there is an
1006 * interrupt pending then the flash erase or write operation is suspended,
1007 * array mode restored and interrupts unmasked. Task scheduling might also
1008 * happen at that point. The CPU eventually returns from the interrupt or
1009 * the call to schedule() and the suspended flash operation is resumed for
1010 * the remaining of the delay period.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 *
1012 * Warning: this function _will_ fool interrupt latency tracing tools.
1013 */
1014
Nicolas Pitrec1724712006-03-30 15:52:41 +01001015static int __xipram xip_wait_for_operation(
1016 struct map_info *map, struct flchip *chip,
Alexey Korolev46a16522006-06-28 19:22:07 +01001017 unsigned long adr, unsigned int chip_op_time )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct cfi_private *cfi = map->fldrv_priv;
1020 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
1021 map_word status, OK = CMD(0x80);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001022 unsigned long usec, suspended, start, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 flstate_t oldstate, newstate;
1024
Nicolas Pitrec1724712006-03-30 15:52:41 +01001025 start = xip_currtime();
Alexey Korolev46a16522006-06-28 19:22:07 +01001026 usec = chip_op_time * 8;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001027 if (usec == 0)
1028 usec = 500000;
1029 done = 0;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 do {
1032 cpu_relax();
1033 if (xip_irqpending() && cfip &&
1034 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1035 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1036 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1037 /*
1038 * Let's suspend the erase or write operation when
1039 * supported. Note that we currently don't try to
1040 * suspend interleaved chips if there is already
1041 * another operation suspended (imagine what happens
1042 * when one chip was already done with the current
1043 * operation while another chip suspended it, then
1044 * we resume the whole thing at once). Yes, it
1045 * can happen!
1046 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001047 usec -= done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 map_write(map, CMD(0xb0), adr);
1049 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 suspended = xip_currtime();
1051 do {
1052 if (xip_elapsed_since(suspended) > 100000) {
1053 /*
1054 * The chip doesn't want to suspend
1055 * after waiting for 100 msecs.
1056 * This is a critical error but there
1057 * is not much we can do here.
1058 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001059 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 status = map_read(map, adr);
1062 } while (!map_word_andequal(map, status, OK, OK));
1063
1064 /* Suspend succeeded */
1065 oldstate = chip->state;
1066 if (oldstate == FL_ERASING) {
1067 if (!map_word_bitsset(map, status, CMD(0x40)))
1068 break;
1069 newstate = FL_XIP_WHILE_ERASING;
1070 chip->erase_suspended = 1;
1071 } else {
1072 if (!map_word_bitsset(map, status, CMD(0x04)))
1073 break;
1074 newstate = FL_XIP_WHILE_WRITING;
1075 chip->write_suspended = 1;
1076 }
1077 chip->state = newstate;
1078 map_write(map, CMD(0xff), adr);
1079 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001080 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 local_irq_enable();
Nicolas Pitre6da70122005-05-19 18:05:47 +01001082 spin_unlock(chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001083 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 cond_resched();
1085
1086 /*
1087 * We're back. However someone else might have
1088 * decided to go write to the chip if we are in
1089 * a suspended erase state. If so let's wait
1090 * until it's done.
1091 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001092 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 while (chip->state != newstate) {
1094 DECLARE_WAITQUEUE(wait, current);
1095 set_current_state(TASK_UNINTERRUPTIBLE);
1096 add_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001097 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 schedule();
1099 remove_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001100 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102 /* Disallow XIP again */
1103 local_irq_disable();
1104
1105 /* Resume the write or erase operation */
1106 map_write(map, CMD(0xd0), adr);
1107 map_write(map, CMD(0x70), adr);
1108 chip->state = oldstate;
1109 start = xip_currtime();
1110 } else if (usec >= 1000000/HZ) {
1111 /*
1112 * Try to save on CPU power when waiting delay
1113 * is at least a system timer tick period.
1114 * No need to be extremely accurate here.
1115 */
1116 xip_cpu_idle();
1117 }
1118 status = map_read(map, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001119 done = xip_elapsed_since(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 } while (!map_word_andequal(map, status, OK, OK)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001121 && done < usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Nicolas Pitrec1724712006-03-30 15:52:41 +01001123 return (done >= usec) ? -ETIME : 0;
1124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126/*
1127 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1128 * the flash is actively programming or erasing since we have to poll for
1129 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001130 * a XIP setup so do it before the actual flash operation in this case
Nicolas Pitrec1724712006-03-30 15:52:41 +01001131 * and stub it out from INVAL_CACHE_AND_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001133#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1134 INVALIDATE_CACHED_RANGE(map, from, size)
1135
Alexey Korolev46a16522006-06-28 19:22:07 +01001136#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec) \
1137 xip_wait_for_operation(map, chip, cmd_adr, usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139#else
1140
1141#define xip_disable(map, chip, adr)
1142#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143#define XIP_INVAL_CACHED_RANGE(x...)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001144#define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Nicolas Pitrec1724712006-03-30 15:52:41 +01001146static int inval_cache_and_wait_for_operation(
1147 struct map_info *map, struct flchip *chip,
1148 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001149 unsigned int chip_op_time)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001150{
1151 struct cfi_private *cfi = map->fldrv_priv;
1152 map_word status, status_OK = CMD(0x80);
Alexey Korolev46a16522006-06-28 19:22:07 +01001153 int chip_state = chip->state;
1154 unsigned int timeo, sleep_time;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001155
Nicolas Pitrec1724712006-03-30 15:52:41 +01001156 spin_unlock(chip->mutex);
1157 if (inval_len)
1158 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001159 spin_lock(chip->mutex);
1160
Alexey Korolev46a16522006-06-28 19:22:07 +01001161 /* set our timeout to 8 times the expected delay */
1162 timeo = chip_op_time * 8;
1163 if (!timeo)
1164 timeo = 500000;
1165 sleep_time = chip_op_time / 2;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001166
Nicolas Pitrec1724712006-03-30 15:52:41 +01001167 for (;;) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001168 status = map_read(map, cmd_adr);
1169 if (map_word_andequal(map, status, status_OK, status_OK))
1170 break;
1171
Alexey Korolev46a16522006-06-28 19:22:07 +01001172 if (!timeo) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001173 map_write(map, CMD(0x70), cmd_adr);
1174 chip->state = FL_STATUS;
1175 return -ETIME;
1176 }
1177
Alexey Korolev46a16522006-06-28 19:22:07 +01001178 /* OK Still waiting. Drop the lock, wait a while and retry. */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001179 spin_unlock(chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001180 if (sleep_time >= 1000000/HZ) {
1181 /*
1182 * Half of the normal delay still remaining
1183 * can be performed with a sleeping delay instead
1184 * of busy waiting.
1185 */
1186 msleep(sleep_time/1000);
1187 timeo -= sleep_time;
1188 sleep_time = 1000000/HZ;
1189 } else {
1190 udelay(1);
1191 cond_resched();
1192 timeo--;
1193 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001194 spin_lock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001195
Joakim Tjernlund967bf622006-11-28 23:11:52 +00001196 while (chip->state != chip_state) {
Alexey Korolev46a16522006-06-28 19:22:07 +01001197 /* Someone's suspended the operation: sleep */
1198 DECLARE_WAITQUEUE(wait, current);
1199 set_current_state(TASK_UNINTERRUPTIBLE);
1200 add_wait_queue(&chip->wq, &wait);
1201 spin_unlock(chip->mutex);
1202 schedule();
1203 remove_wait_queue(&chip->wq, &wait);
1204 spin_lock(chip->mutex);
1205 }
1206 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001207
1208 /* Done and happy. */
1209 chip->state = FL_STATUS;
1210 return 0;
1211}
Nicolas Pitre6da70122005-05-19 18:05:47 +01001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#endif
1214
Nicolas Pitrec1724712006-03-30 15:52:41 +01001215#define WAIT_TIMEOUT(map, chip, adr, udelay) \
Alexey Korolev46a16522006-06-28 19:22:07 +01001216 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001217
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1220{
1221 unsigned long cmd_addr;
1222 struct cfi_private *cfi = map->fldrv_priv;
1223 int ret = 0;
1224
1225 adr += chip->start;
1226
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001227 /* Ensure cmd read/writes are aligned. */
1228 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 spin_lock(chip->mutex);
1231
1232 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1233
1234 if (!ret) {
1235 if (chip->state != FL_POINT && chip->state != FL_READY)
1236 map_write(map, CMD(0xff), cmd_addr);
1237
1238 chip->state = FL_POINT;
1239 chip->ref_point_counter++;
1240 }
1241 spin_unlock(chip->mutex);
1242
1243 return ret;
1244}
1245
Jared Hulberta98889f2008-04-29 23:26:49 -07001246static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
1247 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct map_info *map = mtd->priv;
1250 struct cfi_private *cfi = map->fldrv_priv;
Andy Lowe097f2572007-01-12 18:05:10 -05001251 unsigned long ofs, last_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int chipnum;
1253 int ret = 0;
1254
1255 if (!map->virt || (from + len > mtd->size))
1256 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Now lock the chip(s) to POINT state */
1259
1260 /* ofs: offset within the first chip that the first read should start */
1261 chipnum = (from >> cfi->chipshift);
1262 ofs = from - (chipnum << cfi->chipshift);
1263
Jared Hulberta98889f2008-04-29 23:26:49 -07001264 *virt = map->virt + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001265 *retlen = 0;
Jared Hulberta98889f2008-04-29 23:26:49 -07001266 if (phys)
1267 *phys = map->phys + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 while (len) {
1270 unsigned long thislen;
1271
1272 if (chipnum >= cfi->numchips)
1273 break;
1274
Andy Lowe097f2572007-01-12 18:05:10 -05001275 /* We cannot point across chips that are virtually disjoint */
1276 if (!last_end)
1277 last_end = cfi->chips[chipnum].start;
1278 else if (cfi->chips[chipnum].start != last_end)
1279 break;
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if ((len + ofs -1) >> cfi->chipshift)
1282 thislen = (1<<cfi->chipshift) - ofs;
1283 else
1284 thislen = len;
1285
1286 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1287 if (ret)
1288 break;
1289
1290 *retlen += thislen;
1291 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ofs = 0;
Andy Lowe097f2572007-01-12 18:05:10 -05001294 last_end += 1 << cfi->chipshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 chipnum++;
1296 }
1297 return 0;
1298}
1299
Jared Hulberta98889f2008-04-29 23:26:49 -07001300static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct map_info *map = mtd->priv;
1303 struct cfi_private *cfi = map->fldrv_priv;
1304 unsigned long ofs;
1305 int chipnum;
1306
1307 /* Now unlock the chip(s) POINT state */
1308
1309 /* ofs: offset within the first chip that the first read should start */
1310 chipnum = (from >> cfi->chipshift);
1311 ofs = from - (chipnum << cfi->chipshift);
1312
1313 while (len) {
1314 unsigned long thislen;
1315 struct flchip *chip;
1316
1317 chip = &cfi->chips[chipnum];
1318 if (chipnum >= cfi->numchips)
1319 break;
1320
1321 if ((len + ofs -1) >> cfi->chipshift)
1322 thislen = (1<<cfi->chipshift) - ofs;
1323 else
1324 thislen = len;
1325
1326 spin_lock(chip->mutex);
1327 if (chip->state == FL_POINT) {
1328 chip->ref_point_counter--;
1329 if(chip->ref_point_counter == 0)
1330 chip->state = FL_READY;
1331 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001332 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 -07001333
1334 put_chip(map, chip, chip->start);
1335 spin_unlock(chip->mutex);
1336
1337 len -= thislen;
1338 ofs = 0;
1339 chipnum++;
1340 }
1341}
1342
1343static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1344{
1345 unsigned long cmd_addr;
1346 struct cfi_private *cfi = map->fldrv_priv;
1347 int ret;
1348
1349 adr += chip->start;
1350
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001351 /* Ensure cmd read/writes are aligned. */
1352 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 spin_lock(chip->mutex);
1355 ret = get_chip(map, chip, cmd_addr, FL_READY);
1356 if (ret) {
1357 spin_unlock(chip->mutex);
1358 return ret;
1359 }
1360
1361 if (chip->state != FL_POINT && chip->state != FL_READY) {
1362 map_write(map, CMD(0xff), cmd_addr);
1363
1364 chip->state = FL_READY;
1365 }
1366
1367 map_copy_from(map, buf, adr, len);
1368
1369 put_chip(map, chip, cmd_addr);
1370
1371 spin_unlock(chip->mutex);
1372 return 0;
1373}
1374
1375static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1376{
1377 struct map_info *map = mtd->priv;
1378 struct cfi_private *cfi = map->fldrv_priv;
1379 unsigned long ofs;
1380 int chipnum;
1381 int ret = 0;
1382
1383 /* ofs: offset within the first chip that the first read should start */
1384 chipnum = (from >> cfi->chipshift);
1385 ofs = from - (chipnum << cfi->chipshift);
1386
1387 *retlen = 0;
1388
1389 while (len) {
1390 unsigned long thislen;
1391
1392 if (chipnum >= cfi->numchips)
1393 break;
1394
1395 if ((len + ofs -1) >> cfi->chipshift)
1396 thislen = (1<<cfi->chipshift) - ofs;
1397 else
1398 thislen = len;
1399
1400 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1401 if (ret)
1402 break;
1403
1404 *retlen += thislen;
1405 len -= thislen;
1406 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ofs = 0;
1409 chipnum++;
1410 }
1411 return ret;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001415 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001418 map_word status, write_cmd;
1419 int ret=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 adr += chip->start;
1422
Nicolas Pitref77814d2005-02-08 17:11:19 +00001423 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001424 case FL_WRITING:
1425 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0x40) : CMD(0x41);
1426 break;
1427 case FL_OTP_WRITE:
1428 write_cmd = CMD(0xc0);
1429 break;
1430 default:
1431 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 spin_lock(chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001435 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (ret) {
1437 spin_unlock(chip->mutex);
1438 return ret;
1439 }
1440
1441 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1442 ENABLE_VPP(map);
1443 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001444 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001446 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Nicolas Pitrec1724712006-03-30 15:52:41 +01001448 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1449 adr, map_bankwidth(map),
Alexey Korolev46a16522006-06-28 19:22:07 +01001450 chip->word_write_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001451 if (ret) {
1452 xip_enable(map, chip, adr);
1453 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Nicolas Pitre48436532005-08-06 05:16:52 +01001457 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001458 status = map_read(map, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001459 if (map_word_bitsset(map, status, CMD(0x1a))) {
1460 unsigned long chipstatus = MERGESTATUS(status);
1461
1462 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001465 xip_enable(map, chip, adr);
1466
1467 if (chipstatus & 0x02) {
1468 ret = -EROFS;
1469 } else if (chipstatus & 0x08) {
1470 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1471 ret = -EIO;
1472 } else {
1473 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1474 ret = -EINVAL;
1475 }
1476
1477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479
1480 xip_enable(map, chip, adr);
1481 out: put_chip(map, chip, adr);
1482 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return ret;
1484}
1485
1486
1487static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1488{
1489 struct map_info *map = mtd->priv;
1490 struct cfi_private *cfi = map->fldrv_priv;
1491 int ret = 0;
1492 int chipnum;
1493 unsigned long ofs;
1494
1495 *retlen = 0;
1496 if (!len)
1497 return 0;
1498
1499 chipnum = to >> cfi->chipshift;
1500 ofs = to - (chipnum << cfi->chipshift);
1501
1502 /* If it's not bus-aligned, do the first byte write */
1503 if (ofs & (map_bankwidth(map)-1)) {
1504 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1505 int gap = ofs - bus_ofs;
1506 int n;
1507 map_word datum;
1508
1509 n = min_t(int, len, map_bankwidth(map)-gap);
1510 datum = map_word_ff(map);
1511 datum = map_word_load_partial(map, datum, buf, gap, n);
1512
1513 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001514 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001515 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return ret;
1517
1518 len -= n;
1519 ofs += n;
1520 buf += n;
1521 (*retlen) += n;
1522
1523 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001524 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ofs = 0;
1526 if (chipnum == cfi->numchips)
1527 return 0;
1528 }
1529 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 while(len >= map_bankwidth(map)) {
1532 map_word datum = map_word_load(map, buf);
1533
1534 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001535 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (ret)
1537 return ret;
1538
1539 ofs += map_bankwidth(map);
1540 buf += map_bankwidth(map);
1541 (*retlen) += map_bankwidth(map);
1542 len -= map_bankwidth(map);
1543
1544 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001545 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 ofs = 0;
1547 if (chipnum == cfi->numchips)
1548 return 0;
1549 }
1550 }
1551
1552 if (len & (map_bankwidth(map)-1)) {
1553 map_word datum;
1554
1555 datum = map_word_ff(map);
1556 datum = map_word_load_partial(map, datum, buf, 0, len);
1557
1558 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001559 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001560 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 (*retlen) += len;
1564 }
1565
1566 return 0;
1567}
1568
1569
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001570static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001571 unsigned long adr, const struct kvec **pvec,
1572 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001575 map_word status, write_cmd, datum;
1576 unsigned long cmd_adr;
1577 int ret, wbufsize, word_gap, words;
Nicolas Pitree102d542005-08-06 05:46:59 +01001578 const struct kvec *vec;
1579 unsigned long vec_seek;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001580 unsigned long initial_adr;
1581 int initial_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1584 adr += chip->start;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001585 initial_adr = adr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* Let's determine this according to the interleave only once */
Nicolas Pitre638d9832005-08-06 05:40:46 +01001589 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 spin_lock(chip->mutex);
1592 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1593 if (ret) {
1594 spin_unlock(chip->mutex);
1595 return ret;
1596 }
1597
Massimo Cirillo646fd122008-01-11 10:24:11 +00001598 XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ENABLE_VPP(map);
1600 xip_disable(map, chip, cmd_adr);
1601
David Woodhouse151e7652006-05-14 01:51:54 +01001602 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001603 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 So we must check here and reset those bits if they're set. Otherwise
1605 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001606 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001608 chip->state = FL_STATUS;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 status = map_read(map, cmd_adr);
1611 if (map_word_bitsset(map, status, CMD(0x30))) {
1612 xip_enable(map, chip, cmd_adr);
1613 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1614 xip_disable(map, chip, cmd_adr);
1615 map_write(map, CMD(0x50), cmd_adr);
1616 map_write(map, CMD(0x70), cmd_adr);
1617 }
1618
1619 chip->state = FL_WRITING_TO_BUFFER;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001620 map_write(map, write_cmd, cmd_adr);
1621 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0);
1622 if (ret) {
1623 /* Argh. Not ready for write to buffer */
1624 map_word Xstatus = map_read(map, cmd_adr);
1625 map_write(map, CMD(0x70), cmd_adr);
1626 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 status = map_read(map, cmd_adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001628 map_write(map, CMD(0x50), cmd_adr);
1629 map_write(map, CMD(0x70), cmd_adr);
1630 xip_enable(map, chip, cmd_adr);
1631 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1632 map->name, Xstatus.x[0], status.x[0]);
1633 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635
Nicolas Pitree102d542005-08-06 05:46:59 +01001636 /* Figure out the number of words to write */
1637 word_gap = (-adr & (map_bankwidth(map)-1));
1638 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
1639 if (!word_gap) {
1640 words--;
1641 } else {
1642 word_gap = map_bankwidth(map) - word_gap;
1643 adr -= word_gap;
1644 datum = map_word_ff(map);
1645 }
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001648 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001651 vec = *pvec;
1652 vec_seek = *pvec_seek;
1653 do {
1654 int n = map_bankwidth(map) - word_gap;
1655 if (n > vec->iov_len - vec_seek)
1656 n = vec->iov_len - vec_seek;
1657 if (n > len)
1658 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Nicolas Pitree102d542005-08-06 05:46:59 +01001660 if (!word_gap && len < map_bankwidth(map))
1661 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Nicolas Pitree102d542005-08-06 05:46:59 +01001663 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001664 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001665 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Nicolas Pitree102d542005-08-06 05:46:59 +01001667 len -= n;
1668 word_gap += n;
1669 if (!len || word_gap == map_bankwidth(map)) {
1670 map_write(map, datum, adr);
1671 adr += map_bankwidth(map);
1672 word_gap = 0;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Nicolas Pitree102d542005-08-06 05:46:59 +01001675 vec_seek += n;
1676 if (vec_seek == vec->iov_len) {
1677 vec++;
1678 vec_seek = 0;
1679 }
1680 } while (len);
1681 *pvec = vec;
1682 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /* GO GO GO */
1685 map_write(map, CMD(0xd0), cmd_adr);
1686 chip->state = FL_WRITING;
1687
Nicolas Pitrec1724712006-03-30 15:52:41 +01001688 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
Massimo Cirillo646fd122008-01-11 10:24:11 +00001689 initial_adr, initial_len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001690 chip->buffer_write_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001691 if (ret) {
1692 map_write(map, CMD(0x70), cmd_adr);
1693 chip->state = FL_STATUS;
1694 xip_enable(map, chip, cmd_adr);
1695 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Nicolas Pitre48436532005-08-06 05:16:52 +01001699 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001700 status = map_read(map, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001701 if (map_word_bitsset(map, status, CMD(0x1a))) {
1702 unsigned long chipstatus = MERGESTATUS(status);
1703
1704 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001706 map_write(map, CMD(0x70), cmd_adr);
1707 xip_enable(map, chip, cmd_adr);
1708
1709 if (chipstatus & 0x02) {
1710 ret = -EROFS;
1711 } else if (chipstatus & 0x08) {
1712 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1713 ret = -EIO;
1714 } else {
1715 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1716 ret = -EINVAL;
1717 }
1718
1719 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721
1722 xip_enable(map, chip, cmd_adr);
1723 out: put_chip(map, chip, cmd_adr);
1724 spin_unlock(chip->mutex);
1725 return ret;
1726}
1727
Nicolas Pitree102d542005-08-06 05:46:59 +01001728static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1729 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct map_info *map = mtd->priv;
1732 struct cfi_private *cfi = map->fldrv_priv;
1733 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1734 int ret = 0;
1735 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001736 unsigned long ofs, vec_seek, i;
1737 size_t len = 0;
1738
1739 for (i = 0; i < count; i++)
1740 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 *retlen = 0;
1743 if (!len)
1744 return 0;
1745
1746 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001747 ofs = to - (chipnum << cfi->chipshift);
1748 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Nicolas Pitree102d542005-08-06 05:46:59 +01001750 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* We must not cross write block boundaries */
1752 int size = wbufsize - (ofs & (wbufsize-1));
1753
1754 if (size > len)
1755 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001756 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001757 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (ret)
1759 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 (*retlen) += size;
1763 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 if (ofs >> cfi->chipshift) {
1766 chipnum ++;
1767 ofs = 0;
1768 if (chipnum == cfi->numchips)
1769 return 0;
1770 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001771
1772 /* Be nice and reschedule with the chip in a usable state for other
1773 processes. */
1774 cond_resched();
1775
Nicolas Pitree102d542005-08-06 05:46:59 +01001776 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return 0;
1779}
1780
Nicolas Pitree102d542005-08-06 05:46:59 +01001781static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1782 size_t len, size_t *retlen, const u_char *buf)
1783{
1784 struct kvec vec;
1785
1786 vec.iov_base = (void *) buf;
1787 vec.iov_len = len;
1788
1789 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1793 unsigned long adr, int len, void *thunk)
1794{
1795 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001796 map_word status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int retries = 3;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001798 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 adr += chip->start;
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 retry:
1803 spin_lock(chip->mutex);
1804 ret = get_chip(map, chip, adr, FL_ERASING);
1805 if (ret) {
1806 spin_unlock(chip->mutex);
1807 return ret;
1808 }
1809
1810 XIP_INVAL_CACHED_RANGE(map, adr, len);
1811 ENABLE_VPP(map);
1812 xip_disable(map, chip, adr);
1813
1814 /* Clear the status register first */
1815 map_write(map, CMD(0x50), adr);
1816
1817 /* Now erase */
1818 map_write(map, CMD(0x20), adr);
1819 map_write(map, CMD(0xD0), adr);
1820 chip->state = FL_ERASING;
1821 chip->erase_suspended = 0;
1822
Nicolas Pitrec1724712006-03-30 15:52:41 +01001823 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1824 adr, len,
Alexey Korolev46a16522006-06-28 19:22:07 +01001825 chip->erase_time);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001826 if (ret) {
1827 map_write(map, CMD(0x70), adr);
1828 chip->state = FL_STATUS;
1829 xip_enable(map, chip, adr);
1830 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
1834 /* We've broken this before. It doesn't hurt to be safe */
1835 map_write(map, CMD(0x70), adr);
1836 chip->state = FL_STATUS;
1837 status = map_read(map, adr);
1838
Nicolas Pitre48436532005-08-06 05:16:52 +01001839 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001841 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 /* Reset the error bits */
1844 map_write(map, CMD(0x50), adr);
1845 map_write(map, CMD(0x70), adr);
1846 xip_enable(map, chip, adr);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001849 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1850 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 } else if (chipstatus & 0x02) {
1852 /* Protection bit set */
1853 ret = -EROFS;
1854 } else if (chipstatus & 0x8) {
1855 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001856 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001858 } else if (chipstatus & 0x20 && retries--) {
1859 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
Nicolas Pitre48436532005-08-06 05:16:52 +01001860 put_chip(map, chip, adr);
1861 spin_unlock(chip->mutex);
1862 goto retry;
1863 } else {
1864 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 -07001865 ret = -EIO;
1866 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001867
1868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
Nicolas Pitre48436532005-08-06 05:16:52 +01001871 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 out: put_chip(map, chip, adr);
1873 spin_unlock(chip->mutex);
1874 return ret;
1875}
1876
Ben Dooks029a9eb2007-05-28 20:11:37 +01001877static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 unsigned long ofs, len;
1880 int ret;
1881
1882 ofs = instr->addr;
1883 len = instr->len;
1884
1885 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1886 if (ret)
1887 return ret;
1888
1889 instr->state = MTD_ERASE_DONE;
1890 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893}
1894
1895static void cfi_intelext_sync (struct mtd_info *mtd)
1896{
1897 struct map_info *map = mtd->priv;
1898 struct cfi_private *cfi = map->fldrv_priv;
1899 int i;
1900 struct flchip *chip;
1901 int ret = 0;
1902
1903 for (i=0; !ret && i<cfi->numchips; i++) {
1904 chip = &cfi->chips[i];
1905
1906 spin_lock(chip->mutex);
1907 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1908
1909 if (!ret) {
1910 chip->oldstate = chip->state;
1911 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001912 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * as the whole point is that nobody can do anything
1914 * with the chip now anyway.
1915 */
1916 }
1917 spin_unlock(chip->mutex);
1918 }
1919
1920 /* Unlock the chips again */
1921
1922 for (i--; i >=0; i--) {
1923 chip = &cfi->chips[i];
1924
1925 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (chip->state == FL_SYNCING) {
1928 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00001929 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 wake_up(&chip->wq);
1931 }
1932 spin_unlock(chip->mutex);
1933 }
1934}
1935
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001936static int __xipram do_getlockstatus_oneblock(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct flchip *chip,
1938 unsigned long adr,
1939 int len, void *thunk)
1940{
1941 struct cfi_private *cfi = map->fldrv_priv;
1942 int status, ofs_factor = cfi->interleave * cfi->device_type;
1943
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001944 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001946 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 chip->state = FL_JEDEC_QUERY;
1948 status = cfi_read_query(map, adr+(2*ofs_factor));
1949 xip_enable(map, chip, 0);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001950 return status;
1951}
1952
1953#ifdef DEBUG_LOCK_BITS
1954static int __xipram do_printlockstatus_oneblock(struct map_info *map,
1955 struct flchip *chip,
1956 unsigned long adr,
1957 int len, void *thunk)
1958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001960 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return 0;
1962}
1963#endif
1964
1965#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1966#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1967
1968static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
1969 unsigned long adr, int len, void *thunk)
1970{
1971 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001972 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001973 int udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int ret;
1975
1976 adr += chip->start;
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 spin_lock(chip->mutex);
1979 ret = get_chip(map, chip, adr, FL_LOCKING);
1980 if (ret) {
1981 spin_unlock(chip->mutex);
1982 return ret;
1983 }
1984
1985 ENABLE_VPP(map);
1986 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 map_write(map, CMD(0x60), adr);
1989 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
1990 map_write(map, CMD(0x01), adr);
1991 chip->state = FL_LOCKING;
1992 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
1993 map_write(map, CMD(0xD0), adr);
1994 chip->state = FL_UNLOCKING;
1995 } else
1996 BUG();
1997
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001998 /*
1999 * If Instant Individual Block Locking supported then no need
2000 * to delay.
2001 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01002002 udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002003
Nicolas Pitrec1724712006-03-30 15:52:41 +01002004 ret = WAIT_TIMEOUT(map, chip, adr, udelay);
2005 if (ret) {
2006 map_write(map, CMD(0x70), adr);
2007 chip->state = FL_STATUS;
2008 xip_enable(map, chip, adr);
2009 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
2010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 xip_enable(map, chip, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002014out: put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 spin_unlock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
2019static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2020{
2021 int ret;
2022
2023#ifdef DEBUG_LOCK_BITS
2024 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002025 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002027 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028#endif
2029
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002030 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033#ifdef DEBUG_LOCK_BITS
2034 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002035 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002037 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038#endif
2039
2040 return ret;
2041}
2042
2043static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2044{
2045 int ret;
2046
2047#ifdef DEBUG_LOCK_BITS
2048 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002049 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002051 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052#endif
2053
2054 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2055 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057#ifdef DEBUG_LOCK_BITS
2058 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002059 __func__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002060 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002061 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return ret;
2065}
2066
Nicolas Pitref77814d2005-02-08 17:11:19 +00002067#ifdef CONFIG_MTD_OTP
2068
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002069typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002070 u_long data_offset, u_char *buf, u_int size,
2071 u_long prot_offset, u_int groupno, u_int groupsize);
2072
2073static int __xipram
2074do_otp_read(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 struct cfi_private *cfi = map->fldrv_priv;
2078 int ret;
2079
2080 spin_lock(chip->mutex);
2081 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2082 if (ret) {
2083 spin_unlock(chip->mutex);
2084 return ret;
2085 }
2086
2087 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002088 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002089
2090 xip_disable(map, chip, chip->start);
2091 if (chip->state != FL_JEDEC_QUERY) {
2092 map_write(map, CMD(0x90), chip->start);
2093 chip->state = FL_JEDEC_QUERY;
2094 }
2095 map_copy_from(map, buf, chip->start + offset, size);
2096 xip_enable(map, chip, chip->start);
2097
2098 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002099 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002100
2101 put_chip(map, chip, chip->start);
2102 spin_unlock(chip->mutex);
2103 return 0;
2104}
2105
2106static int
2107do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2108 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2109{
2110 int ret;
2111
2112 while (size) {
2113 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2114 int gap = offset - bus_ofs;
2115 int n = min_t(int, size, map_bankwidth(map)-gap);
2116 map_word datum = map_word_ff(map);
2117
2118 datum = map_word_load_partial(map, datum, buf, gap, n);
2119 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002120 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002121 return ret;
2122
2123 offset += n;
2124 buf += n;
2125 size -= n;
2126 }
2127
2128 return 0;
2129}
2130
2131static int
2132do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2133 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2134{
2135 struct cfi_private *cfi = map->fldrv_priv;
2136 map_word datum;
2137
2138 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002139 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002140 return -EXDEV;
2141
2142 datum = map_word_ff(map);
2143 datum = map_word_clr(map, datum, CMD(1 << grpno));
2144 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2145}
2146
2147static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2148 size_t *retlen, u_char *buf,
2149 otp_op_t action, int user_regs)
2150{
2151 struct map_info *map = mtd->priv;
2152 struct cfi_private *cfi = map->fldrv_priv;
2153 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2154 struct flchip *chip;
2155 struct cfi_intelext_otpinfo *otp;
2156 u_long devsize, reg_prot_offset, data_offset;
2157 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2158 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2159 int ret;
2160
2161 *retlen = 0;
2162
2163 /* Check that we actually have some OTP registers */
2164 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2165 return -ENODATA;
2166
2167 /* we need real chips here not virtual ones */
2168 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2169 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002170 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002171
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002172 /* Some chips have OTP located in the _top_ partition only.
2173 For example: Intel 28F256L18T (T means top-parameter device) */
2174 if (cfi->mfr == MANUFACTURER_INTEL) {
2175 switch (cfi->id) {
2176 case 0x880b:
2177 case 0x880c:
2178 case 0x880d:
2179 chip_num = chip_step - 1;
2180 }
2181 }
2182
2183 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002184 chip = &cfi->chips[chip_num];
2185 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2186
2187 /* first OTP region */
2188 field = 0;
2189 reg_prot_offset = extp->ProtRegAddr;
2190 reg_fact_groups = 1;
2191 reg_fact_size = 1 << extp->FactProtRegSize;
2192 reg_user_groups = 1;
2193 reg_user_size = 1 << extp->UserProtRegSize;
2194
2195 while (len > 0) {
2196 /* flash geometry fixup */
2197 data_offset = reg_prot_offset + 1;
2198 data_offset *= cfi->interleave * cfi->device_type;
2199 reg_prot_offset *= cfi->interleave * cfi->device_type;
2200 reg_fact_size *= cfi->interleave;
2201 reg_user_size *= cfi->interleave;
2202
2203 if (user_regs) {
2204 groups = reg_user_groups;
2205 groupsize = reg_user_size;
2206 /* skip over factory reg area */
2207 groupno = reg_fact_groups;
2208 data_offset += reg_fact_groups * reg_fact_size;
2209 } else {
2210 groups = reg_fact_groups;
2211 groupsize = reg_fact_size;
2212 groupno = 0;
2213 }
2214
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002215 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002216 if (!action) {
2217 /*
2218 * Special case: if action is NULL
2219 * we fill buf with otp_info records.
2220 */
2221 struct otp_info *otpinfo;
2222 map_word lockword;
2223 len -= sizeof(struct otp_info);
2224 if (len <= 0)
2225 return -ENOSPC;
2226 ret = do_otp_read(map, chip,
2227 reg_prot_offset,
2228 (u_char *)&lockword,
2229 map_bankwidth(map),
2230 0, 0, 0);
2231 if (ret)
2232 return ret;
2233 otpinfo = (struct otp_info *)buf;
2234 otpinfo->start = from;
2235 otpinfo->length = groupsize;
2236 otpinfo->locked =
2237 !map_word_bitsset(map, lockword,
2238 CMD(1 << groupno));
2239 from += groupsize;
2240 buf += sizeof(*otpinfo);
2241 *retlen += sizeof(*otpinfo);
2242 } else if (from >= groupsize) {
2243 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002244 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002245 } else {
2246 int size = groupsize;
2247 data_offset += from;
2248 size -= from;
2249 from = 0;
2250 if (size > len)
2251 size = len;
2252 ret = action(map, chip, data_offset,
2253 buf, size, reg_prot_offset,
2254 groupno, groupsize);
2255 if (ret < 0)
2256 return ret;
2257 buf += size;
2258 len -= size;
2259 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002260 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002261 }
2262 groupno++;
2263 groups--;
2264 }
2265
2266 /* next OTP region */
2267 if (++field == extp->NumProtectionFields)
2268 break;
2269 reg_prot_offset = otp->ProtRegAddr;
2270 reg_fact_groups = otp->FactGroups;
2271 reg_fact_size = 1 << otp->FactProtRegSize;
2272 reg_user_groups = otp->UserGroups;
2273 reg_user_size = 1 << otp->UserProtRegSize;
2274 otp++;
2275 }
2276 }
2277
2278 return 0;
2279}
2280
2281static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2282 size_t len, size_t *retlen,
2283 u_char *buf)
2284{
2285 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2286 buf, do_otp_read, 0);
2287}
2288
2289static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2290 size_t len, size_t *retlen,
2291 u_char *buf)
2292{
2293 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2294 buf, do_otp_read, 1);
2295}
2296
2297static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2298 size_t len, size_t *retlen,
2299 u_char *buf)
2300{
2301 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2302 buf, do_otp_write, 1);
2303}
2304
2305static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2306 loff_t from, size_t len)
2307{
2308 size_t retlen;
2309 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2310 NULL, do_otp_lock, 1);
2311}
2312
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002313static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002314 struct otp_info *buf, size_t len)
2315{
2316 size_t retlen;
2317 int ret;
2318
2319 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2320 return ret ? : retlen;
2321}
2322
2323static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2324 struct otp_info *buf, size_t len)
2325{
2326 size_t retlen;
2327 int ret;
2328
2329 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2330 return ret ? : retlen;
2331}
2332
2333#endif
2334
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002335static void cfi_intelext_save_locks(struct mtd_info *mtd)
2336{
2337 struct mtd_erase_region_info *region;
2338 int block, status, i;
2339 unsigned long adr;
2340 size_t len;
2341
2342 for (i = 0; i < mtd->numeraseregions; i++) {
2343 region = &mtd->eraseregions[i];
2344 if (!region->lockmap)
2345 continue;
2346
2347 for (block = 0; block < region->numblocks; block++){
2348 len = region->erasesize;
2349 adr = region->offset + block * len;
2350
2351 status = cfi_varsize_frob(mtd,
Ben Dooks029a9eb2007-05-28 20:11:37 +01002352 do_getlockstatus_oneblock, adr, len, NULL);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002353 if (status)
2354 set_bit(block, region->lockmap);
2355 else
2356 clear_bit(block, region->lockmap);
2357 }
2358 }
2359}
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361static int cfi_intelext_suspend(struct mtd_info *mtd)
2362{
2363 struct map_info *map = mtd->priv;
2364 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002365 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 int i;
2367 struct flchip *chip;
2368 int ret = 0;
2369
Justin Treone619a752008-01-30 10:25:49 -08002370 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002371 && extp && (extp->FeatureSupport & (1 << 5)))
2372 cfi_intelext_save_locks(mtd);
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 for (i=0; !ret && i<cfi->numchips; i++) {
2375 chip = &cfi->chips[i];
2376
2377 spin_lock(chip->mutex);
2378
2379 switch (chip->state) {
2380 case FL_READY:
2381 case FL_STATUS:
2382 case FL_CFI_QUERY:
2383 case FL_JEDEC_QUERY:
2384 if (chip->oldstate == FL_READY) {
David Andersa86aaa62006-10-19 19:33:19 +03002385 /* place the chip in a known state before suspend */
2386 map_write(map, CMD(0xFF), cfi->chips[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 chip->oldstate = chip->state;
2388 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002389 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * as the whole point is that nobody can do anything
2391 * with the chip now anyway.
2392 */
2393 } else {
2394 /* There seems to be an operation pending. We must wait for it. */
2395 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2396 ret = -EAGAIN;
2397 }
2398 break;
2399 default:
2400 /* Should we actually wait? Once upon a time these routines weren't
2401 allowed to. Or should we return -EAGAIN, because the upper layers
2402 ought to have already shut down anything which was using the device
2403 anyway? The latter for now. */
2404 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2405 ret = -EAGAIN;
2406 case FL_PM_SUSPENDED:
2407 break;
2408 }
2409 spin_unlock(chip->mutex);
2410 }
2411
2412 /* Unlock the chips again */
2413
2414 if (ret) {
2415 for (i--; i >=0; i--) {
2416 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (chip->state == FL_PM_SUSPENDED) {
2421 /* No need to force it into a known state here,
2422 because we're returning failure, and it didn't
2423 get power cycled */
2424 chip->state = chip->oldstate;
2425 chip->oldstate = FL_READY;
2426 wake_up(&chip->wq);
2427 }
2428 spin_unlock(chip->mutex);
2429 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002430 }
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return ret;
2433}
2434
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002435static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2436{
2437 struct mtd_erase_region_info *region;
2438 int block, i;
2439 unsigned long adr;
2440 size_t len;
2441
2442 for (i = 0; i < mtd->numeraseregions; i++) {
2443 region = &mtd->eraseregions[i];
2444 if (!region->lockmap)
2445 continue;
2446
2447 for (block = 0; block < region->numblocks; block++) {
2448 len = region->erasesize;
2449 adr = region->offset + block * len;
2450
2451 if (!test_bit(block, region->lockmap))
2452 cfi_intelext_unlock(mtd, adr, len);
2453 }
2454 }
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static void cfi_intelext_resume(struct mtd_info *mtd)
2458{
2459 struct map_info *map = mtd->priv;
2460 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002461 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 int i;
2463 struct flchip *chip;
2464
2465 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 chip = &cfi->chips[i];
2468
2469 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* Go to known state. Chip may have been power cycled */
2472 if (chip->state == FL_PM_SUSPENDED) {
2473 map_write(map, CMD(0xFF), cfi->chips[i].start);
2474 chip->oldstate = chip->state = FL_READY;
2475 wake_up(&chip->wq);
2476 }
2477
2478 spin_unlock(chip->mutex);
2479 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002480
Justin Treone619a752008-01-30 10:25:49 -08002481 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002482 && extp && (extp->FeatureSupport & (1 << 5)))
2483 cfi_intelext_restore_locks(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002486static int cfi_intelext_reset(struct mtd_info *mtd)
2487{
2488 struct map_info *map = mtd->priv;
2489 struct cfi_private *cfi = map->fldrv_priv;
2490 int i, ret;
2491
2492 for (i=0; i < cfi->numchips; i++) {
2493 struct flchip *chip = &cfi->chips[i];
2494
2495 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002496 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002497 flash is accessible for soft reboot. */
2498 spin_lock(chip->mutex);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002499 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002500 if (!ret) {
2501 map_write(map, CMD(0xff), chip->start);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002502 chip->state = FL_SHUTDOWN;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002503 }
2504 spin_unlock(chip->mutex);
2505 }
2506
2507 return 0;
2508}
2509
2510static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2511 void *v)
2512{
2513 struct mtd_info *mtd;
2514
2515 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2516 cfi_intelext_reset(mtd);
2517 return NOTIFY_DONE;
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520static void cfi_intelext_destroy(struct mtd_info *mtd)
2521{
2522 struct map_info *map = mtd->priv;
2523 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002524 struct mtd_erase_region_info *region;
2525 int i;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002526 cfi_intelext_reset(mtd);
2527 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 kfree(cfi->cmdset_priv);
2529 kfree(cfi->cfiq);
2530 kfree(cfi->chips[0].priv);
2531 kfree(cfi);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002532 for (i = 0; i < mtd->numeraseregions; i++) {
2533 region = &mtd->eraseregions[i];
2534 if (region->lockmap)
2535 kfree(region->lockmap);
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 kfree(mtd->eraseregions);
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540MODULE_LICENSE("GPL");
2541MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2542MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002543MODULE_ALIAS("cfi_cmdset_0003");
2544MODULE_ALIAS("cfi_cmdset_0200");