blob: 44cbfc093ecc03f75a51b77fc37e295610dc82e6 [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 *
Nicolas Pitre2f82af02009-09-14 03:25:28 -04008 * 10/10/2000 Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * - 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mtd/cfi.h>
38
39/* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
40/* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
41
42// debugging, turns off buffer write mode if set to 1
43#define FORCE_WORD_WRITE 0
44
Hans-Christian Egtvedtb2ef1a22009-11-05 15:53:43 +010045/* Intel chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define I82802AB 0x00ad
47#define I82802AC 0x00ac
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -030048#define PF38F4476 0x881c
Hans-Christian Egtvedtb2ef1a22009-11-05 15:53:43 +010049/* STMicroelectronics chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define M50LPW080 0x002F
Nate Casedeb1a5f2008-05-13 14:45:29 -050051#define M50FLW080A 0x0080
52#define M50FLW080B 0x0081
Hans-Christian Egtvedt8dbaea42009-11-05 15:53:37 +010053/* Atmel chips */
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +010054#define AT49BV640D 0x02de
Hans-Christian Egtvedt8dbaea42009-11-05 15:53:37 +010055#define AT49BV640DT 0x02db
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
59static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
Nicolas Pitree102d542005-08-06 05:46:59 +010060static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
62static void cfi_intelext_sync (struct mtd_info *);
Adrian Hunter69423d92008-12-10 13:37:21 +000063static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
64static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
Richard Cochran99384242010-06-14 18:10:33 +020065static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
66 uint64_t len);
Todd Poynor8048d2f2005-03-31 00:57:33 +010067#ifdef CONFIG_MTD_OTP
Nicolas Pitref77814d2005-02-08 17:11:19 +000068static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
69static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
70static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
71static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
72static int cfi_intelext_get_fact_prot_info (struct mtd_info *,
73 struct otp_info *, size_t);
74static int cfi_intelext_get_user_prot_info (struct mtd_info *,
75 struct otp_info *, size_t);
Todd Poynor8048d2f2005-03-31 00:57:33 +010076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int cfi_intelext_suspend (struct mtd_info *);
78static void cfi_intelext_resume (struct mtd_info *);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +010079static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static void cfi_intelext_destroy(struct mtd_info *);
82
83struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
84
85static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
86static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
87
88static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
Jared Hulberta98889f2008-04-29 23:26:49 -070089 size_t *retlen, void **virt, resource_size_t *phys);
90static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Alexey Korolev5a37cf12007-10-22 17:55:20 +010092static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
94static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
95#include "fwh_lock.h"
96
97
98
99/*
100 * *********** SETUP AND PROBE BITS ***********
101 */
102
103static struct mtd_chip_driver cfi_intelext_chipdrv = {
104 .probe = NULL, /* Not usable directly */
105 .destroy = cfi_intelext_destroy,
106 .name = "cfi_cmdset_0001",
107 .module = THIS_MODULE
108};
109
110/* #define DEBUG_LOCK_BITS */
111/* #define DEBUG_CFI_FEATURES */
112
113#ifdef DEBUG_CFI_FEATURES
114static void cfi_tell_features(struct cfi_pri_intelext *extp)
115{
116 int i;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100117 printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
119 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
120 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
121 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
122 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
123 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
124 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
125 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
126 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
127 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
128 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100129 printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
130 for (i=11; i<32; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000131 if (extp->FeatureSupport & (1<<i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 printk(" - Unknown Bit %X: supported\n", i);
133 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
136 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
137 for (i=1; i<8; i++) {
138 if (extp->SuspendCmdSupport & (1<<i))
139 printk(" - Unknown Bit %X: supported\n", i);
140 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
143 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
Nicolas Pitre638d9832005-08-06 05:40:46 +0100144 printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
145 for (i=2; i<3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (extp->BlkStatusRegMask & (1<<i))
147 printk(" - Unknown Bit %X Active: yes\n",i);
148 }
Nicolas Pitre638d9832005-08-06 05:40:46 +0100149 printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
150 printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
151 for (i=6; i<16; i++) {
152 if (extp->BlkStatusRegMask & (1<<i))
153 printk(" - Unknown Bit %X Active: yes\n",i);
154 }
155
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000156 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
158 if (extp->VppOptimal)
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000159 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
161}
162#endif
163
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100164/* Atmel chips don't use the same PRI format as Intel chips */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100165static void fixup_convert_atmel_pri(struct mtd_info *mtd)
Hans-Christian Egtvedtd10a39d2007-10-30 16:33:07 +0100166{
167 struct map_info *map = mtd->priv;
168 struct cfi_private *cfi = map->fldrv_priv;
169 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
170 struct cfi_pri_atmel atmel_pri;
171 uint32_t features = 0;
172
173 /* Reverse byteswapping */
174 extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
175 extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
176 extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
177
178 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
179 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
180
181 printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
182
183 if (atmel_pri.Features & 0x01) /* chip erase supported */
184 features |= (1<<0);
185 if (atmel_pri.Features & 0x02) /* erase suspend supported */
186 features |= (1<<1);
187 if (atmel_pri.Features & 0x04) /* program suspend supported */
188 features |= (1<<2);
189 if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
190 features |= (1<<9);
191 if (atmel_pri.Features & 0x20) /* page mode read supported */
192 features |= (1<<7);
193 if (atmel_pri.Features & 0x40) /* queued erase supported */
194 features |= (1<<4);
195 if (atmel_pri.Features & 0x80) /* Protection bits supported */
196 features |= (1<<6);
197
198 extp->FeatureSupport = features;
199
200 /* burst write mode not supported */
201 cfi->cfiq->BufWriteTimeoutTyp = 0;
202 cfi->cfiq->BufWriteTimeoutMax = 0;
203}
204
Guillaume LECERFcc318222010-11-17 12:35:50 +0100205static void fixup_at49bv640dx_lock(struct mtd_info *mtd)
Hans-Christian Egtvedt8dbaea42009-11-05 15:53:37 +0100206{
207 struct map_info *map = mtd->priv;
208 struct cfi_private *cfi = map->fldrv_priv;
209 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
210
211 cfip->FeatureSupport |= (1 << 5);
212 mtd->flags |= MTD_POWERUP_LOCK;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000216/* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100217static void fixup_intel_strataflash(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct map_info *map = mtd->priv;
220 struct cfi_private *cfi = map->fldrv_priv;
Alexander Belyakov91949d62008-05-04 14:32:58 +0400221 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
224 "erase on write disabled.\n");
225 extp->SuspendCmdSupport &= ~1;
226}
227#endif
228
229#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
Guillaume LECERFcc318222010-11-17 12:35:50 +0100230static void fixup_no_write_suspend(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct map_info *map = mtd->priv;
233 struct cfi_private *cfi = map->fldrv_priv;
234 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
235
236 if (cfip && (cfip->FeatureSupport&4)) {
237 cfip->FeatureSupport &= ~4;
238 printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
239 }
240}
241#endif
242
Guillaume LECERFcc318222010-11-17 12:35:50 +0100243static void fixup_st_m28w320ct(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct map_info *map = mtd->priv;
246 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
249 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
250}
251
Guillaume LECERFcc318222010-11-17 12:35:50 +0100252static void fixup_st_m28w320cb(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct map_info *map = mtd->priv;
255 struct cfi_private *cfi = map->fldrv_priv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* Note this is done after the region info is endian swapped */
258 cfi->cfiq->EraseRegionInfo[1] =
259 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
260};
261
Guillaume LECERFcc318222010-11-17 12:35:50 +0100262static void fixup_use_point(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct map_info *map = mtd->priv;
265 if (!mtd->point && map_is_linear(map)) {
266 mtd->point = cfi_intelext_point;
267 mtd->unpoint = cfi_intelext_unpoint;
268 }
269}
270
Guillaume LECERFcc318222010-11-17 12:35:50 +0100271static void fixup_use_write_buffers(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct map_info *map = mtd->priv;
274 struct cfi_private *cfi = map->fldrv_priv;
275 if (cfi->cfiq->BufWriteTimeoutTyp) {
276 printk(KERN_INFO "Using buffer write method\n" );
277 mtd->write = cfi_intelext_write_buffers;
Nicolas Pitree102d542005-08-06 05:46:59 +0100278 mtd->writev = cfi_intelext_writev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280}
281
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800282/*
283 * Some chips power-up with all sectors locked by default.
284 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100285static void fixup_unlock_powerup_lock(struct mtd_info *mtd)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800286{
Justin Treone619a752008-01-30 10:25:49 -0800287 struct map_info *map = mtd->priv;
288 struct cfi_private *cfi = map->fldrv_priv;
289 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
290
291 if (cfip->FeatureSupport&32) {
292 printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
293 mtd->flags |= MTD_POWERUP_LOCK;
294 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static struct cfi_fixup cfi_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100298 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
299 { CFI_MFR_ATMEL, AT49BV640D, fixup_at49bv640dx_lock },
300 { CFI_MFR_ATMEL, AT49BV640DT, fixup_at49bv640dx_lock },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
Guillaume LECERFcc318222010-11-17 12:35:50 +0100302 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#endif
304#ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
Guillaume LECERFcc318222010-11-17 12:35:50 +0100305 { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif
307#if !FORCE_WORD_WRITE
Guillaume LECERFcc318222010-11-17 12:35:50 +0100308 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#endif
Guillaume LECERFcc318222010-11-17 12:35:50 +0100310 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct },
311 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb },
312 { CFI_MFR_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock },
313 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314};
315
316static struct cfi_fixup jedec_fixup_table[] = {
Guillaume LECERFcc318222010-11-17 12:35:50 +0100317 { CFI_MFR_INTEL, I82802AB, fixup_use_fwh_lock },
318 { CFI_MFR_INTEL, I82802AC, fixup_use_fwh_lock },
319 { CFI_MFR_ST, M50LPW080, fixup_use_fwh_lock },
320 { CFI_MFR_ST, M50FLW080A, fixup_use_fwh_lock },
321 { CFI_MFR_ST, M50FLW080B, fixup_use_fwh_lock },
322 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324static struct cfi_fixup fixup_table[] = {
325 /* The CFI vendor ids and the JEDEC vendor IDs appear
326 * to be common. It is like the devices id's are as
327 * well. This table is to pick all cases where
328 * we know that is the case.
329 */
Guillaume LECERFcc318222010-11-17 12:35:50 +0100330 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point },
331 { 0, 0, NULL }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332};
333
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300334static void cfi_fixup_major_minor(struct cfi_private *cfi,
335 struct cfi_pri_intelext *extp)
336{
Hans-Christian Egtvedtb2ef1a22009-11-05 15:53:43 +0100337 if (cfi->mfr == CFI_MFR_INTEL &&
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300338 cfi->id == PF38F4476 && extp->MinorVersion == '3')
339 extp->MinorVersion = '1';
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static inline struct cfi_pri_intelext *
343read_pri_intelext(struct map_info *map, __u16 adr)
344{
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300345 struct cfi_private *cfi = map->fldrv_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct cfi_pri_intelext *extp;
Daniel Ribeiroe1b158a2009-05-17 08:02:08 -0300347 unsigned int extra_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned int extp_size = sizeof(*extp);
349
350 again:
351 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
352 if (!extp)
353 return NULL;
354
Daniel Ribeiroec2d0d82009-05-17 08:02:17 -0300355 cfi_fixup_major_minor(cfi, extp);
356
Todd Poynord88f9772005-07-20 22:01:17 +0100357 if (extp->MajorVersion != '1' ||
Alexey Korolevb1c9c9b2007-11-23 09:31:56 +0000358 (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
Todd Poynord88f9772005-07-20 22:01:17 +0100359 printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
360 "version %c.%c.\n", extp->MajorVersion,
361 extp->MinorVersion);
362 kfree(extp);
363 return NULL;
364 }
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Do some byteswapping if necessary */
367 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
368 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
369 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
370
Daniel Ribeiroe1b158a2009-05-17 08:02:08 -0300371 if (extp->MinorVersion >= '0') {
372 extra_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000375 extra_size += (extp->NumProtectionFields - 1) *
376 sizeof(struct cfi_intelext_otpinfo);
Daniel Ribeiroe1b158a2009-05-17 08:02:08 -0300377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Daniel Ribeiroe1b158a2009-05-17 08:02:08 -0300379 if (extp->MinorVersion >= '1') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100381 extra_size += 2;
382 if (extp_size < sizeof(*extp) + extra_size)
383 goto need_more;
Daniel Ribeiroe1b158a2009-05-17 08:02:08 -0300384 extra_size += extp->extra[extra_size - 1];
385 }
386
387 if (extp->MinorVersion >= '3') {
388 int nb_parts, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* Number of hardware-partitions */
391 extra_size += 1;
392 if (extp_size < sizeof(*extp) + extra_size)
393 goto need_more;
394 nb_parts = extp->extra[extra_size - 1];
395
Nicolas Pitre638d9832005-08-06 05:40:46 +0100396 /* skip the sizeof(partregion) field in CFI 1.4 */
397 if (extp->MinorVersion >= '4')
398 extra_size += 2;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 for (i = 0; i < nb_parts; i++) {
401 struct cfi_intelext_regioninfo *rinfo;
402 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
403 extra_size += sizeof(*rinfo);
404 if (extp_size < sizeof(*extp) + extra_size)
405 goto need_more;
406 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
407 extra_size += (rinfo->NumBlockTypes - 1)
408 * sizeof(struct cfi_intelext_blockinfo);
409 }
410
Nicolas Pitre638d9832005-08-06 05:40:46 +0100411 if (extp->MinorVersion >= '4')
412 extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (extp_size < sizeof(*extp) + extra_size) {
415 need_more:
416 extp_size = sizeof(*extp) + extra_size;
417 kfree(extp);
418 if (extp_size > 4096) {
419 printk(KERN_ERR
420 "%s: cfi_pri_intelext is too fat\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700421 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return NULL;
423 }
424 goto again;
425 }
426 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return extp;
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
432{
433 struct cfi_private *cfi = map->fldrv_priv;
434 struct mtd_info *mtd;
435 int i;
436
Burman Yan95b93a02006-11-15 21:10:29 +0200437 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!mtd) {
439 printk(KERN_ERR "Failed to allocate memory for MTD device\n");
440 return NULL;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 mtd->priv = map;
443 mtd->type = MTD_NORFLASH;
444
445 /* Fill in the default mtd operations */
446 mtd->erase = cfi_intelext_erase_varsize;
447 mtd->read = cfi_intelext_read;
448 mtd->write = cfi_intelext_write_words;
449 mtd->sync = cfi_intelext_sync;
450 mtd->lock = cfi_intelext_lock;
451 mtd->unlock = cfi_intelext_unlock;
Richard Cochran99384242010-06-14 18:10:33 +0200452 mtd->is_locked = cfi_intelext_is_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 mtd->suspend = cfi_intelext_suspend;
454 mtd->resume = cfi_intelext_resume;
455 mtd->flags = MTD_CAP_NORFLASH;
456 mtd->name = map->name;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400457 mtd->writesize = 1;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100458
459 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (cfi->cfi_mode == CFI_MODE_CFI) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000462 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * It's a real CFI chip, not one for which the probe
464 * routine faked a CFI structure. So we read the feature
465 * table from it.
466 */
467 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
468 struct cfi_pri_intelext *extp;
469
470 extp = read_pri_intelext(map, adr);
471 if (!extp) {
472 kfree(mtd);
473 return NULL;
474 }
475
476 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000477 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 cfi_fixup(mtd, cfi_fixup_table);
480
481#ifdef DEBUG_CFI_FEATURES
482 /* Tell the user about it in lots of lovely detail */
483 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if(extp->SuspendCmdSupport & 1) {
487 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
488 }
489 }
490 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
491 /* Apply jedec specific fixups */
492 cfi_fixup(mtd, jedec_fixup_table);
493 }
494 /* Apply generic fixups */
495 cfi_fixup(mtd, fixup_table);
496
497 for (i=0; i< cfi->numchips; i++) {
David Woodhouse2a5bd592007-02-09 14:39:10 +0000498 if (cfi->cfiq->WordWriteTimeoutTyp)
499 cfi->chips[i].word_write_time =
500 1<<cfi->cfiq->WordWriteTimeoutTyp;
501 else
502 cfi->chips[i].word_write_time = 50000;
503
504 if (cfi->cfiq->BufWriteTimeoutTyp)
505 cfi->chips[i].buffer_write_time =
506 1<<cfi->cfiq->BufWriteTimeoutTyp;
507 /* No default; if it isn't specified, we won't use it */
508
509 if (cfi->cfiq->BlockEraseTimeoutTyp)
510 cfi->chips[i].erase_time =
511 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
512 else
513 cfi->chips[i].erase_time = 2000000;
514
Anders Grafströme93cafe2008-08-05 18:37:41 +0200515 if (cfi->cfiq->WordWriteTimeoutTyp &&
516 cfi->cfiq->WordWriteTimeoutMax)
517 cfi->chips[i].word_write_time_max =
518 1<<(cfi->cfiq->WordWriteTimeoutTyp +
519 cfi->cfiq->WordWriteTimeoutMax);
520 else
521 cfi->chips[i].word_write_time_max = 50000 * 8;
522
523 if (cfi->cfiq->BufWriteTimeoutTyp &&
524 cfi->cfiq->BufWriteTimeoutMax)
525 cfi->chips[i].buffer_write_time_max =
526 1<<(cfi->cfiq->BufWriteTimeoutTyp +
527 cfi->cfiq->BufWriteTimeoutMax);
528
529 if (cfi->cfiq->BlockEraseTimeoutTyp &&
530 cfi->cfiq->BlockEraseTimeoutMax)
531 cfi->chips[i].erase_time_max =
532 1000<<(cfi->cfiq->BlockEraseTimeoutTyp +
533 cfi->cfiq->BlockEraseTimeoutMax);
534 else
535 cfi->chips[i].erase_time_max = 2000000 * 8;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 cfi->chips[i].ref_point_counter = 0;
Simon Voglc314b6f2006-02-24 13:04:09 -0800538 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 map->fldrv = &cfi_intelext_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return cfi_intelext_setup(mtd);
544}
David Woodhousea15bdee2006-05-08 22:35:05 +0100545struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
546struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
547EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
548EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
549EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
552{
553 struct map_info *map = mtd->priv;
554 struct cfi_private *cfi = map->fldrv_priv;
555 unsigned long offset = 0;
556 int i,j;
557 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
558
559 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
560
561 mtd->size = devsize * cfi->numchips;
562
563 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000564 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000566 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
568 goto setup_err;
569 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
572 unsigned long ernum, ersize;
573 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
574 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
575
576 if (mtd->erasesize < ersize) {
577 mtd->erasesize = ersize;
578 }
579 for (j=0; j<cfi->numchips; j++) {
580 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
581 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
582 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800583 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 offset += (ersize * ernum);
586 }
587
588 if (offset != devsize) {
589 /* Argh */
590 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
591 goto setup_err;
592 }
593
594 for (i=0; i<mtd->numeraseregions;i++){
Adrian Hunter69423d92008-12-10 13:37:21 +0000595 printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n",
596 i,(unsigned long long)mtd->eraseregions[i].offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 mtd->eraseregions[i].erasesize,
598 mtd->eraseregions[i].numblocks);
599 }
600
Nicolas Pitref77814d2005-02-08 17:11:19 +0000601#ifdef CONFIG_MTD_OTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000603 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
604 mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
605 mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
606 mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
607 mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#endif
609
610 /* This function has the potential to distort the reality
611 a bit and therefore should be called last. */
612 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
613 goto setup_err;
614
615 __module_get(THIS_MODULE);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100616 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return mtd;
618
619 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100620 kfree(mtd->eraseregions);
621 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 kfree(cfi->cmdset_priv);
623 return NULL;
624}
625
626static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
627 struct cfi_private **pcfi)
628{
629 struct map_info *map = mtd->priv;
630 struct cfi_private *cfi = *pcfi;
631 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
632
633 /*
Jesper Juhl8f1a8662007-07-05 02:18:34 +0200634 * Probing of multi-partition flash chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
636 * To support multiple partitions when available, we simply arrange
637 * for each of them to have their own flchip structure even if they
638 * are on the same physical chip. This means completely recreating
639 * a new cfi_private structure right here which is a blatent code
640 * layering violation, but this is still the least intrusive
641 * arrangement at this point. This can be rearranged in the future
642 * if someone feels motivated enough. --nico
643 */
Nicolas Pitre638d9832005-08-06 05:40:46 +0100644 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 && extp->FeatureSupport & (1 << 9)) {
646 struct cfi_private *newcfi;
647 struct flchip *chip;
648 struct flchip_shared *shared;
649 int offs, numregions, numparts, partshift, numvirtchips, i, j;
650
651 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000652 offs = (extp->NumProtectionFields - 1) *
653 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100656 offs += extp->extra[offs+1]+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Number of partition regions */
659 numregions = extp->extra[offs];
660 offs += 1;
661
Nicolas Pitre638d9832005-08-06 05:40:46 +0100662 /* skip the sizeof(partregion) field in CFI 1.4 */
663 if (extp->MinorVersion >= '4')
664 offs += 2;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* Number of hardware partitions */
667 numparts = 0;
668 for (i = 0; i < numregions; i++) {
669 struct cfi_intelext_regioninfo *rinfo;
670 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
671 numparts += rinfo->NumIdentPartitions;
672 offs += sizeof(*rinfo)
673 + (rinfo->NumBlockTypes - 1) *
674 sizeof(struct cfi_intelext_blockinfo);
675 }
676
Thomas Kunzefe224662008-04-23 01:40:52 +0200677 if (!numparts)
678 numparts = 1;
679
Nicolas Pitre638d9832005-08-06 05:40:46 +0100680 /* Programming Region info */
681 if (extp->MinorVersion >= '4') {
682 struct cfi_intelext_programming_regioninfo *prinfo;
683 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
Joern Engel28318772006-05-22 23:18:05 +0200684 mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
Joern Engel5fa43392006-05-22 23:18:29 +0200685 mtd->flags &= ~MTD_BIT_WRITEABLE;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100686 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
Joern Engel28318772006-05-22 23:18:05 +0200687 map->name, mtd->writesize,
Artem Bityutskiyd4160852007-01-30 10:45:55 +0200688 cfi->interleave * prinfo->ControlValid,
689 cfi->interleave * prinfo->ControlInvalid);
Nicolas Pitre638d9832005-08-06 05:40:46 +0100690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * All functions below currently rely on all chips having
694 * the same geometry so we'll just assume that all hardware
695 * partitions are of the same size too.
696 */
697 partshift = cfi->chipshift - __ffs(numparts);
698
699 if ((1 << partshift) < mtd->erasesize) {
700 printk( KERN_ERR
701 "%s: bad number of hw partitions (%d)\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700702 __func__, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -EINVAL;
704 }
705
706 numvirtchips = cfi->numchips * numparts;
707 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
708 if (!newcfi)
709 return -ENOMEM;
710 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
711 if (!shared) {
712 kfree(newcfi);
713 return -ENOMEM;
714 }
715 memcpy(newcfi, cfi, sizeof(struct cfi_private));
716 newcfi->numchips = numvirtchips;
717 newcfi->chipshift = partshift;
718
719 chip = &newcfi->chips[0];
720 for (i = 0; i < cfi->numchips; i++) {
721 shared[i].writing = shared[i].erasing = NULL;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200722 mutex_init(&shared[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 for (j = 0; j < numparts; j++) {
724 *chip = cfi->chips[i];
725 chip->start += j << partshift;
726 chip->priv = &shared[i];
727 /* those should be reset too since
728 they create memory references. */
729 init_waitqueue_head(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200730 mutex_init(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 chip++;
732 }
733 }
734
735 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
736 "--> %d partitions of %d KiB\n",
737 map->name, cfi->numchips, cfi->interleave,
738 newcfi->numchips, 1<<(newcfi->chipshift-10));
739
740 map->fldrv_priv = newcfi;
741 *pcfi = newcfi;
742 kfree(cfi);
743 }
744
745 return 0;
746}
747
748/*
749 * *********** CHIP ACCESS FUNCTIONS ***********
750 */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100751static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 DECLARE_WAITQUEUE(wait, current);
754 struct cfi_private *cfi = map->fldrv_priv;
755 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100757 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400759 /* Prevent setting state FL_SYNCING for chip in suspended state. */
760 if (mode == FL_SYNCING && chip->oldstate != FL_READY)
761 goto sleep;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 switch (chip->state) {
764
765 case FL_STATUS:
766 for (;;) {
767 status = map_read(map, adr);
768 if (map_word_andequal(map, status, status_OK, status_OK))
769 break;
770
771 /* At this point we're fine with write operations
772 in other partitions as they don't conflict. */
773 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
774 break;
775
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200776 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200778 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* Someone else might have been playing with it. */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100780 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700782 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 case FL_READY:
784 case FL_CFI_QUERY:
785 case FL_JEDEC_QUERY:
786 return 0;
787
788 case FL_ERASING:
789 if (!cfip ||
790 !(cfip->FeatureSupport & 2) ||
791 !(mode == FL_READY || mode == FL_POINT ||
792 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
793 goto sleep;
794
795
796 /* Erase suspend */
797 map_write(map, CMD(0xB0), adr);
798
799 /* If the flash has finished erasing, then 'erase suspend'
800 * appears to make some (28F320) flash devices switch to
801 * 'read' mode. Make sure that we switch to 'read status'
802 * mode so we get the right data. --rmk
803 */
804 map_write(map, CMD(0x70), adr);
805 chip->oldstate = FL_ERASING;
806 chip->state = FL_ERASE_SUSPENDING;
807 chip->erase_suspended = 1;
808 for (;;) {
809 status = map_read(map, adr);
810 if (map_word_andequal(map, status, status_OK, status_OK))
811 break;
812
813 if (time_after(jiffies, timeo)) {
814 /* Urgh. Resume and pretend we weren't here. */
815 map_write(map, CMD(0xd0), adr);
816 /* Make sure we're in 'read status' mode if it had finished */
817 map_write(map, CMD(0x70), adr);
818 chip->state = FL_ERASING;
819 chip->oldstate = FL_READY;
Nicolas Pitre48436532005-08-06 05:16:52 +0100820 printk(KERN_ERR "%s: Chip not ready after erase "
821 "suspended: status = 0x%lx\n", map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -EIO;
823 }
824
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200825 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200827 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
829 So we can just loop here. */
830 }
831 chip->state = FL_STATUS;
832 return 0;
833
834 case FL_XIP_WHILE_ERASING:
835 if (mode != FL_READY && mode != FL_POINT &&
836 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
837 goto sleep;
838 chip->oldstate = chip->state;
839 chip->state = FL_READY;
840 return 0;
841
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700842 case FL_SHUTDOWN:
843 /* The machine is rebooting now,so no one can get chip anymore */
844 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 case FL_POINT:
846 /* Only if there's no operation suspended... */
847 if (mode == FL_READY && chip->oldstate == FL_READY)
848 return 0;
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700849 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 default:
851 sleep:
852 set_current_state(TASK_UNINTERRUPTIBLE);
853 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200854 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 schedule();
856 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200857 mutex_lock(&chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100858 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860}
861
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100862static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
863{
864 int ret;
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300865 DECLARE_WAITQUEUE(wait, current);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100866
867 retry:
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400868 if (chip->priv &&
869 (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE
870 || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) {
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100871 /*
872 * OK. We have possibility for contention on the write/erase
873 * operations which are global to the real chip and not per
874 * partition. So let's fight it over in the partition which
875 * currently has authority on the operation.
876 *
877 * The rules are as follows:
878 *
879 * - any write operation must own shared->writing.
880 *
881 * - any erase operation must own _both_ shared->writing and
882 * shared->erasing.
883 *
884 * - contention arbitration is handled in the owner's context.
885 *
886 * The 'shared' struct can be read and/or written only when
887 * its lock is taken.
888 */
889 struct flchip_shared *shared = chip->priv;
890 struct flchip *contender;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200891 mutex_lock(&shared->lock);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100892 contender = shared->writing;
893 if (contender && contender != chip) {
894 /*
895 * The engine to perform desired operation on this
896 * partition is already in use by someone else.
897 * Let's fight over it in the context of the chip
898 * currently using it. If it is possible to suspend,
899 * that other partition will do just that, otherwise
900 * it'll happily send us to sleep. In any case, when
901 * get_chip returns success we're clear to go ahead.
902 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200903 ret = mutex_trylock(&contender->mutex);
Stefani Seibold8ae66412010-08-05 09:19:26 +0200904 mutex_unlock(&shared->lock);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100905 if (!ret)
906 goto retry;
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200907 mutex_unlock(&chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100908 ret = chip_ready(map, contender, contender->start, mode);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200909 mutex_lock(&chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100910
911 if (ret == -EAGAIN) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200912 mutex_unlock(&contender->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100913 goto retry;
914 }
915 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200916 mutex_unlock(&contender->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100917 return ret;
918 }
Stefani Seibold8ae66412010-08-05 09:19:26 +0200919 mutex_lock(&shared->lock);
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400920
921 /* We should not own chip if it is already
922 * in FL_SYNCING state. Put contender and retry. */
923 if (chip->state == FL_SYNCING) {
924 put_chip(map, contender, contender->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200925 mutex_unlock(&contender->mutex);
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400926 goto retry;
927 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200928 mutex_unlock(&contender->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100929 }
930
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300931 /* Check if we already have suspended erase
932 * on this chip. Sleep. */
933 if (mode == FL_ERASING && shared->erasing
934 && shared->erasing->oldstate == FL_ERASING) {
Stefani Seibold8ae66412010-08-05 09:19:26 +0200935 mutex_unlock(&shared->lock);
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300936 set_current_state(TASK_UNINTERRUPTIBLE);
937 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200938 mutex_unlock(&chip->mutex);
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300939 schedule();
940 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200941 mutex_lock(&chip->mutex);
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300942 goto retry;
943 }
944
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100945 /* We now own it */
946 shared->writing = chip;
947 if (mode == FL_ERASING)
948 shared->erasing = chip;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200949 mutex_unlock(&shared->lock);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100950 }
951 ret = chip_ready(map, chip, adr, mode);
952 if (ret == -EAGAIN)
953 goto retry;
954
955 return ret;
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
959{
960 struct cfi_private *cfi = map->fldrv_priv;
961
962 if (chip->priv) {
963 struct flchip_shared *shared = chip->priv;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200964 mutex_lock(&shared->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (shared->writing == chip && chip->oldstate == FL_READY) {
966 /* We own the ability to write, but we're done */
967 shared->writing = shared->erasing;
968 if (shared->writing && shared->writing != chip) {
969 /* give back ownership to who we loaned it from */
970 struct flchip *loaner = shared->writing;
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200971 mutex_lock(&loaner->mutex);
Stefani Seibold8ae66412010-08-05 09:19:26 +0200972 mutex_unlock(&shared->lock);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200973 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 put_chip(map, loaner, loaner->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200975 mutex_lock(&chip->mutex);
976 mutex_unlock(&loaner->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 wake_up(&chip->wq);
978 return;
979 }
980 shared->erasing = NULL;
981 shared->writing = NULL;
982 } else if (shared->erasing == chip && shared->writing != chip) {
983 /*
984 * We own the ability to erase without the ability
985 * to write, which means the erase was suspended
986 * and some other partition is currently writing.
987 * Don't let the switch below mess things up since
988 * we don't have ownership to resume anything.
989 */
Stefani Seibold8ae66412010-08-05 09:19:26 +0200990 mutex_unlock(&shared->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 wake_up(&chip->wq);
992 return;
993 }
Stefani Seibold8ae66412010-08-05 09:19:26 +0200994 mutex_unlock(&shared->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 switch(chip->oldstate) {
998 case FL_ERASING:
999 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001000 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001002 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 errors to be returned from do_erase_oneblock because
1004 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001005 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001007 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 do. */
1009 map_write(map, CMD(0xd0), adr);
1010 map_write(map, CMD(0x70), adr);
1011 chip->oldstate = FL_READY;
1012 chip->state = FL_ERASING;
1013 break;
1014
1015 case FL_XIP_WHILE_ERASING:
1016 chip->state = chip->oldstate;
1017 chip->oldstate = FL_READY;
1018 break;
1019
1020 case FL_READY:
1021 case FL_STATUS:
1022 case FL_JEDEC_QUERY:
1023 /* We should really make set_vpp() count, rather than doing this */
1024 DISABLE_VPP(map);
1025 break;
1026 default:
Nicolas Pitre48436532005-08-06 05:16:52 +01001027 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029 wake_up(&chip->wq);
1030}
1031
1032#ifdef CONFIG_MTD_XIP
1033
1034/*
1035 * No interrupt what so ever can be serviced while the flash isn't in array
1036 * mode. This is ensured by the xip_disable() and xip_enable() functions
1037 * enclosing any code path where the flash is known not to be in array mode.
1038 * And within a XIP disabled code path, only functions marked with __xipram
1039 * may be called and nothing else (it's a good thing to inspect generated
1040 * assembly to make sure inline functions were actually inlined and that gcc
1041 * didn't emit calls to its own support functions). Also configuring MTD CFI
1042 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044
1045static void xip_disable(struct map_info *map, struct flchip *chip,
1046 unsigned long adr)
1047{
1048 /* TODO: chips with no XIP use should ignore and return */
1049 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 local_irq_disable();
1051}
1052
1053static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
1054 unsigned long adr)
1055{
1056 struct cfi_private *cfi = map->fldrv_priv;
1057 if (chip->state != FL_POINT && chip->state != FL_READY) {
1058 map_write(map, CMD(0xff), adr);
1059 chip->state = FL_READY;
1060 }
1061 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +02001062 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/*
1067 * When a delay is required for the flash operation to complete, the
Nicolas Pitrec1724712006-03-30 15:52:41 +01001068 * xip_wait_for_operation() function is polling for both the given timeout
1069 * and pending (but still masked) hardware interrupts. Whenever there is an
1070 * interrupt pending then the flash erase or write operation is suspended,
1071 * array mode restored and interrupts unmasked. Task scheduling might also
1072 * happen at that point. The CPU eventually returns from the interrupt or
1073 * the call to schedule() and the suspended flash operation is resumed for
1074 * the remaining of the delay period.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
1076 * Warning: this function _will_ fool interrupt latency tracing tools.
1077 */
1078
Nicolas Pitrec1724712006-03-30 15:52:41 +01001079static int __xipram xip_wait_for_operation(
1080 struct map_info *map, struct flchip *chip,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001081 unsigned long adr, unsigned int chip_op_time_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct cfi_private *cfi = map->fldrv_priv;
1084 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
1085 map_word status, OK = CMD(0x80);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001086 unsigned long usec, suspended, start, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 flstate_t oldstate, newstate;
1088
Nicolas Pitrec1724712006-03-30 15:52:41 +01001089 start = xip_currtime();
Anders Grafströme93cafe2008-08-05 18:37:41 +02001090 usec = chip_op_time_max;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001091 if (usec == 0)
1092 usec = 500000;
1093 done = 0;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 do {
1096 cpu_relax();
1097 if (xip_irqpending() && cfip &&
1098 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1099 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1100 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1101 /*
1102 * Let's suspend the erase or write operation when
1103 * supported. Note that we currently don't try to
1104 * suspend interleaved chips if there is already
1105 * another operation suspended (imagine what happens
1106 * when one chip was already done with the current
1107 * operation while another chip suspended it, then
1108 * we resume the whole thing at once). Yes, it
1109 * can happen!
1110 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001111 usec -= done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 map_write(map, CMD(0xb0), adr);
1113 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 suspended = xip_currtime();
1115 do {
1116 if (xip_elapsed_since(suspended) > 100000) {
1117 /*
1118 * The chip doesn't want to suspend
1119 * after waiting for 100 msecs.
1120 * This is a critical error but there
1121 * is not much we can do here.
1122 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001123 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 status = map_read(map, adr);
1126 } while (!map_word_andequal(map, status, OK, OK));
1127
1128 /* Suspend succeeded */
1129 oldstate = chip->state;
1130 if (oldstate == FL_ERASING) {
1131 if (!map_word_bitsset(map, status, CMD(0x40)))
1132 break;
1133 newstate = FL_XIP_WHILE_ERASING;
1134 chip->erase_suspended = 1;
1135 } else {
1136 if (!map_word_bitsset(map, status, CMD(0x04)))
1137 break;
1138 newstate = FL_XIP_WHILE_WRITING;
1139 chip->write_suspended = 1;
1140 }
1141 chip->state = newstate;
1142 map_write(map, CMD(0xff), adr);
1143 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001144 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001146 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001147 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 cond_resched();
1149
1150 /*
1151 * We're back. However someone else might have
1152 * decided to go write to the chip if we are in
1153 * a suspended erase state. If so let's wait
1154 * until it's done.
1155 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001156 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 while (chip->state != newstate) {
1158 DECLARE_WAITQUEUE(wait, current);
1159 set_current_state(TASK_UNINTERRUPTIBLE);
1160 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001161 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 schedule();
1163 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001164 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 /* Disallow XIP again */
1167 local_irq_disable();
1168
1169 /* Resume the write or erase operation */
1170 map_write(map, CMD(0xd0), adr);
1171 map_write(map, CMD(0x70), adr);
1172 chip->state = oldstate;
1173 start = xip_currtime();
1174 } else if (usec >= 1000000/HZ) {
1175 /*
1176 * Try to save on CPU power when waiting delay
1177 * is at least a system timer tick period.
1178 * No need to be extremely accurate here.
1179 */
1180 xip_cpu_idle();
1181 }
1182 status = map_read(map, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001183 done = xip_elapsed_since(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 } while (!map_word_andequal(map, status, OK, OK)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001185 && done < usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Nicolas Pitrec1724712006-03-30 15:52:41 +01001187 return (done >= usec) ? -ETIME : 0;
1188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190/*
1191 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1192 * the flash is actively programming or erasing since we have to poll for
1193 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001194 * a XIP setup so do it before the actual flash operation in this case
Nicolas Pitrec1724712006-03-30 15:52:41 +01001195 * and stub it out from INVAL_CACHE_AND_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001197#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1198 INVALIDATE_CACHED_RANGE(map, from, size)
1199
Anders Grafströme93cafe2008-08-05 18:37:41 +02001200#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \
1201 xip_wait_for_operation(map, chip, cmd_adr, usec_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203#else
1204
1205#define xip_disable(map, chip, adr)
1206#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207#define XIP_INVAL_CACHED_RANGE(x...)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001208#define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Nicolas Pitrec1724712006-03-30 15:52:41 +01001210static int inval_cache_and_wait_for_operation(
1211 struct map_info *map, struct flchip *chip,
1212 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001213 unsigned int chip_op_time, unsigned int chip_op_time_max)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001214{
1215 struct cfi_private *cfi = map->fldrv_priv;
1216 map_word status, status_OK = CMD(0x80);
Alexey Korolev46a16522006-06-28 19:22:07 +01001217 int chip_state = chip->state;
Alexey Korolev998453f2008-07-16 15:28:56 +01001218 unsigned int timeo, sleep_time, reset_timeo;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001219
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001220 mutex_unlock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001221 if (inval_len)
1222 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001223 mutex_lock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001224
Anders Grafströme93cafe2008-08-05 18:37:41 +02001225 timeo = chip_op_time_max;
Alexey Korolev46a16522006-06-28 19:22:07 +01001226 if (!timeo)
1227 timeo = 500000;
Alexey Korolev998453f2008-07-16 15:28:56 +01001228 reset_timeo = timeo;
Alexey Korolev46a16522006-06-28 19:22:07 +01001229 sleep_time = chip_op_time / 2;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001230
Nicolas Pitrec1724712006-03-30 15:52:41 +01001231 for (;;) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001232 status = map_read(map, cmd_adr);
1233 if (map_word_andequal(map, status, status_OK, status_OK))
1234 break;
1235
Alexey Korolev46a16522006-06-28 19:22:07 +01001236 if (!timeo) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001237 map_write(map, CMD(0x70), cmd_adr);
1238 chip->state = FL_STATUS;
1239 return -ETIME;
1240 }
1241
Alexey Korolev46a16522006-06-28 19:22:07 +01001242 /* OK Still waiting. Drop the lock, wait a while and retry. */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001243 mutex_unlock(&chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001244 if (sleep_time >= 1000000/HZ) {
1245 /*
1246 * Half of the normal delay still remaining
1247 * can be performed with a sleeping delay instead
1248 * of busy waiting.
1249 */
1250 msleep(sleep_time/1000);
1251 timeo -= sleep_time;
1252 sleep_time = 1000000/HZ;
1253 } else {
1254 udelay(1);
1255 cond_resched();
1256 timeo--;
1257 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001258 mutex_lock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001259
Joakim Tjernlund967bf622006-11-28 23:11:52 +00001260 while (chip->state != chip_state) {
Alexey Korolev46a16522006-06-28 19:22:07 +01001261 /* Someone's suspended the operation: sleep */
1262 DECLARE_WAITQUEUE(wait, current);
1263 set_current_state(TASK_UNINTERRUPTIBLE);
1264 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001265 mutex_unlock(&chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001266 schedule();
1267 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001268 mutex_lock(&chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001269 }
Graff Yang6ac15e92009-03-02 16:31:29 +08001270 if (chip->erase_suspended && chip_state == FL_ERASING) {
1271 /* Erase suspend occured while sleep: reset timeout */
Alexey Korolev998453f2008-07-16 15:28:56 +01001272 timeo = reset_timeo;
1273 chip->erase_suspended = 0;
Graff Yang6ac15e92009-03-02 16:31:29 +08001274 }
1275 if (chip->write_suspended && chip_state == FL_WRITING) {
1276 /* Write suspend occured while sleep: reset timeout */
1277 timeo = reset_timeo;
Alexey Korolev998453f2008-07-16 15:28:56 +01001278 chip->write_suspended = 0;
1279 }
Alexey Korolev46a16522006-06-28 19:22:07 +01001280 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001281
1282 /* Done and happy. */
1283 chip->state = FL_STATUS;
1284 return 0;
1285}
Nicolas Pitre6da70122005-05-19 18:05:47 +01001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#endif
1288
Anders Grafströme93cafe2008-08-05 18:37:41 +02001289#define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \
1290 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001291
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1294{
1295 unsigned long cmd_addr;
1296 struct cfi_private *cfi = map->fldrv_priv;
1297 int ret = 0;
1298
1299 adr += chip->start;
1300
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001301 /* Ensure cmd read/writes are aligned. */
1302 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001304 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1307
1308 if (!ret) {
1309 if (chip->state != FL_POINT && chip->state != FL_READY)
1310 map_write(map, CMD(0xff), cmd_addr);
1311
1312 chip->state = FL_POINT;
1313 chip->ref_point_counter++;
1314 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001315 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 return ret;
1318}
1319
Jared Hulberta98889f2008-04-29 23:26:49 -07001320static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
1321 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct map_info *map = mtd->priv;
1324 struct cfi_private *cfi = map->fldrv_priv;
Andy Lowe097f2572007-01-12 18:05:10 -05001325 unsigned long ofs, last_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int chipnum;
1327 int ret = 0;
1328
1329 if (!map->virt || (from + len > mtd->size))
1330 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* Now lock the chip(s) to POINT state */
1333
1334 /* ofs: offset within the first chip that the first read should start */
1335 chipnum = (from >> cfi->chipshift);
1336 ofs = from - (chipnum << cfi->chipshift);
1337
Jared Hulberta98889f2008-04-29 23:26:49 -07001338 *virt = map->virt + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001339 *retlen = 0;
Jared Hulberta98889f2008-04-29 23:26:49 -07001340 if (phys)
1341 *phys = map->phys + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 while (len) {
1344 unsigned long thislen;
1345
1346 if (chipnum >= cfi->numchips)
1347 break;
1348
Andy Lowe097f2572007-01-12 18:05:10 -05001349 /* We cannot point across chips that are virtually disjoint */
1350 if (!last_end)
1351 last_end = cfi->chips[chipnum].start;
1352 else if (cfi->chips[chipnum].start != last_end)
1353 break;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if ((len + ofs -1) >> cfi->chipshift)
1356 thislen = (1<<cfi->chipshift) - ofs;
1357 else
1358 thislen = len;
1359
1360 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1361 if (ret)
1362 break;
1363
1364 *retlen += thislen;
1365 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ofs = 0;
Andy Lowe097f2572007-01-12 18:05:10 -05001368 last_end += 1 << cfi->chipshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 chipnum++;
1370 }
1371 return 0;
1372}
1373
Jared Hulberta98889f2008-04-29 23:26:49 -07001374static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct map_info *map = mtd->priv;
1377 struct cfi_private *cfi = map->fldrv_priv;
1378 unsigned long ofs;
1379 int chipnum;
1380
1381 /* Now unlock the chip(s) POINT state */
1382
1383 /* ofs: offset within the first chip that the first read should start */
1384 chipnum = (from >> cfi->chipshift);
1385 ofs = from - (chipnum << cfi->chipshift);
1386
1387 while (len) {
1388 unsigned long thislen;
1389 struct flchip *chip;
1390
1391 chip = &cfi->chips[chipnum];
1392 if (chipnum >= cfi->numchips)
1393 break;
1394
1395 if ((len + ofs -1) >> cfi->chipshift)
1396 thislen = (1<<cfi->chipshift) - ofs;
1397 else
1398 thislen = len;
1399
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001400 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (chip->state == FL_POINT) {
1402 chip->ref_point_counter--;
1403 if(chip->ref_point_counter == 0)
1404 chip->state = FL_READY;
1405 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001406 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 -07001407
1408 put_chip(map, chip, chip->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001409 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 len -= thislen;
1412 ofs = 0;
1413 chipnum++;
1414 }
1415}
1416
1417static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1418{
1419 unsigned long cmd_addr;
1420 struct cfi_private *cfi = map->fldrv_priv;
1421 int ret;
1422
1423 adr += chip->start;
1424
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001425 /* Ensure cmd read/writes are aligned. */
1426 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001428 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ret = get_chip(map, chip, cmd_addr, FL_READY);
1430 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001431 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return ret;
1433 }
1434
1435 if (chip->state != FL_POINT && chip->state != FL_READY) {
1436 map_write(map, CMD(0xff), cmd_addr);
1437
1438 chip->state = FL_READY;
1439 }
1440
1441 map_copy_from(map, buf, adr, len);
1442
1443 put_chip(map, chip, cmd_addr);
1444
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001445 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447}
1448
1449static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1450{
1451 struct map_info *map = mtd->priv;
1452 struct cfi_private *cfi = map->fldrv_priv;
1453 unsigned long ofs;
1454 int chipnum;
1455 int ret = 0;
1456
1457 /* ofs: offset within the first chip that the first read should start */
1458 chipnum = (from >> cfi->chipshift);
1459 ofs = from - (chipnum << cfi->chipshift);
1460
1461 *retlen = 0;
1462
1463 while (len) {
1464 unsigned long thislen;
1465
1466 if (chipnum >= cfi->numchips)
1467 break;
1468
1469 if ((len + ofs -1) >> cfi->chipshift)
1470 thislen = (1<<cfi->chipshift) - ofs;
1471 else
1472 thislen = len;
1473
1474 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1475 if (ret)
1476 break;
1477
1478 *retlen += thislen;
1479 len -= thislen;
1480 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 ofs = 0;
1483 chipnum++;
1484 }
1485 return ret;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001489 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001492 map_word status, write_cmd;
1493 int ret=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 adr += chip->start;
1496
Nicolas Pitref77814d2005-02-08 17:11:19 +00001497 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001498 case FL_WRITING:
Guillaume LECERFb5d194c2010-10-26 10:55:29 +01001499 write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0x40) : CMD(0x41);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001500 break;
1501 case FL_OTP_WRITE:
1502 write_cmd = CMD(0xc0);
1503 break;
1504 default:
1505 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001508 mutex_lock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001509 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001511 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return ret;
1513 }
1514
1515 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1516 ENABLE_VPP(map);
1517 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001518 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001520 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Nicolas Pitrec1724712006-03-30 15:52:41 +01001522 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1523 adr, map_bankwidth(map),
Anders Grafströme93cafe2008-08-05 18:37:41 +02001524 chip->word_write_time,
1525 chip->word_write_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001526 if (ret) {
1527 xip_enable(map, chip, adr);
1528 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1529 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Nicolas Pitre48436532005-08-06 05:16:52 +01001532 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001533 status = map_read(map, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001534 if (map_word_bitsset(map, status, CMD(0x1a))) {
1535 unsigned long chipstatus = MERGESTATUS(status);
1536
1537 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001540 xip_enable(map, chip, adr);
1541
1542 if (chipstatus & 0x02) {
1543 ret = -EROFS;
1544 } else if (chipstatus & 0x08) {
1545 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1546 ret = -EIO;
1547 } else {
1548 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1549 ret = -EINVAL;
1550 }
1551
1552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
1554
1555 xip_enable(map, chip, adr);
1556 out: put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001557 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return ret;
1559}
1560
1561
1562static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1563{
1564 struct map_info *map = mtd->priv;
1565 struct cfi_private *cfi = map->fldrv_priv;
1566 int ret = 0;
1567 int chipnum;
1568 unsigned long ofs;
1569
1570 *retlen = 0;
1571 if (!len)
1572 return 0;
1573
1574 chipnum = to >> cfi->chipshift;
1575 ofs = to - (chipnum << cfi->chipshift);
1576
1577 /* If it's not bus-aligned, do the first byte write */
1578 if (ofs & (map_bankwidth(map)-1)) {
1579 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1580 int gap = ofs - bus_ofs;
1581 int n;
1582 map_word datum;
1583
1584 n = min_t(int, len, map_bankwidth(map)-gap);
1585 datum = map_word_ff(map);
1586 datum = map_word_load_partial(map, datum, buf, gap, n);
1587
1588 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001589 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001590 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return ret;
1592
1593 len -= n;
1594 ofs += n;
1595 buf += n;
1596 (*retlen) += n;
1597
1598 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001599 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ofs = 0;
1601 if (chipnum == cfi->numchips)
1602 return 0;
1603 }
1604 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 while(len >= map_bankwidth(map)) {
1607 map_word datum = map_word_load(map, buf);
1608
1609 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001610 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (ret)
1612 return ret;
1613
1614 ofs += map_bankwidth(map);
1615 buf += map_bankwidth(map);
1616 (*retlen) += map_bankwidth(map);
1617 len -= map_bankwidth(map);
1618
1619 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001620 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 ofs = 0;
1622 if (chipnum == cfi->numchips)
1623 return 0;
1624 }
1625 }
1626
1627 if (len & (map_bankwidth(map)-1)) {
1628 map_word datum;
1629
1630 datum = map_word_ff(map);
1631 datum = map_word_load_partial(map, datum, buf, 0, len);
1632
1633 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001634 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001635 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 (*retlen) += len;
1639 }
1640
1641 return 0;
1642}
1643
1644
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001645static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001646 unsigned long adr, const struct kvec **pvec,
1647 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001650 map_word status, write_cmd, datum;
1651 unsigned long cmd_adr;
1652 int ret, wbufsize, word_gap, words;
Nicolas Pitree102d542005-08-06 05:46:59 +01001653 const struct kvec *vec;
1654 unsigned long vec_seek;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001655 unsigned long initial_adr;
1656 int initial_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1659 adr += chip->start;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001660 initial_adr = adr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* Let's determine this according to the interleave only once */
Guillaume LECERFb5d194c2010-10-26 10:55:29 +01001664 write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001666 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1668 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001669 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return ret;
1671 }
1672
Massimo Cirillo646fd122008-01-11 10:24:11 +00001673 XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ENABLE_VPP(map);
1675 xip_disable(map, chip, cmd_adr);
1676
David Woodhouse151e7652006-05-14 01:51:54 +01001677 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001678 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 So we must check here and reset those bits if they're set. Otherwise
1680 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001681 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001683 chip->state = FL_STATUS;
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 status = map_read(map, cmd_adr);
1686 if (map_word_bitsset(map, status, CMD(0x30))) {
1687 xip_enable(map, chip, cmd_adr);
1688 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1689 xip_disable(map, chip, cmd_adr);
1690 map_write(map, CMD(0x50), cmd_adr);
1691 map_write(map, CMD(0x70), cmd_adr);
1692 }
1693
1694 chip->state = FL_WRITING_TO_BUFFER;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001695 map_write(map, write_cmd, cmd_adr);
Anders Grafströme93cafe2008-08-05 18:37:41 +02001696 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001697 if (ret) {
1698 /* Argh. Not ready for write to buffer */
1699 map_word Xstatus = map_read(map, cmd_adr);
1700 map_write(map, CMD(0x70), cmd_adr);
1701 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 status = map_read(map, cmd_adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001703 map_write(map, CMD(0x50), cmd_adr);
1704 map_write(map, CMD(0x70), cmd_adr);
1705 xip_enable(map, chip, cmd_adr);
1706 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1707 map->name, Xstatus.x[0], status.x[0]);
1708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Nicolas Pitree102d542005-08-06 05:46:59 +01001711 /* Figure out the number of words to write */
1712 word_gap = (-adr & (map_bankwidth(map)-1));
Julia Lawallc8872b02008-08-02 17:14:21 +02001713 words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map));
Nicolas Pitree102d542005-08-06 05:46:59 +01001714 if (!word_gap) {
1715 words--;
1716 } else {
1717 word_gap = map_bankwidth(map) - word_gap;
1718 adr -= word_gap;
1719 datum = map_word_ff(map);
1720 }
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001723 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001726 vec = *pvec;
1727 vec_seek = *pvec_seek;
1728 do {
1729 int n = map_bankwidth(map) - word_gap;
1730 if (n > vec->iov_len - vec_seek)
1731 n = vec->iov_len - vec_seek;
1732 if (n > len)
1733 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Nicolas Pitree102d542005-08-06 05:46:59 +01001735 if (!word_gap && len < map_bankwidth(map))
1736 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Nicolas Pitree102d542005-08-06 05:46:59 +01001738 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001739 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001740 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Nicolas Pitree102d542005-08-06 05:46:59 +01001742 len -= n;
1743 word_gap += n;
1744 if (!len || word_gap == map_bankwidth(map)) {
1745 map_write(map, datum, adr);
1746 adr += map_bankwidth(map);
1747 word_gap = 0;
1748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Nicolas Pitree102d542005-08-06 05:46:59 +01001750 vec_seek += n;
1751 if (vec_seek == vec->iov_len) {
1752 vec++;
1753 vec_seek = 0;
1754 }
1755 } while (len);
1756 *pvec = vec;
1757 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /* GO GO GO */
1760 map_write(map, CMD(0xd0), cmd_adr);
1761 chip->state = FL_WRITING;
1762
Nicolas Pitrec1724712006-03-30 15:52:41 +01001763 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
Massimo Cirillo646fd122008-01-11 10:24:11 +00001764 initial_adr, initial_len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001765 chip->buffer_write_time,
1766 chip->buffer_write_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001767 if (ret) {
1768 map_write(map, CMD(0x70), cmd_adr);
1769 chip->state = FL_STATUS;
1770 xip_enable(map, chip, cmd_adr);
1771 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1772 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Nicolas Pitre48436532005-08-06 05:16:52 +01001775 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001776 status = map_read(map, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001777 if (map_word_bitsset(map, status, CMD(0x1a))) {
1778 unsigned long chipstatus = MERGESTATUS(status);
1779
1780 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001782 map_write(map, CMD(0x70), cmd_adr);
1783 xip_enable(map, chip, cmd_adr);
1784
1785 if (chipstatus & 0x02) {
1786 ret = -EROFS;
1787 } else if (chipstatus & 0x08) {
1788 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1789 ret = -EIO;
1790 } else {
1791 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1792 ret = -EINVAL;
1793 }
1794
1795 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 xip_enable(map, chip, cmd_adr);
1799 out: put_chip(map, chip, cmd_adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001800 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return ret;
1802}
1803
Nicolas Pitree102d542005-08-06 05:46:59 +01001804static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1805 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct map_info *map = mtd->priv;
1808 struct cfi_private *cfi = map->fldrv_priv;
1809 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1810 int ret = 0;
1811 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001812 unsigned long ofs, vec_seek, i;
1813 size_t len = 0;
1814
1815 for (i = 0; i < count; i++)
1816 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 *retlen = 0;
1819 if (!len)
1820 return 0;
1821
1822 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001823 ofs = to - (chipnum << cfi->chipshift);
1824 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Nicolas Pitree102d542005-08-06 05:46:59 +01001826 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* We must not cross write block boundaries */
1828 int size = wbufsize - (ofs & (wbufsize-1));
1829
1830 if (size > len)
1831 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001832 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001833 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (ret)
1835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 (*retlen) += size;
1839 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 if (ofs >> cfi->chipshift) {
1842 chipnum ++;
1843 ofs = 0;
1844 if (chipnum == cfi->numchips)
1845 return 0;
1846 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001847
1848 /* Be nice and reschedule with the chip in a usable state for other
1849 processes. */
1850 cond_resched();
1851
Nicolas Pitree102d542005-08-06 05:46:59 +01001852 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return 0;
1855}
1856
Nicolas Pitree102d542005-08-06 05:46:59 +01001857static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1858 size_t len, size_t *retlen, const u_char *buf)
1859{
1860 struct kvec vec;
1861
1862 vec.iov_base = (void *) buf;
1863 vec.iov_len = len;
1864
1865 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1869 unsigned long adr, int len, void *thunk)
1870{
1871 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001872 map_word status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 int retries = 3;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001874 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 adr += chip->start;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001879 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 ret = get_chip(map, chip, adr, FL_ERASING);
1881 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001882 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return ret;
1884 }
1885
1886 XIP_INVAL_CACHED_RANGE(map, adr, len);
1887 ENABLE_VPP(map);
1888 xip_disable(map, chip, adr);
1889
1890 /* Clear the status register first */
1891 map_write(map, CMD(0x50), adr);
1892
1893 /* Now erase */
1894 map_write(map, CMD(0x20), adr);
1895 map_write(map, CMD(0xD0), adr);
1896 chip->state = FL_ERASING;
1897 chip->erase_suspended = 0;
1898
Nicolas Pitrec1724712006-03-30 15:52:41 +01001899 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1900 adr, len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001901 chip->erase_time,
1902 chip->erase_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001903 if (ret) {
1904 map_write(map, CMD(0x70), adr);
1905 chip->state = FL_STATUS;
1906 xip_enable(map, chip, adr);
1907 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
1911 /* We've broken this before. It doesn't hurt to be safe */
1912 map_write(map, CMD(0x70), adr);
1913 chip->state = FL_STATUS;
1914 status = map_read(map, adr);
1915
Nicolas Pitre48436532005-08-06 05:16:52 +01001916 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001918 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* Reset the error bits */
1921 map_write(map, CMD(0x50), adr);
1922 map_write(map, CMD(0x70), adr);
1923 xip_enable(map, chip, adr);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001926 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1927 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 } else if (chipstatus & 0x02) {
1929 /* Protection bit set */
1930 ret = -EROFS;
1931 } else if (chipstatus & 0x8) {
1932 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001933 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001935 } else if (chipstatus & 0x20 && retries--) {
1936 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
Nicolas Pitre48436532005-08-06 05:16:52 +01001937 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001938 mutex_unlock(&chip->mutex);
Nicolas Pitre48436532005-08-06 05:16:52 +01001939 goto retry;
1940 } else {
1941 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 -07001942 ret = -EIO;
1943 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001944
1945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947
Nicolas Pitre48436532005-08-06 05:16:52 +01001948 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 out: put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001950 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return ret;
1952}
1953
Ben Dooks029a9eb2007-05-28 20:11:37 +01001954static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 unsigned long ofs, len;
1957 int ret;
1958
1959 ofs = instr->addr;
1960 len = instr->len;
1961
1962 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1963 if (ret)
1964 return ret;
1965
1966 instr->state = MTD_ERASE_DONE;
1967 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return 0;
1970}
1971
1972static void cfi_intelext_sync (struct mtd_info *mtd)
1973{
1974 struct map_info *map = mtd->priv;
1975 struct cfi_private *cfi = map->fldrv_priv;
1976 int i;
1977 struct flchip *chip;
1978 int ret = 0;
1979
1980 for (i=0; !ret && i<cfi->numchips; i++) {
1981 chip = &cfi->chips[i];
1982
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001983 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1985
1986 if (!ret) {
1987 chip->oldstate = chip->state;
1988 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001989 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 * as the whole point is that nobody can do anything
1991 * with the chip now anyway.
1992 */
1993 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001994 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996
1997 /* Unlock the chips again */
1998
1999 for (i--; i >=0; i--) {
2000 chip = &cfi->chips[i];
2001
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002002 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (chip->state == FL_SYNCING) {
2005 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00002006 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 wake_up(&chip->wq);
2008 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002009 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011}
2012
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002013static int __xipram do_getlockstatus_oneblock(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct flchip *chip,
2015 unsigned long adr,
2016 int len, void *thunk)
2017{
2018 struct cfi_private *cfi = map->fldrv_priv;
2019 int status, ofs_factor = cfi->interleave * cfi->device_type;
2020
Todd Poynorc25bb1f2005-04-27 21:01:52 +01002021 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01002023 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 chip->state = FL_JEDEC_QUERY;
2025 status = cfi_read_query(map, adr+(2*ofs_factor));
2026 xip_enable(map, chip, 0);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002027 return status;
2028}
2029
2030#ifdef DEBUG_LOCK_BITS
2031static int __xipram do_printlockstatus_oneblock(struct map_info *map,
2032 struct flchip *chip,
2033 unsigned long adr,
2034 int len, void *thunk)
2035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002037 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039}
2040#endif
2041
2042#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
2043#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
2044
2045static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
2046 unsigned long adr, int len, void *thunk)
2047{
2048 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002049 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01002050 int udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int ret;
2052
2053 adr += chip->start;
2054
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002055 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ret = get_chip(map, chip, adr, FL_LOCKING);
2057 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002058 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return ret;
2060 }
2061
2062 ENABLE_VPP(map);
2063 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 map_write(map, CMD(0x60), adr);
2066 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2067 map_write(map, CMD(0x01), adr);
2068 chip->state = FL_LOCKING;
2069 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2070 map_write(map, CMD(0xD0), adr);
2071 chip->state = FL_UNLOCKING;
2072 } else
2073 BUG();
2074
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002075 /*
2076 * If Instant Individual Block Locking supported then no need
2077 * to delay.
2078 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01002079 udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002080
Anders Grafströme93cafe2008-08-05 18:37:41 +02002081 ret = WAIT_TIMEOUT(map, chip, adr, udelay, udelay * 100);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002082 if (ret) {
2083 map_write(map, CMD(0x70), adr);
2084 chip->state = FL_STATUS;
2085 xip_enable(map, chip, adr);
2086 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
2087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 xip_enable(map, chip, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002091out: put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002092 mutex_unlock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002093 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Adrian Hunter69423d92008-12-10 13:37:21 +00002096static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 int ret;
2099
2100#ifdef DEBUG_LOCK_BITS
2101 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002102 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002104 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105#endif
2106
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002107 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110#ifdef DEBUG_LOCK_BITS
2111 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002112 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002114 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115#endif
2116
2117 return ret;
2118}
2119
Adrian Hunter69423d92008-12-10 13:37:21 +00002120static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 int ret;
2123
2124#ifdef DEBUG_LOCK_BITS
2125 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002126 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002128 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129#endif
2130
2131 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2132 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134#ifdef DEBUG_LOCK_BITS
2135 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002136 __func__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002137 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002138 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return ret;
2142}
2143
Richard Cochran99384242010-06-14 18:10:33 +02002144static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
2145 uint64_t len)
2146{
2147 return cfi_varsize_frob(mtd, do_getlockstatus_oneblock,
2148 ofs, len, NULL) ? 1 : 0;
2149}
2150
Nicolas Pitref77814d2005-02-08 17:11:19 +00002151#ifdef CONFIG_MTD_OTP
2152
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002153typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002154 u_long data_offset, u_char *buf, u_int size,
2155 u_long prot_offset, u_int groupno, u_int groupsize);
2156
2157static int __xipram
2158do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2159 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2160{
2161 struct cfi_private *cfi = map->fldrv_priv;
2162 int ret;
2163
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002164 mutex_lock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002165 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2166 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002167 mutex_unlock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002168 return ret;
2169 }
2170
2171 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002172 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002173
2174 xip_disable(map, chip, chip->start);
2175 if (chip->state != FL_JEDEC_QUERY) {
2176 map_write(map, CMD(0x90), chip->start);
2177 chip->state = FL_JEDEC_QUERY;
2178 }
2179 map_copy_from(map, buf, chip->start + offset, size);
2180 xip_enable(map, chip, chip->start);
2181
2182 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002183 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002184
2185 put_chip(map, chip, chip->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002186 mutex_unlock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002187 return 0;
2188}
2189
2190static int
2191do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2192 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2193{
2194 int ret;
2195
2196 while (size) {
2197 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2198 int gap = offset - bus_ofs;
2199 int n = min_t(int, size, map_bankwidth(map)-gap);
2200 map_word datum = map_word_ff(map);
2201
2202 datum = map_word_load_partial(map, datum, buf, gap, n);
2203 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002204 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002205 return ret;
2206
2207 offset += n;
2208 buf += n;
2209 size -= n;
2210 }
2211
2212 return 0;
2213}
2214
2215static int
2216do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2217 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2218{
2219 struct cfi_private *cfi = map->fldrv_priv;
2220 map_word datum;
2221
2222 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002223 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002224 return -EXDEV;
2225
2226 datum = map_word_ff(map);
2227 datum = map_word_clr(map, datum, CMD(1 << grpno));
2228 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2229}
2230
2231static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2232 size_t *retlen, u_char *buf,
2233 otp_op_t action, int user_regs)
2234{
2235 struct map_info *map = mtd->priv;
2236 struct cfi_private *cfi = map->fldrv_priv;
2237 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2238 struct flchip *chip;
2239 struct cfi_intelext_otpinfo *otp;
2240 u_long devsize, reg_prot_offset, data_offset;
2241 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2242 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2243 int ret;
2244
2245 *retlen = 0;
2246
2247 /* Check that we actually have some OTP registers */
2248 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2249 return -ENODATA;
2250
2251 /* we need real chips here not virtual ones */
2252 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2253 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002254 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002255
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002256 /* Some chips have OTP located in the _top_ partition only.
2257 For example: Intel 28F256L18T (T means top-parameter device) */
Hans-Christian Egtvedtb2ef1a22009-11-05 15:53:43 +01002258 if (cfi->mfr == CFI_MFR_INTEL) {
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002259 switch (cfi->id) {
2260 case 0x880b:
2261 case 0x880c:
2262 case 0x880d:
2263 chip_num = chip_step - 1;
2264 }
2265 }
2266
2267 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002268 chip = &cfi->chips[chip_num];
2269 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2270
2271 /* first OTP region */
2272 field = 0;
2273 reg_prot_offset = extp->ProtRegAddr;
2274 reg_fact_groups = 1;
2275 reg_fact_size = 1 << extp->FactProtRegSize;
2276 reg_user_groups = 1;
2277 reg_user_size = 1 << extp->UserProtRegSize;
2278
2279 while (len > 0) {
2280 /* flash geometry fixup */
2281 data_offset = reg_prot_offset + 1;
2282 data_offset *= cfi->interleave * cfi->device_type;
2283 reg_prot_offset *= cfi->interleave * cfi->device_type;
2284 reg_fact_size *= cfi->interleave;
2285 reg_user_size *= cfi->interleave;
2286
2287 if (user_regs) {
2288 groups = reg_user_groups;
2289 groupsize = reg_user_size;
2290 /* skip over factory reg area */
2291 groupno = reg_fact_groups;
2292 data_offset += reg_fact_groups * reg_fact_size;
2293 } else {
2294 groups = reg_fact_groups;
2295 groupsize = reg_fact_size;
2296 groupno = 0;
2297 }
2298
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002299 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002300 if (!action) {
2301 /*
2302 * Special case: if action is NULL
2303 * we fill buf with otp_info records.
2304 */
2305 struct otp_info *otpinfo;
2306 map_word lockword;
2307 len -= sizeof(struct otp_info);
2308 if (len <= 0)
2309 return -ENOSPC;
2310 ret = do_otp_read(map, chip,
2311 reg_prot_offset,
2312 (u_char *)&lockword,
2313 map_bankwidth(map),
2314 0, 0, 0);
2315 if (ret)
2316 return ret;
2317 otpinfo = (struct otp_info *)buf;
2318 otpinfo->start = from;
2319 otpinfo->length = groupsize;
2320 otpinfo->locked =
2321 !map_word_bitsset(map, lockword,
2322 CMD(1 << groupno));
2323 from += groupsize;
2324 buf += sizeof(*otpinfo);
2325 *retlen += sizeof(*otpinfo);
2326 } else if (from >= groupsize) {
2327 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002328 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002329 } else {
2330 int size = groupsize;
2331 data_offset += from;
2332 size -= from;
2333 from = 0;
2334 if (size > len)
2335 size = len;
2336 ret = action(map, chip, data_offset,
2337 buf, size, reg_prot_offset,
2338 groupno, groupsize);
2339 if (ret < 0)
2340 return ret;
2341 buf += size;
2342 len -= size;
2343 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002344 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002345 }
2346 groupno++;
2347 groups--;
2348 }
2349
2350 /* next OTP region */
2351 if (++field == extp->NumProtectionFields)
2352 break;
2353 reg_prot_offset = otp->ProtRegAddr;
2354 reg_fact_groups = otp->FactGroups;
2355 reg_fact_size = 1 << otp->FactProtRegSize;
2356 reg_user_groups = otp->UserGroups;
2357 reg_user_size = 1 << otp->UserProtRegSize;
2358 otp++;
2359 }
2360 }
2361
2362 return 0;
2363}
2364
2365static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2366 size_t len, size_t *retlen,
2367 u_char *buf)
2368{
2369 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2370 buf, do_otp_read, 0);
2371}
2372
2373static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2374 size_t len, size_t *retlen,
2375 u_char *buf)
2376{
2377 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2378 buf, do_otp_read, 1);
2379}
2380
2381static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2382 size_t len, size_t *retlen,
2383 u_char *buf)
2384{
2385 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2386 buf, do_otp_write, 1);
2387}
2388
2389static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2390 loff_t from, size_t len)
2391{
2392 size_t retlen;
2393 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2394 NULL, do_otp_lock, 1);
2395}
2396
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002397static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002398 struct otp_info *buf, size_t len)
2399{
2400 size_t retlen;
2401 int ret;
2402
2403 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2404 return ret ? : retlen;
2405}
2406
2407static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2408 struct otp_info *buf, size_t len)
2409{
2410 size_t retlen;
2411 int ret;
2412
2413 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2414 return ret ? : retlen;
2415}
2416
2417#endif
2418
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002419static void cfi_intelext_save_locks(struct mtd_info *mtd)
2420{
2421 struct mtd_erase_region_info *region;
2422 int block, status, i;
2423 unsigned long adr;
2424 size_t len;
2425
2426 for (i = 0; i < mtd->numeraseregions; i++) {
2427 region = &mtd->eraseregions[i];
2428 if (!region->lockmap)
2429 continue;
2430
2431 for (block = 0; block < region->numblocks; block++){
2432 len = region->erasesize;
2433 adr = region->offset + block * len;
2434
2435 status = cfi_varsize_frob(mtd,
Ben Dooks029a9eb2007-05-28 20:11:37 +01002436 do_getlockstatus_oneblock, adr, len, NULL);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002437 if (status)
2438 set_bit(block, region->lockmap);
2439 else
2440 clear_bit(block, region->lockmap);
2441 }
2442 }
2443}
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445static int cfi_intelext_suspend(struct mtd_info *mtd)
2446{
2447 struct map_info *map = mtd->priv;
2448 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002449 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int i;
2451 struct flchip *chip;
2452 int ret = 0;
2453
Justin Treone619a752008-01-30 10:25:49 -08002454 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002455 && extp && (extp->FeatureSupport & (1 << 5)))
2456 cfi_intelext_save_locks(mtd);
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 for (i=0; !ret && i<cfi->numchips; i++) {
2459 chip = &cfi->chips[i];
2460
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002461 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 switch (chip->state) {
2464 case FL_READY:
2465 case FL_STATUS:
2466 case FL_CFI_QUERY:
2467 case FL_JEDEC_QUERY:
2468 if (chip->oldstate == FL_READY) {
David Andersa86aaa62006-10-19 19:33:19 +03002469 /* place the chip in a known state before suspend */
2470 map_write(map, CMD(0xFF), cfi->chips[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 chip->oldstate = chip->state;
2472 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002473 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 * as the whole point is that nobody can do anything
2475 * with the chip now anyway.
2476 */
2477 } else {
2478 /* There seems to be an operation pending. We must wait for it. */
2479 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2480 ret = -EAGAIN;
2481 }
2482 break;
2483 default:
2484 /* Should we actually wait? Once upon a time these routines weren't
2485 allowed to. Or should we return -EAGAIN, because the upper layers
2486 ought to have already shut down anything which was using the device
2487 anyway? The latter for now. */
2488 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2489 ret = -EAGAIN;
2490 case FL_PM_SUSPENDED:
2491 break;
2492 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002493 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495
2496 /* Unlock the chips again */
2497
2498 if (ret) {
2499 for (i--; i >=0; i--) {
2500 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002501
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002502 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (chip->state == FL_PM_SUSPENDED) {
2505 /* No need to force it into a known state here,
2506 because we're returning failure, and it didn't
2507 get power cycled */
2508 chip->state = chip->oldstate;
2509 chip->oldstate = FL_READY;
2510 wake_up(&chip->wq);
2511 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002512 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002514 }
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return ret;
2517}
2518
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002519static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2520{
2521 struct mtd_erase_region_info *region;
2522 int block, i;
2523 unsigned long adr;
2524 size_t len;
2525
2526 for (i = 0; i < mtd->numeraseregions; i++) {
2527 region = &mtd->eraseregions[i];
2528 if (!region->lockmap)
2529 continue;
2530
2531 for (block = 0; block < region->numblocks; block++) {
2532 len = region->erasesize;
2533 adr = region->offset + block * len;
2534
2535 if (!test_bit(block, region->lockmap))
2536 cfi_intelext_unlock(mtd, adr, len);
2537 }
2538 }
2539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541static void cfi_intelext_resume(struct mtd_info *mtd)
2542{
2543 struct map_info *map = mtd->priv;
2544 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002545 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 int i;
2547 struct flchip *chip;
2548
2549 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 chip = &cfi->chips[i];
2552
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002553 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /* Go to known state. Chip may have been power cycled */
2556 if (chip->state == FL_PM_SUSPENDED) {
2557 map_write(map, CMD(0xFF), cfi->chips[i].start);
2558 chip->oldstate = chip->state = FL_READY;
2559 wake_up(&chip->wq);
2560 }
2561
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002562 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002564
Justin Treone619a752008-01-30 10:25:49 -08002565 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002566 && extp && (extp->FeatureSupport & (1 << 5)))
2567 cfi_intelext_restore_locks(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002570static int cfi_intelext_reset(struct mtd_info *mtd)
2571{
2572 struct map_info *map = mtd->priv;
2573 struct cfi_private *cfi = map->fldrv_priv;
2574 int i, ret;
2575
2576 for (i=0; i < cfi->numchips; i++) {
2577 struct flchip *chip = &cfi->chips[i];
2578
2579 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002580 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002581 flash is accessible for soft reboot. */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002582 mutex_lock(&chip->mutex);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002583 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002584 if (!ret) {
2585 map_write(map, CMD(0xff), chip->start);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002586 chip->state = FL_SHUTDOWN;
Nicolas Pitrec9f7ec32009-10-23 16:02:42 -04002587 put_chip(map, chip, chip->start);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002588 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002589 mutex_unlock(&chip->mutex);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002590 }
2591
2592 return 0;
2593}
2594
2595static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2596 void *v)
2597{
2598 struct mtd_info *mtd;
2599
2600 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2601 cfi_intelext_reset(mtd);
2602 return NOTIFY_DONE;
2603}
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605static void cfi_intelext_destroy(struct mtd_info *mtd)
2606{
2607 struct map_info *map = mtd->priv;
2608 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002609 struct mtd_erase_region_info *region;
2610 int i;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002611 cfi_intelext_reset(mtd);
2612 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 kfree(cfi->cmdset_priv);
2614 kfree(cfi->cfiq);
2615 kfree(cfi->chips[0].priv);
2616 kfree(cfi);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002617 for (i = 0; i < mtd->numeraseregions; i++) {
2618 region = &mtd->eraseregions[i];
2619 if (region->lockmap)
2620 kfree(region->lockmap);
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 kfree(mtd->eraseregions);
2623}
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625MODULE_LICENSE("GPL");
2626MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2627MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002628MODULE_ALIAS("cfi_cmdset_0003");
2629MODULE_ALIAS("cfi_cmdset_0200");