blob: e772803b49590b7b65372bfb3200fdf9d4adac17 [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 *
Thomas Gleixner1f948b42005-11-07 11:15:37 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 10/10/2000 Nicolas Pitre <nico@cam.org>
9 * - completely revamped method functions so they are aware and
10 * independent of the flash geometry (buswidth, interleave, etc.)
11 * - scalability vs code size is completely set at compile-time
12 * (see include/linux/mtd/cfi.h for selection)
13 * - optimized write buffer method
14 * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
15 * - reworked lock/unlock/erase support for var size flash
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -080016 * 21/03/2007 Rodolfo Giometti <giometti@linux.it>
17 * - auto unlock sectors on resume for auto locking flash on power up
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
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>
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -080033#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/mtd/xip.h>
35#include <linux/mtd/map.h>
36#include <linux/mtd/mtd.h>
37#include <linux/mtd/compatmac.h>
38#include <linux/mtd/cfi.h>
39
40/* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
41/* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
42
43// debugging, turns off buffer write mode if set to 1
44#define FORCE_WORD_WRITE 0
45
46#define MANUFACTURER_INTEL 0x0089
47#define I82802AB 0x00ad
48#define I82802AC 0x00ac
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -030049#define PF38F4476 0x881c
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define MANUFACTURER_ST 0x0020
51#define M50LPW080 0x002F
Nate Casedeb1a5f2008-05-13 14:45:29 -050052#define M50FLW080A 0x0080
53#define M50FLW080B 0x0081
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +010054#define AT49BV640D 0x02de
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
58static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
Nicolas Pitree102d542005-08-06 05:46:59 +010059static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
61static void cfi_intelext_sync (struct mtd_info *);
Adrian Hunter69423d92008-12-10 13:37:21 +000062static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
63static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Todd Poynor8048d2f2005-03-31 00:57:33 +010064#ifdef CONFIG_MTD_OTP
Nicolas Pitref77814d2005-02-08 17:11:19 +000065static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
66static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
67static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
68static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
69static int cfi_intelext_get_fact_prot_info (struct mtd_info *,
70 struct otp_info *, size_t);
71static int cfi_intelext_get_user_prot_info (struct mtd_info *,
72 struct otp_info *, size_t);
Todd Poynor8048d2f2005-03-31 00:57:33 +010073#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int cfi_intelext_suspend (struct mtd_info *);
75static void cfi_intelext_resume (struct mtd_info *);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010076static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static void cfi_intelext_destroy(struct mtd_info *);
79
80struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
81
82static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
83static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
84
85static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
Jared Hulberta98889f2008-04-29 23:26:49 -070086 size_t *retlen, void **virt, resource_size_t *phys);
87static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Alexey Korolev5a37cf12007-10-22 17:55:20 +010089static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
91static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
92#include "fwh_lock.h"
93
94
95
96/*
97 * *********** SETUP AND PROBE BITS ***********
98 */
99
100static struct mtd_chip_driver cfi_intelext_chipdrv = {
101 .probe = NULL, /* Not usable directly */
102 .destroy = cfi_intelext_destroy,
103 .name = "cfi_cmdset_0001",
104 .module = THIS_MODULE
105};
106
107/* #define DEBUG_LOCK_BITS */
108/* #define DEBUG_CFI_FEATURES */
109
110#ifdef DEBUG_CFI_FEATURES
111static void cfi_tell_features(struct cfi_pri_intelext *extp)
112{
113 int i;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100114 printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
116 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
117 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
118 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
119 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
120 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
121 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
122 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
123 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
124 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
125 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100126 printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
127 for (i=11; i<32; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000128 if (extp->FeatureSupport & (1<<i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 printk(" - Unknown Bit %X: supported\n", i);
130 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
133 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
134 for (i=1; i<8; i++) {
135 if (extp->SuspendCmdSupport & (1<<i))
136 printk(" - Unknown Bit %X: supported\n", i);
137 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
140 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100141 printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
142 for (i=2; i<3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (extp->BlkStatusRegMask & (1<<i))
144 printk(" - Unknown Bit %X Active: yes\n",i);
145 }
Nicolas Pitre638d9832005-08-06 05:40:46 +0100146 printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
147 printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
148 for (i=6; i<16; i++) {
149 if (extp->BlkStatusRegMask & (1<<i))
150 printk(" - Unknown Bit %X Active: yes\n",i);
151 }
152
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000153 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
155 if (extp->VppOptimal)
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000156 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
158}
159#endif
160
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100161/* Atmel chips don't use the same PRI format as Intel chips */
162static void fixup_convert_atmel_pri(struct mtd_info *mtd, void *param)
163{
164 struct map_info *map = mtd->priv;
165 struct cfi_private *cfi = map->fldrv_priv;
166 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
167 struct cfi_pri_atmel atmel_pri;
168 uint32_t features = 0;
169
170 /* Reverse byteswapping */
171 extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
172 extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
173 extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
174
175 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
176 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
177
178 printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
179
180 if (atmel_pri.Features & 0x01) /* chip erase supported */
181 features |= (1<<0);
182 if (atmel_pri.Features & 0x02) /* erase suspend supported */
183 features |= (1<<1);
184 if (atmel_pri.Features & 0x04) /* program suspend supported */
185 features |= (1<<2);
186 if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
187 features |= (1<<9);
188 if (atmel_pri.Features & 0x20) /* page mode read supported */
189 features |= (1<<7);
190 if (atmel_pri.Features & 0x40) /* queued erase supported */
191 features |= (1<<4);
192 if (atmel_pri.Features & 0x80) /* Protection bits supported */
193 features |= (1<<6);
194
195 extp->FeatureSupport = features;
196
197 /* burst write mode not supported */
198 cfi->cfiq->BufWriteTimeoutTyp = 0;
199 cfi->cfiq->BufWriteTimeoutMax = 0;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000203/* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static void fixup_intel_strataflash(struct mtd_info *mtd, void* param)
205{
206 struct map_info *map = mtd->priv;
207 struct cfi_private *cfi = map->fldrv_priv;
Alexander Belyakov91949d62008-05-04 14:32:58 +0400208 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
211 "erase on write disabled.\n");
212 extp->SuspendCmdSupport &= ~1;
213}
214#endif
215
216#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
217static void fixup_no_write_suspend(struct mtd_info *mtd, void* param)
218{
219 struct map_info *map = mtd->priv;
220 struct cfi_private *cfi = map->fldrv_priv;
221 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
222
223 if (cfip && (cfip->FeatureSupport&4)) {
224 cfip->FeatureSupport &= ~4;
225 printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
226 }
227}
228#endif
229
230static void fixup_st_m28w320ct(struct mtd_info *mtd, void* param)
231{
232 struct map_info *map = mtd->priv;
233 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
236 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
237}
238
239static void fixup_st_m28w320cb(struct mtd_info *mtd, void* param)
240{
241 struct map_info *map = mtd->priv;
242 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Note this is done after the region info is endian swapped */
245 cfi->cfiq->EraseRegionInfo[1] =
246 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
247};
248
249static void fixup_use_point(struct mtd_info *mtd, void *param)
250{
251 struct map_info *map = mtd->priv;
252 if (!mtd->point && map_is_linear(map)) {
253 mtd->point = cfi_intelext_point;
254 mtd->unpoint = cfi_intelext_unpoint;
255 }
256}
257
258static void fixup_use_write_buffers(struct mtd_info *mtd, void *param)
259{
260 struct map_info *map = mtd->priv;
261 struct cfi_private *cfi = map->fldrv_priv;
262 if (cfi->cfiq->BufWriteTimeoutTyp) {
263 printk(KERN_INFO "Using buffer write method\n" );
264 mtd->write = cfi_intelext_write_buffers;
Nicolas Pitree102d542005-08-06 05:46:59 +0100265 mtd->writev = cfi_intelext_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267}
268
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800269/*
270 * Some chips power-up with all sectors locked by default.
271 */
Justin Treone619a752008-01-30 10:25:49 -0800272static void fixup_unlock_powerup_lock(struct mtd_info *mtd, void *param)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800273{
Justin Treone619a752008-01-30 10:25:49 -0800274 struct map_info *map = mtd->priv;
275 struct cfi_private *cfi = map->fldrv_priv;
276 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
277
278 if (cfip->FeatureSupport&32) {
279 printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
280 mtd->flags |= MTD_POWERUP_LOCK;
281 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static struct cfi_fixup cfi_fixup_table[] = {
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100285 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000287 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash, NULL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#endif
289#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
290 { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend, NULL },
291#endif
292#if !FORCE_WORD_WRITE
293 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers, NULL },
294#endif
295 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct, NULL },
296 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb, NULL },
Justin Treone619a752008-01-30 10:25:49 -0800297 { MANUFACTURER_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 { 0, 0, NULL, NULL }
299};
300
301static struct cfi_fixup jedec_fixup_table[] = {
302 { MANUFACTURER_INTEL, I82802AB, fixup_use_fwh_lock, NULL, },
303 { MANUFACTURER_INTEL, I82802AC, fixup_use_fwh_lock, NULL, },
304 { MANUFACTURER_ST, M50LPW080, fixup_use_fwh_lock, NULL, },
Nate Casedeb1a5f2008-05-13 14:45:29 -0500305 { MANUFACTURER_ST, M50FLW080A, fixup_use_fwh_lock, NULL, },
306 { MANUFACTURER_ST, M50FLW080B, fixup_use_fwh_lock, NULL, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 { 0, 0, NULL, NULL }
308};
309static struct cfi_fixup fixup_table[] = {
310 /* The CFI vendor ids and the JEDEC vendor IDs appear
311 * to be common. It is like the devices id's are as
312 * well. This table is to pick all cases where
313 * we know that is the case.
314 */
315 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point, NULL },
316 { 0, 0, NULL, NULL }
317};
318
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300319static void cfi_fixup_major_minor(struct cfi_private *cfi,
320 struct cfi_pri_intelext *extp)
321{
322 if (cfi->mfr == MANUFACTURER_INTEL &&
323 cfi->id == PF38F4476 && extp->MinorVersion == '3')
324 extp->MinorVersion = '1';
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static inline struct cfi_pri_intelext *
328read_pri_intelext(struct map_info *map, __u16 adr)
329{
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300330 struct cfi_private *cfi = map->fldrv_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct cfi_pri_intelext *extp;
332 unsigned int extp_size = sizeof(*extp);
333
334 again:
335 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
336 if (!extp)
337 return NULL;
338
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300339 cfi_fixup_major_minor(cfi, extp);
340
Todd Poynord88f9772005-07-20 22:01:17 +0100341 if (extp->MajorVersion != '1' ||
Alexey Korolevb1c9c9b2007-11-23 09:31:56 +0000342 (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
Todd Poynord88f9772005-07-20 22:01:17 +0100343 printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
344 "version %c.%c.\n", extp->MajorVersion,
345 extp->MinorVersion);
346 kfree(extp);
347 return NULL;
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* Do some byteswapping if necessary */
351 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
352 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
353 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
354
Nicolas Pitre638d9832005-08-06 05:40:46 +0100355 if (extp->MajorVersion == '1' && extp->MinorVersion >= '3') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned int extra_size = 0;
357 int nb_parts, i;
358
359 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000360 extra_size += (extp->NumProtectionFields - 1) *
361 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100364 extra_size += 2;
365 if (extp_size < sizeof(*extp) + extra_size)
366 goto need_more;
367 extra_size += extp->extra[extra_size-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Number of hardware-partitions */
370 extra_size += 1;
371 if (extp_size < sizeof(*extp) + extra_size)
372 goto need_more;
373 nb_parts = extp->extra[extra_size - 1];
374
Nicolas Pitre638d9832005-08-06 05:40:46 +0100375 /* skip the sizeof(partregion) field in CFI 1.4 */
376 if (extp->MinorVersion >= '4')
377 extra_size += 2;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 for (i = 0; i < nb_parts; i++) {
380 struct cfi_intelext_regioninfo *rinfo;
381 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
382 extra_size += sizeof(*rinfo);
383 if (extp_size < sizeof(*extp) + extra_size)
384 goto need_more;
385 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
386 extra_size += (rinfo->NumBlockTypes - 1)
387 * sizeof(struct cfi_intelext_blockinfo);
388 }
389
Nicolas Pitre638d9832005-08-06 05:40:46 +0100390 if (extp->MinorVersion >= '4')
391 extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (extp_size < sizeof(*extp) + extra_size) {
394 need_more:
395 extp_size = sizeof(*extp) + extra_size;
396 kfree(extp);
397 if (extp_size > 4096) {
398 printk(KERN_ERR
399 "%s: cfi_pri_intelext is too fat\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700400 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return NULL;
402 }
403 goto again;
404 }
405 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return extp;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
411{
412 struct cfi_private *cfi = map->fldrv_priv;
413 struct mtd_info *mtd;
414 int i;
415
Burman Yan95b93a02006-11-15 21:10:29 +0200416 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!mtd) {
418 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
419 return NULL;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 mtd->priv = map;
422 mtd->type = MTD_NORFLASH;
423
424 /* Fill in the default mtd operations */
425 mtd->erase = cfi_intelext_erase_varsize;
426 mtd->read = cfi_intelext_read;
427 mtd->write = cfi_intelext_write_words;
428 mtd->sync = cfi_intelext_sync;
429 mtd->lock = cfi_intelext_lock;
430 mtd->unlock = cfi_intelext_unlock;
431 mtd->suspend = cfi_intelext_suspend;
432 mtd->resume = cfi_intelext_resume;
433 mtd->flags = MTD_CAP_NORFLASH;
434 mtd->name = map->name;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400435 mtd->writesize = 1;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100436
437 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (cfi->cfi_mode == CFI_MODE_CFI) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000440 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * It's a real CFI chip, not one for which the probe
442 * routine faked a CFI structure. So we read the feature
443 * table from it.
444 */
445 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
446 struct cfi_pri_intelext *extp;
447
448 extp = read_pri_intelext(map, adr);
449 if (!extp) {
450 kfree(mtd);
451 return NULL;
452 }
453
454 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000455 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 cfi_fixup(mtd, cfi_fixup_table);
458
459#ifdef DEBUG_CFI_FEATURES
460 /* Tell the user about it in lots of lovely detail */
461 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if(extp->SuspendCmdSupport & 1) {
465 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
466 }
467 }
468 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
469 /* Apply jedec specific fixups */
470 cfi_fixup(mtd, jedec_fixup_table);
471 }
472 /* Apply generic fixups */
473 cfi_fixup(mtd, fixup_table);
474
475 for (i=0; i< cfi->numchips; i++) {
David Woodhouse2a5bd592007-02-09 14:39:10 +0000476 if (cfi->cfiq->WordWriteTimeoutTyp)
477 cfi->chips[i].word_write_time =
478 1<<cfi->cfiq->WordWriteTimeoutTyp;
479 else
480 cfi->chips[i].word_write_time = 50000;
481
482 if (cfi->cfiq->BufWriteTimeoutTyp)
483 cfi->chips[i].buffer_write_time =
484 1<<cfi->cfiq->BufWriteTimeoutTyp;
485 /* No default; if it isn't specified, we won't use it */
486
487 if (cfi->cfiq->BlockEraseTimeoutTyp)
488 cfi->chips[i].erase_time =
489 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
490 else
491 cfi->chips[i].erase_time = 2000000;
492
Anders Grafströme93cafe2008-08-05 18:37:41 +0200493 if (cfi->cfiq->WordWriteTimeoutTyp &&
494 cfi->cfiq->WordWriteTimeoutMax)
495 cfi->chips[i].word_write_time_max =
496 1<<(cfi->cfiq->WordWriteTimeoutTyp +
497 cfi->cfiq->WordWriteTimeoutMax);
498 else
499 cfi->chips[i].word_write_time_max = 50000 * 8;
500
501 if (cfi->cfiq->BufWriteTimeoutTyp &&
502 cfi->cfiq->BufWriteTimeoutMax)
503 cfi->chips[i].buffer_write_time_max =
504 1<<(cfi->cfiq->BufWriteTimeoutTyp +
505 cfi->cfiq->BufWriteTimeoutMax);
506
507 if (cfi->cfiq->BlockEraseTimeoutTyp &&
508 cfi->cfiq->BlockEraseTimeoutMax)
509 cfi->chips[i].erase_time_max =
510 1000<<(cfi->cfiq->BlockEraseTimeoutTyp +
511 cfi->cfiq->BlockEraseTimeoutMax);
512 else
513 cfi->chips[i].erase_time_max = 2000000 * 8;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 cfi->chips[i].ref_point_counter = 0;
Simon Voglc314b6f2006-02-24 13:04:09 -0800516 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 map->fldrv = &cfi_intelext_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return cfi_intelext_setup(mtd);
522}
David Woodhousea15bdee2006-05-08 22:35:05 +0100523struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
524struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
525EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
526EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
527EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
530{
531 struct map_info *map = mtd->priv;
532 struct cfi_private *cfi = map->fldrv_priv;
533 unsigned long offset = 0;
534 int i,j;
535 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
536
537 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
538
539 mtd->size = devsize * cfi->numchips;
540
541 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000542 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000544 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
546 goto setup_err;
547 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
550 unsigned long ernum, ersize;
551 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
552 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
553
554 if (mtd->erasesize < ersize) {
555 mtd->erasesize = ersize;
556 }
557 for (j=0; j<cfi->numchips; j++) {
558 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
559 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
560 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800561 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 offset += (ersize * ernum);
564 }
565
566 if (offset != devsize) {
567 /* Argh */
568 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
569 goto setup_err;
570 }
571
572 for (i=0; i<mtd->numeraseregions;i++){
Adrian Hunter69423d92008-12-10 13:37:21 +0000573 printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n",
574 i,(unsigned long long)mtd->eraseregions[i].offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mtd->eraseregions[i].erasesize,
576 mtd->eraseregions[i].numblocks);
577 }
578
Nicolas Pitref77814d2005-02-08 17:11:19 +0000579#ifdef CONFIG_MTD_OTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000581 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
582 mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
583 mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
584 mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
585 mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#endif
587
588 /* This function has the potential to distort the reality
589 a bit and therefore should be called last. */
590 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
591 goto setup_err;
592
593 __module_get(THIS_MODULE);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100594 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return mtd;
596
597 setup_err:
598 if(mtd) {
Jesper Juhlfa671642005-11-07 01:01:27 -0800599 kfree(mtd->eraseregions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 kfree(mtd);
601 }
602 kfree(cfi->cmdset_priv);
603 return NULL;
604}
605
606static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
607 struct cfi_private **pcfi)
608{
609 struct map_info *map = mtd->priv;
610 struct cfi_private *cfi = *pcfi;
611 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
612
613 /*
Jesper Juhl8f1a8662007-07-05 02:18:34 +0200614 * Probing of multi-partition flash chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 *
616 * To support multiple partitions when available, we simply arrange
617 * for each of them to have their own flchip structure even if they
618 * are on the same physical chip. This means completely recreating
619 * a new cfi_private structure right here which is a blatent code
620 * layering violation, but this is still the least intrusive
621 * arrangement at this point. This can be rearranged in the future
622 * if someone feels motivated enough. --nico
623 */
Nicolas Pitre638d9832005-08-06 05:40:46 +0100624 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 && extp->FeatureSupport & (1 << 9)) {
626 struct cfi_private *newcfi;
627 struct flchip *chip;
628 struct flchip_shared *shared;
629 int offs, numregions, numparts, partshift, numvirtchips, i, j;
630
631 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000632 offs = (extp->NumProtectionFields - 1) *
633 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100636 offs += extp->extra[offs+1]+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* Number of partition regions */
639 numregions = extp->extra[offs];
640 offs += 1;
641
Nicolas Pitre638d9832005-08-06 05:40:46 +0100642 /* skip the sizeof(partregion) field in CFI 1.4 */
643 if (extp->MinorVersion >= '4')
644 offs += 2;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* Number of hardware partitions */
647 numparts = 0;
648 for (i = 0; i < numregions; i++) {
649 struct cfi_intelext_regioninfo *rinfo;
650 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
651 numparts += rinfo->NumIdentPartitions;
652 offs += sizeof(*rinfo)
653 + (rinfo->NumBlockTypes - 1) *
654 sizeof(struct cfi_intelext_blockinfo);
655 }
656
Thomas Kunzefe224662008-04-23 01:40:52 +0200657 if (!numparts)
658 numparts = 1;
659
Nicolas Pitre638d9832005-08-06 05:40:46 +0100660 /* Programming Region info */
661 if (extp->MinorVersion >= '4') {
662 struct cfi_intelext_programming_regioninfo *prinfo;
663 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
Joern Engel28318772006-05-22 23:18:05 +0200664 mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
Joern Engel5fa43392006-05-22 23:18:29 +0200665 mtd->flags &= ~MTD_BIT_WRITEABLE;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100666 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
Joern Engel28318772006-05-22 23:18:05 +0200667 map->name, mtd->writesize,
Artem Bityutskiyd4160852007-01-30 10:45:55 +0200668 cfi->interleave * prinfo->ControlValid,
669 cfi->interleave * prinfo->ControlInvalid);
Nicolas Pitre638d9832005-08-06 05:40:46 +0100670 }
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /*
673 * All functions below currently rely on all chips having
674 * the same geometry so we'll just assume that all hardware
675 * partitions are of the same size too.
676 */
677 partshift = cfi->chipshift - __ffs(numparts);
678
679 if ((1 << partshift) < mtd->erasesize) {
680 printk( KERN_ERR
681 "%s: bad number of hw partitions (%d)\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700682 __func__, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -EINVAL;
684 }
685
686 numvirtchips = cfi->numchips * numparts;
687 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
688 if (!newcfi)
689 return -ENOMEM;
690 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
691 if (!shared) {
692 kfree(newcfi);
693 return -ENOMEM;
694 }
695 memcpy(newcfi, cfi, sizeof(struct cfi_private));
696 newcfi->numchips = numvirtchips;
697 newcfi->chipshift = partshift;
698
699 chip = &newcfi->chips[0];
700 for (i = 0; i < cfi->numchips; i++) {
701 shared[i].writing = shared[i].erasing = NULL;
702 spin_lock_init(&shared[i].lock);
703 for (j = 0; j < numparts; j++) {
704 *chip = cfi->chips[i];
705 chip->start += j << partshift;
706 chip->priv = &shared[i];
707 /* those should be reset too since
708 they create memory references. */
709 init_waitqueue_head(&chip->wq);
710 spin_lock_init(&chip->_spinlock);
711 chip->mutex = &chip->_spinlock;
712 chip++;
713 }
714 }
715
716 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
717 "--> %d partitions of %d KiB\n",
718 map->name, cfi->numchips, cfi->interleave,
719 newcfi->numchips, 1<<(newcfi->chipshift-10));
720
721 map->fldrv_priv = newcfi;
722 *pcfi = newcfi;
723 kfree(cfi);
724 }
725
726 return 0;
727}
728
729/*
730 * *********** CHIP ACCESS FUNCTIONS ***********
731 */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100732static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 DECLARE_WAITQUEUE(wait, current);
735 struct cfi_private *cfi = map->fldrv_priv;
736 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100738 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400740 /* Prevent setting state FL_SYNCING for chip in suspended state. */
741 if (mode == FL_SYNCING && chip->oldstate != FL_READY)
742 goto sleep;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 switch (chip->state) {
745
746 case FL_STATUS:
747 for (;;) {
748 status = map_read(map, adr);
749 if (map_word_andequal(map, status, status_OK, status_OK))
750 break;
751
752 /* At this point we're fine with write operations
753 in other partitions as they don't conflict. */
754 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
755 break;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 spin_unlock(chip->mutex);
758 cfi_udelay(1);
759 spin_lock(chip->mutex);
760 /* Someone else might have been playing with it. */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100761 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700763 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 case FL_READY:
765 case FL_CFI_QUERY:
766 case FL_JEDEC_QUERY:
767 return 0;
768
769 case FL_ERASING:
770 if (!cfip ||
771 !(cfip->FeatureSupport & 2) ||
772 !(mode == FL_READY || mode == FL_POINT ||
773 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
774 goto sleep;
775
776
777 /* Erase suspend */
778 map_write(map, CMD(0xB0), adr);
779
780 /* If the flash has finished erasing, then 'erase suspend'
781 * appears to make some (28F320) flash devices switch to
782 * 'read' mode. Make sure that we switch to 'read status'
783 * mode so we get the right data. --rmk
784 */
785 map_write(map, CMD(0x70), adr);
786 chip->oldstate = FL_ERASING;
787 chip->state = FL_ERASE_SUSPENDING;
788 chip->erase_suspended = 1;
789 for (;;) {
790 status = map_read(map, adr);
791 if (map_word_andequal(map, status, status_OK, status_OK))
792 break;
793
794 if (time_after(jiffies, timeo)) {
795 /* Urgh. Resume and pretend we weren't here. */
796 map_write(map, CMD(0xd0), adr);
797 /* Make sure we're in 'read status' mode if it had finished */
798 map_write(map, CMD(0x70), adr);
799 chip->state = FL_ERASING;
800 chip->oldstate = FL_READY;
Nicolas Pitre48436532005-08-06 05:16:52 +0100801 printk(KERN_ERR "%s: Chip not ready after erase "
802 "suspended: status = 0x%lx\n", map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EIO;
804 }
805
806 spin_unlock(chip->mutex);
807 cfi_udelay(1);
808 spin_lock(chip->mutex);
809 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
810 So we can just loop here. */
811 }
812 chip->state = FL_STATUS;
813 return 0;
814
815 case FL_XIP_WHILE_ERASING:
816 if (mode != FL_READY && mode != FL_POINT &&
817 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
818 goto sleep;
819 chip->oldstate = chip->state;
820 chip->state = FL_READY;
821 return 0;
822
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700823 case FL_SHUTDOWN:
824 /* The machine is rebooting now,so no one can get chip anymore */
825 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 case FL_POINT:
827 /* Only if there's no operation suspended... */
828 if (mode == FL_READY && chip->oldstate == FL_READY)
829 return 0;
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700830 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 default:
832 sleep:
833 set_current_state(TASK_UNINTERRUPTIBLE);
834 add_wait_queue(&chip->wq, &wait);
835 spin_unlock(chip->mutex);
836 schedule();
837 remove_wait_queue(&chip->wq, &wait);
838 spin_lock(chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100839 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841}
842
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100843static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
844{
845 int ret;
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300846 DECLARE_WAITQUEUE(wait, current);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100847
848 retry:
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400849 if (chip->priv &&
850 (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE
851 || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) {
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100852 /*
853 * OK. We have possibility for contention on the write/erase
854 * operations which are global to the real chip and not per
855 * partition. So let's fight it over in the partition which
856 * currently has authority on the operation.
857 *
858 * The rules are as follows:
859 *
860 * - any write operation must own shared->writing.
861 *
862 * - any erase operation must own _both_ shared->writing and
863 * shared->erasing.
864 *
865 * - contention arbitration is handled in the owner's context.
866 *
867 * The 'shared' struct can be read and/or written only when
868 * its lock is taken.
869 */
870 struct flchip_shared *shared = chip->priv;
871 struct flchip *contender;
872 spin_lock(&shared->lock);
873 contender = shared->writing;
874 if (contender && contender != chip) {
875 /*
876 * The engine to perform desired operation on this
877 * partition is already in use by someone else.
878 * Let's fight over it in the context of the chip
879 * currently using it. If it is possible to suspend,
880 * that other partition will do just that, otherwise
881 * it'll happily send us to sleep. In any case, when
882 * get_chip returns success we're clear to go ahead.
883 */
884 ret = spin_trylock(contender->mutex);
885 spin_unlock(&shared->lock);
886 if (!ret)
887 goto retry;
888 spin_unlock(chip->mutex);
889 ret = chip_ready(map, contender, contender->start, mode);
890 spin_lock(chip->mutex);
891
892 if (ret == -EAGAIN) {
893 spin_unlock(contender->mutex);
894 goto retry;
895 }
896 if (ret) {
897 spin_unlock(contender->mutex);
898 return ret;
899 }
900 spin_lock(&shared->lock);
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400901
902 /* We should not own chip if it is already
903 * in FL_SYNCING state. Put contender and retry. */
904 if (chip->state == FL_SYNCING) {
905 put_chip(map, contender, contender->start);
906 spin_unlock(contender->mutex);
907 goto retry;
908 }
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100909 spin_unlock(contender->mutex);
910 }
911
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300912 /* Check if we already have suspended erase
913 * on this chip. Sleep. */
914 if (mode == FL_ERASING && shared->erasing
915 && shared->erasing->oldstate == FL_ERASING) {
916 spin_unlock(&shared->lock);
917 set_current_state(TASK_UNINTERRUPTIBLE);
918 add_wait_queue(&chip->wq, &wait);
919 spin_unlock(chip->mutex);
920 schedule();
921 remove_wait_queue(&chip->wq, &wait);
922 spin_lock(chip->mutex);
923 goto retry;
924 }
925
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100926 /* We now own it */
927 shared->writing = chip;
928 if (mode == FL_ERASING)
929 shared->erasing = chip;
930 spin_unlock(&shared->lock);
931 }
932 ret = chip_ready(map, chip, adr, mode);
933 if (ret == -EAGAIN)
934 goto retry;
935
936 return ret;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
940{
941 struct cfi_private *cfi = map->fldrv_priv;
942
943 if (chip->priv) {
944 struct flchip_shared *shared = chip->priv;
945 spin_lock(&shared->lock);
946 if (shared->writing == chip && chip->oldstate == FL_READY) {
947 /* We own the ability to write, but we're done */
948 shared->writing = shared->erasing;
949 if (shared->writing && shared->writing != chip) {
950 /* give back ownership to who we loaned it from */
951 struct flchip *loaner = shared->writing;
952 spin_lock(loaner->mutex);
953 spin_unlock(&shared->lock);
954 spin_unlock(chip->mutex);
955 put_chip(map, loaner, loaner->start);
956 spin_lock(chip->mutex);
957 spin_unlock(loaner->mutex);
958 wake_up(&chip->wq);
959 return;
960 }
961 shared->erasing = NULL;
962 shared->writing = NULL;
963 } else if (shared->erasing == chip && shared->writing != chip) {
964 /*
965 * We own the ability to erase without the ability
966 * to write, which means the erase was suspended
967 * and some other partition is currently writing.
968 * Don't let the switch below mess things up since
969 * we don't have ownership to resume anything.
970 */
971 spin_unlock(&shared->lock);
972 wake_up(&chip->wq);
973 return;
974 }
975 spin_unlock(&shared->lock);
976 }
977
978 switch(chip->oldstate) {
979 case FL_ERASING:
980 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000981 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000983 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 errors to be returned from do_erase_oneblock because
985 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000986 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000988 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 do. */
990 map_write(map, CMD(0xd0), adr);
991 map_write(map, CMD(0x70), adr);
992 chip->oldstate = FL_READY;
993 chip->state = FL_ERASING;
994 break;
995
996 case FL_XIP_WHILE_ERASING:
997 chip->state = chip->oldstate;
998 chip->oldstate = FL_READY;
999 break;
1000
1001 case FL_READY:
1002 case FL_STATUS:
1003 case FL_JEDEC_QUERY:
1004 /* We should really make set_vpp() count, rather than doing this */
1005 DISABLE_VPP(map);
1006 break;
1007 default:
Nicolas Pitre48436532005-08-06 05:16:52 +01001008 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 wake_up(&chip->wq);
1011}
1012
1013#ifdef CONFIG_MTD_XIP
1014
1015/*
1016 * No interrupt what so ever can be serviced while the flash isn't in array
1017 * mode. This is ensured by the xip_disable() and xip_enable() functions
1018 * enclosing any code path where the flash is known not to be in array mode.
1019 * And within a XIP disabled code path, only functions marked with __xipram
1020 * may be called and nothing else (it's a good thing to inspect generated
1021 * assembly to make sure inline functions were actually inlined and that gcc
1022 * didn't emit calls to its own support functions). Also configuring MTD CFI
1023 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025
1026static void xip_disable(struct map_info *map, struct flchip *chip,
1027 unsigned long adr)
1028{
1029 /* TODO: chips with no XIP use should ignore and return */
1030 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 local_irq_disable();
1032}
1033
1034static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
1035 unsigned long adr)
1036{
1037 struct cfi_private *cfi = map->fldrv_priv;
1038 if (chip->state != FL_POINT && chip->state != FL_READY) {
1039 map_write(map, CMD(0xff), adr);
1040 chip->state = FL_READY;
1041 }
1042 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +02001043 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/*
1048 * When a delay is required for the flash operation to complete, the
Nicolas Pitrec1724712006-03-30 15:52:41 +01001049 * xip_wait_for_operation() function is polling for both the given timeout
1050 * and pending (but still masked) hardware interrupts. Whenever there is an
1051 * interrupt pending then the flash erase or write operation is suspended,
1052 * array mode restored and interrupts unmasked. Task scheduling might also
1053 * happen at that point. The CPU eventually returns from the interrupt or
1054 * the call to schedule() and the suspended flash operation is resumed for
1055 * the remaining of the delay period.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
1057 * Warning: this function _will_ fool interrupt latency tracing tools.
1058 */
1059
Nicolas Pitrec1724712006-03-30 15:52:41 +01001060static int __xipram xip_wait_for_operation(
1061 struct map_info *map, struct flchip *chip,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001062 unsigned long adr, unsigned int chip_op_time_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct cfi_private *cfi = map->fldrv_priv;
1065 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
1066 map_word status, OK = CMD(0x80);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001067 unsigned long usec, suspended, start, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 flstate_t oldstate, newstate;
1069
Nicolas Pitrec1724712006-03-30 15:52:41 +01001070 start = xip_currtime();
Anders Grafströme93cafe2008-08-05 18:37:41 +02001071 usec = chip_op_time_max;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001072 if (usec == 0)
1073 usec = 500000;
1074 done = 0;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 do {
1077 cpu_relax();
1078 if (xip_irqpending() && cfip &&
1079 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1080 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1081 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1082 /*
1083 * Let's suspend the erase or write operation when
1084 * supported. Note that we currently don't try to
1085 * suspend interleaved chips if there is already
1086 * another operation suspended (imagine what happens
1087 * when one chip was already done with the current
1088 * operation while another chip suspended it, then
1089 * we resume the whole thing at once). Yes, it
1090 * can happen!
1091 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001092 usec -= done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 map_write(map, CMD(0xb0), adr);
1094 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 suspended = xip_currtime();
1096 do {
1097 if (xip_elapsed_since(suspended) > 100000) {
1098 /*
1099 * The chip doesn't want to suspend
1100 * after waiting for 100 msecs.
1101 * This is a critical error but there
1102 * is not much we can do here.
1103 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001104 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 status = map_read(map, adr);
1107 } while (!map_word_andequal(map, status, OK, OK));
1108
1109 /* Suspend succeeded */
1110 oldstate = chip->state;
1111 if (oldstate == FL_ERASING) {
1112 if (!map_word_bitsset(map, status, CMD(0x40)))
1113 break;
1114 newstate = FL_XIP_WHILE_ERASING;
1115 chip->erase_suspended = 1;
1116 } else {
1117 if (!map_word_bitsset(map, status, CMD(0x04)))
1118 break;
1119 newstate = FL_XIP_WHILE_WRITING;
1120 chip->write_suspended = 1;
1121 }
1122 chip->state = newstate;
1123 map_write(map, CMD(0xff), adr);
1124 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001125 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 local_irq_enable();
Nicolas Pitre6da70122005-05-19 18:05:47 +01001127 spin_unlock(chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001128 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 cond_resched();
1130
1131 /*
1132 * We're back. However someone else might have
1133 * decided to go write to the chip if we are in
1134 * a suspended erase state. If so let's wait
1135 * until it's done.
1136 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001137 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 while (chip->state != newstate) {
1139 DECLARE_WAITQUEUE(wait, current);
1140 set_current_state(TASK_UNINTERRUPTIBLE);
1141 add_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001142 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 schedule();
1144 remove_wait_queue(&chip->wq, &wait);
Nicolas Pitre6da70122005-05-19 18:05:47 +01001145 spin_lock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 /* Disallow XIP again */
1148 local_irq_disable();
1149
1150 /* Resume the write or erase operation */
1151 map_write(map, CMD(0xd0), adr);
1152 map_write(map, CMD(0x70), adr);
1153 chip->state = oldstate;
1154 start = xip_currtime();
1155 } else if (usec >= 1000000/HZ) {
1156 /*
1157 * Try to save on CPU power when waiting delay
1158 * is at least a system timer tick period.
1159 * No need to be extremely accurate here.
1160 */
1161 xip_cpu_idle();
1162 }
1163 status = map_read(map, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001164 done = xip_elapsed_since(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } while (!map_word_andequal(map, status, OK, OK)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001166 && done < usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Nicolas Pitrec1724712006-03-30 15:52:41 +01001168 return (done >= usec) ? -ETIME : 0;
1169}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171/*
1172 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1173 * the flash is actively programming or erasing since we have to poll for
1174 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001175 * a XIP setup so do it before the actual flash operation in this case
Nicolas Pitrec1724712006-03-30 15:52:41 +01001176 * and stub it out from INVAL_CACHE_AND_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001178#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1179 INVALIDATE_CACHED_RANGE(map, from, size)
1180
Anders Grafströme93cafe2008-08-05 18:37:41 +02001181#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \
1182 xip_wait_for_operation(map, chip, cmd_adr, usec_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184#else
1185
1186#define xip_disable(map, chip, adr)
1187#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188#define XIP_INVAL_CACHED_RANGE(x...)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001189#define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Nicolas Pitrec1724712006-03-30 15:52:41 +01001191static int inval_cache_and_wait_for_operation(
1192 struct map_info *map, struct flchip *chip,
1193 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001194 unsigned int chip_op_time, unsigned int chip_op_time_max)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001195{
1196 struct cfi_private *cfi = map->fldrv_priv;
1197 map_word status, status_OK = CMD(0x80);
Alexey Korolev46a16522006-06-28 19:22:07 +01001198 int chip_state = chip->state;
Alexey Korolev998453f2008-07-16 15:28:56 +01001199 unsigned int timeo, sleep_time, reset_timeo;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001200
Nicolas Pitrec1724712006-03-30 15:52:41 +01001201 spin_unlock(chip->mutex);
1202 if (inval_len)
1203 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001204 spin_lock(chip->mutex);
1205
Anders Grafströme93cafe2008-08-05 18:37:41 +02001206 timeo = chip_op_time_max;
Alexey Korolev46a16522006-06-28 19:22:07 +01001207 if (!timeo)
1208 timeo = 500000;
Alexey Korolev998453f2008-07-16 15:28:56 +01001209 reset_timeo = timeo;
Alexey Korolev46a16522006-06-28 19:22:07 +01001210 sleep_time = chip_op_time / 2;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001211
Nicolas Pitrec1724712006-03-30 15:52:41 +01001212 for (;;) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001213 status = map_read(map, cmd_adr);
1214 if (map_word_andequal(map, status, status_OK, status_OK))
1215 break;
1216
Alexey Korolev46a16522006-06-28 19:22:07 +01001217 if (!timeo) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001218 map_write(map, CMD(0x70), cmd_adr);
1219 chip->state = FL_STATUS;
1220 return -ETIME;
1221 }
1222
Alexey Korolev46a16522006-06-28 19:22:07 +01001223 /* OK Still waiting. Drop the lock, wait a while and retry. */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001224 spin_unlock(chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001225 if (sleep_time >= 1000000/HZ) {
1226 /*
1227 * Half of the normal delay still remaining
1228 * can be performed with a sleeping delay instead
1229 * of busy waiting.
1230 */
1231 msleep(sleep_time/1000);
1232 timeo -= sleep_time;
1233 sleep_time = 1000000/HZ;
1234 } else {
1235 udelay(1);
1236 cond_resched();
1237 timeo--;
1238 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001239 spin_lock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001240
Joakim Tjernlund967bf622006-11-28 23:11:52 +00001241 while (chip->state != chip_state) {
Alexey Korolev46a16522006-06-28 19:22:07 +01001242 /* Someone's suspended the operation: sleep */
1243 DECLARE_WAITQUEUE(wait, current);
1244 set_current_state(TASK_UNINTERRUPTIBLE);
1245 add_wait_queue(&chip->wq, &wait);
1246 spin_unlock(chip->mutex);
1247 schedule();
1248 remove_wait_queue(&chip->wq, &wait);
1249 spin_lock(chip->mutex);
1250 }
Graff Yang6ac15e92009-03-02 16:31:29 +08001251 if (chip->erase_suspended && chip_state == FL_ERASING) {
1252 /* Erase suspend occured while sleep: reset timeout */
Alexey Korolev998453f2008-07-16 15:28:56 +01001253 timeo = reset_timeo;
1254 chip->erase_suspended = 0;
Graff Yang6ac15e92009-03-02 16:31:29 +08001255 }
1256 if (chip->write_suspended && chip_state == FL_WRITING) {
1257 /* Write suspend occured while sleep: reset timeout */
1258 timeo = reset_timeo;
Alexey Korolev998453f2008-07-16 15:28:56 +01001259 chip->write_suspended = 0;
1260 }
Alexey Korolev46a16522006-06-28 19:22:07 +01001261 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001262
1263 /* Done and happy. */
1264 chip->state = FL_STATUS;
1265 return 0;
1266}
Nicolas Pitre6da70122005-05-19 18:05:47 +01001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#endif
1269
Anders Grafströme93cafe2008-08-05 18:37:41 +02001270#define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \
1271 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001272
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1275{
1276 unsigned long cmd_addr;
1277 struct cfi_private *cfi = map->fldrv_priv;
1278 int ret = 0;
1279
1280 adr += chip->start;
1281
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001282 /* Ensure cmd read/writes are aligned. */
1283 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 spin_lock(chip->mutex);
1286
1287 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1288
1289 if (!ret) {
1290 if (chip->state != FL_POINT && chip->state != FL_READY)
1291 map_write(map, CMD(0xff), cmd_addr);
1292
1293 chip->state = FL_POINT;
1294 chip->ref_point_counter++;
1295 }
1296 spin_unlock(chip->mutex);
1297
1298 return ret;
1299}
1300
Jared Hulberta98889f2008-04-29 23:26:49 -07001301static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
1302 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct map_info *map = mtd->priv;
1305 struct cfi_private *cfi = map->fldrv_priv;
Andy Lowe097f2572007-01-12 18:05:10 -05001306 unsigned long ofs, last_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int chipnum;
1308 int ret = 0;
1309
1310 if (!map->virt || (from + len > mtd->size))
1311 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* Now lock the chip(s) to POINT state */
1314
1315 /* ofs: offset within the first chip that the first read should start */
1316 chipnum = (from >> cfi->chipshift);
1317 ofs = from - (chipnum << cfi->chipshift);
1318
Jared Hulberta98889f2008-04-29 23:26:49 -07001319 *virt = map->virt + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001320 *retlen = 0;
Jared Hulberta98889f2008-04-29 23:26:49 -07001321 if (phys)
1322 *phys = map->phys + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 while (len) {
1325 unsigned long thislen;
1326
1327 if (chipnum >= cfi->numchips)
1328 break;
1329
Andy Lowe097f2572007-01-12 18:05:10 -05001330 /* We cannot point across chips that are virtually disjoint */
1331 if (!last_end)
1332 last_end = cfi->chips[chipnum].start;
1333 else if (cfi->chips[chipnum].start != last_end)
1334 break;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if ((len + ofs -1) >> cfi->chipshift)
1337 thislen = (1<<cfi->chipshift) - ofs;
1338 else
1339 thislen = len;
1340
1341 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1342 if (ret)
1343 break;
1344
1345 *retlen += thislen;
1346 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 ofs = 0;
Andy Lowe097f2572007-01-12 18:05:10 -05001349 last_end += 1 << cfi->chipshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 chipnum++;
1351 }
1352 return 0;
1353}
1354
Jared Hulberta98889f2008-04-29 23:26:49 -07001355static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 struct map_info *map = mtd->priv;
1358 struct cfi_private *cfi = map->fldrv_priv;
1359 unsigned long ofs;
1360 int chipnum;
1361
1362 /* Now unlock the chip(s) POINT state */
1363
1364 /* ofs: offset within the first chip that the first read should start */
1365 chipnum = (from >> cfi->chipshift);
1366 ofs = from - (chipnum << cfi->chipshift);
1367
1368 while (len) {
1369 unsigned long thislen;
1370 struct flchip *chip;
1371
1372 chip = &cfi->chips[chipnum];
1373 if (chipnum >= cfi->numchips)
1374 break;
1375
1376 if ((len + ofs -1) >> cfi->chipshift)
1377 thislen = (1<<cfi->chipshift) - ofs;
1378 else
1379 thislen = len;
1380
1381 spin_lock(chip->mutex);
1382 if (chip->state == FL_POINT) {
1383 chip->ref_point_counter--;
1384 if(chip->ref_point_counter == 0)
1385 chip->state = FL_READY;
1386 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001387 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 -07001388
1389 put_chip(map, chip, chip->start);
1390 spin_unlock(chip->mutex);
1391
1392 len -= thislen;
1393 ofs = 0;
1394 chipnum++;
1395 }
1396}
1397
1398static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1399{
1400 unsigned long cmd_addr;
1401 struct cfi_private *cfi = map->fldrv_priv;
1402 int ret;
1403
1404 adr += chip->start;
1405
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001406 /* Ensure cmd read/writes are aligned. */
1407 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 spin_lock(chip->mutex);
1410 ret = get_chip(map, chip, cmd_addr, FL_READY);
1411 if (ret) {
1412 spin_unlock(chip->mutex);
1413 return ret;
1414 }
1415
1416 if (chip->state != FL_POINT && chip->state != FL_READY) {
1417 map_write(map, CMD(0xff), cmd_addr);
1418
1419 chip->state = FL_READY;
1420 }
1421
1422 map_copy_from(map, buf, adr, len);
1423
1424 put_chip(map, chip, cmd_addr);
1425
1426 spin_unlock(chip->mutex);
1427 return 0;
1428}
1429
1430static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1431{
1432 struct map_info *map = mtd->priv;
1433 struct cfi_private *cfi = map->fldrv_priv;
1434 unsigned long ofs;
1435 int chipnum;
1436 int ret = 0;
1437
1438 /* ofs: offset within the first chip that the first read should start */
1439 chipnum = (from >> cfi->chipshift);
1440 ofs = from - (chipnum << cfi->chipshift);
1441
1442 *retlen = 0;
1443
1444 while (len) {
1445 unsigned long thislen;
1446
1447 if (chipnum >= cfi->numchips)
1448 break;
1449
1450 if ((len + ofs -1) >> cfi->chipshift)
1451 thislen = (1<<cfi->chipshift) - ofs;
1452 else
1453 thislen = len;
1454
1455 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1456 if (ret)
1457 break;
1458
1459 *retlen += thislen;
1460 len -= thislen;
1461 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ofs = 0;
1464 chipnum++;
1465 }
1466 return ret;
1467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001470 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001473 map_word status, write_cmd;
1474 int ret=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 adr += chip->start;
1477
Nicolas Pitref77814d2005-02-08 17:11:19 +00001478 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001479 case FL_WRITING:
1480 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0x40) : CMD(0x41);
1481 break;
1482 case FL_OTP_WRITE:
1483 write_cmd = CMD(0xc0);
1484 break;
1485 default:
1486 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 spin_lock(chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001490 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (ret) {
1492 spin_unlock(chip->mutex);
1493 return ret;
1494 }
1495
1496 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1497 ENABLE_VPP(map);
1498 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001499 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001501 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Nicolas Pitrec1724712006-03-30 15:52:41 +01001503 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1504 adr, map_bankwidth(map),
Anders Grafströme93cafe2008-08-05 18:37:41 +02001505 chip->word_write_time,
1506 chip->word_write_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001507 if (ret) {
1508 xip_enable(map, chip, adr);
1509 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Nicolas Pitre48436532005-08-06 05:16:52 +01001513 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001514 status = map_read(map, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001515 if (map_word_bitsset(map, status, CMD(0x1a))) {
1516 unsigned long chipstatus = MERGESTATUS(status);
1517
1518 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001521 xip_enable(map, chip, adr);
1522
1523 if (chipstatus & 0x02) {
1524 ret = -EROFS;
1525 } else if (chipstatus & 0x08) {
1526 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1527 ret = -EIO;
1528 } else {
1529 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1530 ret = -EINVAL;
1531 }
1532
1533 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
1536 xip_enable(map, chip, adr);
1537 out: put_chip(map, chip, adr);
1538 spin_unlock(chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return ret;
1540}
1541
1542
1543static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1544{
1545 struct map_info *map = mtd->priv;
1546 struct cfi_private *cfi = map->fldrv_priv;
1547 int ret = 0;
1548 int chipnum;
1549 unsigned long ofs;
1550
1551 *retlen = 0;
1552 if (!len)
1553 return 0;
1554
1555 chipnum = to >> cfi->chipshift;
1556 ofs = to - (chipnum << cfi->chipshift);
1557
1558 /* If it's not bus-aligned, do the first byte write */
1559 if (ofs & (map_bankwidth(map)-1)) {
1560 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1561 int gap = ofs - bus_ofs;
1562 int n;
1563 map_word datum;
1564
1565 n = min_t(int, len, map_bankwidth(map)-gap);
1566 datum = map_word_ff(map);
1567 datum = map_word_load_partial(map, datum, buf, gap, n);
1568
1569 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001570 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001571 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return ret;
1573
1574 len -= n;
1575 ofs += n;
1576 buf += n;
1577 (*retlen) += n;
1578
1579 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001580 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 ofs = 0;
1582 if (chipnum == cfi->numchips)
1583 return 0;
1584 }
1585 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 while(len >= map_bankwidth(map)) {
1588 map_word datum = map_word_load(map, buf);
1589
1590 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001591 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (ret)
1593 return ret;
1594
1595 ofs += map_bankwidth(map);
1596 buf += map_bankwidth(map);
1597 (*retlen) += map_bankwidth(map);
1598 len -= map_bankwidth(map);
1599
1600 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001601 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 ofs = 0;
1603 if (chipnum == cfi->numchips)
1604 return 0;
1605 }
1606 }
1607
1608 if (len & (map_bankwidth(map)-1)) {
1609 map_word datum;
1610
1611 datum = map_word_ff(map);
1612 datum = map_word_load_partial(map, datum, buf, 0, len);
1613
1614 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001615 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001616 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 (*retlen) += len;
1620 }
1621
1622 return 0;
1623}
1624
1625
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001626static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001627 unsigned long adr, const struct kvec **pvec,
1628 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001631 map_word status, write_cmd, datum;
1632 unsigned long cmd_adr;
1633 int ret, wbufsize, word_gap, words;
Nicolas Pitree102d542005-08-06 05:46:59 +01001634 const struct kvec *vec;
1635 unsigned long vec_seek;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001636 unsigned long initial_adr;
1637 int initial_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1640 adr += chip->start;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001641 initial_adr = adr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* Let's determine this according to the interleave only once */
Nicolas Pitre638d9832005-08-06 05:40:46 +01001645 write_cmd = (cfi->cfiq->P_ID != 0x0200) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 spin_lock(chip->mutex);
1648 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1649 if (ret) {
1650 spin_unlock(chip->mutex);
1651 return ret;
1652 }
1653
Massimo Cirillo646fd122008-01-11 10:24:11 +00001654 XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 ENABLE_VPP(map);
1656 xip_disable(map, chip, cmd_adr);
1657
David Woodhouse151e7652006-05-14 01:51:54 +01001658 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001659 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 So we must check here and reset those bits if they're set. Otherwise
1661 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001662 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001664 chip->state = FL_STATUS;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 status = map_read(map, cmd_adr);
1667 if (map_word_bitsset(map, status, CMD(0x30))) {
1668 xip_enable(map, chip, cmd_adr);
1669 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1670 xip_disable(map, chip, cmd_adr);
1671 map_write(map, CMD(0x50), cmd_adr);
1672 map_write(map, CMD(0x70), cmd_adr);
1673 }
1674
1675 chip->state = FL_WRITING_TO_BUFFER;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001676 map_write(map, write_cmd, cmd_adr);
Anders Grafströme93cafe2008-08-05 18:37:41 +02001677 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001678 if (ret) {
1679 /* Argh. Not ready for write to buffer */
1680 map_word Xstatus = map_read(map, cmd_adr);
1681 map_write(map, CMD(0x70), cmd_adr);
1682 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 status = map_read(map, cmd_adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001684 map_write(map, CMD(0x50), cmd_adr);
1685 map_write(map, CMD(0x70), cmd_adr);
1686 xip_enable(map, chip, cmd_adr);
1687 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1688 map->name, Xstatus.x[0], status.x[0]);
1689 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
Nicolas Pitree102d542005-08-06 05:46:59 +01001692 /* Figure out the number of words to write */
1693 word_gap = (-adr & (map_bankwidth(map)-1));
Julia Lawallc8872b02008-08-02 17:14:21 +02001694 words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map));
Nicolas Pitree102d542005-08-06 05:46:59 +01001695 if (!word_gap) {
1696 words--;
1697 } else {
1698 word_gap = map_bankwidth(map) - word_gap;
1699 adr -= word_gap;
1700 datum = map_word_ff(map);
1701 }
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001704 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001707 vec = *pvec;
1708 vec_seek = *pvec_seek;
1709 do {
1710 int n = map_bankwidth(map) - word_gap;
1711 if (n > vec->iov_len - vec_seek)
1712 n = vec->iov_len - vec_seek;
1713 if (n > len)
1714 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Nicolas Pitree102d542005-08-06 05:46:59 +01001716 if (!word_gap && len < map_bankwidth(map))
1717 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Nicolas Pitree102d542005-08-06 05:46:59 +01001719 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001720 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001721 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Nicolas Pitree102d542005-08-06 05:46:59 +01001723 len -= n;
1724 word_gap += n;
1725 if (!len || word_gap == map_bankwidth(map)) {
1726 map_write(map, datum, adr);
1727 adr += map_bankwidth(map);
1728 word_gap = 0;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Nicolas Pitree102d542005-08-06 05:46:59 +01001731 vec_seek += n;
1732 if (vec_seek == vec->iov_len) {
1733 vec++;
1734 vec_seek = 0;
1735 }
1736 } while (len);
1737 *pvec = vec;
1738 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* GO GO GO */
1741 map_write(map, CMD(0xd0), cmd_adr);
1742 chip->state = FL_WRITING;
1743
Nicolas Pitrec1724712006-03-30 15:52:41 +01001744 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
Massimo Cirillo646fd122008-01-11 10:24:11 +00001745 initial_adr, initial_len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001746 chip->buffer_write_time,
1747 chip->buffer_write_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001748 if (ret) {
1749 map_write(map, CMD(0x70), cmd_adr);
1750 chip->state = FL_STATUS;
1751 xip_enable(map, chip, cmd_adr);
1752 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1753 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Nicolas Pitre48436532005-08-06 05:16:52 +01001756 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001757 status = map_read(map, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001758 if (map_word_bitsset(map, status, CMD(0x1a))) {
1759 unsigned long chipstatus = MERGESTATUS(status);
1760
1761 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001763 map_write(map, CMD(0x70), cmd_adr);
1764 xip_enable(map, chip, cmd_adr);
1765
1766 if (chipstatus & 0x02) {
1767 ret = -EROFS;
1768 } else if (chipstatus & 0x08) {
1769 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1770 ret = -EIO;
1771 } else {
1772 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1773 ret = -EINVAL;
1774 }
1775
1776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
1779 xip_enable(map, chip, cmd_adr);
1780 out: put_chip(map, chip, cmd_adr);
1781 spin_unlock(chip->mutex);
1782 return ret;
1783}
1784
Nicolas Pitree102d542005-08-06 05:46:59 +01001785static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1786 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct map_info *map = mtd->priv;
1789 struct cfi_private *cfi = map->fldrv_priv;
1790 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1791 int ret = 0;
1792 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001793 unsigned long ofs, vec_seek, i;
1794 size_t len = 0;
1795
1796 for (i = 0; i < count; i++)
1797 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 *retlen = 0;
1800 if (!len)
1801 return 0;
1802
1803 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001804 ofs = to - (chipnum << cfi->chipshift);
1805 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Nicolas Pitree102d542005-08-06 05:46:59 +01001807 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* We must not cross write block boundaries */
1809 int size = wbufsize - (ofs & (wbufsize-1));
1810
1811 if (size > len)
1812 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001813 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001814 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (ret)
1816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 (*retlen) += size;
1820 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (ofs >> cfi->chipshift) {
1823 chipnum ++;
1824 ofs = 0;
1825 if (chipnum == cfi->numchips)
1826 return 0;
1827 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001828
1829 /* Be nice and reschedule with the chip in a usable state for other
1830 processes. */
1831 cond_resched();
1832
Nicolas Pitree102d542005-08-06 05:46:59 +01001833 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
Nicolas Pitree102d542005-08-06 05:46:59 +01001838static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1839 size_t len, size_t *retlen, const u_char *buf)
1840{
1841 struct kvec vec;
1842
1843 vec.iov_base = (void *) buf;
1844 vec.iov_len = len;
1845
1846 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1850 unsigned long adr, int len, void *thunk)
1851{
1852 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001853 map_word status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 int retries = 3;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001855 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 adr += chip->start;
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 retry:
1860 spin_lock(chip->mutex);
1861 ret = get_chip(map, chip, adr, FL_ERASING);
1862 if (ret) {
1863 spin_unlock(chip->mutex);
1864 return ret;
1865 }
1866
1867 XIP_INVAL_CACHED_RANGE(map, adr, len);
1868 ENABLE_VPP(map);
1869 xip_disable(map, chip, adr);
1870
1871 /* Clear the status register first */
1872 map_write(map, CMD(0x50), adr);
1873
1874 /* Now erase */
1875 map_write(map, CMD(0x20), adr);
1876 map_write(map, CMD(0xD0), adr);
1877 chip->state = FL_ERASING;
1878 chip->erase_suspended = 0;
1879
Nicolas Pitrec1724712006-03-30 15:52:41 +01001880 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1881 adr, len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001882 chip->erase_time,
1883 chip->erase_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001884 if (ret) {
1885 map_write(map, CMD(0x70), adr);
1886 chip->state = FL_STATUS;
1887 xip_enable(map, chip, adr);
1888 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1889 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
1892 /* We've broken this before. It doesn't hurt to be safe */
1893 map_write(map, CMD(0x70), adr);
1894 chip->state = FL_STATUS;
1895 status = map_read(map, adr);
1896
Nicolas Pitre48436532005-08-06 05:16:52 +01001897 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001899 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* Reset the error bits */
1902 map_write(map, CMD(0x50), adr);
1903 map_write(map, CMD(0x70), adr);
1904 xip_enable(map, chip, adr);
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001907 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1908 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 } else if (chipstatus & 0x02) {
1910 /* Protection bit set */
1911 ret = -EROFS;
1912 } else if (chipstatus & 0x8) {
1913 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001914 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001916 } else if (chipstatus & 0x20 && retries--) {
1917 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
Nicolas Pitre48436532005-08-06 05:16:52 +01001918 put_chip(map, chip, adr);
1919 spin_unlock(chip->mutex);
1920 goto retry;
1921 } else {
1922 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 -07001923 ret = -EIO;
1924 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001925
1926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
Nicolas Pitre48436532005-08-06 05:16:52 +01001929 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 out: put_chip(map, chip, adr);
1931 spin_unlock(chip->mutex);
1932 return ret;
1933}
1934
Ben Dooks029a9eb2007-05-28 20:11:37 +01001935static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 unsigned long ofs, len;
1938 int ret;
1939
1940 ofs = instr->addr;
1941 len = instr->len;
1942
1943 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1944 if (ret)
1945 return ret;
1946
1947 instr->state = MTD_ERASE_DONE;
1948 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return 0;
1951}
1952
1953static void cfi_intelext_sync (struct mtd_info *mtd)
1954{
1955 struct map_info *map = mtd->priv;
1956 struct cfi_private *cfi = map->fldrv_priv;
1957 int i;
1958 struct flchip *chip;
1959 int ret = 0;
1960
1961 for (i=0; !ret && i<cfi->numchips; i++) {
1962 chip = &cfi->chips[i];
1963
1964 spin_lock(chip->mutex);
1965 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1966
1967 if (!ret) {
1968 chip->oldstate = chip->state;
1969 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001970 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * as the whole point is that nobody can do anything
1972 * with the chip now anyway.
1973 */
1974 }
1975 spin_unlock(chip->mutex);
1976 }
1977
1978 /* Unlock the chips again */
1979
1980 for (i--; i >=0; i--) {
1981 chip = &cfi->chips[i];
1982
1983 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (chip->state == FL_SYNCING) {
1986 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00001987 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 wake_up(&chip->wq);
1989 }
1990 spin_unlock(chip->mutex);
1991 }
1992}
1993
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08001994static int __xipram do_getlockstatus_oneblock(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct flchip *chip,
1996 unsigned long adr,
1997 int len, void *thunk)
1998{
1999 struct cfi_private *cfi = map->fldrv_priv;
2000 int status, ofs_factor = cfi->interleave * cfi->device_type;
2001
Todd Poynorc25bb1f2005-04-27 21:01:52 +01002002 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01002004 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 chip->state = FL_JEDEC_QUERY;
2006 status = cfi_read_query(map, adr+(2*ofs_factor));
2007 xip_enable(map, chip, 0);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002008 return status;
2009}
2010
2011#ifdef DEBUG_LOCK_BITS
2012static int __xipram do_printlockstatus_oneblock(struct map_info *map,
2013 struct flchip *chip,
2014 unsigned long adr,
2015 int len, void *thunk)
2016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002018 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 0;
2020}
2021#endif
2022
2023#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
2024#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
2025
2026static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
2027 unsigned long adr, int len, void *thunk)
2028{
2029 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002030 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01002031 int udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int ret;
2033
2034 adr += chip->start;
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 spin_lock(chip->mutex);
2037 ret = get_chip(map, chip, adr, FL_LOCKING);
2038 if (ret) {
2039 spin_unlock(chip->mutex);
2040 return ret;
2041 }
2042
2043 ENABLE_VPP(map);
2044 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 map_write(map, CMD(0x60), adr);
2047 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2048 map_write(map, CMD(0x01), adr);
2049 chip->state = FL_LOCKING;
2050 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2051 map_write(map, CMD(0xD0), adr);
2052 chip->state = FL_UNLOCKING;
2053 } else
2054 BUG();
2055
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002056 /*
2057 * If Instant Individual Block Locking supported then no need
2058 * to delay.
2059 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01002060 udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002061
Anders Grafströme93cafe2008-08-05 18:37:41 +02002062 ret = WAIT_TIMEOUT(map, chip, adr, udelay, udelay * 100);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002063 if (ret) {
2064 map_write(map, CMD(0x70), adr);
2065 chip->state = FL_STATUS;
2066 xip_enable(map, chip, adr);
2067 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
2068 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 xip_enable(map, chip, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002072out: put_chip(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 spin_unlock(chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Adrian Hunter69423d92008-12-10 13:37:21 +00002077static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 int ret;
2080
2081#ifdef DEBUG_LOCK_BITS
2082 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002083 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002085 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#endif
2087
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002088 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091#ifdef DEBUG_LOCK_BITS
2092 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002093 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002095 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096#endif
2097
2098 return ret;
2099}
2100
Adrian Hunter69423d92008-12-10 13:37:21 +00002101static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 int ret;
2104
2105#ifdef DEBUG_LOCK_BITS
2106 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002107 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002109 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110#endif
2111
2112 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2113 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115#ifdef DEBUG_LOCK_BITS
2116 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002117 __func__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002118 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002119 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return ret;
2123}
2124
Nicolas Pitref77814d2005-02-08 17:11:19 +00002125#ifdef CONFIG_MTD_OTP
2126
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002127typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002128 u_long data_offset, u_char *buf, u_int size,
2129 u_long prot_offset, u_int groupno, u_int groupsize);
2130
2131static int __xipram
2132do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2133 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2134{
2135 struct cfi_private *cfi = map->fldrv_priv;
2136 int ret;
2137
2138 spin_lock(chip->mutex);
2139 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2140 if (ret) {
2141 spin_unlock(chip->mutex);
2142 return ret;
2143 }
2144
2145 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002146 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002147
2148 xip_disable(map, chip, chip->start);
2149 if (chip->state != FL_JEDEC_QUERY) {
2150 map_write(map, CMD(0x90), chip->start);
2151 chip->state = FL_JEDEC_QUERY;
2152 }
2153 map_copy_from(map, buf, chip->start + offset, size);
2154 xip_enable(map, chip, chip->start);
2155
2156 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002157 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002158
2159 put_chip(map, chip, chip->start);
2160 spin_unlock(chip->mutex);
2161 return 0;
2162}
2163
2164static int
2165do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2166 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2167{
2168 int ret;
2169
2170 while (size) {
2171 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2172 int gap = offset - bus_ofs;
2173 int n = min_t(int, size, map_bankwidth(map)-gap);
2174 map_word datum = map_word_ff(map);
2175
2176 datum = map_word_load_partial(map, datum, buf, gap, n);
2177 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002178 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002179 return ret;
2180
2181 offset += n;
2182 buf += n;
2183 size -= n;
2184 }
2185
2186 return 0;
2187}
2188
2189static int
2190do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2191 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2192{
2193 struct cfi_private *cfi = map->fldrv_priv;
2194 map_word datum;
2195
2196 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002197 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002198 return -EXDEV;
2199
2200 datum = map_word_ff(map);
2201 datum = map_word_clr(map, datum, CMD(1 << grpno));
2202 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2203}
2204
2205static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2206 size_t *retlen, u_char *buf,
2207 otp_op_t action, int user_regs)
2208{
2209 struct map_info *map = mtd->priv;
2210 struct cfi_private *cfi = map->fldrv_priv;
2211 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2212 struct flchip *chip;
2213 struct cfi_intelext_otpinfo *otp;
2214 u_long devsize, reg_prot_offset, data_offset;
2215 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2216 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2217 int ret;
2218
2219 *retlen = 0;
2220
2221 /* Check that we actually have some OTP registers */
2222 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2223 return -ENODATA;
2224
2225 /* we need real chips here not virtual ones */
2226 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2227 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002228 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002229
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002230 /* Some chips have OTP located in the _top_ partition only.
2231 For example: Intel 28F256L18T (T means top-parameter device) */
2232 if (cfi->mfr == MANUFACTURER_INTEL) {
2233 switch (cfi->id) {
2234 case 0x880b:
2235 case 0x880c:
2236 case 0x880d:
2237 chip_num = chip_step - 1;
2238 }
2239 }
2240
2241 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002242 chip = &cfi->chips[chip_num];
2243 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2244
2245 /* first OTP region */
2246 field = 0;
2247 reg_prot_offset = extp->ProtRegAddr;
2248 reg_fact_groups = 1;
2249 reg_fact_size = 1 << extp->FactProtRegSize;
2250 reg_user_groups = 1;
2251 reg_user_size = 1 << extp->UserProtRegSize;
2252
2253 while (len > 0) {
2254 /* flash geometry fixup */
2255 data_offset = reg_prot_offset + 1;
2256 data_offset *= cfi->interleave * cfi->device_type;
2257 reg_prot_offset *= cfi->interleave * cfi->device_type;
2258 reg_fact_size *= cfi->interleave;
2259 reg_user_size *= cfi->interleave;
2260
2261 if (user_regs) {
2262 groups = reg_user_groups;
2263 groupsize = reg_user_size;
2264 /* skip over factory reg area */
2265 groupno = reg_fact_groups;
2266 data_offset += reg_fact_groups * reg_fact_size;
2267 } else {
2268 groups = reg_fact_groups;
2269 groupsize = reg_fact_size;
2270 groupno = 0;
2271 }
2272
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002273 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002274 if (!action) {
2275 /*
2276 * Special case: if action is NULL
2277 * we fill buf with otp_info records.
2278 */
2279 struct otp_info *otpinfo;
2280 map_word lockword;
2281 len -= sizeof(struct otp_info);
2282 if (len <= 0)
2283 return -ENOSPC;
2284 ret = do_otp_read(map, chip,
2285 reg_prot_offset,
2286 (u_char *)&lockword,
2287 map_bankwidth(map),
2288 0, 0, 0);
2289 if (ret)
2290 return ret;
2291 otpinfo = (struct otp_info *)buf;
2292 otpinfo->start = from;
2293 otpinfo->length = groupsize;
2294 otpinfo->locked =
2295 !map_word_bitsset(map, lockword,
2296 CMD(1 << groupno));
2297 from += groupsize;
2298 buf += sizeof(*otpinfo);
2299 *retlen += sizeof(*otpinfo);
2300 } else if (from >= groupsize) {
2301 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002302 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002303 } else {
2304 int size = groupsize;
2305 data_offset += from;
2306 size -= from;
2307 from = 0;
2308 if (size > len)
2309 size = len;
2310 ret = action(map, chip, data_offset,
2311 buf, size, reg_prot_offset,
2312 groupno, groupsize);
2313 if (ret < 0)
2314 return ret;
2315 buf += size;
2316 len -= size;
2317 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002318 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002319 }
2320 groupno++;
2321 groups--;
2322 }
2323
2324 /* next OTP region */
2325 if (++field == extp->NumProtectionFields)
2326 break;
2327 reg_prot_offset = otp->ProtRegAddr;
2328 reg_fact_groups = otp->FactGroups;
2329 reg_fact_size = 1 << otp->FactProtRegSize;
2330 reg_user_groups = otp->UserGroups;
2331 reg_user_size = 1 << otp->UserProtRegSize;
2332 otp++;
2333 }
2334 }
2335
2336 return 0;
2337}
2338
2339static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2340 size_t len, size_t *retlen,
2341 u_char *buf)
2342{
2343 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2344 buf, do_otp_read, 0);
2345}
2346
2347static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2348 size_t len, size_t *retlen,
2349 u_char *buf)
2350{
2351 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2352 buf, do_otp_read, 1);
2353}
2354
2355static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2356 size_t len, size_t *retlen,
2357 u_char *buf)
2358{
2359 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2360 buf, do_otp_write, 1);
2361}
2362
2363static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2364 loff_t from, size_t len)
2365{
2366 size_t retlen;
2367 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2368 NULL, do_otp_lock, 1);
2369}
2370
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002371static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002372 struct otp_info *buf, size_t len)
2373{
2374 size_t retlen;
2375 int ret;
2376
2377 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2378 return ret ? : retlen;
2379}
2380
2381static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2382 struct otp_info *buf, size_t len)
2383{
2384 size_t retlen;
2385 int ret;
2386
2387 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2388 return ret ? : retlen;
2389}
2390
2391#endif
2392
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002393static void cfi_intelext_save_locks(struct mtd_info *mtd)
2394{
2395 struct mtd_erase_region_info *region;
2396 int block, status, i;
2397 unsigned long adr;
2398 size_t len;
2399
2400 for (i = 0; i < mtd->numeraseregions; i++) {
2401 region = &mtd->eraseregions[i];
2402 if (!region->lockmap)
2403 continue;
2404
2405 for (block = 0; block < region->numblocks; block++){
2406 len = region->erasesize;
2407 adr = region->offset + block * len;
2408
2409 status = cfi_varsize_frob(mtd,
Ben Dooks029a9eb2007-05-28 20:11:37 +01002410 do_getlockstatus_oneblock, adr, len, NULL);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002411 if (status)
2412 set_bit(block, region->lockmap);
2413 else
2414 clear_bit(block, region->lockmap);
2415 }
2416 }
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419static int cfi_intelext_suspend(struct mtd_info *mtd)
2420{
2421 struct map_info *map = mtd->priv;
2422 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002423 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int i;
2425 struct flchip *chip;
2426 int ret = 0;
2427
Justin Treone619a752008-01-30 10:25:49 -08002428 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002429 && extp && (extp->FeatureSupport & (1 << 5)))
2430 cfi_intelext_save_locks(mtd);
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 for (i=0; !ret && i<cfi->numchips; i++) {
2433 chip = &cfi->chips[i];
2434
2435 spin_lock(chip->mutex);
2436
2437 switch (chip->state) {
2438 case FL_READY:
2439 case FL_STATUS:
2440 case FL_CFI_QUERY:
2441 case FL_JEDEC_QUERY:
2442 if (chip->oldstate == FL_READY) {
David Andersa86aaa62006-10-19 19:33:19 +03002443 /* place the chip in a known state before suspend */
2444 map_write(map, CMD(0xFF), cfi->chips[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 chip->oldstate = chip->state;
2446 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002447 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 * as the whole point is that nobody can do anything
2449 * with the chip now anyway.
2450 */
2451 } else {
2452 /* There seems to be an operation pending. We must wait for it. */
2453 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2454 ret = -EAGAIN;
2455 }
2456 break;
2457 default:
2458 /* Should we actually wait? Once upon a time these routines weren't
2459 allowed to. Or should we return -EAGAIN, because the upper layers
2460 ought to have already shut down anything which was using the device
2461 anyway? The latter for now. */
2462 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2463 ret = -EAGAIN;
2464 case FL_PM_SUSPENDED:
2465 break;
2466 }
2467 spin_unlock(chip->mutex);
2468 }
2469
2470 /* Unlock the chips again */
2471
2472 if (ret) {
2473 for (i--; i >=0; i--) {
2474 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (chip->state == FL_PM_SUSPENDED) {
2479 /* No need to force it into a known state here,
2480 because we're returning failure, and it didn't
2481 get power cycled */
2482 chip->state = chip->oldstate;
2483 chip->oldstate = FL_READY;
2484 wake_up(&chip->wq);
2485 }
2486 spin_unlock(chip->mutex);
2487 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002488 }
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return ret;
2491}
2492
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002493static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2494{
2495 struct mtd_erase_region_info *region;
2496 int block, i;
2497 unsigned long adr;
2498 size_t len;
2499
2500 for (i = 0; i < mtd->numeraseregions; i++) {
2501 region = &mtd->eraseregions[i];
2502 if (!region->lockmap)
2503 continue;
2504
2505 for (block = 0; block < region->numblocks; block++) {
2506 len = region->erasesize;
2507 adr = region->offset + block * len;
2508
2509 if (!test_bit(block, region->lockmap))
2510 cfi_intelext_unlock(mtd, adr, len);
2511 }
2512 }
2513}
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515static void cfi_intelext_resume(struct mtd_info *mtd)
2516{
2517 struct map_info *map = mtd->priv;
2518 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002519 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 int i;
2521 struct flchip *chip;
2522
2523 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 chip = &cfi->chips[i];
2526
2527 spin_lock(chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /* Go to known state. Chip may have been power cycled */
2530 if (chip->state == FL_PM_SUSPENDED) {
2531 map_write(map, CMD(0xFF), cfi->chips[i].start);
2532 chip->oldstate = chip->state = FL_READY;
2533 wake_up(&chip->wq);
2534 }
2535
2536 spin_unlock(chip->mutex);
2537 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002538
Justin Treone619a752008-01-30 10:25:49 -08002539 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002540 && extp && (extp->FeatureSupport & (1 << 5)))
2541 cfi_intelext_restore_locks(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002544static int cfi_intelext_reset(struct mtd_info *mtd)
2545{
2546 struct map_info *map = mtd->priv;
2547 struct cfi_private *cfi = map->fldrv_priv;
2548 int i, ret;
2549
2550 for (i=0; i < cfi->numchips; i++) {
2551 struct flchip *chip = &cfi->chips[i];
2552
2553 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002554 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002555 flash is accessible for soft reboot. */
2556 spin_lock(chip->mutex);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002557 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002558 if (!ret) {
2559 map_write(map, CMD(0xff), chip->start);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002560 chip->state = FL_SHUTDOWN;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002561 }
2562 spin_unlock(chip->mutex);
2563 }
2564
2565 return 0;
2566}
2567
2568static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2569 void *v)
2570{
2571 struct mtd_info *mtd;
2572
2573 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2574 cfi_intelext_reset(mtd);
2575 return NOTIFY_DONE;
2576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static void cfi_intelext_destroy(struct mtd_info *mtd)
2579{
2580 struct map_info *map = mtd->priv;
2581 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002582 struct mtd_erase_region_info *region;
2583 int i;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002584 cfi_intelext_reset(mtd);
2585 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 kfree(cfi->cmdset_priv);
2587 kfree(cfi->cfiq);
2588 kfree(cfi->chips[0].priv);
2589 kfree(cfi);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002590 for (i = 0; i < mtd->numeraseregions; i++) {
2591 region = &mtd->eraseregions[i];
2592 if (region->lockmap)
2593 kfree(region->lockmap);
2594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 kfree(mtd->eraseregions);
2596}
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598MODULE_LICENSE("GPL");
2599MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2600MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002601MODULE_ALIAS("cfi_cmdset_0003");
2602MODULE_ALIAS("cfi_cmdset_0200");