blob: ab3888f5e4645bf4d264174d4179a637c285b6d7 [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
18 */
19
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/sched.h>
24#include <linux/init.h>
25#include <asm/io.h>
26#include <asm/byteorder.h>
27
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31#include <linux/interrupt.h>
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010032#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/mtd/xip.h>
34#include <linux/mtd/map.h>
35#include <linux/mtd/mtd.h>
36#include <linux/mtd/compatmac.h>
37#include <linux/mtd/cfi.h>
38
39/* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
40/* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
41
42// debugging, turns off buffer write mode if set to 1
43#define FORCE_WORD_WRITE 0
44
45#define MANUFACTURER_INTEL 0x0089
46#define I82802AB 0x00ad
47#define I82802AC 0x00ac
48#define MANUFACTURER_ST 0x0020
49#define M50LPW080 0x002F
50
51static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
53static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
Nicolas Pitree102d542005-08-06 05:46:59 +010054static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
56static void cfi_intelext_sync (struct mtd_info *);
57static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len);
58static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len);
Todd Poynor8048d2f2005-03-31 00:57:33 +010059#ifdef CONFIG_MTD_OTP
Nicolas Pitref77814d2005-02-08 17:11:19 +000060static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
61static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
62static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
63static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
64static int cfi_intelext_get_fact_prot_info (struct mtd_info *,
65 struct otp_info *, size_t);
66static int cfi_intelext_get_user_prot_info (struct mtd_info *,
67 struct otp_info *, size_t);
Todd Poynor8048d2f2005-03-31 00:57:33 +010068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int cfi_intelext_suspend (struct mtd_info *);
70static void cfi_intelext_resume (struct mtd_info *);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010071static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static void cfi_intelext_destroy(struct mtd_info *);
74
75struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
76
77static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
78static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
79
80static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
81 size_t *retlen, u_char **mtdbuf);
82static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from,
83 size_t len);
84
85static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
86static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
87#include "fwh_lock.h"
88
89
90
91/*
92 * *********** SETUP AND PROBE BITS ***********
93 */
94
95static struct mtd_chip_driver cfi_intelext_chipdrv = {
96 .probe = NULL, /* Not usable directly */
97 .destroy = cfi_intelext_destroy,
98 .name = "cfi_cmdset_0001",
99 .module = THIS_MODULE
100};
101
102/* #define DEBUG_LOCK_BITS */
103/* #define DEBUG_CFI_FEATURES */
104
105#ifdef DEBUG_CFI_FEATURES
106static void cfi_tell_features(struct cfi_pri_intelext *extp)
107{
108 int i;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100109 printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
111 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
112 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
113 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
114 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
115 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
116 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
117 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
118 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
119 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
120 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100121 printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
122 for (i=11; i<32; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000123 if (extp->FeatureSupport & (1<<i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 printk(" - Unknown Bit %X: supported\n", i);
125 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
128 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
129 for (i=1; i<8; i++) {
130 if (extp->SuspendCmdSupport & (1<<i))
131 printk(" - Unknown Bit %X: supported\n", i);
132 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
135 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100136 printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
137 for (i=2; i<3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (extp->BlkStatusRegMask & (1<<i))
139 printk(" - Unknown Bit %X Active: yes\n",i);
140 }
Nicolas Pitre638d9832005-08-06 05:40:46 +0100141 printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
142 printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
143 for (i=6; i<16; i++) {
144 if (extp->BlkStatusRegMask & (1<<i))
145 printk(" - Unknown Bit %X Active: yes\n",i);
146 }
147
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000148 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
150 if (extp->VppOptimal)
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000151 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
153}
154#endif
155
156#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000157/* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void fixup_intel_strataflash(struct mtd_info *mtd, void* param)
159{
160 struct map_info *map = mtd->priv;
161 struct cfi_private *cfi = map->fldrv_priv;
162 struct cfi_pri_amdstd *extp = cfi->cmdset_priv;
163
164 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
165 "erase on write disabled.\n");
166 extp->SuspendCmdSupport &= ~1;
167}
168#endif
169
170#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
171static void fixup_no_write_suspend(struct mtd_info *mtd, void* param)
172{
173 struct map_info *map = mtd->priv;
174 struct cfi_private *cfi = map->fldrv_priv;
175 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
176
177 if (cfip && (cfip->FeatureSupport&4)) {
178 cfip->FeatureSupport &= ~4;
179 printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
180 }
181}
182#endif
183
184static void fixup_st_m28w320ct(struct mtd_info *mtd, void* param)
185{
186 struct map_info *map = mtd->priv;
187 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
190 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
191}
192
193static void fixup_st_m28w320cb(struct mtd_info *mtd, void* param)
194{
195 struct map_info *map = mtd->priv;
196 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Note this is done after the region info is endian swapped */
199 cfi->cfiq->EraseRegionInfo[1] =
200 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
201};
202
203static void fixup_use_point(struct mtd_info *mtd, void *param)
204{
205 struct map_info *map = mtd->priv;
206 if (!mtd->point && map_is_linear(map)) {
207 mtd->point = cfi_intelext_point;
208 mtd->unpoint = cfi_intelext_unpoint;
209 }
210}
211
212static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
213{
214 struct map_info *map = mtd->priv;
215 struct cfi_private *cfi = map->fldrv_priv;
216 if (cfi->cfiq->BufWriteTimeoutTyp) {
217 printk(KERN_INFO "Using buffer write method\n" );
218 mtd->write = cfi_intelext_write_buffers;
Nicolas Pitree102d542005-08-06 05:46:59 +0100219 mtd->writev = cfi_intelext_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221}
222
223static struct cfi_fixup cfi_fixup_table[] = {
224#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000225 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif
227#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
228 { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend, NULL },
229#endif
230#if !FORCE_WORD_WRITE
231 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL },
232#endif
233 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
234 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
235 { 0, 0, NULL, NULL }
236};
237
238static struct cfi_fixup jedec_fixup_table[] = {
239 { MANUFACTURER_INTEL, I82802AB, fixup_use_fwh_lock, NULL, },
240 { MANUFACTURER_INTEL, I82802AC, fixup_use_fwh_lock, NULL, },
241 { MANUFACTURER_ST, M50LPW080, fixup_use_fwh_lock, NULL, },
242 { 0, 0, NULL, NULL }
243};
244static struct cfi_fixup fixup_table[] = {
245 /* The CFI vendor ids and the JEDEC vendor IDs appear
246 * to be common. It is like the devices id's are as
247 * well. This table is to pick all cases where
248 * we know that is the case.
249 */
250 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point, NULL },
251 { 0, 0, NULL, NULL }
252};
253
254static inline struct cfi_pri_intelext *
255read_pri_intelext(struct map_info *map, __u16 adr)
256{
257 struct cfi_pri_intelext *extp;
258 unsigned int extp_size = sizeof(*extp);
259
260 again:
261 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
262 if (!extp)
263 return NULL;
264
Todd Poynord88f9772005-07-20 22:01:17 +0100265 if (extp->MajorVersion != '1' ||
Nicolas Pitre638d9832005-08-06 05:40:46 +0100266 (extp->MinorVersion < '0' || extp->MinorVersion > '4')) {
Todd Poynord88f9772005-07-20 22:01:17 +0100267 printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
268 "version %c.%c.\n", extp->MajorVersion,
269 extp->MinorVersion);
270 kfree(extp);
271 return NULL;
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Do some byteswapping if necessary */
275 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
276 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
277 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
278
Nicolas Pitre638d9832005-08-06 05:40:46 +0100279 if (extp->MajorVersion == '1' && extp->MinorVersion >= '3') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned int extra_size = 0;
281 int nb_parts, i;
282
283 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000284 extra_size += (extp->NumProtectionFields - 1) *
285 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100288 extra_size += 2;
289 if (extp_size < sizeof(*extp) + extra_size)
290 goto need_more;
291 extra_size += extp->extra[extra_size-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* Number of hardware-partitions */
294 extra_size += 1;
295 if (extp_size < sizeof(*extp) + extra_size)
296 goto need_more;
297 nb_parts = extp->extra[extra_size - 1];
298
Nicolas Pitre638d9832005-08-06 05:40:46 +0100299 /* skip the sizeof(partregion) field in CFI 1.4 */
300 if (extp->MinorVersion >= '4')
301 extra_size += 2;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 for (i = 0; i < nb_parts; i++) {
304 struct cfi_intelext_regioninfo *rinfo;
305 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
306 extra_size += sizeof(*rinfo);
307 if (extp_size < sizeof(*extp) + extra_size)
308 goto need_more;
309 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
310 extra_size += (rinfo->NumBlockTypes - 1)
311 * sizeof(struct cfi_intelext_blockinfo);
312 }
313
Nicolas Pitre638d9832005-08-06 05:40:46 +0100314 if (extp->MinorVersion >= '4')
315 extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (extp_size < sizeof(*extp) + extra_size) {
318 need_more:
319 extp_size = sizeof(*extp) + extra_size;
320 kfree(extp);
321 if (extp_size > 4096) {
322 printk(KERN_ERR
323 "%s: cfi_pri_intelext is too fat\n",
324 __FUNCTION__);
325 return NULL;
326 }
327 goto again;
328 }
329 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return extp;
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
335{
336 struct cfi_private *cfi = map->fldrv_priv;
337 struct mtd_info *mtd;
338 int i;
339
340 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
341 if (!mtd) {
342 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
343 return NULL;
344 }
345 memset(mtd, 0, sizeof(*mtd));
346 mtd->priv = map;
347 mtd->type = MTD_NORFLASH;
348
349 /* Fill in the default mtd operations */
350 mtd->erase = cfi_intelext_erase_varsize;
351 mtd->read = cfi_intelext_read;
352 mtd->write = cfi_intelext_write_words;
353 mtd->sync = cfi_intelext_sync;
354 mtd->lock = cfi_intelext_lock;
355 mtd->unlock = cfi_intelext_unlock;
356 mtd->suspend = cfi_intelext_suspend;
357 mtd->resume = cfi_intelext_resume;
358 mtd->flags = MTD_CAP_NORFLASH;
359 mtd->name = map->name;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100360
361 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (cfi->cfi_mode == CFI_MODE_CFI) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000364 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * It's a real CFI chip, not one for which the probe
366 * routine faked a CFI structure. So we read the feature
367 * table from it.
368 */
369 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
370 struct cfi_pri_intelext *extp;
371
372 extp = read_pri_intelext(map, adr);
373 if (!extp) {
374 kfree(mtd);
375 return NULL;
376 }
377
378 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000379 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 cfi_fixup(mtd, cfi_fixup_table);
382
383#ifdef DEBUG_CFI_FEATURES
384 /* Tell the user about it in lots of lovely detail */
385 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000386#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if(extp->SuspendCmdSupport & 1) {
389 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
390 }
391 }
392 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
393 /* Apply jedec specific fixups */
394 cfi_fixup(mtd, jedec_fixup_table);
395 }
396 /* Apply generic fixups */
397 cfi_fixup(mtd, fixup_table);
398
399 for (i=0; i< cfi->numchips; i++) {
400 cfi->chips[i].word_write_time = 1<<cfi->cfiq->WordWriteTimeoutTyp;
401 cfi->chips[i].buffer_write_time = 1<<cfi->cfiq->BufWriteTimeoutTyp;
402 cfi->chips[i].erase_time = 1<<cfi->cfiq->BlockEraseTimeoutTyp;
403 cfi->chips[i].ref_point_counter = 0;
Simon Voglc314b6f2006-02-24 13:04:09 -0800404 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 map->fldrv = &cfi_intelext_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return cfi_intelext_setup(mtd);
410}
David Woodhousea15bdee2006-05-08 22:35:05 +0100411struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
412struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
413EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
414EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
415EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
418{
419 struct map_info *map = mtd->priv;
420 struct cfi_private *cfi = map->fldrv_priv;
421 unsigned long offset = 0;
422 int i,j;
423 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
424
425 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
426
427 mtd->size = devsize * cfi->numchips;
428
429 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000430 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000432 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
434 goto setup_err;
435 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
438 unsigned long ernum, ersize;
439 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
440 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
441
442 if (mtd->erasesize < ersize) {
443 mtd->erasesize = ersize;
444 }
445 for (j=0; j<cfi->numchips; j++) {
446 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
447 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
448 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
449 }
450 offset += (ersize * ernum);
451 }
452
453 if (offset != devsize) {
454 /* Argh */
455 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
456 goto setup_err;
457 }
458
459 for (i=0; i<mtd->numeraseregions;i++){
Nicolas Pitre48436532005-08-06 05:16:52 +0100460 printk(KERN_DEBUG "erase region %d: offset=0x%x,size=0x%x,blocks=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 i,mtd->eraseregions[i].offset,
462 mtd->eraseregions[i].erasesize,
463 mtd->eraseregions[i].numblocks);
464 }
465
Nicolas Pitref77814d2005-02-08 17:11:19 +0000466#ifdef CONFIG_MTD_OTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000468 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
469 mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
470 mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
471 mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
472 mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#endif
474
475 /* This function has the potential to distort the reality
476 a bit and therefore should be called last. */
477 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
478 goto setup_err;
479
480 __module_get(THIS_MODULE);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100481 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return mtd;
483
484 setup_err:
485 if(mtd) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800486 kfree(mtd->eraseregions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 kfree(mtd);
488 }
489 kfree(cfi->cmdset_priv);
490 return NULL;
491}
492
493static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
494 struct cfi_private **pcfi)
495{
496 struct map_info *map = mtd->priv;
497 struct cfi_private *cfi = *pcfi;
498 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
499
500 /*
501 * Probing of multi-partition flash ships.
502 *
503 * To support multiple partitions when available, we simply arrange
504 * for each of them to have their own flchip structure even if they
505 * are on the same physical chip. This means completely recreating
506 * a new cfi_private structure right here which is a blatent code
507 * layering violation, but this is still the least intrusive
508 * arrangement at this point. This can be rearranged in the future
509 * if someone feels motivated enough. --nico
510 */
Nicolas Pitre638d9832005-08-06 05:40:46 +0100511 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 && extp->FeatureSupport & (1 << 9)) {
513 struct cfi_private *newcfi;
514 struct flchip *chip;
515 struct flchip_shared *shared;
516 int offs, numregions, numparts, partshift, numvirtchips, i, j;
517
518 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000519 offs = (extp->NumProtectionFields - 1) *
520 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100523 offs += extp->extra[offs+1]+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* Number of partition regions */
526 numregions = extp->extra[offs];
527 offs += 1;
528
Nicolas Pitre638d9832005-08-06 05:40:46 +0100529 /* skip the sizeof(partregion) field in CFI 1.4 */
530 if (extp->MinorVersion >= '4')
531 offs += 2;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Number of hardware partitions */
534 numparts = 0;
535 for (i = 0; i < numregions; i++) {
536 struct cfi_intelext_regioninfo *rinfo;
537 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
538 numparts += rinfo->NumIdentPartitions;
539 offs += sizeof(*rinfo)
540 + (rinfo->NumBlockTypes - 1) *
541 sizeof(struct cfi_intelext_blockinfo);
542 }
543
Nicolas Pitre638d9832005-08-06 05:40:46 +0100544 /* Programming Region info */
545 if (extp->MinorVersion >= '4') {
546 struct cfi_intelext_programming_regioninfo *prinfo;
547 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
548 MTD_PROGREGION_SIZE(mtd) = cfi->interleave << prinfo->ProgRegShift;
549 MTD_PROGREGION_CTRLMODE_VALID(mtd) = cfi->interleave * prinfo->ControlValid;
550 MTD_PROGREGION_CTRLMODE_INVALID(mtd) = cfi->interleave * prinfo->ControlInvalid;
551 mtd->flags |= MTD_PROGRAM_REGIONS;
552 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
553 map->name, MTD_PROGREGION_SIZE(mtd),
554 MTD_PROGREGION_CTRLMODE_VALID(mtd),
555 MTD_PROGREGION_CTRLMODE_INVALID(mtd));
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * All functions below currently rely on all chips having
560 * the same geometry so we'll just assume that all hardware
561 * partitions are of the same size too.
562 */
563 partshift = cfi->chipshift - __ffs(numparts);
564
565 if ((1 << partshift) < mtd->erasesize) {
566 printk( KERN_ERR
567 "%s: bad number of hw partitions (%d)\n",
568 __FUNCTION__, numparts);
569 return -EINVAL;
570 }
571
572 numvirtchips = cfi->numchips * numparts;
573 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
574 if (!newcfi)
575 return -ENOMEM;
576 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
577 if (!shared) {
578 kfree(newcfi);
579 return -ENOMEM;
580 }
581 memcpy(newcfi, cfi, sizeof(struct cfi_private));
582 newcfi->numchips = numvirtchips;
583 newcfi->chipshift = partshift;
584
585 chip = &newcfi->chips[0];
586 for (i = 0; i < cfi->numchips; i++) {
587 shared[i].writing = shared[i].erasing = NULL;
588 spin_lock_init(&shared[i].lock);
589 for (j = 0; j < numparts; j++) {
590 *chip = cfi->chips[i];
591 chip->start += j << partshift;
592 chip->priv = &shared[i];
593 /* those should be reset too since
594 they create memory references. */
595 init_waitqueue_head(&chip->wq);
596 spin_lock_init(&chip->_spinlock);
597 chip->mutex = &chip->_spinlock;
598 chip++;
599 }
600 }
601
602 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
603 "--> %d partitions of %d KiB\n",
604 map->name, cfi->numchips, cfi->interleave,
605 newcfi->numchips, 1<<(newcfi->chipshift-10));
606
607 map->fldrv_priv = newcfi;
608 *pcfi = newcfi;
609 kfree(cfi);
610 }
611
612 return 0;
613}
614
615/*
616 * *********** CHIP ACCESS FUNCTIONS ***********
617 */
618
619static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
620{
621 DECLARE_WAITQUEUE(wait, current);
622 struct cfi_private *cfi = map->fldrv_priv;
623 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
624 unsigned long timeo;
625 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
626
627 resettime:
628 timeo = jiffies + HZ;
629 retry:
Nicolas Pitref77814d2005-02-08 17:11:19 +0000630 if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * OK. We have possibility for contension on the write/erase
633 * operations which are global to the real chip and not per
634 * partition. So let's fight it over in the partition which
635 * currently has authority on the operation.
636 *
637 * The rules are as follows:
638 *
639 * - any write operation must own shared->writing.
640 *
641 * - any erase operation must own _both_ shared->writing and
642 * shared->erasing.
643 *
644 * - contension arbitration is handled in the owner's context.
645 *
Nicolas Pitre8bc3b382005-11-23 22:07:56 +0000646 * The 'shared' struct can be read and/or written only when
647 * its lock is taken.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
649 struct flchip_shared *shared = chip->priv;
650 struct flchip *contender;
651 spin_lock(&shared->lock);
652 contender = shared->writing;
653 if (contender && contender != chip) {
654 /*
655 * The engine to perform desired operation on this
656 * partition is already in use by someone else.
657 * Let's fight over it in the context of the chip
658 * currently using it. If it is possible to suspend,
659 * that other partition will do just that, otherwise
660 * it'll happily send us to sleep. In any case, when
661 * get_chip returns success we're clear to go ahead.
662 */
663 int ret = spin_trylock(contender->mutex);
664 spin_unlock(&shared->lock);
665 if (!ret)
666 goto retry;
667 spin_unlock(chip->mutex);
668 ret = get_chip(map, contender, contender->start, mode);
669 spin_lock(chip->mutex);
670 if (ret) {
671 spin_unlock(contender->mutex);
672 return ret;
673 }
674 timeo = jiffies + HZ;
675 spin_lock(&shared->lock);
Nicolas Pitre8bc3b382005-11-23 22:07:56 +0000676 spin_unlock(contender->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 /* We now own it */
680 shared->writing = chip;
681 if (mode == FL_ERASING)
682 shared->erasing = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 spin_unlock(&shared->lock);
684 }
685
686 switch (chip->state) {
687
688 case FL_STATUS:
689 for (;;) {
690 status = map_read(map, adr);
691 if (map_word_andequal(map, status, status_OK, status_OK))
692 break;
693
694 /* At this point we're fine with write operations
695 in other partitions as they don't conflict. */
696 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
697 break;
698
699 if (time_after(jiffies, timeo)) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000700 printk(KERN_ERR "%s: Waiting for chip to be ready timed out. Status %lx\n",
Nicolas Pitre48436532005-08-06 05:16:52 +0100701 map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EIO;
703 }
704 spin_unlock(chip->mutex);
705 cfi_udelay(1);
706 spin_lock(chip->mutex);
707 /* Someone else might have been playing with it. */
708 goto retry;
709 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 case FL_READY:
712 case FL_CFI_QUERY:
713 case FL_JEDEC_QUERY:
714 return 0;
715
716 case FL_ERASING:
717 if (!cfip ||
718 !(cfip->FeatureSupport & 2) ||
719 !(mode == FL_READY || mode == FL_POINT ||
720 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
721 goto sleep;
722
723
724 /* Erase suspend */
725 map_write(map, CMD(0xB0), adr);
726
727 /* If the flash has finished erasing, then 'erase suspend'
728 * appears to make some (28F320) flash devices switch to
729 * 'read' mode. Make sure that we switch to 'read status'
730 * mode so we get the right data. --rmk
731 */
732 map_write(map, CMD(0x70), adr);
733 chip->oldstate = FL_ERASING;
734 chip->state = FL_ERASE_SUSPENDING;
735 chip->erase_suspended = 1;
736 for (;;) {
737 status = map_read(map, adr);
738 if (map_word_andequal(map, status, status_OK, status_OK))
739 break;
740
741 if (time_after(jiffies, timeo)) {
742 /* Urgh. Resume and pretend we weren't here. */
743 map_write(map, CMD(0xd0), adr);
744 /* Make sure we're in 'read status' mode if it had finished */
745 map_write(map, CMD(0x70), adr);
746 chip->state = FL_ERASING;
747 chip->oldstate = FL_READY;
Nicolas Pitre48436532005-08-06 05:16:52 +0100748 printk(KERN_ERR "%s: Chip not ready after erase "
749 "suspended: status = 0x%lx\n", map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -EIO;
751 }
752
753 spin_unlock(chip->mutex);
754 cfi_udelay(1);
755 spin_lock(chip->mutex);
756 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
757 So we can just loop here. */
758 }
759 chip->state = FL_STATUS;
760 return 0;
761
762 case FL_XIP_WHILE_ERASING:
763 if (mode != FL_READY && mode != FL_POINT &&
764 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
765 goto sleep;
766 chip->oldstate = chip->state;
767 chip->state = FL_READY;
768 return 0;
769
770 case FL_POINT:
771 /* Only if there's no operation suspended... */
772 if (mode == FL_READY && chip->oldstate == FL_READY)
773 return 0;
774
775 default:
776 sleep:
777 set_current_state(TASK_UNINTERRUPTIBLE);
778 add_wait_queue(&chip->wq, &wait);
779 spin_unlock(chip->mutex);
780 schedule();
781 remove_wait_queue(&chip->wq, &wait);
782 spin_lock(chip->mutex);
783 goto resettime;
784 }
785}
786
787static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
788{
789 struct cfi_private *cfi = map->fldrv_priv;
790
791 if (chip->priv) {
792 struct flchip_shared *shared = chip->priv;
793 spin_lock(&shared->lock);
794 if (shared->writing == chip && chip->oldstate == FL_READY) {
795 /* We own the ability to write, but we're done */
796 shared->writing = shared->erasing;
797 if (shared->writing && shared->writing != chip) {
798 /* give back ownership to who we loaned it from */
799 struct flchip *loaner = shared->writing;
800 spin_lock(loaner->mutex);
801 spin_unlock(&shared->lock);
802 spin_unlock(chip->mutex);
803 put_chip(map, loaner, loaner->start);
804 spin_lock(chip->mutex);
805 spin_unlock(loaner->mutex);
806 wake_up(&chip->wq);
807 return;
808 }
809 shared->erasing = NULL;
810 shared->writing = NULL;
811 } else if (shared->erasing == chip && shared->writing != chip) {
812 /*
813 * We own the ability to erase without the ability
814 * to write, which means the erase was suspended
815 * and some other partition is currently writing.
816 * Don't let the switch below mess things up since
817 * we don't have ownership to resume anything.
818 */
819 spin_unlock(&shared->lock);
820 wake_up(&chip->wq);
821 return;
822 }
823 spin_unlock(&shared->lock);
824 }
825
826 switch(chip->oldstate) {
827 case FL_ERASING:
828 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000829 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000831 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 errors to be returned from do_erase_oneblock because
833 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000834 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000836 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 do. */
838 map_write(map, CMD(0xd0), adr);
839 map_write(map, CMD(0x70), adr);
840 chip->oldstate = FL_READY;
841 chip->state = FL_ERASING;
842 break;
843
844 case FL_XIP_WHILE_ERASING:
845 chip->state = chip->oldstate;
846 chip->oldstate = FL_READY;
847 break;
848
849 case FL_READY:
850 case FL_STATUS:
851 case FL_JEDEC_QUERY:
852 /* We should really make set_vpp() count, rather than doing this */
853 DISABLE_VPP(map);
854 break;
855 default:
Nicolas Pitre48436532005-08-06 05:16:52 +0100856 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 wake_up(&chip->wq);
859}
860
861#ifdef CONFIG_MTD_XIP
862
863/*
864 * No interrupt what so ever can be serviced while the flash isn't in array
865 * mode. This is ensured by the xip_disable() and xip_enable() functions
866 * enclosing any code path where the flash is known not to be in array mode.
867 * And within a XIP disabled code path, only functions marked with __xipram
868 * may be called and nothing else (it's a good thing to inspect generated
869 * assembly to make sure inline functions were actually inlined and that gcc
870 * didn't emit calls to its own support functions). Also configuring MTD CFI
871 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
873
874static void xip_disable(struct map_info *map, struct flchip *chip,
875 unsigned long adr)
876{
877 /* TODO: chips with no XIP use should ignore and return */
878 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 local_irq_disable();
880}
881
882static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
883 unsigned long adr)
884{
885 struct cfi_private *cfi = map->fldrv_priv;
886 if (chip->state != FL_POINT && chip->state != FL_READY) {
887 map_write(map, CMD(0xff), adr);
888 chip->state = FL_READY;
889 }
890 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +0200891 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
896 * When a delay is required for the flash operation to complete, the
897 * xip_udelay() function is polling for both the given timeout and pending
898 * (but still masked) hardware interrupts. Whenever there is an interrupt
899 * pending then the flash erase or write operation is suspended, array mode
900 * restored and interrupts unmasked. Task scheduling might also happen at that
901 * point. The CPU eventually returns from the interrupt or the call to
902 * schedule() and the suspended flash operation is resumed for the remaining
903 * of the delay period.
904 *
905 * Warning: this function _will_ fool interrupt latency tracing tools.
906 */
907
908static void __xipram xip_udelay(struct map_info *map, struct flchip *chip,
909 unsigned long adr, int usec)
910{
911 struct cfi_private *cfi = map->fldrv_priv;
912 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
913 map_word status, OK = CMD(0x80);
914 unsigned long suspended, start = xip_currtime();
915 flstate_t oldstate, newstate;
916
917 do {
918 cpu_relax();
919 if (xip_irqpending() && cfip &&
920 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
921 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
922 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
923 /*
924 * Let's suspend the erase or write operation when
925 * supported. Note that we currently don't try to
926 * suspend interleaved chips if there is already
927 * another operation suspended (imagine what happens
928 * when one chip was already done with the current
929 * operation while another chip suspended it, then
930 * we resume the whole thing at once). Yes, it
931 * can happen!
932 */
933 map_write(map, CMD(0xb0), adr);
934 map_write(map, CMD(0x70), adr);
935 usec -= xip_elapsed_since(start);
936 suspended = xip_currtime();
937 do {
938 if (xip_elapsed_since(suspended) > 100000) {
939 /*
940 * The chip doesn't want to suspend
941 * after waiting for 100 msecs.
942 * This is a critical error but there
943 * is not much we can do here.
944 */
945 return;
946 }
947 status = map_read(map, adr);
948 } while (!map_word_andequal(map, status, OK, OK));
949
950 /* Suspend succeeded */
951 oldstate = chip->state;
952 if (oldstate == FL_ERASING) {
953 if (!map_word_bitsset(map, status, CMD(0x40)))
954 break;
955 newstate = FL_XIP_WHILE_ERASING;
956 chip->erase_suspended = 1;
957 } else {
958 if (!map_word_bitsset(map, status, CMD(0x04)))
959 break;
960 newstate = FL_XIP_WHILE_WRITING;
961 chip->write_suspended = 1;
962 }
963 chip->state = newstate;
964 map_write(map, CMD(0xff), adr);
965 (void) map_read(map, adr);
966 asm volatile (".rep 8; nop; .endr");
967 local_irq_enable();
Nicolas Pitre6da70122005-05-19 18:05:47 +0100968 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 asm volatile (".rep 8; nop; .endr");
970 cond_resched();
971
972 /*
973 * We're back. However someone else might have
974 * decided to go write to the chip if we are in
975 * a suspended erase state. If so let's wait
976 * until it's done.
977 */
Nicolas Pitre6da70122005-05-19 18:05:47 +0100978 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 while (chip->state != newstate) {
980 DECLARE_WAITQUEUE(wait, current);
981 set_current_state(TASK_UNINTERRUPTIBLE);
982 add_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +0100983 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 schedule();
985 remove_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +0100986 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 /* Disallow XIP again */
989 local_irq_disable();
990
991 /* Resume the write or erase operation */
992 map_write(map, CMD(0xd0), adr);
993 map_write(map, CMD(0x70), adr);
994 chip->state = oldstate;
995 start = xip_currtime();
996 } else if (usec >= 1000000/HZ) {
997 /*
998 * Try to save on CPU power when waiting delay
999 * is at least a system timer tick period.
1000 * No need to be extremely accurate here.
1001 */
1002 xip_cpu_idle();
1003 }
1004 status = map_read(map, adr);
1005 } while (!map_word_andequal(map, status, OK, OK)
1006 && xip_elapsed_since(start) < usec);
1007}
1008
1009#define UDELAY(map, chip, adr, usec) xip_udelay(map, chip, adr, usec)
1010
1011/*
1012 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1013 * the flash is actively programming or erasing since we have to poll for
1014 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001015 * a XIP setup so do it before the actual flash operation in this case
1016 * and stub it out from INVALIDATE_CACHE_UDELAY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001018#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1019 INVALIDATE_CACHED_RANGE(map, from, size)
1020
Alexey Korolevd86d4372006-02-20 18:27:55 -08001021#define INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr, adr, len, usec) \
1022 UDELAY(map, chip, cmd_adr, usec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024/*
1025 * Extra notes:
1026 *
1027 * Activating this XIP support changes the way the code works a bit. For
1028 * example the code to suspend the current process when concurrent access
1029 * happens is never executed because xip_udelay() will always return with the
1030 * same chip state as it was entered with. This is why there is no care for
1031 * the presence of add_wait_queue() or schedule() calls from within a couple
1032 * xip_disable()'d areas of code, like in do_erase_oneblock for example.
1033 * The queueing and scheduling are always happening within xip_udelay().
1034 *
1035 * Similarly, get_chip() and put_chip() just happen to always be executed
1036 * with chip->state set to FL_READY (or FL_XIP_WHILE_*) where flash state
1037 * is in array mode, therefore never executing many cases therein and not
1038 * causing any problem with XIP.
1039 */
1040
1041#else
1042
1043#define xip_disable(map, chip, adr)
1044#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045#define XIP_INVAL_CACHED_RANGE(x...)
1046
Nicolas Pitre6da70122005-05-19 18:05:47 +01001047#define UDELAY(map, chip, adr, usec) \
1048do { \
1049 spin_unlock(chip->mutex); \
1050 cfi_udelay(usec); \
1051 spin_lock(chip->mutex); \
1052} while (0)
1053
Alexey Korolevd86d4372006-02-20 18:27:55 -08001054#define INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr, adr, len, usec) \
Nicolas Pitre6da70122005-05-19 18:05:47 +01001055do { \
1056 spin_unlock(chip->mutex); \
1057 INVALIDATE_CACHED_RANGE(map, adr, len); \
1058 cfi_udelay(usec); \
1059 spin_lock(chip->mutex); \
1060} while (0)
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#endif
1063
1064static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1065{
1066 unsigned long cmd_addr;
1067 struct cfi_private *cfi = map->fldrv_priv;
1068 int ret = 0;
1069
1070 adr += chip->start;
1071
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001072 /* Ensure cmd read/writes are aligned. */
1073 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 spin_lock(chip->mutex);
1076
1077 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1078
1079 if (!ret) {
1080 if (chip->state != FL_POINT && chip->state != FL_READY)
1081 map_write(map, CMD(0xff), cmd_addr);
1082
1083 chip->state = FL_POINT;
1084 chip->ref_point_counter++;
1085 }
1086 spin_unlock(chip->mutex);
1087
1088 return ret;
1089}
1090
1091static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char **mtdbuf)
1092{
1093 struct map_info *map = mtd->priv;
1094 struct cfi_private *cfi = map->fldrv_priv;
1095 unsigned long ofs;
1096 int chipnum;
1097 int ret = 0;
1098
1099 if (!map->virt || (from + len > mtd->size))
1100 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 *mtdbuf = (void *)map->virt + from;
1103 *retlen = 0;
1104
1105 /* Now lock the chip(s) to POINT state */
1106
1107 /* ofs: offset within the first chip that the first read should start */
1108 chipnum = (from >> cfi->chipshift);
1109 ofs = from - (chipnum << cfi->chipshift);
1110
1111 while (len) {
1112 unsigned long thislen;
1113
1114 if (chipnum >= cfi->numchips)
1115 break;
1116
1117 if ((len + ofs -1) >> cfi->chipshift)
1118 thislen = (1<<cfi->chipshift) - ofs;
1119 else
1120 thislen = len;
1121
1122 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1123 if (ret)
1124 break;
1125
1126 *retlen += thislen;
1127 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ofs = 0;
1130 chipnum++;
1131 }
1132 return 0;
1133}
1134
1135static void cfi_intelext_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
1136{
1137 struct map_info *map = mtd->priv;
1138 struct cfi_private *cfi = map->fldrv_priv;
1139 unsigned long ofs;
1140 int chipnum;
1141
1142 /* Now unlock the chip(s) POINT state */
1143
1144 /* ofs: offset within the first chip that the first read should start */
1145 chipnum = (from >> cfi->chipshift);
1146 ofs = from - (chipnum << cfi->chipshift);
1147
1148 while (len) {
1149 unsigned long thislen;
1150 struct flchip *chip;
1151
1152 chip = &cfi->chips[chipnum];
1153 if (chipnum >= cfi->numchips)
1154 break;
1155
1156 if ((len + ofs -1) >> cfi->chipshift)
1157 thislen = (1<<cfi->chipshift) - ofs;
1158 else
1159 thislen = len;
1160
1161 spin_lock(chip->mutex);
1162 if (chip->state == FL_POINT) {
1163 chip->ref_point_counter--;
1164 if(chip->ref_point_counter == 0)
1165 chip->state = FL_READY;
1166 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001167 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 -07001168
1169 put_chip(map, chip, chip->start);
1170 spin_unlock(chip->mutex);
1171
1172 len -= thislen;
1173 ofs = 0;
1174 chipnum++;
1175 }
1176}
1177
1178static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1179{
1180 unsigned long cmd_addr;
1181 struct cfi_private *cfi = map->fldrv_priv;
1182 int ret;
1183
1184 adr += chip->start;
1185
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001186 /* Ensure cmd read/writes are aligned. */
1187 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 spin_lock(chip->mutex);
1190 ret = get_chip(map, chip, cmd_addr, FL_READY);
1191 if (ret) {
1192 spin_unlock(chip->mutex);
1193 return ret;
1194 }
1195
1196 if (chip->state != FL_POINT && chip->state != FL_READY) {
1197 map_write(map, CMD(0xff), cmd_addr);
1198
1199 chip->state = FL_READY;
1200 }
1201
1202 map_copy_from(map, buf, adr, len);
1203
1204 put_chip(map, chip, cmd_addr);
1205
1206 spin_unlock(chip->mutex);
1207 return 0;
1208}
1209
1210static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1211{
1212 struct map_info *map = mtd->priv;
1213 struct cfi_private *cfi = map->fldrv_priv;
1214 unsigned long ofs;
1215 int chipnum;
1216 int ret = 0;
1217
1218 /* ofs: offset within the first chip that the first read should start */
1219 chipnum = (from >> cfi->chipshift);
1220 ofs = from - (chipnum << cfi->chipshift);
1221
1222 *retlen = 0;
1223
1224 while (len) {
1225 unsigned long thislen;
1226
1227 if (chipnum >= cfi->numchips)
1228 break;
1229
1230 if ((len + ofs -1) >> cfi->chipshift)
1231 thislen = (1<<cfi->chipshift) - ofs;
1232 else
1233 thislen = len;
1234
1235 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1236 if (ret)
1237 break;
1238
1239 *retlen += thislen;
1240 len -= thislen;
1241 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 ofs = 0;
1244 chipnum++;
1245 }
1246 return ret;
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001250 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001253 map_word status, status_OK, write_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 unsigned long timeo;
1255 int z, ret=0;
1256
1257 adr += chip->start;
1258
Nicolas Pitre638d9832005-08-06 05:40:46 +01001259 /* Let's determine those according to the interleave only once */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 status_OK = CMD(0x80);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001261 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001262 case FL_WRITING:
1263 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0x40) : CMD(0x41);
1264 break;
1265 case FL_OTP_WRITE:
1266 write_cmd = CMD(0xc0);
1267 break;
1268 default:
1269 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 spin_lock(chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001273 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (ret) {
1275 spin_unlock(chip->mutex);
1276 return ret;
1277 }
1278
1279 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1280 ENABLE_VPP(map);
1281 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001282 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001284 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Alexey Korolevd86d4372006-02-20 18:27:55 -08001286 INVALIDATE_CACHE_UDELAY(map, chip, adr,
Nicolas Pitre6da70122005-05-19 18:05:47 +01001287 adr, map_bankwidth(map),
1288 chip->word_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 timeo = jiffies + (HZ/2);
1291 z = 0;
1292 for (;;) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00001293 if (chip->state != mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* Someone's suspended the write. Sleep */
1295 DECLARE_WAITQUEUE(wait, current);
1296
1297 set_current_state(TASK_UNINTERRUPTIBLE);
1298 add_wait_queue(&chip->wq, &wait);
1299 spin_unlock(chip->mutex);
1300 schedule();
1301 remove_wait_queue(&chip->wq, &wait);
1302 timeo = jiffies + (HZ / 2); /* FIXME */
1303 spin_lock(chip->mutex);
1304 continue;
1305 }
1306
1307 status = map_read(map, adr);
1308 if (map_word_andequal(map, status, status_OK, status_OK))
1309 break;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* OK Still waiting */
1312 if (time_after(jiffies, timeo)) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001313 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 chip->state = FL_STATUS;
1315 xip_enable(map, chip, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001316 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ret = -EIO;
1318 goto out;
1319 }
1320
1321 /* Latency issues. Drop the lock, wait a while and retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 z++;
1323 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 if (!z) {
1326 chip->word_write_time--;
1327 if (!chip->word_write_time)
Nicolas Pitre48436532005-08-06 05:16:52 +01001328 chip->word_write_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001330 if (z > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 chip->word_write_time++;
1332
1333 /* Done and happy. */
1334 chip->state = FL_STATUS;
1335
Nicolas Pitre48436532005-08-06 05:16:52 +01001336 /* check for errors */
1337 if (map_word_bitsset(map, status, CMD(0x1a))) {
1338 unsigned long chipstatus = MERGESTATUS(status);
1339
1340 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001343 xip_enable(map, chip, adr);
1344
1345 if (chipstatus & 0x02) {
1346 ret = -EROFS;
1347 } else if (chipstatus & 0x08) {
1348 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1349 ret = -EIO;
1350 } else {
1351 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1352 ret = -EINVAL;
1353 }
1354
1355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
1358 xip_enable(map, chip, adr);
1359 out: put_chip(map, chip, adr);
1360 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return ret;
1362}
1363
1364
1365static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1366{
1367 struct map_info *map = mtd->priv;
1368 struct cfi_private *cfi = map->fldrv_priv;
1369 int ret = 0;
1370 int chipnum;
1371 unsigned long ofs;
1372
1373 *retlen = 0;
1374 if (!len)
1375 return 0;
1376
1377 chipnum = to >> cfi->chipshift;
1378 ofs = to - (chipnum << cfi->chipshift);
1379
1380 /* If it's not bus-aligned, do the first byte write */
1381 if (ofs & (map_bankwidth(map)-1)) {
1382 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1383 int gap = ofs - bus_ofs;
1384 int n;
1385 map_word datum;
1386
1387 n = min_t(int, len, map_bankwidth(map)-gap);
1388 datum = map_word_ff(map);
1389 datum = map_word_load_partial(map, datum, buf, gap, n);
1390
1391 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001392 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001393 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return ret;
1395
1396 len -= n;
1397 ofs += n;
1398 buf += n;
1399 (*retlen) += n;
1400
1401 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001402 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ofs = 0;
1404 if (chipnum == cfi->numchips)
1405 return 0;
1406 }
1407 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 while(len >= map_bankwidth(map)) {
1410 map_word datum = map_word_load(map, buf);
1411
1412 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001413 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (ret)
1415 return ret;
1416
1417 ofs += map_bankwidth(map);
1418 buf += map_bankwidth(map);
1419 (*retlen) += map_bankwidth(map);
1420 len -= map_bankwidth(map);
1421
1422 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001423 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ofs = 0;
1425 if (chipnum == cfi->numchips)
1426 return 0;
1427 }
1428 }
1429
1430 if (len & (map_bankwidth(map)-1)) {
1431 map_word datum;
1432
1433 datum = map_word_ff(map);
1434 datum = map_word_load_partial(map, datum, buf, 0, len);
1435
1436 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001437 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001438 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 (*retlen) += len;
1442 }
1443
1444 return 0;
1445}
1446
1447
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001448static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001449 unsigned long adr, const struct kvec **pvec,
1450 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitree102d542005-08-06 05:46:59 +01001453 map_word status, status_OK, write_cmd, datum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 unsigned long cmd_adr, timeo;
Nicolas Pitree102d542005-08-06 05:46:59 +01001455 int wbufsize, z, ret=0, word_gap, words;
1456 const struct kvec *vec;
1457 unsigned long vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1460 adr += chip->start;
1461 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Let's determine this according to the interleave only once */
1464 status_OK = CMD(0x80);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001465 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 spin_lock(chip->mutex);
1468 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1469 if (ret) {
1470 spin_unlock(chip->mutex);
1471 return ret;
1472 }
1473
1474 XIP_INVAL_CACHED_RANGE(map, adr, len);
1475 ENABLE_VPP(map);
1476 xip_disable(map, chip, cmd_adr);
1477
David Woodhouse151e7652006-05-14 01:51:54 +01001478 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001479 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 So we must check here and reset those bits if they're set. Otherwise
1481 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001482 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001484 chip->state = FL_STATUS;
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 status = map_read(map, cmd_adr);
1487 if (map_word_bitsset(map, status, CMD(0x30))) {
1488 xip_enable(map, chip, cmd_adr);
1489 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1490 xip_disable(map, chip, cmd_adr);
1491 map_write(map, CMD(0x50), cmd_adr);
1492 map_write(map, CMD(0x70), cmd_adr);
1493 }
1494
1495 chip->state = FL_WRITING_TO_BUFFER;
1496
1497 z = 0;
1498 for (;;) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001499 map_write(map, write_cmd, cmd_adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 status = map_read(map, cmd_adr);
1502 if (map_word_andequal(map, status, status_OK, status_OK))
1503 break;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 UDELAY(map, chip, cmd_adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (++z > 20) {
1508 /* Argh. Not ready for write to buffer */
1509 map_word Xstatus;
1510 map_write(map, CMD(0x70), cmd_adr);
1511 chip->state = FL_STATUS;
1512 Xstatus = map_read(map, cmd_adr);
1513 /* Odd. Clear status bits */
1514 map_write(map, CMD(0x50), cmd_adr);
1515 map_write(map, CMD(0x70), cmd_adr);
1516 xip_enable(map, chip, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001517 printk(KERN_ERR "%s: Chip not ready for buffer write. status = %lx, Xstatus = %lx\n",
1518 map->name, status.x[0], Xstatus.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 ret = -EIO;
1520 goto out;
1521 }
1522 }
1523
Nicolas Pitree102d542005-08-06 05:46:59 +01001524 /* Figure out the number of words to write */
1525 word_gap = (-adr & (map_bankwidth(map)-1));
1526 words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
1527 if (!word_gap) {
1528 words--;
1529 } else {
1530 word_gap = map_bankwidth(map) - word_gap;
1531 adr -= word_gap;
1532 datum = map_word_ff(map);
1533 }
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001536 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001539 vec = *pvec;
1540 vec_seek = *pvec_seek;
1541 do {
1542 int n = map_bankwidth(map) - word_gap;
1543 if (n > vec->iov_len - vec_seek)
1544 n = vec->iov_len - vec_seek;
1545 if (n > len)
1546 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Nicolas Pitree102d542005-08-06 05:46:59 +01001548 if (!word_gap && len < map_bankwidth(map))
1549 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Nicolas Pitree102d542005-08-06 05:46:59 +01001551 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001552 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001553 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Nicolas Pitree102d542005-08-06 05:46:59 +01001555 len -= n;
1556 word_gap += n;
1557 if (!len || word_gap == map_bankwidth(map)) {
1558 map_write(map, datum, adr);
1559 adr += map_bankwidth(map);
1560 word_gap = 0;
1561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Nicolas Pitree102d542005-08-06 05:46:59 +01001563 vec_seek += n;
1564 if (vec_seek == vec->iov_len) {
1565 vec++;
1566 vec_seek = 0;
1567 }
1568 } while (len);
1569 *pvec = vec;
1570 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /* GO GO GO */
1573 map_write(map, CMD(0xd0), cmd_adr);
1574 chip->state = FL_WRITING;
1575
Alexey Korolevd86d4372006-02-20 18:27:55 -08001576 INVALIDATE_CACHE_UDELAY(map, chip, cmd_adr,
1577 adr, len,
Nicolas Pitre6da70122005-05-19 18:05:47 +01001578 chip->buffer_write_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 timeo = jiffies + (HZ/2);
1581 z = 0;
1582 for (;;) {
1583 if (chip->state != FL_WRITING) {
1584 /* Someone's suspended the write. Sleep */
1585 DECLARE_WAITQUEUE(wait, current);
1586 set_current_state(TASK_UNINTERRUPTIBLE);
1587 add_wait_queue(&chip->wq, &wait);
1588 spin_unlock(chip->mutex);
1589 schedule();
1590 remove_wait_queue(&chip->wq, &wait);
1591 timeo = jiffies + (HZ / 2); /* FIXME */
1592 spin_lock(chip->mutex);
1593 continue;
1594 }
1595
1596 status = map_read(map, cmd_adr);
1597 if (map_word_andequal(map, status, status_OK, status_OK))
1598 break;
1599
1600 /* OK Still waiting */
1601 if (time_after(jiffies, timeo)) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001602 map_write(map, CMD(0x70), cmd_adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 chip->state = FL_STATUS;
1604 xip_enable(map, chip, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001605 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ret = -EIO;
1607 goto out;
1608 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* Latency issues. Drop the lock, wait a while and retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 z++;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001612 UDELAY(map, chip, cmd_adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 if (!z) {
1615 chip->buffer_write_time--;
1616 if (!chip->buffer_write_time)
Nicolas Pitre48436532005-08-06 05:16:52 +01001617 chip->buffer_write_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001619 if (z > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 chip->buffer_write_time++;
1621
1622 /* Done and happy. */
1623 chip->state = FL_STATUS;
1624
Nicolas Pitre48436532005-08-06 05:16:52 +01001625 /* check for errors */
1626 if (map_word_bitsset(map, status, CMD(0x1a))) {
1627 unsigned long chipstatus = MERGESTATUS(status);
1628
1629 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001631 map_write(map, CMD(0x70), cmd_adr);
1632 xip_enable(map, chip, cmd_adr);
1633
1634 if (chipstatus & 0x02) {
1635 ret = -EROFS;
1636 } else if (chipstatus & 0x08) {
1637 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1638 ret = -EIO;
1639 } else {
1640 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1641 ret = -EINVAL;
1642 }
1643
1644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646
1647 xip_enable(map, chip, cmd_adr);
1648 out: put_chip(map, chip, cmd_adr);
1649 spin_unlock(chip->mutex);
1650 return ret;
1651}
1652
Nicolas Pitree102d542005-08-06 05:46:59 +01001653static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1654 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct map_info *map = mtd->priv;
1657 struct cfi_private *cfi = map->fldrv_priv;
1658 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1659 int ret = 0;
1660 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001661 unsigned long ofs, vec_seek, i;
1662 size_t len = 0;
1663
1664 for (i = 0; i < count; i++)
1665 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 *retlen = 0;
1668 if (!len)
1669 return 0;
1670
1671 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001672 ofs = to - (chipnum << cfi->chipshift);
1673 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Nicolas Pitree102d542005-08-06 05:46:59 +01001675 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* We must not cross write block boundaries */
1677 int size = wbufsize - (ofs & (wbufsize-1));
1678
1679 if (size > len)
1680 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001681 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001682 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (ret)
1684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 (*retlen) += size;
1688 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (ofs >> cfi->chipshift) {
1691 chipnum ++;
1692 ofs = 0;
1693 if (chipnum == cfi->numchips)
1694 return 0;
1695 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001696
1697 /* Be nice and reschedule with the chip in a usable state for other
1698 processes. */
1699 cond_resched();
1700
Nicolas Pitree102d542005-08-06 05:46:59 +01001701 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return 0;
1704}
1705
Nicolas Pitree102d542005-08-06 05:46:59 +01001706static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1707 size_t len, size_t *retlen, const u_char *buf)
1708{
1709 struct kvec vec;
1710
1711 vec.iov_base = (void *) buf;
1712 vec.iov_len = len;
1713
1714 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1718 unsigned long adr, int len, void *thunk)
1719{
1720 struct cfi_private *cfi = map->fldrv_priv;
1721 map_word status, status_OK;
1722 unsigned long timeo;
1723 int retries = 3;
1724 DECLARE_WAITQUEUE(wait, current);
1725 int ret = 0;
1726
1727 adr += chip->start;
1728
1729 /* Let's determine this according to the interleave only once */
1730 status_OK = CMD(0x80);
1731
1732 retry:
1733 spin_lock(chip->mutex);
1734 ret = get_chip(map, chip, adr, FL_ERASING);
1735 if (ret) {
1736 spin_unlock(chip->mutex);
1737 return ret;
1738 }
1739
1740 XIP_INVAL_CACHED_RANGE(map, adr, len);
1741 ENABLE_VPP(map);
1742 xip_disable(map, chip, adr);
1743
1744 /* Clear the status register first */
1745 map_write(map, CMD(0x50), adr);
1746
1747 /* Now erase */
1748 map_write(map, CMD(0x20), adr);
1749 map_write(map, CMD(0xD0), adr);
1750 chip->state = FL_ERASING;
1751 chip->erase_suspended = 0;
1752
Alexey Korolevd86d4372006-02-20 18:27:55 -08001753 INVALIDATE_CACHE_UDELAY(map, chip, adr,
Nicolas Pitre6da70122005-05-19 18:05:47 +01001754 adr, len,
1755 chip->erase_time*1000/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* FIXME. Use a timer to check this, and return immediately. */
1758 /* Once the state machine's known to be working I'll do that */
1759
1760 timeo = jiffies + (HZ*20);
1761 for (;;) {
1762 if (chip->state != FL_ERASING) {
1763 /* Someone's suspended the erase. Sleep */
1764 set_current_state(TASK_UNINTERRUPTIBLE);
1765 add_wait_queue(&chip->wq, &wait);
1766 spin_unlock(chip->mutex);
1767 schedule();
1768 remove_wait_queue(&chip->wq, &wait);
1769 spin_lock(chip->mutex);
1770 continue;
1771 }
1772 if (chip->erase_suspended) {
1773 /* This erase was suspended and resumed.
1774 Adjust the timeout */
1775 timeo = jiffies + (HZ*20); /* FIXME */
1776 chip->erase_suspended = 0;
1777 }
1778
1779 status = map_read(map, adr);
1780 if (map_word_andequal(map, status, status_OK, status_OK))
1781 break;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* OK Still waiting */
1784 if (time_after(jiffies, timeo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 map_write(map, CMD(0x70), adr);
1786 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 xip_enable(map, chip, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001788 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ret = -EIO;
1790 goto out;
1791 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 /* Latency issues. Drop the lock, wait a while and retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
1797 /* We've broken this before. It doesn't hurt to be safe */
1798 map_write(map, CMD(0x70), adr);
1799 chip->state = FL_STATUS;
1800 status = map_read(map, adr);
1801
Nicolas Pitre48436532005-08-06 05:16:52 +01001802 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001804 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 /* Reset the error bits */
1807 map_write(map, CMD(0x50), adr);
1808 map_write(map, CMD(0x70), adr);
1809 xip_enable(map, chip, adr);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001812 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1813 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 } else if (chipstatus & 0x02) {
1815 /* Protection bit set */
1816 ret = -EROFS;
1817 } else if (chipstatus & 0x8) {
1818 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001819 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001821 } else if (chipstatus & 0x20 && retries--) {
1822 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
1823 timeo = jiffies + HZ;
1824 put_chip(map, chip, adr);
1825 spin_unlock(chip->mutex);
1826 goto retry;
1827 } else {
1828 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 -07001829 ret = -EIO;
1830 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001831
1832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834
Nicolas Pitre48436532005-08-06 05:16:52 +01001835 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 out: put_chip(map, chip, adr);
1837 spin_unlock(chip->mutex);
1838 return ret;
1839}
1840
1841int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
1842{
1843 unsigned long ofs, len;
1844 int ret;
1845
1846 ofs = instr->addr;
1847 len = instr->len;
1848
1849 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1850 if (ret)
1851 return ret;
1852
1853 instr->state = MTD_ERASE_DONE;
1854 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return 0;
1857}
1858
1859static void cfi_intelext_sync (struct mtd_info *mtd)
1860{
1861 struct map_info *map = mtd->priv;
1862 struct cfi_private *cfi = map->fldrv_priv;
1863 int i;
1864 struct flchip *chip;
1865 int ret = 0;
1866
1867 for (i=0; !ret && i<cfi->numchips; i++) {
1868 chip = &cfi->chips[i];
1869
1870 spin_lock(chip->mutex);
1871 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1872
1873 if (!ret) {
1874 chip->oldstate = chip->state;
1875 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001876 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * as the whole point is that nobody can do anything
1878 * with the chip now anyway.
1879 */
1880 }
1881 spin_unlock(chip->mutex);
1882 }
1883
1884 /* Unlock the chips again */
1885
1886 for (i--; i >=0; i--) {
1887 chip = &cfi->chips[i];
1888
1889 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (chip->state == FL_SYNCING) {
1892 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00001893 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 wake_up(&chip->wq);
1895 }
1896 spin_unlock(chip->mutex);
1897 }
1898}
1899
1900#ifdef DEBUG_LOCK_BITS
1901static int __xipram do_printlockstatus_oneblock(struct map_info *map,
1902 struct flchip *chip,
1903 unsigned long adr,
1904 int len, void *thunk)
1905{
1906 struct cfi_private *cfi = map->fldrv_priv;
1907 int status, ofs_factor = cfi->interleave * cfi->device_type;
1908
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001909 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01001911 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 chip->state = FL_JEDEC_QUERY;
1913 status = cfi_read_query(map, adr+(2*ofs_factor));
1914 xip_enable(map, chip, 0);
1915 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
1916 adr, status);
1917 return 0;
1918}
1919#endif
1920
1921#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
1922#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
1923
1924static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
1925 unsigned long adr, int len, void *thunk)
1926{
1927 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001928 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 map_word status, status_OK;
1930 unsigned long timeo = jiffies + HZ;
1931 int ret;
1932
1933 adr += chip->start;
1934
1935 /* Let's determine this according to the interleave only once */
1936 status_OK = CMD(0x80);
1937
1938 spin_lock(chip->mutex);
1939 ret = get_chip(map, chip, adr, FL_LOCKING);
1940 if (ret) {
1941 spin_unlock(chip->mutex);
1942 return ret;
1943 }
1944
1945 ENABLE_VPP(map);
1946 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 map_write(map, CMD(0x60), adr);
1949 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
1950 map_write(map, CMD(0x01), adr);
1951 chip->state = FL_LOCKING;
1952 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
1953 map_write(map, CMD(0xD0), adr);
1954 chip->state = FL_UNLOCKING;
1955 } else
1956 BUG();
1957
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001958 /*
1959 * If Instant Individual Block Locking supported then no need
1960 * to delay.
1961 */
1962
Nicolas Pitre6da70122005-05-19 18:05:47 +01001963 if (!extp || !(extp->FeatureSupport & (1 << 5)))
Todd Poynor9a6e73e2005-03-29 23:06:40 +01001964 UDELAY(map, chip, adr, 1000000/HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* FIXME. Use a timer to check this, and return immediately. */
1967 /* Once the state machine's known to be working I'll do that */
1968
1969 timeo = jiffies + (HZ*20);
1970 for (;;) {
1971
1972 status = map_read(map, adr);
1973 if (map_word_andequal(map, status, status_OK, status_OK))
1974 break;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* OK Still waiting */
1977 if (time_after(jiffies, timeo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 map_write(map, CMD(0x70), adr);
1979 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 xip_enable(map, chip, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001981 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 put_chip(map, chip, adr);
1983 spin_unlock(chip->mutex);
1984 return -EIO;
1985 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /* Latency issues. Drop the lock, wait a while and retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 UDELAY(map, chip, adr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* Done and happy. */
1992 chip->state = FL_STATUS;
1993 xip_enable(map, chip, adr);
1994 put_chip(map, chip, adr);
1995 spin_unlock(chip->mutex);
1996 return 0;
1997}
1998
1999static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
2000{
2001 int ret;
2002
2003#ifdef DEBUG_LOCK_BITS
2004 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2005 __FUNCTION__, ofs, len);
2006 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2007 ofs, len, 0);
2008#endif
2009
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002010 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013#ifdef DEBUG_LOCK_BITS
2014 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2015 __FUNCTION__, ret);
2016 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2017 ofs, len, 0);
2018#endif
2019
2020 return ret;
2021}
2022
2023static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
2024{
2025 int ret;
2026
2027#ifdef DEBUG_LOCK_BITS
2028 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2029 __FUNCTION__, ofs, len);
2030 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2031 ofs, len, 0);
2032#endif
2033
2034 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2035 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037#ifdef DEBUG_LOCK_BITS
2038 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2039 __FUNCTION__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002040 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 ofs, len, 0);
2042#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return ret;
2045}
2046
Nicolas Pitref77814d2005-02-08 17:11:19 +00002047#ifdef CONFIG_MTD_OTP
2048
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002049typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002050 u_long data_offset, u_char *buf, u_int size,
2051 u_long prot_offset, u_int groupno, u_int groupsize);
2052
2053static int __xipram
2054do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2055 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2056{
2057 struct cfi_private *cfi = map->fldrv_priv;
2058 int ret;
2059
2060 spin_lock(chip->mutex);
2061 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2062 if (ret) {
2063 spin_unlock(chip->mutex);
2064 return ret;
2065 }
2066
2067 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002068 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002069
2070 xip_disable(map, chip, chip->start);
2071 if (chip->state != FL_JEDEC_QUERY) {
2072 map_write(map, CMD(0x90), chip->start);
2073 chip->state = FL_JEDEC_QUERY;
2074 }
2075 map_copy_from(map, buf, chip->start + offset, size);
2076 xip_enable(map, chip, chip->start);
2077
2078 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002079 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002080
2081 put_chip(map, chip, chip->start);
2082 spin_unlock(chip->mutex);
2083 return 0;
2084}
2085
2086static int
2087do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2088 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2089{
2090 int ret;
2091
2092 while (size) {
2093 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2094 int gap = offset - bus_ofs;
2095 int n = min_t(int, size, map_bankwidth(map)-gap);
2096 map_word datum = map_word_ff(map);
2097
2098 datum = map_word_load_partial(map, datum, buf, gap, n);
2099 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002100 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002101 return ret;
2102
2103 offset += n;
2104 buf += n;
2105 size -= n;
2106 }
2107
2108 return 0;
2109}
2110
2111static int
2112do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2113 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2114{
2115 struct cfi_private *cfi = map->fldrv_priv;
2116 map_word datum;
2117
2118 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002119 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002120 return -EXDEV;
2121
2122 datum = map_word_ff(map);
2123 datum = map_word_clr(map, datum, CMD(1 << grpno));
2124 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2125}
2126
2127static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2128 size_t *retlen, u_char *buf,
2129 otp_op_t action, int user_regs)
2130{
2131 struct map_info *map = mtd->priv;
2132 struct cfi_private *cfi = map->fldrv_priv;
2133 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2134 struct flchip *chip;
2135 struct cfi_intelext_otpinfo *otp;
2136 u_long devsize, reg_prot_offset, data_offset;
2137 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2138 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2139 int ret;
2140
2141 *retlen = 0;
2142
2143 /* Check that we actually have some OTP registers */
2144 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2145 return -ENODATA;
2146
2147 /* we need real chips here not virtual ones */
2148 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2149 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002150 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002151
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002152 /* Some chips have OTP located in the _top_ partition only.
2153 For example: Intel 28F256L18T (T means top-parameter device) */
2154 if (cfi->mfr == MANUFACTURER_INTEL) {
2155 switch (cfi->id) {
2156 case 0x880b:
2157 case 0x880c:
2158 case 0x880d:
2159 chip_num = chip_step - 1;
2160 }
2161 }
2162
2163 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002164 chip = &cfi->chips[chip_num];
2165 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2166
2167 /* first OTP region */
2168 field = 0;
2169 reg_prot_offset = extp->ProtRegAddr;
2170 reg_fact_groups = 1;
2171 reg_fact_size = 1 << extp->FactProtRegSize;
2172 reg_user_groups = 1;
2173 reg_user_size = 1 << extp->UserProtRegSize;
2174
2175 while (len > 0) {
2176 /* flash geometry fixup */
2177 data_offset = reg_prot_offset + 1;
2178 data_offset *= cfi->interleave * cfi->device_type;
2179 reg_prot_offset *= cfi->interleave * cfi->device_type;
2180 reg_fact_size *= cfi->interleave;
2181 reg_user_size *= cfi->interleave;
2182
2183 if (user_regs) {
2184 groups = reg_user_groups;
2185 groupsize = reg_user_size;
2186 /* skip over factory reg area */
2187 groupno = reg_fact_groups;
2188 data_offset += reg_fact_groups * reg_fact_size;
2189 } else {
2190 groups = reg_fact_groups;
2191 groupsize = reg_fact_size;
2192 groupno = 0;
2193 }
2194
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002195 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002196 if (!action) {
2197 /*
2198 * Special case: if action is NULL
2199 * we fill buf with otp_info records.
2200 */
2201 struct otp_info *otpinfo;
2202 map_word lockword;
2203 len -= sizeof(struct otp_info);
2204 if (len <= 0)
2205 return -ENOSPC;
2206 ret = do_otp_read(map, chip,
2207 reg_prot_offset,
2208 (u_char *)&lockword,
2209 map_bankwidth(map),
2210 0, 0, 0);
2211 if (ret)
2212 return ret;
2213 otpinfo = (struct otp_info *)buf;
2214 otpinfo->start = from;
2215 otpinfo->length = groupsize;
2216 otpinfo->locked =
2217 !map_word_bitsset(map, lockword,
2218 CMD(1 << groupno));
2219 from += groupsize;
2220 buf += sizeof(*otpinfo);
2221 *retlen += sizeof(*otpinfo);
2222 } else if (from >= groupsize) {
2223 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002224 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002225 } else {
2226 int size = groupsize;
2227 data_offset += from;
2228 size -= from;
2229 from = 0;
2230 if (size > len)
2231 size = len;
2232 ret = action(map, chip, data_offset,
2233 buf, size, reg_prot_offset,
2234 groupno, groupsize);
2235 if (ret < 0)
2236 return ret;
2237 buf += size;
2238 len -= size;
2239 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002240 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002241 }
2242 groupno++;
2243 groups--;
2244 }
2245
2246 /* next OTP region */
2247 if (++field == extp->NumProtectionFields)
2248 break;
2249 reg_prot_offset = otp->ProtRegAddr;
2250 reg_fact_groups = otp->FactGroups;
2251 reg_fact_size = 1 << otp->FactProtRegSize;
2252 reg_user_groups = otp->UserGroups;
2253 reg_user_size = 1 << otp->UserProtRegSize;
2254 otp++;
2255 }
2256 }
2257
2258 return 0;
2259}
2260
2261static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2262 size_t len, size_t *retlen,
2263 u_char *buf)
2264{
2265 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2266 buf, do_otp_read, 0);
2267}
2268
2269static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2270 size_t len, size_t *retlen,
2271 u_char *buf)
2272{
2273 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2274 buf, do_otp_read, 1);
2275}
2276
2277static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2278 size_t len, size_t *retlen,
2279 u_char *buf)
2280{
2281 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2282 buf, do_otp_write, 1);
2283}
2284
2285static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2286 loff_t from, size_t len)
2287{
2288 size_t retlen;
2289 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2290 NULL, do_otp_lock, 1);
2291}
2292
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002293static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002294 struct otp_info *buf, size_t len)
2295{
2296 size_t retlen;
2297 int ret;
2298
2299 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2300 return ret ? : retlen;
2301}
2302
2303static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2304 struct otp_info *buf, size_t len)
2305{
2306 size_t retlen;
2307 int ret;
2308
2309 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2310 return ret ? : retlen;
2311}
2312
2313#endif
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315static int cfi_intelext_suspend(struct mtd_info *mtd)
2316{
2317 struct map_info *map = mtd->priv;
2318 struct cfi_private *cfi = map->fldrv_priv;
2319 int i;
2320 struct flchip *chip;
2321 int ret = 0;
2322
2323 for (i=0; !ret && i<cfi->numchips; i++) {
2324 chip = &cfi->chips[i];
2325
2326 spin_lock(chip->mutex);
2327
2328 switch (chip->state) {
2329 case FL_READY:
2330 case FL_STATUS:
2331 case FL_CFI_QUERY:
2332 case FL_JEDEC_QUERY:
2333 if (chip->oldstate == FL_READY) {
2334 chip->oldstate = chip->state;
2335 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002336 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 * as the whole point is that nobody can do anything
2338 * with the chip now anyway.
2339 */
2340 } else {
2341 /* There seems to be an operation pending. We must wait for it. */
2342 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2343 ret = -EAGAIN;
2344 }
2345 break;
2346 default:
2347 /* Should we actually wait? Once upon a time these routines weren't
2348 allowed to. Or should we return -EAGAIN, because the upper layers
2349 ought to have already shut down anything which was using the device
2350 anyway? The latter for now. */
2351 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2352 ret = -EAGAIN;
2353 case FL_PM_SUSPENDED:
2354 break;
2355 }
2356 spin_unlock(chip->mutex);
2357 }
2358
2359 /* Unlock the chips again */
2360
2361 if (ret) {
2362 for (i--; i >=0; i--) {
2363 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (chip->state == FL_PM_SUSPENDED) {
2368 /* No need to force it into a known state here,
2369 because we're returning failure, and it didn't
2370 get power cycled */
2371 chip->state = chip->oldstate;
2372 chip->oldstate = FL_READY;
2373 wake_up(&chip->wq);
2374 }
2375 spin_unlock(chip->mutex);
2376 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002377 }
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return ret;
2380}
2381
2382static void cfi_intelext_resume(struct mtd_info *mtd)
2383{
2384 struct map_info *map = mtd->priv;
2385 struct cfi_private *cfi = map->fldrv_priv;
2386 int i;
2387 struct flchip *chip;
2388
2389 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 chip = &cfi->chips[i];
2392
2393 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /* Go to known state. Chip may have been power cycled */
2396 if (chip->state == FL_PM_SUSPENDED) {
2397 map_write(map, CMD(0xFF), cfi->chips[i].start);
2398 chip->oldstate = chip->state = FL_READY;
2399 wake_up(&chip->wq);
2400 }
2401
2402 spin_unlock(chip->mutex);
2403 }
2404}
2405
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002406static int cfi_intelext_reset(struct mtd_info *mtd)
2407{
2408 struct map_info *map = mtd->priv;
2409 struct cfi_private *cfi = map->fldrv_priv;
2410 int i, ret;
2411
2412 for (i=0; i < cfi->numchips; i++) {
2413 struct flchip *chip = &cfi->chips[i];
2414
2415 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002416 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002417 flash is accessible for soft reboot. */
2418 spin_lock(chip->mutex);
2419 ret = get_chip(map, chip, chip->start, FL_SYNCING);
2420 if (!ret) {
2421 map_write(map, CMD(0xff), chip->start);
2422 chip->state = FL_READY;
2423 }
2424 spin_unlock(chip->mutex);
2425 }
2426
2427 return 0;
2428}
2429
2430static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2431 void *v)
2432{
2433 struct mtd_info *mtd;
2434
2435 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2436 cfi_intelext_reset(mtd);
2437 return NOTIFY_DONE;
2438}
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static void cfi_intelext_destroy(struct mtd_info *mtd)
2441{
2442 struct map_info *map = mtd->priv;
2443 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002444 cfi_intelext_reset(mtd);
2445 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 kfree(cfi->cmdset_priv);
2447 kfree(cfi->cfiq);
2448 kfree(cfi->chips[0].priv);
2449 kfree(cfi);
2450 kfree(mtd->eraseregions);
2451}
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453MODULE_LICENSE("GPL");
2454MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2455MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002456MODULE_ALIAS("cfi_cmdset_0003");
2457MODULE_ALIAS("cfi_cmdset_0200");